File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.374: download - view: text, annotated - select for diffs
Wed Dec 23 21:39:23 2020 UTC (3 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6914. Add missing call to &wafproxy_titles(), and some readability
  changes to web form used to configure WAF/reverse proxy settings.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.374 2020/12/23 21:39:23 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);
  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:                       print => \&print_login,
  302:                       modify => \&modify_login,
  303:                     },
  304:         'defaults' => 
  305:                     { text => 'Default authentication/language/timezone/portal/types',
  306:                       help => 'Domain_Configuration_LangTZAuth',
  307:                       header => [{col1 => 'Setting',
  308:                                   col2 => 'Value'},
  309:                                  {col1 => 'Institutional user types',
  310:                                   col2 => 'Name displayed'}],
  311:                       print => \&print_defaults,
  312:                       modify => \&modify_defaults,
  313:                     },
  314:         'wafproxy' =>  
  315:                     { text => 'Web Application Firewall/Reverse Proxy',  
  316:                       help => 'Domain_Configuration_WAF_Proxy',
  317:                       header => [{col1 => 'Domain server',
  318:                                   col2 => 'Alias for WAF/Reverse Proxy',
  319:                                  },
  320:                                  {col1 => 'Setting',
  321:                                   col2 => 'Value',}],
  322:                       print => \&print_wafproxy,
  323:                       modify => \&modify_wafproxy, 
  324:                     },
  325:         'passwords' =>
  326:                     { text => 'Passwords (Internal authentication)',
  327:                       help => 'Domain_Configuration_Passwords',
  328:                       header => [{col1 => 'Resetting Forgotten Password',
  329:                                   col2 => 'Settings'},
  330:                                  {col1 => 'Encryption of Stored Passwords (Internal Auth)',
  331:                                   col2 => 'Settings'},
  332:                                  {col1 => 'Rules for LON-CAPA Passwords',
  333:                                   col2 => 'Settings'},
  334:                                  {col1 => 'Course Owner Changing Student Passwords',
  335:                                   col2 => 'Settings'}],
  336:                       print => \&print_passwords,
  337:                       modify => \&modify_passwords,
  338:                     },
  339:         'quotas' => 
  340:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  341:                       help => 'Domain_Configuration_Quotas',
  342:                       header => [{col1 => 'User affiliation',
  343:                                   col2 => 'Available tools',
  344:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  345:                       print => \&print_quotas,
  346:                       modify => \&modify_quotas,
  347:                     },
  348:         'autoenroll' =>
  349:                    { text => 'Auto-enrollment settings',
  350:                      help => 'Domain_Configuration_Auto_Enrollment',
  351:                      header => [{col1 => 'Configuration setting',
  352:                                  col2 => 'Value(s)'}],
  353:                      print => \&print_autoenroll,
  354:                      modify => \&modify_autoenroll,
  355:                    },
  356:         'autoupdate' => 
  357:                    { text => 'Auto-update settings',
  358:                      help => 'Domain_Configuration_Auto_Updates',
  359:                      header => [{col1 => 'Setting',
  360:                                  col2 => 'Value',},
  361:                                 {col1 => 'Setting',
  362:                                  col2 => 'Affiliation'},
  363:                                 {col1 => 'User population',
  364:                                  col2 => 'Updatable user data'}],
  365:                      print => \&print_autoupdate,
  366:                      modify => \&modify_autoupdate,
  367:                   },
  368:         'autocreate' => 
  369:                   { text => 'Auto-course creation settings',
  370:                      help => 'Domain_Configuration_Auto_Creation',
  371:                      header => [{col1 => 'Configuration Setting',
  372:                                  col2 => 'Value',}],
  373:                      print => \&print_autocreate,
  374:                      modify => \&modify_autocreate,
  375:                   },
  376:         'directorysrch' => 
  377:                   { text => 'Directory searches',
  378:                     help => 'Domain_Configuration_InstDirectory_Search',
  379:                     header => [{col1 => 'Institutional Directory Setting',
  380:                                 col2 => 'Value',},
  381:                                {col1 => 'LON-CAPA Directory Setting',
  382:                                 col2 => 'Value',}],
  383:                     print => \&print_directorysrch,
  384:                     modify => \&modify_directorysrch,
  385:                   },
  386:         'contacts' =>
  387:                   { text => 'E-mail addresses and helpform',
  388:                     help => 'Domain_Configuration_Contact_Info',
  389:                     header => [{col1 => 'Default e-mail addresses',
  390:                                 col2 => 'Value',},
  391:                                {col1 => 'Recipient(s) for notifications',
  392:                                 col2 => 'Value',},
  393:                                {col1 => 'Nightly status check e-mail',
  394:                                 col2 => 'Settings',},
  395:                                {col1 => 'Ask helpdesk form settings',
  396:                                 col2 => 'Value',},],
  397:                     print => \&print_contacts,
  398:                     modify => \&modify_contacts,
  399:                   },
  400:         'usercreation' => 
  401:                   { text => 'User creation',
  402:                     help => 'Domain_Configuration_User_Creation',
  403:                     header => [{col1 => 'Format rule type',
  404:                                 col2 => 'Format rules in force'},
  405:                                {col1 => 'User account creation',
  406:                                 col2 => 'Usernames which may be created',},
  407:                                {col1 => 'Context',
  408:                                 col2 => 'Assignable authentication types'}],
  409:                     print => \&print_usercreation,
  410:                     modify => \&modify_usercreation,
  411:                   },
  412:         'selfcreation' => 
  413:                   { text => 'Users self-creating accounts',
  414:                     help => 'Domain_Configuration_Self_Creation', 
  415:                     header => [{col1 => 'Self-creation with institutional username',
  416:                                 col2 => 'Enabled?'},
  417:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  418:                                 col2 => 'Information user can enter'},
  419:                                {col1 => 'Self-creation with e-mail verification',
  420:                                 col2 => 'Settings'}],
  421:                     print => \&print_selfcreation,
  422:                     modify => \&modify_selfcreation,
  423:                   },
  424:         'usermodification' =>
  425:                   { text => 'User modification',
  426:                     help => 'Domain_Configuration_User_Modification',
  427:                     header => [{col1 => 'Target user has role',
  428:                                 col2 => 'User information updatable in author context'},
  429:                                {col1 => 'Target user has role',
  430:                                 col2 => 'User information updatable in course context'}],
  431:                     print => \&print_usermodification,
  432:                     modify => \&modify_usermodification,
  433:                   },
  434:         'scantron' =>
  435:                   { text => 'Bubblesheet format',
  436:                     help => 'Domain_Configuration_Scantron_Format',
  437:                     header => [ {col1 => 'Bubblesheet format file',
  438:                                  col2 => ''},
  439:                                 {col1 => 'Bubblesheet data upload formats',
  440:                                  col2 => 'Settings'}],
  441:                     print => \&print_scantron,
  442:                     modify => \&modify_scantron,
  443:                   },
  444:         'requestcourses' => 
  445:                  {text => 'Request creation of courses',
  446:                   help => 'Domain_Configuration_Request_Courses',
  447:                   header => [{col1 => 'User affiliation',
  448:                               col2 => 'Availability/Processing of requests',},
  449:                              {col1 => 'Setting',
  450:                               col2 => 'Value'},
  451:                              {col1 => 'Available textbooks',
  452:                               col2 => ''},
  453:                              {col1 => 'Available templates',
  454:                               col2 => ''},
  455:                              {col1 => 'Validation (not official courses)',
  456:                               col2 => 'Value'},],
  457:                   print => \&print_quotas,
  458:                   modify => \&modify_quotas,
  459:                  },
  460:         'requestauthor' =>
  461:                  {text => 'Request Authoring Space',
  462:                   help => 'Domain_Configuration_Request_Author',
  463:                   header => [{col1 => 'User affiliation',
  464:                               col2 => 'Availability/Processing of requests',},
  465:                              {col1 => 'Setting',
  466:                               col2 => 'Value'}],
  467:                   print => \&print_quotas,
  468:                   modify => \&modify_quotas,
  469:                  },
  470:         'coursecategories' =>
  471:                   { text => 'Cataloging of courses/communities',
  472:                     help => 'Domain_Configuration_Cataloging_Courses',
  473:                     header => [{col1 => 'Catalog type/availability',
  474:                                 col2 => '',},
  475:                                {col1 => 'Category settings for standard catalog',
  476:                                 col2 => '',},
  477:                                {col1 => 'Categories',
  478:                                 col2 => '',
  479:                                }],
  480:                     print => \&print_coursecategories,
  481:                     modify => \&modify_coursecategories,
  482:                   },
  483:         'serverstatuses' =>
  484:                  {text   => 'Access to server status pages',
  485:                   help   => 'Domain_Configuration_Server_Status',
  486:                   header => [{col1 => 'Status Page',
  487:                               col2 => 'Other named users',
  488:                               col3 => 'Specific IPs',
  489:                             }],
  490:                   print => \&print_serverstatuses,
  491:                   modify => \&modify_serverstatuses,
  492:                  },
  493:         'helpsettings' =>
  494:                  {text   => 'Support settings',
  495:                   help   => 'Domain_Configuration_Help_Settings',
  496:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  497:                               col2 => 'Value'},
  498:                              {col1 => 'Helpdesk Roles',
  499:                               col2 => 'Settings'},],
  500:                   print  => \&print_helpsettings,
  501:                   modify => \&modify_helpsettings,
  502:                  },
  503:         'coursedefaults' => 
  504:                  {text => 'Course/Community defaults',
  505:                   help => 'Domain_Configuration_Course_Defaults',
  506:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  507:                               col2 => 'Value',},
  508:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  509:                               col2 => 'Value',},],
  510:                   print => \&print_coursedefaults,
  511:                   modify => \&modify_coursedefaults,
  512:                  },
  513:         'selfenrollment' => 
  514:                  {text   => 'Self-enrollment in Course/Community',
  515:                   help   => 'Domain_Configuration_Selfenrollment',
  516:                   header => [{col1 => 'Configuration Rights',
  517:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  518:                              {col1 => 'Defaults',
  519:                               col2 => 'Value'},
  520:                              {col1 => 'Self-enrollment validation (optional)',
  521:                               col2 => 'Value'},],
  522:                   print => \&print_selfenrollment,
  523:                   modify => \&modify_selfenrollment,
  524:                  },
  525:         'privacy' => 
  526:                  {text   => 'Availability of User Information',
  527:                   help   => 'Domain_Configuration_User_Privacy',
  528:                   header => [{col1 => 'Role assigned in different domain',
  529:                               col2 => 'Approval options'},
  530:                              {col1 => 'Role assigned in different domain to user of type',
  531:                               col2 => 'User information available in that domain'},
  532:                              {col1 => "Role assigned in user's domain",
  533:                               col2 => 'Information viewable by privileged user'},
  534:                              {col1 => "Role assigned in user's domain",
  535:                               col2 => 'Information viewable by unprivileged user'}],
  536:                   print => \&print_privacy,
  537:                   modify => \&modify_privacy,
  538:                  },
  539:         'usersessions' =>
  540:                  {text  => 'User session hosting/offloading',
  541:                   help  => 'Domain_Configuration_User_Sessions',
  542:                   header => [{col1 => 'Domain server',
  543:                               col2 => 'Servers to offload sessions to when busy'},
  544:                              {col1 => 'Hosting of users from other domains',
  545:                               col2 => 'Rules'},
  546:                              {col1 => "Hosting domain's own users elsewhere",
  547:                               col2 => 'Rules'}],
  548:                   print => \&print_usersessions,
  549:                   modify => \&modify_usersessions,
  550:                  },
  551:         'loadbalancing' =>
  552:                  {text  => 'Dedicated Load Balancer(s)',
  553:                   help  => 'Domain_Configuration_Load_Balancing',
  554:                   header => [{col1 => 'Balancers',
  555:                               col2 => 'Default destinations',
  556:                               col3 => 'User affiliation',
  557:                               col4 => 'Overrides'},
  558:                             ],
  559:                   print => \&print_loadbalancing,
  560:                   modify => \&modify_loadbalancing,
  561:                  },
  562:         'ltitools' => 
  563:                  {text => 'External Tools (LTI)',
  564:                   help => 'Domain_Configuration_LTI_Tools',
  565:                   header => [{col1 => 'Setting',
  566:                               col2 => 'Value',}],
  567:                   print => \&print_ltitools,
  568:                   modify => \&modify_ltitools,
  569:                  },
  570:         'proctoring' =>
  571:                  {text => 'Remote Proctoring Integration',
  572:                   help => 'Domain_Configuration_Proctoring',
  573:                   header => [{col1 => 'Name',
  574:                               col2 => 'Configuration'}],
  575:                   print => \&print_proctoring,
  576:                   modify => \&modify_proctoring,
  577:                  },
  578:         'ssl' =>
  579:                  {text  => 'LON-CAPA Network (SSL)',
  580:                   help  => 'Domain_Configuration_Network_SSL',
  581:                   header => [{col1 => 'Server',
  582:                               col2 => 'Certificate Status'},
  583:                              {col1 => 'Connections to other servers',
  584:                               col2 => 'Rules'},
  585:                              {col1 => 'Connections from other servers',
  586:                               col2 => 'Rules'},
  587:                              {col1 => "Replicating domain's published content",
  588:                               col2 => 'Rules'}],
  589:                   print => \&print_ssl,
  590:                   modify => \&modify_ssl,
  591:                  },
  592:         'trust' =>
  593:                  {text   => 'Trust Settings',
  594:                   help   => 'Domain_Configuration_Trust',
  595:                   header => [{col1 => "Access to this domain's content by others",
  596:                               col2 => 'Rules'},
  597:                              {col1 => "Access to other domain's content by this domain",
  598:                               col2 => 'Rules'},
  599:                              {col1 => "Enrollment in this domain's courses by others",
  600:                               col2 => 'Rules',},
  601:                              {col1 => "Co-author roles in this domain for others",
  602:                               col2 => 'Rules',},
  603:                              {col1 => "Co-author roles for this domain's users elsewhere",
  604:                               col2 => 'Rules',},
  605:                              {col1 => "Domain roles in this domain assignable to others",
  606:                               col2 => 'Rules'},
  607:                              {col1 => "Course catalog for this domain displayed elsewhere",
  608:                               col2 => 'Rules'},
  609:                              {col1 => "Requests for creation of courses in this domain by others",
  610:                               col2 => 'Rules'},
  611:                              {col1 => "Users in other domains can send messages to this domain",
  612:                               col2 => 'Rules'},],
  613:                   print => \&print_trust,
  614:                   modify => \&modify_trust,
  615:                  },
  616:           'lti' =>
  617:                  {text => 'LTI Provider',
  618:                   help => 'Domain_Configuration_LTI_Provider',
  619:                   header => [{col1 => 'Setting',
  620:                               col2 => 'Value',}],
  621:                   print => \&print_lti,
  622:                   modify => \&modify_lti,
  623:                  },
  624:     );
  625:     if (keys(%servers) > 1) {
  626:         $prefs{'login'}  = { text   => 'Log-in page options',
  627:                              help   => 'Domain_Configuration_Login_Page',
  628:                             header => [{col1 => 'Log-in Service',
  629:                                         col2 => 'Server Setting',},
  630:                                        {col1 => 'Log-in Page Items',
  631:                                         col2 => ''},
  632:                                        {col1 => 'Log-in Help',
  633:                                         col2 => 'Value'},
  634:                                        {col1 => 'Custom HTML in document head',
  635:                                         col2 => 'Value'}],
  636:                             print => \&print_login,
  637:                             modify => \&modify_login,
  638:                            };
  639:     }
  640: 
  641:     my @roles = ('student','coordinator','author','admin');
  642:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  643:     &Apache::lonhtmlcommon::add_breadcrumb
  644:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  645:       text=>"Settings to display/modify"});
  646:     my $confname = $dom.'-domainconfig';
  647: 
  648:     if ($phase eq 'process') {
  649:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  650:                                                               \%prefs,\%domconfig,$confname,\@roles);
  651:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  652:             $r->rflush();
  653:             &devalidate_remote_domconfs($dom,$result);
  654:         }
  655:     } elsif ($phase eq 'display') {
  656:         my $js = &recaptcha_js().
  657:                  &toggle_display_js();
  658:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  659:             my ($othertitle,$usertypes,$types) =
  660:                 &Apache::loncommon::sorted_inst_types($dom);
  661:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  662:                                           $domconfig{'loadbalancing'}).
  663:                    &new_spares_js().
  664:                    &common_domprefs_js().
  665:                    &Apache::loncommon::javascript_array_indexof();
  666:         }
  667:         if (grep(/^requestcourses$/,@actions)) {
  668:             my $javascript_validations;
  669:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  670:             $js .= <<END;
  671: <script type="text/javascript">
  672: $javascript_validations
  673: </script>
  674: $coursebrowserjs
  675: END
  676:         }
  677:         if (grep(/^selfcreation$/,@actions)) {
  678:             $js .= &selfcreate_javascript();
  679:         }
  680:         if (grep(/^contacts$/,@actions)) {
  681:             $js .= &contacts_javascript();
  682:         }
  683:         if (grep(/^scantron$/,@actions)) {
  684:             $js .= &scantron_javascript();
  685:         }
  686:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  687:     } else {
  688: # check if domconfig user exists for the domain.
  689:         my $servadm = $r->dir_config('lonAdmEMail');
  690:         my ($configuserok,$author_ok,$switchserver) =
  691:             &config_check($dom,$confname,$servadm);
  692:         unless ($configuserok eq 'ok') {
  693:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  694:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  695:                           $confname).
  696:                       '<br />'
  697:             );
  698:             if ($switchserver) {
  699:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  700:                           '<br />'.
  701:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  702:                           '<br />'.
  703:                           &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).
  704:                           '<br />'.
  705:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  706:                 );
  707:             } else {
  708:                 $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.').
  709:                           '<br />'.
  710:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  711:                 );
  712:             }
  713:             $r->print(&Apache::loncommon::end_page());
  714:             return OK;
  715:         }
  716:         if (keys(%domconfig) == 0) {
  717:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  718:             my @ids=&Apache::lonnet::current_machine_ids();
  719:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  720:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  721:                 my @loginimages = ('img','logo','domlogo','login');
  722:                 my $custom_img_count = 0;
  723:                 foreach my $img (@loginimages) {
  724:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  725:                         $custom_img_count ++;
  726:                     }
  727:                 }
  728:                 foreach my $role (@roles) {
  729:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  730:                         $custom_img_count ++;
  731:                     }
  732:                 }
  733:                 if ($custom_img_count > 0) {
  734:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  735:                     my $switch_server = &check_switchserver($dom,$confname);
  736:                     $r->print(
  737:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  738:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  739:     &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 />'.
  740:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  741:                     if ($switch_server) {
  742:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  743:                     }
  744:                     $r->print(&Apache::loncommon::end_page());
  745:                     return OK;
  746:                 }
  747:             }
  748:         }
  749:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  750:     }
  751:     return OK;
  752: }
  753: 
  754: sub process_changes {
  755:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  756:     my %domconfig;
  757:     if (ref($values) eq 'HASH') {
  758:         %domconfig = %{$values};
  759:     }
  760:     my $output;
  761:     if ($action eq 'login') {
  762:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  763:     } elsif ($action eq 'rolecolors') {
  764:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  765:                                      $lastactref,%domconfig);
  766:     } elsif ($action eq 'quotas') {
  767:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  768:     } elsif ($action eq 'autoenroll') {
  769:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  770:     } elsif ($action eq 'autoupdate') {
  771:         $output = &modify_autoupdate($dom,%domconfig);
  772:     } elsif ($action eq 'autocreate') {
  773:         $output = &modify_autocreate($dom,%domconfig);
  774:     } elsif ($action eq 'directorysrch') {
  775:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  776:     } elsif ($action eq 'usercreation') {
  777:         $output = &modify_usercreation($dom,%domconfig);
  778:     } elsif ($action eq 'selfcreation') {
  779:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  780:     } elsif ($action eq 'usermodification') {
  781:         $output = &modify_usermodification($dom,%domconfig);
  782:     } elsif ($action eq 'contacts') {
  783:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  784:     } elsif ($action eq 'defaults') {
  785:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  786:     } elsif ($action eq 'scantron') {
  787:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  788:     } elsif ($action eq 'coursecategories') {
  789:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  790:     } elsif ($action eq 'serverstatuses') {
  791:         $output = &modify_serverstatuses($dom,%domconfig);
  792:     } elsif ($action eq 'requestcourses') {
  793:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  794:     } elsif ($action eq 'requestauthor') {
  795:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  796:     } elsif ($action eq 'helpsettings') {
  797:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  798:     } elsif ($action eq 'coursedefaults') {
  799:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  800:     } elsif ($action eq 'selfenrollment') {
  801:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  802:     } elsif ($action eq 'usersessions') {
  803:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  804:     } elsif ($action eq 'loadbalancing') {
  805:         $output = &modify_loadbalancing($dom,%domconfig);
  806:     } elsif ($action eq 'ltitools') {
  807:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  808:     } elsif ($action eq 'proctoring') {
  809:         $output = &modify_proctoring($r,$dom,$action,$lastactref,%domconfig);
  810:     } elsif ($action eq 'ssl') {
  811:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  812:     } elsif ($action eq 'trust') {
  813:         $output = &modify_trust($dom,$lastactref,%domconfig);
  814:     } elsif ($action eq 'lti') {
  815:         $output = &modify_lti($r,$dom,$action,$lastactref,%domconfig);
  816:     } elsif ($action eq 'privacy') {
  817:         $output = &modify_privacy($dom,%domconfig);
  818:     } elsif ($action eq 'passwords') {
  819:         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
  820:     } elsif ($action eq 'wafproxy') {
  821:         $output = &modify_wafproxy($dom,$action,$lastactref,%domconfig);
  822:     }
  823:     return $output;
  824: }
  825: 
  826: sub print_config_box {
  827:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  828:     my $rowtotal = 0;
  829:     my $output;
  830:     if ($action eq 'coursecategories') {
  831:         $output = &coursecategories_javascript($settings);
  832:     } elsif ($action eq 'defaults') {
  833:         $output = &defaults_javascript($settings); 
  834:     } elsif ($action eq 'passwords') {
  835:         $output = &passwords_javascript();
  836:     } elsif ($action eq 'helpsettings') {
  837:         my (%privs,%levelscurrent);
  838:         my %full=();
  839:         my %levels=(
  840:                      course => {},
  841:                      domain => {},
  842:                      system => {},
  843:                    );
  844:         my $context = 'domain';
  845:         my $crstype = 'Course';
  846:         my $formname = 'display';
  847:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  848:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  849:         $output =
  850:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full, 
  851:                                                       \@templateroles);
  852:     } elsif ($action eq 'ltitools') {
  853:         $output .= &ltitools_javascript($settings);
  854:     } elsif ($action eq 'lti') {
  855:         $output .= &lti_javascript($settings);
  856:     } elsif ($action eq 'proctoring') {
  857:         $output .= &proctoring_javascript($settings);
  858:     }
  859:     $output .=
  860:          '<table class="LC_nested_outer">
  861:           <tr>
  862:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  863:            &mt($item->{text}).'&nbsp;'.
  864:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  865:           '</tr>';
  866:     $rowtotal ++;
  867:     my $numheaders = 1;
  868:     if (ref($item->{'header'}) eq 'ARRAY') {
  869:         $numheaders = scalar(@{$item->{'header'}});
  870:     }
  871:     if ($numheaders > 1) {
  872:         my $colspan = '';
  873:         my $rightcolspan = '';
  874:         my $leftnobr = '';
  875:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  876:             ($action eq 'directorysrch') ||
  877:             (($action eq 'login') && ($numheaders < 4))) {
  878:             $colspan = ' colspan="2"';
  879:         }
  880:         if ($action eq 'usersessions') {
  881:             $rightcolspan = ' colspan="3"'; 
  882:         }
  883:         if ($action eq 'passwords') {
  884:             $leftnobr = ' LC_nobreak';
  885:         }
  886:         $output .= '
  887:           <tr>
  888:            <td>
  889:             <table class="LC_nested">
  890:              <tr class="LC_info_row">
  891:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  892:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  893:              </tr>';
  894:         $rowtotal ++;
  895:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  896:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  897:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  898:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  899:             ($action eq 'contacts') || ($action eq 'privacy') || ($action eq 'wafproxy')) {
  900:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  901:         } elsif ($action eq 'passwords') {
  902:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
  903:         } elsif ($action eq 'coursecategories') {
  904:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  905:         } elsif ($action eq 'scantron') {
  906:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
  907:         } elsif ($action eq 'login') {
  908:             if ($numheaders == 4) {
  909:                 $colspan = ' colspan="2"';
  910:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  911:             } else {
  912:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  913:             }
  914:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  915:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  916:         } elsif ($action eq 'rolecolors') {
  917:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  918:         }
  919:         $output .= '
  920:            </table>
  921:           </td>
  922:          </tr>
  923:          <tr>
  924:            <td>
  925:             <table class="LC_nested">
  926:              <tr class="LC_info_row">
  927:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  928:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  929:              </tr>';
  930:             $rowtotal ++;
  931:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  932:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  933:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
  934:             ($action eq 'trust') || ($action eq 'contacts') ||
  935:             ($action eq 'privacy') || ($action eq 'passwords')) {
  936:             if ($action eq 'coursecategories') {
  937:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  938:                 $colspan = ' colspan="2"';
  939:             } elsif ($action eq 'trust') {
  940:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
  941:             } elsif ($action eq 'passwords') {
  942:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
  943:             } else {
  944:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  945:             }
  946:             if ($action eq 'trust') {
  947:                 $output .= '
  948:             </table>
  949:           </td>
  950:          </tr>';
  951:                 my @trusthdrs = qw(2 3 4 5 6 7);
  952:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
  953:                 for (my $i=0; $i<@trusthdrs; $i++) {
  954:                     $output .= '
  955:          <tr>
  956:            <td>
  957:             <table class="LC_nested">
  958:              <tr class="LC_info_row">
  959:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
  960:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
  961:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
  962:             </table>
  963:           </td>
  964:          </tr>';
  965:                 }
  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'}->[8]->{'col1'}).'</td>
  972:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
  973:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  974:             } else {
  975:                 $output .= '
  976:            </table>
  977:           </td>
  978:          </tr>
  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'}->[2]->{'col1'}).'</td>
  984:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  985:              </tr>'."\n";
  986:                 if ($action eq 'coursecategories') {
  987:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  988:                 } elsif (($action eq 'contacts') || ($action eq 'privacy') || ($action eq 'passwords')) {
  989:                     if ($action eq 'passwords') {
  990:                         $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal);
  991:                     } else {
  992:                         $output .= $item->{'print'}->('lower',$dom,$settings,\$rowtotal);
  993:                     }
  994:                     $output .= '
  995:              </tr>
  996:             </table>
  997:            </td>
  998:           </tr>
  999:           <tr>
 1000:            <td>
 1001:             <table class="LC_nested">
 1002:              <tr class="LC_info_row">
 1003:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1004:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'."\n";
 1005:                     if ($action eq 'passwords') {
 1006:                         $output .= $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal);
 1007:                     } else {
 1008:                         $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1009:                     }
 1010:                     $output .= '
 1011:             </table>
 1012:           </td>
 1013:          </tr>
 1014:          <tr>';
 1015:                 } else {
 1016:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1017:                 }
 1018:             }
 1019:             $rowtotal ++;
 1020:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
 1021:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
 1022:                  ($action eq 'helpsettings') || ($action eq 'wafproxy')) {
 1023:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1024:         } elsif ($action eq 'scantron') {
 1025:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
 1026:         } elsif ($action eq 'ssl') {
 1027:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
 1028:             </table>
 1029:           </td>
 1030:          </tr>
 1031:          <tr>
 1032:            <td>
 1033:             <table class="LC_nested">
 1034:              <tr class="LC_info_row">
 1035:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1036:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1037:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
 1038:             </table>
 1039:           </td>
 1040:          </tr>
 1041:          <tr>
 1042:            <td>
 1043:             <table class="LC_nested">
 1044:              <tr class="LC_info_row">
 1045:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1046:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
 1047:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1048:         } elsif ($action eq 'login') {
 1049:             if ($numheaders == 4) {
 1050:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
 1051:            </table>
 1052:           </td>
 1053:          </tr>
 1054:          <tr>
 1055:            <td>
 1056:             <table class="LC_nested">
 1057:              <tr class="LC_info_row">
 1058:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1059:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1060:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1061:                 $rowtotal ++;
 1062:             } else {
 1063:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1064:             }
 1065:             $output .= '
 1066:            </table>
 1067:           </td>
 1068:          </tr>
 1069:          <tr>
 1070:            <td>
 1071:             <table class="LC_nested">
 1072:              <tr class="LC_info_row">';
 1073:             if ($numheaders == 4) {
 1074:                 $output .= '
 1075:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1076:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1077:              </tr>';
 1078:             } else {
 1079:                 $output .= '
 1080:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1081:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1082:              </tr>';
 1083:             }
 1084:             $rowtotal ++;
 1085:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
 1086:         } elsif ($action eq 'requestcourses') {
 1087:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1088:             $rowtotal ++;
 1089:             $output .= &print_studentcode($settings,\$rowtotal).'
 1090:            </table>
 1091:           </td>
 1092:          </tr>
 1093:          <tr>
 1094:            <td>
 1095:             <table class="LC_nested">
 1096:              <tr class="LC_info_row">
 1097:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1098:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
 1099:                        &textbookcourses_javascript($settings).
 1100:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
 1101:             </table>
 1102:            </td>
 1103:           </tr>
 1104:          <tr>
 1105:            <td>
 1106:             <table class="LC_nested">
 1107:              <tr class="LC_info_row">
 1108:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1109:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
 1110:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
 1111:             </table>
 1112:            </td>
 1113:           </tr>
 1114:           <tr>
 1115:            <td>
 1116:             <table class="LC_nested">
 1117:              <tr class="LC_info_row">
 1118:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1119:               <td class="LC_right_item" style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1120:              </tr>'.
 1121:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
 1122:         } elsif ($action eq 'requestauthor') {
 1123:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1124:             $rowtotal ++;
 1125:         } elsif ($action eq 'rolecolors') {
 1126:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
 1127:            </table>
 1128:           </td>
 1129:          </tr>
 1130:          <tr>
 1131:            <td>
 1132:             <table class="LC_nested">
 1133:              <tr class="LC_info_row">
 1134:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.
 1135:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
 1136:               <td class="LC_right_item" style="vertical-align: top">'.
 1137:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1138:              </tr>'.
 1139:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1140:            </table>
 1141:           </td>
 1142:          </tr>
 1143:          <tr>
 1144:            <td>
 1145:             <table class="LC_nested">
 1146:              <tr class="LC_info_row">
 1147:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1148:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1149:              </tr>'.
 1150:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1151:             $rowtotal += 2;
 1152:         }
 1153:     } else {
 1154:         $output .= '
 1155:           <tr>
 1156:            <td>
 1157:             <table class="LC_nested">
 1158:              <tr class="LC_info_row">';
 1159:         if ($action eq 'login') {
 1160:             $output .= '  
 1161:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1162:         } elsif ($action eq 'serverstatuses') {
 1163:             $output .= '
 1164:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1165:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1166: 
 1167:         } else {
 1168:             $output .= '
 1169:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1170:         }
 1171:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1172:             $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1173:                        &mt($item->{'header'}->[0]->{'col2'});
 1174:             if ($action eq 'serverstatuses') {
 1175:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1176:             } 
 1177:         } else {
 1178:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1179:                        &mt($item->{'header'}->[0]->{'col2'});
 1180:         }
 1181:         $output .= '</td>';
 1182:         if ($item->{'header'}->[0]->{'col3'}) {
 1183:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1184:                 $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1185:                             &mt($item->{'header'}->[0]->{'col3'});
 1186:             } else {
 1187:                 $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1188:                            &mt($item->{'header'}->[0]->{'col3'});
 1189:             }
 1190:             if ($action eq 'serverstatuses') {
 1191:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1192:             }
 1193:             $output .= '</td>';
 1194:         }
 1195:         if ($item->{'header'}->[0]->{'col4'}) {
 1196:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1197:                        &mt($item->{'header'}->[0]->{'col4'});
 1198:         }
 1199:         $output .= '</tr>';
 1200:         $rowtotal ++;
 1201:         if ($action eq 'quotas') {
 1202:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1203:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1204:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1205:                  ($action eq 'ltitools') || ($action eq 'lti') ||
 1206:                  ($action eq 'proctoring')) {
 1207:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1208:         }
 1209:     }
 1210:     $output .= '
 1211:    </table>
 1212:   </td>
 1213:  </tr>
 1214: </table><br />';
 1215:     return ($output,$rowtotal);
 1216: }
 1217: 
 1218: sub print_login {
 1219:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1220:     my ($css_class,$datatable);
 1221:     my %choices = &login_choices();
 1222: 
 1223:     if ($caller eq 'service') {
 1224:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1225:         my $choice = $choices{'disallowlogin'};
 1226:         $css_class = ' class="LC_odd_row"';
 1227:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1228:                       '<td style="text-align: right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1229:                       '<th>'.$choices{'server'}.'</th>'.
 1230:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1231:                       '<th>'.$choices{'custompath'}.'</th>'.
 1232:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1233:         my %disallowed;
 1234:         if (ref($settings) eq 'HASH') {
 1235:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1236:                %disallowed = %{$settings->{'loginvia'}};
 1237:             }
 1238:         }
 1239:         foreach my $lonhost (sort(keys(%servers))) {
 1240:             my $direct = 'selected="selected"';
 1241:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1242:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1243:                     $direct = '';
 1244:                 }
 1245:             }
 1246:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1247:                           '<td><select name="'.$lonhost.'_server">'.
 1248:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1249:                           '</option>';
 1250:             foreach my $hostid (sort(keys(%servers))) {
 1251:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1252:                 my $selected = '';
 1253:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1254:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1255:                         $selected = 'selected="selected"';
 1256:                     }
 1257:                 }
 1258:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1259:                               $servers{$hostid}.'</option>';
 1260:             }
 1261:             $datatable .= '</select></td>'.
 1262:                           '<td><select name="'.$lonhost.'_serverpath">';
 1263:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1264:                 my $pathname = $path;
 1265:                 if ($path eq 'custom') {
 1266:                     $pathname = &mt('Custom Path').' ->';
 1267:                 }
 1268:                 my $selected = '';
 1269:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1270:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1271:                         $selected = 'selected="selected"';
 1272:                     }
 1273:                 } elsif ($path eq '') {
 1274:                     $selected = 'selected="selected"';
 1275:                 }
 1276:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1277:             }
 1278:             $datatable .= '</select></td>';
 1279:             my ($custom,$exempt);
 1280:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1281:                 $custom = $disallowed{$lonhost}{'custompath'};
 1282:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1283:             }
 1284:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1285:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1286:                           '</tr>';
 1287:         }
 1288:         $datatable .= '</table></td></tr>';
 1289:         return $datatable;
 1290:     } elsif ($caller eq 'page') {
 1291:         my %defaultchecked = ( 
 1292:                                'coursecatalog' => 'on',
 1293:                                'helpdesk'      => 'on',
 1294:                                'adminmail'     => 'off',
 1295:                                'newuser'       => 'off',
 1296:                              );
 1297:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1298:         my (%checkedon,%checkedoff);
 1299:         foreach my $item (@toggles) {
 1300:             if ($defaultchecked{$item} eq 'on') { 
 1301:                 $checkedon{$item} = ' checked="checked" ';
 1302:                 $checkedoff{$item} = ' ';
 1303:             } elsif ($defaultchecked{$item} eq 'off') {
 1304:                 $checkedoff{$item} = ' checked="checked" ';
 1305:                 $checkedon{$item} = ' ';
 1306:             }
 1307:         }
 1308:         my @images = ('img','logo','domlogo','login');
 1309:         my @logintext = ('textcol','bgcol');
 1310:         my @bgs = ('pgbg','mainbg','sidebg');
 1311:         my @links = ('link','alink','vlink');
 1312:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1313:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1314:         my (%is_custom,%designs);
 1315:         my %defaults = (
 1316:                        font => $defaultdesign{'login.font'},
 1317:                        );
 1318:         foreach my $item (@images) {
 1319:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1320:             $defaults{'showlogo'}{$item} = 1;
 1321:         }
 1322:         foreach my $item (@bgs) {
 1323:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1324:         }
 1325:         foreach my $item (@logintext) {
 1326:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1327:         }
 1328:         foreach my $item (@links) {
 1329:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1330:         }
 1331:         if (ref($settings) eq 'HASH') {
 1332:             foreach my $item (@toggles) {
 1333:                 if ($settings->{$item} eq '1') {
 1334:                     $checkedon{$item} =  ' checked="checked" ';
 1335:                     $checkedoff{$item} = ' ';
 1336:                 } elsif ($settings->{$item} eq '0') {
 1337:                     $checkedoff{$item} =  ' checked="checked" ';
 1338:                     $checkedon{$item} = ' ';
 1339:                 }
 1340:             }
 1341:             foreach my $item (@images) {
 1342:                 if (defined($settings->{$item})) {
 1343:                     $designs{$item} = $settings->{$item};
 1344:                     $is_custom{$item} = 1;
 1345:                 }
 1346:                 if (defined($settings->{'showlogo'}{$item})) {
 1347:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1348:                 }
 1349:             }
 1350:             foreach my $item (@logintext) {
 1351:                 if ($settings->{$item} ne '') {
 1352:                     $designs{'logintext'}{$item} = $settings->{$item};
 1353:                     $is_custom{$item} = 1;
 1354:                 }
 1355:             }
 1356:             if ($settings->{'font'} ne '') {
 1357:                 $designs{'font'} = $settings->{'font'};
 1358:                 $is_custom{'font'} = 1;
 1359:             }
 1360:             foreach my $item (@bgs) {
 1361:                 if ($settings->{$item} ne '') {
 1362:                     $designs{'bgs'}{$item} = $settings->{$item};
 1363:                     $is_custom{$item} = 1;
 1364:                 }
 1365:             }
 1366:             foreach my $item (@links) {
 1367:                 if ($settings->{$item} ne '') {
 1368:                     $designs{'links'}{$item} = $settings->{$item};
 1369:                     $is_custom{$item} = 1;
 1370:                 }
 1371:             }
 1372:         } else {
 1373:             if ($designhash{$dom.'.login.font'} ne '') {
 1374:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1375:                 $is_custom{'font'} = 1;
 1376:             }
 1377:             foreach my $item (@images) {
 1378:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1379:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1380:                     $is_custom{$item} = 1;
 1381:                 }
 1382:             }
 1383:             foreach my $item (@bgs) {
 1384:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1385:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1386:                     $is_custom{$item} = 1;
 1387:                 }
 1388:             }
 1389:             foreach my $item (@links) {
 1390:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1391:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1392:                     $is_custom{$item} = 1;
 1393:                 }
 1394:             }
 1395:         }
 1396:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1397:                                                       logo => 'Institution Logo',
 1398:                                                       domlogo => 'Domain Logo',
 1399:                                                       login => 'Login box');
 1400:         my $itemcount = 1;
 1401:         foreach my $item (@toggles) {
 1402:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1403:             $datatable .=  
 1404:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1405:                 '</td><td>'.
 1406:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1407:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1408:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1409:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1410:                 '</tr>';
 1411:             $itemcount ++;
 1412:         }
 1413:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1414:         $datatable .= '</tr></table></td></tr>';
 1415:     } elsif ($caller eq 'help') {
 1416:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1417:         my $switchserver = &check_switchserver($dom,$confname);
 1418:         my $itemcount = 1;
 1419:         $defaulturl = '/adm/loginproblems.html';
 1420:         $defaulttype = 'default';
 1421:         %lt = &Apache::lonlocal::texthash (
 1422:                      del     => 'Delete?',
 1423:                      rep     => 'Replace:',
 1424:                      upl     => 'Upload:',
 1425:                      default => 'Default',
 1426:                      custom  => 'Custom',
 1427:                                              );
 1428:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1429:         my @currlangs;
 1430:         if (ref($settings) eq 'HASH') {
 1431:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1432:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1433:                     next if ($settings->{'helpurl'}{$key} eq '');
 1434:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1435:                     $type{$key} = 'custom';
 1436:                     unless ($key eq 'nolang') {
 1437:                         push(@currlangs,$key);
 1438:                     }
 1439:                 }
 1440:             } elsif ($settings->{'helpurl'} ne '') {
 1441:                 $type{'nolang'} = 'custom';
 1442:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1443:             }
 1444:         }
 1445:         foreach my $lang ('nolang',sort(@currlangs)) {
 1446:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1447:             $datatable .= '<tr'.$css_class.'>';
 1448:             if ($url{$lang} eq '') {
 1449:                 $url{$lang} = $defaulturl;
 1450:             }
 1451:             if ($type{$lang} eq '') {
 1452:                 $type{$lang} = $defaulttype;
 1453:             }
 1454:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1455:             if ($lang eq 'nolang') {
 1456:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1457:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1458:             } else {
 1459:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1460:                                   $langchoices{$lang},
 1461:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1462:             }
 1463:             $datatable .= '</span></td>'."\n".
 1464:                           '<td class="LC_left_item">';
 1465:             if ($type{$lang} eq 'custom') {
 1466:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1467:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1468:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1469:             } else {
 1470:                 $datatable .= $lt{'upl'};
 1471:             }
 1472:             $datatable .='<br />';
 1473:             if ($switchserver) {
 1474:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1475:             } else {
 1476:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1477:             }
 1478:             $datatable .= '</td></tr>';
 1479:             $itemcount ++;
 1480:         }
 1481:         my @addlangs;
 1482:         foreach my $lang (sort(keys(%langchoices))) {
 1483:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1484:             push(@addlangs,$lang);
 1485:         }
 1486:         if (@addlangs > 0) {
 1487:             my %toadd;
 1488:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1489:             $toadd{''} = &mt('Select');
 1490:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1491:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1492:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1493:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1494:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1495:             if ($switchserver) {
 1496:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1497:             } else {
 1498:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1499:             }
 1500:             $datatable .= '</td></tr>';
 1501:             $itemcount ++;
 1502:         }
 1503:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1504:     } elsif ($caller eq 'headtag') {
 1505:         my %domservers = &Apache::lonnet::get_servers($dom);
 1506:         my $choice = $choices{'headtag'};
 1507:         $css_class = ' class="LC_odd_row"';
 1508:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1509:                       '<td style="text-align: left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1510:                       '<th>'.$choices{'current'}.'</th>'.
 1511:                       '<th>'.$choices{'action'}.'</th>'.
 1512:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1513:         my (%currurls,%currexempt);
 1514:         if (ref($settings) eq 'HASH') {
 1515:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1516:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1517:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1518:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1519:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1520:                     }
 1521:                 }
 1522:             }
 1523:         }
 1524:         my %lt = &Apache::lonlocal::texthash(
 1525:                                                del  => 'Delete?',
 1526:                                                rep  => 'Replace:',
 1527:                                                upl  => 'Upload:',
 1528:                                                curr => 'View contents',
 1529:                                                none => 'None',
 1530:         );
 1531:         my $switchserver = &check_switchserver($dom,$confname);
 1532:         foreach my $lonhost (sort(keys(%domservers))) {
 1533:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1534:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1535:             if ($currurls{$lonhost}) {
 1536:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1537:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1538:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1539:                               '">'.$lt{'curr'}.'</a></td>'.
 1540:                               '<td><span class="LC_nobreak"><label>'.
 1541:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1542:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1543:             } else {
 1544:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1545:             }
 1546:             $datatable .='<br />';
 1547:             if ($switchserver) {
 1548:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1549:             } else {
 1550:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1551:             }
 1552:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1553:         }
 1554:         $datatable .= '</table></td></tr>';
 1555:     }
 1556:     return $datatable;
 1557: }
 1558: 
 1559: sub login_choices {
 1560:     my %choices =
 1561:         &Apache::lonlocal::texthash (
 1562:             coursecatalog => 'Display Course/Community Catalog link?',
 1563:             adminmail     => "Display Administrator's E-mail Address?",
 1564:             helpdesk      => 'Display "Contact Helpdesk" link',
 1565:             disallowlogin => "Login page requests redirected",
 1566:             hostid        => "Server",
 1567:             server        => "Redirect to:",
 1568:             serverpath    => "Path",
 1569:             custompath    => "Custom", 
 1570:             exempt        => "Exempt IP(s)",
 1571:             directlogin   => "No redirect",
 1572:             newuser       => "Link to create a user account",
 1573:             img           => "Header",
 1574:             logo          => "Main Logo",
 1575:             domlogo       => "Domain Logo",
 1576:             login         => "Log-in Header", 
 1577:             textcol       => "Text color",
 1578:             bgcol         => "Box color",
 1579:             bgs           => "Background colors",
 1580:             links         => "Link colors",
 1581:             font          => "Font color",
 1582:             pgbg          => "Header",
 1583:             mainbg        => "Page",
 1584:             sidebg        => "Login box",
 1585:             link          => "Link",
 1586:             alink         => "Active link",
 1587:             vlink         => "Visited link",
 1588:             headtag       => "Custom markup",
 1589:             action        => "Action",
 1590:             current       => "Current",
 1591:         );
 1592:     return %choices;
 1593: }
 1594: 
 1595: sub print_rolecolors {
 1596:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1597:     my %choices = &color_font_choices();
 1598:     my @bgs = ('pgbg','tabbg','sidebg');
 1599:     my @links = ('link','alink','vlink');
 1600:     my @images = ('img');
 1601:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1602:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1603:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1604:     my (%is_custom,%designs);
 1605:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1606:     if (ref($settings) eq 'HASH') {
 1607:         if (ref($settings->{$role}) eq 'HASH') {
 1608:             if ($settings->{$role}->{'img'} ne '') {
 1609:                 $designs{'img'} = $settings->{$role}->{'img'};
 1610:                 $is_custom{'img'} = 1;
 1611:             }
 1612:             if ($settings->{$role}->{'font'} ne '') {
 1613:                 $designs{'font'} = $settings->{$role}->{'font'};
 1614:                 $is_custom{'font'} = 1;
 1615:             }
 1616:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1617:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1618:                 $is_custom{'fontmenu'} = 1;
 1619:             }
 1620:             foreach my $item (@bgs) {
 1621:                 if ($settings->{$role}->{$item} ne '') {
 1622:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1623:                     $is_custom{$item} = 1;
 1624:                 }
 1625:             }
 1626:             foreach my $item (@links) {
 1627:                 if ($settings->{$role}->{$item} ne '') {
 1628:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1629:                     $is_custom{$item} = 1;
 1630:                 }
 1631:             }
 1632:         }
 1633:     } else {
 1634:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1635:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1636:             $is_custom{'img'} = 1;
 1637:         }
 1638:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1639:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1640:             $is_custom{'fontmenu'} = 1; 
 1641:         }
 1642:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1643:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1644:             $is_custom{'font'} = 1;
 1645:         }
 1646:         foreach my $item (@bgs) {
 1647:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1648:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1649:                 $is_custom{$item} = 1;
 1650:             
 1651:             }
 1652:         }
 1653:         foreach my $item (@links) {
 1654:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1655:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1656:                 $is_custom{$item} = 1;
 1657:             }
 1658:         }
 1659:     }
 1660:     my $itemcount = 1;
 1661:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1662:     $datatable .= '</tr></table></td></tr>';
 1663:     return $datatable;
 1664: }
 1665: 
 1666: sub role_defaults {
 1667:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1668:     my %defaults;
 1669:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1670:         return %defaults;
 1671:     }
 1672:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1673:     if ($role eq 'login') {
 1674:         %defaults = (
 1675:                        font => $defaultdesign{$role.'.font'},
 1676:                     );
 1677:         if (ref($logintext) eq 'ARRAY') {
 1678:             foreach my $item (@{$logintext}) {
 1679:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1680:             }
 1681:         }
 1682:         foreach my $item (@{$images}) {
 1683:             $defaults{'showlogo'}{$item} = 1;
 1684:         }
 1685:     } else {
 1686:         %defaults = (
 1687:                        img => $defaultdesign{$role.'.img'},
 1688:                        font => $defaultdesign{$role.'.font'},
 1689:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1690:                     );
 1691:     }
 1692:     foreach my $item (@{$bgs}) {
 1693:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1694:     }
 1695:     foreach my $item (@{$links}) {
 1696:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1697:     }
 1698:     foreach my $item (@{$images}) {
 1699:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1700:     }
 1701:     return %defaults;
 1702: }
 1703: 
 1704: sub display_color_options {
 1705:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1706:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1707:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1708:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1709:     my $datatable = '<tr'.$css_class.'>'.
 1710:         '<td>'.$choices->{'font'}.'</td>';
 1711:     if (!$is_custom->{'font'}) {
 1712:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1713:     } else {
 1714:         $datatable .= '<td>&nbsp;</td>';
 1715:     }
 1716:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1717: 
 1718:     $datatable .= '<td><span class="LC_nobreak">'.
 1719:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1720:                   ' value="'.$current_color.'" />&nbsp;'.
 1721:                   '&nbsp;</span></td></tr>';
 1722:     unless ($role eq 'login') { 
 1723:         $datatable .= '<tr'.$css_class.'>'.
 1724:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1725:         if (!$is_custom->{'fontmenu'}) {
 1726:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1727:         } else {
 1728:             $datatable .= '<td>&nbsp;</td>';
 1729:         }
 1730: 	$current_color = $designs->{'fontmenu'} ?
 1731: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1732:         $datatable .= '<td><span class="LC_nobreak">'.
 1733:                       '<input class="colorchooser" type="text" size="10" name="'
 1734: 		      .$role.'_fontmenu"'.
 1735:                       ' value="'.$current_color.'" />&nbsp;'.
 1736:                       '&nbsp;</span></td></tr>';
 1737:     }
 1738:     my $switchserver = &check_switchserver($dom,$confname);
 1739:     foreach my $img (@{$images}) {
 1740: 	$itemcount ++;
 1741:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1742:         $datatable .= '<tr'.$css_class.'>'.
 1743:                       '<td>'.$choices->{$img};
 1744:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1745:         if ($role eq 'login') {
 1746:             if ($img eq 'login') {
 1747:                 $login_hdr_pick =
 1748:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1749:                 $logincolors =
 1750:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1751:                                        $designs,$defaults);
 1752:             } elsif ($img ne 'domlogo') {
 1753:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1754:             }
 1755:         }
 1756:         $datatable .= '</td>';
 1757:         if ($designs->{$img} ne '') {
 1758:             $imgfile = $designs->{$img};
 1759: 	    $img_import = ($imgfile =~ m{^/adm/});
 1760:         } else {
 1761:             $imgfile = $defaults->{$img};
 1762:         }
 1763:         if ($imgfile) {
 1764:             my ($showfile,$fullsize);
 1765:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1766:                 my $urldir = $1;
 1767:                 my $filename = $2;
 1768:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1769:                 if (@info) {
 1770:                     my $thumbfile = 'tn-'.$filename;
 1771:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1772:                     if (@thumb) {
 1773:                         $showfile = $urldir.'/'.$thumbfile;
 1774:                     } else {
 1775:                         $showfile = $imgfile;
 1776:                     }
 1777:                 } else {
 1778:                     $showfile = '';
 1779:                 }
 1780:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1781:                 $showfile = $imgfile;
 1782:                 my $imgdir = $1;
 1783:                 my $filename = $2;
 1784:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1785:                     $showfile = "/$imgdir/tn-".$filename;
 1786:                 } else {
 1787:                     my $input = $londocroot.$imgfile;
 1788:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1789:                     if (!-e $output) {
 1790:                         my ($width,$height) = &thumb_dimensions();
 1791:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1792:                         if ($fullwidth ne '' && $fullheight ne '') {
 1793:                             if ($fullwidth > $width && $fullheight > $height) { 
 1794:                                 my $size = $width.'x'.$height;
 1795:                                 my @args = ('convert','-sample',$size,$input,$output);
 1796:                                 system({$args[0]} @args);
 1797:                                 $showfile = "/$imgdir/tn-".$filename;
 1798:                             }
 1799:                         }
 1800:                     }
 1801:                 }
 1802:             }
 1803:             if ($showfile) {
 1804:                 if ($showfile =~ m{^/(adm|res)/}) {
 1805:                     if ($showfile =~ m{^/res/}) {
 1806:                         my $local_showfile =
 1807:                             &Apache::lonnet::filelocation('',$showfile);
 1808:                         &Apache::lonnet::repcopy($local_showfile);
 1809:                     }
 1810:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1811:                 }
 1812:                 if ($imgfile) {
 1813:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1814:                         if ($imgfile =~ m{^/res/}) {
 1815:                             my $local_imgfile =
 1816:                                 &Apache::lonnet::filelocation('',$imgfile);
 1817:                             &Apache::lonnet::repcopy($local_imgfile);
 1818:                         }
 1819:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1820:                     } else {
 1821:                         $fullsize = $imgfile;
 1822:                     }
 1823:                 }
 1824:                 $datatable .= '<td>';
 1825:                 if ($img eq 'login') {
 1826:                     $datatable .= $login_hdr_pick;
 1827:                 } 
 1828:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1829:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1830:             } else {
 1831:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1832:                               &mt('Upload:').'<br />';
 1833:             }
 1834:         } else {
 1835:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1836:                           &mt('Upload:').'<br />';
 1837:         }
 1838:         if ($switchserver) {
 1839:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1840:         } else {
 1841:             if ($img ne 'login') { # suppress file selection for Log-in header
 1842:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1843:             }
 1844:         }
 1845:         $datatable .= '</td></tr>';
 1846:     }
 1847:     $itemcount ++;
 1848:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1849:     $datatable .= '<tr'.$css_class.'>'.
 1850:                   '<td>'.$choices->{'bgs'}.'</td>';
 1851:     my $bgs_def;
 1852:     foreach my $item (@{$bgs}) {
 1853:         if (!$is_custom->{$item}) {
 1854:             $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>';
 1855:         }
 1856:     }
 1857:     if ($bgs_def) {
 1858:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1859:     } else {
 1860:         $datatable .= '<td>&nbsp;</td>';
 1861:     }
 1862:     $datatable .= '<td class="LC_right_item">'.
 1863:                   '<table border="0"><tr>';
 1864: 
 1865:     foreach my $item (@{$bgs}) {
 1866:         $datatable .= '<td style="text-align: center">'.$choices->{$item};
 1867: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1868:         if ($designs->{'bgs'}{$item}) {
 1869:             $datatable .= '&nbsp;';
 1870:         }
 1871:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1872:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1873:     }
 1874:     $datatable .= '</tr></table></td></tr>';
 1875:     $itemcount ++;
 1876:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1877:     $datatable .= '<tr'.$css_class.'>'.
 1878:                   '<td>'.$choices->{'links'}.'</td>';
 1879:     my $links_def;
 1880:     foreach my $item (@{$links}) {
 1881:         if (!$is_custom->{$item}) {
 1882:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1883:         }
 1884:     }
 1885:     if ($links_def) {
 1886:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1887:     } else {
 1888:         $datatable .= '<td>&nbsp;</td>';
 1889:     }
 1890:     $datatable .= '<td class="LC_right_item">'.
 1891:                   '<table border="0"><tr>';
 1892:     foreach my $item (@{$links}) {
 1893: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1894:         $datatable .= '<td style="text-align: center">'.$choices->{$item}."\n";
 1895:         if ($designs->{'links'}{$item}) {
 1896:             $datatable.='&nbsp;';
 1897:         }
 1898:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1899:                       '" /></td>';
 1900:     }
 1901:     $$rowtotal += $itemcount;
 1902:     return $datatable;
 1903: }
 1904: 
 1905: sub logo_display_options {
 1906:     my ($img,$defaults,$designs) = @_;
 1907:     my $checkedon;
 1908:     if (ref($defaults) eq 'HASH') {
 1909:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1910:             if ($defaults->{'showlogo'}{$img}) {
 1911:                 $checkedon = 'checked="checked" ';     
 1912:             }
 1913:         } 
 1914:     }
 1915:     if (ref($designs) eq 'HASH') {
 1916:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1917:             if (defined($designs->{'showlogo'}{$img})) {
 1918:                 if ($designs->{'showlogo'}{$img} == 0) {
 1919:                     $checkedon = '';
 1920:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1921:                     $checkedon = 'checked="checked" ';
 1922:                 }
 1923:             }
 1924:         }
 1925:     }
 1926:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1927:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1928:            &mt('show').'</label>'."\n";
 1929: }
 1930: 
 1931: sub login_header_options  {
 1932:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1933:     my $output = '';
 1934:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1935:         $output .= &mt('Text default(s):').'<br />';
 1936:         if (!$is_custom->{'textcol'}) {
 1937:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1938:                        '&nbsp;&nbsp;&nbsp;';
 1939:         }
 1940:         if (!$is_custom->{'bgcol'}) {
 1941:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1942:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1943:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1944:         }
 1945:         $output .= '<br />';
 1946:     }
 1947:     $output .='<br />';
 1948:     return $output;
 1949: }
 1950: 
 1951: sub login_text_colors {
 1952:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1953:     my $color_menu = '<table border="0"><tr>';
 1954:     foreach my $item (@{$logintext}) {
 1955:         $color_menu .= '<td style="text-align: center">'.$choices->{$item};
 1956:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1957:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1958:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1959:     }
 1960:     $color_menu .= '</tr></table><br />';
 1961:     return $color_menu;
 1962: }
 1963: 
 1964: sub image_changes {
 1965:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1966:     my $output;
 1967:     if ($img eq 'login') {
 1968:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 1969:     } elsif (!$is_custom) {
 1970:         if ($img ne 'domlogo') {
 1971:             $output = &mt('Default image:').'<br />';
 1972:         } else {
 1973:             $output = &mt('Default in use:').'<br />';
 1974:         }
 1975:     }
 1976:     if ($img ne 'login') {
 1977:         if ($img_import) {
 1978:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1979:         }
 1980:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1981:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1982:         if ($is_custom) {
 1983:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1984:                        '<input type="checkbox" name="'.
 1985:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1986:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1987:         } else {
 1988:             $output .= '<td class="LC_middle">'.$logincolors.&mt('Upload:').'<br />';
 1989:         }
 1990:     }
 1991:     return $output;
 1992: }
 1993: 
 1994: sub print_quotas {
 1995:     my ($dom,$settings,$rowtotal,$action) = @_;
 1996:     my $context;
 1997:     if ($action eq 'quotas') {
 1998:         $context = 'tools';
 1999:     } else {
 2000:         $context = $action;
 2001:     }
 2002:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 2003:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2004:     my $typecount = 0;
 2005:     my ($css_class,%titles);
 2006:     if ($context eq 'requestcourses') {
 2007:         @usertools = ('official','unofficial','community','textbook','placement','lti');
 2008:         @options =('norequest','approval','validate','autolimit');
 2009:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 2010:         %titles = &courserequest_titles();
 2011:     } elsif ($context eq 'requestauthor') {
 2012:         @usertools = ('author');
 2013:         @options = ('norequest','approval','automatic');
 2014:         %titles = &authorrequest_titles();
 2015:     } else {
 2016:         @usertools = ('aboutme','blog','webdav','portfolio');
 2017:         %titles = &tool_titles();
 2018:     }
 2019:     if (ref($types) eq 'ARRAY') {
 2020:         foreach my $type (@{$types}) {
 2021:             my ($currdefquota,$currauthorquota);
 2022:             unless (($context eq 'requestcourses') ||
 2023:                     ($context eq 'requestauthor')) {
 2024:                 if (ref($settings) eq 'HASH') {
 2025:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 2026:                         $currdefquota = $settings->{defaultquota}->{$type};
 2027:                     } else {
 2028:                         $currdefquota = $settings->{$type};
 2029:                     }
 2030:                     if (ref($settings->{authorquota}) eq 'HASH') {
 2031:                         $currauthorquota = $settings->{authorquota}->{$type};
 2032:                     }
 2033:                 }
 2034:             }
 2035:             if (defined($usertypes->{$type})) {
 2036:                 $typecount ++;
 2037:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 2038:                 $datatable .= '<tr'.$css_class.'>'.
 2039:                               '<td>'.$usertypes->{$type}.'</td>'.
 2040:                               '<td class="LC_left_item">';
 2041:                 if ($context eq 'requestcourses') {
 2042:                     $datatable .= '<table><tr>';
 2043:                 }
 2044:                 my %cell;  
 2045:                 foreach my $item (@usertools) {
 2046:                     if ($context eq 'requestcourses') {
 2047:                         my ($curroption,$currlimit);
 2048:                         if (ref($settings) eq 'HASH') {
 2049:                             if (ref($settings->{$item}) eq 'HASH') {
 2050:                                 $curroption = $settings->{$item}->{$type};
 2051:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 2052:                                     $currlimit = $1; 
 2053:                                 }
 2054:                             }
 2055:                         }
 2056:                         if (!$curroption) {
 2057:                             $curroption = 'norequest';
 2058:                         }
 2059:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 2060:                         foreach my $option (@options) {
 2061:                             my $val = $option;
 2062:                             if ($option eq 'norequest') {
 2063:                                 $val = 0;  
 2064:                             }
 2065:                             if ($option eq 'validate') {
 2066:                                 my $canvalidate = 0;
 2067:                                 if (ref($validations{$item}) eq 'HASH') { 
 2068:                                     if ($validations{$item}{$type}) {
 2069:                                         $canvalidate = 1;
 2070:                                     }
 2071:                                 }
 2072:                                 next if (!$canvalidate);
 2073:                             }
 2074:                             my $checked = '';
 2075:                             if ($option eq $curroption) {
 2076:                                 $checked = ' checked="checked"';
 2077:                             } elsif ($option eq 'autolimit') {
 2078:                                 if ($curroption =~ /^autolimit/) {
 2079:                                     $checked = ' checked="checked"';
 2080:                                 }                       
 2081:                             } 
 2082:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 2083:                                   '<input type="radio" name="crsreq_'.$item.
 2084:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 2085:                                   $titles{$option}.'</label>';
 2086:                             if ($option eq 'autolimit') {
 2087:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2088:                                                 $item.'_limit_'.$type.'" size="1" '.
 2089:                                                 'value="'.$currlimit.'" />';
 2090:                             }
 2091:                             $cell{$item} .= '</span> ';
 2092:                             if ($option eq 'autolimit') {
 2093:                                 $cell{$item} .= $titles{'unlimited'};
 2094:                             }
 2095:                         }
 2096:                     } elsif ($context eq 'requestauthor') {
 2097:                         my $curroption;
 2098:                         if (ref($settings) eq 'HASH') {
 2099:                             $curroption = $settings->{$type};
 2100:                         }
 2101:                         if (!$curroption) {
 2102:                             $curroption = 'norequest';
 2103:                         }
 2104:                         foreach my $option (@options) {
 2105:                             my $val = $option;
 2106:                             if ($option eq 'norequest') {
 2107:                                 $val = 0;
 2108:                             }
 2109:                             my $checked = '';
 2110:                             if ($option eq $curroption) {
 2111:                                 $checked = ' checked="checked"';
 2112:                             }
 2113:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2114:                                   '<input type="radio" name="authorreq_'.$type.
 2115:                                   '" value="'.$val.'"'.$checked.' />'.
 2116:                                   $titles{$option}.'</label></span>&nbsp; ';
 2117:                         }
 2118:                     } else {
 2119:                         my $checked = 'checked="checked" ';
 2120:                         if (ref($settings) eq 'HASH') {
 2121:                             if (ref($settings->{$item}) eq 'HASH') {
 2122:                                 if ($settings->{$item}->{$type} == 0) {
 2123:                                     $checked = '';
 2124:                                 } elsif ($settings->{$item}->{$type} == 1) {
 2125:                                     $checked =  'checked="checked" ';
 2126:                                 }
 2127:                             }
 2128:                         }
 2129:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2130:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 2131:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 2132:                                       '</label></span>&nbsp; ';
 2133:                     }
 2134:                 }
 2135:                 if ($context eq 'requestcourses') {
 2136:                     $datatable .= '</tr><tr>';
 2137:                     foreach my $item (@usertools) {
 2138:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2139:                     }
 2140:                     $datatable .= '</tr></table>';
 2141:                 }
 2142:                 $datatable .= '</td>';
 2143:                 unless (($context eq 'requestcourses') ||
 2144:                         ($context eq 'requestauthor')) {
 2145:                     $datatable .= 
 2146:                               '<td class="LC_right_item">'.
 2147:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2148:                               '<input type="text" name="quota_'.$type.
 2149:                               '" value="'.$currdefquota.
 2150:                               '" size="5" /></span>'.('&nbsp;' x 2).
 2151:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2152:                               '<input type="text" name="authorquota_'.$type.
 2153:                               '" value="'.$currauthorquota.
 2154:                               '" size="5" /></span></td>';
 2155:                 }
 2156:                 $datatable .= '</tr>';
 2157:             }
 2158:         }
 2159:     }
 2160:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2161:         $defaultquota = '20';
 2162:         $authorquota = '500';
 2163:         if (ref($settings) eq 'HASH') {
 2164:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2165:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2166:             } elsif (defined($settings->{'default'})) {
 2167:                 $defaultquota = $settings->{'default'};
 2168:             }
 2169:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 2170:                 $authorquota = $settings->{'authorquota'}->{'default'};
 2171:             }
 2172:         }
 2173:     }
 2174:     $typecount ++;
 2175:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2176:     $datatable .= '<tr'.$css_class.'>'.
 2177:                   '<td>'.$othertitle.'</td>'.
 2178:                   '<td class="LC_left_item">';
 2179:     if ($context eq 'requestcourses') {
 2180:         $datatable .= '<table><tr>';
 2181:     }
 2182:     my %defcell;
 2183:     foreach my $item (@usertools) {
 2184:         if ($context eq 'requestcourses') {
 2185:             my ($curroption,$currlimit);
 2186:             if (ref($settings) eq 'HASH') {
 2187:                 if (ref($settings->{$item}) eq 'HASH') {
 2188:                     $curroption = $settings->{$item}->{'default'};
 2189:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2190:                         $currlimit = $1;
 2191:                     }
 2192:                 }
 2193:             }
 2194:             if (!$curroption) {
 2195:                 $curroption = 'norequest';
 2196:             }
 2197:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2198:             foreach my $option (@options) {
 2199:                 my $val = $option;
 2200:                 if ($option eq 'norequest') {
 2201:                     $val = 0;
 2202:                 }
 2203:                 if ($option eq 'validate') {
 2204:                     my $canvalidate = 0;
 2205:                     if (ref($validations{$item}) eq 'HASH') {
 2206:                         if ($validations{$item}{'default'}) {
 2207:                             $canvalidate = 1;
 2208:                         }
 2209:                     }
 2210:                     next if (!$canvalidate);
 2211:                 }
 2212:                 my $checked = '';
 2213:                 if ($option eq $curroption) {
 2214:                     $checked = ' checked="checked"';
 2215:                 } elsif ($option eq 'autolimit') {
 2216:                     if ($curroption =~ /^autolimit/) {
 2217:                         $checked = ' checked="checked"';
 2218:                     }
 2219:                 }
 2220:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2221:                                   '<input type="radio" name="crsreq_'.$item.
 2222:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2223:                                   $titles{$option}.'</label>';
 2224:                 if ($option eq 'autolimit') {
 2225:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2226:                                        $item.'_limit_default" size="1" '.
 2227:                                        'value="'.$currlimit.'" />';
 2228:                 }
 2229:                 $defcell{$item} .= '</span> ';
 2230:                 if ($option eq 'autolimit') {
 2231:                     $defcell{$item} .= $titles{'unlimited'};
 2232:                 }
 2233:             }
 2234:         } elsif ($context eq 'requestauthor') {
 2235:             my $curroption;
 2236:             if (ref($settings) eq 'HASH') {
 2237:                 $curroption = $settings->{'default'};
 2238:             }
 2239:             if (!$curroption) {
 2240:                 $curroption = 'norequest';
 2241:             }
 2242:             foreach my $option (@options) {
 2243:                 my $val = $option;
 2244:                 if ($option eq 'norequest') {
 2245:                     $val = 0;
 2246:                 }
 2247:                 my $checked = '';
 2248:                 if ($option eq $curroption) {
 2249:                     $checked = ' checked="checked"';
 2250:                 }
 2251:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2252:                               '<input type="radio" name="authorreq_default"'.
 2253:                               ' value="'.$val.'"'.$checked.' />'.
 2254:                               $titles{$option}.'</label></span>&nbsp; ';
 2255:             }
 2256:         } else {
 2257:             my $checked = 'checked="checked" ';
 2258:             if (ref($settings) eq 'HASH') {
 2259:                 if (ref($settings->{$item}) eq 'HASH') {
 2260:                     if ($settings->{$item}->{'default'} == 0) {
 2261:                         $checked = '';
 2262:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2263:                         $checked = 'checked="checked" ';
 2264:                     }
 2265:                 }
 2266:             }
 2267:             $datatable .= '<span class="LC_nobreak"><label>'.
 2268:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2269:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2270:                           '</label></span>&nbsp; ';
 2271:         }
 2272:     }
 2273:     if ($context eq 'requestcourses') {
 2274:         $datatable .= '</tr><tr>';
 2275:         foreach my $item (@usertools) {
 2276:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2277:         }
 2278:         $datatable .= '</tr></table>';
 2279:     }
 2280:     $datatable .= '</td>';
 2281:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2282:         $datatable .= '<td class="LC_right_item">'.
 2283:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2284:                       '<input type="text" name="defaultquota" value="'.
 2285:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2286:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2287:                       '<input type="text" name="authorquota" value="'.
 2288:                       $authorquota.'" size="5" /></span></td>';
 2289:     }
 2290:     $datatable .= '</tr>';
 2291:     $typecount ++;
 2292:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2293:     $datatable .= '<tr'.$css_class.'>'.
 2294:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2295:     if ($context eq 'requestcourses') {
 2296:         $datatable .= &mt('(overrides affiliation, if set)').
 2297:                       '</td>'.
 2298:                       '<td class="LC_left_item">'.
 2299:                       '<table><tr>';
 2300:     } else {
 2301:         $datatable .= &mt('(overrides affiliation, if checked)').
 2302:                       '</td>'.
 2303:                       '<td class="LC_left_item" colspan="2">'.
 2304:                       '<br />';
 2305:     }
 2306:     my %advcell;
 2307:     foreach my $item (@usertools) {
 2308:         if ($context eq 'requestcourses') {
 2309:             my ($curroption,$currlimit);
 2310:             if (ref($settings) eq 'HASH') {
 2311:                 if (ref($settings->{$item}) eq 'HASH') {
 2312:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2313:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2314:                         $currlimit = $1;
 2315:                     }
 2316:                 }
 2317:             }
 2318:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2319:             my $checked = '';
 2320:             if ($curroption eq '') {
 2321:                 $checked = ' checked="checked"';
 2322:             }
 2323:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2324:                                '<input type="radio" name="crsreq_'.$item.
 2325:                                '__LC_adv" value=""'.$checked.' />'.
 2326:                                &mt('No override set').'</label></span>&nbsp; ';
 2327:             foreach my $option (@options) {
 2328:                 my $val = $option;
 2329:                 if ($option eq 'norequest') {
 2330:                     $val = 0;
 2331:                 }
 2332:                 if ($option eq 'validate') {
 2333:                     my $canvalidate = 0;
 2334:                     if (ref($validations{$item}) eq 'HASH') {
 2335:                         if ($validations{$item}{'_LC_adv'}) {
 2336:                             $canvalidate = 1;
 2337:                         }
 2338:                     }
 2339:                     next if (!$canvalidate);
 2340:                 }
 2341:                 my $checked = '';
 2342:                 if ($val eq $curroption) {
 2343:                     $checked = ' checked="checked"';
 2344:                 } elsif ($option eq 'autolimit') {
 2345:                     if ($curroption =~ /^autolimit/) {
 2346:                         $checked = ' checked="checked"';
 2347:                     }
 2348:                 }
 2349:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2350:                                   '<input type="radio" name="crsreq_'.$item.
 2351:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2352:                                   $titles{$option}.'</label>';
 2353:                 if ($option eq 'autolimit') {
 2354:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2355:                                        $item.'_limit__LC_adv" size="1" '.
 2356:                                        'value="'.$currlimit.'" />';
 2357:                 }
 2358:                 $advcell{$item} .= '</span> ';
 2359:                 if ($option eq 'autolimit') {
 2360:                     $advcell{$item} .= $titles{'unlimited'};
 2361:                 }
 2362:             }
 2363:         } elsif ($context eq 'requestauthor') {
 2364:             my $curroption;
 2365:             if (ref($settings) eq 'HASH') {
 2366:                 $curroption = $settings->{'_LC_adv'};
 2367:             }
 2368:             my $checked = '';
 2369:             if ($curroption eq '') {
 2370:                 $checked = ' checked="checked"';
 2371:             }
 2372:             $datatable .= '<span class="LC_nobreak"><label>'.
 2373:                           '<input type="radio" name="authorreq__LC_adv"'.
 2374:                           ' value=""'.$checked.' />'.
 2375:                           &mt('No override set').'</label></span>&nbsp; ';
 2376:             foreach my $option (@options) {
 2377:                 my $val = $option;
 2378:                 if ($option eq 'norequest') {
 2379:                     $val = 0;
 2380:                 }
 2381:                 my $checked = '';
 2382:                 if ($val eq $curroption) {
 2383:                     $checked = ' checked="checked"';
 2384:                 }
 2385:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2386:                               '<input type="radio" name="authorreq__LC_adv"'.
 2387:                               ' value="'.$val.'"'.$checked.' />'.
 2388:                               $titles{$option}.'</label></span>&nbsp; ';
 2389:             }
 2390:         } else {
 2391:             my $checked = 'checked="checked" ';
 2392:             if (ref($settings) eq 'HASH') {
 2393:                 if (ref($settings->{$item}) eq 'HASH') {
 2394:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2395:                         $checked = '';
 2396:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2397:                         $checked = 'checked="checked" ';
 2398:                     }
 2399:                 }
 2400:             }
 2401:             $datatable .= '<span class="LC_nobreak"><label>'.
 2402:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2403:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2404:                           '</label></span>&nbsp; ';
 2405:         }
 2406:     }
 2407:     if ($context eq 'requestcourses') {
 2408:         $datatable .= '</tr><tr>';
 2409:         foreach my $item (@usertools) {
 2410:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2411:         }
 2412:         $datatable .= '</tr></table>';
 2413:     }
 2414:     $datatable .= '</td></tr>';
 2415:     $$rowtotal += $typecount;
 2416:     return $datatable;
 2417: }
 2418: 
 2419: sub print_requestmail {
 2420:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2421:     my ($now,$datatable,%currapp);
 2422:     $now = time;
 2423:     if (ref($settings) eq 'HASH') {
 2424:         if (ref($settings->{'notify'}) eq 'HASH') {
 2425:             if ($settings->{'notify'}{'approval'} ne '') {
 2426:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2427:             }
 2428:         }
 2429:     }
 2430:     my $numinrow = 2;
 2431:     my $css_class;
 2432:     if ($$rowtotal%2) {
 2433:         $css_class = 'LC_odd_row';
 2434:     }
 2435:     if ($customcss) {
 2436:         $css_class .= " $customcss";
 2437:     }
 2438:     $css_class =~ s/^\s+//;
 2439:     if ($css_class) {
 2440:         $css_class = ' class="'.$css_class.'"';
 2441:     }
 2442:     if ($rowstyle) {
 2443:         $css_class .= ' style="'.$rowstyle.'"';
 2444:     }
 2445:     my $text;
 2446:     if ($action eq 'requestcourses') {
 2447:         $text = &mt('Receive notification of course requests requiring approval');
 2448:     } elsif ($action eq 'requestauthor') {
 2449:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2450:     } else {
 2451:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2452:     }
 2453:     $datatable = '<tr'.$css_class.'>'.
 2454:                  ' <td>'.$text.'</td>'.
 2455:                  ' <td class="LC_left_item">';
 2456:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2457:                                                  $action.'notifyapproval',%currapp);
 2458:     if ($numdc > 0) {
 2459:         $datatable .= $table;
 2460:     } else {
 2461:         $datatable .= &mt('There are no active Domain Coordinators');
 2462:     }
 2463:     $datatable .='</td></tr>';
 2464:     return $datatable;
 2465: }
 2466: 
 2467: sub print_studentcode {
 2468:     my ($settings,$rowtotal) = @_;
 2469:     my $rownum = 0; 
 2470:     my ($output,%current);
 2471:     my @crstypes = ('official','unofficial','community','textbook','placement','lti');
 2472:     if (ref($settings) eq 'HASH') {
 2473:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2474:             foreach my $type (@crstypes) {
 2475:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2476:             }
 2477:         }
 2478:     }
 2479:     $output .= '<tr>'.
 2480:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2481:                '<td class="LC_left_item">';
 2482:     foreach my $type (@crstypes) {
 2483:         my $check = ' ';
 2484:         if ($current{$type}) {
 2485:             $check = ' checked="checked" ';
 2486:         }
 2487:         $output .= '<span class="LC_nobreak"><label>'.
 2488:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2489:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2490:     }
 2491:     $output .= '</td></tr>';
 2492:     $$rowtotal ++;
 2493:     return $output;
 2494: }
 2495: 
 2496: sub print_textbookcourses {
 2497:     my ($dom,$type,$settings,$rowtotal) = @_;
 2498:     my $rownum = 0;
 2499:     my $css_class;
 2500:     my $itemcount = 1;
 2501:     my $maxnum = 0;
 2502:     my $bookshash;
 2503:     if (ref($settings) eq 'HASH') {
 2504:         $bookshash = $settings->{$type};
 2505:     }
 2506:     my %ordered;
 2507:     if (ref($bookshash) eq 'HASH') {
 2508:         foreach my $item (keys(%{$bookshash})) {
 2509:             if (ref($bookshash->{$item}) eq 'HASH') {
 2510:                 my $num = $bookshash->{$item}{'order'};
 2511:                 $ordered{$num} = $item;
 2512:             }
 2513:         }
 2514:     }
 2515:     my $confname = $dom.'-domainconfig';
 2516:     my $switchserver = &check_switchserver($dom,$confname);
 2517:     my $maxnum = scalar(keys(%ordered));
 2518:     my $datatable;
 2519:     if (keys(%ordered)) {
 2520:         my @items = sort { $a <=> $b } keys(%ordered);
 2521:         for (my $i=0; $i<@items; $i++) {
 2522:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2523:             my $key = $ordered{$items[$i]};
 2524:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2525:             my $coursetitle = $coursehash{'description'};
 2526:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2527:             if (ref($bookshash->{$key}) eq 'HASH') {
 2528:                 $subject = $bookshash->{$key}->{'subject'};
 2529:                 $title = $bookshash->{$key}->{'title'};
 2530:                 if ($type eq 'textbooks') {
 2531:                     $publisher = $bookshash->{$key}->{'publisher'};
 2532:                     $author = $bookshash->{$key}->{'author'};
 2533:                     $image = $bookshash->{$key}->{'image'};
 2534:                     if ($image ne '') {
 2535:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2536:                         my $imagethumb = "$path/tn-".$imagefile;
 2537:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2538:                     }
 2539:                 }
 2540:             }
 2541:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2542:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2543:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2544:             for (my $k=0; $k<=$maxnum; $k++) {
 2545:                 my $vpos = $k+1;
 2546:                 my $selstr;
 2547:                 if ($k == $i) {
 2548:                     $selstr = ' selected="selected" ';
 2549:                 }
 2550:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2551:             }
 2552:             $datatable .= '</select>'.('&nbsp;'x2).
 2553:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2554:                 &mt('Delete?').'</label></span></td>'.
 2555:                 '<td colspan="2">'.
 2556:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2557:                 ('&nbsp;'x2).
 2558:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2559:             if ($type eq 'textbooks') {
 2560:                 $datatable .= ('&nbsp;'x2).
 2561:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2562:                               ('&nbsp;'x2).
 2563:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2564:                               ('&nbsp;'x2).
 2565:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2566:                 if ($image) {
 2567:                     $datatable .= $imgsrc.
 2568:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2569:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2570:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2571:                 }
 2572:                 if ($switchserver) {
 2573:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2574:                 } else {
 2575:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2576:                 }
 2577:             }
 2578:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2579:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2580:                           $coursetitle.'</span></td></tr>'."\n";
 2581:             $itemcount ++;
 2582:         }
 2583:     }
 2584:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2585:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2586:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2587:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2588:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2589:     for (my $k=0; $k<$maxnum+1; $k++) {
 2590:         my $vpos = $k+1;
 2591:         my $selstr;
 2592:         if ($k == $maxnum) {
 2593:             $selstr = ' selected="selected" ';
 2594:         }
 2595:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2596:     }
 2597:     $datatable .= '</select>&nbsp;'."\n".
 2598:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 2599:                   '<td colspan="2">'.
 2600:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2601:                   ('&nbsp;'x2).
 2602:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2603:                   ('&nbsp;'x2);
 2604:     if ($type eq 'textbooks') {
 2605:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2606:                       ('&nbsp;'x2).
 2607:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2608:                       ('&nbsp;'x2).
 2609:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2610:         if ($switchserver) {
 2611:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2612:         } else {
 2613:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2614:         }
 2615:         $datatable .= '</span>'."\n";
 2616:     }
 2617:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2618:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2619:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2620:                   &Apache::loncommon::selectcourse_link
 2621:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 2622:                   '</span></td>'."\n".
 2623:                   '</tr>'."\n";
 2624:     $itemcount ++;
 2625:     return $datatable;
 2626: }
 2627: 
 2628: sub textbookcourses_javascript {
 2629:     my ($settings) = @_;
 2630:     return unless(ref($settings) eq 'HASH');
 2631:     my (%ordered,%total,%jstext);
 2632:     foreach my $type ('textbooks','templates') {
 2633:         $total{$type} = 0;
 2634:         if (ref($settings->{$type}) eq 'HASH') {
 2635:             foreach my $item (keys(%{$settings->{$type}})) {
 2636:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2637:                     my $num = $settings->{$type}->{$item}{'order'};
 2638:                     $ordered{$type}{$num} = $item;
 2639:                 }
 2640:             }
 2641:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2642:         }
 2643:         my @jsarray = ();
 2644:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2645:             push(@jsarray,$ordered{$type}{$item});
 2646:         }
 2647:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2648:     }
 2649:     return <<"ENDSCRIPT";
 2650: <script type="text/javascript">
 2651: // <![CDATA[
 2652: function reorderBooks(form,item,caller) {
 2653:     var changedVal;
 2654: $jstext{'textbooks'};
 2655: $jstext{'templates'};
 2656:     var newpos;
 2657:     var maxh;
 2658:     if (caller == 'textbooks') {  
 2659:         newpos = 'textbooks_addbook_pos';
 2660:         maxh = 1 + $total{'textbooks'};
 2661:     } else {
 2662:         newpos = 'templates_addbook_pos';
 2663:         maxh = 1 + $total{'templates'};
 2664:     }
 2665:     var current = new Array;
 2666:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2667:     if (item == newpos) {
 2668:         changedVal = newitemVal;
 2669:     } else {
 2670:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2671:         current[newitemVal] = newpos;
 2672:     }
 2673:     if (caller == 'textbooks') {
 2674:         for (var i=0; i<textbooks.length; i++) {
 2675:             var elementName = 'textbooks_'+textbooks[i];
 2676:             if (elementName != item) {
 2677:                 if (form.elements[elementName]) {
 2678:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2679:                     current[currVal] = elementName;
 2680:                 }
 2681:             }
 2682:         }
 2683:     }
 2684:     if (caller == 'templates') {
 2685:         for (var i=0; i<templates.length; i++) {
 2686:             var elementName = 'templates_'+templates[i];
 2687:             if (elementName != item) {
 2688:                 if (form.elements[elementName]) {
 2689:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2690:                     current[currVal] = elementName;
 2691:                 }
 2692:             }
 2693:         }
 2694:     }
 2695:     var oldVal;
 2696:     for (var j=0; j<maxh; j++) {
 2697:         if (current[j] == undefined) {
 2698:             oldVal = j;
 2699:         }
 2700:     }
 2701:     if (oldVal < changedVal) {
 2702:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2703:            var elementName = current[k];
 2704:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2705:         }
 2706:     } else {
 2707:         for (var k=changedVal; k<oldVal; k++) {
 2708:             var elementName = current[k];
 2709:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2710:         }
 2711:     }
 2712:     return;
 2713: }
 2714: 
 2715: // ]]>
 2716: </script>
 2717: 
 2718: ENDSCRIPT
 2719: }
 2720: 
 2721: sub ltitools_javascript {
 2722:     my ($settings) = @_;
 2723:     my $togglejs = &ltitools_toggle_js();
 2724:     unless (ref($settings) eq 'HASH') {
 2725:         return $togglejs;
 2726:     }
 2727:     my (%ordered,$total,%jstext);
 2728:     $total = 0;
 2729:     foreach my $item (keys(%{$settings})) {
 2730:         if (ref($settings->{$item}) eq 'HASH') {
 2731:             my $num = $settings->{$item}{'order'};
 2732:             $ordered{$num} = $item;
 2733:         }
 2734:     }
 2735:     $total = scalar(keys(%{$settings}));
 2736:     my @jsarray = ();
 2737:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2738:         push(@jsarray,$ordered{$item});
 2739:     }
 2740:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2741:     return <<"ENDSCRIPT";
 2742: <script type="text/javascript">
 2743: // <![CDATA[
 2744: function reorderLTITools(form,item) {
 2745:     var changedVal;
 2746: $jstext
 2747:     var newpos = 'ltitools_add_pos';
 2748:     var maxh = 1 + $total;
 2749:     var current = new Array;
 2750:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2751:     if (item == newpos) {
 2752:         changedVal = newitemVal;
 2753:     } else {
 2754:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2755:         current[newitemVal] = newpos;
 2756:     }
 2757:     for (var i=0; i<ltitools.length; i++) {
 2758:         var elementName = 'ltitools_'+ltitools[i];
 2759:         if (elementName != item) {
 2760:             if (form.elements[elementName]) {
 2761:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2762:                 current[currVal] = elementName;
 2763:             }
 2764:         }
 2765:     }
 2766:     var oldVal;
 2767:     for (var j=0; j<maxh; j++) {
 2768:         if (current[j] == undefined) {
 2769:             oldVal = j;
 2770:         }
 2771:     }
 2772:     if (oldVal < changedVal) {
 2773:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2774:            var elementName = current[k];
 2775:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2776:         }
 2777:     } else {
 2778:         for (var k=changedVal; k<oldVal; k++) {
 2779:             var elementName = current[k];
 2780:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2781:         }
 2782:     }
 2783:     return;
 2784: }
 2785: 
 2786: // ]]>
 2787: </script>
 2788: 
 2789: $togglejs
 2790: 
 2791: ENDSCRIPT
 2792: }
 2793: 
 2794: sub ltitools_toggle_js {
 2795:     return <<"ENDSCRIPT";
 2796: <script type="text/javascript">
 2797: // <![CDATA[
 2798: 
 2799: function toggleLTITools(form,setting,item) {
 2800:     var radioname = '';
 2801:     var divid = '';
 2802:     if ((setting == 'passback') || (setting == 'roster')) {
 2803:         radioname = 'ltitools_'+setting+'_'+item;
 2804:         divid = 'ltitools_'+setting+'time_'+item;
 2805:         var num = form.elements[radioname].length;
 2806:         if (num) {
 2807:             var setvis = '';
 2808:             for (var i=0; i<num; i++) {
 2809:                 if (form.elements[radioname][i].checked) {
 2810:                     if (form.elements[radioname][i].value == '1') {
 2811:                         if (document.getElementById(divid)) {
 2812:                             document.getElementById(divid).style.display = 'inline-block';
 2813:                         }
 2814:                         setvis = 1;
 2815:                     }
 2816:                     break;
 2817:                 }
 2818:             }
 2819:         }
 2820:         if (!setvis) {
 2821:             if (document.getElementById(divid)) {
 2822:                 document.getElementById(divid).style.display = 'none';
 2823:             }
 2824:         }
 2825:     }
 2826:     if (setting == 'user') {
 2827:         divid = 'ltitools_'+setting+'_div_'+item;
 2828:         var checkid = 'ltitools_'+setting+'_field_'+item;
 2829:         if (document.getElementById(divid)) {
 2830:             if (document.getElementById(checkid)) {
 2831:                 if (document.getElementById(checkid).checked) {
 2832:                     document.getElementById(divid).style.display = 'inline-block';
 2833:                 } else {
 2834:                     document.getElementById(divid).style.display = 'none';
 2835:                 }
 2836:             }
 2837:         }
 2838:     }
 2839:     return;
 2840: }
 2841: // ]]>
 2842: </script>
 2843: 
 2844: ENDSCRIPT
 2845: }
 2846: 
 2847: sub proctoring_javascript {
 2848:     my ($settings) = @_;
 2849:     my (%ordered,$total,%jstext);
 2850:     $total = 0;
 2851:     if (ref($settings) eq 'HASH') {
 2852:         foreach my $item (keys(%{$settings})) {
 2853:             if (ref($settings->{$item}) eq 'HASH') {
 2854:                 my $num = $settings->{$item}{'order'};
 2855:                 $ordered{$num} = $item;
 2856:             }
 2857:         }
 2858:         $total = scalar(keys(%{$settings}));
 2859:     } else {
 2860:         %ordered = (
 2861:                        0 => 'proctorio',
 2862:                        1 => 'examity',
 2863:                    );
 2864:         $total = 2; 
 2865:     }
 2866:     my @jsarray = ();
 2867:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2868:         push(@jsarray,$ordered{$item});
 2869:     }
 2870:     my $jstext = '    var proctors = Array('."'".join("','",@jsarray)."'".');'."\n";
 2871:     return <<"ENDSCRIPT";
 2872: <script type="text/javascript">
 2873: // <![CDATA[
 2874: function reorderProctoring(form,item) {
 2875:     var changedVal;
 2876: $jstext
 2877:     var maxh = $total;
 2878:     var current = new Array;
 2879:     var changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2880:     for (var i=0; i<proctors.length; i++) {
 2881:         var elementName = 'proctoring_pos_'+proctors[i];
 2882:         if (elementName != item) {
 2883:             if (form.elements[elementName]) {
 2884:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2885:                 current[currVal] = elementName;
 2886:             }
 2887:         }
 2888:     }
 2889:     var oldVal;
 2890:     for (var j=0; j<maxh; j++) {
 2891:         if (current[j] == undefined) {
 2892:             oldVal = j;
 2893:         }
 2894:     }
 2895:     if (oldVal < changedVal) {
 2896:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2897:            var elementName = current[k];
 2898:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2899:         }
 2900:     } else {
 2901:         for (var k=changedVal; k<oldVal; k++) {
 2902:             var elementName = current[k];
 2903:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2904:         }
 2905:     }
 2906:     return;
 2907: }
 2908: 
 2909: function toggleProctoring(form,item) {
 2910:     var fieldsets = document.getElementsByClassName('proctoring_'+item);
 2911:     if (fieldsets.length) {
 2912:         var radioname = 'proctoring_available_'+item;
 2913:         var num = form.elements[radioname].length;
 2914:         if (num) {
 2915:             var setvis = '';
 2916:             for (var i=0; i<num; i++) {
 2917:                 if (form.elements[radioname][i].checked) {
 2918:                     if (form.elements[radioname][i].value == '1') {
 2919:                        setvis = 1;
 2920:                        break;
 2921:                     }
 2922:                 }
 2923:             }
 2924:             for (var j=0; j<fieldsets.length; j++) {
 2925:                 if (setvis) {
 2926:                     fieldsets[j].style.display = 'block';
 2927:                 } else {
 2928:                     fieldsets[j].style.display = 'none';
 2929:                 }
 2930:             }
 2931:         }
 2932:     }
 2933:     return;
 2934: }
 2935: 
 2936: // ]]>
 2937: </script>
 2938: 
 2939: ENDSCRIPT
 2940: }
 2941: 
 2942: 
 2943: sub lti_javascript {
 2944:     my ($settings) = @_;
 2945:     my $togglejs = &lti_toggle_js();
 2946:     unless (ref($settings) eq 'HASH') {
 2947:         return $togglejs;
 2948:     }
 2949:     my (%ordered,$total,%jstext);
 2950:     $total = 0;
 2951:     foreach my $item (keys(%{$settings})) {
 2952:         if (ref($settings->{$item}) eq 'HASH') {
 2953:             my $num = $settings->{$item}{'order'};
 2954:             $ordered{$num} = $item;
 2955:         }
 2956:     }
 2957:     $total = scalar(keys(%{$settings}));
 2958:     my @jsarray = ();
 2959:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2960:         push(@jsarray,$ordered{$item});
 2961:     }
 2962:     my $jstext = '    var lti = Array('."'".join("','",@jsarray)."'".');'."\n";
 2963:     return <<"ENDSCRIPT";
 2964: <script type="text/javascript">
 2965: // <![CDATA[
 2966: function reorderLTI(form,item) {
 2967:     var changedVal;
 2968: $jstext
 2969:     var newpos = 'lti_pos_add';
 2970:     var maxh = 1 + $total;
 2971:     var current = new Array;
 2972:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2973:     if (item == newpos) {
 2974:         changedVal = newitemVal;
 2975:     } else {
 2976:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2977:         current[newitemVal] = newpos;
 2978:     }
 2979:     for (var i=0; i<lti.length; i++) {
 2980:         var elementName = 'lti_pos_'+lti[i];
 2981:         if (elementName != item) {
 2982:             if (form.elements[elementName]) {
 2983:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2984:                 current[currVal] = elementName;
 2985:             }
 2986:         }
 2987:     }
 2988:     var oldVal;
 2989:     for (var j=0; j<maxh; j++) {
 2990:         if (current[j] == undefined) {
 2991:             oldVal = j;
 2992:         }
 2993:     }
 2994:     if (oldVal < changedVal) {
 2995:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2996:            var elementName = current[k];
 2997:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2998:         }
 2999:     } else {
 3000:         for (var k=changedVal; k<oldVal; k++) {
 3001:             var elementName = current[k];
 3002:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3003:         }
 3004:     }
 3005:     return;
 3006: }
 3007: // ]]>
 3008: </script>
 3009: 
 3010: $togglejs
 3011: 
 3012: ENDSCRIPT
 3013: }
 3014: 
 3015: sub lti_toggle_js {
 3016:     my %lcauthparmtext = &Apache::lonlocal::texthash (
 3017:                             localauth => 'Local auth argument',
 3018:                             krb       => 'Kerberos domain',
 3019:                          );
 3020:     return <<"ENDSCRIPT";
 3021: <script type="text/javascript">
 3022: // <![CDATA[
 3023: 
 3024: function toggleLTI(form,setting,item) {
 3025:     if (setting == 'requser') {
 3026:         var fieldsets = document.getElementsByClassName('ltioption_'+item);
 3027:         if (fieldsets.length) {
 3028:             var radioname = 'lti_'+setting+'_'+item;
 3029:             var num = form.elements[radioname].length;
 3030:             if (num) {
 3031:                 var setvis = '';
 3032:                 for (var i=0; i<num; i++) {
 3033:                     if (form.elements[radioname][i].checked) {
 3034:                         if (form.elements[radioname][i].value == '1') {
 3035:                            setvis = 1;
 3036:                            break;
 3037:                         }
 3038:                     }
 3039:                 }
 3040:                 for (var j=0; j<fieldsets.length; j++) {
 3041:                     if (setvis) {
 3042:                         fieldsets[j].style.display = 'block';
 3043:                     } else {
 3044:                         fieldsets[j].style.display = 'none';
 3045:                     }
 3046:                 }
 3047:             }
 3048:         }
 3049:     } else if ((setting == 'user') || (setting == 'crs') || (setting == 'passback') || (setting == 'callback')) {
 3050:         var radioname = '';
 3051:         var divid = '';
 3052:         if (setting == 'user') {
 3053:             radioname = 'lti_mapuser_'+item;
 3054:             divid = 'lti_userfield_'+item;
 3055:         } else if (setting == 'crs') {
 3056:             radioname = 'lti_mapcrs_'+item;
 3057:             divid = 'lti_crsfield_'+item;
 3058:         } else if (setting == 'callback') {
 3059:             radioname = 'lti_callback_'+item;
 3060:             divid = 'lti_callbackfield_'+item;
 3061:         } else {
 3062:             radioname = 'lti_passback_'+item;
 3063:             divid =  'lti_passback_'+item;
 3064:         }
 3065:         var num = form.elements[radioname].length;
 3066:         if (num) {
 3067:             var setvis = '';
 3068:             for (var i=0; i<num; i++) {
 3069:                if (form.elements[radioname][i].checked) {
 3070:                    if ((setting == 'passback') || (setting == 'callback')) {
 3071:                        if (form.elements[radioname][i].value == '1') {
 3072:                            if (document.getElementById(divid)) {
 3073:                                document.getElementById(divid).style.display = 'inline-block';
 3074:                            }
 3075:                            setvis = 1;
 3076:                            break;
 3077:                        }
 3078:                    } else {
 3079:                        if (form.elements[radioname][i].value == 'other') {
 3080:                            if (document.getElementById(divid)) {
 3081:                                document.getElementById(divid).style.display = 'inline-block';
 3082:                            }
 3083:                            setvis = 1;
 3084:                            break;
 3085:                        }
 3086:                    }
 3087:                } 
 3088:             }
 3089:             if (!setvis) {
 3090:                 if (document.getElementById(divid)) {
 3091:                     document.getElementById(divid).style.display = 'none';
 3092:                 }
 3093:             }
 3094:         }
 3095:     } else if ((setting == 'sec') || (setting == 'secsrc')) {
 3096:         var numsec = form.elements['lti_crssec_'+item].length;
 3097:         if (numsec) {
 3098:             var setvis = '';
 3099:             for (var i=0; i<numsec; i++) {
 3100:                 if (form.elements['lti_crssec_'+item][i].checked) {
 3101:                     if (form.elements['lti_crssec_'+item][i].value == '1') {
 3102:                         if (document.getElementById('lti_crssecfield_'+item)) {
 3103:                             document.getElementById('lti_crssecfield_'+item).style.display = 'inline-block';
 3104:                             setvis = 1;
 3105:                             var numsrcsec = form.elements['lti_crssecsrc_'+item].length;
 3106:                             if (numsrcsec) {
 3107:                                 var setsrcvis = '';
 3108:                                 for (var j=0; j<numsrcsec; j++) {
 3109:                                     if (form.elements['lti_crssecsrc_'+item][j].checked) {
 3110:                                         if (form.elements['lti_crssecsrc_'+item][j].value == 'other') {
 3111:                                             if (document.getElementById('lti_secsrcfield_'+item)) {
 3112:                                                 document.getElementById('lti_secsrcfield_'+item).style.display = 'inline-block';
 3113:                                                 setsrcvis = 1;
 3114:                                             }
 3115:                                         }
 3116:                                     }
 3117:                                 }
 3118:                                 if (!setsrcvis) {
 3119:                                     if (document.getElementById('lti_secsrcfield_'+item)) {
 3120:                                         document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3121:                                     }
 3122:                                 }
 3123:                             }
 3124:                         }
 3125:                     }
 3126:                 }
 3127:             }
 3128:             if (!setvis) {
 3129:                 if (document.getElementById('lti_crssecfield_'+item)) {
 3130:                     document.getElementById('lti_crssecfield_'+item).style.display = 'none';
 3131:                 }
 3132:                 if (document.getElementById('lti_secsrcfield_'+item)) {
 3133:                     document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3134:                 }
 3135:             }
 3136:         }
 3137:     } else if (setting == 'lcauth') {
 3138:         var numauth = form.elements['lti_lcauth_'+item].length;
 3139:         if (numauth) {
 3140:             for (var i=0; i<numauth; i++) {
 3141:                 if (form.elements['lti_lcauth_'+item][i].checked) {
 3142:                     if (document.getElementById('lti_'+setting+'_parmrow_'+item)) {
 3143:                         if ((form.elements['lti_'+setting+'_'+item][i].value == 'internal') || (form.elements['lti_'+setting+'_'+item][i].value == 'lti')) {
 3144:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'none';
 3145:                         } else {
 3146:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'table-row';
 3147:                             if (document.getElementById('lti_'+setting+'_parmtext_'+item)) {
 3148:                                 if (form.elements['lti_'+setting+'_'+item][i].value == 'localauth') {
 3149:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'localauth'}";
 3150:                                 } else {
 3151:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'krb'}";
 3152:                                 }
 3153:                             }
 3154:                         }
 3155:                     }
 3156:                 }
 3157:             }
 3158:         }
 3159:     } else if (setting == 'lcmenu') {
 3160:         var menus = new Array('lti_topmenu_'+item,'lti_inlinemenu_'+item);
 3161:         var divid = 'lti_menufield_'+item;
 3162:         var setvis = '';
 3163:         for (var i=0; i<menus.length; i++) {
 3164:             var radioname = menus[i];  
 3165:             var num = form.elements[radioname].length;
 3166:             if (num) {
 3167:                 for (var j=0; j<num; j++) {
 3168:                     if (form.elements[radioname][j].checked) {
 3169:                         if (form.elements[radioname][j].value == '1') {
 3170:                             if (document.getElementById(divid)) {
 3171:                                 document.getElementById(divid).style.display = 'inline-block';
 3172:                             }
 3173:                             setvis = 1;
 3174:                             break;
 3175:                         }
 3176:                     }
 3177:                 }
 3178:             }
 3179:             if (setvis == 1) {
 3180:                 break;
 3181:             }
 3182:         }
 3183:         if (!setvis) {
 3184:             if (document.getElementById(divid)) {
 3185:                 document.getElementById(divid).style.display = 'none';
 3186:             }
 3187:         }
 3188:     }
 3189:     return;
 3190: }
 3191: // ]]>
 3192: </script>
 3193: 
 3194: ENDSCRIPT
 3195: }
 3196: 
 3197: sub print_autoenroll {
 3198:     my ($dom,$settings,$rowtotal) = @_;
 3199:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 3200:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 3201:     if (ref($settings) eq 'HASH') {
 3202:         if (exists($settings->{'run'})) {
 3203:             if ($settings->{'run'} eq '0') {
 3204:                 $runoff = ' checked="checked" ';
 3205:                 $runon = ' ';
 3206:             } else {
 3207:                 $runon = ' checked="checked" ';
 3208:                 $runoff = ' ';
 3209:             }
 3210:         } else {
 3211:             if ($autorun) {
 3212:                 $runon = ' checked="checked" ';
 3213:                 $runoff = ' ';
 3214:             } else {
 3215:                 $runoff = ' checked="checked" ';
 3216:                 $runon = ' ';
 3217:             }
 3218:         }
 3219:         if (exists($settings->{'co-owners'})) {
 3220:             if ($settings->{'co-owners'} eq '0') {
 3221:                 $coownersoff = ' checked="checked" ';
 3222:                 $coownerson = ' ';
 3223:             } else {
 3224:                 $coownerson = ' checked="checked" ';
 3225:                 $coownersoff = ' ';
 3226:             }
 3227:         } else {
 3228:             $coownersoff = ' checked="checked" ';
 3229:             $coownerson = ' ';
 3230:         }
 3231:         if (exists($settings->{'sender_domain'})) {
 3232:             $defdom = $settings->{'sender_domain'};
 3233:         }
 3234:         if (exists($settings->{'autofailsafe'})) {
 3235:             $failsafe = $settings->{'autofailsafe'};
 3236:         }
 3237:     } else {
 3238:         if ($autorun) {
 3239:             $runon = ' checked="checked" ';
 3240:             $runoff = ' ';
 3241:         } else {
 3242:             $runoff = ' checked="checked" ';
 3243:             $runon = ' ';
 3244:         }
 3245:     }
 3246:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 3247:     my $notif_sender;
 3248:     if (ref($settings) eq 'HASH') {
 3249:         $notif_sender = $settings->{'sender_uname'};
 3250:     }
 3251:     my $datatable='<tr class="LC_odd_row">'.
 3252:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 3253:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3254:                   '<input type="radio" name="autoenroll_run"'.
 3255:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3256:                   '<label><input type="radio" name="autoenroll_run"'.
 3257:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3258:                   '</tr><tr>'.
 3259:                   '<td>'.&mt('Notification messages - sender').
 3260:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 3261:                   &mt('username').':&nbsp;'.
 3262:                   '<input type="text" name="sender_uname" value="'.
 3263:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 3264:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 3265:                   '<tr class="LC_odd_row">'.
 3266:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 3267:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3268:                   '<input type="radio" name="autoassign_coowners"'.
 3269:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3270:                   '<label><input type="radio" name="autoassign_coowners"'.
 3271:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3272:                   '</tr><tr>'.
 3273:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 3274:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3275:                   '<input type="text" name="autoenroll_failsafe"'.
 3276:                   ' value="'.$failsafe.'" size="4" /></span></td></tr>';
 3277:     $$rowtotal += 4;
 3278:     return $datatable;
 3279: }
 3280: 
 3281: sub print_autoupdate {
 3282:     my ($position,$dom,$settings,$rowtotal) = @_;
 3283:     my $datatable;
 3284:     if ($position eq 'top') {
 3285:         my $updateon = ' ';
 3286:         my $updateoff = ' checked="checked" ';
 3287:         my $classlistson = ' ';
 3288:         my $classlistsoff = ' checked="checked" ';
 3289:         if (ref($settings) eq 'HASH') {
 3290:             if ($settings->{'run'} eq '1') {
 3291:                 $updateon = $updateoff;
 3292:                 $updateoff = ' ';
 3293:             }
 3294:             if ($settings->{'classlists'} eq '1') {
 3295:                 $classlistson = $classlistsoff;
 3296:                 $classlistsoff = ' ';
 3297:             }
 3298:         }
 3299:         my %title = (
 3300:                    run => 'Auto-update active?',
 3301:                    classlists => 'Update information in classlists?',
 3302:                     );
 3303:         $datatable = '<tr class="LC_odd_row">'. 
 3304:                   '<td>'.&mt($title{'run'}).'</td>'.
 3305:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3306:                   '<input type="radio" name="autoupdate_run"'.
 3307:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3308:                   '<label><input type="radio" name="autoupdate_run"'.
 3309:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 3310:                   '</tr><tr>'.
 3311:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 3312:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3313:                   '<label><input type="radio" name="classlists"'.
 3314:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3315:                   '<label><input type="radio" name="classlists"'.
 3316:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 3317:                   '</tr>';
 3318:         $$rowtotal += 2;
 3319:     } elsif ($position eq 'middle') {
 3320:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3321:         my $numinrow = 3;
 3322:         my $locknamesettings;
 3323:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 3324:                                      $dom,$numinrow,$othertitle,
 3325:                                     'lockablenames',$rowtotal);
 3326:         $$rowtotal ++;
 3327:     } else {
 3328:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3329:         my @fields = ('lastname','firstname','middlename','generation',
 3330:                       'permanentemail','id');
 3331:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 3332:         my $numrows = 0;
 3333:         if (ref($types) eq 'ARRAY') {
 3334:             if (@{$types} > 0) {
 3335:                 $datatable = 
 3336:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 3337:                                          \@fields,$types,\$numrows);
 3338:                     $$rowtotal += @{$types}; 
 3339:             }
 3340:         }
 3341:         $datatable .= 
 3342:             &usertype_update_row($settings,{'default' => $othertitle},
 3343:                                  \%fieldtitles,\@fields,['default'],
 3344:                                  \$numrows);
 3345:         $$rowtotal ++;     
 3346:     }
 3347:     return $datatable;
 3348: }
 3349: 
 3350: sub print_autocreate {
 3351:     my ($dom,$settings,$rowtotal) = @_;
 3352:     my (%createon,%createoff,%currhash);
 3353:     my @types = ('xml','req');
 3354:     if (ref($settings) eq 'HASH') {
 3355:         foreach my $item (@types) {
 3356:             $createoff{$item} = ' checked="checked" ';
 3357:             $createon{$item} = ' ';
 3358:             if (exists($settings->{$item})) {
 3359:                 if ($settings->{$item}) {
 3360:                     $createon{$item} = ' checked="checked" ';
 3361:                     $createoff{$item} = ' ';
 3362:                 }
 3363:             }
 3364:         }
 3365:         if ($settings->{'xmldc'} ne '') {
 3366:             $currhash{$settings->{'xmldc'}} = 1;
 3367:         }
 3368:     } else {
 3369:         foreach my $item (@types) {
 3370:             $createoff{$item} = ' checked="checked" ';
 3371:             $createon{$item} = ' ';
 3372:         }
 3373:     }
 3374:     $$rowtotal += 2;
 3375:     my $numinrow = 2;
 3376:     my $datatable='<tr class="LC_odd_row">'.
 3377:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 3378:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3379:                   '<input type="radio" name="autocreate_xml"'.
 3380:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3381:                   '<label><input type="radio" name="autocreate_xml"'.
 3382:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 3383:                   '</td></tr><tr>'.
 3384:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 3385:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3386:                   '<input type="radio" name="autocreate_req"'.
 3387:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3388:                   '<label><input type="radio" name="autocreate_req"'.
 3389:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 3390:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3391:                                                    'autocreate_xmldc',%currhash);
 3392:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 3393:     if ($numdc > 1) {
 3394:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 3395:                       '</td><td class="LC_left_item">';
 3396:     } else {
 3397:         $datatable .= &mt('Course creation processed as:').
 3398:                       '</td><td class="LC_right_item">';
 3399:     }
 3400:     $datatable .= $dctable.'</td></tr>';
 3401:     $$rowtotal += $rows;
 3402:     return $datatable;
 3403: }
 3404: 
 3405: sub print_directorysrch {
 3406:     my ($position,$dom,$settings,$rowtotal) = @_;
 3407:     my $datatable;
 3408:     if ($position eq 'top') {
 3409:         my $instsrchon = ' ';
 3410:         my $instsrchoff = ' checked="checked" ';
 3411:         my ($exacton,$containson,$beginson);
 3412:         my $instlocalon = ' ';
 3413:         my $instlocaloff = ' checked="checked" ';
 3414:         if (ref($settings) eq 'HASH') {
 3415:             if ($settings->{'available'} eq '1') {
 3416:                 $instsrchon = $instsrchoff;
 3417:                 $instsrchoff = ' ';
 3418:             }
 3419:             if ($settings->{'localonly'} eq '1') {
 3420:                 $instlocalon = $instlocaloff;
 3421:                 $instlocaloff = ' ';
 3422:             }
 3423:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 3424:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 3425:                     if ($type eq 'exact') {
 3426:                         $exacton = ' checked="checked" ';
 3427:                     } elsif ($type eq 'contains') {
 3428:                         $containson = ' checked="checked" ';
 3429:                     } elsif ($type eq 'begins') {
 3430:                         $beginson = ' checked="checked" ';
 3431:                     }
 3432:                 }
 3433:             } else {
 3434:                 if ($settings->{'searchtypes'} eq 'exact') {
 3435:                     $exacton = ' checked="checked" ';
 3436:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 3437:                     $containson = ' checked="checked" ';
 3438:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 3439:                     $exacton = ' checked="checked" ';
 3440:                     $containson = ' checked="checked" ';
 3441:                 }
 3442:             }
 3443:         }
 3444:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 3445:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3446: 
 3447:         my $numinrow = 4;
 3448:         my $cansrchrow = 0;
 3449:         $datatable='<tr class="LC_odd_row">'.
 3450:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 3451:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3452:                    '<input type="radio" name="dirsrch_available"'.
 3453:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3454:                    '<label><input type="radio" name="dirsrch_available"'.
 3455:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3456:                    '</tr><tr>'.
 3457:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 3458:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3459:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 3460:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3461:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 3462:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3463:                    '</tr>';
 3464:         $$rowtotal += 2;
 3465:         if (ref($usertypes) eq 'HASH') {
 3466:             if (keys(%{$usertypes}) > 0) {
 3467:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3468:                                              $numinrow,$othertitle,'cansearch',
 3469:                                              $rowtotal);
 3470:                 $cansrchrow = 1;
 3471:             }
 3472:         }
 3473:         if ($cansrchrow) {
 3474:             $$rowtotal ++;
 3475:             $datatable .= '<tr>';
 3476:         } else {
 3477:             $datatable .= '<tr class="LC_odd_row">';
 3478:         }
 3479:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 3480:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 3481:         foreach my $title (@{$titleorder}) {
 3482:             if (defined($searchtitles->{$title})) {
 3483:                 my $check = ' ';
 3484:                 if (ref($settings) eq 'HASH') {
 3485:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 3486:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 3487:                             $check = ' checked="checked" ';
 3488:                         }
 3489:                     }
 3490:                 }
 3491:                 $datatable .= '<td class="LC_left_item">'.
 3492:                               '<span class="LC_nobreak"><label>'.
 3493:                               '<input type="checkbox" name="searchby" '.
 3494:                               'value="'.$title.'"'.$check.'/>'.
 3495:                               $searchtitles->{$title}.'</label></span></td>';
 3496:             }
 3497:         }
 3498:         $datatable .= '</tr></table></td></tr>';
 3499:         $$rowtotal ++;
 3500:         if ($cansrchrow) {
 3501:             $datatable .= '<tr class="LC_odd_row">';
 3502:         } else {
 3503:             $datatable .= '<tr>';
 3504:         }
 3505:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 3506:                       '<td class="LC_left_item" colspan="2">'.
 3507:                       '<span class="LC_nobreak"><label>'.
 3508:                       '<input type="checkbox" name="searchtypes" '.
 3509:                       $exacton.' value="exact" />'.&mt('Exact match').
 3510:                       '</label>&nbsp;'.
 3511:                       '<label><input type="checkbox" name="searchtypes" '.
 3512:                       $beginson.' value="begins" />'.&mt('Begins with').
 3513:                       '</label>&nbsp;'.
 3514:                       '<label><input type="checkbox" name="searchtypes" '.
 3515:                       $containson.' value="contains" />'.&mt('Contains').
 3516:                       '</label></span></td></tr>';
 3517:         $$rowtotal ++;
 3518:     } else {
 3519:         my $domsrchon = ' checked="checked" ';
 3520:         my $domsrchoff = ' ';
 3521:         my $domlocalon = ' ';
 3522:         my $domlocaloff = ' checked="checked" ';
 3523:         if (ref($settings) eq 'HASH') {
 3524:             if ($settings->{'lclocalonly'} eq '1') {
 3525:                 $domlocalon = $domlocaloff;
 3526:                 $domlocaloff = ' ';
 3527:             }
 3528:             if ($settings->{'lcavailable'} eq '0') {
 3529:                 $domsrchoff = $domsrchon;
 3530:                 $domsrchon = ' ';
 3531:             }
 3532:         }
 3533:         $datatable='<tr class="LC_odd_row">'.
 3534:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 3535:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3536:                       '<input type="radio" name="dirsrch_domavailable"'.
 3537:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3538:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 3539:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3540:                       '</tr><tr>'.
 3541:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 3542:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3543:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 3544:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3545:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 3546:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3547:                       '</tr>';
 3548:         $$rowtotal += 2;
 3549:     }
 3550:     return $datatable;
 3551: }
 3552: 
 3553: sub print_contacts {
 3554:     my ($position,$dom,$settings,$rowtotal) = @_;
 3555:     my $datatable;
 3556:     my @contacts = ('adminemail','supportemail');
 3557:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 3558:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings,%lonstatus);
 3559:     if ($position eq 'top') {
 3560:         if (ref($settings) eq 'HASH') {
 3561:             foreach my $item (@contacts) {
 3562:                 if (exists($settings->{$item})) {
 3563:                     $to{$item} = $settings->{$item};
 3564:                 }
 3565:             }
 3566:         }
 3567:     } elsif ($position eq 'middle') {
 3568:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 3569:                      'updatesmail','idconflictsmail','hostipmail');
 3570:         foreach my $type (@mailings) {
 3571:             $otheremails{$type} = '';
 3572:         }
 3573:     } elsif ($position eq 'lower') {
 3574:         if (ref($settings) eq 'HASH') {
 3575:             if (ref($settings->{'lonstatus'}) eq 'HASH') {
 3576:                 %lonstatus = %{$settings->{'lonstatus'}};
 3577:             }
 3578:         }
 3579:     } else {
 3580:         @mailings = ('helpdeskmail','otherdomsmail');
 3581:         foreach my $type (@mailings) {
 3582:             $otheremails{$type} = '';
 3583:         }
 3584:         $bccemails{'helpdeskmail'} = '';
 3585:         $bccemails{'otherdomsmail'} = '';
 3586:         $includestr{'helpdeskmail'} = '';
 3587:         $includestr{'otherdomsmail'} = '';
 3588:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 3589:     }
 3590:     if (ref($settings) eq 'HASH') {
 3591:         unless (($position eq 'top') || ($position eq 'lower')) {
 3592:             foreach my $type (@mailings) {
 3593:                 if (exists($settings->{$type})) {
 3594:                     if (ref($settings->{$type}) eq 'HASH') {
 3595:                         foreach my $item (@contacts) {
 3596:                             if ($settings->{$type}{$item}) {
 3597:                                 $checked{$type}{$item} = ' checked="checked" ';
 3598:                             }
 3599:                         }
 3600:                         $otheremails{$type} = $settings->{$type}{'others'};
 3601:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3602:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 3603:                             if ($settings->{$type}{'include'} ne '') {
 3604:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3605:                                 $includestr{$type} = &unescape($includestr{$type});
 3606:                             }
 3607:                         }
 3608:                     }
 3609:                 } elsif ($type eq 'lonstatusmail') {
 3610:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3611:                 }
 3612:             }
 3613:         }
 3614:         if ($position eq 'bottom') {
 3615:             foreach my $type (@mailings) {
 3616:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 3617:                 if ($settings->{$type}{'include'} ne '') {
 3618:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3619:                     $includestr{$type} = &unescape($includestr{$type});
 3620:                 }
 3621:             }
 3622:             if (ref($settings->{'helpform'}) eq 'HASH') {
 3623:                 if (ref($fields) eq 'ARRAY') {
 3624:                     foreach my $field (@{$fields}) {
 3625:                         $currfield{$field} = $settings->{'helpform'}{$field};
 3626:                     }
 3627:                 }
 3628:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 3629:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 3630:                 } else {
 3631:                     $maxsize = '1.0';
 3632:                 }
 3633:             } else {
 3634:                 if (ref($fields) eq 'ARRAY') {
 3635:                     foreach my $field (@{$fields}) {
 3636:                         $currfield{$field} = 'yes';
 3637:                     }
 3638:                 }
 3639:                 $maxsize = '1.0';
 3640:             }
 3641:         }
 3642:     } else {
 3643:         if ($position eq 'top') {
 3644:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 3645:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 3646:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 3647:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 3648:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3649:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 3650:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 3651:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 3652:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 3653:         } elsif ($position eq 'bottom') {
 3654:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 3655:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 3656:             if (ref($fields) eq 'ARRAY') {
 3657:                 foreach my $field (@{$fields}) {
 3658:                     $currfield{$field} = 'yes';
 3659:                 }
 3660:             }
 3661:             $maxsize = '1.0';
 3662:         }
 3663:     }
 3664:     my ($titles,$short_titles) = &contact_titles();
 3665:     my $rownum = 0;
 3666:     my $css_class;
 3667:     if ($position eq 'top') {
 3668:         foreach my $item (@contacts) {
 3669:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3670:             $datatable .= '<tr'.$css_class.'>'. 
 3671:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3672:                           '</span></td><td class="LC_right_item">'.
 3673:                           '<input type="text" name="'.$item.'" value="'.
 3674:                           $to{$item}.'" /></td></tr>';
 3675:             $rownum ++;
 3676:         }
 3677:     } elsif ($position eq 'bottom') {
 3678:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3679:         $datatable .= '<tr'.$css_class.'>'.
 3680:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3681:                       &mt('(e-mail, subject, and description always shown)').
 3682:                       '</td><td class="LC_left_item">';
 3683:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3684:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3685:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3686:             foreach my $field (@{$fields}) {
 3687:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3688:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3689:                     $datatable .= ' '.&mt('(logged-in users)');
 3690:                 }
 3691:                 $datatable .='</td><td>';
 3692:                 my $clickaction;
 3693:                 if ($field eq 'screenshot') {
 3694:                     $clickaction = ' onclick="screenshotSize(this);"';
 3695:                 }
 3696:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3697:                     foreach my $option (@{$possoptions->{$field}}) {
 3698:                         my $checked;
 3699:                         if ($currfield{$field} eq $option) {
 3700:                             $checked = ' checked="checked"';
 3701:                         }
 3702:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3703:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3704:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3705:                                       '</label></span>'.('&nbsp;'x2);
 3706:                     }
 3707:                 }
 3708:                 if ($field eq 'screenshot') {
 3709:                     my $display;
 3710:                     if ($currfield{$field} eq 'no') {
 3711:                         $display = ' style="display:none"';
 3712:                     }
 3713:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 3714:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3715:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3716:                 }
 3717:                 $datatable .= '</td></tr>';
 3718:             }
 3719:             $datatable .= '</table>';
 3720:         }
 3721:         $datatable .= '</td></tr>'."\n";
 3722:         $rownum ++;
 3723:     }
 3724:     unless (($position eq 'top') || ($position eq 'lower')) {
 3725:         foreach my $type (@mailings) {
 3726:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3727:             $datatable .= '<tr'.$css_class.'>'.
 3728:                           '<td><span class="LC_nobreak">'.
 3729:                           $titles->{$type}.': </span></td>'.
 3730:                           '<td class="LC_left_item">';
 3731:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3732:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3733:             }
 3734:             $datatable .= '<span class="LC_nobreak">';
 3735:             foreach my $item (@contacts) {
 3736:                 $datatable .= '<label>'.
 3737:                               '<input type="checkbox" name="'.$type.'"'.
 3738:                               $checked{$type}{$item}.
 3739:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3740:                               '</label>&nbsp;';
 3741:             }
 3742:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3743:                           '<input type="text" name="'.$type.'_others" '.
 3744:                           'value="'.$otheremails{$type}.'"  />';
 3745:             my %locchecked;
 3746:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3747:                 foreach my $loc ('s','b') {
 3748:                     if ($includeloc{$type} eq $loc) {
 3749:                         $locchecked{$loc} = ' checked="checked"';
 3750:                         last;
 3751:                     }
 3752:                 }
 3753:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3754:                               '<input type="text" name="'.$type.'_bcc" '.
 3755:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3756:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3757:                               &mt('Text automatically added to e-mail:').' '.
 3758:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 3759:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3760:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3761:                               ('&nbsp;'x2).
 3762:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3763:                               '</span></fieldset>';
 3764:             }
 3765:             $datatable .= '</td></tr>'."\n";
 3766:             $rownum ++;
 3767:         }
 3768:     }
 3769:     if ($position eq 'middle') {
 3770:         my %choices;
 3771:         my $corelink = &core_link_msu();
 3772:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 3773:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3774:                                         $corelink);
 3775:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 3776:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 3777:         my %defaultchecked = ('reporterrors'  => 'on',
 3778:                               'reportupdates' => 'on',
 3779:                               'reportstatus'  => 'on');
 3780:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3781:                                                    \%choices,$rownum);
 3782:         $datatable .= $reports;
 3783:     } elsif ($position eq 'lower') {
 3784:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3785:         my ($threshold,$sysmail,%excluded,%weights);
 3786:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 3787:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 3788:             $threshold = $lonstatus{'threshold'};
 3789:         } else {
 3790:             $threshold = $defaults->{'threshold'};
 3791:         }
 3792:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 3793:             $sysmail = $lonstatus{'sysmail'};
 3794:         } else {
 3795:             $sysmail = $defaults->{'sysmail'};
 3796:         }
 3797:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 3798:             foreach my $type ('E','W','N','U') {
 3799:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 3800:                     $weights{$type} = $lonstatus{'weights'}{$type};
 3801:                 } else {
 3802:                     $weights{$type} = $defaults->{$type};
 3803:                 }
 3804:             }
 3805:         } else {
 3806:             foreach my $type ('E','W','N','U') {
 3807:                 $weights{$type} = $defaults->{$type};
 3808:             }
 3809:         }
 3810:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 3811:             if (@{$lonstatus{'excluded'}} > 0) {
 3812:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 3813:             }
 3814:         }
 3815:         $datatable .= '<tr'.$css_class.'>'.
 3816:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 3817:                       $titles->{'errorthreshold'}.
 3818:                       '</span></td><td class="LC_left_item">'.
 3819:                       '<input type="text" name="errorthreshold" value="'.
 3820:                       $threshold.'" size="5" /></td></tr>';
 3821:         $rownum ++;
 3822:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3823:         $datatable .= '<tr'.$css_class.'>'.
 3824:                       '<td class="LC_left_item">'.
 3825:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 3826:                       '</span></td><td class="LC_left_item"><table><tr>';
 3827:         foreach my $type ('E','W','N','U') {
 3828:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 3829:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 3830:                           $weights{$type}.'" size="5" /></td>';
 3831:         }
 3832:         $datatable .= '</tr></table></tr>';
 3833:         $rownum ++;
 3834:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3835:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 3836:                       $titles->{'errorexcluded'}.'</td>'.
 3837:                       '<td class="LC_left_item"><table>';
 3838:         my $numinrow = 4;
 3839:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 3840:         for (my $i=0; $i<@ids; $i++) {
 3841:             my $rem = $i%($numinrow);
 3842:             if ($rem == 0) {
 3843:                 if ($i > 0) {
 3844:                     $datatable .= '</tr>';
 3845:                 }
 3846:                 $datatable .= '<tr>';
 3847:             }
 3848:             my $check;
 3849:             if ($excluded{$ids[$i]}) {
 3850:                 $check = ' checked="checked" ';
 3851:             }
 3852:             $datatable .= '<td class="LC_left_item">'.
 3853:                           '<span class="LC_nobreak"><label>'.
 3854:                           '<input type="checkbox" name="errorexcluded" '.
 3855:                           'value="'.$ids[$i].'"'.$check.' />'.
 3856:                           $ids[$i].'</label></span></td>';
 3857:         }
 3858:         my $colsleft = $numinrow - @ids%($numinrow);
 3859:         if ($colsleft > 1 ) {
 3860:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3861:                           '&nbsp;</td>';
 3862:         } elsif ($colsleft == 1) {
 3863:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3864:         }
 3865:         $datatable .= '</tr></table></td></tr>';
 3866:         $rownum ++;
 3867:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3868:         $datatable .= '<tr'.$css_class.'>'.
 3869:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 3870:                       $titles->{'errorsysmail'}.
 3871:                       '</span></td><td class="LC_left_item">'.
 3872:                       '<input type="text" name="errorsysmail" value="'.
 3873:                       $sysmail.'" size="5" /></td></tr>';
 3874:         $rownum ++;
 3875:     } elsif ($position eq 'bottom') {
 3876:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3877:         my (@posstypes,%usertypeshash);
 3878:         if (ref($types) eq 'ARRAY') {
 3879:             @posstypes = @{$types};
 3880:         }
 3881:         if (@posstypes) {
 3882:             if (ref($usertypes) eq 'HASH') {
 3883:                 %usertypeshash = %{$usertypes};
 3884:             }
 3885:             my @overridden;
 3886:             my $numinrow = 4;
 3887:             if (ref($settings) eq 'HASH') {
 3888:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 3889:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 3890:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 3891:                             push(@overridden,$key);
 3892:                             foreach my $item (@contacts) {
 3893:                                 if ($settings->{'overrides'}{$key}{$item}) {
 3894:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 3895:                                 }
 3896:                             }
 3897:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 3898:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 3899:                             $includeloc{'override_'.$key} = '';
 3900:                             $includestr{'override_'.$key} = '';
 3901:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 3902:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) = 
 3903:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 3904:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 3905:                             }
 3906:                         }
 3907:                     }
 3908:                 }
 3909:             }
 3910:             my $customclass = 'LC_helpdesk_override';
 3911:             my $optionsprefix = 'LC_options_helpdesk_';
 3912: 
 3913:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 3914:  
 3915:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3916:                                          $numinrow,$othertitle,'overrides',
 3917:                                          \$rownum,$onclicktypes,$customclass);
 3918:             $rownum ++;
 3919:             $usertypeshash{'default'} = $othertitle;
 3920:             foreach my $status (@posstypes) {
 3921:                 my $css_class;
 3922:                 if ($rownum%2) {
 3923:                     $css_class = 'LC_odd_row ';
 3924:                 }
 3925:                 $css_class .= $customclass;
 3926:                 my $rowid = $optionsprefix.$status;
 3927:                 my $hidden = 1;
 3928:                 my $currstyle = 'display:none';
 3929:                 if (grep(/^\Q$status\E$/,@overridden)) {
 3930:                     $currstyle = 'display:table-row';
 3931:                     $hidden = 0;
 3932:                 }
 3933:                 my $key = 'override_'.$status;
 3934:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 3935:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 3936:                                                   $usertypeshash{$status},$css_class,$currstyle,
 3937:                                                   \@contacts,$short_titles);
 3938:                 unless ($hidden) {
 3939:                     $rownum ++;
 3940:                 }
 3941:             }
 3942:         }
 3943:     }
 3944:     $$rowtotal += $rownum;
 3945:     return $datatable;
 3946: }
 3947: 
 3948: sub core_link_msu {
 3949:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3950:                                           &mt('LON-CAPA core group - MSU'),600,500);
 3951: }
 3952: 
 3953: sub overridden_helpdesk {
 3954:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 3955:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 3956:     my $class = 'LC_left_item';
 3957:     if ($css_class) {
 3958:         $css_class = ' class="'.$css_class.'"';
 3959:     }
 3960:     if ($rowid) {
 3961:         $rowid = ' id="'.$rowid.'"';
 3962:     }
 3963:     if ($rowstyle) {
 3964:         $rowstyle = ' style="'.$rowstyle.'"';
 3965:     }
 3966:     my ($output,$description);
 3967:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 3968:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 3969:               "<td>$description</td>\n".
 3970:               '<td class="'.$class.'" colspan="2">'.
 3971:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 3972:               '<span class="LC_nobreak">';
 3973:     if (ref($contacts) eq 'ARRAY') {
 3974:         foreach my $item (@{$contacts}) {
 3975:             my $check;
 3976:             if (ref($checked) eq 'HASH') {
 3977:                $check = $checked->{$item};
 3978:             }
 3979:             my $title;
 3980:             if (ref($short_titles) eq 'HASH') {
 3981:                 $title = $short_titles->{$item}; 
 3982:             }
 3983:             $output .= '<label>'.
 3984:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 3985:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 3986:         }
 3987:     }
 3988:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3989:                '<input type="text" name="override_'.$type.'_others" '.
 3990:                'value="'.$otheremails.'"  />';
 3991:     my %locchecked;
 3992:     foreach my $loc ('s','b') {
 3993:         if ($includeloc eq $loc) {
 3994:             $locchecked{$loc} = ' checked="checked"';
 3995:             last;
 3996:         }
 3997:     }
 3998:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3999:                '<input type="text" name="override_'.$type.'_bcc" '.
 4000:                'value="'.$bccemails.'"  /></fieldset>'.
 4001:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4002:                &mt('Text automatically added to e-mail:').' '.
 4003:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 4004:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4005:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4006:                ('&nbsp;'x2).
 4007:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4008:                '</span></fieldset>'.
 4009:                '</td></tr>'."\n";
 4010:     return $output;
 4011: }
 4012: 
 4013: sub contacts_javascript {
 4014:     return <<"ENDSCRIPT";
 4015: 
 4016: <script type="text/javascript">
 4017: // <![CDATA[
 4018: 
 4019: function screenshotSize(field) {
 4020:     if (document.getElementById('help_screenshotsize')) {
 4021:         if (field.value == 'no') {
 4022:             document.getElementById('help_screenshotsize').style.display="none";
 4023:         } else {
 4024:             document.getElementById('help_screenshotsize').style.display="";
 4025:         }
 4026:     }
 4027:     return;
 4028: }
 4029: 
 4030: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 4031:     if (form.elements[checkbox].length != undefined) {
 4032:         var count = 0;
 4033:         if (docount) {
 4034:             for (var i=0; i<form.elements[checkbox].length; i++) {
 4035:                 if (form.elements[checkbox][i].checked) {
 4036:                     count ++;
 4037:                 }
 4038:             }
 4039:         }
 4040:         for (var i=0; i<form.elements[checkbox].length; i++) {
 4041:             var type = form.elements[checkbox][i].value;
 4042:             if (document.getElementById(prefix+type)) {
 4043:                 if (form.elements[checkbox][i].checked) {
 4044:                     document.getElementById(prefix+type).style.display = 'table-row';
 4045:                     if (count % 2 == 1) {
 4046:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 4047:                     } else {
 4048:                         document.getElementById(prefix+type).className = target;
 4049:                     }
 4050:                     count ++;
 4051:                 } else {
 4052:                     document.getElementById(prefix+type).style.display = 'none';
 4053:                 }
 4054:             }
 4055:         }
 4056:     }
 4057:     return;
 4058: }
 4059: 
 4060: 
 4061: // ]]>
 4062: </script>
 4063: 
 4064: ENDSCRIPT
 4065: }
 4066: 
 4067: sub print_helpsettings {
 4068:     my ($position,$dom,$settings,$rowtotal) = @_;
 4069:     my $confname = $dom.'-domainconfig';
 4070:     my $formname = 'display';
 4071:     my ($datatable,$itemcount);
 4072:     if ($position eq 'top') {
 4073:         $itemcount = 1;
 4074:         my (%choices,%defaultchecked,@toggles);
 4075:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 4076:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 4077:                                      &mt('LON-CAPA bug tracker'),600,500));
 4078:         %defaultchecked = ('submitbugs' => 'on');
 4079:         @toggles = ('submitbugs');
 4080:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4081:                                                      \%choices,$itemcount);
 4082:         $$rowtotal ++;
 4083:     } else {
 4084:         my $css_class;
 4085:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 4086:         my (%customroles,%ordered,%current);
 4087:         if (ref($settings) eq 'HASH') {
 4088:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 4089:                 %current = %{$settings->{'adhoc'}};
 4090:             }
 4091:         }
 4092:         my $count = 0;
 4093:         foreach my $key (sort(keys(%existing))) {
 4094:             if ($key=~/^rolesdef\_(\w+)$/) {
 4095:                 my $rolename = $1;
 4096:                 my (%privs,$order);
 4097:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 4098:                 $customroles{$rolename} = \%privs;
 4099:                 if (ref($current{$rolename}) eq 'HASH') {
 4100:                     $order = $current{$rolename}{'order'};
 4101:                 }
 4102:                 if ($order eq '') {
 4103:                     $order = $count;
 4104:                 }
 4105:                 $ordered{$order} = $rolename;
 4106:                 $count++;
 4107:             }
 4108:         }
 4109:         my $maxnum = scalar(keys(%ordered));
 4110:         my @roles_by_num = ();
 4111:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 4112:             push(@roles_by_num,$item);
 4113:         }
 4114:         my $context = 'domprefs';
 4115:         my $crstype = 'Course';
 4116:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4117:         my @accesstypes = ('all','dh','da','none');
 4118:         my ($numstatustypes,@jsarray);
 4119:         if (ref($types) eq 'ARRAY') {
 4120:             if (@{$types} > 0) {
 4121:                 $numstatustypes = scalar(@{$types});
 4122:                 push(@accesstypes,'status');
 4123:                 @jsarray = ('bystatus');
 4124:             }
 4125:         }
 4126:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 4127:         if (keys(%domhelpdesk)) {
 4128:             push(@accesstypes,('inc','exc'));
 4129:             push(@jsarray,('notinc','notexc'));
 4130:         }
 4131:         my $hiddenstr = join("','",@jsarray);
 4132:         my $context = 'domprefs';
 4133:         my $crstype = 'Course';
 4134:         my $prefix = 'helproles_';
 4135:         my $add_class = 'LC_hidden';
 4136:         foreach my $num (@roles_by_num) {
 4137:             my $role = $ordered{$num};
 4138:             my ($desc,$access,@statuses);
 4139:             if (ref($current{$role}) eq 'HASH') {
 4140:                 $desc = $current{$role}{'desc'};
 4141:                 $access = $current{$role}{'access'};
 4142:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 4143:                     @statuses = @{$current{$role}{'insttypes'}};
 4144:                 }
 4145:             }
 4146:             if ($desc eq '') {
 4147:                 $desc = $role;
 4148:             }
 4149:             my $identifier = 'custhelp'.$num;
 4150:             my %full=();
 4151:             my %levels= (
 4152:                          course => {},
 4153:                          domain => {},
 4154:                          system => {},
 4155:                         );
 4156:             my %levelscurrent=(
 4157:                                course => {},
 4158:                                domain => {},
 4159:                                system => {},
 4160:                               );
 4161:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 4162:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4163:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4164:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 4165:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 4166:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 4167:             for (my $k=0; $k<=$maxnum; $k++) {
 4168:                 my $vpos = $k+1;
 4169:                 my $selstr;
 4170:                 if ($k == $num) {
 4171:                     $selstr = ' selected="selected" ';
 4172:                 }
 4173:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4174:             }
 4175:             $datatable .= '</select>'.('&nbsp;'x2).
 4176:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 4177:                           '</td>'.
 4178:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4179:                           &mt('Name shown to users:').
 4180:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 4181:                           '</fieldset>'.
 4182:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 4183:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 4184:                           '<fieldset>'.
 4185:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 4186:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 4187:                                                                    \%levelscurrent,$identifier,
 4188:                                                                    'LC_hidden',$prefix.$num.'_privs').
 4189:                           '</fieldset></td>';
 4190:             $itemcount ++;
 4191:         }
 4192:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4193:         my $newcust = 'custhelp'.$count;
 4194:         my (%privs,%levelscurrent);
 4195:         my %full=();
 4196:         my %levels= (
 4197:                      course => {},
 4198:                      domain => {},
 4199:                      system => {},
 4200:                     );
 4201:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 4202:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4203:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 4204:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 4205:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 4206:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 4207:         for (my $k=0; $k<$maxnum+1; $k++) {
 4208:             my $vpos = $k+1;
 4209:             my $selstr;
 4210:             if ($k == $maxnum) {
 4211:                 $selstr = ' selected="selected" ';
 4212:             }
 4213:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4214:         }
 4215:         $datatable .= '</select>&nbsp;'."\n".
 4216:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 4217:                       '</label></span></td>'.
 4218:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4219:                       '<span class="LC_nobreak">'.
 4220:                       &mt('Internal name:').
 4221:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 4222:                       '</span>'.('&nbsp;'x4).
 4223:                       '<span class="LC_nobreak">'.
 4224:                       &mt('Name shown to users:').
 4225:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 4226:                       '</span></fieldset>'.
 4227:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 4228:                                              $usertypes,$types,\%domhelpdesk).
 4229:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 4230:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 4231:                                                                 \@templateroles,$newcust).
 4232:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 4233:                                                                \%levelscurrent,$newcust).
 4234:                       '</fieldset>'.
 4235:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 4236:                       '</td></tr>';
 4237:         $count ++;
 4238:         $$rowtotal += $count;
 4239:     }
 4240:     return $datatable;
 4241: }
 4242: 
 4243: sub adhocbutton {
 4244:     my ($prefix,$num,$field,$visibility) = @_;
 4245:     my %lt = &Apache::lonlocal::texthash(
 4246:                                           show => 'Show details',
 4247:                                           hide => 'Hide details',
 4248:                                         );
 4249:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 4250:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 4251:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 4252:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 4253: }
 4254: 
 4255: sub helpsettings_javascript {
 4256:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 4257:     return unless(ref($roles_by_num) eq 'ARRAY');
 4258:     my %html_js_lt = &Apache::lonlocal::texthash(
 4259:                                           show => 'Show details',
 4260:                                           hide => 'Hide details',
 4261:                                         );
 4262:     &html_escape(\%html_js_lt);
 4263:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 4264:     return <<"ENDSCRIPT";
 4265: <script type="text/javascript">
 4266: // <![CDATA[
 4267: 
 4268: function reorderHelpRoles(form,item) {
 4269:     var changedVal;
 4270: $jstext
 4271:     var newpos = 'helproles_${total}_pos';
 4272:     var maxh = 1 + $total;
 4273:     var current = new Array();
 4274:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4275:     if (item == newpos) {
 4276:         changedVal = newitemVal;
 4277:     } else {
 4278:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4279:         current[newitemVal] = newpos;
 4280:     }
 4281:     for (var i=0; i<helproles.length; i++) {
 4282:         var elementName = 'helproles_'+helproles[i]+'_pos';
 4283:         if (elementName != item) {
 4284:             if (form.elements[elementName]) {
 4285:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4286:                 current[currVal] = elementName;
 4287:             }
 4288:         }
 4289:     }
 4290:     var oldVal;
 4291:     for (var j=0; j<maxh; j++) {
 4292:         if (current[j] == undefined) {
 4293:             oldVal = j;
 4294:         }
 4295:     }
 4296:     if (oldVal < changedVal) {
 4297:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4298:            var elementName = current[k];
 4299:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4300:         }
 4301:     } else {
 4302:         for (var k=changedVal; k<oldVal; k++) {
 4303:             var elementName = current[k];
 4304:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4305:         }
 4306:     }
 4307:     return;
 4308: }
 4309: 
 4310: function helpdeskAccess(num) {
 4311:     var curraccess = null;
 4312:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 4313:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 4314:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 4315:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 4316:             }
 4317:         }
 4318:     }
 4319:     var shown = Array();
 4320:     var hidden = Array();
 4321:     if (curraccess == 'none') {
 4322:         hidden = Array('$hiddenstr');
 4323:     } else {
 4324:         if (curraccess == 'status') {
 4325:             shown = Array('bystatus');
 4326:             hidden = Array('notinc','notexc');
 4327:         } else {
 4328:             if (curraccess == 'exc') {
 4329:                 shown = Array('notexc');
 4330:                 hidden = Array('notinc','bystatus');
 4331:             }
 4332:             if (curraccess == 'inc') {
 4333:                 shown = Array('notinc');
 4334:                 hidden = Array('notexc','bystatus');
 4335:             }
 4336:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 4337:                 hidden = Array('notinc','notexc','bystatus');
 4338:             }
 4339:         }
 4340:     }
 4341:     if (hidden.length > 0) {
 4342:         for (var i=0; i<hidden.length; i++) {
 4343:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 4344:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 4345:             }
 4346:         }
 4347:     }
 4348:     if (shown.length > 0) {
 4349:         for (var i=0; i<shown.length; i++) {
 4350:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 4351:                 if (shown[i] == 'privs') {
 4352:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 4353:                 } else {
 4354:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 4355:                 }
 4356:             }
 4357:         }
 4358:     }
 4359:     return;
 4360: }
 4361: 
 4362: function toggleHelpdeskItem(num,field) {
 4363:     if (document.getElementById('helproles_'+num+'_'+field)) {
 4364:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 4365:             document.getElementById('helproles_'+num+'_'+field).className =
 4366:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 4367:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4368:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 4369:             }
 4370:         } else {
 4371:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 4372:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4373:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 4374:             }
 4375:         }
 4376:     }
 4377:     return;
 4378: }
 4379: 
 4380: // ]]>
 4381: </script>
 4382: 
 4383: ENDSCRIPT
 4384: }
 4385: 
 4386: sub helpdeskroles_access {
 4387:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 4388:         $usertypes,$types,$domhelpdesk) = @_;
 4389:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 4390:     my %lt = &Apache::lonlocal::texthash(
 4391:                     'rou'    => 'Role usage',
 4392:                     'whi'    => 'Which helpdesk personnel may use this role?',
 4393:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 4394:                     'dh'     => 'All with domain helpdesk role',
 4395:                     'da'     => 'All with domain helpdesk assistant role',
 4396:                     'none'   => 'None',
 4397:                     'status' => 'Determined based on institutional status',
 4398:                     'inc'    => 'Include all, but exclude specific personnel',
 4399:                     'exc'    => 'Exclude all, but include specific personnel',
 4400:                   );
 4401:     my %usecheck = (
 4402:                      all => ' checked="checked"',
 4403:                    );
 4404:     my %displaydiv = (
 4405:                       status => 'none',
 4406:                       inc    => 'none',
 4407:                       exc    => 'none',
 4408:                       priv   => 'block',
 4409:                      );
 4410:     my $output;
 4411:     if (ref($current) eq 'HASH') {
 4412:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 4413:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 4414:                 $usecheck{$current->{access}} = $usecheck{'all'};
 4415:                 delete($usecheck{'all'});
 4416:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 4417:                     my $access = $1;
 4418:                     $displaydiv{$access} = 'inline';
 4419:                 } elsif ($current->{access} eq 'none') {
 4420:                     $displaydiv{'priv'} = 'none';
 4421:                 }
 4422:             }
 4423:         }
 4424:     }
 4425:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 4426:               '<p>'.$lt{'whi'}.'</p>';
 4427:     foreach my $access (@{$accesstypes}) {
 4428:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 4429:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 4430:                    $lt{$access}.'</label>';
 4431:         if ($access eq 'status') {
 4432:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 4433:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 4434:                                                                  $othertitle,$usertypes,$types).
 4435:                        '</div>';
 4436:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 4437:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 4438:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4439:                        '</div>';
 4440:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 4441:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 4442:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4443:                        '</div>';
 4444:         }
 4445:         $output .= '</p>';
 4446:     }
 4447:     $output .= '</fieldset>';
 4448:     return $output;
 4449: }
 4450: 
 4451: sub radiobutton_prefs {
 4452:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 4453:         $additional,$align) = @_;
 4454:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 4455:                    (ref($choices) eq 'HASH'));
 4456: 
 4457:     my (%checkedon,%checkedoff,$datatable,$css_class);
 4458: 
 4459:     foreach my $item (@{$toggles}) {
 4460:         if ($defaultchecked->{$item} eq 'on') {
 4461:             $checkedon{$item} = ' checked="checked" ';
 4462:             $checkedoff{$item} = ' ';
 4463:         } elsif ($defaultchecked->{$item} eq 'off') {
 4464:             $checkedoff{$item} = ' checked="checked" ';
 4465:             $checkedon{$item} = ' ';
 4466:         }
 4467:     }
 4468:     if (ref($settings) eq 'HASH') {
 4469:         foreach my $item (@{$toggles}) {
 4470:             if ($settings->{$item} eq '1') {
 4471:                 $checkedon{$item} =  ' checked="checked" ';
 4472:                 $checkedoff{$item} = ' ';
 4473:             } elsif ($settings->{$item} eq '0') {
 4474:                 $checkedoff{$item} =  ' checked="checked" ';
 4475:                 $checkedon{$item} = ' ';
 4476:             }
 4477:         }
 4478:     }
 4479:     if ($onclick) {
 4480:         $onclick = ' onclick="'.$onclick.'"';
 4481:     }
 4482:     foreach my $item (@{$toggles}) {
 4483:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4484:         $datatable .=
 4485:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 4486:             '<span class="LC_nobreak">'.$choices->{$item}.
 4487:             '</span></td>';
 4488:         if ($align eq 'left') {
 4489:             $datatable .= '<td class="LC_left_item">';
 4490:         } else {
 4491:             $datatable .= '<td class="LC_right_item">';
 4492:         }
 4493:         $datatable .=
 4494:             '<span class="LC_nobreak">'.
 4495:             '<label><input type="radio" name="'.
 4496:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 4497:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 4498:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 4499:             '</span>'.$additional.
 4500:             '</td>'.
 4501:             '</tr>';
 4502:         $itemcount ++;
 4503:     }
 4504:     return ($datatable,$itemcount);
 4505: }
 4506: 
 4507: sub print_ltitools {
 4508:     my ($dom,$settings,$rowtotal) = @_;
 4509:     my $rownum = 0;
 4510:     my $css_class;
 4511:     my $itemcount = 1;
 4512:     my $maxnum = 0;
 4513:     my %ordered;
 4514:     if (ref($settings) eq 'HASH') {
 4515:         foreach my $item (keys(%{$settings})) {
 4516:             if (ref($settings->{$item}) eq 'HASH') {
 4517:                 my $num = $settings->{$item}{'order'};
 4518:                 $ordered{$num} = $item;
 4519:             }
 4520:         }
 4521:     }
 4522:     my $confname = $dom.'-domainconfig';
 4523:     my $switchserver = &check_switchserver($dom,$confname);
 4524:     my $maxnum = scalar(keys(%ordered));
 4525:     my $datatable;
 4526:     my %lt = &ltitools_names();
 4527:     my @courseroles = ('cc','in','ta','ep','st');
 4528:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 4529:     my @fields = ('fullname','firstname','lastname','email','roles','user');
 4530:     if (keys(%ordered)) {
 4531:         my @items = sort { $a <=> $b } keys(%ordered);
 4532:         for (my $i=0; $i<@items; $i++) {
 4533:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4534:             my $item = $ordered{$items[$i]};
 4535:             my ($title,$key,$secret,$url,$lifetime,$imgsrc,%sigsel);
 4536:             if (ref($settings->{$item}) eq 'HASH') {
 4537:                 $title = $settings->{$item}->{'title'};
 4538:                 $url = $settings->{$item}->{'url'};
 4539:                 $key = $settings->{$item}->{'key'};
 4540:                 $secret = $settings->{$item}->{'secret'};
 4541:                 $lifetime = $settings->{$item}->{'lifetime'};
 4542:                 my $image = $settings->{$item}->{'image'};
 4543:                 if ($image ne '') {
 4544:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 4545:                 }
 4546:                 if ($settings->{$item}->{'sigmethod'} eq 'HMAC-256') {
 4547:                     $sigsel{'HMAC-256'} = ' selected="selected"';
 4548:                 } else {
 4549:                     $sigsel{'HMAC-SHA1'} = ' selected="selected"';
 4550:                 }
 4551:             }
 4552:             my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_".$item."'".');"';
 4553:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4554:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 4555:             for (my $k=0; $k<=$maxnum; $k++) {
 4556:                 my $vpos = $k+1;
 4557:                 my $selstr;
 4558:                 if ($k == $i) {
 4559:                     $selstr = ' selected="selected" ';
 4560:                 }
 4561:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4562:             }
 4563:             $datatable .= '</select>'.('&nbsp;'x2).
 4564:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 4565:                 &mt('Delete?').'</label></span></td>'.
 4566:                 '<td colspan="2">'.
 4567:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4568:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 4569:                 ('&nbsp;'x2).
 4570:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 4571:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 4572:                 ('&nbsp;'x2).
 4573:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 4574:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4575:                 ('&nbsp;'x2).
 4576:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_sigmethod_'.$i.'">'.
 4577:                 '<option value="HMAC-SHA1"'.$sigsel{'HMAC-SHA1'}.'>HMAC-SHA1</option>'.
 4578:                 '<option value="HMAC-SHA256"'.$sigsel{'HMAC-SHA256'}.'>HMAC-SHA256</option></select></span>'.
 4579:                 '<br /><br />'.
 4580:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_url_'.$i.'"'.
 4581:                 ' value="'.$url.'" /></span>'.
 4582:                 ('&nbsp;'x2).
 4583:                 '<span class="LC_nobreak">'.$lt{'key'}.':'.
 4584:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 4585:                 ('&nbsp;'x2).
 4586:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':'.
 4587:                 '<input type="text" size="5" name="ltitools_lifetime_'.$i.'" value="'.$lifetime.'" /></span> '.
 4588:                 ('&nbsp;'x2).
 4589:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 4590:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 4591:                 '<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>'.
 4592:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 4593:                 '</fieldset>'.
 4594:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4595:                 '<span class="LC_nobreak">'.&mt('Display target:');
 4596:             my %currdisp;
 4597:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 4598:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 4599:                     $currdisp{'window'} = ' checked="checked"';
 4600:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 4601:                     $currdisp{'tab'} = ' checked="checked"';
 4602:                 } else {
 4603:                     $currdisp{'iframe'} = ' checked="checked"';
 4604:                 }
 4605:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 4606:                     $currdisp{'width'} = $1;
 4607:                 }
 4608:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 4609:                      $currdisp{'height'} = $1;
 4610:                 }
 4611:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 4612:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 4613:             } else {
 4614:                 $currdisp{'iframe'} = ' checked="checked"';
 4615:             }
 4616:             foreach my $disp ('iframe','tab','window') {
 4617:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 4618:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 4619:             }
 4620:             $datatable .= ('&nbsp;'x4);
 4621:             foreach my $dimen ('width','height') {
 4622:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4623:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 4624:                               ('&nbsp;'x2);
 4625:             }
 4626:             $datatable .= '</span><br />'.
 4627:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4628:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></div>'.
 4629:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4630:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 4631:                           '</textarea></div><div style=""></div><br />';
 4632:             my %units = (
 4633:                           'passback' => 'days',
 4634:                           'roster'   => 'seconds',
 4635:                         );
 4636:             foreach my $extra ('passback','roster') {
 4637:                 my $validsty = 'none';
 4638:                 my $currvalid;
 4639:                 my $checkedon = '';
 4640:                 my $checkedoff = ' checked="checked"';
 4641:                 if ($settings->{$item}->{$extra}) {
 4642:                     $checkedon = $checkedoff;
 4643:                     $checkedoff = '';
 4644:                     $validsty = 'inline-block';
 4645:                     if ($settings->{$item}->{$extra.'valid'} =~ /^\d+\.?\d*$/) {
 4646:                         $currvalid = $settings->{$item}->{$extra.'valid'};
 4647:                     }
 4648:                 }
 4649:                 my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','$i'".');"';
 4650:                 $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 4651:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.$onclick.' />'.
 4652:                               &mt('No').'</label>'.('&nbsp;'x2).
 4653:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.$onclick.' />'.
 4654:                               &mt('Yes').'</label></span></div>'.
 4655:                               '<div class="LC_floatleft" style="display:'.$validsty.';" id="ltitools_'.$extra.'time_'.$i.'">'.
 4656:                               '<span class="LC_nobreak">'.
 4657:                               &mt("at least [_1] $units{$extra} after launch",
 4658:                                   '<input type="text" name="ltitools_'.$extra.'valid_'.$i.'" value="'.$currvalid.'" />').
 4659:                               '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 4660:             }
 4661:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 4662:             if ($imgsrc) {
 4663:                 $datatable .= $imgsrc.
 4664:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 4665:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 4666:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 4667:             } else {
 4668:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4669:             }
 4670:             if ($switchserver) {
 4671:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4672:             } else {
 4673:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 4674:             }
 4675:             $datatable .= '</span></fieldset>';
 4676:             my (%checkedfields,%rolemaps,$userincdom);
 4677:             if (ref($settings->{$item}) eq 'HASH') {
 4678:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 4679:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 4680:                 }
 4681:                 $userincdom = $settings->{$item}->{'incdom'};
 4682:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 4683:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 4684:                     $checkedfields{'roles'} = 1;
 4685:                 }
 4686:             }
 4687:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4688:                           '<span class="LC_nobreak">';
 4689:             my $userfieldstyle = 'display:none;';
 4690:             my $seluserdom = '';
 4691:             my $unseluserdom = ' selected="selected"';
 4692:             foreach my $field (@fields) {
 4693:                 my ($checked,$onclick,$id,$spacer);
 4694:                 if ($checkedfields{$field}) {
 4695:                     $checked = ' checked="checked"';
 4696:                 }
 4697:                 if ($field eq 'user') {
 4698:                     $id = ' id="ltitools_user_field_'.$i.'"';
 4699:                     $onclick = ' onclick="toggleLTITools(this.form,'."'$field','$i'".')"';
 4700:                     if ($checked) {
 4701:                         $userfieldstyle = 'display:inline-block';
 4702:                         if ($userincdom) {
 4703:                             $seluserdom = $unseluserdom;
 4704:                             $unseluserdom = '';
 4705:                         }
 4706:                     }
 4707:                 } else {
 4708:                     $spacer = ('&nbsp;' x2);
 4709:                 }
 4710:                 $datatable .= '<label>'.
 4711:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$id.$checked.$onclick.' />'.
 4712:                               $lt{$field}.'</label>'.$spacer;
 4713:             }
 4714:             $datatable .= '</span>';
 4715:             $datatable .= '<div style="'.$userfieldstyle.'" id="ltitools_user_div_'.$i.'">'.
 4716:                           '<span class="LC_nobreak"> : '.
 4717:                           '<select name="ltitools_userincdom_'.$i.'">'.
 4718:                           '<option value="">'.&mt('Select').'</option>'.
 4719:                           '<option value="0"'.$unseluserdom.'>'.&mt('username').'</option>'.
 4720:                           '<option value="1"'.$seluserdom.'>'.&mt('username:domain').'</option>'.
 4721:                           '</select></span></div>';
 4722:             $datatable .= '</fieldset>'.
 4723:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4724:             foreach my $role (@courseroles) {
 4725:                 my ($selected,$selectnone);
 4726:                 if (!$rolemaps{$role}) {
 4727:                     $selectnone = ' selected="selected"';
 4728:                 }
 4729:                 $datatable .= '<td style="text-align: center">'. 
 4730:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4731:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 4732:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 4733:                 foreach my $ltirole (@ltiroles) {
 4734:                     unless ($selectnone) {
 4735:                         if ($rolemaps{$role} eq $ltirole) {
 4736:                             $selected = ' selected="selected"';
 4737:                         } else {
 4738:                             $selected = '';
 4739:                         }
 4740:                     }
 4741:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 4742:                 }
 4743:                 $datatable .= '</select></td>';
 4744:             }
 4745:             $datatable .= '</tr></table></fieldset>';
 4746:             my %courseconfig;
 4747:             if (ref($settings->{$item}) eq 'HASH') {
 4748:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 4749:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 4750:                 }
 4751:             }
 4752:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4753:             foreach my $item ('label','title','target','linktext','explanation','append') {
 4754:                 my $checked;
 4755:                 if ($courseconfig{$item}) {
 4756:                     $checked = ' checked="checked"';
 4757:                 }
 4758:                 $datatable .= '<label>'.
 4759:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 4760:                        $lt{'crs'.$item}.'</label>&nbsp; '."\n";
 4761:             }
 4762:             $datatable .= '</span></fieldset>'.
 4763:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4764:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 4765:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 4766:                 my %custom = %{$settings->{$item}->{'custom'}};
 4767:                 if (keys(%custom) > 0) {
 4768:                     foreach my $key (sort(keys(%custom))) {
 4769:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 4770:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 4771:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 4772:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 4773:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 4774:                     }
 4775:                 }
 4776:             }
 4777:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 4778:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 4779:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 4780:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 4781:             $datatable .= '</table></fieldset></td></tr>'."\n";
 4782:             $itemcount ++;
 4783:         }
 4784:     }
 4785:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4786:     my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_add_pos'".');"';
 4787:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 4788:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 4789:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 4790:     for (my $k=0; $k<$maxnum+1; $k++) {
 4791:         my $vpos = $k+1;
 4792:         my $selstr;
 4793:         if ($k == $maxnum) {
 4794:             $selstr = ' selected="selected" ';
 4795:         }
 4796:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4797:     }
 4798:     $datatable .= '</select>&nbsp;'."\n".
 4799:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 4800:                   '<td colspan="2">'.
 4801:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4802:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_add_title" value="" /></span> '."\n".
 4803:                   ('&nbsp;'x2).
 4804:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 4805:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 4806:                   ('&nbsp;'x2).
 4807:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 4808:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4809:                   '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_add_sigmethod">'.
 4810:                   '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 4811:                   '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 4812:                   '<br />'.
 4813:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_add_url" value="" /></span> '."\n".
 4814:                   ('&nbsp;'x2).
 4815:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 4816:                   ('&nbsp;'x2).
 4817:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="ltitools_add_lifetime" value="300" /></span> '."\n".
 4818:                   ('&nbsp;'x2).
 4819:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 4820:                   '<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".
 4821:                   '</fieldset>'.
 4822:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4823:                   '<span class="LC_nobreak">'.&mt('Display target:');
 4824:     my %defaultdisp;
 4825:     $defaultdisp{'iframe'} = ' checked="checked"';
 4826:     foreach my $disp ('iframe','tab','window') {
 4827:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 4828:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 4829:     }
 4830:     $datatable .= ('&nbsp;'x4);
 4831:     foreach my $dimen ('width','height') {
 4832:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4833:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 4834:                       ('&nbsp;'x2);
 4835:     }
 4836:     $datatable .= '</span><br />'.
 4837:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4838:                   '<input type="text" name="ltitools_add_linktext" size="5" /></div>'.
 4839:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4840:                   '<textarea name="ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 4841:                   '</div><div style=""></div><br />';
 4842:     my %units = (
 4843:                   'passback' => 'days',
 4844:                   'roster'   => 'seconds',
 4845:                 );
 4846:     my %defaulttimes = (
 4847:                      'passback' => '7',
 4848:                      'roster'   => '300',
 4849:                    );
 4850:     foreach my $extra ('passback','roster') {
 4851:         my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','add'".');"';
 4852:         $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 4853:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="0" checked="checked"'.$onclick.' />'.
 4854:                       &mt('No').'</label></span>'.('&nbsp;'x2).'<span class="LC_nobreak">'.
 4855:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="1"'.$onclick.' />'.
 4856:                       &mt('Yes').'</label></span></div>'.
 4857:                       '<div class="LC_floatleft" style="display:none;" id="ltitools_'.$extra.'time_add">'.
 4858:                       '<span class="LC_nobreak">'.
 4859:                       &mt("at least [_1] $units{$extra} after launch",
 4860:                           '<input type="text" name="ltitools_'.$extra.'valid_add" value="'.$defaulttimes{$extra}.'" />').
 4861:                       '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 4862:     }
 4863:     $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 4864:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4865:     if ($switchserver) {
 4866:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4867:     } else {
 4868:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 4869:     }
 4870:     $datatable .= '</span></fieldset>'.
 4871:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4872:                   '<span class="LC_nobreak">';
 4873:     foreach my $field (@fields) {
 4874:         my ($id,$onclick,$spacer);
 4875:         if ($field eq 'user') {
 4876:             $id = ' id="ltitools_user_field_add"';
 4877:             $onclick = ' onclick="toggleLTITools(this.form,'."'$field','add'".')"';
 4878:         } else {
 4879:             $spacer = ('&nbsp;' x2);
 4880:         }
 4881:         $datatable .= '<label>'.
 4882:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'"'.$id.$onclick.' />'.
 4883:                       $lt{$field}.'</label>'.$spacer;
 4884:     }
 4885:     $datatable .= '</span>'.
 4886:                   '<div style="display:none;" id="ltitools_user_div_add">'.
 4887:                   '<span class="LC_nobreak"> : '.
 4888:                   '<select name="ltitools_userincdom_add">'.
 4889:                   '<option value="" selected="selected">'.&mt('Select').'</option>'.
 4890:                   '<option value="0">'.&mt('username').'</option>'.
 4891:                   '<option value="1">'.&mt('username:domain').'</option>'.
 4892:                   '</select></span></div></fieldset>';
 4893:     $datatable .= '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4894:     foreach my $role (@courseroles) {
 4895:         my ($checked,$checkednone);
 4896:         $datatable .= '<td style="text-align: center">'.
 4897:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4898:                       '<select name="ltitools_add_roles_'.$role.'">'.
 4899:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 4900:         foreach my $ltirole (@ltiroles) {
 4901:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 4902:         }
 4903:         $datatable .= '</select></td>';
 4904:     }
 4905:     $datatable .= '</tr></table></fieldset>'.
 4906:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4907:     foreach my $item ('label','title','target','linktext','explanation','append') {
 4908:         $datatable .= '<label>'.
 4909:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 4910:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4911:     }
 4912:     $datatable .= '</span></fieldset>'.
 4913:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4914:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 4915:                   '<tr><td><span class="LC_nobreak">'.
 4916:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 4917:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 4918:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 4919:                   '</table></fieldset>'."\n".
 4920:                   '</td>'."\n".
 4921:                   '</tr>'."\n";
 4922:     $itemcount ++;
 4923:     return $datatable;
 4924: }
 4925: 
 4926: sub ltitools_names {
 4927:     my %lt = &Apache::lonlocal::texthash(
 4928:                                           'title'          => 'Title',
 4929:                                           'version'        => 'Version',
 4930:                                           'msgtype'        => 'Message Type',
 4931:                                           'sigmethod'      => 'Signature Method',
 4932:                                           'url'            => 'URL',
 4933:                                           'key'            => 'Key',
 4934:                                           'lifetime'       => 'Nonce lifetime (s)',
 4935:                                           'secret'         => 'Secret',
 4936:                                           'icon'           => 'Icon',   
 4937:                                           'user'           => 'User',
 4938:                                           'fullname'       => 'Full Name',
 4939:                                           'firstname'      => 'First Name',
 4940:                                           'lastname'       => 'Last Name',
 4941:                                           'email'          => 'E-mail',
 4942:                                           'roles'          => 'Role',
 4943:                                           'window'         => 'Window',
 4944:                                           'tab'            => 'Tab',
 4945:                                           'iframe'         => 'iFrame',
 4946:                                           'height'         => 'Height',
 4947:                                           'width'          => 'Width',
 4948:                                           'linktext'       => 'Default Link Text',
 4949:                                           'explanation'    => 'Default Explanation',
 4950:                                           'passback'       => 'Tool can return grades:',
 4951:                                           'roster'         => 'Tool can retrieve roster:',
 4952:                                           'crstarget'      => 'Display target',
 4953:                                           'crslabel'       => 'Course label',
 4954:                                           'crstitle'       => 'Course title', 
 4955:                                           'crslinktext'    => 'Link Text',
 4956:                                           'crsexplanation' => 'Explanation',
 4957:                                           'crsappend'      => 'Provider URL',
 4958:                                         );
 4959:     return %lt;
 4960: }
 4961: 
 4962: sub print_proctoring {
 4963:     my ($dom,$settings,$rowtotal) = @_;
 4964:     my $itemcount = 1;
 4965:     my (%ordered,%providernames,%current,%currentdef);
 4966:     my $confname = $dom.'-domainconfig';
 4967:     my $switchserver = &check_switchserver($dom,$confname);
 4968:     if (ref($settings) eq 'HASH') {
 4969:         foreach my $item (keys(%{$settings})) {
 4970:             if (ref($settings->{$item}) eq 'HASH') {
 4971:                 my $num = $settings->{$item}{'order'};
 4972:                 $ordered{$num} = $item;
 4973:             }
 4974:         }
 4975:     } else {
 4976:         %ordered = (
 4977:                      1 => 'proctorio',
 4978:                      2 => 'examity',
 4979:                    );
 4980:     }
 4981:     %providernames = &proctoring_providernames();
 4982:     my $maxnum = scalar(keys(%ordered));
 4983:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
 4984:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
 4985:     if (ref($requref) eq 'HASH') {
 4986:         %requserfields = %{$requref};
 4987:     }
 4988:     if (ref($opturef) eq 'HASH') {
 4989:         %optuserfields = %{$opturef};
 4990:     }
 4991:     if (ref($defref) eq 'HASH') {
 4992:         %defaults = %{$defref};
 4993:     }
 4994:     if (ref($extref) eq 'HASH') {
 4995:         %extended = %{$extref};
 4996:     }
 4997:     if (ref($crsref) eq 'HASH') {
 4998:         %crsconf = %{$crsref};
 4999:     }
 5000:     if (ref($rolesref) eq 'ARRAY') {
 5001:         @courseroles = @{$rolesref};
 5002:     }
 5003:     if (ref($ltiref) eq 'ARRAY') {
 5004:         @ltiroles = @{$ltiref};
 5005:     }
 5006:     my $datatable;
 5007:     my $css_class;
 5008:     if (keys(%ordered)) {
 5009:         my @items = sort { $a <=> $b } keys(%ordered);
 5010:         for (my $i=0; $i<@items; $i++) {
 5011:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5012:             my $provider = $ordered{$items[$i]};
 5013:             my $optionsty = 'none';
 5014:             my ($available,$version,$lifetime,$imgsrc,$userincdom,$showroles,
 5015:                 %checkedfields,%rolemaps,%inuse,%crsconfig,%current);
 5016:             if (ref($settings) eq 'HASH') {
 5017:                 if (ref($settings->{$provider}) eq 'HASH') {
 5018:                     %current = %{$settings->{$provider}};
 5019:                     if ($current{'available'}) {
 5020:                         $optionsty = 'block';
 5021:                         $available = 1;
 5022:                     }
 5023:                     if ($current{'lifetime'} =~ /^\d+$/) {
 5024:                         $lifetime = $current{'lifetime'};
 5025:                     }
 5026:                     if ($current{'version'} =~ /^\d+\.\d+$/) {
 5027:                         $version = $current{'version'};
 5028:                     }
 5029:                     if ($current{'image'} ne '') {
 5030:                         $imgsrc = '<img src="'.$current{'image'}.'" alt="'.&mt('Proctoring service icon').'" />';
 5031:                     }
 5032:                     if (ref($current{'fields'}) eq 'ARRAY') {
 5033:                         map { $checkedfields{$_} = 1; } @{$current{'fields'}};
 5034:                     }
 5035:                     $userincdom = $current{'incdom'};
 5036:                     if (ref($current{'roles'}) eq 'HASH') {
 5037:                         %rolemaps = %{$current{'roles'}};
 5038:                         $checkedfields{'roles'} = 1;
 5039:                     }
 5040:                     if (ref($current{'defaults'}) eq 'ARRAY') {
 5041:                         foreach my $val (@{$current{'defaults'}}) {
 5042:                             if (grep(/^\Q$val\E$/,@{$defaults{$provider}})) {
 5043:                                 $inuse{$val} = 1;
 5044:                             } else {
 5045:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5046:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5047:                                         if (grep(/^\Q$val\E$/,@{$extended{$provider}{$poss}})) {
 5048:                                             $inuse{$poss} = $val;
 5049:                                             last;
 5050:                                         }
 5051:                                     }
 5052:                                 }
 5053:                             }
 5054:                         }
 5055:                     } elsif (ref($current{'defaults'}) eq 'HASH') {
 5056:                         foreach my $key (keys(%{$current{'defaults'}})) {
 5057:                             my $currval = $current{'defaults'}{$key}; 
 5058:                             if (grep(/^\Q$key\E$/,@{$defaults{$provider}})) {
 5059:                                 $inuse{$key} = 1;
 5060:                             } else {
 5061:                                 my $match;
 5062:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5063:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5064:                                         if (grep(/^\Q$key\E$/,@{$extended{$provider}{$poss}})) {
 5065:                                             $inuse{$poss} = $key;
 5066:                                             last;
 5067:                                         }
 5068:                                     } elsif (ref($extended{$provider}{$poss}) eq 'HASH') {
 5069:                                         foreach my $inner (sort(keys(%{$extended{$provider}{$poss}}))) {
 5070:                                             if (ref($extended{$provider}{$poss}{$inner}) eq 'ARRAY') {
 5071:                                                 if (grep(/^\Q$currval\E$/,@{$extended{$provider}{$poss}{$inner}})) {
 5072:                                                     $currentdef{$inner} = $currval;
 5073:                                                     $match = 1;
 5074:                                                     last;
 5075:                                                 }
 5076:                                             } elsif ($inner eq $key) {
 5077:                                                 $currentdef{$key} = $currval;
 5078:                                                 $match = 1;
 5079:                                                 last;
 5080:                                             }
 5081:                                         }
 5082:                                     }
 5083:                                     last if ($match);
 5084:                                 }
 5085:                             }
 5086:                         }
 5087:                     }
 5088:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
 5089:                         map { $crsconfig{$_} = 1; } @{$current{'crsconf'}};
 5090:                     }
 5091:                 }
 5092:             }
 5093:             my %lt = &proctoring_titles($provider);
 5094:             my %fieldtitles = &proctoring_fieldtitles($provider);
 5095:             my $onclickavailable = ' onclick="toggleProctoring(this.form,'."'$provider'".');"';
 5096:             my %checkedavailable = (
 5097:                                    yes => '',
 5098:                                    no  => ' checked="checked"',
 5099:                                  );
 5100:             if ($available) {
 5101:                 $checkedavailable{'yes'} = $checkedavailable{'no'};
 5102:                 $checkedavailable{'no'} = '';
 5103:             }
 5104:             my $chgstr = ' onchange="javascript:reorderProctoring(this.form,'."'proctoring_pos_".$provider."'".');"';
 5105:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5106:                          .'<select name="proctoring_pos_'.$provider.'"'.$chgstr.'>';
 5107:             for (my $k=0; $k<$maxnum; $k++) {
 5108:                 my $vpos = $k+1;
 5109:                 my $selstr;
 5110:                 if ($k == $i) {
 5111:                     $selstr = ' selected="selected" ';
 5112:                 }
 5113:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5114:             }
 5115:             if ($version eq '') {
 5116:                 if ($provider eq 'proctorio') {
 5117:                     $version = '1.0';
 5118:                 } elsif ($provider eq 'examity') {
 5119:                     $version = '1.1';
 5120:                 }
 5121:             }
 5122:             if ($lifetime eq '') {
 5123:                 $lifetime = '300';
 5124:             }
 5125:             $datatable .=
 5126:                 '</select>'.('&nbsp;'x2).'<b>'.$providernames{$provider}.'</b></span><br />'.
 5127:                 '<span class="LC_nobreak">'.$lt{'avai'}.'&nbsp;'.
 5128:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="1"'.$onclickavailable.$checkedavailable{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 5129:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="0"'.$onclickavailable.$checkedavailable{no}.' />'.&mt('No').'</label></span>'."\n".
 5130:                 '</td>'.
 5131:                 '<td colspan="2">'.
 5132:                 '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'base'}.'</legend>'.
 5133:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="proctoring_'.$provider.'_version">'.
 5134:                 '<option value="'.$version.'" selected="selected">'.$version.'</option></select></span> '."\n".
 5135:                 ('&nbsp;'x2).
 5136:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="proctoring_'.$provider.'_sigmethod">'.
 5137:                 '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 5138:                 '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 5139:                 ('&nbsp;'x2).
 5140:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="proctoring_'.$provider.'_lifetime" value="'.$lifetime.'" /></span> '."\n".
 5141:                 '<br />'.
 5142:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="proctoring_'.$provider.'_url" value="'.$current{'url'}.'" /></span> '."\n".
 5143:                 '<br />'.
 5144:                 '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="proctoring_'.$provider.'_key" value="'.$current{'key'}.'" /></span> '."\n".
 5145:                 ('&nbsp;'x2).
 5146:                 '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="proctoring_'.$provider.'_secret" value="'.$current{'secret'}.'" />'.
 5147:                 '<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";
 5148:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 5149:             if ($imgsrc) {
 5150:                 $datatable .= $imgsrc.
 5151:                               '<label><input type="checkbox" name="proctoring_image_del"'.
 5152:                               ' value="'.$provider.'" />'.&mt('Delete?').'</label></span> '.
 5153:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:');
 5154:             }
 5155:             $datatable .= '&nbsp;';
 5156:             if ($switchserver) {
 5157:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5158:             } else {
 5159:                 $datatable .= '<input type="file" name="proctoring_image_'.$provider.'" value="" />';
 5160:             }
 5161:             unless ($imgsrc) {
 5162:                 $datatable .= '<br />('.&mt('if larger than 21x21 pixels, image will be scaled').')';
 5163:             }
 5164:             $datatable .= '</fieldset>'."\n";
 5165:             if (ref($requserfields{$provider}) eq 'ARRAY') {
 5166:                 if (@{$requserfields{$provider}} > 0) {
 5167:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'requ'}.'</legend>';
 5168:                     foreach my $field (@{$requserfields{$provider}}) {
 5169:                         $datatable .= '<span class="LC_nobreak">'.
 5170:                                       '<label><input type="checkbox" name="proctoring_reqd_'.$provider.'" value="'.$field.'" checked="checked" disabled="disabled" />'.
 5171:                                       $lt{$field}.'</label>';
 5172:                         if ($field eq 'user') {
 5173:                             my $seluserdom = '';
 5174:                             my $unseluserdom = ' selected="selected"';
 5175:                             if ($userincdom) {
 5176:                                 $seluserdom = $unseluserdom;
 5177:                                 $unseluserdom = '';
 5178:                             }
 5179:                             $datatable .= ':&nbsp;'.
 5180:                                 '<select name="proctoring_userincdom_'.$provider.'">'.
 5181:                                 '<option value="0"'.$unseluserdom.'>'.$lt{'username'}.'</option>'.
 5182:                                 '<option value="1"'.$seluserdom.'>'.$lt{'uname:dom'}.'</option>'.
 5183:                                 '</select>&nbsp;';
 5184:                         } else {
 5185:                             $datatable .= '&nbsp;';
 5186:                             if ($field eq 'roles') {
 5187:                                 $showroles = 1; 
 5188:                             } 
 5189:                         }
 5190:                         $datatable .= '</span> ';
 5191:                     }
 5192:                 }
 5193:                 $datatable .= '</fieldset>'."\n";
 5194:             }
 5195:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
 5196:                 if (@{$optuserfields{$provider}} > 0) {
 5197:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'optu'}.'</legend>'; 
 5198:                     foreach my $field (@{$optuserfields{$provider}}) {
 5199:                         my $checked;
 5200:                         if ($checkedfields{$field}) {
 5201:                             $checked = ' checked="checked"';
 5202:                         }
 5203:                         $datatable .= '<span class="LC_nobreak">'.
 5204:                                       '<label><input type="checkbox" name="proctoring_optional_'.$provider.'" value="'.$field.'"'.$checked.' />'.$lt{$field}.'</label></span>&nbsp; ';
 5205:                     }
 5206:                     $datatable .= '</fieldset>'."\n";
 5207:                 }
 5208:             }
 5209:             if (ref($defaults{$provider}) eq 'ARRAY') {
 5210:                 if (@{$defaults{$provider}}) {
 5211:                     my (%options,@selectboxes);
 5212:                     if (ref($extended{$provider}) eq 'HASH') {
 5213:                         %options = %{$extended{$provider}};
 5214:                     }
 5215:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'defa'}.'</legend>';
 5216:                     my ($rem,$numinrow,$dropdowns);
 5217:                     if ($provider eq 'proctorio') { 
 5218:                         $datatable .= '<table>';
 5219:                         $numinrow = 4;
 5220:                     }
 5221:                     my $i = 0;
 5222:                     foreach my $field (@{$defaults{$provider}}) {
 5223:                         my $checked;
 5224:                         if ($inuse{$field}) {
 5225:                             $checked = ' checked="checked"';
 5226:                         }
 5227:                         if ($provider eq 'examity') {
 5228:                             if ($field eq 'display') {
 5229:                                 $datatable .= '<span class="LC_nobreak">'.&mt('Display target:');
 5230:                                 foreach my $option ('iframe','tab','window') {
 5231:                                     my $checkdisp;
 5232:                                     if ($currentdef{'target'} eq $option) {
 5233:                                         $checkdisp = ' checked="checked"';
 5234:                                     }
 5235:                                     $datatable .= '<label><input type="radio" name="proctoring_target_'.$provider.'" value="'.$option.'"'.$checkdisp.' />'.
 5236:                                     $fieldtitles{$option}.'</label>'.('&nbsp;'x2);
 5237:                                 }
 5238:                                 $datatable .= ('&nbsp;'x4);
 5239:                                 foreach my $dimen ('width','height') {
 5240:                                     $datatable .= '<label>'.$fieldtitles{$dimen}.'&nbsp;'.
 5241:                                                   '<input type="text" name="proctoring_'.$dimen.'_'.$provider.'" size="5" '.
 5242:                                                   'value="'.$currentdef{$dimen}.'" /></label>'.
 5243:                                                   ('&nbsp;'x2);
 5244:                                 }
 5245:                                 $datatable .= '</span><br />'.
 5246:                                               '<div class="LC_left_float">'.$fieldtitles{'linktext'}.'<br />'.
 5247:                                               '<input type="text" name="proctoring_linktext_'.$provider.'" '.
 5248:                                               'size="25" value="'.$currentdef{'linktext'}.'" /></div>'.
 5249:                                               '<div class="LC_left_float">'.$fieldtitles{'explanation'}.'<br />'.
 5250:                                               '<textarea name="proctoring_explanation_'.$provider.'" rows="5" cols="40">'.
 5251:                                               $currentdef{'explanation'}.
 5252:                                               '</textarea></div><div style=""></div><br />';
 5253:                             }
 5254:                         } else {
 5255:                             if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
 5256:                                 my ($output,$selnone);
 5257:                                 unless ($checked) {
 5258:                                     $selnone = ' selected="selected"';
 5259:                                 }
 5260:                                 $output .= '<span class="LC_nobreak">'.$fieldtitles{$field}.': '.
 5261:                                            '<select name="proctoring_defaults_'.$field.'_'.$provider.'">'.
 5262:                                            '<option value=""'.$selnone.'>'.&mt('Not in use').'</option>'; 
 5263:                                 foreach my $option (@{$options{$field}}) {
 5264:                                     my $sel; 
 5265:                                     if ($inuse{$field} eq $option) {
 5266:                                         $sel = ' selected="selected"';
 5267:                                     }
 5268:                                     $output .= '<option value="'.$option.'"'.$sel.'>'.$fieldtitles{$option}.'</option>';
 5269:                                 }
 5270:                                 $output .= '</select></span>';
 5271:                                 push(@selectboxes,$output);
 5272:                             } else {
 5273:                                 $rem = $i%($numinrow);
 5274:                                 if ($rem == 0) {
 5275:                                     if ($i > 0) {
 5276:                                         $datatable .= '</tr>';
 5277:                                     }
 5278:                                     $datatable .= '<tr>';
 5279:                                 }
 5280:                                 $datatable .= '<td class="LC_left_item">'.
 5281:                                               '<span class="LC_nobreak">'.
 5282:                                               '<label><input type="checkbox" name="proctoring_defaults_'.$provider.'" value="'.$field.'"'.$checked.' />'.
 5283:                                               $fieldtitles{$field}.'</label></span></td>';
 5284:                                 $i++;
 5285:                             }
 5286:                         }
 5287:                     }
 5288:                     if ($provider eq 'proctorio') {
 5289:                         if ($numinrow) {
 5290:                             $rem = $i%$numinrow;
 5291:                         }
 5292:                         my $colsleft = $numinrow - $rem;
 5293:                         if ($colsleft > 1) {
 5294:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5295:                         } else {
 5296:                             $datatable .= '<td class="LC_left_item">';
 5297:                         }
 5298:                         $datatable .= '&nbsp;'.
 5299:                                       '</td></tr></table>';
 5300:                         if (@selectboxes) {
 5301:                             $datatable .= '<hr /><table>';
 5302:                             $numinrow = 2;
 5303:                             for (my $i=0; $i<@selectboxes; $i++) {
 5304:                                 $rem = $i%($numinrow);
 5305:                                 if ($rem == 0) {
 5306:                                     if ($i > 0) {
 5307:                                         $datatable .= '</tr>';
 5308:                                     }
 5309:                                     $datatable .= '<tr>';
 5310:                                 }
 5311:                                 $datatable .= '<td class="LC_left_item">'.
 5312:                                               $selectboxes[$i].'</td>';
 5313:                             }
 5314:                             if ($numinrow) {
 5315:                                 $rem = $i%$numinrow;
 5316:                             }
 5317:                             $colsleft = $numinrow - $rem;
 5318:                             if ($colsleft > 1) {
 5319:                                 $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5320:                             } else {
 5321:                                 $datatable .= '<td class="LC_left_item">';
 5322:                             }
 5323:                             $datatable .= '&nbsp;'.
 5324:                                           '</td></tr></table>';
 5325:                         }
 5326:                     }
 5327:                     $datatable .= '</fieldset>';
 5328:                 }
 5329:                 if (ref($crsconf{$provider}) eq 'ARRAY') {
 5330:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5331:                                   '<legend>'.&mt('Configurable in course').'</legend>';
 5332:                     my ($rem,$numinrow);
 5333:                     if ($provider eq 'proctorio') {
 5334:                         $datatable .= '<table>';
 5335:                         $numinrow = 4;
 5336:                     }
 5337:                     my $i = 0;
 5338:                     foreach my $item (@{$crsconf{$provider}}) {
 5339:                         my $name;
 5340:                         if ($provider eq 'examity') {
 5341:                             $name = $lt{'crs'.$item};
 5342:                         } elsif ($provider eq 'proctorio') {
 5343:                             $name = $fieldtitles{$item};
 5344:                             $rem = $i%($numinrow);
 5345:                             if ($rem == 0) {
 5346:                                 if ($i > 0) {
 5347:                                     $datatable .= '</tr>';
 5348:                                 }
 5349:                                 $datatable .= '<tr>';
 5350:                             }
 5351:                             $datatable .= '<td class="LC_left_item>';
 5352:                         }
 5353:                         my $checked;
 5354:                         if ($crsconfig{$item}) {
 5355:                             $checked = ' checked="checked"';
 5356:                         }
 5357:                         $datatable .= '<span class="LC_nobreak"><label>'.
 5358:                                       '<input type="checkbox" name="proctoring_crsconf_'.$provider.'" value="'.$item.'"'.$checked.' />'.
 5359:                                       $name.'</label></span>';
 5360:                         if ($provider eq 'examity') {
 5361:                             $datatable .= '&nbsp; ';
 5362:                         }
 5363:                         $datatable .= "\n";
 5364:                         $i++;
 5365:                     }
 5366:                     if ($provider eq 'proctorio') {
 5367:                         if ($numinrow) {
 5368:                             $rem = $i%$numinrow;
 5369:                         }
 5370:                         my $colsleft = $numinrow - $rem;
 5371:                         if ($colsleft > 1) {
 5372:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5373:                         } else {
 5374:                             $datatable .= '<td class="LC_left_item">';
 5375:                         }
 5376:                         $datatable .= '&nbsp;'.
 5377:                                       '</td></tr></table>';
 5378:                     }
 5379:                     $datatable .= '</fieldset>';
 5380:                 }
 5381:                 if ($showroles) {
 5382:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5383:                                   '<legend>'.&mt('Role mapping').'</legend><table><tr>';
 5384:                     foreach my $role (@courseroles) {
 5385:                         my ($selected,$selectnone);
 5386:                         if (!$rolemaps{$role}) {
 5387:                             $selectnone = ' selected="selected"';
 5388:                         }
 5389:                         $datatable .= '<td style="text-align: center">'.
 5390:                                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 5391:                                       '<select name="proctoring_roles_'.$role.'_'.$provider.'">'.
 5392:                                       '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5393:                         foreach my $ltirole (@ltiroles) {
 5394:                             unless ($selectnone) {
 5395:                                 if ($rolemaps{$role} eq $ltirole) {
 5396:                                     $selected = ' selected="selected"';
 5397:                                 } else {
 5398:                                     $selected = '';
 5399:                                 }
 5400:                             }
 5401:                             $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 5402:                         }
 5403:                         $datatable .= '</select></td>';
 5404:                     }
 5405:                     $datatable .= '</tr></table></fieldset>'.
 5406:                                   '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5407:                                   '<legend>'.&mt('Custom items sent on launch').'</legend>'.
 5408:                                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 5409:                                   '<tr><td></td><td>lms</td>'.
 5410:                                   '<td><input type="text" name="proctoring_customval_lms_'.$provider.'"'.
 5411:                                   ' value="Loncapa" disabled="disabled"/></td></tr>';
 5412:                     if ((ref($settings) eq 'HASH') && (ref($settings->{$provider}) eq 'HASH') &&
 5413:                         (ref($settings->{$provider}->{'custom'}) eq 'HASH')) {
 5414:                         my %custom = %{$settings->{$provider}->{'custom'}};
 5415:                         if (keys(%custom) > 0) {
 5416:                             foreach my $key (sort(keys(%custom))) {
 5417:                                 next if ($key eq 'lms');
 5418:                                 $datatable .= '<tr><td><span class="LC_nobreak">'.
 5419:                                               '<label><input type="checkbox" name="proctoring_customdel_'.$provider.'" value="'.
 5420:                                               $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 5421:                                               '<td><input type="text" name="proctoring_customval_'.$key.'_'.$provider.'"'.
 5422:                                               ' value="'.$custom{$key}.'" /></td></tr>';
 5423:                             }
 5424:                         }
 5425:                     }
 5426:                     $datatable .= '<tr><td><span class="LC_nobreak">'.
 5427:                                   '<label><input type="checkbox" name="proctoring_customadd" value="'.$provider.'" />'.
 5428:                                   &mt('Add more').'</label></span></td><td><input type="text" name="proctoring_custom_name_'.$provider.'" />'.
 5429:                                   '</td><td><input type="text" name="proctoring_custom_value_'.$provider.'" /></td></tr>'.
 5430:                                   '</table></fieldset></td></tr>'."\n";
 5431:                 }
 5432:                 $datatable .= '</td></tr>';
 5433:             }
 5434:             $itemcount ++;
 5435:         }
 5436:     }
 5437:     return $datatable;
 5438: }
 5439: 
 5440: sub proctoring_data {
 5441:     my $requserfields = {
 5442:                       proctorio => ['user'],
 5443:                       examity   => ['roles','user'],
 5444:                      };
 5445:     my $optuserfields = {
 5446:                         proctorio => ['fullname'],
 5447:                         examity   => ['fullname','firstname','lastname','email'],
 5448:                      };
 5449:     my $defaults = {
 5450:                   proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 5451:                                 'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 5452:                                 'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 5453:                                 'closetabs','onescreen','print','downloads','cache','rightclick',
 5454:                                 'reentry','calculator','whiteboard'],
 5455:                   examity   => ['display'],
 5456:                 };
 5457:     my $extended = { 
 5458:                   proctorio => {
 5459:                                  verifyid => ['verifyidauto','verifyidlive'],
 5460:                                  fullscreen => ['fullscreenlenient','fullscreenmoderate','fullscreensever'],
 5461:                                  tabslinks => ['notabs','linksonly'],
 5462:                                  reentry => ['noreentry','agentreentry'],
 5463:                                  calculator => ['calculatorbasic','calculatorsci'],
 5464:                                },
 5465:                   examity => {
 5466:                                display => {
 5467:                                            target      => ['iframe','tab','window'],
 5468:                                            width       => '',
 5469:                                            height      => '',
 5470:                                            linktext    => '',
 5471:                                            explanation => '',
 5472:                                           },
 5473:                              },
 5474:                 };
 5475:     my $crsconf = {
 5476:                  proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 5477:                                'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 5478:                                'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 5479:                                'closetabs','onescreen','print','downloads','cache','rightclick',
 5480:                                'reentry','calculator','whiteboard'],
 5481:                  examity => ['label','title','target','linktext','explanation','append'],
 5482:                };
 5483:     my $courseroles = ['cc','in','ta','ep','st'];
 5484:     my $ltiroles = ['Instructor','ContentDeveloper','TeachingAssistant','Learner'];
 5485:     return ($requserfields,$optuserfields,$defaults,$extended,$crsconf,$courseroles,$ltiroles);
 5486: }
 5487: 
 5488: sub proctoring_titles {
 5489:     my ($item) = @_;
 5490:     my (%common_lt,%custom_lt);
 5491:     %common_lt = &Apache::lonlocal::texthash (
 5492:                      'avai'      => 'Available?',
 5493:                      'base'      => 'Basic Settings',
 5494:                      'requ'      => 'User data required to be sent on launch',
 5495:                      'optu'      => 'User data optionally sent on launch',
 5496:                      'udsl'      => 'User data sent on launch',
 5497:                      'defa'      => 'Defaults for items configurable in course',
 5498:                      'sigmethod' => 'Signature Method',
 5499:                      'key'       => 'Key',
 5500:                      'lifetime'  => 'Nonce lifetime (s)',
 5501:                      'secret'    => 'Secret',
 5502:                      'icon'      => 'Icon',
 5503:                      'fullname'  => 'Full Name',
 5504:                      'visible'   => 'Visible input',
 5505:                      'username'  => 'username',
 5506:                      'user'      => 'User',
 5507:                  );
 5508:     if ($item eq 'proctorio') {
 5509:         %custom_lt = &Apache::lonlocal::texthash (
 5510:                          'version'        => 'OAuth version',
 5511:                          'url'            => 'API URL',
 5512:                          'uname:dom'      => 'username-domain',
 5513:         );
 5514:     } elsif ($item eq 'examity') {
 5515:         %custom_lt = &Apache::lonlocal::texthash (
 5516:                          'version'        => 'LTI Version',
 5517:                          'url'            => 'URL',
 5518:                          'uname:dom'      => 'username:domain',
 5519:                          'msgtype'        => 'Message Type',
 5520:                          'firstname'      => 'First Name',
 5521:                          'lastname'       => 'Last Name',
 5522:                          'email'          => 'E-mail',
 5523:                          'roles'          => 'Role',
 5524:                          'crstarget'      => 'Display target',
 5525:                          'crslabel'       => 'Course label',
 5526:                          'crstitle'       => 'Course title', 
 5527:                          'crslinktext'    => 'Link Text',
 5528:                          'crsexplanation' => 'Explanation',
 5529:                          'crsappend'      => 'Provider URL',
 5530:         );
 5531:     }
 5532:     my %lt = (%common_lt,%custom_lt);
 5533:     return %lt;
 5534: }
 5535: 
 5536: sub proctoring_fieldtitles {
 5537:     my ($item) = @_;
 5538:     if ($item eq 'proctorio') {
 5539:         return &Apache::lonlocal::texthash (
 5540:                   'recordvideo' => 'Record video',
 5541:                   'recordaudio' => 'Record audio',
 5542:                   'recordscreen' => 'Record screen',
 5543:                   'recordwebtraffic' => 'Record web traffic',
 5544:                   'recordroomstart' => 'Record room scan',
 5545:                   'verifyvideo' => 'Verify webcam',
 5546:                   'verifyaudio' => 'Verify microphone',
 5547:                   'verifydesktop' => 'Verify desktop recording',
 5548:                   'verifyid' => 'Photo ID verification',
 5549:                   'verifysignature' => 'Require signature',
 5550:                   'fullscreen' => 'Fullscreen',
 5551:                   'clipboard' => 'Disable copy/paste',
 5552:                   'tabslinks' => 'New tabs/windows',
 5553:                   'closetabs' => 'Close other tabs',
 5554:                   'onescreen' => 'Limit to single screen',
 5555:                   'print' => 'Disable Printing',
 5556:                   'downloads' => 'Disable Downloads',
 5557:                   'cache' => 'Empty cache after exam',
 5558:                   'rightclick' => 'Disable right click',
 5559:                   'reentry' => 'Re-entry to exam',
 5560:                   'calculator' => 'Onscreen calculator',
 5561:                   'whiteboard' => 'Onscreen whiteboard',
 5562:                   'verifyidauto' => 'Automated verification',
 5563:                   'verifyidlive' => 'Live agent verification',
 5564:                   'fullscreenlenient' => 'Forced, but can navigate away for up to 30s',
 5565:                   'fullscreenmoderate' => 'Forced, but can navigate away for up to 15s',
 5566:                   'fullscreensever' => 'Forced, navigation away ends exam',
 5567:                   'notabs' => 'Disaallowed',
 5568:                   'linksonly' => 'Allowed from links in exam',
 5569:                   'noreentry' => 'Disallowed',
 5570:                   'agentreentry' => 'Agent required for re-entry',
 5571:                   'calculatorbasic' => 'Basic',
 5572:                   'calculatorsci' => 'Scientific',
 5573:         );
 5574:     } elsif ($item eq 'examity') {
 5575:         return &Apache::lonlocal::texthash (
 5576:                 'target'         => 'Display target',   
 5577:                 'window'         => 'Window',
 5578:                 'tab'            => 'Tab',
 5579:                 'iframe'         => 'iFrame',
 5580:                 'height'         => 'Height (pixels)',
 5581:                 'width'          => 'Width (pixels)',
 5582:                 'linktext'       => 'Default Link Text',
 5583:                 'explanation'    => 'Default Explanation',
 5584:                 'append'         => 'Provider URL',
 5585:         );
 5586:     }
 5587: }
 5588: 
 5589: sub proctoring_providernames {
 5590:     return (
 5591:              proctorio => 'Proctorio',
 5592:              examity   => 'Examity',
 5593:            );
 5594: }
 5595: 
 5596: sub print_lti {
 5597:     my ($dom,$settings,$rowtotal) = @_;
 5598:     my $itemcount = 1;
 5599:     my $maxnum = 0;
 5600:     my $css_class;
 5601:     my %ordered;
 5602:     if (ref($settings) eq 'HASH') {
 5603:         foreach my $item (keys(%{$settings})) {
 5604:             if (ref($settings->{$item}) eq 'HASH') {
 5605:                 my $num = $settings->{$item}{'order'};
 5606:                 $ordered{$num} = $item;
 5607:             }
 5608:         }
 5609:     }
 5610:     my $maxnum = scalar(keys(%ordered));
 5611:     my $datatable;
 5612:     my %lt = &lti_names();
 5613:     if (keys(%ordered)) {
 5614:         my @items = sort { $a <=> $b } keys(%ordered);
 5615:         for (my $i=0; $i<@items; $i++) {
 5616:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5617:             my $item = $ordered{$items[$i]};
 5618:             my ($key,$secret,$lifetime,$consumer,$requser,$current);
 5619:             if (ref($settings->{$item}) eq 'HASH') {
 5620:                 $key = $settings->{$item}->{'key'};
 5621:                 $secret = $settings->{$item}->{'secret'};
 5622:                 $lifetime = $settings->{$item}->{'lifetime'};
 5623:                 $consumer = $settings->{$item}->{'consumer'};
 5624:                 $requser = $settings->{$item}->{'requser'};
 5625:                 $current = $settings->{$item};
 5626:             }
 5627:             my $onclickrequser = ' onclick="toggleLTI(this.form,'."'requser','$i'".');"';
 5628:             my %checkedrequser = (
 5629:                                    yes => ' checked="checked"',
 5630:                                    no  => '',
 5631:                                  );
 5632:             if (!$requser) {
 5633:                 $checkedrequser{'no'} = $checkedrequser{'yes'};
 5634:                 $checkedrequser{'yes'} = '';
 5635:             }
 5636:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_".$item."'".');"';
 5637:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5638:                          .'<select name="lti_pos_'.$item.'"'.$chgstr.'>';
 5639:             for (my $k=0; $k<=$maxnum; $k++) {
 5640:                 my $vpos = $k+1;
 5641:                 my $selstr;
 5642:                 if ($k == $i) {
 5643:                     $selstr = ' selected="selected" ';
 5644:                 }
 5645:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5646:             }
 5647:             $datatable .= '</select>'.('&nbsp;'x2).
 5648:                 '<label><input type="checkbox" name="lti_del" value="'.$item.'" />'.
 5649:                 &mt('Delete?').'</label></span></td>'.
 5650:                 '<td colspan="2">'.
 5651:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 5652:                 '<span class="LC_nobreak">'.$lt{'consumer'}.
 5653:                 ':<input type="text" size="15" name="lti_consumer_'.$i.'" value="'.$consumer.'" /></span> '.
 5654:                 ('&nbsp;'x2).
 5655:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_'.$i.'">'.
 5656:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 5657:                 ('&nbsp;'x2).
 5658:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" name="lti_lifetime_'.$i.'"'.
 5659:                 'value="'.$lifetime.'" size="3" /></span>'.
 5660:                 ('&nbsp;'x2).
 5661:                  '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 5662:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="1"'.$onclickrequser.$checkedrequser{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 5663:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="0"'.$onclickrequser.$checkedrequser{no}.' />'.&mt('No').'</label></span>'."\n".
 5664:                 '<br /><br />'.
 5665:                 '<span class="LC_nobreak">'.$lt{'key'}.
 5666:                 ':<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" /></span> '.
 5667:                 ('&nbsp;'x2).
 5668:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 5669:                 '<input type="password" size="20" name="lti_secret_'.$i.'" value="'.$secret.'" />'.
 5670:                 '<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>'.
 5671:                 '<input type="hidden" name="lti_id_'.$i.'" value="'.$item.'" /></span>'.
 5672:                 '</fieldset>'.&lti_options($i,$current,$itemcount,%lt).'</td></tr>';
 5673:             $itemcount ++;
 5674:         }
 5675:     }
 5676:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5677:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_add'".');"';
 5678:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 5679:                   '<input type="hidden" name="lti_maxnum" value="'.$maxnum.'" />'."\n".
 5680:                   '<select name="lti_pos_add"'.$chgstr.'>';
 5681:     for (my $k=0; $k<$maxnum+1; $k++) {
 5682:         my $vpos = $k+1;
 5683:         my $selstr;
 5684:         if ($k == $maxnum) {
 5685:             $selstr = ' selected="selected" ';
 5686:         }
 5687:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5688:     }
 5689:     $datatable .= '</select>&nbsp;'."\n".
 5690:                   '<input type="checkbox" name="lti_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 5691:                   '<td colspan="2">'.
 5692:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 5693:                   '<span class="LC_nobreak">'.$lt{'consumer'}.
 5694:                   ':<input type="text" size="15" name="lti_consumer_add" value="" /></span> '."\n".
 5695:                   ('&nbsp;'x2).
 5696:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_add">'.
 5697:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 5698:                   ('&nbsp;'x2).
 5699:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="3" name="lti_lifetime_add" value="300" /></span> '."\n".
 5700:                   ('&nbsp;'x2).
 5701:                   '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 5702:                   '<label><input type="radio" name="lti_requser_add" value="1" onclick="toggleLTI(this.form,'."'requser','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 5703:                   '<label><input type="radio" name="lti_requser_add" value="0" onclick="toggleLTI(this.form,'."'requser','add'".');" />'.&mt('No').'</label></span>'."\n".
 5704:                   '<br /><br />'.
 5705:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="lti_key_add" value="" /></span> '."\n".
 5706:                   ('&nbsp;'x2).
 5707:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="lti_secret_add" value="" />'.
 5708:                   '<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".
 5709:                   '</fieldset>'.&lti_options('add',undef,$itemcount,%lt).
 5710:                   '</td>'."\n".
 5711:                   '</tr>'."\n";
 5712:     $$rowtotal ++;
 5713:     return $datatable;;
 5714: }
 5715: 
 5716: sub lti_names {
 5717:     my %lt = &Apache::lonlocal::texthash(
 5718:                                           'version'   => 'LTI Version',
 5719:                                           'url'       => 'URL',
 5720:                                           'key'       => 'Key',
 5721:                                           'lifetime'  => 'Nonce lifetime (s)',
 5722:                                           'consumer'  => 'Consumer',
 5723:                                           'secret'    => 'Secret',
 5724:                                           'requser'   => "User's identity sent",
 5725:                                           'email'     => 'Email address',
 5726:                                           'sourcedid' => 'User ID',
 5727:                                           'other'     => 'Other',
 5728:                                           'passback'  => 'Can return grades to Consumer:',
 5729:                                           'roster'    => 'Can retrieve roster from Consumer:',
 5730:                                           'topmenu'   => 'Display LON-CAPA page header',
 5731:                                           'inlinemenu'=> 'Display LON-CAPA inline menu',
 5732:                                         );
 5733:     return %lt;
 5734: }
 5735: 
 5736: sub lti_options {
 5737:     my ($num,$current,$itemcount,%lt) = @_;
 5738:     my (%checked,%rolemaps,$crssecsrc,$userfield,$cidfield,$callback);
 5739:     $checked{'mapuser'}{'sourcedid'} = ' checked="checked"';
 5740:     $checked{'mapcrs'}{'course_offering_sourcedid'} = ' checked="checked"';
 5741:     $checked{'makecrs'}{'N'} = '  checked="checked"';
 5742:     $checked{'mapcrstype'} = {};
 5743:     $checked{'makeuser'} = {};
 5744:     $checked{'selfenroll'} = {};
 5745:     $checked{'crssec'} = {};
 5746:     $checked{'crssecsrc'} = {};
 5747:     $checked{'lcauth'} = {};
 5748:     $checked{'menuitem'} = {};
 5749:     if ($num eq 'add') {
 5750:         $checked{'lcauth'}{'lti'} = ' checked="checked"';
 5751:     }
 5752:     my $userfieldsty = 'none';
 5753:     my $crsfieldsty = 'none';
 5754:     my $crssecfieldsty = 'none';
 5755:     my $secsrcfieldsty = 'none';
 5756:     my $callbacksty = 'none';
 5757:     my $passbacksty = 'none';
 5758:     my $optionsty = 'block';
 5759:     my $lcauthparm;
 5760:     my $lcauthparmstyle = 'display:none';
 5761:     my $lcauthparmtext;
 5762:     my $menusty;
 5763:     my $numinrow = 4;
 5764:     my %menutitles = &ltimenu_titles();
 5765: 
 5766:     if (ref($current) eq 'HASH') {
 5767:         if (!$current->{'requser'}) {
 5768:             $optionsty = 'none';
 5769:         }
 5770:         if (($current->{'mapuser'} ne '') && ($current->{'mapuser'} ne 'lis_person_sourcedid')) {
 5771:             $checked{'mapuser'}{'sourcedid'} = '';
 5772:             if ($current->{'mapuser'} eq 'lis_person_contact_email_primary') {
 5773:                 $checked{'mapuser'}{'email'} = ' checked="checked"'; 
 5774:             } else {
 5775:                 $checked{'mapuser'}{'other'} = ' checked="checked"';
 5776:                 $userfield = $current->{'mapuser'};
 5777:                 $userfieldsty = 'inline-block';
 5778:             }
 5779:         }
 5780:         if (($current->{'mapcrs'} ne '') && ($current->{'mapcrs'} ne 'course_offering_sourcedid')) {
 5781:             $checked{'mapcrs'}{'course_offering_sourcedid'} = '';
 5782:             if ($current->{'mapcrs'} eq 'context_id') {
 5783:                 $checked{'mapcrs'}{'context_id'} = ' checked="checked"'; 
 5784:             } else {
 5785:                 $checked{'mapcrs'}{'other'} = ' checked="checked"';
 5786:                 $cidfield = $current->{'mapcrs'};
 5787:                 $crsfieldsty = 'inline-block';
 5788:             }
 5789:         }
 5790:         if (ref($current->{'mapcrstype'}) eq 'ARRAY') {
 5791:             foreach my $type (@{$current->{'mapcrstype'}}) {
 5792:                 $checked{'mapcrstype'}{$type} = ' checked="checked"';
 5793:             }
 5794:         }
 5795:         if ($current->{'makecrs'}) {
 5796:             $checked{'makecrs'}{'Y'} = '  checked="checked"';
 5797:         }
 5798:         if (ref($current->{'makeuser'}) eq 'ARRAY') {
 5799:             foreach my $role (@{$current->{'makeuser'}}) {
 5800:                 $checked{'makeuser'}{$role} = ' checked="checked"';
 5801:             }
 5802:         }
 5803:         if ($current->{'lcauth'} =~ /^(internal|localauth|krb4|krb5|lti)$/) {
 5804:             $checked{'lcauth'}{$1} = ' checked="checked"';
 5805:             unless (($current->{'lcauth'} eq 'lti') || ($current->{'lcauth'} eq 'internal')) {
 5806:                 $lcauthparm = $current->{'lcauthparm'};
 5807:                 $lcauthparmstyle = 'display:table-row'; 
 5808:                 if ($current->{'lcauth'} eq 'localauth') {
 5809:                     $lcauthparmtext = &mt('Local auth argument');
 5810:                 } else {
 5811:                     $lcauthparmtext = &mt('Kerberos domain');
 5812:                 }
 5813:             }
 5814:         }
 5815:         if (ref($current->{'selfenroll'}) eq 'ARRAY') {
 5816:             foreach my $role (@{$current->{'selfenroll'}}) {
 5817:                 $checked{'selfenroll'}{$role} = ' checked="checked"';
 5818:             }
 5819:         }
 5820:         if (ref($current->{'maproles'}) eq 'HASH') {
 5821:             %rolemaps = %{$current->{'maproles'}};
 5822:         }
 5823:         if ($current->{'section'} ne '') {
 5824:             $checked{'crssec'}{'Y'} = '  checked="checked"'; 
 5825:             $crssecfieldsty = 'inline-block';
 5826:             if ($current->{'section'} eq 'course_section_sourcedid') {
 5827:                 $checked{'crssecsrc'}{'sourcedid'} = ' checked="checked"';
 5828:             } else {
 5829:                 $checked{'crssecsrc'}{'other'} = ' checked="checked"';
 5830:                 $crssecsrc = $current->{'section'};
 5831:                 $secsrcfieldsty = 'inline-block';
 5832:             }
 5833:         } else {
 5834:             $checked{'crssec'}{'N'} = ' checked="checked"';
 5835:         }
 5836:         if ($current->{'callback'} ne '') {
 5837:             $callback = $current->{'callback'};
 5838:             $checked{'callback'}{'Y'} = ' checked="checked"';
 5839:             $callbacksty = 'inline-block';
 5840:         } else {
 5841:             $checked{'callback'}{'N'} = ' checked="checked"';
 5842:         }
 5843:         if ($current->{'topmenu'}) {
 5844:             $checked{'topmenu'}{'Y'} = ' checked="checked"';
 5845:         } else {
 5846:             $checked{'topmenu'}{'N'} = ' checked="checked"';
 5847:         }
 5848:         if ($current->{'inlinemenu'}) {
 5849:             $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 5850:         } else {
 5851:             $checked{'inlinemenu'}{'N'} = ' checked="checked"';
 5852:         }
 5853:         if (($current->{'topmenu'}) || ($current->{'inlinemenu'})) {
 5854:             $menusty = 'inline-block';
 5855:             if (ref($current->{'lcmenu'}) eq 'ARRAY') {
 5856:                 foreach my $item (@{$current->{'lcmenu'}}) {
 5857:                     if (exists($menutitles{$item})) {
 5858:                         $checked{'menuitem'}{$item} = ' checked="checked"';
 5859:                     }
 5860:                 }
 5861:             }
 5862:         } else {
 5863:             $menusty = 'none';
 5864:         }
 5865:     } else {
 5866:         $checked{'makecrs'}{'N'} = ' checked="checked"';
 5867:         $checked{'crssec'}{'N'} = ' checked="checked"';
 5868:         $checked{'callback'}{'N'} = ' checked="checked"';
 5869:         $checked{'topmenu'}{'N'} = ' checked="checked"';
 5870:         $checked{'inlinemenu'}{'Y'} = ' checked="checked"'; 
 5871:         $checked{'menuitem'}{'grades'} = ' checked="checked"';
 5872:         $menusty = 'inline-block'; 
 5873:     }
 5874:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
 5875:     my %coursetypetitles = &Apache::lonlocal::texthash (
 5876:                                official   => 'Official',
 5877:                                unofficial => 'Unofficial',
 5878:                                community  => 'Community',
 5879:                                textbook   => 'Textbook',
 5880:                                placement  => 'Placement Test',
 5881:                                lti        => 'LTI Provider',
 5882:     );
 5883:     my @authtypes = ('internal','krb4','krb5','localauth');
 5884:     my %shortauth = (
 5885:                      internal => 'int',
 5886:                      krb4 => 'krb4',
 5887:                      krb5 => 'krb5',
 5888:                      localauth  => 'loc'
 5889:                     );
 5890:     my %authnames = &authtype_names();
 5891:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
 5892:     my @lticourseroles = qw(Learner Instructor TeachingAssistant Mentor);
 5893:     my @courseroles = ('cc','in','ta','ep','st');
 5894:     my $onclickuser = ' onclick="toggleLTI(this.form,'."'user','$num'".');"';
 5895:     my $onclickcrs = ' onclick="toggleLTI(this.form,'."'crs','$num'".');"';
 5896:     my $onclicksec = ' onclick="toggleLTI(this.form,'."'sec','$num'".');"';
 5897:     my $onclickcallback = ' onclick="toggleLTI(this.form,'."'callback','$num'".');"';
 5898:     my $onclicksecsrc = ' onclick="toggleLTI(this.form,'."'secsrc','$num'".')"';
 5899:     my $onclicklcauth = ' onclick="toggleLTI(this.form,'."'lcauth','$num'".')"';
 5900:     my $onclickmenu = ' onclick="toggleLTI(this.form,'."'lcmenu','$num'".');"';
 5901:     my $output = '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping users').'</legend>'.
 5902:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('LON-CAPA username').':&nbsp;';
 5903:     foreach my $option ('sourcedid','email','other') {
 5904:         $output .= '<label><input type="radio" name="lti_mapuser_'.$num.'" value="'.$option.'"'.
 5905:                    $checked{'mapuser'}{$option}.$onclickuser.' />'.$lt{$option}.'</label>'.
 5906:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 5907:     }
 5908:     $output .= '</span></div>'.
 5909:                '<div class="LC_floatleft" style="display:'.$userfieldsty.';" id="lti_userfield_'.$num.'">'.
 5910:                '<input type="text" name="lti_customuser_'.$num.'" '.
 5911:                'value="'.$userfield.'" /></div></fieldset>'.
 5912:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping course roles').'</legend><table><tr>';
 5913:     foreach my $ltirole (@lticourseroles) {
 5914:         my ($selected,$selectnone);
 5915:         if ($rolemaps{$ltirole} eq '') {
 5916:             $selectnone = ' selected="selected"';
 5917:         }
 5918:         $output .= '<td style="text-align: center">'.$ltirole.'<br />'.
 5919:                    '<select name="lti_maprole_'.$ltirole.'_'.$num.'">'.
 5920:                    '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5921:         foreach my $role (@courseroles) {
 5922:             unless ($selectnone) {
 5923:                 if ($rolemaps{$ltirole} eq $role) {
 5924:                     $selected = ' selected="selected"';
 5925:                 } else {
 5926:                     $selected = '';
 5927:                 }
 5928:             }
 5929:             $output .= '<option value="'.$role.'"'.$selected.'>'.
 5930:                        &Apache::lonnet::plaintext($role,'Course').
 5931:                        '</option>';
 5932:         }
 5933:         $output .= '</select></td>';
 5934:     }
 5935:     $output .= '</tr></table></fieldset>'.
 5936:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may create user accounts').'</legend>';
 5937:     foreach my $ltirole (@ltiroles) {
 5938:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_makeuser_'.$num.'" value="'.$ltirole.'"'.
 5939:                    $checked{'makeuser'}{$ltirole}.' />'.$ltirole.'</label>&nbsp;</span> ';     
 5940:     }
 5941:     $output .= '</fieldset>'.
 5942:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('New user accounts created for LTI users').'</legend>'.
 5943:                '<table>'.
 5944:                &modifiable_userdata_row('lti','instdata_'.$num,$current,$numinrow,$itemcount).
 5945:                '</table>'.
 5946:                '<table class="LC_nested"><tr><td class="LC_left_item">LON-CAPA Authentication</td>'.
 5947:                '<td class="LC_left_item">';
 5948:     foreach my $auth ('lti',@authtypes) {
 5949:         my $authtext;
 5950:         if ($auth eq 'lti') {
 5951:             $authtext = &mt('None');
 5952:         } else {
 5953:             $authtext = $authnames{$shortauth{$auth}};
 5954:         }
 5955:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="lti_lcauth_'.$num.
 5956:                    '" value="'.$auth.'"'.$checked{'lcauth'}{$auth}.$onclicklcauth.' />'.
 5957:                    $authtext.'</label></span> &nbsp;';
 5958:     }
 5959:     $output .= '</td></tr>'.
 5960:                '<tr id="lti_lcauth_parmrow_'.$num.'" style="'.$lcauthparmstyle.'">'.
 5961:                '<td class="LC_right_item" colspan="2"><span class="LC_nobreak">'.
 5962:                '<span id="lti_lcauth_parmtext_'.$num.'">'.$lcauthparmtext.'</span>'.
 5963:                '<input type="text" name="lti_lcauthparm_'.$num.'" value="" /></span></td></tr>'.
 5964:                '</table></fieldset>'.
 5965:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping courses').'</legend>'.
 5966:                '<div class="LC_floatleft"><span class="LC_nobreak">'.
 5967:                &mt('Unique course identifier').':&nbsp;';
 5968:     foreach my $option ('course_offering_sourcedid','context_id','other') {
 5969:         $output .= '<label><input type="radio" name="lti_mapcrs_'.$num.'" value="'.$option.'"'.
 5970:                    $checked{'mapcrs'}{$option}.$onclickcrs.' />'.$option.'</label>'.
 5971:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 5972:     }
 5973:     $output .= '</span></div><div class="LC_floatleft" style="display:'.$crsfieldsty.';" id="lti_crsfield_'.$num.'">'.
 5974:                '<input type="text" name="lti_mapcrsfield_'.$num.'" value="'.$cidfield.'" />'.
 5975:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'.
 5976:                '<span class="LC_nobreak">'.&mt('LON-CAPA course type(s)').':&nbsp;';
 5977:     foreach my $type (@coursetypes) {
 5978:         $output .= '<label><input type="checkbox" name="lti_mapcrstype_'.$num.'" value="'.$type.'"'.
 5979:                    $checked{'mapcrstype'}{$type}.' />'.$coursetypetitles{$type}.'</label>'.
 5980:                    ('&nbsp;'x2);
 5981:     }
 5982:     $output .= '</span></fieldset>'.
 5983:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Creating courses').'</legend>'.
 5984:                '<span class="LC_nobreak">'.&mt('Course created (if absent) on Instructor access').':&nbsp;'.
 5985:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="0"'.
 5986:                $checked{'makecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5987:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="1"'.
 5988:                $checked{'makecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 5989:                '</fieldset>'.
 5990:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may self-enroll').'</legend>';
 5991:     foreach my $lticrsrole (@lticourseroles) {
 5992:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_selfenroll_'.$num.'" value="'.$lticrsrole.'"'.
 5993:                    $checked{'selfenroll'}{$lticrsrole}.' />'.$lticrsrole.'</label>&nbsp;</span> ';
 5994:     }
 5995:     $output .= '</fieldset>'.
 5996:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Course options').'</legend>'.
 5997:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Assign users to sections').':&nbsp;'.
 5998:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="0"'.
 5999:                $checked{'crssec'}{'N'}.$onclicksec.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6000:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="1"'.
 6001:                $checked{'crssec'}{'Y'}.$onclicksec.' />'.&mt('Yes').'</label></span></div>'.
 6002:                '<div class="LC_floatleft" style="display:'.$crssecfieldsty.';" id="lti_crssecfield_'.$num.'">'.
 6003:                '<span class="LC_nobreak">'.&mt('From').':<label>'.
 6004:                '<input type="radio" name="lti_crssecsrc_'.$num.'" value="course_section_sourcedid"'.
 6005:                $checked{'crssecsrc'}{'sourcedid'}.$onclicksecsrc.' />'.
 6006:                &mt('Standard field').'</label>'.('&nbsp;'x2).
 6007:                '<label><input type="radio" name="lti_crssecsrc_'.$num.'" value="other"'.
 6008:                $checked{'crssecsrc'}{'other'}.$onclicksecsrc.' />'.&mt('Other').
 6009:                '</label></span></div><div class="LC_floatleft" style="display:'.$secsrcfieldsty.';" id="lti_secsrcfield_'.$num.'">'.
 6010:                '<input type="text" name="lti_customsection_'.$num.'" value="'.$crssecsrc.'" />'.
 6011:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 6012:     my ($pb1p1chk,$pb1p0chk,$onclickpb);
 6013:     foreach my $extra ('roster','passback') {
 6014:         my $checkedon = '';
 6015:         my $checkedoff = ' checked="checked"';
 6016:         if ($extra eq 'passback') {
 6017:             $pb1p1chk = ' checked="checked"';
 6018:             $pb1p0chk = '';
 6019:             $onclickpb = ' onclick="toggleLTI(this.form,'."'passback','$num'".');"'; 
 6020:         } else {
 6021:             $onclickpb = ''; 
 6022:         }
 6023:         if (ref($current) eq 'HASH') {
 6024:             if (($current->{$extra})) {
 6025:                 $checkedon = $checkedoff;
 6026:                 $checkedoff = '';
 6027:                 if ($extra eq 'passback') {
 6028:                     $passbacksty = 'inline-block';
 6029:                 }
 6030:                 if ($current->{'passbackformat'} eq '1.0') {
 6031:                     $pb1p0chk =  ' checked="checked"';
 6032:                     $pb1p1chk = '';
 6033:                 }
 6034:             }
 6035:         }
 6036:         $output .= $lt{$extra}.'&nbsp;'.
 6037:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="0"'.$checkedoff.$onclickpb.' />'.
 6038:                    &mt('No').'</label>'.('&nbsp;'x2).
 6039:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="1"'.$checkedon.$onclickpb.' />'.
 6040:                    &mt('Yes').'</label><br />';
 6041:     }
 6042:     $output .= '<div class="LC_floatleft" style="display:'.$passbacksty.';" id="lti_passback_'.$num.'">'.
 6043:                '<span class="LC_nobreak">'.&mt('Grade format').
 6044:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.1"'.$pb1p1chk.' />'.
 6045:                &mt('Outcomes Service (1.1)').'</label>'.('&nbsp;'x2).
 6046:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.0"'.$pb1p0chk.'/>'.
 6047:                &mt('Outcomes Extension (1.0)').'</label></span></div>'.
 6048:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6049:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Callback on logout').':&nbsp;'.
 6050:                '<label><input type="radio" name="lti_callback_'.$num.'" value="0"'.
 6051:                $checked{'callback'}{'N'}.$onclickcallback.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6052:                '<label><input type="radio" name="lti_callback_'.$num.'" value="1"'.
 6053:                $checked{'callback'}{'Y'}.$onclickcallback.' />'.&mt('Yes').'</label></span></div>'.
 6054:                '<div class="LC_floatleft" style="display:'.$callbacksty.';" id="lti_callbackfield_'.$num.'">'.
 6055:                '<span class="LC_nobreak">'.&mt('Parameter').': '.
 6056:                '<input type="text" name="lti_callbackparam_'.$num.'" value="'.$callback.'" /></span>'.
 6057:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>'.
 6058:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Course defaults (Course Coordinator can override)').'</legend>'.
 6059:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'topmenu'}.':&nbsp;'.
 6060:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="0"'.
 6061:                $checked{'topmenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6062:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="1"'.
 6063:                $checked{'topmenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>'.
 6064:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6065:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'inlinemenu'}.':&nbsp;'.
 6066:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="0"'.
 6067:                $checked{'inlinemenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6068:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="1"'.
 6069:                $checked{'inlinemenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>';
 6070:      $output .='<div style="padding:0;clear:both;margin:0;border:0"></div>'. 
 6071:                '<div class="LC_floatleft" style="display:'.$menusty.';" id="lti_menufield_'.$num.'">'.
 6072:                '<span class="LC_nobreak">'.&mt('Menu items').':&nbsp;';
 6073:     foreach my $type ('fullname','coursetitle','role','logout','grades') {
 6074:         $output .= '<label><input type="checkbox" name="lti_menuitem_'.$num.'" value="'.$type.'"'.
 6075:                    $checked{'menuitem'}{$type}.' />'.$menutitles{$type}.'</label>'.
 6076:                    ('&nbsp;'x2);
 6077:     }
 6078:     $output .= '</span></div></fieldset>';
 6079: #        '<fieldset><legend>'.&mt('Assigning author roles').'</legend>';
 6080: #
 6081: #    $output .= '</fieldset>'.
 6082: #        '<fieldset><legend>'.&mt('Assigning domain roles').'</legend>';
 6083:     return $output;
 6084: }
 6085: 
 6086: sub ltimenu_titles {
 6087:     return &Apache::lonlocal::texthash(
 6088:                                         fullname    => 'Full name',
 6089:                                         coursetitle => 'Course title',
 6090:                                         role        => 'Role',
 6091:                                         logout      => 'Logout',
 6092:                                         grades      => 'Grades',
 6093:     );
 6094: }
 6095: 
 6096: sub print_coursedefaults {
 6097:     my ($position,$dom,$settings,$rowtotal) = @_;
 6098:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 6099:     my $itemcount = 1;
 6100:     my %choices =  &Apache::lonlocal::texthash (
 6101:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 6102:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 6103:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 6104:         coursecredits        => 'Credits can be specified for courses',
 6105:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 6106:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 6107:         texengine            => 'Default method to display mathematics',
 6108:         postsubmit           => 'Disable submit button/keypress following student submission',
 6109:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 6110:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 6111:     );
 6112:     my %staticdefaults = (
 6113:                            anonsurvey_threshold => 10,
 6114:                            uploadquota          => 500,
 6115:                            postsubmit           => 60,
 6116:                            mysqltables          => 172800,
 6117:                          );
 6118:     if ($position eq 'top') {
 6119:         %defaultchecked = (
 6120:                             'canuse_pdfforms' => 'off',
 6121:                             'uselcmath'       => 'on',
 6122:                             'usejsme'         => 'on',
 6123:                             'canclone'        => 'none',
 6124:                           );
 6125:         @toggles = ('canuse_pdfforms','uselcmath','usejsme');
 6126:         my $deftex = $Apache::lonnet::deftex;
 6127:         if (ref($settings) eq 'HASH') {
 6128:             if ($settings->{'texengine'}) {
 6129:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 6130:                     $deftex = $settings->{'texengine'};
 6131:                 }
 6132:             }
 6133:         }
 6134:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6135:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 6136:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 6137:                        '</span></td><td class="LC_right_item">'.
 6138:                        '<select name="texengine">'."\n";
 6139:         my %texoptions = (
 6140:                             MathJax  => 'MathJax',
 6141:                             mimetex  => &mt('Convert to Images'),
 6142:                             tth      => &mt('TeX to HTML'),
 6143:                          );
 6144:         foreach my $renderer ('MathJax','mimetex','tth') {
 6145:             my $selected = '';
 6146:             if ($renderer eq $deftex) {
 6147:                 $selected = ' selected="selected"';
 6148:             }
 6149:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 6150:         }
 6151:         $mathdisp .= '</select></td></tr>'."\n";
 6152:         $itemcount ++;
 6153:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 6154:                                                      \%choices,$itemcount);
 6155:         $datatable = $mathdisp.$datatable;
 6156:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6157:         $datatable .=
 6158:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 6159:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 6160:             '</span></td><td class="LC_left_item">';
 6161:         my $currcanclone = 'none';
 6162:         my $onclick;
 6163:         my @cloneoptions = ('none','domain');
 6164:         my %clonetitles = (
 6165:                              none     => 'No additional course requesters',
 6166:                              domain   => "Any course requester in course's domain",
 6167:                              instcode => 'Course requests for official courses ...',
 6168:                           );
 6169:         my (%codedefaults,@code_order,@posscodes);
 6170:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 6171:                                                     \@code_order) eq 'ok') {
 6172:             if (@code_order > 0) {
 6173:                 push(@cloneoptions,'instcode');
 6174:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 6175:             }
 6176:         }
 6177:         if (ref($settings) eq 'HASH') {
 6178:             if ($settings->{'canclone'}) {
 6179:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 6180:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 6181:                         if (@code_order > 0) {
 6182:                             $currcanclone = 'instcode';
 6183:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 6184:                         }
 6185:                     }
 6186:                 } elsif ($settings->{'canclone'} eq 'domain') {
 6187:                     $currcanclone = $settings->{'canclone'};
 6188:                 }
 6189:             }
 6190:         }
 6191:         foreach my $option (@cloneoptions) {
 6192:             my ($checked,$additional);
 6193:             if ($currcanclone eq $option) {
 6194:                 $checked = ' checked="checked"';
 6195:             }
 6196:             if ($option eq 'instcode') {
 6197:                 if (@code_order) {
 6198:                     my $show = 'none';
 6199:                     if ($checked) {
 6200:                         $show = 'block';
 6201:                     }
 6202:                     $additional = '<div id="cloneinstcode" style="display:'.$show.';" />'.
 6203:                                   &mt('Institutional codes for new and cloned course have identical:').
 6204:                                   '<br />';
 6205:                     foreach my $item (@code_order) {
 6206:                         my $codechk;
 6207:                         if ($checked) {
 6208:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 6209:                                 $codechk = ' checked="checked"';
 6210:                             }
 6211:                         }
 6212:                         $additional .= '<label>'.
 6213:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 6214:                                        $item.'</label>';
 6215:                     }
 6216:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 6217:                 }
 6218:             }
 6219:             $datatable .=
 6220:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 6221:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 6222:                 '</label>&nbsp;'.$additional.'</span><br />';
 6223:         }
 6224:         $datatable .= '</td>'.
 6225:                       '</tr>';
 6226:         $itemcount ++;
 6227:     } else {
 6228:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6229:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 6230:         my $currusecredits = 0;
 6231:         my $postsubmitclient = 1;
 6232:         my @types = ('official','unofficial','community','textbook','placement');
 6233:         if (ref($settings) eq 'HASH') {
 6234:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 6235:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 6236:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 6237:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 6238:                 }
 6239:             }
 6240:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 6241:                 foreach my $type (@types) {
 6242:                     next if ($type eq 'community');
 6243:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 6244:                     if ($defcredits{$type} ne '') {
 6245:                         $currusecredits = 1;
 6246:                     }
 6247:                 }
 6248:             }
 6249:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 6250:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 6251:                     $postsubmitclient = 0;
 6252:                     foreach my $type (@types) {
 6253:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6254:                     }
 6255:                 } else {
 6256:                     foreach my $type (@types) {
 6257:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 6258:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 6259:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 6260:                             } else {
 6261:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6262:                             }
 6263:                         } else {
 6264:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6265:                         }
 6266:                     }
 6267:                 }
 6268:             } else {
 6269:                 foreach my $type (@types) {
 6270:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6271:                 }
 6272:             }
 6273:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 6274:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 6275:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 6276:                 }
 6277:             } else {
 6278:                 foreach my $type (@types) {
 6279:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 6280:                 }
 6281:             }
 6282:         } else {
 6283:             foreach my $type (@types) {
 6284:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6285:             }
 6286:         }
 6287:         if (!$currdefresponder) {
 6288:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 6289:         } elsif ($currdefresponder < 1) {
 6290:             $currdefresponder = 1;
 6291:         }
 6292:         foreach my $type (@types) {
 6293:             if ($curruploadquota{$type} eq '') {
 6294:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 6295:             }
 6296:         }
 6297:         $datatable .=
 6298:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6299:                 $choices{'anonsurvey_threshold'}.
 6300:                 '</span></td>'.
 6301:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 6302:                 '<input type="text" name="anonsurvey_threshold"'.
 6303:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 6304:                 '</td></tr>'."\n";
 6305:         $itemcount ++;
 6306:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6307:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6308:                       $choices{'uploadquota'}.
 6309:                       '</span></td>'.
 6310:                       '<td style="text-align: right" class="LC_right_item">'.
 6311:                       '<table><tr>';
 6312:         foreach my $type (@types) {
 6313:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6314:                            '<input type="text" name="uploadquota_'.$type.'"'.
 6315:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 6316:         }
 6317:         $datatable .= '</tr></table></td></tr>'."\n";
 6318:         $itemcount ++;
 6319:         my $onclick = "toggleDisplay(this.form,'credits');";
 6320:         my $display = 'none';
 6321:         if ($currusecredits) {
 6322:             $display = 'block';
 6323:         }
 6324:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 6325:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 6326:         foreach my $type (@types) {
 6327:             next if ($type eq 'community');
 6328:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6329:                            '<input type="text" name="'.$type.'_credits"'.
 6330:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 6331:         }
 6332:         $additional .= '</tr></table></div>'."\n";
 6333:         %defaultchecked = ('coursecredits' => 'off');
 6334:         @toggles = ('coursecredits');
 6335:         my $current = {
 6336:                         'coursecredits' => $currusecredits,
 6337:                       };
 6338:         (my $table,$itemcount) =
 6339:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 6340:                                \%choices,$itemcount,$onclick,$additional,'left');
 6341:         $datatable .= $table;
 6342:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 6343:         my $display = 'none';
 6344:         if ($postsubmitclient) {
 6345:             $display = 'block';
 6346:         }
 6347:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 6348:                       &mt('Number of seconds submit is disabled').'<br />'.
 6349:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 6350:                       '<table><tr>';
 6351:         foreach my $type (@types) {
 6352:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6353:                            '<input type="text" name="'.$type.'_timeout" value="'.
 6354:                            $deftimeout{$type}.'" size="5" /></td>';
 6355:         }
 6356:         $additional .= '</tr></table></div>'."\n";
 6357:         %defaultchecked = ('postsubmit' => 'on');
 6358:         @toggles = ('postsubmit');
 6359:         $current = {
 6360:                        'postsubmit' => $postsubmitclient,
 6361:                    };
 6362:         ($table,$itemcount) =
 6363:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 6364:                                \%choices,$itemcount,$onclick,$additional,'left');
 6365:         $datatable .= $table;
 6366:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6367:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6368:                       $choices{'mysqltables'}.
 6369:                       '</span></td>'.
 6370:                       '<td style="text-align: right" class="LC_right_item">'.
 6371:                       '<table><tr>';
 6372:         foreach my $type (@types) {
 6373:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6374:                            '<input type="text" name="mysqltables_'.$type.'"'.
 6375:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 6376:         }
 6377:         $datatable .= '</tr></table></td></tr>'."\n";
 6378:         $itemcount ++;
 6379: 
 6380:     }
 6381:     $$rowtotal += $itemcount;
 6382:     return $datatable;
 6383: }
 6384: 
 6385: sub print_selfenrollment {
 6386:     my ($position,$dom,$settings,$rowtotal) = @_;
 6387:     my ($css_class,$datatable);
 6388:     my $itemcount = 1;
 6389:     my @types = ('official','unofficial','community','textbook','placement');
 6390:     if (($position eq 'top') || ($position eq 'middle')) {
 6391:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 6392:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 6393:         my @rows;
 6394:         my $key;
 6395:         if ($position eq 'top') {
 6396:             $key = 'admin'; 
 6397:             if (ref($rowsref) eq 'ARRAY') {
 6398:                 @rows = @{$rowsref};
 6399:             }
 6400:         } elsif ($position eq 'middle') {
 6401:             $key = 'default';
 6402:             @rows = ('types','registered','approval','limit');
 6403:         }
 6404:         foreach my $row (@rows) {
 6405:             if (defined($titlesref->{$row})) {
 6406:                 $itemcount ++;
 6407:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6408:                 $datatable .= '<tr'.$css_class.'>'.
 6409:                               '<td>'.$titlesref->{$row}.'</td>'.
 6410:                               '<td class="LC_left_item">'.
 6411:                               '<table><tr>';
 6412:                 my (%current,%currentcap);
 6413:                 if (ref($settings) eq 'HASH') {
 6414:                     if (ref($settings->{$key}) eq 'HASH') {
 6415:                         foreach my $type (@types) {
 6416:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 6417:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 6418:                             }
 6419:                             if (($row eq 'limit') && ($key eq 'default')) {
 6420:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 6421:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 6422:                                 }
 6423:                             }
 6424:                         }
 6425:                     }
 6426:                 }
 6427:                 my %roles = (
 6428:                              '0' => &Apache::lonnet::plaintext('dc'),
 6429:                             ); 
 6430:             
 6431:                 foreach my $type (@types) {
 6432:                     unless (($row eq 'registered') && ($key eq 'default')) {
 6433:                         $datatable .= '<th>'.&mt($type).'</th>';
 6434:                     }
 6435:                 }
 6436:                 unless (($row eq 'registered') && ($key eq 'default')) {
 6437:                     $datatable .= '</tr><tr>';
 6438:                 }
 6439:                 foreach my $type (@types) {
 6440:                     if ($type eq 'community') {
 6441:                         $roles{'1'} = &mt('Community personnel');
 6442:                     } else {
 6443:                         $roles{'1'} = &mt('Course personnel');
 6444:                     }
 6445:                     $datatable .= '<td style="vertical-align: top">';
 6446:                     if ($position eq 'top') {
 6447:                         my %checked;
 6448:                         if ($current{$type} eq '0') {
 6449:                             $checked{'0'} = ' checked="checked"';
 6450:                         } else {
 6451:                             $checked{'1'} = ' checked="checked"';
 6452:                         }
 6453:                         foreach my $role ('1','0') {
 6454:                             $datatable .= '<span class="LC_nobreak"><label>'.
 6455:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 6456:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 6457:                                           $roles{$role}.'</label></span> ';
 6458:                         }
 6459:                     } else {
 6460:                         if ($row eq 'types') {
 6461:                             my %checked;
 6462:                             if ($current{$type} =~ /^(all|dom)$/) {
 6463:                                 $checked{$1} = ' checked="checked"';
 6464:                             } else {
 6465:                                 $checked{''} = ' checked="checked"';
 6466:                             }
 6467:                             foreach my $val ('','dom','all') {
 6468:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6469:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6470:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6471:                             }
 6472:                         } elsif ($row eq 'registered') {
 6473:                             my %checked;
 6474:                             if ($current{$type} eq '1') {
 6475:                                 $checked{'1'} = ' checked="checked"';
 6476:                             } else {
 6477:                                 $checked{'0'} = ' checked="checked"';
 6478:                             }
 6479:                             foreach my $val ('0','1') {
 6480:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6481:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6482:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6483:                             }
 6484:                         } elsif ($row eq 'approval') {
 6485:                             my %checked;
 6486:                             if ($current{$type} =~ /^([12])$/) {
 6487:                                 $checked{$1} = ' checked="checked"';
 6488:                             } else {
 6489:                                 $checked{'0'} = ' checked="checked"';
 6490:                             }
 6491:                             for my $val (0..2) {
 6492:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6493:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6494:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6495:                             }
 6496:                         } elsif ($row eq 'limit') {
 6497:                             my %checked;
 6498:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 6499:                                 $checked{$1} = ' checked="checked"';
 6500:                             } else {
 6501:                                 $checked{'none'} = ' checked="checked"';
 6502:                             }
 6503:                             my $cap;
 6504:                             if ($currentcap{$type} =~ /^\d+$/) {
 6505:                                 $cap = $currentcap{$type};
 6506:                             }
 6507:                             foreach my $val ('none','allstudents','selfenrolled') {
 6508:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6509:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6510:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6511:                             }
 6512:                             $datatable .= '<br />'.
 6513:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 6514:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 6515:                                           '</span>'; 
 6516:                         }
 6517:                     }
 6518:                     $datatable .= '</td>';
 6519:                 }
 6520:                 $datatable .= '</tr>';
 6521:             }
 6522:             $datatable .= '</table></td></tr>';
 6523:         }
 6524:     } elsif ($position eq 'bottom') {
 6525:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 6526:     }
 6527:     $$rowtotal += $itemcount;
 6528:     return $datatable;
 6529: }
 6530: 
 6531: sub print_validation_rows {
 6532:     my ($caller,$dom,$settings,$rowtotal) = @_;
 6533:     my ($itemsref,$namesref,$fieldsref);
 6534:     if ($caller eq 'selfenroll') { 
 6535:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 6536:     } elsif ($caller eq 'requestcourses') {
 6537:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 6538:     }
 6539:     my %currvalidation;
 6540:     if (ref($settings) eq 'HASH') {
 6541:         if (ref($settings->{'validation'}) eq 'HASH') {
 6542:             %currvalidation = %{$settings->{'validation'}};
 6543:         }
 6544:     }
 6545:     my $datatable;
 6546:     my $itemcount = 0;
 6547:     foreach my $item (@{$itemsref}) {
 6548:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6549:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6550:                       $namesref->{$item}.
 6551:                       '</span></td>'.
 6552:                       '<td class="LC_left_item">';
 6553:         if (($item eq 'url') || ($item eq 'button')) {
 6554:             $datatable .= '<span class="LC_nobreak">'.
 6555:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 6556:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 6557:         } elsif ($item eq 'fields') {
 6558:             my @currfields;
 6559:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 6560:                 @currfields = @{$currvalidation{$item}};
 6561:             }
 6562:             foreach my $field (@{$fieldsref}) {
 6563:                 my $check = '';
 6564:                 if (grep(/^\Q$field\E$/,@currfields)) {
 6565:                     $check = ' checked="checked"';
 6566:                 }
 6567:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6568:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 6569:                               ' value="'.$field.'"'.$check.' />'.$field.
 6570:                               '</label></span> ';
 6571:             }
 6572:         } elsif ($item eq 'markup') {
 6573:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 6574:                            $currvalidation{$item}.
 6575:                               '</textarea>';
 6576:         }
 6577:         $datatable .= '</td></tr>'."\n";
 6578:         if (ref($rowtotal)) {
 6579:             $itemcount ++;
 6580:         }
 6581:     }
 6582:     if ($caller eq 'requestcourses') {
 6583:         my %currhash;
 6584:         if (ref($settings) eq 'HASH') {
 6585:             if (ref($settings->{'validation'}) eq 'HASH') {
 6586:                 if ($settings->{'validation'}{'dc'} ne '') {
 6587:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 6588:                 }
 6589:             }
 6590:         }
 6591:         my $numinrow = 2;
 6592:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 6593:                                                        'validationdc',%currhash);
 6594:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6595:         $datatable .= '<tr'.$css_class.'><td>';
 6596:         if ($numdc > 1) {
 6597:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 6598:         } else {
 6599:             $datatable .=  &mt('Course creation processed as: ');
 6600:         }
 6601:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 6602:         $itemcount ++;
 6603:     }
 6604:     if (ref($rowtotal)) {
 6605:         $$rowtotal += $itemcount;
 6606:     }
 6607:     return $datatable;
 6608: }
 6609: 
 6610: sub print_privacy {
 6611:     my ($position,$dom,$settings,$rowtotal) = @_;
 6612:     my ($datatable,$css_class,$numinrow,@items,%names,$othertitle,$usertypes,$types);
 6613:     my $itemcount = 0;
 6614:     unless ($position eq 'top') {
 6615:         @items = ('domain','author','course','community');
 6616:         %names = &Apache::lonlocal::texthash (
 6617:                      domain => 'Assigned domain role(s)',
 6618:                      author => 'Assigned co-author role(s)',
 6619:                      course => 'Assigned course role(s)',
 6620:                      community => 'Assigned community role',
 6621:                  );
 6622:         $numinrow = 4;
 6623:         ($othertitle,$usertypes,$types) =
 6624:             &Apache::loncommon::sorted_inst_types($dom);
 6625:     }
 6626:     if (($position eq 'top') || ($position eq 'middle')) {
 6627:         my (%by_ip,%by_location,@intdoms,@instdoms);
 6628:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 6629:         if ($position eq 'top') {
 6630:             my %curr;
 6631:             my @options = ('none','user','domain','auto');
 6632:             my %titles = &Apache::lonlocal::texthash (
 6633:                 none   => 'Not allowed',
 6634:                 user   => 'User authorizes',
 6635:                 domain => 'DC authorizes',
 6636:                 auto   => 'Unrestricted',
 6637:                 instdom => 'Other domain shares institution/provider',
 6638:                 extdom => 'Other domain has different institution/provider',
 6639:             );
 6640:             my %names = &Apache::lonlocal::texthash (
 6641:                 domain => 'Domain role',
 6642:                 author => 'Co-author role',
 6643:                 course => 'Course role',
 6644:                 community => 'Community role',
 6645:             );
 6646:             my $primary_id = &Apache::lonnet::domain($dom,'primary');
 6647:             my $intdom = &Apache::lonnet::internet_dom($primary_id);
 6648:             foreach my $domtype ('instdom','extdom') {
 6649:                 my (%checked,$skip);
 6650:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6651:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$domtype}.'</td>'.
 6652:                               '<td class="LC_left_item">';
 6653:                 if ($domtype eq 'instdom') {
 6654:                     unless (@instdoms > 1) {
 6655:                         $datatable .= &mt('Nothing to set, as no domains besides [_1] are hosted by [_2]',$dom,$intdom);
 6656:                         $skip = 1;
 6657:                     }
 6658:                 } elsif ($domtype eq 'extdom') {
 6659:                     if (keys(%by_location) == 0) {
 6660:                         $datatable .= &mt('Nothing to set, as no other hosts besides [_1]',$intdom);
 6661:                         $skip = 1;
 6662:                     }
 6663:                 }
 6664:                 unless ($skip) {
 6665:                     foreach my $roletype ('domain','author','course','community') {
 6666:                         $checked{'auto'} = ' checked="checked"';
 6667:                         if (ref($settings) eq 'HASH') {
 6668:                             if (ref($settings->{approval}) eq 'HASH') {
 6669:                                 if (ref($settings->{approval}->{$domtype}) eq 'HASH') {
 6670:                                     if ($settings->{approval}->{$domtype}->{$roletype}=~ /^(none|user|domain)$/) {
 6671:                                         $checked{$1} = ' checked="checked"';
 6672:                                         $checked{'auto'} = '';
 6673:                                     }
 6674:                                 }
 6675:                             }
 6676:                         }
 6677:                         $datatable .= '<fieldset><legend>'.$names{$roletype}.'</legend>';
 6678:                         foreach my $option (@options) {
 6679:                             $datatable .= '<span class="LC_nobreak"><label>'.
 6680:                                           '<input type="radio" name="privacy_approval_'.$domtype.'_'.$roletype.'" '.
 6681:                                           'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 6682:                                           '</label></span>&nbsp; ';
 6683:                         }
 6684:                         $datatable .= '</fieldset>';
 6685:                     }
 6686:                 }
 6687:                 $datatable .= '</td></tr>';
 6688:                 $itemcount ++;
 6689:             }
 6690:         } elsif ($position eq 'middle') {
 6691:             if ((@instdoms > 1) || (keys(%by_location) > 0)) {
 6692:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 6693:                     foreach my $item (@{$types}) {
 6694:                         $datatable .= &modifiable_userdata_row('privacy','othdom_'.$item,$settings,
 6695:                                                                $numinrow,$itemcount,'','','','','',
 6696:                                                                '',$usertypes->{$item});
 6697:                         $itemcount ++;
 6698:                     }
 6699:                 }
 6700:                 $datatable .= &modifiable_userdata_row('privacy','othdom_default',$settings,
 6701:                                                        $numinrow,$itemcount,'','','','','',
 6702:                                                        '',$othertitle);
 6703:                 $itemcount ++;
 6704:             } else {
 6705:                 my (@insttypes,%insttitles);
 6706:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 6707:                     @insttypes = @{$types};
 6708:                     %insttitles = %{$usertypes};
 6709:                 }
 6710:                 foreach my $item (@insttypes,'default') {
 6711:                     my $title;
 6712:                     if ($item eq 'default') {
 6713:                         $title = $othertitle;
 6714:                     } else {
 6715:                         $title = $insttitles{$item};
 6716:                     }
 6717:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6718:                     $datatable .= '<tr'.$css_class.'>'.
 6719:                                   '<td class="LC_left_item">'.$title.'</td>'.
 6720:                                   '<td class="LC_left_item">'.
 6721:                                   &mt('Nothing to set here, as there are no other domains').
 6722:                                   '</td></tr>';
 6723:                     $itemcount ++;
 6724:                 }
 6725:             }
 6726:         }
 6727:     } else {
 6728:         my $prefix;
 6729:         if ($position eq 'lower') {
 6730:             $prefix = 'priv';
 6731:         } else {
 6732:             $prefix = 'unpriv';
 6733:         }
 6734:         foreach my $item (@items) {
 6735:             $datatable .= &modifiable_userdata_row('privacy',$prefix.'_'.$item,$settings,
 6736:                                                    $numinrow,$itemcount,'','','','','',
 6737:                                                    '',$names{$item});
 6738:             $itemcount ++;
 6739:         }
 6740:     }
 6741:     if (ref($rowtotal)) {
 6742:         $$rowtotal += $itemcount;
 6743:     }
 6744:     return $datatable;
 6745: }
 6746: 
 6747: sub print_passwords {
 6748:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 6749:     my ($datatable,$css_class);
 6750:     my $itemcount = 0;
 6751:     my %titles = &Apache::lonlocal::texthash (
 6752:         captcha        => '"Forgot Password" CAPTCHA validation',
 6753:         link           => 'Reset link expiration (hours)',
 6754:         case           => 'Case-sensitive usernames/e-mail',
 6755:         prelink        => 'Information required (form 1)',
 6756:         postlink       => 'Information required (form 2)',
 6757:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 6758:         customtext     => 'Domain specific text (HTML)',
 6759:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 6760:         intauth_check  => 'Check bcrypt cost if authenticated',
 6761:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 6762:         permanent      => 'Permanent e-mail address',
 6763:         critical       => 'Critical notification address',
 6764:         notify         => 'Notification address',
 6765:         min            => 'Minimum password length',
 6766:         max            => 'Maximum password length',
 6767:         chars          => 'Required characters',
 6768:         expire         => 'Password expiration (days)',
 6769:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 6770:     );
 6771:     if ($position eq 'top') {
 6772:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6773:         my $shownlinklife = 2;
 6774:         my $prelink = 'both';
 6775:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 6776:         if (ref($settings) eq 'HASH') {
 6777:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 6778:                 $shownlinklife = $settings->{resetlink};
 6779:             }
 6780:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 6781:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 6782:             }
 6783:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 6784:                 $prelink = $settings->{resetprelink};
 6785:             }
 6786:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 6787:                 %postlink = %{$settings->{resetpostlink}};
 6788:             }
 6789:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 6790:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 6791:             }
 6792:             if ($settings->{resetremove}) {
 6793:                 $nostdtext = 1;
 6794:             }
 6795:             if ($settings->{resetcustom}) {
 6796:                 $customurl = $settings->{resetcustom};
 6797:             }
 6798:         } else {
 6799:             if (ref($types) eq 'ARRAY') {
 6800:                 foreach my $item (@{$types}) {
 6801:                     $casesens{$item} = 1;
 6802:                     $postlink{$item} = ['username','email'];
 6803:                 }
 6804:             }
 6805:             $casesens{'default'} = 1;
 6806:             $postlink{'default'} = ['username','email'];
 6807:             $prelink = 'both';
 6808:             %emailsrc = (
 6809:                           permanent => 1,
 6810:                           critical  => 1,
 6811:                           notify    => 1,
 6812:             );
 6813:         }
 6814:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 6815:         $itemcount ++;
 6816:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6817:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 6818:                       '<td class="LC_left_item">'.
 6819:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 6820:                       'name="passwords_link" size="3" /></td></tr>';
 6821:         $itemcount ++;
 6822:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6823:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 6824:                       '<td class="LC_left_item">';
 6825:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 6826:             foreach my $item (@{$types}) {
 6827:                 my $checkedcase;
 6828:                 if ($casesens{$item}) {
 6829:                     $checkedcase = ' checked="checked"';
 6830:                 }
 6831:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6832:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 6833:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 6834:                               '</span>&nbsp;&nbsp; ';
 6835:             }
 6836:         }
 6837:         my $checkedcase;
 6838:         if ($casesens{'default'}) {
 6839:             $checkedcase = ' checked="checked"';
 6840:         }
 6841:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 6842:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 6843:                       $othertitle.'</label></span></td>';
 6844:         $itemcount ++;
 6845:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6846:         my %checkedpre = (
 6847:                              both => ' checked="checked"',
 6848:                              either => '',
 6849:                          );
 6850:         if ($prelink eq 'either') {
 6851:             $checkedpre{either} = ' checked="checked"';
 6852:             $checkedpre{both} = '';
 6853:         }
 6854:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 6855:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 6856:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 6857:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 6858:                       '<span class="LC_nobreak"><label>'.
 6859:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 6860:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 6861:         $itemcount ++;
 6862:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6863:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 6864:                       '<td class="LC_left_item">';
 6865:         my %postlinked;
 6866:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 6867:             foreach my $item (@{$types}) {
 6868:                 undef(%postlinked);
 6869:                 $datatable .= '<fieldset style="display: inline-block;">'.
 6870:                               '<legend>'.$usertypes->{$item}.'</legend>';
 6871:                 if (ref($postlink{$item}) eq 'ARRAY') {
 6872:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 6873:                 }
 6874:                 foreach my $field ('email','username') {
 6875:                     my $checked;
 6876:                     if ($postlinked{$field}) {
 6877:                         $checked = ' checked="checked"';
 6878:                     }
 6879:                     $datatable .= '<span class="LC_nobreak"><label>'.
 6880:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 6881:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 6882:                                   '<span>&nbsp;&nbsp; ';
 6883:                 }
 6884:                 $datatable .= '</fieldset>';
 6885:             }
 6886:         }
 6887:         if (ref($postlink{'default'}) eq 'ARRAY') {
 6888:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 6889:         }
 6890:         $datatable .= '<fieldset style="display: inline-block;">'.
 6891:                       '<legend>'.$othertitle.'</legend>';
 6892:         foreach my $field ('email','username') {
 6893:             my $checked;
 6894:             if ($postlinked{$field}) {
 6895:                 $checked = ' checked="checked"';
 6896:             }
 6897:             $datatable .= '<span class="LC_nobreak"><label>'.
 6898:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 6899:                           $field.'"'.$checked.' />'.$field.'</label>'.
 6900:                           '<span>&nbsp;&nbsp; ';
 6901:         }
 6902:         $datatable .= '</fieldset></td></tr>';
 6903:         $itemcount ++;
 6904:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6905:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 6906:                       '<td class="LC_left_item">';
 6907:         foreach my $type ('permanent','critical','notify') {
 6908:             my $checkedemail;
 6909:             if ($emailsrc{$type}) {
 6910:                 $checkedemail = ' checked="checked"';
 6911:             }
 6912:             $datatable .= '<span class="LC_nobreak"><label>'.
 6913:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 6914:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 6915:                           '<span>&nbsp;&nbsp; ';
 6916:         }
 6917:         $datatable .= '</td></tr>';
 6918:         $itemcount ++;
 6919:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6920:         my $switchserver = &check_switchserver($dom,$confname);
 6921:         my ($showstd,$noshowstd);
 6922:         if ($nostdtext) {
 6923:             $noshowstd = ' checked="checked"';
 6924:         } else {
 6925:             $showstd = ' checked="checked"';
 6926:         }
 6927:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 6928:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 6929:                       &mt('Retain standard text:').
 6930:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 6931:                       &mt('Yes').'</label>'.'&nbsp;'.
 6932:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 6933:                       &mt('No').'</label></span><br />'.
 6934:                       '<span class="LC_fontsize_small">'.
 6935:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 6936:                       &mt('Include custom text:');
 6937:         if ($customurl) {
 6938:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('custom text'),600,500,
 6939:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 6940:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 6941:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 6942:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 6943:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 6944:         }
 6945:         if ($switchserver) {
 6946:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 6947:         } else {
 6948:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 6949:                          '<input type="file" name="passwords_customfile" /></span>';
 6950:         }
 6951:         $datatable .= '</td></tr>';
 6952:     } elsif ($position eq 'middle') {
 6953:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 6954:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 6955:         my %defaults;
 6956:         if (ref($domconf{'defaults'}) eq 'HASH') {
 6957:             %defaults = %{$domconf{'defaults'}};
 6958:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 6959:                 $defaults{'intauth_cost'} = 10;
 6960:             }
 6961:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 6962:                 $defaults{'intauth_check'} = 0;
 6963:             }
 6964:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 6965:                 $defaults{'intauth_switch'} = 0;
 6966:             }
 6967:         } else {
 6968:             %defaults = (
 6969:                           'intauth_cost'   => 10,
 6970:                           'intauth_check'  => 0,
 6971:                           'intauth_switch' => 0,
 6972:                         );
 6973:         }
 6974:         foreach my $item (@items) {
 6975:             if ($itemcount%2) {
 6976:                 $css_class = '';
 6977:             } else {
 6978:                 $css_class = ' class="LC_odd_row" ';
 6979:             }
 6980:             $datatable .= '<tr'.$css_class.'>'.
 6981:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 6982:                           '</span></td><td class="LC_left_item" colspan="3">';
 6983:             if ($item eq 'intauth_switch') {
 6984:                 my @options = (0,1,2);
 6985:                 my %optiondesc = &Apache::lonlocal::texthash (
 6986:                                    0 => 'No',
 6987:                                    1 => 'Yes',
 6988:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 6989:                                  );
 6990:                 $datatable .= '<table width="100%">';
 6991:                 foreach my $option (@options) {
 6992:                     my $checked = ' ';
 6993:                     if ($defaults{$item} eq $option) {
 6994:                         $checked = ' checked="checked"';
 6995:                     }
 6996:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6997:                                   '<label><input type="radio" name="'.$item.
 6998:                                   '" value="'.$option.'"'.$checked.' />'.
 6999:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7000:                 }
 7001:                 $datatable .= '</table>';
 7002:             } elsif ($item eq 'intauth_check') {
 7003:                 my @options = (0,1,2);
 7004:                 my %optiondesc = &Apache::lonlocal::texthash (
 7005:                                    0 => 'No',
 7006:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 7007:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 7008:                                  );
 7009:                 $datatable .= '<table width="100%">';
 7010:                 foreach my $option (@options) {
 7011:                     my $checked = ' ';
 7012:                     my $onclick;
 7013:                     if ($defaults{$item} eq $option) {
 7014:                         $checked = ' checked="checked"';
 7015:                     }
 7016:                     if ($option == 2) {
 7017:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 7018:                     }
 7019:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7020:                                   '<label><input type="radio" name="'.$item.
 7021:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 7022:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7023:                 }
 7024:                 $datatable .= '</table>';
 7025:             } else {
 7026:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 7027:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 7028:             }
 7029:             $datatable .= '</td></tr>';
 7030:             $itemcount ++;
 7031:         }
 7032:     } elsif ($position eq 'lower') {
 7033:         my ($min,$max,%chars,$expire,$numsaved);
 7034:         $min = $Apache::lonnet::passwdmin;
 7035:         if (ref($settings) eq 'HASH') {
 7036:             if ($settings->{min}) {
 7037:                 $min = $settings->{min};
 7038:             }
 7039:             if ($settings->{max}) {
 7040:                 $max = $settings->{max};
 7041:             }
 7042:             if (ref($settings->{chars}) eq 'ARRAY') {
 7043:                 map { $chars{$_} = 1; } (@{$settings->{chars}});
 7044:             }
 7045:             if ($settings->{expire}) {
 7046:                 $expire = $settings->{expire};
 7047:             }
 7048:             if ($settings->{numsaved}) {
 7049:                 $numsaved = $settings->{numsaved};
 7050:             }
 7051:         }
 7052:         my %rulenames = &Apache::lonlocal::texthash(
 7053:                                                      uc => 'At least one upper case letter',
 7054:                                                      lc => 'At least one lower case letter',
 7055:                                                      num => 'At least one number',
 7056:                                                      spec => 'At least one non-alphanumeric',
 7057:                                                    );
 7058:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7059:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 7060:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7061:                       '<input type="text" name="passwords_min" value="'.$min.'" size="3" '.
 7062:                       'onblur="javascript:warnIntPass(this);" />'.
 7063:                       '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 7064:                       '</span></td></tr>';
 7065:         $itemcount ++;
 7066:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7067:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 7068:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7069:                       '<input type="text" name="passwords_max" value="'.$max.'" size="3" '.
 7070:                       'onblur="javascript:warnIntPass(this);" />'.
 7071:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 7072:                       '</span></td></tr>';
 7073:         $itemcount ++;
 7074:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7075:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 7076:                       '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 7077:                       '</span></td>';
 7078:         my $numinrow = 2;
 7079:         my @possrules = ('uc','lc','num','spec');
 7080:         $datatable .= '<td class="LC_left_item"><table>';
 7081:         for (my $i=0; $i<@possrules; $i++) {
 7082:             my ($rem,$checked);
 7083:             if ($chars{$possrules[$i]}) {
 7084:                 $checked = ' checked="checked"';
 7085:             }
 7086:             $rem = $i%($numinrow);
 7087:             if ($rem == 0) {
 7088:                 if ($i > 0) {
 7089:                     $datatable .= '</tr>';
 7090:                 }
 7091:                 $datatable .= '<tr>';
 7092:             }
 7093:             $datatable .= '<td><span class="LC_nobreak"><label>'.
 7094:                           '<input type="checkbox" name="passwords_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 7095:                           $rulenames{$possrules[$i]}.'</label></span></td>';
 7096:         }
 7097:         my $rem = @possrules%($numinrow);
 7098:         my $colsleft = $numinrow - $rem;
 7099:         if ($colsleft > 1 ) {
 7100:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7101:                           '&nbsp;</td>';
 7102:         } elsif ($colsleft == 1) {
 7103:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 7104:         }
 7105:         $datatable .='</table></td></tr>';
 7106:         $itemcount ++;
 7107:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7108:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'expire'}.'</td>'.
 7109:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7110:                       '<input type="text" name="passwords_expire" value="'.$expire.'" size="4" '.
 7111:                       'onblur="javascript:warnIntPass(this);" />'.
 7112:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no expiration)').'</span>'.
 7113:                       '</span></td></tr>';
 7114:         $itemcount ++;
 7115:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7116:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 7117:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7118:                       '<input type="text" name="passwords_numsaved" value="'.$numsaved.'" size="3" '.
 7119:                       'onblur="javascript:warnIntPass(this);" />'. 
 7120:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 7121:                       '</span></td></tr>';
 7122:     } else {
 7123:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7124:         my %ownerchg = (
 7125:                           by  => {},
 7126:                           for => {},
 7127:                        );
 7128:         my %ownertitles = &Apache::lonlocal::texthash (
 7129:                             by  => 'Course owner status(es) allowed',
 7130:                             for => 'Student status(es) allowed',
 7131:                           );
 7132:         if (ref($settings) eq 'HASH') {
 7133:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 7134:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 7135:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 7136:                 }
 7137:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 7138:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 7139:                 }
 7140:             }
 7141:         }
 7142:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7143:         $datatable .= '<tr '.$css_class.'>'.
 7144:                       '<td>'.
 7145:                       &mt('Requirements').'<ul>'.
 7146:                       '<li>'.&mt("Course 'type' is not a Community or Placement Test").'</li>'.
 7147:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 7148:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 7149:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 7150:                       '</ul>'.
 7151:                       '</td>'.
 7152:                       '<td class="LC_left_item">';
 7153:         foreach my $item ('by','for') {
 7154:             $datatable .= '<fieldset style="display: inline-block;">'.
 7155:                           '<legend>'.$ownertitles{$item}.'</legend>';
 7156:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7157:                 foreach my $type (@{$types}) {
 7158:                     my $checked;
 7159:                     if ($ownerchg{$item}{$type}) {
 7160:                         $checked = ' checked="checked"';
 7161:                     }
 7162:                     $datatable .= '<span class="LC_nobreak"><label>'.
 7163:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 7164:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 7165:                                   '</span>&nbsp;&nbsp; ';
 7166:                 }
 7167:             }
 7168:             my $checked;
 7169:             if ($ownerchg{$item}{'default'}) {
 7170:                 $checked = ' checked="checked"';
 7171:             }
 7172:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 7173:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 7174:                           $othertitle.'</label></span></fieldset>';
 7175:         }
 7176:         $datatable .= '</td></tr>';
 7177:     }
 7178:     return $datatable;
 7179: }
 7180: 
 7181: sub print_wafproxy {
 7182:     my ($position,$dom,$settings,$rowtotal) = @_;
 7183:     my $css_class;
 7184:     my $itemcount = 0;
 7185:     my $datatable;
 7186:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7187:     my (%othercontrol,%otherdoms,%aliases,%values,$setdom);
 7188:     my %lt = &wafproxy_titles();
 7189:     foreach my $server (sort(keys(%servers))) {
 7190:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 7191:         my $serverdom;
 7192:         if ($serverhome ne $server) {
 7193:             $serverdom = &Apache::lonnet::host_domain($serverhome);
 7194:             $othercontrol{$server} = $serverdom;
 7195:         } else {
 7196:             $serverdom = &Apache::lonnet::host_domain($server);
 7197:             if ($serverdom ne $dom) {
 7198:                 $othercontrol{$server} = $serverdom;
 7199:             } else {
 7200:                 $setdom = 1;
 7201:                 if (ref($settings) eq 'HASH') {
 7202:                     %{$values{$dom}} = ();
 7203:                     if (ref($settings->{'alias'}) eq 'HASH') {
 7204:                         $aliases{$dom} = $settings->{'alias'};
 7205:                     }
 7206:                     foreach my $item ('ipheader','trusted','exempt') {
 7207:                         $values{$dom}{$item} = $settings->{$item};
 7208:                     }
 7209:                 }
 7210:             }
 7211:         }
 7212:     }
 7213:     if (keys(%othercontrol)) {
 7214:         %otherdoms = reverse(%othercontrol);
 7215:         foreach my $domain (keys(%otherdoms)) {
 7216:             %{$values{$domain}} = ();
 7217:             my %config = &Apache::lonnet::get_dom('configuration',['wafproxy'],$domain);
 7218:             if (ref($config{$domain}) eq 'HASH') {
 7219:                 if (ref($config{$domain}{'wafproxy'}) eq 'HASH') {
 7220:                     $aliases{$domain} = $config{$domain}{'wafproxy'}{'alias'};
 7221:                     foreach my $item ('ipheader','trusted','exempt') {
 7222:                         $values{$domain}{$item} = $config{$domain}{'wafproxy'}{$item};
 7223:                     }
 7224:                 }
 7225:             }
 7226:         }
 7227:     }
 7228:     if ($position eq 'top') {
 7229:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7230:         foreach my $server (sort(keys(%servers))) {
 7231:             $itemcount ++; 
 7232:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7233:             $datatable .= '<tr'.$css_class.'>'.
 7234:                           '<td>'.&mt('Hostname').':&nbsp;'.
 7235:                                 &Apache::lonnet::hostname($server).'</td>'.
 7236:                           '<td class="LC_right_item">';
 7237:             if ($othercontrol{$server}) {
 7238:                 my $current;
 7239:                 if (ref($aliases{$othercontrol{$server}}) eq 'HASH') {
 7240:                     $current = $aliases{$othercontrol{$server}{$server}};
 7241:                 }
 7242:                 if ($current) {
 7243:                     $datatable .= $current;
 7244:                 } else {
 7245:                     $datatable .= &mt('None in effect');
 7246:                 }
 7247:                 $datatable .= '<br /><span class="LC_small">('.
 7248:                               &mt('WAF/Reverse Proxy controlled by domain: [_1]',
 7249:                                   '<b>'.$othercontrol{$server}.'</b>').'</span>';
 7250:             } else {
 7251:                 my $current;
 7252:                 if (ref($aliases{$dom}) eq 'HASH') {
 7253:                     if ($aliases{$dom}{$server}) {
 7254:                         $current = $aliases{$dom}{$server};
 7255:                     }
 7256:                 }
 7257:                 $datatable .= '<input type="text" name="wafproxy_alias_'.$server.'" '.
 7258:                               'value="'.$current.'" size="30" />';
 7259:             }
 7260:             $datatable .= '</td></tr>';
 7261:         }
 7262:     } else {
 7263:         if ($setdom) {
 7264:             $itemcount ++;
 7265:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7266:             $datatable .= '<tr'.$css_class.'>'.
 7267:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br /><br />'.
 7268:                           &mt('Format for comma separated IP blocks').':<br />'.
 7269:                           &mt('A.B.C.D/N or A.B.C.D - E.F.G.H').'</td>'.
 7270:                           '<td class="LC_left_item"><table>';
 7271:             foreach my $item ('ipheader','trusted','exempt') {
 7272:                 $datatable .= '<tr>'.
 7273:                               '<td valign="top">'.$lt{$item}.':&nbsp;';
 7274:                 if ($item eq 'ipheader') {
 7275:                     $datatable .= '<input type="text" value="'.$values{$dom}{$item}.'" '.
 7276:                                   'name="wafproxy_'.$item.'" />';
 7277:  
 7278:                 } else {
 7279:                     $datatable .= '<textarea name="wafproxy_'.$item.'" rows="3" cols="80">'.
 7280:                                   $values{$dom}{$item}.'</textarea>';
 7281:                 }
 7282:                 $datatable .= '</td></tr>';
 7283:             }
 7284:             $datatable .= '</table></td></tr>';
 7285:         }
 7286:         if (keys(%otherdoms)) {
 7287:             foreach my $domain (sort(keys(%otherdoms))) {
 7288:                 $itemcount ++;
 7289:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7290:                 $datatable .= '<tr'.$css_class.'>'.
 7291:                               '<td class="LC_left_item">'.&mt('Domain: [_1]',$domain).'</td>'.
 7292:                               '<td class="LC_left_item"><table>';
 7293:                 foreach my $item ('ipheader','trusted','exempt') {
 7294:                     my $showval = &mt('None');
 7295:                     if ($values{$domain}{$item}) {
 7296:                         $showval = $values{$domain}{$item}; 
 7297:                     }
 7298:                     $datatable .= '<tr>'.
 7299:                                   '<td>'.$lt{$item}.':&nbsp;'.$showval.'</td></tr>';
 7300:                 }
 7301:                 $datatable .= '</table></td></tr>'; 
 7302:             }
 7303:         }
 7304:     }
 7305:     $$rowtotal += $itemcount;
 7306:     return $datatable;
 7307: }
 7308: 
 7309: sub wafproxy_titles {
 7310:     return &Apache::lonlocal::texthash(
 7311:                exempt => 'Exempt IP range(s)',
 7312:                trusted => 'Trusted IP range(s)',
 7313:                ipheader => 'Custom request header',
 7314:            );
 7315: }
 7316: 
 7317: sub print_usersessions {
 7318:     my ($position,$dom,$settings,$rowtotal) = @_;
 7319:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 7320:     my (%by_ip,%by_location,@intdoms,@instdoms);
 7321:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 7322: 
 7323:     my @alldoms = &Apache::lonnet::all_domains();
 7324:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 7325:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7326:     my %altids = &id_for_thisdom(%servers);
 7327:     if ($position eq 'top') {
 7328:         if (keys(%serverhomes) > 1) {
 7329:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 7330:             my ($curroffloadnow,$curroffloadoth);
 7331:             if (ref($settings) eq 'HASH') {
 7332:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 7333:                     $curroffloadnow = $settings->{'offloadnow'};
 7334:                 }
 7335:                 if (ref($settings->{'offloadoth'}) eq 'HASH') {
 7336:                     $curroffloadoth = $settings->{'offloadoth'};
 7337:                 }
 7338:             }
 7339:             my $other_insts = scalar(keys(%by_location));
 7340:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,
 7341:                                       $other_insts,$curroffloadnow,$curroffloadoth,$rowtotal);
 7342:         } else {
 7343:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 7344:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 7345:                           '</td></tr>';
 7346:         }
 7347:     } else {
 7348:         my %titles = &usersession_titles();
 7349:         my ($prefix,@types);
 7350:         if ($position eq 'bottom') {
 7351:             $prefix = 'remote';
 7352:             @types = ('version','excludedomain','includedomain');
 7353:         } else {
 7354:             $prefix = 'hosted';
 7355:             @types = ('excludedomain','includedomain');
 7356:         }
 7357:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 7358:     }
 7359:     $$rowtotal += $itemcount;
 7360:     return $datatable;
 7361: }
 7362: 
 7363: sub rules_by_location {
 7364:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 7365:     my ($datatable,$itemcount,$css_class);
 7366:     if (keys(%{$by_location}) == 0) {
 7367:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7368:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 7369:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 7370:                      '</td></tr>';
 7371:         $itemcount = 1;
 7372:     } else {
 7373:         $itemcount = 0;
 7374:         my $numinrow = 5;
 7375:         my (%current,%checkedon,%checkedoff);
 7376:         my @locations = sort(keys(%{$by_location}));
 7377:         foreach my $type (@{$types}) {
 7378:             $checkedon{$type} = '';
 7379:             $checkedoff{$type} = ' checked="checked"';
 7380:         }
 7381:         if (ref($settings) eq 'HASH') {
 7382:             if (ref($settings->{$prefix}) eq 'HASH') {
 7383:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 7384:                     $current{$key} = $settings->{$prefix}{$key};
 7385:                     if ($key eq 'version') {
 7386:                         if ($current{$key} ne '') {
 7387:                             $checkedon{$key} = ' checked="checked"';
 7388:                             $checkedoff{$key} = '';
 7389:                         }
 7390:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 7391:                         $checkedon{$key} = ' checked="checked"';
 7392:                         $checkedoff{$key} = '';
 7393:                     }
 7394:                 }
 7395:             }
 7396:         }
 7397:         foreach my $type (@{$types}) {
 7398:             next if ($type ne 'version' && !@locations);
 7399:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7400:             $datatable .= '<tr'.$css_class.'>
 7401:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 7402:                            <span class="LC_nobreak">&nbsp;
 7403:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 7404:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 7405:             if ($type eq 'version') {
 7406:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 7407:                 my $selector = '<select name="'.$prefix.'_version">';
 7408:                 foreach my $version (@lcversions) {
 7409:                     my $selected = '';
 7410:                     if ($current{'version'} eq $version) {
 7411:                         $selected = ' selected="selected"';
 7412:                     }
 7413:                     $selector .= ' <option value="'.$version.'"'.
 7414:                                  $selected.'>'.$version.'</option>';
 7415:                 }
 7416:                 $selector .= '</select> ';
 7417:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 7418:             } else {
 7419:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 7420:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 7421:                              ' />'.('&nbsp;'x2).
 7422:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 7423:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 7424:                              "\n".
 7425:                              '</div><div><table>';
 7426:                 my $rem;
 7427:                 for (my $i=0; $i<@locations; $i++) {
 7428:                     my ($showloc,$value,$checkedtype);
 7429:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 7430:                         my $ip = $by_location->{$locations[$i]}->[0];
 7431:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 7432:                             $value = join(':',@{$by_ip->{$ip}});
 7433:                             $showloc = join(', ',@{$by_ip->{$ip}});
 7434:                             if (ref($current{$type}) eq 'ARRAY') {
 7435:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 7436:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 7437:                                         $checkedtype = ' checked="checked"';
 7438:                                         last;
 7439:                                     }
 7440:                                 }
 7441:                             }
 7442:                         }
 7443:                     }
 7444:                     $rem = $i%($numinrow);
 7445:                     if ($rem == 0) {
 7446:                         if ($i > 0) {
 7447:                             $datatable .= '</tr>';
 7448:                         }
 7449:                         $datatable .= '<tr>';
 7450:                     }
 7451:                     $datatable .= '<td class="LC_left_item">'.
 7452:                                   '<span class="LC_nobreak"><label>'.
 7453:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 7454:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 7455:                                   '</label></span></td>';
 7456:                 }
 7457:                 $rem = @locations%($numinrow);
 7458:                 my $colsleft = $numinrow - $rem;
 7459:                 if ($colsleft > 1 ) {
 7460:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7461:                                   '&nbsp;</td>';
 7462:                 } elsif ($colsleft == 1) {
 7463:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 7464:                 }
 7465:                 $datatable .= '</tr></table>';
 7466:             }
 7467:             $datatable .= '</td></tr>';
 7468:             $itemcount ++;
 7469:         }
 7470:     }
 7471:     return ($datatable,$itemcount);
 7472: }
 7473: 
 7474: sub print_ssl {
 7475:     my ($position,$dom,$settings,$rowtotal) = @_;
 7476:     my ($css_class,$datatable);
 7477:     my $itemcount = 1;
 7478:     if ($position eq 'top') {
 7479:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7480:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7481:         my $same_institution;
 7482:         if ($intdom ne '') {
 7483:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 7484:             if (ref($internet_names) eq 'ARRAY') {
 7485:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 7486:                     $same_institution = 1;
 7487:                 }
 7488:             }
 7489:         }
 7490:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7491:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 7492:         if ($same_institution) {
 7493:             my %domservers = &Apache::lonnet::get_servers($dom);
 7494:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 7495:         } else {
 7496:             $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.");
 7497:         }
 7498:         $datatable .= '</td></tr>';
 7499:         $itemcount ++;
 7500:     } else {
 7501:         my %titles = &ssl_titles();
 7502:         my (%by_ip,%by_location,@intdoms,@instdoms);
 7503:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 7504:         my @alldoms = &Apache::lonnet::all_domains();
 7505:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 7506:         my @domservers = &Apache::lonnet::get_servers($dom);
 7507:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7508:         my %altids = &id_for_thisdom(%servers);
 7509:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 7510:             my $legacy;
 7511:             unless (ref($settings) eq 'HASH') {
 7512:                 my $name;
 7513:                 if ($position eq 'connto') {
 7514:                     $name = 'loncAllowInsecure';
 7515:                 } else {
 7516:                     $name = 'londAllowInsecure';
 7517:                 }
 7518:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 7519:                 my @ids=&Apache::lonnet::current_machine_ids();
 7520:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 7521:                     my %what = (
 7522:                                    $name => 1,
 7523:                                );
 7524:                     my ($result,$returnhash) =
 7525:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 7526:                     if ($result eq 'ok') {
 7527:                         if (ref($returnhash) eq 'HASH') {
 7528:                             $legacy = $returnhash->{$name};
 7529:                         }
 7530:                     }
 7531:                 } else {
 7532:                     $legacy = $Apache::lonnet::perlvar{$name};
 7533:                 }
 7534:             }
 7535:             foreach my $type ('dom','intdom','other') {
 7536:                 my %checked;
 7537:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7538:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 7539:                               '<td class="LC_right_item">';
 7540:                 my $skip; 
 7541:                 if ($type eq 'dom') {
 7542:                     unless (keys(%servers) > 1) {
 7543:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 7544:                         $skip = 1;
 7545:                     }
 7546:                 }
 7547:                 if ($type eq 'intdom') {
 7548:                     unless (@instdoms > 1) {
 7549:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 7550:                         $skip = 1;
 7551:                     } 
 7552:                 } elsif ($type eq 'other') {
 7553:                     if (keys(%by_location) == 0) {
 7554:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 7555:                         $skip = 1;
 7556:                     }
 7557:                 }
 7558:                 unless ($skip) {
 7559:                     $checked{'yes'} = ' checked="checked"'; 
 7560:                     if (ref($settings) eq 'HASH') {
 7561:                         if (ref($settings->{$position}) eq 'HASH') {
 7562:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 7563:                                 $checked{$1} = $checked{'yes'};
 7564:                                 delete($checked{'yes'}); 
 7565:                             }
 7566:                         }
 7567:                     } else {
 7568:                         if ($legacy == 0) {
 7569:                             $checked{'req'} = $checked{'yes'};
 7570:                             delete($checked{'yes'});    
 7571:                         }
 7572:                     }
 7573:                     foreach my $option ('no','yes','req') {
 7574:                         $datatable .= '<span class="LC_nobreak"><label>'.
 7575:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 7576:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 7577:                                       '</label></span>'.('&nbsp;'x2);
 7578:                     }
 7579:                 }
 7580:                 $datatable .= '</td></tr>';
 7581:                 $itemcount ++; 
 7582:             }
 7583:         } else {
 7584:             my $prefix = 'replication';
 7585:             my @types = ('certreq','nocertreq');
 7586:             if (keys(%by_location) == 0) {
 7587:                 $datatable .= '<tr'.$css_class.'><td>'.
 7588:                               &mt('Nothing to set here, as there are no other institutions').
 7589:                               '</td></tr>';
 7590:                 $itemcount ++;
 7591:             } else {
 7592:                 ($datatable,$itemcount) = 
 7593:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 7594:             }
 7595:         }
 7596:     }
 7597:     $$rowtotal += $itemcount;
 7598:     return $datatable;
 7599: }
 7600: 
 7601: sub ssl_titles {
 7602:     return &Apache::lonlocal::texthash (
 7603:                dom           => 'LON-CAPA servers/VMs from same domain',
 7604:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 7605:                other         => 'External LON-CAPA servers/VMs',
 7606:                connto        => 'Connections to other servers',
 7607:                connfrom      => 'Connections from other servers',
 7608:                replication   => 'Replicating content to other institutions',
 7609:                certreq       => 'Client certificate required, but specific domains exempt',
 7610:                nocertreq     => 'No client certificate required, except for specific domains',
 7611:                no            => 'SSL not used',
 7612:                yes           => 'SSL Optional (used if available)',
 7613:                req           => 'SSL Required',
 7614:     );
 7615: }
 7616: 
 7617: sub print_trust {
 7618:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 7619:     my ($css_class,$datatable,%checked,%choices);
 7620:     my (%by_ip,%by_location,@intdoms,@instdoms);
 7621:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 7622:     my $itemcount = 1;
 7623:     my %titles = &trust_titles();
 7624:     my @types = ('exc','inc');
 7625:     if ($prefix eq 'top') {
 7626:         $prefix = 'content';
 7627:     } elsif ($prefix eq 'bottom') {
 7628:         $prefix = 'msg';
 7629:     }
 7630:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 7631:     $$rowtotal += $itemcount;
 7632:     return $datatable;
 7633: }
 7634: 
 7635: sub trust_titles {
 7636:     return &Apache::lonlocal::texthash(
 7637:                content  => "Access to this domain's content by others",
 7638:                shared   => "Access to other domain's content by this domain",
 7639:                enroll   => "Enrollment in this domain's courses by others", 
 7640:                othcoau  => "Co-author roles in this domain for others",
 7641:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 7642:                domroles => "Domain roles in this domain assignable to others",
 7643:                catalog  => "Course Catalog for this domain displayed elsewhere",
 7644:                reqcrs   => "Requests for creation of courses in this domain by others",
 7645:                msg      => "Users in other domains can send messages to this domain",
 7646:                exc      => "Allow all, but exclude specific domains",
 7647:                inc      => "Deny all, but include specific domains",
 7648:            );
 7649: } 
 7650: 
 7651: sub build_location_hashes {
 7652:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 7653:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 7654:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 7655:     my %iphost = &Apache::lonnet::get_iphost();
 7656:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 7657:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 7658:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 7659:         foreach my $id (@{$iphost{$primary_ip}}) {
 7660:             my $intdom = &Apache::lonnet::internet_dom($id);
 7661:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 7662:                 push(@{$intdoms},$intdom);
 7663:             }
 7664:         }
 7665:     }
 7666:     foreach my $ip (keys(%iphost)) {
 7667:         if (ref($iphost{$ip}) eq 'ARRAY') {
 7668:             foreach my $id (@{$iphost{$ip}}) {
 7669:                 my $location = &Apache::lonnet::internet_dom($id);
 7670:                 if ($location) {
 7671:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 7672:                         my $dom = &Apache::lonnet::host_domain($id);
 7673:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 7674:                             push(@{$instdoms},$dom);
 7675:                         }
 7676:                         next;
 7677:                     }
 7678:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 7679:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 7680:                             push(@{$by_ip->{$ip}},$location);
 7681:                         }
 7682:                     } else {
 7683:                         $by_ip->{$ip} = [$location];
 7684:                     }
 7685:                 }
 7686:             }
 7687:         }
 7688:     }
 7689:     foreach my $ip (sort(keys(%{$by_ip}))) {
 7690:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 7691:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 7692:             my $first = $by_ip->{$ip}->[0];
 7693:             if (ref($by_location->{$first}) eq 'ARRAY') {
 7694:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 7695:                     push(@{$by_location->{$first}},$ip);
 7696:                 }
 7697:             } else {
 7698:                 $by_location->{$first} = [$ip];
 7699:             }
 7700:         }
 7701:     }
 7702:     return;
 7703: }
 7704: 
 7705: sub current_offloads_to {
 7706:     my ($dom,$settings,$servers) = @_;
 7707:     my (%spareid,%otherdomconfigs);
 7708:     if (ref($servers) eq 'HASH') {
 7709:         foreach my $lonhost (sort(keys(%{$servers}))) {
 7710:             my $gotspares;
 7711:             if (ref($settings) eq 'HASH') {
 7712:                 if (ref($settings->{'spares'}) eq 'HASH') {
 7713:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 7714:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 7715:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 7716:                         $gotspares = 1;
 7717:                     }
 7718:                 }
 7719:             }
 7720:             unless ($gotspares) {
 7721:                 my $gotspares;
 7722:                 my $serverhomeID =
 7723:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 7724:                 my $serverhomedom =
 7725:                     &Apache::lonnet::host_domain($serverhomeID);
 7726:                 if ($serverhomedom ne $dom) {
 7727:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 7728:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 7729:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 7730:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 7731:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 7732:                                 $gotspares = 1;
 7733:                             }
 7734:                         }
 7735:                     } else {
 7736:                         $otherdomconfigs{$serverhomedom} =
 7737:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 7738:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 7739:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 7740:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 7741:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 7742:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 7743:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 7744:                                         $gotspares = 1;
 7745:                                     }
 7746:                                 }
 7747:                             }
 7748:                         }
 7749:                     }
 7750:                 }
 7751:             }
 7752:             unless ($gotspares) {
 7753:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 7754:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 7755:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 7756:                } else {
 7757:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 7758:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 7759:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 7760:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 7761:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 7762:                     } else {
 7763:                         my %what = (
 7764:                              spareid => 1,
 7765:                         );
 7766:                         my ($result,$returnhash) = 
 7767:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 7768:                         if ($result eq 'ok') { 
 7769:                             if (ref($returnhash) eq 'HASH') {
 7770:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 7771:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 7772:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 7773:                                 }
 7774:                             }
 7775:                         }
 7776:                     }
 7777:                 }
 7778:             }
 7779:         }
 7780:     }
 7781:     return %spareid;
 7782: }
 7783: 
 7784: sub spares_row {
 7785:     my ($dom,$servers,$spareid,$serverhomes,$altids,$other_insts,
 7786:         $curroffloadnow,$curroffloadoth,$rowtotal) = @_;
 7787:     my $css_class;
 7788:     my $numinrow = 4;
 7789:     my $itemcount = 1;
 7790:     my $datatable;
 7791:     my %typetitles = &sparestype_titles();
 7792:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 7793:         foreach my $server (sort(keys(%{$servers}))) {
 7794:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 7795:             my ($othercontrol,$serverdom);
 7796:             if ($serverhome ne $server) {
 7797:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 7798:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 7799:             } else {
 7800:                 $serverdom = &Apache::lonnet::host_domain($server);
 7801:                 if ($serverdom ne $dom) {
 7802:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 7803:                 }
 7804:             }
 7805:             next unless (ref($spareid->{$server}) eq 'HASH');
 7806:             my ($checkednow,$checkedoth);
 7807:             if (ref($curroffloadnow) eq 'HASH') {
 7808:                 if ($curroffloadnow->{$server}) {
 7809:                     $checkednow = ' checked="checked"';
 7810:                 }
 7811:             }
 7812:             if (ref($curroffloadoth) eq 'HASH') {
 7813:                 if ($curroffloadoth->{$server}) {
 7814:                     $checkedoth = ' checked="checked"';
 7815:                 }
 7816:             }
 7817:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7818:             $datatable .= '<tr'.$css_class.'>
 7819:                            <td rowspan="2">
 7820:                             <span class="LC_nobreak">'.
 7821:                           &mt('[_1] when busy, offloads to:'
 7822:                               ,'<b>'.$server.'</b>').'</span><br />'.
 7823:                           '<span class="LC_nobreak">'."\n".
 7824:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 7825:                           '&nbsp;'.&mt('Switch any active user on next access').'</label></span>'.
 7826:                           "\n";
 7827:             if ($other_insts) {
 7828:                 $datatable .= '<br />'.
 7829:                               '<span class="LC_nobreak">'."\n".
 7830:                           '<label><input type="checkbox" name="offloadoth" value="'.$server.'"'.$checkedoth.' />'.
 7831:                           '&nbsp;'.&mt('Switch other institutions on next access').'</label></span>'.
 7832:                           "\n";
 7833:             }
 7834:             my (%current,%canselect);
 7835:             my @choices = 
 7836:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 7837:             foreach my $type ('primary','default') {
 7838:                 if (ref($spareid->{$server}) eq 'HASH') {
 7839:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 7840:                         my @spares = @{$spareid->{$server}{$type}};
 7841:                         if (@spares > 0) {
 7842:                             if ($othercontrol) {
 7843:                                 $current{$type} = join(', ',@spares);
 7844:                             } else {
 7845:                                 $current{$type} .= '<table>';
 7846:                                 my $numspares = scalar(@spares);
 7847:                                 for (my $i=0;  $i<@spares; $i++) {
 7848:                                     my $rem = $i%($numinrow);
 7849:                                     if ($rem == 0) {
 7850:                                         if ($i > 0) {
 7851:                                             $current{$type} .= '</tr>';
 7852:                                         }
 7853:                                         $current{$type} .= '<tr>';
 7854:                                     }
 7855:                                     $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;'.
 7856:                                                        $spareid->{$server}{$type}[$i].
 7857:                                                        '</label></td>'."\n";
 7858:                                 }
 7859:                                 my $rem = @spares%($numinrow);
 7860:                                 my $colsleft = $numinrow - $rem;
 7861:                                 if ($colsleft > 1 ) {
 7862:                                     $current{$type} .= '<td colspan="'.$colsleft.
 7863:                                                        '" class="LC_left_item">'.
 7864:                                                        '&nbsp;</td>';
 7865:                                 } elsif ($colsleft == 1) {
 7866:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 7867:                                 }
 7868:                                 $current{$type} .= '</tr></table>';
 7869:                             }
 7870:                         }
 7871:                     }
 7872:                     if ($current{$type} eq '') {
 7873:                         $current{$type} = &mt('None specified');
 7874:                     }
 7875:                     if ($othercontrol) {
 7876:                         if ($type eq 'primary') {
 7877:                             $canselect{$type} = $othercontrol;
 7878:                         }
 7879:                     } else {
 7880:                         $canselect{$type} = 
 7881:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 7882:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 7883:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 7884:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 7885:                         if (@choices > 0) {
 7886:                             foreach my $lonhost (@choices) {
 7887:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 7888:                             }
 7889:                         }
 7890:                         $canselect{$type} .= '</select>'."\n";
 7891:                     }
 7892:                 } else {
 7893:                     $current{$type} = &mt('Could not be determined');
 7894:                     if ($type eq 'primary') {
 7895:                         $canselect{$type} =  $othercontrol;
 7896:                     }
 7897:                 }
 7898:                 if ($type eq 'default') {
 7899:                     $datatable .= '<tr'.$css_class.'>';
 7900:                 }
 7901:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 7902:                               '<td>'.$current{$type}.'</td>'."\n".
 7903:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 7904:             }
 7905:             $itemcount ++;
 7906:         }
 7907:     }
 7908:     $$rowtotal += $itemcount;
 7909:     return $datatable;
 7910: }
 7911: 
 7912: sub possible_newspares {
 7913:     my ($server,$currspares,$serverhomes,$altids) = @_;
 7914:     my $serverhostname = &Apache::lonnet::hostname($server);
 7915:     my %excluded;
 7916:     if ($serverhostname ne '') {
 7917:         %excluded = (
 7918:                        $serverhostname => 1,
 7919:                     );
 7920:     }
 7921:     if (ref($currspares) eq 'HASH') {
 7922:         foreach my $type (keys(%{$currspares})) {
 7923:             if (ref($currspares->{$type}) eq 'ARRAY') {
 7924:                 if (@{$currspares->{$type}} > 0) {
 7925:                     foreach my $curr (@{$currspares->{$type}}) {
 7926:                         my $hostname = &Apache::lonnet::hostname($curr);
 7927:                         $excluded{$hostname} = 1;
 7928:                     }
 7929:                 }
 7930:             }
 7931:         }
 7932:     }
 7933:     my @choices;
 7934:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 7935:         if (keys(%{$serverhomes}) > 1) {
 7936:             foreach my $name (sort(keys(%{$serverhomes}))) {
 7937:                 unless ($excluded{$name}) {
 7938:                     if (exists($altids->{$serverhomes->{$name}})) {
 7939:                         push(@choices,$altids->{$serverhomes->{$name}});
 7940:                     } else {
 7941:                         push(@choices,$serverhomes->{$name});
 7942:                     }
 7943:                 }
 7944:             }
 7945:         }
 7946:     }
 7947:     return sort(@choices);
 7948: }
 7949: 
 7950: sub print_loadbalancing {
 7951:     my ($dom,$settings,$rowtotal) = @_;
 7952:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7953:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7954:     my $numinrow = 1;
 7955:     my $datatable;
 7956:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7957:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 7958:     if (ref($settings) eq 'HASH') {
 7959:         %existing = %{$settings};
 7960:     }
 7961:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 7962:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 7963:                                   \%currtargets,\%currrules,\%currcookies);
 7964:     } else {
 7965:         return;
 7966:     }
 7967:     my ($othertitle,$usertypes,$types) =
 7968:         &Apache::loncommon::sorted_inst_types($dom);
 7969:     my $rownum = 8;
 7970:     if (ref($types) eq 'ARRAY') {
 7971:         $rownum += scalar(@{$types});
 7972:     }
 7973:     my @css_class = ('LC_odd_row','LC_even_row');
 7974:     my $balnum = 0;
 7975:     my $islast;
 7976:     my (@toshow,$disabledtext);
 7977:     if (keys(%currbalancer) > 0) {
 7978:         @toshow = sort(keys(%currbalancer));
 7979:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 7980:             push(@toshow,'');
 7981:         }
 7982:     } else {
 7983:         @toshow = ('');
 7984:         $disabledtext = &mt('No existing load balancer');
 7985:     }
 7986:     foreach my $lonhost (@toshow) {
 7987:         if ($balnum == scalar(@toshow)-1) {
 7988:             $islast = 1;
 7989:         } else {
 7990:             $islast = 0;
 7991:         }
 7992:         my $cssidx = $balnum%2;
 7993:         my $targets_div_style = 'display: none';
 7994:         my $disabled_div_style = 'display: block';
 7995:         my $homedom_div_style = 'display: none';
 7996:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 7997:                       '<td rowspan="'.$rownum.'" style="vertical-align: top">'.
 7998:                       '<p>';
 7999:         if ($lonhost eq '') {
 8000:             $datatable .= '<span class="LC_nobreak">';
 8001:             if (keys(%currbalancer) > 0) {
 8002:                 $datatable .= &mt('Add balancer:');
 8003:             } else {
 8004:                 $datatable .= &mt('Enable balancer:');
 8005:             }
 8006:             $datatable .= '&nbsp;'.
 8007:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 8008:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 8009:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 8010:                           '<option value="" selected="selected">'.&mt('None').
 8011:                           '</option>'."\n";
 8012:             foreach my $server (sort(keys(%servers))) {
 8013:                 next if ($currbalancer{$server});
 8014:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 8015:             }
 8016:             $datatable .=
 8017:                 '</select>'."\n".
 8018:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 8019:         } else {
 8020:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 8021:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 8022:                            &mt('Stop balancing').'</label>'.
 8023:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 8024:             $targets_div_style = 'display: block';
 8025:             $disabled_div_style = 'display: none';
 8026:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 8027:                 $homedom_div_style = 'display: block';
 8028:             }
 8029:         }
 8030:         $datatable .= '</p></td><td rowspan="'.$rownum.'" style="vertical-align: top">'.
 8031:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 8032:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 8033:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 8034:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 8035:         my @sparestypes = ('primary','default');
 8036:         my %typetitles = &sparestype_titles();
 8037:         my %hostherechecked = (
 8038:                                   no => ' checked="checked"',
 8039:                               );
 8040:         my %balcookiechecked = (
 8041:                                   no => ' checked="checked"', 
 8042:                                );
 8043:         foreach my $sparetype (@sparestypes) {
 8044:             my $targettable;
 8045:             for (my $i=0; $i<$numspares; $i++) {
 8046:                 my $checked;
 8047:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 8048:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 8049:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 8050:                             $checked = ' checked="checked"';
 8051:                         }
 8052:                     }
 8053:                 }
 8054:                 my ($chkboxval,$disabled);
 8055:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 8056:                     $chkboxval = $spares[$i];
 8057:                 }
 8058:                 if (exists($currbalancer{$spares[$i]})) {
 8059:                     $disabled = ' disabled="disabled"';
 8060:                 }
 8061:                 $targettable .=
 8062:                     '<td><span class="LC_nobreak"><label>'.
 8063:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 8064:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 8065:                     '</span></label></span></td>';
 8066:                 my $rem = $i%($numinrow);
 8067:                 if ($rem == 0) {
 8068:                     if (($i > 0) && ($i < $numspares-1)) {
 8069:                         $targettable .= '</tr>';
 8070:                     }
 8071:                     if ($i < $numspares-1) {
 8072:                         $targettable .= '<tr>';
 8073:                     }
 8074:                 }
 8075:             }
 8076:             if ($targettable ne '') {
 8077:                 my $rem = $numspares%($numinrow);
 8078:                 my $colsleft = $numinrow - $rem;
 8079:                 if ($colsleft > 1 ) {
 8080:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8081:                                     '&nbsp;</td>';
 8082:                 } elsif ($colsleft == 1) {
 8083:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 8084:                 }
 8085:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 8086:                                '<table><tr>'.$targettable.'</tr></table><br />';
 8087:             }
 8088:             $hostherechecked{$sparetype} = '';
 8089:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 8090:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 8091:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 8092:                         $hostherechecked{$sparetype} = ' checked="checked"';
 8093:                         $hostherechecked{'no'} = '';
 8094:                     }
 8095:                 }
 8096:             }
 8097:         }
 8098:         if ($currcookies{$lonhost}) {
 8099:             %balcookiechecked = (
 8100:                                     yes => ' checked="checked"',
 8101:                                 );
 8102:         }
 8103:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 8104:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 8105:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 8106:         foreach my $sparetype (@sparestypes) {
 8107:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 8108:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 8109:                           '</i></label><br />';
 8110:         }
 8111:         $datatable .= &mt('Use balancer cookie').'<br />'.
 8112:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 8113:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 8114:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 8115:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 8116:                       '</div></td></tr>'.
 8117:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 8118:                                            $othertitle,$usertypes,$types,\%servers,
 8119:                                            \%currbalancer,$lonhost,
 8120:                                            $targets_div_style,$homedom_div_style,
 8121:                                            $css_class[$cssidx],$balnum,$islast);
 8122:         $$rowtotal += $rownum;
 8123:         $balnum ++;
 8124:     }
 8125:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 8126:     return $datatable;
 8127: }
 8128: 
 8129: sub get_loadbalancers_config {
 8130:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 8131:     return unless ((ref($servers) eq 'HASH') &&
 8132:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 8133:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 8134:                    (ref($currcookies) eq 'HASH'));
 8135:     if (keys(%{$existing}) > 0) {
 8136:         my $oldlonhost;
 8137:         foreach my $key (sort(keys(%{$existing}))) {
 8138:             if ($key eq 'lonhost') {
 8139:                 $oldlonhost = $existing->{'lonhost'};
 8140:                 $currbalancer->{$oldlonhost} = 1;
 8141:             } elsif ($key eq 'targets') {
 8142:                 if ($oldlonhost) {
 8143:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 8144:                 }
 8145:             } elsif ($key eq 'rules') {
 8146:                 if ($oldlonhost) {
 8147:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 8148:                 }
 8149:             } elsif (ref($existing->{$key}) eq 'HASH') {
 8150:                 $currbalancer->{$key} = 1;
 8151:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 8152:                 $currrules->{$key} = $existing->{$key}{'rules'};
 8153:                 if ($existing->{$key}{'cookie'}) {
 8154:                     $currcookies->{$key} = 1;
 8155:                 }
 8156:             }
 8157:         }
 8158:     } else {
 8159:         my ($balancerref,$targetsref) =
 8160:                 &Apache::lonnet::get_lonbalancer_config($servers);
 8161:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 8162:             foreach my $server (sort(keys(%{$balancerref}))) {
 8163:                 $currbalancer->{$server} = 1;
 8164:                 $currtargets->{$server} = $targetsref->{$server};
 8165:             }
 8166:         }
 8167:     }
 8168:     return;
 8169: }
 8170: 
 8171: sub loadbalancing_rules {
 8172:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 8173:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 8174:         $css_class,$balnum,$islast) = @_;
 8175:     my $output;
 8176:     my $num = 0;
 8177:     my ($alltypes,$othertypes,$titles) =
 8178:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 8179:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 8180:         foreach my $type (@{$alltypes}) {
 8181:             $num ++;
 8182:             my $current;
 8183:             if (ref($currrules) eq 'HASH') {
 8184:                 $current = $currrules->{$type};
 8185:             }
 8186:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 8187:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 8188:                     $current = '';
 8189:                 }
 8190:             }
 8191:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 8192:                                              $servers,$currbalancer,$lonhost,$dom,
 8193:                                              $targets_div_style,$homedom_div_style,
 8194:                                              $css_class,$balnum,$num,$islast);
 8195:         }
 8196:     }
 8197:     return $output;
 8198: }
 8199: 
 8200: sub loadbalancing_titles {
 8201:     my ($dom,$intdom,$usertypes,$types) = @_;
 8202:     my %othertypes = (
 8203:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 8204:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 8205:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 8206:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 8207:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 8208:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 8209:                      );
 8210:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 8211:     my @available;
 8212:     if (ref($types) eq 'ARRAY') {
 8213:         @available = @{$types};
 8214:     }
 8215:     unless (grep(/^default$/,@available)) {
 8216:         push(@available,'default');
 8217:     }
 8218:     unshift(@alltypes,@available);
 8219:     my %titles;
 8220:     foreach my $type (@alltypes) {
 8221:         if ($type =~ /^_LC_/) {
 8222:             $titles{$type} = $othertypes{$type};
 8223:         } elsif ($type eq 'default') {
 8224:             $titles{$type} = &mt('All users from [_1]',$dom);
 8225:             if (ref($types) eq 'ARRAY') {
 8226:                 if (@{$types} > 0) {
 8227:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 8228:                 }
 8229:             }
 8230:         } elsif (ref($usertypes) eq 'HASH') {
 8231:             $titles{$type} = $usertypes->{$type};
 8232:         }
 8233:     }
 8234:     return (\@alltypes,\%othertypes,\%titles);
 8235: }
 8236: 
 8237: sub loadbalance_rule_row {
 8238:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 8239:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 8240:     my @rulenames;
 8241:     my %ruletitles = &offloadtype_text();
 8242:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 8243:         @rulenames = ('balancer','offloadedto','specific');
 8244:     } else {
 8245:         @rulenames = ('default','homeserver');
 8246:         if ($type eq '_LC_external') {
 8247:             push(@rulenames,'externalbalancer');
 8248:         } else {
 8249:             push(@rulenames,'specific');
 8250:         }
 8251:         push(@rulenames,'none');
 8252:     }
 8253:     my $style = $targets_div_style;
 8254:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 8255:         $style = $homedom_div_style;
 8256:     }
 8257:     my $space;
 8258:     if ($islast && $num == 1) {
 8259:         $space = '<div style="display:inline-block;">&nbsp;</div>';
 8260:     }
 8261:     my $output =
 8262:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td style="vertical-align: top">'.$space.
 8263:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 8264:         '<td valaign="top">'.$space.
 8265:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 8266:     for (my $i=0; $i<@rulenames; $i++) {
 8267:         my $rule = $rulenames[$i];
 8268:         my ($checked,$extra);
 8269:         if ($rulenames[$i] eq 'default') {
 8270:             $rule = '';
 8271:         }
 8272:         if ($rulenames[$i] eq 'specific') {
 8273:             if (ref($servers) eq 'HASH') {
 8274:                 my $default;
 8275:                 if (($current ne '') && (exists($servers->{$current}))) {
 8276:                     $checked = ' checked="checked"';
 8277:                 }
 8278:                 unless ($checked) {
 8279:                     $default = ' selected="selected"';
 8280:                 }
 8281:                 $extra =
 8282:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 8283:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 8284:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 8285:                     '<option value=""'.$default.'></option>'."\n";
 8286:                 foreach my $server (sort(keys(%{$servers}))) {
 8287:                     if (ref($currbalancer) eq 'HASH') {
 8288:                         next if (exists($currbalancer->{$server}));
 8289:                     }
 8290:                     my $selected;
 8291:                     if ($server eq $current) {
 8292:                         $selected = ' selected="selected"';
 8293:                     }
 8294:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 8295:                 }
 8296:                 $extra .= '</select>';
 8297:             }
 8298:         } elsif ($rule eq $current) {
 8299:             $checked = ' checked="checked"';
 8300:         }
 8301:         $output .= '<span class="LC_nobreak"><label>'.
 8302:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 8303:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 8304:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 8305:                    ')"'.$checked.' />&nbsp;';
 8306:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 8307:             $output .= $ruletitles{'particular'};
 8308:         } else {
 8309:             $output .= $ruletitles{$rulenames[$i]};
 8310:         }
 8311:         $output .= '</label>'.$extra.'</span><br />'."\n";
 8312:     }
 8313:     $output .= '</div></td></tr>'."\n";
 8314:     return $output;
 8315: }
 8316: 
 8317: sub offloadtype_text {
 8318:     my %ruletitles = &Apache::lonlocal::texthash (
 8319:            'default'          => 'Offloads to default destinations',
 8320:            'homeserver'       => "Offloads to user's home server",
 8321:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 8322:            'specific'         => 'Offloads to specific server',
 8323:            'none'             => 'No offload',
 8324:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 8325:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 8326:            'particular'       => 'Session hosted (after re-auth) on server:',
 8327:     );
 8328:     return %ruletitles;
 8329: }
 8330: 
 8331: sub sparestype_titles {
 8332:     my %typestitles = &Apache::lonlocal::texthash (
 8333:                           'primary' => 'primary',
 8334:                           'default' => 'default',
 8335:                       );
 8336:     return %typestitles;
 8337: }
 8338: 
 8339: sub contact_titles {
 8340:     my %titles = &Apache::lonlocal::texthash (
 8341:                    'supportemail'    => 'Support E-mail address',
 8342:                    'adminemail'      => 'Default Server Admin E-mail address',
 8343:                    'errormail'       => 'Error reports to be e-mailed to',
 8344:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 8345:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 8346:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 8347:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 8348:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 8349:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 8350:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 8351:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 8352:                    'errorthreshold'  => 'Error/warning threshold for status e-mail',
 8353:                    'errorsysmail'    => 'Error threshold for e-mail to core group',
 8354:                    'errorweights'    => 'Weights used to compute error count',
 8355:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 8356:                  );
 8357:     my %short_titles = &Apache::lonlocal::texthash (
 8358:                            adminemail   => 'Admin E-mail address',
 8359:                            supportemail => 'Support E-mail',
 8360:                        );   
 8361:     return (\%titles,\%short_titles);
 8362: }
 8363: 
 8364: sub helpform_fields {
 8365:     my %titles =  &Apache::lonlocal::texthash (
 8366:                        'username'   => 'Name',
 8367:                        'user'       => 'Username/domain',
 8368:                        'phone'      => 'Phone',
 8369:                        'cc'         => 'Cc e-mail',
 8370:                        'course'     => 'Course Details',
 8371:                        'section'    => 'Sections',
 8372:                        'screenshot' => 'File upload',
 8373:     );
 8374:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 8375:     my %possoptions = (
 8376:                         username     => ['yes','no','req'],
 8377:                         phone        => ['yes','no','req'],
 8378:                         user         => ['yes','no'],
 8379:                         cc           => ['yes','no'],
 8380:                         course       => ['yes','no'],
 8381:                         section      => ['yes','no'],
 8382:                         screenshot   => ['yes','no'],
 8383:                       );
 8384:     my %fieldoptions = &Apache::lonlocal::texthash (
 8385:                          'yes'  => 'Optional',
 8386:                          'req'  => 'Required',
 8387:                          'no'   => "Not shown",
 8388:     );
 8389:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 8390: }
 8391: 
 8392: sub tool_titles {
 8393:     my %titles = &Apache::lonlocal::texthash (
 8394:                      aboutme    => 'Personal web page',
 8395:                      blog       => 'Blog',
 8396:                      webdav     => 'WebDAV',
 8397:                      portfolio  => 'Portfolio',
 8398:                      official   => 'Official courses (with institutional codes)',
 8399:                      unofficial => 'Unofficial courses',
 8400:                      community  => 'Communities',
 8401:                      textbook   => 'Textbook courses',
 8402:                      placement  => 'Placement tests',
 8403:                  );
 8404:     return %titles;
 8405: }
 8406: 
 8407: sub courserequest_titles {
 8408:     my %titles = &Apache::lonlocal::texthash (
 8409:                                    official   => 'Official',
 8410:                                    unofficial => 'Unofficial',
 8411:                                    community  => 'Communities',
 8412:                                    textbook   => 'Textbook',
 8413:                                    placement  => 'Placement tests',
 8414:                                    lti        => 'LTI Provider',
 8415:                                    norequest  => 'Not allowed',
 8416:                                    approval   => 'Approval by DC',
 8417:                                    validate   => 'With validation',
 8418:                                    autolimit  => 'Numerical limit',
 8419:                                    unlimited  => '(blank for unlimited)',
 8420:                  );
 8421:     return %titles;
 8422: }
 8423: 
 8424: sub authorrequest_titles {
 8425:     my %titles = &Apache::lonlocal::texthash (
 8426:                                    norequest  => 'Not allowed',
 8427:                                    approval   => 'Approval by Dom. Coord.',
 8428:                                    automatic  => 'Automatic approval',
 8429:                  );
 8430:     return %titles;
 8431: }
 8432: 
 8433: sub courserequest_conditions {
 8434:     my %conditions = &Apache::lonlocal::texthash (
 8435:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 8436:        validate   => '(Processing of request subject to institutional validation).',
 8437:                  );
 8438:     return %conditions;
 8439: }
 8440: 
 8441: 
 8442: sub print_usercreation {
 8443:     my ($position,$dom,$settings,$rowtotal) = @_;
 8444:     my $numinrow = 4;
 8445:     my $datatable;
 8446:     if ($position eq 'top') {
 8447:         $$rowtotal ++;
 8448:         my $rowcount = 0;
 8449:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 8450:         if (ref($rules) eq 'HASH') {
 8451:             if (keys(%{$rules}) > 0) {
 8452:                 $datatable .= &user_formats_row('username',$settings,$rules,
 8453:                                                 $ruleorder,$numinrow,$rowcount);
 8454:                 $$rowtotal ++;
 8455:                 $rowcount ++;
 8456:             }
 8457:         }
 8458:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 8459:         if (ref($idrules) eq 'HASH') {
 8460:             if (keys(%{$idrules}) > 0) {
 8461:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 8462:                                                 $idruleorder,$numinrow,$rowcount);
 8463:                 $$rowtotal ++;
 8464:                 $rowcount ++;
 8465:             }
 8466:         }
 8467:         if ($rowcount == 0) {
 8468:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 8469:             $$rowtotal ++;
 8470:             $rowcount ++;
 8471:         }
 8472:     } elsif ($position eq 'middle') {
 8473:         my @creators = ('author','course','requestcrs');
 8474:         my ($rules,$ruleorder) =
 8475:             &Apache::lonnet::inst_userrules($dom,'username');
 8476:         my %lt = &usercreation_types();
 8477:         my %checked;
 8478:         if (ref($settings) eq 'HASH') {
 8479:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 8480:                 foreach my $item (@creators) {
 8481:                     $checked{$item} = $settings->{'cancreate'}{$item};
 8482:                 }
 8483:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 8484:                 foreach my $item (@creators) {
 8485:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 8486:                         $checked{$item} = 'none';
 8487:                     }
 8488:                 }
 8489:             }
 8490:         }
 8491:         my $rownum = 0;
 8492:         foreach my $item (@creators) {
 8493:             $rownum ++;
 8494:             if ($checked{$item} eq '') {
 8495:                 $checked{$item} = 'any';
 8496:             }
 8497:             my $css_class;
 8498:             if ($rownum%2) {
 8499:                 $css_class = '';
 8500:             } else {
 8501:                 $css_class = ' class="LC_odd_row" ';
 8502:             }
 8503:             $datatable .= '<tr'.$css_class.'>'.
 8504:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 8505:                          '</span></td><td style="text-align: right">';
 8506:             my @options = ('any');
 8507:             if (ref($rules) eq 'HASH') {
 8508:                 if (keys(%{$rules}) > 0) {
 8509:                     push(@options,('official','unofficial'));
 8510:                 }
 8511:             }
 8512:             push(@options,'none');
 8513:             foreach my $option (@options) {
 8514:                 my $type = 'radio';
 8515:                 my $check = ' ';
 8516:                 if ($checked{$item} eq $option) {
 8517:                     $check = ' checked="checked" ';
 8518:                 } 
 8519:                 $datatable .= '<span class="LC_nobreak"><label>'.
 8520:                               '<input type="'.$type.'" name="can_createuser_'.
 8521:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 8522:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 8523:             }
 8524:             $datatable .= '</td></tr>';
 8525:         }
 8526:     } else {
 8527:         my @contexts = ('author','course','domain');
 8528:         my @authtypes = ('int','krb4','krb5','loc','lti');
 8529:         my %checked;
 8530:         if (ref($settings) eq 'HASH') {
 8531:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 8532:                 foreach my $item (@contexts) {
 8533:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 8534:                         foreach my $auth (@authtypes) {
 8535:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 8536:                                 $checked{$item}{$auth} = ' checked="checked" ';
 8537:                             }
 8538:                         }
 8539:                     }
 8540:                 }
 8541:             }
 8542:         } else {
 8543:             foreach my $item (@contexts) {
 8544:                 foreach my $auth (@authtypes) {
 8545:                     $checked{$item}{$auth} = ' checked="checked" ';
 8546:                 }
 8547:             }
 8548:         }
 8549:         my %title = &context_names();
 8550:         my %authname = &authtype_names();
 8551:         my $rownum = 0;
 8552:         my $css_class; 
 8553:         foreach my $item (@contexts) {
 8554:             if ($rownum%2) {
 8555:                 $css_class = '';
 8556:             } else {
 8557:                 $css_class = ' class="LC_odd_row" ';
 8558:             }
 8559:             $datatable .=   '<tr'.$css_class.'>'.
 8560:                             '<td>'.$title{$item}.
 8561:                             '</td><td class="LC_left_item">'.
 8562:                             '<span class="LC_nobreak">';
 8563:             foreach my $auth (@authtypes) {
 8564:                 $datatable .= '<label>'. 
 8565:                               '<input type="checkbox" name="'.$item.'_auth" '.
 8566:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 8567:                               $authname{$auth}.'</label>&nbsp;';
 8568:             }
 8569:             $datatable .= '</span></td></tr>';
 8570:             $rownum ++;
 8571:         }
 8572:         $$rowtotal += $rownum;
 8573:     }
 8574:     return $datatable;
 8575: }
 8576: 
 8577: sub print_selfcreation {
 8578:     my ($position,$dom,$settings,$rowtotal) = @_;
 8579:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 8580:         $emaildomain,$datatable);
 8581:     if (ref($settings) eq 'HASH') {
 8582:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 8583:             $createsettings = $settings->{'cancreate'};
 8584:             if (ref($createsettings) eq 'HASH') {
 8585:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 8586:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 8587:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 8588:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 8589:                         @selfcreate = ('email','login','sso');
 8590:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 8591:                         @selfcreate = ($createsettings->{'selfcreate'});
 8592:                     }
 8593:                 }
 8594:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 8595:                     $processing = $createsettings->{'selfcreateprocessing'};
 8596:                 }
 8597:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 8598:                     $emailoptions = $createsettings->{'emailoptions'};
 8599:                 }
 8600:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 8601:                     $emailverified = $createsettings->{'emailverified'};
 8602:                 }
 8603:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 8604:                     $emaildomain = $createsettings->{'emaildomain'};
 8605:                 }
 8606:             }
 8607:         }
 8608:     }
 8609:     my %radiohash;
 8610:     my $numinrow = 4;
 8611:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 8612:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8613:     if ($position eq 'top') {
 8614:         my %choices = &Apache::lonlocal::texthash (
 8615:                                                       cancreate_login      => 'Institutional Login',
 8616:                                                       cancreate_sso        => 'Institutional Single Sign On',
 8617:                                                   );
 8618:         my @toggles = sort(keys(%choices));
 8619:         my %defaultchecked = (
 8620:                                'cancreate_login' => 'off',
 8621:                                'cancreate_sso'   => 'off',
 8622:                              );
 8623:         my ($onclick,$itemcount);
 8624:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 8625:                                                      \%choices,$itemcount,$onclick);
 8626:         $$rowtotal += $itemcount;
 8627:         
 8628:         if (ref($usertypes) eq 'HASH') {
 8629:             if (keys(%{$usertypes}) > 0) {
 8630:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 8631:                                              $dom,$numinrow,$othertitle,
 8632:                                              'statustocreate',$rowtotal);
 8633:                 $$rowtotal ++;
 8634:             }
 8635:         }
 8636:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 8637:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8638:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 8639:         my $rem;
 8640:         my $numperrow = 2;
 8641:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 8642:         $datatable .= '<tr'.$css_class.'>'.
 8643:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 8644:                      '<td class="LC_left_item">'."\n".
 8645:                      '<table>'."\n";
 8646:         for (my $i=0; $i<@fields; $i++) {
 8647:             $rem = $i%($numperrow);
 8648:             if ($rem == 0) {
 8649:                 if ($i > 0) {
 8650:                     $datatable .= '</tr>';
 8651:                 }
 8652:                 $datatable .= '<tr>';
 8653:             }
 8654:             my $currval;
 8655:             if (ref($createsettings) eq 'HASH') {
 8656:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 8657:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 8658:                 }
 8659:             }
 8660:             $datatable .= '<td class="LC_left_item">'.
 8661:                           '<span class="LC_nobreak">'.
 8662:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 8663:                           'value="'.$currval.'" size="10" />&nbsp;'.
 8664:                           $fieldtitles{$fields[$i]}.'</span></td>';
 8665:         }
 8666:         my $colsleft = $numperrow - $rem;
 8667:         if ($colsleft > 1 ) {
 8668:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8669:                          '&nbsp;</td>';
 8670:         } elsif ($colsleft == 1) {
 8671:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 8672:         }
 8673:         $datatable .= '</tr></table></td></tr>';
 8674:         $$rowtotal ++;
 8675:     } elsif ($position eq 'middle') {
 8676:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 8677:         my @posstypes;
 8678:         if (ref($types) eq 'ARRAY') {
 8679:             @posstypes = @{$types};
 8680:         }
 8681:         unless (grep(/^default$/,@posstypes)) {
 8682:             push(@posstypes,'default');
 8683:         }
 8684:         my %usertypeshash;
 8685:         if (ref($usertypes) eq 'HASH') {
 8686:             %usertypeshash = %{$usertypes};
 8687:         }
 8688:         $usertypeshash{'default'} = $othertitle;
 8689:         foreach my $status (@posstypes) {
 8690:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 8691:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 8692:             $$rowtotal ++;
 8693:         }
 8694:     } else {
 8695:         my %choices = &Apache::lonlocal::texthash (
 8696:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 8697:                                                   );
 8698:         my @toggles = sort(keys(%choices));
 8699:         my %defaultchecked = (
 8700:                                'cancreate_email' => 'off',
 8701:                              );
 8702:         my $customclass = 'LC_selfcreate_email';
 8703:         my $classprefix = 'LC_canmodify_emailusername_';
 8704:         my $optionsprefix = 'LC_options_emailusername_';
 8705:         my $display = 'none';
 8706:         my $rowstyle = 'display:none';
 8707:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 8708:             $display = 'block';
 8709:             $rowstyle = 'display:table-row';
 8710:         }
 8711:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 8712:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 8713:                                                      \%choices,$$rowtotal,$onclick);
 8714:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 8715:                                          $rowstyle);
 8716:         $$rowtotal ++;
 8717:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 8718:                                       $rowstyle);
 8719:         $$rowtotal ++;
 8720:         my (@ordered,@posstypes,%usertypeshash);
 8721:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 8722:         my ($emailrules,$emailruleorder) =
 8723:             &Apache::lonnet::inst_userrules($dom,'email');
 8724:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8725:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8726:         if (ref($types) eq 'ARRAY') {
 8727:             @posstypes = @{$types};
 8728:         }
 8729:         if (@posstypes) {
 8730:             unless (grep(/^default$/,@posstypes)) {
 8731:                 push(@posstypes,'default');
 8732:             }
 8733:             if (ref($usertypes) eq 'HASH') {
 8734:                 %usertypeshash = %{$usertypes};
 8735:             }
 8736:             my $currassign;
 8737:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 8738:                 $currassign = {
 8739:                                   selfassign => $domdefaults{'inststatusguest'},
 8740:                               };
 8741:                 @ordered = @{$domdefaults{'inststatusguest'}};
 8742:             } else {
 8743:                 $currassign = { selfassign => [] };
 8744:             }
 8745:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 8746:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 8747:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 8748:                                          $numinrow,$othertitle,'selfassign',
 8749:                                          $rowtotal,$onclicktypes,$customclass,
 8750:                                          $rowstyle);
 8751:             $$rowtotal ++;
 8752:             $usertypeshash{'default'} = $othertitle;
 8753:             foreach my $status (@posstypes) {
 8754:                 my $css_class;
 8755:                 if ($$rowtotal%2) {
 8756:                     $css_class = 'LC_odd_row ';
 8757:                 }
 8758:                 $css_class .= $customclass;
 8759:                 my $rowid = $optionsprefix.$status;
 8760:                 my $hidden = 1;
 8761:                 my $currstyle = 'display:none';
 8762:                 if (grep(/^\Q$status\E$/,@ordered)) {
 8763:                     $currstyle = $rowstyle;
 8764:                     $hidden = 0; 
 8765:                 }
 8766:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 8767:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 8768:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 8769:                 unless ($hidden) {
 8770:                     $$rowtotal ++;
 8771:                 }
 8772:             }
 8773:         } else {
 8774:             my $css_class;
 8775:             if ($$rowtotal%2) {
 8776:                 $css_class = 'LC_odd_row ';
 8777:             }
 8778:             $css_class .= $customclass;
 8779:             $usertypeshash{'default'} = $othertitle;
 8780:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 8781:                                          $emailrules,$emailruleorder,$settings,'default','',
 8782:                                          $othertitle,$css_class,$rowstyle,$intdom);
 8783:             $$rowtotal ++;
 8784:         }
 8785:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 8786:         $numinrow = 1;
 8787:         if (@posstypes) {
 8788:             foreach my $status (@posstypes) {
 8789:                 my $rowid = $classprefix.$status;
 8790:                 my $datarowstyle = 'display:none';
 8791:                 if (grep(/^\Q$status\E$/,@ordered)) { 
 8792:                     $datarowstyle = $rowstyle; 
 8793:                 }
 8794:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 8795:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 8796:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 8797:                 unless ($datarowstyle eq 'display:none') {
 8798:                     $$rowtotal ++;
 8799:                 }
 8800:             }
 8801:         } else {
 8802:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 8803:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 8804:                                                    $infotitles,'',$customclass,$rowstyle);
 8805:         }
 8806:     }
 8807:     return $datatable;
 8808: }
 8809: 
 8810: sub selfcreate_javascript {
 8811:     return <<"ENDSCRIPT";
 8812: 
 8813: <script type="text/javascript">
 8814: // <![CDATA[
 8815: 
 8816: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 8817:     var x = document.getElementsByClassName(target);
 8818:     var insttypes = 0;
 8819:     var insttypeRegExp = new RegExp(prefix);
 8820:     if ((x.length != undefined) && (x.length > 0)) {
 8821:         if (form.elements[radio].length != undefined) {
 8822:             for (var i=0; i<form.elements[radio].length; i++) {
 8823:                 if (form.elements[radio][i].checked) {
 8824:                     if (form.elements[radio][i].value == 1) {
 8825:                         for (var j=0; j<x.length; j++) {
 8826:                             if (x[j].id == 'undefined') {
 8827:                                 x[j].style.display = 'table-row';
 8828:                             } else if (insttypeRegExp.test(x[j].id)) {
 8829:                                 insttypes ++;
 8830:                             } else {
 8831:                                 x[j].style.display = 'table-row';
 8832:                             }
 8833:                         }
 8834:                     } else {
 8835:                         for (var j=0; j<x.length; j++) {
 8836:                             x[j].style.display = 'none';
 8837:                         }
 8838:                     }
 8839:                     break;
 8840:                 }
 8841:             }
 8842:             if (insttypes > 0) {
 8843:                 toggleDataRow(form,checkbox,target,altprefix);
 8844:                 toggleDataRow(form,checkbox,target,prefix,1);
 8845:             }
 8846:         }
 8847:     }
 8848:     return;
 8849: }
 8850: 
 8851: function toggleDataRow(form,checkbox,target,prefix,docount) {
 8852:     if (form.elements[checkbox].length != undefined) {
 8853:         var count = 0;
 8854:         if (docount) {
 8855:             for (var i=0; i<form.elements[checkbox].length; i++) {
 8856:                 if (form.elements[checkbox][i].checked) {
 8857:                     count ++;
 8858:                 }
 8859:             }
 8860:         }
 8861:         for (var i=0; i<form.elements[checkbox].length; i++) {
 8862:             var type = form.elements[checkbox][i].value;
 8863:             if (document.getElementById(prefix+type)) {
 8864:                 if (form.elements[checkbox][i].checked) {
 8865:                     document.getElementById(prefix+type).style.display = 'table-row';
 8866:                     if (count % 2 == 1) {
 8867:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 8868:                     } else {
 8869:                         document.getElementById(prefix+type).className = target;
 8870:                     }
 8871:                     count ++;
 8872:                 } else {
 8873:                     document.getElementById(prefix+type).style.display = 'none';
 8874:                 }
 8875:             }
 8876:         }
 8877:     }
 8878:     return;
 8879: }
 8880: 
 8881: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 8882:     var caller = radio+'_'+status;
 8883:     if (form.elements[caller].length != undefined) {
 8884:         for (var i=0; i<form.elements[caller].length; i++) {
 8885:             if (form.elements[caller][i].checked) {
 8886:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 8887:                     var curr = form.elements[caller][i].value;
 8888:                     if (prefix) {
 8889:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 8890:                     }
 8891:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 8892:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 8893:                     if (curr == 'custom') {
 8894:                         if (prefix) { 
 8895:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 8896:                         }
 8897:                     } else if (curr == 'inst') {
 8898:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 8899:                     } else if (curr == 'noninst') {
 8900:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 8901:                     }
 8902:                     break;
 8903:                 }
 8904:             }
 8905:         }
 8906:     }
 8907: }
 8908: 
 8909: // ]]>
 8910: </script>
 8911: 
 8912: ENDSCRIPT
 8913: }
 8914: 
 8915: sub noninst_users {
 8916:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 8917:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_; 
 8918:     my $class = 'LC_left_item';
 8919:     if ($css_class) {
 8920:         $css_class = ' class="'.$css_class.'"'; 
 8921:     }
 8922:     if ($rowid) {
 8923:         $rowid = ' id="'.$rowid.'"';
 8924:     }
 8925:     if ($rowstyle) {
 8926:         $rowstyle = ' style="'.$rowstyle.'"';
 8927:     }
 8928:     my ($output,$description);
 8929:     if ($type eq 'default') {
 8930:         $description = &mt('Requests for: [_1]',$typetitle);
 8931:     } else {
 8932:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 8933:     }
 8934:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 8935:               "<td>$description</td>\n".           
 8936:               '<td class="'.$class.'" colspan="2">'.
 8937:               '<table><tr>';
 8938:     my %headers = &Apache::lonlocal::texthash( 
 8939:               approve  => 'Processing',
 8940:               email    => 'E-mail',
 8941:               username => 'Username',
 8942:     );
 8943:     foreach my $item ('approve','email','username') {
 8944:         $output .= '<th>'.$headers{$item}.'</th>';
 8945:     }
 8946:     $output .= '</tr><tr>';
 8947:     foreach my $item ('approve','email','username') {
 8948:         $output .= '<td style="vertical-align: top">';
 8949:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 8950:         if ($item eq 'approve') {
 8951:             %choices = &Apache::lonlocal::texthash (
 8952:                                                      automatic => 'Automatically approved',
 8953:                                                      approval  => 'Queued for approval',
 8954:                                                    );
 8955:             @options = ('automatic','approval');
 8956:             $hashref = $processing;
 8957:             $defoption = 'automatic';
 8958:             $name = 'cancreate_emailprocess_'.$type;
 8959:         } elsif ($item eq 'email') {
 8960:             %choices = &Apache::lonlocal::texthash (
 8961:                                                      any     => 'Any e-mail',
 8962:                                                      inst    => 'Institutional only',
 8963:                                                      noninst => 'Non-institutional only',
 8964:                                                      custom  => 'Custom restrictions',
 8965:                                                    );
 8966:             @options = ('any','inst','noninst');
 8967:             my $showcustom;
 8968:             if (ref($emailrules) eq 'HASH') {
 8969:                 if (keys(%{$emailrules}) > 0) {
 8970:                     push(@options,'custom');
 8971:                     $showcustom = 'cancreate_emailrule';
 8972:                     if (ref($settings) eq 'HASH') {
 8973:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 8974:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 8975:                                 if (exists($emailrules->{$rule})) {
 8976:                                     $hascustom ++;
 8977:                                 }
 8978:                             }
 8979:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 8980:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 8981:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 8982:                                     if (exists($emailrules->{$rule})) {
 8983:                                         $hascustom ++;
 8984:                                     }
 8985:                                 }
 8986:                             }
 8987:                         }
 8988:                     }
 8989:                 }
 8990:             }
 8991:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 8992:                                                      "'cancreate_emaildomain','$type'".');"';
 8993:             $hashref = $emailoptions;
 8994:             $defoption = 'any';
 8995:             $name = 'cancreate_emailoptions_'.$type;
 8996:         } elsif ($item eq 'username') {
 8997:             %choices = &Apache::lonlocal::texthash (
 8998:                                                      all    => 'Same as e-mail',
 8999:                                                      first  => 'Omit @domain',
 9000:                                                      free   => 'Free to choose',
 9001:                                                    );
 9002:             @options = ('all','first','free');
 9003:             $hashref = $emailverified;
 9004:             $defoption = 'all';
 9005:             $name = 'cancreate_usernameoptions_'.$type;
 9006:         }
 9007:         foreach my $option (@options) {
 9008:             my $checked;
 9009:             if (ref($hashref) eq 'HASH') {
 9010:                 if ($type eq '') {
 9011:                     if (!exists($hashref->{'default'})) {
 9012:                         if ($option eq $defoption) {
 9013:                             $checked = ' checked="checked"';
 9014:                         }
 9015:                     } else {
 9016:                         if ($hashref->{'default'} eq $option) {
 9017:                             $checked = ' checked="checked"';
 9018:                         }
 9019:                     }
 9020:                 } else {
 9021:                     if (!exists($hashref->{$type})) {
 9022:                         if ($option eq $defoption) {
 9023:                             $checked = ' checked="checked"';
 9024:                         }
 9025:                     } else {
 9026:                         if ($hashref->{$type} eq $option) {
 9027:                             $checked = ' checked="checked"';
 9028:                         }
 9029:                     }
 9030:                 }
 9031:             } elsif (($item eq 'email') && ($hascustom)) {
 9032:                 if ($option eq 'custom') {
 9033:                     $checked = ' checked="checked"';
 9034:                 }
 9035:             } elsif ($option eq $defoption) {
 9036:                 $checked = ' checked="checked"';
 9037:             }
 9038:             $output .= '<span class="LC_nobreak"><label>'.
 9039:                        '<input type="radio" name="'.$name.'"'.
 9040:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 9041:                        $choices{$option}.'</label></span><br />';
 9042:             if ($item eq 'email') {
 9043:                 if ($option eq 'custom') {
 9044:                     my $id = 'cancreate_emailrule_'.$type;
 9045:                     my $display = 'none';
 9046:                     if ($checked) {
 9047:                         $display = 'inline';
 9048:                     }
 9049:                     my $numinrow = 2;
 9050:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 9051:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 9052:                                &user_formats_row('email',$settings,$emailrules,
 9053:                                                  $emailruleorder,$numinrow,'',$type);
 9054:                               '</table></fieldset>';
 9055:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 9056:                     my %text = &Apache::lonlocal::texthash (
 9057:                                                              inst    => 'must end:',
 9058:                                                              noninst => 'cannot end:',
 9059:                                                            );
 9060:                     my $value;
 9061:                     if (ref($emaildomain) eq 'HASH') {
 9062:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 9063:                             $value = $emaildomain->{$type}->{$option}; 
 9064:                         }
 9065:                     }
 9066:                     if ($value eq '') {
 9067:                         $value = '@'.$intdom;
 9068:                     }
 9069:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 9070:                     my $display = 'none';
 9071:                     if ($checked) {
 9072:                         $display = 'inline';
 9073:                     }
 9074:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 9075:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 9076:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 9077:                                '</div>';
 9078:                 }
 9079:             }
 9080:         }
 9081:         $output .= '</td>'."\n";
 9082:     }
 9083:     $output .= "</tr></table></td></tr>\n";
 9084:     return $output;
 9085: }
 9086: 
 9087: sub captcha_choice {
 9088:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 9089:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 9090:         $vertext,$currver);
 9091:     my %lt = &captcha_phrases();
 9092:     $keyentry = 'hidden';
 9093:     my $colspan=2;
 9094:     if ($context eq 'cancreate') {
 9095:         $rowname = &mt('CAPTCHA validation');
 9096:     } elsif ($context eq 'login') {
 9097:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 9098:     } elsif ($context eq 'passwords') {
 9099:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
 9100:         $colspan=1;
 9101:     }
 9102:     if (ref($settings) eq 'HASH') {
 9103:         if ($settings->{'captcha'}) {
 9104:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 9105:         } else {
 9106:             $checked{'original'} = ' checked="checked"';
 9107:         }
 9108:         if ($settings->{'captcha'} eq 'recaptcha') {
 9109:             $pubtext = $lt{'pub'};
 9110:             $privtext = $lt{'priv'};
 9111:             $keyentry = 'text';
 9112:             $vertext = $lt{'ver'};
 9113:             $currver = $settings->{'recaptchaversion'};
 9114:             if ($currver ne '2') {
 9115:                 $currver = 1;
 9116:             }
 9117:         }
 9118:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 9119:             $currpub = $settings->{'recaptchakeys'}{'public'};
 9120:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 9121:         }
 9122:     } else {
 9123:         $checked{'original'} = ' checked="checked"';
 9124:     }
 9125:     my $css_class;
 9126:     if ($itemcount%2) {
 9127:         $css_class = 'LC_odd_row';
 9128:     }
 9129:     if ($customcss) {
 9130:         $css_class .= " $customcss";
 9131:     }
 9132:     $css_class =~ s/^\s+//;
 9133:     if ($css_class) {
 9134:         $css_class = ' class="'.$css_class.'"';
 9135:     }
 9136:     if ($rowstyle) {
 9137:         $css_class .= ' style="'.$rowstyle.'"';
 9138:     }
 9139:     my $output = '<tr'.$css_class.'>'.
 9140:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
 9141:                  '<table><tr><td>'."\n";
 9142:     foreach my $option ('original','recaptcha','notused') {
 9143:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 9144:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 9145:                    $lt{$option}.'</label></span>';
 9146:         unless ($option eq 'notused') {
 9147:             $output .= ('&nbsp;'x2)."\n";
 9148:         }
 9149:     }
 9150: #
 9151: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 9152: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 9153: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 9154: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 9155: #
 9156:     $output .= '</td></tr>'."\n".
 9157:                '<tr><td class="LC_zero_height">'."\n".
 9158:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 9159:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 9160:                $currpub.'" size="40" /></span><br />'."\n".
 9161:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 9162:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 9163:                $currpriv.'" size="40" /></span><br />'.
 9164:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 9165:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 9166:                $currver.'" size="3" /></span><br />'.
 9167:                '</td></tr></table>'."\n".
 9168:                '</td></tr>';
 9169:     return $output;
 9170: }
 9171: 
 9172: sub user_formats_row {
 9173:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 9174:     my $output;
 9175:     my %text = (
 9176:                    'username' => 'new usernames',
 9177:                    'id'       => 'IDs',
 9178:                );
 9179:     unless ($type eq 'email') {
 9180:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 9181:         $output = '<tr '.$css_class.'>'.
 9182:                   '<td><span class="LC_nobreak">'.
 9183:                   &mt("Format rules to check for $text{$type}: ").
 9184:                   '</td><td class="LC_left_item" colspan="2"><table>';
 9185:     }
 9186:     my $rem;
 9187:     if (ref($ruleorder) eq 'ARRAY') {
 9188:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 9189:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 9190:                 my $rem = $i%($numinrow);
 9191:                 if ($rem == 0) {
 9192:                     if ($i > 0) {
 9193:                         $output .= '</tr>';
 9194:                     }
 9195:                     $output .= '<tr>';
 9196:                 }
 9197:                 my $check = ' ';
 9198:                 if (ref($settings) eq 'HASH') {
 9199:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 9200:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 9201:                             $check = ' checked="checked" ';
 9202:                         }
 9203:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 9204:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 9205:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 9206:                                 $check = ' checked="checked" ';
 9207:                             }
 9208:                         }
 9209:                     }
 9210:                 }
 9211:                 my $name = $type.'_rule';
 9212:                 if ($type eq 'email') {
 9213:                     $name .= '_'.$status;
 9214:                 }
 9215:                 $output .= '<td class="LC_left_item">'.
 9216:                            '<span class="LC_nobreak"><label>'.
 9217:                            '<input type="checkbox" name="'.$name.'" '.
 9218:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 9219:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 9220:             }
 9221:         }
 9222:         $rem = @{$ruleorder}%($numinrow);
 9223:     }
 9224:     my $colsleft;
 9225:     if ($rem) {
 9226:         $colsleft = $numinrow - $rem;
 9227:     }
 9228:     if ($colsleft > 1 ) {
 9229:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9230:                    '&nbsp;</td>';
 9231:     } elsif ($colsleft == 1) {
 9232:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 9233:     }
 9234:     $output .= '</tr></table>';
 9235:     unless ($type eq 'email') {
 9236:         $output .= '</td></tr>';
 9237:     }
 9238:     return $output;
 9239: }
 9240: 
 9241: sub usercreation_types {
 9242:     my %lt = &Apache::lonlocal::texthash (
 9243:                     author     => 'When adding a co-author',
 9244:                     course     => 'When adding a user to a course',
 9245:                     requestcrs => 'When requesting a course',
 9246:                     any        => 'Any',
 9247:                     official   => 'Institutional only ',
 9248:                     unofficial => 'Non-institutional only',
 9249:                     none       => 'None',
 9250:     );
 9251:     return %lt;
 9252: }
 9253: 
 9254: sub selfcreation_types {
 9255:     my %lt = &Apache::lonlocal::texthash (
 9256:                     selfcreate => 'User creates own account',
 9257:                     any        => 'Any',
 9258:                     official   => 'Institutional only ',
 9259:                     unofficial => 'Non-institutional only',
 9260:                     email      => 'E-mail address',
 9261:                     login      => 'Institutional Login',
 9262:                     sso        => 'SSO',
 9263:              );
 9264: }
 9265: 
 9266: sub authtype_names {
 9267:     my %lt = &Apache::lonlocal::texthash(
 9268:                       int    => 'Internal',
 9269:                       krb4   => 'Kerberos 4',
 9270:                       krb5   => 'Kerberos 5',
 9271:                       loc    => 'Local',
 9272:                       lti    => 'LTI',
 9273:                   );
 9274:     return %lt;
 9275: }
 9276: 
 9277: sub context_names {
 9278:     my %context_title = &Apache::lonlocal::texthash(
 9279:        author => 'Creating users when an Author',
 9280:        course => 'Creating users when in a course',
 9281:        domain => 'Creating users when a Domain Coordinator',
 9282:     );
 9283:     return %context_title;
 9284: }
 9285: 
 9286: sub print_usermodification {
 9287:     my ($position,$dom,$settings,$rowtotal) = @_;
 9288:     my $numinrow = 4;
 9289:     my ($context,$datatable,$rowcount);
 9290:     if ($position eq 'top') {
 9291:         $rowcount = 0;
 9292:         $context = 'author'; 
 9293:         foreach my $role ('ca','aa') {
 9294:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 9295:                                                    $numinrow,$rowcount);
 9296:             $$rowtotal ++;
 9297:             $rowcount ++;
 9298:         }
 9299:     } elsif ($position eq 'bottom') {
 9300:         $context = 'course';
 9301:         $rowcount = 0;
 9302:         foreach my $role ('st','ep','ta','in','cr') {
 9303:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 9304:                                                    $numinrow,$rowcount);
 9305:             $$rowtotal ++;
 9306:             $rowcount ++;
 9307:         }
 9308:     }
 9309:     return $datatable;
 9310: }
 9311: 
 9312: sub print_defaults {
 9313:     my ($position,$dom,$settings,$rowtotal) = @_;
 9314:     my $rownum = 0;
 9315:     my ($datatable,$css_class,$titles);
 9316:     unless ($position eq 'bottom') {
 9317:         $titles = &defaults_titles($dom);
 9318:     }
 9319:     if ($position eq 'top') {
 9320:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 9321:                      'datelocale_def','portal_def');
 9322:         my %defaults;
 9323:         if (ref($settings) eq 'HASH') {
 9324:             %defaults = %{$settings};
 9325:         } else {
 9326:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9327:             foreach my $item (@items) {
 9328:                 $defaults{$item} = $domdefaults{$item};
 9329:             }
 9330:         }
 9331:         foreach my $item (@items) {
 9332:             if ($rownum%2) {
 9333:                 $css_class = '';
 9334:             } else {
 9335:                 $css_class = ' class="LC_odd_row" ';
 9336:             }
 9337:             $datatable .= '<tr'.$css_class.'>'.
 9338:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 9339:                           '</span></td><td class="LC_right_item" colspan="3">';
 9340:             if ($item eq 'auth_def') {
 9341:                 my @authtypes = ('internal','krb4','krb5','localauth','lti');
 9342:                 my %shortauth = (
 9343:                                  internal => 'int',
 9344:                                  krb4 => 'krb4',
 9345:                                  krb5 => 'krb5',
 9346:                                  localauth  => 'loc',
 9347:                                  lti => 'lti',
 9348:                                 );
 9349:                 my %authnames = &authtype_names();
 9350:                 foreach my $auth (@authtypes) {
 9351:                     my $checked = ' ';
 9352:                     if ($defaults{$item} eq $auth) {
 9353:                         $checked = ' checked="checked" ';
 9354:                     }
 9355:                     $datatable .= '<label><input type="radio" name="'.$item.
 9356:                                   '" value="'.$auth.'"'.$checked.'/>'.
 9357:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 9358:                 }
 9359:             } elsif ($item eq 'timezone_def') {
 9360:                 my $includeempty = 1;
 9361:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 9362:             } elsif ($item eq 'datelocale_def') {
 9363:                 my $includeempty = 1;
 9364:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 9365:             } elsif ($item eq 'lang_def') {
 9366:                 my $includeempty = 1;
 9367:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 9368:             } else {
 9369:                 my $size;
 9370:                 if ($item eq 'portal_def') {
 9371:                     $size = ' size="25"';
 9372:                 }
 9373:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 9374:                               $defaults{$item}.'"'.$size.' />';
 9375:             }
 9376:             $datatable .= '</td></tr>';
 9377:             $rownum ++;
 9378:         }
 9379:     } else {
 9380:         my %defaults;
 9381:         if (ref($settings) eq 'HASH') {
 9382:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 9383:                 my $maxnum = @{$settings->{'inststatusorder'}};
 9384:                 for (my $i=0; $i<$maxnum; $i++) {
 9385:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 9386:                     my $item = $settings->{'inststatusorder'}->[$i];
 9387:                     my $title = $settings->{'inststatustypes'}->{$item};
 9388:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 9389:                     $datatable .= '<tr'.$css_class.'>'.
 9390:                                   '<td><span class="LC_nobreak">'.
 9391:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 9392:                     for (my $k=0; $k<=$maxnum; $k++) {
 9393:                         my $vpos = $k+1;
 9394:                         my $selstr;
 9395:                         if ($k == $i) {
 9396:                             $selstr = ' selected="selected" ';
 9397:                         }
 9398:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 9399:                     }
 9400:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 9401:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 9402:                                   &mt('delete').'</span></td>'.
 9403:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed:').
 9404:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 9405:                                   '</span></td></tr>';
 9406:                 }
 9407:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 9408:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 9409:                 $datatable .= '<tr '.$css_class.'>'.
 9410:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 9411:                 for (my $k=0; $k<=$maxnum; $k++) {
 9412:                     my $vpos = $k+1;
 9413:                     my $selstr;
 9414:                     if ($k == $maxnum) {
 9415:                         $selstr = ' selected="selected" ';
 9416:                     }
 9417:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 9418:                 }
 9419:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 9420:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 9421:                               '&nbsp;'.&mt('(new)').
 9422:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
 9423:                               &mt('Name displayed:').
 9424:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 9425:                               '</tr>'."\n";
 9426:                 $rownum ++;
 9427:             }
 9428:         }
 9429:     }
 9430:     $$rowtotal += $rownum;
 9431:     return $datatable;
 9432: }
 9433: 
 9434: sub get_languages_hash {
 9435:     my %langchoices;
 9436:     foreach my $id (&Apache::loncommon::languageids()) {
 9437:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 9438:         if ($code ne '') {
 9439:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 9440:         }
 9441:     }
 9442:     return %langchoices;
 9443: }
 9444: 
 9445: sub defaults_titles {
 9446:     my ($dom) = @_;
 9447:     my %titles = &Apache::lonlocal::texthash (
 9448:                    'auth_def'      => 'Default authentication type',
 9449:                    'auth_arg_def'  => 'Default authentication argument',
 9450:                    'lang_def'      => 'Default language',
 9451:                    'timezone_def'  => 'Default timezone',
 9452:                    'datelocale_def' => 'Default locale for dates',
 9453:                    'portal_def'     => 'Portal/Default URL',
 9454:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 9455:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 9456:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 9457:                  );
 9458:     if ($dom) {
 9459:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 9460:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 9461:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 9462:         $protocol = 'http' if ($protocol ne 'https');
 9463:         if ($uint_dom) {
 9464:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 9465:                                          $uint_dom);
 9466:         }
 9467:     }
 9468:     return (\%titles);
 9469: }
 9470: 
 9471: sub print_scantron {
 9472:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
 9473:     if ($position eq 'top') {
 9474:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 9475:     } else {
 9476:         return &print_scantronconfig($dom,$settings,\$rowtotal);
 9477:     }
 9478: }
 9479: 
 9480: sub scantron_javascript {
 9481:     return <<"ENDSCRIPT";
 9482: 
 9483: <script type="text/javascript">
 9484: // <![CDATA[
 9485: 
 9486: function toggleScantron(form) {
 9487:     var csvfieldset = new Array();
 9488:     if (document.getElementById('scantroncsv_cols')) {
 9489:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
 9490:     }
 9491:     if (document.getElementById('scantroncsv_options')) {
 9492:         csvfieldset.push(document.getElementById('scantroncsv_options'));
 9493:     }
 9494:     if (csvfieldset.length) {
 9495:         if (document.getElementById('scantronconfcsv')) {
 9496:             var scantroncsv = document.getElementById('scantronconfcsv');
 9497:             if (scantroncsv.checked) {
 9498:                 for (var i=0; i<csvfieldset.length; i++) {
 9499:                     csvfieldset[i].style.display = 'block';
 9500:                 }
 9501:             } else {
 9502:                 for (var i=0; i<csvfieldset.length; i++) {
 9503:                     csvfieldset[i].style.display = 'none';
 9504:                 }
 9505:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
 9506:                 if (csvselects.length) {
 9507:                     for (var j=0; j<csvselects.length; j++) {
 9508:                         csvselects[j].selectedIndex = 0;
 9509:                     }
 9510:                 }
 9511:             }
 9512:         }
 9513:     }
 9514:     return;
 9515: }
 9516: // ]]>
 9517: </script>
 9518: 
 9519: ENDSCRIPT
 9520: 
 9521: }
 9522: 
 9523: sub print_scantronformat {
 9524:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 9525:     my $itemcount = 1;
 9526:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 9527:         %confhash);
 9528:     my $switchserver = &check_switchserver($dom,$confname);
 9529:     my %lt = &Apache::lonlocal::texthash (
 9530:                 default => 'Default bubblesheet format file error',
 9531:                 custom  => 'Custom bubblesheet format file error',
 9532:              );
 9533:     my %scantronfiles = (
 9534:         default => 'default.tab',
 9535:         custom => 'custom.tab',
 9536:     );
 9537:     foreach my $key (keys(%scantronfiles)) {
 9538:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 9539:                               .$scantronfiles{$key};
 9540:     }
 9541:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 9542:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 9543:         if (!$switchserver) {
 9544:             my $servadm = $r->dir_config('lonAdmEMail');
 9545:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 9546:             if ($configuserok eq 'ok') {
 9547:                 if ($author_ok eq 'ok') {
 9548:                     my %legacyfile = (
 9549:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
 9550:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
 9551:                     );
 9552:                     my %md5chk;
 9553:                     foreach my $type (keys(%legacyfile)) {
 9554:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 9555:                         chomp($md5chk{$type});
 9556:                     }
 9557:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 9558:                         foreach my $type (keys(%legacyfile)) {
 9559:                             ($scantronurls{$type},my $error) =
 9560:                                 &legacy_scantronformat($r,$dom,$confname,
 9561:                                                  $type,$legacyfile{$type},
 9562:                                                  $scantronurls{$type},
 9563:                                                  $scantronfiles{$type});
 9564:                             if ($error ne '') {
 9565:                                 $error{$type} = $error;
 9566:                             }
 9567:                         }
 9568:                         if (keys(%error) == 0) {
 9569:                             $is_custom = 1;
 9570:                             $confhash{'scantron'}{'scantronformat'} =
 9571:                                 $scantronurls{'custom'};
 9572:                             my $putresult =
 9573:                                 &Apache::lonnet::put_dom('configuration',
 9574:                                                          \%confhash,$dom);
 9575:                             if ($putresult ne 'ok') {
 9576:                                 $error{'custom'} =
 9577:                                     '<span class="LC_error">'.
 9578:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 9579:                             }
 9580:                         }
 9581:                     } else {
 9582:                         ($scantronurls{'default'},my $error) =
 9583:                             &legacy_scantronformat($r,$dom,$confname,
 9584:                                           'default',$legacyfile{'default'},
 9585:                                           $scantronurls{'default'},
 9586:                                           $scantronfiles{'default'});
 9587:                         if ($error eq '') {
 9588:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 9589:                             my $putresult =
 9590:                                 &Apache::lonnet::put_dom('configuration',
 9591:                                                          \%confhash,$dom);
 9592:                             if ($putresult ne 'ok') {
 9593:                                 $error{'default'} =
 9594:                                     '<span class="LC_error">'.
 9595:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 9596:                             }
 9597:                         } else {
 9598:                             $error{'default'} = $error;
 9599:                         }
 9600:                     }
 9601:                 }
 9602:             }
 9603:         } else {
 9604:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 9605:         }
 9606:     }
 9607:     if (ref($settings) eq 'HASH') {
 9608:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 9609:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 9610:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 9611:                 $scantronurl = '';
 9612:             } else {
 9613:                 $scantronurl = $settings->{'scantronformat'};
 9614:             }
 9615:             $is_custom = 1;
 9616:         } else {
 9617:             $scantronurl = $scantronurls{'default'};
 9618:         }
 9619:     } else {
 9620:         if ($is_custom) {
 9621:             $scantronurl = $scantronurls{'custom'};
 9622:         } else {
 9623:             $scantronurl = $scantronurls{'default'};
 9624:         }
 9625:     }
 9626:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9627:     $datatable .= '<tr'.$css_class.'>';
 9628:     if (!$is_custom) {
 9629:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 9630:                       '<span class="LC_nobreak">';
 9631:         if ($scantronurl) {
 9632:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 9633:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 9634:         } else {
 9635:             $datatable = &mt('File unavailable for display');
 9636:         }
 9637:         $datatable .= '</span></td>';
 9638:         if (keys(%error) == 0) { 
 9639:             $datatable .= '<td style="vertical-align: bottom">';
 9640:             if (!$switchserver) {
 9641:                 $datatable .= &mt('Upload:').'<br />';
 9642:             }
 9643:         } else {
 9644:             my $errorstr;
 9645:             foreach my $key (sort(keys(%error))) {
 9646:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 9647:             }
 9648:             $datatable .= '<td>'.$errorstr;
 9649:         }
 9650:     } else {
 9651:         if (keys(%error) > 0) {
 9652:             my $errorstr;
 9653:             foreach my $key (sort(keys(%error))) {
 9654:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 9655:             } 
 9656:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 9657:         } elsif ($scantronurl) {
 9658:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 9659:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 9660:             $datatable .= '<td><span class="LC_nobreak">'.
 9661:                           $link.
 9662:                           '<label><input type="checkbox" name="scantronformat_del"'.
 9663:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 9664:                           '<td><span class="LC_nobreak">&nbsp;'.
 9665:                           &mt('Replace:').'</span><br />';
 9666:         }
 9667:     }
 9668:     if (keys(%error) == 0) {
 9669:         if ($switchserver) {
 9670:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 9671:         } else {
 9672:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 9673:                          '<input type="file" name="scantronformat" /></span>';
 9674:         }
 9675:     }
 9676:     $datatable .= '</td></tr>';
 9677:     $$rowtotal ++;
 9678:     return $datatable;
 9679: }
 9680: 
 9681: sub legacy_scantronformat {
 9682:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 9683:     my ($url,$error);
 9684:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 9685:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 9686:         (my $result,$url) =
 9687:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 9688:                          '','',$newfile);
 9689:         if ($result ne 'ok') {
 9690:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 9691:         }
 9692:     }
 9693:     return ($url,$error);
 9694: }
 9695: 
 9696: sub print_scantronconfig {
 9697:     my ($dom,$settings,$rowtotal) = @_;
 9698:     my $itemcount = 2;
 9699:     my $is_checked = ' checked="checked"';
 9700:     my %optionson = (
 9701:                      hdr => ' checked="checked"',
 9702:                      pad => ' checked="checked"',
 9703:                      rem => ' checked="checked"',
 9704:                     );
 9705:     my %optionsoff = (
 9706:                       hdr => '',
 9707:                       pad => '',
 9708:                       rem => '',
 9709:                      );
 9710:     my $currcsvsty = 'none';
 9711:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
 9712:     my @fields = &scantroncsv_fields();
 9713:     my %titles = &scantronconfig_titles();
 9714:     if (ref($settings) eq 'HASH') {
 9715:         if (ref($settings->{config}) eq 'HASH') {
 9716:             if ($settings->{config}->{dat}) {
 9717:                 $checked{'dat'} = $is_checked;
 9718:             }
 9719:             if (ref($settings->{config}->{csv}) eq 'HASH') {
 9720:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
 9721:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
 9722:                     if (keys(%csvfields) > 0) {
 9723:                         $checked{'csv'} = $is_checked;
 9724:                         $currcsvsty = 'block';
 9725:                     }
 9726:                 }
 9727:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
 9728:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
 9729:                     foreach my $option (keys(%optionson)) {
 9730:                         unless ($csvoptions{$option}) {
 9731:                             $optionsoff{$option} = $optionson{$option};
 9732:                             $optionson{$option} = '';
 9733:                         }
 9734:                     }
 9735:                 }
 9736:             }
 9737:         } else {
 9738:             $checked{'dat'} = $is_checked;
 9739:         }
 9740:     } else {
 9741:         $checked{'dat'} = $is_checked;
 9742:     }
 9743:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
 9744:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 9745:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
 9746:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
 9747:     foreach my $item ('dat','csv') {
 9748:         my $id;
 9749:         if ($item eq 'csv') {
 9750:             $id = 'id="scantronconfcsv" ';
 9751:         }
 9752:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
 9753:                       $titles{$item}.'</label>'.('&nbsp;'x3);
 9754:         if ($item eq 'csv') {
 9755:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
 9756:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
 9757:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
 9758:             foreach my $col (@fields) {
 9759:                 my $selnone;
 9760:                 if ($csvfields{$col} eq '') {
 9761:                     $selnone = ' selected="selected"';
 9762:                 }
 9763:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
 9764:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
 9765:                               '<option value=""'.$selnone.'></option>';
 9766:                 for (my $i=0; $i<20; $i++) {
 9767:                     my $shown = $i+1;
 9768:                     my $sel;
 9769:                     unless ($selnone) {
 9770:                         if (exists($csvfields{$col})) {
 9771:                             if ($csvfields{$col} == $i) {
 9772:                                 $sel = ' selected="selected"';
 9773:                             }
 9774:                         }
 9775:                     }
 9776:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
 9777:                 }
 9778:                 $datatable .= '</select></td></tr>';
 9779:            }
 9780:            $datatable .= '</table></fieldset>'.
 9781:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
 9782:                          '<legend>'.&mt('CSV Options').'</legend>';
 9783:            foreach my $option ('hdr','pad','rem') {
 9784:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
 9785:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
 9786:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
 9787:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
 9788:            }
 9789:            $datatable .= '</fieldset>';
 9790:            $itemcount ++;
 9791:         }
 9792:     }
 9793:     $datatable .= '</td></tr>';
 9794:     $$rowtotal ++;
 9795:     return $datatable;
 9796: }
 9797: 
 9798: sub scantronconfig_titles {
 9799:     return &Apache::lonlocal::texthash(
 9800:                                           dat => 'Standard format (.dat)',
 9801:                                           csv => 'Comma separated values (.csv)',
 9802:                                           hdr => 'Remove first line in file (contains column titles)',
 9803:                                           pad => 'Prepend 0s to PaperID',
 9804:                                           rem => 'Remove leading spaces (except Question Response columns)',
 9805:                                           CODE => 'CODE',
 9806:                                           ID   => 'Student ID',
 9807:                                           PaperID => 'Paper ID',
 9808:                                           FirstName => 'First Name',
 9809:                                           LastName => 'Last Name',
 9810:                                           FirstQuestion => 'First Question Response',
 9811:                                           Section => 'Section',
 9812:     );
 9813: }
 9814: 
 9815: sub scantroncsv_fields {
 9816:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
 9817: }
 9818: 
 9819: sub print_coursecategories {
 9820:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 9821:     my $datatable;
 9822:     if ($position eq 'top') {
 9823:         my (%checked);
 9824:         my @catitems = ('unauth','auth');
 9825:         my @cattypes = ('std','domonly','codesrch','none');
 9826:         $checked{'unauth'} = 'std';
 9827:         $checked{'auth'} = 'std';
 9828:         if (ref($settings) eq 'HASH') {
 9829:             foreach my $type (@cattypes) {
 9830:                 if ($type eq $settings->{'unauth'}) {
 9831:                     $checked{'unauth'} = $type;
 9832:                 }
 9833:                 if ($type eq $settings->{'auth'}) {
 9834:                     $checked{'auth'} = $type;
 9835:                 }
 9836:             }
 9837:         }
 9838:         my %lt = &Apache::lonlocal::texthash (
 9839:                                                unauth   => 'Catalog type for unauthenticated users',
 9840:                                                auth     => 'Catalog type for authenticated users',
 9841:                                                none     => 'No catalog',
 9842:                                                std      => 'Standard catalog',
 9843:                                                domonly  => 'Domain-only catalog',
 9844:                                                codesrch => "Code search form",
 9845:                                              );
 9846:        my $itemcount = 0;
 9847:        foreach my $item (@catitems) {
 9848:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 9849:            $datatable .= '<tr '.$css_class.'>'.
 9850:                          '<td>'.$lt{$item}.'</td>'.
 9851:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 9852:            foreach my $type (@cattypes) {
 9853:                my $ischecked;
 9854:                if ($checked{$item} eq $type) {
 9855:                    $ischecked=' checked="checked"';
 9856:                }
 9857:                $datatable .= '<label>'.
 9858:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 9859:                              ' />'.$lt{$type}.'</label>&nbsp;';
 9860:            }
 9861:            $datatable .= '</span></td></tr>';
 9862:            $itemcount ++;
 9863:         }
 9864:         $$rowtotal += $itemcount;
 9865:     } elsif ($position eq 'middle') {
 9866:         my $toggle_cats_crs = ' ';
 9867:         my $toggle_cats_dom = ' checked="checked" ';
 9868:         my $can_cat_crs = ' ';
 9869:         my $can_cat_dom = ' checked="checked" ';
 9870:         my $toggle_catscomm_comm = ' ';
 9871:         my $toggle_catscomm_dom = ' checked="checked" ';
 9872:         my $can_catcomm_comm = ' ';
 9873:         my $can_catcomm_dom = ' checked="checked" ';
 9874:         my $toggle_catsplace_place = ' ';
 9875:         my $toggle_catsplace_dom = ' checked="checked" ';
 9876:         my $can_catplace_place = ' ';
 9877:         my $can_catplace_dom = ' checked="checked" ';
 9878: 
 9879:         if (ref($settings) eq 'HASH') {
 9880:             if ($settings->{'togglecats'} eq 'crs') {
 9881:                 $toggle_cats_crs = $toggle_cats_dom;
 9882:                 $toggle_cats_dom = ' ';
 9883:             }
 9884:             if ($settings->{'categorize'} eq 'crs') {
 9885:                 $can_cat_crs = $can_cat_dom;
 9886:                 $can_cat_dom = ' ';
 9887:             }
 9888:             if ($settings->{'togglecatscomm'} eq 'comm') {
 9889:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 9890:                 $toggle_catscomm_dom = ' ';
 9891:             }
 9892:             if ($settings->{'categorizecomm'} eq 'comm') {
 9893:                 $can_catcomm_comm = $can_catcomm_dom;
 9894:                 $can_catcomm_dom = ' ';
 9895:             }
 9896:             if ($settings->{'togglecatsplace'} eq 'place') {
 9897:                 $toggle_catsplace_place = $toggle_catsplace_dom;
 9898:                 $toggle_catsplace_dom = ' ';
 9899:             }
 9900:             if ($settings->{'categorizeplace'} eq 'place') {
 9901:                 $can_catplace_place = $can_catplace_dom;
 9902:                 $can_catplace_dom = ' ';
 9903:             }
 9904:         }
 9905:         my %title = &Apache::lonlocal::texthash (
 9906:                      togglecats      => 'Show/Hide a course in catalog',
 9907:                      togglecatscomm  => 'Show/Hide a community in catalog',
 9908:                      togglecatsplace => 'Show/Hide a placement test in catalog',
 9909:                      categorize      => 'Assign a category to a course',
 9910:                      categorizecomm  => 'Assign a category to a community',
 9911:                      categorizeplace => 'Assign a category to a placement test',
 9912:                     );
 9913:         my %level = &Apache::lonlocal::texthash (
 9914:                      dom   => 'Set in Domain',
 9915:                      crs   => 'Set in Course',
 9916:                      comm  => 'Set in Community',
 9917:                      place => 'Set in Placement Test',
 9918:                     );
 9919:         $datatable = '<tr class="LC_odd_row">'.
 9920:                   '<td>'.$title{'togglecats'}.'</td>'.
 9921:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 9922:                   '<input type="radio" name="togglecats"'.
 9923:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 9924:                   '<label><input type="radio" name="togglecats"'.
 9925:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 9926:                   '</tr><tr>'.
 9927:                   '<td>'.$title{'categorize'}.'</td>'.
 9928:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 9929:                   '<label><input type="radio" name="categorize"'.
 9930:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 9931:                   '<label><input type="radio" name="categorize"'.
 9932:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 9933:                   '</tr><tr class="LC_odd_row">'.
 9934:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 9935:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 9936:                   '<input type="radio" name="togglecatscomm"'.
 9937:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 9938:                   '<label><input type="radio" name="togglecatscomm"'.
 9939:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 9940:                   '</tr><tr>'.
 9941:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 9942:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 9943:                   '<label><input type="radio" name="categorizecomm"'.
 9944:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 9945:                   '<label><input type="radio" name="categorizecomm"'.
 9946:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 9947:                   '</tr><tr class="LC_odd_row">'.
 9948:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
 9949:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 9950:                   '<input type="radio" name="togglecatsplace"'.
 9951:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 9952:                   '<label><input type="radio" name="togglecatscomm"'.
 9953:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
 9954:                   '</tr><tr>'.
 9955:                   '<td>'.$title{'categorizeplace'}.'</td>'.
 9956:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 9957:                   '<label><input type="radio" name="categorizeplace"'.
 9958:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 9959:                   '<label><input type="radio" name="categorizeplace"'.
 9960:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
 9961:                   '</tr>';
 9962:         $$rowtotal += 6;
 9963:     } else {
 9964:         my $css_class;
 9965:         my $itemcount = 1;
 9966:         my $cathash; 
 9967:         if (ref($settings) eq 'HASH') {
 9968:             $cathash = $settings->{'cats'};
 9969:         }
 9970:         if (ref($cathash) eq 'HASH') {
 9971:             my (@cats,@trails,%allitems,%idx,@jsarray);
 9972:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 9973:                                                    \%allitems,\%idx,\@jsarray);
 9974:             my $maxdepth = scalar(@cats);
 9975:             my $colattrib = '';
 9976:             if ($maxdepth > 2) {
 9977:                 $colattrib = ' colspan="2" ';
 9978:             }
 9979:             my @path;
 9980:             if (@cats > 0) {
 9981:                 if (ref($cats[0]) eq 'ARRAY') {
 9982:                     my $numtop = @{$cats[0]};
 9983:                     my $maxnum = $numtop;
 9984:                     my %default_names = (
 9985:                           instcode    => &mt('Official courses'),
 9986:                           communities => &mt('Communities'),
 9987:                           placement   => &mt('Placement Tests'),
 9988:                     );
 9989: 
 9990:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 9991:                         ($cathash->{'instcode::0'} eq '') ||
 9992:                         (!grep(/^communities$/,@{$cats[0]})) || 
 9993:                         ($cathash->{'communities::0'} eq '') ||
 9994:                         (!grep(/^placement$/,@{$cats[0]})) ||
 9995:                         ($cathash->{'placement::0'} eq '')) {
 9996:                         $maxnum ++;
 9997:                     }
 9998:                     my $lastidx;
 9999:                     for (my $i=0; $i<$numtop; $i++) {
10000:                         my $parent = $cats[0][$i];
10001:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
10002:                         my $item = &escape($parent).'::0';
10003:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
10004:                         $lastidx = $idx{$item};
10005:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
10006:                                       .'<select name="'.$item.'"'.$chgstr.'>';
10007:                         for (my $k=0; $k<=$maxnum; $k++) {
10008:                             my $vpos = $k+1;
10009:                             my $selstr;
10010:                             if ($k == $i) {
10011:                                 $selstr = ' selected="selected" ';
10012:                             }
10013:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10014:                         }
10015:                         $datatable .= '</select></span></td><td>';
10016:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
10017:                             $datatable .=  '<span class="LC_nobreak">'
10018:                                            .$default_names{$parent}.'</span>';
10019:                             if ($parent eq 'instcode') {
10020:                                 $datatable .= '<br /><span class="LC_nobreak">('
10021:                                               .&mt('with institutional codes')
10022:                                               .')</span></td><td'.$colattrib.'>';
10023:                             } else {
10024:                                 $datatable .= '<table><tr><td>';
10025:                             }
10026:                             $datatable .= '<span class="LC_nobreak">'
10027:                                           .'<label><input type="radio" name="'
10028:                                           .$parent.'" value="1" checked="checked" />'
10029:                                           .&mt('Display').'</label>';
10030:                             if ($parent eq 'instcode') {
10031:                                 $datatable .= '&nbsp;';
10032:                             } else {
10033:                                 $datatable .= '</span></td></tr><tr><td>'
10034:                                               .'<span class="LC_nobreak">';
10035:                             }
10036:                             $datatable .= '<label><input type="radio" name="'
10037:                                           .$parent.'" value="0" />'
10038:                                           .&mt('Do not display').'</label></span>';
10039:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
10040:                                 $datatable .= '</td></tr></table>';
10041:                             }
10042:                             $datatable .= '</td>';
10043:                         } else {
10044:                             $datatable .= $parent
10045:                                           .'&nbsp;<span class="LC_nobreak"><label>'
10046:                                           .'<input type="checkbox" name="deletecategory" '
10047:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
10048:                         }
10049:                         my $depth = 1;
10050:                         push(@path,$parent);
10051:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
10052:                         pop(@path);
10053:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
10054:                         $itemcount ++;
10055:                     }
10056:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
10057:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
10058:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
10059:                     for (my $k=0; $k<=$maxnum; $k++) {
10060:                         my $vpos = $k+1;
10061:                         my $selstr;
10062:                         if ($k == $numtop) {
10063:                             $selstr = ' selected="selected" ';
10064:                         }
10065:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10066:                     }
10067:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
10068:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
10069:                                   .'</tr>'."\n";
10070:                     $itemcount ++;
10071:                     foreach my $default ('instcode','communities','placement') {
10072:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
10073:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
10074:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
10075:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
10076:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
10077:                             for (my $k=0; $k<=$maxnum; $k++) {
10078:                                 my $vpos = $k+1;
10079:                                 my $selstr;
10080:                                 if ($k == $maxnum) {
10081:                                     $selstr = ' selected="selected" ';
10082:                                 }
10083:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10084:                             }
10085:                             $datatable .= '</select></span></td>'.
10086:                                           '<td><span class="LC_nobreak">'.
10087:                                           $default_names{$default}.'</span>';
10088:                             if ($default eq 'instcode') {
10089:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
10090:                                               .&mt('with institutional codes').')</span>';
10091:                             }
10092:                             $datatable .= '</td>'
10093:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
10094:                                           .&mt('Display').'</label>&nbsp;'
10095:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
10096:                                           .&mt('Do not display').'</label></span></td></tr>';
10097:                         }
10098:                     }
10099:                 }
10100:             } else {
10101:                 $datatable .= &initialize_categories($itemcount);
10102:             }
10103:         } else {
10104:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
10105:                           .&initialize_categories($itemcount);
10106:         }
10107:         $$rowtotal += $itemcount;
10108:     }
10109:     return $datatable;
10110: }
10111: 
10112: sub print_serverstatuses {
10113:     my ($dom,$settings,$rowtotal) = @_;
10114:     my $datatable;
10115:     my @pages = &serverstatus_pages();
10116:     my (%namedaccess,%machineaccess);
10117:     foreach my $type (@pages) {
10118:         $namedaccess{$type} = '';
10119:         $machineaccess{$type}= '';
10120:     }
10121:     if (ref($settings) eq 'HASH') {
10122:         foreach my $type (@pages) {
10123:             if (exists($settings->{$type})) {
10124:                 if (ref($settings->{$type}) eq 'HASH') {
10125:                     foreach my $key (keys(%{$settings->{$type}})) {
10126:                         if ($key eq 'namedusers') {
10127:                             $namedaccess{$type} = $settings->{$type}->{$key};
10128:                         } elsif ($key eq 'machines') {
10129:                             $machineaccess{$type} = $settings->{$type}->{$key};
10130:                         }
10131:                     }
10132:                 }
10133:             }
10134:         }
10135:     }
10136:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
10137:     my $rownum = 0;
10138:     my $css_class;
10139:     foreach my $type (@pages) {
10140:         $rownum ++;
10141:         $css_class = $rownum%2?' class="LC_odd_row"':'';
10142:         $datatable .= '<tr'.$css_class.'>'.
10143:                       '<td><span class="LC_nobreak">'.
10144:                       $titles->{$type}.'</span></td>'.
10145:                       '<td class="LC_left_item">'.
10146:                       '<input type="text" name="'.$type.'_namedusers" '.
10147:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
10148:                       '<td class="LC_right_item">'.
10149:                       '<span class="LC_nobreak">'.
10150:                       '<input type="text" name="'.$type.'_machines" '.
10151:                       'value="'.$machineaccess{$type}.'" size="10" />'.
10152:                       '</span></td></tr>'."\n";
10153:     }
10154:     $$rowtotal += $rownum;
10155:     return $datatable;
10156: }
10157: 
10158: sub serverstatus_pages {
10159:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
10160:             'checksums','clusterstatus','certstatus','metadata_keywords',
10161:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
10162:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
10163: }
10164: 
10165: sub defaults_javascript {
10166:     my ($settings) = @_;
10167:     return unless (ref($settings) eq 'HASH');
10168:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
10169:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
10170:         if ($maxnum eq '') {
10171:             $maxnum = 0;
10172:         }
10173:         $maxnum ++;
10174:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
10175:         return <<"ENDSCRIPT";
10176: <script type="text/javascript">
10177: // <![CDATA[
10178: function reorderTypes(form,caller) {
10179:     var changedVal;
10180: $jstext 
10181:     var newpos = 'addinststatus_pos';
10182:     var current = new Array;
10183:     var maxh = $maxnum;
10184:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
10185:     var oldVal;
10186:     if (caller == newpos) {
10187:         changedVal = newitemVal;
10188:     } else {
10189:         var curritem = 'inststatus_pos_'+caller;
10190:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
10191:         current[newitemVal] = newpos;
10192:     }
10193:     for (var i=0; i<inststatuses.length; i++) {
10194:         if (inststatuses[i] != caller) {
10195:             var elementName = 'inststatus_pos_'+inststatuses[i];
10196:             if (form.elements[elementName]) {
10197:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
10198:                 current[currVal] = elementName;
10199:             }
10200:         }
10201:     }
10202:     for (var j=0; j<maxh; j++) {
10203:         if (current[j] == undefined) {
10204:             oldVal = j;
10205:         }
10206:     }
10207:     if (oldVal < changedVal) {
10208:         for (var k=oldVal+1; k<=changedVal ; k++) {
10209:            var elementName = current[k];
10210:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
10211:         }
10212:     } else {
10213:         for (var k=changedVal; k<oldVal; k++) {
10214:             var elementName = current[k];
10215:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
10216:         }
10217:     }
10218:     return;
10219: }
10220: 
10221: // ]]>
10222: </script>
10223: 
10224: ENDSCRIPT
10225:     }
10226:     return;
10227: }
10228: 
10229: sub passwords_javascript {
10230:     my %intalert = &Apache::lonlocal::texthash (
10231:         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.',
10232:         authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
10233:         passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
10234:         passmax => 'Warning: maximum password length must be a positive integer (or blank).',
10235:         passexp => 'Warning: days before password expiration must be a positive integer (or blank).',
10236:         passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
10237:     );
10238:     &js_escape(\%intalert);
10239:     my $defmin = $Apache::lonnet::passwdmin;
10240:     my $intauthjs = <<"ENDSCRIPT";
10241: 
10242: function warnIntAuth(field) {
10243:     if (field.name == 'intauth_check') {
10244:         if (field.value == '2') {
10245:             alert('$intalert{authcheck}');
10246:         }
10247:     }
10248:     if (field.name == 'intauth_cost') {
10249:         field.value.replace(/\s/g,'');
10250:         if (field.value != '') {
10251:             var regexdigit=/^\\d+\$/;
10252:             if (!regexdigit.test(field.value)) {
10253:                 alert('$intalert{authcost}');
10254:             }
10255:         }
10256:     }
10257:     return;
10258: }
10259: 
10260: function warnIntPass(field) {
10261:     field.value.replace(/^\s+/,'');
10262:     field.value.replace(/\s+\$/,'');
10263:     var regexdigit=/^\\d+\$/;
10264:     if (field.name == 'passwords_min') {
10265:         if (field.value == '') {
10266:             alert('$intalert{passmin}');
10267:             field.value = '$defmin';
10268:         } else {
10269:             if (!regexdigit.test(field.value)) {
10270:                 alert('$intalert{passmin}');
10271:                 field.value = '$defmin';
10272:             }
10273:             var minval = parseInt(field.value,10);
10274:             if (minval < $defmin) {
10275:                 alert('$intalert{passmin}');
10276:                 field.value = '$defmin';
10277:             }
10278:         }
10279:     } else {
10280:         if (field.value == '0') {
10281:             field.value = '';
10282:         }
10283:         if (field.value != '') {
10284:             if (field.name == 'passwords_expire') {
10285:                 var regexpposnum=/^\\d+(|\\.\\d*)\$/; 
10286:                 if (!regexpposnum.test(field.value)) {
10287:                     alert('$intalert{passexp}');
10288:                     field.value = '';
10289:                 } else {
10290:                     var expval = parseFloat(field.value);
10291:                     if (expval == 0) {
10292:                         alert('$intalert{passexp}');
10293:                         field.value = '';
10294:                     }
10295:                 }
10296:             } else {
10297:                 if (!regexdigit.test(field.value)) {
10298:                     if (field.name == 'passwords_max') {
10299:                         alert('$intalert{passmax}');
10300:                     } else {
10301:                         if (field.name == 'passwords_numsaved') {
10302:                             alert('$intalert{passnum}');
10303:                         }
10304:                     }
10305:                     field.value = '';
10306:                 }
10307:             }
10308:         }
10309:     }
10310:     return;
10311: }
10312: 
10313: ENDSCRIPT
10314:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
10315: }
10316: 
10317: sub coursecategories_javascript {
10318:     my ($settings) = @_;
10319:     my ($output,$jstext,$cathash);
10320:     if (ref($settings) eq 'HASH') {
10321:         $cathash = $settings->{'cats'};
10322:     }
10323:     if (ref($cathash) eq 'HASH') {
10324:         my (@cats,@jsarray,%idx);
10325:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
10326:         if (@jsarray > 0) {
10327:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
10328:             for (my $i=0; $i<@jsarray; $i++) {
10329:                 if (ref($jsarray[$i]) eq 'ARRAY') {
10330:                     my $catstr = join('","',@{$jsarray[$i]});
10331:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
10332:                 }
10333:             }
10334:         }
10335:     } else {
10336:         $jstext  = '    var categories = Array(1);'."\n".
10337:                    '    categories[0] = Array("instcode_pos");'."\n"; 
10338:     }
10339:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
10340:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
10341:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
10342:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
10343:     &js_escape(\$instcode_reserved);
10344:     &js_escape(\$communities_reserved);
10345:     &js_escape(\$placement_reserved);
10346:     &js_escape(\$choose_again);
10347:     $output = <<"ENDSCRIPT";
10348: <script type="text/javascript">
10349: // <![CDATA[
10350: function reorderCats(form,parent,item,idx) {
10351:     var changedVal;
10352: $jstext
10353:     var newpos = 'addcategory_pos';
10354:     if (parent == '') {
10355:         var has_instcode = 0;
10356:         var maxtop = categories[idx].length;
10357:         for (var j=0; j<maxtop; j++) {
10358:             if (categories[idx][j] == 'instcode::0') {
10359:                 has_instcode == 1;
10360:             }
10361:         }
10362:         if (has_instcode == 0) {
10363:             categories[idx][maxtop] = 'instcode_pos';
10364:         }
10365:     } else {
10366:         newpos += '_'+parent;
10367:     }
10368:     var maxh = 1 + categories[idx].length;
10369:     var current = new Array;
10370:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
10371:     if (item == newpos) {
10372:         changedVal = newitemVal;
10373:     } else {
10374:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
10375:         current[newitemVal] = newpos;
10376:     }
10377:     for (var i=0; i<categories[idx].length; i++) {
10378:         var elementName = categories[idx][i];
10379:         if (elementName != item) {
10380:             if (form.elements[elementName]) {
10381:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
10382:                 current[currVal] = elementName;
10383:             }
10384:         }
10385:     }
10386:     var oldVal;
10387:     for (var j=0; j<maxh; j++) {
10388:         if (current[j] == undefined) {
10389:             oldVal = j;
10390:         }
10391:     }
10392:     if (oldVal < changedVal) {
10393:         for (var k=oldVal+1; k<=changedVal ; k++) {
10394:            var elementName = current[k];
10395:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
10396:         }
10397:     } else {
10398:         for (var k=changedVal; k<oldVal; k++) {
10399:             var elementName = current[k];
10400:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
10401:         }
10402:     }
10403:     return;
10404: }
10405: 
10406: function categoryCheck(form) {
10407:     if (form.elements['addcategory_name'].value == 'instcode') {
10408:         alert('$instcode_reserved\\n$choose_again');
10409:         return false;
10410:     }
10411:     if (form.elements['addcategory_name'].value == 'communities') {
10412:         alert('$communities_reserved\\n$choose_again');
10413:         return false;
10414:     }
10415:     if (form.elements['addcategory_name'].value == 'placement') {
10416:         alert('$placement_reserved\\n$choose_again');
10417:         return false;
10418:     }
10419:     return true;
10420: }
10421: 
10422: // ]]>
10423: </script>
10424: 
10425: ENDSCRIPT
10426:     return $output;
10427: }
10428: 
10429: sub initialize_categories {
10430:     my ($itemcount) = @_;
10431:     my ($datatable,$css_class,$chgstr);
10432:     my %default_names = (
10433:                       instcode    => 'Official courses (with institutional codes)',
10434:                       communities => 'Communities',
10435:                       placement   => 'Placement Tests',
10436:                         );
10437:     my %selnum = (
10438:                    instcode    => '0',
10439:                    communities => '1',
10440:                    placement   => '2',
10441:                  );
10442:     my %selected;
10443:     foreach my $default ('instcode','communities','placement') {
10444:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
10445:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
10446:         map { $selected{$selnum{$_}} = '' } keys(%selnum);
10447:         $selected{$selnum{$default}} = ' selected="selected"';
10448:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
10449:                      .'<select name="'.$default.'_pos"'.$chgstr.'>'
10450:                      .'<option value="0"'.$selected{'0'}.'>1</option>'
10451:                      .'<option value="1"'.$selected{'1'}.'>2</option>'
10452:                      .'<option value="2"'.$selected{'2'}.'>3</option>'
10453:                      .'<option value="3">4</option></select>&nbsp;'
10454:                      .$default_names{$default}
10455:                      .'</span></td><td><span class="LC_nobreak">'
10456:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
10457:                      .&mt('Display').'</label>&nbsp;<label>'
10458:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
10459:                  .'</label></span></td></tr>';
10460:         $itemcount ++;
10461:     }
10462:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
10463:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
10464:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
10465:                   .'<select name="addcategory_pos"'.$chgstr.'>'
10466:                   .'<option value="0">1</option>'
10467:                   .'<option value="1">2</option>'
10468:                   .'<option value="2">3</option>'
10469:                   .'<option value="3" selected="selected">4</option></select>&nbsp;'
10470:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
10471:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
10472:                   .'</td></tr>';
10473:     return $datatable;
10474: }
10475: 
10476: sub build_category_rows {
10477:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
10478:     my ($text,$name,$item,$chgstr);
10479:     if (ref($cats) eq 'ARRAY') {
10480:         my $maxdepth = scalar(@{$cats});
10481:         if (ref($cats->[$depth]) eq 'HASH') {
10482:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
10483:                 my $numchildren = @{$cats->[$depth]{$parent}};
10484:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
10485:                 $text .= '<td><table class="LC_data_table">';
10486:                 my ($idxnum,$parent_name,$parent_item);
10487:                 my $higher = $depth - 1;
10488:                 if ($higher == 0) {
10489:                     $parent_name = &escape($parent).'::'.$higher;
10490:                 } else {
10491:                     if (ref($path) eq 'ARRAY') {
10492:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
10493:                     }
10494:                 }
10495:                 $parent_item = 'addcategory_pos_'.$parent_name;
10496:                 for (my $j=0; $j<=$numchildren; $j++) {
10497:                     if ($j < $numchildren) {
10498:                         $name = $cats->[$depth]{$parent}[$j];
10499:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
10500:                         $idxnum = $idx->{$item};
10501:                     } else {
10502:                         $name = $parent_name;
10503:                         $item = $parent_item;
10504:                     }
10505:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
10506:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
10507:                     for (my $i=0; $i<=$numchildren; $i++) {
10508:                         my $vpos = $i+1;
10509:                         my $selstr;
10510:                         if ($j == $i) {
10511:                             $selstr = ' selected="selected" ';
10512:                         }
10513:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
10514:                     }
10515:                     $text .= '</select>&nbsp;';
10516:                     if ($j < $numchildren) {
10517:                         my $deeper = $depth+1;
10518:                         $text .= $name.'&nbsp;'
10519:                                  .'<label><input type="checkbox" name="deletecategory" value="'
10520:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
10521:                         if(ref($path) eq 'ARRAY') {
10522:                             push(@{$path},$name);
10523:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
10524:                             pop(@{$path});
10525:                         }
10526:                     } else {
10527:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
10528:                         if ($j == $numchildren) {
10529:                             $text .= $name;
10530:                         } else {
10531:                             $text .= $item;
10532:                         }
10533:                         $text .= '" value="" />';
10534:                     }
10535:                     $text .= '</td></tr>';
10536:                 }
10537:                 $text .= '</table></td>';
10538:             } else {
10539:                 my $higher = $depth-1;
10540:                 if ($higher == 0) {
10541:                     $name = &escape($parent).'::'.$higher;
10542:                 } else {
10543:                     if (ref($path) eq 'ARRAY') {
10544:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
10545:                     }
10546:                 }
10547:                 my $colspan;
10548:                 if ($parent ne 'instcode') {
10549:                     $colspan = $maxdepth - $depth - 1;
10550:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
10551:                 }
10552:             }
10553:         }
10554:     }
10555:     return $text;
10556: }
10557: 
10558: sub modifiable_userdata_row {
10559:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
10560:         $rowid,$customcss,$rowstyle,$itemdesc) = @_;
10561:     my ($role,$rolename,$statustype);
10562:     $role = $item;
10563:     if ($context eq 'cancreate') {
10564:         if ($item =~ /^(emailusername)_(.+)$/) {
10565:             $role = $1;
10566:             $statustype = $2;
10567:             if (ref($usertypes) eq 'HASH') {
10568:                 if ($usertypes->{$statustype}) {
10569:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
10570:                 } else {
10571:                     $rolename = &mt('Data provided by user');
10572:                 }
10573:             }
10574:         }
10575:     } elsif ($context eq 'selfcreate') {
10576:         if (ref($usertypes) eq 'HASH') {
10577:             $rolename = $usertypes->{$role};
10578:         } else {
10579:             $rolename = $role;
10580:         }
10581:     } elsif ($context eq 'lti') {
10582:         $rolename = &mt('Institutional data used (if available)');
10583:     } elsif ($context eq 'privacy') {
10584:         $rolename = $itemdesc;
10585:     } else {
10586:         if ($role eq 'cr') {
10587:             $rolename = &mt('Custom role');
10588:         } else {
10589:             $rolename = &Apache::lonnet::plaintext($role);
10590:         }
10591:     }
10592:     my (@fields,%fieldtitles);
10593:     if (ref($fieldsref) eq 'ARRAY') {
10594:         @fields = @{$fieldsref};
10595:     } else {
10596:         @fields = ('lastname','firstname','middlename','generation',
10597:                    'permanentemail','id');
10598:     }
10599:     if ((ref($titlesref) eq 'HASH')) {
10600:         %fieldtitles = %{$titlesref};
10601:     } else {
10602:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
10603:     }
10604:     my $output;
10605:     my $css_class;
10606:     if ($rowcount%2) {
10607:         $css_class = 'LC_odd_row';
10608:     }
10609:     if ($customcss) {
10610:         $css_class .= " $customcss";
10611:     }
10612:     $css_class =~ s/^\s+//;
10613:     if ($css_class) {
10614:         $css_class = ' class="'.$css_class.'"';
10615:     }
10616:     if ($rowstyle) {
10617:         $css_class .= ' style="'.$rowstyle.'"';
10618:     }
10619:     if ($rowid) {
10620:         $rowid = ' id="'.$rowid.'"';
10621:     }
10622:     $output = '<tr '.$css_class.$rowid.'>'.
10623:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
10624:               '<td class="LC_left_item" colspan="2"><table>';
10625:     my $rem;
10626:     my %checks;
10627:     my %current;
10628:     if (ref($settings) eq 'HASH') {
10629:         my $hashref;
10630:         if ($context eq 'lti') {
10631:             if (ref($settings) eq 'HASH') {
10632:                 $hashref = $settings->{'instdata'};
10633:             }
10634:         } elsif ($context eq 'privacy') {
10635:             my ($key,$inner) = split(/_/,$role);
10636:             if (ref($settings) eq 'HASH') {
10637:                 if (ref($settings->{$key}) eq 'HASH') {
10638:                     $hashref = $settings->{$key}->{$inner};
10639:                 }
10640:             }
10641:         } elsif (ref($settings->{$context}) eq 'HASH') {
10642:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
10643:                 $hashref = $settings->{'lti_instdata'};
10644:             }
10645:             if ($role eq 'emailusername') {
10646:                 if ($statustype) {
10647:                     if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
10648:                         $hashref = $settings->{$context}->{$role}->{$statustype};
10649:                     }
10650:                 }
10651:             }
10652:         }
10653:         if (ref($hashref) eq 'HASH') { 
10654:             foreach my $field (@fields) {
10655:                 if ($hashref->{$field}) {
10656:                     if ($role eq 'emailusername') {
10657:                         $checks{$field} = $hashref->{$field};
10658:                     } else {
10659:                         $checks{$field} = ' checked="checked" ';
10660:                     }
10661:                 }
10662:             }
10663:         }
10664:     }
10665:  
10666:     my $total = scalar(@fields);
10667:     for (my $i=0; $i<$total; $i++) {
10668:         $rem = $i%($numinrow);
10669:         if ($rem == 0) {
10670:             if ($i > 0) {
10671:                 $output .= '</tr>';
10672:             }
10673:             $output .= '<tr>';
10674:         }
10675:         my $check = ' ';
10676:         unless ($role eq 'emailusername') {
10677:             if (exists($checks{$fields[$i]})) {
10678:                 $check = $checks{$fields[$i]};
10679:             } elsif ($context eq 'privacy') {
10680:                 if ($role =~ /^priv_(domain|course)$/) {
10681:                     if (ref($settings) ne 'HASH') {
10682:                         $check = ' checked="checked" ';
10683:                     }
10684:                 } elsif ($role =~ /^priv_(author|community)$/) {
10685:                     if (ref($settings) ne 'HASH') {
10686:                         unless ($fields[$i] eq 'id') {
10687:                             $check = ' checked="checked" ';
10688:                         }
10689:                     }
10690:                 } elsif ($role =~ /^(unpriv|othdom)_/) {
10691:                     if (ref($settings) ne 'HASH') {
10692:                         if (($fields[$i] eq 'lastname') || ($fields[$i] eq 'firstname')) {
10693:                             $check = ' checked="checked" ';
10694:                         }
10695:                     }
10696:                 }
10697:             } elsif ($context ne 'lti') {
10698:                 if ($role eq 'st') {
10699:                     if (ref($settings) ne 'HASH') {
10700:                         $check = ' checked="checked" '; 
10701:                     }
10702:                 }
10703:             }
10704:         }
10705:         $output .= '<td class="LC_left_item">'.
10706:                    '<span class="LC_nobreak">';
10707:         my $prefix = 'canmodify';
10708:         if ($role eq 'emailusername') {
10709:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
10710:                 $checks{$fields[$i]} = 'omit';
10711:             }
10712:             foreach my $option ('required','optional','omit') {
10713:                 my $checked='';
10714:                 if ($checks{$fields[$i]} eq $option) {
10715:                     $checked='checked="checked" ';
10716:                 }
10717:                 $output .= '<label>'.
10718:                            '<input type="radio" name="'.$prefix.'_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
10719:                            &mt($option).'</label>'.('&nbsp;' x2);
10720:             }
10721:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
10722:         } else {
10723:             if ($context eq 'lti') {
10724:                 $prefix = 'lti';
10725:             } elsif ($context eq 'privacy') {
10726:                 $prefix = 'privacy';
10727:             }
10728:             $output .= '<label>'.
10729:                        '<input type="checkbox" name="'.$prefix.'_'.$role.'" '.
10730:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
10731:                        '</label>';
10732:         }
10733:         $output .= '</span></td>';
10734:     }
10735:     $rem = $total%$numinrow;
10736:     my $colsleft;
10737:     if ($rem) {
10738:         $colsleft = $numinrow - $rem;
10739:     }
10740:     if ($colsleft > 1) {
10741:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
10742:                    '&nbsp;</td>';
10743:     } elsif ($colsleft == 1) {
10744:         $output .= '<td class="LC_left_item">&nbsp;</td>';
10745:     }
10746:     $output .= '</tr></table></td></tr>';
10747:     return $output;
10748: }
10749: 
10750: sub insttypes_row {
10751:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
10752:         $customcss,$rowstyle) = @_;
10753:     my %lt = &Apache::lonlocal::texthash (
10754:                       cansearch => 'Users allowed to search',
10755:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
10756:                       lockablenames => 'User preference to lock name',
10757:                       selfassign    => 'Self-reportable affiliations',
10758:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
10759:              );
10760:     my $showdom;
10761:     if ($context eq 'cansearch') {
10762:         $showdom = ' ('.$dom.')';
10763:     }
10764:     my $class = 'LC_left_item';
10765:     if ($context eq 'statustocreate') {
10766:         $class = 'LC_right_item';
10767:     }
10768:     my $css_class;
10769:     if ($$rowtotal%2) {
10770:         $css_class = 'LC_odd_row';
10771:     }
10772:     if ($customcss) {
10773:         $css_class .= ' '.$customcss;
10774:     }
10775:     $css_class =~ s/^\s+//;
10776:     if ($css_class) {
10777:         $css_class = ' class="'.$css_class.'"';
10778:     }
10779:     if ($rowstyle) {
10780:         $css_class .= ' style="'.$rowstyle.'"';
10781:     }
10782:     if ($onclick) {
10783:         $onclick = 'onclick="'.$onclick.'" ';
10784:     }
10785:     my $output = '<tr'.$css_class.'>'.
10786:                  '<td>'.$lt{$context}.$showdom.
10787:                  '</td><td class="'.$class.'" colspan="2"><table>';
10788:     my $rem;
10789:     if (ref($types) eq 'ARRAY') {
10790:         for (my $i=0; $i<@{$types}; $i++) {
10791:             if (defined($usertypes->{$types->[$i]})) {
10792:                 my $rem = $i%($numinrow);
10793:                 if ($rem == 0) {
10794:                     if ($i > 0) {
10795:                         $output .= '</tr>';
10796:                     }
10797:                     $output .= '<tr>';
10798:                 }
10799:                 my $check = ' ';
10800:                 if (ref($settings) eq 'HASH') {
10801:                     if (ref($settings->{$context}) eq 'ARRAY') {
10802:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
10803:                             $check = ' checked="checked" ';
10804:                         }
10805:                     } elsif (ref($settings->{$context}) eq 'HASH') {
10806:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
10807:                             $check = ' checked="checked" ';
10808:                         }
10809:                     } elsif ($context eq 'statustocreate') {
10810:                         $check = ' checked="checked" ';
10811:                     }
10812:                 }
10813:                 $output .= '<td class="LC_left_item">'.
10814:                            '<span class="LC_nobreak"><label>'.
10815:                            '<input type="checkbox" name="'.$context.'" '.
10816:                            'value="'.$types->[$i].'"'.$check.$onclick.' />'.
10817:                            $usertypes->{$types->[$i]}.'</label></span></td>';
10818:             }
10819:         }
10820:         $rem = @{$types}%($numinrow);
10821:     }
10822:     my $colsleft = $numinrow - $rem;
10823:     if ($context eq 'overrides') {
10824:         if ($colsleft > 1) {
10825:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
10826:         } else {
10827:             $output .= '<td class="LC_left_item">';
10828:         }
10829:         $output .= '&nbsp;';  
10830:     } else {
10831:         if ($rem == 0) {
10832:             $output .= '<tr>';
10833:         }
10834:         if ($colsleft > 1) {
10835:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
10836:         } else {
10837:             $output .= '<td class="LC_left_item">';
10838:         }
10839:         my $defcheck = ' ';
10840:         if (ref($settings) eq 'HASH') {  
10841:             if (ref($settings->{$context}) eq 'ARRAY') {
10842:                 if (grep(/^default$/,@{$settings->{$context}})) {
10843:                     $defcheck = ' checked="checked" ';
10844:                 }
10845:             } elsif ($context eq 'statustocreate') {
10846:                 $defcheck = ' checked="checked" ';
10847:             }
10848:         }
10849:         $output .= '<span class="LC_nobreak"><label>'.
10850:                    '<input type="checkbox" name="'.$context.'" '.
10851:                    'value="default"'.$defcheck.$onclick.' />'.
10852:                    $othertitle.'</label></span>';
10853:     }
10854:     $output .= '</td></tr></table></td></tr>';
10855:     return $output;
10856: }
10857: 
10858: sub sorted_searchtitles {
10859:     my %searchtitles = &Apache::lonlocal::texthash(
10860:                          'uname' => 'username',
10861:                          'lastname' => 'last name',
10862:                          'lastfirst' => 'last name, first name',
10863:                      );
10864:     my @titleorder = ('uname','lastname','lastfirst');
10865:     return (\%searchtitles,\@titleorder);
10866: }
10867: 
10868: sub sorted_searchtypes {
10869:     my %srchtypes_desc = (
10870:                            exact    => 'is exact match',
10871:                            contains => 'contains ..',
10872:                            begins   => 'begins with ..',
10873:                          );
10874:     my @srchtypeorder = ('exact','begins','contains');
10875:     return (\%srchtypes_desc,\@srchtypeorder);
10876: }
10877: 
10878: sub usertype_update_row {
10879:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
10880:     my $datatable;
10881:     my $numinrow = 4;
10882:     foreach my $type (@{$types}) {
10883:         if (defined($usertypes->{$type})) {
10884:             $$rownums ++;
10885:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
10886:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
10887:                           '</td><td class="LC_left_item"><table>';
10888:             for (my $i=0; $i<@{$fields}; $i++) {
10889:                 my $rem = $i%($numinrow);
10890:                 if ($rem == 0) {
10891:                     if ($i > 0) {
10892:                         $datatable .= '</tr>';
10893:                     }
10894:                     $datatable .= '<tr>';
10895:                 }
10896:                 my $check = ' ';
10897:                 if (ref($settings) eq 'HASH') {
10898:                     if (ref($settings->{'fields'}) eq 'HASH') {
10899:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
10900:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
10901:                                 $check = ' checked="checked" ';
10902:                             }
10903:                         }
10904:                     }
10905:                 }
10906: 
10907:                 if ($i == @{$fields}-1) {
10908:                     my $colsleft = $numinrow - $rem;
10909:                     if ($colsleft > 1) {
10910:                         $datatable .= '<td colspan="'.$colsleft.'">';
10911:                     } else {
10912:                         $datatable .= '<td>';
10913:                     }
10914:                 } else {
10915:                     $datatable .= '<td>';
10916:                 }
10917:                 $datatable .= '<span class="LC_nobreak"><label>'.
10918:                               '<input type="checkbox" name="updateable_'.$type.
10919:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
10920:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
10921:             }
10922:             $datatable .= '</tr></table></td></tr>';
10923:         }
10924:     }
10925:     return $datatable;
10926: }
10927: 
10928: sub modify_login {
10929:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
10930:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
10931:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
10932:     %title = ( coursecatalog => 'Display course catalog',
10933:                adminmail => 'Display administrator E-mail address',
10934:                helpdesk  => 'Display "Contact Helpdesk" link',
10935:                newuser => 'Link for visitors to create a user account',
10936:                loginheader => 'Log-in box header');
10937:     @offon = ('off','on');
10938:     if (ref($domconfig{login}) eq 'HASH') {
10939:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
10940:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
10941:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
10942:             }
10943:         }
10944:     }
10945:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
10946:                                            \%domconfig,\%loginhash);
10947:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
10948:     foreach my $item (@toggles) {
10949:         $loginhash{login}{$item} = $env{'form.'.$item};
10950:     }
10951:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
10952:     if (ref($colchanges{'login'}) eq 'HASH') {  
10953:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
10954:                                          \%loginhash);
10955:     }
10956: 
10957:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10958:     my %domservers = &Apache::lonnet::get_servers($dom);
10959:     my @loginvia_attribs = ('serverpath','custompath','exempt');
10960:     if (keys(%servers) > 1) {
10961:         foreach my $lonhost (keys(%servers)) {
10962:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
10963:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
10964:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
10965:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
10966:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
10967:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
10968:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
10969:                         $changes{'loginvia'}{$lonhost} = 1;
10970:                     } else {
10971:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
10972:                         $changes{'loginvia'}{$lonhost} = 1;
10973:                     }
10974:                 } else {
10975:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
10976:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
10977:                         $changes{'loginvia'}{$lonhost} = 1;
10978:                     }
10979:                 }
10980:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
10981:                     foreach my $item (@loginvia_attribs) {
10982:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
10983:                     }
10984:                 } else {
10985:                     foreach my $item (@loginvia_attribs) {
10986:                         my $new = $env{'form.'.$lonhost.'_'.$item};
10987:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
10988:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
10989:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
10990:                                 $new = '/';
10991:                             }
10992:                         }
10993:                         if (($item eq 'custompath') && 
10994:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
10995:                             $new = '';
10996:                         }
10997:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
10998:                             $changes{'loginvia'}{$lonhost} = 1;
10999:                         }
11000:                         if ($item eq 'exempt') {
11001:                             $new = &check_exempt_addresses($new);
11002:                         }
11003:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
11004:                     }
11005:                 }
11006:             } else {
11007:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
11008:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
11009:                     $changes{'loginvia'}{$lonhost} = 1;
11010:                     foreach my $item (@loginvia_attribs) {
11011:                         my $new = $env{'form.'.$lonhost.'_'.$item};
11012:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
11013:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
11014:                                 $new = '/';
11015:                             }
11016:                         }
11017:                         if (($item eq 'custompath') && 
11018:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
11019:                             $new = '';
11020:                         }
11021:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
11022:                     }
11023:                 }
11024:             }
11025:         }
11026:     }
11027: 
11028:     my $servadm = $r->dir_config('lonAdmEMail');
11029:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
11030:     if (ref($domconfig{'login'}) eq 'HASH') {
11031:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
11032:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
11033:                 if ($lang eq 'nolang') {
11034:                     push(@currlangs,$lang);
11035:                 } elsif (defined($langchoices{$lang})) {
11036:                     push(@currlangs,$lang);
11037:                 } else {
11038:                     next;
11039:                 }
11040:             }
11041:         }
11042:     }
11043:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
11044:     if (@currlangs > 0) {
11045:         foreach my $lang (@currlangs) {
11046:             if (grep(/^\Q$lang\E$/,@delurls)) {
11047:                 $changes{'helpurl'}{$lang} = 1;
11048:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
11049:                 $changes{'helpurl'}{$lang} = 1;
11050:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
11051:                 push(@newlangs,$lang);
11052:             } else {
11053:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
11054:             }
11055:         }
11056:     }
11057:     unless (grep(/^nolang$/,@currlangs)) {
11058:         if ($env{'form.loginhelpurl_nolang.filename'}) {
11059:             $changes{'helpurl'}{'nolang'} = 1;
11060:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
11061:             push(@newlangs,'nolang');
11062:         }
11063:     }
11064:     if ($env{'form.loginhelpurl_add_lang'}) {
11065:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
11066:             ($env{'form.loginhelpurl_add_file.filename'})) {
11067:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
11068:             $addedfile = $env{'form.loginhelpurl_add_lang'};
11069:         }
11070:     }
11071:     if ((@newlangs > 0) || ($addedfile)) {
11072:         my $error;
11073:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11074:         if ($configuserok eq 'ok') {
11075:             if ($switchserver) {
11076:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
11077:             } elsif ($author_ok eq 'ok') {
11078:                 my @allnew = @newlangs;
11079:                 if ($addedfile ne '') {
11080:                     push(@allnew,$addedfile);
11081:                 }
11082:                 foreach my $lang (@allnew) {
11083:                     my $formelem = 'loginhelpurl_'.$lang;
11084:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
11085:                         $formelem = 'loginhelpurl_add_file';
11086:                     }
11087:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
11088:                                                                "help/$lang",'','',$newfile{$lang});
11089:                     if ($result eq 'ok') {
11090:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
11091:                         $changes{'helpurl'}{$lang} = 1;
11092:                     } else {
11093:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
11094:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
11095:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
11096:                             (!grep(/^\Q$lang\E$/,@delurls))) {
11097:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
11098:                         }
11099:                     }
11100:                 }
11101:             } else {
11102:                 $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);
11103:             }
11104:         } else {
11105:             $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);
11106:         }
11107:         if ($error) {
11108:             &Apache::lonnet::logthis($error);
11109:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11110:         }
11111:     }
11112: 
11113:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
11114:     if (ref($domconfig{'login'}) eq 'HASH') {
11115:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
11116:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
11117:                 if ($domservers{$lonhost}) {
11118:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
11119:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
11120:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
11121:                     }
11122:                 }
11123:             }
11124:         }
11125:     }
11126:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
11127:     foreach my $lonhost (sort(keys(%domservers))) {
11128:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
11129:             $changes{'headtag'}{$lonhost} = 1;
11130:         } else {
11131:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
11132:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
11133:             }
11134:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
11135:                 push(@newhosts,$lonhost);
11136:             } elsif ($currheadtagurls{$lonhost}) {
11137:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
11138:                 if ($currexempt{$lonhost}) {
11139:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
11140:                         $changes{'headtag'}{$lonhost} = 1;
11141:                     }
11142:                 } elsif ($possexempt{$lonhost}) {
11143:                     $changes{'headtag'}{$lonhost} = 1;
11144:                 }
11145:                 if ($possexempt{$lonhost}) {
11146:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
11147:                 }
11148:             }
11149:         }
11150:     }
11151:     if (@newhosts) {
11152:         my $error;
11153:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11154:         if ($configuserok eq 'ok') {
11155:             if ($switchserver) {
11156:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
11157:             } elsif ($author_ok eq 'ok') {
11158:                 foreach my $lonhost (@newhosts) {
11159:                     my $formelem = 'loginheadtag_'.$lonhost;
11160:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
11161:                                                                           "login/headtag/$lonhost",'','',
11162:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
11163:                     if ($result eq 'ok') {
11164:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
11165:                         $changes{'headtag'}{$lonhost} = 1;
11166:                         if ($possexempt{$lonhost}) {
11167:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
11168:                         }
11169:                     } else {
11170:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
11171:                                            $newheadtagurls{$lonhost},$result);
11172:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
11173:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
11174:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
11175:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
11176:                         }
11177:                     }
11178:                 }
11179:             } else {
11180:                 $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);
11181:             }
11182:         } else {
11183:             $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);
11184:         }
11185:         if ($error) {
11186:             &Apache::lonnet::logthis($error);
11187:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11188:         }
11189:     }
11190:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
11191: 
11192:     my $defaulthelpfile = '/adm/loginproblems.html';
11193:     my $defaulttext = &mt('Default in use');
11194: 
11195:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
11196:                                              $dom);
11197:     if ($putresult eq 'ok') {
11198:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
11199:         my %defaultchecked = (
11200:                     'coursecatalog' => 'on',
11201:                     'helpdesk'      => 'on',
11202:                     'adminmail'     => 'off',
11203:                     'newuser'       => 'off',
11204:         );
11205:         if (ref($domconfig{'login'}) eq 'HASH') {
11206:             foreach my $item (@toggles) {
11207:                 if ($defaultchecked{$item} eq 'on') { 
11208:                     if (($domconfig{'login'}{$item} eq '0') &&
11209:                         ($env{'form.'.$item} eq '1')) {
11210:                         $changes{$item} = 1;
11211:                     } elsif (($domconfig{'login'}{$item} eq '' ||
11212:                               $domconfig{'login'}{$item} eq '1') &&
11213:                              ($env{'form.'.$item} eq '0')) {
11214:                         $changes{$item} = 1;
11215:                     }
11216:                 } elsif ($defaultchecked{$item} eq 'off') {
11217:                     if (($domconfig{'login'}{$item} eq '1') &&
11218:                         ($env{'form.'.$item} eq '0')) {
11219:                         $changes{$item} = 1;
11220:                     } elsif (($domconfig{'login'}{$item} eq '' ||
11221:                               $domconfig{'login'}{$item} eq '0') &&
11222:                              ($env{'form.'.$item} eq '1')) {
11223:                         $changes{$item} = 1;
11224:                     }
11225:                 }
11226:             }
11227:         }
11228:         if (keys(%changes) > 0 || $colchgtext) {
11229:             &Apache::loncommon::devalidate_domconfig_cache($dom);
11230:             if (ref($lastactref) eq 'HASH') {
11231:                 $lastactref->{'domainconfig'} = 1;
11232:             }
11233:             $resulttext = &mt('Changes made:').'<ul>';
11234:             foreach my $item (sort(keys(%changes))) {
11235:                 if ($item eq 'loginvia') {
11236:                     if (ref($changes{$item}) eq 'HASH') {
11237:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
11238:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
11239:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
11240:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
11241:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
11242:                                     $protocol = 'http' if ($protocol ne 'https');
11243:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
11244: 
11245:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
11246:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
11247:                                     } else {
11248:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
11249:                                     }
11250:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
11251:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
11252:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
11253:                                     }
11254:                                     $resulttext .= '</li>';
11255:                                 } else {
11256:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
11257:                                 }
11258:                             } else {
11259:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
11260:                             }
11261:                         }
11262:                         $resulttext .= '</ul></li>';
11263:                     }
11264:                 } elsif ($item eq 'helpurl') {
11265:                     if (ref($changes{$item}) eq 'HASH') {
11266:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
11267:                             if (grep(/^\Q$lang\E$/,@delurls)) {
11268:                                 my ($chg,$link);
11269:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
11270:                                 if ($lang eq 'nolang') {
11271:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
11272:                                 } else {
11273:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
11274:                                 }
11275:                                 $resulttext .= '<li>'.$chg.'</li>';
11276:                             } else {
11277:                                 my $chg;
11278:                                 if ($lang eq 'nolang') {
11279:                                     $chg = &mt('custom log-in help file for no preferred language');
11280:                                 } else {
11281:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
11282:                                 }
11283:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
11284:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
11285:                                                       '?inhibitmenu=yes',$chg,600,500).
11286:                                                '</li>';
11287:                             }
11288:                         }
11289:                     }
11290:                 } elsif ($item eq 'headtag') {
11291:                     if (ref($changes{$item}) eq 'HASH') {
11292:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
11293:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
11294:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
11295:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
11296:                                 $resulttext .= '<li><a href="'.
11297:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
11298:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
11299:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
11300:                                 if ($possexempt{$lonhost}) {
11301:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
11302:                                 } else {
11303:                                     $resulttext .= &mt('included for any client IP');
11304:                                 }
11305:                                 $resulttext .= '</li>';
11306:                             }
11307:                         }
11308:                     }
11309:                 } elsif ($item eq 'captcha') {
11310:                     if (ref($loginhash{'login'}) eq 'HASH') {
11311:                         my $chgtxt;
11312:                         if ($loginhash{'login'}{$item} eq 'notused') {
11313:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
11314:                         } else {
11315:                             my %captchas = &captcha_phrases();
11316:                             if ($captchas{$loginhash{'login'}{$item}}) {
11317:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
11318:                             } else {
11319:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
11320:                             }
11321:                         }
11322:                         $resulttext .= '<li>'.$chgtxt.'</li>';
11323:                     }
11324:                 } elsif ($item eq 'recaptchakeys') {
11325:                     if (ref($loginhash{'login'}) eq 'HASH') {
11326:                         my ($privkey,$pubkey);
11327:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
11328:                             $pubkey = $loginhash{'login'}{$item}{'public'};
11329:                             $privkey = $loginhash{'login'}{$item}{'private'};
11330:                         }
11331:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
11332:                         if (!$pubkey) {
11333:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
11334:                         } else {
11335:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
11336:                         }
11337:                         if (!$privkey) {
11338:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
11339:                         } else {
11340:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
11341:                         }
11342:                         $chgtxt .= '</ul>';
11343:                         $resulttext .= '<li>'.$chgtxt.'</li>';
11344:                     }
11345:                 } elsif ($item eq 'recaptchaversion') {
11346:                     if (ref($loginhash{'login'}) eq 'HASH') {
11347:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
11348:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
11349:                                            '</li>';
11350:                         }
11351:                     }
11352:                 } else {
11353:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
11354:                 }
11355:             }
11356:             $resulttext .= $colchgtext.'</ul>';
11357:         } else {
11358:             $resulttext = &mt('No changes made to log-in page settings');
11359:         }
11360:     } else {
11361:         $resulttext = '<span class="LC_error">'.
11362: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11363:     }
11364:     if ($errors) {
11365:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
11366:                        $errors.'</ul>';
11367:     }
11368:     return $resulttext;
11369: }
11370: 
11371: sub check_exempt_addresses {
11372:     my ($iplist) = @_;
11373:     $iplist =~ s/^\s+//;
11374:     $iplist =~ s/\s+$//;
11375:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
11376:     my (@okips,$new);
11377:     foreach my $ip (@poss_ips) {
11378:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
11379:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
11380:                 push(@okips,$ip);
11381:             }
11382:         }
11383:     }
11384:     if (@okips > 0) {
11385:         $new = join(',',@okips);
11386:     } else {
11387:         $new = '';
11388:     }
11389:     return $new;
11390: }
11391: 
11392: sub color_font_choices {
11393:     my %choices =
11394:         &Apache::lonlocal::texthash (
11395:             img => "Header",
11396:             bgs => "Background colors",
11397:             links => "Link colors",
11398:             images => "Images",
11399:             font => "Font color",
11400:             fontmenu => "Font menu",
11401:             pgbg => "Page",
11402:             tabbg => "Header",
11403:             sidebg => "Border",
11404:             link => "Link",
11405:             alink => "Active link",
11406:             vlink => "Visited link",
11407:         );
11408:     return %choices;
11409: }
11410: 
11411: sub modify_rolecolors {
11412:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
11413:     my ($resulttext,%rolehash);
11414:     $rolehash{'rolecolors'} = {};
11415:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
11416:         if ($domconfig{'rolecolors'} eq '') {
11417:             $domconfig{'rolecolors'} = {};
11418:         }
11419:     }
11420:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
11421:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
11422:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
11423:                                              $dom);
11424:     if ($putresult eq 'ok') {
11425:         if (keys(%changes) > 0) {
11426:             &Apache::loncommon::devalidate_domconfig_cache($dom);
11427:             if (ref($lastactref) eq 'HASH') {
11428:                 $lastactref->{'domainconfig'} = 1;
11429:             }
11430:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
11431:                                              $rolehash{'rolecolors'});
11432:         } else {
11433:             $resulttext = &mt('No changes made to default color schemes');
11434:         }
11435:     } else {
11436:         $resulttext = '<span class="LC_error">'.
11437: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11438:     }
11439:     if ($errors) {
11440:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
11441:                        $errors.'</ul>';
11442:     }
11443:     return $resulttext;
11444: }
11445: 
11446: sub modify_colors {
11447:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
11448:     my (%changes,%choices);
11449:     my @bgs;
11450:     my @links = ('link','alink','vlink');
11451:     my @logintext;
11452:     my @images;
11453:     my $servadm = $r->dir_config('lonAdmEMail');
11454:     my $errors;
11455:     my %defaults;
11456:     foreach my $role (@{$roles}) {
11457:         if ($role eq 'login') {
11458:             %choices = &login_choices();
11459:             @logintext = ('textcol','bgcol');
11460:         } else {
11461:             %choices = &color_font_choices();
11462:         }
11463:         if ($role eq 'login') {
11464:             @images = ('img','logo','domlogo','login');
11465:             @bgs = ('pgbg','mainbg','sidebg');
11466:         } else {
11467:             @images = ('img');
11468:             @bgs = ('pgbg','tabbg','sidebg');
11469:         }
11470:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
11471:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
11472:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
11473:         }
11474:         if ($role eq 'login') {
11475:             foreach my $item (@logintext) {
11476:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
11477:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
11478:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
11479:                 }
11480:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
11481:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
11482:                 }
11483:             }
11484:         } else {
11485:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
11486:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
11487:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
11488:             }
11489:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
11490:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
11491:             }
11492:         }
11493:         foreach my $item (@bgs) {
11494:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
11495:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
11496:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
11497:             }
11498:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
11499:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
11500:             }
11501:         }
11502:         foreach my $item (@links) {
11503:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
11504:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
11505:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
11506:             }
11507:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
11508:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
11509:             }
11510:         }
11511:         my ($configuserok,$author_ok,$switchserver) = 
11512:             &config_check($dom,$confname,$servadm);
11513:         my ($width,$height) = &thumb_dimensions();
11514:         if (ref($domconfig->{$role}) ne 'HASH') {
11515:             $domconfig->{$role} = {};
11516:         }
11517:         foreach my $img (@images) {
11518:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
11519:                 if (defined($env{'form.login_showlogo_'.$img})) {
11520:                     $confhash->{$role}{'showlogo'}{$img} = 1;
11521:                 } else { 
11522:                     $confhash->{$role}{'showlogo'}{$img} = 0;
11523:                 }
11524:             } 
11525: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
11526: 		 && !defined($domconfig->{$role}{$img})
11527: 		 && !$env{'form.'.$role.'_del_'.$img}
11528: 		 && $env{'form.'.$role.'_import_'.$img}) {
11529: 		# import the old configured image from the .tab setting
11530: 		# if they haven't provided a new one 
11531: 		$domconfig->{$role}{$img} = 
11532: 		    $env{'form.'.$role.'_import_'.$img};
11533: 	    }
11534:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
11535:                 my $error;
11536:                 if ($configuserok eq 'ok') {
11537:                     if ($switchserver) {
11538:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
11539:                     } else {
11540:                         if ($author_ok eq 'ok') {
11541:                             my ($result,$logourl) = 
11542:                                 &publishlogo($r,'upload',$role.'_'.$img,
11543:                                            $dom,$confname,$img,$width,$height);
11544:                             if ($result eq 'ok') {
11545:                                 $confhash->{$role}{$img} = $logourl;
11546:                                 $changes{$role}{'images'}{$img} = 1;
11547:                             } else {
11548:                                 $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);
11549:                             }
11550:                         } else {
11551:                             $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);
11552:                         }
11553:                     }
11554:                 } else {
11555:                     $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);
11556:                 }
11557:                 if ($error) {
11558:                     &Apache::lonnet::logthis($error);
11559:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11560:                 }
11561:             } elsif ($domconfig->{$role}{$img} ne '') {
11562:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
11563:                     my $error;
11564:                     if ($configuserok eq 'ok') {
11565: # is confname an author?
11566:                         if ($switchserver eq '') {
11567:                             if ($author_ok eq 'ok') {
11568:                                 my ($result,$logourl) = 
11569:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
11570:                                             $dom,$confname,$img,$width,$height);
11571:                                 if ($result eq 'ok') {
11572:                                     $confhash->{$role}{$img} = $logourl;
11573: 				    $changes{$role}{'images'}{$img} = 1;
11574:                                 }
11575:                             }
11576:                         }
11577:                     }
11578:                 }
11579:             }
11580:         }
11581:         if (ref($domconfig) eq 'HASH') {
11582:             if (ref($domconfig->{$role}) eq 'HASH') {
11583:                 foreach my $img (@images) {
11584:                     if ($domconfig->{$role}{$img} ne '') {
11585:                         if ($env{'form.'.$role.'_del_'.$img}) {
11586:                             $confhash->{$role}{$img} = '';
11587:                             $changes{$role}{'images'}{$img} = 1;
11588:                         } else {
11589:                             if ($confhash->{$role}{$img} eq '') {
11590:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
11591:                             }
11592:                         }
11593:                     } else {
11594:                         if ($env{'form.'.$role.'_del_'.$img}) {
11595:                             $confhash->{$role}{$img} = '';
11596:                             $changes{$role}{'images'}{$img} = 1;
11597:                         } 
11598:                     }
11599:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
11600:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
11601:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
11602:                                 $domconfig->{$role}{'showlogo'}{$img}) {
11603:                                 $changes{$role}{'showlogo'}{$img} = 1; 
11604:                             }
11605:                         } else {
11606:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
11607:                                 $changes{$role}{'showlogo'}{$img} = 1;
11608:                             }
11609:                         }
11610:                     }
11611:                 }
11612:                 if ($domconfig->{$role}{'font'} ne '') {
11613:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
11614:                         $changes{$role}{'font'} = 1;
11615:                     }
11616:                 } else {
11617:                     if ($confhash->{$role}{'font'}) {
11618:                         $changes{$role}{'font'} = 1;
11619:                     }
11620:                 }
11621:                 if ($role ne 'login') {
11622:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
11623:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
11624:                             $changes{$role}{'fontmenu'} = 1;
11625:                         }
11626:                     } else {
11627:                         if ($confhash->{$role}{'fontmenu'}) {
11628:                             $changes{$role}{'fontmenu'} = 1;
11629:                         }
11630:                     }
11631:                 }
11632:                 foreach my $item (@bgs) {
11633:                     if ($domconfig->{$role}{$item} ne '') {
11634:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
11635:                             $changes{$role}{'bgs'}{$item} = 1;
11636:                         } 
11637:                     } else {
11638:                         if ($confhash->{$role}{$item}) {
11639:                             $changes{$role}{'bgs'}{$item} = 1;
11640:                         }
11641:                     }
11642:                 }
11643:                 foreach my $item (@links) {
11644:                     if ($domconfig->{$role}{$item} ne '') {
11645:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
11646:                             $changes{$role}{'links'}{$item} = 1;
11647:                         }
11648:                     } else {
11649:                         if ($confhash->{$role}{$item}) {
11650:                             $changes{$role}{'links'}{$item} = 1;
11651:                         }
11652:                     }
11653:                 }
11654:                 foreach my $item (@logintext) {
11655:                     if ($domconfig->{$role}{$item} ne '') {
11656:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
11657:                             $changes{$role}{'logintext'}{$item} = 1;
11658:                         }
11659:                     } else {
11660:                         if ($confhash->{$role}{$item}) {
11661:                             $changes{$role}{'logintext'}{$item} = 1;
11662:                         }
11663:                     }
11664:                 }
11665:             } else {
11666:                 &default_change_checker($role,\@images,\@links,\@bgs,
11667:                                         \@logintext,$confhash,\%changes); 
11668:             }
11669:         } else {
11670:             &default_change_checker($role,\@images,\@links,\@bgs,
11671:                                     \@logintext,$confhash,\%changes); 
11672:         }
11673:     }
11674:     return ($errors,%changes);
11675: }
11676: 
11677: sub config_check {
11678:     my ($dom,$confname,$servadm) = @_;
11679:     my ($configuserok,$author_ok,$switchserver,%currroles);
11680:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
11681:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
11682:                                                    $confname,$servadm);
11683:     if ($configuserok eq 'ok') {
11684:         $switchserver = &check_switchserver($dom,$confname);
11685:         if ($switchserver eq '') {
11686:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
11687:         }
11688:     }
11689:     return ($configuserok,$author_ok,$switchserver);
11690: }
11691: 
11692: sub default_change_checker {
11693:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
11694:     foreach my $item (@{$links}) {
11695:         if ($confhash->{$role}{$item}) {
11696:             $changes->{$role}{'links'}{$item} = 1;
11697:         }
11698:     }
11699:     foreach my $item (@{$bgs}) {
11700:         if ($confhash->{$role}{$item}) {
11701:             $changes->{$role}{'bgs'}{$item} = 1;
11702:         }
11703:     }
11704:     foreach my $item (@{$logintext}) {
11705:         if ($confhash->{$role}{$item}) {
11706:             $changes->{$role}{'logintext'}{$item} = 1;
11707:         }
11708:     }
11709:     foreach my $img (@{$images}) {
11710:         if ($env{'form.'.$role.'_del_'.$img}) {
11711:             $confhash->{$role}{$img} = '';
11712:             $changes->{$role}{'images'}{$img} = 1;
11713:         }
11714:         if ($role eq 'login') {
11715:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
11716:                 $changes->{$role}{'showlogo'}{$img} = 1;
11717:             }
11718:         }
11719:     }
11720:     if ($confhash->{$role}{'font'}) {
11721:         $changes->{$role}{'font'} = 1;
11722:     }
11723: }
11724: 
11725: sub display_colorchgs {
11726:     my ($dom,$changes,$roles,$confhash) = @_;
11727:     my (%choices,$resulttext);
11728:     if (!grep(/^login$/,@{$roles})) {
11729:         $resulttext = &mt('Changes made:').'<br />';
11730:     }
11731:     foreach my $role (@{$roles}) {
11732:         if ($role eq 'login') {
11733:             %choices = &login_choices();
11734:         } else {
11735:             %choices = &color_font_choices();
11736:         }
11737:         if (ref($changes->{$role}) eq 'HASH') {
11738:             if ($role ne 'login') {
11739:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
11740:             }
11741:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
11742:                 if ($role ne 'login') {
11743:                     $resulttext .= '<ul>';
11744:                 }
11745:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
11746:                     if ($role ne 'login') {
11747:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
11748:                     }
11749:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
11750:                         if (($role eq 'login') && ($key eq 'showlogo')) {
11751:                             if ($confhash->{$role}{$key}{$item}) {
11752:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
11753:                             } else {
11754:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
11755:                             }
11756:                         } elsif ($confhash->{$role}{$item} eq '') {
11757:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
11758:                         } else {
11759:                             my $newitem = $confhash->{$role}{$item};
11760:                             if ($key eq 'images') {
11761:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
11762:                             }
11763:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
11764:                         }
11765:                     }
11766:                     if ($role ne 'login') {
11767:                         $resulttext .= '</ul></li>';
11768:                     }
11769:                 } else {
11770:                     if ($confhash->{$role}{$key} eq '') {
11771:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
11772:                     } else {
11773:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
11774:                     }
11775:                 }
11776:                 if ($role ne 'login') {
11777:                     $resulttext .= '</ul>';
11778:                 }
11779:             }
11780:         }
11781:     }
11782:     return $resulttext;
11783: }
11784: 
11785: sub thumb_dimensions {
11786:     return ('200','50');
11787: }
11788: 
11789: sub check_dimensions {
11790:     my ($inputfile) = @_;
11791:     my ($fullwidth,$fullheight);
11792:     if ($inputfile =~ m|^[/\w.\-]+$|) {
11793:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
11794:             my $imageinfo = <PIPE>;
11795:             if (!close(PIPE)) {
11796:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
11797:             }
11798:             chomp($imageinfo);
11799:             my ($fullsize) = 
11800:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
11801:             if ($fullsize) {
11802:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
11803:             }
11804:         }
11805:     }
11806:     return ($fullwidth,$fullheight);
11807: }
11808: 
11809: sub check_configuser {
11810:     my ($uhome,$dom,$confname,$servadm) = @_;
11811:     my ($configuserok,%currroles);
11812:     if ($uhome eq 'no_host') {
11813:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
11814:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
11815:         $configuserok = 
11816:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
11817:                              $configpass,'','','','','',undef,$servadm);
11818:     } else {
11819:         $configuserok = 'ok';
11820:         %currroles = 
11821:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
11822:     }
11823:     return ($configuserok,%currroles);
11824: }
11825: 
11826: sub check_authorstatus {
11827:     my ($dom,$confname,%currroles) = @_;
11828:     my $author_ok;
11829:     if (!$currroles{':'.$dom.':au'}) {
11830:         my $start = time;
11831:         my $end = 0;
11832:         $author_ok = 
11833:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
11834:                                         'au',$end,$start,'','','domconfig');
11835:     } else {
11836:         $author_ok = 'ok';
11837:     }
11838:     return $author_ok;
11839: }
11840: 
11841: sub publishlogo {
11842:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
11843:     my ($output,$fname,$logourl,$madethumb);
11844:     if ($action eq 'upload') {
11845:         $fname=$env{'form.'.$formname.'.filename'};
11846:         chop($env{'form.'.$formname});
11847:     } else {
11848:         ($fname) = ($formname =~ /([^\/]+)$/);
11849:     }
11850:     if ($savefileas ne '') {
11851:         $fname = $savefileas;
11852:     }
11853:     $fname=&Apache::lonnet::clean_filename($fname);
11854: # See if there is anything left
11855:     unless ($fname) { return ('error: no uploaded file'); }
11856:     $fname="$subdir/$fname";
11857:     my $docroot=$r->dir_config('lonDocRoot');
11858:     my $filepath="$docroot/priv";
11859:     my $relpath = "$dom/$confname";
11860:     my ($fnamepath,$file,$fetchthumb);
11861:     $file=$fname;
11862:     if ($fname=~m|/|) {
11863:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
11864:     }
11865:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
11866:     my $count;
11867:     for ($count=5;$count<=$#parts;$count++) {
11868:         $filepath.="/$parts[$count]";
11869:         if ((-e $filepath)!=1) {
11870:             mkdir($filepath,02770);
11871:         }
11872:     }
11873:     # Check for bad extension and disallow upload
11874:     if ($file=~/\.(\w+)$/ &&
11875:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
11876:         $output = 
11877:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
11878:     } elsif ($file=~/\.(\w+)$/ &&
11879:         !defined(&Apache::loncommon::fileembstyle($1))) {
11880:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
11881:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
11882:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
11883:     } elsif (-d "$filepath/$file") {
11884:         $output = &mt('Filename is a directory name - rename the file and re-upload');
11885:     } else {
11886:         my $source = $filepath.'/'.$file;
11887:         my $logfile;
11888:         if (!open($logfile,">>",$source.'.log')) {
11889:             return (&mt('No write permission to Authoring Space'));
11890:         }
11891:         print $logfile
11892: "\n================= Publish ".localtime()." ================\n".
11893: $env{'user.name'}.':'.$env{'user.domain'}."\n";
11894: # Save the file
11895:         if (!open(FH,">",$source)) {
11896:             &Apache::lonnet::logthis('Failed to create '.$source);
11897:             return (&mt('Failed to create file'));
11898:         }
11899:         if ($action eq 'upload') {
11900:             if (!print FH ($env{'form.'.$formname})) {
11901:                 &Apache::lonnet::logthis('Failed to write to '.$source);
11902:                 return (&mt('Failed to write file'));
11903:             }
11904:         } else {
11905:             my $original = &Apache::lonnet::filelocation('',$formname);
11906:             if(!copy($original,$source)) {
11907:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
11908:                 return (&mt('Failed to write file'));
11909:             }
11910:         }
11911:         close(FH);
11912:         chmod(0660, $source); # Permissions to rw-rw---.
11913: 
11914:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
11915:         my $copyfile=$targetdir.'/'.$file;
11916: 
11917:         my @parts=split(/\//,$targetdir);
11918:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
11919:         for (my $count=5;$count<=$#parts;$count++) {
11920:             $path.="/$parts[$count]";
11921:             if (!-e $path) {
11922:                 print $logfile "\nCreating directory ".$path;
11923:                 mkdir($path,02770);
11924:             }
11925:         }
11926:         my $versionresult;
11927:         if (-e $copyfile) {
11928:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
11929:         } else {
11930:             $versionresult = 'ok';
11931:         }
11932:         if ($versionresult eq 'ok') {
11933:             if (copy($source,$copyfile)) {
11934:                 print $logfile "\nCopied original source to ".$copyfile."\n";
11935:                 $output = 'ok';
11936:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
11937:                 push(@{$modified_urls},[$copyfile,$source]);
11938:                 my $metaoutput = 
11939:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
11940:                 unless ($registered_cleanup) {
11941:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
11942:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
11943:                     $registered_cleanup=1;
11944:                 }
11945:             } else {
11946:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
11947:                 $output = &mt('Failed to copy file to RES space').", $!";
11948:             }
11949:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
11950:                 my $inputfile = $filepath.'/'.$file;
11951:                 my $outfile = $filepath.'/'.'tn-'.$file;
11952:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
11953:                 if ($fullwidth ne '' && $fullheight ne '') { 
11954:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
11955:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
11956:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
11957:                         system({$args[0]} @args);
11958:                         chmod(0660, $filepath.'/tn-'.$file);
11959:                         if (-e $outfile) {
11960:                             my $copyfile=$targetdir.'/tn-'.$file;
11961:                             if (copy($outfile,$copyfile)) {
11962:                                 print $logfile "\nCopied source to ".$copyfile."\n";
11963:                                 my $thumb_metaoutput = 
11964:                                     &write_metadata($dom,$confname,$formname,
11965:                                                     $targetdir,'tn-'.$file,$logfile);
11966:                                 push(@{$modified_urls},[$copyfile,$outfile]);
11967:                                 unless ($registered_cleanup) {
11968:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
11969:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
11970:                                     $registered_cleanup=1;
11971:                                 }
11972:                                 $madethumb = 1;
11973:                             } else {
11974:                                 print $logfile "\nUnable to write ".$copyfile.
11975:                                                ':'.$!."\n";
11976:                             }
11977:                         }
11978:                     }
11979:                 }
11980:             }
11981:         } else {
11982:             $output = $versionresult;
11983:         }
11984:     }
11985:     return ($output,$logourl,$madethumb);
11986: }
11987: 
11988: sub logo_versioning {
11989:     my ($targetdir,$file,$logfile) = @_;
11990:     my $target = $targetdir.'/'.$file;
11991:     my ($maxversion,$fn,$extn,$output);
11992:     $maxversion = 0;
11993:     if ($file =~ /^(.+)\.(\w+)$/) {
11994:         $fn=$1;
11995:         $extn=$2;
11996:     }
11997:     opendir(DIR,$targetdir);
11998:     while (my $filename=readdir(DIR)) {
11999:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
12000:             $maxversion=($1>$maxversion)?$1:$maxversion;
12001:         }
12002:     }
12003:     $maxversion++;
12004:     print $logfile "\nCreating old version ".$maxversion."\n";
12005:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
12006:     if (copy($target,$copyfile)) {
12007:         print $logfile "Copied old target to ".$copyfile."\n";
12008:         $copyfile=$copyfile.'.meta';
12009:         if (copy($target.'.meta',$copyfile)) {
12010:             print $logfile "Copied old target metadata to ".$copyfile."\n";
12011:             $output = 'ok';
12012:         } else {
12013:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
12014:             $output = &mt('Failed to copy old meta').", $!, ";
12015:         }
12016:     } else {
12017:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
12018:         $output = &mt('Failed to copy old target').", $!, ";
12019:     }
12020:     return $output;
12021: }
12022: 
12023: sub write_metadata {
12024:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
12025:     my (%metadatafields,%metadatakeys,$output);
12026:     $metadatafields{'title'}=$formname;
12027:     $metadatafields{'creationdate'}=time;
12028:     $metadatafields{'lastrevisiondate'}=time;
12029:     $metadatafields{'copyright'}='public';
12030:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
12031:                                          $env{'user.domain'};
12032:     $metadatafields{'authorspace'}=$confname.':'.$dom;
12033:     $metadatafields{'domain'}=$dom;
12034:     {
12035:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
12036:         my $mfh;
12037:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
12038:             foreach (sort(keys(%metadatafields))) {
12039:                 unless ($_=~/\./) {
12040:                     my $unikey=$_;
12041:                     $unikey=~/^([A-Za-z]+)/;
12042:                     my $tag=$1;
12043:                     $tag=~tr/A-Z/a-z/;
12044:                     print $mfh "\n\<$tag";
12045:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
12046:                         my $value=$metadatafields{$unikey.'.'.$_};
12047:                         $value=~s/\"/\'\'/g;
12048:                         print $mfh ' '.$_.'="'.$value.'"';
12049:                     }
12050:                     print $mfh '>'.
12051:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
12052:                             .'</'.$tag.'>';
12053:                 }
12054:             }
12055:             $output = 'ok';
12056:             print $logfile "\nWrote metadata";
12057:             close($mfh);
12058:         } else {
12059:             print $logfile "\nFailed to open metadata file";
12060:             $output = &mt('Could not write metadata');
12061:         }
12062:     }
12063:     return $output;
12064: }
12065: 
12066: sub notifysubscribed {
12067:     foreach my $targetsource (@{$modified_urls}){
12068:         next unless (ref($targetsource) eq 'ARRAY');
12069:         my ($target,$source)=@{$targetsource};
12070:         if ($source ne '') {
12071:             if (open(my $logfh,">>",$source.'.log')) {
12072:                 print $logfh "\nCleanup phase: Notifications\n";
12073:                 my @subscribed=&subscribed_hosts($target);
12074:                 foreach my $subhost (@subscribed) {
12075:                     print $logfh "\nNotifying host ".$subhost.':';
12076:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
12077:                     print $logfh $reply;
12078:                 }
12079:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
12080:                 foreach my $subhost (@subscribedmeta) {
12081:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
12082:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
12083:                                                         $subhost);
12084:                     print $logfh $reply;
12085:                 }
12086:                 print $logfh "\n============ Done ============\n";
12087:                 close($logfh);
12088:             }
12089:         }
12090:     }
12091:     return OK;
12092: }
12093: 
12094: sub subscribed_hosts {
12095:     my ($target) = @_;
12096:     my @subscribed;
12097:     if (open(my $fh,"<","$target.subscription")) {
12098:         while (my $subline=<$fh>) {
12099:             if ($subline =~ /^($match_lonid):/) {
12100:                 my $host = $1;
12101:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
12102:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
12103:                         push(@subscribed,$host);
12104:                     }
12105:                 }
12106:             }
12107:         }
12108:     }
12109:     return @subscribed;
12110: }
12111: 
12112: sub check_switchserver {
12113:     my ($dom,$confname) = @_;
12114:     my ($allowed,$switchserver);
12115:     my $home = &Apache::lonnet::homeserver($confname,$dom);
12116:     if ($home eq 'no_host') {
12117:         $home = &Apache::lonnet::domain($dom,'primary');
12118:     }
12119:     my @ids=&Apache::lonnet::current_machine_ids();
12120:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
12121:     if (!$allowed) {
12122: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
12123:     }
12124:     return $switchserver;
12125: }
12126: 
12127: sub modify_quotas {
12128:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
12129:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
12130:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
12131:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
12132:         $validationfieldsref);
12133:     if ($action eq 'quotas') {
12134:         $context = 'tools'; 
12135:     } else {
12136:         $context = $action;
12137:     }
12138:     if ($context eq 'requestcourses') {
12139:         @usertools = ('official','unofficial','community','textbook','placement','lti');
12140:         @options =('norequest','approval','validate','autolimit');
12141:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
12142:         %titles = &courserequest_titles();
12143:         $toolregexp = join('|',@usertools);
12144:         %conditions = &courserequest_conditions();
12145:         $confname = $dom.'-domainconfig';
12146:         my $servadm = $r->dir_config('lonAdmEMail');
12147:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12148:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
12149:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
12150:     } elsif ($context eq 'requestauthor') {
12151:         @usertools = ('author');
12152:         %titles = &authorrequest_titles();
12153:     } else {
12154:         @usertools = ('aboutme','blog','webdav','portfolio');
12155:         %titles = &tool_titles();
12156:     }
12157:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12158:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12159:     foreach my $key (keys(%env)) {
12160:         if ($context eq 'requestcourses') {
12161:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
12162:                 my $item = $1;
12163:                 my $type = $2;
12164:                 if ($type =~ /^limit_(.+)/) {
12165:                     $limithash{$item}{$1} = $env{$key};
12166:                 } else {
12167:                     $confhash{$item}{$type} = $env{$key};
12168:                 }
12169:             }
12170:         } elsif ($context eq 'requestauthor') {
12171:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
12172:                 $confhash{$1} = $env{$key};
12173:             }
12174:         } else {
12175:             if ($key =~ /^form\.quota_(.+)$/) {
12176:                 $confhash{'defaultquota'}{$1} = $env{$key};
12177:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
12178:                 $confhash{'authorquota'}{$1} = $env{$key};
12179:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
12180:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
12181:             }
12182:         }
12183:     }
12184:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
12185:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
12186:         @approvalnotify = sort(@approvalnotify);
12187:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
12188:         my @crstypes = ('official','unofficial','community','textbook','placement','lti');
12189:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
12190:         foreach my $type (@hasuniquecode) {
12191:             if (grep(/^\Q$type\E$/,@crstypes)) {
12192:                 $confhash{'uniquecode'}{$type} = 1;
12193:             }
12194:         }
12195:         my (%newbook,%allpos);
12196:         if ($context eq 'requestcourses') {
12197:             foreach my $type ('textbooks','templates') {
12198:                 @{$allpos{$type}} = (); 
12199:                 my $invalid;
12200:                 if ($type eq 'textbooks') {
12201:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
12202:                 } else {
12203:                     $invalid = &mt('Invalid LON-CAPA course for template');
12204:                 }
12205:                 if ($env{'form.'.$type.'_addbook'}) {
12206:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
12207:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
12208:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
12209:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
12210:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
12211:                         } else {
12212:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
12213:                             my $position = $env{'form.'.$type.'_addbook_pos'};
12214:                             $position =~ s/\D+//g;
12215:                             if ($position ne '') {
12216:                                 $allpos{$type}[$position] = $newbook{$type};
12217:                             }
12218:                         }
12219:                     } else {
12220:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
12221:                     }
12222:                 }
12223:             } 
12224:         }
12225:         if (ref($domconfig{$action}) eq 'HASH') {
12226:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
12227:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
12228:                     $changes{'notify'}{'approval'} = 1;
12229:                 }
12230:             } else {
12231:                 if ($confhash{'notify'}{'approval'}) {
12232:                     $changes{'notify'}{'approval'} = 1;
12233:                 }
12234:             }
12235:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
12236:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
12237:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
12238:                         unless ($confhash{'uniquecode'}{$crstype}) {
12239:                             $changes{'uniquecode'} = 1;
12240:                         }
12241:                     }
12242:                     unless ($changes{'uniquecode'}) {
12243:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
12244:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
12245:                                 $changes{'uniquecode'} = 1;
12246:                             }
12247:                         }
12248:                     }
12249:                } else {
12250:                    $changes{'uniquecode'} = 1;
12251:                }
12252:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
12253:                 $changes{'uniquecode'} = 1;
12254:             }
12255:             if ($context eq 'requestcourses') {
12256:                 foreach my $type ('textbooks','templates') {
12257:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
12258:                         my %deletions;
12259:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
12260:                         if (@todelete) {
12261:                             map { $deletions{$_} = 1; } @todelete;
12262:                         }
12263:                         my %imgdeletions;
12264:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
12265:                         if (@todeleteimages) {
12266:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
12267:                         }
12268:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
12269:                         for (my $i=0; $i<=$maxnum; $i++) {
12270:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
12271:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
12272:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
12273:                                 if ($deletions{$key}) {
12274:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
12275:                                         #FIXME need to obsolete item in RES space
12276:                                     }
12277:                                     next;
12278:                                 } else {
12279:                                     my $newpos = $env{'form.'.$itemid};
12280:                                     $newpos =~ s/\D+//g;
12281:                                     foreach my $item ('subject','title','publisher','author') {
12282:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
12283:                                                  ($type eq 'templates'));
12284:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
12285:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
12286:                                             $changes{$type}{$key} = 1;
12287:                                         }
12288:                                     }
12289:                                     $allpos{$type}[$newpos] = $key;
12290:                                 }
12291:                                 if ($imgdeletions{$key}) {
12292:                                     $changes{$type}{$key} = 1;
12293:                                     #FIXME need to obsolete item in RES space
12294:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
12295:                                     my ($cdom,$cnum) = split(/_/,$key);
12296:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
12297:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
12298:                                     } else {
12299:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
12300:                                                                                       $cdom,$cnum,$type,$configuserok,
12301:                                                                                       $switchserver,$author_ok);
12302:                                         if ($imgurl) {
12303:                                             $confhash{$type}{$key}{'image'} = $imgurl;
12304:                                             $changes{$type}{$key} = 1; 
12305:                                         }
12306:                                         if ($error) {
12307:                                             &Apache::lonnet::logthis($error);
12308:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12309:                                         }
12310:                                     }
12311:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
12312:                                     $confhash{$type}{$key}{'image'} = 
12313:                                         $domconfig{$action}{$type}{$key}{'image'};
12314:                                 }
12315:                             }
12316:                         }
12317:                     }
12318:                 }
12319:             }
12320:         } else {
12321:             if ($confhash{'notify'}{'approval'}) {
12322:                 $changes{'notify'}{'approval'} = 1;
12323:             }
12324:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
12325:                 $changes{'uniquecode'} = 1;
12326:             }
12327:         }
12328:         if ($context eq 'requestcourses') {
12329:             foreach my $type ('textbooks','templates') {
12330:                 if ($newbook{$type}) {
12331:                     $changes{$type}{$newbook{$type}} = 1;
12332:                     foreach my $item ('subject','title','publisher','author') {
12333:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
12334:                                  ($type eq 'template'));
12335:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
12336:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
12337:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
12338:                         }
12339:                     }
12340:                     if ($type eq 'textbooks') {
12341:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
12342:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
12343:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
12344:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
12345:                             } else {
12346:                                 my ($imageurl,$error) =
12347:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
12348:                                                             $configuserok,$switchserver,$author_ok);
12349:                                 if ($imageurl) {
12350:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
12351:                                 }
12352:                                 if ($error) {
12353:                                     &Apache::lonnet::logthis($error);
12354:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12355:                                 }
12356:                             }
12357:                         }
12358:                     }
12359:                 }
12360:                 if (@{$allpos{$type}} > 0) {
12361:                     my $idx = 0;
12362:                     foreach my $item (@{$allpos{$type}}) {
12363:                         if ($item ne '') {
12364:                             $confhash{$type}{$item}{'order'} = $idx;
12365:                             if (ref($domconfig{$action}) eq 'HASH') {
12366:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
12367:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
12368:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
12369:                                             $changes{$type}{$item} = 1;
12370:                                         }
12371:                                     }
12372:                                 }
12373:                             }
12374:                             $idx ++;
12375:                         }
12376:                     }
12377:                 }
12378:             }
12379:             if (ref($validationitemsref) eq 'ARRAY') {
12380:                 foreach my $item (@{$validationitemsref}) {
12381:                     if ($item eq 'fields') {
12382:                         my @changed;
12383:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
12384:                         if (@{$confhash{'validation'}{$item}} > 0) {
12385:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
12386:                         }
12387:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
12388:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
12389:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
12390:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
12391:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
12392:                                 } else {
12393:                                     @changed = @{$confhash{'validation'}{$item}};
12394:                                 }
12395:                             } else {
12396:                                 @changed = @{$confhash{'validation'}{$item}};
12397:                             }
12398:                         } else {
12399:                             @changed = @{$confhash{'validation'}{$item}};
12400:                         }
12401:                         if (@changed) {
12402:                             if ($confhash{'validation'}{$item}) {
12403:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
12404:                             } else {
12405:                                 $changes{'validation'}{$item} = &mt('None');
12406:                             }
12407:                         }
12408:                     } else {
12409:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
12410:                         if ($item eq 'markup') {
12411:                             if ($env{'form.requestcourses_validation_'.$item}) {
12412:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
12413:                             }
12414:                         }
12415:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
12416:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
12417:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
12418:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
12419:                                 }
12420:                             } else {
12421:                                 if ($confhash{'validation'}{$item} ne '') {
12422:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
12423:                                 }
12424:                             }
12425:                         } else {
12426:                             if ($confhash{'validation'}{$item} ne '') {
12427:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
12428:                             }
12429:                         }
12430:                     }
12431:                 }
12432:             }
12433:             if ($env{'form.validationdc'}) {
12434:                 my $newval = $env{'form.validationdc'};
12435:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
12436:                 if (exists($domcoords{$newval})) {
12437:                     $confhash{'validation'}{'dc'} = $newval;
12438:                 }
12439:             }
12440:             if (ref($confhash{'validation'}) eq 'HASH') {
12441:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
12442:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
12443:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
12444:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
12445:                                 if ($confhash{'validation'}{'dc'} eq '') {
12446:                                     $changes{'validation'}{'dc'} = &mt('None');
12447:                                 } else {
12448:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
12449:                                 }
12450:                             }
12451:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
12452:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
12453:                         }
12454:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
12455:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
12456:                     }
12457:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
12458:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
12459:                 }
12460:             } else {
12461:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
12462:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
12463:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
12464:                             $changes{'validation'}{'dc'} = &mt('None');
12465:                         }
12466:                     }
12467:                 }
12468:             }
12469:         }
12470:     } else {
12471:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
12472:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
12473:     }
12474:     foreach my $item (@usertools) {
12475:         foreach my $type (@{$types},'default','_LC_adv') {
12476:             my $unset; 
12477:             if ($context eq 'requestcourses') {
12478:                 $unset = '0';
12479:                 if ($type eq '_LC_adv') {
12480:                     $unset = '';
12481:                 }
12482:                 if ($confhash{$item}{$type} eq 'autolimit') {
12483:                     $confhash{$item}{$type} .= '=';
12484:                     unless ($limithash{$item}{$type} =~ /\D/) {
12485:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
12486:                     }
12487:                 }
12488:             } elsif ($context eq 'requestauthor') {
12489:                 $unset = '0';
12490:                 if ($type eq '_LC_adv') {
12491:                     $unset = '';
12492:                 }
12493:             } else {
12494:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
12495:                     $confhash{$item}{$type} = 1;
12496:                 } else {
12497:                     $confhash{$item}{$type} = 0;
12498:                 }
12499:             }
12500:             if (ref($domconfig{$action}) eq 'HASH') {
12501:                 if ($action eq 'requestauthor') {
12502:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
12503:                         $changes{$type} = 1;
12504:                     }
12505:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
12506:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
12507:                         $changes{$item}{$type} = 1;
12508:                     }
12509:                 } else {
12510:                     if ($context eq 'requestcourses') {
12511:                         if ($confhash{$item}{$type} ne $unset) {
12512:                             $changes{$item}{$type} = 1;
12513:                         }
12514:                     } else {
12515:                         if (!$confhash{$item}{$type}) {
12516:                             $changes{$item}{$type} = 1;
12517:                         }
12518:                     }
12519:                 }
12520:             } else {
12521:                 if ($context eq 'requestcourses') {
12522:                     if ($confhash{$item}{$type} ne $unset) {
12523:                         $changes{$item}{$type} = 1;
12524:                     }
12525:                 } elsif ($context eq 'requestauthor') {
12526:                     if ($confhash{$type} ne $unset) {
12527:                         $changes{$type} = 1;
12528:                     }
12529:                 } else {
12530:                     if (!$confhash{$item}{$type}) {
12531:                         $changes{$item}{$type} = 1;
12532:                     }
12533:                 }
12534:             }
12535:         }
12536:     }
12537:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
12538:         if (ref($domconfig{'quotas'}) eq 'HASH') {
12539:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
12540:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
12541:                     if (exists($confhash{'defaultquota'}{$key})) {
12542:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
12543:                             $changes{'defaultquota'}{$key} = 1;
12544:                         }
12545:                     } else {
12546:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
12547:                     }
12548:                 }
12549:             } else {
12550:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
12551:                     if (exists($confhash{'defaultquota'}{$key})) {
12552:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
12553:                             $changes{'defaultquota'}{$key} = 1;
12554:                         }
12555:                     } else {
12556:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
12557:                     }
12558:                 }
12559:             }
12560:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
12561:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
12562:                     if (exists($confhash{'authorquota'}{$key})) {
12563:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
12564:                             $changes{'authorquota'}{$key} = 1;
12565:                         }
12566:                     } else {
12567:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
12568:                     }
12569:                 }
12570:             }
12571:         }
12572:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
12573:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
12574:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
12575:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
12576:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
12577:                             $changes{'defaultquota'}{$key} = 1;
12578:                         }
12579:                     } else {
12580:                         if (!exists($domconfig{'quotas'}{$key})) {
12581:                             $changes{'defaultquota'}{$key} = 1;
12582:                         }
12583:                     }
12584:                 } else {
12585:                     $changes{'defaultquota'}{$key} = 1;
12586:                 }
12587:             }
12588:         }
12589:         if (ref($confhash{'authorquota'}) eq 'HASH') {
12590:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
12591:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
12592:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
12593:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
12594:                             $changes{'authorquota'}{$key} = 1;
12595:                         }
12596:                     } else {
12597:                         $changes{'authorquota'}{$key} = 1;
12598:                     }
12599:                 } else {
12600:                     $changes{'authorquota'}{$key} = 1;
12601:                 }
12602:             }
12603:         }
12604:     }
12605: 
12606:     if ($context eq 'requestauthor') {
12607:         $domdefaults{'requestauthor'} = \%confhash;
12608:     } else {
12609:         foreach my $key (keys(%confhash)) {
12610:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
12611:                 $domdefaults{$key} = $confhash{$key};
12612:             }
12613:         }
12614:     }
12615: 
12616:     my %quotahash = (
12617:                       $action => { %confhash }
12618:                     );
12619:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
12620:                                              $dom);
12621:     if ($putresult eq 'ok') {
12622:         if (keys(%changes) > 0) {
12623:             my $cachetime = 24*60*60;
12624:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12625:             if (ref($lastactref) eq 'HASH') {
12626:                 $lastactref->{'domdefaults'} = 1;
12627:             }
12628:             $resulttext = &mt('Changes made:').'<ul>';
12629:             unless (($context eq 'requestcourses') ||
12630:                     ($context eq 'requestauthor')) {
12631:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
12632:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
12633:                     foreach my $type (@{$types},'default') {
12634:                         if (defined($changes{'defaultquota'}{$type})) {
12635:                             my $typetitle = $usertypes->{$type};
12636:                             if ($type eq 'default') {
12637:                                 $typetitle = $othertitle;
12638:                             }
12639:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
12640:                         }
12641:                     }
12642:                     $resulttext .= '</ul></li>';
12643:                 }
12644:                 if (ref($changes{'authorquota'}) eq 'HASH') {
12645:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
12646:                     foreach my $type (@{$types},'default') {
12647:                         if (defined($changes{'authorquota'}{$type})) {
12648:                             my $typetitle = $usertypes->{$type};
12649:                             if ($type eq 'default') {
12650:                                 $typetitle = $othertitle;
12651:                             }
12652:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
12653:                         }
12654:                     }
12655:                     $resulttext .= '</ul></li>';
12656:                 }
12657:             }
12658:             my %newenv;
12659:             foreach my $item (@usertools) {
12660:                 my (%haschgs,%inconf);
12661:                 if ($context eq 'requestauthor') {
12662:                     %haschgs = %changes;
12663:                     %inconf = %confhash;
12664:                 } else {
12665:                     if (ref($changes{$item}) eq 'HASH') {
12666:                         %haschgs = %{$changes{$item}};
12667:                     }
12668:                     if (ref($confhash{$item}) eq 'HASH') {
12669:                         %inconf = %{$confhash{$item}};
12670:                     }
12671:                 }
12672:                 if (keys(%haschgs) > 0) {
12673:                     my $newacc = 
12674:                         &Apache::lonnet::usertools_access($env{'user.name'},
12675:                                                           $env{'user.domain'},
12676:                                                           $item,'reload',$context);
12677:                     if (($context eq 'requestcourses') ||
12678:                         ($context eq 'requestauthor')) {
12679:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
12680:                             $newenv{'environment.canrequest.'.$item} = $newacc;
12681:                         }
12682:                     } else {
12683:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
12684:                             $newenv{'environment.availabletools.'.$item} = $newacc;
12685:                         }
12686:                     }
12687:                     unless ($context eq 'requestauthor') {
12688:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
12689:                     }
12690:                     foreach my $type (@{$types},'default','_LC_adv') {
12691:                         if ($haschgs{$type}) {
12692:                             my $typetitle = $usertypes->{$type};
12693:                             if ($type eq 'default') {
12694:                                 $typetitle = $othertitle;
12695:                             } elsif ($type eq '_LC_adv') {
12696:                                 $typetitle = 'LON-CAPA Advanced Users'; 
12697:                             }
12698:                             if ($inconf{$type}) {
12699:                                 if ($context eq 'requestcourses') {
12700:                                     my $cond;
12701:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
12702:                                         if ($1 eq '') {
12703:                                             $cond = &mt('(Automatic processing of any request).');
12704:                                         } else {
12705:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
12706:                                         }
12707:                                     } else { 
12708:                                         $cond = $conditions{$inconf{$type}};
12709:                                     }
12710:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
12711:                                 } elsif ($context eq 'requestauthor') {
12712:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
12713:                                                              $titles{$inconf{$type}},$typetitle);
12714: 
12715:                                 } else {
12716:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
12717:                                 }
12718:                             } else {
12719:                                 if ($type eq '_LC_adv') {
12720:                                     if ($inconf{$type} eq '0') {
12721:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
12722:                                     } else { 
12723:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
12724:                                     }
12725:                                 } else {
12726:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
12727:                                 }
12728:                             }
12729:                         }
12730:                     }
12731:                     unless ($context eq 'requestauthor') {
12732:                         $resulttext .= '</ul></li>';
12733:                     }
12734:                 }
12735:             }
12736:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
12737:                 if (ref($changes{'notify'}) eq 'HASH') {
12738:                     if ($changes{'notify'}{'approval'}) {
12739:                         if (ref($confhash{'notify'}) eq 'HASH') {
12740:                             if ($confhash{'notify'}{'approval'}) {
12741:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
12742:                             } else {
12743:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
12744:                             }
12745:                         }
12746:                     }
12747:                 }
12748:             }
12749:             if ($action eq 'requestcourses') {
12750:                 my @offon = ('off','on');
12751:                 if ($changes{'uniquecode'}) {
12752:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
12753:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
12754:                         $resulttext .= '<li>'.
12755:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
12756:                                        '</li>';
12757:                     } else {
12758:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
12759:                                        '</li>';
12760:                     }
12761:                 }
12762:                 foreach my $type ('textbooks','templates') {
12763:                     if (ref($changes{$type}) eq 'HASH') {
12764:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
12765:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
12766:                             my %coursehash = &Apache::lonnet::coursedescription($key);
12767:                             my $coursetitle = $coursehash{'description'};
12768:                             my $position = $confhash{$type}{$key}{'order'} + 1;
12769:                             $resulttext .= '<li>';
12770:                             foreach my $item ('subject','title','publisher','author') {
12771:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
12772:                                          ($type eq 'templates'));
12773:                                 my $name = $item.':';
12774:                                 $name =~ s/^(\w)/\U$1/;
12775:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
12776:                             }
12777:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
12778:                             if ($type eq 'textbooks') {
12779:                                 if ($confhash{$type}{$key}{'image'}) {
12780:                                     $resulttext .= ' '.&mt('Image: [_1]',
12781:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
12782:                                                    ' alt="Textbook cover" />').'<br />';
12783:                                 }
12784:                             }
12785:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
12786:                         }
12787:                         $resulttext .= '</ul></li>';
12788:                     }
12789:                 }
12790:                 if (ref($changes{'validation'}) eq 'HASH') {
12791:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
12792:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
12793:                         foreach my $item (@{$validationitemsref}) {
12794:                             if (exists($changes{'validation'}{$item})) {
12795:                                 if ($item eq 'markup') {
12796:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
12797:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
12798:                                 } else {
12799:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
12800:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
12801:                                 }
12802:                             }
12803:                         }
12804:                         if (exists($changes{'validation'}{'dc'})) {
12805:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
12806:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
12807:                         }
12808:                     }
12809:                 }
12810:             }
12811:             $resulttext .= '</ul>';
12812:             if (keys(%newenv)) {
12813:                 &Apache::lonnet::appenv(\%newenv);
12814:             }
12815:         } else {
12816:             if ($context eq 'requestcourses') {
12817:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
12818:             } elsif ($context eq 'requestauthor') {
12819:                 $resulttext = &mt('No changes made to rights to request author space.');
12820:             } else {
12821:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
12822:             }
12823:         }
12824:     } else {
12825:         $resulttext = '<span class="LC_error">'.
12826: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12827:     }
12828:     if ($errors) {
12829:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
12830:                        '<ul>'.$errors.'</ul></p>';
12831:     }
12832:     return $resulttext;
12833: }
12834: 
12835: sub process_textbook_image {
12836:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
12837:     my $filename = $env{'form.'.$caller.'.filename'};
12838:     my ($error,$url);
12839:     my ($width,$height) = (50,50);
12840:     if ($configuserok eq 'ok') {
12841:         if ($switchserver) {
12842:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
12843:                          $switchserver);
12844:         } elsif ($author_ok eq 'ok') {
12845:             my ($result,$imageurl) =
12846:                 &publishlogo($r,'upload',$caller,$dom,$confname,
12847:                              "$type/$cdom/$cnum/cover",$width,$height);
12848:             if ($result eq 'ok') {
12849:                 $url = $imageurl;
12850:             } else {
12851:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
12852:             }
12853:         } else {
12854:             $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);
12855:         }
12856:     } else {
12857:         $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);
12858:     }
12859:     return ($url,$error);
12860: }
12861: 
12862: sub modify_ltitools {
12863:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
12864:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12865:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
12866:     my $confname = $dom.'-domainconfig';
12867:     my $servadm = $r->dir_config('lonAdmEMail');
12868:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12869:     my (%posslti,%possfield);
12870:     my @courseroles = ('cc','in','ta','ep','st');
12871:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
12872:     map { $posslti{$_} = 1; } @ltiroles;
12873:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
12874:     map { $possfield{$_} = 1; } @allfields;
12875:     my %lt = &ltitools_names(); 
12876:     if ($env{'form.ltitools_add'}) {
12877:         my $title = $env{'form.ltitools_add_title'};
12878:         $title =~ s/(`)/'/g;
12879:         ($newid,my $error) = &get_ltitools_id($dom,$title);
12880:         if ($newid) {
12881:             my $position = $env{'form.ltitools_add_pos'};
12882:             $position =~ s/\D+//g;
12883:             if ($position ne '') {
12884:                 $allpos[$position] = $newid;
12885:             }
12886:             $changes{$newid} = 1;
12887:             foreach my $item ('title','url','key','secret','lifetime') {
12888:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
12889:                 if ($item eq 'lifetime') {
12890:                     $env{'form.ltitools_add_'.$item} =~ s/[^\d.]//g;
12891:                 }
12892:                 if ($env{'form.ltitools_add_'.$item}) {
12893:                     if (($item eq 'key') || ($item eq 'secret')) {
12894:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
12895:                     } else {
12896:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
12897:                     }
12898:                 }
12899:             }
12900:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
12901:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
12902:             }
12903:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
12904:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
12905:             }
12906:             if ($env{'form.ltitools_add_sigmethod'} eq 'HMAC-SHA256') {
12907:                 $confhash{$newid}{'sigmethod'} = $env{'form.ltitools_add_sigmethod'};
12908:             } else {
12909:                 $confhash{$newid}{'sigmethod'} = 'HMAC-SHA1';
12910:             }
12911:             foreach my $item ('width','height','linktext','explanation') {
12912:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
12913:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
12914:                 if (($item eq 'width') || ($item eq 'height')) {
12915:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
12916:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
12917:                     }
12918:                 } else {
12919:                     if ($env{'form.ltitools_add_'.$item} ne '') {
12920:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item}; 
12921:                     }
12922:                 }
12923:             }
12924:             if ($env{'form.ltitools_add_target'} eq 'window') {
12925:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
12926:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
12927:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
12928:             } else {
12929:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
12930:             }
12931:             foreach my $item ('passback','roster') {
12932:                 if ($env{'form.ltitools_'.$item.'_add'}) {
12933:                     $confhash{$newid}{$item} = 1;
12934:                     if ($env{'form.ltitools_'.$item.'valid_add'} ne '') {
12935:                         my $lifetime = $env{'form.ltitools_'.$item.'valid_add'};
12936:                         $lifetime =~ s/^\s+|\s+$//g;
12937:                         if ($lifetime =~ /^\d+\.?\d*$/) {
12938:                             $confhash{$newid}{$item.'valid'} = $lifetime;
12939:                         }
12940:                     }
12941:                 }
12942:             }
12943:             if ($env{'form.ltitools_add_image.filename'} ne '') {
12944:                 my ($imageurl,$error) =
12945:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
12946:                                             $configuserok,$switchserver,$author_ok);
12947:                 if ($imageurl) {
12948:                     $confhash{$newid}{'image'} = $imageurl;
12949:                 }
12950:                 if ($error) {
12951:                     &Apache::lonnet::logthis($error);
12952:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12953:                 }
12954:             }
12955:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
12956:             foreach my $field (@fields) {
12957:                 if ($possfield{$field}) {
12958:                     if ($field eq 'roles') {
12959:                         foreach my $role (@courseroles) {
12960:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
12961:                             if (($choice ne '') && ($posslti{$choice})) {
12962:                                 $confhash{$newid}{'roles'}{$role} = $choice;
12963:                                 if ($role eq 'cc') {
12964:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
12965:                                 }
12966:                             }
12967:                         }
12968:                     } else {
12969:                         $confhash{$newid}{'fields'}{$field} = 1;
12970:                     }
12971:                 }
12972:             }
12973:             if (ref($confhash{$newid}{'fields'}) eq 'HASH') {
12974:                 if ($confhash{$newid}{'fields'}{'user'}) {
12975:                     if ($env{'form.ltitools_userincdom_add'}) {
12976:                         $confhash{$newid}{'incdom'} = 1;
12977:                     }
12978:                 }
12979:             }
12980:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
12981:             foreach my $item (@courseconfig) {
12982:                 $confhash{$newid}{'crsconf'}{$item} = 1;
12983:             }
12984:             if ($env{'form.ltitools_add_custom'}) {
12985:                 my $name = $env{'form.ltitools_add_custom_name'};
12986:                 my $value = $env{'form.ltitools_add_custom_value'};
12987:                 $value =~ s/(`)/'/g;
12988:                 $name =~ s/(`)/'/g;
12989:                 $confhash{$newid}{'custom'}{$name} = $value;
12990:             }
12991:         } else {
12992:             my $error = &mt('Failed to acquire unique ID for new external tool');   
12993:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12994:         }
12995:     }
12996:     if (ref($domconfig{$action}) eq 'HASH') {
12997:         my %deletions;
12998:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
12999:         if (@todelete) {
13000:             map { $deletions{$_} = 1; } @todelete;
13001:         }
13002:         my %customadds;
13003:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
13004:         if (@newcustom) {
13005:             map { $customadds{$_} = 1; } @newcustom;
13006:         } 
13007:         my %imgdeletions;
13008:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
13009:         if (@todeleteimages) {
13010:             map { $imgdeletions{$_} = 1; } @todeleteimages;
13011:         }
13012:         my $maxnum = $env{'form.ltitools_maxnum'};
13013:         for (my $i=0; $i<=$maxnum; $i++) {
13014:             my $itemid = $env{'form.ltitools_id_'.$i};
13015:             $itemid =~ s/\D+//g;
13016:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
13017:                 if ($deletions{$itemid}) {
13018:                     if ($domconfig{$action}{$itemid}{'image'}) {
13019:                         #FIXME need to obsolete item in RES space
13020:                     }
13021:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
13022:                     next;
13023:                 } else {
13024:                     my $newpos = $env{'form.ltitools_'.$itemid};
13025:                     $newpos =~ s/\D+//g;
13026:                     foreach my $item ('title','url','lifetime') {
13027:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
13028:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
13029:                             $changes{$itemid} = 1;
13030:                         }
13031:                     }
13032:                     foreach my $item ('key','secret') {
13033:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
13034:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
13035:                             $changes{$itemid} = 1;
13036:                         }
13037:                     }
13038:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
13039:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
13040:                     }
13041:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
13042:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
13043:                     }
13044:                     if ($env{'form.ltitools_sigmethod_'.$i} eq 'HMAC-SHA256') {
13045:                         $confhash{$itemid}{'sigmethod'} = $env{'form.ltitools_sigmethod_'.$i};
13046:                     } else {
13047:                         $confhash{$itemid}{'sigmethod'} = 'HMAC-SHA1'; 
13048:                     }
13049:                     if ($domconfig{$action}{$itemid}{'sigmethod'} eq '') {
13050:                         if ($confhash{$itemid}{'sigmethod'} ne 'HMAC-SHA1') {
13051:                             $changes{$itemid} = 1;
13052:                         }
13053:                     } elsif ($domconfig{$action}{$itemid}{'sigmethod'} ne $confhash{$itemid}{'sigmethod'}) {
13054:                         $changes{$itemid} = 1;
13055:                     }
13056:                     foreach my $size ('width','height') {
13057:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
13058:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
13059:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
13060:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
13061:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
13062:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
13063:                                     $changes{$itemid} = 1;
13064:                                 }
13065:                             } else {
13066:                                 $changes{$itemid} = 1;
13067:                             }
13068:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
13069:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
13070:                                 $changes{$itemid} = 1;
13071:                             }
13072:                         }
13073:                     }
13074:                     foreach my $item ('linktext','explanation') {
13075:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
13076:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
13077:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
13078:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
13079:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
13080:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
13081:                                     $changes{$itemid} = 1;
13082:                                 }
13083:                             } else {
13084:                                 $changes{$itemid} = 1;
13085:                             }
13086:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
13087:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
13088:                                 $changes{$itemid} = 1;
13089:                             }
13090:                         }
13091:                     }
13092:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
13093:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
13094:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
13095:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
13096:                     } else {
13097:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
13098:                     }
13099:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
13100:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
13101:                             $changes{$itemid} = 1;
13102:                         }
13103:                     } else {
13104:                         $changes{$itemid} = 1;
13105:                     }
13106:                     foreach my $extra ('passback','roster') {
13107:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
13108:                             $confhash{$itemid}{$extra} = 1;
13109:                             if ($env{'form.ltitools_'.$extra.'valid_'.$i} ne '') {
13110:                                 my $lifetime = $env{'form.ltitools_'.$extra.'valid_'.$i};
13111:                                 $lifetime =~ s/^\s+|\s+$//g;
13112:                                 if ($lifetime =~ /^\d+\.?\d*$/) {
13113:                                     $confhash{$itemid}{$extra.'valid'} = $lifetime;
13114:                                 }
13115:                             }
13116:                         }
13117:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
13118:                             $changes{$itemid} = 1;
13119:                         }
13120:                         if ($domconfig{$action}{$itemid}{$extra.'valid'} ne $confhash{$itemid}{$extra.'valid'}) {
13121:                             $changes{$itemid} = 1;
13122:                         }
13123:                     }
13124:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
13125:                     foreach my $item ('label','title','target','linktext','explanation','append') {
13126:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
13127:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
13128:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
13129:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
13130:                                     $changes{$itemid} = 1;
13131:                                 }
13132:                             } else {
13133:                                 $changes{$itemid} = 1;
13134:                             }
13135:                         }
13136:                     }
13137:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
13138:                     foreach my $field (@fields) {
13139:                         if ($possfield{$field}) {
13140:                             if ($field eq 'roles') {
13141:                                 foreach my $role (@courseroles) {
13142:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
13143:                                     if (($choice ne '') && ($posslti{$choice})) {
13144:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
13145:                                         if ($role eq 'cc') {
13146:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
13147:                                         }
13148:                                     }
13149:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
13150:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
13151:                                             $changes{$itemid} = 1;
13152:                                         }
13153:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
13154:                                         $changes{$itemid} = 1;
13155:                                     }
13156:                                 }
13157:                             } else {
13158:                                 $confhash{$itemid}{'fields'}{$field} = 1;
13159:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
13160:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
13161:                                         $changes{$itemid} = 1;
13162:                                     }
13163:                                 } else {
13164:                                     $changes{$itemid} = 1;
13165:                                 }
13166:                             }
13167:                         }
13168:                     }
13169:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
13170:                         if ($confhash{$itemid}{'fields'}{'user'}) {
13171:                             if ($env{'form.ltitools_userincdom_'.$i}) {
13172:                                 $confhash{$itemid}{'incdom'} = 1;
13173:                             }
13174:                             if ($domconfig{$action}{$itemid}{'incdom'} ne $confhash{$itemid}{'incdom'}) {
13175:                                 $changes{$itemid} = 1;
13176:                             }
13177:                         }
13178:                     }
13179:                     $allpos[$newpos] = $itemid;
13180:                 }
13181:                 if ($imgdeletions{$itemid}) {
13182:                     $changes{$itemid} = 1;
13183:                     #FIXME need to obsolete item in RES space
13184:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
13185:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
13186:                                                                  $itemid,$configuserok,$switchserver,
13187:                                                                  $author_ok);
13188:                     if ($imgurl) {
13189:                         $confhash{$itemid}{'image'} = $imgurl;
13190:                         $changes{$itemid} = 1;
13191:                     }
13192:                     if ($error) {
13193:                         &Apache::lonnet::logthis($error);
13194:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13195:                     }
13196:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
13197:                     $confhash{$itemid}{'image'} =
13198:                        $domconfig{$action}{$itemid}{'image'};
13199:                 }
13200:                 if ($customadds{$i}) {
13201:                     my $name = $env{'form.ltitools_custom_name_'.$i};
13202:                     $name =~ s/(`)/'/g;
13203:                     $name =~ s/^\s+//;
13204:                     $name =~ s/\s+$//;
13205:                     my $value = $env{'form.ltitools_custom_value_'.$i};
13206:                     $value =~ s/(`)/'/g;
13207:                     $value =~ s/^\s+//;
13208:                     $value =~ s/\s+$//;
13209:                     if ($name ne '') {
13210:                         $confhash{$itemid}{'custom'}{$name} = $value;
13211:                         $changes{$itemid} = 1;
13212:                     }
13213:                 }
13214:                 my %customdels;
13215:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
13216:                 if (@customdeletions) {
13217:                     $changes{$itemid} = 1;
13218:                 }
13219:                 map { $customdels{$_} = 1; } @customdeletions;
13220:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
13221:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
13222:                         unless ($customdels{$key}) {
13223:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
13224:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
13225:                             }
13226:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
13227:                                 $changes{$itemid} = 1;
13228:                             }
13229:                         }
13230:                     }
13231:                 }
13232:                 unless ($changes{$itemid}) {
13233:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
13234:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
13235:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
13236:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
13237:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
13238:                                         $changes{$itemid} = 1;
13239:                                         last;
13240:                                     }
13241:                                 }
13242:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
13243:                                 $changes{$itemid} = 1;
13244:                             }
13245:                         }
13246:                         last if ($changes{$itemid});
13247:                     }
13248:                 }
13249:             }
13250:         }
13251:     }
13252:     if (@allpos > 0) {
13253:         my $idx = 0;
13254:         foreach my $itemid (@allpos) {
13255:             if ($itemid ne '') {
13256:                 $confhash{$itemid}{'order'} = $idx;
13257:                 if (ref($domconfig{$action}) eq 'HASH') {
13258:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
13259:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
13260:                             $changes{$itemid} = 1;
13261:                         }
13262:                     }
13263:                 }
13264:                 $idx ++;
13265:             }
13266:         }
13267:     }
13268:     my %ltitoolshash = (
13269:                           $action => { %confhash }
13270:                        );
13271:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
13272:                                              $dom);
13273:     if ($putresult eq 'ok') {
13274:         my %ltienchash = (
13275:                              $action => { %encconfig }
13276:                          );
13277:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
13278:         if (keys(%changes) > 0) {
13279:             my $cachetime = 24*60*60;
13280:             my %ltiall = %confhash;
13281:             foreach my $id (keys(%ltiall)) {
13282:                 if (ref($encconfig{$id}) eq 'HASH') {
13283:                     foreach my $item ('key','secret') {
13284:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
13285:                     }
13286:                 }
13287:             }
13288:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
13289:             if (ref($lastactref) eq 'HASH') {
13290:                 $lastactref->{'ltitools'} = 1;
13291:             }
13292:             $resulttext = &mt('Changes made:').'<ul>';
13293:             my %bynum;
13294:             foreach my $itemid (sort(keys(%changes))) {
13295:                 my $position = $confhash{$itemid}{'order'};
13296:                 $bynum{$position} = $itemid;
13297:             }
13298:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
13299:                 my $itemid = $bynum{$pos}; 
13300:                 if (ref($confhash{$itemid}) ne 'HASH') {
13301:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
13302:                 } else {
13303:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
13304:                     if ($confhash{$itemid}{'image'}) {
13305:                         $resulttext .= '&nbsp;'.
13306:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
13307:                                        ' alt="'.&mt('Tool Provider icon').'" />';
13308:                     }
13309:                     $resulttext .= '</li><ul>';
13310:                     my $position = $pos + 1;
13311:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
13312:                     foreach my $item ('version','msgtype','sigmethod','url','lifetime') {
13313:                         if ($confhash{$itemid}{$item} ne '') {
13314:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
13315:                         }
13316:                     }
13317:                     if ($encconfig{$itemid}{'key'} ne '') {
13318:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
13319:                     }
13320:                     if ($encconfig{$itemid}{'secret'} ne '') {
13321:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
13322:                         my $num = length($encconfig{$itemid}{'secret'});
13323:                         $resulttext .= ('*'x$num).'</li>';
13324:                     }
13325:                     $resulttext .= '<li>'.&mt('Configurable in course:');
13326:                     my @possconfig = ('label','title','target','linktext','explanation','append');
13327:                     my $numconfig = 0; 
13328:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') { 
13329:                         foreach my $item (@possconfig) {
13330:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
13331:                                 $numconfig ++;
13332:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
13333:                             }
13334:                         }
13335:                     }
13336:                     if (!$numconfig) {
13337:                         $resulttext .= '&nbsp;'.&mt('None');
13338:                     }
13339:                     $resulttext .= '</li>';
13340:                     foreach my $item ('passback','roster') {
13341:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
13342:                         if ($confhash{$itemid}{$item}) {
13343:                             $resulttext .= &mt('Yes');
13344:                             if ($confhash{$itemid}{$item.'valid'}) {
13345:                                 if ($item eq 'passback') {
13346:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,day] after launch',
13347:                                                            $confhash{$itemid}{$item.'valid'});
13348:                                 } else {
13349:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,second] after launch',
13350:                                                            $confhash{$itemid}{$item.'valid'});
13351:                                 }
13352:                             }
13353:                         } else {
13354:                             $resulttext .= &mt('No');
13355:                         }
13356:                         $resulttext .= '</li>';
13357:                     }
13358:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
13359:                         my $displaylist;
13360:                         if ($confhash{$itemid}{'display'}{'target'}) {
13361:                             $displaylist = &mt('Display target').':&nbsp;'.
13362:                                            $confhash{$itemid}{'display'}{'target'}.',';
13363:                         }
13364:                         foreach my $size ('width','height') { 
13365:                             if ($confhash{$itemid}{'display'}{$size}) {
13366:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
13367:                                                 $confhash{$itemid}{'display'}{$size}.',';
13368:                             }
13369:                         }
13370:                         if ($displaylist) {
13371:                             $displaylist =~ s/,$//;
13372:                             $resulttext .= '<li>'.$displaylist.'</li>';
13373:                         }
13374:                         foreach my $item ('linktext','explanation') {
13375:                             if ($confhash{$itemid}{'display'}{$item}) {
13376:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
13377:                             }
13378:                         }
13379:                     }
13380:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
13381:                         my $fieldlist;
13382:                         foreach my $field (@allfields) {
13383:                             if ($confhash{$itemid}{'fields'}{$field}) {
13384:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
13385:                             }
13386:                         }
13387:                         if ($fieldlist) {
13388:                             $fieldlist =~ s/,$//;
13389:                             if ($confhash{$itemid}{'fields'}{'user'}) {
13390:                                 if ($confhash{$itemid}{'incdom'}) {
13391:                                     $fieldlist .= ' ('.&mt('username:domain').')';
13392:                                 } else {
13393:                                     $fieldlist .= ' ('.&mt('username').')';
13394:                                 }
13395:                             }
13396:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
13397:                         }
13398:                     }
13399:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
13400:                         my $rolemaps;
13401:                         foreach my $role (@courseroles) {
13402:                             if ($confhash{$itemid}{'roles'}{$role}) {
13403:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
13404:                                              $confhash{$itemid}{'roles'}{$role}.',';
13405:                             }
13406:                         }
13407:                         if ($rolemaps) {
13408:                             $rolemaps =~ s/,$//; 
13409:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
13410:                         }
13411:                     }
13412:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
13413:                         my $customlist;
13414:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
13415:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
13416:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
13417:                             } 
13418:                         }
13419:                         if ($customlist) {
13420:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
13421:                         }
13422:                     } 
13423:                     $resulttext .= '</ul></li>';
13424:                 }
13425:             }
13426:             $resulttext .= '</ul>';
13427:         } else {
13428:             $resulttext = &mt('No changes made.');
13429:         }
13430:     } else {
13431:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
13432:     }
13433:     if ($errors) {
13434:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
13435:                        $errors.'</ul>';
13436:     }
13437:     return $resulttext;
13438: }
13439: 
13440: sub process_ltitools_image {
13441:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
13442:     my $filename = $env{'form.'.$caller.'.filename'};
13443:     my ($error,$url);
13444:     my ($width,$height) = (21,21);
13445:     if ($configuserok eq 'ok') {
13446:         if ($switchserver) {
13447:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
13448:                          $switchserver);
13449:         } elsif ($author_ok eq 'ok') {
13450:             my ($result,$imageurl,$madethumb) =
13451:                 &publishlogo($r,'upload',$caller,$dom,$confname,
13452:                              "ltitools/$itemid/icon",$width,$height);
13453:             if ($result eq 'ok') {
13454:                 if ($madethumb) {
13455:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
13456:                     my $imagethumb = "$path/tn-".$imagefile;
13457:                     $url = $imagethumb;
13458:                 } else {
13459:                     $url = $imageurl;
13460:                 }
13461:             } else {
13462:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
13463:             }
13464:         } else {
13465:             $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);
13466:         }
13467:     } else {
13468:         $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);
13469:     }
13470:     return ($url,$error);
13471: }
13472: 
13473: sub get_ltitools_id {
13474:     my ($cdom,$title) = @_;
13475:     # get lock on ltitools db
13476:     my $lockhash = {
13477:                       lock => $env{'user.name'}.
13478:                               ':'.$env{'user.domain'},
13479:                    };
13480:     my $tries = 0;
13481:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
13482:     my ($id,$error);
13483:  
13484:     while (($gotlock ne 'ok') && ($tries<10)) {
13485:         $tries ++;
13486:         sleep (0.1);
13487:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
13488:     }
13489:     if ($gotlock eq 'ok') {
13490:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
13491:         if ($currids{'lock'}) {
13492:             delete($currids{'lock'});
13493:             if (keys(%currids)) {
13494:                 my @curr = sort { $a <=> $b } keys(%currids);
13495:                 if ($curr[-1] =~ /^\d+$/) {
13496:                     $id = 1 + $curr[-1];
13497:                 }
13498:             } else {
13499:                 $id = 1;
13500:             }
13501:             if ($id) {
13502:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
13503:                     $error = 'nostore';
13504:                 }
13505:             } else {
13506:                 $error = 'nonumber';
13507:             }
13508:         }
13509:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
13510:     } else {
13511:         $error = 'nolock';
13512:     }
13513:     return ($id,$error);
13514: }
13515: 
13516: sub modify_proctoring {
13517:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
13518:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13519:     my (@allpos,%changes,%confhash,%encconfhash,$errors,$resulttext,%imgdeletions);
13520:     my $confname = $dom.'-domainconfig';
13521:     my $servadm = $r->dir_config('lonAdmEMail');
13522:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
13523:     my %providernames = &proctoring_providernames();
13524:     my $maxnum = scalar(keys(%providernames));
13525: 
13526:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
13527:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
13528:     if (ref($requref) eq 'HASH') {
13529:         %requserfields = %{$requref};
13530:     }
13531:     if (ref($opturef) eq 'HASH') {
13532:         %optuserfields = %{$opturef};
13533:     }
13534:     if (ref($defref) eq 'HASH') {
13535:         %defaults = %{$defref};
13536:     }
13537:     if (ref($extref) eq 'HASH') {
13538:         %extended = %{$extref};
13539:     }
13540:     if (ref($crsref) eq 'HASH') {
13541:         %crsconf = %{$crsref};
13542:     }
13543:     if (ref($rolesref) eq 'ARRAY') {
13544:         @courseroles = @{$rolesref};
13545:     }
13546:     if (ref($ltiref) eq 'ARRAY') {
13547:         @ltiroles = @{$ltiref};
13548:     }
13549: 
13550:     if (ref($domconfig{$action}) eq 'HASH') {
13551:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.proctoring_image_del');
13552:         if (@todeleteimages) {
13553:             map { $imgdeletions{$_} = 1; } @todeleteimages;
13554:         }
13555:     }
13556:     my %customadds;
13557:     my @newcustom = &Apache::loncommon::get_env_multiple('form.proctoring_customadd');
13558:     if (@newcustom) {
13559:         map { $customadds{$_} = 1; } @newcustom;
13560:     }
13561:     foreach my $provider (sort(keys(%providernames))) {
13562:         $confhash{$provider} = {};
13563:         my $pos = $env{'form.proctoring_pos_'.$provider};
13564:         $pos =~ s/\D+//g;
13565:         $allpos[$pos] = $provider;
13566:         my (%current,%currentenc);
13567:         my $showroles = 0;
13568:         if (ref($domconfig{$action}) eq 'HASH') {
13569:             if (ref($domconfig{$action}{$provider}) eq 'HASH') {
13570:                 %current = %{$domconfig{$action}{$provider}};
13571:                 foreach my $item ('key','secret') { 
13572:                     $currentenc{$item} = $current{$item};
13573:                     delete($current{$item});
13574:                 }
13575:             }
13576:         }
13577:         if ($env{'form.proctoring_available_'.$provider}) {
13578:             $confhash{$provider}{'available'} = 1;
13579:             unless ($current{'available'}) {
13580:                 $changes{$provider} = 1;
13581:             }
13582:         } else {
13583:             %{$confhash{$provider}} = %current;
13584:             %{$encconfhash{$provider}} = %currentenc;
13585:             $confhash{$provider}{'available'} = 0;
13586:             if ($current{'available'}) {
13587:                 $changes{$provider} = 1;
13588:             }
13589:         }
13590:         if ($confhash{$provider}{'available'}) {
13591:             foreach my $field ('lifetime','version','sigmethod','url','key','secret') {
13592:                 my $possval = $env{'form.proctoring_'.$provider.'_'.$field};
13593:                 if ($field eq 'lifetime') {
13594:                     if ($possval =~ /^\d+$/) {
13595:                         $confhash{$provider}{$field} = $possval;
13596:                     }
13597:                 } elsif ($field eq 'version') {
13598:                     if ($possval =~ /^\d+\.\d+$/) {
13599:                         $confhash{$provider}{$field} = $possval;
13600:                     }
13601:                 } elsif ($field eq 'sigmethod') {
13602:                     if ($possval =~ /^\QHMAC-SHA\E(1|256)$/) {
13603:                         $confhash{$provider}{$field} = $possval;
13604:                     }
13605:                 } elsif ($field eq 'url') {
13606:                     $confhash{$provider}{$field} = $possval;
13607:                 } elsif (($field eq 'key') || ($field eq 'secret')) {
13608:                     $encconfhash{$provider}{$field} = $possval;
13609:                     unless ($currentenc{$field} eq $possval) {
13610:                         $changes{$provider} = 1;
13611:                     }
13612:                 }
13613:                 unless (($field eq 'key') || ($field eq 'secret')) {
13614:                     unless ($current{$field} eq $confhash{$provider}{$field}) {
13615:                         $changes{$provider} = 1;
13616:                     }
13617:                 }
13618:             }
13619:             if ($imgdeletions{$provider}) {
13620:                 $changes{$provider} = 1;
13621:             } elsif ($env{'form.proctoring_image_'.$provider.'.filename'} ne '') {
13622:                 my ($imageurl,$error) =
13623:                     &process_proctoring_image($r,$dom,$confname,'proctoring_image_'.$provider,$provider,
13624:                                               $configuserok,$switchserver,$author_ok);
13625:                 if ($imageurl) {
13626:                     $confhash{$provider}{'image'} = $imageurl;
13627:                     $changes{$provider} = 1; 
13628:                 }
13629:                 if ($error) {
13630:                     &Apache::lonnet::logthis($error);
13631:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13632:                 }
13633:             } elsif (exists($current{'image'})) {
13634:                 $confhash{$provider}{'image'} = $current{'image'};
13635:             }
13636:             if (ref($requserfields{$provider}) eq 'ARRAY') {
13637:                 if (@{$requserfields{$provider}} > 0) {
13638:                     if (grep(/^user$/,@{$requserfields{$provider}})) {
13639:                         if ($env{'form.proctoring_userincdom_'.$provider}) {
13640:                             $confhash{$provider}{'incdom'} = 1;
13641:                         }
13642:                         unless ($current{'incdom'} eq $confhash{$provider}{'incdom'}) {
13643:                             $changes{$provider} = 1;
13644:                         }
13645:                     }
13646:                     if (grep(/^roles$/,@{$requserfields{$provider}})) {
13647:                         $showroles = 1;
13648:                     }
13649:                 }
13650:             }
13651:             $confhash{$provider}{'fields'} = [];
13652:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
13653:                 if (@{$optuserfields{$provider}} > 0) {
13654:                     my @optfields = &Apache::loncommon::get_env_multiple('form.proctoring_optional_'.$provider);
13655:                     foreach my $field (@{$optuserfields{$provider}}) {
13656:                         if (grep(/^\Q$field\E$/,@optfields)) {
13657:                             push(@{$confhash{$provider}{'fields'}},$field);
13658:                         }
13659:                     }
13660:                 }
13661:                 if (ref($current{'fields'}) eq 'ARRAY') {
13662:                     unless ($changes{$provider}) {
13663:                         my @new = sort(@{$confhash{$provider}{'fields'}});
13664:                         my @old = sort(@{$current{'fields'}}); 
13665:                         my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
13666:                         if (@diffs) {
13667:                             $changes{$provider} = 1;
13668:                         }
13669:                     }
13670:                 } elsif (@{$confhash{$provider}{'fields'}}) {
13671:                     $changes{$provider} = 1;
13672:                 }
13673:             }
13674:             if (ref($defaults{$provider}) eq 'ARRAY') {  
13675:                 if (@{$defaults{$provider}} > 0) {
13676:                     my %options;
13677:                     if (ref($extended{$provider}) eq 'HASH') {
13678:                         %options = %{$extended{$provider}};
13679:                     }
13680:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_defaults_'.$provider);
13681:                     foreach my $field (@{$defaults{$provider}}) {
13682:                         if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
13683:                             my $poss = $env{'form.proctoring_defaults_'.$field.'_'.$provider};
13684:                             if (grep(/^\Q$poss\E$/,@{$options{$field}})) {
13685:                                 push(@{$confhash{$provider}{'defaults'}},$poss); 
13686:                             }
13687:                         } elsif ((exists($options{$field})) && (ref($options{$field}) eq 'HASH')) {
13688:                             foreach my $inner (keys(%{$options{$field}})) {
13689:                                 if (ref($options{$field}{$inner}) eq 'ARRAY') {
13690:                                     my $poss = $env{'form.proctoring_'.$inner.'_'.$provider};
13691:                                     if (grep(/^\Q$poss\E$/,@{$options{$field}{$inner}})) {
13692:                                         $confhash{$provider}{'defaults'}{$inner} = $poss;
13693:                                     }
13694:                                 } else {
13695:                                     $confhash{$provider}{'defaults'}{$inner} = $env{'form.proctoring_'.$inner.'_'.$provider};
13696:                                 }
13697:                             }
13698:                         } else {
13699:                             if (grep(/^\Q$field\E$/,@checked)) {
13700:                                 push(@{$confhash{$provider}{'defaults'}},$field);
13701:                             }
13702:                         }
13703:                     }
13704:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
13705:                         if (ref($current{'defaults'}) eq 'ARRAY') {
13706:                             unless ($changes{$provider}) {
13707:                                 my @new = sort(@{$confhash{$provider}{'defaults'}});
13708:                                 my @old = sort(@{$current{'defaults'}});
13709:                                 my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
13710:                                 if (@diffs) {
13711:                                     $changes{$provider} = 1; 
13712:                                 }
13713:                             }
13714:                         } elsif (ref($current{'defaults'}) eq 'ARRAY') {
13715:                             if (@{$current{'defaults'}}) {
13716:                                 $changes{$provider} = 1;
13717:                             }
13718:                         }
13719:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
13720:                         if (ref($current{'defaults'}) eq 'HASH') {
13721:                             unless ($changes{$provider}) {
13722:                                 foreach my $key (keys(%{$confhash{$provider}{'defaults'}})) {
13723:                                     unless ($confhash{$provider}{'defaults'}{$key} eq $current{'defaults'}{$key}) {
13724:                                         $changes{$provider} = 1;
13725:                                         last;
13726:                                     }
13727:                                 }
13728:                             }
13729:                             unless ($changes{$provider}) {
13730:                                 foreach my $key (keys(%{$current{'defaults'}})) {
13731:                                     unless ($current{'defaults'}{$key} eq $confhash{$provider}{'defaults'}{$key}) {
13732:                                         $changes{$provider} = 1;
13733:                                         last;
13734:                                     }
13735:                                 }
13736:                             }
13737:                         } elsif (keys(%{$confhash{$provider}{'defaults'}})) {
13738:                             $changes{$provider} = 1;
13739:                         }
13740:                     }
13741:                 }
13742:             }
13743:             if (ref($crsconf{$provider}) eq 'ARRAY') {
13744:                 if (@{$crsconf{$provider}} > 0) {
13745:                     $confhash{$provider}{'crsconf'} = [];
13746:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_crsconf_'.$provider);
13747:                     foreach my $crsfield (@{$crsconf{$provider}}) {
13748:                         if (grep(/^\Q$crsfield\E$/,@checked)) {
13749:                             push(@{$confhash{$provider}{'crsconf'}},$crsfield);
13750:                         }
13751:                     }
13752:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
13753:                         unless ($changes{$provider}) {  
13754:                             my @new = sort(@{$confhash{$provider}{'crsconf'}});
13755:                             my @old = sort(@{$current{'crsconf'}});
13756:                             my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
13757:                             if (@diffs) {
13758:                                 $changes{$provider} = 1;
13759:                             }
13760:                         }
13761:                     } elsif (@{$confhash{$provider}{'crsconf'}}) {
13762:                         $changes{$provider} = 1;
13763:                     }
13764:                 }
13765:             }
13766:             if ($showroles) {
13767:                 $confhash{$provider}{'roles'} = {};
13768:                 foreach my $role (@courseroles) {
13769:                     my $poss = $env{'form.proctoring_roles_'.$role.'_'.$provider};
13770:                     if (grep(/^\Q$poss\E$/,@ltiroles)) {
13771:                         $confhash{$provider}{'roles'}{$role} = $poss;
13772:                     }
13773:                 }
13774:                 unless ($changes{$provider}) {
13775:                     if (ref($current{'roles'}) eq 'HASH') {
13776:                         foreach my $role (keys(%{$current{'roles'}})) {
13777:                             unless ($current{'roles'}{$role} eq $confhash{$provider}{'roles'}{$role}) {
13778:                                 $changes{$provider} = 1;
13779:                                 last
13780:                             }
13781:                         }
13782:                         unless ($changes{$provider}) {
13783:                             foreach my $role (keys(%{$confhash{$provider}{'roles'}})) {
13784:                                 unless ($confhash{$provider}{'roles'}{$role} eq $current{'roles'}{$role}) {
13785:                                     $changes{$provider} = 1;
13786:                                     last;
13787:                                 }
13788:                             }
13789:                         }
13790:                     } elsif (keys(%{$confhash{$provider}{'roles'}})) {
13791:                         $changes{$provider} = 1;
13792:                     }
13793:                 }
13794:             }
13795:             if (ref($current{'custom'}) eq 'HASH') {
13796:                 my @customdels = &Apache::loncommon::get_env_multiple('form.proctoring_customdel_'.$provider);
13797:                 foreach my $key (keys(%{$current{'custom'}})) {
13798:                     if (grep(/^\Q$key\E$/,@customdels)) {
13799:                         $changes{$provider} = 1;
13800:                     } else {
13801:                         $confhash{$provider}{'custom'}{$key} = $env{'form.proctoring_customval_'.$key.'_'.$provider};
13802:                         if ($confhash{$provider}{'custom'}{$key} ne $current{'custom'}{$key}) {
13803:                             $changes{$provider} = 1;
13804:                         }
13805:                     }
13806:                 }
13807:             }
13808:             if ($customadds{$provider}) {
13809:                 my $name = $env{'form.proctoring_custom_name_'.$provider};
13810:                 $name =~ s/(`)/'/g;
13811:                 $name =~ s/^\s+//;
13812:                 $name =~ s/\s+$//;
13813:                 my $value = $env{'form.proctoring_custom_value_'.$provider};
13814:                 $value =~ s/(`)/'/g;
13815:                 $value =~ s/^\s+//;
13816:                 $value =~ s/\s+$//;
13817:                 if ($name ne '') {
13818:                     $confhash{$provider}{'custom'}{$name} = $value;
13819:                     $changes{$provider} = 1;
13820:                 }
13821:             }
13822:         }
13823:     }
13824:     if (@allpos > 0) {
13825:         my $idx = 0;
13826:         foreach my $provider (@allpos) {
13827:             if ($provider ne '') {
13828:                 $confhash{$provider}{'order'} = $idx;
13829:                 unless ($changes{$provider}) {
13830:                     if (ref($domconfig{$action}) eq 'HASH') {
13831:                         if (ref($domconfig{$action}{$provider}) eq 'HASH') {
13832:                             if ($domconfig{$action}{$provider}{'order'} ne $idx) {
13833:                                 $changes{$provider} = 1;
13834:                             }
13835:                         }
13836:                     }
13837:                 }
13838:                 $idx ++;
13839:             }
13840:         }
13841:     }
13842:     my %proc_hash = (
13843:                           $action => { %confhash }
13844:                        );
13845:     my $putresult = &Apache::lonnet::put_dom('configuration',\%proc_hash,
13846:                                              $dom);
13847:     if ($putresult eq 'ok') {
13848:         my %proc_enchash = (
13849:                              $action => { %encconfhash }
13850:                          );
13851:         &Apache::lonnet::put_dom('encconfig',\%proc_enchash,$dom);
13852:         if (keys(%changes) > 0) {
13853:             my $cachetime = 24*60*60;
13854:             my %procall = %confhash;
13855:             foreach my $provider (keys(%procall)) {
13856:                 if (ref($encconfhash{$provider}) eq 'HASH') {
13857:                     foreach my $key ('key','secret') {
13858:                         $procall{$provider}{$key} = $encconfhash{$provider}{$key};
13859:                     }
13860:                 }
13861:             }
13862:             &Apache::lonnet::do_cache_new('proctoring',$dom,\%procall,$cachetime);
13863:             if (ref($lastactref) eq 'HASH') {
13864:                 $lastactref->{'proctoring'} = 1;
13865:             }
13866:             $resulttext = &mt('Configuration for Provider(s) with changes:').'<ul>';
13867:             my %bynum;
13868:             foreach my $provider (sort(keys(%changes))) {
13869:                 my $position = $confhash{$provider}{'order'};
13870:                 $bynum{$position} = $provider;
13871:             }
13872:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
13873:                 my $provider = $bynum{$pos};
13874:                 my %lt = &proctoring_titles($provider);
13875:                 my %fieldtitles = &proctoring_fieldtitles($provider);
13876:                 if (!$confhash{$provider}{'available'}) {
13877:                     $resulttext .= '<li>'.&mt('Proctoring integration unavailable for: [_1]','<b>'.$providernames{$provider}.'</b>').'</li>';
13878:                 } else {
13879:                     $resulttext .= '<li>'.&mt('Proctoring integration available for: [_1]','<b>'.$providernames{$provider}.'</b>');
13880:                     if ($confhash{$provider}{'image'}) {
13881:                         $resulttext .= '&nbsp;'.
13882:                                        '<img src="'.$confhash{$provider}{'image'}.'"'.
13883:                                        ' alt="'.&mt('Proctoring icon').'" />';
13884:                     }
13885:                     $resulttext .= '<ul>';
13886:                     my $position = $pos + 1;
13887:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
13888:                     foreach my $key ('version','sigmethod','url','lifetime') {
13889:                         if ($confhash{$provider}{$key} ne '') {
13890:                             $resulttext .= '<li>'.$lt{$key}.':&nbsp;'.$confhash{$provider}{$key}.'</li>';
13891:                         }
13892:                     }
13893:                     if ($encconfhash{$provider}{'key'} ne '') {
13894:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfhash{$provider}{'key'}.'</li>';
13895:                     }
13896:                     if ($encconfhash{$provider}{'secret'} ne '') {
13897:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
13898:                         my $num = length($encconfhash{$provider}{'secret'});
13899:                         $resulttext .= ('*'x$num).'</li>';
13900:                     }
13901:                     my (@fields,$showroles);
13902:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
13903:                         push(@fields,@{$requserfields{$provider}});
13904:                     }
13905:                     if (ref($confhash{$provider}{'fields'}) eq 'ARRAY') {
13906:                         push(@fields,@{$confhash{$provider}{'fields'}});
13907:                     } elsif (ref($confhash{$provider}{'fields'}) eq 'HASH') {
13908:                         push(@fields,(keys(%{$confhash{$provider}{'fields'}})));
13909:                     }
13910:                     if (@fields) {
13911:                         if (grep(/^roles$/,@fields)) {
13912:                             $showroles = 1;
13913:                         }
13914:                         $resulttext .= '<li>'.$lt{'udsl'}.':&nbsp;"'.
13915:                                        join('", "', map { $lt{$_}; } @fields).'"</li>';
13916:                     }
13917:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
13918:                         if (grep(/^user$/,@{$requserfields{$provider}})) {
13919:                             if ($confhash{$provider}{'incdom'}) {
13920:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'uname:dom'}).'</li>';
13921:                             } else { 
13922:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'username'}).'</li>';
13923:                             }
13924:                         }
13925:                     }
13926:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
13927:                         if (@{$confhash{$provider}{'defaults'}} > 0) {
13928:                             $resulttext .= '<li>'.$lt{'defa'};
13929:                             foreach my $field (@{$confhash{$provider}{'defaults'}}) {
13930:                                 $resulttext .= ' "'.$fieldtitles{$field}.'",';
13931:                             }
13932:                             $resulttext =~ s/,$//;
13933:                             $resulttext .= '</li>';
13934:                         }
13935:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
13936:                         if (keys(%{$confhash{$provider}{'defaults'}})) {
13937:                             $resulttext .= '<li>'.$lt{'defa'}.':&nbsp;<ul>';
13938:                             foreach my $key (sort(keys(%{$confhash{$provider}{'defaults'}}))) {
13939:                                 if ($confhash{$provider}{'defaults'}{$key} ne '') {
13940:                                     $resulttext .= '<li>'.$fieldtitles{$key}.' = '.$confhash{$provider}{'defaults'}{$key}.'</li>';
13941:                                 }
13942:                             }
13943:                             $resulttext .= '</ul></li>';
13944:                         }
13945:                     }
13946:                     if (ref($crsconf{$provider}) eq 'ARRAY') {
13947:                         if (@{$crsconf{$provider}} > 0) {
13948:                             $resulttext .= '<li>'.&mt('Configurable in course:');
13949:                             my $numconfig = 0;
13950:                             if (ref($confhash{$provider}{'crsconf'}) eq 'ARRAY') {
13951:                                 if (@{$confhash{$provider}{'crsconf'}} > 0) {
13952:                                     foreach my $field (@{$confhash{$provider}{'crsconf'}}) {
13953:                                         $numconfig ++;
13954:                                         if ($provider eq 'examity') {
13955:                                             $resulttext .= ' "'.$lt{'crs'.$field}.'",';
13956:                                         } else {
13957:                                             $resulttext .= ' "'.$fieldtitles{$field}.'",';
13958:                                         }
13959:                                     }
13960:                                     $resulttext =~ s/,$//;
13961:                                 }
13962:                             }
13963:                             if (!$numconfig) {
13964:                                 $resulttext .= '&nbsp;'.&mt('None');
13965:                             }
13966:                             $resulttext .= '</li>';
13967:                         }
13968:                     }
13969:                     if ($showroles) {
13970:                         if (ref($confhash{$provider}{'roles'}) eq 'HASH') {
13971:                             my $rolemaps;
13972:                             foreach my $role (@courseroles) {
13973:                                 if ($confhash{$provider}{'roles'}{$role}) {
13974:                                     $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
13975:                                                  $confhash{$provider}{'roles'}{$role}.',';
13976:                                 }
13977:                             }
13978:                             if ($rolemaps) {
13979:                                 $rolemaps =~ s/,$//;
13980:                                 $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
13981:                             }
13982:                         }
13983:                     }
13984:                     if (ref($confhash{$provider}{'custom'}) eq 'HASH') {
13985:                         my $customlist;
13986:                         if (keys(%{$confhash{$provider}{'custom'}})) {
13987:                             foreach my $key (sort(keys(%{$confhash{$provider}{'custom'}}))) {
13988:                                 $customlist .= $key.'='.$confhash{$provider}{'custom'}{$key}.', ';
13989:                             }
13990:                             $customlist =~ s/,$//;
13991:                         }
13992:                         if ($customlist) {
13993:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
13994:                         }
13995:                     } 
13996:                     $resulttext .= '</ul></li>';
13997:                 }
13998:             }
13999:             $resulttext .= '</ul>';
14000:         } else {
14001:             $resulttext = &mt('No changes made.');
14002:         }
14003:     } else {
14004:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
14005:     }
14006:     if ($errors) {
14007:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
14008:                        $errors.'</ul>';
14009:     }
14010:     return $resulttext;
14011: }
14012: 
14013: sub process_proctoring_image {
14014:     my ($r,$dom,$confname,$caller,$provider,$configuserok,$switchserver,$author_ok) = @_;
14015:     my $filename = $env{'form.'.$caller.'.filename'};
14016:     my ($error,$url);
14017:     my ($width,$height) = (21,21);
14018:     if ($configuserok eq 'ok') {
14019:         if ($switchserver) {
14020:             $error = &mt('Upload of Remote Proctoring Provider icon is not permitted to this server: [_1]',
14021:                          $switchserver);
14022:         } elsif ($author_ok eq 'ok') {
14023:             my ($result,$imageurl,$madethumb) =
14024:                 &publishlogo($r,'upload',$caller,$dom,$confname,
14025:                              "proctoring/$provider/icon",$width,$height);
14026:             if ($result eq 'ok') {
14027:                 if ($madethumb) {
14028:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
14029:                     my $imagethumb = "$path/tn-".$imagefile;
14030:                     $url = $imagethumb;
14031:                 } else {
14032:                     $url = $imageurl;
14033:                 }
14034:             } else {
14035:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
14036:             }
14037:         } else {
14038:             $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);
14039:         }
14040:     } else {
14041:         $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);
14042:     }
14043:     return ($url,$error);
14044: }
14045: 
14046: sub modify_lti {
14047:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14048:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14049:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
14050:     my (%posslti,%posslticrs,%posscrstype);
14051:     my @courseroles = ('cc','in','ta','ep','st');
14052:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
14053:     my @lticourseroles = qw(Instructor TeachingAssistant Mentor Learner);
14054:     my @coursetypes = ('official','unofficial','community','textbook','placement');
14055:     my %coursetypetitles = &Apache::lonlocal::texthash (
14056:                                official   => 'Official',
14057:                                unofficial => 'Unofficial',
14058:                                community  => 'Community',
14059:                                textbook   => 'Textbook',
14060:                                placement  => 'Placement Test',
14061:     );
14062:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
14063:     my %lt = &lti_names();
14064:     map { $posslti{$_} = 1; } @ltiroles;
14065:     map { $posslticrs{$_} = 1; } @lticourseroles;
14066:     map { $posscrstype{$_} = 1; } @coursetypes;
14067: 
14068:     my %menutitles = &ltimenu_titles();
14069: 
14070:     my (@items,%deletions,%itemids);
14071:     if ($env{'form.lti_add'}) {
14072:         my $consumer = $env{'form.lti_consumer_add'};
14073:         $consumer =~ s/(`)/'/g;
14074:         ($newid,my $error) = &get_lti_id($dom,$consumer);
14075:         if ($newid) {
14076:             $itemids{'add'} = $newid;
14077:             push(@items,'add');
14078:             $changes{$newid} = 1;
14079:         } else {
14080:             my $error = &mt('Failed to acquire unique ID for new LTI configuration');
14081:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14082:         }
14083:     }
14084:     if (ref($domconfig{$action}) eq 'HASH') {
14085:         my @todelete = &Apache::loncommon::get_env_multiple('form.lti_del');
14086:         if (@todelete) {
14087:             map { $deletions{$_} = 1; } @todelete;
14088:         }
14089:         my $maxnum = $env{'form.lti_maxnum'};
14090:         for (my $i=0; $i<=$maxnum; $i++) {
14091:             my $itemid = $env{'form.lti_id_'.$i};
14092:             $itemid =~ s/\D+//g;
14093:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
14094:                 if ($deletions{$itemid}) {
14095:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'consumer'};
14096:                 } else {
14097:                    push(@items,$i);
14098:                    $itemids{$i} = $itemid;
14099:                 }
14100:             }
14101:         }
14102:     }
14103:     foreach my $idx (@items) {
14104:         my $itemid = $itemids{$idx};
14105:         next unless ($itemid);
14106:         my $position = $env{'form.lti_pos_'.$idx};
14107:         $position =~ s/\D+//g;
14108:         if ($position ne '') {
14109:             $allpos[$position] = $itemid;
14110:         }
14111:         foreach my $item ('consumer','key','secret','lifetime','requser') {
14112:             my $formitem = 'form.lti_'.$item.'_'.$idx;
14113:             $env{$formitem} =~ s/(`)/'/g;
14114:             if ($item eq 'lifetime') {
14115:                 $env{$formitem} =~ s/[^\d.]//g;
14116:             }
14117:             if ($env{$formitem} ne '') {
14118:                 if (($item eq 'key') || ($item eq 'secret')) {
14119:                     $encconfig{$itemid}{$item} = $env{$formitem};
14120:                 } else {
14121:                     $confhash{$itemid}{$item} = $env{$formitem};
14122:                     unless (($idx eq 'add') || ($changes{$itemid})) {
14123:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
14124:                             $changes{$itemid} = 1;
14125:                         }
14126:                     }
14127:                 }
14128:             }
14129:         }
14130:         if ($env{'form.lti_version_'.$idx} eq 'LTI-1p0') {
14131:             $confhash{$itemid}{'version'} = $env{'form.lti_version_'.$idx};
14132:         }
14133:         if ($confhash{$itemid}{'requser'}) {
14134:             if ($env{'form.lti_mapuser_'.$idx} eq 'sourcedid') {
14135:                 $confhash{$itemid}{'mapuser'} = 'lis_person_sourcedid'; 
14136:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'email') {
14137:                 $confhash{$itemid}{'mapuser'} = 'lis_person_contact_email_primary';
14138:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'other') {
14139:                 my $mapuser = $env{'form.lti_customuser_'.$idx};
14140:                 $mapuser =~ s/(`)/'/g;
14141:                 $mapuser =~ s/^\s+|\s+$//g; 
14142:                 $confhash{$itemid}{'mapuser'} = $mapuser; 
14143:             }
14144:             foreach my $ltirole (@lticourseroles) {
14145:                 my $possrole = $env{'form.lti_maprole_'.$ltirole.'_'.$idx};
14146:                 if (grep(/^\Q$possrole\E$/,@courseroles)) {
14147:                     $confhash{$itemid}{'maproles'}{$ltirole} = $possrole;
14148:                 }
14149:             }
14150:             my @possmakeuser = &Apache::loncommon::get_env_multiple('form.lti_makeuser_'.$idx);
14151:             my @makeuser;
14152:             foreach my $ltirole (sort(@possmakeuser)) {
14153:                 if ($posslti{$ltirole}) {
14154:                     push(@makeuser,$ltirole);
14155:                 }
14156:             }
14157:             $confhash{$itemid}{'makeuser'} = \@makeuser;
14158:             if (@makeuser) {
14159:                 my $lcauth = $env{'form.lti_lcauth_'.$idx};
14160:                 if ($lcauth =~ /^(internal|krb4|krb5|localauth)$/) {
14161:                     $confhash{$itemid}{'lcauth'} = $lcauth;
14162:                     if ($lcauth ne 'internal') {
14163:                         my $lcauthparm = $env{'form.lti_lcauthparm_'.$idx};
14164:                         $lcauthparm =~ s/^(\s+|\s+)$//g;
14165:                         $lcauthparm =~ s/`//g;
14166:                         if ($lcauthparm ne '') {
14167:                             $confhash{$itemid}{'lcauthparm'} = $lcauthparm;
14168:                         }
14169:                     }
14170:                 } else {
14171:                     $confhash{$itemid}{'lcauth'} = 'lti';
14172:                 }
14173:             }
14174:             my @possinstdata =  &Apache::loncommon::get_env_multiple('form.lti_instdata_'.$idx);
14175:             if (@possinstdata) {
14176:                 foreach my $field (@possinstdata) {
14177:                     if (exists($fieldtitles{$field})) {
14178:                         push(@{$confhash{$itemid}{'instdata'}});
14179:                     }
14180:                 }
14181:             }
14182:             if (($env{'form.lti_mapcrs_'.$idx} eq 'course_offering_sourcedid') ||
14183:                 ($env{'form.lti_mapcrs_'.$idx} eq 'context_id'))  {
14184:                 $confhash{$itemid}{'mapcrs'} = $env{'form.lti_mapcrs_'.$idx};
14185:             } elsif ($env{'form.lti_mapcrs_'.$idx} eq 'other') {
14186:                 my $mapcrs = $env{'form.lti_mapcrsfield_'.$idx}; 
14187:                 $mapcrs =~ s/(`)/'/g;
14188:                 $mapcrs =~ s/^\s+|\s+$//g;
14189:                 $confhash{$itemid}{'mapcrs'} = $mapcrs;
14190:             }
14191:             my @posstypes = &Apache::loncommon::get_env_multiple('form.lti_mapcrstype_'.$idx);
14192:             my @crstypes;
14193:             foreach my $type (sort(@posstypes)) {
14194:                 if ($posscrstype{$type}) {
14195:                     push(@crstypes,$type);
14196:                 }
14197:             }
14198:             $confhash{$itemid}{'mapcrstype'} = \@crstypes;
14199:             if ($env{'form.lti_makecrs_'.$idx}) {
14200:                 $confhash{$itemid}{'makecrs'} = 1;
14201:             }
14202:             my @possenroll = &Apache::loncommon::get_env_multiple('form.lti_selfenroll_'.$idx);
14203:             my @selfenroll;
14204:             foreach my $type (sort(@possenroll)) {
14205:                 if ($posslticrs{$type}) {
14206:                     push(@selfenroll,$type);
14207:                 }
14208:             }
14209:             $confhash{$itemid}{'selfenroll'} = \@selfenroll;
14210:             if ($env{'form.lti_crssec_'.$idx}) {
14211:                 if ($env{'form.lti_crssecsrc_'.$idx} eq 'course_section_sourcedid') {
14212:                     $confhash{$itemid}{'section'} = $env{'form.lti_crssecsrc_'.$idx};
14213:                 } elsif ($env{'form.lti_crssecsrc_'.$idx} eq 'other') {
14214:                     my $section = $env{'form.lti_customsection_'.$idx};
14215:                     $section =~ s/(`)/'/g;
14216:                     $section =~ s/^\s+|\s+$//g;
14217:                     if ($section ne '') {
14218:                         $confhash{$itemid}{'section'} = $section;
14219:                     }
14220:                 }
14221:             }
14222:             if ($env{'form.lti_callback_'.$idx}) {
14223:                 if ($env{'form.lti_callbackparam_'.$idx}) {
14224:                     my $callback = $env{'form.lti_callbackparam_'.$idx};
14225:                     $callback =~ s/^\s+|\s+$//g;
14226:                     $confhash{$itemid}{'callback'} = $callback;
14227:                 }
14228:             }
14229:             foreach my $field ('passback','roster','topmenu','inlinemenu') {
14230:                 if ($env{'form.lti_'.$field.'_'.$idx}) {
14231:                     $confhash{$itemid}{$field} = 1;
14232:                 }
14233:             }
14234:             if ($env{'form.lti_passback_'.$idx}) {
14235:                 if ($env{'form.lti_passbackformat_'.$idx} eq '1.0') {
14236:                     $confhash{$itemid}{'passbackformat'} = '1.0';
14237:                 } else {
14238:                     $confhash{$itemid}{'passbackformat'} = '1.1';
14239:                 }
14240:             }
14241:             if ($env{'form.lti_topmenu_'.$idx} || $env{'form.lti_inlinemenu_'.$idx}) {
14242:                 $confhash{$itemid}{lcmenu} = [];
14243:                 my @possmenu = &Apache::loncommon::get_env_multiple('form.lti_menuitem_'.$idx);
14244:                 foreach my $field (@possmenu) {
14245:                     if (exists($menutitles{$field})) {
14246:                         if ($field eq 'grades') {
14247:                             next unless ($env{'form.lti_inlinemenu_'.$idx});
14248:                         }
14249:                         push(@{$confhash{$itemid}{lcmenu}},$field);
14250:                     }
14251:                 }
14252:             }
14253:             unless (($idx eq 'add') || ($changes{$itemid})) {
14254:                 foreach my $field ('mapuser','mapcrs','makecrs','section','passback','roster','lcauth','lcauthparm','topmenu','inlinemenu','callback') {
14255:                     if ($domconfig{$action}{$itemid}{$field} ne $confhash{$itemid}{$field}) {
14256:                         $changes{$itemid} = 1;
14257:                     }
14258:                 }
14259:                 unless ($changes{$itemid}) {
14260:                     if ($domconfig{$action}{$itemid}{'passback'} eq $confhash{$itemid}{'passback'}) {
14261:                         if ($domconfig{$action}{$itemid}{'passbackformat'} ne $confhash{$itemid}{'passbackformat'}) {
14262:                             $changes{$itemid} = 1;
14263:                         }
14264:                     }
14265:                 }
14266:                 foreach my $field ('makeuser','mapcrstype','selfenroll','instdata','lcmenu') {
14267:                     unless ($changes{$itemid}) {
14268:                         if (ref($domconfig{$action}{$itemid}{$field}) eq 'ARRAY') {
14269:                             if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
14270:                                 my @diffs = &Apache::loncommon::compare_arrays($domconfig{$action}{$itemid}{$field},
14271:                                                                                $confhash{$itemid}{$field});
14272:                                 if (@diffs) {
14273:                                     $changes{$itemid} = 1;
14274:                                 }
14275:                             } elsif (@{$domconfig{$action}{$itemid}{$field}} > 0) {
14276:                                 $changes{$itemid} = 1;
14277:                             }
14278:                         } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
14279:                             if (@{$confhash{$itemid}{$field}} > 0) {
14280:                                 $changes{$itemid} = 1;
14281:                             }
14282:                         }
14283:                     }
14284:                 }
14285:                 unless ($changes{$itemid}) {
14286:                     if (ref($domconfig{$action}{$itemid}{'maproles'}) eq 'HASH') {
14287:                         if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
14288:                             foreach my $ltirole (keys(%{$domconfig{$action}{$itemid}{'maproles'}})) {
14289:                                 if ($domconfig{$action}{$itemid}{'maproles'}{$ltirole} ne 
14290:                                     $confhash{$itemid}{'maproles'}{$ltirole}) {
14291:                                     $changes{$itemid} = 1;
14292:                                     last;
14293:                                 }
14294:                             }
14295:                             unless ($changes{$itemid}) {
14296:                                 foreach my $ltirole (keys(%{$confhash{$itemid}{'maproles'}})) {
14297:                                     if ($confhash{$itemid}{'maproles'}{$ltirole} ne 
14298:                                         $domconfig{$action}{$itemid}{'maproles'}{$ltirole}) {
14299:                                         $changes{$itemid} = 1;
14300:                                         last;
14301:                                     }
14302:                                 }
14303:                             }
14304:                         } elsif (keys(%{$domconfig{$action}{$itemid}{'maproles'}}) > 0) {
14305:                             $changes{$itemid} = 1;
14306:                         }
14307:                     } elsif (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
14308:                         unless ($changes{$itemid}) {
14309:                             if (keys(%{$confhash{$itemid}{'maproles'}}) > 0) {
14310:                                 $changes{$itemid} = 1;
14311:                             }
14312:                         }
14313:                     }
14314:                 }
14315:             }
14316:         }
14317:     }
14318:     if (@allpos > 0) {
14319:         my $idx = 0;
14320:         foreach my $itemid (@allpos) {
14321:             if ($itemid ne '') {
14322:                 $confhash{$itemid}{'order'} = $idx;
14323:                 if (ref($domconfig{$action}) eq 'HASH') {
14324:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
14325:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
14326:                             $changes{$itemid} = 1;
14327:                         }
14328:                     }
14329:                 }
14330:                 $idx ++;
14331:             }
14332:         }
14333:     }
14334:     my %ltihash = (
14335:                           $action => { %confhash }
14336:                        );
14337:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltihash,
14338:                                              $dom);
14339:     if ($putresult eq 'ok') {
14340:         my %ltienchash = (
14341:                              $action => { %encconfig }
14342:                          );
14343:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
14344:         if (keys(%changes) > 0) {
14345:             my $cachetime = 24*60*60;
14346:             my %ltiall = %confhash;
14347:             foreach my $id (keys(%ltiall)) {
14348:                 if (ref($encconfig{$id}) eq 'HASH') {
14349:                     foreach my $item ('key','secret') {
14350:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
14351:                     }
14352:                 }
14353:             }
14354:             &Apache::lonnet::do_cache_new('lti',$dom,\%ltiall,$cachetime);
14355:             if (ref($lastactref) eq 'HASH') {
14356:                 $lastactref->{'lti'} = 1;
14357:             }
14358:             $resulttext = &mt('Changes made:').'<ul>';
14359:             my %bynum;
14360:             foreach my $itemid (sort(keys(%changes))) {
14361:                 my $position = $confhash{$itemid}{'order'};
14362:                 $bynum{$position} = $itemid;
14363:             }
14364:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
14365:                 my $itemid = $bynum{$pos};
14366:                 if (ref($confhash{$itemid}) ne 'HASH') {
14367:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
14368:                 } else {
14369:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'consumer'}.'</b></li><ul>';
14370:                     my $position = $pos + 1;
14371:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
14372:                     foreach my $item ('version','lifetime') {
14373:                         if ($confhash{$itemid}{$item} ne '') {
14374:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
14375:                         }
14376:                     }
14377:                     if ($encconfig{$itemid}{'key'} ne '') {
14378:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
14379:                     }
14380:                     if ($encconfig{$itemid}{'secret'} ne '') {
14381:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
14382:                         my $num = length($encconfig{$itemid}{'secret'});
14383:                         $resulttext .= ('*'x$num).'</li>';
14384:                     }
14385:                     if ($confhash{$itemid}{'requser'}) {
14386:                         if ($confhash{$itemid}{'mapuser'}) {
14387:                             my $shownmapuser;
14388:                             if ($confhash{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
14389:                                 $shownmapuser = $lt{'sourcedid'}.' (lis_person_sourcedid)';
14390:                             } elsif ($confhash{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
14391:                                 $shownmapuser = $lt{'email'}.' (lis_person_contact_email_primary)';
14392:                             } else {
14393:                                 $shownmapuser = &mt('Other').' ('.$confhash{$itemid}{'mapuser'}.')';
14394:                             }
14395:                             $resulttext .= '<li>'.&mt('LON-CAPA username').': '.$shownmapuser.'</li>';
14396:                         }
14397:                         if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
14398:                             my $rolemaps;
14399:                             foreach my $role (@ltiroles) {
14400:                                 if ($confhash{$itemid}{'maproles'}{$role}) {
14401:                                     $rolemaps .= ('&nbsp;'x2).$role.'='.
14402:                                                  &Apache::lonnet::plaintext($confhash{$itemid}{'maproles'}{$role},
14403:                                                                             'Course').',';
14404:                                 }
14405:                             }
14406:                             if ($rolemaps) {
14407:                                 $rolemaps =~ s/,$//;
14408:                                 $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
14409:                             }
14410:                         }
14411:                         if (ref($confhash{$itemid}{'makeuser'}) eq 'ARRAY') {
14412:                             if (@{$confhash{$itemid}{'makeuser'}} > 0) { 
14413:                                 $resulttext .= '<li>'.&mt('Following roles may create user accounts: [_1]',
14414:                                                           join(', ',@{$confhash{$itemid}{'makeuser'}})).'<br />';
14415:                                 if ($confhash{$itemid}{'lcauth'} eq 'lti') {
14416:                                     $resulttext .= &mt('New users will only be able to authenticate via LTI').'</li>';
14417:                                 } else {
14418:                                     $resulttext .= &mt('New users will be assigned LON-CAPA authentication: [_1]',
14419:                                                        $confhash{$itemid}{'lcauth'});
14420:                                     if ($confhash{$itemid}{'lcauth'} eq 'internal') {
14421:                                         $resulttext .= '; '.&mt('a randomly generated password will be created');
14422:                                     } elsif ($confhash{$itemid}{'lcauth'} eq 'localauth') {
14423:                                         if ($confhash{$itemid}{'lcauthparm'} ne '') {
14424:                                             $resulttext .= ' '.&mt('with argument: [_1]',$confhash{$itemid}{'lcauthparm'});
14425:                                         }
14426:                                     } else {
14427:                                         $resulttext .= '; '.&mt('Kerberos domain: [_1]',$confhash{$itemid}{'lcauthparm'});
14428:                                     }
14429:                                 }
14430:                                 $resulttext .= '</li>';
14431:                             } else {
14432:                                 $resulttext .= '<li>'.&mt('User account creation not permitted.').'</li>';
14433:                             }
14434:                         }
14435:                         if (ref($confhash{$itemid}{'instdata'}) eq 'ARRAY') {
14436:                             if (@{$confhash{$itemid}{'instdata'}} > 0) {
14437:                                 $resulttext .= '<li>'.&mt('Institutional data will be used when creating a new user for: [_1]',
14438:                                                           join(', ',map { $fieldtitles{$_}; } @{$confhash{$itemid}{'instdata'}})).'</li>';
14439:                             } else {
14440:                                 $resulttext .= '<li>'.&mt('No institutional data used when creating a new user.').'</li>';
14441:                             }
14442:                         }
14443:                         if ($confhash{$itemid}{'mapcrs'}) {
14444:                             $resulttext .= '<li>'.&mt('Unique course identifier').': '.$confhash{$itemid}{'mapcrs'}.'</li>';
14445:                         }
14446:                         if (ref($confhash{$itemid}{'mapcrstype'}) eq 'ARRAY') {
14447:                             if (@{$confhash{$itemid}{'mapcrstype'}} > 0) {
14448:                                 $resulttext .= '<li>'.&mt('Mapping for the following LON-CAPA course types: [_1]',
14449:                                                join(', ',map { $coursetypetitles{$_}; } @coursetypes)).
14450:                                                '</li>';
14451:                             } else {
14452:                                 $resulttext .= '<li>'.&mt('No mapping to LON-CAPA courses').'</li>';
14453:                             }
14454:                         }
14455:                         if ($confhash{$itemid}{'makecrs'}) {
14456:                             $resulttext .= '<li>'.&mt('Instructor may create course (if absent).').'</li>';
14457:                         } else {
14458:                             $resulttext .= '<li>'.&mt('Instructor may not create course (if absent).').'</li>';
14459:                         }
14460:                         if (ref($confhash{$itemid}{'selfenroll'}) eq 'ARRAY') {
14461:                             if (@{$confhash{$itemid}{'selfenroll'}} > 0) {
14462:                                 $resulttext .= '<li>'.&mt('Self-enrollment for following roles: [_1]',
14463:                                                           join(', ',@{$confhash{$itemid}{'selfenroll'}})).
14464:                                                '</li>';
14465:                             } else {
14466:                                 $resulttext .= '<li>'.&mt('Self-enrollment not permitted').'</li>';
14467:                             }
14468:                         }
14469:                         if ($confhash{$itemid}{'section'}) {
14470:                             if ($confhash{$itemid}{'section'} eq 'course_section_sourcedid') {
14471:                                 $resulttext .= '<li>'.&mt('User section from standard field:').
14472:                                                      ' (course_section_sourcedid)'.'</li>';  
14473:                             } else {
14474:                                 $resulttext .= '<li>'.&mt('User section from:').' '.
14475:                                                       $confhash{$itemid}{'section'}.'</li>';
14476:                             }
14477:                         } else {
14478:                             $resulttext .= '<li>'.&mt('No section assignment').'</li>';
14479:                         }
14480:                         if ($confhash{$itemid}{'callback'}) {
14481:                             $resulttext .= '<li>'.&mt('Callback setting').': '.$confhash{$itemid}{'callback'}.'</li>';
14482:                         } else {
14483:                             $resulttext .= '<li>'.&mt('No callback to logout LON-CAPA session when user logs out of Comsumer');
14484:                         }
14485:                         foreach my $item ('passback','roster','topmenu','inlinemenu') {
14486:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
14487:                             if ($confhash{$itemid}{$item}) {
14488:                                 $resulttext .= &mt('Yes');
14489:                                 if ($item eq 'passback') {
14490:                                     if ($confhash{$itemid}{'passbackformat'} eq '1.0') {
14491:                                         $resulttext .= '&nbsp;('.&mt('Outcomes Extension (1.0)').')';
14492:                                     } elsif ($confhash{$itemid}{'passbackformat'} eq '1.1') {
14493:                                         $resulttext .= '&nbsp;('.&mt('Outcomes Service (1.1)').')';
14494:                                     }
14495:                                 }
14496:                             } else {
14497:                                 $resulttext .= &mt('No');
14498:                             }
14499:                             $resulttext .= '</li>';
14500:                         }
14501:                         if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
14502:                             if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
14503:                                 $resulttext .= '<li>'.&mt('Menu items:').' '.
14504:                                                join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>'; 
14505:                             } else {
14506:                                 $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>'; 
14507:                             }
14508:                         }
14509:                     }
14510:                     $resulttext .= '</ul></li>';
14511:                 }
14512:             }
14513:             $resulttext .= '</ul>';
14514:         } else {
14515:             $resulttext = &mt('No changes made.');
14516:         }
14517:     } else {
14518:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
14519:     }
14520:     if ($errors) {
14521:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
14522:                        $errors.'</ul>';
14523:     }
14524:     return $resulttext;
14525: }
14526: 
14527: sub get_lti_id {
14528:     my ($domain,$consumer) = @_;
14529:     # get lock on lti db
14530:     my $lockhash = {
14531:                       lock => $env{'user.name'}.
14532:                               ':'.$env{'user.domain'},
14533:                    };
14534:     my $tries = 0;
14535:     my $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
14536:     my ($id,$error);
14537: 
14538:     while (($gotlock ne 'ok') && ($tries<10)) {
14539:         $tries ++;
14540:         sleep (0.1);
14541:         $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
14542:     }
14543:     if ($gotlock eq 'ok') {
14544:         my %currids = &Apache::lonnet::dump_dom('lti',$domain);
14545:         if ($currids{'lock'}) {
14546:             delete($currids{'lock'});
14547:             if (keys(%currids)) {
14548:                 my @curr = sort { $a <=> $b } keys(%currids);
14549:                 if ($curr[-1] =~ /^\d+$/) {
14550:                     $id = 1 + $curr[-1];
14551:                 }
14552:             } else {
14553:                 $id = 1;
14554:             }
14555:             if ($id) {
14556:                 unless (&Apache::lonnet::newput_dom('lti',{ $id => $consumer },$domain) eq 'ok') {
14557:                     $error = 'nostore';
14558:                 }
14559:             } else {
14560:                 $error = 'nonumber';
14561:             }
14562:         }
14563:         my $dellockoutcome = &Apache::lonnet::del_dom('lti',['lock'],$domain);
14564:     } else {
14565:         $error = 'nolock';
14566:     }
14567:     return ($id,$error);
14568: }
14569: 
14570: sub modify_autoenroll {
14571:     my ($dom,$lastactref,%domconfig) = @_;
14572:     my ($resulttext,%changes);
14573:     my %currautoenroll;
14574:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
14575:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
14576:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
14577:         }
14578:     }
14579:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
14580:     my %title = ( run => 'Auto-enrollment active',
14581:                   sender => 'Sender for notification messages',
14582:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
14583:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
14584:     my @offon = ('off','on');
14585:     my $sender_uname = $env{'form.sender_uname'};
14586:     my $sender_domain = $env{'form.sender_domain'};
14587:     if ($sender_domain eq '') {
14588:         $sender_uname = '';
14589:     } elsif ($sender_uname eq '') {
14590:         $sender_domain = '';
14591:     }
14592:     my $coowners = $env{'form.autoassign_coowners'};
14593:     my $failsafe = $env{'form.autoenroll_failsafe'};
14594:     $failsafe =~ s{^\s+|\s+$}{}g;
14595:     if ($failsafe =~ /\D/) {
14596:         undef($failsafe);
14597:     }
14598:     my %autoenrollhash =  (
14599:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
14600:                                        'sender_uname' => $sender_uname,
14601:                                        'sender_domain' => $sender_domain,
14602:                                        'co-owners' => $coowners,
14603:                                        'autofailsafe' => $failsafe,
14604:                                 }
14605:                      );
14606:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
14607:                                              $dom);
14608:     if ($putresult eq 'ok') {
14609:         if (exists($currautoenroll{'run'})) {
14610:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
14611:                  $changes{'run'} = 1;
14612:              }
14613:         } elsif ($autorun) {
14614:             if ($env{'form.autoenroll_run'} ne '1') {
14615:                  $changes{'run'} = 1;
14616:             }
14617:         }
14618:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
14619:             $changes{'sender'} = 1;
14620:         }
14621:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
14622:             $changes{'sender'} = 1;
14623:         }
14624:         if ($currautoenroll{'co-owners'} ne '') {
14625:             if ($currautoenroll{'co-owners'} ne $coowners) {
14626:                 $changes{'coowners'} = 1;
14627:             }
14628:         } elsif ($coowners) {
14629:             $changes{'coowners'} = 1;
14630:         }
14631:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
14632:             $changes{'autofailsafe'} = 1;
14633:         }
14634:         if (keys(%changes) > 0) {
14635:             $resulttext = &mt('Changes made:').'<ul>';
14636:             if ($changes{'run'}) {
14637:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
14638:             }
14639:             if ($changes{'sender'}) {
14640:                 if ($sender_uname eq '' || $sender_domain eq '') {
14641:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
14642:                 } else {
14643:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
14644:                 }
14645:             }
14646:             if ($changes{'coowners'}) {
14647:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
14648:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
14649:                 if (ref($lastactref) eq 'HASH') {
14650:                     $lastactref->{'domainconfig'} = 1;
14651:                 }
14652:             }
14653:             if ($changes{'autofailsafe'}) {
14654:                 if ($failsafe ne '') {
14655:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
14656:                 } else {
14657:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
14658:                 }
14659:                 &Apache::lonnet::get_domain_defaults($dom,1);
14660:                 if (ref($lastactref) eq 'HASH') {
14661:                     $lastactref->{'domdefaults'} = 1;
14662:                 }
14663:             }
14664:             $resulttext .= '</ul>';
14665:         } else {
14666:             $resulttext = &mt('No changes made to auto-enrollment settings');
14667:         }
14668:     } else {
14669:         $resulttext = '<span class="LC_error">'.
14670: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
14671:     }
14672:     return $resulttext;
14673: }
14674: 
14675: sub modify_autoupdate {
14676:     my ($dom,%domconfig) = @_;
14677:     my ($resulttext,%currautoupdate,%fields,%changes);
14678:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
14679:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
14680:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
14681:         }
14682:     }
14683:     my @offon = ('off','on');
14684:     my %title = &Apache::lonlocal::texthash (
14685:                    run => 'Auto-update:',
14686:                    classlists => 'Updates to user information in classlists?'
14687:                 );
14688:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14689:     my %fieldtitles = &Apache::lonlocal::texthash (
14690:                         id => 'Student/Employee ID',
14691:                         permanentemail => 'E-mail address',
14692:                         lastname => 'Last Name',
14693:                         firstname => 'First Name',
14694:                         middlename => 'Middle Name',
14695:                         generation => 'Generation',
14696:                       );
14697:     $othertitle = &mt('All users');
14698:     if (keys(%{$usertypes}) >  0) {
14699:         $othertitle = &mt('Other users');
14700:     }
14701:     foreach my $key (keys(%env)) {
14702:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
14703:             my ($usertype,$item) = ($1,$2);
14704:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
14705:                 if ($usertype eq 'default') {   
14706:                     push(@{$fields{$1}},$2);
14707:                 } elsif (ref($types) eq 'ARRAY') {
14708:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
14709:                         push(@{$fields{$1}},$2);
14710:                     }
14711:                 }
14712:             }
14713:         }
14714:     }
14715:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
14716:     @lockablenames = sort(@lockablenames);
14717:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
14718:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
14719:         if (@changed) {
14720:             $changes{'lockablenames'} = 1;
14721:         }
14722:     } else {
14723:         if (@lockablenames) {
14724:             $changes{'lockablenames'} = 1;
14725:         }
14726:     }
14727:     my %updatehash = (
14728:                       autoupdate => { run => $env{'form.autoupdate_run'},
14729:                                       classlists => $env{'form.classlists'},
14730:                                       fields => {%fields},
14731:                                       lockablenames => \@lockablenames,
14732:                                     }
14733:                      );
14734:     foreach my $key (keys(%currautoupdate)) {
14735:         if (($key eq 'run') || ($key eq 'classlists')) {
14736:             if (exists($updatehash{autoupdate}{$key})) {
14737:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
14738:                     $changes{$key} = 1;
14739:                 }
14740:             }
14741:         } elsif ($key eq 'fields') {
14742:             if (ref($currautoupdate{$key}) eq 'HASH') {
14743:                 foreach my $item (@{$types},'default') {
14744:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
14745:                         my $change = 0;
14746:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
14747:                             if (!exists($fields{$item})) {
14748:                                 $change = 1;
14749:                                 last;
14750:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
14751:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
14752:                                     $change = 1;
14753:                                     last;
14754:                                 }
14755:                             }
14756:                         }
14757:                         if ($change) {
14758:                             push(@{$changes{$key}},$item);
14759:                         }
14760:                     } 
14761:                 }
14762:             }
14763:         } elsif ($key eq 'lockablenames') {
14764:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
14765:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
14766:                 if (@changed) {
14767:                     $changes{'lockablenames'} = 1;
14768:                 }
14769:             } else {
14770:                 if (@lockablenames) {
14771:                     $changes{'lockablenames'} = 1;
14772:                 }
14773:             }
14774:         }
14775:     }
14776:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
14777:         if (@lockablenames) {
14778:             $changes{'lockablenames'} = 1;
14779:         }
14780:     }
14781:     foreach my $item (@{$types},'default') {
14782:         if (defined($fields{$item})) {
14783:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
14784:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
14785:                     my $change = 0;
14786:                     if (ref($fields{$item}) eq 'ARRAY') {
14787:                         foreach my $type (@{$fields{$item}}) {
14788:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
14789:                                 $change = 1;
14790:                                 last;
14791:                             }
14792:                         }
14793:                     }
14794:                     if ($change) {
14795:                         push(@{$changes{'fields'}},$item);
14796:                     }
14797:                 } else {
14798:                     push(@{$changes{'fields'}},$item);
14799:                 }
14800:             } else {
14801:                 push(@{$changes{'fields'}},$item);
14802:             }
14803:         }
14804:     }
14805:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
14806:                                              $dom);
14807:     if ($putresult eq 'ok') {
14808:         if (keys(%changes) > 0) {
14809:             $resulttext = &mt('Changes made:').'<ul>';
14810:             foreach my $key (sort(keys(%changes))) {
14811:                 if ($key eq 'lockablenames') {
14812:                     $resulttext .= '<li>';
14813:                     if (@lockablenames) {
14814:                         $usertypes->{'default'} = $othertitle;
14815:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
14816:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
14817:                     } else {
14818:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
14819:                     }
14820:                     $resulttext .= '</li>';
14821:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
14822:                     foreach my $item (@{$changes{$key}}) {
14823:                         my @newvalues;
14824:                         foreach my $type (@{$fields{$item}}) {
14825:                             push(@newvalues,$fieldtitles{$type});
14826:                         }
14827:                         my $newvaluestr;
14828:                         if (@newvalues > 0) {
14829:                             $newvaluestr = join(', ',@newvalues);
14830:                         } else {
14831:                             $newvaluestr = &mt('none');
14832:                         }
14833:                         if ($item eq 'default') {
14834:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
14835:                         } else {
14836:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
14837:                         }
14838:                     }
14839:                 } else {
14840:                     my $newvalue;
14841:                     if ($key eq 'run') {
14842:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
14843:                     } else {
14844:                         $newvalue = $offon[$env{'form.'.$key}];
14845:                     }
14846:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
14847:                 }
14848:             }
14849:             $resulttext .= '</ul>';
14850:         } else {
14851:             $resulttext = &mt('No changes made to autoupdates');
14852:         }
14853:     } else {
14854:         $resulttext = '<span class="LC_error">'.
14855: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
14856:     }
14857:     return $resulttext;
14858: }
14859: 
14860: sub modify_autocreate {
14861:     my ($dom,%domconfig) = @_;
14862:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
14863:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
14864:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
14865:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
14866:         }
14867:     }
14868:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
14869:                  req => 'Auto-creation of validated requests for official courses',
14870:                  xmldc => 'Identity of course creator of courses from XML files',
14871:                );
14872:     my @types = ('xml','req');
14873:     foreach my $item (@types) {
14874:         $newvals{$item} = $env{'form.autocreate_'.$item};
14875:         $newvals{$item} =~ s/\D//g;
14876:         $newvals{$item} = 0 if ($newvals{$item} eq '');
14877:     }
14878:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
14879:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
14880:     unless (exists($domcoords{$newvals{'xmldc'}})) {
14881:         $newvals{'xmldc'} = '';
14882:     } 
14883:     %autocreatehash =  (
14884:                         autocreate => { xml => $newvals{'xml'},
14885:                                         req => $newvals{'req'},
14886:                                       }
14887:                        );
14888:     if ($newvals{'xmldc'} ne '') {
14889:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
14890:     }
14891:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
14892:                                              $dom);
14893:     if ($putresult eq 'ok') {
14894:         my @items = @types;
14895:         if ($newvals{'xml'}) {
14896:             push(@items,'xmldc');
14897:         }
14898:         foreach my $item (@items) {
14899:             if (exists($currautocreate{$item})) {
14900:                 if ($currautocreate{$item} ne $newvals{$item}) {
14901:                     $changes{$item} = 1;
14902:                 }
14903:             } elsif ($newvals{$item}) {
14904:                 $changes{$item} = 1;
14905:             }
14906:         }
14907:         if (keys(%changes) > 0) {
14908:             my @offon = ('off','on'); 
14909:             $resulttext = &mt('Changes made:').'<ul>';
14910:             foreach my $item (@types) {
14911:                 if ($changes{$item}) {
14912:                     my $newtxt = $offon[$newvals{$item}];
14913:                     $resulttext .= '<li>'.
14914:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
14915:                                        '<b>','</b>').
14916:                                    '</li>';
14917:                 }
14918:             }
14919:             if ($changes{'xmldc'}) {
14920:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
14921:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
14922:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
14923:             }
14924:             $resulttext .= '</ul>';
14925:         } else {
14926:             $resulttext = &mt('No changes made to auto-creation settings');
14927:         }
14928:     } else {
14929:         $resulttext = '<span class="LC_error">'.
14930:             &mt('An error occurred: [_1]',$putresult).'</span>';
14931:     }
14932:     return $resulttext;
14933: }
14934: 
14935: sub modify_directorysrch {
14936:     my ($dom,$lastactref,%domconfig) = @_;
14937:     my ($resulttext,%changes);
14938:     my %currdirsrch;
14939:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
14940:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
14941:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
14942:         }
14943:     }
14944:     my %title = ( available => 'Institutional directory search available',
14945:                   localonly => 'Other domains can search institution',
14946:                   lcavailable => 'LON-CAPA directory search available',
14947:                   lclocalonly => 'Other domains can search LON-CAPA domain',
14948:                   searchby => 'Search types',
14949:                   searchtypes => 'Search latitude');
14950:     my @offon = ('off','on');
14951:     my @otherdoms = ('Yes','No');
14952: 
14953:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
14954:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
14955:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
14956: 
14957:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14958:     if (keys(%{$usertypes}) == 0) {
14959:         @cansearch = ('default');
14960:     } else {
14961:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
14962:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
14963:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
14964:                     push(@{$changes{'cansearch'}},$type);
14965:                 }
14966:             }
14967:             foreach my $type (@cansearch) {
14968:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
14969:                     push(@{$changes{'cansearch'}},$type);
14970:                 }
14971:             }
14972:         } else {
14973:             push(@{$changes{'cansearch'}},@cansearch);
14974:         }
14975:     }
14976: 
14977:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
14978:         foreach my $by (@{$currdirsrch{'searchby'}}) {
14979:             if (!grep(/^\Q$by\E$/,@searchby)) {
14980:                 push(@{$changes{'searchby'}},$by);
14981:             }
14982:         }
14983:         foreach my $by (@searchby) {
14984:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
14985:                 push(@{$changes{'searchby'}},$by);
14986:             }
14987:         }
14988:     } else {
14989:         push(@{$changes{'searchby'}},@searchby);
14990:     }
14991: 
14992:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
14993:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
14994:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
14995:                 push(@{$changes{'searchtypes'}},$type);
14996:             }
14997:         }
14998:         foreach my $type (@searchtypes) {
14999:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
15000:                 push(@{$changes{'searchtypes'}},$type);
15001:             }
15002:         }
15003:     } else {
15004:         if (exists($currdirsrch{'searchtypes'})) {
15005:             foreach my $type (@searchtypes) {  
15006:                 if ($type ne $currdirsrch{'searchtypes'}) { 
15007:                     push(@{$changes{'searchtypes'}},$type);
15008:                 }
15009:             }
15010:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
15011:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
15012:             }   
15013:         } else {
15014:             push(@{$changes{'searchtypes'}},@searchtypes); 
15015:         }
15016:     }
15017: 
15018:     my %dirsrch_hash =  (
15019:             directorysrch => { available => $env{'form.dirsrch_available'},
15020:                                cansearch => \@cansearch,
15021:                                localonly => $env{'form.dirsrch_instlocalonly'},
15022:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
15023:                                lcavailable => $env{'form.dirsrch_domavailable'},
15024:                                searchby => \@searchby,
15025:                                searchtypes => \@searchtypes,
15026:                              }
15027:             );
15028:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
15029:                                              $dom);
15030:     if ($putresult eq 'ok') {
15031:         if (exists($currdirsrch{'available'})) {
15032:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
15033:                  $changes{'available'} = 1;
15034:              }
15035:         } else {
15036:             if ($env{'form.dirsrch_available'} eq '1') {
15037:                 $changes{'available'} = 1;
15038:             }
15039:         }
15040:         if (exists($currdirsrch{'lcavailable'})) {
15041:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
15042:                 $changes{'lcavailable'} = 1;
15043:             }
15044:         } else {
15045:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
15046:                 $changes{'lcavailable'} = 1;
15047:             }
15048:         }
15049:         if (exists($currdirsrch{'localonly'})) {
15050:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
15051:                 $changes{'localonly'} = 1;
15052:             }
15053:         } else {
15054:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
15055:                 $changes{'localonly'} = 1;
15056:             }
15057:         }
15058:         if (exists($currdirsrch{'lclocalonly'})) {
15059:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
15060:                 $changes{'lclocalonly'} = 1;
15061:             }
15062:         } else {
15063:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
15064:                 $changes{'lclocalonly'} = 1;
15065:             }
15066:         }
15067:         if (keys(%changes) > 0) {
15068:             $resulttext = &mt('Changes made:').'<ul>';
15069:             if ($changes{'available'}) {
15070:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
15071:             }
15072:             if ($changes{'lcavailable'}) {
15073:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
15074:             }
15075:             if ($changes{'localonly'}) {
15076:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
15077:             }
15078:             if ($changes{'lclocalonly'}) {
15079:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
15080:             }
15081:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
15082:                 my $chgtext;
15083:                 if (ref($usertypes) eq 'HASH') {
15084:                     if (keys(%{$usertypes}) > 0) {
15085:                         foreach my $type (@{$types}) {
15086:                             if (grep(/^\Q$type\E$/,@cansearch)) {
15087:                                 $chgtext .= $usertypes->{$type}.'; ';
15088:                             }
15089:                         }
15090:                         if (grep(/^default$/,@cansearch)) {
15091:                             $chgtext .= $othertitle;
15092:                         } else {
15093:                             $chgtext =~ s/\; $//;
15094:                         }
15095:                         $resulttext .=
15096:                             '<li>'.
15097:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
15098:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
15099:                             '</li>';
15100:                     }
15101:                 }
15102:             }
15103:             if (ref($changes{'searchby'}) eq 'ARRAY') {
15104:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
15105:                 my $chgtext;
15106:                 foreach my $type (@{$titleorder}) {
15107:                     if (grep(/^\Q$type\E$/,@searchby)) {
15108:                         if (defined($searchtitles->{$type})) {
15109:                             $chgtext .= $searchtitles->{$type}.'; ';
15110:                         }
15111:                     }
15112:                 }
15113:                 $chgtext =~ s/\; $//;
15114:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
15115:             }
15116:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
15117:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
15118:                 my $chgtext;
15119:                 foreach my $type (@{$srchtypeorder}) {
15120:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
15121:                         if (defined($srchtypes_desc->{$type})) {
15122:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
15123:                         }
15124:                     }
15125:                 }
15126:                 $chgtext =~ s/\; $//;
15127:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
15128:             }
15129:             $resulttext .= '</ul>';
15130:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
15131:             if (ref($lastactref) eq 'HASH') {
15132:                 $lastactref->{'directorysrch'} = 1;
15133:             }
15134:         } else {
15135:             $resulttext = &mt('No changes made to directory search settings');
15136:         }
15137:     } else {
15138:         $resulttext = '<span class="LC_error">'.
15139:                       &mt('An error occurred: [_1]',$putresult).'</span>';
15140:     }
15141:     return $resulttext;
15142: }
15143: 
15144: sub modify_contacts {
15145:     my ($dom,$lastactref,%domconfig) = @_;
15146:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
15147:     if (ref($domconfig{'contacts'}) eq 'HASH') {
15148:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
15149:             $currsetting{$key} = $domconfig{'contacts'}{$key};
15150:         }
15151:     }
15152:     my (%others,%to,%bcc,%includestr,%includeloc);
15153:     my @contacts = ('supportemail','adminemail');
15154:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
15155:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
15156:     my @toggles = ('reporterrors','reportupdates','reportstatus');
15157:     my @lonstatus = ('threshold','sysmail','weights','excluded');
15158:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
15159:     foreach my $type (@mailings) {
15160:         @{$newsetting{$type}} = 
15161:             &Apache::loncommon::get_env_multiple('form.'.$type);
15162:         foreach my $item (@contacts) {
15163:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
15164:                 $contacts_hash{contacts}{$type}{$item} = 1;
15165:             } else {
15166:                 $contacts_hash{contacts}{$type}{$item} = 0;
15167:             }
15168:         }
15169:         $others{$type} = $env{'form.'.$type.'_others'};
15170:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
15171:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15172:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
15173:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
15174:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
15175:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
15176:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
15177:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
15178:             }
15179:         }
15180:     }
15181:     foreach my $item (@contacts) {
15182:         $to{$item} = $env{'form.'.$item};
15183:         $contacts_hash{'contacts'}{$item} = $to{$item};
15184:     }
15185:     foreach my $item (@toggles) {
15186:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
15187:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
15188:         }
15189:     }
15190:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
15191:     foreach my $item (@lonstatus) {
15192:         if ($item eq 'excluded') {
15193:             my (%serverhomes,@excluded);
15194:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
15195:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
15196:             if (@possexcluded) {
15197:                 foreach my $id (sort(@possexcluded)) {
15198:                     if ($serverhomes{$id}) {
15199:                         push(@excluded,$id);
15200:                     }
15201:                 }
15202:             }
15203:             if (@excluded) {
15204:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
15205:             }
15206:         } elsif ($item eq 'weights') {
15207:             foreach my $type ('E','W','N') {
15208:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
15209:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
15210:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
15211:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
15212:                             $env{'form.error'.$item.'_'.$type};
15213:                     }
15214:                 }
15215:             }
15216:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
15217:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
15218:             if ($env{'form.error'.$item} =~ /^\d+$/) {
15219:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
15220:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
15221:                 }
15222:             }
15223:         }
15224:     }
15225:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
15226:         foreach my $field (@{$fields}) {
15227:             if (ref($possoptions->{$field}) eq 'ARRAY') {
15228:                 my $value = $env{'form.helpform_'.$field};
15229:                 $value =~ s/^\s+|\s+$//g;
15230:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
15231:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
15232:                     if ($field eq 'screenshot') {
15233:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
15234:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
15235:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
15236:                         }
15237:                     }
15238:                 }
15239:             }
15240:         }
15241:     }
15242:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
15243:     my (@statuses,%usertypeshash,@overrides);
15244:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
15245:         @statuses = @{$types};
15246:         if (ref($usertypes) eq 'HASH') {
15247:             %usertypeshash = %{$usertypes};
15248:         }
15249:     }
15250:     if (@statuses) {
15251:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
15252:         foreach my $type (@possoverrides) {
15253:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
15254:                 push(@overrides,$type);
15255:             }
15256:         }
15257:         if (@overrides) {
15258:             foreach my $type (@overrides) {
15259:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
15260:                 foreach my $item (@contacts) {
15261:                     if (grep(/^\Q$item\E$/,@standard)) {
15262:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
15263:                         $newsetting{'override_'.$type}{$item} = 1;
15264:                     } else {
15265:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
15266:                         $newsetting{'override_'.$type}{$item} = 0;
15267:                     }
15268:                 }
15269:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
15270:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
15271:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
15272:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
15273:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
15274:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
15275:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
15276:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
15277:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
15278:                 }
15279:             }     
15280:         }
15281:     }
15282:     if (keys(%currsetting) > 0) {
15283:         foreach my $item (@contacts) {
15284:             if ($to{$item} ne $currsetting{$item}) {
15285:                 $changes{$item} = 1;
15286:             }
15287:         }
15288:         foreach my $type (@mailings) {
15289:             foreach my $item (@contacts) {
15290:                 if (ref($currsetting{$type}) eq 'HASH') {
15291:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
15292:                         push(@{$changes{$type}},$item);
15293:                     }
15294:                 } else {
15295:                     push(@{$changes{$type}},@{$newsetting{$type}});
15296:                 }
15297:             }
15298:             if ($others{$type} ne $currsetting{$type}{'others'}) {
15299:                 push(@{$changes{$type}},'others');
15300:             }
15301:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15302:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
15303:                     push(@{$changes{$type}},'bcc'); 
15304:                 }
15305:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
15306:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
15307:                     push(@{$changes{$type}},'include');
15308:                 }
15309:             }
15310:         }
15311:         if (ref($fields) eq 'ARRAY') {
15312:             if (ref($currsetting{'helpform'}) eq 'HASH') {
15313:                 foreach my $field (@{$fields}) {
15314:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
15315:                         push(@{$changes{'helpform'}},$field);
15316:                     }
15317:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
15318:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
15319:                             push(@{$changes{'helpform'}},'maxsize');
15320:                         }
15321:                     }
15322:                 }
15323:             } else {
15324:                 foreach my $field (@{$fields}) {
15325:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
15326:                         push(@{$changes{'helpform'}},$field);
15327:                     }
15328:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
15329:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
15330:                             push(@{$changes{'helpform'}},'maxsize');
15331:                         }
15332:                     }
15333:                 }
15334:             }
15335:         }
15336:         if (@statuses) {
15337:             if (ref($currsetting{'overrides'}) eq 'HASH') { 
15338:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
15339:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
15340:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
15341:                             foreach my $item (@contacts,'bcc','others','include') {
15342:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) { 
15343:                                     push(@{$changes{'overrides'}},$key);
15344:                                     last;
15345:                                 }
15346:                             }
15347:                         } else {
15348:                             push(@{$changes{'overrides'}},$key);
15349:                         }
15350:                     }
15351:                 }
15352:                 foreach my $key (@overrides) {
15353:                     unless (exists($currsetting{'overrides'}{$key})) {
15354:                         push(@{$changes{'overrides'}},$key);
15355:                     }
15356:                 }
15357:             } else {
15358:                 foreach my $key (@overrides) {
15359:                     push(@{$changes{'overrides'}},$key); 
15360:                 }
15361:             }
15362:         }
15363:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
15364:             foreach my $key ('excluded','weights','threshold','sysmail') {
15365:                 if ($key eq 'excluded') {
15366:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
15367:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
15368:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
15369:                             (@{$currsetting{'lonstatus'}{$key}})) {
15370:                             my @diffs =
15371:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
15372:                                                                    $currsetting{'lonstatus'}{$key});
15373:                             if (@diffs) {
15374:                                 push(@{$changes{'lonstatus'}},$key);
15375:                             }
15376:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
15377:                             push(@{$changes{'lonstatus'}},$key);
15378:                         }
15379:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
15380:                              (@{$currsetting{'lonstatus'}{$key}})) {
15381:                         push(@{$changes{'lonstatus'}},$key);
15382:                     }
15383:                 } elsif ($key eq 'weights') {
15384:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
15385:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
15386:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
15387:                             foreach my $type ('E','W','N','U') {
15388:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
15389:                                         $currsetting{'lonstatus'}{$key}{$type}) {
15390:                                     push(@{$changes{'lonstatus'}},$key);
15391:                                     last;
15392:                                 }
15393:                             }
15394:                         } else {
15395:                             foreach my $type ('E','W','N','U') {
15396:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
15397:                                     push(@{$changes{'lonstatus'}},$key);
15398:                                     last;
15399:                                 }
15400:                             }
15401:                         }
15402:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
15403:                         foreach my $type ('E','W','N','U') {
15404:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
15405:                                 push(@{$changes{'lonstatus'}},$key);
15406:                                 last;
15407:                             }
15408:                         }
15409:                     }
15410:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
15411:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
15412:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
15413:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
15414:                                 push(@{$changes{'lonstatus'}},$key);
15415:                             }
15416:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
15417:                             push(@{$changes{'lonstatus'}},$key);
15418:                         }
15419:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
15420:                         push(@{$changes{'lonstatus'}},$key);
15421:                     }
15422:                 }
15423:             }
15424:         } else {
15425:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
15426:                 foreach my $key ('excluded','weights','threshold','sysmail') {
15427:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
15428:                         push(@{$changes{'lonstatus'}},$key);
15429:                     }
15430:                 }
15431:             }
15432:         }
15433:     } else {
15434:         my %default;
15435:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
15436:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
15437:         $default{'errormail'} = 'adminemail';
15438:         $default{'packagesmail'} = 'adminemail';
15439:         $default{'helpdeskmail'} = 'supportemail';
15440:         $default{'otherdomsmail'} = 'supportemail';
15441:         $default{'lonstatusmail'} = 'adminemail';
15442:         $default{'requestsmail'} = 'adminemail';
15443:         $default{'updatesmail'} = 'adminemail';
15444:         $default{'hostipmail'} = 'adminemail';
15445:         foreach my $item (@contacts) {
15446:            if ($to{$item} ne $default{$item}) {
15447:                $changes{$item} = 1;
15448:            }
15449:         }
15450:         foreach my $type (@mailings) {
15451:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
15452:                 push(@{$changes{$type}},@{$newsetting{$type}});
15453:             }
15454:             if ($others{$type} ne '') {
15455:                 push(@{$changes{$type}},'others');
15456:             }
15457:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15458:                 if ($bcc{$type} ne '') {
15459:                     push(@{$changes{$type}},'bcc');
15460:                 }
15461:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
15462:                     push(@{$changes{$type}},'include');
15463:                 }
15464:             }
15465:         }
15466:         if (ref($fields) eq 'ARRAY') {
15467:             foreach my $field (@{$fields}) {
15468:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
15469:                     push(@{$changes{'helpform'}},$field);
15470:                 }
15471:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
15472:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
15473:                         push(@{$changes{'helpform'}},'maxsize');
15474:                     }
15475:                 }
15476:             }
15477:         }
15478:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
15479:             foreach my $key ('excluded','weights','threshold','sysmail') {
15480:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
15481:                     push(@{$changes{'lonstatus'}},$key);
15482:                 }
15483:             }
15484:         }
15485:     }
15486:     foreach my $item (@toggles) {
15487:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
15488:             $changes{$item} = 1;
15489:         } elsif ((!$env{'form.'.$item}) &&
15490:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
15491:             $changes{$item} = 1;
15492:         }
15493:     }
15494:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
15495:                                              $dom);
15496:     if ($putresult eq 'ok') {
15497:         if (keys(%changes) > 0) {
15498:             &Apache::loncommon::devalidate_domconfig_cache($dom);
15499:             if (ref($lastactref) eq 'HASH') {
15500:                 $lastactref->{'domainconfig'} = 1;
15501:             }
15502:             my ($titles,$short_titles)  = &contact_titles();
15503:             $resulttext = &mt('Changes made:').'<ul>';
15504:             foreach my $item (@contacts) {
15505:                 if ($changes{$item}) {
15506:                     $resulttext .= '<li>'.$titles->{$item}.
15507:                                     &mt(' set to: ').
15508:                                     '<span class="LC_cusr_emph">'.
15509:                                     $to{$item}.'</span></li>';
15510:                 }
15511:             }
15512:             foreach my $type (@mailings) {
15513:                 if (ref($changes{$type}) eq 'ARRAY') {
15514:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15515:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
15516:                     } else {
15517:                         $resulttext .= '<li>'.$titles->{$type}.': ';
15518:                     }
15519:                     my @text;
15520:                     foreach my $item (@{$newsetting{$type}}) {
15521:                         push(@text,$short_titles->{$item});
15522:                     }
15523:                     if ($others{$type} ne '') {
15524:                         push(@text,$others{$type});
15525:                     }
15526:                     if (@text) {
15527:                         $resulttext .= '<span class="LC_cusr_emph">'.
15528:                                        join(', ',@text).'</span>';
15529:                     }
15530:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15531:                         if ($bcc{$type} ne '') {
15532:                             my $bcctext;
15533:                             if (@text) {
15534:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
15535:                             } else {
15536:                                 $bcctext = '(Bcc)';
15537:                             }
15538:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
15539:                         } elsif (!@text) {
15540:                             $resulttext .= &mt('No one');
15541:                         }   
15542:                         if ($includestr{$type} ne '') {
15543:                             if ($includeloc{$type} eq 'b') {
15544:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
15545:                             } elsif ($includeloc{$type} eq 's') {
15546:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
15547:                             }
15548:                         }
15549:                     } elsif (!@text) {
15550:                         $resulttext .= &mt('No recipients');
15551:                     }
15552:                     $resulttext .= '</li>';
15553:                 }
15554:             }
15555:             if (ref($changes{'overrides'}) eq 'ARRAY') {
15556:                 my @deletions;
15557:                 foreach my $type (@{$changes{'overrides'}}) {
15558:                     if ($usertypeshash{$type}) {
15559:                         if (grep(/^\Q$type\E/,@overrides)) {
15560:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
15561:                                                       $usertypeshash{$type}).'<ul><li>';
15562:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
15563:                                 my @text;
15564:                                 foreach my $item (@contacts) {
15565:                                     if ($newsetting{'override_'.$type}{$item}) { 
15566:                                         push(@text,$short_titles->{$item});
15567:                                     }
15568:                                 }
15569:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
15570:                                     push(@text,$newsetting{'override_'.$type}{'others'});
15571:                                 }
15572:   
15573:                                 if (@text) {
15574:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
15575:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
15576:                                 }
15577:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
15578:                                     my $bcctext;
15579:                                     if (@text) {
15580:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
15581:                                     } else {
15582:                                         $bcctext = '(Bcc)';
15583:                                     }
15584:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
15585:                                 } elsif (!@text) {
15586:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
15587:                                 }
15588:                                 $resulttext .= '</li>';
15589:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
15590:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
15591:                                     if ($loc eq 'b') {
15592:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
15593:                                     } elsif ($loc eq 's') {
15594:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
15595:                                     }
15596:                                 }
15597:                             }
15598:                             $resulttext .= '</li></ul></li>';
15599:                         } else {
15600:                             push(@deletions,$usertypeshash{$type});
15601:                         }
15602:                     }
15603:                 }
15604:                 if (@deletions) {
15605:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
15606:                                               join(', ',@deletions)).'</li>';
15607:                 }
15608:             }
15609:             my @offon = ('off','on');
15610:             my $corelink = &core_link_msu();
15611:             if ($changes{'reporterrors'}) {
15612:                 $resulttext .= '<li>'.
15613:                                &mt('E-mail error reports to [_1] set to "'.
15614:                                    $offon[$env{'form.reporterrors'}].'".',
15615:                                    $corelink).
15616:                                '</li>';
15617:             }
15618:             if ($changes{'reportupdates'}) {
15619:                 $resulttext .= '<li>'.
15620:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
15621:                                     $offon[$env{'form.reportupdates'}].'".',
15622:                                     $corelink).
15623:                                 '</li>';
15624:             }
15625:             if ($changes{'reportstatus'}) {
15626:                 $resulttext .= '<li>'.
15627:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
15628:                                     $offon[$env{'form.reportstatus'}].'".',
15629:                                     $corelink).
15630:                                 '</li>';
15631:             }
15632:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
15633:                 $resulttext .= '<li>'.
15634:                                &mt('Nightly status check e-mail settings').':<ul>';
15635:                 my (%defval,%use_def,%shown);
15636:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
15637:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
15638:                 $defval{'weights'} =
15639:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
15640:                 $defval{'excluded'} = &mt('None');
15641:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
15642:                     foreach my $item ('threshold','sysmail','weights','excluded') {
15643:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
15644:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
15645:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
15646:                             } elsif ($item eq 'weights') {
15647:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
15648:                                     foreach my $type ('E','W','N','U') {
15649:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
15650:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
15651:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
15652:                                         } else {
15653:                                             $shown{$item} .= $lonstatus_defs->{$type};
15654:                                         }
15655:                                         $shown{$item} .= ', ';
15656:                                     }
15657:                                     $shown{$item} =~ s/, $//;
15658:                                 } else {
15659:                                     $shown{$item} = $defval{$item};
15660:                                 }
15661:                             } elsif ($item eq 'excluded') {
15662:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
15663:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
15664:                                 } else {
15665:                                     $shown{$item} = $defval{$item};
15666:                                 }
15667:                             }
15668:                         } else {
15669:                             $shown{$item} = $defval{$item};
15670:                         }
15671:                     }
15672:                 } else {
15673:                     foreach my $item ('threshold','weights','excluded','sysmail') {
15674:                         $shown{$item} = $defval{$item};
15675:                     }
15676:                 }
15677:                 foreach my $item ('threshold','weights','excluded','sysmail') {
15678:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
15679:                                           $shown{$item}).'</li>';
15680:                 }
15681:                 $resulttext .= '</ul></li>';
15682:             }
15683:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
15684:                 my (@optional,@required,@unused,$maxsizechg);
15685:                 foreach my $field (@{$changes{'helpform'}}) {
15686:                     if ($field eq 'maxsize') {
15687:                         $maxsizechg = 1;
15688:                         next;
15689:                     }
15690:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
15691:                         push(@optional,$field);
15692:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
15693:                         push(@unused,$field);
15694:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
15695:                         push(@required,$field);
15696:                     }
15697:                 }
15698:                 if (@optional) {
15699:                     $resulttext .= '<li>'.
15700:                                    &mt('Help form fields changed to "Optional": [_1].',
15701:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
15702:                                    '</li>';
15703:                 }
15704:                 if (@required) {
15705:                     $resulttext .= '<li>'.
15706:                                    &mt('Help form fields changed to "Required": [_1].',
15707:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
15708:                                    '</li>';
15709:                 }
15710:                 if (@unused) {
15711:                     $resulttext .= '<li>'.
15712:                                    &mt('Help form fields changed to "Not shown": [_1].',
15713:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
15714:                                    '</li>';
15715:                 }
15716:                 if ($maxsizechg) {
15717:                     $resulttext .= '<li>'.
15718:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
15719:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
15720:                                    '</li>';
15721:                 }
15722:             }
15723:             $resulttext .= '</ul>';
15724:         } else {
15725:             $resulttext = &mt('No changes made to contacts and form settings');
15726:         }
15727:     } else {
15728:         $resulttext = '<span class="LC_error">'.
15729:             &mt('An error occurred: [_1].',$putresult).'</span>';
15730:     }
15731:     return $resulttext;
15732: }
15733: 
15734: sub modify_privacy {
15735:     my ($dom,%domconfig) = @_;
15736:     my ($resulttext,%current,%changes);
15737:     if (ref($domconfig{'privacy'}) eq 'HASH') {
15738:         %current = %{$domconfig{'privacy'}};
15739:     }
15740:     my @fields = ('lastname','firstname','middlename','generation','permanentemail','id');
15741:     my @items = ('domain','author','course','community');
15742:     my %names = &Apache::lonlocal::texthash (
15743:                    domain => 'Assigned domain role(s)',
15744:                    author => 'Assigned co-author role(s)',
15745:                    course => 'Assigned course role(s)',
15746:                    community => 'Assigned community role',
15747:                 );
15748:     my %roles = &Apache::lonlocal::texthash (
15749:                    domain => 'Domain role',
15750:                    author => 'Co-author role',
15751:                    course => 'Course role',
15752:                    community => 'Community role',
15753:                 );
15754:     my %titles = &Apache::lonlocal::texthash (
15755:                   approval => 'Approval for role in different domain',
15756:                   othdom   => 'User information available in other domain',
15757:                   priv     => 'Information viewable by privileged user in same domain',
15758:                   unpriv   => 'Information viewable by unprivileged user in same domain',
15759:                   instdom  => 'Other domain shares institution/provider',
15760:                   extdom   => 'Other domain has different institution/provider',
15761:                   none     => 'Not allowed',
15762:                   user     => 'User authorizes',
15763:                   domain   => 'Domain Coordinator authorizes',
15764:                   auto     => 'Unrestricted',
15765:     );
15766:     my %fieldnames = &Apache::lonlocal::texthash (
15767:                         id => 'Student/Employee ID',
15768:                         permanentemail => 'E-mail address',
15769:                         lastname => 'Last Name',
15770:                         firstname => 'First Name',
15771:                         middlename => 'Middle Name',
15772:                         generation => 'Generation',
15773:     );
15774:     my ($othertitle,$usertypes,$types) =
15775:         &Apache::loncommon::sorted_inst_types($dom);
15776:     my (%by_ip,%by_location,@intdoms,@instdoms);
15777:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
15778: 
15779:     my %privacyhash = (
15780:                        'approval' => {
15781:                                        instdom => {},
15782:                                        extdom  => {},
15783:                                      },
15784:                        'othdom'   => {},
15785:                        'priv'     => {},
15786:                        'unpriv'   => {},
15787:                       );
15788:     foreach my $item (@items) {
15789:         if (@instdoms > 1) {
15790:             if ($env{'form.privacy_approval_instdom'.$item} =~ /^(none|user|domain|auto)$/) {
15791:                 $privacyhash{'approval'}{'instdom'}{$item} = $env{'form.privacy_approval_instdom_'.$item};
15792:             }
15793:             if (ref($current{'approval'}) eq 'HASH') {
15794:                 if (ref($current{'approval'}{'instdom'}) eq 'HASH') {
15795:                     unless ($privacyhash{'approval'}{'instdom'}{$item} eq $current{'approval'}{'instdom'}{$item}) {
15796:                         $changes{'approval'} = 1;
15797:                     }
15798:                 }
15799:             } elsif ($privacyhash{'approval'}{'instdom'}{$item} ne 'auto') {
15800:                 $changes{'approval'} = 1;
15801:             }
15802:         }
15803:         if (keys(%by_location) > 0) {
15804:             if ($env{'form.privacy_approval_extdom_'.$item} =~ /^(none|user|domain|auto)$/) {
15805:                 $privacyhash{'approval'}{'extdom'}{$item} = $env{'form.privacy_approval_extdom_'.$item};
15806:             }
15807:             if (ref($current{'approval'}) eq 'HASH') {
15808:                 if (ref($current{'approval'}{'extdom'}) eq 'HASH') {
15809:                     unless ($privacyhash{'approval'}{'extdom'}{$item} eq $current{'approval'}{'extdom'}{$item}) {
15810:                         $changes{'approval'} = 1;
15811:                     }
15812:                 }
15813:             } elsif ($privacyhash{'approval'}{'extdom'}{$item} ne 'auto') {
15814:                 $changes{'approval'} = 1;
15815:             }
15816:         }
15817:         foreach my $status ('priv','unpriv') {
15818:             my @possibles = sort(&Apache::loncommon::get_env_multiple('form.privacy_'.$status.'_'.$item));
15819:             my @newvalues;
15820:             foreach my $field (@possibles) {
15821:                 if (grep(/^\Q$field\E$/,@fields)) {
15822:                     $privacyhash{$status}{$item}{$field} = 1;
15823:                     push(@newvalues,$field);
15824:                 }
15825:             }
15826:             @newvalues = sort(@newvalues);
15827:             if (ref($current{$status}) eq 'HASH') {
15828:                 if (ref($current{$status}{$item}) eq 'HASH') {
15829:                     my @currvalues = sort(keys(%{$current{$status}{$item}}));
15830:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
15831:                     if (@diffs > 0) {
15832:                         $changes{$status} = 1;
15833:                     }
15834:                 }
15835:             } else {
15836:                 my @stdfields;
15837:                 foreach my $field (@fields) {
15838:                     if ($field eq 'id') {
15839:                         next if ($status eq 'unpriv');
15840:                         next if (($status eq 'priv') && ($item eq 'community'));
15841:                     }
15842:                     push(@stdfields,$field);
15843:                 }
15844:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
15845:                 if (@diffs > 0) {
15846:                     $changes{$status} = 1;
15847:                 }
15848:             }
15849:         }
15850:     }
15851:     if ((@instdoms > 1) || (keys(%by_location) > 0)) {
15852:         my @statuses;
15853:         if (ref($types) eq 'ARRAY') {
15854:             @statuses = @{$types};
15855:         }
15856:         foreach my $type (@statuses,'default') {
15857:             my @possfields = &Apache::loncommon::get_env_multiple('form.privacy_othdom_'.$type);
15858:             my @newvalues;
15859:             foreach my $field (sort(@possfields)) {
15860:                 if (grep(/^\Q$field\E$/,@fields)) {
15861:                     $privacyhash{'othdom'}{$type}{$field} = 1;
15862:                     push(@newvalues,$field);
15863:                 }
15864:             }
15865:             @newvalues = sort(@newvalues);
15866:             if (ref($current{'othdom'}) eq 'HASH') {
15867:                 if (ref($current{'othdom'}{$type}) eq 'HASH') {
15868:                     my @currvalues = sort(keys(%{$current{'othdom'}{$type}}));
15869:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
15870:                     if (@diffs > 0) {
15871:                         $changes{'othdom'} = 1;
15872:                     }
15873:                 }
15874:             } else {
15875:                 my @stdfields = ('lastname','firstname','middlename','generation','permanentemail');
15876:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
15877:                 if (@diffs > 0) {
15878:                     $changes{'othdom'} = 1;
15879:                 }
15880:             }
15881:         }
15882:     }
15883:     my %confighash = (
15884:                         privacy => \%privacyhash,
15885:                      );
15886:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
15887:     if ($putresult eq 'ok') {
15888:         if (keys(%changes) > 0) {
15889:             $resulttext = &mt('Changes made: ').'<ul>';
15890:             foreach my $key ('approval','othdom','priv','unpriv') {
15891:                 if ($changes{$key}) {
15892:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
15893:                     if ($key eq 'approval') {
15894:                         if (keys(%{$privacyhash{$key}{instdom}})) {
15895:                             $resulttext .= '<li>'.$titles{'instdom'}.'<ul>';
15896:                             foreach my $item (@items) {
15897:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{instdom}{$item}}.'</li>';
15898:                             }
15899:                             $resulttext .= '</ul></li>';
15900:                         }
15901:                         if (keys(%{$privacyhash{$key}{extdom}})) {
15902:                             $resulttext .= '<li>'.$titles{'extdom'}.'<ul>';
15903:                             foreach my $item (@items) {
15904:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{extdom}{$item}}.'</li>';
15905:                             }
15906:                             $resulttext .= '</ul></li>';
15907:                         }
15908:                     } elsif ($key eq 'othdom') {
15909:                         my @statuses;
15910:                         if (ref($types) eq 'ARRAY') {
15911:                             @statuses = @{$types};
15912:                         }
15913:                         if (ref($privacyhash{$key}) eq 'HASH') {
15914:                             foreach my $status (@statuses,'default') {
15915:                                 if ($status eq 'default') {
15916:                                     $resulttext .= '<li>'.$othertitle.': ';
15917:                                 } elsif (ref($usertypes) eq 'HASH') {
15918:                                     $resulttext .= '<li>'.$usertypes->{$status}.': ';
15919:                                 } else {
15920:                                     next;
15921:                                 }
15922:                                 if (ref($privacyhash{$key}{$status}) eq 'HASH') {
15923:                                     if (keys(%{$privacyhash{$key}{$status}})) {
15924:                                         $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$status}}))));
15925:                                     } else {
15926:                                         $resulttext .= &mt('none');
15927:                                     }
15928:                                 }
15929:                                 $resulttext .= '</li>';
15930:                             }
15931:                         }
15932:                     } else {
15933:                         foreach my $item (@items) {
15934:                             if (ref($privacyhash{$key}{$item}) eq 'HASH') {
15935:                                 $resulttext .= '<li>'.$names{$item}.': ';
15936:                                 if (keys(%{$privacyhash{$key}{$item}})) {
15937:                                     $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$item}}))));
15938:                                 } else {
15939:                                     $resulttext .= &mt('none');
15940:                                 }
15941:                                 $resulttext .= '</li>';
15942:                             }
15943:                         }
15944:                     }
15945:                     $resulttext .= '</ul></li>';
15946:                 }
15947:             }
15948:         } else {
15949:             $resulttext = &mt('No changes made to user information settings');
15950:         }
15951:     } else {
15952:         $resulttext = '<span class="LC_error">'.
15953:             &mt('An error occurred: [_1]',$putresult).'</span>';
15954:     }
15955:     return $resulttext;
15956: }
15957: 
15958: sub modify_passwords {
15959:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
15960:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
15961:         $updatedefaults,$updateconf);
15962:     my $customfn = 'resetpw.html';
15963:     if (ref($domconfig{'passwords'}) eq 'HASH') {
15964:         %current = %{$domconfig{'passwords'}};
15965:     }
15966:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15967:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
15968:     if (ref($types) eq 'ARRAY') {
15969:         @oktypes = @{$types};
15970:     }
15971:     push(@oktypes,'default');
15972: 
15973:     my %titles = &Apache::lonlocal::texthash (
15974:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
15975:         intauth_check  => 'Check bcrypt cost if authenticated',
15976:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
15977:         permanent      => 'Permanent e-mail address',
15978:         critical       => 'Critical notification address',
15979:         notify         => 'Notification address',
15980:         min            => 'Minimum password length',
15981:         max            => 'Maximum password length',
15982:         chars          => 'Required characters',
15983:         expire         => 'Password expiration (days)',
15984:         numsaved       => 'Number of previous passwords to save',
15985:         reset          => 'Resetting Forgotten Password',
15986:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
15987:         rules          => 'Rules for LON-CAPA Passwords',
15988:         crsownerchg    => 'Course Owner Changing Student Passwords',
15989:         username       => 'Username',
15990:         email          => 'E-mail address',
15991:     );
15992: 
15993: #
15994: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
15995: #
15996:     my (%curr_defaults,%save_defaults);
15997:     if (ref($domconfig{'defaults'}) eq 'HASH') {
15998:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
15999:             if ($key =~ /^intauth_(cost|check|switch)$/) {
16000:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
16001:             } else {
16002:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
16003:             }
16004:         }
16005:     }
16006:     my %staticdefaults = (
16007:         'resetlink'      => 2,
16008:         'resetcase'      => \@oktypes,
16009:         'resetprelink'   => 'both',
16010:         'resetemail'     => ['critical','notify','permanent'],
16011:         'intauth_cost'   => 10,
16012:         'intauth_check'  => 0,
16013:         'intauth_switch' => 0,
16014:     );
16015:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
16016:     foreach my $type (@oktypes) {
16017:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
16018:     }
16019:     my $linklife = $env{'form.passwords_link'};
16020:     $linklife =~ s/^\s+|\s+$//g;
16021:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
16022:         $newvalues{'resetlink'} = $linklife;
16023:         if ($current{'resetlink'}) {
16024:             if ($current{'resetlink'} ne $linklife) {
16025:                 $changes{'reset'} = 1;
16026:             }
16027:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
16028:             if ($staticdefaults{'resetlink'} ne $linklife) {
16029:                 $changes{'reset'} = 1;
16030:             }
16031:         }
16032:     } elsif ($current{'resetlink'}) {
16033:         $changes{'reset'} = 1;
16034:     }
16035:     my @casesens;
16036:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
16037:     foreach my $case (sort(@posscase)) {
16038:         if (grep(/^\Q$case\E$/,@oktypes)) {
16039:             push(@casesens,$case);
16040:         }
16041:     }
16042:     $newvalues{'resetcase'} = \@casesens;
16043:     if (ref($current{'resetcase'}) eq 'ARRAY') {
16044:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
16045:         if (@diffs > 0) {
16046:             $changes{'reset'} = 1;
16047:         }
16048:     } elsif (!ref($domconfig{passwords}) eq 'HASH') {
16049:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
16050:         if (@diffs > 0) {
16051:             $changes{'reset'} = 1;
16052:         }
16053:     }
16054:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
16055:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
16056:         if (exists($current{'resetprelink'})) {
16057:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
16058:                 $changes{'reset'} = 1;
16059:             }
16060:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
16061:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
16062:                 $changes{'reset'} = 1;
16063:             }
16064:         }
16065:     } elsif ($current{'resetprelink'}) {
16066:         $changes{'reset'} = 1;
16067:     }
16068:     foreach my $type (@oktypes) {
16069:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
16070:         my @postlink;
16071:         foreach my $item (sort(@possplink)) {
16072:             if ($item =~ /^(email|username)$/) {
16073:                 push(@postlink,$item);
16074:             }
16075:         }
16076:         $newvalues{'resetpostlink'}{$type} = \@postlink;
16077:         unless ($changes{'reset'}) {
16078:             if (ref($current{'resetpostlink'}) eq 'HASH') {
16079:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
16080:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
16081:                     if (@diffs > 0) {
16082:                         $changes{'reset'} = 1;
16083:                     }
16084:                 } else {
16085:                     $changes{'reset'} = 1;
16086:                 }
16087:             } elsif (!ref($domconfig{passwords}) eq 'HASH') {
16088:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
16089:                 if (@diffs > 0) {
16090:                     $changes{'reset'} = 1;
16091:                 }
16092:             }
16093:         }
16094:     }
16095:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
16096:     my @resetemail;
16097:     foreach my $item (sort(@possemailsrc)) {
16098:         if ($item =~ /^(permanent|critical|notify)$/) {
16099:             push(@resetemail,$item);
16100:         }
16101:     }
16102:     $newvalues{'resetemail'} = \@resetemail;
16103:     unless ($changes{'reset'}) {
16104:         if (ref($current{'resetemail'}) eq 'ARRAY') {
16105:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
16106:             if (@diffs > 0) {
16107:                 $changes{'reset'} = 1;
16108:             }
16109:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
16110:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
16111:             if (@diffs > 0) {
16112:                 $changes{'reset'} = 1;
16113:             }
16114:         }
16115:     }
16116:     if ($env{'form.passwords_stdtext'} == 0) {
16117:         $newvalues{'resetremove'} = 1;
16118:         unless ($current{'resetremove'}) {
16119:             $changes{'reset'} = 1;
16120:         }
16121:     } elsif ($current{'resetremove'}) {
16122:         $changes{'reset'} = 1;
16123:     }
16124:     if ($env{'form.passwords_customfile.filename'} ne '') {
16125:         my $servadm = $r->dir_config('lonAdmEMail');
16126:         my ($configuserok,$author_ok,$switchserver) =
16127:             &config_check($dom,$confname,$servadm);
16128:         my $error;
16129:         if ($configuserok eq 'ok') {
16130:             if ($switchserver) {
16131:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
16132:             } else {
16133:                 if ($author_ok eq 'ok') {
16134:                     my ($result,$customurl) =
16135:                         &publishlogo($r,'upload','passwords_customfile',$dom,
16136:                                      $confname,'customtext/resetpw','','',$customfn);
16137:                     if ($result eq 'ok') {
16138:                         $newvalues{'resetcustom'} = $customurl;
16139:                         $changes{'reset'} = 1;
16140:                     } else {
16141:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
16142:                     }
16143:                 } else {
16144:                     $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);
16145:                 }
16146:             }
16147:         } else {
16148:             $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);
16149:         }
16150:         if ($error) {
16151:             &Apache::lonnet::logthis($error);
16152:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
16153:         }
16154:     } elsif ($current{'resetcustom'}) {
16155:         if ($env{'form.passwords_custom_del'}) {
16156:             $changes{'reset'} = 1;
16157:         } else {
16158:             $newvalues{'resetcustom'} = $current{'resetcustom'};
16159:         }
16160:     }
16161:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
16162:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
16163:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
16164:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
16165:             $changes{'intauth'} = 1;
16166:         }
16167:     } else {
16168:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
16169:     }
16170:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
16171:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
16172:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
16173:             $changes{'intauth'} = 1;
16174:         }
16175:     } else {
16176:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
16177:     }
16178:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
16179:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
16180:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
16181:             $changes{'intauth'} = 1;
16182:         }
16183:     } else {
16184:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
16185:     }
16186:     foreach my $item ('cost','check','switch') {
16187:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
16188:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
16189:             $updatedefaults = 1;
16190:         }
16191:     }
16192:     foreach my $rule ('min','max','expire','numsaved') {
16193:         $env{'form.passwords_'.$rule} =~ s/^\s+|\s+$//g;
16194:         my $ruleok;
16195:         if ($rule eq 'expire') {
16196:             if (($env{'form.passwords_'.$rule} =~ /^\d+(|\.\d*)$/) &&
16197:                 ($env{'form.passwords_'.$rule} ne '0')) {
16198:                 $ruleok = 1;
16199:             }
16200:         } elsif ($rule eq 'min') {
16201:             if ($env{'form.passwords_'.$rule} =~ /^\d+$/) {
16202:                 if ($env{'form.passwords_'.$rule} >= $Apache::lonnet::passwdmin) {
16203:                     $ruleok = 1;
16204:                 }
16205:             }
16206:         } elsif (($env{'form.passwords_'.$rule} =~ /^\d+$/) &&
16207:                  ($env{'form.passwords_'.$rule} ne '0')) {
16208:             $ruleok = 1;
16209:         }
16210:         if ($ruleok) {
16211:             $newvalues{$rule} = $env{'form.passwords_'.$rule};
16212:             if (exists($current{$rule})) {
16213:                 if ($newvalues{$rule} ne $current{$rule}) {
16214:                     $changes{'rules'} = 1;
16215:                 }
16216:             } elsif ($rule eq 'min') {
16217:                 if ($staticdefaults{$rule} ne $newvalues{$rule}) {
16218:                     $changes{'rules'} = 1;
16219:                 }
16220:             } else {
16221:                 $changes{'rules'} = 1;
16222:             }
16223:         } elsif (exists($current{$rule})) {
16224:             $changes{'rules'} = 1;
16225:         }
16226:     }
16227:     my @posschars = &Apache::loncommon::get_env_multiple('form.passwords_chars');
16228:     my @chars;
16229:     foreach my $item (sort(@posschars)) {
16230:         if ($item =~ /^(uc|lc|num|spec)$/) {
16231:             push(@chars,$item);
16232:         }
16233:     }
16234:     $newvalues{'chars'} = \@chars;
16235:     unless ($changes{'rules'}) {
16236:         if (ref($current{'chars'}) eq 'ARRAY') {
16237:             my @diffs = &Apache::loncommon::compare_arrays($current{'chars'},\@chars);
16238:             if (@diffs > 0) {
16239:                 $changes{'rules'} = 1;
16240:             }
16241:         } else {
16242:             if (@chars > 0) {
16243:                 $changes{'rules'} = 1;
16244:             }
16245:         }
16246:     }
16247:     my %crsownerchg = (
16248:                         by => [],
16249:                         for => [],
16250:                       );
16251:     foreach my $item ('by','for') {
16252:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
16253:         foreach my $type (sort(@posstypes)) {
16254:             if (grep(/^\Q$type\E$/,@oktypes)) {
16255:                 push(@{$crsownerchg{$item}},$type);
16256:             }
16257:         }
16258:     }
16259:     $newvalues{'crsownerchg'} = \%crsownerchg;
16260:     if (ref($current{'crsownerchg'}) eq 'HASH') {
16261:         foreach my $item ('by','for') {
16262:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
16263:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
16264:                 if (@diffs > 0) {
16265:                     $changes{'crsownerchg'} = 1;
16266:                     last;
16267:                 }
16268:             }
16269:         }
16270:     } elsif (!(ref($domconfig{passwords}) eq 'HASH')) {
16271:         foreach my $item ('by','for') {
16272:             if (@{$crsownerchg{$item}} > 0) {
16273:                 $changes{'crsownerchg'} = 1;
16274:                 last;
16275:             }
16276:         }
16277:     }
16278: 
16279:     my %confighash = (
16280:                         defaults  => \%save_defaults,
16281:                         passwords => \%newvalues,
16282:                      );
16283:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
16284: 
16285:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
16286:     if ($putresult eq 'ok') {
16287:         if (keys(%changes) > 0) {
16288:             $resulttext = &mt('Changes made: ').'<ul>';
16289:             foreach my $key ('reset','intauth','rules','crsownerchg') {
16290:                 if ($changes{$key}) {
16291:                     unless ($key eq 'intauth') {
16292:                         $updateconf = 1;
16293:                     }
16294:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
16295:                     if ($key eq 'reset') {
16296:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
16297:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
16298:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
16299:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
16300:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />';
16301:                             if (ref($confighash{'passwords'}{'recaptchakeys'}) eq 'HASH') {
16302:                                 $resulttext .= &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'public'}).'</br>'.
16303:                                                &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'private'}).'</li>';
16304:                             }
16305:                         } else {
16306:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
16307:                         }
16308:                         if ($confighash{'passwords'}{'resetlink'}) {
16309:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
16310:                         } else {
16311:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
16312:                                                   &mt('Will default to 2 hours').'</li>';
16313:                         }
16314:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
16315:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
16316:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
16317:                             } else {
16318:                                 my $casesens;
16319:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
16320:                                     if ($type eq 'default') {
16321:                                         $casesens .= $othertitle.', ';
16322:                                     } elsif ($usertypes->{$type} ne '') {
16323:                                         $casesens .= $usertypes->{$type}.', ';
16324:                                     }
16325:                                 }
16326:                                 $casesens =~ s/\Q, \E$//;
16327:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
16328:                             }
16329:                         } else {
16330:                             $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>';
16331:                         }
16332:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
16333:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
16334:                         } else {
16335:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
16336:                         }
16337:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
16338:                             my $output;
16339:                             if (ref($types) eq 'ARRAY') {
16340:                                 foreach my $type (@{$types}) {
16341:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
16342:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
16343:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
16344:                                         } else {
16345:                                             $output .= $usertypes->{$type}.' -- '.
16346:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
16347:                                         }
16348:                                     }
16349:                                 }
16350:                             }
16351:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
16352:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
16353:                                     $output .= $othertitle.' -- '.&mt('none');
16354:                                 } else {
16355:                                     $output .= $othertitle.' -- '.
16356:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
16357:                                 }
16358:                             }
16359:                             if ($output) {
16360:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
16361:                             } else {
16362:                                 $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>';
16363:                             }
16364:                         } else {
16365:                             $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>';
16366:                         }
16367:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
16368:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
16369:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
16370:                             } else {
16371:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
16372:                             }
16373:                         } else {
16374:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA usedfor verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
16375:                         }
16376:                         if ($confighash{'passwords'}{'resetremove'}) {
16377:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
16378:                         } else {
16379:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
16380:                         }
16381:                         if ($confighash{'passwords'}{'resetcustom'}) {
16382:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
16383:                                                                             &mt('custom text'),600,500,undef,undef,
16384:                                                                             undef,undef,'background-color:#ffffff');
16385:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes: [_1]',$customlink).'</li>';
16386:                         } else {
16387:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
16388:                         }
16389:                     } elsif ($key eq 'intauth') {
16390:                         foreach my $item ('cost','switch','check') {
16391:                             my $value = $save_defaults{$key.'_'.$item};
16392:                             if ($item eq 'switch') {
16393:                                 my %optiondesc = &Apache::lonlocal::texthash (
16394:                                                      0 => 'No',
16395:                                                      1 => 'Yes',
16396:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
16397:                                                  );
16398:                                 if ($value =~ /^(0|1|2)$/) {
16399:                                     $value = $optiondesc{$value};
16400:                                 } else {
16401:                                     $value = &mt('none -- defaults to No');
16402:                                 }
16403:                             } elsif ($item eq 'check') {
16404:                                 my %optiondesc = &Apache::lonlocal::texthash (
16405:                                                      0 => 'No',
16406:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
16407:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
16408:                                                  );
16409:                                 if ($value =~ /^(0|1|2)$/) {
16410:                                     $value = $optiondesc{$value};
16411:                                 } else {
16412:                                     $value = &mt('none -- defaults to No');
16413:                                 }
16414:                             }
16415:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
16416:                         }
16417:                     } elsif ($key eq 'rules') {
16418:                         foreach my $rule ('min','max','expire','numsaved') {
16419:                             if ($confighash{'passwords'}{$rule} eq '') {
16420:                                 if ($rule eq 'min') {
16421:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
16422:                                                    ' '.&mt('Default of [_1] will be used',
16423:                                                            $Apache::lonnet::passwdmin).'</li>';
16424:                                 } else {
16425:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
16426:                                 }
16427:                             } else {
16428:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
16429:                             }
16430:                         }
16431:                         if (ref($confighash{'passwords'}{'chars'}) eq 'ARRAY') {
16432:                             if (@{$confighash{'passwords'}{'chars'}} > 0) {
16433:                                 my %rulenames = &Apache::lonlocal::texthash(
16434:                                                      uc => 'At least one upper case letter',
16435:                                                      lc => 'At least one lower case letter',
16436:                                                      num => 'At least one number',
16437:                                                      spec => 'At least one non-alphanumeric',
16438:                                                    );
16439:                                 my $needed = '<ul><li>'.
16440:                                              join('</li><li>',map {$rulenames{$_} } @{$confighash{'passwords'}{'chars'}}).
16441:                                              '</li></ul>'; 
16442:                                 $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
16443:                             } else {
16444:                                 $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
16445:                             }
16446:                         } else {
16447:                             $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
16448:                         }
16449:                     } elsif ($key eq 'crsownerchg') {
16450:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
16451:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
16452:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
16453:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
16454:                             } else {
16455:                                 my %crsownerstr;
16456:                                 foreach my $item ('by','for') {
16457:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
16458:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
16459:                                             if ($type eq 'default') {
16460:                                                 $crsownerstr{$item} .= $othertitle.', ';
16461:                                             } elsif ($usertypes->{$type} ne '') {
16462:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
16463:                                             }
16464:                                         }
16465:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
16466:                                     }
16467:                                 }
16468:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
16469:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
16470:                             }
16471:                         } else {
16472:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
16473:                         }
16474:                     }
16475:                     $resulttext .= '</ul></li>';
16476:                 }
16477:             }
16478:             $resulttext .= '</ul>';
16479:         } else {
16480:             $resulttext = &mt('No changes made to password settings');
16481:         }
16482:         my $cachetime = 24*60*60;
16483:         if ($updatedefaults) {
16484:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16485:             if (ref($lastactref) eq 'HASH') {
16486:                 $lastactref->{'domdefaults'} = 1;
16487:             }
16488:         }
16489:         if ($updateconf) {
16490:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
16491:             if (ref($lastactref) eq 'HASH') {
16492:                 $lastactref->{'passwdconf'} = 1;
16493:             }
16494:         }
16495:     } else {
16496:         $resulttext = '<span class="LC_error">'.
16497:             &mt('An error occurred: [_1]',$putresult).'</span>';
16498:     }
16499:     if ($errors) {
16500:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
16501:                        $errors.'</ul></p>';
16502:     }
16503:     return $resulttext;
16504: }
16505: 
16506: sub modify_usercreation {
16507:     my ($dom,%domconfig) = @_;
16508:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
16509:     my $warningmsg;
16510:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
16511:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
16512:             if ($key eq 'cancreate') {
16513:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
16514:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
16515:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
16516:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
16517:                         } else {
16518:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
16519:                         }
16520:                     }
16521:                 }
16522:             } elsif ($key eq 'email_rule') {
16523:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
16524:             } else {
16525:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
16526:             }
16527:         }
16528:     }
16529:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
16530:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
16531:     my @contexts = ('author','course','requestcrs');
16532:     foreach my $item(@contexts) {
16533:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
16534:     }
16535:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
16536:         foreach my $item (@contexts) {
16537:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
16538:                 push(@{$changes{'cancreate'}},$item);
16539:             }
16540:         }
16541:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
16542:         foreach my $item (@contexts) {
16543:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
16544:                 if ($cancreate{$item} ne 'any') {
16545:                     push(@{$changes{'cancreate'}},$item);
16546:                 }
16547:             } else {
16548:                 if ($cancreate{$item} ne 'none') {
16549:                     push(@{$changes{'cancreate'}},$item);
16550:                 }
16551:             }
16552:         }
16553:     } else {
16554:         foreach my $item (@contexts)  {
16555:             push(@{$changes{'cancreate'}},$item);
16556:         }
16557:     }
16558: 
16559:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
16560:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
16561:             if (!grep(/^\Q$type\E$/,@username_rule)) {
16562:                 push(@{$changes{'username_rule'}},$type);
16563:             }
16564:         }
16565:         foreach my $type (@username_rule) {
16566:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
16567:                 push(@{$changes{'username_rule'}},$type);
16568:             }
16569:         }
16570:     } else {
16571:         push(@{$changes{'username_rule'}},@username_rule);
16572:     }
16573: 
16574:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
16575:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
16576:             if (!grep(/^\Q$type\E$/,@id_rule)) {
16577:                 push(@{$changes{'id_rule'}},$type);
16578:             }
16579:         }
16580:         foreach my $type (@id_rule) {
16581:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
16582:                 push(@{$changes{'id_rule'}},$type);
16583:             }
16584:         }
16585:     } else {
16586:         push(@{$changes{'id_rule'}},@id_rule);
16587:     }
16588: 
16589:     my @authen_contexts = ('author','course','domain');
16590:     my @authtypes = ('int','krb4','krb5','loc','lti');
16591:     my %authhash;
16592:     foreach my $item (@authen_contexts) {
16593:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
16594:         foreach my $auth (@authtypes) {
16595:             if (grep(/^\Q$auth\E$/,@authallowed)) {
16596:                 $authhash{$item}{$auth} = 1;
16597:             } else {
16598:                 $authhash{$item}{$auth} = 0;
16599:             }
16600:         }
16601:     }
16602:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
16603:         foreach my $item (@authen_contexts) {
16604:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
16605:                 foreach my $auth (@authtypes) {
16606:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
16607:                         push(@{$changes{'authtypes'}},$item);
16608:                         last;
16609:                     }
16610:                 }
16611:             }
16612:         }
16613:     } else {
16614:         foreach my $item (@authen_contexts) {
16615:             push(@{$changes{'authtypes'}},$item);
16616:         }
16617:     }
16618: 
16619:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
16620:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
16621:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
16622:     $save_usercreate{'id_rule'} = \@id_rule;
16623:     $save_usercreate{'username_rule'} = \@username_rule,
16624:     $save_usercreate{'authtypes'} = \%authhash;
16625: 
16626:     my %usercreation_hash =  (
16627:         usercreation     => \%save_usercreate,
16628:     );
16629: 
16630:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
16631:                                              $dom);
16632: 
16633:     if ($putresult eq 'ok') {
16634:         if (keys(%changes) > 0) {
16635:             $resulttext = &mt('Changes made:').'<ul>';
16636:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
16637:                 my %lt = &usercreation_types();
16638:                 foreach my $type (@{$changes{'cancreate'}}) {
16639:                     my $chgtext = $lt{$type}.', ';
16640:                     if ($cancreate{$type} eq 'none') {
16641:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
16642:                     } elsif ($cancreate{$type} eq 'any') {
16643:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
16644:                     } elsif ($cancreate{$type} eq 'official') {
16645:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
16646:                     } elsif ($cancreate{$type} eq 'unofficial') {
16647:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
16648:                     }
16649:                     $resulttext .= '<li>'.$chgtext.'</li>';
16650:                 }
16651:             }
16652:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
16653:                 my ($rules,$ruleorder) = 
16654:                     &Apache::lonnet::inst_userrules($dom,'username');
16655:                 my $chgtext = '<ul>';
16656:                 foreach my $type (@username_rule) {
16657:                     if (ref($rules->{$type}) eq 'HASH') {
16658:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
16659:                     }
16660:                 }
16661:                 $chgtext .= '</ul>';
16662:                 if (@username_rule > 0) {
16663:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
16664:                 } else {
16665:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
16666:                 }
16667:             }
16668:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
16669:                 my ($idrules,$idruleorder) = 
16670:                     &Apache::lonnet::inst_userrules($dom,'id');
16671:                 my $chgtext = '<ul>';
16672:                 foreach my $type (@id_rule) {
16673:                     if (ref($idrules->{$type}) eq 'HASH') {
16674:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
16675:                     }
16676:                 }
16677:                 $chgtext .= '</ul>';
16678:                 if (@id_rule > 0) {
16679:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
16680:                 } else {
16681:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
16682:                 }
16683:             }
16684:             my %authname = &authtype_names();
16685:             my %context_title = &context_names();
16686:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
16687:                 my $chgtext = '<ul>';
16688:                 foreach my $type (@{$changes{'authtypes'}}) {
16689:                     my @allowed;
16690:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
16691:                     foreach my $auth (@authtypes) {
16692:                         if ($authhash{$type}{$auth}) {
16693:                             push(@allowed,$authname{$auth});
16694:                         }
16695:                     }
16696:                     if (@allowed > 0) {
16697:                         $chgtext .= join(', ',@allowed).'</li>';
16698:                     } else {
16699:                         $chgtext .= &mt('none').'</li>';
16700:                     }
16701:                 }
16702:                 $chgtext .= '</ul>';
16703:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
16704:                 $resulttext .= '</li>';
16705:             }
16706:             $resulttext .= '</ul>';
16707:         } else {
16708:             $resulttext = &mt('No changes made to user creation settings');
16709:         }
16710:     } else {
16711:         $resulttext = '<span class="LC_error">'.
16712:             &mt('An error occurred: [_1]',$putresult).'</span>';
16713:     }
16714:     if ($warningmsg ne '') {
16715:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
16716:     }
16717:     return $resulttext;
16718: }
16719: 
16720: sub modify_selfcreation {
16721:     my ($dom,$lastactref,%domconfig) = @_;
16722:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
16723:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
16724:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16725:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
16726:     if (ref($typesref) eq 'ARRAY') {
16727:         @types = @{$typesref};
16728:     }
16729:     if (ref($usertypesref) eq 'HASH') {
16730:         %usertypes = %{$usertypesref};
16731:     }
16732:     $usertypes{'default'} = $othertitle;
16733: #
16734: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
16735: #
16736:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
16737:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
16738:             if ($key eq 'cancreate') {
16739:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
16740:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
16741:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
16742:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
16743:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
16744:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
16745:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
16746:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
16747:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
16748:                         } else {
16749:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
16750:                         }
16751:                     }
16752:                 }
16753:             } elsif ($key eq 'email_rule') {
16754:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
16755:             } else {
16756:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
16757:             }
16758:         }
16759:     }
16760: #
16761: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
16762: #
16763:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
16764:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
16765:             if ($key eq 'selfcreate') {
16766:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
16767:             } else {
16768:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
16769:             }
16770:         }
16771:     }
16772: #
16773: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
16774: #
16775:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
16776:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
16777:             if ($key eq 'inststatusguest') {
16778:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
16779:             } else {
16780:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
16781:             }
16782:         }
16783:     }
16784: 
16785:     my @contexts = ('selfcreate');
16786:     @{$cancreate{'selfcreate'}} = ();
16787:     %{$cancreate{'emailusername'}} = ();
16788:     if (@types) {
16789:         @{$cancreate{'statustocreate'}} = ();
16790:     }
16791:     %{$cancreate{'selfcreateprocessing'}} = ();
16792:     %{$cancreate{'shibenv'}} = ();
16793:     %{$cancreate{'emailverified'}} = ();
16794:     %{$cancreate{'emailoptions'}} = ();
16795:     %{$cancreate{'emaildomain'}} = ();
16796:     my %selfcreatetypes = (
16797:                              sso   => 'users authenticated by institutional single sign on',
16798:                              login => 'users authenticated by institutional log-in',
16799:                              email => 'users verified by e-mail',
16800:                           );
16801: #
16802: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
16803: # is permitted.
16804: #
16805:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
16806: 
16807:     my (@statuses,%email_rule);
16808:     foreach my $item ('login','sso','email') {
16809:         if ($item eq 'email') {
16810:             if ($env{'form.cancreate_email'}) {
16811:                 if (@types) {
16812:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
16813:                     foreach my $status (@poss_statuses) {
16814:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
16815:                             push(@statuses,$status);
16816:                         }
16817:                     }
16818:                     $save_inststatus{'inststatusguest'} = \@statuses;
16819:                 } else {
16820:                     push(@statuses,'default');
16821:                 }
16822:                 if (@statuses) {
16823:                     my %curr_rule;
16824:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
16825:                         foreach my $type (@statuses) {
16826:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
16827:                         }
16828:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
16829:                         foreach my $type (@statuses) {
16830:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
16831:                         }
16832:                     }
16833:                     push(@{$cancreate{'selfcreate'}},'email');
16834:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
16835:                     my %curremaildom;
16836:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
16837:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
16838:                     }
16839:                     foreach my $type (@statuses) {
16840:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
16841:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
16842:                         }
16843:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
16844:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
16845:                         }
16846:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
16847: #
16848: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
16849: #
16850:                             my $chosen = $1;
16851:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
16852:                                 my $emaildom;
16853:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
16854:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type}; 
16855:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
16856:                                     if (ref($curremaildom{$type}) eq 'HASH') {
16857:                                         if (exists($curremaildom{$type}{$chosen})) {
16858:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
16859:                                                 push(@{$changes{'cancreate'}},'emaildomain');
16860:                                             }
16861:                                         } elsif ($emaildom ne '') {
16862:                                             push(@{$changes{'cancreate'}},'emaildomain');
16863:                                         }
16864:                                     } elsif ($emaildom ne '') {
16865:                                         push(@{$changes{'cancreate'}},'emaildomain');
16866:                                     } 
16867:                                 }
16868:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
16869:                             } elsif ($chosen eq 'custom') {
16870:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
16871:                                 $email_rule{$type} = [];
16872:                                 if (ref($emailrules) eq 'HASH') {
16873:                                     foreach my $rule (@possemail_rules) {
16874:                                         if (exists($emailrules->{$rule})) {
16875:                                             push(@{$email_rule{$type}},$rule);
16876:                                         }
16877:                                     }
16878:                                 }
16879:                                 if (@{$email_rule{$type}}) {
16880:                                     $cancreate{'emailoptions'}{$type} = 'custom';
16881:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
16882:                                         if (@{$curr_rule{$type}} > 0) {
16883:                                             foreach my $rule (@{$curr_rule{$type}}) {
16884:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
16885:                                                     push(@{$changes{'email_rule'}},$type);
16886:                                                 }
16887:                                             }
16888:                                         }
16889:                                         foreach my $type (@{$email_rule{$type}}) {
16890:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
16891:                                                 push(@{$changes{'email_rule'}},$type);
16892:                                             }
16893:                                         }
16894:                                     } else {
16895:                                         push(@{$changes{'email_rule'}},$type);
16896:                                     }
16897:                                 }
16898:                             } else {
16899:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
16900:                             }
16901:                         }
16902:                     }
16903:                     if (@types) {
16904:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
16905:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
16906:                             if (@changed) {
16907:                                 push(@{$changes{'inststatus'}},'inststatusguest');
16908:                             }
16909:                         } else {
16910:                             push(@{$changes{'inststatus'}},'inststatusguest');
16911:                         }
16912:                     }
16913:                 } else {
16914:                     delete($env{'form.cancreate_email'});
16915:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
16916:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
16917:                             push(@{$changes{'inststatus'}},'inststatusguest');
16918:                         }
16919:                     }
16920:                 }
16921:             } else {
16922:                 $save_inststatus{'inststatusguest'} = [];
16923:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
16924:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
16925:                         push(@{$changes{'inststatus'}},'inststatusguest');
16926:                     }
16927:                 }
16928:             }
16929:         } else {
16930:             if ($env{'form.cancreate_'.$item}) {
16931:                 push(@{$cancreate{'selfcreate'}},$item);
16932:             }
16933:         }
16934:     }
16935:     my (%userinfo,%savecaptcha);
16936:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
16937: #
16938: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
16939: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
16940: #
16941: 
16942:     if ($env{'form.cancreate_email'}) {
16943:         push(@contexts,'emailusername');
16944:         if (@statuses) {
16945:             foreach my $type (@statuses) {
16946:                 if (ref($infofields) eq 'ARRAY') {
16947:                     foreach my $field (@{$infofields}) {
16948:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
16949:                             $cancreate{'emailusername'}{$type}{$field} = $1;
16950:                         }
16951:                     }
16952:                 }
16953:             }
16954:         }
16955: #
16956: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
16957: # queued requests for self-creation of account verified by e-mail.
16958: #
16959: 
16960:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
16961:         @approvalnotify = sort(@approvalnotify);
16962:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
16963:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
16964:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
16965:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
16966:                     push(@{$changes{'cancreate'}},'notify');
16967:                 }
16968:             } else {
16969:                 if ($cancreate{'notify'}{'approval'}) {
16970:                     push(@{$changes{'cancreate'}},'notify');
16971:                 }
16972:             }
16973:         } elsif ($cancreate{'notify'}{'approval'}) {
16974:             push(@{$changes{'cancreate'}},'notify');
16975:         }
16976: 
16977:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
16978:     }
16979: #  
16980: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
16981: # institutional log-in.
16982: #
16983:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
16984:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
16985:                ($domdefaults{'auth_def'} eq 'localauth'))) {
16986:             $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.').' '.
16987:                           &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.');
16988:         }
16989:     }
16990:     my @fields = ('lastname','firstname','middlename','generation',
16991:                   'permanentemail','id');
16992:     my @shibfields = (@fields,'inststatus');
16993:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
16994: #
16995: # Where usernames may created for institutional log-in and/or institutional single sign on:
16996: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
16997: # may self-create accounts 
16998: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
16999: # which the user may supply, if institutional data is unavailable.
17000: #
17001:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
17002:         if (@types) {
17003:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
17004:             push(@contexts,'statustocreate');
17005:             foreach my $type (@types) {
17006:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
17007:                 foreach my $field (@fields) {
17008:                     if (grep(/^\Q$field\E$/,@modifiable)) {
17009:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
17010:                     } else {
17011:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
17012:                     }
17013:                 }
17014:             }
17015:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
17016:                 foreach my $type (@types) {
17017:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
17018:                         foreach my $field (@fields) {
17019:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
17020:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
17021:                                 push(@{$changes{'selfcreate'}},$type);
17022:                                 last;
17023:                             }
17024:                         }
17025:                     }
17026:                 }
17027:             } else {
17028:                 foreach my $type (@types) {
17029:                     push(@{$changes{'selfcreate'}},$type);
17030:                 }
17031:             }
17032:         }
17033:         foreach my $field (@shibfields) {
17034:             if ($env{'form.shibenv_'.$field} ne '') {
17035:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
17036:             }
17037:         }
17038:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
17039:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
17040:                 foreach my $field (@shibfields) {
17041:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
17042:                         push(@{$changes{'cancreate'}},'shibenv');
17043:                     }
17044:                 }
17045:             } else {
17046:                 foreach my $field (@shibfields) {
17047:                     if ($env{'form.shibenv_'.$field}) {
17048:                         push(@{$changes{'cancreate'}},'shibenv');
17049:                         last;
17050:                     }
17051:                 }
17052:             }
17053:         }
17054:     }
17055:     foreach my $item (@contexts) {
17056:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
17057:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
17058:                 if (ref($cancreate{$item}) eq 'ARRAY') {
17059:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
17060:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17061:                             push(@{$changes{'cancreate'}},$item);
17062:                         }
17063:                     }
17064:                 }
17065:             }
17066:             if (ref($cancreate{$item}) eq 'ARRAY') {
17067:                 foreach my $type (@{$cancreate{$item}}) {
17068:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
17069:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17070:                             push(@{$changes{'cancreate'}},$item);
17071:                         }
17072:                     }
17073:                 }
17074:             }
17075:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
17076:             if (ref($cancreate{$item}) eq 'HASH') {
17077:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
17078:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
17079:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
17080:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
17081:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17082:                                     push(@{$changes{'cancreate'}},$item);
17083:                                 }
17084:                             }
17085:                         }
17086:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
17087:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
17088:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17089:                                 push(@{$changes{'cancreate'}},$item);
17090:                             }
17091:                         }
17092:                     }
17093:                 }
17094:                 foreach my $type (keys(%{$cancreate{$item}})) {
17095:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
17096:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
17097:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
17098:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
17099:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17100:                                         push(@{$changes{'cancreate'}},$item);
17101:                                     }
17102:                                 }
17103:                             } else {
17104:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17105:                                     push(@{$changes{'cancreate'}},$item);
17106:                                 }
17107:                             }
17108:                         }
17109:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
17110:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
17111:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17112:                                 push(@{$changes{'cancreate'}},$item);
17113:                             }
17114:                         }
17115:                     }
17116:                 }
17117:             }
17118:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
17119:             if (ref($cancreate{$item}) eq 'ARRAY') {
17120:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
17121:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17122:                         push(@{$changes{'cancreate'}},$item);
17123:                     }
17124:                 }
17125:             }
17126:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
17127:             if (ref($cancreate{$item}) eq 'HASH') {
17128:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17129:                     push(@{$changes{'cancreate'}},$item);
17130:                 }
17131:             }
17132:         } elsif ($item eq 'emailusername') {
17133:             if (ref($cancreate{$item}) eq 'HASH') {
17134:                 foreach my $type (keys(%{$cancreate{$item}})) {
17135:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
17136:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
17137:                             if ($cancreate{$item}{$type}{$field}) {
17138:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17139:                                     push(@{$changes{'cancreate'}},$item);
17140:                                 }
17141:                                 last;
17142:                             }
17143:                         }
17144:                     }
17145:                 }
17146:             }
17147:         }
17148:     }
17149: #
17150: # Populate %save_usercreate hash with updates to self-creation configuration.
17151: #
17152:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
17153:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
17154:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
17155:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
17156:     if (ref($cancreate{'notify'}) eq 'HASH') {
17157:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
17158:     }
17159:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
17160:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
17161:     }
17162:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
17163:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
17164:     }
17165:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
17166:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
17167:     }
17168:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
17169:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
17170:     }
17171:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
17172:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
17173:     }
17174:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
17175:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
17176:     }
17177:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
17178:     $save_usercreate{'email_rule'} = \%email_rule;
17179: 
17180:     my %userconfig_hash = (
17181:             usercreation     => \%save_usercreate,
17182:             usermodification => \%save_usermodify,
17183:             inststatus       => \%save_inststatus,
17184:     );
17185: 
17186:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
17187:                                              $dom);
17188: #
17189: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
17190: #
17191:     if ($putresult eq 'ok') {
17192:         if (keys(%changes) > 0) {
17193:             $resulttext = &mt('Changes made:').'<ul>';
17194:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
17195:                 my %lt = &selfcreation_types();
17196:                 foreach my $type (@{$changes{'cancreate'}}) {
17197:                     my $chgtext = '';
17198:                     if ($type eq 'selfcreate') {
17199:                         if (@{$cancreate{$type}} == 0) {
17200:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
17201:                         } else {
17202:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
17203:                                         '<ul>';
17204:                             foreach my $case (@{$cancreate{$type}}) {
17205:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
17206:                             }
17207:                             $chgtext .= '</ul>';
17208:                             if (ref($cancreate{$type}) eq 'ARRAY') {
17209:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
17210:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
17211:                                         if (@{$cancreate{'statustocreate'}} == 0) {
17212:                                             $chgtext .= '<span class="LC_warning">'.
17213:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
17214:                                                         '</span><br />';
17215:                                         }
17216:                                     }
17217:                                 }
17218:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
17219:                                     if (!@statuses) {
17220:                                         $chgtext .= '<span class="LC_warning">'.
17221:                                                     &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.").
17222:                                                     '</span><br />';
17223: 
17224:                                     }
17225:                                 }
17226:                             }
17227:                         }
17228:                     } elsif ($type eq 'shibenv') {
17229:                         if (keys(%{$cancreate{$type}}) == 0) {
17230:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
17231:                         } else {
17232:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
17233:                                         '<ul>';
17234:                             foreach my $field (@shibfields) {
17235:                                 next if ($cancreate{$type}{$field} eq '');
17236:                                 if ($field eq 'inststatus') {
17237:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
17238:                                 } else {
17239:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
17240:                                 }
17241:                             }
17242:                             $chgtext .= '</ul>';
17243:                         }
17244:                     } elsif ($type eq 'statustocreate') {
17245:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
17246:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
17247:                             if (@{$cancreate{'selfcreate'}} > 0) {
17248:                                 if (@{$cancreate{'statustocreate'}} == 0) {
17249:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
17250:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
17251:                                         $chgtext .= '<br />'.
17252:                                                     '<span class="LC_warning">'.
17253:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
17254:                                                     '</span>';
17255:                                     }
17256:                                 } elsif (keys(%usertypes) > 0) {
17257:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
17258:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
17259:                                     } else {
17260:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
17261:                                     }
17262:                                     $chgtext .= '<ul>';
17263:                                     foreach my $case (@{$cancreate{$type}}) {
17264:                                         if ($case eq 'default') {
17265:                                             $chgtext .= '<li>'.$othertitle.'</li>';
17266:                                         } else {
17267:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
17268:                                         }
17269:                                     }
17270:                                     $chgtext .= '</ul>';
17271:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
17272:                                         $chgtext .= '<span class="LC_warning">'.
17273:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
17274:                                                     '</span>';
17275:                                     }
17276:                                 }
17277:                             } else {
17278:                                 if (@{$cancreate{$type}} == 0) {
17279:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
17280:                                 } else {
17281:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
17282:                                 }
17283:                             }
17284:                             $chgtext .= '<br />';
17285:                         }
17286:                     } elsif ($type eq 'selfcreateprocessing') {
17287:                         my %choices = &Apache::lonlocal::texthash (
17288:                                                                     automatic => 'Automatic approval',
17289:                                                                     approval  => 'Queued for approval',
17290:                                                                   );
17291:                         if (@types) {
17292:                             if (@statuses) {
17293:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:'). 
17294:                                             '<ul>';
17295:                                 foreach my $status (@statuses) {
17296:                                     if ($status eq 'default') {
17297:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
17298:                                     } else {
17299:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
17300:                                     }
17301:                                 }
17302:                                 $chgtext .= '</ul>';
17303:                             }
17304:                         } else {
17305:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
17306:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
17307:                         }
17308:                     } elsif ($type eq 'emailverified') {
17309:                         my %options = &Apache::lonlocal::texthash (
17310:                                                                     all   => 'Same as e-mail',
17311:                                                                     first => 'Omit @domain',
17312:                                                                     free  => 'Free to choose',
17313:                                                                   );
17314:                         if (@types) {
17315:                             if (@statuses) {
17316:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
17317:                                             '<ul>';
17318:                                 foreach my $status (@statuses) {
17319:                                     if ($status eq 'default') {
17320:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
17321:                                     } else {
17322:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
17323:                                     }
17324:                                 }
17325:                                 $chgtext .= '</ul>';
17326:                             }
17327:                         } else {
17328:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
17329:                                             $options{$cancreate{'emailverified'}{'default'}});
17330:                         }
17331:                     } elsif ($type eq 'emailoptions') {
17332:                         my %options = &Apache::lonlocal::texthash (
17333:                                                                     any     => 'Any e-mail',
17334:                                                                     inst    => 'Institutional only',
17335:                                                                     noninst => 'Non-institutional only',
17336:                                                                     custom  => 'Custom restrictions',
17337:                                                                   );
17338:                         if (@types) {
17339:                             if (@statuses) {
17340:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
17341:                                             '<ul>';
17342:                                 foreach my $status (@statuses) {
17343:                                     if ($type eq 'default') {
17344:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
17345:                                     } else {
17346:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
17347:                                     }
17348:                                 }
17349:                                 $chgtext .= '</ul>';
17350:                             }
17351:                         } else {
17352:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
17353:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
17354:                             } else {
17355:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
17356:                                                 $options{$cancreate{'emailoptions'}{'default'}});
17357:                             }
17358:                         }
17359:                     } elsif ($type eq 'emaildomain') {
17360:                         my $output;
17361:                         if (@statuses) {
17362:                             foreach my $type (@statuses) {
17363:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
17364:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
17365:                                         if ($type eq 'default') {
17366:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
17367:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
17368:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
17369:                                             } else {
17370:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
17371:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
17372:                                             }
17373:                                         } else {
17374:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
17375:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
17376:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
17377:                                             } else {
17378:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
17379:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>'; 
17380:                                             }
17381:                                         }
17382:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
17383:                                         if ($type eq 'default') {
17384:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
17385:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
17386:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
17387:                                             } else {
17388:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
17389:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
17390:                                             }
17391:                                         } else {
17392:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
17393:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
17394:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
17395:                                             } else {
17396:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
17397:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';   
17398:                                             }
17399:                                         }
17400:                                     }
17401:                                 }
17402:                             }
17403:                         }
17404:                         if ($output ne '') {
17405:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
17406:                                         '<ul>'.$output.'</ul>';
17407:                         }
17408:                     } elsif ($type eq 'captcha') {
17409:                         if ($savecaptcha{$type} eq 'notused') {
17410:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
17411:                         } else {
17412:                             my %captchas = &captcha_phrases();
17413:                             if ($captchas{$savecaptcha{$type}}) {
17414:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
17415:                             } else {
17416:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
17417:                             }
17418:                         }
17419:                     } elsif ($type eq 'recaptchakeys') {
17420:                         my ($privkey,$pubkey);
17421:                         if (ref($savecaptcha{$type}) eq 'HASH') {
17422:                             $pubkey = $savecaptcha{$type}{'public'};
17423:                             $privkey = $savecaptcha{$type}{'private'};
17424:                         }
17425:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
17426:                         if (!$pubkey) {
17427:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
17428:                         } else {
17429:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
17430:                         }
17431:                         if (!$privkey) {
17432:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
17433:                         } else {
17434:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
17435:                         }
17436:                         $chgtext .= '</ul>';
17437:                     } elsif ($type eq 'recaptchaversion') {
17438:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
17439:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
17440:                         }
17441:                     } elsif ($type eq 'emailusername') {
17442:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
17443:                             if (@statuses) {
17444:                                 foreach my $type (@statuses) {
17445:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
17446:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
17447:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
17448:                                                     '<ul>';
17449:                                             foreach my $field (@{$infofields}) {
17450:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
17451:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
17452:                                                 }
17453:                                             }
17454:                                             $chgtext .= '</ul>';
17455:                                         } else {
17456:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
17457:                                         }
17458:                                     } else {
17459:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
17460:                                     }
17461:                                 }
17462:                             }
17463:                         }
17464:                     } elsif ($type eq 'notify') {
17465:                         my $numapprove = 0;
17466:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
17467:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
17468:                                 if ($cancreate{'notify'}{'approval'}) {
17469:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
17470:                                     $numapprove ++;
17471:                                 }
17472:                             }
17473:                         }
17474:                         unless ($numapprove) {
17475:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
17476:                         }
17477:                     }
17478:                     if ($chgtext) {
17479:                         $resulttext .= '<li>'.$chgtext.'</li>';
17480:                     }
17481:                 }
17482:             }
17483:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
17484:                 my ($emailrules,$emailruleorder) =
17485:                     &Apache::lonnet::inst_userrules($dom,'email');
17486:                 foreach my $type (@{$changes{'email_rule'}}) {
17487:                     if (ref($email_rule{$type}) eq 'ARRAY') {
17488:                         my $chgtext = '<ul>';
17489:                         foreach my $rule (@{$email_rule{$type}}) {
17490:                             if (ref($emailrules->{$rule}) eq 'HASH') {
17491:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
17492:                             }
17493:                         }
17494:                         $chgtext .= '</ul>';
17495:                         my $typename;
17496:                         if (@types) {
17497:                             if ($type eq 'default') {
17498:                                 $typename = $othertitle;
17499:                             } else {
17500:                                 $typename = $usertypes{$type};
17501:                             } 
17502:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
17503:                         }
17504:                         if (@{$email_rule{$type}} > 0) {
17505:                             $resulttext .= '<li>'.
17506:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
17507:                                                $usertypes{$type}).
17508:                                            $chgtext.
17509:                                            '</li>';
17510:                         } else {
17511:                             $resulttext .= '<li>'.
17512:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
17513:                                            '</li>'.
17514:                                            &mt('(Affiliation: [_1])',$typename);
17515:                         }
17516:                     }
17517:                 }
17518:             }
17519:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
17520:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
17521:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
17522:                         my $chgtext = '<ul>';
17523:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
17524:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
17525:                         }
17526:                         $chgtext .= '</ul>';
17527:                         $resulttext .= '<li>'.
17528:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
17529:                                           $chgtext.
17530:                                        '</li>';
17531:                     } else {
17532:                         $resulttext .= '<li>'.
17533:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
17534:                                        '</li>';
17535:                     }
17536:                 }
17537:             }
17538:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
17539:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
17540:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
17541:                 foreach my $type (@{$changes{'selfcreate'}}) {
17542:                     my $typename = $type;
17543:                     if (keys(%usertypes) > 0) {
17544:                         if ($usertypes{$type} ne '') {
17545:                             $typename = $usertypes{$type};
17546:                         }
17547:                     }
17548:                     my @modifiable;
17549:                     $resulttext .= '<li>'.
17550:                                     &mt('Self-creation of account by users with status: [_1]',
17551:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
17552:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
17553:                     foreach my $field (@fields) {
17554:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
17555:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
17556:                         }
17557:                     }
17558:                     if (@modifiable > 0) {
17559:                         $resulttext .= join(', ',@modifiable);
17560:                     } else {
17561:                         $resulttext .= &mt('none');
17562:                     }
17563:                     $resulttext .= '</li>';
17564:                 }
17565:                 $resulttext .= '</ul></li>';
17566:             }
17567:             $resulttext .= '</ul>';
17568:             my $cachetime = 24*60*60;
17569:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
17570:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17571:             if (ref($lastactref) eq 'HASH') {
17572:                 $lastactref->{'domdefaults'} = 1;
17573:             }
17574:         } else {
17575:             $resulttext = &mt('No changes made to self-creation settings');
17576:         }
17577:     } else {
17578:         $resulttext = '<span class="LC_error">'.
17579:             &mt('An error occurred: [_1]',$putresult).'</span>';
17580:     }
17581:     if ($warningmsg ne '') {
17582:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
17583:     }
17584:     return $resulttext;
17585: }
17586: 
17587: sub process_captcha {
17588:     my ($container,$changes,$newsettings,$currsettings) = @_;
17589:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH'));
17590:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
17591:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
17592:         $newsettings->{'captcha'} = 'original';
17593:     }
17594:     my %current;
17595:     if (ref($currsettings) eq 'HASH') {
17596:         %current = %{$currsettings};
17597:     }
17598:     if ($current{'captcha'} ne $newsettings->{'captcha'}) {
17599:         if ($container eq 'cancreate') {
17600:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
17601:                 push(@{$changes->{'cancreate'}},'captcha');
17602:             } elsif (!defined($changes->{'cancreate'})) {
17603:                 $changes->{'cancreate'} = ['captcha'];
17604:             }
17605:         } elsif ($container eq 'passwords') {
17606:             $changes->{'reset'} = 1;
17607:         } else {
17608:             $changes->{'captcha'} = 1;
17609:         }
17610:     }
17611:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
17612:     if ($newsettings->{'captcha'} eq 'recaptcha') {
17613:         $newpub = $env{'form.'.$container.'_recaptchapub'};
17614:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
17615:         $newpub =~ s/[^\w\-]//g;
17616:         $newpriv =~ s/[^\w\-]//g;
17617:         $newsettings->{'recaptchakeys'} = {
17618:                                              public  => $newpub,
17619:                                              private => $newpriv,
17620:                                           };
17621:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
17622:         $newversion =~ s/\D//g;
17623:         if ($newversion ne '2') {
17624:             $newversion = 1;
17625:         }
17626:         $newsettings->{'recaptchaversion'} = $newversion;
17627:     }
17628:     if (ref($current{'recaptchakeys'}) eq 'HASH') {
17629:         $currpub = $current{'recaptchakeys'}{'public'};
17630:         $currpriv = $current{'recaptchakeys'}{'private'};
17631:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
17632:             $newsettings->{'recaptchakeys'} = {
17633:                                                  public  => '',
17634:                                                  private => '',
17635:                                               }
17636:         }
17637:     }
17638:     if ($current{'captcha'} eq 'recaptcha') {
17639:         $currversion = $current{'recaptchaversion'};
17640:         if ($currversion ne '2') {
17641:             $currversion = 1;
17642:         }
17643:     }
17644:     if ($currversion ne $newversion) {
17645:         if ($container eq 'cancreate') {
17646:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
17647:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
17648:             } elsif (!defined($changes->{'cancreate'})) {
17649:                 $changes->{'cancreate'} = ['recaptchaversion'];
17650:             }
17651:         } elsif ($container eq 'passwords') {
17652:             $changes->{'reset'} = 1;
17653:         } else {
17654:             $changes->{'recaptchaversion'} = 1;
17655:         }
17656:     }
17657:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
17658:         if ($container eq 'cancreate') {
17659:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
17660:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
17661:             } elsif (!defined($changes->{'cancreate'})) {
17662:                 $changes->{'cancreate'} = ['recaptchakeys'];
17663:             }
17664:         } elsif ($container eq 'passwords') {
17665:             $changes->{'reset'} = 1;
17666:         } else {
17667:             $changes->{'recaptchakeys'} = 1;
17668:         }
17669:     }
17670:     return;
17671: }
17672: 
17673: sub modify_usermodification {
17674:     my ($dom,%domconfig) = @_;
17675:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
17676:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
17677:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
17678:             if ($key eq 'selfcreate') {
17679:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
17680:             } else {  
17681:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
17682:             }
17683:         }
17684:     }
17685:     my @contexts = ('author','course');
17686:     my %context_title = (
17687:                            author => 'In author context',
17688:                            course => 'In course context',
17689:                         );
17690:     my @fields = ('lastname','firstname','middlename','generation',
17691:                   'permanentemail','id');
17692:     my %roles = (
17693:                   author => ['ca','aa'],
17694:                   course => ['st','ep','ta','in','cr'],
17695:                 );
17696:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
17697:     foreach my $context (@contexts) {
17698:         foreach my $role (@{$roles{$context}}) {
17699:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
17700:             foreach my $item (@fields) {
17701:                 if (grep(/^\Q$item\E$/,@modifiable)) {
17702:                     $modifyhash{$context}{$role}{$item} = 1;
17703:                 } else {
17704:                     $modifyhash{$context}{$role}{$item} = 0;
17705:                 }
17706:             }
17707:         }
17708:         if (ref($curr_usermodification{$context}) eq 'HASH') {
17709:             foreach my $role (@{$roles{$context}}) {
17710:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
17711:                     foreach my $field (@fields) {
17712:                         if ($modifyhash{$context}{$role}{$field} ne 
17713:                                 $curr_usermodification{$context}{$role}{$field}) {
17714:                             push(@{$changes{$context}},$role);
17715:                             last;
17716:                         }
17717:                     }
17718:                 }
17719:             }
17720:         } else {
17721:             foreach my $context (@contexts) {
17722:                 foreach my $role (@{$roles{$context}}) {
17723:                     push(@{$changes{$context}},$role);
17724:                 }
17725:             }
17726:         }
17727:     }
17728:     my %usermodification_hash =  (
17729:                                    usermodification => \%modifyhash,
17730:                                  );
17731:     my $putresult = &Apache::lonnet::put_dom('configuration',
17732:                                              \%usermodification_hash,$dom);
17733:     if ($putresult eq 'ok') {
17734:         if (keys(%changes) > 0) {
17735:             $resulttext = &mt('Changes made: ').'<ul>';
17736:             foreach my $context (@contexts) {
17737:                 if (ref($changes{$context}) eq 'ARRAY') {
17738:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
17739:                     if (ref($changes{$context}) eq 'ARRAY') {
17740:                         foreach my $role (@{$changes{$context}}) {
17741:                             my $rolename;
17742:                             if ($role eq 'cr') {
17743:                                 $rolename = &mt('Custom');
17744:                             } else {
17745:                                 $rolename = &Apache::lonnet::plaintext($role);
17746:                             }
17747:                             my @modifiable;
17748:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
17749:                             foreach my $field (@fields) {
17750:                                 if ($modifyhash{$context}{$role}{$field}) {
17751:                                     push(@modifiable,$fieldtitles{$field});
17752:                                 }
17753:                             }
17754:                             if (@modifiable > 0) {
17755:                                 $resulttext .= join(', ',@modifiable);
17756:                             } else {
17757:                                 $resulttext .= &mt('none'); 
17758:                             }
17759:                             $resulttext .= '</li>';
17760:                         }
17761:                         $resulttext .= '</ul></li>';
17762:                     }
17763:                 }
17764:             }
17765:             $resulttext .= '</ul>';
17766:         } else {
17767:             $resulttext = &mt('No changes made to user modification settings');
17768:         }
17769:     } else {
17770:         $resulttext = '<span class="LC_error">'.
17771:             &mt('An error occurred: [_1]',$putresult).'</span>';
17772:     }
17773:     return $resulttext;
17774: }
17775: 
17776: sub modify_defaults {
17777:     my ($dom,$lastactref,%domconfig) = @_;
17778:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
17779:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17780:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
17781:                  'portal_def');
17782:     my @authtypes = ('internal','krb4','krb5','localauth','lti');
17783:     foreach my $item (@items) {
17784:         $newvalues{$item} = $env{'form.'.$item};
17785:         if ($item eq 'auth_def') {
17786:             if ($newvalues{$item} ne '') {
17787:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
17788:                     push(@errors,$item);
17789:                 }
17790:             }
17791:         } elsif ($item eq 'lang_def') {
17792:             if ($newvalues{$item} ne '') {
17793:                 if ($newvalues{$item} =~ /^(\w+)/) {
17794:                     my $langcode = $1;
17795:                     if ($langcode ne 'x_chef') {
17796:                         if (code2language($langcode) eq '') {
17797:                             push(@errors,$item);
17798:                         }
17799:                     }
17800:                 } else {
17801:                     push(@errors,$item);
17802:                 }
17803:             }
17804:         } elsif ($item eq 'timezone_def') {
17805:             if ($newvalues{$item} ne '') {
17806:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
17807:                     push(@errors,$item);   
17808:                 }
17809:             }
17810:         } elsif ($item eq 'datelocale_def') {
17811:             if ($newvalues{$item} ne '') {
17812:                 my @datelocale_ids = DateTime::Locale->ids();
17813:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
17814:                     push(@errors,$item);
17815:                 }
17816:             }
17817:         } elsif ($item eq 'portal_def') {
17818:             if ($newvalues{$item} ne '') {
17819:                 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])\/?$/) {
17820:                     push(@errors,$item);
17821:                 }
17822:             }
17823:         }
17824:         if (grep(/^\Q$item\E$/,@errors)) {
17825:             $newvalues{$item} = $domdefaults{$item};
17826:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
17827:             $changes{$item} = 1;
17828:         }
17829:         $domdefaults{$item} = $newvalues{$item};
17830:     }
17831:     my %staticdefaults = (
17832:                            'intauth_cost'   => 10,
17833:                            'intauth_check'  => 0,
17834:                            'intauth_switch' => 0,
17835:                          );
17836:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
17837:         if (exists($domdefaults{$item})) {
17838:             $newvalues{$item} = $domdefaults{$item};
17839:         } else {
17840:             $newvalues{$item} = $staticdefaults{$item};
17841:         }
17842:     }
17843:     my %defaults_hash = (
17844:                          defaults => \%newvalues,
17845:                         );
17846:     my $title = &defaults_titles();
17847: 
17848:     my $currinststatus;
17849:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
17850:         $currinststatus = $domconfig{'inststatus'};
17851:     } else {
17852:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
17853:         $currinststatus = {
17854:                              inststatustypes => $usertypes,
17855:                              inststatusorder => $types,
17856:                              inststatusguest => [],
17857:                           };
17858:     }
17859:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
17860:     my @allpos;
17861:     my %alltypes;
17862:     my @inststatusguest;
17863:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
17864:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
17865:             unless (grep(/^\Q$type\E$/,@todelete)) {
17866:                 push(@inststatusguest,$type);
17867:             }
17868:         }
17869:     }
17870:     my ($currtitles,$currorder);
17871:     if (ref($currinststatus) eq 'HASH') {
17872:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
17873:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
17874:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
17875:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
17876:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
17877:                     }
17878:                 }
17879:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
17880:                     my $position = $env{'form.inststatus_pos_'.$type};
17881:                     $position =~ s/\D+//g;
17882:                     $allpos[$position] = $type;
17883:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
17884:                     $alltypes{$type} =~ s/`//g;
17885:                 }
17886:             }
17887:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
17888:             $currtitles =~ s/,$//;
17889:         }
17890:     }
17891:     if ($env{'form.addinststatus'}) {
17892:         my $newtype = $env{'form.addinststatus'};
17893:         $newtype =~ s/\W//g;
17894:         unless (exists($alltypes{$newtype})) {
17895:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
17896:             $alltypes{$newtype} =~ s/`//g; 
17897:             my $position = $env{'form.addinststatus_pos'};
17898:             $position =~ s/\D+//g;
17899:             if ($position ne '') {
17900:                 $allpos[$position] = $newtype;
17901:             }
17902:         }
17903:     }
17904:     my @orderedstatus;
17905:     foreach my $type (@allpos) {
17906:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
17907:             push(@orderedstatus,$type);
17908:         }
17909:     }
17910:     foreach my $type (keys(%alltypes)) {
17911:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
17912:             delete($alltypes{$type});
17913:         }
17914:     }
17915:     $defaults_hash{'inststatus'} = {
17916:                                      inststatustypes => \%alltypes,
17917:                                      inststatusorder => \@orderedstatus,
17918:                                      inststatusguest => \@inststatusguest,
17919:                                    };
17920:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
17921:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
17922:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
17923:         }
17924:     }
17925:     if ($currorder ne join(',',@orderedstatus)) {
17926:         $changes{'inststatus'}{'inststatusorder'} = 1;
17927:     }
17928:     my $newtitles;
17929:     foreach my $item (@orderedstatus) {
17930:         $newtitles .= $alltypes{$item}.',';
17931:     }
17932:     $newtitles =~ s/,$//;
17933:     if ($currtitles ne $newtitles) {
17934:         $changes{'inststatus'}{'inststatustypes'} = 1;
17935:     }
17936:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
17937:                                              $dom);
17938:     if ($putresult eq 'ok') {
17939:         if (keys(%changes) > 0) {
17940:             $resulttext = &mt('Changes made:').'<ul>';
17941:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
17942:             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";
17943:             foreach my $item (sort(keys(%changes))) {
17944:                 if ($item eq 'inststatus') {
17945:                     if (ref($changes{'inststatus'}) eq 'HASH') {
17946:                         if (@orderedstatus) {
17947:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
17948:                             foreach my $type (@orderedstatus) { 
17949:                                 $resulttext .= $alltypes{$type}.', ';
17950:                             }
17951:                             $resulttext =~ s/, $//;
17952:                             $resulttext .= '</li>';
17953:                         } else {
17954:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>'; 
17955:                         }
17956:                     }
17957:                 } else {
17958:                     my $value = $env{'form.'.$item};
17959:                     if ($value eq '') {
17960:                         $value = &mt('none');
17961:                     } elsif ($item eq 'auth_def') {
17962:                         my %authnames = &authtype_names();
17963:                         my %shortauth = (
17964:                                           internal   => 'int',
17965:                                           krb4       => 'krb4',
17966:                                           krb5       => 'krb5',
17967:                                           localauth  => 'loc',
17968:                                           lti        => 'lti',
17969:                         );
17970:                         $value = $authnames{$shortauth{$value}};
17971:                     }
17972:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
17973:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
17974:                 }
17975:             }
17976:             $resulttext .= '</ul>';
17977:             $mailmsgtext .= "\n";
17978:             my $cachetime = 24*60*60;
17979:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17980:             if (ref($lastactref) eq 'HASH') {
17981:                 $lastactref->{'domdefaults'} = 1;
17982:             }
17983:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
17984:                 my $notify = 1;
17985:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
17986:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
17987:                         $notify = 0;
17988:                     }
17989:                 }
17990:                 if ($notify) {
17991:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
17992:                                                "LON-CAPA Domain Settings Change - $dom",
17993:                                                $mailmsgtext);
17994:                 }
17995:             }
17996:         } else {
17997:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
17998:         }
17999:     } else {
18000:         $resulttext = '<span class="LC_error">'.
18001:             &mt('An error occurred: [_1]',$putresult).'</span>';
18002:     }
18003:     if (@errors > 0) {
18004:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
18005:         foreach my $item (@errors) {
18006:             $resulttext .= ' "'.$title->{$item}.'",';
18007:         }
18008:         $resulttext =~ s/,$//;
18009:     }
18010:     return $resulttext;
18011: }
18012: 
18013: sub modify_scantron {
18014:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
18015:     my ($resulttext,%confhash,%changes,$errors);
18016:     my $custom = 'custom.tab';
18017:     my $default = 'default.tab';
18018:     my $servadm = $r->dir_config('lonAdmEMail');
18019:     my ($configuserok,$author_ok,$switchserver) =
18020:         &config_check($dom,$confname,$servadm);
18021:     if ($env{'form.scantronformat.filename'} ne '') {
18022:         my $error;
18023:         if ($configuserok eq 'ok') {
18024:             if ($switchserver) {
18025:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
18026:             } else {
18027:                 if ($author_ok eq 'ok') {
18028:                     my ($result,$scantronurl) =
18029:                         &publishlogo($r,'upload','scantronformat',$dom,
18030:                                      $confname,'scantron','','',$custom);
18031:                     if ($result eq 'ok') {
18032:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
18033:                         $changes{'scantronformat'} = 1;
18034:                     } else {
18035:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
18036:                     }
18037:                 } else {
18038:                     $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);
18039:                 }
18040:             }
18041:         } else {
18042:             $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);
18043:         }
18044:         if ($error) {
18045:             &Apache::lonnet::logthis($error);
18046:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
18047:         }
18048:     }
18049:     if (ref($domconfig{'scantron'}) eq 'HASH') {
18050:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
18051:             if ($env{'form.scantronformat_del'}) {
18052:                 $confhash{'scantron'}{'scantronformat'} = '';
18053:                 $changes{'scantronformat'} = 1;
18054:             } else {
18055:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
18056:             }
18057:         }
18058:     }
18059:     my @options = ('hdr','pad','rem');
18060:     my @fields = &scantroncsv_fields();
18061:     my %titles = &scantronconfig_titles();
18062:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
18063:     my ($newdat,$currdat,%newcol,%currcol);
18064:     if (grep(/^dat$/,@formats)) {
18065:         $confhash{'scantron'}{config}{dat} = 1;
18066:         $newdat = 1;
18067:     } else {
18068:         $newdat = 0;
18069:     }
18070:     if (grep(/^csv$/,@formats)) {
18071:         my %bynum;
18072:         foreach my $field (@fields) {
18073:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
18074:                 my $posscol = $1;
18075:                 if (($posscol < 20) && (!$bynum{$posscol})) {
18076:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
18077:                     $bynum{$posscol} = $field;
18078:                     $newcol{$field} = $posscol;
18079:                 }
18080:             }
18081:         }
18082:         if (keys(%newcol)) {
18083:             foreach my $option (@options) {
18084:                 if ($env{'form.scantroncsv_'.$option}) {
18085:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
18086:                 }
18087:             }
18088:         }
18089:     }
18090:     $currdat = 1;
18091:     if (ref($domconfig{'scantron'}) eq 'HASH') {
18092:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
18093:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
18094:                 $currdat = 0;
18095:             }
18096:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
18097:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
18098:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
18099:                 }
18100:             }
18101:         }
18102:     }
18103:     if ($currdat != $newdat) {
18104:         $changes{'config'} = 1;
18105:     } else {
18106:         foreach my $field (@fields) {
18107:             if ($currcol{$field} ne '') {
18108:                 if ($currcol{$field} ne $newcol{$field}) {
18109:                     $changes{'config'} = 1;
18110:                     last;
18111:                 }
18112:             } elsif ($newcol{$field} ne '') {
18113:                 $changes{'config'} = 1;
18114:                 last;
18115:             }
18116:         }
18117:     }
18118:     if (keys(%confhash) > 0) {
18119:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
18120:                                                  $dom);
18121:         if ($putresult eq 'ok') {
18122:             if (keys(%changes) > 0) {
18123:                 if (ref($confhash{'scantron'}) eq 'HASH') {
18124:                     $resulttext = &mt('Changes made:').'<ul>';
18125:                     if ($changes{'scantronformat'}) {
18126:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
18127:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
18128:                         } else {
18129:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
18130:                         }
18131:                     }
18132:                     if ($changes{'config'}) {
18133:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
18134:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
18135:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
18136:                             }
18137:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
18138:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
18139:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
18140:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
18141:                                         foreach my $field (@fields) {
18142:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
18143:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
18144:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
18145:                                             }
18146:                                         }
18147:                                         $resulttext .= '</ul></li>';
18148:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
18149:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
18150:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
18151:                                                 foreach my $option (@options) {
18152:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
18153:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
18154:                                                     }
18155:                                                 }
18156:                                                 $resulttext .= '</ul></li>';
18157:                                             }
18158:                                         }
18159:                                     }
18160:                                 }
18161:                             }
18162:                         } else {
18163:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
18164:                         }
18165:                     }
18166:                     $resulttext .= '</ul>';
18167:                 } else {
18168:                     $resulttext = &mt('Changes made to bubblesheet format file.');
18169:                 }
18170:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
18171:                 if (ref($lastactref) eq 'HASH') {
18172:                     $lastactref->{'domainconfig'} = 1;
18173:                 }
18174:             } else {
18175:                 $resulttext = &mt('No changes made to bubblesheet format settings');
18176:             }
18177:         } else {
18178:             $resulttext = '<span class="LC_error">'.
18179:                 &mt('An error occurred: [_1]',$putresult).'</span>';
18180:         }
18181:     } else {
18182:         $resulttext = &mt('No changes made to bubblesheet format file'); 
18183:     }
18184:     if ($errors) {
18185:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
18186:                        $errors.'</ul></p>';
18187:     }
18188:     return $resulttext;
18189: }
18190: 
18191: sub modify_coursecategories {
18192:     my ($dom,$lastactref,%domconfig) = @_;
18193:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
18194:         $cathash);
18195:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
18196:     my @catitems = ('unauth','auth');
18197:     my @cattypes = ('std','domonly','codesrch','none');
18198:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
18199:         $cathash = $domconfig{'coursecategories'}{'cats'};
18200:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
18201:             $changes{'togglecats'} = 1;
18202:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
18203:         }
18204:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
18205:             $changes{'categorize'} = 1;
18206:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
18207:         }
18208:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
18209:             $changes{'togglecatscomm'} = 1;
18210:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
18211:         }
18212:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
18213:             $changes{'categorizecomm'} = 1;
18214:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
18215: 
18216:         }
18217:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
18218:             $changes{'togglecatsplace'} = 1;
18219:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
18220:         }
18221:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
18222:             $changes{'categorizeplace'} = 1;
18223:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
18224:         }
18225:         foreach my $item (@catitems) {
18226:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
18227:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
18228:                     $changes{$item} = 1;
18229:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
18230:                 }
18231:             }
18232:         }
18233:     } else {
18234:         $changes{'togglecats'} = 1;
18235:         $changes{'categorize'} = 1;
18236:         $changes{'togglecatscomm'} = 1;
18237:         $changes{'categorizecomm'} = 1;
18238:         $changes{'togglecatsplace'} = 1;
18239:         $changes{'categorizeplace'} = 1;
18240:         $domconfig{'coursecategories'} = {
18241:                                              togglecats => $env{'form.togglecats'},
18242:                                              categorize => $env{'form.categorize'},
18243:                                              togglecatscomm => $env{'form.togglecatscomm'},
18244:                                              categorizecomm => $env{'form.categorizecomm'},
18245:                                              togglecatsplace => $env{'form.togglecatsplace'},
18246:                                              categorizeplace => $env{'form.categorizeplace'},
18247:                                          };
18248:         foreach my $item (@catitems) {
18249:             if ($env{'form.coursecat_'.$item} ne 'std') {
18250:                 $changes{$item} = 1;
18251:             }
18252:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
18253:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
18254:             }
18255:         }
18256:     }
18257:     if (ref($cathash) eq 'HASH') {
18258:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
18259:             push (@deletecategory,'instcode::0');
18260:         }
18261:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
18262:             push(@deletecategory,'communities::0');
18263:         }
18264:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
18265:             push(@deletecategory,'placement::0');
18266:         }
18267:     }
18268:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
18269:     if (ref($cathash) eq 'HASH') {
18270:         if (@deletecategory > 0) {
18271:             #FIXME Need to remove category from all courses using a deleted category 
18272:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
18273:             foreach my $item (@deletecategory) {
18274:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
18275:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
18276:                     $deletions{$item} = 1;
18277:                     &recurse_cat_deletes($item,$cathash,\%deletions);
18278:                 }
18279:             }
18280:         }
18281:         foreach my $item (keys(%{$cathash})) {
18282:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
18283:             if ($cathash->{$item} ne $env{'form.'.$item}) {
18284:                 $reorderings{$item} = 1;
18285:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
18286:             }
18287:             if ($env{'form.addcategory_name_'.$item} ne '') {
18288:                 my $newcat = $env{'form.addcategory_name_'.$item};
18289:                 my $newdepth = $depth+1;
18290:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
18291:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
18292:                 $adds{$newitem} = 1; 
18293:             }
18294:             if ($env{'form.subcat_'.$item} ne '') {
18295:                 my $newcat = $env{'form.subcat_'.$item};
18296:                 my $newdepth = $depth+1;
18297:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
18298:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
18299:                 $adds{$newitem} = 1;
18300:             }
18301:         }
18302:     }
18303:     if ($env{'form.instcode'} eq '1') {
18304:         if (ref($cathash) eq 'HASH') {
18305:             my $newitem = 'instcode::0';
18306:             if ($cathash->{$newitem} eq '') {  
18307:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
18308:                 $adds{$newitem} = 1;
18309:             }
18310:         } else {
18311:             my $newitem = 'instcode::0';
18312:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
18313:             $adds{$newitem} = 1;
18314:         }
18315:     }
18316:     if ($env{'form.communities'} eq '1') {
18317:         if (ref($cathash) eq 'HASH') {
18318:             my $newitem = 'communities::0';
18319:             if ($cathash->{$newitem} eq '') {
18320:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
18321:                 $adds{$newitem} = 1;
18322:             }
18323:         } else {
18324:             my $newitem = 'communities::0';
18325:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
18326:             $adds{$newitem} = 1;
18327:         }
18328:     }
18329:     if ($env{'form.placement'} eq '1') {
18330:         if (ref($cathash) eq 'HASH') {
18331:             my $newitem = 'placement::0';
18332:             if ($cathash->{$newitem} eq '') {
18333:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
18334:                 $adds{$newitem} = 1;
18335:             }
18336:         } else {
18337:             my $newitem = 'placement::0';
18338:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
18339:             $adds{$newitem} = 1;
18340:         }
18341:     }
18342:     if ($env{'form.addcategory_name'} ne '') {
18343:         if (($env{'form.addcategory_name'} ne 'instcode') &&
18344:             ($env{'form.addcategory_name'} ne 'communities') &&
18345:             ($env{'form.addcategory_name'} ne 'placement')) {
18346:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
18347:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
18348:             $adds{$newitem} = 1;
18349:         }
18350:     }
18351:     my $putresult;
18352:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
18353:         if (keys(%deletions) > 0) {
18354:             foreach my $key (keys(%deletions)) {
18355:                 if ($predelallitems{$key} ne '') {
18356:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
18357:                 }
18358:             }
18359:         }
18360:         my (@chkcats,@chktrails,%chkallitems);
18361:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
18362:         if (ref($chkcats[0]) eq 'ARRAY') {
18363:             my $depth = 0;
18364:             my $chg = 0;
18365:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
18366:                 my $name = $chkcats[0][$i];
18367:                 my $item;
18368:                 if ($name eq '') {
18369:                     $chg ++;
18370:                 } else {
18371:                     $item = &escape($name).'::0';
18372:                     if ($chg) {
18373:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
18374:                     }
18375:                     $depth ++; 
18376:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
18377:                     $depth --;
18378:                 }
18379:             }
18380:         }
18381:     }
18382:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
18383:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
18384:         if ($putresult eq 'ok') {
18385:             my %title = (
18386:                          togglecats     => 'Show/Hide a course in catalog',
18387:                          categorize     => 'Assign a category to a course',
18388:                          togglecatscomm => 'Show/Hide a community in catalog',
18389:                          categorizecomm => 'Assign a category to a community',
18390:                         );
18391:             my %level = (
18392:                          dom  => 'set in Domain ("Modify Course/Community")',
18393:                          crs  => 'set in Course ("Course Configuration")',
18394:                          comm => 'set in Community ("Community Configuration")',
18395:                          none     => 'No catalog',
18396:                          std      => 'Standard catalog',
18397:                          domonly  => 'Domain-only catalog',
18398:                          codesrch => 'Code search form',
18399:                         );
18400:             $resulttext = &mt('Changes made:').'<ul>';
18401:             if ($changes{'togglecats'}) {
18402:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
18403:             }
18404:             if ($changes{'categorize'}) {
18405:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
18406:             }
18407:             if ($changes{'togglecatscomm'}) {
18408:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
18409:             }
18410:             if ($changes{'categorizecomm'}) {
18411:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
18412:             }
18413:             if ($changes{'unauth'}) {
18414:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
18415:             }
18416:             if ($changes{'auth'}) {
18417:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
18418:             }
18419:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
18420:                 my $cathash;
18421:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
18422:                     $cathash = $domconfig{'coursecategories'}{'cats'};
18423:                 } else {
18424:                     $cathash = {};
18425:                 } 
18426:                 my (@cats,@trails,%allitems);
18427:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
18428:                 if (keys(%deletions) > 0) {
18429:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
18430:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
18431:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
18432:                     }
18433:                     $resulttext .= '</ul></li>';
18434:                 }
18435:                 if (keys(%reorderings) > 0) {
18436:                     my %sort_by_trail;
18437:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
18438:                     foreach my $key (keys(%reorderings)) {
18439:                         if ($allitems{$key} ne '') {
18440:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
18441:                         }
18442:                     }
18443:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
18444:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
18445:                     }
18446:                     $resulttext .= '</ul></li>';
18447:                 }
18448:                 if (keys(%adds) > 0) {
18449:                     my %sort_by_trail;
18450:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
18451:                     foreach my $key (keys(%adds)) {
18452:                         if ($allitems{$key} ne '') {
18453:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
18454:                         }
18455:                     }
18456:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
18457:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
18458:                     }
18459:                     $resulttext .= '</ul></li>';
18460:                 }
18461:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
18462:                 if (ref($lastactref) eq 'HASH') {
18463:                     $lastactref->{'cats'} = 1;
18464:                 }
18465:             }
18466:             $resulttext .= '</ul>';
18467:             if ($changes{'unauth'} || $changes{'auth'}) {
18468:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
18469:                 if ($changes{'auth'}) {
18470:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
18471:                 }
18472:                 if ($changes{'unauth'}) {
18473:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
18474:                 }
18475:                 my $cachetime = 24*60*60;
18476:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18477:                 if (ref($lastactref) eq 'HASH') {
18478:                     $lastactref->{'domdefaults'} = 1;
18479:                 }
18480:             }
18481:         } else {
18482:             $resulttext = '<span class="LC_error">'.
18483:                           &mt('An error occurred: [_1]',$putresult).'</span>';
18484:         }
18485:     } else {
18486:         $resulttext = &mt('No changes made to course and community categories');
18487:     }
18488:     return $resulttext;
18489: }
18490: 
18491: sub modify_serverstatuses {
18492:     my ($dom,%domconfig) = @_;
18493:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
18494:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
18495:         %currserverstatus = %{$domconfig{'serverstatuses'}};
18496:     }
18497:     my @pages = &serverstatus_pages();
18498:     foreach my $type (@pages) {
18499:         $newserverstatus{$type}{'namedusers'} = '';
18500:         $newserverstatus{$type}{'machines'} = '';
18501:         if (defined($env{'form.'.$type.'_namedusers'})) {
18502:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
18503:             my @okusers;
18504:             foreach my $user (@users) {
18505:                 my ($uname,$udom) = split(/:/,$user);
18506:                 if (($udom =~ /^$match_domain$/) &&   
18507:                     (&Apache::lonnet::domain($udom)) &&
18508:                     ($uname =~ /^$match_username$/)) {
18509:                     if (!grep(/^\Q$user\E/,@okusers)) {
18510:                         push(@okusers,$user);
18511:                     }
18512:                 }
18513:             }
18514:             if (@okusers > 0) {
18515:                  @okusers = sort(@okusers);
18516:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
18517:             }
18518:         }
18519:         if (defined($env{'form.'.$type.'_machines'})) {
18520:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
18521:             my @okmachines;
18522:             foreach my $ip (@machines) {
18523:                 my @parts = split(/\./,$ip);
18524:                 next if (@parts < 4);
18525:                 my $badip = 0;
18526:                 for (my $i=0; $i<4; $i++) {
18527:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
18528:                         $badip = 1;
18529:                         last;
18530:                     }
18531:                 }
18532:                 if (!$badip) {
18533:                     push(@okmachines,$ip);     
18534:                 }
18535:             }
18536:             @okmachines = sort(@okmachines);
18537:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
18538:         }
18539:     }
18540:     my %serverstatushash =  (
18541:                                 serverstatuses => \%newserverstatus,
18542:                             );
18543:     foreach my $type (@pages) {
18544:         foreach my $setting ('namedusers','machines') {
18545:             my (@current,@new);
18546:             if (ref($currserverstatus{$type}) eq 'HASH') {
18547:                 if ($currserverstatus{$type}{$setting} ne '') { 
18548:                     @current = split(/,/,$currserverstatus{$type}{$setting});
18549:                 }
18550:             }
18551:             if ($newserverstatus{$type}{$setting} ne '') {
18552:                 @new = split(/,/,$newserverstatus{$type}{$setting});
18553:             }
18554:             if (@current > 0) {
18555:                 if (@new > 0) {
18556:                     foreach my $item (@current) {
18557:                         if (!grep(/^\Q$item\E$/,@new)) {
18558:                             $changes{$type}{$setting} = 1;
18559:                             last;
18560:                         }
18561:                     }
18562:                     foreach my $item (@new) {
18563:                         if (!grep(/^\Q$item\E$/,@current)) {
18564:                             $changes{$type}{$setting} = 1;
18565:                             last;
18566:                         }
18567:                     }
18568:                 } else {
18569:                     $changes{$type}{$setting} = 1;
18570:                 }
18571:             } elsif (@new > 0) {
18572:                 $changes{$type}{$setting} = 1;
18573:             }
18574:         }
18575:     }
18576:     if (keys(%changes) > 0) {
18577:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
18578:         my $putresult = &Apache::lonnet::put_dom('configuration',
18579:                                                  \%serverstatushash,$dom);
18580:         if ($putresult eq 'ok') {
18581:             $resulttext .= &mt('Changes made:').'<ul>';
18582:             foreach my $type (@pages) {
18583:                 if (ref($changes{$type}) eq 'HASH') {
18584:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
18585:                     if ($changes{$type}{'namedusers'}) {
18586:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
18587:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
18588:                         } else {
18589:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
18590:                         }
18591:                     }
18592:                     if ($changes{$type}{'machines'}) {
18593:                         if ($newserverstatus{$type}{'machines'} eq '') {
18594:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
18595:                         } else {
18596:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
18597:                         }
18598: 
18599:                     }
18600:                     $resulttext .= '</ul></li>';
18601:                 }
18602:             }
18603:             $resulttext .= '</ul>';
18604:         } else {
18605:             $resulttext = '<span class="LC_error">'.
18606:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
18607: 
18608:         }
18609:     } else {
18610:         $resulttext = &mt('No changes made to access to server status pages');
18611:     }
18612:     return $resulttext;
18613: }
18614: 
18615: sub modify_helpsettings {
18616:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
18617:     my ($resulttext,$errors,%changes,%helphash);
18618:     my %defaultchecked = ('submitbugs' => 'on');
18619:     my @offon = ('off','on');
18620:     my @toggles = ('submitbugs');
18621:     my %current = ('submitbugs' => '',
18622:                    'adhoc'      => {},
18623:                   );
18624:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
18625:         %current = %{$domconfig{'helpsettings'}};
18626:     }
18627:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18628:     foreach my $item (@toggles) {
18629:         if ($defaultchecked{$item} eq 'on') { 
18630:             if ($current{$item} eq '') {
18631:                 if ($env{'form.'.$item} eq '0') {
18632:                     $changes{$item} = 1;
18633:                 }
18634:             } elsif ($current{$item} ne $env{'form.'.$item}) {
18635:                 $changes{$item} = 1;
18636:             }
18637:         } elsif ($defaultchecked{$item} eq 'off') {
18638:             if ($current{$item} eq '') {
18639:                 if ($env{'form.'.$item} eq '1') {
18640:                     $changes{$item} = 1;
18641:                 }
18642:             } elsif ($current{$item} ne $env{'form.'.$item}) {
18643:                 $changes{$item} = 1;
18644:             }
18645:         }
18646:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
18647:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
18648:         }
18649:     }
18650:     my $maxnum = $env{'form.helproles_maxnum'};
18651:     my $confname = $dom.'-domainconfig';
18652:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
18653:     my (@allpos,%newsettings,%changedprivs,$newrole);
18654:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
18655:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
18656:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
18657:     my %lt = &Apache::lonlocal::texthash(
18658:                     s      => 'system',
18659:                     d      => 'domain',
18660:                     order  => 'Display order',
18661:                     access => 'Role usage',
18662:                     all    => 'All with domain helpdesk or helpdesk assistant role',
18663:                     dh     => 'All with domain helpdesk role',
18664:                     da     => 'All with domain helpdesk assistant role',
18665:                     none   => 'None',
18666:                     status => 'Determined based on institutional status',
18667:                     inc    => 'Include all, but exclude specific personnel',
18668:                     exc    => 'Exclude all, but include specific personnel',
18669:     );
18670:     for (my $num=0; $num<=$maxnum; $num++) {
18671:         my ($prefix,$identifier,$rolename,%curr);
18672:         if ($num == $maxnum) {
18673:             next unless ($env{'form.newcusthelp'} == $maxnum);
18674:             $identifier = 'custhelp'.$num;
18675:             $prefix = 'helproles_'.$num;
18676:             $rolename = $env{'form.custhelpname'.$num};
18677:             $rolename=~s/[^A-Za-z0-9]//gs;
18678:             next if ($rolename eq '');
18679:             next if (exists($existing{'rolesdef_'.$rolename}));
18680:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
18681:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
18682:                                                      $newprivs{'c'},$confname,$dom);
18683:             if ($result ne 'ok') {
18684:                 $errors .= '<li><span class="LC_error">'.
18685:                            &mt('An error occurred storing the new custom role: [_1]',
18686:                            $result).'</span></li>';
18687:                 next;
18688:             } else {
18689:                 $changedprivs{$rolename} = \%newprivs;
18690:                 $newrole = $rolename;
18691:             }
18692:         } else {
18693:             $prefix = 'helproles_'.$num;
18694:             $rolename = $env{'form.'.$prefix};
18695:             next if ($rolename eq '');
18696:             next unless (exists($existing{'rolesdef_'.$rolename}));
18697:             $identifier = 'custhelp'.$num;
18698:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
18699:             my %currprivs;
18700:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
18701:                 split(/\_/,$existing{'rolesdef_'.$rolename});
18702:             foreach my $level ('c','d','s') {
18703:                 if ($newprivs{$level} ne $currprivs{$level}) {
18704:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
18705:                                                              $newprivs{'c'},$confname,$dom);
18706:                     if ($result ne 'ok') {
18707:                         $errors .= '<li><span class="LC_error">'.
18708:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
18709:                                        $rolename,$result).'</span></li>';
18710:                     } else {
18711:                         $changedprivs{$rolename} = \%newprivs;
18712:                     }
18713:                     last;
18714:                 }
18715:             }
18716:             if (ref($current{'adhoc'}) eq 'HASH') {
18717:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
18718:                     %curr = %{$current{'adhoc'}{$rolename}};
18719:                 }
18720:             }
18721:         }
18722:         my $newpos = $env{'form.'.$prefix.'_pos'};
18723:         $newpos =~ s/\D+//g;
18724:         $allpos[$newpos] = $rolename;
18725:         my $newdesc = $env{'form.'.$prefix.'_desc'};
18726:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
18727:         if ($curr{'desc'}) {
18728:             if ($curr{'desc'} ne $newdesc) {
18729:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
18730:                 $newsettings{$rolename}{'desc'} = $newdesc;
18731:             }
18732:         } elsif ($newdesc ne '') {
18733:             $changes{'customrole'}{$rolename}{'desc'} = 1;
18734:             $newsettings{$rolename}{'desc'} = $newdesc;
18735:         }
18736:         my $access = $env{'form.'.$prefix.'_access'};
18737:         if (grep(/^\Q$access\E$/,@accesstypes)) {
18738:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
18739:             if ($access eq 'status') {
18740:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
18741:                 if (scalar(@statuses) == 0) {
18742:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
18743:                 } else {
18744:                     my (@shownstatus,$numtypes);
18745:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
18746:                     if (ref($types) eq 'ARRAY') {
18747:                         $numtypes = scalar(@{$types});
18748:                         foreach my $type (sort(@statuses)) {
18749:                             if ($type eq 'default') {
18750:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
18751:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
18752:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
18753:                                 push(@shownstatus,$usertypes->{$type});
18754:                             }
18755:                         }
18756:                     }
18757:                     if (grep(/^default$/,@statuses)) {
18758:                         push(@shownstatus,$othertitle);
18759:                     }
18760:                     if (scalar(@shownstatus) == 1+$numtypes) {
18761:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
18762:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
18763:                     } else {
18764:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
18765:                         if (ref($curr{'status'}) eq 'ARRAY') {
18766:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
18767:                             if (@diffs) {
18768:                                 $changes{'customrole'}{$rolename}{$access} = 1;
18769:                             }
18770:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
18771:                             $changes{'customrole'}{$rolename}{$access} = 1;
18772:                         }
18773:                     }
18774:                 }
18775:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
18776:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
18777:                 my @newspecstaff;
18778:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
18779:                 foreach my $person (sort(@personnel)) {
18780:                     if ($domhelpdesk{$person}) {
18781:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
18782:                     }
18783:                 }
18784:                 if (ref($curr{$access}) eq 'ARRAY') {
18785:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
18786:                     if (@diffs) {
18787:                         $changes{'customrole'}{$rolename}{$access} = 1;
18788:                     }
18789:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
18790:                     $changes{'customrole'}{$rolename}{$access} = 1;
18791:                 }
18792:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
18793:                     my ($uname,$udom) = split(/:/,$person);
18794:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
18795:                 }
18796:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
18797:             }
18798:         } else {
18799:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
18800:         }
18801:         unless ($curr{'access'} eq $access) {
18802:             $changes{'customrole'}{$rolename}{'access'} = 1;
18803:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
18804:         }
18805:     }
18806:     if (@allpos > 0) {
18807:         my $idx = 0;
18808:         foreach my $rolename (@allpos) {
18809:             if ($rolename ne '') {
18810:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
18811:                 if (ref($current{'adhoc'}) eq 'HASH') {
18812:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
18813:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
18814:                             $changes{'customrole'}{$rolename}{'order'} = 1;
18815:                             $newsettings{$rolename}{'order'} = $idx+1;
18816:                         }
18817:                     }
18818:                 }
18819:                 $idx ++;
18820:             }
18821:         }
18822:     }
18823:     my $putresult;
18824:     if (keys(%changes) > 0) {
18825:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
18826:         if ($putresult eq 'ok') {
18827:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
18828:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
18829:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
18830:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
18831:                 }
18832:             }
18833:             my $cachetime = 24*60*60;
18834:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18835:             if (ref($lastactref) eq 'HASH') {
18836:                 $lastactref->{'domdefaults'} = 1;
18837:             }
18838:         } else {
18839:             $errors .= '<li><span class="LC_error">'.
18840:                        &mt('An error occurred storing the settings: [_1]',
18841:                            $putresult).'</span></li>';
18842:         }
18843:     }
18844:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
18845:         $resulttext = &mt('Changes made:').'<ul>';
18846:         my (%shownprivs,@levelorder);
18847:         @levelorder = ('c','d','s');
18848:         if ((keys(%changes)) && ($putresult eq 'ok')) {
18849:             foreach my $item (sort(keys(%changes))) {
18850:                 if ($item eq 'submitbugs') {
18851:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
18852:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
18853:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
18854:                 } elsif ($item eq 'customrole') {
18855:                     if (ref($changes{'customrole'}) eq 'HASH') {
18856:                         my @keyorder = ('order','desc','access','status','exc','inc');
18857:                         my %keytext = &Apache::lonlocal::texthash(
18858:                                                                    order  => 'Order',
18859:                                                                    desc   => 'Role description',
18860:                                                                    access => 'Role usage',
18861:                                                                    status => 'Allowed institutional types',
18862:                                                                    exc    => 'Allowed personnel',
18863:                                                                    inc    => 'Disallowed personnel',
18864:                         );
18865:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
18866:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
18867:                                 if ($role eq $newrole) {
18868:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
18869:                                                               $role).'<ul>';
18870:                                 } else {
18871:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
18872:                                                               $role).'<ul>';
18873:                                 }
18874:                                 foreach my $key (@keyorder) {
18875:                                     if ($changes{'customrole'}{$role}{$key}) {
18876:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
18877:                                                                   $keytext{$key},$newsettings{$role}{$key}).
18878:                                                        '</li>';
18879:                                     }
18880:                                 }
18881:                                 if (ref($changedprivs{$role}) eq 'HASH') {
18882:                                     $shownprivs{$role} = 1;
18883:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
18884:                                     foreach my $level (@levelorder) {
18885:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
18886:                                             next if ($item eq '');
18887:                                             my ($priv) = split(/\&/,$item,2);
18888:                                             if (&Apache::lonnet::plaintext($priv)) {
18889:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
18890:                                                 unless ($level eq 'c') {
18891:                                                     $resulttext .= ' ('.$lt{$level}.')';
18892:                                                 }
18893:                                                 $resulttext .= '</li>';
18894:                                             }
18895:                                         }
18896:                                     }
18897:                                     $resulttext .= '</ul>';
18898:                                 }
18899:                                 $resulttext .= '</ul></li>';
18900:                             }
18901:                         }
18902:                     }
18903:                 }
18904:             }
18905:         }
18906:         if (keys(%changedprivs)) {
18907:             foreach my $role (sort(keys(%changedprivs))) {
18908:                 unless ($shownprivs{$role}) {
18909:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
18910:                                               $role).'<ul>'.
18911:                                    '<li>'.&mt('Privileges set to :').'<ul>';
18912:                     foreach my $level (@levelorder) {
18913:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
18914:                             next if ($item eq '');
18915:                             my ($priv) = split(/\&/,$item,2);
18916:                             if (&Apache::lonnet::plaintext($priv)) {
18917:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
18918:                                 unless ($level eq 'c') {
18919:                                     $resulttext .= ' ('.$lt{$level}.')';
18920:                                 }
18921:                                 $resulttext .= '</li>';
18922:                             }
18923:                         }
18924:                     }
18925:                     $resulttext .= '</ul></li></ul></li>';
18926:                 }
18927:             }
18928:         }
18929:         $resulttext .= '</ul>';
18930:     } else {
18931:         $resulttext = &mt('No changes made to help settings');
18932:     }
18933:     if ($errors) {
18934:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
18935:                                     $errors.'</ul>';
18936:     }
18937:     return $resulttext;
18938: }
18939: 
18940: sub modify_coursedefaults {
18941:     my ($dom,$lastactref,%domconfig) = @_;
18942:     my ($resulttext,$errors,%changes,%defaultshash);
18943:     my %defaultchecked = (
18944:                            'canuse_pdfforms' => 'off',
18945:                            'uselcmath'       => 'on',
18946:                            'usejsme'         => 'on'
18947:                          );
18948:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
18949:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
18950:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
18951:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
18952:                    'mysqltables_textbook','mysqltables_placement');
18953:     my @types = ('official','unofficial','community','textbook','placement');
18954:     my %staticdefaults = (
18955:                            anonsurvey_threshold => 10,
18956:                            uploadquota          => 500,
18957:                            postsubmit           => 60,
18958:                            mysqltables          => 172800,
18959:                          );
18960:     my %texoptions = (
18961:                         MathJax  => 'MathJax',
18962:                         mimetex  => &mt('Convert to Images'),
18963:                         tth      => &mt('TeX to HTML'),
18964:                      );
18965:     $defaultshash{'coursedefaults'} = {};
18966: 
18967:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
18968:         if ($domconfig{'coursedefaults'} eq '') {
18969:             $domconfig{'coursedefaults'} = {};
18970:         }
18971:     }
18972: 
18973:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
18974:         foreach my $item (@toggles) {
18975:             if ($defaultchecked{$item} eq 'on') {
18976:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
18977:                     ($env{'form.'.$item} eq '0')) {
18978:                     $changes{$item} = 1;
18979:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
18980:                     $changes{$item} = 1;
18981:                 }
18982:             } elsif ($defaultchecked{$item} eq 'off') {
18983:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
18984:                     ($env{'form.'.$item} eq '1')) {
18985:                     $changes{$item} = 1;
18986:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
18987:                     $changes{$item} = 1;
18988:                 }
18989:             }
18990:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
18991:         }
18992:         foreach my $item (@numbers) {
18993:             my ($currdef,$newdef);
18994:             $newdef = $env{'form.'.$item};
18995:             if ($item eq 'anonsurvey_threshold') {
18996:                 $currdef = $domconfig{'coursedefaults'}{$item};
18997:                 $newdef =~ s/\D//g;
18998:                 if ($newdef eq '' || $newdef < 1) {
18999:                     $newdef = 1;
19000:                 }
19001:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
19002:             } else {
19003:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
19004:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
19005:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
19006:                 }
19007:                 $newdef =~ s/[^\w.\-]//g;
19008:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
19009:             }
19010:             if ($currdef ne $newdef) {
19011:                 if ($item eq 'anonsurvey_threshold') {
19012:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
19013:                         $changes{$item} = 1;
19014:                     }
19015:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
19016:                     my $setting = $1;
19017:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
19018:                         $changes{$setting} = 1;
19019:                     }
19020:                 }
19021:             }
19022:         }
19023:         my $texengine;
19024:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
19025:             $texengine = $env{'form.texengine'};
19026:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
19027:             if ($currdef eq '') {
19028:                 unless ($texengine eq $Apache::lonnet::deftex) {
19029:                     $changes{'texengine'} = 1;
19030:                 }
19031:             } elsif ($currdef ne $texengine) {
19032:                 $changes{'texengine'} = 1;
19033:             }
19034:         }
19035:         if ($texengine ne '') {
19036:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
19037:         }
19038:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
19039:         my @currclonecode;
19040:         if (ref($currclone) eq 'HASH') {
19041:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
19042:                 @currclonecode = @{$currclone->{'instcode'}};
19043:             }
19044:         }
19045:         my $newclone;
19046:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
19047:             $newclone = $env{'form.canclone'};
19048:         }
19049:         if ($newclone eq 'instcode') {
19050:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
19051:             my (%codedefaults,@code_order,@clonecode);
19052:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
19053:                                                     \@code_order);
19054:             foreach my $item (@code_order) {
19055:                 if (grep(/^\Q$item\E$/,@newcodes)) {
19056:                     push(@clonecode,$item);
19057:                 }
19058:             }
19059:             if (@clonecode) {
19060:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
19061:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
19062:                 if (@diffs) {
19063:                     $changes{'canclone'} = 1;
19064:                 }
19065:             } else {
19066:                 $newclone eq '';
19067:             }
19068:         } elsif ($newclone ne '') {
19069:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
19070:         }
19071:         if ($newclone ne $currclone) {
19072:             $changes{'canclone'} = 1;
19073:         }
19074:         my %credits;
19075:         foreach my $type (@types) {
19076:             unless ($type eq 'community') {
19077:                 $credits{$type} = $env{'form.'.$type.'_credits'};
19078:                 $credits{$type} =~ s/[^\d.]+//g;
19079:             }
19080:         }
19081:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
19082:             ($env{'form.coursecredits'} eq '1')) {
19083:             $changes{'coursecredits'} = 1;
19084:             foreach my $type (keys(%credits)) {
19085:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
19086:             }
19087:         } else {
19088:             if ($env{'form.coursecredits'} eq '1') {
19089:                 foreach my $type (@types) {
19090:                     unless ($type eq 'community') {
19091:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
19092:                             $changes{'coursecredits'} = 1;
19093:                         }
19094:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
19095:                     }
19096:                 }
19097:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
19098:                 foreach my $type (@types) {
19099:                     unless ($type eq 'community') {
19100:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
19101:                             $changes{'coursecredits'} = 1;
19102:                             last;
19103:                         }
19104:                     }
19105:                 }
19106:             }
19107:         }
19108:         if ($env{'form.postsubmit'} eq '1') {
19109:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
19110:             my %currtimeout;
19111:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
19112:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
19113:                     $changes{'postsubmit'} = 1;
19114:                 }
19115:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
19116:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
19117:                 }
19118:             } else {
19119:                 $changes{'postsubmit'} = 1;
19120:             }
19121:             foreach my $type (@types) {
19122:                 my $timeout = $env{'form.'.$type.'_timeout'};
19123:                 $timeout =~ s/\D//g;
19124:                 if ($timeout == $staticdefaults{'postsubmit'}) {
19125:                     $timeout = '';
19126:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
19127:                     $timeout = '0';
19128:                 }
19129:                 unless ($timeout eq '') {
19130:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
19131:                 }
19132:                 if (exists($currtimeout{$type})) {
19133:                     if ($timeout ne $currtimeout{$type}) {
19134:                         $changes{'postsubmit'} = 1;
19135:                     }
19136:                 } elsif ($timeout ne '') {
19137:                     $changes{'postsubmit'} = 1;
19138:                 }
19139:             }
19140:         } else {
19141:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
19142:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
19143:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
19144:                     $changes{'postsubmit'} = 1;
19145:                 }
19146:             } else {
19147:                 $changes{'postsubmit'} = 1;
19148:             }
19149:         }
19150:     }
19151:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
19152:                                              $dom);
19153:     if ($putresult eq 'ok') {
19154:         if (keys(%changes) > 0) {
19155:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19156:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
19157:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
19158:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'})) {
19159:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme','texengine') {
19160:                     if ($changes{$item}) {
19161:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
19162:                     }
19163:                 }
19164:                 if ($changes{'coursecredits'}) {
19165:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
19166:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
19167:                             $domdefaults{$type.'credits'} =
19168:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
19169:                         }
19170:                     }
19171:                 }
19172:                 if ($changes{'postsubmit'}) {
19173:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
19174:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
19175:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
19176:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
19177:                                 $domdefaults{$type.'postsubtimeout'} =
19178:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
19179:                             }
19180:                         }
19181:                     }
19182:                 }
19183:                 if ($changes{'uploadquota'}) {
19184:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
19185:                         foreach my $type (@types) {
19186:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
19187:                         }
19188:                     }
19189:                 }
19190:                 if ($changes{'canclone'}) {
19191:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
19192:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
19193:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
19194:                             if (@clonecodes) {
19195:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
19196:                             }
19197:                         }
19198:                     } else {
19199:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
19200:                     }
19201:                 }
19202:                 my $cachetime = 24*60*60;
19203:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19204:                 if (ref($lastactref) eq 'HASH') {
19205:                     $lastactref->{'domdefaults'} = 1;
19206:                 }
19207:             }
19208:             $resulttext = &mt('Changes made:').'<ul>';
19209:             foreach my $item (sort(keys(%changes))) {
19210:                 if ($item eq 'canuse_pdfforms') {
19211:                     if ($env{'form.'.$item} eq '1') {
19212:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
19213:                     } else {
19214:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
19215:                     }
19216:                 } elsif ($item eq 'uselcmath') {
19217:                     if ($env{'form.'.$item} eq '1') {
19218:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
19219:                     } else {
19220:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
19221:                     }
19222:                 } elsif ($item eq 'usejsme') {
19223:                     if ($env{'form.'.$item} eq '1') {
19224:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
19225:                     } else {
19226:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
19227:                     }
19228:                 } elsif ($item eq 'texengine') {
19229:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
19230:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
19231:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
19232:                     }
19233:                 } elsif ($item eq 'anonsurvey_threshold') {
19234:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
19235:                 } elsif ($item eq 'uploadquota') {
19236:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
19237:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
19238:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
19239:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
19240:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
19241:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
19242:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
19243:                                        '</ul>'.
19244:                                        '</li>';
19245:                     } else {
19246:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
19247:                     }
19248:                 } elsif ($item eq 'mysqltables') {
19249:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
19250:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
19251:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
19252:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
19253:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
19254:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
19255:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
19256:                                        '</ul>'.
19257:                                        '</li>';
19258:                     } else {
19259:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
19260:                     }
19261:                 } elsif ($item eq 'postsubmit') {
19262:                     if ($domdefaults{'postsubmit'} eq 'off') {
19263:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
19264:                     } else {
19265:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
19266:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
19267:                             $resulttext .= &mt('durations:').'<ul>';
19268:                             foreach my $type (@types) {
19269:                                 $resulttext .= '<li>';
19270:                                 my $timeout;
19271:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
19272:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
19273:                                 }
19274:                                 my $display;
19275:                                 if ($timeout eq '0') {
19276:                                     $display = &mt('unlimited');
19277:                                 } elsif ($timeout eq '') {
19278:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
19279:                                 } else {
19280:                                     $display = &mt('[quant,_1,second]',$timeout);
19281:                                 }
19282:                                 if ($type eq 'community') {
19283:                                     $resulttext .= &mt('Communities');
19284:                                 } elsif ($type eq 'official') {
19285:                                     $resulttext .= &mt('Official courses');
19286:                                 } elsif ($type eq 'unofficial') {
19287:                                     $resulttext .= &mt('Unofficial courses');
19288:                                 } elsif ($type eq 'textbook') {
19289:                                     $resulttext .= &mt('Textbook courses');
19290:                                 } elsif ($type eq 'placement') {
19291:                                     $resulttext .= &mt('Placement tests');
19292:                                 }
19293:                                 $resulttext .= ' -- '.$display.'</li>';
19294:                             }
19295:                             $resulttext .= '</ul>';
19296:                         }
19297:                         $resulttext .= '</li>';
19298:                     }
19299:                 } elsif ($item eq 'coursecredits') {
19300:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
19301:                         if (($domdefaults{'officialcredits'} eq '') &&
19302:                             ($domdefaults{'unofficialcredits'} eq '') &&
19303:                             ($domdefaults{'textbookcredits'} eq '')) {
19304:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
19305:                         } else {
19306:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
19307:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
19308:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
19309:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
19310:                                            '</ul>'.
19311:                                            '</li>';
19312:                         }
19313:                     } else {
19314:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
19315:                     }
19316:                 } elsif ($item eq 'canclone') {
19317:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
19318:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
19319:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
19320:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
19321:                         }
19322:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
19323:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
19324:                     } else {
19325:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
19326:                     }
19327:                 }
19328:             }
19329:             $resulttext .= '</ul>';
19330:         } else {
19331:             $resulttext = &mt('No changes made to course defaults');
19332:         }
19333:     } else {
19334:         $resulttext = '<span class="LC_error">'.
19335:             &mt('An error occurred: [_1]',$putresult).'</span>';
19336:     }
19337:     return $resulttext;
19338: }
19339: 
19340: sub modify_selfenrollment {
19341:     my ($dom,$lastactref,%domconfig) = @_;
19342:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
19343:     my @types = ('official','unofficial','community','textbook','placement');
19344:     my %titles = &tool_titles();
19345:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
19346:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
19347:     $ordered{'default'} = ['types','registered','approval','limit'];
19348: 
19349:     my (%roles,%shown,%toplevel);
19350:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
19351: 
19352:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
19353:         if ($domconfig{'selfenrollment'} eq '') {
19354:             $domconfig{'selfenrollment'} = {};
19355:         }
19356:     }
19357:     %toplevel = (
19358:                   admin      => 'Configuration Rights',
19359:                   default    => 'Default settings',
19360:                   validation => 'Validation of self-enrollment requests',
19361:                 );
19362:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
19363: 
19364:     if (ref($ordered{'admin'}) eq 'ARRAY') {
19365:         foreach my $item (@{$ordered{'admin'}}) {
19366:             foreach my $type (@types) {
19367:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
19368:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
19369:                 } else {
19370:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
19371:                 }
19372:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
19373:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
19374:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
19375:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
19376:                             push(@{$changes{'admin'}{$type}},$item);
19377:                         }
19378:                     } else {
19379:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
19380:                             push(@{$changes{'admin'}{$type}},$item);
19381:                         }
19382:                     }
19383:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
19384:                     push(@{$changes{'admin'}{$type}},$item);
19385:                 }
19386:             }
19387:         }
19388:     }
19389: 
19390:     foreach my $item (@{$ordered{'default'}}) {
19391:         foreach my $type (@types) {
19392:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
19393:             if ($item eq 'types') {
19394:                 unless (($value eq 'all') || ($value eq 'dom')) {
19395:                     $value = '';
19396:                 }
19397:             } elsif ($item eq 'registered') {
19398:                 unless ($value eq '1') {
19399:                     $value = 0;
19400:                 }
19401:             } elsif ($item eq 'approval') {
19402:                 unless ($value =~ /^[012]$/) {
19403:                     $value = 0;
19404:                 }
19405:             } else {
19406:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
19407:                     $value = 'none';
19408:                 }
19409:             }
19410:             $selfenrollhash{'default'}{$type}{$item} = $value;
19411:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
19412:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
19413:                     if ($selfenrollhash{'default'}{$type}{$item} ne
19414:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
19415:                          push(@{$changes{'default'}{$type}},$item);
19416:                     }
19417:                 } else {
19418:                     push(@{$changes{'default'}{$type}},$item);
19419:                 }
19420:             } else {
19421:                 push(@{$changes{'default'}{$type}},$item);
19422:             }
19423:             if ($item eq 'limit') {
19424:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
19425:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
19426:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
19427:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
19428:                     }
19429:                 } else {
19430:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
19431:                 }
19432:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
19433:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
19434:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
19435:                          push(@{$changes{'default'}{$type}},'cap');
19436:                     }
19437:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
19438:                     push(@{$changes{'default'}{$type}},'cap');
19439:                 }
19440:             }
19441:         }
19442:     }
19443: 
19444:     foreach my $item (@{$itemsref}) {
19445:         if ($item eq 'fields') {
19446:             my @changed;
19447:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
19448:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
19449:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
19450:             }
19451:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
19452:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
19453:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
19454:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
19455:                 } else {
19456:                     @changed = @{$selfenrollhash{'validation'}{$item}};
19457:                 }
19458:             } else {
19459:                 @changed = @{$selfenrollhash{'validation'}{$item}};
19460:             }
19461:             if (@changed) {
19462:                 if ($selfenrollhash{'validation'}{$item}) { 
19463:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
19464:                 } else {
19465:                     $changes{'validation'}{$item} = &mt('None');
19466:                 }
19467:             }
19468:         } else {
19469:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
19470:             if ($item eq 'markup') {
19471:                if ($env{'form.selfenroll_validation_'.$item}) {
19472:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
19473:                }
19474:             }
19475:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
19476:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
19477:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
19478:                 }
19479:             }
19480:         }
19481:     }
19482: 
19483:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
19484:                                              $dom);
19485:     if ($putresult eq 'ok') {
19486:         if (keys(%changes) > 0) {
19487:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19488:             $resulttext = &mt('Changes made:').'<ul>';
19489:             foreach my $key ('admin','default','validation') {
19490:                 if (ref($changes{$key}) eq 'HASH') {
19491:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
19492:                     if ($key eq 'validation') {
19493:                         foreach my $item (@{$itemsref}) {
19494:                             if (exists($changes{$key}{$item})) {
19495:                                 if ($item eq 'markup') {
19496:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
19497:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
19498:                                 } else {  
19499:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
19500:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
19501:                                 }
19502:                             }
19503:                         }
19504:                     } else {
19505:                         foreach my $type (@types) {
19506:                             if ($type eq 'community') {
19507:                                 $roles{'1'} = &mt('Community personnel');
19508:                             } else {
19509:                                 $roles{'1'} = &mt('Course personnel');
19510:                             }
19511:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
19512:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
19513:                                     if ($key eq 'admin') {
19514:                                         my @mgrdc = ();
19515:                                         if (ref($ordered{$key}) eq 'ARRAY') {
19516:                                             foreach my $item (@{$ordered{'admin'}}) {
19517:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
19518:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
19519:                                                         push(@mgrdc,$item);
19520:                                                     }
19521:                                                 }
19522:                                             }
19523:                                             if (@mgrdc) {
19524:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
19525:                                             } else {
19526:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
19527:                                             }
19528:                                         }
19529:                                     } else {
19530:                                         if (ref($ordered{$key}) eq 'ARRAY') {
19531:                                             foreach my $item (@{$ordered{$key}}) {
19532:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
19533:                                                     $domdefaults{$type.'selfenroll'.$item} =
19534:                                                         $selfenrollhash{$key}{$type}{$item};
19535:                                                 }
19536:                                             }
19537:                                         }
19538:                                     }
19539:                                 }
19540:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
19541:                                 foreach my $item (@{$ordered{$key}}) {
19542:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
19543:                                         $resulttext .= '<li>';
19544:                                         if ($key eq 'admin') {
19545:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
19546:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
19547:                                         } else {
19548:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
19549:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
19550:                                         }
19551:                                         $resulttext .= '</li>';
19552:                                     }
19553:                                 }
19554:                                 $resulttext .= '</ul></li>';
19555:                             }
19556:                         }
19557:                         $resulttext .= '</ul></li>'; 
19558:                     }
19559:                 }
19560:             }
19561:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
19562:                 my $cachetime = 24*60*60;
19563:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19564:                 if (ref($lastactref) eq 'HASH') {
19565:                     $lastactref->{'domdefaults'} = 1;
19566:                 }
19567:             }
19568:             $resulttext .= '</ul>';
19569:         } else {
19570:             $resulttext = &mt('No changes made to self-enrollment settings');
19571:         }
19572:     } else {
19573:         $resulttext = '<span class="LC_error">'.
19574:             &mt('An error occurred: [_1]',$putresult).'</span>';
19575:     }
19576:     return $resulttext;
19577: }
19578: 
19579: sub modify_wafproxy {
19580:     my ($dom,$action,$lastactref,%domconfig) = @_;
19581:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
19582:     my (%othercontrol,%canset,%values,%curralias,%currvalue,@warnings,%wafproxy,
19583:         %changes,%expirecache);
19584:     foreach my $server (sort(keys(%servers))) {
19585:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
19586:         if ($serverhome eq $server) {
19587:             my $serverdom = &Apache::lonnet::host_domain($server);
19588:             if ($serverdom eq $dom) {
19589:                 $canset{$server} = 1;
19590:                 if (ref($domconfig{'wafproxy'}) eq 'HASH') {
19591:                     %{$values{$dom}} = ();
19592:                     if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
19593:                         %curralias = %{$domconfig{'wafproxy'}{'alias'}};
19594:                     }
19595:                     foreach my $item ('ipheader','trusted','exempt') {
19596:                         $currvalue{$item} = $domconfig{'wafproxy'}{$item};
19597:                     }
19598:                 }
19599:             }
19600:         }
19601:     }
19602:     my $output;
19603:     if (keys(%canset)) {
19604:         %{$wafproxy{'alias'}} = ();
19605:         foreach my $key (sort(keys(%canset))) {
19606:             $wafproxy{'alias'}{$key} = $env{'form.wafproxy_alias_'.$key};
19607:             $wafproxy{'alias'}{$key} =~ s/^\s+|\s+$//g;
19608:             if ($wafproxy{'alias'}{$key} ne $curralias{$key}) {
19609:                 $changes{'alias'} = 1;
19610:             }
19611:             if ($wafproxy{'alias'}{$key} eq '') {
19612:                 if ($curralias{$key}) {
19613:                     $expirecache{$key} = 1;
19614:                 }
19615:                 delete($wafproxy{'alias'}{$key});
19616:             }
19617:         }
19618:         unless (keys(%{$wafproxy{'alias'}})) {
19619:             delete($wafproxy{'alias'});
19620:         }
19621:         # Localization for values in %warn occus in &mt() calls separately.
19622:         my %warn = (
19623:                      trusted => 'trusted IP range(s)',
19624:                      exempt => 'exempt IP range(s)', 
19625:                    );
19626:         foreach my $item ('ipheader','trusted','exempt') {
19627:             my $possible = $env{'form.wafproxy_'.$item};
19628:             $possible =~ s/^\s+|\s+$//g;
19629:             if ($possible ne '') {
19630:                 if ($item eq 'ipheader') {
19631:                     $wafproxy{$item} = $possible;
19632:                 } else {
19633:                     my (@ok,$count);
19634:                     $possible =~ s/[\r\n]+/\s/g;
19635:                     $possible =~ s/\s*-\s*/-/g;
19636:                     $possible =~ s/\s+/,/g;
19637:                     $count = 0;
19638:                     if ($possible) {
19639:                         foreach my $poss (split(/\,/,$possible)) {
19640:                             $count ++;
19641:                             if (&validate_ip_pattern($poss)) {
19642:                                 push(@ok,$poss);
19643:                             }
19644:                         }
19645:                         if (@ok) {
19646:                             $wafproxy{$item} = join(',',@ok);
19647:                         }
19648:                         my $diff = $count - scalar(@ok);
19649:                         if ($diff) {
19650:                             push(@warnings,'<li>'.
19651:                                  &mt('[quant,_1,IP] invalid and excluded from saved value for [_2]',
19652:                                      $diff,$warn{$item}).
19653:                                  '</li>');
19654:                         }
19655:                         if ($wafproxy{$item} ne $currvalue{$item}) {
19656:                             $changes{$item} = 1; 
19657:                         }
19658:                     }
19659:                 }
19660:             } else {
19661:                 if ($currvalue{$item}) {
19662:                     $changes{$item} = 1;
19663:                 }
19664:             }
19665:         }
19666:     }
19667:     if (keys(%changes)) {
19668:         my %defaultshash = (
19669:                               wafproxy => \%wafproxy,
19670:                            ); 
19671:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
19672:                                                  $dom);
19673:         if ($putresult eq 'ok') {
19674:             my $cachetime = 24*60*60;
19675:             my (%domdefaults,$updatedomdefs);
19676:             foreach my $item ('ipheader','trusted','exempt') {
19677:                 if ($changes{$item}) {
19678:                     unless ($updatedomdefs) {
19679:                         %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
19680:                         $updatedomdefs = 1;
19681:                     }
19682:                     if ($wafproxy{$item}) {
19683:                         $domdefaults{'waf_'.$item} = $wafproxy{$item};
19684:                     } elsif (exists($domdefaults{'waf_'.$item})) {
19685:                         delete($domdefaults{'waf_'.$item});
19686:                     } 
19687:                 }
19688:             }
19689:             if ($updatedomdefs) {
19690:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19691:                 if (ref($lastactref) eq 'HASH') {
19692:                     $lastactref->{'domdefaults'} = 1;
19693:                 }
19694:             }
19695:             if ((exists($wafproxy{'alias'})) || (keys(%expirecache))) {
19696:                 my %updates = %expirecache;
19697:                 foreach my $key (keys(%expirecache)) {
19698:                     &Apache::lonnet::devalidate_cache_new('proxyalias',$key);
19699:                 }
19700:                 if (ref($wafproxy{'alias'}) eq 'HASH') {
19701:                     my $cachetime = 24*60*60;
19702:                     foreach my $key (keys(%{$wafproxy{'alias'}})) {
19703:                         $updates{$key} = 1;
19704:                         &Apache::lonnet::do_cache_new('proxyalias',$key,$wafproxy{'alias'}{$key},
19705:                                                       $cachetime);
19706:                     }
19707:                 }
19708:                 if (ref($lastactref) eq 'HASH') {
19709:                     $lastactref->{'proxyalias'} = \%updates;
19710:                 }
19711:             }
19712:             $output = &mt('Changes were made to Web Application Firewall/Reverse Proxy').'<ul>';
19713:             foreach my $item ('alias','ipheader','trusted','exempt') {
19714:                 if ($changes{$item}) {
19715:                     if ($item eq 'alias') {
19716:                         my $numaliased = 0;
19717:                         if (ref($wafproxy{'alias'}) eq 'HASH') {
19718:                             my $shown;
19719:                             if (keys(%{$wafproxy{'alias'}})) {
19720:                                 foreach my $server (sort(keys(%{$wafproxy{'alias'}}))) {
19721:                                     $shown .= '<li>'.&mt('[_1] aliased by [_2]',
19722:                                                          &Apache::lonnet::hostname($server),
19723:                                                          $wafproxy{'alias'}{$server}).'</li>';
19724:                                     $numaliased ++;
19725:                                 }
19726:                                 if ($numaliased) {
19727:                                     $output .= '<li>'.&mt('Aliases for hostnames set to: [_1]',
19728:                                                           '<ul>'.$shown.'</ul>').'</li>';
19729:                                 }
19730:                             }
19731:                         }
19732:                         unless ($numaliased) {
19733:                             $output .= '<li>'.&mt('Aliases deleted for hostnames').'</li>';
19734:                         }
19735:                     } else {
19736:                         if ($item eq 'ipheader') {
19737:                             if ($wafproxy{$item}) {
19738:                                 $output .= '<li>'.&mt('Custom request header set to [_1]',
19739:                                                       $wafproxy{$item}).'</li>';
19740:                             } else {
19741:                                 $output .= '<li>'.&mt('Custom request header deleted').'</li>';
19742:                             }
19743:                         } elsif ($item eq 'trusted') {
19744:                             if ($wafproxy{$item}) {
19745:                                 $output .= '<li>'.&mt('Trusted IP range(s) set to [_1]',
19746:                                                       $wafproxy{$item}).'</li>';
19747:                             } else {
19748:                                 $output .= '<li>'.&mt('Trusted IP range(s) deleted').'</li>';
19749:                             }
19750:                         } elsif ($item eq 'exempt') {
19751:                             if ($wafproxy{$item}) {
19752:                                 $output .= '<li>'.&mt('Exempt IP range(s) set to [_1]',
19753:                                                        $wafproxy{$item}).'</li>';
19754:                             } else {
19755:                                 $output .= '<li>'.&mt('Exempt IP range(s) deleted').'</li>';
19756:                             }
19757:                         }
19758:                     }
19759:                 }
19760:             }
19761:         } else {
19762:             $output = '<span class="LC_error">'.
19763:                       &mt('An error occurred: [_1]',$putresult).'</span>';
19764:         }
19765:     } elsif (keys(%canset)) {
19766:         $output = &mt('No changes made to Web Application Firewall/Reverse Proxy settings');
19767:     }
19768:     if (@warnings) {
19769:         $output .= '<br />'.&mt('Warnings:').'<ul>'.
19770:                        join("\n",@warnings).'</ul>';
19771:     }
19772:     return $output;
19773: }
19774: 
19775: sub validate_ip_pattern {
19776:     my ($pattern) = @_;
19777:     if ($pattern =~ /^([^-]+)\-([^-]+)$/) {
19778:         my ($start,$end) = ($1,$2);
19779:         if ((&Net::CIDR::cidrvalidate($start)) && (&Net::CIDR::cidrvalidate($end))) {
19780:             return 1;
19781:         }
19782:     } elsif (&Net::CIDR::cidrvalidate($pattern)) {
19783:         return 1;
19784:     }
19785:     return
19786: }
19787: 
19788: sub modify_usersessions {
19789:     my ($dom,$lastactref,%domconfig) = @_;
19790:     my @hostingtypes = ('version','excludedomain','includedomain');
19791:     my @offloadtypes = ('primary','default');
19792:     my %types = (
19793:                   remote => \@hostingtypes,
19794:                   hosted => \@hostingtypes,
19795:                   spares => \@offloadtypes,
19796:                 );
19797:     my @prefixes = ('remote','hosted','spares');
19798:     my @lcversions = &Apache::lonnet::all_loncaparevs();
19799:     my (%by_ip,%by_location,@intdoms,@instdoms);
19800:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
19801:     my @locations = sort(keys(%by_location));
19802:     my (%defaultshash,%changes);
19803:     foreach my $prefix (@prefixes) {
19804:         $defaultshash{'usersessions'}{$prefix} = {};
19805:     }
19806:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19807:     my $resulttext;
19808:     my %iphost = &Apache::lonnet::get_iphost();
19809:     foreach my $prefix (@prefixes) {
19810:         next if ($prefix eq 'spares');
19811:         foreach my $type (@{$types{$prefix}}) {
19812:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
19813:             if ($type eq 'version') {
19814:                 my $value = $env{'form.'.$prefix.'_'.$type};
19815:                 my $okvalue;
19816:                 if ($value ne '') {
19817:                     if (grep(/^\Q$value\E$/,@lcversions)) {
19818:                         $okvalue = $value;
19819:                     }
19820:                 }
19821:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
19822:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
19823:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
19824:                             if ($inuse == 0) {
19825:                                 $changes{$prefix}{$type} = 1;
19826:                             } else {
19827:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
19828:                                     $changes{$prefix}{$type} = 1;
19829:                                 }
19830:                                 if ($okvalue ne '') {
19831:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
19832:                                 } 
19833:                             }
19834:                         } else {
19835:                             if (($inuse == 1) && ($okvalue ne '')) {
19836:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
19837:                                 $changes{$prefix}{$type} = 1;
19838:                             }
19839:                         }
19840:                     } else {
19841:                         if (($inuse == 1) && ($okvalue ne '')) {
19842:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
19843:                             $changes{$prefix}{$type} = 1;
19844:                         }
19845:                     }
19846:                 } else {
19847:                     if (($inuse == 1) && ($okvalue ne '')) {
19848:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
19849:                         $changes{$prefix}{$type} = 1;
19850:                     }
19851:                 }
19852:             } else {
19853:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
19854:                 my @okvals;
19855:                 foreach my $val (@vals) {
19856:                     if ($val =~ /:/) {
19857:                         my @items = split(/:/,$val);
19858:                         foreach my $item (@items) {
19859:                             if (ref($by_location{$item}) eq 'ARRAY') {
19860:                                 push(@okvals,$item);
19861:                             }
19862:                         }
19863:                     } else {
19864:                         if (ref($by_location{$val}) eq 'ARRAY') {
19865:                             push(@okvals,$val);
19866:                         }
19867:                     }
19868:                 }
19869:                 @okvals = sort(@okvals);
19870:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
19871:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
19872:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
19873:                             if ($inuse == 0) {
19874:                                 $changes{$prefix}{$type} = 1; 
19875:                             } else {
19876:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
19877:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
19878:                                 if (@changed > 0) {
19879:                                     $changes{$prefix}{$type} = 1;
19880:                                 }
19881:                             }
19882:                         } else {
19883:                             if ($inuse == 1) {
19884:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
19885:                                 $changes{$prefix}{$type} = 1;
19886:                             }
19887:                         } 
19888:                     } else {
19889:                         if ($inuse == 1) {
19890:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
19891:                             $changes{$prefix}{$type} = 1;
19892:                         }
19893:                     }
19894:                 } else {
19895:                     if ($inuse == 1) {
19896:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
19897:                         $changes{$prefix}{$type} = 1;
19898:                     }
19899:                 }
19900:             }
19901:         }
19902:     }
19903: 
19904:     my @alldoms = &Apache::lonnet::all_domains();
19905:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
19906:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
19907:     my $savespares;
19908: 
19909:     foreach my $lonhost (sort(keys(%servers))) {
19910:         my $serverhomeID =
19911:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
19912:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
19913:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
19914:         my %spareschg;
19915:         foreach my $type (@{$types{'spares'}}) {
19916:             my @okspares;
19917:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
19918:             foreach my $server (@checked) {
19919:                 if (&Apache::lonnet::hostname($server) ne '') {
19920:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
19921:                         unless (grep(/^\Q$server\E$/,@okspares)) {
19922:                             push(@okspares,$server);
19923:                         }
19924:                     }
19925:                 }
19926:             }
19927:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
19928:             my $newspare;
19929:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
19930:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
19931:                     $newspare = $new;
19932:                 }
19933:             }
19934:             my @spares;
19935:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
19936:                 @spares = sort(@okspares,$newspare);
19937:             } else {
19938:                 @spares = sort(@okspares);
19939:             }
19940:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
19941:             if (ref($spareid{$lonhost}) eq 'HASH') {
19942:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
19943:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
19944:                     if (@diffs > 0) {
19945:                         $spareschg{$type} = 1;
19946:                     }
19947:                 }
19948:             }
19949:         }
19950:         if (keys(%spareschg) > 0) {
19951:             $changes{'spares'}{$lonhost} = \%spareschg;
19952:         }
19953:     }
19954:     $defaultshash{'usersessions'}{'offloadnow'} = {};
19955:     $defaultshash{'usersessions'}{'offloadoth'} = {};
19956:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
19957:     my @okoffload;
19958:     if (@offloadnow) {
19959:         foreach my $server (@offloadnow) {
19960:             if (&Apache::lonnet::hostname($server) ne '') {
19961:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
19962:                     push(@okoffload,$server);
19963:                 }
19964:             }
19965:         }
19966:         if (@okoffload) {
19967:             foreach my $lonhost (@okoffload) {
19968:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
19969:             }
19970:         }
19971:     }
19972:     my @offloadoth = &Apache::loncommon::get_env_multiple('form.offloadoth');
19973:     my @okoffloadoth;
19974:     if (@offloadoth) {
19975:         foreach my $server (@offloadoth) {
19976:             if (&Apache::lonnet::hostname($server) ne '') {
19977:                 unless (grep(/^\Q$server\E$/,@okoffloadoth)) {
19978:                     push(@okoffloadoth,$server);
19979:                 }
19980:             }
19981:         }
19982:         if (@okoffloadoth) {
19983:             foreach my $lonhost (@okoffloadoth) {
19984:                 $defaultshash{'usersessions'}{'offloadoth'}{$lonhost} = 1;
19985:             }
19986:         }
19987:     }
19988:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
19989:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
19990:             if (ref($changes{'spares'}) eq 'HASH') {
19991:                 if (keys(%{$changes{'spares'}}) > 0) {
19992:                     $savespares = 1;
19993:                 }
19994:             }
19995:         } else {
19996:             $savespares = 1;
19997:         }
19998:         foreach my $offload ('offloadnow','offloadoth') {
19999:             if (ref($domconfig{'usersessions'}{$offload}) eq 'HASH') {
20000:                 foreach my $lonhost (keys(%{$domconfig{'usersessions'}{$offload}})) {
20001:                     unless ($defaultshash{'usersessions'}{$offload}{$lonhost}) {
20002:                         $changes{$offload} = 1;
20003:                         last;
20004:                     }
20005:                 }
20006:                 unless ($changes{$offload}) {
20007:                     foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{$offload}})) {
20008:                         unless ($domconfig{'usersessions'}{$offload}{$lonhost}) {
20009:                             $changes{$offload} = 1;
20010:                             last;
20011:                         }
20012:                     }
20013:                 }
20014:             } else {
20015:                 if (($offload eq 'offloadnow') && (@okoffload)) {
20016:                      $changes{'offloadnow'} = 1;
20017:                 }
20018:                 if (($offload eq 'offloadoth') && (@okoffloadoth)) {
20019:                     $changes{'offloadoth'} = 1;
20020:                 }
20021:             } 
20022:         }
20023:     } else {
20024:         if (@okoffload) {
20025:             $changes{'offloadnow'} = 1;
20026:         }
20027:         if (@okoffloadoth) {
20028:             $changes{'offloadoth'} = 1;
20029:         }
20030:     }
20031:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
20032:     if ((keys(%changes) > 0) || ($savespares)) {
20033:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
20034:                                                  $dom);
20035:         if ($putresult eq 'ok') {
20036:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
20037:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
20038:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
20039:                 }
20040:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
20041:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
20042:                 }
20043:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
20044:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
20045:                 }
20046:                 if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
20047:                     $domdefaults{'offloadoth'} = $defaultshash{'usersessions'}{'offloadoth'};
20048:                 }
20049:             }
20050:             my $cachetime = 24*60*60;
20051:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20052:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
20053:             if (ref($lastactref) eq 'HASH') {
20054:                 $lastactref->{'domdefaults'} = 1;
20055:                 $lastactref->{'usersessions'} = 1;
20056:             }
20057:             if (keys(%changes) > 0) {
20058:                 my %lt = &usersession_titles();
20059:                 $resulttext = &mt('Changes made:').'<ul>';
20060:                 foreach my $prefix (@prefixes) {
20061:                     if (ref($changes{$prefix}) eq 'HASH') {
20062:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
20063:                         if ($prefix eq 'spares') {
20064:                             if (ref($changes{$prefix}) eq 'HASH') {
20065:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
20066:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
20067:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
20068:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
20069:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
20070:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
20071:                                         foreach my $type (@{$types{$prefix}}) {
20072:                                             if ($changes{$prefix}{$lonhost}{$type}) {
20073:                                                 my $offloadto = &mt('None');
20074:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
20075:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
20076:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
20077:                                                     }
20078:                                                 }
20079:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
20080:                                             }
20081:                                         }
20082:                                     }
20083:                                     $resulttext .= '</li>';
20084:                                 }
20085:                             }
20086:                         } else {
20087:                             foreach my $type (@{$types{$prefix}}) {
20088:                                 if (defined($changes{$prefix}{$type})) {
20089:                                     my ($newvalue,$notinuse);
20090:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
20091:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
20092:                                             if ($type eq 'version') {
20093:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
20094:                                             } else {
20095:                                                 if (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
20096:                                                     if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
20097:                                                         $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
20098:                                                     }
20099:                                                 } else {
20100:                                                     $notinuse = 1;
20101:                                                 }
20102:                                             }
20103:                                         }
20104:                                     }
20105:                                     if ($newvalue eq '') {
20106:                                         if ($type eq 'version') {
20107:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
20108:                                         } elsif ($notinuse) {
20109:                                             $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
20110:                                         } else {
20111:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
20112:                                         }
20113:                                     } else {
20114:                                         if ($type eq 'version') {
20115:                                             $newvalue .= ' '.&mt('(or later)');
20116:                                         }
20117:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
20118:                                     }
20119:                                 }
20120:                             }
20121:                         }
20122:                         $resulttext .= '</ul>';
20123:                     }
20124:                 }
20125:                 if ($changes{'offloadnow'}) {
20126:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
20127:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
20128:                             $resulttext .= '<li>'.&mt('Switch any active user on next access, for server(s):').'<ul>';
20129:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
20130:                                 $resulttext .= '<li>'.$lonhost.'</li>';
20131:                             }
20132:                             $resulttext .= '</ul>';
20133:                         } else {
20134:                             $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.');
20135:                         }
20136:                     } else {
20137:                         $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.').'</li>';
20138:                     }
20139:                 }
20140:                 if ($changes{'offloadoth'}) {
20141:                     if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
20142:                         if (keys(%{$defaultshash{'usersessions'}{'offloadoth'}}) > 0) {
20143:                             $resulttext .= '<li>'.&mt('Switch other institutions on next access, for server(s):').'<ul>';
20144:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadoth'}}))) {
20145:                                 $resulttext .= '<li>'.$lonhost.'</li>';
20146:                             }
20147:                             $resulttext .= '</ul>';
20148:                         } else {
20149:                             $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.');
20150:                         }
20151:                     } else {
20152:                         $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.').'</li>';
20153:                     }
20154:                 }
20155:                 $resulttext .= '</ul>';
20156:             } else {
20157:                 $resulttext = $nochgmsg;
20158:             }
20159:         } else {
20160:             $resulttext = '<span class="LC_error">'.
20161:                           &mt('An error occurred: [_1]',$putresult).'</span>';
20162:         }
20163:     } else {
20164:         $resulttext = $nochgmsg;
20165:     }
20166:     return $resulttext;
20167: }
20168: 
20169: sub modify_ssl {
20170:     my ($dom,$lastactref,%domconfig) = @_;
20171:     my (%by_ip,%by_location,@intdoms,@instdoms);
20172:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
20173:     my @locations = sort(keys(%by_location));
20174:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
20175:     my (%defaultshash,%changes);
20176:     my $action = 'ssl';
20177:     my @prefixes = ('connto','connfrom','replication');
20178:     foreach my $prefix (@prefixes) {
20179:         $defaultshash{$action}{$prefix} = {};
20180:     }
20181:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20182:     my $resulttext;
20183:     my %iphost = &Apache::lonnet::get_iphost();
20184:     my @reptypes = ('certreq','nocertreq');
20185:     my @connecttypes = ('dom','intdom','other');
20186:     my %types = (
20187:                   connto      => \@connecttypes,
20188:                   connfrom    => \@connecttypes,
20189:                   replication => \@reptypes,
20190:                 );
20191:     foreach my $prefix (sort(keys(%types))) {
20192:         foreach my $type (@{$types{$prefix}}) {
20193:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
20194:                 my $value = 'yes';
20195:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
20196:                     $value = $env{'form.'.$prefix.'_'.$type};
20197:                 }
20198:                 if (ref($domconfig{$action}) eq 'HASH') {
20199:                     if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
20200:                         if ($domconfig{$action}{$prefix}{$type} ne '') {
20201:                             if ($value ne $domconfig{$action}{$prefix}{$type}) {
20202:                                 $changes{$prefix}{$type} = 1;
20203:                             }
20204:                             $defaultshash{$action}{$prefix}{$type} = $value;
20205:                         } else {
20206:                             $defaultshash{$action}{$prefix}{$type} = $value;
20207:                             $changes{$prefix}{$type} = 1;
20208:                         }
20209:                     } else {
20210:                         $defaultshash{$action}{$prefix}{$type} = $value;
20211:                         $changes{$prefix}{$type} = 1;
20212:                     }
20213:                 } else {
20214:                     $defaultshash{$action}{$prefix}{$type} = $value;
20215:                     $changes{$prefix}{$type} = 1;
20216:                 }
20217:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
20218:                     delete($changes{$prefix}{$type});
20219:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
20220:                     delete($changes{$prefix}{$type});
20221:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
20222:                     delete($changes{$prefix}{$type});
20223:                 }
20224:             } elsif ($prefix eq 'replication') {
20225:                 if (@locations > 0) {
20226:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
20227:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
20228:                     my @okvals;
20229:                     foreach my $val (@vals) {
20230:                         if ($val =~ /:/) {
20231:                             my @items = split(/:/,$val);
20232:                             foreach my $item (@items) {
20233:                                 if (ref($by_location{$item}) eq 'ARRAY') {
20234:                                     push(@okvals,$item);
20235:                                 }
20236:                             }
20237:                         } else {
20238:                             if (ref($by_location{$val}) eq 'ARRAY') {
20239:                                 push(@okvals,$val);
20240:                             }
20241:                         }
20242:                     }
20243:                     @okvals = sort(@okvals);
20244:                     if (ref($domconfig{$action}) eq 'HASH') {
20245:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
20246:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
20247:                                 if ($inuse == 0) {
20248:                                     $changes{$prefix}{$type} = 1;
20249:                                 } else {
20250:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
20251:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
20252:                                     if (@changed > 0) {
20253:                                         $changes{$prefix}{$type} = 1;
20254:                                     }
20255:                                 }
20256:                             } else {
20257:                                 if ($inuse == 1) {
20258:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
20259:                                     $changes{$prefix}{$type} = 1;
20260:                                 }
20261:                             }
20262:                         } else {
20263:                             if ($inuse == 1) {
20264:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
20265:                                 $changes{$prefix}{$type} = 1;
20266:                             }
20267:                         }
20268:                     } else {
20269:                         if ($inuse == 1) {
20270:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
20271:                             $changes{$prefix}{$type} = 1;
20272:                         }
20273:                     }
20274:                 }
20275:             }
20276:         }
20277:     }
20278:     if (keys(%changes)) {
20279:         foreach my $prefix (keys(%changes)) {
20280:             if (ref($changes{$prefix}) eq 'HASH') {
20281:                 if (scalar(keys(%{$changes{$prefix}})) == 0) {
20282:                     delete($changes{$prefix});
20283:                 }
20284:             } else {
20285:                 delete($changes{$prefix});
20286:             }
20287:         }
20288:     }
20289:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
20290:     if (keys(%changes) > 0) {
20291:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
20292:                                                  $dom);
20293:         if ($putresult eq 'ok') {
20294:             if (ref($defaultshash{$action}) eq 'HASH') {
20295:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
20296:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
20297:                 }
20298:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
20299:                     $domdefaults{'connto'} = $defaultshash{$action}{'connto'};
20300:                 }
20301:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
20302:                     $domdefaults{'connfrom'} = $defaultshash{$action}{'connfrom'};
20303:                 }
20304:             }
20305:             my $cachetime = 24*60*60;
20306:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20307:             if (ref($lastactref) eq 'HASH') {
20308:                 $lastactref->{'domdefaults'} = 1;
20309:             }
20310:             if (keys(%changes) > 0) {
20311:                 my %titles = &ssl_titles();
20312:                 $resulttext = &mt('Changes made:').'<ul>';
20313:                 foreach my $prefix (@prefixes) {
20314:                     if (ref($changes{$prefix}) eq 'HASH') {
20315:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
20316:                         foreach my $type (@{$types{$prefix}}) {
20317:                             if (defined($changes{$prefix}{$type})) {
20318:                                 my ($newvalue,$notinuse);
20319:                                 if (ref($defaultshash{$action}) eq 'HASH') {
20320:                                     if (ref($defaultshash{$action}{$prefix})) {
20321:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
20322:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
20323:                                         } else {
20324:                                             if (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
20325:                                                 if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
20326:                                                     $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
20327:                                                 }
20328:                                             } else {
20329:                                                 $notinuse = 1;
20330:                                             }
20331:                                         }
20332:                                     }
20333:                                     if ($notinuse) {
20334:                                         $resulttext .= '<li>'.&mt('[_1] set to: not in use',$titles{$type}).'</li>';
20335:                                     } elsif ($newvalue eq '') {
20336:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
20337:                                     } else {
20338:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
20339:                                     }
20340:                                 }
20341:                             }
20342:                         }
20343:                         $resulttext .= '</ul>';
20344:                     }
20345:                 }
20346:             } else {
20347:                 $resulttext = $nochgmsg;
20348:             }
20349:         } else {
20350:             $resulttext = '<span class="LC_error">'.
20351:                           &mt('An error occurred: [_1]',$putresult).'</span>';
20352:         }
20353:     } else {
20354:         $resulttext = $nochgmsg;
20355:     }
20356:     return $resulttext;
20357: }
20358: 
20359: sub modify_trust {
20360:     my ($dom,$lastactref,%domconfig) = @_;
20361:     my (%by_ip,%by_location,@intdoms,@instdoms);
20362:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
20363:     my @locations = sort(keys(%by_location));
20364:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
20365:     my @types = ('exc','inc');
20366:     my (%defaultshash,%changes);
20367:     foreach my $prefix (@prefixes) {
20368:         $defaultshash{'trust'}{$prefix} = {};
20369:     }
20370:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20371:     my $resulttext;
20372:     foreach my $prefix (@prefixes) {
20373:         foreach my $type (@types) {
20374:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
20375:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
20376:             my @okvals;
20377:             foreach my $val (@vals) {
20378:                 if ($val =~ /:/) {
20379:                     my @items = split(/:/,$val);
20380:                     foreach my $item (@items) {
20381:                         if (ref($by_location{$item}) eq 'ARRAY') {
20382:                             push(@okvals,$item);
20383:                         }
20384:                     }
20385:                 } else {
20386:                     if (ref($by_location{$val}) eq 'ARRAY') {
20387:                         push(@okvals,$val);
20388:                     }
20389:                 }
20390:             }
20391:             @okvals = sort(@okvals);
20392:             if (ref($domconfig{'trust'}) eq 'HASH') {
20393:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
20394:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
20395:                         if ($inuse == 0) {
20396:                             $changes{$prefix}{$type} = 1;
20397:                         } else {
20398:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
20399:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
20400:                             if (@changed > 0) {
20401:                                 $changes{$prefix}{$type} = 1;
20402:                             }
20403:                         }
20404:                     } else {
20405:                         if ($inuse == 1) {
20406:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
20407:                             $changes{$prefix}{$type} = 1;
20408:                         }
20409:                     }
20410:                 } else {
20411:                     if ($inuse == 1) {
20412:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
20413:                         $changes{$prefix}{$type} = 1;
20414:                     }
20415:                 }
20416:             } else {
20417:                 if ($inuse == 1) {
20418:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
20419:                     $changes{$prefix}{$type} = 1;
20420:                 }
20421:             }
20422:         }
20423:     }
20424:     my $nochgmsg = &mt('No changes made to trust settings.');
20425:     if (keys(%changes) > 0) {
20426:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
20427:                                                  $dom);
20428:         if ($putresult eq 'ok') {
20429:             if (ref($defaultshash{'trust'}) eq 'HASH') {
20430:                 foreach my $prefix (@prefixes) {
20431:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
20432:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
20433:                     }
20434:                 }
20435:             }
20436:             my $cachetime = 24*60*60;
20437:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20438:             if (ref($lastactref) eq 'HASH') {
20439:                 $lastactref->{'domdefaults'} = 1;
20440:             }
20441:             if (keys(%changes) > 0) {
20442:                 my %lt = &trust_titles();
20443:                 $resulttext = &mt('Changes made:').'<ul>';
20444:                 foreach my $prefix (@prefixes) {
20445:                     if (ref($changes{$prefix}) eq 'HASH') {
20446:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
20447:                         foreach my $type (@types) {
20448:                             if (defined($changes{$prefix}{$type})) {
20449:                                 my ($newvalue,$notinuse);
20450:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
20451:                                     if (ref($defaultshash{'trust'}{$prefix})) {
20452:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
20453:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
20454:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
20455:                                             }
20456:                                         } else {
20457:                                             $notinuse = 1;
20458:                                         }
20459:                                     }
20460:                                 }
20461:                                 if ($notinuse) {
20462:                                     $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
20463:                                 } elsif ($newvalue eq '') {
20464:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
20465:                                 } else {
20466:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
20467:                                 }
20468:                             }
20469:                         }
20470:                         $resulttext .= '</ul>';
20471:                     }
20472:                 }
20473:                 $resulttext .= '</ul>';
20474:             } else {
20475:                 $resulttext = $nochgmsg;
20476:             }
20477:         } else {
20478:             $resulttext = '<span class="LC_error">'.
20479:                           &mt('An error occurred: [_1]',$putresult).'</span>';
20480:         }
20481:     } else {
20482:         $resulttext = $nochgmsg;
20483:     }
20484:     return $resulttext;
20485: }
20486: 
20487: sub modify_loadbalancing {
20488:     my ($dom,%domconfig) = @_;
20489:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
20490:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
20491:     my ($othertitle,$usertypes,$types) =
20492:         &Apache::loncommon::sorted_inst_types($dom);
20493:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
20494:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
20495:     my @sparestypes = ('primary','default');
20496:     my %typetitles = &sparestype_titles();
20497:     my $resulttext;
20498:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
20499:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
20500:         %existing = %{$domconfig{'loadbalancing'}};
20501:     }
20502:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
20503:                               \%currtargets,\%currrules,\%currcookies);
20504:     my ($saveloadbalancing,%defaultshash,%changes);
20505:     my ($alltypes,$othertypes,$titles) =
20506:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
20507:     my %ruletitles = &offloadtype_text();
20508:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
20509:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
20510:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
20511:         if ($balancer eq '') {
20512:             next;
20513:         }
20514:         if (!exists($servers{$balancer})) {
20515:             if (exists($currbalancer{$balancer})) {
20516:                 push(@{$changes{'delete'}},$balancer);
20517:             }
20518:             next;
20519:         }
20520:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
20521:             push(@{$changes{'delete'}},$balancer);
20522:             next;
20523:         }
20524:         if (!exists($currbalancer{$balancer})) {
20525:             push(@{$changes{'add'}},$balancer);
20526:         }
20527:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
20528:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
20529:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
20530:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
20531:             $saveloadbalancing = 1;
20532:         }
20533:         foreach my $sparetype (@sparestypes) {
20534:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
20535:             my @offloadto;
20536:             foreach my $target (@targets) {
20537:                 if (($servers{$target}) && ($target ne $balancer)) {
20538:                     if ($sparetype eq 'default') {
20539:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
20540:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
20541:                         }
20542:                     }
20543:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
20544:                         push(@offloadto,$target);
20545:                     }
20546:                 }
20547:             }
20548:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
20549:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
20550:                     push(@offloadto,$balancer);
20551:                 }
20552:             }
20553:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
20554:         }
20555:         if ($env{'form.loadbalancing_cookie_'.$i}) {
20556:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
20557:             if (exists($currbalancer{$balancer})) { 
20558:                 unless ($currcookies{$balancer}) {
20559:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
20560:                 }
20561:             }
20562:         } elsif (exists($currbalancer{$balancer})) {
20563:             if ($currcookies{$balancer}) {
20564:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
20565:             }
20566:         }
20567:         if (ref($currtargets{$balancer}) eq 'HASH') {
20568:             foreach my $sparetype (@sparestypes) {
20569:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
20570:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
20571:                     if (@targetdiffs > 0) {
20572:                         $changes{'curr'}{$balancer}{'targets'} = 1;
20573:                     }
20574:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
20575:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
20576:                         $changes{'curr'}{$balancer}{'targets'} = 1;
20577:                     }
20578:                 }
20579:             }
20580:         } else {
20581:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
20582:                 foreach my $sparetype (@sparestypes) {
20583:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
20584:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
20585:                             $changes{'curr'}{$balancer}{'targets'} = 1;
20586:                         }
20587:                     }
20588:                 }
20589:             }
20590:         }
20591:         my $ishomedom;
20592:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
20593:             $ishomedom = 1;
20594:         }
20595:         if (ref($alltypes) eq 'ARRAY') {
20596:             foreach my $type (@{$alltypes}) {
20597:                 my $rule;
20598:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
20599:                          (!$ishomedom)) {
20600:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
20601:                 }
20602:                 if ($rule eq 'specific') {
20603:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
20604:                     if (exists($servers{$specifiedhost})) {
20605:                         $rule = $specifiedhost;
20606:                     }
20607:                 }
20608:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
20609:                 if (ref($currrules{$balancer}) eq 'HASH') {
20610:                     if ($rule ne $currrules{$balancer}{$type}) {
20611:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
20612:                     }
20613:                 } elsif ($rule ne '') {
20614:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
20615:                 }
20616:             }
20617:         }
20618:     }
20619:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
20620:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
20621:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
20622:             $defaultshash{'loadbalancing'} = {};
20623:         }
20624:         my $putresult = &Apache::lonnet::put_dom('configuration',
20625:                                                  \%defaultshash,$dom);
20626:         if ($putresult eq 'ok') {
20627:             if (keys(%changes) > 0) {
20628:                 my %toupdate;
20629:                 if (ref($changes{'delete'}) eq 'ARRAY') {
20630:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
20631:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
20632:                         $toupdate{$balancer} = 1;
20633:                     }
20634:                 }
20635:                 if (ref($changes{'add'}) eq 'ARRAY') {
20636:                     foreach my $balancer (sort(@{$changes{'add'}})) {
20637:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
20638:                         $toupdate{$balancer} = 1;
20639:                     }
20640:                 }
20641:                 if (ref($changes{'curr'}) eq 'HASH') {
20642:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
20643:                         $toupdate{$balancer} = 1;
20644:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
20645:                             if ($changes{'curr'}{$balancer}{'targets'}) {
20646:                                 my %offloadstr;
20647:                                 foreach my $sparetype (@sparestypes) {
20648:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
20649:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
20650:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
20651:                                         }
20652:                                     }
20653:                                 }
20654:                                 if (keys(%offloadstr) == 0) {
20655:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
20656:                                 } else {
20657:                                     my $showoffload;
20658:                                     foreach my $sparetype (@sparestypes) {
20659:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
20660:                                         if (defined($offloadstr{$sparetype})) {
20661:                                             $showoffload .= $offloadstr{$sparetype};
20662:                                         } else {
20663:                                             $showoffload .= &mt('None');
20664:                                         }
20665:                                         $showoffload .= ('&nbsp;'x3);
20666:                                     }
20667:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
20668:                                 }
20669:                             }
20670:                         }
20671:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
20672:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
20673:                                 foreach my $type (@{$alltypes}) {
20674:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
20675:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
20676:                                         my $balancetext;
20677:                                         if ($rule eq '') {
20678:                                             $balancetext =  $ruletitles{'default'};
20679:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
20680:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
20681:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
20682:                                                 foreach my $sparetype (@sparestypes) {
20683:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
20684:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
20685:                                                     }
20686:                                                 }
20687:                                                 foreach my $item (@{$alltypes}) {
20688:                                                     next if ($item =~  /^_LC_ipchange/);
20689:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
20690:                                                     if ($hasrule eq 'homeserver') {
20691:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
20692:                                                     } else {
20693:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
20694:                                                             if ($servers{$hasrule}) {
20695:                                                                 $toupdate{$hasrule} = 1;
20696:                                                             }
20697:                                                         }
20698:                                                     }
20699:                                                 }
20700:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
20701:                                                     $balancetext =  $ruletitles{$rule};
20702:                                                 } else {
20703:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
20704:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
20705:                                                     if ($receiver) {
20706:                                                         $toupdate{$receiver};
20707:                                                     }
20708:                                                 }
20709:                                             } else {
20710:                                                 $balancetext =  $ruletitles{$rule};
20711:                                             }
20712:                                         } else {
20713:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
20714:                                         }
20715:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
20716:                                     }
20717:                                 }
20718:                             }
20719:                         }
20720:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
20721:                             $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
20722:                                                       $balancer).'</li>'; 
20723:                         }
20724:                         if (keys(%toupdate)) {
20725:                             my %thismachine;
20726:                             my $updatedhere;
20727:                             my $cachetime = 60*60*24;
20728:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
20729:                             foreach my $lonhost (keys(%toupdate)) {
20730:                                 if ($thismachine{$lonhost}) {
20731:                                     unless ($updatedhere) {
20732:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
20733:                                                                       $defaultshash{'loadbalancing'},
20734:                                                                       $cachetime);
20735:                                         $updatedhere = 1;
20736:                                     }
20737:                                 } else {
20738:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
20739:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
20740:                                 }
20741:                             }
20742:                         }
20743:                     }
20744:                 }
20745:                 if ($resulttext ne '') {
20746:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
20747:                 } else {
20748:                     $resulttext = $nochgmsg;
20749:                 }
20750:             } else {
20751:                 $resulttext = $nochgmsg;
20752:             }
20753:         } else {
20754:             $resulttext = '<span class="LC_error">'.
20755:                           &mt('An error occurred: [_1]',$putresult).'</span>';
20756:         }
20757:     } else {
20758:         $resulttext = $nochgmsg;
20759:     }
20760:     return $resulttext;
20761: }
20762: 
20763: sub recurse_check {
20764:     my ($chkcats,$categories,$depth,$name) = @_;
20765:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
20766:         my $chg = 0;
20767:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
20768:             my $category = $chkcats->[$depth]{$name}[$j];
20769:             my $item;
20770:             if ($category eq '') {
20771:                 $chg ++;
20772:             } else {
20773:                 my $deeper = $depth + 1;
20774:                 $item = &escape($category).':'.&escape($name).':'.$depth;
20775:                 if ($chg) {
20776:                     $categories->{$item} -= $chg;
20777:                 }
20778:                 &recurse_check($chkcats,$categories,$deeper,$category);
20779:                 $deeper --;
20780:             }
20781:         }
20782:     }
20783:     return;
20784: }
20785: 
20786: sub recurse_cat_deletes {
20787:     my ($item,$coursecategories,$deletions) = @_;
20788:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
20789:     my $subdepth = $depth + 1;
20790:     if (ref($coursecategories) eq 'HASH') {
20791:         foreach my $subitem (keys(%{$coursecategories})) {
20792:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
20793:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
20794:                 delete($coursecategories->{$subitem});
20795:                 $deletions->{$subitem} = 1;
20796:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
20797:             }
20798:         }
20799:     }
20800:     return;
20801: }
20802: 
20803: sub active_dc_picker {
20804:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
20805:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
20806:     my @domcoord = keys(%domcoords);
20807:     if (keys(%currhash)) {
20808:         foreach my $dc (keys(%currhash)) {
20809:             unless (exists($domcoords{$dc})) {
20810:                 push(@domcoord,$dc);
20811:             }
20812:         }
20813:     }
20814:     @domcoord = sort(@domcoord);
20815:     my $numdcs = scalar(@domcoord);
20816:     my $rows = 0;
20817:     my $table;
20818:     if ($numdcs > 1) {
20819:         $table = '<table>';
20820:         for (my $i=0; $i<@domcoord; $i++) {
20821:             my $rem = $i%($numinrow);
20822:             if ($rem == 0) {
20823:                 if ($i > 0) {
20824:                     $table .= '</tr>';
20825:                 }
20826:                 $table .= '<tr>';
20827:                 $rows ++;
20828:             }
20829:             my $check = '';
20830:             if ($inputtype eq 'radio') {
20831:                 if (keys(%currhash) == 0) {
20832:                     if (!$i) {
20833:                         $check = ' checked="checked"';
20834:                     }
20835:                 } elsif (exists($currhash{$domcoord[$i]})) {
20836:                     $check = ' checked="checked"';
20837:                 }
20838:             } else {
20839:                 if (exists($currhash{$domcoord[$i]})) {
20840:                     $check = ' checked="checked"';
20841:                 }
20842:             }
20843:             if ($i == @domcoord - 1) {
20844:                 my $colsleft = $numinrow - $rem;
20845:                 if ($colsleft > 1) {
20846:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
20847:                 } else {
20848:                     $table .= '<td class="LC_left_item">';
20849:                 }
20850:             } else {
20851:                 $table .= '<td class="LC_left_item">';
20852:             }
20853:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
20854:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
20855:             $table .= '<span class="LC_nobreak"><label>'.
20856:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
20857:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
20858:             if ($user ne $dcname.':'.$dcdom) {
20859:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
20860:             }
20861:             $table .= '</label></span></td>';
20862:         }
20863:         $table .= '</tr></table>';
20864:     } elsif ($numdcs == 1) {
20865:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
20866:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
20867:         if ($inputtype eq 'radio') {
20868:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
20869:             if ($user ne $dcname.':'.$dcdom) {
20870:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
20871:             }
20872:         } else {
20873:             my $check;
20874:             if (exists($currhash{$domcoord[0]})) {
20875:                 $check = ' checked="checked"';
20876:             }
20877:             $table = '<span class="LC_nobreak"><label>'.
20878:                      '<input type="checkbox" name="'.$name.'" '.
20879:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
20880:             if ($user ne $dcname.':'.$dcdom) {
20881:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
20882:             }
20883:             $table .= '</label></span>';
20884:             $rows ++;
20885:         }
20886:     }
20887:     return ($numdcs,$table,$rows);
20888: }
20889: 
20890: sub usersession_titles {
20891:     return &Apache::lonlocal::texthash(
20892:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
20893:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
20894:                spares => 'Servers offloaded to, when busy',
20895:                version => 'LON-CAPA version requirement',
20896:                excludedomain => 'Allow all, but exclude specific domains',
20897:                includedomain => 'Deny all, but include specific domains',
20898:                primary => 'Primary (checked first)',
20899:                default => 'Default',
20900:            );
20901: }
20902: 
20903: sub id_for_thisdom {
20904:     my (%servers) = @_;
20905:     my %altids;
20906:     foreach my $server (keys(%servers)) {
20907:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
20908:         if ($serverhome ne $server) {
20909:             $altids{$serverhome} = $server;
20910:         }
20911:     }
20912:     return %altids;
20913: }
20914: 
20915: sub count_servers {
20916:     my ($currbalancer,%servers) = @_;
20917:     my (@spares,$numspares);
20918:     foreach my $lonhost (sort(keys(%servers))) {
20919:         next if ($currbalancer eq $lonhost);
20920:         push(@spares,$lonhost);
20921:     }
20922:     if ($currbalancer) {
20923:         $numspares = scalar(@spares);
20924:     } else {
20925:         $numspares = scalar(@spares) - 1;
20926:     }
20927:     return ($numspares,@spares);
20928: }
20929: 
20930: sub lonbalance_targets_js {
20931:     my ($dom,$types,$servers,$settings) = @_;
20932:     my $select = &mt('Select');
20933:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
20934:     if (ref($servers) eq 'HASH') {
20935:         $alltargets = join("','",sort(keys(%{$servers})));
20936:         my @homedoms;
20937:         foreach my $server (sort(keys(%{$servers}))) {
20938:             if (&Apache::lonnet::host_domain($server) eq $dom) {
20939:                 push(@homedoms,'1');
20940:             } else {
20941:                 push(@homedoms,'0');
20942:             }
20943:         }
20944:         $allishome = join("','",@homedoms);
20945:     }
20946:     if (ref($types) eq 'ARRAY') {
20947:         if (@{$types} > 0) {
20948:             @alltypes = @{$types};
20949:         }
20950:     }
20951:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
20952:     $allinsttypes = join("','",@alltypes);
20953:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
20954:     if (ref($settings) eq 'HASH') {
20955:         %existing = %{$settings};
20956:     }
20957:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
20958:                               \%currtargets,\%currrules,\%currcookies);
20959:     my $balancers = join("','",sort(keys(%currbalancer)));
20960:     return <<"END";
20961: 
20962: <script type="text/javascript">
20963: // <![CDATA[
20964: 
20965: currBalancers = new Array('$balancers');
20966: 
20967: function toggleTargets(balnum) {
20968:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
20969:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
20970:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
20971:     var prevbalancer = prevhostitem.value;
20972:     var baltotal = document.getElementById('loadbalancing_total').value;
20973:     prevhostitem.value = balancer;
20974:     if (prevbalancer != '') {
20975:         var prevIdx = currBalancers.indexOf(prevbalancer);
20976:         if (prevIdx != -1) {
20977:             currBalancers.splice(prevIdx,1);
20978:         }
20979:     }
20980:     if (balancer == '') {
20981:         hideSpares(balnum);
20982:     } else {
20983:         var currIdx = currBalancers.indexOf(balancer);
20984:         if (currIdx == -1) {
20985:             currBalancers.push(balancer);
20986:         }
20987:         var homedoms = new Array('$allishome');
20988:         var ishomedom = homedoms[lonhostitem.selectedIndex];
20989:         showSpares(balancer,ishomedom,balnum);
20990:     }
20991:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
20992:     return;
20993: }
20994: 
20995: function showSpares(balancer,ishomedom,balnum) {
20996:     var alltargets = new Array('$alltargets');
20997:     var insttypes = new Array('$allinsttypes');
20998:     var offloadtypes = new Array('primary','default');
20999: 
21000:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
21001:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
21002:  
21003:     for (var i=0; i<offloadtypes.length; i++) {
21004:         var count = 0;
21005:         for (var j=0; j<alltargets.length; j++) {
21006:             if (alltargets[j] != balancer) {
21007:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
21008:                 item.value = alltargets[j];
21009:                 item.style.textAlign='left';
21010:                 item.style.textFace='normal';
21011:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
21012:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
21013:                     item.disabled = '';
21014:                 } else {
21015:                     item.disabled = 'disabled';
21016:                     item.checked = false;
21017:                 }
21018:                 count ++;
21019:             }
21020:         }
21021:     }
21022:     for (var k=0; k<insttypes.length; k++) {
21023:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
21024:             if (ishomedom == 1) {
21025:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
21026:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
21027:             } else {
21028:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
21029:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
21030:             }
21031:         } else {
21032:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
21033:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
21034:         }
21035:         if ((insttypes[k] != '_LC_external') && 
21036:             ((insttypes[k] != '_LC_internetdom') ||
21037:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
21038:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
21039:             item.options.length = 0;
21040:             item.options[0] = new Option("","",true,true);
21041:             var idx = 0;
21042:             for (var m=0; m<alltargets.length; m++) {
21043:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
21044:                     idx ++;
21045:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
21046:                 }
21047:             }
21048:         }
21049:     }
21050:     return;
21051: }
21052: 
21053: function hideSpares(balnum) {
21054:     var alltargets = new Array('$alltargets');
21055:     var insttypes = new Array('$allinsttypes');
21056:     var offloadtypes = new Array('primary','default');
21057: 
21058:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
21059:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
21060: 
21061:     var total = alltargets.length - 1;
21062:     for (var i=0; i<offloadtypes; i++) {
21063:         for (var j=0; j<total; j++) {
21064:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
21065:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
21066:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
21067:         }
21068:     }
21069:     for (var k=0; k<insttypes.length; k++) {
21070:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
21071:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
21072:         if (insttypes[k] != '_LC_external') {
21073:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
21074:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
21075:         }
21076:     }
21077:     return;
21078: }
21079: 
21080: function checkOffloads(item,balnum,type) {
21081:     var alltargets = new Array('$alltargets');
21082:     var offloadtypes = new Array('primary','default');
21083:     if (item.checked) {
21084:         var total = alltargets.length - 1;
21085:         var other;
21086:         if (type == offloadtypes[0]) {
21087:             other = offloadtypes[1];
21088:         } else {
21089:             other = offloadtypes[0];
21090:         }
21091:         for (var i=0; i<total; i++) {
21092:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
21093:             if (server == item.value) {
21094:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
21095:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
21096:                 }
21097:             }
21098:         }
21099:     }
21100:     return;
21101: }
21102: 
21103: function singleServerToggle(balnum,type) {
21104:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
21105:     if (offloadtoSelIdx == 0) {
21106:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
21107:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
21108: 
21109:     } else {
21110:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
21111:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
21112:     }
21113:     return;
21114: }
21115: 
21116: function balanceruleChange(formname,balnum,type) {
21117:     if (type == '_LC_external') {
21118:         return;
21119:     }
21120:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
21121:     for (var i=0; i<typesRules.length; i++) {
21122:         if (formname.elements[typesRules[i]].checked) {
21123:             if (formname.elements[typesRules[i]].value != 'specific') {
21124:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
21125:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
21126:             } else {
21127:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
21128:             }
21129:         }
21130:     }
21131:     return;
21132: }
21133: 
21134: function balancerDeleteChange(balnum) {
21135:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
21136:     var baltotal = document.getElementById('loadbalancing_total').value;
21137:     var addtarget;
21138:     var removetarget;
21139:     var action = 'delete';
21140:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
21141:         var lonhost = hostitem.value;
21142:         var currIdx = currBalancers.indexOf(lonhost);
21143:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
21144:             if (currIdx != -1) {
21145:                 currBalancers.splice(currIdx,1);
21146:             }
21147:             addtarget = lonhost;
21148:         } else {
21149:             if (currIdx == -1) {
21150:                 currBalancers.push(lonhost);
21151:             }
21152:             removetarget = lonhost;
21153:             action = 'undelete';
21154:         }
21155:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
21156:     }
21157:     return;
21158: }
21159: 
21160: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
21161:     if (baltotal > 1) {
21162:         var offloadtypes = new Array('primary','default');
21163:         var alltargets = new Array('$alltargets');
21164:         var insttypes = new Array('$allinsttypes');
21165:         for (var i=0; i<baltotal; i++) {
21166:             if (i != balnum) {
21167:                 for (var j=0; j<offloadtypes.length; j++) {
21168:                     var total = alltargets.length - 1;
21169:                     for (var k=0; k<total; k++) {
21170:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
21171:                         var server = serveritem.value;
21172:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
21173:                             if (server == addtarget) {
21174:                                 serveritem.disabled = '';
21175:                             }
21176:                         }
21177:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
21178:                             if (server == removetarget) {
21179:                                 serveritem.disabled = 'disabled';
21180:                                 serveritem.checked = false;
21181:                             }
21182:                         }
21183:                     }
21184:                 }
21185:                 for (var j=0; j<insttypes.length; j++) {
21186:                     if (insttypes[j] != '_LC_external') {
21187:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
21188:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
21189:                             var currSel = singleserver.selectedIndex;
21190:                             var currVal = singleserver.options[currSel].value;
21191:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
21192:                                 var numoptions = singleserver.options.length;
21193:                                 var needsnew = 1;
21194:                                 for (var k=0; k<numoptions; k++) {
21195:                                     if (singleserver.options[k] == addtarget) {
21196:                                         needsnew = 0;
21197:                                         break;
21198:                                     }
21199:                                 }
21200:                                 if (needsnew == 1) {
21201:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
21202:                                 }
21203:                             }
21204:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
21205:                                 singleserver.options.length = 0;
21206:                                 if ((currVal) && (currVal != removetarget)) {
21207:                                     singleserver.options[0] = new Option("","",false,false);
21208:                                 } else {
21209:                                     singleserver.options[0] = new Option("","",true,true);
21210:                                 }
21211:                                 var idx = 0;
21212:                                 for (var m=0; m<alltargets.length; m++) {
21213:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
21214:                                         idx ++;
21215:                                         if (currVal == alltargets[m]) {
21216:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
21217:                                         } else {
21218:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
21219:                                         }
21220:                                     }
21221:                                 }
21222:                             }
21223:                         }
21224:                     }
21225:                 }
21226:             }
21227:         }
21228:     }
21229:     return;
21230: }
21231: 
21232: // ]]>
21233: </script>
21234: 
21235: END
21236: }
21237: 
21238: 
21239: sub new_spares_js {
21240:     my @sparestypes = ('primary','default');
21241:     my $types = join("','",@sparestypes);
21242:     my $select = &mt('Select');
21243:     return <<"END";
21244: 
21245: <script type="text/javascript">
21246: // <![CDATA[
21247: 
21248: function updateNewSpares(formname,lonhost) {
21249:     var types = new Array('$types');
21250:     var include = new Array();
21251:     var exclude = new Array();
21252:     for (var i=0; i<types.length; i++) {
21253:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
21254:         for (var j=0; j<spareboxes.length; j++) {
21255:             if (formname.elements[spareboxes[j]].checked) {
21256:                 exclude.push(formname.elements[spareboxes[j]].value);
21257:             } else {
21258:                 include.push(formname.elements[spareboxes[j]].value);
21259:             }
21260:         }
21261:     }
21262:     for (var i=0; i<types.length; i++) {
21263:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
21264:         var selIdx = newSpare.selectedIndex;
21265:         var currnew = newSpare.options[selIdx].value;
21266:         var okSpares = new Array();
21267:         for (var j=0; j<newSpare.options.length; j++) {
21268:             var possible = newSpare.options[j].value;
21269:             if (possible != '') {
21270:                 if (exclude.indexOf(possible) == -1) {
21271:                     okSpares.push(possible);
21272:                 } else {
21273:                     if (currnew == possible) {
21274:                         selIdx = 0;
21275:                     }
21276:                 }
21277:             }
21278:         }
21279:         for (var k=0; k<include.length; k++) {
21280:             if (okSpares.indexOf(include[k]) == -1) {
21281:                 okSpares.push(include[k]);
21282:             }
21283:         }
21284:         okSpares.sort();
21285:         newSpare.options.length = 0;
21286:         if (selIdx == 0) {
21287:             newSpare.options[0] = new Option("$select","",true,true);
21288:         } else {
21289:             newSpare.options[0] = new Option("$select","",false,false);
21290:         }
21291:         for (var m=0; m<okSpares.length; m++) {
21292:             var idx = m+1;
21293:             var selThis = 0;
21294:             if (selIdx != 0) {
21295:                 if (okSpares[m] == currnew) {
21296:                     selThis = 1;
21297:                 }
21298:             }
21299:             if (selThis == 1) {
21300:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
21301:             } else {
21302:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
21303:             }
21304:         }
21305:     }
21306:     return;
21307: }
21308: 
21309: function checkNewSpares(lonhost,type) {
21310:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
21311:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
21312:     if (chosen != '') {
21313:         var othertype;
21314:         var othernewSpare;
21315:         if (type == 'primary') {
21316:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
21317:         }
21318:         if (type == 'default') {
21319:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
21320:         }
21321:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
21322:             othernewSpare.selectedIndex = 0;
21323:         }
21324:     }
21325:     return;
21326: }
21327: 
21328: // ]]>
21329: </script>
21330: 
21331: END
21332: 
21333: }
21334: 
21335: sub common_domprefs_js {
21336:     return <<"END";
21337: 
21338: <script type="text/javascript">
21339: // <![CDATA[
21340: 
21341: function getIndicesByName(formname,item) {
21342:     var group = new Array();
21343:     for (var i=0;i<formname.elements.length;i++) {
21344:         if (formname.elements[i].name == item) {
21345:             group.push(formname.elements[i].id);
21346:         }
21347:     }
21348:     return group;
21349: }
21350: 
21351: // ]]>
21352: </script>
21353: 
21354: END
21355: 
21356: }
21357: 
21358: sub recaptcha_js {
21359:     my %lt = &captcha_phrases();
21360:     return <<"END";
21361: 
21362: <script type="text/javascript">
21363: // <![CDATA[
21364: 
21365: function updateCaptcha(caller,context) {
21366:     var privitem;
21367:     var pubitem;
21368:     var privtext;
21369:     var pubtext;
21370:     var versionitem;
21371:     var versiontext;
21372:     if (document.getElementById(context+'_recaptchapub')) {
21373:         pubitem = document.getElementById(context+'_recaptchapub');
21374:     } else {
21375:         return;
21376:     }
21377:     if (document.getElementById(context+'_recaptchapriv')) {
21378:         privitem = document.getElementById(context+'_recaptchapriv');
21379:     } else {
21380:         return;
21381:     }
21382:     if (document.getElementById(context+'_recaptchapubtxt')) {
21383:         pubtext = document.getElementById(context+'_recaptchapubtxt');
21384:     } else {
21385:         return;
21386:     }
21387:     if (document.getElementById(context+'_recaptchaprivtxt')) {
21388:         privtext = document.getElementById(context+'_recaptchaprivtxt');
21389:     } else {
21390:         return;
21391:     }
21392:     if (document.getElementById(context+'_recaptchaversion')) {
21393:         versionitem = document.getElementById(context+'_recaptchaversion');
21394:     } else {
21395:         return;
21396:     }
21397:     if (document.getElementById(context+'_recaptchavertxt')) {
21398:         versiontext = document.getElementById(context+'_recaptchavertxt');
21399:     } else {
21400:         return;
21401:     }
21402:     if (caller.checked) {
21403:         if (caller.value == 'recaptcha') {
21404:             pubitem.type = 'text';
21405:             privitem.type = 'text';
21406:             pubitem.size = '40';
21407:             privitem.size = '40';
21408:             pubtext.innerHTML = "$lt{'pub'}";
21409:             privtext.innerHTML = "$lt{'priv'}";
21410:             versionitem.type = 'text';
21411:             versionitem.size = '3';
21412:             versiontext.innerHTML = "$lt{'ver'}";
21413:         } else {
21414:             pubitem.type = 'hidden';
21415:             privitem.type = 'hidden';
21416:             versionitem.type = 'hidden';
21417:             pubtext.innerHTML = '';
21418:             privtext.innerHTML = '';
21419:             versiontext.innerHTML = '';
21420:         }
21421:     }
21422:     return;
21423: }
21424: 
21425: // ]]>
21426: </script>
21427: 
21428: END
21429: 
21430: }
21431: 
21432: sub toggle_display_js {
21433:     return <<"END";
21434: 
21435: <script type="text/javascript">
21436: // <![CDATA[
21437: 
21438: function toggleDisplay(domForm,caller) {
21439:     if (document.getElementById(caller)) {
21440:         var divitem = document.getElementById(caller);
21441:         var optionsElement = domForm.coursecredits;
21442:         var checkval = 1;
21443:         var dispval = 'block';
21444:         var selfcreateRegExp = /^cancreate_emailverified/;
21445:         if (caller == 'emailoptions') {
21446:             optionsElement = domForm.cancreate_email;
21447:         }
21448:         if (caller == 'studentsubmission') {
21449:             optionsElement = domForm.postsubmit;
21450:         }
21451:         if (caller == 'cloneinstcode') {
21452:             optionsElement = domForm.canclone;
21453:             checkval = 'instcode';
21454:         }
21455:         if (selfcreateRegExp.test(caller)) {
21456:             optionsElement = domForm.elements[caller];
21457:             checkval = 'other';
21458:             dispval = 'inline'
21459:         }
21460:         if (optionsElement.length) {
21461:             var currval;
21462:             for (var i=0; i<optionsElement.length; i++) {
21463:                 if (optionsElement[i].checked) {
21464:                    currval = optionsElement[i].value;
21465:                 }
21466:             }
21467:             if (currval == checkval) {
21468:                 divitem.style.display = dispval;
21469:             } else {
21470:                 divitem.style.display = 'none';
21471:             }
21472:         }
21473:     }
21474:     return;
21475: }
21476: 
21477: // ]]>
21478: </script>
21479: 
21480: END
21481: 
21482: }
21483: 
21484: sub captcha_phrases {
21485:     return &Apache::lonlocal::texthash (
21486:                  priv => 'Private key',
21487:                  pub  => 'Public key',
21488:                  original  => 'original (CAPTCHA)',
21489:                  recaptcha => 'successor (ReCAPTCHA)',
21490:                  notused   => 'unused',
21491:                  ver => 'ReCAPTCHA version (1 or 2)',
21492:     );
21493: }
21494: 
21495: sub devalidate_remote_domconfs {
21496:     my ($dom,$cachekeys) = @_;
21497:     return unless (ref($cachekeys) eq 'HASH');
21498:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
21499:     my %thismachine;
21500:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
21501:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions',
21502:                       'directorysrch','passwdconf','cats','proxyalias');
21503:     if (keys(%servers)) {
21504:         foreach my $server (keys(%servers)) {
21505:             next if ($thismachine{$server});
21506:             my @cached;
21507:             foreach my $name (@posscached) {
21508:                 if ($cachekeys->{$name}) {
21509:                     if ($name eq 'proxyalias') {
21510:                         if (ref($cachekeys->{$name}) eq 'HASH') {  
21511:                             foreach my $key (keys(%{$cachekeys->{$name}})) {
21512:                                 push(@cached,&escape($name).':'.&escape($key));
21513:                             }
21514:                         }
21515:                     } else {
21516:                         push(@cached,&escape($name).':'.&escape($dom));
21517:                     }
21518:                 }
21519:             }
21520:             if (@cached) {
21521:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
21522:             }
21523:         }
21524:     }
21525:     return;
21526: }
21527: 
21528: 1;

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