File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.385: download - view: text, annotated - select for diffs
Wed Sep 1 00:21:52 2021 UTC (2 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6959 Automated update can be configured to skip checking for changes
  in user information for users deemed inactive.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.385 2021/09/01 00:21:52 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: ###############################################################
   30: ###############################################################
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::domainprefs.pm
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: Handles configuration of a LON-CAPA domain.  
   41: 
   42: This is part of the LearningOnline Network with CAPA project
   43: described at http://www.lon-capa.org.
   44: 
   45: 
   46: =head1 OVERVIEW
   47: 
   48: Each institution using LON-CAPA will typically have a single domain designated 
   49: for use by individuals affiliated with the institution.  Accordingly, each domain
   50: may define a default set of logos and a color scheme which can be used to "brand"
   51: the LON-CAPA instance. In addition, an institution will typically have a language
   52: and timezone which are used for the majority of courses.
   53: 
   54: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   55: host of other domain-wide settings which determine the types of functionality
   56: available to users and courses in the domain.
   57: 
   58: There is also a mechanism to configure cataloging of courses in the domain, and
   59: controls on the operation of automated processes which govern such things as
   60: roster updates, user directory updates and processing of course requests.
   61: 
   62: The domain coordination manual which is built dynamically on install/update of 
   63: LON-CAPA from the relevant help items provides more information about domain 
   64: configuration.
   65: 
   66: Most of the domain settings are stored in the configuration.db GDBM file which is
   67: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   68: where $dom is the domain.  The configuration.db stores settings in a number of 
   69: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   70: the domain as files (e.g., image files for logos etc., or plain text files for
   71: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   72: session hosted on the primary library server in the domain, as these files are 
   73: stored in author space belonging to a special $dom-domainconfig user.   
   74: 
   75: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   76: the current settings, and provides an interface to make modifications.
   77: 
   78: =head1 SUBROUTINES
   79: 
   80: =over
   81: 
   82: =item print_quotas()
   83: 
   84: Inputs: 4 
   85: 
   86: $dom,$settings,$rowtotal,$action.
   87: 
   88: $dom is the domain, $settings is a reference to a hash of current settings for
   89: the current context, $rowtotal is a reference to the scalar used to record the 
   90: number of rows displayed on the page, and $action is the context (quotas, 
   91: requestcourses or requestauthor).
   92: 
   93: The print_quotas routine was orginally created to display/store information
   94: about default quota sizes for portfolio spaces for the different types of 
   95: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   96: but is now also used to manage availability of user tools: 
   97: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   98: used by course owners to request creation of a course, and to display/store
   99: default quota sizes for Authoring Spaces.
  100: 
  101: Outputs: 1
  102: 
  103: $datatable  - HTML containing form elements which allow settings to be changed. 
  104: 
  105: In the case of course requests, radio buttons are displayed for each institutional
  106: affiliate type (and also default, and _LC_adv) for each of the course types 
  107: (official, unofficial, community, textbook, placement, and lti).  
  108: In each case the radio buttons allow the selection of one of four values:  
  109: 
  110: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  111: which have the following effects:
  112: 
  113: 0
  114: 
  115: =over
  116: 
  117: - course requests are not allowed for this course types/affiliation
  118: 
  119: =back
  120: 
  121: approval 
  122: 
  123: =over 
  124: 
  125: - course requests must be approved by a Doman Coordinator in the 
  126: course's domain
  127: 
  128: =back
  129: 
  130: validate 
  131: 
  132: =over
  133: 
  134: - an institutional validation (e.g., check requestor is instructor
  135: of record) needs to be passed before the course will be created.  The required
  136: validation is in localenroll.pm on the primary library server for the course 
  137: domain.
  138: 
  139: =back
  140: 
  141: autolimit 
  142: 
  143: =over
  144:  
  145: - course requests will be processed automatically up to a limit of
  146: N requests for the course type for the particular requestor.
  147: If N is undefined, there is no limit to the number of course requests
  148: which a course owner may submit and have processed automatically. 
  149: 
  150: =back
  151: 
  152: =item modify_quotas() 
  153: 
  154: =back
  155: 
  156: =cut
  157: 
  158: package Apache::domainprefs;
  159: 
  160: use strict;
  161: use Apache::Constants qw(:common :http);
  162: use Apache::lonnet;
  163: use Apache::loncommon();
  164: use Apache::lonhtmlcommon();
  165: use Apache::lonlocal;
  166: use Apache::lonmsg();
  167: use Apache::lonconfigsettings;
  168: use Apache::lonuserutils();
  169: use Apache::loncoursequeueadmin();
  170: use LONCAPA qw(:DEFAULT :match);
  171: use LONCAPA::Enrollment;
  172: use LONCAPA::lonauthcgi();
  173: use LONCAPA::SSL;
  174: use File::Copy;
  175: use Locale::Language;
  176: use DateTime::TimeZone;
  177: use DateTime::Locale;
  178: use Time::HiRes qw( sleep );
  179: use Net::CIDR;
  180: 
  181: my $registered_cleanup;
  182: my $modified_urls;
  183: 
  184: sub handler {
  185:     my $r=shift;
  186:     if ($r->header_only) {
  187:         &Apache::loncommon::content_type($r,'text/html');
  188:         $r->send_http_header;
  189:         return OK;
  190:     }
  191: 
  192:     my $context = 'domain';
  193:     my $dom = $env{'request.role.domain'};
  194:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  195:     if (&Apache::lonnet::allowed('mau',$dom)) {
  196:         &Apache::loncommon::content_type($r,'text/html');
  197:         $r->send_http_header;
  198:     } else {
  199:         $env{'user.error.msg'}=
  200:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  201:         return HTTP_NOT_ACCEPTABLE;
  202:     }
  203: 
  204:     $registered_cleanup=0;
  205:     @{$modified_urls}=();
  206: 
  207:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  208:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  209:                                             ['phase','actions']);
  210:     my $phase = 'pickactions';
  211:     if ( exists($env{'form.phase'}) ) {
  212:         $phase = $env{'form.phase'};
  213:     }
  214:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  215:     my %domconfig =
  216:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  217:                 'quotas','autoenroll','autoupdate','autocreate',
  218:                 'directorysrch','usercreation','usermodification',
  219:                 'contacts','defaults','scantron','coursecategories',
  220:                 'serverstatuses','requestcourses','helpsettings',
  221:                 'coursedefaults','usersessions','loadbalancing',
  222:                 'requestauthor','selfenrollment','inststatus',
  223:                 'ltitools','ssl','trust','lti','privacy','passwords',
  224:                 'proctoring','wafproxy'],$dom);
  225:     my %encconfig =
  226:         &Apache::lonnet::get_dom('encconfig',['ltitools','lti','proctoring'],$dom,undef,1);
  227:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  228:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  229:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  230:                 if ((ref($domconfig{'ltitools'}{$id}) eq 'HASH') &&
  231:                     (ref($encconfig{'ltitools'}{$id}) eq 'HASH')) {
  232:                     foreach my $item ('key','secret') {
  233:                         $domconfig{'ltitools'}{$id}{$item} = $encconfig{'ltitools'}{$id}{$item};
  234:                     }
  235:                 }
  236:             }
  237:         }
  238:     }
  239:     if (ref($domconfig{'lti'}) eq 'HASH') {
  240:         if (ref($encconfig{'lti'}) eq 'HASH') {
  241:             foreach my $id (keys(%{$domconfig{'lti'}})) {
  242:                 if ((ref($domconfig{'lti'}{$id}) eq 'HASH') &&
  243:                     (ref($encconfig{'lti'}{$id}) eq 'HASH')) {
  244:                     foreach my $item ('key','secret') {
  245:                         $domconfig{'lti'}{$id}{$item} = $encconfig{'lti'}{$id}{$item};
  246:                     }
  247:                 }
  248:             }
  249:         }
  250:     }
  251:     if (ref($domconfig{'proctoring'}) eq 'HASH') {
  252:         if (ref($encconfig{'proctoring'}) eq 'HASH') {
  253:             foreach my $provider (keys(%{$domconfig{'proctoring'}})) {
  254:                 if ((ref($domconfig{'proctoring'}{$provider}) eq 'HASH') &&
  255:                     (ref($encconfig{'proctoring'}{$provider}) eq 'HASH')) {
  256:                     foreach my $item ('key','secret') {
  257:                         $domconfig{'proctoring'}{$provider}{$item} = $encconfig{'proctoring'}{$provider}{$item};
  258:                     }
  259:                 }
  260:             }
  261:         }
  262:     }
  263:     my @prefs_order = ('rolecolors','login','defaults','wafproxy','passwords','quotas',
  264:                        'autoenroll','autoupdate','autocreate','directorysrch',
  265:                        'contacts','privacy','usercreation','selfcreation',
  266:                        'usermodification','scantron','requestcourses','requestauthor',
  267:                        'coursecategories','serverstatuses','helpsettings','coursedefaults',
  268:                        'ltitools','proctoring','selfenrollment','usersessions','ssl',
  269:                        'trust','lti');
  270:     my %existing;
  271:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  272:         %existing = %{$domconfig{'loadbalancing'}};
  273:     }
  274:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  275:         push(@prefs_order,'loadbalancing');
  276:     }
  277:     my %prefs = (
  278:         'rolecolors' =>
  279:                    { text => 'Default color schemes',
  280:                      help => 'Domain_Configuration_Color_Schemes',
  281:                      header => [{col1 => 'Student Settings',
  282:                                  col2 => '',},
  283:                                 {col1 => 'Coordinator Settings',
  284:                                  col2 => '',},
  285:                                 {col1 => 'Author Settings',
  286:                                  col2 => '',},
  287:                                 {col1 => 'Administrator Settings',
  288:                                  col2 => '',}],
  289:                       print => \&print_rolecolors,
  290:                       modify => \&modify_rolecolors,
  291:                     },
  292:         'login' =>
  293:                     { text => 'Log-in page options',
  294:                       help => 'Domain_Configuration_Login_Page',
  295:                       header => [{col1 => 'Log-in Page Items',
  296:                                   col2 => '',},
  297:                                  {col1 => 'Log-in Help',
  298:                                   col2 => 'Value'},
  299:                                  {col1 => 'Custom HTML in document head',
  300:                                   col2 => 'Value'}],
  301:                       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(s)',
  318:                                   col2 => 'Servers and WAF/Reverse Proxy alias(es)',
  319:                                  },
  320:                                  {col1 => 'Domain(s)',
  321:                                   col2 => 'WAF Configuration',}],
  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:     } elsif ($action eq 'wafproxy') {
  859:         $output .= &wafproxy_javascript($dom);
  860:     } elsif ($action eq 'autoupdate') {
  861:         $output .= &autoupdate_javascript();
  862:     }
  863:     $output .=
  864:          '<table class="LC_nested_outer">
  865:           <tr>
  866:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  867:            &mt($item->{text}).'&nbsp;'.
  868:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  869:           '</tr>';
  870:     $rowtotal ++;
  871:     my $numheaders = 1;
  872:     if (ref($item->{'header'}) eq 'ARRAY') {
  873:         $numheaders = scalar(@{$item->{'header'}});
  874:     }
  875:     if ($numheaders > 1) {
  876:         my $colspan = '';
  877:         my $rightcolspan = '';
  878:         my $leftnobr = '';
  879:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  880:             ($action eq 'directorysrch') ||
  881:             (($action eq 'login') && ($numheaders < 4))) {
  882:             $colspan = ' colspan="2"';
  883:         }
  884:         if ($action eq 'usersessions') {
  885:             $rightcolspan = ' colspan="3"'; 
  886:         }
  887:         if ($action eq 'passwords') {
  888:             $leftnobr = ' LC_nobreak';
  889:         }
  890:         $output .= '
  891:           <tr>
  892:            <td>
  893:             <table class="LC_nested">
  894:              <tr class="LC_info_row">
  895:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  896:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  897:              </tr>';
  898:         $rowtotal ++;
  899:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  900:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  901:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  902:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  903:             ($action eq 'contacts') || ($action eq 'privacy') || ($action eq 'wafproxy')) {
  904:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  905:         } elsif ($action eq 'passwords') {
  906:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
  907:         } elsif ($action eq 'coursecategories') {
  908:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  909:         } elsif ($action eq 'scantron') {
  910:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
  911:         } elsif ($action eq 'login') {
  912:             if ($numheaders == 4) {
  913:                 $colspan = ' colspan="2"';
  914:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  915:             } else {
  916:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  917:             }
  918:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  919:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  920:         } elsif ($action eq 'rolecolors') {
  921:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  922:         }
  923:         $output .= '
  924:            </table>
  925:           </td>
  926:          </tr>
  927:          <tr>
  928:            <td>
  929:             <table class="LC_nested">
  930:              <tr class="LC_info_row">
  931:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  932:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  933:              </tr>';
  934:             $rowtotal ++;
  935:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  936:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  937:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
  938:             ($action eq 'trust') || ($action eq 'contacts') ||
  939:             ($action eq 'privacy') || ($action eq 'passwords')) {
  940:             if ($action eq 'coursecategories') {
  941:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  942:                 $colspan = ' colspan="2"';
  943:             } elsif ($action eq 'trust') {
  944:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
  945:             } elsif ($action eq 'passwords') {
  946:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
  947:             } else {
  948:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  949:             }
  950:             if ($action eq 'trust') {
  951:                 $output .= '
  952:             </table>
  953:           </td>
  954:          </tr>';
  955:                 my @trusthdrs = qw(2 3 4 5 6 7);
  956:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
  957:                 for (my $i=0; $i<@trusthdrs; $i++) {
  958:                     $output .= '
  959:          <tr>
  960:            <td>
  961:             <table class="LC_nested">
  962:              <tr class="LC_info_row">
  963:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
  964:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
  965:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
  966:             </table>
  967:           </td>
  968:          </tr>';
  969:                 }
  970:                 $output .= '
  971:          <tr>
  972:            <td>
  973:             <table class="LC_nested">
  974:              <tr class="LC_info_row">
  975:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
  976:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
  977:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  978:             } else {
  979:                 $output .= '
  980:            </table>
  981:           </td>
  982:          </tr>
  983:          <tr>
  984:            <td>
  985:             <table class="LC_nested">
  986:              <tr class="LC_info_row">
  987:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  988:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  989:              </tr>'."\n";
  990:                 if ($action eq 'coursecategories') {
  991:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  992:                 } elsif (($action eq 'contacts') || ($action eq 'privacy') || ($action eq 'passwords')) {
  993:                     if ($action eq 'passwords') {
  994:                         $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal);
  995:                     } else {
  996:                         $output .= $item->{'print'}->('lower',$dom,$settings,\$rowtotal);
  997:                     }
  998:                     $output .= '
  999:              </tr>
 1000:             </table>
 1001:            </td>
 1002:           </tr>
 1003:           <tr>
 1004:            <td>
 1005:             <table class="LC_nested">
 1006:              <tr class="LC_info_row">
 1007:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1008:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'."\n";
 1009:                     if ($action eq 'passwords') {
 1010:                         $output .= $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal);
 1011:                     } else {
 1012:                         $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1013:                     }
 1014:                     $output .= '
 1015:             </table>
 1016:           </td>
 1017:          </tr>
 1018:          <tr>';
 1019:                 } else {
 1020:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1021:                 }
 1022:             }
 1023:             $rowtotal ++;
 1024:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
 1025:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
 1026:                  ($action eq 'helpsettings') || ($action eq 'wafproxy')) {
 1027:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1028:         } elsif ($action eq 'scantron') {
 1029:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
 1030:         } elsif ($action eq 'ssl') {
 1031:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
 1032:             </table>
 1033:           </td>
 1034:          </tr>
 1035:          <tr>
 1036:            <td>
 1037:             <table class="LC_nested">
 1038:              <tr class="LC_info_row">
 1039:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1040:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1041:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
 1042:             </table>
 1043:           </td>
 1044:          </tr>
 1045:          <tr>
 1046:            <td>
 1047:             <table class="LC_nested">
 1048:              <tr class="LC_info_row">
 1049:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1050:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
 1051:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1052:         } elsif ($action eq 'login') {
 1053:             if ($numheaders == 4) {
 1054:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
 1055:            </table>
 1056:           </td>
 1057:          </tr>
 1058:          <tr>
 1059:            <td>
 1060:             <table class="LC_nested">
 1061:              <tr class="LC_info_row">
 1062:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1063:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1064:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1065:                 $rowtotal ++;
 1066:             } else {
 1067:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1068:             }
 1069:             $output .= '
 1070:            </table>
 1071:           </td>
 1072:          </tr>
 1073:          <tr>
 1074:            <td>
 1075:             <table class="LC_nested">
 1076:              <tr class="LC_info_row">';
 1077:             if ($numheaders == 4) {
 1078:                 $output .= '
 1079:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1080:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1081:              </tr>';
 1082:             } else {
 1083:                 $output .= '
 1084:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1085:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1086:              </tr>';
 1087:             }
 1088:             $rowtotal ++;
 1089:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
 1090:         } elsif ($action eq 'requestcourses') {
 1091:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1092:             $rowtotal ++;
 1093:             $output .= &print_studentcode($settings,\$rowtotal).'
 1094:            </table>
 1095:           </td>
 1096:          </tr>
 1097:          <tr>
 1098:            <td>
 1099:             <table class="LC_nested">
 1100:              <tr class="LC_info_row">
 1101:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1102:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
 1103:                        &textbookcourses_javascript($settings).
 1104:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
 1105:             </table>
 1106:            </td>
 1107:           </tr>
 1108:          <tr>
 1109:            <td>
 1110:             <table class="LC_nested">
 1111:              <tr class="LC_info_row">
 1112:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1113:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
 1114:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
 1115:             </table>
 1116:            </td>
 1117:           </tr>
 1118:           <tr>
 1119:            <td>
 1120:             <table class="LC_nested">
 1121:              <tr class="LC_info_row">
 1122:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1123:               <td class="LC_right_item" style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1124:              </tr>'.
 1125:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
 1126:         } elsif ($action eq 'requestauthor') {
 1127:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1128:             $rowtotal ++;
 1129:         } elsif ($action eq 'rolecolors') {
 1130:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
 1131:            </table>
 1132:           </td>
 1133:          </tr>
 1134:          <tr>
 1135:            <td>
 1136:             <table class="LC_nested">
 1137:              <tr class="LC_info_row">
 1138:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.
 1139:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
 1140:               <td class="LC_right_item" style="vertical-align: top">'.
 1141:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1142:              </tr>'.
 1143:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1144:            </table>
 1145:           </td>
 1146:          </tr>
 1147:          <tr>
 1148:            <td>
 1149:             <table class="LC_nested">
 1150:              <tr class="LC_info_row">
 1151:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1152:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1153:              </tr>'.
 1154:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1155:             $rowtotal += 2;
 1156:         }
 1157:     } else {
 1158:         $output .= '
 1159:           <tr>
 1160:            <td>
 1161:             <table class="LC_nested">
 1162:              <tr class="LC_info_row">';
 1163:         if ($action eq 'login') {
 1164:             $output .= '  
 1165:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1166:         } elsif ($action eq 'serverstatuses') {
 1167:             $output .= '
 1168:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1169:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1170: 
 1171:         } else {
 1172:             $output .= '
 1173:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1174:         }
 1175:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1176:             $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1177:                        &mt($item->{'header'}->[0]->{'col2'});
 1178:             if ($action eq 'serverstatuses') {
 1179:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1180:             } 
 1181:         } else {
 1182:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1183:                        &mt($item->{'header'}->[0]->{'col2'});
 1184:         }
 1185:         $output .= '</td>';
 1186:         if ($item->{'header'}->[0]->{'col3'}) {
 1187:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1188:                 $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1189:                             &mt($item->{'header'}->[0]->{'col3'});
 1190:             } else {
 1191:                 $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1192:                            &mt($item->{'header'}->[0]->{'col3'});
 1193:             }
 1194:             if ($action eq 'serverstatuses') {
 1195:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1196:             }
 1197:             $output .= '</td>';
 1198:         }
 1199:         if ($item->{'header'}->[0]->{'col4'}) {
 1200:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1201:                        &mt($item->{'header'}->[0]->{'col4'});
 1202:         }
 1203:         $output .= '</tr>';
 1204:         $rowtotal ++;
 1205:         if ($action eq 'quotas') {
 1206:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1207:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1208:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1209:                  ($action eq 'ltitools') || ($action eq 'lti') ||
 1210:                  ($action eq 'proctoring')) {
 1211:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1212:         }
 1213:     }
 1214:     $output .= '
 1215:    </table>
 1216:   </td>
 1217:  </tr>
 1218: </table><br />';
 1219:     return ($output,$rowtotal);
 1220: }
 1221: 
 1222: sub print_login {
 1223:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1224:     my ($css_class,$datatable);
 1225:     my %choices = &login_choices();
 1226: 
 1227:     if ($caller eq 'service') {
 1228:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1229:         my $choice = $choices{'disallowlogin'};
 1230:         $css_class = ' class="LC_odd_row"';
 1231:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1232:                       '<td style="text-align: right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1233:                       '<th>'.$choices{'server'}.'</th>'.
 1234:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1235:                       '<th>'.$choices{'custompath'}.'</th>'.
 1236:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1237:         my %disallowed;
 1238:         if (ref($settings) eq 'HASH') {
 1239:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1240:                %disallowed = %{$settings->{'loginvia'}};
 1241:             }
 1242:         }
 1243:         foreach my $lonhost (sort(keys(%servers))) {
 1244:             my $direct = 'selected="selected"';
 1245:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1246:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1247:                     $direct = '';
 1248:                 }
 1249:             }
 1250:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1251:                           '<td><select name="'.$lonhost.'_server">'.
 1252:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1253:                           '</option>';
 1254:             foreach my $hostid (sort(keys(%servers))) {
 1255:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1256:                 my $selected = '';
 1257:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1258:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1259:                         $selected = 'selected="selected"';
 1260:                     }
 1261:                 }
 1262:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1263:                               $servers{$hostid}.'</option>';
 1264:             }
 1265:             $datatable .= '</select></td>'.
 1266:                           '<td><select name="'.$lonhost.'_serverpath">';
 1267:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1268:                 my $pathname = $path;
 1269:                 if ($path eq 'custom') {
 1270:                     $pathname = &mt('Custom Path').' ->';
 1271:                 }
 1272:                 my $selected = '';
 1273:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1274:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1275:                         $selected = 'selected="selected"';
 1276:                     }
 1277:                 } elsif ($path eq '') {
 1278:                     $selected = 'selected="selected"';
 1279:                 }
 1280:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1281:             }
 1282:             $datatable .= '</select></td>';
 1283:             my ($custom,$exempt);
 1284:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1285:                 $custom = $disallowed{$lonhost}{'custompath'};
 1286:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1287:             }
 1288:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1289:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1290:                           '</tr>';
 1291:         }
 1292:         $datatable .= '</table></td></tr>';
 1293:         return $datatable;
 1294:     } elsif ($caller eq 'page') {
 1295:         my %defaultchecked = ( 
 1296:                                'coursecatalog' => 'on',
 1297:                                'helpdesk'      => 'on',
 1298:                                'adminmail'     => 'off',
 1299:                                'newuser'       => 'off',
 1300:                              );
 1301:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1302:         my (%checkedon,%checkedoff);
 1303:         foreach my $item (@toggles) {
 1304:             if ($defaultchecked{$item} eq 'on') { 
 1305:                 $checkedon{$item} = ' checked="checked" ';
 1306:                 $checkedoff{$item} = ' ';
 1307:             } elsif ($defaultchecked{$item} eq 'off') {
 1308:                 $checkedoff{$item} = ' checked="checked" ';
 1309:                 $checkedon{$item} = ' ';
 1310:             }
 1311:         }
 1312:         my @images = ('img','logo','domlogo','login');
 1313:         my @logintext = ('textcol','bgcol');
 1314:         my @bgs = ('pgbg','mainbg','sidebg');
 1315:         my @links = ('link','alink','vlink');
 1316:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1317:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1318:         my (%is_custom,%designs);
 1319:         my %defaults = (
 1320:                        font => $defaultdesign{'login.font'},
 1321:                        );
 1322:         foreach my $item (@images) {
 1323:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1324:             $defaults{'showlogo'}{$item} = 1;
 1325:         }
 1326:         foreach my $item (@bgs) {
 1327:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1328:         }
 1329:         foreach my $item (@logintext) {
 1330:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1331:         }
 1332:         foreach my $item (@links) {
 1333:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1334:         }
 1335:         if (ref($settings) eq 'HASH') {
 1336:             foreach my $item (@toggles) {
 1337:                 if ($settings->{$item} eq '1') {
 1338:                     $checkedon{$item} =  ' checked="checked" ';
 1339:                     $checkedoff{$item} = ' ';
 1340:                 } elsif ($settings->{$item} eq '0') {
 1341:                     $checkedoff{$item} =  ' checked="checked" ';
 1342:                     $checkedon{$item} = ' ';
 1343:                 }
 1344:             }
 1345:             foreach my $item (@images) {
 1346:                 if (defined($settings->{$item})) {
 1347:                     $designs{$item} = $settings->{$item};
 1348:                     $is_custom{$item} = 1;
 1349:                 }
 1350:                 if (defined($settings->{'showlogo'}{$item})) {
 1351:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1352:                 }
 1353:             }
 1354:             foreach my $item (@logintext) {
 1355:                 if ($settings->{$item} ne '') {
 1356:                     $designs{'logintext'}{$item} = $settings->{$item};
 1357:                     $is_custom{$item} = 1;
 1358:                 }
 1359:             }
 1360:             if ($settings->{'font'} ne '') {
 1361:                 $designs{'font'} = $settings->{'font'};
 1362:                 $is_custom{'font'} = 1;
 1363:             }
 1364:             foreach my $item (@bgs) {
 1365:                 if ($settings->{$item} ne '') {
 1366:                     $designs{'bgs'}{$item} = $settings->{$item};
 1367:                     $is_custom{$item} = 1;
 1368:                 }
 1369:             }
 1370:             foreach my $item (@links) {
 1371:                 if ($settings->{$item} ne '') {
 1372:                     $designs{'links'}{$item} = $settings->{$item};
 1373:                     $is_custom{$item} = 1;
 1374:                 }
 1375:             }
 1376:         } else {
 1377:             if ($designhash{$dom.'.login.font'} ne '') {
 1378:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1379:                 $is_custom{'font'} = 1;
 1380:             }
 1381:             foreach my $item (@images) {
 1382:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1383:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1384:                     $is_custom{$item} = 1;
 1385:                 }
 1386:             }
 1387:             foreach my $item (@bgs) {
 1388:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1389:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1390:                     $is_custom{$item} = 1;
 1391:                 }
 1392:             }
 1393:             foreach my $item (@links) {
 1394:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1395:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1396:                     $is_custom{$item} = 1;
 1397:                 }
 1398:             }
 1399:         }
 1400:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1401:                                                       logo => 'Institution Logo',
 1402:                                                       domlogo => 'Domain Logo',
 1403:                                                       login => 'Login box');
 1404:         my $itemcount = 1;
 1405:         foreach my $item (@toggles) {
 1406:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1407:             $datatable .=  
 1408:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1409:                 '</td><td>'.
 1410:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1411:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1412:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1413:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1414:                 '</tr>';
 1415:             $itemcount ++;
 1416:         }
 1417:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1418:         $datatable .= '</tr></table></td></tr>';
 1419:     } elsif ($caller eq 'help') {
 1420:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1421:         my $switchserver = &check_switchserver($dom,$confname);
 1422:         my $itemcount = 1;
 1423:         $defaulturl = '/adm/loginproblems.html';
 1424:         $defaulttype = 'default';
 1425:         %lt = &Apache::lonlocal::texthash (
 1426:                      del     => 'Delete?',
 1427:                      rep     => 'Replace:',
 1428:                      upl     => 'Upload:',
 1429:                      default => 'Default',
 1430:                      custom  => 'Custom',
 1431:                                              );
 1432:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1433:         my @currlangs;
 1434:         if (ref($settings) eq 'HASH') {
 1435:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1436:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1437:                     next if ($settings->{'helpurl'}{$key} eq '');
 1438:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1439:                     $type{$key} = 'custom';
 1440:                     unless ($key eq 'nolang') {
 1441:                         push(@currlangs,$key);
 1442:                     }
 1443:                 }
 1444:             } elsif ($settings->{'helpurl'} ne '') {
 1445:                 $type{'nolang'} = 'custom';
 1446:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1447:             }
 1448:         }
 1449:         foreach my $lang ('nolang',sort(@currlangs)) {
 1450:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1451:             $datatable .= '<tr'.$css_class.'>';
 1452:             if ($url{$lang} eq '') {
 1453:                 $url{$lang} = $defaulturl;
 1454:             }
 1455:             if ($type{$lang} eq '') {
 1456:                 $type{$lang} = $defaulttype;
 1457:             }
 1458:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1459:             if ($lang eq 'nolang') {
 1460:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1461:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1462:             } else {
 1463:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1464:                                   $langchoices{$lang},
 1465:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1466:             }
 1467:             $datatable .= '</span></td>'."\n".
 1468:                           '<td class="LC_left_item">';
 1469:             if ($type{$lang} eq 'custom') {
 1470:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1471:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1472:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1473:             } else {
 1474:                 $datatable .= $lt{'upl'};
 1475:             }
 1476:             $datatable .='<br />';
 1477:             if ($switchserver) {
 1478:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1479:             } else {
 1480:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1481:             }
 1482:             $datatable .= '</td></tr>';
 1483:             $itemcount ++;
 1484:         }
 1485:         my @addlangs;
 1486:         foreach my $lang (sort(keys(%langchoices))) {
 1487:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1488:             push(@addlangs,$lang);
 1489:         }
 1490:         if (@addlangs > 0) {
 1491:             my %toadd;
 1492:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1493:             $toadd{''} = &mt('Select');
 1494:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1495:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1496:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1497:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1498:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1499:             if ($switchserver) {
 1500:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1501:             } else {
 1502:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1503:             }
 1504:             $datatable .= '</td></tr>';
 1505:             $itemcount ++;
 1506:         }
 1507:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1508:     } elsif ($caller eq 'headtag') {
 1509:         my %domservers = &Apache::lonnet::get_servers($dom);
 1510:         my $choice = $choices{'headtag'};
 1511:         $css_class = ' class="LC_odd_row"';
 1512:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1513:                       '<td style="text-align: left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1514:                       '<th>'.$choices{'current'}.'</th>'.
 1515:                       '<th>'.$choices{'action'}.'</th>'.
 1516:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1517:         my (%currurls,%currexempt);
 1518:         if (ref($settings) eq 'HASH') {
 1519:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1520:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1521:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1522:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1523:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1524:                     }
 1525:                 }
 1526:             }
 1527:         }
 1528:         my %lt = &Apache::lonlocal::texthash(
 1529:                                                del  => 'Delete?',
 1530:                                                rep  => 'Replace:',
 1531:                                                upl  => 'Upload:',
 1532:                                                curr => 'View contents',
 1533:                                                none => 'None',
 1534:         );
 1535:         my $switchserver = &check_switchserver($dom,$confname);
 1536:         foreach my $lonhost (sort(keys(%domservers))) {
 1537:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1538:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1539:             if ($currurls{$lonhost}) {
 1540:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1541:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1542:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1543:                               '">'.$lt{'curr'}.'</a></td>'.
 1544:                               '<td><span class="LC_nobreak"><label>'.
 1545:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1546:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1547:             } else {
 1548:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1549:             }
 1550:             $datatable .='<br />';
 1551:             if ($switchserver) {
 1552:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1553:             } else {
 1554:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1555:             }
 1556:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1557:         }
 1558:         $datatable .= '</table></td></tr>';
 1559:     }
 1560:     return $datatable;
 1561: }
 1562: 
 1563: sub login_choices {
 1564:     my %choices =
 1565:         &Apache::lonlocal::texthash (
 1566:             coursecatalog => 'Display Course/Community Catalog link?',
 1567:             adminmail     => "Display Administrator's E-mail Address?",
 1568:             helpdesk      => 'Display "Contact Helpdesk" link',
 1569:             disallowlogin => "Login page requests redirected",
 1570:             hostid        => "Server",
 1571:             server        => "Redirect to:",
 1572:             serverpath    => "Path",
 1573:             custompath    => "Custom", 
 1574:             exempt        => "Exempt IP(s)",
 1575:             directlogin   => "No redirect",
 1576:             newuser       => "Link to create a user account",
 1577:             img           => "Header",
 1578:             logo          => "Main Logo",
 1579:             domlogo       => "Domain Logo",
 1580:             login         => "Log-in Header", 
 1581:             textcol       => "Text color",
 1582:             bgcol         => "Box color",
 1583:             bgs           => "Background colors",
 1584:             links         => "Link colors",
 1585:             font          => "Font color",
 1586:             pgbg          => "Header",
 1587:             mainbg        => "Page",
 1588:             sidebg        => "Login box",
 1589:             link          => "Link",
 1590:             alink         => "Active link",
 1591:             vlink         => "Visited link",
 1592:             headtag       => "Custom markup",
 1593:             action        => "Action",
 1594:             current       => "Current",
 1595:         );
 1596:     return %choices;
 1597: }
 1598: 
 1599: sub print_rolecolors {
 1600:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1601:     my %choices = &color_font_choices();
 1602:     my @bgs = ('pgbg','tabbg','sidebg');
 1603:     my @links = ('link','alink','vlink');
 1604:     my @images = ('img');
 1605:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1606:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1607:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1608:     my (%is_custom,%designs);
 1609:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1610:     if (ref($settings) eq 'HASH') {
 1611:         if (ref($settings->{$role}) eq 'HASH') {
 1612:             if ($settings->{$role}->{'img'} ne '') {
 1613:                 $designs{'img'} = $settings->{$role}->{'img'};
 1614:                 $is_custom{'img'} = 1;
 1615:             }
 1616:             if ($settings->{$role}->{'font'} ne '') {
 1617:                 $designs{'font'} = $settings->{$role}->{'font'};
 1618:                 $is_custom{'font'} = 1;
 1619:             }
 1620:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1621:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1622:                 $is_custom{'fontmenu'} = 1;
 1623:             }
 1624:             foreach my $item (@bgs) {
 1625:                 if ($settings->{$role}->{$item} ne '') {
 1626:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1627:                     $is_custom{$item} = 1;
 1628:                 }
 1629:             }
 1630:             foreach my $item (@links) {
 1631:                 if ($settings->{$role}->{$item} ne '') {
 1632:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1633:                     $is_custom{$item} = 1;
 1634:                 }
 1635:             }
 1636:         }
 1637:     } else {
 1638:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1639:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1640:             $is_custom{'img'} = 1;
 1641:         }
 1642:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1643:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1644:             $is_custom{'fontmenu'} = 1; 
 1645:         }
 1646:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1647:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1648:             $is_custom{'font'} = 1;
 1649:         }
 1650:         foreach my $item (@bgs) {
 1651:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1652:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1653:                 $is_custom{$item} = 1;
 1654:             
 1655:             }
 1656:         }
 1657:         foreach my $item (@links) {
 1658:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1659:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1660:                 $is_custom{$item} = 1;
 1661:             }
 1662:         }
 1663:     }
 1664:     my $itemcount = 1;
 1665:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1666:     $datatable .= '</tr></table></td></tr>';
 1667:     return $datatable;
 1668: }
 1669: 
 1670: sub role_defaults {
 1671:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1672:     my %defaults;
 1673:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1674:         return %defaults;
 1675:     }
 1676:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1677:     if ($role eq 'login') {
 1678:         %defaults = (
 1679:                        font => $defaultdesign{$role.'.font'},
 1680:                     );
 1681:         if (ref($logintext) eq 'ARRAY') {
 1682:             foreach my $item (@{$logintext}) {
 1683:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1684:             }
 1685:         }
 1686:         foreach my $item (@{$images}) {
 1687:             $defaults{'showlogo'}{$item} = 1;
 1688:         }
 1689:     } else {
 1690:         %defaults = (
 1691:                        img => $defaultdesign{$role.'.img'},
 1692:                        font => $defaultdesign{$role.'.font'},
 1693:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1694:                     );
 1695:     }
 1696:     foreach my $item (@{$bgs}) {
 1697:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1698:     }
 1699:     foreach my $item (@{$links}) {
 1700:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1701:     }
 1702:     foreach my $item (@{$images}) {
 1703:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1704:     }
 1705:     return %defaults;
 1706: }
 1707: 
 1708: sub display_color_options {
 1709:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1710:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1711:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1712:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1713:     my $datatable = '<tr'.$css_class.'>'.
 1714:         '<td>'.$choices->{'font'}.'</td>';
 1715:     if (!$is_custom->{'font'}) {
 1716:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1717:     } else {
 1718:         $datatable .= '<td>&nbsp;</td>';
 1719:     }
 1720:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1721: 
 1722:     $datatable .= '<td><span class="LC_nobreak">'.
 1723:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1724:                   ' value="'.$current_color.'" />&nbsp;'.
 1725:                   '&nbsp;</span></td></tr>';
 1726:     unless ($role eq 'login') { 
 1727:         $datatable .= '<tr'.$css_class.'>'.
 1728:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1729:         if (!$is_custom->{'fontmenu'}) {
 1730:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1731:         } else {
 1732:             $datatable .= '<td>&nbsp;</td>';
 1733:         }
 1734: 	$current_color = $designs->{'fontmenu'} ?
 1735: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1736:         $datatable .= '<td><span class="LC_nobreak">'.
 1737:                       '<input class="colorchooser" type="text" size="10" name="'
 1738: 		      .$role.'_fontmenu"'.
 1739:                       ' value="'.$current_color.'" />&nbsp;'.
 1740:                       '&nbsp;</span></td></tr>';
 1741:     }
 1742:     my $switchserver = &check_switchserver($dom,$confname);
 1743:     foreach my $img (@{$images}) {
 1744: 	$itemcount ++;
 1745:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1746:         $datatable .= '<tr'.$css_class.'>'.
 1747:                       '<td>'.$choices->{$img};
 1748:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1749:         if ($role eq 'login') {
 1750:             if ($img eq 'login') {
 1751:                 $login_hdr_pick =
 1752:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1753:                 $logincolors =
 1754:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1755:                                        $designs,$defaults);
 1756:             } elsif ($img ne 'domlogo') {
 1757:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1758:             }
 1759:         }
 1760:         $datatable .= '</td>';
 1761:         if ($designs->{$img} ne '') {
 1762:             $imgfile = $designs->{$img};
 1763: 	    $img_import = ($imgfile =~ m{^/adm/});
 1764:         } else {
 1765:             $imgfile = $defaults->{$img};
 1766:         }
 1767:         if ($imgfile) {
 1768:             my ($showfile,$fullsize);
 1769:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1770:                 my $urldir = $1;
 1771:                 my $filename = $2;
 1772:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1773:                 if (@info) {
 1774:                     my $thumbfile = 'tn-'.$filename;
 1775:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1776:                     if (@thumb) {
 1777:                         $showfile = $urldir.'/'.$thumbfile;
 1778:                     } else {
 1779:                         $showfile = $imgfile;
 1780:                     }
 1781:                 } else {
 1782:                     $showfile = '';
 1783:                 }
 1784:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1785:                 $showfile = $imgfile;
 1786:                 my $imgdir = $1;
 1787:                 my $filename = $2;
 1788:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1789:                     $showfile = "/$imgdir/tn-".$filename;
 1790:                 } else {
 1791:                     my $input = $londocroot.$imgfile;
 1792:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1793:                     if (!-e $output) {
 1794:                         my ($width,$height) = &thumb_dimensions();
 1795:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1796:                         if ($fullwidth ne '' && $fullheight ne '') {
 1797:                             if ($fullwidth > $width && $fullheight > $height) { 
 1798:                                 my $size = $width.'x'.$height;
 1799:                                 my @args = ('convert','-sample',$size,$input,$output);
 1800:                                 system({$args[0]} @args);
 1801:                                 $showfile = "/$imgdir/tn-".$filename;
 1802:                             }
 1803:                         }
 1804:                     }
 1805:                 }
 1806:             }
 1807:             if ($showfile) {
 1808:                 if ($showfile =~ m{^/(adm|res)/}) {
 1809:                     if ($showfile =~ m{^/res/}) {
 1810:                         my $local_showfile =
 1811:                             &Apache::lonnet::filelocation('',$showfile);
 1812:                         &Apache::lonnet::repcopy($local_showfile);
 1813:                     }
 1814:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1815:                 }
 1816:                 if ($imgfile) {
 1817:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1818:                         if ($imgfile =~ m{^/res/}) {
 1819:                             my $local_imgfile =
 1820:                                 &Apache::lonnet::filelocation('',$imgfile);
 1821:                             &Apache::lonnet::repcopy($local_imgfile);
 1822:                         }
 1823:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1824:                     } else {
 1825:                         $fullsize = $imgfile;
 1826:                     }
 1827:                 }
 1828:                 $datatable .= '<td>';
 1829:                 if ($img eq 'login') {
 1830:                     $datatable .= $login_hdr_pick;
 1831:                 } 
 1832:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1833:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1834:             } else {
 1835:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1836:                               &mt('Upload:').'<br />';
 1837:             }
 1838:         } else {
 1839:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1840:                           &mt('Upload:').'<br />';
 1841:         }
 1842:         if ($switchserver) {
 1843:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1844:         } else {
 1845:             if ($img ne 'login') { # suppress file selection for Log-in header
 1846:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1847:             }
 1848:         }
 1849:         $datatable .= '</td></tr>';
 1850:     }
 1851:     $itemcount ++;
 1852:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1853:     $datatable .= '<tr'.$css_class.'>'.
 1854:                   '<td>'.$choices->{'bgs'}.'</td>';
 1855:     my $bgs_def;
 1856:     foreach my $item (@{$bgs}) {
 1857:         if (!$is_custom->{$item}) {
 1858:             $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>';
 1859:         }
 1860:     }
 1861:     if ($bgs_def) {
 1862:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1863:     } else {
 1864:         $datatable .= '<td>&nbsp;</td>';
 1865:     }
 1866:     $datatable .= '<td class="LC_right_item">'.
 1867:                   '<table border="0"><tr>';
 1868: 
 1869:     foreach my $item (@{$bgs}) {
 1870:         $datatable .= '<td style="text-align: center">'.$choices->{$item};
 1871: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1872:         if ($designs->{'bgs'}{$item}) {
 1873:             $datatable .= '&nbsp;';
 1874:         }
 1875:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1876:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1877:     }
 1878:     $datatable .= '</tr></table></td></tr>';
 1879:     $itemcount ++;
 1880:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1881:     $datatable .= '<tr'.$css_class.'>'.
 1882:                   '<td>'.$choices->{'links'}.'</td>';
 1883:     my $links_def;
 1884:     foreach my $item (@{$links}) {
 1885:         if (!$is_custom->{$item}) {
 1886:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1887:         }
 1888:     }
 1889:     if ($links_def) {
 1890:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1891:     } else {
 1892:         $datatable .= '<td>&nbsp;</td>';
 1893:     }
 1894:     $datatable .= '<td class="LC_right_item">'.
 1895:                   '<table border="0"><tr>';
 1896:     foreach my $item (@{$links}) {
 1897: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1898:         $datatable .= '<td style="text-align: center">'.$choices->{$item}."\n";
 1899:         if ($designs->{'links'}{$item}) {
 1900:             $datatable.='&nbsp;';
 1901:         }
 1902:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1903:                       '" /></td>';
 1904:     }
 1905:     $$rowtotal += $itemcount;
 1906:     return $datatable;
 1907: }
 1908: 
 1909: sub logo_display_options {
 1910:     my ($img,$defaults,$designs) = @_;
 1911:     my $checkedon;
 1912:     if (ref($defaults) eq 'HASH') {
 1913:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1914:             if ($defaults->{'showlogo'}{$img}) {
 1915:                 $checkedon = 'checked="checked" ';     
 1916:             }
 1917:         } 
 1918:     }
 1919:     if (ref($designs) eq 'HASH') {
 1920:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1921:             if (defined($designs->{'showlogo'}{$img})) {
 1922:                 if ($designs->{'showlogo'}{$img} == 0) {
 1923:                     $checkedon = '';
 1924:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1925:                     $checkedon = 'checked="checked" ';
 1926:                 }
 1927:             }
 1928:         }
 1929:     }
 1930:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1931:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1932:            &mt('show').'</label>'."\n";
 1933: }
 1934: 
 1935: sub login_header_options  {
 1936:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1937:     my $output = '';
 1938:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1939:         $output .= &mt('Text default(s):').'<br />';
 1940:         if (!$is_custom->{'textcol'}) {
 1941:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1942:                        '&nbsp;&nbsp;&nbsp;';
 1943:         }
 1944:         if (!$is_custom->{'bgcol'}) {
 1945:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1946:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1947:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1948:         }
 1949:         $output .= '<br />';
 1950:     }
 1951:     $output .='<br />';
 1952:     return $output;
 1953: }
 1954: 
 1955: sub login_text_colors {
 1956:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1957:     my $color_menu = '<table border="0"><tr>';
 1958:     foreach my $item (@{$logintext}) {
 1959:         $color_menu .= '<td style="text-align: center">'.$choices->{$item};
 1960:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1961:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1962:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1963:     }
 1964:     $color_menu .= '</tr></table><br />';
 1965:     return $color_menu;
 1966: }
 1967: 
 1968: sub image_changes {
 1969:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1970:     my $output;
 1971:     if ($img eq 'login') {
 1972:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 1973:     } elsif (!$is_custom) {
 1974:         if ($img ne 'domlogo') {
 1975:             $output = &mt('Default image:').'<br />';
 1976:         } else {
 1977:             $output = &mt('Default in use:').'<br />';
 1978:         }
 1979:     }
 1980:     if ($img ne 'login') {
 1981:         if ($img_import) {
 1982:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1983:         }
 1984:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1985:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1986:         if ($is_custom) {
 1987:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1988:                        '<input type="checkbox" name="'.
 1989:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1990:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1991:         } else {
 1992:             $output .= '<td class="LC_middle">'.$logincolors.&mt('Upload:').'<br />';
 1993:         }
 1994:     }
 1995:     return $output;
 1996: }
 1997: 
 1998: sub print_quotas {
 1999:     my ($dom,$settings,$rowtotal,$action) = @_;
 2000:     my $context;
 2001:     if ($action eq 'quotas') {
 2002:         $context = 'tools';
 2003:     } else {
 2004:         $context = $action;
 2005:     }
 2006:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 2007:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2008:     my $typecount = 0;
 2009:     my ($css_class,%titles);
 2010:     if ($context eq 'requestcourses') {
 2011:         @usertools = ('official','unofficial','community','textbook','placement','lti');
 2012:         @options =('norequest','approval','validate','autolimit');
 2013:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 2014:         %titles = &courserequest_titles();
 2015:     } elsif ($context eq 'requestauthor') {
 2016:         @usertools = ('author');
 2017:         @options = ('norequest','approval','automatic');
 2018:         %titles = &authorrequest_titles();
 2019:     } else {
 2020:         @usertools = ('aboutme','blog','webdav','portfolio');
 2021:         %titles = &tool_titles();
 2022:     }
 2023:     if (ref($types) eq 'ARRAY') {
 2024:         foreach my $type (@{$types}) {
 2025:             my ($currdefquota,$currauthorquota);
 2026:             unless (($context eq 'requestcourses') ||
 2027:                     ($context eq 'requestauthor')) {
 2028:                 if (ref($settings) eq 'HASH') {
 2029:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 2030:                         $currdefquota = $settings->{defaultquota}->{$type};
 2031:                     } else {
 2032:                         $currdefquota = $settings->{$type};
 2033:                     }
 2034:                     if (ref($settings->{authorquota}) eq 'HASH') {
 2035:                         $currauthorquota = $settings->{authorquota}->{$type};
 2036:                     }
 2037:                 }
 2038:             }
 2039:             if (defined($usertypes->{$type})) {
 2040:                 $typecount ++;
 2041:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 2042:                 $datatable .= '<tr'.$css_class.'>'.
 2043:                               '<td>'.$usertypes->{$type}.'</td>'.
 2044:                               '<td class="LC_left_item">';
 2045:                 if ($context eq 'requestcourses') {
 2046:                     $datatable .= '<table><tr>';
 2047:                 }
 2048:                 my %cell;  
 2049:                 foreach my $item (@usertools) {
 2050:                     if ($context eq 'requestcourses') {
 2051:                         my ($curroption,$currlimit);
 2052:                         if (ref($settings) eq 'HASH') {
 2053:                             if (ref($settings->{$item}) eq 'HASH') {
 2054:                                 $curroption = $settings->{$item}->{$type};
 2055:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 2056:                                     $currlimit = $1; 
 2057:                                 }
 2058:                             }
 2059:                         }
 2060:                         if (!$curroption) {
 2061:                             $curroption = 'norequest';
 2062:                         }
 2063:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 2064:                         foreach my $option (@options) {
 2065:                             my $val = $option;
 2066:                             if ($option eq 'norequest') {
 2067:                                 $val = 0;  
 2068:                             }
 2069:                             if ($option eq 'validate') {
 2070:                                 my $canvalidate = 0;
 2071:                                 if (ref($validations{$item}) eq 'HASH') { 
 2072:                                     if ($validations{$item}{$type}) {
 2073:                                         $canvalidate = 1;
 2074:                                     }
 2075:                                 }
 2076:                                 next if (!$canvalidate);
 2077:                             }
 2078:                             my $checked = '';
 2079:                             if ($option eq $curroption) {
 2080:                                 $checked = ' checked="checked"';
 2081:                             } elsif ($option eq 'autolimit') {
 2082:                                 if ($curroption =~ /^autolimit/) {
 2083:                                     $checked = ' checked="checked"';
 2084:                                 }                       
 2085:                             } 
 2086:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 2087:                                   '<input type="radio" name="crsreq_'.$item.
 2088:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 2089:                                   $titles{$option}.'</label>';
 2090:                             if ($option eq 'autolimit') {
 2091:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2092:                                                 $item.'_limit_'.$type.'" size="1" '.
 2093:                                                 'value="'.$currlimit.'" />';
 2094:                             }
 2095:                             $cell{$item} .= '</span> ';
 2096:                             if ($option eq 'autolimit') {
 2097:                                 $cell{$item} .= $titles{'unlimited'};
 2098:                             }
 2099:                         }
 2100:                     } elsif ($context eq 'requestauthor') {
 2101:                         my $curroption;
 2102:                         if (ref($settings) eq 'HASH') {
 2103:                             $curroption = $settings->{$type};
 2104:                         }
 2105:                         if (!$curroption) {
 2106:                             $curroption = 'norequest';
 2107:                         }
 2108:                         foreach my $option (@options) {
 2109:                             my $val = $option;
 2110:                             if ($option eq 'norequest') {
 2111:                                 $val = 0;
 2112:                             }
 2113:                             my $checked = '';
 2114:                             if ($option eq $curroption) {
 2115:                                 $checked = ' checked="checked"';
 2116:                             }
 2117:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2118:                                   '<input type="radio" name="authorreq_'.$type.
 2119:                                   '" value="'.$val.'"'.$checked.' />'.
 2120:                                   $titles{$option}.'</label></span>&nbsp; ';
 2121:                         }
 2122:                     } else {
 2123:                         my $checked = 'checked="checked" ';
 2124:                         if (ref($settings) eq 'HASH') {
 2125:                             if (ref($settings->{$item}) eq 'HASH') {
 2126:                                 if ($settings->{$item}->{$type} == 0) {
 2127:                                     $checked = '';
 2128:                                 } elsif ($settings->{$item}->{$type} == 1) {
 2129:                                     $checked =  'checked="checked" ';
 2130:                                 }
 2131:                             }
 2132:                         }
 2133:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2134:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 2135:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 2136:                                       '</label></span>&nbsp; ';
 2137:                     }
 2138:                 }
 2139:                 if ($context eq 'requestcourses') {
 2140:                     $datatable .= '</tr><tr>';
 2141:                     foreach my $item (@usertools) {
 2142:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2143:                     }
 2144:                     $datatable .= '</tr></table>';
 2145:                 }
 2146:                 $datatable .= '</td>';
 2147:                 unless (($context eq 'requestcourses') ||
 2148:                         ($context eq 'requestauthor')) {
 2149:                     $datatable .= 
 2150:                               '<td class="LC_right_item">'.
 2151:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2152:                               '<input type="text" name="quota_'.$type.
 2153:                               '" value="'.$currdefquota.
 2154:                               '" size="5" /></span>'.('&nbsp;' x 2).
 2155:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2156:                               '<input type="text" name="authorquota_'.$type.
 2157:                               '" value="'.$currauthorquota.
 2158:                               '" size="5" /></span></td>';
 2159:                 }
 2160:                 $datatable .= '</tr>';
 2161:             }
 2162:         }
 2163:     }
 2164:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2165:         $defaultquota = '20';
 2166:         $authorquota = '500';
 2167:         if (ref($settings) eq 'HASH') {
 2168:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2169:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2170:             } elsif (defined($settings->{'default'})) {
 2171:                 $defaultquota = $settings->{'default'};
 2172:             }
 2173:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 2174:                 $authorquota = $settings->{'authorquota'}->{'default'};
 2175:             }
 2176:         }
 2177:     }
 2178:     $typecount ++;
 2179:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2180:     $datatable .= '<tr'.$css_class.'>'.
 2181:                   '<td>'.$othertitle.'</td>'.
 2182:                   '<td class="LC_left_item">';
 2183:     if ($context eq 'requestcourses') {
 2184:         $datatable .= '<table><tr>';
 2185:     }
 2186:     my %defcell;
 2187:     foreach my $item (@usertools) {
 2188:         if ($context eq 'requestcourses') {
 2189:             my ($curroption,$currlimit);
 2190:             if (ref($settings) eq 'HASH') {
 2191:                 if (ref($settings->{$item}) eq 'HASH') {
 2192:                     $curroption = $settings->{$item}->{'default'};
 2193:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2194:                         $currlimit = $1;
 2195:                     }
 2196:                 }
 2197:             }
 2198:             if (!$curroption) {
 2199:                 $curroption = 'norequest';
 2200:             }
 2201:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2202:             foreach my $option (@options) {
 2203:                 my $val = $option;
 2204:                 if ($option eq 'norequest') {
 2205:                     $val = 0;
 2206:                 }
 2207:                 if ($option eq 'validate') {
 2208:                     my $canvalidate = 0;
 2209:                     if (ref($validations{$item}) eq 'HASH') {
 2210:                         if ($validations{$item}{'default'}) {
 2211:                             $canvalidate = 1;
 2212:                         }
 2213:                     }
 2214:                     next if (!$canvalidate);
 2215:                 }
 2216:                 my $checked = '';
 2217:                 if ($option eq $curroption) {
 2218:                     $checked = ' checked="checked"';
 2219:                 } elsif ($option eq 'autolimit') {
 2220:                     if ($curroption =~ /^autolimit/) {
 2221:                         $checked = ' checked="checked"';
 2222:                     }
 2223:                 }
 2224:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2225:                                   '<input type="radio" name="crsreq_'.$item.
 2226:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2227:                                   $titles{$option}.'</label>';
 2228:                 if ($option eq 'autolimit') {
 2229:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2230:                                        $item.'_limit_default" size="1" '.
 2231:                                        'value="'.$currlimit.'" />';
 2232:                 }
 2233:                 $defcell{$item} .= '</span> ';
 2234:                 if ($option eq 'autolimit') {
 2235:                     $defcell{$item} .= $titles{'unlimited'};
 2236:                 }
 2237:             }
 2238:         } elsif ($context eq 'requestauthor') {
 2239:             my $curroption;
 2240:             if (ref($settings) eq 'HASH') {
 2241:                 $curroption = $settings->{'default'};
 2242:             }
 2243:             if (!$curroption) {
 2244:                 $curroption = 'norequest';
 2245:             }
 2246:             foreach my $option (@options) {
 2247:                 my $val = $option;
 2248:                 if ($option eq 'norequest') {
 2249:                     $val = 0;
 2250:                 }
 2251:                 my $checked = '';
 2252:                 if ($option eq $curroption) {
 2253:                     $checked = ' checked="checked"';
 2254:                 }
 2255:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2256:                               '<input type="radio" name="authorreq_default"'.
 2257:                               ' value="'.$val.'"'.$checked.' />'.
 2258:                               $titles{$option}.'</label></span>&nbsp; ';
 2259:             }
 2260:         } else {
 2261:             my $checked = 'checked="checked" ';
 2262:             if (ref($settings) eq 'HASH') {
 2263:                 if (ref($settings->{$item}) eq 'HASH') {
 2264:                     if ($settings->{$item}->{'default'} == 0) {
 2265:                         $checked = '';
 2266:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2267:                         $checked = 'checked="checked" ';
 2268:                     }
 2269:                 }
 2270:             }
 2271:             $datatable .= '<span class="LC_nobreak"><label>'.
 2272:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2273:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2274:                           '</label></span>&nbsp; ';
 2275:         }
 2276:     }
 2277:     if ($context eq 'requestcourses') {
 2278:         $datatable .= '</tr><tr>';
 2279:         foreach my $item (@usertools) {
 2280:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2281:         }
 2282:         $datatable .= '</tr></table>';
 2283:     }
 2284:     $datatable .= '</td>';
 2285:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2286:         $datatable .= '<td class="LC_right_item">'.
 2287:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2288:                       '<input type="text" name="defaultquota" value="'.
 2289:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2290:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2291:                       '<input type="text" name="authorquota" value="'.
 2292:                       $authorquota.'" size="5" /></span></td>';
 2293:     }
 2294:     $datatable .= '</tr>';
 2295:     $typecount ++;
 2296:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2297:     $datatable .= '<tr'.$css_class.'>'.
 2298:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2299:     if ($context eq 'requestcourses') {
 2300:         $datatable .= &mt('(overrides affiliation, if set)').
 2301:                       '</td>'.
 2302:                       '<td class="LC_left_item">'.
 2303:                       '<table><tr>';
 2304:     } else {
 2305:         $datatable .= &mt('(overrides affiliation, if checked)').
 2306:                       '</td>'.
 2307:                       '<td class="LC_left_item" colspan="2">'.
 2308:                       '<br />';
 2309:     }
 2310:     my %advcell;
 2311:     foreach my $item (@usertools) {
 2312:         if ($context eq 'requestcourses') {
 2313:             my ($curroption,$currlimit);
 2314:             if (ref($settings) eq 'HASH') {
 2315:                 if (ref($settings->{$item}) eq 'HASH') {
 2316:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2317:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2318:                         $currlimit = $1;
 2319:                     }
 2320:                 }
 2321:             }
 2322:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2323:             my $checked = '';
 2324:             if ($curroption eq '') {
 2325:                 $checked = ' checked="checked"';
 2326:             }
 2327:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2328:                                '<input type="radio" name="crsreq_'.$item.
 2329:                                '__LC_adv" value=""'.$checked.' />'.
 2330:                                &mt('No override set').'</label></span>&nbsp; ';
 2331:             foreach my $option (@options) {
 2332:                 my $val = $option;
 2333:                 if ($option eq 'norequest') {
 2334:                     $val = 0;
 2335:                 }
 2336:                 if ($option eq 'validate') {
 2337:                     my $canvalidate = 0;
 2338:                     if (ref($validations{$item}) eq 'HASH') {
 2339:                         if ($validations{$item}{'_LC_adv'}) {
 2340:                             $canvalidate = 1;
 2341:                         }
 2342:                     }
 2343:                     next if (!$canvalidate);
 2344:                 }
 2345:                 my $checked = '';
 2346:                 if ($val eq $curroption) {
 2347:                     $checked = ' checked="checked"';
 2348:                 } elsif ($option eq 'autolimit') {
 2349:                     if ($curroption =~ /^autolimit/) {
 2350:                         $checked = ' checked="checked"';
 2351:                     }
 2352:                 }
 2353:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2354:                                   '<input type="radio" name="crsreq_'.$item.
 2355:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2356:                                   $titles{$option}.'</label>';
 2357:                 if ($option eq 'autolimit') {
 2358:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2359:                                        $item.'_limit__LC_adv" size="1" '.
 2360:                                        'value="'.$currlimit.'" />';
 2361:                 }
 2362:                 $advcell{$item} .= '</span> ';
 2363:                 if ($option eq 'autolimit') {
 2364:                     $advcell{$item} .= $titles{'unlimited'};
 2365:                 }
 2366:             }
 2367:         } elsif ($context eq 'requestauthor') {
 2368:             my $curroption;
 2369:             if (ref($settings) eq 'HASH') {
 2370:                 $curroption = $settings->{'_LC_adv'};
 2371:             }
 2372:             my $checked = '';
 2373:             if ($curroption eq '') {
 2374:                 $checked = ' checked="checked"';
 2375:             }
 2376:             $datatable .= '<span class="LC_nobreak"><label>'.
 2377:                           '<input type="radio" name="authorreq__LC_adv"'.
 2378:                           ' value=""'.$checked.' />'.
 2379:                           &mt('No override set').'</label></span>&nbsp; ';
 2380:             foreach my $option (@options) {
 2381:                 my $val = $option;
 2382:                 if ($option eq 'norequest') {
 2383:                     $val = 0;
 2384:                 }
 2385:                 my $checked = '';
 2386:                 if ($val eq $curroption) {
 2387:                     $checked = ' checked="checked"';
 2388:                 }
 2389:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2390:                               '<input type="radio" name="authorreq__LC_adv"'.
 2391:                               ' value="'.$val.'"'.$checked.' />'.
 2392:                               $titles{$option}.'</label></span>&nbsp; ';
 2393:             }
 2394:         } else {
 2395:             my $checked = 'checked="checked" ';
 2396:             if (ref($settings) eq 'HASH') {
 2397:                 if (ref($settings->{$item}) eq 'HASH') {
 2398:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2399:                         $checked = '';
 2400:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2401:                         $checked = 'checked="checked" ';
 2402:                     }
 2403:                 }
 2404:             }
 2405:             $datatable .= '<span class="LC_nobreak"><label>'.
 2406:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2407:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2408:                           '</label></span>&nbsp; ';
 2409:         }
 2410:     }
 2411:     if ($context eq 'requestcourses') {
 2412:         $datatable .= '</tr><tr>';
 2413:         foreach my $item (@usertools) {
 2414:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2415:         }
 2416:         $datatable .= '</tr></table>';
 2417:     }
 2418:     $datatable .= '</td></tr>';
 2419:     $$rowtotal += $typecount;
 2420:     return $datatable;
 2421: }
 2422: 
 2423: sub print_requestmail {
 2424:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2425:     my ($now,$datatable,%currapp);
 2426:     $now = time;
 2427:     if (ref($settings) eq 'HASH') {
 2428:         if (ref($settings->{'notify'}) eq 'HASH') {
 2429:             if ($settings->{'notify'}{'approval'} ne '') {
 2430:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2431:             }
 2432:         }
 2433:     }
 2434:     my $numinrow = 2;
 2435:     my $css_class;
 2436:     if ($$rowtotal%2) {
 2437:         $css_class = 'LC_odd_row';
 2438:     }
 2439:     if ($customcss) {
 2440:         $css_class .= " $customcss";
 2441:     }
 2442:     $css_class =~ s/^\s+//;
 2443:     if ($css_class) {
 2444:         $css_class = ' class="'.$css_class.'"';
 2445:     }
 2446:     if ($rowstyle) {
 2447:         $css_class .= ' style="'.$rowstyle.'"';
 2448:     }
 2449:     my $text;
 2450:     if ($action eq 'requestcourses') {
 2451:         $text = &mt('Receive notification of course requests requiring approval');
 2452:     } elsif ($action eq 'requestauthor') {
 2453:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2454:     } else {
 2455:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2456:     }
 2457:     $datatable = '<tr'.$css_class.'>'.
 2458:                  ' <td>'.$text.'</td>'.
 2459:                  ' <td class="LC_left_item">';
 2460:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2461:                                                  $action.'notifyapproval',%currapp);
 2462:     if ($numdc > 0) {
 2463:         $datatable .= $table;
 2464:     } else {
 2465:         $datatable .= &mt('There are no active Domain Coordinators');
 2466:     }
 2467:     $datatable .='</td></tr>';
 2468:     return $datatable;
 2469: }
 2470: 
 2471: sub print_studentcode {
 2472:     my ($settings,$rowtotal) = @_;
 2473:     my $rownum = 0; 
 2474:     my ($output,%current);
 2475:     my @crstypes = ('official','unofficial','community','textbook','placement','lti');
 2476:     if (ref($settings) eq 'HASH') {
 2477:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2478:             foreach my $type (@crstypes) {
 2479:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2480:             }
 2481:         }
 2482:     }
 2483:     $output .= '<tr>'.
 2484:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2485:                '<td class="LC_left_item">';
 2486:     foreach my $type (@crstypes) {
 2487:         my $check = ' ';
 2488:         if ($current{$type}) {
 2489:             $check = ' checked="checked" ';
 2490:         }
 2491:         $output .= '<span class="LC_nobreak"><label>'.
 2492:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2493:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2494:     }
 2495:     $output .= '</td></tr>';
 2496:     $$rowtotal ++;
 2497:     return $output;
 2498: }
 2499: 
 2500: sub print_textbookcourses {
 2501:     my ($dom,$type,$settings,$rowtotal) = @_;
 2502:     my $rownum = 0;
 2503:     my $css_class;
 2504:     my $itemcount = 1;
 2505:     my $maxnum = 0;
 2506:     my $bookshash;
 2507:     if (ref($settings) eq 'HASH') {
 2508:         $bookshash = $settings->{$type};
 2509:     }
 2510:     my %ordered;
 2511:     if (ref($bookshash) eq 'HASH') {
 2512:         foreach my $item (keys(%{$bookshash})) {
 2513:             if (ref($bookshash->{$item}) eq 'HASH') {
 2514:                 my $num = $bookshash->{$item}{'order'};
 2515:                 $ordered{$num} = $item;
 2516:             }
 2517:         }
 2518:     }
 2519:     my $confname = $dom.'-domainconfig';
 2520:     my $switchserver = &check_switchserver($dom,$confname);
 2521:     my $maxnum = scalar(keys(%ordered));
 2522:     my $datatable;
 2523:     if (keys(%ordered)) {
 2524:         my @items = sort { $a <=> $b } keys(%ordered);
 2525:         for (my $i=0; $i<@items; $i++) {
 2526:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2527:             my $key = $ordered{$items[$i]};
 2528:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2529:             my $coursetitle = $coursehash{'description'};
 2530:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2531:             if (ref($bookshash->{$key}) eq 'HASH') {
 2532:                 $subject = $bookshash->{$key}->{'subject'};
 2533:                 $title = $bookshash->{$key}->{'title'};
 2534:                 if ($type eq 'textbooks') {
 2535:                     $publisher = $bookshash->{$key}->{'publisher'};
 2536:                     $author = $bookshash->{$key}->{'author'};
 2537:                     $image = $bookshash->{$key}->{'image'};
 2538:                     if ($image ne '') {
 2539:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2540:                         my $imagethumb = "$path/tn-".$imagefile;
 2541:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2542:                     }
 2543:                 }
 2544:             }
 2545:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2546:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2547:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2548:             for (my $k=0; $k<=$maxnum; $k++) {
 2549:                 my $vpos = $k+1;
 2550:                 my $selstr;
 2551:                 if ($k == $i) {
 2552:                     $selstr = ' selected="selected" ';
 2553:                 }
 2554:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2555:             }
 2556:             $datatable .= '</select>'.('&nbsp;'x2).
 2557:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2558:                 &mt('Delete?').'</label></span></td>'.
 2559:                 '<td colspan="2">'.
 2560:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2561:                 ('&nbsp;'x2).
 2562:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2563:             if ($type eq 'textbooks') {
 2564:                 $datatable .= ('&nbsp;'x2).
 2565:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2566:                               ('&nbsp;'x2).
 2567:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2568:                               ('&nbsp;'x2).
 2569:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2570:                 if ($image) {
 2571:                     $datatable .= $imgsrc.
 2572:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2573:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2574:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2575:                 }
 2576:                 if ($switchserver) {
 2577:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2578:                 } else {
 2579:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2580:                 }
 2581:             }
 2582:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2583:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2584:                           $coursetitle.'</span></td></tr>'."\n";
 2585:             $itemcount ++;
 2586:         }
 2587:     }
 2588:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2589:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2590:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2591:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2592:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2593:     for (my $k=0; $k<$maxnum+1; $k++) {
 2594:         my $vpos = $k+1;
 2595:         my $selstr;
 2596:         if ($k == $maxnum) {
 2597:             $selstr = ' selected="selected" ';
 2598:         }
 2599:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2600:     }
 2601:     $datatable .= '</select>&nbsp;'."\n".
 2602:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 2603:                   '<td colspan="2">'.
 2604:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2605:                   ('&nbsp;'x2).
 2606:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2607:                   ('&nbsp;'x2);
 2608:     if ($type eq 'textbooks') {
 2609:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2610:                       ('&nbsp;'x2).
 2611:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2612:                       ('&nbsp;'x2).
 2613:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2614:         if ($switchserver) {
 2615:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2616:         } else {
 2617:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2618:         }
 2619:         $datatable .= '</span>'."\n";
 2620:     }
 2621:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2622:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2623:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2624:                   &Apache::loncommon::selectcourse_link
 2625:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 2626:                   '</span></td>'."\n".
 2627:                   '</tr>'."\n";
 2628:     $itemcount ++;
 2629:     return $datatable;
 2630: }
 2631: 
 2632: sub textbookcourses_javascript {
 2633:     my ($settings) = @_;
 2634:     return unless(ref($settings) eq 'HASH');
 2635:     my (%ordered,%total,%jstext);
 2636:     foreach my $type ('textbooks','templates') {
 2637:         $total{$type} = 0;
 2638:         if (ref($settings->{$type}) eq 'HASH') {
 2639:             foreach my $item (keys(%{$settings->{$type}})) {
 2640:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2641:                     my $num = $settings->{$type}->{$item}{'order'};
 2642:                     $ordered{$type}{$num} = $item;
 2643:                 }
 2644:             }
 2645:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2646:         }
 2647:         my @jsarray = ();
 2648:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2649:             push(@jsarray,$ordered{$type}{$item});
 2650:         }
 2651:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2652:     }
 2653:     return <<"ENDSCRIPT";
 2654: <script type="text/javascript">
 2655: // <![CDATA[
 2656: function reorderBooks(form,item,caller) {
 2657:     var changedVal;
 2658: $jstext{'textbooks'};
 2659: $jstext{'templates'};
 2660:     var newpos;
 2661:     var maxh;
 2662:     if (caller == 'textbooks') {  
 2663:         newpos = 'textbooks_addbook_pos';
 2664:         maxh = 1 + $total{'textbooks'};
 2665:     } else {
 2666:         newpos = 'templates_addbook_pos';
 2667:         maxh = 1 + $total{'templates'};
 2668:     }
 2669:     var current = new Array;
 2670:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2671:     if (item == newpos) {
 2672:         changedVal = newitemVal;
 2673:     } else {
 2674:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2675:         current[newitemVal] = newpos;
 2676:     }
 2677:     if (caller == 'textbooks') {
 2678:         for (var i=0; i<textbooks.length; i++) {
 2679:             var elementName = 'textbooks_'+textbooks[i];
 2680:             if (elementName != item) {
 2681:                 if (form.elements[elementName]) {
 2682:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2683:                     current[currVal] = elementName;
 2684:                 }
 2685:             }
 2686:         }
 2687:     }
 2688:     if (caller == 'templates') {
 2689:         for (var i=0; i<templates.length; i++) {
 2690:             var elementName = 'templates_'+templates[i];
 2691:             if (elementName != item) {
 2692:                 if (form.elements[elementName]) {
 2693:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2694:                     current[currVal] = elementName;
 2695:                 }
 2696:             }
 2697:         }
 2698:     }
 2699:     var oldVal;
 2700:     for (var j=0; j<maxh; j++) {
 2701:         if (current[j] == undefined) {
 2702:             oldVal = j;
 2703:         }
 2704:     }
 2705:     if (oldVal < changedVal) {
 2706:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2707:            var elementName = current[k];
 2708:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2709:         }
 2710:     } else {
 2711:         for (var k=changedVal; k<oldVal; k++) {
 2712:             var elementName = current[k];
 2713:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2714:         }
 2715:     }
 2716:     return;
 2717: }
 2718: 
 2719: // ]]>
 2720: </script>
 2721: 
 2722: ENDSCRIPT
 2723: }
 2724: 
 2725: sub ltitools_javascript {
 2726:     my ($settings) = @_;
 2727:     my $togglejs = &ltitools_toggle_js();
 2728:     unless (ref($settings) eq 'HASH') {
 2729:         return $togglejs;
 2730:     }
 2731:     my (%ordered,$total,%jstext);
 2732:     $total = 0;
 2733:     foreach my $item (keys(%{$settings})) {
 2734:         if (ref($settings->{$item}) eq 'HASH') {
 2735:             my $num = $settings->{$item}{'order'};
 2736:             $ordered{$num} = $item;
 2737:         }
 2738:     }
 2739:     $total = scalar(keys(%{$settings}));
 2740:     my @jsarray = ();
 2741:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2742:         push(@jsarray,$ordered{$item});
 2743:     }
 2744:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2745:     return <<"ENDSCRIPT";
 2746: <script type="text/javascript">
 2747: // <![CDATA[
 2748: function reorderLTITools(form,item) {
 2749:     var changedVal;
 2750: $jstext
 2751:     var newpos = 'ltitools_add_pos';
 2752:     var maxh = 1 + $total;
 2753:     var current = new Array;
 2754:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2755:     if (item == newpos) {
 2756:         changedVal = newitemVal;
 2757:     } else {
 2758:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2759:         current[newitemVal] = newpos;
 2760:     }
 2761:     for (var i=0; i<ltitools.length; i++) {
 2762:         var elementName = 'ltitools_'+ltitools[i];
 2763:         if (elementName != item) {
 2764:             if (form.elements[elementName]) {
 2765:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2766:                 current[currVal] = elementName;
 2767:             }
 2768:         }
 2769:     }
 2770:     var oldVal;
 2771:     for (var j=0; j<maxh; j++) {
 2772:         if (current[j] == undefined) {
 2773:             oldVal = j;
 2774:         }
 2775:     }
 2776:     if (oldVal < changedVal) {
 2777:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2778:            var elementName = current[k];
 2779:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2780:         }
 2781:     } else {
 2782:         for (var k=changedVal; k<oldVal; k++) {
 2783:             var elementName = current[k];
 2784:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2785:         }
 2786:     }
 2787:     return;
 2788: }
 2789: 
 2790: // ]]>
 2791: </script>
 2792: 
 2793: $togglejs
 2794: 
 2795: ENDSCRIPT
 2796: }
 2797: 
 2798: sub ltitools_toggle_js {
 2799:     return <<"ENDSCRIPT";
 2800: <script type="text/javascript">
 2801: // <![CDATA[
 2802: 
 2803: function toggleLTITools(form,setting,item) {
 2804:     var radioname = '';
 2805:     var divid = '';
 2806:     if ((setting == 'passback') || (setting == 'roster')) {
 2807:         radioname = 'ltitools_'+setting+'_'+item;
 2808:         divid = 'ltitools_'+setting+'time_'+item;
 2809:         var num = form.elements[radioname].length;
 2810:         if (num) {
 2811:             var setvis = '';
 2812:             for (var i=0; i<num; i++) {
 2813:                 if (form.elements[radioname][i].checked) {
 2814:                     if (form.elements[radioname][i].value == '1') {
 2815:                         if (document.getElementById(divid)) {
 2816:                             document.getElementById(divid).style.display = 'inline-block';
 2817:                         }
 2818:                         setvis = 1;
 2819:                     }
 2820:                     break;
 2821:                 }
 2822:             }
 2823:         }
 2824:         if (!setvis) {
 2825:             if (document.getElementById(divid)) {
 2826:                 document.getElementById(divid).style.display = 'none';
 2827:             }
 2828:         }
 2829:     }
 2830:     if (setting == 'user') {
 2831:         divid = 'ltitools_'+setting+'_div_'+item;
 2832:         var checkid = 'ltitools_'+setting+'_field_'+item;
 2833:         if (document.getElementById(divid)) {
 2834:             if (document.getElementById(checkid)) {
 2835:                 if (document.getElementById(checkid).checked) {
 2836:                     document.getElementById(divid).style.display = 'inline-block';
 2837:                 } else {
 2838:                     document.getElementById(divid).style.display = 'none';
 2839:                 }
 2840:             }
 2841:         }
 2842:     }
 2843:     return;
 2844: }
 2845: // ]]>
 2846: </script>
 2847: 
 2848: ENDSCRIPT
 2849: }
 2850: 
 2851: sub wafproxy_javascript {
 2852:     my ($dom) = @_;
 2853:     return <<"ENDSCRIPT";
 2854: <script type="text/javascript">
 2855: // <![CDATA[
 2856: function updateWAF() {
 2857:     if (document.getElementById('wafproxy_remoteip')) {
 2858:         var wafremote = 0;
 2859:         if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value == 'h') {
 2860:             wafremote = 1;
 2861:         }
 2862:         var fields = new Array('header','trust');
 2863:         for (var i=0; i<fields.length; i++) {
 2864:             if (document.getElementById('wafproxy_'+fields[i])) {
 2865:                 if (wafremote == 1) {
 2866:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'table-row';
 2867:                 }
 2868:                 else {
 2869:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'none';
 2870:                 }
 2871:             }
 2872:         }
 2873:         if (document.getElementById('wafproxyranges_$dom')) {
 2874:             if (wafremote == 1) {
 2875:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 2876:             } else {
 2877:                 for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 2878:                     if (document.display.wafproxy_vpnaccess[i].checked) {
 2879:                         if (document.display.wafproxy_vpnaccess[i].value == 0) {
 2880:                             document.getElementById('wafproxyranges_$dom').style.display = 'none';
 2881:                         }
 2882:                     }
 2883:                 }
 2884:             }
 2885:         }
 2886:     }
 2887:     return;
 2888: }
 2889: 
 2890: function checkWAF() {
 2891:     if (document.getElementById('wafproxy_remoteip')) {
 2892:         var wafvpn = 0;
 2893:         for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 2894:             if (document.display.wafproxy_vpnaccess[i].checked) {
 2895:                 if (document.display.wafproxy_vpnaccess[i].value == 1) {
 2896:                     wafvpn = 1;
 2897:                 }
 2898:                 break;
 2899:             }
 2900:         }
 2901:         var vpn = new Array('vpnint','vpnext');
 2902:         for (var i=0; i<vpn.length; i++) {
 2903:             if (document.getElementById('wafproxy_show_'+vpn[i])) {
 2904:                 if (wafvpn == 1) {
 2905:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'table-row';
 2906:                 }
 2907:                 else {
 2908:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'none';
 2909:                 }
 2910:             }
 2911:         }
 2912:         if (document.getElementById('wafproxyranges_$dom')) {
 2913:             if (wafvpn == 1) {
 2914:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 2915:             }
 2916:             else if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value != 'h') {
 2917:                 document.getElementById('wafproxyranges_$dom').style.display = 'none';
 2918:             }
 2919:         }
 2920:     }
 2921:     return;
 2922: }
 2923: 
 2924: function toggleWAF() {
 2925:     if (document.getElementById('wafproxy_table')) {
 2926:         var wafproxy = 0;
 2927:         for (var i=0; i<document.display.wafproxy_${dom}.length; i++) {
 2928:              if (document.display.wafproxy_${dom}[i].checked) {
 2929:                  if (document.display.wafproxy_${dom}[i].value == 1) {
 2930:                      wafproxy = 1;
 2931:                      break;
 2932:                 }
 2933:             }
 2934:         }
 2935:         if (wafproxy == 1) {
 2936:             document.getElementById('wafproxy_table').style.display='inline';
 2937:         }
 2938:         else {
 2939:            document.getElementById('wafproxy_table').style.display='none';
 2940:         }
 2941:         if (document.getElementById('wafproxyrow_${dom}')) {
 2942:             if (wafproxy == 1) {
 2943:                 document.getElementById('wafproxyrow_${dom}').style.display = 'table-row';
 2944:             }
 2945:             else {
 2946:                 document.getElementById('wafproxyrow_${dom}').style.display = 'none';
 2947:             }
 2948:         }
 2949:         if (document.getElementById('nowafproxyrow_$dom')) {
 2950:             if (wafproxy == 1) {
 2951:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'none';
 2952:             }
 2953:             else {
 2954:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'table-row';
 2955:             }
 2956:         }
 2957:     }
 2958:     return;
 2959: }
 2960: // ]]>
 2961: </script>
 2962: 
 2963: ENDSCRIPT
 2964: }
 2965: 
 2966: sub proctoring_javascript {
 2967:     my ($settings) = @_;
 2968:     my (%ordered,$total,%jstext);
 2969:     $total = 0;
 2970:     if (ref($settings) eq 'HASH') {
 2971:         foreach my $item (keys(%{$settings})) {
 2972:             if (ref($settings->{$item}) eq 'HASH') {
 2973:                 my $num = $settings->{$item}{'order'};
 2974:                 $ordered{$num} = $item;
 2975:             }
 2976:         }
 2977:         $total = scalar(keys(%{$settings}));
 2978:     } else {
 2979:         %ordered = (
 2980:                        0 => 'proctorio',
 2981:                        1 => 'examity',
 2982:                    );
 2983:         $total = 2; 
 2984:     }
 2985:     my @jsarray = ();
 2986:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2987:         push(@jsarray,$ordered{$item});
 2988:     }
 2989:     my $jstext = '    var proctors = Array('."'".join("','",@jsarray)."'".');'."\n";
 2990:     return <<"ENDSCRIPT";
 2991: <script type="text/javascript">
 2992: // <![CDATA[
 2993: function reorderProctoring(form,item) {
 2994:     var changedVal;
 2995: $jstext
 2996:     var maxh = $total;
 2997:     var current = new Array;
 2998:     var changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2999:     for (var i=0; i<proctors.length; i++) {
 3000:         var elementName = 'proctoring_pos_'+proctors[i];
 3001:         if (elementName != item) {
 3002:             if (form.elements[elementName]) {
 3003:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3004:                 current[currVal] = elementName;
 3005:             }
 3006:         }
 3007:     }
 3008:     var oldVal;
 3009:     for (var j=0; j<maxh; j++) {
 3010:         if (current[j] == undefined) {
 3011:             oldVal = j;
 3012:         }
 3013:     }
 3014:     if (oldVal < changedVal) {
 3015:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3016:            var elementName = current[k];
 3017:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3018:         }
 3019:     } else {
 3020:         for (var k=changedVal; k<oldVal; k++) {
 3021:             var elementName = current[k];
 3022:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3023:         }
 3024:     }
 3025:     return;
 3026: }
 3027: 
 3028: function toggleProctoring(form,item) {
 3029:     var fieldsets = document.getElementsByClassName('proctoring_'+item);
 3030:     if (fieldsets.length) {
 3031:         var radioname = 'proctoring_available_'+item;
 3032:         var num = form.elements[radioname].length;
 3033:         if (num) {
 3034:             var setvis = '';
 3035:             for (var i=0; i<num; i++) {
 3036:                 if (form.elements[radioname][i].checked) {
 3037:                     if (form.elements[radioname][i].value == '1') {
 3038:                        setvis = 1;
 3039:                        break;
 3040:                     }
 3041:                 }
 3042:             }
 3043:             for (var j=0; j<fieldsets.length; j++) {
 3044:                 if (setvis) {
 3045:                     fieldsets[j].style.display = 'block';
 3046:                 } else {
 3047:                     fieldsets[j].style.display = 'none';
 3048:                 }
 3049:             }
 3050:         }
 3051:     }
 3052:     return;
 3053: }
 3054: 
 3055: // ]]>
 3056: </script>
 3057: 
 3058: ENDSCRIPT
 3059: }
 3060: 
 3061: 
 3062: sub lti_javascript {
 3063:     my ($settings) = @_;
 3064:     my $togglejs = &lti_toggle_js();
 3065:     unless (ref($settings) eq 'HASH') {
 3066:         return $togglejs;
 3067:     }
 3068:     my (%ordered,$total,%jstext);
 3069:     $total = 0;
 3070:     foreach my $item (keys(%{$settings})) {
 3071:         if (ref($settings->{$item}) eq 'HASH') {
 3072:             my $num = $settings->{$item}{'order'};
 3073:             $ordered{$num} = $item;
 3074:         }
 3075:     }
 3076:     $total = scalar(keys(%{$settings}));
 3077:     my @jsarray = ();
 3078:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3079:         push(@jsarray,$ordered{$item});
 3080:     }
 3081:     my $jstext = '    var lti = Array('."'".join("','",@jsarray)."'".');'."\n";
 3082:     return <<"ENDSCRIPT";
 3083: <script type="text/javascript">
 3084: // <![CDATA[
 3085: function reorderLTI(form,item) {
 3086:     var changedVal;
 3087: $jstext
 3088:     var newpos = 'lti_pos_add';
 3089:     var maxh = 1 + $total;
 3090:     var current = new Array;
 3091:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3092:     if (item == newpos) {
 3093:         changedVal = newitemVal;
 3094:     } else {
 3095:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3096:         current[newitemVal] = newpos;
 3097:     }
 3098:     for (var i=0; i<lti.length; i++) {
 3099:         var elementName = 'lti_pos_'+lti[i];
 3100:         if (elementName != item) {
 3101:             if (form.elements[elementName]) {
 3102:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3103:                 current[currVal] = elementName;
 3104:             }
 3105:         }
 3106:     }
 3107:     var oldVal;
 3108:     for (var j=0; j<maxh; j++) {
 3109:         if (current[j] == undefined) {
 3110:             oldVal = j;
 3111:         }
 3112:     }
 3113:     if (oldVal < changedVal) {
 3114:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3115:            var elementName = current[k];
 3116:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3117:         }
 3118:     } else {
 3119:         for (var k=changedVal; k<oldVal; k++) {
 3120:             var elementName = current[k];
 3121:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3122:         }
 3123:     }
 3124:     return;
 3125: }
 3126: // ]]>
 3127: </script>
 3128: 
 3129: $togglejs
 3130: 
 3131: ENDSCRIPT
 3132: }
 3133: 
 3134: sub lti_toggle_js {
 3135:     my %lcauthparmtext = &Apache::lonlocal::texthash (
 3136:                             localauth => 'Local auth argument',
 3137:                             krb       => 'Kerberos domain',
 3138:                          );
 3139:     return <<"ENDSCRIPT";
 3140: <script type="text/javascript">
 3141: // <![CDATA[
 3142: 
 3143: function toggleLTI(form,setting,item) {
 3144:     if (setting == 'requser') {
 3145:         var fieldsets = document.getElementsByClassName('ltioption_'+item);
 3146:         if (fieldsets.length) {
 3147:             var radioname = 'lti_'+setting+'_'+item;
 3148:             var num = form.elements[radioname].length;
 3149:             if (num) {
 3150:                 var setvis = '';
 3151:                 for (var i=0; i<num; i++) {
 3152:                     if (form.elements[radioname][i].checked) {
 3153:                         if (form.elements[radioname][i].value == '1') {
 3154:                            setvis = 1;
 3155:                            break;
 3156:                         }
 3157:                     }
 3158:                 }
 3159:                 for (var j=0; j<fieldsets.length; j++) {
 3160:                     if (setvis) {
 3161:                         fieldsets[j].style.display = 'block';
 3162:                     } else {
 3163:                         fieldsets[j].style.display = 'none';
 3164:                     }
 3165:                 }
 3166:             }
 3167:         }
 3168:     } else if ((setting == 'user') || (setting == 'crs') || (setting == 'passback') || (setting == 'callback')) {
 3169:         var radioname = '';
 3170:         var divid = '';
 3171:         if (setting == 'user') {
 3172:             radioname = 'lti_mapuser_'+item;
 3173:             divid = 'lti_userfield_'+item;
 3174:         } else if (setting == 'crs') {
 3175:             radioname = 'lti_mapcrs_'+item;
 3176:             divid = 'lti_crsfield_'+item;
 3177:         } else if (setting == 'callback') {
 3178:             radioname = 'lti_callback_'+item;
 3179:             divid = 'lti_callbackfield_'+item;
 3180:         } else {
 3181:             radioname = 'lti_passback_'+item;
 3182:             divid =  'lti_passback_'+item;
 3183:         }
 3184:         var num = form.elements[radioname].length;
 3185:         if (num) {
 3186:             var setvis = '';
 3187:             for (var i=0; i<num; i++) {
 3188:                if (form.elements[radioname][i].checked) {
 3189:                    if ((setting == 'passback') || (setting == 'callback')) {
 3190:                        if (form.elements[radioname][i].value == '1') {
 3191:                            if (document.getElementById(divid)) {
 3192:                                document.getElementById(divid).style.display = 'inline-block';
 3193:                            }
 3194:                            setvis = 1;
 3195:                            break;
 3196:                        }
 3197:                    } else {
 3198:                        if (form.elements[radioname][i].value == 'other') {
 3199:                            if (document.getElementById(divid)) {
 3200:                                document.getElementById(divid).style.display = 'inline-block';
 3201:                            }
 3202:                            setvis = 1;
 3203:                            break;
 3204:                        }
 3205:                    }
 3206:                } 
 3207:             }
 3208:             if (!setvis) {
 3209:                 if (document.getElementById(divid)) {
 3210:                     document.getElementById(divid).style.display = 'none';
 3211:                 }
 3212:             }
 3213:         }
 3214:     } else if ((setting == 'sec') || (setting == 'secsrc')) {
 3215:         var numsec = form.elements['lti_crssec_'+item].length;
 3216:         if (numsec) {
 3217:             var setvis = '';
 3218:             for (var i=0; i<numsec; i++) {
 3219:                 if (form.elements['lti_crssec_'+item][i].checked) {
 3220:                     if (form.elements['lti_crssec_'+item][i].value == '1') {
 3221:                         if (document.getElementById('lti_crssecfield_'+item)) {
 3222:                             document.getElementById('lti_crssecfield_'+item).style.display = 'inline-block';
 3223:                             setvis = 1;
 3224:                             var numsrcsec = form.elements['lti_crssecsrc_'+item].length;
 3225:                             if (numsrcsec) {
 3226:                                 var setsrcvis = '';
 3227:                                 for (var j=0; j<numsrcsec; j++) {
 3228:                                     if (form.elements['lti_crssecsrc_'+item][j].checked) {
 3229:                                         if (form.elements['lti_crssecsrc_'+item][j].value == 'other') {
 3230:                                             if (document.getElementById('lti_secsrcfield_'+item)) {
 3231:                                                 document.getElementById('lti_secsrcfield_'+item).style.display = 'inline-block';
 3232:                                                 setsrcvis = 1;
 3233:                                             }
 3234:                                         }
 3235:                                     }
 3236:                                 }
 3237:                                 if (!setsrcvis) {
 3238:                                     if (document.getElementById('lti_secsrcfield_'+item)) {
 3239:                                         document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3240:                                     }
 3241:                                 }
 3242:                             }
 3243:                         }
 3244:                     }
 3245:                 }
 3246:             }
 3247:             if (!setvis) {
 3248:                 if (document.getElementById('lti_crssecfield_'+item)) {
 3249:                     document.getElementById('lti_crssecfield_'+item).style.display = 'none';
 3250:                 }
 3251:                 if (document.getElementById('lti_secsrcfield_'+item)) {
 3252:                     document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3253:                 }
 3254:             }
 3255:         }
 3256:     } else if (setting == 'lcauth') {
 3257:         var numauth = form.elements['lti_lcauth_'+item].length;
 3258:         if (numauth) {
 3259:             for (var i=0; i<numauth; i++) {
 3260:                 if (form.elements['lti_lcauth_'+item][i].checked) {
 3261:                     if (document.getElementById('lti_'+setting+'_parmrow_'+item)) {
 3262:                         if ((form.elements['lti_'+setting+'_'+item][i].value == 'internal') || (form.elements['lti_'+setting+'_'+item][i].value == 'lti')) {
 3263:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'none';
 3264:                         } else {
 3265:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'table-row';
 3266:                             if (document.getElementById('lti_'+setting+'_parmtext_'+item)) {
 3267:                                 if (form.elements['lti_'+setting+'_'+item][i].value == 'localauth') {
 3268:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'localauth'}";
 3269:                                 } else {
 3270:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'krb'}";
 3271:                                 }
 3272:                             }
 3273:                         }
 3274:                     }
 3275:                 }
 3276:             }
 3277:         }
 3278:     } else if (setting == 'lcmenu') {
 3279:         var menus = new Array('lti_topmenu_'+item,'lti_inlinemenu_'+item);
 3280:         var divid = 'lti_menufield_'+item;
 3281:         var setvis = '';
 3282:         for (var i=0; i<menus.length; i++) {
 3283:             var radioname = menus[i];  
 3284:             var num = form.elements[radioname].length;
 3285:             if (num) {
 3286:                 for (var j=0; j<num; j++) {
 3287:                     if (form.elements[radioname][j].checked) {
 3288:                         if (form.elements[radioname][j].value == '1') {
 3289:                             if (document.getElementById(divid)) {
 3290:                                 document.getElementById(divid).style.display = 'inline-block';
 3291:                             }
 3292:                             setvis = 1;
 3293:                             break;
 3294:                         }
 3295:                     }
 3296:                 }
 3297:             }
 3298:             if (setvis == 1) {
 3299:                 break;
 3300:             }
 3301:         }
 3302:         if (!setvis) {
 3303:             if (document.getElementById(divid)) {
 3304:                 document.getElementById(divid).style.display = 'none';
 3305:             }
 3306:         }
 3307:     }
 3308:     return;
 3309: }
 3310: // ]]>
 3311: </script>
 3312: 
 3313: ENDSCRIPT
 3314: }
 3315: 
 3316: sub autoupdate_javascript {
 3317:     return <<"ENDSCRIPT";
 3318: <script type="text/javascript">
 3319: // <![CDATA[
 3320: function toggleLastActiveDays(form) {
 3321:     var radioname = 'lastactive';
 3322:     var divid = 'lastactive_div';
 3323:     var num = form.elements[radioname].length;
 3324:     if (num) {
 3325:         var setvis = '';
 3326:         for (var i=0; i<num; i++) {
 3327:             if (form.elements[radioname][i].checked) {
 3328:                 if (form.elements[radioname][i].value == '1') {
 3329:                     if (document.getElementById(divid)) {
 3330:                         document.getElementById(divid).style.display = 'inline-block';
 3331:                     }
 3332:                     setvis = 1;
 3333:                 }
 3334:                 break;
 3335:             }
 3336:         }
 3337:         if (!setvis) {
 3338:             if (document.getElementById(divid)) {
 3339:                 document.getElementById(divid).style.display = 'none';
 3340:             }
 3341:         }
 3342:     }
 3343:     return;
 3344: }
 3345: // ]]>
 3346: </script>
 3347: 
 3348: ENDSCRIPT
 3349: }
 3350: 
 3351: sub print_autoenroll {
 3352:     my ($dom,$settings,$rowtotal) = @_;
 3353:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 3354:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 3355:     if (ref($settings) eq 'HASH') {
 3356:         if (exists($settings->{'run'})) {
 3357:             if ($settings->{'run'} eq '0') {
 3358:                 $runoff = ' checked="checked" ';
 3359:                 $runon = ' ';
 3360:             } else {
 3361:                 $runon = ' checked="checked" ';
 3362:                 $runoff = ' ';
 3363:             }
 3364:         } else {
 3365:             if ($autorun) {
 3366:                 $runon = ' checked="checked" ';
 3367:                 $runoff = ' ';
 3368:             } else {
 3369:                 $runoff = ' checked="checked" ';
 3370:                 $runon = ' ';
 3371:             }
 3372:         }
 3373:         if (exists($settings->{'co-owners'})) {
 3374:             if ($settings->{'co-owners'} eq '0') {
 3375:                 $coownersoff = ' checked="checked" ';
 3376:                 $coownerson = ' ';
 3377:             } else {
 3378:                 $coownerson = ' checked="checked" ';
 3379:                 $coownersoff = ' ';
 3380:             }
 3381:         } else {
 3382:             $coownersoff = ' checked="checked" ';
 3383:             $coownerson = ' ';
 3384:         }
 3385:         if (exists($settings->{'sender_domain'})) {
 3386:             $defdom = $settings->{'sender_domain'};
 3387:         }
 3388:         if (exists($settings->{'autofailsafe'})) {
 3389:             $failsafe = $settings->{'autofailsafe'};
 3390:         }
 3391:     } else {
 3392:         if ($autorun) {
 3393:             $runon = ' checked="checked" ';
 3394:             $runoff = ' ';
 3395:         } else {
 3396:             $runoff = ' checked="checked" ';
 3397:             $runon = ' ';
 3398:         }
 3399:     }
 3400:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 3401:     my $notif_sender;
 3402:     if (ref($settings) eq 'HASH') {
 3403:         $notif_sender = $settings->{'sender_uname'};
 3404:     }
 3405:     my $datatable='<tr class="LC_odd_row">'.
 3406:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 3407:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3408:                   '<input type="radio" name="autoenroll_run"'.
 3409:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3410:                   '<label><input type="radio" name="autoenroll_run"'.
 3411:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3412:                   '</tr><tr>'.
 3413:                   '<td>'.&mt('Notification messages - sender').
 3414:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 3415:                   &mt('username').':&nbsp;'.
 3416:                   '<input type="text" name="sender_uname" value="'.
 3417:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 3418:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 3419:                   '<tr class="LC_odd_row">'.
 3420:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 3421:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3422:                   '<input type="radio" name="autoassign_coowners"'.
 3423:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3424:                   '<label><input type="radio" name="autoassign_coowners"'.
 3425:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3426:                   '</tr><tr>'.
 3427:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 3428:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3429:                   '<input type="text" name="autoenroll_failsafe"'.
 3430:                   ' value="'.$failsafe.'" size="4" /></span></td></tr>';
 3431:     $$rowtotal += 4;
 3432:     return $datatable;
 3433: }
 3434: 
 3435: sub print_autoupdate {
 3436:     my ($position,$dom,$settings,$rowtotal) = @_;
 3437:     my ($enable,$datatable);
 3438:     if ($position eq 'top') {
 3439:         my %choices = &Apache::lonlocal::texthash (
 3440:                           run        => 'Auto-update active?',
 3441:                           classlists => 'Update information in classlists?',
 3442:                           unexpired  => 'Skip updates for users without active or future roles?',
 3443:                           lastactive => 'Skip updates for inactive users?',
 3444:         );
 3445:         my $itemcount = 0;
 3446:         my $updateon = ' ';
 3447:         my $updateoff = ' checked="checked" ';
 3448:         if (ref($settings) eq 'HASH') {
 3449:             if ($settings->{'run'} eq '1') {
 3450:                 $updateon = $updateoff;
 3451:                 $updateoff = ' ';
 3452:             }
 3453:         }
 3454:         $enable = '<tr class="LC_odd_row">'.
 3455:                   '<td>'.$choices{'run'}.'</td>'.
 3456:                   '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 3457:                   '<input type="radio" name="autoupdate_run"'.
 3458:                   $updateoff.' value="0" />'.&mt('No').'</label>&nbsp;'.
 3459:                   '<label><input type="radio" name="autoupdate_run"'.
 3460:                   $updateon.'value="1" />'.&mt('Yes').'</label></span></td>'.
 3461:                   '</tr>';
 3462:         my @toggles = ('classlists','unexpired');
 3463:         my %defaultchecked = ('classlists' => 'off',
 3464:                               'unexpired'  => 'off'
 3465:                               );
 3466:         $$rowtotal ++;
 3467:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3468:                                                      \%choices,$itemcount,'','','left','no');
 3469:         $datatable = $enable.$datatable;
 3470:         $$rowtotal += $itemcount;
 3471:         my $lastactiveon = ' ';
 3472:         my $lastactiveoff = ' checked="checked" ';
 3473:         my $lastactivestyle = 'none';
 3474:         my $lastactivedays;
 3475:         my $onclick = ' onclick="javascript:toggleLastActiveDays(this.form);"';
 3476:         if (ref($settings) eq 'HASH') {
 3477:             if ($settings->{'lastactive'} =~ /^\d+$/) {
 3478:                 $lastactiveon = $lastactiveoff;
 3479:                 $lastactiveoff = ' ';
 3480:                 $lastactivestyle = 'inline-block';
 3481:                 $lastactivedays = $settings->{'lastactive'};
 3482:             }
 3483:         }
 3484:         my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3485:         $datatable .= '<tr'.$css_class.'>'.
 3486:                       '<td>'.$choices{'lastactive'}.'</td>'.
 3487:                       '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 3488:                       '<input type="radio" name="lastactive"'.
 3489:                       $lastactiveoff.'value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3490:                       '&nbsp;<label>'.
 3491:                       '<input type="radio" name="lastactive"'.
 3492:                       $lastactiveon.' value="1"'.$onclick.' />'.&mt('Yes').'</label>'.
 3493:                       '<div id="lastactive_div" style="display:'.$lastactivestyle.';">'.
 3494:                       ':&nbsp;'.&mt('inactive = no activity in last [_1] days',
 3495:                           '<input type="text" size="5" name="lastactivedays" value="'.
 3496:                           $lastactivedays.'" />').
 3497:                       '</span></td>'.
 3498:                       '</tr>';
 3499:         $$rowtotal ++;
 3500:     } elsif ($position eq 'middle') {
 3501:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3502:         my $numinrow = 3;
 3503:         my $locknamesettings;
 3504:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 3505:                                      $dom,$numinrow,$othertitle,
 3506:                                     'lockablenames',$rowtotal);
 3507:         $$rowtotal ++;
 3508:     } else {
 3509:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3510:         my @fields = ('lastname','firstname','middlename','generation',
 3511:                       'permanentemail','id');
 3512:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 3513:         my $numrows = 0;
 3514:         if (ref($types) eq 'ARRAY') {
 3515:             if (@{$types} > 0) {
 3516:                 $datatable = 
 3517:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 3518:                                          \@fields,$types,\$numrows);
 3519:                     $$rowtotal += @{$types}; 
 3520:             }
 3521:         }
 3522:         $datatable .= 
 3523:             &usertype_update_row($settings,{'default' => $othertitle},
 3524:                                  \%fieldtitles,\@fields,['default'],
 3525:                                  \$numrows);
 3526:         $$rowtotal ++;     
 3527:     }
 3528:     return $datatable;
 3529: }
 3530: 
 3531: sub print_autocreate {
 3532:     my ($dom,$settings,$rowtotal) = @_;
 3533:     my (%createon,%createoff,%currhash);
 3534:     my @types = ('xml','req');
 3535:     if (ref($settings) eq 'HASH') {
 3536:         foreach my $item (@types) {
 3537:             $createoff{$item} = ' checked="checked" ';
 3538:             $createon{$item} = ' ';
 3539:             if (exists($settings->{$item})) {
 3540:                 if ($settings->{$item}) {
 3541:                     $createon{$item} = ' checked="checked" ';
 3542:                     $createoff{$item} = ' ';
 3543:                 }
 3544:             }
 3545:         }
 3546:         if ($settings->{'xmldc'} ne '') {
 3547:             $currhash{$settings->{'xmldc'}} = 1;
 3548:         }
 3549:     } else {
 3550:         foreach my $item (@types) {
 3551:             $createoff{$item} = ' checked="checked" ';
 3552:             $createon{$item} = ' ';
 3553:         }
 3554:     }
 3555:     $$rowtotal += 2;
 3556:     my $numinrow = 2;
 3557:     my $datatable='<tr class="LC_odd_row">'.
 3558:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 3559:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3560:                   '<input type="radio" name="autocreate_xml"'.
 3561:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3562:                   '<label><input type="radio" name="autocreate_xml"'.
 3563:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 3564:                   '</td></tr><tr>'.
 3565:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 3566:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3567:                   '<input type="radio" name="autocreate_req"'.
 3568:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3569:                   '<label><input type="radio" name="autocreate_req"'.
 3570:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 3571:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3572:                                                    'autocreate_xmldc',%currhash);
 3573:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 3574:     if ($numdc > 1) {
 3575:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 3576:                       '</td><td class="LC_left_item">';
 3577:     } else {
 3578:         $datatable .= &mt('Course creation processed as:').
 3579:                       '</td><td class="LC_right_item">';
 3580:     }
 3581:     $datatable .= $dctable.'</td></tr>';
 3582:     $$rowtotal += $rows;
 3583:     return $datatable;
 3584: }
 3585: 
 3586: sub print_directorysrch {
 3587:     my ($position,$dom,$settings,$rowtotal) = @_;
 3588:     my $datatable;
 3589:     if ($position eq 'top') {
 3590:         my $instsrchon = ' ';
 3591:         my $instsrchoff = ' checked="checked" ';
 3592:         my ($exacton,$containson,$beginson);
 3593:         my $instlocalon = ' ';
 3594:         my $instlocaloff = ' checked="checked" ';
 3595:         if (ref($settings) eq 'HASH') {
 3596:             if ($settings->{'available'} eq '1') {
 3597:                 $instsrchon = $instsrchoff;
 3598:                 $instsrchoff = ' ';
 3599:             }
 3600:             if ($settings->{'localonly'} eq '1') {
 3601:                 $instlocalon = $instlocaloff;
 3602:                 $instlocaloff = ' ';
 3603:             }
 3604:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 3605:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 3606:                     if ($type eq 'exact') {
 3607:                         $exacton = ' checked="checked" ';
 3608:                     } elsif ($type eq 'contains') {
 3609:                         $containson = ' checked="checked" ';
 3610:                     } elsif ($type eq 'begins') {
 3611:                         $beginson = ' checked="checked" ';
 3612:                     }
 3613:                 }
 3614:             } else {
 3615:                 if ($settings->{'searchtypes'} eq 'exact') {
 3616:                     $exacton = ' checked="checked" ';
 3617:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 3618:                     $containson = ' checked="checked" ';
 3619:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 3620:                     $exacton = ' checked="checked" ';
 3621:                     $containson = ' checked="checked" ';
 3622:                 }
 3623:             }
 3624:         }
 3625:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 3626:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3627: 
 3628:         my $numinrow = 4;
 3629:         my $cansrchrow = 0;
 3630:         $datatable='<tr class="LC_odd_row">'.
 3631:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 3632:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3633:                    '<input type="radio" name="dirsrch_available"'.
 3634:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3635:                    '<label><input type="radio" name="dirsrch_available"'.
 3636:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3637:                    '</tr><tr>'.
 3638:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 3639:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3640:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 3641:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3642:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 3643:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3644:                    '</tr>';
 3645:         $$rowtotal += 2;
 3646:         if (ref($usertypes) eq 'HASH') {
 3647:             if (keys(%{$usertypes}) > 0) {
 3648:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3649:                                              $numinrow,$othertitle,'cansearch',
 3650:                                              $rowtotal);
 3651:                 $cansrchrow = 1;
 3652:             }
 3653:         }
 3654:         if ($cansrchrow) {
 3655:             $$rowtotal ++;
 3656:             $datatable .= '<tr>';
 3657:         } else {
 3658:             $datatable .= '<tr class="LC_odd_row">';
 3659:         }
 3660:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 3661:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 3662:         foreach my $title (@{$titleorder}) {
 3663:             if (defined($searchtitles->{$title})) {
 3664:                 my $check = ' ';
 3665:                 if (ref($settings) eq 'HASH') {
 3666:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 3667:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 3668:                             $check = ' checked="checked" ';
 3669:                         }
 3670:                     }
 3671:                 }
 3672:                 $datatable .= '<td class="LC_left_item">'.
 3673:                               '<span class="LC_nobreak"><label>'.
 3674:                               '<input type="checkbox" name="searchby" '.
 3675:                               'value="'.$title.'"'.$check.'/>'.
 3676:                               $searchtitles->{$title}.'</label></span></td>';
 3677:             }
 3678:         }
 3679:         $datatable .= '</tr></table></td></tr>';
 3680:         $$rowtotal ++;
 3681:         if ($cansrchrow) {
 3682:             $datatable .= '<tr class="LC_odd_row">';
 3683:         } else {
 3684:             $datatable .= '<tr>';
 3685:         }
 3686:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 3687:                       '<td class="LC_left_item" colspan="2">'.
 3688:                       '<span class="LC_nobreak"><label>'.
 3689:                       '<input type="checkbox" name="searchtypes" '.
 3690:                       $exacton.' value="exact" />'.&mt('Exact match').
 3691:                       '</label>&nbsp;'.
 3692:                       '<label><input type="checkbox" name="searchtypes" '.
 3693:                       $beginson.' value="begins" />'.&mt('Begins with').
 3694:                       '</label>&nbsp;'.
 3695:                       '<label><input type="checkbox" name="searchtypes" '.
 3696:                       $containson.' value="contains" />'.&mt('Contains').
 3697:                       '</label></span></td></tr>';
 3698:         $$rowtotal ++;
 3699:     } else {
 3700:         my $domsrchon = ' checked="checked" ';
 3701:         my $domsrchoff = ' ';
 3702:         my $domlocalon = ' ';
 3703:         my $domlocaloff = ' checked="checked" ';
 3704:         if (ref($settings) eq 'HASH') {
 3705:             if ($settings->{'lclocalonly'} eq '1') {
 3706:                 $domlocalon = $domlocaloff;
 3707:                 $domlocaloff = ' ';
 3708:             }
 3709:             if ($settings->{'lcavailable'} eq '0') {
 3710:                 $domsrchoff = $domsrchon;
 3711:                 $domsrchon = ' ';
 3712:             }
 3713:         }
 3714:         $datatable='<tr class="LC_odd_row">'.
 3715:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 3716:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3717:                       '<input type="radio" name="dirsrch_domavailable"'.
 3718:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3719:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 3720:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3721:                       '</tr><tr>'.
 3722:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 3723:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3724:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 3725:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3726:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 3727:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3728:                       '</tr>';
 3729:         $$rowtotal += 2;
 3730:     }
 3731:     return $datatable;
 3732: }
 3733: 
 3734: sub print_contacts {
 3735:     my ($position,$dom,$settings,$rowtotal) = @_;
 3736:     my $datatable;
 3737:     my @contacts = ('adminemail','supportemail');
 3738:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 3739:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings,%lonstatus);
 3740:     if ($position eq 'top') {
 3741:         if (ref($settings) eq 'HASH') {
 3742:             foreach my $item (@contacts) {
 3743:                 if (exists($settings->{$item})) {
 3744:                     $to{$item} = $settings->{$item};
 3745:                 }
 3746:             }
 3747:         }
 3748:     } elsif ($position eq 'middle') {
 3749:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 3750:                      'updatesmail','idconflictsmail','hostipmail');
 3751:         foreach my $type (@mailings) {
 3752:             $otheremails{$type} = '';
 3753:         }
 3754:     } elsif ($position eq 'lower') {
 3755:         if (ref($settings) eq 'HASH') {
 3756:             if (ref($settings->{'lonstatus'}) eq 'HASH') {
 3757:                 %lonstatus = %{$settings->{'lonstatus'}};
 3758:             }
 3759:         }
 3760:     } else {
 3761:         @mailings = ('helpdeskmail','otherdomsmail');
 3762:         foreach my $type (@mailings) {
 3763:             $otheremails{$type} = '';
 3764:         }
 3765:         $bccemails{'helpdeskmail'} = '';
 3766:         $bccemails{'otherdomsmail'} = '';
 3767:         $includestr{'helpdeskmail'} = '';
 3768:         $includestr{'otherdomsmail'} = '';
 3769:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 3770:     }
 3771:     if (ref($settings) eq 'HASH') {
 3772:         unless (($position eq 'top') || ($position eq 'lower')) {
 3773:             foreach my $type (@mailings) {
 3774:                 if (exists($settings->{$type})) {
 3775:                     if (ref($settings->{$type}) eq 'HASH') {
 3776:                         foreach my $item (@contacts) {
 3777:                             if ($settings->{$type}{$item}) {
 3778:                                 $checked{$type}{$item} = ' checked="checked" ';
 3779:                             }
 3780:                         }
 3781:                         $otheremails{$type} = $settings->{$type}{'others'};
 3782:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3783:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 3784:                             if ($settings->{$type}{'include'} ne '') {
 3785:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3786:                                 $includestr{$type} = &unescape($includestr{$type});
 3787:                             }
 3788:                         }
 3789:                     }
 3790:                 } elsif ($type eq 'lonstatusmail') {
 3791:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3792:                 }
 3793:             }
 3794:         }
 3795:         if ($position eq 'bottom') {
 3796:             foreach my $type (@mailings) {
 3797:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 3798:                 if ($settings->{$type}{'include'} ne '') {
 3799:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3800:                     $includestr{$type} = &unescape($includestr{$type});
 3801:                 }
 3802:             }
 3803:             if (ref($settings->{'helpform'}) eq 'HASH') {
 3804:                 if (ref($fields) eq 'ARRAY') {
 3805:                     foreach my $field (@{$fields}) {
 3806:                         $currfield{$field} = $settings->{'helpform'}{$field};
 3807:                     }
 3808:                 }
 3809:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 3810:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 3811:                 } else {
 3812:                     $maxsize = '1.0';
 3813:                 }
 3814:             } else {
 3815:                 if (ref($fields) eq 'ARRAY') {
 3816:                     foreach my $field (@{$fields}) {
 3817:                         $currfield{$field} = 'yes';
 3818:                     }
 3819:                 }
 3820:                 $maxsize = '1.0';
 3821:             }
 3822:         }
 3823:     } else {
 3824:         if ($position eq 'top') {
 3825:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 3826:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 3827:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 3828:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 3829:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3830:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 3831:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 3832:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 3833:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 3834:         } elsif ($position eq 'bottom') {
 3835:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 3836:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 3837:             if (ref($fields) eq 'ARRAY') {
 3838:                 foreach my $field (@{$fields}) {
 3839:                     $currfield{$field} = 'yes';
 3840:                 }
 3841:             }
 3842:             $maxsize = '1.0';
 3843:         }
 3844:     }
 3845:     my ($titles,$short_titles) = &contact_titles();
 3846:     my $rownum = 0;
 3847:     my $css_class;
 3848:     if ($position eq 'top') {
 3849:         foreach my $item (@contacts) {
 3850:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3851:             $datatable .= '<tr'.$css_class.'>'. 
 3852:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3853:                           '</span></td><td class="LC_right_item">'.
 3854:                           '<input type="text" name="'.$item.'" value="'.
 3855:                           $to{$item}.'" /></td></tr>';
 3856:             $rownum ++;
 3857:         }
 3858:     } elsif ($position eq 'bottom') {
 3859:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3860:         $datatable .= '<tr'.$css_class.'>'.
 3861:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3862:                       &mt('(e-mail, subject, and description always shown)').
 3863:                       '</td><td class="LC_left_item">';
 3864:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3865:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3866:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3867:             foreach my $field (@{$fields}) {
 3868:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3869:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3870:                     $datatable .= ' '.&mt('(logged-in users)');
 3871:                 }
 3872:                 $datatable .='</td><td>';
 3873:                 my $clickaction;
 3874:                 if ($field eq 'screenshot') {
 3875:                     $clickaction = ' onclick="screenshotSize(this);"';
 3876:                 }
 3877:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3878:                     foreach my $option (@{$possoptions->{$field}}) {
 3879:                         my $checked;
 3880:                         if ($currfield{$field} eq $option) {
 3881:                             $checked = ' checked="checked"';
 3882:                         }
 3883:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3884:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3885:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3886:                                       '</label></span>'.('&nbsp;'x2);
 3887:                     }
 3888:                 }
 3889:                 if ($field eq 'screenshot') {
 3890:                     my $display;
 3891:                     if ($currfield{$field} eq 'no') {
 3892:                         $display = ' style="display:none"';
 3893:                     }
 3894:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 3895:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3896:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3897:                 }
 3898:                 $datatable .= '</td></tr>';
 3899:             }
 3900:             $datatable .= '</table>';
 3901:         }
 3902:         $datatable .= '</td></tr>'."\n";
 3903:         $rownum ++;
 3904:     }
 3905:     unless (($position eq 'top') || ($position eq 'lower')) {
 3906:         foreach my $type (@mailings) {
 3907:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3908:             $datatable .= '<tr'.$css_class.'>'.
 3909:                           '<td><span class="LC_nobreak">'.
 3910:                           $titles->{$type}.': </span></td>'.
 3911:                           '<td class="LC_left_item">';
 3912:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3913:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3914:             }
 3915:             $datatable .= '<span class="LC_nobreak">';
 3916:             foreach my $item (@contacts) {
 3917:                 $datatable .= '<label>'.
 3918:                               '<input type="checkbox" name="'.$type.'"'.
 3919:                               $checked{$type}{$item}.
 3920:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3921:                               '</label>&nbsp;';
 3922:             }
 3923:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3924:                           '<input type="text" name="'.$type.'_others" '.
 3925:                           'value="'.$otheremails{$type}.'"  />';
 3926:             my %locchecked;
 3927:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3928:                 foreach my $loc ('s','b') {
 3929:                     if ($includeloc{$type} eq $loc) {
 3930:                         $locchecked{$loc} = ' checked="checked"';
 3931:                         last;
 3932:                     }
 3933:                 }
 3934:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3935:                               '<input type="text" name="'.$type.'_bcc" '.
 3936:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3937:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3938:                               &mt('Text automatically added to e-mail:').' '.
 3939:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 3940:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3941:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3942:                               ('&nbsp;'x2).
 3943:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3944:                               '</span></fieldset>';
 3945:             }
 3946:             $datatable .= '</td></tr>'."\n";
 3947:             $rownum ++;
 3948:         }
 3949:     }
 3950:     if ($position eq 'middle') {
 3951:         my %choices;
 3952:         my $corelink = &core_link_msu();
 3953:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 3954:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3955:                                         $corelink);
 3956:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 3957:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 3958:         my %defaultchecked = ('reporterrors'  => 'on',
 3959:                               'reportupdates' => 'on',
 3960:                               'reportstatus'  => 'on');
 3961:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3962:                                                    \%choices,$rownum);
 3963:         $datatable .= $reports;
 3964:     } elsif ($position eq 'lower') {
 3965:         my (%current,%excluded,%weights);
 3966:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 3967:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 3968:             $current{'errorthreshold'} = $lonstatus{'threshold'};
 3969:         } else {
 3970:             $current{'errorthreshold'} = $defaults->{'threshold'};
 3971:         }
 3972:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 3973:             $current{'errorsysmail'} = $lonstatus{'sysmail'};
 3974:         } else {
 3975:             $current{'errorsysmail'} = $defaults->{'sysmail'};
 3976:         }
 3977:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 3978:             foreach my $type ('E','W','N','U') {
 3979:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 3980:                     $weights{$type} = $lonstatus{'weights'}{$type};
 3981:                 } else {
 3982:                     $weights{$type} = $defaults->{$type};
 3983:                 }
 3984:             }
 3985:         } else {
 3986:             foreach my $type ('E','W','N','U') {
 3987:                 $weights{$type} = $defaults->{$type};
 3988:             }
 3989:         }
 3990:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 3991:             if (@{$lonstatus{'excluded'}} > 0) {
 3992:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 3993:             }
 3994:         }
 3995:         foreach my $item ('errorthreshold','errorsysmail') { 
 3996:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3997:             $datatable .= '<tr'.$css_class.'>'.
 3998:                           '<td class="LC_left_item"><span class="LC_nobreak">'.
 3999:                           $titles->{$item}.
 4000:                           '</span></td><td class="LC_left_item">'.
 4001:                           '<input type="text" name="'.$item.'" value="'.
 4002:                           $current{$item}.'" size="5" /></td></tr>';
 4003:             $rownum ++;
 4004:         }
 4005:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4006:         $datatable .= '<tr'.$css_class.'>'.
 4007:                       '<td class="LC_left_item">'.
 4008:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 4009:                       '</span></td><td class="LC_left_item"><table><tr>';
 4010:         foreach my $type ('E','W','N','U') {
 4011:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 4012:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 4013:                           $weights{$type}.'" size="5" /></td>';
 4014:         }
 4015:         $datatable .= '</tr></table></tr>';
 4016:         $rownum ++;
 4017:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4018:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 4019:                       $titles->{'errorexcluded'}.'</td>'.
 4020:                       '<td class="LC_left_item"><table>';
 4021:         my $numinrow = 4;
 4022:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 4023:         for (my $i=0; $i<@ids; $i++) {
 4024:             my $rem = $i%($numinrow);
 4025:             if ($rem == 0) {
 4026:                 if ($i > 0) {
 4027:                     $datatable .= '</tr>';
 4028:                 }
 4029:                 $datatable .= '<tr>';
 4030:             }
 4031:             my $check;
 4032:             if ($excluded{$ids[$i]}) {
 4033:                 $check = ' checked="checked" ';
 4034:             }
 4035:             $datatable .= '<td class="LC_left_item">'.
 4036:                           '<span class="LC_nobreak"><label>'.
 4037:                           '<input type="checkbox" name="errorexcluded" '.
 4038:                           'value="'.$ids[$i].'"'.$check.' />'.
 4039:                           $ids[$i].'</label></span></td>';
 4040:         }
 4041:         my $colsleft = $numinrow - @ids%($numinrow);
 4042:         if ($colsleft > 1 ) {
 4043:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4044:                           '&nbsp;</td>';
 4045:         } elsif ($colsleft == 1) {
 4046:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4047:         }
 4048:         $datatable .= '</tr></table></td></tr>';
 4049:         $rownum ++;
 4050:     } elsif ($position eq 'bottom') {
 4051:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4052:         my (@posstypes,%usertypeshash);
 4053:         if (ref($types) eq 'ARRAY') {
 4054:             @posstypes = @{$types};
 4055:         }
 4056:         if (@posstypes) {
 4057:             if (ref($usertypes) eq 'HASH') {
 4058:                 %usertypeshash = %{$usertypes};
 4059:             }
 4060:             my @overridden;
 4061:             my $numinrow = 4;
 4062:             if (ref($settings) eq 'HASH') {
 4063:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 4064:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 4065:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 4066:                             push(@overridden,$key);
 4067:                             foreach my $item (@contacts) {
 4068:                                 if ($settings->{'overrides'}{$key}{$item}) {
 4069:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 4070:                                 }
 4071:                             }
 4072:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 4073:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 4074:                             $includeloc{'override_'.$key} = '';
 4075:                             $includestr{'override_'.$key} = '';
 4076:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 4077:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) = 
 4078:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 4079:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 4080:                             }
 4081:                         }
 4082:                     }
 4083:                 }
 4084:             }
 4085:             my $customclass = 'LC_helpdesk_override';
 4086:             my $optionsprefix = 'LC_options_helpdesk_';
 4087: 
 4088:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 4089:  
 4090:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4091:                                          $numinrow,$othertitle,'overrides',
 4092:                                          \$rownum,$onclicktypes,$customclass);
 4093:             $rownum ++;
 4094:             $usertypeshash{'default'} = $othertitle;
 4095:             foreach my $status (@posstypes) {
 4096:                 my $css_class;
 4097:                 if ($rownum%2) {
 4098:                     $css_class = 'LC_odd_row ';
 4099:                 }
 4100:                 $css_class .= $customclass;
 4101:                 my $rowid = $optionsprefix.$status;
 4102:                 my $hidden = 1;
 4103:                 my $currstyle = 'display:none';
 4104:                 if (grep(/^\Q$status\E$/,@overridden)) {
 4105:                     $currstyle = 'display:table-row';
 4106:                     $hidden = 0;
 4107:                 }
 4108:                 my $key = 'override_'.$status;
 4109:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 4110:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 4111:                                                   $usertypeshash{$status},$css_class,$currstyle,
 4112:                                                   \@contacts,$short_titles);
 4113:                 unless ($hidden) {
 4114:                     $rownum ++;
 4115:                 }
 4116:             }
 4117:         }
 4118:     }
 4119:     $$rowtotal += $rownum;
 4120:     return $datatable;
 4121: }
 4122: 
 4123: sub core_link_msu {
 4124:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 4125:                                           &mt('LON-CAPA core group - MSU'),600,500);
 4126: }
 4127: 
 4128: sub overridden_helpdesk {
 4129:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 4130:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 4131:     my $class = 'LC_left_item';
 4132:     if ($css_class) {
 4133:         $css_class = ' class="'.$css_class.'"';
 4134:     }
 4135:     if ($rowid) {
 4136:         $rowid = ' id="'.$rowid.'"';
 4137:     }
 4138:     if ($rowstyle) {
 4139:         $rowstyle = ' style="'.$rowstyle.'"';
 4140:     }
 4141:     my ($output,$description);
 4142:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 4143:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 4144:               "<td>$description</td>\n".
 4145:               '<td class="'.$class.'" colspan="2">'.
 4146:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 4147:               '<span class="LC_nobreak">';
 4148:     if (ref($contacts) eq 'ARRAY') {
 4149:         foreach my $item (@{$contacts}) {
 4150:             my $check;
 4151:             if (ref($checked) eq 'HASH') {
 4152:                $check = $checked->{$item};
 4153:             }
 4154:             my $title;
 4155:             if (ref($short_titles) eq 'HASH') {
 4156:                 $title = $short_titles->{$item}; 
 4157:             }
 4158:             $output .= '<label>'.
 4159:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 4160:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 4161:         }
 4162:     }
 4163:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4164:                '<input type="text" name="override_'.$type.'_others" '.
 4165:                'value="'.$otheremails.'"  />';
 4166:     my %locchecked;
 4167:     foreach my $loc ('s','b') {
 4168:         if ($includeloc eq $loc) {
 4169:             $locchecked{$loc} = ' checked="checked"';
 4170:             last;
 4171:         }
 4172:     }
 4173:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4174:                '<input type="text" name="override_'.$type.'_bcc" '.
 4175:                'value="'.$bccemails.'"  /></fieldset>'.
 4176:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4177:                &mt('Text automatically added to e-mail:').' '.
 4178:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 4179:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4180:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4181:                ('&nbsp;'x2).
 4182:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4183:                '</span></fieldset>'.
 4184:                '</td></tr>'."\n";
 4185:     return $output;
 4186: }
 4187: 
 4188: sub contacts_javascript {
 4189:     return <<"ENDSCRIPT";
 4190: 
 4191: <script type="text/javascript">
 4192: // <![CDATA[
 4193: 
 4194: function screenshotSize(field) {
 4195:     if (document.getElementById('help_screenshotsize')) {
 4196:         if (field.value == 'no') {
 4197:             document.getElementById('help_screenshotsize').style.display="none";
 4198:         } else {
 4199:             document.getElementById('help_screenshotsize').style.display="";
 4200:         }
 4201:     }
 4202:     return;
 4203: }
 4204: 
 4205: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 4206:     if (form.elements[checkbox].length != undefined) {
 4207:         var count = 0;
 4208:         if (docount) {
 4209:             for (var i=0; i<form.elements[checkbox].length; i++) {
 4210:                 if (form.elements[checkbox][i].checked) {
 4211:                     count ++;
 4212:                 }
 4213:             }
 4214:         }
 4215:         for (var i=0; i<form.elements[checkbox].length; i++) {
 4216:             var type = form.elements[checkbox][i].value;
 4217:             if (document.getElementById(prefix+type)) {
 4218:                 if (form.elements[checkbox][i].checked) {
 4219:                     document.getElementById(prefix+type).style.display = 'table-row';
 4220:                     if (count % 2 == 1) {
 4221:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 4222:                     } else {
 4223:                         document.getElementById(prefix+type).className = target;
 4224:                     }
 4225:                     count ++;
 4226:                 } else {
 4227:                     document.getElementById(prefix+type).style.display = 'none';
 4228:                 }
 4229:             }
 4230:         }
 4231:     }
 4232:     return;
 4233: }
 4234: 
 4235: 
 4236: // ]]>
 4237: </script>
 4238: 
 4239: ENDSCRIPT
 4240: }
 4241: 
 4242: sub print_helpsettings {
 4243:     my ($position,$dom,$settings,$rowtotal) = @_;
 4244:     my $confname = $dom.'-domainconfig';
 4245:     my $formname = 'display';
 4246:     my ($datatable,$itemcount);
 4247:     if ($position eq 'top') {
 4248:         $itemcount = 1;
 4249:         my (%choices,%defaultchecked,@toggles);
 4250:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 4251:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 4252:                                      &mt('LON-CAPA bug tracker'),600,500));
 4253:         %defaultchecked = ('submitbugs' => 'on');
 4254:         @toggles = ('submitbugs');
 4255:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4256:                                                      \%choices,$itemcount);
 4257:         $$rowtotal ++;
 4258:     } else {
 4259:         my $css_class;
 4260:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 4261:         my (%customroles,%ordered,%current);
 4262:         if (ref($settings) eq 'HASH') {
 4263:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 4264:                 %current = %{$settings->{'adhoc'}};
 4265:             }
 4266:         }
 4267:         my $count = 0;
 4268:         foreach my $key (sort(keys(%existing))) {
 4269:             if ($key=~/^rolesdef\_(\w+)$/) {
 4270:                 my $rolename = $1;
 4271:                 my (%privs,$order);
 4272:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 4273:                 $customroles{$rolename} = \%privs;
 4274:                 if (ref($current{$rolename}) eq 'HASH') {
 4275:                     $order = $current{$rolename}{'order'};
 4276:                 }
 4277:                 if ($order eq '') {
 4278:                     $order = $count;
 4279:                 }
 4280:                 $ordered{$order} = $rolename;
 4281:                 $count++;
 4282:             }
 4283:         }
 4284:         my $maxnum = scalar(keys(%ordered));
 4285:         my @roles_by_num = ();
 4286:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 4287:             push(@roles_by_num,$item);
 4288:         }
 4289:         my $context = 'domprefs';
 4290:         my $crstype = 'Course';
 4291:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4292:         my @accesstypes = ('all','dh','da','none');
 4293:         my ($numstatustypes,@jsarray);
 4294:         if (ref($types) eq 'ARRAY') {
 4295:             if (@{$types} > 0) {
 4296:                 $numstatustypes = scalar(@{$types});
 4297:                 push(@accesstypes,'status');
 4298:                 @jsarray = ('bystatus');
 4299:             }
 4300:         }
 4301:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 4302:         if (keys(%domhelpdesk)) {
 4303:             push(@accesstypes,('inc','exc'));
 4304:             push(@jsarray,('notinc','notexc'));
 4305:         }
 4306:         my $hiddenstr = join("','",@jsarray);
 4307:         my $context = 'domprefs';
 4308:         my $crstype = 'Course';
 4309:         my $prefix = 'helproles_';
 4310:         my $add_class = 'LC_hidden';
 4311:         foreach my $num (@roles_by_num) {
 4312:             my $role = $ordered{$num};
 4313:             my ($desc,$access,@statuses);
 4314:             if (ref($current{$role}) eq 'HASH') {
 4315:                 $desc = $current{$role}{'desc'};
 4316:                 $access = $current{$role}{'access'};
 4317:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 4318:                     @statuses = @{$current{$role}{'insttypes'}};
 4319:                 }
 4320:             }
 4321:             if ($desc eq '') {
 4322:                 $desc = $role;
 4323:             }
 4324:             my $identifier = 'custhelp'.$num;
 4325:             my %full=();
 4326:             my %levels= (
 4327:                          course => {},
 4328:                          domain => {},
 4329:                          system => {},
 4330:                         );
 4331:             my %levelscurrent=(
 4332:                                course => {},
 4333:                                domain => {},
 4334:                                system => {},
 4335:                               );
 4336:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 4337:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4338:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4339:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 4340:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 4341:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 4342:             for (my $k=0; $k<=$maxnum; $k++) {
 4343:                 my $vpos = $k+1;
 4344:                 my $selstr;
 4345:                 if ($k == $num) {
 4346:                     $selstr = ' selected="selected" ';
 4347:                 }
 4348:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4349:             }
 4350:             $datatable .= '</select>'.('&nbsp;'x2).
 4351:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 4352:                           '</td>'.
 4353:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4354:                           &mt('Name shown to users:').
 4355:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 4356:                           '</fieldset>'.
 4357:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 4358:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 4359:                           '<fieldset>'.
 4360:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 4361:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 4362:                                                                    \%levelscurrent,$identifier,
 4363:                                                                    'LC_hidden',$prefix.$num.'_privs').
 4364:                           '</fieldset></td>';
 4365:             $itemcount ++;
 4366:         }
 4367:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4368:         my $newcust = 'custhelp'.$count;
 4369:         my (%privs,%levelscurrent);
 4370:         my %full=();
 4371:         my %levels= (
 4372:                      course => {},
 4373:                      domain => {},
 4374:                      system => {},
 4375:                     );
 4376:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 4377:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4378:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 4379:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 4380:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 4381:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 4382:         for (my $k=0; $k<$maxnum+1; $k++) {
 4383:             my $vpos = $k+1;
 4384:             my $selstr;
 4385:             if ($k == $maxnum) {
 4386:                 $selstr = ' selected="selected" ';
 4387:             }
 4388:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4389:         }
 4390:         $datatable .= '</select>&nbsp;'."\n".
 4391:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 4392:                       '</label></span></td>'.
 4393:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4394:                       '<span class="LC_nobreak">'.
 4395:                       &mt('Internal name:').
 4396:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 4397:                       '</span>'.('&nbsp;'x4).
 4398:                       '<span class="LC_nobreak">'.
 4399:                       &mt('Name shown to users:').
 4400:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 4401:                       '</span></fieldset>'.
 4402:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 4403:                                              $usertypes,$types,\%domhelpdesk).
 4404:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 4405:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 4406:                                                                 \@templateroles,$newcust).
 4407:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 4408:                                                                \%levelscurrent,$newcust).
 4409:                       '</fieldset>'.
 4410:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 4411:                       '</td></tr>';
 4412:         $count ++;
 4413:         $$rowtotal += $count;
 4414:     }
 4415:     return $datatable;
 4416: }
 4417: 
 4418: sub adhocbutton {
 4419:     my ($prefix,$num,$field,$visibility) = @_;
 4420:     my %lt = &Apache::lonlocal::texthash(
 4421:                                           show => 'Show details',
 4422:                                           hide => 'Hide details',
 4423:                                         );
 4424:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 4425:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 4426:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 4427:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 4428: }
 4429: 
 4430: sub helpsettings_javascript {
 4431:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 4432:     return unless(ref($roles_by_num) eq 'ARRAY');
 4433:     my %html_js_lt = &Apache::lonlocal::texthash(
 4434:                                           show => 'Show details',
 4435:                                           hide => 'Hide details',
 4436:                                         );
 4437:     &html_escape(\%html_js_lt);
 4438:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 4439:     return <<"ENDSCRIPT";
 4440: <script type="text/javascript">
 4441: // <![CDATA[
 4442: 
 4443: function reorderHelpRoles(form,item) {
 4444:     var changedVal;
 4445: $jstext
 4446:     var newpos = 'helproles_${total}_pos';
 4447:     var maxh = 1 + $total;
 4448:     var current = new Array();
 4449:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4450:     if (item == newpos) {
 4451:         changedVal = newitemVal;
 4452:     } else {
 4453:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4454:         current[newitemVal] = newpos;
 4455:     }
 4456:     for (var i=0; i<helproles.length; i++) {
 4457:         var elementName = 'helproles_'+helproles[i]+'_pos';
 4458:         if (elementName != item) {
 4459:             if (form.elements[elementName]) {
 4460:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4461:                 current[currVal] = elementName;
 4462:             }
 4463:         }
 4464:     }
 4465:     var oldVal;
 4466:     for (var j=0; j<maxh; j++) {
 4467:         if (current[j] == undefined) {
 4468:             oldVal = j;
 4469:         }
 4470:     }
 4471:     if (oldVal < changedVal) {
 4472:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4473:            var elementName = current[k];
 4474:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4475:         }
 4476:     } else {
 4477:         for (var k=changedVal; k<oldVal; k++) {
 4478:             var elementName = current[k];
 4479:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4480:         }
 4481:     }
 4482:     return;
 4483: }
 4484: 
 4485: function helpdeskAccess(num) {
 4486:     var curraccess = null;
 4487:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 4488:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 4489:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 4490:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 4491:             }
 4492:         }
 4493:     }
 4494:     var shown = Array();
 4495:     var hidden = Array();
 4496:     if (curraccess == 'none') {
 4497:         hidden = Array('$hiddenstr');
 4498:     } else {
 4499:         if (curraccess == 'status') {
 4500:             shown = Array('bystatus');
 4501:             hidden = Array('notinc','notexc');
 4502:         } else {
 4503:             if (curraccess == 'exc') {
 4504:                 shown = Array('notexc');
 4505:                 hidden = Array('notinc','bystatus');
 4506:             }
 4507:             if (curraccess == 'inc') {
 4508:                 shown = Array('notinc');
 4509:                 hidden = Array('notexc','bystatus');
 4510:             }
 4511:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 4512:                 hidden = Array('notinc','notexc','bystatus');
 4513:             }
 4514:         }
 4515:     }
 4516:     if (hidden.length > 0) {
 4517:         for (var i=0; i<hidden.length; i++) {
 4518:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 4519:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 4520:             }
 4521:         }
 4522:     }
 4523:     if (shown.length > 0) {
 4524:         for (var i=0; i<shown.length; i++) {
 4525:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 4526:                 if (shown[i] == 'privs') {
 4527:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 4528:                 } else {
 4529:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 4530:                 }
 4531:             }
 4532:         }
 4533:     }
 4534:     return;
 4535: }
 4536: 
 4537: function toggleHelpdeskItem(num,field) {
 4538:     if (document.getElementById('helproles_'+num+'_'+field)) {
 4539:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 4540:             document.getElementById('helproles_'+num+'_'+field).className =
 4541:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 4542:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4543:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 4544:             }
 4545:         } else {
 4546:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 4547:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4548:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 4549:             }
 4550:         }
 4551:     }
 4552:     return;
 4553: }
 4554: 
 4555: // ]]>
 4556: </script>
 4557: 
 4558: ENDSCRIPT
 4559: }
 4560: 
 4561: sub helpdeskroles_access {
 4562:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 4563:         $usertypes,$types,$domhelpdesk) = @_;
 4564:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 4565:     my %lt = &Apache::lonlocal::texthash(
 4566:                     'rou'    => 'Role usage',
 4567:                     'whi'    => 'Which helpdesk personnel may use this role?',
 4568:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 4569:                     'dh'     => 'All with domain helpdesk role',
 4570:                     'da'     => 'All with domain helpdesk assistant role',
 4571:                     'none'   => 'None',
 4572:                     'status' => 'Determined based on institutional status',
 4573:                     'inc'    => 'Include all, but exclude specific personnel',
 4574:                     'exc'    => 'Exclude all, but include specific personnel',
 4575:                   );
 4576:     my %usecheck = (
 4577:                      all => ' checked="checked"',
 4578:                    );
 4579:     my %displaydiv = (
 4580:                       status => 'none',
 4581:                       inc    => 'none',
 4582:                       exc    => 'none',
 4583:                       priv   => 'block',
 4584:                      );
 4585:     my $output;
 4586:     if (ref($current) eq 'HASH') {
 4587:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 4588:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 4589:                 $usecheck{$current->{access}} = $usecheck{'all'};
 4590:                 delete($usecheck{'all'});
 4591:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 4592:                     my $access = $1;
 4593:                     $displaydiv{$access} = 'inline';
 4594:                 } elsif ($current->{access} eq 'none') {
 4595:                     $displaydiv{'priv'} = 'none';
 4596:                 }
 4597:             }
 4598:         }
 4599:     }
 4600:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 4601:               '<p>'.$lt{'whi'}.'</p>';
 4602:     foreach my $access (@{$accesstypes}) {
 4603:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 4604:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 4605:                    $lt{$access}.'</label>';
 4606:         if ($access eq 'status') {
 4607:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 4608:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 4609:                                                                  $othertitle,$usertypes,$types).
 4610:                        '</div>';
 4611:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 4612:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 4613:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4614:                        '</div>';
 4615:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 4616:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 4617:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4618:                        '</div>';
 4619:         }
 4620:         $output .= '</p>';
 4621:     }
 4622:     $output .= '</fieldset>';
 4623:     return $output;
 4624: }
 4625: 
 4626: sub radiobutton_prefs {
 4627:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 4628:         $additional,$align,$firstval) = @_;
 4629:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 4630:                    (ref($choices) eq 'HASH'));
 4631: 
 4632:     my (%checkedon,%checkedoff,$datatable,$css_class);
 4633: 
 4634:     foreach my $item (@{$toggles}) {
 4635:         if ($defaultchecked->{$item} eq 'on') {
 4636:             $checkedon{$item} = ' checked="checked" ';
 4637:             $checkedoff{$item} = ' ';
 4638:         } elsif ($defaultchecked->{$item} eq 'off') {
 4639:             $checkedoff{$item} = ' checked="checked" ';
 4640:             $checkedon{$item} = ' ';
 4641:         }
 4642:     }
 4643:     if (ref($settings) eq 'HASH') {
 4644:         foreach my $item (@{$toggles}) {
 4645:             if ($settings->{$item} eq '1') {
 4646:                 $checkedon{$item} =  ' checked="checked" ';
 4647:                 $checkedoff{$item} = ' ';
 4648:             } elsif ($settings->{$item} eq '0') {
 4649:                 $checkedoff{$item} =  ' checked="checked" ';
 4650:                 $checkedon{$item} = ' ';
 4651:             }
 4652:         }
 4653:     }
 4654:     if ($onclick) {
 4655:         $onclick = ' onclick="'.$onclick.'"';
 4656:     }
 4657:     foreach my $item (@{$toggles}) {
 4658:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4659:         $datatable .=
 4660:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 4661:             '<span class="LC_nobreak">'.$choices->{$item}.
 4662:             '</span></td>';
 4663:         if ($align eq 'left') {
 4664:             $datatable .= '<td class="LC_left_item">';
 4665:         } else {
 4666:             $datatable .= '<td class="LC_right_item">';
 4667:         }
 4668:         $datatable .= '<span class="LC_nobreak">';
 4669:         if ($firstval eq 'no') {
 4670:             $datatable .=
 4671:                 '<label><input type="radio" name="'.
 4672:                 $item.'" '.$checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').
 4673:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 4674:                 $checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').'</label>';
 4675:         } else {
 4676:             $datatable .=
 4677:                 '<label><input type="radio" name="'.
 4678:                 $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 4679:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 4680:                 $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>';
 4681:         }
 4682:         $datatable .= '</span>'.$additional.'</td></tr>';
 4683:         $itemcount ++;
 4684:     }
 4685:     return ($datatable,$itemcount);
 4686: }
 4687: 
 4688: sub print_ltitools {
 4689:     my ($dom,$settings,$rowtotal) = @_;
 4690:     my $rownum = 0;
 4691:     my $css_class;
 4692:     my $itemcount = 1;
 4693:     my $maxnum = 0;
 4694:     my %ordered;
 4695:     if (ref($settings) eq 'HASH') {
 4696:         foreach my $item (keys(%{$settings})) {
 4697:             if (ref($settings->{$item}) eq 'HASH') {
 4698:                 my $num = $settings->{$item}{'order'};
 4699:                 $ordered{$num} = $item;
 4700:             }
 4701:         }
 4702:     }
 4703:     my $confname = $dom.'-domainconfig';
 4704:     my $switchserver = &check_switchserver($dom,$confname);
 4705:     my $maxnum = scalar(keys(%ordered));
 4706:     my $datatable;
 4707:     my %lt = &ltitools_names();
 4708:     my @courseroles = ('cc','in','ta','ep','st');
 4709:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 4710:     my @fields = ('fullname','firstname','lastname','email','roles','user');
 4711:     if (keys(%ordered)) {
 4712:         my @items = sort { $a <=> $b } keys(%ordered);
 4713:         for (my $i=0; $i<@items; $i++) {
 4714:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4715:             my $item = $ordered{$items[$i]};
 4716:             my ($title,$key,$secret,$url,$lifetime,$imgsrc,%sigsel);
 4717:             if (ref($settings->{$item}) eq 'HASH') {
 4718:                 $title = $settings->{$item}->{'title'};
 4719:                 $url = $settings->{$item}->{'url'};
 4720:                 $key = $settings->{$item}->{'key'};
 4721:                 $secret = $settings->{$item}->{'secret'};
 4722:                 $lifetime = $settings->{$item}->{'lifetime'};
 4723:                 my $image = $settings->{$item}->{'image'};
 4724:                 if ($image ne '') {
 4725:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 4726:                 }
 4727:                 if ($settings->{$item}->{'sigmethod'} eq 'HMAC-256') {
 4728:                     $sigsel{'HMAC-256'} = ' selected="selected"';
 4729:                 } else {
 4730:                     $sigsel{'HMAC-SHA1'} = ' selected="selected"';
 4731:                 }
 4732:             }
 4733:             my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_".$item."'".');"';
 4734:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4735:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 4736:             for (my $k=0; $k<=$maxnum; $k++) {
 4737:                 my $vpos = $k+1;
 4738:                 my $selstr;
 4739:                 if ($k == $i) {
 4740:                     $selstr = ' selected="selected" ';
 4741:                 }
 4742:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4743:             }
 4744:             $datatable .= '</select>'.('&nbsp;'x2).
 4745:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 4746:                 &mt('Delete?').'</label></span></td>'.
 4747:                 '<td colspan="2">'.
 4748:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4749:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 4750:                 ('&nbsp;'x2).
 4751:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 4752:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 4753:                 ('&nbsp;'x2).
 4754:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 4755:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4756:                 ('&nbsp;'x2).
 4757:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_sigmethod_'.$i.'">'.
 4758:                 '<option value="HMAC-SHA1"'.$sigsel{'HMAC-SHA1'}.'>HMAC-SHA1</option>'.
 4759:                 '<option value="HMAC-SHA256"'.$sigsel{'HMAC-SHA256'}.'>HMAC-SHA256</option></select></span>'.
 4760:                 '<br /><br />'.
 4761:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_url_'.$i.'"'.
 4762:                 ' value="'.$url.'" /></span>'.
 4763:                 ('&nbsp;'x2).
 4764:                 '<span class="LC_nobreak">'.$lt{'key'}.':'.
 4765:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 4766:                 ('&nbsp;'x2).
 4767:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':'.
 4768:                 '<input type="text" size="5" name="ltitools_lifetime_'.$i.'" value="'.$lifetime.'" /></span> '.
 4769:                 ('&nbsp;'x2).
 4770:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 4771:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 4772:                 '<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>'.
 4773:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 4774:                 '</fieldset>'.
 4775:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4776:                 '<span class="LC_nobreak">'.&mt('Display target:');
 4777:             my %currdisp;
 4778:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 4779:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 4780:                     $currdisp{'window'} = ' checked="checked"';
 4781:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 4782:                     $currdisp{'tab'} = ' checked="checked"';
 4783:                 } else {
 4784:                     $currdisp{'iframe'} = ' checked="checked"';
 4785:                 }
 4786:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 4787:                     $currdisp{'width'} = $1;
 4788:                 }
 4789:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 4790:                      $currdisp{'height'} = $1;
 4791:                 }
 4792:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 4793:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 4794:             } else {
 4795:                 $currdisp{'iframe'} = ' checked="checked"';
 4796:             }
 4797:             foreach my $disp ('iframe','tab','window') {
 4798:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 4799:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 4800:             }
 4801:             $datatable .= ('&nbsp;'x4);
 4802:             foreach my $dimen ('width','height') {
 4803:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4804:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 4805:                               ('&nbsp;'x2);
 4806:             }
 4807:             $datatable .= '</span><br />'.
 4808:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4809:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></div>'.
 4810:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4811:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 4812:                           '</textarea></div><div style=""></div><br />';
 4813:             my %units = (
 4814:                           'passback' => 'days',
 4815:                           'roster'   => 'seconds',
 4816:                         );
 4817:             foreach my $extra ('passback','roster') {
 4818:                 my $validsty = 'none';
 4819:                 my $currvalid;
 4820:                 my $checkedon = '';
 4821:                 my $checkedoff = ' checked="checked"';
 4822:                 if ($settings->{$item}->{$extra}) {
 4823:                     $checkedon = $checkedoff;
 4824:                     $checkedoff = '';
 4825:                     $validsty = 'inline-block';
 4826:                     if ($settings->{$item}->{$extra.'valid'} =~ /^\d+\.?\d*$/) {
 4827:                         $currvalid = $settings->{$item}->{$extra.'valid'};
 4828:                     }
 4829:                 }
 4830:                 my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','$i'".');"';
 4831:                 $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 4832:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.$onclick.' />'.
 4833:                               &mt('No').'</label>'.('&nbsp;'x2).
 4834:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.$onclick.' />'.
 4835:                               &mt('Yes').'</label></span></div>'.
 4836:                               '<div class="LC_floatleft" style="display:'.$validsty.';" id="ltitools_'.$extra.'time_'.$i.'">'.
 4837:                               '<span class="LC_nobreak">'.
 4838:                               &mt("at least [_1] $units{$extra} after launch",
 4839:                                   '<input type="text" name="ltitools_'.$extra.'valid_'.$i.'" value="'.$currvalid.'" />').
 4840:                               '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 4841:             }
 4842:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 4843:             if ($imgsrc) {
 4844:                 $datatable .= $imgsrc.
 4845:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 4846:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 4847:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 4848:             } else {
 4849:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4850:             }
 4851:             if ($switchserver) {
 4852:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4853:             } else {
 4854:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 4855:             }
 4856:             $datatable .= '</span></fieldset>';
 4857:             my (%checkedfields,%rolemaps,$userincdom);
 4858:             if (ref($settings->{$item}) eq 'HASH') {
 4859:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 4860:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 4861:                 }
 4862:                 $userincdom = $settings->{$item}->{'incdom'};
 4863:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 4864:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 4865:                     $checkedfields{'roles'} = 1;
 4866:                 }
 4867:             }
 4868:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4869:                           '<span class="LC_nobreak">';
 4870:             my $userfieldstyle = 'display:none;';
 4871:             my $seluserdom = '';
 4872:             my $unseluserdom = ' selected="selected"';
 4873:             foreach my $field (@fields) {
 4874:                 my ($checked,$onclick,$id,$spacer);
 4875:                 if ($checkedfields{$field}) {
 4876:                     $checked = ' checked="checked"';
 4877:                 }
 4878:                 if ($field eq 'user') {
 4879:                     $id = ' id="ltitools_user_field_'.$i.'"';
 4880:                     $onclick = ' onclick="toggleLTITools(this.form,'."'$field','$i'".')"';
 4881:                     if ($checked) {
 4882:                         $userfieldstyle = 'display:inline-block';
 4883:                         if ($userincdom) {
 4884:                             $seluserdom = $unseluserdom;
 4885:                             $unseluserdom = '';
 4886:                         }
 4887:                     }
 4888:                 } else {
 4889:                     $spacer = ('&nbsp;' x2);
 4890:                 }
 4891:                 $datatable .= '<label>'.
 4892:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$id.$checked.$onclick.' />'.
 4893:                               $lt{$field}.'</label>'.$spacer;
 4894:             }
 4895:             $datatable .= '</span>';
 4896:             $datatable .= '<div style="'.$userfieldstyle.'" id="ltitools_user_div_'.$i.'">'.
 4897:                           '<span class="LC_nobreak"> : '.
 4898:                           '<select name="ltitools_userincdom_'.$i.'">'.
 4899:                           '<option value="">'.&mt('Select').'</option>'.
 4900:                           '<option value="0"'.$unseluserdom.'>'.&mt('username').'</option>'.
 4901:                           '<option value="1"'.$seluserdom.'>'.&mt('username:domain').'</option>'.
 4902:                           '</select></span></div>';
 4903:             $datatable .= '</fieldset>'.
 4904:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4905:             foreach my $role (@courseroles) {
 4906:                 my ($selected,$selectnone);
 4907:                 if (!$rolemaps{$role}) {
 4908:                     $selectnone = ' selected="selected"';
 4909:                 }
 4910:                 $datatable .= '<td style="text-align: center">'. 
 4911:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4912:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 4913:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 4914:                 foreach my $ltirole (@ltiroles) {
 4915:                     unless ($selectnone) {
 4916:                         if ($rolemaps{$role} eq $ltirole) {
 4917:                             $selected = ' selected="selected"';
 4918:                         } else {
 4919:                             $selected = '';
 4920:                         }
 4921:                     }
 4922:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 4923:                 }
 4924:                 $datatable .= '</select></td>';
 4925:             }
 4926:             $datatable .= '</tr></table></fieldset>';
 4927:             my %courseconfig;
 4928:             if (ref($settings->{$item}) eq 'HASH') {
 4929:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 4930:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 4931:                 }
 4932:             }
 4933:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4934:             foreach my $item ('label','title','target','linktext','explanation','append') {
 4935:                 my $checked;
 4936:                 if ($courseconfig{$item}) {
 4937:                     $checked = ' checked="checked"';
 4938:                 }
 4939:                 $datatable .= '<label>'.
 4940:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 4941:                        $lt{'crs'.$item}.'</label>&nbsp; '."\n";
 4942:             }
 4943:             $datatable .= '</span></fieldset>'.
 4944:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4945:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 4946:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 4947:                 my %custom = %{$settings->{$item}->{'custom'}};
 4948:                 if (keys(%custom) > 0) {
 4949:                     foreach my $key (sort(keys(%custom))) {
 4950:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 4951:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 4952:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 4953:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 4954:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 4955:                     }
 4956:                 }
 4957:             }
 4958:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 4959:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 4960:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 4961:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 4962:             $datatable .= '</table></fieldset></td></tr>'."\n";
 4963:             $itemcount ++;
 4964:         }
 4965:     }
 4966:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4967:     my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_add_pos'".');"';
 4968:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 4969:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 4970:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 4971:     for (my $k=0; $k<$maxnum+1; $k++) {
 4972:         my $vpos = $k+1;
 4973:         my $selstr;
 4974:         if ($k == $maxnum) {
 4975:             $selstr = ' selected="selected" ';
 4976:         }
 4977:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4978:     }
 4979:     $datatable .= '</select>&nbsp;'."\n".
 4980:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 4981:                   '<td colspan="2">'.
 4982:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4983:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_add_title" value="" /></span> '."\n".
 4984:                   ('&nbsp;'x2).
 4985:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 4986:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 4987:                   ('&nbsp;'x2).
 4988:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 4989:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4990:                   '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_add_sigmethod">'.
 4991:                   '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 4992:                   '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 4993:                   '<br />'.
 4994:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_add_url" value="" /></span> '."\n".
 4995:                   ('&nbsp;'x2).
 4996:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 4997:                   ('&nbsp;'x2).
 4998:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="ltitools_add_lifetime" value="300" /></span> '."\n".
 4999:                   ('&nbsp;'x2).
 5000:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 5001:                   '<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".
 5002:                   '</fieldset>'.
 5003:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 5004:                   '<span class="LC_nobreak">'.&mt('Display target:');
 5005:     my %defaultdisp;
 5006:     $defaultdisp{'iframe'} = ' checked="checked"';
 5007:     foreach my $disp ('iframe','tab','window') {
 5008:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 5009:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 5010:     }
 5011:     $datatable .= ('&nbsp;'x4);
 5012:     foreach my $dimen ('width','height') {
 5013:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 5014:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 5015:                       ('&nbsp;'x2);
 5016:     }
 5017:     $datatable .= '</span><br />'.
 5018:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 5019:                   '<input type="text" name="ltitools_add_linktext" size="5" /></div>'.
 5020:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 5021:                   '<textarea name="ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 5022:                   '</div><div style=""></div><br />';
 5023:     my %units = (
 5024:                   'passback' => 'days',
 5025:                   'roster'   => 'seconds',
 5026:                 );
 5027:     my %defaulttimes = (
 5028:                      'passback' => '7',
 5029:                      'roster'   => '300',
 5030:                    );
 5031:     foreach my $extra ('passback','roster') {
 5032:         my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','add'".');"';
 5033:         $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 5034:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="0" checked="checked"'.$onclick.' />'.
 5035:                       &mt('No').'</label></span>'.('&nbsp;'x2).'<span class="LC_nobreak">'.
 5036:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="1"'.$onclick.' />'.
 5037:                       &mt('Yes').'</label></span></div>'.
 5038:                       '<div class="LC_floatleft" style="display:none;" id="ltitools_'.$extra.'time_add">'.
 5039:                       '<span class="LC_nobreak">'.
 5040:                       &mt("at least [_1] $units{$extra} after launch",
 5041:                           '<input type="text" name="ltitools_'.$extra.'valid_add" value="'.$defaulttimes{$extra}.'" />').
 5042:                       '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 5043:     }
 5044:     $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 5045:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 5046:     if ($switchserver) {
 5047:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5048:     } else {
 5049:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 5050:     }
 5051:     $datatable .= '</span></fieldset>'.
 5052:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 5053:                   '<span class="LC_nobreak">';
 5054:     foreach my $field (@fields) {
 5055:         my ($id,$onclick,$spacer);
 5056:         if ($field eq 'user') {
 5057:             $id = ' id="ltitools_user_field_add"';
 5058:             $onclick = ' onclick="toggleLTITools(this.form,'."'$field','add'".')"';
 5059:         } else {
 5060:             $spacer = ('&nbsp;' x2);
 5061:         }
 5062:         $datatable .= '<label>'.
 5063:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'"'.$id.$onclick.' />'.
 5064:                       $lt{$field}.'</label>'.$spacer;
 5065:     }
 5066:     $datatable .= '</span>'.
 5067:                   '<div style="display:none;" id="ltitools_user_div_add">'.
 5068:                   '<span class="LC_nobreak"> : '.
 5069:                   '<select name="ltitools_userincdom_add">'.
 5070:                   '<option value="" selected="selected">'.&mt('Select').'</option>'.
 5071:                   '<option value="0">'.&mt('username').'</option>'.
 5072:                   '<option value="1">'.&mt('username:domain').'</option>'.
 5073:                   '</select></span></div></fieldset>';
 5074:     $datatable .= '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 5075:     foreach my $role (@courseroles) {
 5076:         my ($checked,$checkednone);
 5077:         $datatable .= '<td style="text-align: center">'.
 5078:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 5079:                       '<select name="ltitools_add_roles_'.$role.'">'.
 5080:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 5081:         foreach my $ltirole (@ltiroles) {
 5082:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 5083:         }
 5084:         $datatable .= '</select></td>';
 5085:     }
 5086:     $datatable .= '</tr></table></fieldset>'.
 5087:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 5088:     foreach my $item ('label','title','target','linktext','explanation','append') {
 5089:         $datatable .= '<label>'.
 5090:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 5091:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 5092:     }
 5093:     $datatable .= '</span></fieldset>'.
 5094:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 5095:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 5096:                   '<tr><td><span class="LC_nobreak">'.
 5097:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 5098:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 5099:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 5100:                   '</table></fieldset>'."\n".
 5101:                   '</td>'."\n".
 5102:                   '</tr>'."\n";
 5103:     $itemcount ++;
 5104:     return $datatable;
 5105: }
 5106: 
 5107: sub ltitools_names {
 5108:     my %lt = &Apache::lonlocal::texthash(
 5109:                                           'title'          => 'Title',
 5110:                                           'version'        => 'Version',
 5111:                                           'msgtype'        => 'Message Type',
 5112:                                           'sigmethod'      => 'Signature Method',
 5113:                                           'url'            => 'URL',
 5114:                                           'key'            => 'Key',
 5115:                                           'lifetime'       => 'Nonce lifetime (s)',
 5116:                                           'secret'         => 'Secret',
 5117:                                           'icon'           => 'Icon',   
 5118:                                           'user'           => 'User',
 5119:                                           'fullname'       => 'Full Name',
 5120:                                           'firstname'      => 'First Name',
 5121:                                           'lastname'       => 'Last Name',
 5122:                                           'email'          => 'E-mail',
 5123:                                           'roles'          => 'Role',
 5124:                                           'window'         => 'Window',
 5125:                                           'tab'            => 'Tab',
 5126:                                           'iframe'         => 'iFrame',
 5127:                                           'height'         => 'Height',
 5128:                                           'width'          => 'Width',
 5129:                                           'linktext'       => 'Default Link Text',
 5130:                                           'explanation'    => 'Default Explanation',
 5131:                                           'passback'       => 'Tool can return grades:',
 5132:                                           'roster'         => 'Tool can retrieve roster:',
 5133:                                           'crstarget'      => 'Display target',
 5134:                                           'crslabel'       => 'Course label',
 5135:                                           'crstitle'       => 'Course title', 
 5136:                                           'crslinktext'    => 'Link Text',
 5137:                                           'crsexplanation' => 'Explanation',
 5138:                                           'crsappend'      => 'Provider URL',
 5139:                                         );
 5140:     return %lt;
 5141: }
 5142: 
 5143: sub print_proctoring {
 5144:     my ($dom,$settings,$rowtotal) = @_;
 5145:     my $itemcount = 1;
 5146:     my (%ordered,%providernames,%current,%currentdef);
 5147:     my $confname = $dom.'-domainconfig';
 5148:     my $switchserver = &check_switchserver($dom,$confname);
 5149:     if (ref($settings) eq 'HASH') {
 5150:         foreach my $item (keys(%{$settings})) {
 5151:             if (ref($settings->{$item}) eq 'HASH') {
 5152:                 my $num = $settings->{$item}{'order'};
 5153:                 $ordered{$num} = $item;
 5154:             }
 5155:         }
 5156:     } else {
 5157:         %ordered = (
 5158:                      1 => 'proctorio',
 5159:                      2 => 'examity',
 5160:                    );
 5161:     }
 5162:     %providernames = &proctoring_providernames();
 5163:     my $maxnum = scalar(keys(%ordered));
 5164:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
 5165:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
 5166:     if (ref($requref) eq 'HASH') {
 5167:         %requserfields = %{$requref};
 5168:     }
 5169:     if (ref($opturef) eq 'HASH') {
 5170:         %optuserfields = %{$opturef};
 5171:     }
 5172:     if (ref($defref) eq 'HASH') {
 5173:         %defaults = %{$defref};
 5174:     }
 5175:     if (ref($extref) eq 'HASH') {
 5176:         %extended = %{$extref};
 5177:     }
 5178:     if (ref($crsref) eq 'HASH') {
 5179:         %crsconf = %{$crsref};
 5180:     }
 5181:     if (ref($rolesref) eq 'ARRAY') {
 5182:         @courseroles = @{$rolesref};
 5183:     }
 5184:     if (ref($ltiref) eq 'ARRAY') {
 5185:         @ltiroles = @{$ltiref};
 5186:     }
 5187:     my $datatable;
 5188:     my $css_class;
 5189:     if (keys(%ordered)) {
 5190:         my @items = sort { $a <=> $b } keys(%ordered);
 5191:         for (my $i=0; $i<@items; $i++) {
 5192:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5193:             my $provider = $ordered{$items[$i]};
 5194:             my $optionsty = 'none';
 5195:             my ($available,$version,$lifetime,$imgsrc,$userincdom,$showroles,
 5196:                 %checkedfields,%rolemaps,%inuse,%crsconfig,%current);
 5197:             if (ref($settings) eq 'HASH') {
 5198:                 if (ref($settings->{$provider}) eq 'HASH') {
 5199:                     %current = %{$settings->{$provider}};
 5200:                     if ($current{'available'}) {
 5201:                         $optionsty = 'block';
 5202:                         $available = 1;
 5203:                     }
 5204:                     if ($current{'lifetime'} =~ /^\d+$/) {
 5205:                         $lifetime = $current{'lifetime'};
 5206:                     }
 5207:                     if ($current{'version'} =~ /^\d+\.\d+$/) {
 5208:                         $version = $current{'version'};
 5209:                     }
 5210:                     if ($current{'image'} ne '') {
 5211:                         $imgsrc = '<img src="'.$current{'image'}.'" alt="'.&mt('Proctoring service icon').'" />';
 5212:                     }
 5213:                     if (ref($current{'fields'}) eq 'ARRAY') {
 5214:                         map { $checkedfields{$_} = 1; } @{$current{'fields'}};
 5215:                     }
 5216:                     $userincdom = $current{'incdom'};
 5217:                     if (ref($current{'roles'}) eq 'HASH') {
 5218:                         %rolemaps = %{$current{'roles'}};
 5219:                         $checkedfields{'roles'} = 1;
 5220:                     }
 5221:                     if (ref($current{'defaults'}) eq 'ARRAY') {
 5222:                         foreach my $val (@{$current{'defaults'}}) {
 5223:                             if (grep(/^\Q$val\E$/,@{$defaults{$provider}})) {
 5224:                                 $inuse{$val} = 1;
 5225:                             } else {
 5226:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5227:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5228:                                         if (grep(/^\Q$val\E$/,@{$extended{$provider}{$poss}})) {
 5229:                                             $inuse{$poss} = $val;
 5230:                                             last;
 5231:                                         }
 5232:                                     }
 5233:                                 }
 5234:                             }
 5235:                         }
 5236:                     } elsif (ref($current{'defaults'}) eq 'HASH') {
 5237:                         foreach my $key (keys(%{$current{'defaults'}})) {
 5238:                             my $currval = $current{'defaults'}{$key}; 
 5239:                             if (grep(/^\Q$key\E$/,@{$defaults{$provider}})) {
 5240:                                 $inuse{$key} = 1;
 5241:                             } else {
 5242:                                 my $match;
 5243:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5244:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5245:                                         if (grep(/^\Q$key\E$/,@{$extended{$provider}{$poss}})) {
 5246:                                             $inuse{$poss} = $key;
 5247:                                             last;
 5248:                                         }
 5249:                                     } elsif (ref($extended{$provider}{$poss}) eq 'HASH') {
 5250:                                         foreach my $inner (sort(keys(%{$extended{$provider}{$poss}}))) {
 5251:                                             if (ref($extended{$provider}{$poss}{$inner}) eq 'ARRAY') {
 5252:                                                 if (grep(/^\Q$currval\E$/,@{$extended{$provider}{$poss}{$inner}})) {
 5253:                                                     $currentdef{$inner} = $currval;
 5254:                                                     $match = 1;
 5255:                                                     last;
 5256:                                                 }
 5257:                                             } elsif ($inner eq $key) {
 5258:                                                 $currentdef{$key} = $currval;
 5259:                                                 $match = 1;
 5260:                                                 last;
 5261:                                             }
 5262:                                         }
 5263:                                     }
 5264:                                     last if ($match);
 5265:                                 }
 5266:                             }
 5267:                         }
 5268:                     }
 5269:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
 5270:                         map { $crsconfig{$_} = 1; } @{$current{'crsconf'}};
 5271:                     }
 5272:                 }
 5273:             }
 5274:             my %lt = &proctoring_titles($provider);
 5275:             my %fieldtitles = &proctoring_fieldtitles($provider);
 5276:             my $onclickavailable = ' onclick="toggleProctoring(this.form,'."'$provider'".');"';
 5277:             my %checkedavailable = (
 5278:                                    yes => '',
 5279:                                    no  => ' checked="checked"',
 5280:                                  );
 5281:             if ($available) {
 5282:                 $checkedavailable{'yes'} = $checkedavailable{'no'};
 5283:                 $checkedavailable{'no'} = '';
 5284:             }
 5285:             my $chgstr = ' onchange="javascript:reorderProctoring(this.form,'."'proctoring_pos_".$provider."'".');"';
 5286:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5287:                          .'<select name="proctoring_pos_'.$provider.'"'.$chgstr.'>';
 5288:             for (my $k=0; $k<$maxnum; $k++) {
 5289:                 my $vpos = $k+1;
 5290:                 my $selstr;
 5291:                 if ($k == $i) {
 5292:                     $selstr = ' selected="selected" ';
 5293:                 }
 5294:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5295:             }
 5296:             if ($version eq '') {
 5297:                 if ($provider eq 'proctorio') {
 5298:                     $version = '1.0';
 5299:                 } elsif ($provider eq 'examity') {
 5300:                     $version = '1.1';
 5301:                 }
 5302:             }
 5303:             if ($lifetime eq '') {
 5304:                 $lifetime = '300';
 5305:             }
 5306:             $datatable .=
 5307:                 '</select>'.('&nbsp;'x2).'<b>'.$providernames{$provider}.'</b></span><br />'.
 5308:                 '<span class="LC_nobreak">'.$lt{'avai'}.'&nbsp;'.
 5309:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="1"'.$onclickavailable.$checkedavailable{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 5310:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="0"'.$onclickavailable.$checkedavailable{no}.' />'.&mt('No').'</label></span>'."\n".
 5311:                 '</td>'.
 5312:                 '<td colspan="2">'.
 5313:                 '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'base'}.'</legend>'.
 5314:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="proctoring_'.$provider.'_version">'.
 5315:                 '<option value="'.$version.'" selected="selected">'.$version.'</option></select></span> '."\n".
 5316:                 ('&nbsp;'x2).
 5317:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="proctoring_'.$provider.'_sigmethod">'.
 5318:                 '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 5319:                 '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 5320:                 ('&nbsp;'x2).
 5321:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="proctoring_'.$provider.'_lifetime" value="'.$lifetime.'" /></span> '."\n".
 5322:                 '<br />'.
 5323:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="proctoring_'.$provider.'_url" value="'.$current{'url'}.'" /></span> '."\n".
 5324:                 '<br />'.
 5325:                 '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="proctoring_'.$provider.'_key" value="'.$current{'key'}.'" /></span> '."\n".
 5326:                 ('&nbsp;'x2).
 5327:                 '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="proctoring_'.$provider.'_secret" value="'.$current{'secret'}.'" />'.
 5328:                 '<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";
 5329:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 5330:             if ($imgsrc) {
 5331:                 $datatable .= $imgsrc.
 5332:                               '<label><input type="checkbox" name="proctoring_image_del"'.
 5333:                               ' value="'.$provider.'" />'.&mt('Delete?').'</label></span> '.
 5334:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:');
 5335:             }
 5336:             $datatable .= '&nbsp;';
 5337:             if ($switchserver) {
 5338:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5339:             } else {
 5340:                 $datatable .= '<input type="file" name="proctoring_image_'.$provider.'" value="" />';
 5341:             }
 5342:             unless ($imgsrc) {
 5343:                 $datatable .= '<br />('.&mt('if larger than 21x21 pixels, image will be scaled').')';
 5344:             }
 5345:             $datatable .= '</fieldset>'."\n";
 5346:             if (ref($requserfields{$provider}) eq 'ARRAY') {
 5347:                 if (@{$requserfields{$provider}} > 0) {
 5348:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'requ'}.'</legend>';
 5349:                     foreach my $field (@{$requserfields{$provider}}) {
 5350:                         $datatable .= '<span class="LC_nobreak">'.
 5351:                                       '<label><input type="checkbox" name="proctoring_reqd_'.$provider.'" value="'.$field.'" checked="checked" disabled="disabled" />'.
 5352:                                       $lt{$field}.'</label>';
 5353:                         if ($field eq 'user') {
 5354:                             my $seluserdom = '';
 5355:                             my $unseluserdom = ' selected="selected"';
 5356:                             if ($userincdom) {
 5357:                                 $seluserdom = $unseluserdom;
 5358:                                 $unseluserdom = '';
 5359:                             }
 5360:                             $datatable .= ':&nbsp;'.
 5361:                                 '<select name="proctoring_userincdom_'.$provider.'">'.
 5362:                                 '<option value="0"'.$unseluserdom.'>'.$lt{'username'}.'</option>'.
 5363:                                 '<option value="1"'.$seluserdom.'>'.$lt{'uname:dom'}.'</option>'.
 5364:                                 '</select>&nbsp;';
 5365:                         } else {
 5366:                             $datatable .= '&nbsp;';
 5367:                             if ($field eq 'roles') {
 5368:                                 $showroles = 1; 
 5369:                             } 
 5370:                         }
 5371:                         $datatable .= '</span> ';
 5372:                     }
 5373:                 }
 5374:                 $datatable .= '</fieldset>'."\n";
 5375:             }
 5376:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
 5377:                 if (@{$optuserfields{$provider}} > 0) {
 5378:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'optu'}.'</legend>'; 
 5379:                     foreach my $field (@{$optuserfields{$provider}}) {
 5380:                         my $checked;
 5381:                         if ($checkedfields{$field}) {
 5382:                             $checked = ' checked="checked"';
 5383:                         }
 5384:                         $datatable .= '<span class="LC_nobreak">'.
 5385:                                       '<label><input type="checkbox" name="proctoring_optional_'.$provider.'" value="'.$field.'"'.$checked.' />'.$lt{$field}.'</label></span>&nbsp; ';
 5386:                     }
 5387:                     $datatable .= '</fieldset>'."\n";
 5388:                 }
 5389:             }
 5390:             if (ref($defaults{$provider}) eq 'ARRAY') {
 5391:                 if (@{$defaults{$provider}}) {
 5392:                     my (%options,@selectboxes);
 5393:                     if (ref($extended{$provider}) eq 'HASH') {
 5394:                         %options = %{$extended{$provider}};
 5395:                     }
 5396:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'defa'}.'</legend>';
 5397:                     my ($rem,$numinrow,$dropdowns);
 5398:                     if ($provider eq 'proctorio') { 
 5399:                         $datatable .= '<table>';
 5400:                         $numinrow = 4;
 5401:                     }
 5402:                     my $i = 0;
 5403:                     foreach my $field (@{$defaults{$provider}}) {
 5404:                         my $checked;
 5405:                         if ($inuse{$field}) {
 5406:                             $checked = ' checked="checked"';
 5407:                         }
 5408:                         if ($provider eq 'examity') {
 5409:                             if ($field eq 'display') {
 5410:                                 $datatable .= '<span class="LC_nobreak">'.&mt('Display target:');
 5411:                                 foreach my $option ('iframe','tab','window') {
 5412:                                     my $checkdisp;
 5413:                                     if ($currentdef{'target'} eq $option) {
 5414:                                         $checkdisp = ' checked="checked"';
 5415:                                     }
 5416:                                     $datatable .= '<label><input type="radio" name="proctoring_target_'.$provider.'" value="'.$option.'"'.$checkdisp.' />'.
 5417:                                     $fieldtitles{$option}.'</label>'.('&nbsp;'x2);
 5418:                                 }
 5419:                                 $datatable .= ('&nbsp;'x4);
 5420:                                 foreach my $dimen ('width','height') {
 5421:                                     $datatable .= '<label>'.$fieldtitles{$dimen}.'&nbsp;'.
 5422:                                                   '<input type="text" name="proctoring_'.$dimen.'_'.$provider.'" size="5" '.
 5423:                                                   'value="'.$currentdef{$dimen}.'" /></label>'.
 5424:                                                   ('&nbsp;'x2);
 5425:                                 }
 5426:                                 $datatable .= '</span><br />'.
 5427:                                               '<div class="LC_left_float">'.$fieldtitles{'linktext'}.'<br />'.
 5428:                                               '<input type="text" name="proctoring_linktext_'.$provider.'" '.
 5429:                                               'size="25" value="'.$currentdef{'linktext'}.'" /></div>'.
 5430:                                               '<div class="LC_left_float">'.$fieldtitles{'explanation'}.'<br />'.
 5431:                                               '<textarea name="proctoring_explanation_'.$provider.'" rows="5" cols="40">'.
 5432:                                               $currentdef{'explanation'}.
 5433:                                               '</textarea></div><div style=""></div><br />';
 5434:                             }
 5435:                         } else {
 5436:                             if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
 5437:                                 my ($output,$selnone);
 5438:                                 unless ($checked) {
 5439:                                     $selnone = ' selected="selected"';
 5440:                                 }
 5441:                                 $output .= '<span class="LC_nobreak">'.$fieldtitles{$field}.': '.
 5442:                                            '<select name="proctoring_defaults_'.$field.'_'.$provider.'">'.
 5443:                                            '<option value=""'.$selnone.'>'.&mt('Not in use').'</option>'; 
 5444:                                 foreach my $option (@{$options{$field}}) {
 5445:                                     my $sel; 
 5446:                                     if ($inuse{$field} eq $option) {
 5447:                                         $sel = ' selected="selected"';
 5448:                                     }
 5449:                                     $output .= '<option value="'.$option.'"'.$sel.'>'.$fieldtitles{$option}.'</option>';
 5450:                                 }
 5451:                                 $output .= '</select></span>';
 5452:                                 push(@selectboxes,$output);
 5453:                             } else {
 5454:                                 $rem = $i%($numinrow);
 5455:                                 if ($rem == 0) {
 5456:                                     if ($i > 0) {
 5457:                                         $datatable .= '</tr>';
 5458:                                     }
 5459:                                     $datatable .= '<tr>';
 5460:                                 }
 5461:                                 $datatable .= '<td class="LC_left_item">'.
 5462:                                               '<span class="LC_nobreak">'.
 5463:                                               '<label><input type="checkbox" name="proctoring_defaults_'.$provider.'" value="'.$field.'"'.$checked.' />'.
 5464:                                               $fieldtitles{$field}.'</label></span></td>';
 5465:                                 $i++;
 5466:                             }
 5467:                         }
 5468:                     }
 5469:                     if ($provider eq 'proctorio') {
 5470:                         if ($numinrow) {
 5471:                             $rem = $i%$numinrow;
 5472:                         }
 5473:                         my $colsleft = $numinrow - $rem;
 5474:                         if ($colsleft > 1) {
 5475:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5476:                         } else {
 5477:                             $datatable .= '<td class="LC_left_item">';
 5478:                         }
 5479:                         $datatable .= '&nbsp;'.
 5480:                                       '</td></tr></table>';
 5481:                         if (@selectboxes) {
 5482:                             $datatable .= '<hr /><table>';
 5483:                             $numinrow = 2;
 5484:                             for (my $i=0; $i<@selectboxes; $i++) {
 5485:                                 $rem = $i%($numinrow);
 5486:                                 if ($rem == 0) {
 5487:                                     if ($i > 0) {
 5488:                                         $datatable .= '</tr>';
 5489:                                     }
 5490:                                     $datatable .= '<tr>';
 5491:                                 }
 5492:                                 $datatable .= '<td class="LC_left_item">'.
 5493:                                               $selectboxes[$i].'</td>';
 5494:                             }
 5495:                             if ($numinrow) {
 5496:                                 $rem = $i%$numinrow;
 5497:                             }
 5498:                             $colsleft = $numinrow - $rem;
 5499:                             if ($colsleft > 1) {
 5500:                                 $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5501:                             } else {
 5502:                                 $datatable .= '<td class="LC_left_item">';
 5503:                             }
 5504:                             $datatable .= '&nbsp;'.
 5505:                                           '</td></tr></table>';
 5506:                         }
 5507:                     }
 5508:                     $datatable .= '</fieldset>';
 5509:                 }
 5510:                 if (ref($crsconf{$provider}) eq 'ARRAY') {
 5511:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5512:                                   '<legend>'.&mt('Configurable in course').'</legend>';
 5513:                     my ($rem,$numinrow);
 5514:                     if ($provider eq 'proctorio') {
 5515:                         $datatable .= '<table>';
 5516:                         $numinrow = 4;
 5517:                     }
 5518:                     my $i = 0;
 5519:                     foreach my $item (@{$crsconf{$provider}}) {
 5520:                         my $name;
 5521:                         if ($provider eq 'examity') {
 5522:                             $name = $lt{'crs'.$item};
 5523:                         } elsif ($provider eq 'proctorio') {
 5524:                             $name = $fieldtitles{$item};
 5525:                             $rem = $i%($numinrow);
 5526:                             if ($rem == 0) {
 5527:                                 if ($i > 0) {
 5528:                                     $datatable .= '</tr>';
 5529:                                 }
 5530:                                 $datatable .= '<tr>';
 5531:                             }
 5532:                             $datatable .= '<td class="LC_left_item>';
 5533:                         }
 5534:                         my $checked;
 5535:                         if ($crsconfig{$item}) {
 5536:                             $checked = ' checked="checked"';
 5537:                         }
 5538:                         $datatable .= '<span class="LC_nobreak"><label>'.
 5539:                                       '<input type="checkbox" name="proctoring_crsconf_'.$provider.'" value="'.$item.'"'.$checked.' />'.
 5540:                                       $name.'</label></span>';
 5541:                         if ($provider eq 'examity') {
 5542:                             $datatable .= '&nbsp; ';
 5543:                         }
 5544:                         $datatable .= "\n";
 5545:                         $i++;
 5546:                     }
 5547:                     if ($provider eq 'proctorio') {
 5548:                         if ($numinrow) {
 5549:                             $rem = $i%$numinrow;
 5550:                         }
 5551:                         my $colsleft = $numinrow - $rem;
 5552:                         if ($colsleft > 1) {
 5553:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5554:                         } else {
 5555:                             $datatable .= '<td class="LC_left_item">';
 5556:                         }
 5557:                         $datatable .= '&nbsp;'.
 5558:                                       '</td></tr></table>';
 5559:                     }
 5560:                     $datatable .= '</fieldset>';
 5561:                 }
 5562:                 if ($showroles) {
 5563:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5564:                                   '<legend>'.&mt('Role mapping').'</legend><table><tr>';
 5565:                     foreach my $role (@courseroles) {
 5566:                         my ($selected,$selectnone);
 5567:                         if (!$rolemaps{$role}) {
 5568:                             $selectnone = ' selected="selected"';
 5569:                         }
 5570:                         $datatable .= '<td style="text-align: center">'.
 5571:                                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 5572:                                       '<select name="proctoring_roles_'.$role.'_'.$provider.'">'.
 5573:                                       '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5574:                         foreach my $ltirole (@ltiroles) {
 5575:                             unless ($selectnone) {
 5576:                                 if ($rolemaps{$role} eq $ltirole) {
 5577:                                     $selected = ' selected="selected"';
 5578:                                 } else {
 5579:                                     $selected = '';
 5580:                                 }
 5581:                             }
 5582:                             $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 5583:                         }
 5584:                         $datatable .= '</select></td>';
 5585:                     }
 5586:                     $datatable .= '</tr></table></fieldset>'.
 5587:                                   '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5588:                                   '<legend>'.&mt('Custom items sent on launch').'</legend>'.
 5589:                                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 5590:                                   '<tr><td></td><td>lms</td>'.
 5591:                                   '<td><input type="text" name="proctoring_customval_lms_'.$provider.'"'.
 5592:                                   ' value="Loncapa" disabled="disabled"/></td></tr>';
 5593:                     if ((ref($settings) eq 'HASH') && (ref($settings->{$provider}) eq 'HASH') &&
 5594:                         (ref($settings->{$provider}->{'custom'}) eq 'HASH')) {
 5595:                         my %custom = %{$settings->{$provider}->{'custom'}};
 5596:                         if (keys(%custom) > 0) {
 5597:                             foreach my $key (sort(keys(%custom))) {
 5598:                                 next if ($key eq 'lms');
 5599:                                 $datatable .= '<tr><td><span class="LC_nobreak">'.
 5600:                                               '<label><input type="checkbox" name="proctoring_customdel_'.$provider.'" value="'.
 5601:                                               $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 5602:                                               '<td><input type="text" name="proctoring_customval_'.$key.'_'.$provider.'"'.
 5603:                                               ' value="'.$custom{$key}.'" /></td></tr>';
 5604:                             }
 5605:                         }
 5606:                     }
 5607:                     $datatable .= '<tr><td><span class="LC_nobreak">'.
 5608:                                   '<label><input type="checkbox" name="proctoring_customadd" value="'.$provider.'" />'.
 5609:                                   &mt('Add more').'</label></span></td><td><input type="text" name="proctoring_custom_name_'.$provider.'" />'.
 5610:                                   '</td><td><input type="text" name="proctoring_custom_value_'.$provider.'" /></td></tr>'.
 5611:                                   '</table></fieldset></td></tr>'."\n";
 5612:                 }
 5613:                 $datatable .= '</td></tr>';
 5614:             }
 5615:             $itemcount ++;
 5616:         }
 5617:     }
 5618:     return $datatable;
 5619: }
 5620: 
 5621: sub proctoring_data {
 5622:     my $requserfields = {
 5623:                       proctorio => ['user'],
 5624:                       examity   => ['roles','user'],
 5625:                      };
 5626:     my $optuserfields = {
 5627:                         proctorio => ['fullname'],
 5628:                         examity   => ['fullname','firstname','lastname','email'],
 5629:                      };
 5630:     my $defaults = {
 5631:                   proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 5632:                                 'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 5633:                                 'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 5634:                                 'closetabs','onescreen','print','downloads','cache','rightclick',
 5635:                                 'reentry','calculator','whiteboard'],
 5636:                   examity   => ['display'],
 5637:                 };
 5638:     my $extended = { 
 5639:                   proctorio => {
 5640:                                  verifyid => ['verifyidauto','verifyidlive'],
 5641:                                  fullscreen => ['fullscreenlenient','fullscreenmoderate','fullscreensever'],
 5642:                                  tabslinks => ['notabs','linksonly'],
 5643:                                  reentry => ['noreentry','agentreentry'],
 5644:                                  calculator => ['calculatorbasic','calculatorsci'],
 5645:                                },
 5646:                   examity => {
 5647:                                display => {
 5648:                                            target      => ['iframe','tab','window'],
 5649:                                            width       => '',
 5650:                                            height      => '',
 5651:                                            linktext    => '',
 5652:                                            explanation => '',
 5653:                                           },
 5654:                              },
 5655:                 };
 5656:     my $crsconf = {
 5657:                  proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 5658:                                'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 5659:                                'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 5660:                                'closetabs','onescreen','print','downloads','cache','rightclick',
 5661:                                'reentry','calculator','whiteboard'],
 5662:                  examity => ['label','title','target','linktext','explanation','append'],
 5663:                };
 5664:     my $courseroles = ['cc','in','ta','ep','st'];
 5665:     my $ltiroles = ['Instructor','ContentDeveloper','TeachingAssistant','Learner'];
 5666:     return ($requserfields,$optuserfields,$defaults,$extended,$crsconf,$courseroles,$ltiroles);
 5667: }
 5668: 
 5669: sub proctoring_titles {
 5670:     my ($item) = @_;
 5671:     my (%common_lt,%custom_lt);
 5672:     %common_lt = &Apache::lonlocal::texthash (
 5673:                      'avai'      => 'Available?',
 5674:                      'base'      => 'Basic Settings',
 5675:                      'requ'      => 'User data required to be sent on launch',
 5676:                      'optu'      => 'User data optionally sent on launch',
 5677:                      'udsl'      => 'User data sent on launch',
 5678:                      'defa'      => 'Defaults for items configurable in course',
 5679:                      'sigmethod' => 'Signature Method',
 5680:                      'key'       => 'Key',
 5681:                      'lifetime'  => 'Nonce lifetime (s)',
 5682:                      'secret'    => 'Secret',
 5683:                      'icon'      => 'Icon',
 5684:                      'fullname'  => 'Full Name',
 5685:                      'visible'   => 'Visible input',
 5686:                      'username'  => 'username',
 5687:                      'user'      => 'User',
 5688:                  );
 5689:     if ($item eq 'proctorio') {
 5690:         %custom_lt = &Apache::lonlocal::texthash (
 5691:                          'version'        => 'OAuth version',
 5692:                          'url'            => 'API URL',
 5693:                          'uname:dom'      => 'username-domain',
 5694:         );
 5695:     } elsif ($item eq 'examity') {
 5696:         %custom_lt = &Apache::lonlocal::texthash (
 5697:                          'version'        => 'LTI Version',
 5698:                          'url'            => 'URL',
 5699:                          'uname:dom'      => 'username:domain',
 5700:                          'msgtype'        => 'Message Type',
 5701:                          'firstname'      => 'First Name',
 5702:                          'lastname'       => 'Last Name',
 5703:                          'email'          => 'E-mail',
 5704:                          'roles'          => 'Role',
 5705:                          'crstarget'      => 'Display target',
 5706:                          'crslabel'       => 'Course label',
 5707:                          'crstitle'       => 'Course title', 
 5708:                          'crslinktext'    => 'Link Text',
 5709:                          'crsexplanation' => 'Explanation',
 5710:                          'crsappend'      => 'Provider URL',
 5711:         );
 5712:     }
 5713:     my %lt = (%common_lt,%custom_lt);
 5714:     return %lt;
 5715: }
 5716: 
 5717: sub proctoring_fieldtitles {
 5718:     my ($item) = @_;
 5719:     if ($item eq 'proctorio') {
 5720:         return &Apache::lonlocal::texthash (
 5721:                   'recordvideo' => 'Record video',
 5722:                   'recordaudio' => 'Record audio',
 5723:                   'recordscreen' => 'Record screen',
 5724:                   'recordwebtraffic' => 'Record web traffic',
 5725:                   'recordroomstart' => 'Record room scan',
 5726:                   'verifyvideo' => 'Verify webcam',
 5727:                   'verifyaudio' => 'Verify microphone',
 5728:                   'verifydesktop' => 'Verify desktop recording',
 5729:                   'verifyid' => 'Photo ID verification',
 5730:                   'verifysignature' => 'Require signature',
 5731:                   'fullscreen' => 'Fullscreen',
 5732:                   'clipboard' => 'Disable copy/paste',
 5733:                   'tabslinks' => 'New tabs/windows',
 5734:                   'closetabs' => 'Close other tabs',
 5735:                   'onescreen' => 'Limit to single screen',
 5736:                   'print' => 'Disable Printing',
 5737:                   'downloads' => 'Disable Downloads',
 5738:                   'cache' => 'Empty cache after exam',
 5739:                   'rightclick' => 'Disable right click',
 5740:                   'reentry' => 'Re-entry to exam',
 5741:                   'calculator' => 'Onscreen calculator',
 5742:                   'whiteboard' => 'Onscreen whiteboard',
 5743:                   'verifyidauto' => 'Automated verification',
 5744:                   'verifyidlive' => 'Live agent verification',
 5745:                   'fullscreenlenient' => 'Forced, but can navigate away for up to 30s',
 5746:                   'fullscreenmoderate' => 'Forced, but can navigate away for up to 15s',
 5747:                   'fullscreensever' => 'Forced, navigation away ends exam',
 5748:                   'notabs' => 'Disaallowed',
 5749:                   'linksonly' => 'Allowed from links in exam',
 5750:                   'noreentry' => 'Disallowed',
 5751:                   'agentreentry' => 'Agent required for re-entry',
 5752:                   'calculatorbasic' => 'Basic',
 5753:                   'calculatorsci' => 'Scientific',
 5754:         );
 5755:     } elsif ($item eq 'examity') {
 5756:         return &Apache::lonlocal::texthash (
 5757:                 'target'         => 'Display target',   
 5758:                 'window'         => 'Window',
 5759:                 'tab'            => 'Tab',
 5760:                 'iframe'         => 'iFrame',
 5761:                 'height'         => 'Height (pixels)',
 5762:                 'width'          => 'Width (pixels)',
 5763:                 'linktext'       => 'Default Link Text',
 5764:                 'explanation'    => 'Default Explanation',
 5765:                 'append'         => 'Provider URL',
 5766:         );
 5767:     }
 5768: }
 5769: 
 5770: sub proctoring_providernames {
 5771:     return (
 5772:              proctorio => 'Proctorio',
 5773:              examity   => 'Examity',
 5774:            );
 5775: }
 5776: 
 5777: sub print_lti {
 5778:     my ($dom,$settings,$rowtotal) = @_;
 5779:     my $itemcount = 1;
 5780:     my $maxnum = 0;
 5781:     my $css_class;
 5782:     my %ordered;
 5783:     if (ref($settings) eq 'HASH') {
 5784:         foreach my $item (keys(%{$settings})) {
 5785:             if (ref($settings->{$item}) eq 'HASH') {
 5786:                 my $num = $settings->{$item}{'order'};
 5787:                 $ordered{$num} = $item;
 5788:             }
 5789:         }
 5790:     }
 5791:     my $maxnum = scalar(keys(%ordered));
 5792:     my $datatable;
 5793:     my %lt = &lti_names();
 5794:     if (keys(%ordered)) {
 5795:         my @items = sort { $a <=> $b } keys(%ordered);
 5796:         for (my $i=0; $i<@items; $i++) {
 5797:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5798:             my $item = $ordered{$items[$i]};
 5799:             my ($key,$secret,$lifetime,$consumer,$requser,$current);
 5800:             if (ref($settings->{$item}) eq 'HASH') {
 5801:                 $key = $settings->{$item}->{'key'};
 5802:                 $secret = $settings->{$item}->{'secret'};
 5803:                 $lifetime = $settings->{$item}->{'lifetime'};
 5804:                 $consumer = $settings->{$item}->{'consumer'};
 5805:                 $requser = $settings->{$item}->{'requser'};
 5806:                 $current = $settings->{$item};
 5807:             }
 5808:             my $onclickrequser = ' onclick="toggleLTI(this.form,'."'requser','$i'".');"';
 5809:             my %checkedrequser = (
 5810:                                    yes => ' checked="checked"',
 5811:                                    no  => '',
 5812:                                  );
 5813:             if (!$requser) {
 5814:                 $checkedrequser{'no'} = $checkedrequser{'yes'};
 5815:                 $checkedrequser{'yes'} = '';
 5816:             }
 5817:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_".$item."'".');"';
 5818:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5819:                          .'<select name="lti_pos_'.$item.'"'.$chgstr.'>';
 5820:             for (my $k=0; $k<=$maxnum; $k++) {
 5821:                 my $vpos = $k+1;
 5822:                 my $selstr;
 5823:                 if ($k == $i) {
 5824:                     $selstr = ' selected="selected" ';
 5825:                 }
 5826:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5827:             }
 5828:             $datatable .= '</select>'.('&nbsp;'x2).
 5829:                 '<label><input type="checkbox" name="lti_del" value="'.$item.'" />'.
 5830:                 &mt('Delete?').'</label></span></td>'.
 5831:                 '<td colspan="2">'.
 5832:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 5833:                 '<span class="LC_nobreak">'.$lt{'consumer'}.
 5834:                 ':<input type="text" size="15" name="lti_consumer_'.$i.'" value="'.$consumer.'" /></span> '.
 5835:                 ('&nbsp;'x2).
 5836:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_'.$i.'">'.
 5837:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 5838:                 ('&nbsp;'x2).
 5839:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" name="lti_lifetime_'.$i.'"'.
 5840:                 'value="'.$lifetime.'" size="3" /></span>'.
 5841:                 ('&nbsp;'x2).
 5842:                  '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 5843:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="1"'.$onclickrequser.$checkedrequser{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 5844:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="0"'.$onclickrequser.$checkedrequser{no}.' />'.&mt('No').'</label></span>'."\n".
 5845:                 '<br /><br />'.
 5846:                 '<span class="LC_nobreak">'.$lt{'key'}.
 5847:                 ':<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" /></span> '.
 5848:                 ('&nbsp;'x2).
 5849:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 5850:                 '<input type="password" size="20" name="lti_secret_'.$i.'" value="'.$secret.'" />'.
 5851:                 '<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>'.
 5852:                 '<input type="hidden" name="lti_id_'.$i.'" value="'.$item.'" /></span>'.
 5853:                 '</fieldset>'.&lti_options($i,$current,$itemcount,%lt).'</td></tr>';
 5854:             $itemcount ++;
 5855:         }
 5856:     }
 5857:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5858:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_add'".');"';
 5859:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 5860:                   '<input type="hidden" name="lti_maxnum" value="'.$maxnum.'" />'."\n".
 5861:                   '<select name="lti_pos_add"'.$chgstr.'>';
 5862:     for (my $k=0; $k<$maxnum+1; $k++) {
 5863:         my $vpos = $k+1;
 5864:         my $selstr;
 5865:         if ($k == $maxnum) {
 5866:             $selstr = ' selected="selected" ';
 5867:         }
 5868:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5869:     }
 5870:     $datatable .= '</select>&nbsp;'."\n".
 5871:                   '<input type="checkbox" name="lti_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 5872:                   '<td colspan="2">'.
 5873:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 5874:                   '<span class="LC_nobreak">'.$lt{'consumer'}.
 5875:                   ':<input type="text" size="15" name="lti_consumer_add" value="" /></span> '."\n".
 5876:                   ('&nbsp;'x2).
 5877:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_add">'.
 5878:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 5879:                   ('&nbsp;'x2).
 5880:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="3" name="lti_lifetime_add" value="300" /></span> '."\n".
 5881:                   ('&nbsp;'x2).
 5882:                   '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 5883:                   '<label><input type="radio" name="lti_requser_add" value="1" onclick="toggleLTI(this.form,'."'requser','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 5884:                   '<label><input type="radio" name="lti_requser_add" value="0" onclick="toggleLTI(this.form,'."'requser','add'".');" />'.&mt('No').'</label></span>'."\n".
 5885:                   '<br /><br />'.
 5886:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="lti_key_add" value="" /></span> '."\n".
 5887:                   ('&nbsp;'x2).
 5888:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="lti_secret_add" value="" />'.
 5889:                   '<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".
 5890:                   '</fieldset>'.&lti_options('add',undef,$itemcount,%lt).
 5891:                   '</td>'."\n".
 5892:                   '</tr>'."\n";
 5893:     $$rowtotal ++;
 5894:     return $datatable;;
 5895: }
 5896: 
 5897: sub lti_names {
 5898:     my %lt = &Apache::lonlocal::texthash(
 5899:                                           'version'   => 'LTI Version',
 5900:                                           'url'       => 'URL',
 5901:                                           'key'       => 'Key',
 5902:                                           'lifetime'  => 'Nonce lifetime (s)',
 5903:                                           'consumer'  => 'Consumer',
 5904:                                           'secret'    => 'Secret',
 5905:                                           'requser'   => "User's identity sent",
 5906:                                           'email'     => 'Email address',
 5907:                                           'sourcedid' => 'User ID',
 5908:                                           'other'     => 'Other',
 5909:                                           'passback'  => 'Can return grades to Consumer:',
 5910:                                           'roster'    => 'Can retrieve roster from Consumer:',
 5911:                                           'topmenu'   => 'Display LON-CAPA page header',
 5912:                                           'inlinemenu'=> 'Display LON-CAPA inline menu',
 5913:                                         );
 5914:     return %lt;
 5915: }
 5916: 
 5917: sub lti_options {
 5918:     my ($num,$current,$itemcount,%lt) = @_;
 5919:     my (%checked,%rolemaps,$crssecsrc,$userfield,$cidfield,$callback);
 5920:     $checked{'mapuser'}{'sourcedid'} = ' checked="checked"';
 5921:     $checked{'mapcrs'}{'course_offering_sourcedid'} = ' checked="checked"';
 5922:     $checked{'makecrs'}{'N'} = '  checked="checked"';
 5923:     $checked{'mapcrstype'} = {};
 5924:     $checked{'makeuser'} = {};
 5925:     $checked{'selfenroll'} = {};
 5926:     $checked{'crssec'} = {};
 5927:     $checked{'crssecsrc'} = {};
 5928:     $checked{'lcauth'} = {};
 5929:     $checked{'menuitem'} = {};
 5930:     if ($num eq 'add') {
 5931:         $checked{'lcauth'}{'lti'} = ' checked="checked"';
 5932:     }
 5933:     my $userfieldsty = 'none';
 5934:     my $crsfieldsty = 'none';
 5935:     my $crssecfieldsty = 'none';
 5936:     my $secsrcfieldsty = 'none';
 5937:     my $callbacksty = 'none';
 5938:     my $passbacksty = 'none';
 5939:     my $optionsty = 'block';
 5940:     my $lcauthparm;
 5941:     my $lcauthparmstyle = 'display:none';
 5942:     my $lcauthparmtext;
 5943:     my $menusty;
 5944:     my $numinrow = 4;
 5945:     my %menutitles = &ltimenu_titles();
 5946: 
 5947:     if (ref($current) eq 'HASH') {
 5948:         if (!$current->{'requser'}) {
 5949:             $optionsty = 'none';
 5950:         }
 5951:         if (($current->{'mapuser'} ne '') && ($current->{'mapuser'} ne 'lis_person_sourcedid')) {
 5952:             $checked{'mapuser'}{'sourcedid'} = '';
 5953:             if ($current->{'mapuser'} eq 'lis_person_contact_email_primary') {
 5954:                 $checked{'mapuser'}{'email'} = ' checked="checked"'; 
 5955:             } else {
 5956:                 $checked{'mapuser'}{'other'} = ' checked="checked"';
 5957:                 $userfield = $current->{'mapuser'};
 5958:                 $userfieldsty = 'inline-block';
 5959:             }
 5960:         }
 5961:         if (($current->{'mapcrs'} ne '') && ($current->{'mapcrs'} ne 'course_offering_sourcedid')) {
 5962:             $checked{'mapcrs'}{'course_offering_sourcedid'} = '';
 5963:             if ($current->{'mapcrs'} eq 'context_id') {
 5964:                 $checked{'mapcrs'}{'context_id'} = ' checked="checked"'; 
 5965:             } else {
 5966:                 $checked{'mapcrs'}{'other'} = ' checked="checked"';
 5967:                 $cidfield = $current->{'mapcrs'};
 5968:                 $crsfieldsty = 'inline-block';
 5969:             }
 5970:         }
 5971:         if (ref($current->{'mapcrstype'}) eq 'ARRAY') {
 5972:             foreach my $type (@{$current->{'mapcrstype'}}) {
 5973:                 $checked{'mapcrstype'}{$type} = ' checked="checked"';
 5974:             }
 5975:         }
 5976:         if ($current->{'makecrs'}) {
 5977:             $checked{'makecrs'}{'Y'} = '  checked="checked"';
 5978:         }
 5979:         if (ref($current->{'makeuser'}) eq 'ARRAY') {
 5980:             foreach my $role (@{$current->{'makeuser'}}) {
 5981:                 $checked{'makeuser'}{$role} = ' checked="checked"';
 5982:             }
 5983:         }
 5984:         if ($current->{'lcauth'} =~ /^(internal|localauth|krb4|krb5|lti)$/) {
 5985:             $checked{'lcauth'}{$1} = ' checked="checked"';
 5986:             unless (($current->{'lcauth'} eq 'lti') || ($current->{'lcauth'} eq 'internal')) {
 5987:                 $lcauthparm = $current->{'lcauthparm'};
 5988:                 $lcauthparmstyle = 'display:table-row'; 
 5989:                 if ($current->{'lcauth'} eq 'localauth') {
 5990:                     $lcauthparmtext = &mt('Local auth argument');
 5991:                 } else {
 5992:                     $lcauthparmtext = &mt('Kerberos domain');
 5993:                 }
 5994:             }
 5995:         }
 5996:         if (ref($current->{'selfenroll'}) eq 'ARRAY') {
 5997:             foreach my $role (@{$current->{'selfenroll'}}) {
 5998:                 $checked{'selfenroll'}{$role} = ' checked="checked"';
 5999:             }
 6000:         }
 6001:         if (ref($current->{'maproles'}) eq 'HASH') {
 6002:             %rolemaps = %{$current->{'maproles'}};
 6003:         }
 6004:         if ($current->{'section'} ne '') {
 6005:             $checked{'crssec'}{'Y'} = '  checked="checked"'; 
 6006:             $crssecfieldsty = 'inline-block';
 6007:             if ($current->{'section'} eq 'course_section_sourcedid') {
 6008:                 $checked{'crssecsrc'}{'sourcedid'} = ' checked="checked"';
 6009:             } else {
 6010:                 $checked{'crssecsrc'}{'other'} = ' checked="checked"';
 6011:                 $crssecsrc = $current->{'section'};
 6012:                 $secsrcfieldsty = 'inline-block';
 6013:             }
 6014:         } else {
 6015:             $checked{'crssec'}{'N'} = ' checked="checked"';
 6016:         }
 6017:         if ($current->{'callback'} ne '') {
 6018:             $callback = $current->{'callback'};
 6019:             $checked{'callback'}{'Y'} = ' checked="checked"';
 6020:             $callbacksty = 'inline-block';
 6021:         } else {
 6022:             $checked{'callback'}{'N'} = ' checked="checked"';
 6023:         }
 6024:         if ($current->{'topmenu'}) {
 6025:             $checked{'topmenu'}{'Y'} = ' checked="checked"';
 6026:         } else {
 6027:             $checked{'topmenu'}{'N'} = ' checked="checked"';
 6028:         }
 6029:         if ($current->{'inlinemenu'}) {
 6030:             $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 6031:         } else {
 6032:             $checked{'inlinemenu'}{'N'} = ' checked="checked"';
 6033:         }
 6034:         if (($current->{'topmenu'}) || ($current->{'inlinemenu'})) {
 6035:             $menusty = 'inline-block';
 6036:             if (ref($current->{'lcmenu'}) eq 'ARRAY') {
 6037:                 foreach my $item (@{$current->{'lcmenu'}}) {
 6038:                     if (exists($menutitles{$item})) {
 6039:                         $checked{'menuitem'}{$item} = ' checked="checked"';
 6040:                     }
 6041:                 }
 6042:             }
 6043:         } else {
 6044:             $menusty = 'none';
 6045:         }
 6046:     } else {
 6047:         $checked{'makecrs'}{'N'} = ' checked="checked"';
 6048:         $checked{'crssec'}{'N'} = ' checked="checked"';
 6049:         $checked{'callback'}{'N'} = ' checked="checked"';
 6050:         $checked{'topmenu'}{'N'} = ' checked="checked"';
 6051:         $checked{'inlinemenu'}{'Y'} = ' checked="checked"'; 
 6052:         $checked{'menuitem'}{'grades'} = ' checked="checked"';
 6053:         $menusty = 'inline-block'; 
 6054:     }
 6055:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
 6056:     my %coursetypetitles = &Apache::lonlocal::texthash (
 6057:                                official   => 'Official',
 6058:                                unofficial => 'Unofficial',
 6059:                                community  => 'Community',
 6060:                                textbook   => 'Textbook',
 6061:                                placement  => 'Placement Test',
 6062:                                lti        => 'LTI Provider',
 6063:     );
 6064:     my @authtypes = ('internal','krb4','krb5','localauth');
 6065:     my %shortauth = (
 6066:                      internal => 'int',
 6067:                      krb4 => 'krb4',
 6068:                      krb5 => 'krb5',
 6069:                      localauth  => 'loc'
 6070:                     );
 6071:     my %authnames = &authtype_names();
 6072:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
 6073:     my @lticourseroles = qw(Learner Instructor TeachingAssistant Mentor);
 6074:     my @courseroles = ('cc','in','ta','ep','st');
 6075:     my $onclickuser = ' onclick="toggleLTI(this.form,'."'user','$num'".');"';
 6076:     my $onclickcrs = ' onclick="toggleLTI(this.form,'."'crs','$num'".');"';
 6077:     my $onclicksec = ' onclick="toggleLTI(this.form,'."'sec','$num'".');"';
 6078:     my $onclickcallback = ' onclick="toggleLTI(this.form,'."'callback','$num'".');"';
 6079:     my $onclicksecsrc = ' onclick="toggleLTI(this.form,'."'secsrc','$num'".')"';
 6080:     my $onclicklcauth = ' onclick="toggleLTI(this.form,'."'lcauth','$num'".')"';
 6081:     my $onclickmenu = ' onclick="toggleLTI(this.form,'."'lcmenu','$num'".');"';
 6082:     my $output = '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping users').'</legend>'.
 6083:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('LON-CAPA username').':&nbsp;';
 6084:     foreach my $option ('sourcedid','email','other') {
 6085:         $output .= '<label><input type="radio" name="lti_mapuser_'.$num.'" value="'.$option.'"'.
 6086:                    $checked{'mapuser'}{$option}.$onclickuser.' />'.$lt{$option}.'</label>'.
 6087:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6088:     }
 6089:     $output .= '</span></div>'.
 6090:                '<div class="LC_floatleft" style="display:'.$userfieldsty.';" id="lti_userfield_'.$num.'">'.
 6091:                '<input type="text" name="lti_customuser_'.$num.'" '.
 6092:                'value="'.$userfield.'" /></div></fieldset>'.
 6093:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping course roles').'</legend><table><tr>';
 6094:     foreach my $ltirole (@lticourseroles) {
 6095:         my ($selected,$selectnone);
 6096:         if ($rolemaps{$ltirole} eq '') {
 6097:             $selectnone = ' selected="selected"';
 6098:         }
 6099:         $output .= '<td style="text-align: center">'.$ltirole.'<br />'.
 6100:                    '<select name="lti_maprole_'.$ltirole.'_'.$num.'">'.
 6101:                    '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 6102:         foreach my $role (@courseroles) {
 6103:             unless ($selectnone) {
 6104:                 if ($rolemaps{$ltirole} eq $role) {
 6105:                     $selected = ' selected="selected"';
 6106:                 } else {
 6107:                     $selected = '';
 6108:                 }
 6109:             }
 6110:             $output .= '<option value="'.$role.'"'.$selected.'>'.
 6111:                        &Apache::lonnet::plaintext($role,'Course').
 6112:                        '</option>';
 6113:         }
 6114:         $output .= '</select></td>';
 6115:     }
 6116:     $output .= '</tr></table></fieldset>'.
 6117:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may create user accounts').'</legend>';
 6118:     foreach my $ltirole (@ltiroles) {
 6119:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_makeuser_'.$num.'" value="'.$ltirole.'"'.
 6120:                    $checked{'makeuser'}{$ltirole}.' />'.$ltirole.'</label>&nbsp;</span> ';     
 6121:     }
 6122:     $output .= '</fieldset>'.
 6123:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('New user accounts created for LTI users').'</legend>'.
 6124:                '<table>'.
 6125:                &modifiable_userdata_row('lti','instdata_'.$num,$current,$numinrow,$itemcount).
 6126:                '</table>'.
 6127:                '<table class="LC_nested"><tr><td class="LC_left_item">LON-CAPA Authentication</td>'.
 6128:                '<td class="LC_left_item">';
 6129:     foreach my $auth ('lti',@authtypes) {
 6130:         my $authtext;
 6131:         if ($auth eq 'lti') {
 6132:             $authtext = &mt('None');
 6133:         } else {
 6134:             $authtext = $authnames{$shortauth{$auth}};
 6135:         }
 6136:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="lti_lcauth_'.$num.
 6137:                    '" value="'.$auth.'"'.$checked{'lcauth'}{$auth}.$onclicklcauth.' />'.
 6138:                    $authtext.'</label></span> &nbsp;';
 6139:     }
 6140:     $output .= '</td></tr>'.
 6141:                '<tr id="lti_lcauth_parmrow_'.$num.'" style="'.$lcauthparmstyle.'">'.
 6142:                '<td class="LC_right_item" colspan="2"><span class="LC_nobreak">'.
 6143:                '<span id="lti_lcauth_parmtext_'.$num.'">'.$lcauthparmtext.'</span>'.
 6144:                '<input type="text" name="lti_lcauthparm_'.$num.'" value="" /></span></td></tr>'.
 6145:                '</table></fieldset>'.
 6146:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping courses').'</legend>'.
 6147:                '<div class="LC_floatleft"><span class="LC_nobreak">'.
 6148:                &mt('Unique course identifier').':&nbsp;';
 6149:     foreach my $option ('course_offering_sourcedid','context_id','other') {
 6150:         $output .= '<label><input type="radio" name="lti_mapcrs_'.$num.'" value="'.$option.'"'.
 6151:                    $checked{'mapcrs'}{$option}.$onclickcrs.' />'.$option.'</label>'.
 6152:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6153:     }
 6154:     $output .= '</span></div><div class="LC_floatleft" style="display:'.$crsfieldsty.';" id="lti_crsfield_'.$num.'">'.
 6155:                '<input type="text" name="lti_mapcrsfield_'.$num.'" value="'.$cidfield.'" />'.
 6156:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6157:                '<span class="LC_nobreak">'.&mt('LON-CAPA course type(s)').':&nbsp;';
 6158:     foreach my $type (@coursetypes) {
 6159:         $output .= '<label><input type="checkbox" name="lti_mapcrstype_'.$num.'" value="'.$type.'"'.
 6160:                    $checked{'mapcrstype'}{$type}.' />'.$coursetypetitles{$type}.'</label>'.
 6161:                    ('&nbsp;'x2);
 6162:     }
 6163:     $output .= '</span></fieldset>'.
 6164:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Creating courses').'</legend>'.
 6165:                '<span class="LC_nobreak">'.&mt('Course created (if absent) on Instructor access').':&nbsp;'.
 6166:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="0"'.
 6167:                $checked{'makecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6168:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="1"'.
 6169:                $checked{'makecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 6170:                '</fieldset>'.
 6171:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may self-enroll').'</legend>';
 6172:     foreach my $lticrsrole (@lticourseroles) {
 6173:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_selfenroll_'.$num.'" value="'.$lticrsrole.'"'.
 6174:                    $checked{'selfenroll'}{$lticrsrole}.' />'.$lticrsrole.'</label>&nbsp;</span> ';
 6175:     }
 6176:     $output .= '</fieldset>'.
 6177:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Course options').'</legend>'.
 6178:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Assign users to sections').':&nbsp;'.
 6179:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="0"'.
 6180:                $checked{'crssec'}{'N'}.$onclicksec.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6181:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="1"'.
 6182:                $checked{'crssec'}{'Y'}.$onclicksec.' />'.&mt('Yes').'</label></span></div>'.
 6183:                '<div class="LC_floatleft" style="display:'.$crssecfieldsty.';" id="lti_crssecfield_'.$num.'">'.
 6184:                '<span class="LC_nobreak">'.&mt('From').':<label>'.
 6185:                '<input type="radio" name="lti_crssecsrc_'.$num.'" value="course_section_sourcedid"'.
 6186:                $checked{'crssecsrc'}{'sourcedid'}.$onclicksecsrc.' />'.
 6187:                &mt('Standard field').'</label>'.('&nbsp;'x2).
 6188:                '<label><input type="radio" name="lti_crssecsrc_'.$num.'" value="other"'.
 6189:                $checked{'crssecsrc'}{'other'}.$onclicksecsrc.' />'.&mt('Other').
 6190:                '</label></span></div><div class="LC_floatleft" style="display:'.$secsrcfieldsty.';" id="lti_secsrcfield_'.$num.'">'.
 6191:                '<input type="text" name="lti_customsection_'.$num.'" value="'.$crssecsrc.'" />'.
 6192:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 6193:     my ($pb1p1chk,$pb1p0chk,$onclickpb);
 6194:     foreach my $extra ('roster','passback') {
 6195:         my $checkedon = '';
 6196:         my $checkedoff = ' checked="checked"';
 6197:         if ($extra eq 'passback') {
 6198:             $pb1p1chk = ' checked="checked"';
 6199:             $pb1p0chk = '';
 6200:             $onclickpb = ' onclick="toggleLTI(this.form,'."'passback','$num'".');"'; 
 6201:         } else {
 6202:             $onclickpb = ''; 
 6203:         }
 6204:         if (ref($current) eq 'HASH') {
 6205:             if (($current->{$extra})) {
 6206:                 $checkedon = $checkedoff;
 6207:                 $checkedoff = '';
 6208:                 if ($extra eq 'passback') {
 6209:                     $passbacksty = 'inline-block';
 6210:                 }
 6211:                 if ($current->{'passbackformat'} eq '1.0') {
 6212:                     $pb1p0chk =  ' checked="checked"';
 6213:                     $pb1p1chk = '';
 6214:                 }
 6215:             }
 6216:         }
 6217:         $output .= $lt{$extra}.'&nbsp;'.
 6218:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="0"'.$checkedoff.$onclickpb.' />'.
 6219:                    &mt('No').'</label>'.('&nbsp;'x2).
 6220:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="1"'.$checkedon.$onclickpb.' />'.
 6221:                    &mt('Yes').'</label><br />';
 6222:     }
 6223:     $output .= '<div class="LC_floatleft" style="display:'.$passbacksty.';" id="lti_passback_'.$num.'">'.
 6224:                '<span class="LC_nobreak">'.&mt('Grade format').
 6225:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.1"'.$pb1p1chk.' />'.
 6226:                &mt('Outcomes Service (1.1)').'</label>'.('&nbsp;'x2).
 6227:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.0"'.$pb1p0chk.'/>'.
 6228:                &mt('Outcomes Extension (1.0)').'</label></span></div>'.
 6229:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6230:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Callback on logout').':&nbsp;'.
 6231:                '<label><input type="radio" name="lti_callback_'.$num.'" value="0"'.
 6232:                $checked{'callback'}{'N'}.$onclickcallback.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6233:                '<label><input type="radio" name="lti_callback_'.$num.'" value="1"'.
 6234:                $checked{'callback'}{'Y'}.$onclickcallback.' />'.&mt('Yes').'</label></span></div>'.
 6235:                '<div class="LC_floatleft" style="display:'.$callbacksty.';" id="lti_callbackfield_'.$num.'">'.
 6236:                '<span class="LC_nobreak">'.&mt('Parameter').': '.
 6237:                '<input type="text" name="lti_callbackparam_'.$num.'" value="'.$callback.'" /></span>'.
 6238:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>'.
 6239:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Course defaults (Course Coordinator can override)').'</legend>'.
 6240:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'topmenu'}.':&nbsp;'.
 6241:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="0"'.
 6242:                $checked{'topmenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6243:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="1"'.
 6244:                $checked{'topmenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>'.
 6245:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6246:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'inlinemenu'}.':&nbsp;'.
 6247:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="0"'.
 6248:                $checked{'inlinemenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6249:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="1"'.
 6250:                $checked{'inlinemenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>';
 6251:      $output .='<div style="padding:0;clear:both;margin:0;border:0"></div>'. 
 6252:                '<div class="LC_floatleft" style="display:'.$menusty.';" id="lti_menufield_'.$num.'">'.
 6253:                '<span class="LC_nobreak">'.&mt('Menu items').':&nbsp;';
 6254:     foreach my $type ('fullname','coursetitle','role','logout','grades') {
 6255:         $output .= '<label><input type="checkbox" name="lti_menuitem_'.$num.'" value="'.$type.'"'.
 6256:                    $checked{'menuitem'}{$type}.' />'.$menutitles{$type}.'</label>'.
 6257:                    ('&nbsp;'x2);
 6258:     }
 6259:     $output .= '</span></div></fieldset>';
 6260: #        '<fieldset><legend>'.&mt('Assigning author roles').'</legend>';
 6261: #
 6262: #    $output .= '</fieldset>'.
 6263: #        '<fieldset><legend>'.&mt('Assigning domain roles').'</legend>';
 6264:     return $output;
 6265: }
 6266: 
 6267: sub ltimenu_titles {
 6268:     return &Apache::lonlocal::texthash(
 6269:                                         fullname    => 'Full name',
 6270:                                         coursetitle => 'Course title',
 6271:                                         role        => 'Role',
 6272:                                         logout      => 'Logout',
 6273:                                         grades      => 'Grades',
 6274:     );
 6275: }
 6276: 
 6277: sub print_coursedefaults {
 6278:     my ($position,$dom,$settings,$rowtotal) = @_;
 6279:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 6280:     my $itemcount = 1;
 6281:     my %choices =  &Apache::lonlocal::texthash (
 6282:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 6283:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 6284:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 6285:         coursecredits        => 'Credits can be specified for courses',
 6286:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 6287:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 6288:         texengine            => 'Default method to display mathematics',
 6289:         postsubmit           => 'Disable submit button/keypress following student submission',
 6290:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 6291:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 6292:     );
 6293:     my %staticdefaults = (
 6294:                            anonsurvey_threshold => 10,
 6295:                            uploadquota          => 500,
 6296:                            postsubmit           => 60,
 6297:                            mysqltables          => 172800,
 6298:                          );
 6299:     if ($position eq 'top') {
 6300:         %defaultchecked = (
 6301:                             'canuse_pdfforms' => 'off',
 6302:                             'uselcmath'       => 'on',
 6303:                             'usejsme'         => 'on',
 6304:                             'canclone'        => 'none',
 6305:                           );
 6306:         @toggles = ('canuse_pdfforms','uselcmath','usejsme');
 6307:         my $deftex = $Apache::lonnet::deftex;
 6308:         if (ref($settings) eq 'HASH') {
 6309:             if ($settings->{'texengine'}) {
 6310:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 6311:                     $deftex = $settings->{'texengine'};
 6312:                 }
 6313:             }
 6314:         }
 6315:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6316:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 6317:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 6318:                        '</span></td><td class="LC_right_item">'.
 6319:                        '<select name="texengine">'."\n";
 6320:         my %texoptions = (
 6321:                             MathJax  => 'MathJax',
 6322:                             mimetex  => &mt('Convert to Images'),
 6323:                             tth      => &mt('TeX to HTML'),
 6324:                          );
 6325:         foreach my $renderer ('MathJax','mimetex','tth') {
 6326:             my $selected = '';
 6327:             if ($renderer eq $deftex) {
 6328:                 $selected = ' selected="selected"';
 6329:             }
 6330:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 6331:         }
 6332:         $mathdisp .= '</select></td></tr>'."\n";
 6333:         $itemcount ++;
 6334:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 6335:                                                      \%choices,$itemcount);
 6336:         $datatable = $mathdisp.$datatable;
 6337:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6338:         $datatable .=
 6339:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 6340:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 6341:             '</span></td><td class="LC_left_item">';
 6342:         my $currcanclone = 'none';
 6343:         my $onclick;
 6344:         my @cloneoptions = ('none','domain');
 6345:         my %clonetitles = &Apache::lonlocal::texthash (
 6346:                              none     => 'No additional course requesters',
 6347:                              domain   => "Any course requester in course's domain",
 6348:                              instcode => 'Course requests for official courses ...',
 6349:                           );
 6350:         my (%codedefaults,@code_order,@posscodes);
 6351:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 6352:                                                     \@code_order) eq 'ok') {
 6353:             if (@code_order > 0) {
 6354:                 push(@cloneoptions,'instcode');
 6355:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 6356:             }
 6357:         }
 6358:         if (ref($settings) eq 'HASH') {
 6359:             if ($settings->{'canclone'}) {
 6360:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 6361:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 6362:                         if (@code_order > 0) {
 6363:                             $currcanclone = 'instcode';
 6364:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 6365:                         }
 6366:                     }
 6367:                 } elsif ($settings->{'canclone'} eq 'domain') {
 6368:                     $currcanclone = $settings->{'canclone'};
 6369:                 }
 6370:             }
 6371:         }
 6372:         foreach my $option (@cloneoptions) {
 6373:             my ($checked,$additional);
 6374:             if ($currcanclone eq $option) {
 6375:                 $checked = ' checked="checked"';
 6376:             }
 6377:             if ($option eq 'instcode') {
 6378:                 if (@code_order) {
 6379:                     my $show = 'none';
 6380:                     if ($checked) {
 6381:                         $show = 'block';
 6382:                     }
 6383:                     $additional = '<div id="cloneinstcode" style="display:'.$show.';" />'.
 6384:                                   &mt('Institutional codes for new and cloned course have identical:').
 6385:                                   '<br />';
 6386:                     foreach my $item (@code_order) {
 6387:                         my $codechk;
 6388:                         if ($checked) {
 6389:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 6390:                                 $codechk = ' checked="checked"';
 6391:                             }
 6392:                         }
 6393:                         $additional .= '<label>'.
 6394:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 6395:                                        $item.'</label>';
 6396:                     }
 6397:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 6398:                 }
 6399:             }
 6400:             $datatable .=
 6401:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 6402:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 6403:                 '</label>&nbsp;'.$additional.'</span><br />';
 6404:         }
 6405:         $datatable .= '</td>'.
 6406:                       '</tr>';
 6407:         $itemcount ++;
 6408:     } else {
 6409:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6410:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 6411:         my $currusecredits = 0;
 6412:         my $postsubmitclient = 1;
 6413:         my @types = ('official','unofficial','community','textbook','placement');
 6414:         if (ref($settings) eq 'HASH') {
 6415:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 6416:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 6417:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 6418:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 6419:                 }
 6420:             }
 6421:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 6422:                 foreach my $type (@types) {
 6423:                     next if ($type eq 'community');
 6424:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 6425:                     if ($defcredits{$type} ne '') {
 6426:                         $currusecredits = 1;
 6427:                     }
 6428:                 }
 6429:             }
 6430:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 6431:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 6432:                     $postsubmitclient = 0;
 6433:                     foreach my $type (@types) {
 6434:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6435:                     }
 6436:                 } else {
 6437:                     foreach my $type (@types) {
 6438:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 6439:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 6440:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 6441:                             } else {
 6442:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6443:                             }
 6444:                         } else {
 6445:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6446:                         }
 6447:                     }
 6448:                 }
 6449:             } else {
 6450:                 foreach my $type (@types) {
 6451:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6452:                 }
 6453:             }
 6454:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 6455:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 6456:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 6457:                 }
 6458:             } else {
 6459:                 foreach my $type (@types) {
 6460:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 6461:                 }
 6462:             }
 6463:         } else {
 6464:             foreach my $type (@types) {
 6465:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6466:             }
 6467:         }
 6468:         if (!$currdefresponder) {
 6469:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 6470:         } elsif ($currdefresponder < 1) {
 6471:             $currdefresponder = 1;
 6472:         }
 6473:         foreach my $type (@types) {
 6474:             if ($curruploadquota{$type} eq '') {
 6475:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 6476:             }
 6477:         }
 6478:         $datatable .=
 6479:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6480:                 $choices{'anonsurvey_threshold'}.
 6481:                 '</span></td>'.
 6482:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 6483:                 '<input type="text" name="anonsurvey_threshold"'.
 6484:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 6485:                 '</td></tr>'."\n";
 6486:         $itemcount ++;
 6487:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6488:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6489:                       $choices{'uploadquota'}.
 6490:                       '</span></td>'.
 6491:                       '<td style="text-align: right" class="LC_right_item">'.
 6492:                       '<table><tr>';
 6493:         foreach my $type (@types) {
 6494:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6495:                            '<input type="text" name="uploadquota_'.$type.'"'.
 6496:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 6497:         }
 6498:         $datatable .= '</tr></table></td></tr>'."\n";
 6499:         $itemcount ++;
 6500:         my $onclick = "toggleDisplay(this.form,'credits');";
 6501:         my $display = 'none';
 6502:         if ($currusecredits) {
 6503:             $display = 'block';
 6504:         }
 6505:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 6506:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 6507:         foreach my $type (@types) {
 6508:             next if ($type eq 'community');
 6509:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6510:                            '<input type="text" name="'.$type.'_credits"'.
 6511:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 6512:         }
 6513:         $additional .= '</tr></table></div>'."\n";
 6514:         %defaultchecked = ('coursecredits' => 'off');
 6515:         @toggles = ('coursecredits');
 6516:         my $current = {
 6517:                         'coursecredits' => $currusecredits,
 6518:                       };
 6519:         (my $table,$itemcount) =
 6520:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 6521:                                \%choices,$itemcount,$onclick,$additional,'left');
 6522:         $datatable .= $table;
 6523:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 6524:         my $display = 'none';
 6525:         if ($postsubmitclient) {
 6526:             $display = 'block';
 6527:         }
 6528:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 6529:                       &mt('Number of seconds submit is disabled').'<br />'.
 6530:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 6531:                       '<table><tr>';
 6532:         foreach my $type (@types) {
 6533:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6534:                            '<input type="text" name="'.$type.'_timeout" value="'.
 6535:                            $deftimeout{$type}.'" size="5" /></td>';
 6536:         }
 6537:         $additional .= '</tr></table></div>'."\n";
 6538:         %defaultchecked = ('postsubmit' => 'on');
 6539:         @toggles = ('postsubmit');
 6540:         $current = {
 6541:                        'postsubmit' => $postsubmitclient,
 6542:                    };
 6543:         ($table,$itemcount) =
 6544:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 6545:                                \%choices,$itemcount,$onclick,$additional,'left');
 6546:         $datatable .= $table;
 6547:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6548:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6549:                       $choices{'mysqltables'}.
 6550:                       '</span></td>'.
 6551:                       '<td style="text-align: right" class="LC_right_item">'.
 6552:                       '<table><tr>';
 6553:         foreach my $type (@types) {
 6554:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6555:                            '<input type="text" name="mysqltables_'.$type.'"'.
 6556:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 6557:         }
 6558:         $datatable .= '</tr></table></td></tr>'."\n";
 6559:         $itemcount ++;
 6560: 
 6561:     }
 6562:     $$rowtotal += $itemcount;
 6563:     return $datatable;
 6564: }
 6565: 
 6566: sub print_selfenrollment {
 6567:     my ($position,$dom,$settings,$rowtotal) = @_;
 6568:     my ($css_class,$datatable);
 6569:     my $itemcount = 1;
 6570:     my @types = ('official','unofficial','community','textbook','placement');
 6571:     if (($position eq 'top') || ($position eq 'middle')) {
 6572:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 6573:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 6574:         my @rows;
 6575:         my $key;
 6576:         if ($position eq 'top') {
 6577:             $key = 'admin'; 
 6578:             if (ref($rowsref) eq 'ARRAY') {
 6579:                 @rows = @{$rowsref};
 6580:             }
 6581:         } elsif ($position eq 'middle') {
 6582:             $key = 'default';
 6583:             @rows = ('types','registered','approval','limit');
 6584:         }
 6585:         foreach my $row (@rows) {
 6586:             if (defined($titlesref->{$row})) {
 6587:                 $itemcount ++;
 6588:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6589:                 $datatable .= '<tr'.$css_class.'>'.
 6590:                               '<td>'.$titlesref->{$row}.'</td>'.
 6591:                               '<td class="LC_left_item">'.
 6592:                               '<table><tr>';
 6593:                 my (%current,%currentcap);
 6594:                 if (ref($settings) eq 'HASH') {
 6595:                     if (ref($settings->{$key}) eq 'HASH') {
 6596:                         foreach my $type (@types) {
 6597:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 6598:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 6599:                             }
 6600:                             if (($row eq 'limit') && ($key eq 'default')) {
 6601:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 6602:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 6603:                                 }
 6604:                             }
 6605:                         }
 6606:                     }
 6607:                 }
 6608:                 my %roles = (
 6609:                              '0' => &Apache::lonnet::plaintext('dc'),
 6610:                             ); 
 6611:             
 6612:                 foreach my $type (@types) {
 6613:                     unless (($row eq 'registered') && ($key eq 'default')) {
 6614:                         $datatable .= '<th>'.&mt($type).'</th>';
 6615:                     }
 6616:                 }
 6617:                 unless (($row eq 'registered') && ($key eq 'default')) {
 6618:                     $datatable .= '</tr><tr>';
 6619:                 }
 6620:                 foreach my $type (@types) {
 6621:                     if ($type eq 'community') {
 6622:                         $roles{'1'} = &mt('Community personnel');
 6623:                     } else {
 6624:                         $roles{'1'} = &mt('Course personnel');
 6625:                     }
 6626:                     $datatable .= '<td style="vertical-align: top">';
 6627:                     if ($position eq 'top') {
 6628:                         my %checked;
 6629:                         if ($current{$type} eq '0') {
 6630:                             $checked{'0'} = ' checked="checked"';
 6631:                         } else {
 6632:                             $checked{'1'} = ' checked="checked"';
 6633:                         }
 6634:                         foreach my $role ('1','0') {
 6635:                             $datatable .= '<span class="LC_nobreak"><label>'.
 6636:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 6637:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 6638:                                           $roles{$role}.'</label></span> ';
 6639:                         }
 6640:                     } else {
 6641:                         if ($row eq 'types') {
 6642:                             my %checked;
 6643:                             if ($current{$type} =~ /^(all|dom)$/) {
 6644:                                 $checked{$1} = ' checked="checked"';
 6645:                             } else {
 6646:                                 $checked{''} = ' checked="checked"';
 6647:                             }
 6648:                             foreach my $val ('','dom','all') {
 6649:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6650:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6651:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6652:                             }
 6653:                         } elsif ($row eq 'registered') {
 6654:                             my %checked;
 6655:                             if ($current{$type} eq '1') {
 6656:                                 $checked{'1'} = ' checked="checked"';
 6657:                             } else {
 6658:                                 $checked{'0'} = ' checked="checked"';
 6659:                             }
 6660:                             foreach my $val ('0','1') {
 6661:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6662:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6663:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6664:                             }
 6665:                         } elsif ($row eq 'approval') {
 6666:                             my %checked;
 6667:                             if ($current{$type} =~ /^([12])$/) {
 6668:                                 $checked{$1} = ' checked="checked"';
 6669:                             } else {
 6670:                                 $checked{'0'} = ' checked="checked"';
 6671:                             }
 6672:                             for my $val (0..2) {
 6673:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6674:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6675:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6676:                             }
 6677:                         } elsif ($row eq 'limit') {
 6678:                             my %checked;
 6679:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 6680:                                 $checked{$1} = ' checked="checked"';
 6681:                             } else {
 6682:                                 $checked{'none'} = ' checked="checked"';
 6683:                             }
 6684:                             my $cap;
 6685:                             if ($currentcap{$type} =~ /^\d+$/) {
 6686:                                 $cap = $currentcap{$type};
 6687:                             }
 6688:                             foreach my $val ('none','allstudents','selfenrolled') {
 6689:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6690:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6691:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6692:                             }
 6693:                             $datatable .= '<br />'.
 6694:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 6695:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 6696:                                           '</span>'; 
 6697:                         }
 6698:                     }
 6699:                     $datatable .= '</td>';
 6700:                 }
 6701:                 $datatable .= '</tr>';
 6702:             }
 6703:             $datatable .= '</table></td></tr>';
 6704:         }
 6705:     } elsif ($position eq 'bottom') {
 6706:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 6707:     }
 6708:     $$rowtotal += $itemcount;
 6709:     return $datatable;
 6710: }
 6711: 
 6712: sub print_validation_rows {
 6713:     my ($caller,$dom,$settings,$rowtotal) = @_;
 6714:     my ($itemsref,$namesref,$fieldsref);
 6715:     if ($caller eq 'selfenroll') { 
 6716:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 6717:     } elsif ($caller eq 'requestcourses') {
 6718:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 6719:     }
 6720:     my %currvalidation;
 6721:     if (ref($settings) eq 'HASH') {
 6722:         if (ref($settings->{'validation'}) eq 'HASH') {
 6723:             %currvalidation = %{$settings->{'validation'}};
 6724:         }
 6725:     }
 6726:     my $datatable;
 6727:     my $itemcount = 0;
 6728:     foreach my $item (@{$itemsref}) {
 6729:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6730:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6731:                       $namesref->{$item}.
 6732:                       '</span></td>'.
 6733:                       '<td class="LC_left_item">';
 6734:         if (($item eq 'url') || ($item eq 'button')) {
 6735:             $datatable .= '<span class="LC_nobreak">'.
 6736:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 6737:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 6738:         } elsif ($item eq 'fields') {
 6739:             my @currfields;
 6740:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 6741:                 @currfields = @{$currvalidation{$item}};
 6742:             }
 6743:             foreach my $field (@{$fieldsref}) {
 6744:                 my $check = '';
 6745:                 if (grep(/^\Q$field\E$/,@currfields)) {
 6746:                     $check = ' checked="checked"';
 6747:                 }
 6748:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6749:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 6750:                               ' value="'.$field.'"'.$check.' />'.$field.
 6751:                               '</label></span> ';
 6752:             }
 6753:         } elsif ($item eq 'markup') {
 6754:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 6755:                            $currvalidation{$item}.
 6756:                               '</textarea>';
 6757:         }
 6758:         $datatable .= '</td></tr>'."\n";
 6759:         if (ref($rowtotal)) {
 6760:             $itemcount ++;
 6761:         }
 6762:     }
 6763:     if ($caller eq 'requestcourses') {
 6764:         my %currhash;
 6765:         if (ref($settings) eq 'HASH') {
 6766:             if (ref($settings->{'validation'}) eq 'HASH') {
 6767:                 if ($settings->{'validation'}{'dc'} ne '') {
 6768:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 6769:                 }
 6770:             }
 6771:         }
 6772:         my $numinrow = 2;
 6773:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 6774:                                                        'validationdc',%currhash);
 6775:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6776:         $datatable .= '<tr'.$css_class.'><td>';
 6777:         if ($numdc > 1) {
 6778:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 6779:         } else {
 6780:             $datatable .=  &mt('Course creation processed as: ');
 6781:         }
 6782:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 6783:         $itemcount ++;
 6784:     }
 6785:     if (ref($rowtotal)) {
 6786:         $$rowtotal += $itemcount;
 6787:     }
 6788:     return $datatable;
 6789: }
 6790: 
 6791: sub print_privacy {
 6792:     my ($position,$dom,$settings,$rowtotal) = @_;
 6793:     my ($datatable,$css_class,$numinrow,@items,%names,$othertitle,$usertypes,$types);
 6794:     my $itemcount = 0;
 6795:     unless ($position eq 'top') {
 6796:         @items = ('domain','author','course','community');
 6797:         %names = &Apache::lonlocal::texthash (
 6798:                      domain => 'Assigned domain role(s)',
 6799:                      author => 'Assigned co-author role(s)',
 6800:                      course => 'Assigned course role(s)',
 6801:                      community => 'Assigned community role',
 6802:                  );
 6803:         $numinrow = 4;
 6804:         ($othertitle,$usertypes,$types) =
 6805:             &Apache::loncommon::sorted_inst_types($dom);
 6806:     }
 6807:     if (($position eq 'top') || ($position eq 'middle')) {
 6808:         my (%by_ip,%by_location,@intdoms,@instdoms);
 6809:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 6810:         if ($position eq 'top') {
 6811:             my %curr;
 6812:             my @options = ('none','user','domain','auto');
 6813:             my %titles = &Apache::lonlocal::texthash (
 6814:                 none   => 'Not allowed',
 6815:                 user   => 'User authorizes',
 6816:                 domain => 'DC authorizes',
 6817:                 auto   => 'Unrestricted',
 6818:                 instdom => 'Other domain shares institution/provider',
 6819:                 extdom => 'Other domain has different institution/provider',
 6820:             );
 6821:             my %names = &Apache::lonlocal::texthash (
 6822:                 domain => 'Domain role',
 6823:                 author => 'Co-author role',
 6824:                 course => 'Course role',
 6825:                 community => 'Community role',
 6826:             );
 6827:             my $primary_id = &Apache::lonnet::domain($dom,'primary');
 6828:             my $intdom = &Apache::lonnet::internet_dom($primary_id);
 6829:             foreach my $domtype ('instdom','extdom') {
 6830:                 my (%checked,$skip);
 6831:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6832:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$domtype}.'</td>'.
 6833:                               '<td class="LC_left_item">';
 6834:                 if ($domtype eq 'instdom') {
 6835:                     unless (@instdoms > 1) {
 6836:                         $datatable .= &mt('Nothing to set, as no domains besides [_1] are hosted by [_2]',$dom,$intdom);
 6837:                         $skip = 1;
 6838:                     }
 6839:                 } elsif ($domtype eq 'extdom') {
 6840:                     if (keys(%by_location) == 0) {
 6841:                         $datatable .= &mt('Nothing to set, as no other hosts besides [_1]',$intdom);
 6842:                         $skip = 1;
 6843:                     }
 6844:                 }
 6845:                 unless ($skip) {
 6846:                     foreach my $roletype ('domain','author','course','community') {
 6847:                         $checked{'auto'} = ' checked="checked"';
 6848:                         if (ref($settings) eq 'HASH') {
 6849:                             if (ref($settings->{approval}) eq 'HASH') {
 6850:                                 if (ref($settings->{approval}->{$domtype}) eq 'HASH') {
 6851:                                     if ($settings->{approval}->{$domtype}->{$roletype}=~ /^(none|user|domain)$/) {
 6852:                                         $checked{$1} = ' checked="checked"';
 6853:                                         $checked{'auto'} = '';
 6854:                                     }
 6855:                                 }
 6856:                             }
 6857:                         }
 6858:                         $datatable .= '<fieldset><legend>'.$names{$roletype}.'</legend>';
 6859:                         foreach my $option (@options) {
 6860:                             $datatable .= '<span class="LC_nobreak"><label>'.
 6861:                                           '<input type="radio" name="privacy_approval_'.$domtype.'_'.$roletype.'" '.
 6862:                                           'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 6863:                                           '</label></span>&nbsp; ';
 6864:                         }
 6865:                         $datatable .= '</fieldset>';
 6866:                     }
 6867:                 }
 6868:                 $datatable .= '</td></tr>';
 6869:                 $itemcount ++;
 6870:             }
 6871:         } elsif ($position eq 'middle') {
 6872:             if ((@instdoms > 1) || (keys(%by_location) > 0)) {
 6873:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 6874:                     foreach my $item (@{$types}) {
 6875:                         $datatable .= &modifiable_userdata_row('privacy','othdom_'.$item,$settings,
 6876:                                                                $numinrow,$itemcount,'','','','','',
 6877:                                                                '',$usertypes->{$item});
 6878:                         $itemcount ++;
 6879:                     }
 6880:                 }
 6881:                 $datatable .= &modifiable_userdata_row('privacy','othdom_default',$settings,
 6882:                                                        $numinrow,$itemcount,'','','','','',
 6883:                                                        '',$othertitle);
 6884:                 $itemcount ++;
 6885:             } else {
 6886:                 my (@insttypes,%insttitles);
 6887:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 6888:                     @insttypes = @{$types};
 6889:                     %insttitles = %{$usertypes};
 6890:                 }
 6891:                 foreach my $item (@insttypes,'default') {
 6892:                     my $title;
 6893:                     if ($item eq 'default') {
 6894:                         $title = $othertitle;
 6895:                     } else {
 6896:                         $title = $insttitles{$item};
 6897:                     }
 6898:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6899:                     $datatable .= '<tr'.$css_class.'>'.
 6900:                                   '<td class="LC_left_item">'.$title.'</td>'.
 6901:                                   '<td class="LC_left_item">'.
 6902:                                   &mt('Nothing to set here, as there are no other domains').
 6903:                                   '</td></tr>';
 6904:                     $itemcount ++;
 6905:                 }
 6906:             }
 6907:         }
 6908:     } else {
 6909:         my $prefix;
 6910:         if ($position eq 'lower') {
 6911:             $prefix = 'priv';
 6912:         } else {
 6913:             $prefix = 'unpriv';
 6914:         }
 6915:         foreach my $item (@items) {
 6916:             $datatable .= &modifiable_userdata_row('privacy',$prefix.'_'.$item,$settings,
 6917:                                                    $numinrow,$itemcount,'','','','','',
 6918:                                                    '',$names{$item});
 6919:             $itemcount ++;
 6920:         }
 6921:     }
 6922:     if (ref($rowtotal)) {
 6923:         $$rowtotal += $itemcount;
 6924:     }
 6925:     return $datatable;
 6926: }
 6927: 
 6928: sub print_passwords {
 6929:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 6930:     my ($datatable,$css_class);
 6931:     my $itemcount = 0;
 6932:     my %titles = &Apache::lonlocal::texthash (
 6933:         captcha        => '"Forgot Password" CAPTCHA validation',
 6934:         link           => 'Reset link expiration (hours)',
 6935:         case           => 'Case-sensitive usernames/e-mail',
 6936:         prelink        => 'Information required (form 1)',
 6937:         postlink       => 'Information required (form 2)',
 6938:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 6939:         customtext     => 'Domain specific text (HTML)',
 6940:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 6941:         intauth_check  => 'Check bcrypt cost if authenticated',
 6942:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 6943:         permanent      => 'Permanent e-mail address',
 6944:         critical       => 'Critical notification address',
 6945:         notify         => 'Notification address',
 6946:         min            => 'Minimum password length',
 6947:         max            => 'Maximum password length',
 6948:         chars          => 'Required characters',
 6949:         expire         => 'Password expiration (days)',
 6950:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 6951:     );
 6952:     if ($position eq 'top') {
 6953:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6954:         my $shownlinklife = 2;
 6955:         my $prelink = 'both';
 6956:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 6957:         if (ref($settings) eq 'HASH') {
 6958:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 6959:                 $shownlinklife = $settings->{resetlink};
 6960:             }
 6961:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 6962:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 6963:             }
 6964:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 6965:                 $prelink = $settings->{resetprelink};
 6966:             }
 6967:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 6968:                 %postlink = %{$settings->{resetpostlink}};
 6969:             }
 6970:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 6971:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 6972:             }
 6973:             if ($settings->{resetremove}) {
 6974:                 $nostdtext = 1;
 6975:             }
 6976:             if ($settings->{resetcustom}) {
 6977:                 $customurl = $settings->{resetcustom};
 6978:             }
 6979:         } else {
 6980:             if (ref($types) eq 'ARRAY') {
 6981:                 foreach my $item (@{$types}) {
 6982:                     $casesens{$item} = 1;
 6983:                     $postlink{$item} = ['username','email'];
 6984:                 }
 6985:             }
 6986:             $casesens{'default'} = 1;
 6987:             $postlink{'default'} = ['username','email'];
 6988:             $prelink = 'both';
 6989:             %emailsrc = (
 6990:                           permanent => 1,
 6991:                           critical  => 1,
 6992:                           notify    => 1,
 6993:             );
 6994:         }
 6995:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 6996:         $itemcount ++;
 6997:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6998:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 6999:                       '<td class="LC_left_item">'.
 7000:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 7001:                       'name="passwords_link" size="3" /></td></tr>';
 7002:         $itemcount ++;
 7003:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7004:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 7005:                       '<td class="LC_left_item">';
 7006:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7007:             foreach my $item (@{$types}) {
 7008:                 my $checkedcase;
 7009:                 if ($casesens{$item}) {
 7010:                     $checkedcase = ' checked="checked"';
 7011:                 }
 7012:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7013:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 7014:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 7015:                               '</span>&nbsp;&nbsp; ';
 7016:             }
 7017:         }
 7018:         my $checkedcase;
 7019:         if ($casesens{'default'}) {
 7020:             $checkedcase = ' checked="checked"';
 7021:         }
 7022:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 7023:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 7024:                       $othertitle.'</label></span></td>';
 7025:         $itemcount ++;
 7026:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7027:         my %checkedpre = (
 7028:                              both => ' checked="checked"',
 7029:                              either => '',
 7030:                          );
 7031:         if ($prelink eq 'either') {
 7032:             $checkedpre{either} = ' checked="checked"';
 7033:             $checkedpre{both} = '';
 7034:         }
 7035:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 7036:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7037:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 7038:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 7039:                       '<span class="LC_nobreak"><label>'.
 7040:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 7041:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 7042:         $itemcount ++;
 7043:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7044:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 7045:                       '<td class="LC_left_item">';
 7046:         my %postlinked;
 7047:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7048:             foreach my $item (@{$types}) {
 7049:                 undef(%postlinked);
 7050:                 $datatable .= '<fieldset style="display: inline-block;">'.
 7051:                               '<legend>'.$usertypes->{$item}.'</legend>';
 7052:                 if (ref($postlink{$item}) eq 'ARRAY') {
 7053:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 7054:                 }
 7055:                 foreach my $field ('email','username') {
 7056:                     my $checked;
 7057:                     if ($postlinked{$field}) {
 7058:                         $checked = ' checked="checked"';
 7059:                     }
 7060:                     $datatable .= '<span class="LC_nobreak"><label>'.
 7061:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 7062:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 7063:                                   '<span>&nbsp;&nbsp; ';
 7064:                 }
 7065:                 $datatable .= '</fieldset>';
 7066:             }
 7067:         }
 7068:         if (ref($postlink{'default'}) eq 'ARRAY') {
 7069:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 7070:         }
 7071:         $datatable .= '<fieldset style="display: inline-block;">'.
 7072:                       '<legend>'.$othertitle.'</legend>';
 7073:         foreach my $field ('email','username') {
 7074:             my $checked;
 7075:             if ($postlinked{$field}) {
 7076:                 $checked = ' checked="checked"';
 7077:             }
 7078:             $datatable .= '<span class="LC_nobreak"><label>'.
 7079:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 7080:                           $field.'"'.$checked.' />'.$field.'</label>'.
 7081:                           '<span>&nbsp;&nbsp; ';
 7082:         }
 7083:         $datatable .= '</fieldset></td></tr>';
 7084:         $itemcount ++;
 7085:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7086:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 7087:                       '<td class="LC_left_item">';
 7088:         foreach my $type ('permanent','critical','notify') {
 7089:             my $checkedemail;
 7090:             if ($emailsrc{$type}) {
 7091:                 $checkedemail = ' checked="checked"';
 7092:             }
 7093:             $datatable .= '<span class="LC_nobreak"><label>'.
 7094:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 7095:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 7096:                           '<span>&nbsp;&nbsp; ';
 7097:         }
 7098:         $datatable .= '</td></tr>';
 7099:         $itemcount ++;
 7100:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7101:         my $switchserver = &check_switchserver($dom,$confname);
 7102:         my ($showstd,$noshowstd);
 7103:         if ($nostdtext) {
 7104:             $noshowstd = ' checked="checked"';
 7105:         } else {
 7106:             $showstd = ' checked="checked"';
 7107:         }
 7108:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 7109:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7110:                       &mt('Retain standard text:').
 7111:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 7112:                       &mt('Yes').'</label>'.'&nbsp;'.
 7113:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 7114:                       &mt('No').'</label></span><br />'.
 7115:                       '<span class="LC_fontsize_small">'.
 7116:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 7117:                       &mt('Include custom text:');
 7118:         if ($customurl) {
 7119:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('custom text'),600,500,
 7120:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 7121:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 7122:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 7123:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 7124:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 7125:         }
 7126:         if ($switchserver) {
 7127:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 7128:         } else {
 7129:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 7130:                          '<input type="file" name="passwords_customfile" /></span>';
 7131:         }
 7132:         $datatable .= '</td></tr>';
 7133:     } elsif ($position eq 'middle') {
 7134:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 7135:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 7136:         my %defaults;
 7137:         if (ref($domconf{'defaults'}) eq 'HASH') {
 7138:             %defaults = %{$domconf{'defaults'}};
 7139:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 7140:                 $defaults{'intauth_cost'} = 10;
 7141:             }
 7142:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 7143:                 $defaults{'intauth_check'} = 0;
 7144:             }
 7145:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 7146:                 $defaults{'intauth_switch'} = 0;
 7147:             }
 7148:         } else {
 7149:             %defaults = (
 7150:                           'intauth_cost'   => 10,
 7151:                           'intauth_check'  => 0,
 7152:                           'intauth_switch' => 0,
 7153:                         );
 7154:         }
 7155:         foreach my $item (@items) {
 7156:             if ($itemcount%2) {
 7157:                 $css_class = '';
 7158:             } else {
 7159:                 $css_class = ' class="LC_odd_row" ';
 7160:             }
 7161:             $datatable .= '<tr'.$css_class.'>'.
 7162:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 7163:                           '</span></td><td class="LC_left_item" colspan="3">';
 7164:             if ($item eq 'intauth_switch') {
 7165:                 my @options = (0,1,2);
 7166:                 my %optiondesc = &Apache::lonlocal::texthash (
 7167:                                    0 => 'No',
 7168:                                    1 => 'Yes',
 7169:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 7170:                                  );
 7171:                 $datatable .= '<table width="100%">';
 7172:                 foreach my $option (@options) {
 7173:                     my $checked = ' ';
 7174:                     if ($defaults{$item} eq $option) {
 7175:                         $checked = ' checked="checked"';
 7176:                     }
 7177:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7178:                                   '<label><input type="radio" name="'.$item.
 7179:                                   '" value="'.$option.'"'.$checked.' />'.
 7180:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7181:                 }
 7182:                 $datatable .= '</table>';
 7183:             } elsif ($item eq 'intauth_check') {
 7184:                 my @options = (0,1,2);
 7185:                 my %optiondesc = &Apache::lonlocal::texthash (
 7186:                                    0 => 'No',
 7187:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 7188:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 7189:                                  );
 7190:                 $datatable .= '<table width="100%">';
 7191:                 foreach my $option (@options) {
 7192:                     my $checked = ' ';
 7193:                     my $onclick;
 7194:                     if ($defaults{$item} eq $option) {
 7195:                         $checked = ' checked="checked"';
 7196:                     }
 7197:                     if ($option == 2) {
 7198:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 7199:                     }
 7200:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7201:                                   '<label><input type="radio" name="'.$item.
 7202:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 7203:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7204:                 }
 7205:                 $datatable .= '</table>';
 7206:             } else {
 7207:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 7208:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 7209:             }
 7210:             $datatable .= '</td></tr>';
 7211:             $itemcount ++;
 7212:         }
 7213:     } elsif ($position eq 'lower') {
 7214:         my ($min,$max,%chars,$expire,$numsaved);
 7215:         $min = $Apache::lonnet::passwdmin;
 7216:         if (ref($settings) eq 'HASH') {
 7217:             if ($settings->{min}) {
 7218:                 $min = $settings->{min};
 7219:             }
 7220:             if ($settings->{max}) {
 7221:                 $max = $settings->{max};
 7222:             }
 7223:             if (ref($settings->{chars}) eq 'ARRAY') {
 7224:                 map { $chars{$_} = 1; } (@{$settings->{chars}});
 7225:             }
 7226:             if ($settings->{expire}) {
 7227:                 $expire = $settings->{expire};
 7228:             }
 7229:             if ($settings->{numsaved}) {
 7230:                 $numsaved = $settings->{numsaved};
 7231:             }
 7232:         }
 7233:         my %rulenames = &Apache::lonlocal::texthash(
 7234:                                                      uc => 'At least one upper case letter',
 7235:                                                      lc => 'At least one lower case letter',
 7236:                                                      num => 'At least one number',
 7237:                                                      spec => 'At least one non-alphanumeric',
 7238:                                                    );
 7239:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7240:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 7241:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7242:                       '<input type="text" name="passwords_min" value="'.$min.'" size="3" '.
 7243:                       'onblur="javascript:warnIntPass(this);" />'.
 7244:                       '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 7245:                       '</span></td></tr>';
 7246:         $itemcount ++;
 7247:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7248:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 7249:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7250:                       '<input type="text" name="passwords_max" value="'.$max.'" size="3" '.
 7251:                       'onblur="javascript:warnIntPass(this);" />'.
 7252:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 7253:                       '</span></td></tr>';
 7254:         $itemcount ++;
 7255:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7256:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 7257:                       '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 7258:                       '</span></td>';
 7259:         my $numinrow = 2;
 7260:         my @possrules = ('uc','lc','num','spec');
 7261:         $datatable .= '<td class="LC_left_item"><table>';
 7262:         for (my $i=0; $i<@possrules; $i++) {
 7263:             my ($rem,$checked);
 7264:             if ($chars{$possrules[$i]}) {
 7265:                 $checked = ' checked="checked"';
 7266:             }
 7267:             $rem = $i%($numinrow);
 7268:             if ($rem == 0) {
 7269:                 if ($i > 0) {
 7270:                     $datatable .= '</tr>';
 7271:                 }
 7272:                 $datatable .= '<tr>';
 7273:             }
 7274:             $datatable .= '<td><span class="LC_nobreak"><label>'.
 7275:                           '<input type="checkbox" name="passwords_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 7276:                           $rulenames{$possrules[$i]}.'</label></span></td>';
 7277:         }
 7278:         my $rem = @possrules%($numinrow);
 7279:         my $colsleft = $numinrow - $rem;
 7280:         if ($colsleft > 1 ) {
 7281:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7282:                           '&nbsp;</td>';
 7283:         } elsif ($colsleft == 1) {
 7284:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 7285:         }
 7286:         $datatable .='</table></td></tr>';
 7287:         $itemcount ++;
 7288:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7289:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'expire'}.'</td>'.
 7290:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7291:                       '<input type="text" name="passwords_expire" value="'.$expire.'" size="4" '.
 7292:                       'onblur="javascript:warnIntPass(this);" />'.
 7293:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no expiration)').'</span>'.
 7294:                       '</span></td></tr>';
 7295:         $itemcount ++;
 7296:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7297:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 7298:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7299:                       '<input type="text" name="passwords_numsaved" value="'.$numsaved.'" size="3" '.
 7300:                       'onblur="javascript:warnIntPass(this);" />'. 
 7301:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 7302:                       '</span></td></tr>';
 7303:     } else {
 7304:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7305:         my %ownerchg = (
 7306:                           by  => {},
 7307:                           for => {},
 7308:                        );
 7309:         my %ownertitles = &Apache::lonlocal::texthash (
 7310:                             by  => 'Course owner status(es) allowed',
 7311:                             for => 'Student status(es) allowed',
 7312:                           );
 7313:         if (ref($settings) eq 'HASH') {
 7314:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 7315:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 7316:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 7317:                 }
 7318:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 7319:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 7320:                 }
 7321:             }
 7322:         }
 7323:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7324:         $datatable .= '<tr '.$css_class.'>'.
 7325:                       '<td>'.
 7326:                       &mt('Requirements').'<ul>'.
 7327:                       '<li>'.&mt("Course 'type' is not a Community or Placement Test").'</li>'.
 7328:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 7329:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 7330:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 7331:                       '</ul>'.
 7332:                       '</td>'.
 7333:                       '<td class="LC_left_item">';
 7334:         foreach my $item ('by','for') {
 7335:             $datatable .= '<fieldset style="display: inline-block;">'.
 7336:                           '<legend>'.$ownertitles{$item}.'</legend>';
 7337:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7338:                 foreach my $type (@{$types}) {
 7339:                     my $checked;
 7340:                     if ($ownerchg{$item}{$type}) {
 7341:                         $checked = ' checked="checked"';
 7342:                     }
 7343:                     $datatable .= '<span class="LC_nobreak"><label>'.
 7344:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 7345:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 7346:                                   '</span>&nbsp;&nbsp; ';
 7347:                 }
 7348:             }
 7349:             my $checked;
 7350:             if ($ownerchg{$item}{'default'}) {
 7351:                 $checked = ' checked="checked"';
 7352:             }
 7353:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 7354:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 7355:                           $othertitle.'</label></span></fieldset>';
 7356:         }
 7357:         $datatable .= '</td></tr>';
 7358:     }
 7359:     return $datatable;
 7360: }
 7361: 
 7362: sub print_wafproxy {
 7363:     my ($position,$dom,$settings,$rowtotal) = @_;
 7364:     my $css_class;
 7365:     my $itemcount = 0;
 7366:     my $datatable;
 7367:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7368:     my (%othercontrol,%otherdoms,%aliases,%values,$setdom,$showdom);
 7369:     my %lt = &wafproxy_titles();
 7370:     foreach my $server (sort(keys(%servers))) {
 7371:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 7372:         next if ($serverhome eq '');
 7373:         my $serverdom;
 7374:         if ($serverhome ne $server) {
 7375:             $serverdom = &Apache::lonnet::host_domain($serverhome);
 7376:             if (($serverdom ne '') && (&Apache::lonnet::domain($serverdom) ne '')) {
 7377:                 $othercontrol{$server} = $serverdom;
 7378:             }
 7379:         } else {
 7380:             $serverdom = &Apache::lonnet::host_domain($server);
 7381:             next if (($serverdom eq '') || (&Apache::lonnet::domain($serverdom) eq ''));
 7382:             if ($serverdom ne $dom) {
 7383:                 $othercontrol{$server} = $serverdom;
 7384:             } else {
 7385:                 $setdom = 1;
 7386:                 if (ref($settings) eq 'HASH') {
 7387:                     if (ref($settings->{'alias'}) eq 'HASH') {
 7388:                         $aliases{$dom} = $settings->{'alias'};
 7389:                         if ($aliases{$dom} ne '') {
 7390:                             $showdom = 1;
 7391:                         }
 7392:                     }
 7393:                 }
 7394:             }
 7395:         }
 7396:     }
 7397:     if ($setdom) {
 7398:         %{$values{$dom}} = ();
 7399:         if (ref($settings) eq 'HASH') {
 7400:             foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 7401:                 $values{$dom}{$item} = $settings->{$item};
 7402:             }
 7403:         }
 7404:     }
 7405:     if (keys(%othercontrol)) {
 7406:         %otherdoms = reverse(%othercontrol);
 7407:         foreach my $domain (keys(%otherdoms)) {
 7408:             %{$values{$domain}} = ();
 7409:             my %config = &Apache::lonnet::get_dom('configuration',['wafproxy'],$domain);
 7410:             if (ref($config{'wafproxy'}) eq 'HASH') {
 7411:                 $aliases{$domain} = $config{'wafproxy'}{'alias'};
 7412:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 7413:                     $values{$domain}{$item} = $config{'wafproxy'}{$item};
 7414:                 }
 7415:             }
 7416:         }
 7417:     }
 7418:     if ($position eq 'top') {
 7419:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7420:         my %aliasinfo;
 7421:         foreach my $server (sort(keys(%servers))) {
 7422:             $itemcount ++;
 7423:             my $dom_in_effect;
 7424:             my $aliasrows = '<tr>'.
 7425:                             '<td class="LC_left_item" style="vertical-align: baseline;">'.
 7426:                             &mt('Hostname').':&nbsp;'.
 7427:                             '<i>'.&Apache::lonnet::hostname($server).'</i></td><td>&nbsp;</td>';
 7428:             if ($othercontrol{$server}) {
 7429:                 $dom_in_effect = $othercontrol{$server};
 7430:                 my $current;
 7431:                 if (ref($aliases{$dom_in_effect}) eq 'HASH') {
 7432:                     $current = $aliases{$dom_in_effect}{$server};
 7433:                 }
 7434:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 7435:                               &mt('Alias').':&nbsp';
 7436:                 if ($current) {
 7437:                     $aliasrows .= $current;
 7438:                 } else {
 7439:                     $aliasrows .= &mt('None');
 7440:                 }
 7441:                 $aliasrows .= '&nbsp;<span class="LC_small">('.
 7442:                               &mt('controlled by domain: [_1]',
 7443:                                   '<b>'.$dom_in_effect.'</b>').')</span></td>';
 7444:             } else {
 7445:                 $dom_in_effect = $dom;
 7446:                 my $current;
 7447:                 if (ref($aliases{$dom}) eq 'HASH') {
 7448:                     if ($aliases{$dom}{$server}) {
 7449:                         $current = $aliases{$dom}{$server};
 7450:                     }
 7451:                 }
 7452:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 7453:                               &mt('Alias').':&nbsp;'.
 7454:                               '<input type="text" name="wafproxy_alias_'.$server.'" '.
 7455:                               'value="'.$current.'" size="30" /></td>';
 7456:             }
 7457:             $aliasrows .= '</tr>';
 7458:             $aliasinfo{$dom_in_effect} .= $aliasrows;
 7459:         }
 7460:         if ($aliasinfo{$dom}) {
 7461:             my ($onclick,$wafon,$wafoff,$showtable);
 7462:             $onclick = ' onclick="javascript:toggleWAF();"';
 7463:             $wafoff = ' checked="checked"';
 7464:             $showtable = ' style="display:none";';
 7465:             if ($showdom) {
 7466:                 $wafon = $wafoff;
 7467:                 $wafoff = '';
 7468:                 $showtable = ' style="display:inline;"';
 7469:             }
 7470:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7471:             $datatable = '<tr'.$css_class.'>'.
 7472:                          '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br />'.
 7473:                          '<span class="LC_nobreak">'.&mt('WAF in use?').'&nbsp;<label>'.
 7474:                          '<input type="radio" name="wafproxy_'.$dom.'" value="1"'.$wafon.$onclick.' />'.
 7475:                          &mt('Yes').'</label>'.('&nbsp;'x2).'<label>'.
 7476:                          '<input type="radio" name="wafproxy_'.$dom.'" value="0"'.$wafoff.$onclick.' />'.
 7477:                          &mt('No').'</label></span></td>'.
 7478:                          '<td class="LC_left_item">'.
 7479:                          '<table id="wafproxy_table"'.$showtable.'>'.$aliasinfo{$dom}.
 7480:                          '</table></td></tr>';
 7481:             $itemcount++;
 7482:         }
 7483:         if (keys(%otherdoms)) {
 7484:             foreach my $key (sort(keys(%otherdoms))) {
 7485:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7486:                 $datatable .= '<tr'.$css_class.'>'.
 7487:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$key.'</b>').'</td>'.
 7488:                               '<td class="LC_left_item"><table>'.$aliasinfo{$key}.
 7489:                               '</table></td></tr>';
 7490:                 $itemcount++;
 7491:             }
 7492:         }
 7493:     } else {
 7494:         my %ip_methods = &remoteip_methods();
 7495:         if ($setdom) {
 7496:             $itemcount ++;
 7497:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7498:             my ($nowafstyle,$wafstyle,$curr_remotip,$currwafdisplay,$vpndircheck,$vpnaliascheck,
 7499:                 $currwafvpn,$wafrangestyle,$alltossl,$ssltossl);
 7500:             $wafstyle = ' style="display:none;"';
 7501:             $nowafstyle = ' style="display:table-row;"';
 7502:             $currwafdisplay = ' style="display: none"';
 7503:             $wafrangestyle = ' style="display: none"';
 7504:             $curr_remotip = 'n';
 7505:             $ssltossl = ' checked="checked"';
 7506:             if ($showdom) {
 7507:                 $wafstyle = ' style="display:table-row;"';
 7508:                 $nowafstyle =  ' style="display:none;"';
 7509:                 if (keys(%{$values{$dom}})) {
 7510:                     if ($values{$dom}{remoteip} =~ /^[nmh]$/) {
 7511:                         $curr_remotip = $values{$dom}{remoteip};
 7512:                     }
 7513:                     if ($curr_remotip eq 'h') {
 7514:                         $currwafdisplay = ' style="display:table-row"';
 7515:                         $wafrangestyle = ' style="display:inline-block;"';
 7516:                     }
 7517:                     if ($values{$dom}{'sslopt'}) {
 7518:                         $alltossl = ' checked="checked"';
 7519:                         $ssltossl = '';
 7520:                     }
 7521:                 }
 7522:                 if (($values{$dom}{'vpnint'} ne '') || ($values{$dom}{'vpnext'} ne '')) {
 7523:                     $vpndircheck = ' checked="checked"';
 7524:                     $currwafvpn = ' style="display:table-row;"';
 7525:                     $wafrangestyle = ' style="display:inline-block;"';
 7526:                 } else {
 7527:                     $vpnaliascheck = ' checked="checked"';
 7528:                     $currwafvpn = ' style="display:none;"';
 7529:                 }
 7530:             }
 7531:             $datatable .= '<tr'.$css_class.' id="nowafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 7532:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'</td>'.
 7533:                           '<td class="LC_right_item">'.&mt('WAF not in use, nothing to set').'</td>'.
 7534:                           '</tr>'.
 7535:                           '<tr'.$css_class.' id="wafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 7536:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br /><br />'.
 7537:                           '<div id="wafproxyranges_'.$dom.'">'.&mt('Format for comma separated IP ranges').':<br />'.
 7538:                           &mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'</div></td>'.
 7539:                           '<td class="LC_left_item"><table>'.
 7540:                           '<tr>'.
 7541:                           '<td valign="top">'.$lt{'remoteip'}.':&nbsp;'.
 7542:                           '<select name="wafproxy_remoteip" id="wafproxy_remoteip" onchange="javascript:updateWAF();">';
 7543:             foreach my $option ('m','h','n') {
 7544:                 my $sel;
 7545:                 if ($option eq $curr_remotip) {
 7546:                    $sel = ' selected="selected"';
 7547:                 }
 7548:                 $datatable .= '<option value="'.$option.'"'.$sel.'>'.
 7549:                               $ip_methods{$option}.'</option>';
 7550:             }
 7551:             $datatable .= '</select></td></tr>'."\n".
 7552:                           '<tr id="wafproxy_header"'.$currwafdisplay.'><td>'.
 7553:                           $lt{'ipheader'}.':&nbsp;'.
 7554:                           '<input type="text" value="'.$values{$dom}{'ipheader'}.'" '.
 7555:                           'name="wafproxy_ipheader" />'.
 7556:                           '</td></tr>'."\n".
 7557:                           '<tr id="wafproxy_trust"'.$currwafdisplay.'><td>'.
 7558:                           $lt{'trusted'}.':<br />'.
 7559:                           '<textarea name="wafproxy_trusted" rows="3" cols="80">'.
 7560:                           $values{$dom}{'trusted'}.'</textarea>'.
 7561:                           '</td></tr>'."\n".
 7562:                           '<tr><td><hr /></td></tr>'."\n".
 7563:                           '<tr>'.
 7564:                           '<td valign="top">'.$lt{'vpnaccess'}.':<br /><span class="LC_nobreak">'.
 7565:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpndircheck.' value="1" onclick="javascript:checkWAF();" />'.
 7566:                           $lt{'vpndirect'}.'</label>'.('&nbsp;'x2).
 7567:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpnaliascheck.' value="0" onclick="javascript:checkWAF();" />'.
 7568:                           $lt{'vpnaliased'}.'</label></span></td></tr>';
 7569:             foreach my $item ('vpnint','vpnext') {
 7570:                 $datatable .= '<tr id="wafproxy_show_'.$item.'"'.$currwafvpn.'>'.
 7571:                               '<td valign="top">'.$lt{$item}.':<br />'.
 7572:                               '<textarea name="wafproxy_'.$item.'" rows="3" cols="80">'.
 7573:                               $values{$dom}{$item}.'</textarea>'.
 7574:                               '</td></tr>'."\n";
 7575:             }
 7576:             $datatable .= '<tr><td><hr /></td></tr>'."\n".
 7577:                           '<tr>'.
 7578:                           '<td valign="top">'.$lt{'sslopt'}.':<br /><span class="LC_nobreak">'.
 7579:                           '<label><input type="radio" name="wafproxy_sslopt"'.$alltossl.' value="1" />'.
 7580:                           $lt{'alltossl'}.'</label>'.('&nbsp;'x2).
 7581:                           '<label><input type="radio" name="wafproxy_sslopt"'.$ssltossl.' value="0" />'.
 7582:                           $lt{'ssltossl'}.'</label></span></td></tr>'."\n".
 7583:                           '</table></td></tr>';
 7584:         }
 7585:         if (keys(%otherdoms)) {
 7586:             foreach my $domain (sort(keys(%otherdoms))) {
 7587:                 $itemcount ++;
 7588:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7589:                 $datatable .= '<tr'.$css_class.'>'.
 7590:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$domain.'</b>').'</td>'.
 7591:                               '<td class="LC_left_item"><table>';
 7592:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
 7593:                     my $showval = &mt('None');
 7594:                     if ($item eq 'ssl') {
 7595:                         $showval = $lt{'ssltossl'};
 7596:                     }
 7597:                     if ($values{$domain}{$item}) {
 7598:                         $showval = $values{$domain}{$item};
 7599:                         if ($item eq 'ssl') {
 7600:                             $showval = $lt{'alltossl'};
 7601:                         } elsif ($item eq 'remoteip') {
 7602:                             $showval = $ip_methods{$values{$domain}{$item}};
 7603:                         }
 7604:                     }
 7605:                     $datatable .= '<tr>'.
 7606:                                   '<td>'.$lt{$item}.':&nbsp;'.$showval.'</td></tr>';
 7607:                 }
 7608:                 $datatable .= '</table></td></tr>';
 7609:             }
 7610:         }
 7611:     }
 7612:     $$rowtotal += $itemcount;
 7613:     return $datatable;
 7614: }
 7615: 
 7616: sub wafproxy_titles {
 7617:     return &Apache::lonlocal::texthash(
 7618:                remoteip   => "Method for determining user's IP",
 7619:                ipheader   => 'Request header containing remote IP',
 7620:                trusted    => 'Trusted IP range(s)',
 7621:                vpnaccess  => 'Access from institutional VPN',
 7622:                vpndirect  => 'via regular hostname (no WAF)',
 7623:                vpnaliased => 'via aliased hostname (WAF)',
 7624:                vpnint     => 'Internal IP Range(s) for VPN sessions',
 7625:                vpnext     => 'IP Range(s) for backend WAF connections',
 7626:                sslopt     => 'Forwarding http/https',
 7627:                alltossl   => 'WAF forwards both http and https requests to https',
 7628:                ssltossl   => 'WAF forwards http requests to http and https to https',
 7629:            );
 7630: }
 7631: 
 7632: sub remoteip_methods {
 7633:     return &Apache::lonlocal::texthash(
 7634:               m => 'Use Apache mod_remoteip',
 7635:               h => 'Use headers parsed by LON-CAPA',
 7636:               n => 'Not in use',
 7637:            );
 7638: }
 7639: 
 7640: sub print_usersessions {
 7641:     my ($position,$dom,$settings,$rowtotal) = @_;
 7642:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 7643:     my (%by_ip,%by_location,@intdoms,@instdoms);
 7644:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 7645: 
 7646:     my @alldoms = &Apache::lonnet::all_domains();
 7647:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 7648:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7649:     my %altids = &id_for_thisdom(%servers);
 7650:     if ($position eq 'top') {
 7651:         if (keys(%serverhomes) > 1) {
 7652:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 7653:             my ($curroffloadnow,$curroffloadoth);
 7654:             if (ref($settings) eq 'HASH') {
 7655:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 7656:                     $curroffloadnow = $settings->{'offloadnow'};
 7657:                 }
 7658:                 if (ref($settings->{'offloadoth'}) eq 'HASH') {
 7659:                     $curroffloadoth = $settings->{'offloadoth'};
 7660:                 }
 7661:             }
 7662:             my $other_insts = scalar(keys(%by_location));
 7663:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,
 7664:                                       $other_insts,$curroffloadnow,$curroffloadoth,$rowtotal);
 7665:         } else {
 7666:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 7667:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 7668:                           '</td></tr>';
 7669:         }
 7670:     } else {
 7671:         my %titles = &usersession_titles();
 7672:         my ($prefix,@types);
 7673:         if ($position eq 'bottom') {
 7674:             $prefix = 'remote';
 7675:             @types = ('version','excludedomain','includedomain');
 7676:         } else {
 7677:             $prefix = 'hosted';
 7678:             @types = ('excludedomain','includedomain');
 7679:         }
 7680:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 7681:     }
 7682:     $$rowtotal += $itemcount;
 7683:     return $datatable;
 7684: }
 7685: 
 7686: sub rules_by_location {
 7687:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 7688:     my ($datatable,$itemcount,$css_class);
 7689:     if (keys(%{$by_location}) == 0) {
 7690:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7691:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 7692:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 7693:                      '</td></tr>';
 7694:         $itemcount = 1;
 7695:     } else {
 7696:         $itemcount = 0;
 7697:         my $numinrow = 5;
 7698:         my (%current,%checkedon,%checkedoff);
 7699:         my @locations = sort(keys(%{$by_location}));
 7700:         foreach my $type (@{$types}) {
 7701:             $checkedon{$type} = '';
 7702:             $checkedoff{$type} = ' checked="checked"';
 7703:         }
 7704:         if (ref($settings) eq 'HASH') {
 7705:             if (ref($settings->{$prefix}) eq 'HASH') {
 7706:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 7707:                     $current{$key} = $settings->{$prefix}{$key};
 7708:                     if ($key eq 'version') {
 7709:                         if ($current{$key} ne '') {
 7710:                             $checkedon{$key} = ' checked="checked"';
 7711:                             $checkedoff{$key} = '';
 7712:                         }
 7713:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 7714:                         $checkedon{$key} = ' checked="checked"';
 7715:                         $checkedoff{$key} = '';
 7716:                     }
 7717:                 }
 7718:             }
 7719:         }
 7720:         foreach my $type (@{$types}) {
 7721:             next if ($type ne 'version' && !@locations);
 7722:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7723:             $datatable .= '<tr'.$css_class.'>
 7724:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 7725:                            <span class="LC_nobreak">&nbsp;
 7726:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 7727:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 7728:             if ($type eq 'version') {
 7729:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 7730:                 my $selector = '<select name="'.$prefix.'_version">';
 7731:                 foreach my $version (@lcversions) {
 7732:                     my $selected = '';
 7733:                     if ($current{'version'} eq $version) {
 7734:                         $selected = ' selected="selected"';
 7735:                     }
 7736:                     $selector .= ' <option value="'.$version.'"'.
 7737:                                  $selected.'>'.$version.'</option>';
 7738:                 }
 7739:                 $selector .= '</select> ';
 7740:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 7741:             } else {
 7742:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 7743:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 7744:                              ' />'.('&nbsp;'x2).
 7745:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 7746:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 7747:                              "\n".
 7748:                              '</div><div><table>';
 7749:                 my $rem;
 7750:                 for (my $i=0; $i<@locations; $i++) {
 7751:                     my ($showloc,$value,$checkedtype);
 7752:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 7753:                         my $ip = $by_location->{$locations[$i]}->[0];
 7754:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 7755:                             $value = join(':',@{$by_ip->{$ip}});
 7756:                             $showloc = join(', ',@{$by_ip->{$ip}});
 7757:                             if (ref($current{$type}) eq 'ARRAY') {
 7758:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 7759:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 7760:                                         $checkedtype = ' checked="checked"';
 7761:                                         last;
 7762:                                     }
 7763:                                 }
 7764:                             }
 7765:                         }
 7766:                     }
 7767:                     $rem = $i%($numinrow);
 7768:                     if ($rem == 0) {
 7769:                         if ($i > 0) {
 7770:                             $datatable .= '</tr>';
 7771:                         }
 7772:                         $datatable .= '<tr>';
 7773:                     }
 7774:                     $datatable .= '<td class="LC_left_item">'.
 7775:                                   '<span class="LC_nobreak"><label>'.
 7776:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 7777:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 7778:                                   '</label></span></td>';
 7779:                 }
 7780:                 $rem = @locations%($numinrow);
 7781:                 my $colsleft = $numinrow - $rem;
 7782:                 if ($colsleft > 1 ) {
 7783:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7784:                                   '&nbsp;</td>';
 7785:                 } elsif ($colsleft == 1) {
 7786:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 7787:                 }
 7788:                 $datatable .= '</tr></table>';
 7789:             }
 7790:             $datatable .= '</td></tr>';
 7791:             $itemcount ++;
 7792:         }
 7793:     }
 7794:     return ($datatable,$itemcount);
 7795: }
 7796: 
 7797: sub print_ssl {
 7798:     my ($position,$dom,$settings,$rowtotal) = @_;
 7799:     my ($css_class,$datatable);
 7800:     my $itemcount = 1;
 7801:     if ($position eq 'top') {
 7802:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7803:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7804:         my $same_institution;
 7805:         if ($intdom ne '') {
 7806:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 7807:             if (ref($internet_names) eq 'ARRAY') {
 7808:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 7809:                     $same_institution = 1;
 7810:                 }
 7811:             }
 7812:         }
 7813:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7814:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 7815:         if ($same_institution) {
 7816:             my %domservers = &Apache::lonnet::get_servers($dom);
 7817:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 7818:         } else {
 7819:             $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.");
 7820:         }
 7821:         $datatable .= '</td></tr>';
 7822:         $itemcount ++;
 7823:     } else {
 7824:         my %titles = &ssl_titles();
 7825:         my (%by_ip,%by_location,@intdoms,@instdoms);
 7826:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 7827:         my @alldoms = &Apache::lonnet::all_domains();
 7828:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 7829:         my @domservers = &Apache::lonnet::get_servers($dom);
 7830:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7831:         my %altids = &id_for_thisdom(%servers);
 7832:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 7833:             my $legacy;
 7834:             unless (ref($settings) eq 'HASH') {
 7835:                 my $name;
 7836:                 if ($position eq 'connto') {
 7837:                     $name = 'loncAllowInsecure';
 7838:                 } else {
 7839:                     $name = 'londAllowInsecure';
 7840:                 }
 7841:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 7842:                 my @ids=&Apache::lonnet::current_machine_ids();
 7843:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 7844:                     my %what = (
 7845:                                    $name => 1,
 7846:                                );
 7847:                     my ($result,$returnhash) =
 7848:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 7849:                     if ($result eq 'ok') {
 7850:                         if (ref($returnhash) eq 'HASH') {
 7851:                             $legacy = $returnhash->{$name};
 7852:                         }
 7853:                     }
 7854:                 } else {
 7855:                     $legacy = $Apache::lonnet::perlvar{$name};
 7856:                 }
 7857:             }
 7858:             foreach my $type ('dom','intdom','other') {
 7859:                 my %checked;
 7860:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7861:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 7862:                               '<td class="LC_right_item">';
 7863:                 my $skip; 
 7864:                 if ($type eq 'dom') {
 7865:                     unless (keys(%servers) > 1) {
 7866:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 7867:                         $skip = 1;
 7868:                     }
 7869:                 }
 7870:                 if ($type eq 'intdom') {
 7871:                     unless (@instdoms > 1) {
 7872:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 7873:                         $skip = 1;
 7874:                     } 
 7875:                 } elsif ($type eq 'other') {
 7876:                     if (keys(%by_location) == 0) {
 7877:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 7878:                         $skip = 1;
 7879:                     }
 7880:                 }
 7881:                 unless ($skip) {
 7882:                     $checked{'yes'} = ' checked="checked"'; 
 7883:                     if (ref($settings) eq 'HASH') {
 7884:                         if (ref($settings->{$position}) eq 'HASH') {
 7885:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 7886:                                 $checked{$1} = $checked{'yes'};
 7887:                                 delete($checked{'yes'}); 
 7888:                             }
 7889:                         }
 7890:                     } else {
 7891:                         if ($legacy == 0) {
 7892:                             $checked{'req'} = $checked{'yes'};
 7893:                             delete($checked{'yes'});    
 7894:                         }
 7895:                     }
 7896:                     foreach my $option ('no','yes','req') {
 7897:                         $datatable .= '<span class="LC_nobreak"><label>'.
 7898:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 7899:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 7900:                                       '</label></span>'.('&nbsp;'x2);
 7901:                     }
 7902:                 }
 7903:                 $datatable .= '</td></tr>';
 7904:                 $itemcount ++; 
 7905:             }
 7906:         } else {
 7907:             my $prefix = 'replication';
 7908:             my @types = ('certreq','nocertreq');
 7909:             if (keys(%by_location) == 0) {
 7910:                 $datatable .= '<tr'.$css_class.'><td>'.
 7911:                               &mt('Nothing to set here, as there are no other institutions').
 7912:                               '</td></tr>';
 7913:                 $itemcount ++;
 7914:             } else {
 7915:                 ($datatable,$itemcount) = 
 7916:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 7917:             }
 7918:         }
 7919:     }
 7920:     $$rowtotal += $itemcount;
 7921:     return $datatable;
 7922: }
 7923: 
 7924: sub ssl_titles {
 7925:     return &Apache::lonlocal::texthash (
 7926:                dom           => 'LON-CAPA servers/VMs from same domain',
 7927:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 7928:                other         => 'External LON-CAPA servers/VMs',
 7929:                connto        => 'Connections to other servers',
 7930:                connfrom      => 'Connections from other servers',
 7931:                replication   => 'Replicating content to other institutions',
 7932:                certreq       => 'Client certificate required, but specific domains exempt',
 7933:                nocertreq     => 'No client certificate required, except for specific domains',
 7934:                no            => 'SSL not used',
 7935:                yes           => 'SSL Optional (used if available)',
 7936:                req           => 'SSL Required',
 7937:     );
 7938: }
 7939: 
 7940: sub print_trust {
 7941:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 7942:     my ($css_class,$datatable,%checked,%choices);
 7943:     my (%by_ip,%by_location,@intdoms,@instdoms);
 7944:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 7945:     my $itemcount = 1;
 7946:     my %titles = &trust_titles();
 7947:     my @types = ('exc','inc');
 7948:     if ($prefix eq 'top') {
 7949:         $prefix = 'content';
 7950:     } elsif ($prefix eq 'bottom') {
 7951:         $prefix = 'msg';
 7952:     }
 7953:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 7954:     $$rowtotal += $itemcount;
 7955:     return $datatable;
 7956: }
 7957: 
 7958: sub trust_titles {
 7959:     return &Apache::lonlocal::texthash(
 7960:                content  => "Access to this domain's content by others",
 7961:                shared   => "Access to other domain's content by this domain",
 7962:                enroll   => "Enrollment in this domain's courses by others", 
 7963:                othcoau  => "Co-author roles in this domain for others",
 7964:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 7965:                domroles => "Domain roles in this domain assignable to others",
 7966:                catalog  => "Course Catalog for this domain displayed elsewhere",
 7967:                reqcrs   => "Requests for creation of courses in this domain by others",
 7968:                msg      => "Users in other domains can send messages to this domain",
 7969:                exc      => "Allow all, but exclude specific domains",
 7970:                inc      => "Deny all, but include specific domains",
 7971:            );
 7972: } 
 7973: 
 7974: sub build_location_hashes {
 7975:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 7976:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 7977:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 7978:     my %iphost = &Apache::lonnet::get_iphost();
 7979:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 7980:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 7981:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 7982:         foreach my $id (@{$iphost{$primary_ip}}) {
 7983:             my $intdom = &Apache::lonnet::internet_dom($id);
 7984:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 7985:                 push(@{$intdoms},$intdom);
 7986:             }
 7987:         }
 7988:     }
 7989:     foreach my $ip (keys(%iphost)) {
 7990:         if (ref($iphost{$ip}) eq 'ARRAY') {
 7991:             foreach my $id (@{$iphost{$ip}}) {
 7992:                 my $location = &Apache::lonnet::internet_dom($id);
 7993:                 if ($location) {
 7994:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 7995:                         my $dom = &Apache::lonnet::host_domain($id);
 7996:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 7997:                             push(@{$instdoms},$dom);
 7998:                         }
 7999:                         next;
 8000:                     }
 8001:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8002:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 8003:                             push(@{$by_ip->{$ip}},$location);
 8004:                         }
 8005:                     } else {
 8006:                         $by_ip->{$ip} = [$location];
 8007:                     }
 8008:                 }
 8009:             }
 8010:         }
 8011:     }
 8012:     foreach my $ip (sort(keys(%{$by_ip}))) {
 8013:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8014:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 8015:             my $first = $by_ip->{$ip}->[0];
 8016:             if (ref($by_location->{$first}) eq 'ARRAY') {
 8017:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 8018:                     push(@{$by_location->{$first}},$ip);
 8019:                 }
 8020:             } else {
 8021:                 $by_location->{$first} = [$ip];
 8022:             }
 8023:         }
 8024:     }
 8025:     return;
 8026: }
 8027: 
 8028: sub current_offloads_to {
 8029:     my ($dom,$settings,$servers) = @_;
 8030:     my (%spareid,%otherdomconfigs);
 8031:     if (ref($servers) eq 'HASH') {
 8032:         foreach my $lonhost (sort(keys(%{$servers}))) {
 8033:             my $gotspares;
 8034:             if (ref($settings) eq 'HASH') {
 8035:                 if (ref($settings->{'spares'}) eq 'HASH') {
 8036:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 8037:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 8038:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 8039:                         $gotspares = 1;
 8040:                     }
 8041:                 }
 8042:             }
 8043:             unless ($gotspares) {
 8044:                 my $gotspares;
 8045:                 my $serverhomeID =
 8046:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 8047:                 my $serverhomedom =
 8048:                     &Apache::lonnet::host_domain($serverhomeID);
 8049:                 if ($serverhomedom ne $dom) {
 8050:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 8051:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 8052:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 8053:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 8054:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 8055:                                 $gotspares = 1;
 8056:                             }
 8057:                         }
 8058:                     } else {
 8059:                         $otherdomconfigs{$serverhomedom} =
 8060:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 8061:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 8062:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 8063:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 8064:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 8065:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 8066:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 8067:                                         $gotspares = 1;
 8068:                                     }
 8069:                                 }
 8070:                             }
 8071:                         }
 8072:                     }
 8073:                 }
 8074:             }
 8075:             unless ($gotspares) {
 8076:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 8077:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 8078:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 8079:                } else {
 8080:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 8081:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 8082:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 8083:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 8084:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 8085:                     } else {
 8086:                         my %what = (
 8087:                              spareid => 1,
 8088:                         );
 8089:                         my ($result,$returnhash) = 
 8090:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 8091:                         if ($result eq 'ok') { 
 8092:                             if (ref($returnhash) eq 'HASH') {
 8093:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 8094:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 8095:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 8096:                                 }
 8097:                             }
 8098:                         }
 8099:                     }
 8100:                 }
 8101:             }
 8102:         }
 8103:     }
 8104:     return %spareid;
 8105: }
 8106: 
 8107: sub spares_row {
 8108:     my ($dom,$servers,$spareid,$serverhomes,$altids,$other_insts,
 8109:         $curroffloadnow,$curroffloadoth,$rowtotal) = @_;
 8110:     my $css_class;
 8111:     my $numinrow = 4;
 8112:     my $itemcount = 1;
 8113:     my $datatable;
 8114:     my %typetitles = &sparestype_titles();
 8115:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 8116:         foreach my $server (sort(keys(%{$servers}))) {
 8117:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 8118:             my ($othercontrol,$serverdom);
 8119:             if ($serverhome ne $server) {
 8120:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 8121:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 8122:             } else {
 8123:                 $serverdom = &Apache::lonnet::host_domain($server);
 8124:                 if ($serverdom ne $dom) {
 8125:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 8126:                 }
 8127:             }
 8128:             next unless (ref($spareid->{$server}) eq 'HASH');
 8129:             my ($checkednow,$checkedoth);
 8130:             if (ref($curroffloadnow) eq 'HASH') {
 8131:                 if ($curroffloadnow->{$server}) {
 8132:                     $checkednow = ' checked="checked"';
 8133:                 }
 8134:             }
 8135:             if (ref($curroffloadoth) eq 'HASH') {
 8136:                 if ($curroffloadoth->{$server}) {
 8137:                     $checkedoth = ' checked="checked"';
 8138:                 }
 8139:             }
 8140:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8141:             $datatable .= '<tr'.$css_class.'>
 8142:                            <td rowspan="2">
 8143:                             <span class="LC_nobreak">'.
 8144:                           &mt('[_1] when busy, offloads to:'
 8145:                               ,'<b>'.$server.'</b>').'</span><br />'.
 8146:                           '<span class="LC_nobreak">'."\n".
 8147:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 8148:                           '&nbsp;'.&mt('Switch any active user on next access').'</label></span>'.
 8149:                           "\n";
 8150:             if ($other_insts) {
 8151:                 $datatable .= '<br />'.
 8152:                               '<span class="LC_nobreak">'."\n".
 8153:                           '<label><input type="checkbox" name="offloadoth" value="'.$server.'"'.$checkedoth.' />'.
 8154:                           '&nbsp;'.&mt('Switch other institutions on next access').'</label></span>'.
 8155:                           "\n";
 8156:             }
 8157:             my (%current,%canselect);
 8158:             my @choices = 
 8159:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 8160:             foreach my $type ('primary','default') {
 8161:                 if (ref($spareid->{$server}) eq 'HASH') {
 8162:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 8163:                         my @spares = @{$spareid->{$server}{$type}};
 8164:                         if (@spares > 0) {
 8165:                             if ($othercontrol) {
 8166:                                 $current{$type} = join(', ',@spares);
 8167:                             } else {
 8168:                                 $current{$type} .= '<table>';
 8169:                                 my $numspares = scalar(@spares);
 8170:                                 for (my $i=0;  $i<@spares; $i++) {
 8171:                                     my $rem = $i%($numinrow);
 8172:                                     if ($rem == 0) {
 8173:                                         if ($i > 0) {
 8174:                                             $current{$type} .= '</tr>';
 8175:                                         }
 8176:                                         $current{$type} .= '<tr>';
 8177:                                     }
 8178:                                     $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;'.
 8179:                                                        $spareid->{$server}{$type}[$i].
 8180:                                                        '</label></td>'."\n";
 8181:                                 }
 8182:                                 my $rem = @spares%($numinrow);
 8183:                                 my $colsleft = $numinrow - $rem;
 8184:                                 if ($colsleft > 1 ) {
 8185:                                     $current{$type} .= '<td colspan="'.$colsleft.
 8186:                                                        '" class="LC_left_item">'.
 8187:                                                        '&nbsp;</td>';
 8188:                                 } elsif ($colsleft == 1) {
 8189:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 8190:                                 }
 8191:                                 $current{$type} .= '</tr></table>';
 8192:                             }
 8193:                         }
 8194:                     }
 8195:                     if ($current{$type} eq '') {
 8196:                         $current{$type} = &mt('None specified');
 8197:                     }
 8198:                     if ($othercontrol) {
 8199:                         if ($type eq 'primary') {
 8200:                             $canselect{$type} = $othercontrol;
 8201:                         }
 8202:                     } else {
 8203:                         $canselect{$type} = 
 8204:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 8205:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 8206:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 8207:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 8208:                         if (@choices > 0) {
 8209:                             foreach my $lonhost (@choices) {
 8210:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 8211:                             }
 8212:                         }
 8213:                         $canselect{$type} .= '</select>'."\n";
 8214:                     }
 8215:                 } else {
 8216:                     $current{$type} = &mt('Could not be determined');
 8217:                     if ($type eq 'primary') {
 8218:                         $canselect{$type} =  $othercontrol;
 8219:                     }
 8220:                 }
 8221:                 if ($type eq 'default') {
 8222:                     $datatable .= '<tr'.$css_class.'>';
 8223:                 }
 8224:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 8225:                               '<td>'.$current{$type}.'</td>'."\n".
 8226:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 8227:             }
 8228:             $itemcount ++;
 8229:         }
 8230:     }
 8231:     $$rowtotal += $itemcount;
 8232:     return $datatable;
 8233: }
 8234: 
 8235: sub possible_newspares {
 8236:     my ($server,$currspares,$serverhomes,$altids) = @_;
 8237:     my $serverhostname = &Apache::lonnet::hostname($server);
 8238:     my %excluded;
 8239:     if ($serverhostname ne '') {
 8240:         %excluded = (
 8241:                        $serverhostname => 1,
 8242:                     );
 8243:     }
 8244:     if (ref($currspares) eq 'HASH') {
 8245:         foreach my $type (keys(%{$currspares})) {
 8246:             if (ref($currspares->{$type}) eq 'ARRAY') {
 8247:                 if (@{$currspares->{$type}} > 0) {
 8248:                     foreach my $curr (@{$currspares->{$type}}) {
 8249:                         my $hostname = &Apache::lonnet::hostname($curr);
 8250:                         $excluded{$hostname} = 1;
 8251:                     }
 8252:                 }
 8253:             }
 8254:         }
 8255:     }
 8256:     my @choices;
 8257:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 8258:         if (keys(%{$serverhomes}) > 1) {
 8259:             foreach my $name (sort(keys(%{$serverhomes}))) {
 8260:                 unless ($excluded{$name}) {
 8261:                     if (exists($altids->{$serverhomes->{$name}})) {
 8262:                         push(@choices,$altids->{$serverhomes->{$name}});
 8263:                     } else {
 8264:                         push(@choices,$serverhomes->{$name});
 8265:                     }
 8266:                 }
 8267:             }
 8268:         }
 8269:     }
 8270:     return sort(@choices);
 8271: }
 8272: 
 8273: sub print_loadbalancing {
 8274:     my ($dom,$settings,$rowtotal) = @_;
 8275:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8276:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8277:     my $numinrow = 1;
 8278:     my $datatable;
 8279:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8280:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 8281:     if (ref($settings) eq 'HASH') {
 8282:         %existing = %{$settings};
 8283:     }
 8284:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 8285:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 8286:                                   \%currtargets,\%currrules,\%currcookies);
 8287:     } else {
 8288:         return;
 8289:     }
 8290:     my ($othertitle,$usertypes,$types) =
 8291:         &Apache::loncommon::sorted_inst_types($dom);
 8292:     my $rownum = 8;
 8293:     if (ref($types) eq 'ARRAY') {
 8294:         $rownum += scalar(@{$types});
 8295:     }
 8296:     my @css_class = ('LC_odd_row','LC_even_row');
 8297:     my $balnum = 0;
 8298:     my $islast;
 8299:     my (@toshow,$disabledtext);
 8300:     if (keys(%currbalancer) > 0) {
 8301:         @toshow = sort(keys(%currbalancer));
 8302:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 8303:             push(@toshow,'');
 8304:         }
 8305:     } else {
 8306:         @toshow = ('');
 8307:         $disabledtext = &mt('No existing load balancer');
 8308:     }
 8309:     foreach my $lonhost (@toshow) {
 8310:         if ($balnum == scalar(@toshow)-1) {
 8311:             $islast = 1;
 8312:         } else {
 8313:             $islast = 0;
 8314:         }
 8315:         my $cssidx = $balnum%2;
 8316:         my $targets_div_style = 'display: none';
 8317:         my $disabled_div_style = 'display: block';
 8318:         my $homedom_div_style = 'display: none';
 8319:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 8320:                       '<td rowspan="'.$rownum.'" style="vertical-align: top">'.
 8321:                       '<p>';
 8322:         if ($lonhost eq '') {
 8323:             $datatable .= '<span class="LC_nobreak">';
 8324:             if (keys(%currbalancer) > 0) {
 8325:                 $datatable .= &mt('Add balancer:');
 8326:             } else {
 8327:                 $datatable .= &mt('Enable balancer:');
 8328:             }
 8329:             $datatable .= '&nbsp;'.
 8330:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 8331:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 8332:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 8333:                           '<option value="" selected="selected">'.&mt('None').
 8334:                           '</option>'."\n";
 8335:             foreach my $server (sort(keys(%servers))) {
 8336:                 next if ($currbalancer{$server});
 8337:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 8338:             }
 8339:             $datatable .=
 8340:                 '</select>'."\n".
 8341:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 8342:         } else {
 8343:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 8344:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 8345:                            &mt('Stop balancing').'</label>'.
 8346:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 8347:             $targets_div_style = 'display: block';
 8348:             $disabled_div_style = 'display: none';
 8349:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 8350:                 $homedom_div_style = 'display: block';
 8351:             }
 8352:         }
 8353:         $datatable .= '</p></td><td rowspan="'.$rownum.'" style="vertical-align: top">'.
 8354:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 8355:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 8356:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 8357:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 8358:         my @sparestypes = ('primary','default');
 8359:         my %typetitles = &sparestype_titles();
 8360:         my %hostherechecked = (
 8361:                                   no => ' checked="checked"',
 8362:                               );
 8363:         my %balcookiechecked = (
 8364:                                   no => ' checked="checked"', 
 8365:                                );
 8366:         foreach my $sparetype (@sparestypes) {
 8367:             my $targettable;
 8368:             for (my $i=0; $i<$numspares; $i++) {
 8369:                 my $checked;
 8370:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 8371:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 8372:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 8373:                             $checked = ' checked="checked"';
 8374:                         }
 8375:                     }
 8376:                 }
 8377:                 my ($chkboxval,$disabled);
 8378:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 8379:                     $chkboxval = $spares[$i];
 8380:                 }
 8381:                 if (exists($currbalancer{$spares[$i]})) {
 8382:                     $disabled = ' disabled="disabled"';
 8383:                 }
 8384:                 $targettable .=
 8385:                     '<td><span class="LC_nobreak"><label>'.
 8386:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 8387:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 8388:                     '</span></label></span></td>';
 8389:                 my $rem = $i%($numinrow);
 8390:                 if ($rem == 0) {
 8391:                     if (($i > 0) && ($i < $numspares-1)) {
 8392:                         $targettable .= '</tr>';
 8393:                     }
 8394:                     if ($i < $numspares-1) {
 8395:                         $targettable .= '<tr>';
 8396:                     }
 8397:                 }
 8398:             }
 8399:             if ($targettable ne '') {
 8400:                 my $rem = $numspares%($numinrow);
 8401:                 my $colsleft = $numinrow - $rem;
 8402:                 if ($colsleft > 1 ) {
 8403:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8404:                                     '&nbsp;</td>';
 8405:                 } elsif ($colsleft == 1) {
 8406:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 8407:                 }
 8408:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 8409:                                '<table><tr>'.$targettable.'</tr></table><br />';
 8410:             }
 8411:             $hostherechecked{$sparetype} = '';
 8412:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 8413:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 8414:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 8415:                         $hostherechecked{$sparetype} = ' checked="checked"';
 8416:                         $hostherechecked{'no'} = '';
 8417:                     }
 8418:                 }
 8419:             }
 8420:         }
 8421:         if ($currcookies{$lonhost}) {
 8422:             %balcookiechecked = (
 8423:                                     yes => ' checked="checked"',
 8424:                                 );
 8425:         }
 8426:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 8427:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 8428:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 8429:         foreach my $sparetype (@sparestypes) {
 8430:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 8431:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 8432:                           '</i></label><br />';
 8433:         }
 8434:         $datatable .= &mt('Use balancer cookie').'<br />'.
 8435:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 8436:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 8437:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 8438:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 8439:                       '</div></td></tr>'.
 8440:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 8441:                                            $othertitle,$usertypes,$types,\%servers,
 8442:                                            \%currbalancer,$lonhost,
 8443:                                            $targets_div_style,$homedom_div_style,
 8444:                                            $css_class[$cssidx],$balnum,$islast);
 8445:         $$rowtotal += $rownum;
 8446:         $balnum ++;
 8447:     }
 8448:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 8449:     return $datatable;
 8450: }
 8451: 
 8452: sub get_loadbalancers_config {
 8453:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 8454:     return unless ((ref($servers) eq 'HASH') &&
 8455:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 8456:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 8457:                    (ref($currcookies) eq 'HASH'));
 8458:     if (keys(%{$existing}) > 0) {
 8459:         my $oldlonhost;
 8460:         foreach my $key (sort(keys(%{$existing}))) {
 8461:             if ($key eq 'lonhost') {
 8462:                 $oldlonhost = $existing->{'lonhost'};
 8463:                 $currbalancer->{$oldlonhost} = 1;
 8464:             } elsif ($key eq 'targets') {
 8465:                 if ($oldlonhost) {
 8466:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 8467:                 }
 8468:             } elsif ($key eq 'rules') {
 8469:                 if ($oldlonhost) {
 8470:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 8471:                 }
 8472:             } elsif (ref($existing->{$key}) eq 'HASH') {
 8473:                 $currbalancer->{$key} = 1;
 8474:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 8475:                 $currrules->{$key} = $existing->{$key}{'rules'};
 8476:                 if ($existing->{$key}{'cookie'}) {
 8477:                     $currcookies->{$key} = 1;
 8478:                 }
 8479:             }
 8480:         }
 8481:     } else {
 8482:         my ($balancerref,$targetsref) =
 8483:                 &Apache::lonnet::get_lonbalancer_config($servers);
 8484:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 8485:             foreach my $server (sort(keys(%{$balancerref}))) {
 8486:                 $currbalancer->{$server} = 1;
 8487:                 $currtargets->{$server} = $targetsref->{$server};
 8488:             }
 8489:         }
 8490:     }
 8491:     return;
 8492: }
 8493: 
 8494: sub loadbalancing_rules {
 8495:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 8496:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 8497:         $css_class,$balnum,$islast) = @_;
 8498:     my $output;
 8499:     my $num = 0;
 8500:     my ($alltypes,$othertypes,$titles) =
 8501:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 8502:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 8503:         foreach my $type (@{$alltypes}) {
 8504:             $num ++;
 8505:             my $current;
 8506:             if (ref($currrules) eq 'HASH') {
 8507:                 $current = $currrules->{$type};
 8508:             }
 8509:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 8510:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 8511:                     $current = '';
 8512:                 }
 8513:             }
 8514:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 8515:                                              $servers,$currbalancer,$lonhost,$dom,
 8516:                                              $targets_div_style,$homedom_div_style,
 8517:                                              $css_class,$balnum,$num,$islast);
 8518:         }
 8519:     }
 8520:     return $output;
 8521: }
 8522: 
 8523: sub loadbalancing_titles {
 8524:     my ($dom,$intdom,$usertypes,$types) = @_;
 8525:     my %othertypes = (
 8526:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 8527:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 8528:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 8529:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 8530:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 8531:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 8532:                      );
 8533:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 8534:     my @available;
 8535:     if (ref($types) eq 'ARRAY') {
 8536:         @available = @{$types};
 8537:     }
 8538:     unless (grep(/^default$/,@available)) {
 8539:         push(@available,'default');
 8540:     }
 8541:     unshift(@alltypes,@available);
 8542:     my %titles;
 8543:     foreach my $type (@alltypes) {
 8544:         if ($type =~ /^_LC_/) {
 8545:             $titles{$type} = $othertypes{$type};
 8546:         } elsif ($type eq 'default') {
 8547:             $titles{$type} = &mt('All users from [_1]',$dom);
 8548:             if (ref($types) eq 'ARRAY') {
 8549:                 if (@{$types} > 0) {
 8550:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 8551:                 }
 8552:             }
 8553:         } elsif (ref($usertypes) eq 'HASH') {
 8554:             $titles{$type} = $usertypes->{$type};
 8555:         }
 8556:     }
 8557:     return (\@alltypes,\%othertypes,\%titles);
 8558: }
 8559: 
 8560: sub loadbalance_rule_row {
 8561:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 8562:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 8563:     my @rulenames;
 8564:     my %ruletitles = &offloadtype_text();
 8565:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 8566:         @rulenames = ('balancer','offloadedto','specific');
 8567:     } else {
 8568:         @rulenames = ('default','homeserver');
 8569:         if ($type eq '_LC_external') {
 8570:             push(@rulenames,'externalbalancer');
 8571:         } else {
 8572:             push(@rulenames,'specific');
 8573:         }
 8574:         push(@rulenames,'none');
 8575:     }
 8576:     my $style = $targets_div_style;
 8577:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 8578:         $style = $homedom_div_style;
 8579:     }
 8580:     my $space;
 8581:     if ($islast && $num == 1) {
 8582:         $space = '<div style="display:inline-block;">&nbsp;</div>';
 8583:     }
 8584:     my $output =
 8585:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td style="vertical-align: top">'.$space.
 8586:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 8587:         '<td valaign="top">'.$space.
 8588:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 8589:     for (my $i=0; $i<@rulenames; $i++) {
 8590:         my $rule = $rulenames[$i];
 8591:         my ($checked,$extra);
 8592:         if ($rulenames[$i] eq 'default') {
 8593:             $rule = '';
 8594:         }
 8595:         if ($rulenames[$i] eq 'specific') {
 8596:             if (ref($servers) eq 'HASH') {
 8597:                 my $default;
 8598:                 if (($current ne '') && (exists($servers->{$current}))) {
 8599:                     $checked = ' checked="checked"';
 8600:                 }
 8601:                 unless ($checked) {
 8602:                     $default = ' selected="selected"';
 8603:                 }
 8604:                 $extra =
 8605:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 8606:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 8607:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 8608:                     '<option value=""'.$default.'></option>'."\n";
 8609:                 foreach my $server (sort(keys(%{$servers}))) {
 8610:                     if (ref($currbalancer) eq 'HASH') {
 8611:                         next if (exists($currbalancer->{$server}));
 8612:                     }
 8613:                     my $selected;
 8614:                     if ($server eq $current) {
 8615:                         $selected = ' selected="selected"';
 8616:                     }
 8617:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 8618:                 }
 8619:                 $extra .= '</select>';
 8620:             }
 8621:         } elsif ($rule eq $current) {
 8622:             $checked = ' checked="checked"';
 8623:         }
 8624:         $output .= '<span class="LC_nobreak"><label>'.
 8625:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 8626:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 8627:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 8628:                    ')"'.$checked.' />&nbsp;';
 8629:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 8630:             $output .= $ruletitles{'particular'};
 8631:         } else {
 8632:             $output .= $ruletitles{$rulenames[$i]};
 8633:         }
 8634:         $output .= '</label>'.$extra.'</span><br />'."\n";
 8635:     }
 8636:     $output .= '</div></td></tr>'."\n";
 8637:     return $output;
 8638: }
 8639: 
 8640: sub offloadtype_text {
 8641:     my %ruletitles = &Apache::lonlocal::texthash (
 8642:            'default'          => 'Offloads to default destinations',
 8643:            'homeserver'       => "Offloads to user's home server",
 8644:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 8645:            'specific'         => 'Offloads to specific server',
 8646:            'none'             => 'No offload',
 8647:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 8648:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 8649:            'particular'       => 'Session hosted (after re-auth) on server:',
 8650:     );
 8651:     return %ruletitles;
 8652: }
 8653: 
 8654: sub sparestype_titles {
 8655:     my %typestitles = &Apache::lonlocal::texthash (
 8656:                           'primary' => 'primary',
 8657:                           'default' => 'default',
 8658:                       );
 8659:     return %typestitles;
 8660: }
 8661: 
 8662: sub contact_titles {
 8663:     my %titles = &Apache::lonlocal::texthash (
 8664:                    'supportemail'    => 'Support E-mail address',
 8665:                    'adminemail'      => 'Default Server Admin E-mail address',
 8666:                    'errormail'       => 'Error reports to be e-mailed to',
 8667:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 8668:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 8669:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 8670:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 8671:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 8672:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 8673:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 8674:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 8675:                    'errorthreshold'  => 'Error count threshold for status e-mail to admin(s)',
 8676:                    'errorsysmail'    => 'Error count threshold for e-mail to developer group',
 8677:                    'errorweights'    => 'Weights used to compute error count',
 8678:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 8679:                  );
 8680:     my %short_titles = &Apache::lonlocal::texthash (
 8681:                            adminemail   => 'Admin E-mail address',
 8682:                            supportemail => 'Support E-mail',
 8683:                        );   
 8684:     return (\%titles,\%short_titles);
 8685: }
 8686: 
 8687: sub helpform_fields {
 8688:     my %titles =  &Apache::lonlocal::texthash (
 8689:                        'username'   => 'Name',
 8690:                        'user'       => 'Username/domain',
 8691:                        'phone'      => 'Phone',
 8692:                        'cc'         => 'Cc e-mail',
 8693:                        'course'     => 'Course Details',
 8694:                        'section'    => 'Sections',
 8695:                        'screenshot' => 'File upload',
 8696:     );
 8697:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 8698:     my %possoptions = (
 8699:                         username     => ['yes','no','req'],
 8700:                         phone        => ['yes','no','req'],
 8701:                         user         => ['yes','no'],
 8702:                         cc           => ['yes','no'],
 8703:                         course       => ['yes','no'],
 8704:                         section      => ['yes','no'],
 8705:                         screenshot   => ['yes','no'],
 8706:                       );
 8707:     my %fieldoptions = &Apache::lonlocal::texthash (
 8708:                          'yes'  => 'Optional',
 8709:                          'req'  => 'Required',
 8710:                          'no'   => "Not shown",
 8711:     );
 8712:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 8713: }
 8714: 
 8715: sub tool_titles {
 8716:     my %titles = &Apache::lonlocal::texthash (
 8717:                      aboutme    => 'Personal web page',
 8718:                      blog       => 'Blog',
 8719:                      webdav     => 'WebDAV',
 8720:                      portfolio  => 'Portfolio',
 8721:                      official   => 'Official courses (with institutional codes)',
 8722:                      unofficial => 'Unofficial courses',
 8723:                      community  => 'Communities',
 8724:                      textbook   => 'Textbook courses',
 8725:                      placement  => 'Placement tests',
 8726:                  );
 8727:     return %titles;
 8728: }
 8729: 
 8730: sub courserequest_titles {
 8731:     my %titles = &Apache::lonlocal::texthash (
 8732:                                    official   => 'Official',
 8733:                                    unofficial => 'Unofficial',
 8734:                                    community  => 'Communities',
 8735:                                    textbook   => 'Textbook',
 8736:                                    placement  => 'Placement tests',
 8737:                                    lti        => 'LTI Provider',
 8738:                                    norequest  => 'Not allowed',
 8739:                                    approval   => 'Approval by DC',
 8740:                                    validate   => 'With validation',
 8741:                                    autolimit  => 'Numerical limit',
 8742:                                    unlimited  => '(blank for unlimited)',
 8743:                  );
 8744:     return %titles;
 8745: }
 8746: 
 8747: sub authorrequest_titles {
 8748:     my %titles = &Apache::lonlocal::texthash (
 8749:                                    norequest  => 'Not allowed',
 8750:                                    approval   => 'Approval by Dom. Coord.',
 8751:                                    automatic  => 'Automatic approval',
 8752:                  );
 8753:     return %titles;
 8754: }
 8755: 
 8756: sub courserequest_conditions {
 8757:     my %conditions = &Apache::lonlocal::texthash (
 8758:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 8759:        validate   => '(Processing of request subject to institutional validation).',
 8760:                  );
 8761:     return %conditions;
 8762: }
 8763: 
 8764: 
 8765: sub print_usercreation {
 8766:     my ($position,$dom,$settings,$rowtotal) = @_;
 8767:     my $numinrow = 4;
 8768:     my $datatable;
 8769:     if ($position eq 'top') {
 8770:         $$rowtotal ++;
 8771:         my $rowcount = 0;
 8772:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 8773:         if (ref($rules) eq 'HASH') {
 8774:             if (keys(%{$rules}) > 0) {
 8775:                 $datatable .= &user_formats_row('username',$settings,$rules,
 8776:                                                 $ruleorder,$numinrow,$rowcount);
 8777:                 $$rowtotal ++;
 8778:                 $rowcount ++;
 8779:             }
 8780:         }
 8781:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 8782:         if (ref($idrules) eq 'HASH') {
 8783:             if (keys(%{$idrules}) > 0) {
 8784:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 8785:                                                 $idruleorder,$numinrow,$rowcount);
 8786:                 $$rowtotal ++;
 8787:                 $rowcount ++;
 8788:             }
 8789:         }
 8790:         if ($rowcount == 0) {
 8791:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 8792:             $$rowtotal ++;
 8793:             $rowcount ++;
 8794:         }
 8795:     } elsif ($position eq 'middle') {
 8796:         my @creators = ('author','course','requestcrs');
 8797:         my ($rules,$ruleorder) =
 8798:             &Apache::lonnet::inst_userrules($dom,'username');
 8799:         my %lt = &usercreation_types();
 8800:         my %checked;
 8801:         if (ref($settings) eq 'HASH') {
 8802:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 8803:                 foreach my $item (@creators) {
 8804:                     $checked{$item} = $settings->{'cancreate'}{$item};
 8805:                 }
 8806:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 8807:                 foreach my $item (@creators) {
 8808:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 8809:                         $checked{$item} = 'none';
 8810:                     }
 8811:                 }
 8812:             }
 8813:         }
 8814:         my $rownum = 0;
 8815:         foreach my $item (@creators) {
 8816:             $rownum ++;
 8817:             if ($checked{$item} eq '') {
 8818:                 $checked{$item} = 'any';
 8819:             }
 8820:             my $css_class;
 8821:             if ($rownum%2) {
 8822:                 $css_class = '';
 8823:             } else {
 8824:                 $css_class = ' class="LC_odd_row" ';
 8825:             }
 8826:             $datatable .= '<tr'.$css_class.'>'.
 8827:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 8828:                          '</span></td><td style="text-align: right">';
 8829:             my @options = ('any');
 8830:             if (ref($rules) eq 'HASH') {
 8831:                 if (keys(%{$rules}) > 0) {
 8832:                     push(@options,('official','unofficial'));
 8833:                 }
 8834:             }
 8835:             push(@options,'none');
 8836:             foreach my $option (@options) {
 8837:                 my $type = 'radio';
 8838:                 my $check = ' ';
 8839:                 if ($checked{$item} eq $option) {
 8840:                     $check = ' checked="checked" ';
 8841:                 } 
 8842:                 $datatable .= '<span class="LC_nobreak"><label>'.
 8843:                               '<input type="'.$type.'" name="can_createuser_'.
 8844:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 8845:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 8846:             }
 8847:             $datatable .= '</td></tr>';
 8848:         }
 8849:     } else {
 8850:         my @contexts = ('author','course','domain');
 8851:         my @authtypes = ('int','krb4','krb5','loc','lti');
 8852:         my %checked;
 8853:         if (ref($settings) eq 'HASH') {
 8854:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 8855:                 foreach my $item (@contexts) {
 8856:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 8857:                         foreach my $auth (@authtypes) {
 8858:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 8859:                                 $checked{$item}{$auth} = ' checked="checked" ';
 8860:                             }
 8861:                         }
 8862:                     }
 8863:                 }
 8864:             }
 8865:         } else {
 8866:             foreach my $item (@contexts) {
 8867:                 foreach my $auth (@authtypes) {
 8868:                     $checked{$item}{$auth} = ' checked="checked" ';
 8869:                 }
 8870:             }
 8871:         }
 8872:         my %title = &context_names();
 8873:         my %authname = &authtype_names();
 8874:         my $rownum = 0;
 8875:         my $css_class; 
 8876:         foreach my $item (@contexts) {
 8877:             if ($rownum%2) {
 8878:                 $css_class = '';
 8879:             } else {
 8880:                 $css_class = ' class="LC_odd_row" ';
 8881:             }
 8882:             $datatable .=   '<tr'.$css_class.'>'.
 8883:                             '<td>'.$title{$item}.
 8884:                             '</td><td class="LC_left_item">'.
 8885:                             '<span class="LC_nobreak">';
 8886:             foreach my $auth (@authtypes) {
 8887:                 $datatable .= '<label>'. 
 8888:                               '<input type="checkbox" name="'.$item.'_auth" '.
 8889:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 8890:                               $authname{$auth}.'</label>&nbsp;';
 8891:             }
 8892:             $datatable .= '</span></td></tr>';
 8893:             $rownum ++;
 8894:         }
 8895:         $$rowtotal += $rownum;
 8896:     }
 8897:     return $datatable;
 8898: }
 8899: 
 8900: sub print_selfcreation {
 8901:     my ($position,$dom,$settings,$rowtotal) = @_;
 8902:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 8903:         $emaildomain,$datatable);
 8904:     if (ref($settings) eq 'HASH') {
 8905:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 8906:             $createsettings = $settings->{'cancreate'};
 8907:             if (ref($createsettings) eq 'HASH') {
 8908:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 8909:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 8910:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 8911:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 8912:                         @selfcreate = ('email','login','sso');
 8913:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 8914:                         @selfcreate = ($createsettings->{'selfcreate'});
 8915:                     }
 8916:                 }
 8917:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 8918:                     $processing = $createsettings->{'selfcreateprocessing'};
 8919:                 }
 8920:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 8921:                     $emailoptions = $createsettings->{'emailoptions'};
 8922:                 }
 8923:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 8924:                     $emailverified = $createsettings->{'emailverified'};
 8925:                 }
 8926:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 8927:                     $emaildomain = $createsettings->{'emaildomain'};
 8928:                 }
 8929:             }
 8930:         }
 8931:     }
 8932:     my %radiohash;
 8933:     my $numinrow = 4;
 8934:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 8935:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8936:     if ($position eq 'top') {
 8937:         my %choices = &Apache::lonlocal::texthash (
 8938:                                                       cancreate_login      => 'Institutional Login',
 8939:                                                       cancreate_sso        => 'Institutional Single Sign On',
 8940:                                                   );
 8941:         my @toggles = sort(keys(%choices));
 8942:         my %defaultchecked = (
 8943:                                'cancreate_login' => 'off',
 8944:                                'cancreate_sso'   => 'off',
 8945:                              );
 8946:         my ($onclick,$itemcount);
 8947:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 8948:                                                      \%choices,$itemcount,$onclick);
 8949:         $$rowtotal += $itemcount;
 8950:         
 8951:         if (ref($usertypes) eq 'HASH') {
 8952:             if (keys(%{$usertypes}) > 0) {
 8953:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 8954:                                              $dom,$numinrow,$othertitle,
 8955:                                              'statustocreate',$rowtotal);
 8956:                 $$rowtotal ++;
 8957:             }
 8958:         }
 8959:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 8960:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8961:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 8962:         my $rem;
 8963:         my $numperrow = 2;
 8964:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 8965:         $datatable .= '<tr'.$css_class.'>'.
 8966:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 8967:                      '<td class="LC_left_item">'."\n".
 8968:                      '<table>'."\n";
 8969:         for (my $i=0; $i<@fields; $i++) {
 8970:             $rem = $i%($numperrow);
 8971:             if ($rem == 0) {
 8972:                 if ($i > 0) {
 8973:                     $datatable .= '</tr>';
 8974:                 }
 8975:                 $datatable .= '<tr>';
 8976:             }
 8977:             my $currval;
 8978:             if (ref($createsettings) eq 'HASH') {
 8979:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 8980:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 8981:                 }
 8982:             }
 8983:             $datatable .= '<td class="LC_left_item">'.
 8984:                           '<span class="LC_nobreak">'.
 8985:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 8986:                           'value="'.$currval.'" size="10" />&nbsp;'.
 8987:                           $fieldtitles{$fields[$i]}.'</span></td>';
 8988:         }
 8989:         my $colsleft = $numperrow - $rem;
 8990:         if ($colsleft > 1 ) {
 8991:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8992:                          '&nbsp;</td>';
 8993:         } elsif ($colsleft == 1) {
 8994:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 8995:         }
 8996:         $datatable .= '</tr></table></td></tr>';
 8997:         $$rowtotal ++;
 8998:     } elsif ($position eq 'middle') {
 8999:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 9000:         my @posstypes;
 9001:         if (ref($types) eq 'ARRAY') {
 9002:             @posstypes = @{$types};
 9003:         }
 9004:         unless (grep(/^default$/,@posstypes)) {
 9005:             push(@posstypes,'default');
 9006:         }
 9007:         my %usertypeshash;
 9008:         if (ref($usertypes) eq 'HASH') {
 9009:             %usertypeshash = %{$usertypes};
 9010:         }
 9011:         $usertypeshash{'default'} = $othertitle;
 9012:         foreach my $status (@posstypes) {
 9013:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 9014:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 9015:             $$rowtotal ++;
 9016:         }
 9017:     } else {
 9018:         my %choices = &Apache::lonlocal::texthash (
 9019:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 9020:                                                   );
 9021:         my @toggles = sort(keys(%choices));
 9022:         my %defaultchecked = (
 9023:                                'cancreate_email' => 'off',
 9024:                              );
 9025:         my $customclass = 'LC_selfcreate_email';
 9026:         my $classprefix = 'LC_canmodify_emailusername_';
 9027:         my $optionsprefix = 'LC_options_emailusername_';
 9028:         my $display = 'none';
 9029:         my $rowstyle = 'display:none';
 9030:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 9031:             $display = 'block';
 9032:             $rowstyle = 'display:table-row';
 9033:         }
 9034:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 9035:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 9036:                                                      \%choices,$$rowtotal,$onclick);
 9037:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 9038:                                          $rowstyle);
 9039:         $$rowtotal ++;
 9040:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 9041:                                       $rowstyle);
 9042:         $$rowtotal ++;
 9043:         my (@ordered,@posstypes,%usertypeshash);
 9044:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 9045:         my ($emailrules,$emailruleorder) =
 9046:             &Apache::lonnet::inst_userrules($dom,'email');
 9047:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 9048:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 9049:         if (ref($types) eq 'ARRAY') {
 9050:             @posstypes = @{$types};
 9051:         }
 9052:         if (@posstypes) {
 9053:             unless (grep(/^default$/,@posstypes)) {
 9054:                 push(@posstypes,'default');
 9055:             }
 9056:             if (ref($usertypes) eq 'HASH') {
 9057:                 %usertypeshash = %{$usertypes};
 9058:             }
 9059:             my $currassign;
 9060:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 9061:                 $currassign = {
 9062:                                   selfassign => $domdefaults{'inststatusguest'},
 9063:                               };
 9064:                 @ordered = @{$domdefaults{'inststatusguest'}};
 9065:             } else {
 9066:                 $currassign = { selfassign => [] };
 9067:             }
 9068:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 9069:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 9070:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 9071:                                          $numinrow,$othertitle,'selfassign',
 9072:                                          $rowtotal,$onclicktypes,$customclass,
 9073:                                          $rowstyle);
 9074:             $$rowtotal ++;
 9075:             $usertypeshash{'default'} = $othertitle;
 9076:             foreach my $status (@posstypes) {
 9077:                 my $css_class;
 9078:                 if ($$rowtotal%2) {
 9079:                     $css_class = 'LC_odd_row ';
 9080:                 }
 9081:                 $css_class .= $customclass;
 9082:                 my $rowid = $optionsprefix.$status;
 9083:                 my $hidden = 1;
 9084:                 my $currstyle = 'display:none';
 9085:                 if (grep(/^\Q$status\E$/,@ordered)) {
 9086:                     $currstyle = $rowstyle;
 9087:                     $hidden = 0; 
 9088:                 }
 9089:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 9090:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 9091:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 9092:                 unless ($hidden) {
 9093:                     $$rowtotal ++;
 9094:                 }
 9095:             }
 9096:         } else {
 9097:             my $css_class;
 9098:             if ($$rowtotal%2) {
 9099:                 $css_class = 'LC_odd_row ';
 9100:             }
 9101:             $css_class .= $customclass;
 9102:             $usertypeshash{'default'} = $othertitle;
 9103:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 9104:                                          $emailrules,$emailruleorder,$settings,'default','',
 9105:                                          $othertitle,$css_class,$rowstyle,$intdom);
 9106:             $$rowtotal ++;
 9107:         }
 9108:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 9109:         $numinrow = 1;
 9110:         if (@posstypes) {
 9111:             foreach my $status (@posstypes) {
 9112:                 my $rowid = $classprefix.$status;
 9113:                 my $datarowstyle = 'display:none';
 9114:                 if (grep(/^\Q$status\E$/,@ordered)) { 
 9115:                     $datarowstyle = $rowstyle; 
 9116:                 }
 9117:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 9118:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 9119:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 9120:                 unless ($datarowstyle eq 'display:none') {
 9121:                     $$rowtotal ++;
 9122:                 }
 9123:             }
 9124:         } else {
 9125:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 9126:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 9127:                                                    $infotitles,'',$customclass,$rowstyle);
 9128:         }
 9129:     }
 9130:     return $datatable;
 9131: }
 9132: 
 9133: sub selfcreate_javascript {
 9134:     return <<"ENDSCRIPT";
 9135: 
 9136: <script type="text/javascript">
 9137: // <![CDATA[
 9138: 
 9139: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 9140:     var x = document.getElementsByClassName(target);
 9141:     var insttypes = 0;
 9142:     var insttypeRegExp = new RegExp(prefix);
 9143:     if ((x.length != undefined) && (x.length > 0)) {
 9144:         if (form.elements[radio].length != undefined) {
 9145:             for (var i=0; i<form.elements[radio].length; i++) {
 9146:                 if (form.elements[radio][i].checked) {
 9147:                     if (form.elements[radio][i].value == 1) {
 9148:                         for (var j=0; j<x.length; j++) {
 9149:                             if (x[j].id == 'undefined') {
 9150:                                 x[j].style.display = 'table-row';
 9151:                             } else if (insttypeRegExp.test(x[j].id)) {
 9152:                                 insttypes ++;
 9153:                             } else {
 9154:                                 x[j].style.display = 'table-row';
 9155:                             }
 9156:                         }
 9157:                     } else {
 9158:                         for (var j=0; j<x.length; j++) {
 9159:                             x[j].style.display = 'none';
 9160:                         }
 9161:                     }
 9162:                     break;
 9163:                 }
 9164:             }
 9165:             if (insttypes > 0) {
 9166:                 toggleDataRow(form,checkbox,target,altprefix);
 9167:                 toggleDataRow(form,checkbox,target,prefix,1);
 9168:             }
 9169:         }
 9170:     }
 9171:     return;
 9172: }
 9173: 
 9174: function toggleDataRow(form,checkbox,target,prefix,docount) {
 9175:     if (form.elements[checkbox].length != undefined) {
 9176:         var count = 0;
 9177:         if (docount) {
 9178:             for (var i=0; i<form.elements[checkbox].length; i++) {
 9179:                 if (form.elements[checkbox][i].checked) {
 9180:                     count ++;
 9181:                 }
 9182:             }
 9183:         }
 9184:         for (var i=0; i<form.elements[checkbox].length; i++) {
 9185:             var type = form.elements[checkbox][i].value;
 9186:             if (document.getElementById(prefix+type)) {
 9187:                 if (form.elements[checkbox][i].checked) {
 9188:                     document.getElementById(prefix+type).style.display = 'table-row';
 9189:                     if (count % 2 == 1) {
 9190:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 9191:                     } else {
 9192:                         document.getElementById(prefix+type).className = target;
 9193:                     }
 9194:                     count ++;
 9195:                 } else {
 9196:                     document.getElementById(prefix+type).style.display = 'none';
 9197:                 }
 9198:             }
 9199:         }
 9200:     }
 9201:     return;
 9202: }
 9203: 
 9204: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 9205:     var caller = radio+'_'+status;
 9206:     if (form.elements[caller].length != undefined) {
 9207:         for (var i=0; i<form.elements[caller].length; i++) {
 9208:             if (form.elements[caller][i].checked) {
 9209:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 9210:                     var curr = form.elements[caller][i].value;
 9211:                     if (prefix) {
 9212:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 9213:                     }
 9214:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 9215:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 9216:                     if (curr == 'custom') {
 9217:                         if (prefix) { 
 9218:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 9219:                         }
 9220:                     } else if (curr == 'inst') {
 9221:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 9222:                     } else if (curr == 'noninst') {
 9223:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 9224:                     }
 9225:                     break;
 9226:                 }
 9227:             }
 9228:         }
 9229:     }
 9230: }
 9231: 
 9232: // ]]>
 9233: </script>
 9234: 
 9235: ENDSCRIPT
 9236: }
 9237: 
 9238: sub noninst_users {
 9239:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 9240:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_; 
 9241:     my $class = 'LC_left_item';
 9242:     if ($css_class) {
 9243:         $css_class = ' class="'.$css_class.'"'; 
 9244:     }
 9245:     if ($rowid) {
 9246:         $rowid = ' id="'.$rowid.'"';
 9247:     }
 9248:     if ($rowstyle) {
 9249:         $rowstyle = ' style="'.$rowstyle.'"';
 9250:     }
 9251:     my ($output,$description);
 9252:     if ($type eq 'default') {
 9253:         $description = &mt('Requests for: [_1]',$typetitle);
 9254:     } else {
 9255:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 9256:     }
 9257:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 9258:               "<td>$description</td>\n".           
 9259:               '<td class="'.$class.'" colspan="2">'.
 9260:               '<table><tr>';
 9261:     my %headers = &Apache::lonlocal::texthash( 
 9262:               approve  => 'Processing',
 9263:               email    => 'E-mail',
 9264:               username => 'Username',
 9265:     );
 9266:     foreach my $item ('approve','email','username') {
 9267:         $output .= '<th>'.$headers{$item}.'</th>';
 9268:     }
 9269:     $output .= '</tr><tr>';
 9270:     foreach my $item ('approve','email','username') {
 9271:         $output .= '<td style="vertical-align: top">';
 9272:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 9273:         if ($item eq 'approve') {
 9274:             %choices = &Apache::lonlocal::texthash (
 9275:                                                      automatic => 'Automatically approved',
 9276:                                                      approval  => 'Queued for approval',
 9277:                                                    );
 9278:             @options = ('automatic','approval');
 9279:             $hashref = $processing;
 9280:             $defoption = 'automatic';
 9281:             $name = 'cancreate_emailprocess_'.$type;
 9282:         } elsif ($item eq 'email') {
 9283:             %choices = &Apache::lonlocal::texthash (
 9284:                                                      any     => 'Any e-mail',
 9285:                                                      inst    => 'Institutional only',
 9286:                                                      noninst => 'Non-institutional only',
 9287:                                                      custom  => 'Custom restrictions',
 9288:                                                    );
 9289:             @options = ('any','inst','noninst');
 9290:             my $showcustom;
 9291:             if (ref($emailrules) eq 'HASH') {
 9292:                 if (keys(%{$emailrules}) > 0) {
 9293:                     push(@options,'custom');
 9294:                     $showcustom = 'cancreate_emailrule';
 9295:                     if (ref($settings) eq 'HASH') {
 9296:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 9297:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 9298:                                 if (exists($emailrules->{$rule})) {
 9299:                                     $hascustom ++;
 9300:                                 }
 9301:                             }
 9302:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 9303:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 9304:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 9305:                                     if (exists($emailrules->{$rule})) {
 9306:                                         $hascustom ++;
 9307:                                     }
 9308:                                 }
 9309:                             }
 9310:                         }
 9311:                     }
 9312:                 }
 9313:             }
 9314:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 9315:                                                      "'cancreate_emaildomain','$type'".');"';
 9316:             $hashref = $emailoptions;
 9317:             $defoption = 'any';
 9318:             $name = 'cancreate_emailoptions_'.$type;
 9319:         } elsif ($item eq 'username') {
 9320:             %choices = &Apache::lonlocal::texthash (
 9321:                                                      all    => 'Same as e-mail',
 9322:                                                      first  => 'Omit @domain',
 9323:                                                      free   => 'Free to choose',
 9324:                                                    );
 9325:             @options = ('all','first','free');
 9326:             $hashref = $emailverified;
 9327:             $defoption = 'all';
 9328:             $name = 'cancreate_usernameoptions_'.$type;
 9329:         }
 9330:         foreach my $option (@options) {
 9331:             my $checked;
 9332:             if (ref($hashref) eq 'HASH') {
 9333:                 if ($type eq '') {
 9334:                     if (!exists($hashref->{'default'})) {
 9335:                         if ($option eq $defoption) {
 9336:                             $checked = ' checked="checked"';
 9337:                         }
 9338:                     } else {
 9339:                         if ($hashref->{'default'} eq $option) {
 9340:                             $checked = ' checked="checked"';
 9341:                         }
 9342:                     }
 9343:                 } else {
 9344:                     if (!exists($hashref->{$type})) {
 9345:                         if ($option eq $defoption) {
 9346:                             $checked = ' checked="checked"';
 9347:                         }
 9348:                     } else {
 9349:                         if ($hashref->{$type} eq $option) {
 9350:                             $checked = ' checked="checked"';
 9351:                         }
 9352:                     }
 9353:                 }
 9354:             } elsif (($item eq 'email') && ($hascustom)) {
 9355:                 if ($option eq 'custom') {
 9356:                     $checked = ' checked="checked"';
 9357:                 }
 9358:             } elsif ($option eq $defoption) {
 9359:                 $checked = ' checked="checked"';
 9360:             }
 9361:             $output .= '<span class="LC_nobreak"><label>'.
 9362:                        '<input type="radio" name="'.$name.'"'.
 9363:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 9364:                        $choices{$option}.'</label></span><br />';
 9365:             if ($item eq 'email') {
 9366:                 if ($option eq 'custom') {
 9367:                     my $id = 'cancreate_emailrule_'.$type;
 9368:                     my $display = 'none';
 9369:                     if ($checked) {
 9370:                         $display = 'inline';
 9371:                     }
 9372:                     my $numinrow = 2;
 9373:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 9374:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 9375:                                &user_formats_row('email',$settings,$emailrules,
 9376:                                                  $emailruleorder,$numinrow,'',$type);
 9377:                               '</table></fieldset>';
 9378:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 9379:                     my %text = &Apache::lonlocal::texthash (
 9380:                                                              inst    => 'must end:',
 9381:                                                              noninst => 'cannot end:',
 9382:                                                            );
 9383:                     my $value;
 9384:                     if (ref($emaildomain) eq 'HASH') {
 9385:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 9386:                             $value = $emaildomain->{$type}->{$option}; 
 9387:                         }
 9388:                     }
 9389:                     if ($value eq '') {
 9390:                         $value = '@'.$intdom;
 9391:                     }
 9392:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 9393:                     my $display = 'none';
 9394:                     if ($checked) {
 9395:                         $display = 'inline';
 9396:                     }
 9397:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 9398:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 9399:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 9400:                                '</div>';
 9401:                 }
 9402:             }
 9403:         }
 9404:         $output .= '</td>'."\n";
 9405:     }
 9406:     $output .= "</tr></table></td></tr>\n";
 9407:     return $output;
 9408: }
 9409: 
 9410: sub captcha_choice {
 9411:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 9412:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 9413:         $vertext,$currver);
 9414:     my %lt = &captcha_phrases();
 9415:     $keyentry = 'hidden';
 9416:     my $colspan=2;
 9417:     if ($context eq 'cancreate') {
 9418:         $rowname = &mt('CAPTCHA validation');
 9419:     } elsif ($context eq 'login') {
 9420:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 9421:     } elsif ($context eq 'passwords') {
 9422:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
 9423:         $colspan=1;
 9424:     }
 9425:     if (ref($settings) eq 'HASH') {
 9426:         if ($settings->{'captcha'}) {
 9427:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 9428:         } else {
 9429:             $checked{'original'} = ' checked="checked"';
 9430:         }
 9431:         if ($settings->{'captcha'} eq 'recaptcha') {
 9432:             $pubtext = $lt{'pub'};
 9433:             $privtext = $lt{'priv'};
 9434:             $keyentry = 'text';
 9435:             $vertext = $lt{'ver'};
 9436:             $currver = $settings->{'recaptchaversion'};
 9437:             if ($currver ne '2') {
 9438:                 $currver = 1;
 9439:             }
 9440:         }
 9441:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 9442:             $currpub = $settings->{'recaptchakeys'}{'public'};
 9443:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 9444:         }
 9445:     } else {
 9446:         $checked{'original'} = ' checked="checked"';
 9447:     }
 9448:     my $css_class;
 9449:     if ($itemcount%2) {
 9450:         $css_class = 'LC_odd_row';
 9451:     }
 9452:     if ($customcss) {
 9453:         $css_class .= " $customcss";
 9454:     }
 9455:     $css_class =~ s/^\s+//;
 9456:     if ($css_class) {
 9457:         $css_class = ' class="'.$css_class.'"';
 9458:     }
 9459:     if ($rowstyle) {
 9460:         $css_class .= ' style="'.$rowstyle.'"';
 9461:     }
 9462:     my $output = '<tr'.$css_class.'>'.
 9463:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
 9464:                  '<table><tr><td>'."\n";
 9465:     foreach my $option ('original','recaptcha','notused') {
 9466:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 9467:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 9468:                    $lt{$option}.'</label></span>';
 9469:         unless ($option eq 'notused') {
 9470:             $output .= ('&nbsp;'x2)."\n";
 9471:         }
 9472:     }
 9473: #
 9474: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 9475: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 9476: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 9477: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 9478: #
 9479:     $output .= '</td></tr>'."\n".
 9480:                '<tr><td class="LC_zero_height">'."\n".
 9481:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 9482:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 9483:                $currpub.'" size="40" /></span><br />'."\n".
 9484:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 9485:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 9486:                $currpriv.'" size="40" /></span><br />'.
 9487:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 9488:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 9489:                $currver.'" size="3" /></span><br />'.
 9490:                '</td></tr></table>'."\n".
 9491:                '</td></tr>';
 9492:     return $output;
 9493: }
 9494: 
 9495: sub user_formats_row {
 9496:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 9497:     my $output;
 9498:     my %text = (
 9499:                    'username' => 'new usernames',
 9500:                    'id'       => 'IDs',
 9501:                );
 9502:     unless ($type eq 'email') {
 9503:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 9504:         $output = '<tr '.$css_class.'>'.
 9505:                   '<td><span class="LC_nobreak">'.
 9506:                   &mt("Format rules to check for $text{$type}: ").
 9507:                   '</td><td class="LC_left_item" colspan="2"><table>';
 9508:     }
 9509:     my $rem;
 9510:     if (ref($ruleorder) eq 'ARRAY') {
 9511:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 9512:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 9513:                 my $rem = $i%($numinrow);
 9514:                 if ($rem == 0) {
 9515:                     if ($i > 0) {
 9516:                         $output .= '</tr>';
 9517:                     }
 9518:                     $output .= '<tr>';
 9519:                 }
 9520:                 my $check = ' ';
 9521:                 if (ref($settings) eq 'HASH') {
 9522:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 9523:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 9524:                             $check = ' checked="checked" ';
 9525:                         }
 9526:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 9527:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 9528:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 9529:                                 $check = ' checked="checked" ';
 9530:                             }
 9531:                         }
 9532:                     }
 9533:                 }
 9534:                 my $name = $type.'_rule';
 9535:                 if ($type eq 'email') {
 9536:                     $name .= '_'.$status;
 9537:                 }
 9538:                 $output .= '<td class="LC_left_item">'.
 9539:                            '<span class="LC_nobreak"><label>'.
 9540:                            '<input type="checkbox" name="'.$name.'" '.
 9541:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 9542:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 9543:             }
 9544:         }
 9545:         $rem = @{$ruleorder}%($numinrow);
 9546:     }
 9547:     my $colsleft;
 9548:     if ($rem) {
 9549:         $colsleft = $numinrow - $rem;
 9550:     }
 9551:     if ($colsleft > 1 ) {
 9552:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9553:                    '&nbsp;</td>';
 9554:     } elsif ($colsleft == 1) {
 9555:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 9556:     }
 9557:     $output .= '</tr></table>';
 9558:     unless ($type eq 'email') {
 9559:         $output .= '</td></tr>';
 9560:     }
 9561:     return $output;
 9562: }
 9563: 
 9564: sub usercreation_types {
 9565:     my %lt = &Apache::lonlocal::texthash (
 9566:                     author     => 'When adding a co-author',
 9567:                     course     => 'When adding a user to a course',
 9568:                     requestcrs => 'When requesting a course',
 9569:                     any        => 'Any',
 9570:                     official   => 'Institutional only ',
 9571:                     unofficial => 'Non-institutional only',
 9572:                     none       => 'None',
 9573:     );
 9574:     return %lt;
 9575: }
 9576: 
 9577: sub selfcreation_types {
 9578:     my %lt = &Apache::lonlocal::texthash (
 9579:                     selfcreate => 'User creates own account',
 9580:                     any        => 'Any',
 9581:                     official   => 'Institutional only ',
 9582:                     unofficial => 'Non-institutional only',
 9583:                     email      => 'E-mail address',
 9584:                     login      => 'Institutional Login',
 9585:                     sso        => 'SSO',
 9586:              );
 9587: }
 9588: 
 9589: sub authtype_names {
 9590:     my %lt = &Apache::lonlocal::texthash(
 9591:                       int    => 'Internal',
 9592:                       krb4   => 'Kerberos 4',
 9593:                       krb5   => 'Kerberos 5',
 9594:                       loc    => 'Local',
 9595:                       lti    => 'LTI',
 9596:                   );
 9597:     return %lt;
 9598: }
 9599: 
 9600: sub context_names {
 9601:     my %context_title = &Apache::lonlocal::texthash(
 9602:        author => 'Creating users when an Author',
 9603:        course => 'Creating users when in a course',
 9604:        domain => 'Creating users when a Domain Coordinator',
 9605:     );
 9606:     return %context_title;
 9607: }
 9608: 
 9609: sub print_usermodification {
 9610:     my ($position,$dom,$settings,$rowtotal) = @_;
 9611:     my $numinrow = 4;
 9612:     my ($context,$datatable,$rowcount);
 9613:     if ($position eq 'top') {
 9614:         $rowcount = 0;
 9615:         $context = 'author'; 
 9616:         foreach my $role ('ca','aa') {
 9617:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 9618:                                                    $numinrow,$rowcount);
 9619:             $$rowtotal ++;
 9620:             $rowcount ++;
 9621:         }
 9622:     } elsif ($position eq 'bottom') {
 9623:         $context = 'course';
 9624:         $rowcount = 0;
 9625:         foreach my $role ('st','ep','ta','in','cr') {
 9626:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 9627:                                                    $numinrow,$rowcount);
 9628:             $$rowtotal ++;
 9629:             $rowcount ++;
 9630:         }
 9631:     }
 9632:     return $datatable;
 9633: }
 9634: 
 9635: sub print_defaults {
 9636:     my ($position,$dom,$settings,$rowtotal) = @_;
 9637:     my $rownum = 0;
 9638:     my ($datatable,$css_class,$titles);
 9639:     unless ($position eq 'bottom') {
 9640:         $titles = &defaults_titles($dom);
 9641:     }
 9642:     if ($position eq 'top') {
 9643:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 9644:                      'datelocale_def','portal_def');
 9645:         my %defaults;
 9646:         if (ref($settings) eq 'HASH') {
 9647:             %defaults = %{$settings};
 9648:         } else {
 9649:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9650:             foreach my $item (@items) {
 9651:                 $defaults{$item} = $domdefaults{$item};
 9652:             }
 9653:         }
 9654:         foreach my $item (@items) {
 9655:             if ($rownum%2) {
 9656:                 $css_class = '';
 9657:             } else {
 9658:                 $css_class = ' class="LC_odd_row" ';
 9659:             }
 9660:             $datatable .= '<tr'.$css_class.'>'.
 9661:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 9662:                           '</span></td><td class="LC_right_item" colspan="3">';
 9663:             if ($item eq 'auth_def') {
 9664:                 my @authtypes = ('internal','krb4','krb5','localauth','lti');
 9665:                 my %shortauth = (
 9666:                                  internal => 'int',
 9667:                                  krb4 => 'krb4',
 9668:                                  krb5 => 'krb5',
 9669:                                  localauth  => 'loc',
 9670:                                  lti => 'lti',
 9671:                                 );
 9672:                 my %authnames = &authtype_names();
 9673:                 foreach my $auth (@authtypes) {
 9674:                     my $checked = ' ';
 9675:                     if ($defaults{$item} eq $auth) {
 9676:                         $checked = ' checked="checked" ';
 9677:                     }
 9678:                     $datatable .= '<label><input type="radio" name="'.$item.
 9679:                                   '" value="'.$auth.'"'.$checked.'/>'.
 9680:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 9681:                 }
 9682:             } elsif ($item eq 'timezone_def') {
 9683:                 my $includeempty = 1;
 9684:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 9685:             } elsif ($item eq 'datelocale_def') {
 9686:                 my $includeempty = 1;
 9687:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 9688:             } elsif ($item eq 'lang_def') {
 9689:                 my $includeempty = 1;
 9690:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 9691:             } else {
 9692:                 my $size;
 9693:                 if ($item eq 'portal_def') {
 9694:                     $size = ' size="25"';
 9695:                 }
 9696:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 9697:                               $defaults{$item}.'"'.$size.' />';
 9698:             }
 9699:             $datatable .= '</td></tr>';
 9700:             $rownum ++;
 9701:         }
 9702:     } else {
 9703:         my %defaults;
 9704:         if (ref($settings) eq 'HASH') {
 9705:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 9706:                 my $maxnum = @{$settings->{'inststatusorder'}};
 9707:                 for (my $i=0; $i<$maxnum; $i++) {
 9708:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 9709:                     my $item = $settings->{'inststatusorder'}->[$i];
 9710:                     my $title = $settings->{'inststatustypes'}->{$item};
 9711:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 9712:                     $datatable .= '<tr'.$css_class.'>'.
 9713:                                   '<td><span class="LC_nobreak">'.
 9714:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 9715:                     for (my $k=0; $k<=$maxnum; $k++) {
 9716:                         my $vpos = $k+1;
 9717:                         my $selstr;
 9718:                         if ($k == $i) {
 9719:                             $selstr = ' selected="selected" ';
 9720:                         }
 9721:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 9722:                     }
 9723:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 9724:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 9725:                                   &mt('delete').'</span></td>'.
 9726:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed').':'.
 9727:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 9728:                                   '</span></td></tr>';
 9729:                 }
 9730:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 9731:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 9732:                 $datatable .= '<tr '.$css_class.'>'.
 9733:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 9734:                 for (my $k=0; $k<=$maxnum; $k++) {
 9735:                     my $vpos = $k+1;
 9736:                     my $selstr;
 9737:                     if ($k == $maxnum) {
 9738:                         $selstr = ' selected="selected" ';
 9739:                     }
 9740:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 9741:                 }
 9742:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 9743:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 9744:                               '&nbsp;'.&mt('(new)').
 9745:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
 9746:                               &mt('Name displayed').':'.
 9747:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 9748:                               '</tr>'."\n";
 9749:                 $rownum ++;
 9750:             }
 9751:         }
 9752:     }
 9753:     $$rowtotal += $rownum;
 9754:     return $datatable;
 9755: }
 9756: 
 9757: sub get_languages_hash {
 9758:     my %langchoices;
 9759:     foreach my $id (&Apache::loncommon::languageids()) {
 9760:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 9761:         if ($code ne '') {
 9762:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 9763:         }
 9764:     }
 9765:     return %langchoices;
 9766: }
 9767: 
 9768: sub defaults_titles {
 9769:     my ($dom) = @_;
 9770:     my %titles = &Apache::lonlocal::texthash (
 9771:                    'auth_def'      => 'Default authentication type',
 9772:                    'auth_arg_def'  => 'Default authentication argument',
 9773:                    'lang_def'      => 'Default language',
 9774:                    'timezone_def'  => 'Default timezone',
 9775:                    'datelocale_def' => 'Default locale for dates',
 9776:                    'portal_def'     => 'Portal/Default URL',
 9777:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 9778:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 9779:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 9780:                  );
 9781:     if ($dom) {
 9782:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 9783:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 9784:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 9785:         $protocol = 'http' if ($protocol ne 'https');
 9786:         if ($uint_dom) {
 9787:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 9788:                                          $uint_dom);
 9789:         }
 9790:     }
 9791:     return (\%titles);
 9792: }
 9793: 
 9794: sub print_scantron {
 9795:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
 9796:     if ($position eq 'top') {
 9797:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 9798:     } else {
 9799:         return &print_scantronconfig($dom,$settings,\$rowtotal);
 9800:     }
 9801: }
 9802: 
 9803: sub scantron_javascript {
 9804:     return <<"ENDSCRIPT";
 9805: 
 9806: <script type="text/javascript">
 9807: // <![CDATA[
 9808: 
 9809: function toggleScantron(form) {
 9810:     var csvfieldset = new Array();
 9811:     if (document.getElementById('scantroncsv_cols')) {
 9812:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
 9813:     }
 9814:     if (document.getElementById('scantroncsv_options')) {
 9815:         csvfieldset.push(document.getElementById('scantroncsv_options'));
 9816:     }
 9817:     if (csvfieldset.length) {
 9818:         if (document.getElementById('scantronconfcsv')) {
 9819:             var scantroncsv = document.getElementById('scantronconfcsv');
 9820:             if (scantroncsv.checked) {
 9821:                 for (var i=0; i<csvfieldset.length; i++) {
 9822:                     csvfieldset[i].style.display = 'block';
 9823:                 }
 9824:             } else {
 9825:                 for (var i=0; i<csvfieldset.length; i++) {
 9826:                     csvfieldset[i].style.display = 'none';
 9827:                 }
 9828:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
 9829:                 if (csvselects.length) {
 9830:                     for (var j=0; j<csvselects.length; j++) {
 9831:                         csvselects[j].selectedIndex = 0;
 9832:                     }
 9833:                 }
 9834:             }
 9835:         }
 9836:     }
 9837:     return;
 9838: }
 9839: // ]]>
 9840: </script>
 9841: 
 9842: ENDSCRIPT
 9843: 
 9844: }
 9845: 
 9846: sub print_scantronformat {
 9847:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 9848:     my $itemcount = 1;
 9849:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 9850:         %confhash);
 9851:     my $switchserver = &check_switchserver($dom,$confname);
 9852:     my %lt = &Apache::lonlocal::texthash (
 9853:                 default => 'Default bubblesheet format file error',
 9854:                 custom  => 'Custom bubblesheet format file error',
 9855:              );
 9856:     my %scantronfiles = (
 9857:         default => 'default.tab',
 9858:         custom => 'custom.tab',
 9859:     );
 9860:     foreach my $key (keys(%scantronfiles)) {
 9861:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 9862:                               .$scantronfiles{$key};
 9863:     }
 9864:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 9865:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 9866:         if (!$switchserver) {
 9867:             my $servadm = $r->dir_config('lonAdmEMail');
 9868:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 9869:             if ($configuserok eq 'ok') {
 9870:                 if ($author_ok eq 'ok') {
 9871:                     my %legacyfile = (
 9872:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
 9873:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
 9874:                     );
 9875:                     my %md5chk;
 9876:                     foreach my $type (keys(%legacyfile)) {
 9877:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 9878:                         chomp($md5chk{$type});
 9879:                     }
 9880:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 9881:                         foreach my $type (keys(%legacyfile)) {
 9882:                             ($scantronurls{$type},my $error) =
 9883:                                 &legacy_scantronformat($r,$dom,$confname,
 9884:                                                  $type,$legacyfile{$type},
 9885:                                                  $scantronurls{$type},
 9886:                                                  $scantronfiles{$type});
 9887:                             if ($error ne '') {
 9888:                                 $error{$type} = $error;
 9889:                             }
 9890:                         }
 9891:                         if (keys(%error) == 0) {
 9892:                             $is_custom = 1;
 9893:                             $confhash{'scantron'}{'scantronformat'} =
 9894:                                 $scantronurls{'custom'};
 9895:                             my $putresult =
 9896:                                 &Apache::lonnet::put_dom('configuration',
 9897:                                                          \%confhash,$dom);
 9898:                             if ($putresult ne 'ok') {
 9899:                                 $error{'custom'} =
 9900:                                     '<span class="LC_error">'.
 9901:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 9902:                             }
 9903:                         }
 9904:                     } else {
 9905:                         ($scantronurls{'default'},my $error) =
 9906:                             &legacy_scantronformat($r,$dom,$confname,
 9907:                                           'default',$legacyfile{'default'},
 9908:                                           $scantronurls{'default'},
 9909:                                           $scantronfiles{'default'});
 9910:                         if ($error eq '') {
 9911:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 9912:                             my $putresult =
 9913:                                 &Apache::lonnet::put_dom('configuration',
 9914:                                                          \%confhash,$dom);
 9915:                             if ($putresult ne 'ok') {
 9916:                                 $error{'default'} =
 9917:                                     '<span class="LC_error">'.
 9918:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 9919:                             }
 9920:                         } else {
 9921:                             $error{'default'} = $error;
 9922:                         }
 9923:                     }
 9924:                 }
 9925:             }
 9926:         } else {
 9927:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 9928:         }
 9929:     }
 9930:     if (ref($settings) eq 'HASH') {
 9931:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 9932:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 9933:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 9934:                 $scantronurl = '';
 9935:             } else {
 9936:                 $scantronurl = $settings->{'scantronformat'};
 9937:             }
 9938:             $is_custom = 1;
 9939:         } else {
 9940:             $scantronurl = $scantronurls{'default'};
 9941:         }
 9942:     } else {
 9943:         if ($is_custom) {
 9944:             $scantronurl = $scantronurls{'custom'};
 9945:         } else {
 9946:             $scantronurl = $scantronurls{'default'};
 9947:         }
 9948:     }
 9949:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9950:     $datatable .= '<tr'.$css_class.'>';
 9951:     if (!$is_custom) {
 9952:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 9953:                       '<span class="LC_nobreak">';
 9954:         if ($scantronurl) {
 9955:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 9956:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 9957:         } else {
 9958:             $datatable = &mt('File unavailable for display');
 9959:         }
 9960:         $datatable .= '</span></td>';
 9961:         if (keys(%error) == 0) { 
 9962:             $datatable .= '<td style="vertical-align: bottom">';
 9963:             if (!$switchserver) {
 9964:                 $datatable .= &mt('Upload:').'<br />';
 9965:             }
 9966:         } else {
 9967:             my $errorstr;
 9968:             foreach my $key (sort(keys(%error))) {
 9969:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 9970:             }
 9971:             $datatable .= '<td>'.$errorstr;
 9972:         }
 9973:     } else {
 9974:         if (keys(%error) > 0) {
 9975:             my $errorstr;
 9976:             foreach my $key (sort(keys(%error))) {
 9977:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 9978:             } 
 9979:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 9980:         } elsif ($scantronurl) {
 9981:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 9982:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 9983:             $datatable .= '<td><span class="LC_nobreak">'.
 9984:                           $link.
 9985:                           '<label><input type="checkbox" name="scantronformat_del"'.
 9986:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 9987:                           '<td><span class="LC_nobreak">&nbsp;'.
 9988:                           &mt('Replace:').'</span><br />';
 9989:         }
 9990:     }
 9991:     if (keys(%error) == 0) {
 9992:         if ($switchserver) {
 9993:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 9994:         } else {
 9995:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 9996:                          '<input type="file" name="scantronformat" /></span>';
 9997:         }
 9998:     }
 9999:     $datatable .= '</td></tr>';
10000:     $$rowtotal ++;
10001:     return $datatable;
10002: }
10003: 
10004: sub legacy_scantronformat {
10005:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
10006:     my ($url,$error);
10007:     my @statinfo = &Apache::lonnet::stat_file($newurl);
10008:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
10009:         (my $result,$url) =
10010:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
10011:                          '','',$newfile);
10012:         if ($result ne 'ok') {
10013:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
10014:         }
10015:     }
10016:     return ($url,$error);
10017: }
10018: 
10019: sub print_scantronconfig {
10020:     my ($dom,$settings,$rowtotal) = @_;
10021:     my $itemcount = 2;
10022:     my $is_checked = ' checked="checked"';
10023:     my %optionson = (
10024:                      hdr => ' checked="checked"',
10025:                      pad => ' checked="checked"',
10026:                      rem => ' checked="checked"',
10027:                     );
10028:     my %optionsoff = (
10029:                       hdr => '',
10030:                       pad => '',
10031:                       rem => '',
10032:                      );
10033:     my $currcsvsty = 'none';
10034:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
10035:     my @fields = &scantroncsv_fields();
10036:     my %titles = &scantronconfig_titles();
10037:     if (ref($settings) eq 'HASH') {
10038:         if (ref($settings->{config}) eq 'HASH') {
10039:             if ($settings->{config}->{dat}) {
10040:                 $checked{'dat'} = $is_checked;
10041:             }
10042:             if (ref($settings->{config}->{csv}) eq 'HASH') {
10043:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
10044:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
10045:                     if (keys(%csvfields) > 0) {
10046:                         $checked{'csv'} = $is_checked;
10047:                         $currcsvsty = 'block';
10048:                     }
10049:                 }
10050:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
10051:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
10052:                     foreach my $option (keys(%optionson)) {
10053:                         unless ($csvoptions{$option}) {
10054:                             $optionsoff{$option} = $optionson{$option};
10055:                             $optionson{$option} = '';
10056:                         }
10057:                     }
10058:                 }
10059:             }
10060:         } else {
10061:             $checked{'dat'} = $is_checked;
10062:         }
10063:     } else {
10064:         $checked{'dat'} = $is_checked;
10065:     }
10066:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
10067:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
10068:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
10069:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
10070:     foreach my $item ('dat','csv') {
10071:         my $id;
10072:         if ($item eq 'csv') {
10073:             $id = 'id="scantronconfcsv" ';
10074:         }
10075:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
10076:                       $titles{$item}.'</label>'.('&nbsp;'x3);
10077:         if ($item eq 'csv') {
10078:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
10079:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
10080:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
10081:             foreach my $col (@fields) {
10082:                 my $selnone;
10083:                 if ($csvfields{$col} eq '') {
10084:                     $selnone = ' selected="selected"';
10085:                 }
10086:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
10087:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
10088:                               '<option value=""'.$selnone.'></option>';
10089:                 for (my $i=0; $i<20; $i++) {
10090:                     my $shown = $i+1;
10091:                     my $sel;
10092:                     unless ($selnone) {
10093:                         if (exists($csvfields{$col})) {
10094:                             if ($csvfields{$col} == $i) {
10095:                                 $sel = ' selected="selected"';
10096:                             }
10097:                         }
10098:                     }
10099:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
10100:                 }
10101:                 $datatable .= '</select></td></tr>';
10102:            }
10103:            $datatable .= '</table></fieldset>'.
10104:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
10105:                          '<legend>'.&mt('CSV Options').'</legend>';
10106:            foreach my $option ('hdr','pad','rem') {
10107:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
10108:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
10109:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
10110:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
10111:            }
10112:            $datatable .= '</fieldset>';
10113:            $itemcount ++;
10114:         }
10115:     }
10116:     $datatable .= '</td></tr>';
10117:     $$rowtotal ++;
10118:     return $datatable;
10119: }
10120: 
10121: sub scantronconfig_titles {
10122:     return &Apache::lonlocal::texthash(
10123:                                           dat => 'Standard format (.dat)',
10124:                                           csv => 'Comma separated values (.csv)',
10125:                                           hdr => 'Remove first line in file (contains column titles)',
10126:                                           pad => 'Prepend 0s to PaperID',
10127:                                           rem => 'Remove leading spaces (except Question Response columns)',
10128:                                           CODE => 'CODE',
10129:                                           ID   => 'Student ID',
10130:                                           PaperID => 'Paper ID',
10131:                                           FirstName => 'First Name',
10132:                                           LastName => 'Last Name',
10133:                                           FirstQuestion => 'First Question Response',
10134:                                           Section => 'Section',
10135:     );
10136: }
10137: 
10138: sub scantroncsv_fields {
10139:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
10140: }
10141: 
10142: sub print_coursecategories {
10143:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
10144:     my $datatable;
10145:     if ($position eq 'top') {
10146:         my (%checked);
10147:         my @catitems = ('unauth','auth');
10148:         my @cattypes = ('std','domonly','codesrch','none');
10149:         $checked{'unauth'} = 'std';
10150:         $checked{'auth'} = 'std';
10151:         if (ref($settings) eq 'HASH') {
10152:             foreach my $type (@cattypes) {
10153:                 if ($type eq $settings->{'unauth'}) {
10154:                     $checked{'unauth'} = $type;
10155:                 }
10156:                 if ($type eq $settings->{'auth'}) {
10157:                     $checked{'auth'} = $type;
10158:                 }
10159:             }
10160:         }
10161:         my %lt = &Apache::lonlocal::texthash (
10162:                                                unauth   => 'Catalog type for unauthenticated users',
10163:                                                auth     => 'Catalog type for authenticated users',
10164:                                                none     => 'No catalog',
10165:                                                std      => 'Standard catalog',
10166:                                                domonly  => 'Domain-only catalog',
10167:                                                codesrch => "Code search form",
10168:                                              );
10169:        my $itemcount = 0;
10170:        foreach my $item (@catitems) {
10171:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
10172:            $datatable .= '<tr '.$css_class.'>'.
10173:                          '<td>'.$lt{$item}.'</td>'.
10174:                          '<td class="LC_right_item"><span class="LC_nobreak">';
10175:            foreach my $type (@cattypes) {
10176:                my $ischecked;
10177:                if ($checked{$item} eq $type) {
10178:                    $ischecked=' checked="checked"';
10179:                }
10180:                $datatable .= '<label>'.
10181:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
10182:                              ' />'.$lt{$type}.'</label>&nbsp;';
10183:            }
10184:            $datatable .= '</span></td></tr>';
10185:            $itemcount ++;
10186:         }
10187:         $$rowtotal += $itemcount;
10188:     } elsif ($position eq 'middle') {
10189:         my $toggle_cats_crs = ' ';
10190:         my $toggle_cats_dom = ' checked="checked" ';
10191:         my $can_cat_crs = ' ';
10192:         my $can_cat_dom = ' checked="checked" ';
10193:         my $toggle_catscomm_comm = ' ';
10194:         my $toggle_catscomm_dom = ' checked="checked" ';
10195:         my $can_catcomm_comm = ' ';
10196:         my $can_catcomm_dom = ' checked="checked" ';
10197:         my $toggle_catsplace_place = ' ';
10198:         my $toggle_catsplace_dom = ' checked="checked" ';
10199:         my $can_catplace_place = ' ';
10200:         my $can_catplace_dom = ' checked="checked" ';
10201: 
10202:         if (ref($settings) eq 'HASH') {
10203:             if ($settings->{'togglecats'} eq 'crs') {
10204:                 $toggle_cats_crs = $toggle_cats_dom;
10205:                 $toggle_cats_dom = ' ';
10206:             }
10207:             if ($settings->{'categorize'} eq 'crs') {
10208:                 $can_cat_crs = $can_cat_dom;
10209:                 $can_cat_dom = ' ';
10210:             }
10211:             if ($settings->{'togglecatscomm'} eq 'comm') {
10212:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
10213:                 $toggle_catscomm_dom = ' ';
10214:             }
10215:             if ($settings->{'categorizecomm'} eq 'comm') {
10216:                 $can_catcomm_comm = $can_catcomm_dom;
10217:                 $can_catcomm_dom = ' ';
10218:             }
10219:             if ($settings->{'togglecatsplace'} eq 'place') {
10220:                 $toggle_catsplace_place = $toggle_catsplace_dom;
10221:                 $toggle_catsplace_dom = ' ';
10222:             }
10223:             if ($settings->{'categorizeplace'} eq 'place') {
10224:                 $can_catplace_place = $can_catplace_dom;
10225:                 $can_catplace_dom = ' ';
10226:             }
10227:         }
10228:         my %title = &Apache::lonlocal::texthash (
10229:                      togglecats      => 'Show/Hide a course in catalog',
10230:                      togglecatscomm  => 'Show/Hide a community in catalog',
10231:                      togglecatsplace => 'Show/Hide a placement test in catalog',
10232:                      categorize      => 'Assign a category to a course',
10233:                      categorizecomm  => 'Assign a category to a community',
10234:                      categorizeplace => 'Assign a category to a placement test',
10235:                     );
10236:         my %level = &Apache::lonlocal::texthash (
10237:                      dom   => 'Set in Domain',
10238:                      crs   => 'Set in Course',
10239:                      comm  => 'Set in Community',
10240:                      place => 'Set in Placement Test',
10241:                     );
10242:         $datatable = '<tr class="LC_odd_row">'.
10243:                   '<td>'.$title{'togglecats'}.'</td>'.
10244:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
10245:                   '<input type="radio" name="togglecats"'.
10246:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10247:                   '<label><input type="radio" name="togglecats"'.
10248:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
10249:                   '</tr><tr>'.
10250:                   '<td>'.$title{'categorize'}.'</td>'.
10251:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
10252:                   '<label><input type="radio" name="categorize"'.
10253:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10254:                   '<label><input type="radio" name="categorize"'.
10255:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
10256:                   '</tr><tr class="LC_odd_row">'.
10257:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
10258:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
10259:                   '<input type="radio" name="togglecatscomm"'.
10260:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10261:                   '<label><input type="radio" name="togglecatscomm"'.
10262:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
10263:                   '</tr><tr>'.
10264:                   '<td>'.$title{'categorizecomm'}.'</td>'.
10265:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
10266:                   '<label><input type="radio" name="categorizecomm"'.
10267:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10268:                   '<label><input type="radio" name="categorizecomm"'.
10269:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
10270:                   '</tr><tr class="LC_odd_row">'.
10271:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
10272:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
10273:                   '<input type="radio" name="togglecatsplace"'.
10274:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10275:                   '<label><input type="radio" name="togglecatscomm"'.
10276:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
10277:                   '</tr><tr>'.
10278:                   '<td>'.$title{'categorizeplace'}.'</td>'.
10279:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
10280:                   '<label><input type="radio" name="categorizeplace"'.
10281:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10282:                   '<label><input type="radio" name="categorizeplace"'.
10283:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
10284:                   '</tr>';
10285:         $$rowtotal += 6;
10286:     } else {
10287:         my $css_class;
10288:         my $itemcount = 1;
10289:         my $cathash; 
10290:         if (ref($settings) eq 'HASH') {
10291:             $cathash = $settings->{'cats'};
10292:         }
10293:         if (ref($cathash) eq 'HASH') {
10294:             my (@cats,@trails,%allitems,%idx,@jsarray);
10295:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
10296:                                                    \%allitems,\%idx,\@jsarray);
10297:             my $maxdepth = scalar(@cats);
10298:             my $colattrib = '';
10299:             if ($maxdepth > 2) {
10300:                 $colattrib = ' colspan="2" ';
10301:             }
10302:             my @path;
10303:             if (@cats > 0) {
10304:                 if (ref($cats[0]) eq 'ARRAY') {
10305:                     my $numtop = @{$cats[0]};
10306:                     my $maxnum = $numtop;
10307:                     my %default_names = (
10308:                           instcode    => &mt('Official courses'),
10309:                           communities => &mt('Communities'),
10310:                           placement   => &mt('Placement Tests'),
10311:                     );
10312: 
10313:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
10314:                         ($cathash->{'instcode::0'} eq '') ||
10315:                         (!grep(/^communities$/,@{$cats[0]})) || 
10316:                         ($cathash->{'communities::0'} eq '') ||
10317:                         (!grep(/^placement$/,@{$cats[0]})) ||
10318:                         ($cathash->{'placement::0'} eq '')) {
10319:                         $maxnum ++;
10320:                     }
10321:                     my $lastidx;
10322:                     for (my $i=0; $i<$numtop; $i++) {
10323:                         my $parent = $cats[0][$i];
10324:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
10325:                         my $item = &escape($parent).'::0';
10326:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
10327:                         $lastidx = $idx{$item};
10328:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
10329:                                       .'<select name="'.$item.'"'.$chgstr.'>';
10330:                         for (my $k=0; $k<=$maxnum; $k++) {
10331:                             my $vpos = $k+1;
10332:                             my $selstr;
10333:                             if ($k == $i) {
10334:                                 $selstr = ' selected="selected" ';
10335:                             }
10336:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10337:                         }
10338:                         $datatable .= '</select></span></td><td>';
10339:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
10340:                             $datatable .=  '<span class="LC_nobreak">'
10341:                                            .$default_names{$parent}.'</span>';
10342:                             if ($parent eq 'instcode') {
10343:                                 $datatable .= '<br /><span class="LC_nobreak">('
10344:                                               .&mt('with institutional codes')
10345:                                               .')</span></td><td'.$colattrib.'>';
10346:                             } else {
10347:                                 $datatable .= '<table><tr><td>';
10348:                             }
10349:                             $datatable .= '<span class="LC_nobreak">'
10350:                                           .'<label><input type="radio" name="'
10351:                                           .$parent.'" value="1" checked="checked" />'
10352:                                           .&mt('Display').'</label>';
10353:                             if ($parent eq 'instcode') {
10354:                                 $datatable .= '&nbsp;';
10355:                             } else {
10356:                                 $datatable .= '</span></td></tr><tr><td>'
10357:                                               .'<span class="LC_nobreak">';
10358:                             }
10359:                             $datatable .= '<label><input type="radio" name="'
10360:                                           .$parent.'" value="0" />'
10361:                                           .&mt('Do not display').'</label></span>';
10362:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
10363:                                 $datatable .= '</td></tr></table>';
10364:                             }
10365:                             $datatable .= '</td>';
10366:                         } else {
10367:                             $datatable .= $parent
10368:                                           .'&nbsp;<span class="LC_nobreak"><label>'
10369:                                           .'<input type="checkbox" name="deletecategory" '
10370:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
10371:                         }
10372:                         my $depth = 1;
10373:                         push(@path,$parent);
10374:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
10375:                         pop(@path);
10376:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
10377:                         $itemcount ++;
10378:                     }
10379:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
10380:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
10381:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
10382:                     for (my $k=0; $k<=$maxnum; $k++) {
10383:                         my $vpos = $k+1;
10384:                         my $selstr;
10385:                         if ($k == $numtop) {
10386:                             $selstr = ' selected="selected" ';
10387:                         }
10388:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10389:                     }
10390:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
10391:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
10392:                                   .'</tr>'."\n";
10393:                     $itemcount ++;
10394:                     foreach my $default ('instcode','communities','placement') {
10395:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
10396:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
10397:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
10398:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
10399:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
10400:                             for (my $k=0; $k<=$maxnum; $k++) {
10401:                                 my $vpos = $k+1;
10402:                                 my $selstr;
10403:                                 if ($k == $maxnum) {
10404:                                     $selstr = ' selected="selected" ';
10405:                                 }
10406:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10407:                             }
10408:                             $datatable .= '</select></span></td>'.
10409:                                           '<td><span class="LC_nobreak">'.
10410:                                           $default_names{$default}.'</span>';
10411:                             if ($default eq 'instcode') {
10412:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
10413:                                               .&mt('with institutional codes').')</span>';
10414:                             }
10415:                             $datatable .= '</td>'
10416:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
10417:                                           .&mt('Display').'</label>&nbsp;'
10418:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
10419:                                           .&mt('Do not display').'</label></span></td></tr>';
10420:                         }
10421:                     }
10422:                 }
10423:             } else {
10424:                 $datatable .= &initialize_categories($itemcount);
10425:             }
10426:         } else {
10427:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
10428:                           .&initialize_categories($itemcount);
10429:         }
10430:         $$rowtotal += $itemcount;
10431:     }
10432:     return $datatable;
10433: }
10434: 
10435: sub print_serverstatuses {
10436:     my ($dom,$settings,$rowtotal) = @_;
10437:     my $datatable;
10438:     my @pages = &serverstatus_pages();
10439:     my (%namedaccess,%machineaccess);
10440:     foreach my $type (@pages) {
10441:         $namedaccess{$type} = '';
10442:         $machineaccess{$type}= '';
10443:     }
10444:     if (ref($settings) eq 'HASH') {
10445:         foreach my $type (@pages) {
10446:             if (exists($settings->{$type})) {
10447:                 if (ref($settings->{$type}) eq 'HASH') {
10448:                     foreach my $key (keys(%{$settings->{$type}})) {
10449:                         if ($key eq 'namedusers') {
10450:                             $namedaccess{$type} = $settings->{$type}->{$key};
10451:                         } elsif ($key eq 'machines') {
10452:                             $machineaccess{$type} = $settings->{$type}->{$key};
10453:                         }
10454:                     }
10455:                 }
10456:             }
10457:         }
10458:     }
10459:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
10460:     my $rownum = 0;
10461:     my $css_class;
10462:     foreach my $type (@pages) {
10463:         $rownum ++;
10464:         $css_class = $rownum%2?' class="LC_odd_row"':'';
10465:         $datatable .= '<tr'.$css_class.'>'.
10466:                       '<td><span class="LC_nobreak">'.
10467:                       $titles->{$type}.'</span></td>'.
10468:                       '<td class="LC_left_item">'.
10469:                       '<input type="text" name="'.$type.'_namedusers" '.
10470:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
10471:                       '<td class="LC_right_item">'.
10472:                       '<span class="LC_nobreak">'.
10473:                       '<input type="text" name="'.$type.'_machines" '.
10474:                       'value="'.$machineaccess{$type}.'" size="10" />'.
10475:                       '</span></td></tr>'."\n";
10476:     }
10477:     $$rowtotal += $rownum;
10478:     return $datatable;
10479: }
10480: 
10481: sub serverstatus_pages {
10482:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
10483:             'checksums','clusterstatus','certstatus','metadata_keywords',
10484:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
10485:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
10486: }
10487: 
10488: sub defaults_javascript {
10489:     my ($settings) = @_;
10490:     return unless (ref($settings) eq 'HASH');
10491:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
10492:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
10493:         if ($maxnum eq '') {
10494:             $maxnum = 0;
10495:         }
10496:         $maxnum ++;
10497:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
10498:         return <<"ENDSCRIPT";
10499: <script type="text/javascript">
10500: // <![CDATA[
10501: function reorderTypes(form,caller) {
10502:     var changedVal;
10503: $jstext 
10504:     var newpos = 'addinststatus_pos';
10505:     var current = new Array;
10506:     var maxh = $maxnum;
10507:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
10508:     var oldVal;
10509:     if (caller == newpos) {
10510:         changedVal = newitemVal;
10511:     } else {
10512:         var curritem = 'inststatus_pos_'+caller;
10513:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
10514:         current[newitemVal] = newpos;
10515:     }
10516:     for (var i=0; i<inststatuses.length; i++) {
10517:         if (inststatuses[i] != caller) {
10518:             var elementName = 'inststatus_pos_'+inststatuses[i];
10519:             if (form.elements[elementName]) {
10520:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
10521:                 current[currVal] = elementName;
10522:             }
10523:         }
10524:     }
10525:     for (var j=0; j<maxh; j++) {
10526:         if (current[j] == undefined) {
10527:             oldVal = j;
10528:         }
10529:     }
10530:     if (oldVal < changedVal) {
10531:         for (var k=oldVal+1; k<=changedVal ; k++) {
10532:            var elementName = current[k];
10533:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
10534:         }
10535:     } else {
10536:         for (var k=changedVal; k<oldVal; k++) {
10537:             var elementName = current[k];
10538:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
10539:         }
10540:     }
10541:     return;
10542: }
10543: 
10544: // ]]>
10545: </script>
10546: 
10547: ENDSCRIPT
10548:     }
10549:     return;
10550: }
10551: 
10552: sub passwords_javascript {
10553:     my %intalert = &Apache::lonlocal::texthash (
10554:         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.',
10555:         authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
10556:         passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
10557:         passmax => 'Warning: maximum password length must be a positive integer (or blank).',
10558:         passexp => 'Warning: days before password expiration must be a positive integer (or blank).',
10559:         passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
10560:     );
10561:     &js_escape(\%intalert);
10562:     my $defmin = $Apache::lonnet::passwdmin;
10563:     my $intauthjs = <<"ENDSCRIPT";
10564: 
10565: function warnIntAuth(field) {
10566:     if (field.name == 'intauth_check') {
10567:         if (field.value == '2') {
10568:             alert('$intalert{authcheck}');
10569:         }
10570:     }
10571:     if (field.name == 'intauth_cost') {
10572:         field.value.replace(/\s/g,'');
10573:         if (field.value != '') {
10574:             var regexdigit=/^\\d+\$/;
10575:             if (!regexdigit.test(field.value)) {
10576:                 alert('$intalert{authcost}');
10577:             }
10578:         }
10579:     }
10580:     return;
10581: }
10582: 
10583: function warnIntPass(field) {
10584:     field.value.replace(/^\s+/,'');
10585:     field.value.replace(/\s+\$/,'');
10586:     var regexdigit=/^\\d+\$/;
10587:     if (field.name == 'passwords_min') {
10588:         if (field.value == '') {
10589:             alert('$intalert{passmin}');
10590:             field.value = '$defmin';
10591:         } else {
10592:             if (!regexdigit.test(field.value)) {
10593:                 alert('$intalert{passmin}');
10594:                 field.value = '$defmin';
10595:             }
10596:             var minval = parseInt(field.value,10);
10597:             if (minval < $defmin) {
10598:                 alert('$intalert{passmin}');
10599:                 field.value = '$defmin';
10600:             }
10601:         }
10602:     } else {
10603:         if (field.value == '0') {
10604:             field.value = '';
10605:         }
10606:         if (field.value != '') {
10607:             if (field.name == 'passwords_expire') {
10608:                 var regexpposnum=/^\\d+(|\\.\\d*)\$/; 
10609:                 if (!regexpposnum.test(field.value)) {
10610:                     alert('$intalert{passexp}');
10611:                     field.value = '';
10612:                 } else {
10613:                     var expval = parseFloat(field.value);
10614:                     if (expval == 0) {
10615:                         alert('$intalert{passexp}');
10616:                         field.value = '';
10617:                     }
10618:                 }
10619:             } else {
10620:                 if (!regexdigit.test(field.value)) {
10621:                     if (field.name == 'passwords_max') {
10622:                         alert('$intalert{passmax}');
10623:                     } else {
10624:                         if (field.name == 'passwords_numsaved') {
10625:                             alert('$intalert{passnum}');
10626:                         }
10627:                     }
10628:                     field.value = '';
10629:                 }
10630:             }
10631:         }
10632:     }
10633:     return;
10634: }
10635: 
10636: ENDSCRIPT
10637:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
10638: }
10639: 
10640: sub coursecategories_javascript {
10641:     my ($settings) = @_;
10642:     my ($output,$jstext,$cathash);
10643:     if (ref($settings) eq 'HASH') {
10644:         $cathash = $settings->{'cats'};
10645:     }
10646:     if (ref($cathash) eq 'HASH') {
10647:         my (@cats,@jsarray,%idx);
10648:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
10649:         if (@jsarray > 0) {
10650:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
10651:             for (my $i=0; $i<@jsarray; $i++) {
10652:                 if (ref($jsarray[$i]) eq 'ARRAY') {
10653:                     my $catstr = join('","',@{$jsarray[$i]});
10654:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
10655:                 }
10656:             }
10657:         }
10658:     } else {
10659:         $jstext  = '    var categories = Array(1);'."\n".
10660:                    '    categories[0] = Array("instcode_pos");'."\n"; 
10661:     }
10662:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
10663:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
10664:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
10665:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
10666:     &js_escape(\$instcode_reserved);
10667:     &js_escape(\$communities_reserved);
10668:     &js_escape(\$placement_reserved);
10669:     &js_escape(\$choose_again);
10670:     $output = <<"ENDSCRIPT";
10671: <script type="text/javascript">
10672: // <![CDATA[
10673: function reorderCats(form,parent,item,idx) {
10674:     var changedVal;
10675: $jstext
10676:     var newpos = 'addcategory_pos';
10677:     if (parent == '') {
10678:         var has_instcode = 0;
10679:         var maxtop = categories[idx].length;
10680:         for (var j=0; j<maxtop; j++) {
10681:             if (categories[idx][j] == 'instcode::0') {
10682:                 has_instcode == 1;
10683:             }
10684:         }
10685:         if (has_instcode == 0) {
10686:             categories[idx][maxtop] = 'instcode_pos';
10687:         }
10688:     } else {
10689:         newpos += '_'+parent;
10690:     }
10691:     var maxh = 1 + categories[idx].length;
10692:     var current = new Array;
10693:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
10694:     if (item == newpos) {
10695:         changedVal = newitemVal;
10696:     } else {
10697:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
10698:         current[newitemVal] = newpos;
10699:     }
10700:     for (var i=0; i<categories[idx].length; i++) {
10701:         var elementName = categories[idx][i];
10702:         if (elementName != item) {
10703:             if (form.elements[elementName]) {
10704:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
10705:                 current[currVal] = elementName;
10706:             }
10707:         }
10708:     }
10709:     var oldVal;
10710:     for (var j=0; j<maxh; j++) {
10711:         if (current[j] == undefined) {
10712:             oldVal = j;
10713:         }
10714:     }
10715:     if (oldVal < changedVal) {
10716:         for (var k=oldVal+1; k<=changedVal ; k++) {
10717:            var elementName = current[k];
10718:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
10719:         }
10720:     } else {
10721:         for (var k=changedVal; k<oldVal; k++) {
10722:             var elementName = current[k];
10723:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
10724:         }
10725:     }
10726:     return;
10727: }
10728: 
10729: function categoryCheck(form) {
10730:     if (form.elements['addcategory_name'].value == 'instcode') {
10731:         alert('$instcode_reserved\\n$choose_again');
10732:         return false;
10733:     }
10734:     if (form.elements['addcategory_name'].value == 'communities') {
10735:         alert('$communities_reserved\\n$choose_again');
10736:         return false;
10737:     }
10738:     if (form.elements['addcategory_name'].value == 'placement') {
10739:         alert('$placement_reserved\\n$choose_again');
10740:         return false;
10741:     }
10742:     return true;
10743: }
10744: 
10745: // ]]>
10746: </script>
10747: 
10748: ENDSCRIPT
10749:     return $output;
10750: }
10751: 
10752: sub initialize_categories {
10753:     my ($itemcount) = @_;
10754:     my ($datatable,$css_class,$chgstr);
10755:     my %default_names = &Apache::lonlocal::texthash (
10756:                       instcode    => 'Official courses (with institutional codes)',
10757:                       communities => 'Communities',
10758:                       placement   => 'Placement Tests',
10759:                         );
10760:     my %selnum = (
10761:                    instcode    => '0',
10762:                    communities => '1',
10763:                    placement   => '2',
10764:                  );
10765:     my %selected;
10766:     foreach my $default ('instcode','communities','placement') {
10767:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
10768:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
10769:         map { $selected{$selnum{$_}} = '' } keys(%selnum);
10770:         $selected{$selnum{$default}} = ' selected="selected"';
10771:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
10772:                      .'<select name="'.$default.'_pos"'.$chgstr.'>'
10773:                      .'<option value="0"'.$selected{'0'}.'>1</option>'
10774:                      .'<option value="1"'.$selected{'1'}.'>2</option>'
10775:                      .'<option value="2"'.$selected{'2'}.'>3</option>'
10776:                      .'<option value="3">4</option></select>&nbsp;'
10777:                      .$default_names{$default}
10778:                      .'</span></td><td><span class="LC_nobreak">'
10779:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
10780:                      .&mt('Display').'</label>&nbsp;<label>'
10781:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
10782:                  .'</label></span></td></tr>';
10783:         $itemcount ++;
10784:     }
10785:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
10786:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
10787:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
10788:                   .'<select name="addcategory_pos"'.$chgstr.'>'
10789:                   .'<option value="0">1</option>'
10790:                   .'<option value="1">2</option>'
10791:                   .'<option value="2">3</option>'
10792:                   .'<option value="3" selected="selected">4</option></select>&nbsp;'
10793:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
10794:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
10795:                   .'</td></tr>';
10796:     return $datatable;
10797: }
10798: 
10799: sub build_category_rows {
10800:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
10801:     my ($text,$name,$item,$chgstr);
10802:     if (ref($cats) eq 'ARRAY') {
10803:         my $maxdepth = scalar(@{$cats});
10804:         if (ref($cats->[$depth]) eq 'HASH') {
10805:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
10806:                 my $numchildren = @{$cats->[$depth]{$parent}};
10807:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
10808:                 $text .= '<td><table class="LC_data_table">';
10809:                 my ($idxnum,$parent_name,$parent_item);
10810:                 my $higher = $depth - 1;
10811:                 if ($higher == 0) {
10812:                     $parent_name = &escape($parent).'::'.$higher;
10813:                 } else {
10814:                     if (ref($path) eq 'ARRAY') {
10815:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
10816:                     }
10817:                 }
10818:                 $parent_item = 'addcategory_pos_'.$parent_name;
10819:                 for (my $j=0; $j<=$numchildren; $j++) {
10820:                     if ($j < $numchildren) {
10821:                         $name = $cats->[$depth]{$parent}[$j];
10822:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
10823:                         $idxnum = $idx->{$item};
10824:                     } else {
10825:                         $name = $parent_name;
10826:                         $item = $parent_item;
10827:                     }
10828:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
10829:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
10830:                     for (my $i=0; $i<=$numchildren; $i++) {
10831:                         my $vpos = $i+1;
10832:                         my $selstr;
10833:                         if ($j == $i) {
10834:                             $selstr = ' selected="selected" ';
10835:                         }
10836:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
10837:                     }
10838:                     $text .= '</select>&nbsp;';
10839:                     if ($j < $numchildren) {
10840:                         my $deeper = $depth+1;
10841:                         $text .= $name.'&nbsp;'
10842:                                  .'<label><input type="checkbox" name="deletecategory" value="'
10843:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
10844:                         if(ref($path) eq 'ARRAY') {
10845:                             push(@{$path},$name);
10846:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
10847:                             pop(@{$path});
10848:                         }
10849:                     } else {
10850:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
10851:                         if ($j == $numchildren) {
10852:                             $text .= $name;
10853:                         } else {
10854:                             $text .= $item;
10855:                         }
10856:                         $text .= '" value="" />';
10857:                     }
10858:                     $text .= '</td></tr>';
10859:                 }
10860:                 $text .= '</table></td>';
10861:             } else {
10862:                 my $higher = $depth-1;
10863:                 if ($higher == 0) {
10864:                     $name = &escape($parent).'::'.$higher;
10865:                 } else {
10866:                     if (ref($path) eq 'ARRAY') {
10867:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
10868:                     }
10869:                 }
10870:                 my $colspan;
10871:                 if ($parent ne 'instcode') {
10872:                     $colspan = $maxdepth - $depth - 1;
10873:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
10874:                 }
10875:             }
10876:         }
10877:     }
10878:     return $text;
10879: }
10880: 
10881: sub modifiable_userdata_row {
10882:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
10883:         $rowid,$customcss,$rowstyle,$itemdesc) = @_;
10884:     my ($role,$rolename,$statustype);
10885:     $role = $item;
10886:     if ($context eq 'cancreate') {
10887:         if ($item =~ /^(emailusername)_(.+)$/) {
10888:             $role = $1;
10889:             $statustype = $2;
10890:             if (ref($usertypes) eq 'HASH') {
10891:                 if ($usertypes->{$statustype}) {
10892:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
10893:                 } else {
10894:                     $rolename = &mt('Data provided by user');
10895:                 }
10896:             }
10897:         }
10898:     } elsif ($context eq 'selfcreate') {
10899:         if (ref($usertypes) eq 'HASH') {
10900:             $rolename = $usertypes->{$role};
10901:         } else {
10902:             $rolename = $role;
10903:         }
10904:     } elsif ($context eq 'lti') {
10905:         $rolename = &mt('Institutional data used (if available)');
10906:     } elsif ($context eq 'privacy') {
10907:         $rolename = $itemdesc;
10908:     } else {
10909:         if ($role eq 'cr') {
10910:             $rolename = &mt('Custom role');
10911:         } else {
10912:             $rolename = &Apache::lonnet::plaintext($role);
10913:         }
10914:     }
10915:     my (@fields,%fieldtitles);
10916:     if (ref($fieldsref) eq 'ARRAY') {
10917:         @fields = @{$fieldsref};
10918:     } else {
10919:         @fields = ('lastname','firstname','middlename','generation',
10920:                    'permanentemail','id');
10921:     }
10922:     if ((ref($titlesref) eq 'HASH')) {
10923:         %fieldtitles = %{$titlesref};
10924:     } else {
10925:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
10926:     }
10927:     my $output;
10928:     my $css_class;
10929:     if ($rowcount%2) {
10930:         $css_class = 'LC_odd_row';
10931:     }
10932:     if ($customcss) {
10933:         $css_class .= " $customcss";
10934:     }
10935:     $css_class =~ s/^\s+//;
10936:     if ($css_class) {
10937:         $css_class = ' class="'.$css_class.'"';
10938:     }
10939:     if ($rowstyle) {
10940:         $css_class .= ' style="'.$rowstyle.'"';
10941:     }
10942:     if ($rowid) {
10943:         $rowid = ' id="'.$rowid.'"';
10944:     }
10945:     $output = '<tr '.$css_class.$rowid.'>'.
10946:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
10947:               '<td class="LC_left_item" colspan="2"><table>';
10948:     my $rem;
10949:     my %checks;
10950:     my %current;
10951:     if (ref($settings) eq 'HASH') {
10952:         my $hashref;
10953:         if ($context eq 'lti') {
10954:             if (ref($settings) eq 'HASH') {
10955:                 $hashref = $settings->{'instdata'};
10956:             }
10957:         } elsif ($context eq 'privacy') {
10958:             my ($key,$inner) = split(/_/,$role);
10959:             if (ref($settings) eq 'HASH') {
10960:                 if (ref($settings->{$key}) eq 'HASH') {
10961:                     $hashref = $settings->{$key}->{$inner};
10962:                 }
10963:             }
10964:         } elsif (ref($settings->{$context}) eq 'HASH') {
10965:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
10966:                 $hashref = $settings->{'lti_instdata'};
10967:             }
10968:             if ($role eq 'emailusername') {
10969:                 if ($statustype) {
10970:                     if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
10971:                         $hashref = $settings->{$context}->{$role}->{$statustype};
10972:                     }
10973:                 }
10974:             }
10975:         }
10976:         if (ref($hashref) eq 'HASH') { 
10977:             foreach my $field (@fields) {
10978:                 if ($hashref->{$field}) {
10979:                     if ($role eq 'emailusername') {
10980:                         $checks{$field} = $hashref->{$field};
10981:                     } else {
10982:                         $checks{$field} = ' checked="checked" ';
10983:                     }
10984:                 }
10985:             }
10986:         }
10987:     }
10988:  
10989:     my $total = scalar(@fields);
10990:     for (my $i=0; $i<$total; $i++) {
10991:         $rem = $i%($numinrow);
10992:         if ($rem == 0) {
10993:             if ($i > 0) {
10994:                 $output .= '</tr>';
10995:             }
10996:             $output .= '<tr>';
10997:         }
10998:         my $check = ' ';
10999:         unless ($role eq 'emailusername') {
11000:             if (exists($checks{$fields[$i]})) {
11001:                 $check = $checks{$fields[$i]};
11002:             } elsif ($context eq 'privacy') {
11003:                 if ($role =~ /^priv_(domain|course)$/) {
11004:                     if (ref($settings) ne 'HASH') {
11005:                         $check = ' checked="checked" ';
11006:                     }
11007:                 } elsif ($role =~ /^priv_(author|community)$/) {
11008:                     if (ref($settings) ne 'HASH') {
11009:                         unless ($fields[$i] eq 'id') {
11010:                             $check = ' checked="checked" ';
11011:                         }
11012:                     }
11013:                 } elsif ($role =~ /^(unpriv|othdom)_/) {
11014:                     if (ref($settings) ne 'HASH') {
11015:                         if (($fields[$i] eq 'lastname') || ($fields[$i] eq 'firstname')) {
11016:                             $check = ' checked="checked" ';
11017:                         }
11018:                     }
11019:                 }
11020:             } elsif ($context ne 'lti') {
11021:                 if ($role eq 'st') {
11022:                     if (ref($settings) ne 'HASH') {
11023:                         $check = ' checked="checked" '; 
11024:                     }
11025:                 }
11026:             }
11027:         }
11028:         $output .= '<td class="LC_left_item">'.
11029:                    '<span class="LC_nobreak">';
11030:         my $prefix = 'canmodify';
11031:         if ($role eq 'emailusername') {
11032:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
11033:                 $checks{$fields[$i]} = 'omit';
11034:             }
11035:             foreach my $option ('required','optional','omit') {
11036:                 my $checked='';
11037:                 if ($checks{$fields[$i]} eq $option) {
11038:                     $checked='checked="checked" ';
11039:                 }
11040:                 $output .= '<label>'.
11041:                            '<input type="radio" name="'.$prefix.'_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
11042:                            &mt($option).'</label>'.('&nbsp;' x2);
11043:             }
11044:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
11045:         } else {
11046:             if ($context eq 'lti') {
11047:                 $prefix = 'lti';
11048:             } elsif ($context eq 'privacy') {
11049:                 $prefix = 'privacy';
11050:             }
11051:             $output .= '<label>'.
11052:                        '<input type="checkbox" name="'.$prefix.'_'.$role.'" '.
11053:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
11054:                        '</label>';
11055:         }
11056:         $output .= '</span></td>';
11057:     }
11058:     $rem = $total%$numinrow;
11059:     my $colsleft;
11060:     if ($rem) {
11061:         $colsleft = $numinrow - $rem;
11062:     }
11063:     if ($colsleft > 1) {
11064:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
11065:                    '&nbsp;</td>';
11066:     } elsif ($colsleft == 1) {
11067:         $output .= '<td class="LC_left_item">&nbsp;</td>';
11068:     }
11069:     $output .= '</tr></table></td></tr>';
11070:     return $output;
11071: }
11072: 
11073: sub insttypes_row {
11074:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
11075:         $customcss,$rowstyle) = @_;
11076:     my %lt = &Apache::lonlocal::texthash (
11077:                       cansearch => 'Users allowed to search',
11078:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
11079:                       lockablenames => 'User preference to lock name',
11080:                       selfassign    => 'Self-reportable affiliations',
11081:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
11082:              );
11083:     my $showdom;
11084:     if ($context eq 'cansearch') {
11085:         $showdom = ' ('.$dom.')';
11086:     }
11087:     my $class = 'LC_left_item';
11088:     if ($context eq 'statustocreate') {
11089:         $class = 'LC_right_item';
11090:     }
11091:     my $css_class;
11092:     if ($$rowtotal%2) {
11093:         $css_class = 'LC_odd_row';
11094:     }
11095:     if ($customcss) {
11096:         $css_class .= ' '.$customcss;
11097:     }
11098:     $css_class =~ s/^\s+//;
11099:     if ($css_class) {
11100:         $css_class = ' class="'.$css_class.'"';
11101:     }
11102:     if ($rowstyle) {
11103:         $css_class .= ' style="'.$rowstyle.'"';
11104:     }
11105:     if ($onclick) {
11106:         $onclick = 'onclick="'.$onclick.'" ';
11107:     }
11108:     my $output = '<tr'.$css_class.'>'.
11109:                  '<td>'.$lt{$context}.$showdom.
11110:                  '</td><td class="'.$class.'" colspan="2"><table>';
11111:     my $rem;
11112:     if (ref($types) eq 'ARRAY') {
11113:         for (my $i=0; $i<@{$types}; $i++) {
11114:             if (defined($usertypes->{$types->[$i]})) {
11115:                 my $rem = $i%($numinrow);
11116:                 if ($rem == 0) {
11117:                     if ($i > 0) {
11118:                         $output .= '</tr>';
11119:                     }
11120:                     $output .= '<tr>';
11121:                 }
11122:                 my $check = ' ';
11123:                 if (ref($settings) eq 'HASH') {
11124:                     if (ref($settings->{$context}) eq 'ARRAY') {
11125:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
11126:                             $check = ' checked="checked" ';
11127:                         }
11128:                     } elsif (ref($settings->{$context}) eq 'HASH') {
11129:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
11130:                             $check = ' checked="checked" ';
11131:                         }
11132:                     } elsif ($context eq 'statustocreate') {
11133:                         $check = ' checked="checked" ';
11134:                     }
11135:                 }
11136:                 $output .= '<td class="LC_left_item">'.
11137:                            '<span class="LC_nobreak"><label>'.
11138:                            '<input type="checkbox" name="'.$context.'" '.
11139:                            'value="'.$types->[$i].'"'.$check.$onclick.' />'.
11140:                            $usertypes->{$types->[$i]}.'</label></span></td>';
11141:             }
11142:         }
11143:         $rem = @{$types}%($numinrow);
11144:     }
11145:     my $colsleft = $numinrow - $rem;
11146:     if ($context eq 'overrides') {
11147:         if ($colsleft > 1) {
11148:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
11149:         } else {
11150:             $output .= '<td class="LC_left_item">';
11151:         }
11152:         $output .= '&nbsp;';  
11153:     } else {
11154:         if ($rem == 0) {
11155:             $output .= '<tr>';
11156:         }
11157:         if ($colsleft > 1) {
11158:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
11159:         } else {
11160:             $output .= '<td class="LC_left_item">';
11161:         }
11162:         my $defcheck = ' ';
11163:         if (ref($settings) eq 'HASH') {  
11164:             if (ref($settings->{$context}) eq 'ARRAY') {
11165:                 if (grep(/^default$/,@{$settings->{$context}})) {
11166:                     $defcheck = ' checked="checked" ';
11167:                 }
11168:             } elsif ($context eq 'statustocreate') {
11169:                 $defcheck = ' checked="checked" ';
11170:             }
11171:         }
11172:         $output .= '<span class="LC_nobreak"><label>'.
11173:                    '<input type="checkbox" name="'.$context.'" '.
11174:                    'value="default"'.$defcheck.$onclick.' />'.
11175:                    $othertitle.'</label></span>';
11176:     }
11177:     $output .= '</td></tr></table></td></tr>';
11178:     return $output;
11179: }
11180: 
11181: sub sorted_searchtitles {
11182:     my %searchtitles = &Apache::lonlocal::texthash(
11183:                          'uname' => 'username',
11184:                          'lastname' => 'last name',
11185:                          'lastfirst' => 'last name, first name',
11186:                      );
11187:     my @titleorder = ('uname','lastname','lastfirst');
11188:     return (\%searchtitles,\@titleorder);
11189: }
11190: 
11191: sub sorted_searchtypes {
11192:     my %srchtypes_desc = (
11193:                            exact    => 'is exact match',
11194:                            contains => 'contains ..',
11195:                            begins   => 'begins with ..',
11196:                          );
11197:     my @srchtypeorder = ('exact','begins','contains');
11198:     return (\%srchtypes_desc,\@srchtypeorder);
11199: }
11200: 
11201: sub usertype_update_row {
11202:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
11203:     my $datatable;
11204:     my $numinrow = 4;
11205:     foreach my $type (@{$types}) {
11206:         if (defined($usertypes->{$type})) {
11207:             $$rownums ++;
11208:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
11209:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
11210:                           '</td><td class="LC_left_item"><table>';
11211:             for (my $i=0; $i<@{$fields}; $i++) {
11212:                 my $rem = $i%($numinrow);
11213:                 if ($rem == 0) {
11214:                     if ($i > 0) {
11215:                         $datatable .= '</tr>';
11216:                     }
11217:                     $datatable .= '<tr>';
11218:                 }
11219:                 my $check = ' ';
11220:                 if (ref($settings) eq 'HASH') {
11221:                     if (ref($settings->{'fields'}) eq 'HASH') {
11222:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
11223:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
11224:                                 $check = ' checked="checked" ';
11225:                             }
11226:                         }
11227:                     }
11228:                 }
11229: 
11230:                 if ($i == @{$fields}-1) {
11231:                     my $colsleft = $numinrow - $rem;
11232:                     if ($colsleft > 1) {
11233:                         $datatable .= '<td colspan="'.$colsleft.'">';
11234:                     } else {
11235:                         $datatable .= '<td>';
11236:                     }
11237:                 } else {
11238:                     $datatable .= '<td>';
11239:                 }
11240:                 $datatable .= '<span class="LC_nobreak"><label>'.
11241:                               '<input type="checkbox" name="updateable_'.$type.
11242:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
11243:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
11244:             }
11245:             $datatable .= '</tr></table></td></tr>';
11246:         }
11247:     }
11248:     return $datatable;
11249: }
11250: 
11251: sub modify_login {
11252:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
11253:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
11254:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
11255:     %title = ( coursecatalog => 'Display course catalog',
11256:                adminmail => 'Display administrator E-mail address',
11257:                helpdesk  => 'Display "Contact Helpdesk" link',
11258:                newuser => 'Link for visitors to create a user account',
11259:                loginheader => 'Log-in box header');
11260:     @offon = ('off','on');
11261:     if (ref($domconfig{login}) eq 'HASH') {
11262:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
11263:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
11264:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
11265:             }
11266:         }
11267:     }
11268:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
11269:                                            \%domconfig,\%loginhash);
11270:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
11271:     foreach my $item (@toggles) {
11272:         $loginhash{login}{$item} = $env{'form.'.$item};
11273:     }
11274:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
11275:     if (ref($colchanges{'login'}) eq 'HASH') {  
11276:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
11277:                                          \%loginhash);
11278:     }
11279: 
11280:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
11281:     my %domservers = &Apache::lonnet::get_servers($dom);
11282:     my @loginvia_attribs = ('serverpath','custompath','exempt');
11283:     if (keys(%servers) > 1) {
11284:         foreach my $lonhost (keys(%servers)) {
11285:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
11286:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
11287:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
11288:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
11289:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
11290:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
11291:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
11292:                         $changes{'loginvia'}{$lonhost} = 1;
11293:                     } else {
11294:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
11295:                         $changes{'loginvia'}{$lonhost} = 1;
11296:                     }
11297:                 } else {
11298:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
11299:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
11300:                         $changes{'loginvia'}{$lonhost} = 1;
11301:                     }
11302:                 }
11303:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
11304:                     foreach my $item (@loginvia_attribs) {
11305:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
11306:                     }
11307:                 } else {
11308:                     foreach my $item (@loginvia_attribs) {
11309:                         my $new = $env{'form.'.$lonhost.'_'.$item};
11310:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
11311:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
11312:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
11313:                                 $new = '/';
11314:                             }
11315:                         }
11316:                         if (($item eq 'custompath') && 
11317:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
11318:                             $new = '';
11319:                         }
11320:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
11321:                             $changes{'loginvia'}{$lonhost} = 1;
11322:                         }
11323:                         if ($item eq 'exempt') {
11324:                             $new = &check_exempt_addresses($new);
11325:                         }
11326:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
11327:                     }
11328:                 }
11329:             } else {
11330:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
11331:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
11332:                     $changes{'loginvia'}{$lonhost} = 1;
11333:                     foreach my $item (@loginvia_attribs) {
11334:                         my $new = $env{'form.'.$lonhost.'_'.$item};
11335:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
11336:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
11337:                                 $new = '/';
11338:                             }
11339:                         }
11340:                         if (($item eq 'custompath') && 
11341:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
11342:                             $new = '';
11343:                         }
11344:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
11345:                     }
11346:                 }
11347:             }
11348:         }
11349:     }
11350: 
11351:     my $servadm = $r->dir_config('lonAdmEMail');
11352:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
11353:     if (ref($domconfig{'login'}) eq 'HASH') {
11354:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
11355:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
11356:                 if ($lang eq 'nolang') {
11357:                     push(@currlangs,$lang);
11358:                 } elsif (defined($langchoices{$lang})) {
11359:                     push(@currlangs,$lang);
11360:                 } else {
11361:                     next;
11362:                 }
11363:             }
11364:         }
11365:     }
11366:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
11367:     if (@currlangs > 0) {
11368:         foreach my $lang (@currlangs) {
11369:             if (grep(/^\Q$lang\E$/,@delurls)) {
11370:                 $changes{'helpurl'}{$lang} = 1;
11371:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
11372:                 $changes{'helpurl'}{$lang} = 1;
11373:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
11374:                 push(@newlangs,$lang);
11375:             } else {
11376:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
11377:             }
11378:         }
11379:     }
11380:     unless (grep(/^nolang$/,@currlangs)) {
11381:         if ($env{'form.loginhelpurl_nolang.filename'}) {
11382:             $changes{'helpurl'}{'nolang'} = 1;
11383:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
11384:             push(@newlangs,'nolang');
11385:         }
11386:     }
11387:     if ($env{'form.loginhelpurl_add_lang'}) {
11388:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
11389:             ($env{'form.loginhelpurl_add_file.filename'})) {
11390:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
11391:             $addedfile = $env{'form.loginhelpurl_add_lang'};
11392:         }
11393:     }
11394:     if ((@newlangs > 0) || ($addedfile)) {
11395:         my $error;
11396:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11397:         if ($configuserok eq 'ok') {
11398:             if ($switchserver) {
11399:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
11400:             } elsif ($author_ok eq 'ok') {
11401:                 my @allnew = @newlangs;
11402:                 if ($addedfile ne '') {
11403:                     push(@allnew,$addedfile);
11404:                 }
11405:                 foreach my $lang (@allnew) {
11406:                     my $formelem = 'loginhelpurl_'.$lang;
11407:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
11408:                         $formelem = 'loginhelpurl_add_file';
11409:                     }
11410:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
11411:                                                                "help/$lang",'','',$newfile{$lang});
11412:                     if ($result eq 'ok') {
11413:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
11414:                         $changes{'helpurl'}{$lang} = 1;
11415:                     } else {
11416:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
11417:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
11418:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
11419:                             (!grep(/^\Q$lang\E$/,@delurls))) {
11420:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
11421:                         }
11422:                     }
11423:                 }
11424:             } else {
11425:                 $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);
11426:             }
11427:         } else {
11428:             $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);
11429:         }
11430:         if ($error) {
11431:             &Apache::lonnet::logthis($error);
11432:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11433:         }
11434:     }
11435: 
11436:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
11437:     if (ref($domconfig{'login'}) eq 'HASH') {
11438:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
11439:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
11440:                 if ($domservers{$lonhost}) {
11441:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
11442:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
11443:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
11444:                     }
11445:                 }
11446:             }
11447:         }
11448:     }
11449:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
11450:     foreach my $lonhost (sort(keys(%domservers))) {
11451:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
11452:             $changes{'headtag'}{$lonhost} = 1;
11453:         } else {
11454:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
11455:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
11456:             }
11457:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
11458:                 push(@newhosts,$lonhost);
11459:             } elsif ($currheadtagurls{$lonhost}) {
11460:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
11461:                 if ($currexempt{$lonhost}) {
11462:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
11463:                         $changes{'headtag'}{$lonhost} = 1;
11464:                     }
11465:                 } elsif ($possexempt{$lonhost}) {
11466:                     $changes{'headtag'}{$lonhost} = 1;
11467:                 }
11468:                 if ($possexempt{$lonhost}) {
11469:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
11470:                 }
11471:             }
11472:         }
11473:     }
11474:     if (@newhosts) {
11475:         my $error;
11476:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11477:         if ($configuserok eq 'ok') {
11478:             if ($switchserver) {
11479:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
11480:             } elsif ($author_ok eq 'ok') {
11481:                 foreach my $lonhost (@newhosts) {
11482:                     my $formelem = 'loginheadtag_'.$lonhost;
11483:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
11484:                                                                           "login/headtag/$lonhost",'','',
11485:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
11486:                     if ($result eq 'ok') {
11487:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
11488:                         $changes{'headtag'}{$lonhost} = 1;
11489:                         if ($possexempt{$lonhost}) {
11490:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
11491:                         }
11492:                     } else {
11493:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
11494:                                            $newheadtagurls{$lonhost},$result);
11495:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
11496:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
11497:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
11498:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
11499:                         }
11500:                     }
11501:                 }
11502:             } else {
11503:                 $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);
11504:             }
11505:         } else {
11506:             $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);
11507:         }
11508:         if ($error) {
11509:             &Apache::lonnet::logthis($error);
11510:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11511:         }
11512:     }
11513:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
11514: 
11515:     my $defaulthelpfile = '/adm/loginproblems.html';
11516:     my $defaulttext = &mt('Default in use');
11517: 
11518:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
11519:                                              $dom);
11520:     if ($putresult eq 'ok') {
11521:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
11522:         my %defaultchecked = (
11523:                     'coursecatalog' => 'on',
11524:                     'helpdesk'      => 'on',
11525:                     'adminmail'     => 'off',
11526:                     'newuser'       => 'off',
11527:         );
11528:         if (ref($domconfig{'login'}) eq 'HASH') {
11529:             foreach my $item (@toggles) {
11530:                 if ($defaultchecked{$item} eq 'on') { 
11531:                     if (($domconfig{'login'}{$item} eq '0') &&
11532:                         ($env{'form.'.$item} eq '1')) {
11533:                         $changes{$item} = 1;
11534:                     } elsif (($domconfig{'login'}{$item} eq '' ||
11535:                               $domconfig{'login'}{$item} eq '1') &&
11536:                              ($env{'form.'.$item} eq '0')) {
11537:                         $changes{$item} = 1;
11538:                     }
11539:                 } elsif ($defaultchecked{$item} eq 'off') {
11540:                     if (($domconfig{'login'}{$item} eq '1') &&
11541:                         ($env{'form.'.$item} eq '0')) {
11542:                         $changes{$item} = 1;
11543:                     } elsif (($domconfig{'login'}{$item} eq '' ||
11544:                               $domconfig{'login'}{$item} eq '0') &&
11545:                              ($env{'form.'.$item} eq '1')) {
11546:                         $changes{$item} = 1;
11547:                     }
11548:                 }
11549:             }
11550:         }
11551:         if (keys(%changes) > 0 || $colchgtext) {
11552:             &Apache::loncommon::devalidate_domconfig_cache($dom);
11553:             if (ref($lastactref) eq 'HASH') {
11554:                 $lastactref->{'domainconfig'} = 1;
11555:             }
11556:             $resulttext = &mt('Changes made:').'<ul>';
11557:             foreach my $item (sort(keys(%changes))) {
11558:                 if ($item eq 'loginvia') {
11559:                     if (ref($changes{$item}) eq 'HASH') {
11560:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
11561:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
11562:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
11563:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
11564:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
11565:                                     $protocol = 'http' if ($protocol ne 'https');
11566:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
11567: 
11568:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
11569:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
11570:                                     } else {
11571:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
11572:                                     }
11573:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
11574:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
11575:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
11576:                                     }
11577:                                     $resulttext .= '</li>';
11578:                                 } else {
11579:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
11580:                                 }
11581:                             } else {
11582:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
11583:                             }
11584:                         }
11585:                         $resulttext .= '</ul></li>';
11586:                     }
11587:                 } elsif ($item eq 'helpurl') {
11588:                     if (ref($changes{$item}) eq 'HASH') {
11589:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
11590:                             if (grep(/^\Q$lang\E$/,@delurls)) {
11591:                                 my ($chg,$link);
11592:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
11593:                                 if ($lang eq 'nolang') {
11594:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
11595:                                 } else {
11596:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
11597:                                 }
11598:                                 $resulttext .= '<li>'.$chg.'</li>';
11599:                             } else {
11600:                                 my $chg;
11601:                                 if ($lang eq 'nolang') {
11602:                                     $chg = &mt('custom log-in help file for no preferred language');
11603:                                 } else {
11604:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
11605:                                 }
11606:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
11607:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
11608:                                                       '?inhibitmenu=yes',$chg,600,500).
11609:                                                '</li>';
11610:                             }
11611:                         }
11612:                     }
11613:                 } elsif ($item eq 'headtag') {
11614:                     if (ref($changes{$item}) eq 'HASH') {
11615:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
11616:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
11617:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
11618:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
11619:                                 $resulttext .= '<li><a href="'.
11620:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
11621:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
11622:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
11623:                                 if ($possexempt{$lonhost}) {
11624:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
11625:                                 } else {
11626:                                     $resulttext .= &mt('included for any client IP');
11627:                                 }
11628:                                 $resulttext .= '</li>';
11629:                             }
11630:                         }
11631:                     }
11632:                 } elsif ($item eq 'captcha') {
11633:                     if (ref($loginhash{'login'}) eq 'HASH') {
11634:                         my $chgtxt;
11635:                         if ($loginhash{'login'}{$item} eq 'notused') {
11636:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
11637:                         } else {
11638:                             my %captchas = &captcha_phrases();
11639:                             if ($captchas{$loginhash{'login'}{$item}}) {
11640:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
11641:                             } else {
11642:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
11643:                             }
11644:                         }
11645:                         $resulttext .= '<li>'.$chgtxt.'</li>';
11646:                     }
11647:                 } elsif ($item eq 'recaptchakeys') {
11648:                     if (ref($loginhash{'login'}) eq 'HASH') {
11649:                         my ($privkey,$pubkey);
11650:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
11651:                             $pubkey = $loginhash{'login'}{$item}{'public'};
11652:                             $privkey = $loginhash{'login'}{$item}{'private'};
11653:                         }
11654:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
11655:                         if (!$pubkey) {
11656:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
11657:                         } else {
11658:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
11659:                         }
11660:                         if (!$privkey) {
11661:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
11662:                         } else {
11663:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
11664:                         }
11665:                         $chgtxt .= '</ul>';
11666:                         $resulttext .= '<li>'.$chgtxt.'</li>';
11667:                     }
11668:                 } elsif ($item eq 'recaptchaversion') {
11669:                     if (ref($loginhash{'login'}) eq 'HASH') {
11670:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
11671:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
11672:                                            '</li>';
11673:                         }
11674:                     }
11675:                 } else {
11676:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
11677:                 }
11678:             }
11679:             $resulttext .= $colchgtext.'</ul>';
11680:         } else {
11681:             $resulttext = &mt('No changes made to log-in page settings');
11682:         }
11683:     } else {
11684:         $resulttext = '<span class="LC_error">'.
11685: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11686:     }
11687:     if ($errors) {
11688:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
11689:                        $errors.'</ul>';
11690:     }
11691:     return $resulttext;
11692: }
11693: 
11694: sub check_exempt_addresses {
11695:     my ($iplist) = @_;
11696:     $iplist =~ s/^\s+//;
11697:     $iplist =~ s/\s+$//;
11698:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
11699:     my (@okips,$new);
11700:     foreach my $ip (@poss_ips) {
11701:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
11702:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
11703:                 push(@okips,$ip);
11704:             }
11705:         }
11706:     }
11707:     if (@okips > 0) {
11708:         $new = join(',',@okips);
11709:     } else {
11710:         $new = '';
11711:     }
11712:     return $new;
11713: }
11714: 
11715: sub color_font_choices {
11716:     my %choices =
11717:         &Apache::lonlocal::texthash (
11718:             img => "Header",
11719:             bgs => "Background colors",
11720:             links => "Link colors",
11721:             images => "Images",
11722:             font => "Font color",
11723:             fontmenu => "Font menu",
11724:             pgbg => "Page",
11725:             tabbg => "Header",
11726:             sidebg => "Border",
11727:             link => "Link",
11728:             alink => "Active link",
11729:             vlink => "Visited link",
11730:         );
11731:     return %choices;
11732: }
11733: 
11734: sub modify_rolecolors {
11735:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
11736:     my ($resulttext,%rolehash);
11737:     $rolehash{'rolecolors'} = {};
11738:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
11739:         if ($domconfig{'rolecolors'} eq '') {
11740:             $domconfig{'rolecolors'} = {};
11741:         }
11742:     }
11743:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
11744:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
11745:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
11746:                                              $dom);
11747:     if ($putresult eq 'ok') {
11748:         if (keys(%changes) > 0) {
11749:             &Apache::loncommon::devalidate_domconfig_cache($dom);
11750:             if (ref($lastactref) eq 'HASH') {
11751:                 $lastactref->{'domainconfig'} = 1;
11752:             }
11753:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
11754:                                              $rolehash{'rolecolors'});
11755:         } else {
11756:             $resulttext = &mt('No changes made to default color schemes');
11757:         }
11758:     } else {
11759:         $resulttext = '<span class="LC_error">'.
11760: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11761:     }
11762:     if ($errors) {
11763:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
11764:                        $errors.'</ul>';
11765:     }
11766:     return $resulttext;
11767: }
11768: 
11769: sub modify_colors {
11770:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
11771:     my (%changes,%choices);
11772:     my @bgs;
11773:     my @links = ('link','alink','vlink');
11774:     my @logintext;
11775:     my @images;
11776:     my $servadm = $r->dir_config('lonAdmEMail');
11777:     my $errors;
11778:     my %defaults;
11779:     foreach my $role (@{$roles}) {
11780:         if ($role eq 'login') {
11781:             %choices = &login_choices();
11782:             @logintext = ('textcol','bgcol');
11783:         } else {
11784:             %choices = &color_font_choices();
11785:         }
11786:         if ($role eq 'login') {
11787:             @images = ('img','logo','domlogo','login');
11788:             @bgs = ('pgbg','mainbg','sidebg');
11789:         } else {
11790:             @images = ('img');
11791:             @bgs = ('pgbg','tabbg','sidebg');
11792:         }
11793:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
11794:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
11795:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
11796:         }
11797:         if ($role eq 'login') {
11798:             foreach my $item (@logintext) {
11799:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
11800:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
11801:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
11802:                 }
11803:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
11804:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
11805:                 }
11806:             }
11807:         } else {
11808:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
11809:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
11810:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
11811:             }
11812:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
11813:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
11814:             }
11815:         }
11816:         foreach my $item (@bgs) {
11817:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
11818:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
11819:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
11820:             }
11821:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
11822:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
11823:             }
11824:         }
11825:         foreach my $item (@links) {
11826:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
11827:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
11828:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
11829:             }
11830:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
11831:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
11832:             }
11833:         }
11834:         my ($configuserok,$author_ok,$switchserver) = 
11835:             &config_check($dom,$confname,$servadm);
11836:         my ($width,$height) = &thumb_dimensions();
11837:         if (ref($domconfig->{$role}) ne 'HASH') {
11838:             $domconfig->{$role} = {};
11839:         }
11840:         foreach my $img (@images) {
11841:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
11842:                 if (defined($env{'form.login_showlogo_'.$img})) {
11843:                     $confhash->{$role}{'showlogo'}{$img} = 1;
11844:                 } else { 
11845:                     $confhash->{$role}{'showlogo'}{$img} = 0;
11846:                 }
11847:             } 
11848: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
11849: 		 && !defined($domconfig->{$role}{$img})
11850: 		 && !$env{'form.'.$role.'_del_'.$img}
11851: 		 && $env{'form.'.$role.'_import_'.$img}) {
11852: 		# import the old configured image from the .tab setting
11853: 		# if they haven't provided a new one 
11854: 		$domconfig->{$role}{$img} = 
11855: 		    $env{'form.'.$role.'_import_'.$img};
11856: 	    }
11857:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
11858:                 my $error;
11859:                 if ($configuserok eq 'ok') {
11860:                     if ($switchserver) {
11861:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
11862:                     } else {
11863:                         if ($author_ok eq 'ok') {
11864:                             my ($result,$logourl) = 
11865:                                 &publishlogo($r,'upload',$role.'_'.$img,
11866:                                            $dom,$confname,$img,$width,$height);
11867:                             if ($result eq 'ok') {
11868:                                 $confhash->{$role}{$img} = $logourl;
11869:                                 $changes{$role}{'images'}{$img} = 1;
11870:                             } else {
11871:                                 $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);
11872:                             }
11873:                         } else {
11874:                             $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);
11875:                         }
11876:                     }
11877:                 } else {
11878:                     $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);
11879:                 }
11880:                 if ($error) {
11881:                     &Apache::lonnet::logthis($error);
11882:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11883:                 }
11884:             } elsif ($domconfig->{$role}{$img} ne '') {
11885:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
11886:                     my $error;
11887:                     if ($configuserok eq 'ok') {
11888: # is confname an author?
11889:                         if ($switchserver eq '') {
11890:                             if ($author_ok eq 'ok') {
11891:                                 my ($result,$logourl) = 
11892:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
11893:                                             $dom,$confname,$img,$width,$height);
11894:                                 if ($result eq 'ok') {
11895:                                     $confhash->{$role}{$img} = $logourl;
11896: 				    $changes{$role}{'images'}{$img} = 1;
11897:                                 }
11898:                             }
11899:                         }
11900:                     }
11901:                 }
11902:             }
11903:         }
11904:         if (ref($domconfig) eq 'HASH') {
11905:             if (ref($domconfig->{$role}) eq 'HASH') {
11906:                 foreach my $img (@images) {
11907:                     if ($domconfig->{$role}{$img} ne '') {
11908:                         if ($env{'form.'.$role.'_del_'.$img}) {
11909:                             $confhash->{$role}{$img} = '';
11910:                             $changes{$role}{'images'}{$img} = 1;
11911:                         } else {
11912:                             if ($confhash->{$role}{$img} eq '') {
11913:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
11914:                             }
11915:                         }
11916:                     } else {
11917:                         if ($env{'form.'.$role.'_del_'.$img}) {
11918:                             $confhash->{$role}{$img} = '';
11919:                             $changes{$role}{'images'}{$img} = 1;
11920:                         } 
11921:                     }
11922:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
11923:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
11924:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
11925:                                 $domconfig->{$role}{'showlogo'}{$img}) {
11926:                                 $changes{$role}{'showlogo'}{$img} = 1; 
11927:                             }
11928:                         } else {
11929:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
11930:                                 $changes{$role}{'showlogo'}{$img} = 1;
11931:                             }
11932:                         }
11933:                     }
11934:                 }
11935:                 if ($domconfig->{$role}{'font'} ne '') {
11936:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
11937:                         $changes{$role}{'font'} = 1;
11938:                     }
11939:                 } else {
11940:                     if ($confhash->{$role}{'font'}) {
11941:                         $changes{$role}{'font'} = 1;
11942:                     }
11943:                 }
11944:                 if ($role ne 'login') {
11945:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
11946:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
11947:                             $changes{$role}{'fontmenu'} = 1;
11948:                         }
11949:                     } else {
11950:                         if ($confhash->{$role}{'fontmenu'}) {
11951:                             $changes{$role}{'fontmenu'} = 1;
11952:                         }
11953:                     }
11954:                 }
11955:                 foreach my $item (@bgs) {
11956:                     if ($domconfig->{$role}{$item} ne '') {
11957:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
11958:                             $changes{$role}{'bgs'}{$item} = 1;
11959:                         } 
11960:                     } else {
11961:                         if ($confhash->{$role}{$item}) {
11962:                             $changes{$role}{'bgs'}{$item} = 1;
11963:                         }
11964:                     }
11965:                 }
11966:                 foreach my $item (@links) {
11967:                     if ($domconfig->{$role}{$item} ne '') {
11968:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
11969:                             $changes{$role}{'links'}{$item} = 1;
11970:                         }
11971:                     } else {
11972:                         if ($confhash->{$role}{$item}) {
11973:                             $changes{$role}{'links'}{$item} = 1;
11974:                         }
11975:                     }
11976:                 }
11977:                 foreach my $item (@logintext) {
11978:                     if ($domconfig->{$role}{$item} ne '') {
11979:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
11980:                             $changes{$role}{'logintext'}{$item} = 1;
11981:                         }
11982:                     } else {
11983:                         if ($confhash->{$role}{$item}) {
11984:                             $changes{$role}{'logintext'}{$item} = 1;
11985:                         }
11986:                     }
11987:                 }
11988:             } else {
11989:                 &default_change_checker($role,\@images,\@links,\@bgs,
11990:                                         \@logintext,$confhash,\%changes); 
11991:             }
11992:         } else {
11993:             &default_change_checker($role,\@images,\@links,\@bgs,
11994:                                     \@logintext,$confhash,\%changes); 
11995:         }
11996:     }
11997:     return ($errors,%changes);
11998: }
11999: 
12000: sub config_check {
12001:     my ($dom,$confname,$servadm) = @_;
12002:     my ($configuserok,$author_ok,$switchserver,%currroles);
12003:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
12004:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
12005:                                                    $confname,$servadm);
12006:     if ($configuserok eq 'ok') {
12007:         $switchserver = &check_switchserver($dom,$confname);
12008:         if ($switchserver eq '') {
12009:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
12010:         }
12011:     }
12012:     return ($configuserok,$author_ok,$switchserver);
12013: }
12014: 
12015: sub default_change_checker {
12016:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
12017:     foreach my $item (@{$links}) {
12018:         if ($confhash->{$role}{$item}) {
12019:             $changes->{$role}{'links'}{$item} = 1;
12020:         }
12021:     }
12022:     foreach my $item (@{$bgs}) {
12023:         if ($confhash->{$role}{$item}) {
12024:             $changes->{$role}{'bgs'}{$item} = 1;
12025:         }
12026:     }
12027:     foreach my $item (@{$logintext}) {
12028:         if ($confhash->{$role}{$item}) {
12029:             $changes->{$role}{'logintext'}{$item} = 1;
12030:         }
12031:     }
12032:     foreach my $img (@{$images}) {
12033:         if ($env{'form.'.$role.'_del_'.$img}) {
12034:             $confhash->{$role}{$img} = '';
12035:             $changes->{$role}{'images'}{$img} = 1;
12036:         }
12037:         if ($role eq 'login') {
12038:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
12039:                 $changes->{$role}{'showlogo'}{$img} = 1;
12040:             }
12041:         }
12042:     }
12043:     if ($confhash->{$role}{'font'}) {
12044:         $changes->{$role}{'font'} = 1;
12045:     }
12046: }
12047: 
12048: sub display_colorchgs {
12049:     my ($dom,$changes,$roles,$confhash) = @_;
12050:     my (%choices,$resulttext);
12051:     if (!grep(/^login$/,@{$roles})) {
12052:         $resulttext = &mt('Changes made:').'<br />';
12053:     }
12054:     foreach my $role (@{$roles}) {
12055:         if ($role eq 'login') {
12056:             %choices = &login_choices();
12057:         } else {
12058:             %choices = &color_font_choices();
12059:         }
12060:         if (ref($changes->{$role}) eq 'HASH') {
12061:             if ($role ne 'login') {
12062:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
12063:             }
12064:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
12065:                 if ($role ne 'login') {
12066:                     $resulttext .= '<ul>';
12067:                 }
12068:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
12069:                     if ($role ne 'login') {
12070:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
12071:                     }
12072:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
12073:                         if (($role eq 'login') && ($key eq 'showlogo')) {
12074:                             if ($confhash->{$role}{$key}{$item}) {
12075:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
12076:                             } else {
12077:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
12078:                             }
12079:                         } elsif ($confhash->{$role}{$item} eq '') {
12080:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
12081:                         } else {
12082:                             my $newitem = $confhash->{$role}{$item};
12083:                             if ($key eq 'images') {
12084:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
12085:                             }
12086:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
12087:                         }
12088:                     }
12089:                     if ($role ne 'login') {
12090:                         $resulttext .= '</ul></li>';
12091:                     }
12092:                 } else {
12093:                     if ($confhash->{$role}{$key} eq '') {
12094:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
12095:                     } else {
12096:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
12097:                     }
12098:                 }
12099:                 if ($role ne 'login') {
12100:                     $resulttext .= '</ul>';
12101:                 }
12102:             }
12103:         }
12104:     }
12105:     return $resulttext;
12106: }
12107: 
12108: sub thumb_dimensions {
12109:     return ('200','50');
12110: }
12111: 
12112: sub check_dimensions {
12113:     my ($inputfile) = @_;
12114:     my ($fullwidth,$fullheight);
12115:     if ($inputfile =~ m|^[/\w.\-]+$|) {
12116:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
12117:             my $imageinfo = <PIPE>;
12118:             if (!close(PIPE)) {
12119:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
12120:             }
12121:             chomp($imageinfo);
12122:             my ($fullsize) = 
12123:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
12124:             if ($fullsize) {
12125:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
12126:             }
12127:         }
12128:     }
12129:     return ($fullwidth,$fullheight);
12130: }
12131: 
12132: sub check_configuser {
12133:     my ($uhome,$dom,$confname,$servadm) = @_;
12134:     my ($configuserok,%currroles);
12135:     if ($uhome eq 'no_host') {
12136:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
12137:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
12138:         $configuserok = 
12139:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
12140:                              $configpass,'','','','','',undef,$servadm);
12141:     } else {
12142:         $configuserok = 'ok';
12143:         %currroles = 
12144:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
12145:     }
12146:     return ($configuserok,%currroles);
12147: }
12148: 
12149: sub check_authorstatus {
12150:     my ($dom,$confname,%currroles) = @_;
12151:     my $author_ok;
12152:     if (!$currroles{':'.$dom.':au'}) {
12153:         my $start = time;
12154:         my $end = 0;
12155:         $author_ok = 
12156:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
12157:                                         'au',$end,$start,'','','domconfig');
12158:     } else {
12159:         $author_ok = 'ok';
12160:     }
12161:     return $author_ok;
12162: }
12163: 
12164: sub publishlogo {
12165:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
12166:     my ($output,$fname,$logourl,$madethumb);
12167:     if ($action eq 'upload') {
12168:         $fname=$env{'form.'.$formname.'.filename'};
12169:         chop($env{'form.'.$formname});
12170:     } else {
12171:         ($fname) = ($formname =~ /([^\/]+)$/);
12172:     }
12173:     if ($savefileas ne '') {
12174:         $fname = $savefileas;
12175:     }
12176:     $fname=&Apache::lonnet::clean_filename($fname);
12177: # See if there is anything left
12178:     unless ($fname) { return ('error: no uploaded file'); }
12179:     $fname="$subdir/$fname";
12180:     my $docroot=$r->dir_config('lonDocRoot');
12181:     my $filepath="$docroot/priv";
12182:     my $relpath = "$dom/$confname";
12183:     my ($fnamepath,$file,$fetchthumb);
12184:     $file=$fname;
12185:     if ($fname=~m|/|) {
12186:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
12187:     }
12188:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
12189:     my $count;
12190:     for ($count=5;$count<=$#parts;$count++) {
12191:         $filepath.="/$parts[$count]";
12192:         if ((-e $filepath)!=1) {
12193:             mkdir($filepath,02770);
12194:         }
12195:     }
12196:     # Check for bad extension and disallow upload
12197:     if ($file=~/\.(\w+)$/ &&
12198:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
12199:         $output = 
12200:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
12201:     } elsif ($file=~/\.(\w+)$/ &&
12202:         !defined(&Apache::loncommon::fileembstyle($1))) {
12203:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
12204:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
12205:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
12206:     } elsif (-d "$filepath/$file") {
12207:         $output = &mt('Filename is a directory name - rename the file and re-upload');
12208:     } else {
12209:         my $source = $filepath.'/'.$file;
12210:         my $logfile;
12211:         if (!open($logfile,">>",$source.'.log')) {
12212:             return (&mt('No write permission to Authoring Space'));
12213:         }
12214:         print $logfile
12215: "\n================= Publish ".localtime()." ================\n".
12216: $env{'user.name'}.':'.$env{'user.domain'}."\n";
12217: # Save the file
12218:         if (!open(FH,">",$source)) {
12219:             &Apache::lonnet::logthis('Failed to create '.$source);
12220:             return (&mt('Failed to create file'));
12221:         }
12222:         if ($action eq 'upload') {
12223:             if (!print FH ($env{'form.'.$formname})) {
12224:                 &Apache::lonnet::logthis('Failed to write to '.$source);
12225:                 return (&mt('Failed to write file'));
12226:             }
12227:         } else {
12228:             my $original = &Apache::lonnet::filelocation('',$formname);
12229:             if(!copy($original,$source)) {
12230:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
12231:                 return (&mt('Failed to write file'));
12232:             }
12233:         }
12234:         close(FH);
12235:         chmod(0660, $source); # Permissions to rw-rw---.
12236: 
12237:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
12238:         my $copyfile=$targetdir.'/'.$file;
12239: 
12240:         my @parts=split(/\//,$targetdir);
12241:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
12242:         for (my $count=5;$count<=$#parts;$count++) {
12243:             $path.="/$parts[$count]";
12244:             if (!-e $path) {
12245:                 print $logfile "\nCreating directory ".$path;
12246:                 mkdir($path,02770);
12247:             }
12248:         }
12249:         my $versionresult;
12250:         if (-e $copyfile) {
12251:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
12252:         } else {
12253:             $versionresult = 'ok';
12254:         }
12255:         if ($versionresult eq 'ok') {
12256:             if (copy($source,$copyfile)) {
12257:                 print $logfile "\nCopied original source to ".$copyfile."\n";
12258:                 $output = 'ok';
12259:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
12260:                 push(@{$modified_urls},[$copyfile,$source]);
12261:                 my $metaoutput = 
12262:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
12263:                 unless ($registered_cleanup) {
12264:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
12265:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
12266:                     $registered_cleanup=1;
12267:                 }
12268:             } else {
12269:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
12270:                 $output = &mt('Failed to copy file to RES space').", $!";
12271:             }
12272:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
12273:                 my $inputfile = $filepath.'/'.$file;
12274:                 my $outfile = $filepath.'/'.'tn-'.$file;
12275:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
12276:                 if ($fullwidth ne '' && $fullheight ne '') { 
12277:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
12278:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
12279:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
12280:                         system({$args[0]} @args);
12281:                         chmod(0660, $filepath.'/tn-'.$file);
12282:                         if (-e $outfile) {
12283:                             my $copyfile=$targetdir.'/tn-'.$file;
12284:                             if (copy($outfile,$copyfile)) {
12285:                                 print $logfile "\nCopied source to ".$copyfile."\n";
12286:                                 my $thumb_metaoutput = 
12287:                                     &write_metadata($dom,$confname,$formname,
12288:                                                     $targetdir,'tn-'.$file,$logfile);
12289:                                 push(@{$modified_urls},[$copyfile,$outfile]);
12290:                                 unless ($registered_cleanup) {
12291:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
12292:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
12293:                                     $registered_cleanup=1;
12294:                                 }
12295:                                 $madethumb = 1;
12296:                             } else {
12297:                                 print $logfile "\nUnable to write ".$copyfile.
12298:                                                ':'.$!."\n";
12299:                             }
12300:                         }
12301:                     }
12302:                 }
12303:             }
12304:         } else {
12305:             $output = $versionresult;
12306:         }
12307:     }
12308:     return ($output,$logourl,$madethumb);
12309: }
12310: 
12311: sub logo_versioning {
12312:     my ($targetdir,$file,$logfile) = @_;
12313:     my $target = $targetdir.'/'.$file;
12314:     my ($maxversion,$fn,$extn,$output);
12315:     $maxversion = 0;
12316:     if ($file =~ /^(.+)\.(\w+)$/) {
12317:         $fn=$1;
12318:         $extn=$2;
12319:     }
12320:     opendir(DIR,$targetdir);
12321:     while (my $filename=readdir(DIR)) {
12322:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
12323:             $maxversion=($1>$maxversion)?$1:$maxversion;
12324:         }
12325:     }
12326:     $maxversion++;
12327:     print $logfile "\nCreating old version ".$maxversion."\n";
12328:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
12329:     if (copy($target,$copyfile)) {
12330:         print $logfile "Copied old target to ".$copyfile."\n";
12331:         $copyfile=$copyfile.'.meta';
12332:         if (copy($target.'.meta',$copyfile)) {
12333:             print $logfile "Copied old target metadata to ".$copyfile."\n";
12334:             $output = 'ok';
12335:         } else {
12336:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
12337:             $output = &mt('Failed to copy old meta').", $!, ";
12338:         }
12339:     } else {
12340:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
12341:         $output = &mt('Failed to copy old target').", $!, ";
12342:     }
12343:     return $output;
12344: }
12345: 
12346: sub write_metadata {
12347:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
12348:     my (%metadatafields,%metadatakeys,$output);
12349:     $metadatafields{'title'}=$formname;
12350:     $metadatafields{'creationdate'}=time;
12351:     $metadatafields{'lastrevisiondate'}=time;
12352:     $metadatafields{'copyright'}='public';
12353:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
12354:                                          $env{'user.domain'};
12355:     $metadatafields{'authorspace'}=$confname.':'.$dom;
12356:     $metadatafields{'domain'}=$dom;
12357:     {
12358:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
12359:         my $mfh;
12360:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
12361:             foreach (sort(keys(%metadatafields))) {
12362:                 unless ($_=~/\./) {
12363:                     my $unikey=$_;
12364:                     $unikey=~/^([A-Za-z]+)/;
12365:                     my $tag=$1;
12366:                     $tag=~tr/A-Z/a-z/;
12367:                     print $mfh "\n\<$tag";
12368:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
12369:                         my $value=$metadatafields{$unikey.'.'.$_};
12370:                         $value=~s/\"/\'\'/g;
12371:                         print $mfh ' '.$_.'="'.$value.'"';
12372:                     }
12373:                     print $mfh '>'.
12374:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
12375:                             .'</'.$tag.'>';
12376:                 }
12377:             }
12378:             $output = 'ok';
12379:             print $logfile "\nWrote metadata";
12380:             close($mfh);
12381:         } else {
12382:             print $logfile "\nFailed to open metadata file";
12383:             $output = &mt('Could not write metadata');
12384:         }
12385:     }
12386:     return $output;
12387: }
12388: 
12389: sub notifysubscribed {
12390:     foreach my $targetsource (@{$modified_urls}){
12391:         next unless (ref($targetsource) eq 'ARRAY');
12392:         my ($target,$source)=@{$targetsource};
12393:         if ($source ne '') {
12394:             if (open(my $logfh,">>",$source.'.log')) {
12395:                 print $logfh "\nCleanup phase: Notifications\n";
12396:                 my @subscribed=&subscribed_hosts($target);
12397:                 foreach my $subhost (@subscribed) {
12398:                     print $logfh "\nNotifying host ".$subhost.':';
12399:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
12400:                     print $logfh $reply;
12401:                 }
12402:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
12403:                 foreach my $subhost (@subscribedmeta) {
12404:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
12405:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
12406:                                                         $subhost);
12407:                     print $logfh $reply;
12408:                 }
12409:                 print $logfh "\n============ Done ============\n";
12410:                 close($logfh);
12411:             }
12412:         }
12413:     }
12414:     return OK;
12415: }
12416: 
12417: sub subscribed_hosts {
12418:     my ($target) = @_;
12419:     my @subscribed;
12420:     if (open(my $fh,"<","$target.subscription")) {
12421:         while (my $subline=<$fh>) {
12422:             if ($subline =~ /^($match_lonid):/) {
12423:                 my $host = $1;
12424:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
12425:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
12426:                         push(@subscribed,$host);
12427:                     }
12428:                 }
12429:             }
12430:         }
12431:     }
12432:     return @subscribed;
12433: }
12434: 
12435: sub check_switchserver {
12436:     my ($dom,$confname) = @_;
12437:     my ($allowed,$switchserver);
12438:     my $home = &Apache::lonnet::homeserver($confname,$dom);
12439:     if ($home eq 'no_host') {
12440:         $home = &Apache::lonnet::domain($dom,'primary');
12441:     }
12442:     my @ids=&Apache::lonnet::current_machine_ids();
12443:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
12444:     if (!$allowed) {
12445: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
12446:     }
12447:     return $switchserver;
12448: }
12449: 
12450: sub modify_quotas {
12451:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
12452:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
12453:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
12454:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
12455:         $validationfieldsref);
12456:     if ($action eq 'quotas') {
12457:         $context = 'tools'; 
12458:     } else {
12459:         $context = $action;
12460:     }
12461:     if ($context eq 'requestcourses') {
12462:         @usertools = ('official','unofficial','community','textbook','placement','lti');
12463:         @options =('norequest','approval','validate','autolimit');
12464:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
12465:         %titles = &courserequest_titles();
12466:         $toolregexp = join('|',@usertools);
12467:         %conditions = &courserequest_conditions();
12468:         $confname = $dom.'-domainconfig';
12469:         my $servadm = $r->dir_config('lonAdmEMail');
12470:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12471:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
12472:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
12473:     } elsif ($context eq 'requestauthor') {
12474:         @usertools = ('author');
12475:         %titles = &authorrequest_titles();
12476:     } else {
12477:         @usertools = ('aboutme','blog','webdav','portfolio');
12478:         %titles = &tool_titles();
12479:     }
12480:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12481:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12482:     foreach my $key (keys(%env)) {
12483:         if ($context eq 'requestcourses') {
12484:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
12485:                 my $item = $1;
12486:                 my $type = $2;
12487:                 if ($type =~ /^limit_(.+)/) {
12488:                     $limithash{$item}{$1} = $env{$key};
12489:                 } else {
12490:                     $confhash{$item}{$type} = $env{$key};
12491:                 }
12492:             }
12493:         } elsif ($context eq 'requestauthor') {
12494:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
12495:                 $confhash{$1} = $env{$key};
12496:             }
12497:         } else {
12498:             if ($key =~ /^form\.quota_(.+)$/) {
12499:                 $confhash{'defaultquota'}{$1} = $env{$key};
12500:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
12501:                 $confhash{'authorquota'}{$1} = $env{$key};
12502:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
12503:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
12504:             }
12505:         }
12506:     }
12507:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
12508:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
12509:         @approvalnotify = sort(@approvalnotify);
12510:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
12511:         my @crstypes = ('official','unofficial','community','textbook','placement','lti');
12512:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
12513:         foreach my $type (@hasuniquecode) {
12514:             if (grep(/^\Q$type\E$/,@crstypes)) {
12515:                 $confhash{'uniquecode'}{$type} = 1;
12516:             }
12517:         }
12518:         my (%newbook,%allpos);
12519:         if ($context eq 'requestcourses') {
12520:             foreach my $type ('textbooks','templates') {
12521:                 @{$allpos{$type}} = (); 
12522:                 my $invalid;
12523:                 if ($type eq 'textbooks') {
12524:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
12525:                 } else {
12526:                     $invalid = &mt('Invalid LON-CAPA course for template');
12527:                 }
12528:                 if ($env{'form.'.$type.'_addbook'}) {
12529:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
12530:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
12531:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
12532:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
12533:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
12534:                         } else {
12535:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
12536:                             my $position = $env{'form.'.$type.'_addbook_pos'};
12537:                             $position =~ s/\D+//g;
12538:                             if ($position ne '') {
12539:                                 $allpos{$type}[$position] = $newbook{$type};
12540:                             }
12541:                         }
12542:                     } else {
12543:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
12544:                     }
12545:                 }
12546:             } 
12547:         }
12548:         if (ref($domconfig{$action}) eq 'HASH') {
12549:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
12550:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
12551:                     $changes{'notify'}{'approval'} = 1;
12552:                 }
12553:             } else {
12554:                 if ($confhash{'notify'}{'approval'}) {
12555:                     $changes{'notify'}{'approval'} = 1;
12556:                 }
12557:             }
12558:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
12559:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
12560:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
12561:                         unless ($confhash{'uniquecode'}{$crstype}) {
12562:                             $changes{'uniquecode'} = 1;
12563:                         }
12564:                     }
12565:                     unless ($changes{'uniquecode'}) {
12566:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
12567:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
12568:                                 $changes{'uniquecode'} = 1;
12569:                             }
12570:                         }
12571:                     }
12572:                } else {
12573:                    $changes{'uniquecode'} = 1;
12574:                }
12575:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
12576:                 $changes{'uniquecode'} = 1;
12577:             }
12578:             if ($context eq 'requestcourses') {
12579:                 foreach my $type ('textbooks','templates') {
12580:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
12581:                         my %deletions;
12582:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
12583:                         if (@todelete) {
12584:                             map { $deletions{$_} = 1; } @todelete;
12585:                         }
12586:                         my %imgdeletions;
12587:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
12588:                         if (@todeleteimages) {
12589:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
12590:                         }
12591:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
12592:                         for (my $i=0; $i<=$maxnum; $i++) {
12593:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
12594:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
12595:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
12596:                                 if ($deletions{$key}) {
12597:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
12598:                                         #FIXME need to obsolete item in RES space
12599:                                     }
12600:                                     next;
12601:                                 } else {
12602:                                     my $newpos = $env{'form.'.$itemid};
12603:                                     $newpos =~ s/\D+//g;
12604:                                     foreach my $item ('subject','title','publisher','author') {
12605:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
12606:                                                  ($type eq 'templates'));
12607:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
12608:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
12609:                                             $changes{$type}{$key} = 1;
12610:                                         }
12611:                                     }
12612:                                     $allpos{$type}[$newpos] = $key;
12613:                                 }
12614:                                 if ($imgdeletions{$key}) {
12615:                                     $changes{$type}{$key} = 1;
12616:                                     #FIXME need to obsolete item in RES space
12617:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
12618:                                     my ($cdom,$cnum) = split(/_/,$key);
12619:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
12620:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
12621:                                     } else {
12622:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
12623:                                                                                       $cdom,$cnum,$type,$configuserok,
12624:                                                                                       $switchserver,$author_ok);
12625:                                         if ($imgurl) {
12626:                                             $confhash{$type}{$key}{'image'} = $imgurl;
12627:                                             $changes{$type}{$key} = 1; 
12628:                                         }
12629:                                         if ($error) {
12630:                                             &Apache::lonnet::logthis($error);
12631:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12632:                                         }
12633:                                     }
12634:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
12635:                                     $confhash{$type}{$key}{'image'} = 
12636:                                         $domconfig{$action}{$type}{$key}{'image'};
12637:                                 }
12638:                             }
12639:                         }
12640:                     }
12641:                 }
12642:             }
12643:         } else {
12644:             if ($confhash{'notify'}{'approval'}) {
12645:                 $changes{'notify'}{'approval'} = 1;
12646:             }
12647:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
12648:                 $changes{'uniquecode'} = 1;
12649:             }
12650:         }
12651:         if ($context eq 'requestcourses') {
12652:             foreach my $type ('textbooks','templates') {
12653:                 if ($newbook{$type}) {
12654:                     $changes{$type}{$newbook{$type}} = 1;
12655:                     foreach my $item ('subject','title','publisher','author') {
12656:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
12657:                                  ($type eq 'template'));
12658:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
12659:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
12660:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
12661:                         }
12662:                     }
12663:                     if ($type eq 'textbooks') {
12664:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
12665:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
12666:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
12667:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
12668:                             } else {
12669:                                 my ($imageurl,$error) =
12670:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
12671:                                                             $configuserok,$switchserver,$author_ok);
12672:                                 if ($imageurl) {
12673:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
12674:                                 }
12675:                                 if ($error) {
12676:                                     &Apache::lonnet::logthis($error);
12677:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12678:                                 }
12679:                             }
12680:                         }
12681:                     }
12682:                 }
12683:                 if (@{$allpos{$type}} > 0) {
12684:                     my $idx = 0;
12685:                     foreach my $item (@{$allpos{$type}}) {
12686:                         if ($item ne '') {
12687:                             $confhash{$type}{$item}{'order'} = $idx;
12688:                             if (ref($domconfig{$action}) eq 'HASH') {
12689:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
12690:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
12691:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
12692:                                             $changes{$type}{$item} = 1;
12693:                                         }
12694:                                     }
12695:                                 }
12696:                             }
12697:                             $idx ++;
12698:                         }
12699:                     }
12700:                 }
12701:             }
12702:             if (ref($validationitemsref) eq 'ARRAY') {
12703:                 foreach my $item (@{$validationitemsref}) {
12704:                     if ($item eq 'fields') {
12705:                         my @changed;
12706:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
12707:                         if (@{$confhash{'validation'}{$item}} > 0) {
12708:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
12709:                         }
12710:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
12711:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
12712:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
12713:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
12714:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
12715:                                 } else {
12716:                                     @changed = @{$confhash{'validation'}{$item}};
12717:                                 }
12718:                             } else {
12719:                                 @changed = @{$confhash{'validation'}{$item}};
12720:                             }
12721:                         } else {
12722:                             @changed = @{$confhash{'validation'}{$item}};
12723:                         }
12724:                         if (@changed) {
12725:                             if ($confhash{'validation'}{$item}) {
12726:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
12727:                             } else {
12728:                                 $changes{'validation'}{$item} = &mt('None');
12729:                             }
12730:                         }
12731:                     } else {
12732:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
12733:                         if ($item eq 'markup') {
12734:                             if ($env{'form.requestcourses_validation_'.$item}) {
12735:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
12736:                             }
12737:                         }
12738:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
12739:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
12740:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
12741:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
12742:                                 }
12743:                             } else {
12744:                                 if ($confhash{'validation'}{$item} ne '') {
12745:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
12746:                                 }
12747:                             }
12748:                         } else {
12749:                             if ($confhash{'validation'}{$item} ne '') {
12750:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
12751:                             }
12752:                         }
12753:                     }
12754:                 }
12755:             }
12756:             if ($env{'form.validationdc'}) {
12757:                 my $newval = $env{'form.validationdc'};
12758:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
12759:                 if (exists($domcoords{$newval})) {
12760:                     $confhash{'validation'}{'dc'} = $newval;
12761:                 }
12762:             }
12763:             if (ref($confhash{'validation'}) eq 'HASH') {
12764:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
12765:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
12766:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
12767:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
12768:                                 if ($confhash{'validation'}{'dc'} eq '') {
12769:                                     $changes{'validation'}{'dc'} = &mt('None');
12770:                                 } else {
12771:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
12772:                                 }
12773:                             }
12774:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
12775:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
12776:                         }
12777:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
12778:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
12779:                     }
12780:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
12781:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
12782:                 }
12783:             } else {
12784:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
12785:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
12786:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
12787:                             $changes{'validation'}{'dc'} = &mt('None');
12788:                         }
12789:                     }
12790:                 }
12791:             }
12792:         }
12793:     } else {
12794:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
12795:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
12796:     }
12797:     foreach my $item (@usertools) {
12798:         foreach my $type (@{$types},'default','_LC_adv') {
12799:             my $unset; 
12800:             if ($context eq 'requestcourses') {
12801:                 $unset = '0';
12802:                 if ($type eq '_LC_adv') {
12803:                     $unset = '';
12804:                 }
12805:                 if ($confhash{$item}{$type} eq 'autolimit') {
12806:                     $confhash{$item}{$type} .= '=';
12807:                     unless ($limithash{$item}{$type} =~ /\D/) {
12808:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
12809:                     }
12810:                 }
12811:             } elsif ($context eq 'requestauthor') {
12812:                 $unset = '0';
12813:                 if ($type eq '_LC_adv') {
12814:                     $unset = '';
12815:                 }
12816:             } else {
12817:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
12818:                     $confhash{$item}{$type} = 1;
12819:                 } else {
12820:                     $confhash{$item}{$type} = 0;
12821:                 }
12822:             }
12823:             if (ref($domconfig{$action}) eq 'HASH') {
12824:                 if ($action eq 'requestauthor') {
12825:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
12826:                         $changes{$type} = 1;
12827:                     }
12828:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
12829:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
12830:                         $changes{$item}{$type} = 1;
12831:                     }
12832:                 } else {
12833:                     if ($context eq 'requestcourses') {
12834:                         if ($confhash{$item}{$type} ne $unset) {
12835:                             $changes{$item}{$type} = 1;
12836:                         }
12837:                     } else {
12838:                         if (!$confhash{$item}{$type}) {
12839:                             $changes{$item}{$type} = 1;
12840:                         }
12841:                     }
12842:                 }
12843:             } else {
12844:                 if ($context eq 'requestcourses') {
12845:                     if ($confhash{$item}{$type} ne $unset) {
12846:                         $changes{$item}{$type} = 1;
12847:                     }
12848:                 } elsif ($context eq 'requestauthor') {
12849:                     if ($confhash{$type} ne $unset) {
12850:                         $changes{$type} = 1;
12851:                     }
12852:                 } else {
12853:                     if (!$confhash{$item}{$type}) {
12854:                         $changes{$item}{$type} = 1;
12855:                     }
12856:                 }
12857:             }
12858:         }
12859:     }
12860:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
12861:         if (ref($domconfig{'quotas'}) eq 'HASH') {
12862:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
12863:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
12864:                     if (exists($confhash{'defaultquota'}{$key})) {
12865:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
12866:                             $changes{'defaultquota'}{$key} = 1;
12867:                         }
12868:                     } else {
12869:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
12870:                     }
12871:                 }
12872:             } else {
12873:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
12874:                     if (exists($confhash{'defaultquota'}{$key})) {
12875:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
12876:                             $changes{'defaultquota'}{$key} = 1;
12877:                         }
12878:                     } else {
12879:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
12880:                     }
12881:                 }
12882:             }
12883:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
12884:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
12885:                     if (exists($confhash{'authorquota'}{$key})) {
12886:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
12887:                             $changes{'authorquota'}{$key} = 1;
12888:                         }
12889:                     } else {
12890:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
12891:                     }
12892:                 }
12893:             }
12894:         }
12895:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
12896:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
12897:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
12898:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
12899:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
12900:                             $changes{'defaultquota'}{$key} = 1;
12901:                         }
12902:                     } else {
12903:                         if (!exists($domconfig{'quotas'}{$key})) {
12904:                             $changes{'defaultquota'}{$key} = 1;
12905:                         }
12906:                     }
12907:                 } else {
12908:                     $changes{'defaultquota'}{$key} = 1;
12909:                 }
12910:             }
12911:         }
12912:         if (ref($confhash{'authorquota'}) eq 'HASH') {
12913:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
12914:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
12915:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
12916:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
12917:                             $changes{'authorquota'}{$key} = 1;
12918:                         }
12919:                     } else {
12920:                         $changes{'authorquota'}{$key} = 1;
12921:                     }
12922:                 } else {
12923:                     $changes{'authorquota'}{$key} = 1;
12924:                 }
12925:             }
12926:         }
12927:     }
12928: 
12929:     if ($context eq 'requestauthor') {
12930:         $domdefaults{'requestauthor'} = \%confhash;
12931:     } else {
12932:         foreach my $key (keys(%confhash)) {
12933:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
12934:                 $domdefaults{$key} = $confhash{$key};
12935:             }
12936:         }
12937:     }
12938: 
12939:     my %quotahash = (
12940:                       $action => { %confhash }
12941:                     );
12942:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
12943:                                              $dom);
12944:     if ($putresult eq 'ok') {
12945:         if (keys(%changes) > 0) {
12946:             my $cachetime = 24*60*60;
12947:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12948:             if (ref($lastactref) eq 'HASH') {
12949:                 $lastactref->{'domdefaults'} = 1;
12950:             }
12951:             $resulttext = &mt('Changes made:').'<ul>';
12952:             unless (($context eq 'requestcourses') ||
12953:                     ($context eq 'requestauthor')) {
12954:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
12955:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
12956:                     foreach my $type (@{$types},'default') {
12957:                         if (defined($changes{'defaultquota'}{$type})) {
12958:                             my $typetitle = $usertypes->{$type};
12959:                             if ($type eq 'default') {
12960:                                 $typetitle = $othertitle;
12961:                             }
12962:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
12963:                         }
12964:                     }
12965:                     $resulttext .= '</ul></li>';
12966:                 }
12967:                 if (ref($changes{'authorquota'}) eq 'HASH') {
12968:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
12969:                     foreach my $type (@{$types},'default') {
12970:                         if (defined($changes{'authorquota'}{$type})) {
12971:                             my $typetitle = $usertypes->{$type};
12972:                             if ($type eq 'default') {
12973:                                 $typetitle = $othertitle;
12974:                             }
12975:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
12976:                         }
12977:                     }
12978:                     $resulttext .= '</ul></li>';
12979:                 }
12980:             }
12981:             my %newenv;
12982:             foreach my $item (@usertools) {
12983:                 my (%haschgs,%inconf);
12984:                 if ($context eq 'requestauthor') {
12985:                     %haschgs = %changes;
12986:                     %inconf = %confhash;
12987:                 } else {
12988:                     if (ref($changes{$item}) eq 'HASH') {
12989:                         %haschgs = %{$changes{$item}};
12990:                     }
12991:                     if (ref($confhash{$item}) eq 'HASH') {
12992:                         %inconf = %{$confhash{$item}};
12993:                     }
12994:                 }
12995:                 if (keys(%haschgs) > 0) {
12996:                     my $newacc = 
12997:                         &Apache::lonnet::usertools_access($env{'user.name'},
12998:                                                           $env{'user.domain'},
12999:                                                           $item,'reload',$context);
13000:                     if (($context eq 'requestcourses') ||
13001:                         ($context eq 'requestauthor')) {
13002:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
13003:                             $newenv{'environment.canrequest.'.$item} = $newacc;
13004:                         }
13005:                     } else {
13006:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
13007:                             $newenv{'environment.availabletools.'.$item} = $newacc;
13008:                         }
13009:                     }
13010:                     unless ($context eq 'requestauthor') {
13011:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
13012:                     }
13013:                     foreach my $type (@{$types},'default','_LC_adv') {
13014:                         if ($haschgs{$type}) {
13015:                             my $typetitle = $usertypes->{$type};
13016:                             if ($type eq 'default') {
13017:                                 $typetitle = $othertitle;
13018:                             } elsif ($type eq '_LC_adv') {
13019:                                 $typetitle = 'LON-CAPA Advanced Users'; 
13020:                             }
13021:                             if ($inconf{$type}) {
13022:                                 if ($context eq 'requestcourses') {
13023:                                     my $cond;
13024:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
13025:                                         if ($1 eq '') {
13026:                                             $cond = &mt('(Automatic processing of any request).');
13027:                                         } else {
13028:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
13029:                                         }
13030:                                     } else { 
13031:                                         $cond = $conditions{$inconf{$type}};
13032:                                     }
13033:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
13034:                                 } elsif ($context eq 'requestauthor') {
13035:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
13036:                                                              $titles{$inconf{$type}},$typetitle);
13037: 
13038:                                 } else {
13039:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
13040:                                 }
13041:                             } else {
13042:                                 if ($type eq '_LC_adv') {
13043:                                     if ($inconf{$type} eq '0') {
13044:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
13045:                                     } else { 
13046:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
13047:                                     }
13048:                                 } else {
13049:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
13050:                                 }
13051:                             }
13052:                         }
13053:                     }
13054:                     unless ($context eq 'requestauthor') {
13055:                         $resulttext .= '</ul></li>';
13056:                     }
13057:                 }
13058:             }
13059:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
13060:                 if (ref($changes{'notify'}) eq 'HASH') {
13061:                     if ($changes{'notify'}{'approval'}) {
13062:                         if (ref($confhash{'notify'}) eq 'HASH') {
13063:                             if ($confhash{'notify'}{'approval'}) {
13064:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
13065:                             } else {
13066:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
13067:                             }
13068:                         }
13069:                     }
13070:                 }
13071:             }
13072:             if ($action eq 'requestcourses') {
13073:                 my @offon = ('off','on');
13074:                 if ($changes{'uniquecode'}) {
13075:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
13076:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
13077:                         $resulttext .= '<li>'.
13078:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
13079:                                        '</li>';
13080:                     } else {
13081:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
13082:                                        '</li>';
13083:                     }
13084:                 }
13085:                 foreach my $type ('textbooks','templates') {
13086:                     if (ref($changes{$type}) eq 'HASH') {
13087:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
13088:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
13089:                             my %coursehash = &Apache::lonnet::coursedescription($key);
13090:                             my $coursetitle = $coursehash{'description'};
13091:                             my $position = $confhash{$type}{$key}{'order'} + 1;
13092:                             $resulttext .= '<li>';
13093:                             foreach my $item ('subject','title','publisher','author') {
13094:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
13095:                                          ($type eq 'templates'));
13096:                                 my $name = $item.':';
13097:                                 $name =~ s/^(\w)/\U$1/;
13098:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
13099:                             }
13100:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
13101:                             if ($type eq 'textbooks') {
13102:                                 if ($confhash{$type}{$key}{'image'}) {
13103:                                     $resulttext .= ' '.&mt('Image: [_1]',
13104:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
13105:                                                    ' alt="Textbook cover" />').'<br />';
13106:                                 }
13107:                             }
13108:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
13109:                         }
13110:                         $resulttext .= '</ul></li>';
13111:                     }
13112:                 }
13113:                 if (ref($changes{'validation'}) eq 'HASH') {
13114:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
13115:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
13116:                         foreach my $item (@{$validationitemsref}) {
13117:                             if (exists($changes{'validation'}{$item})) {
13118:                                 if ($item eq 'markup') {
13119:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
13120:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
13121:                                 } else {
13122:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
13123:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
13124:                                 }
13125:                             }
13126:                         }
13127:                         if (exists($changes{'validation'}{'dc'})) {
13128:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
13129:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
13130:                         }
13131:                     }
13132:                 }
13133:             }
13134:             $resulttext .= '</ul>';
13135:             if (keys(%newenv)) {
13136:                 &Apache::lonnet::appenv(\%newenv);
13137:             }
13138:         } else {
13139:             if ($context eq 'requestcourses') {
13140:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
13141:             } elsif ($context eq 'requestauthor') {
13142:                 $resulttext = &mt('No changes made to rights to request author space.');
13143:             } else {
13144:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
13145:             }
13146:         }
13147:     } else {
13148:         $resulttext = '<span class="LC_error">'.
13149: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
13150:     }
13151:     if ($errors) {
13152:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
13153:                        '<ul>'.$errors.'</ul></p>';
13154:     }
13155:     return $resulttext;
13156: }
13157: 
13158: sub process_textbook_image {
13159:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
13160:     my $filename = $env{'form.'.$caller.'.filename'};
13161:     my ($error,$url);
13162:     my ($width,$height) = (50,50);
13163:     if ($configuserok eq 'ok') {
13164:         if ($switchserver) {
13165:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
13166:                          $switchserver);
13167:         } elsif ($author_ok eq 'ok') {
13168:             my ($result,$imageurl) =
13169:                 &publishlogo($r,'upload',$caller,$dom,$confname,
13170:                              "$type/$cdom/$cnum/cover",$width,$height);
13171:             if ($result eq 'ok') {
13172:                 $url = $imageurl;
13173:             } else {
13174:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
13175:             }
13176:         } else {
13177:             $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);
13178:         }
13179:     } else {
13180:         $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);
13181:     }
13182:     return ($url,$error);
13183: }
13184: 
13185: sub modify_ltitools {
13186:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
13187:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13188:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
13189:     my $confname = $dom.'-domainconfig';
13190:     my $servadm = $r->dir_config('lonAdmEMail');
13191:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
13192:     my (%posslti,%possfield);
13193:     my @courseroles = ('cc','in','ta','ep','st');
13194:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
13195:     map { $posslti{$_} = 1; } @ltiroles;
13196:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
13197:     map { $possfield{$_} = 1; } @allfields;
13198:     my %lt = &ltitools_names(); 
13199:     if ($env{'form.ltitools_add'}) {
13200:         my $title = $env{'form.ltitools_add_title'};
13201:         $title =~ s/(`)/'/g;
13202:         ($newid,my $error) = &get_ltitools_id($dom,$title);
13203:         if ($newid) {
13204:             my $position = $env{'form.ltitools_add_pos'};
13205:             $position =~ s/\D+//g;
13206:             if ($position ne '') {
13207:                 $allpos[$position] = $newid;
13208:             }
13209:             $changes{$newid} = 1;
13210:             foreach my $item ('title','url','key','secret','lifetime') {
13211:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
13212:                 if ($item eq 'lifetime') {
13213:                     $env{'form.ltitools_add_'.$item} =~ s/[^\d.]//g;
13214:                 }
13215:                 if ($env{'form.ltitools_add_'.$item}) {
13216:                     if (($item eq 'key') || ($item eq 'secret')) {
13217:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
13218:                     } else {
13219:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
13220:                     }
13221:                 }
13222:             }
13223:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
13224:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
13225:             }
13226:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
13227:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
13228:             }
13229:             if ($env{'form.ltitools_add_sigmethod'} eq 'HMAC-SHA256') {
13230:                 $confhash{$newid}{'sigmethod'} = $env{'form.ltitools_add_sigmethod'};
13231:             } else {
13232:                 $confhash{$newid}{'sigmethod'} = 'HMAC-SHA1';
13233:             }
13234:             foreach my $item ('width','height','linktext','explanation') {
13235:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
13236:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
13237:                 if (($item eq 'width') || ($item eq 'height')) {
13238:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
13239:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
13240:                     }
13241:                 } else {
13242:                     if ($env{'form.ltitools_add_'.$item} ne '') {
13243:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item}; 
13244:                     }
13245:                 }
13246:             }
13247:             if ($env{'form.ltitools_add_target'} eq 'window') {
13248:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
13249:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
13250:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
13251:             } else {
13252:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
13253:             }
13254:             foreach my $item ('passback','roster') {
13255:                 if ($env{'form.ltitools_'.$item.'_add'}) {
13256:                     $confhash{$newid}{$item} = 1;
13257:                     if ($env{'form.ltitools_'.$item.'valid_add'} ne '') {
13258:                         my $lifetime = $env{'form.ltitools_'.$item.'valid_add'};
13259:                         $lifetime =~ s/^\s+|\s+$//g;
13260:                         if ($lifetime =~ /^\d+\.?\d*$/) {
13261:                             $confhash{$newid}{$item.'valid'} = $lifetime;
13262:                         }
13263:                     }
13264:                 }
13265:             }
13266:             if ($env{'form.ltitools_add_image.filename'} ne '') {
13267:                 my ($imageurl,$error) =
13268:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
13269:                                             $configuserok,$switchserver,$author_ok);
13270:                 if ($imageurl) {
13271:                     $confhash{$newid}{'image'} = $imageurl;
13272:                 }
13273:                 if ($error) {
13274:                     &Apache::lonnet::logthis($error);
13275:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13276:                 }
13277:             }
13278:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
13279:             foreach my $field (@fields) {
13280:                 if ($possfield{$field}) {
13281:                     if ($field eq 'roles') {
13282:                         foreach my $role (@courseroles) {
13283:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
13284:                             if (($choice ne '') && ($posslti{$choice})) {
13285:                                 $confhash{$newid}{'roles'}{$role} = $choice;
13286:                                 if ($role eq 'cc') {
13287:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
13288:                                 }
13289:                             }
13290:                         }
13291:                     } else {
13292:                         $confhash{$newid}{'fields'}{$field} = 1;
13293:                     }
13294:                 }
13295:             }
13296:             if (ref($confhash{$newid}{'fields'}) eq 'HASH') {
13297:                 if ($confhash{$newid}{'fields'}{'user'}) {
13298:                     if ($env{'form.ltitools_userincdom_add'}) {
13299:                         $confhash{$newid}{'incdom'} = 1;
13300:                     }
13301:                 }
13302:             }
13303:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
13304:             foreach my $item (@courseconfig) {
13305:                 $confhash{$newid}{'crsconf'}{$item} = 1;
13306:             }
13307:             if ($env{'form.ltitools_add_custom'}) {
13308:                 my $name = $env{'form.ltitools_add_custom_name'};
13309:                 my $value = $env{'form.ltitools_add_custom_value'};
13310:                 $value =~ s/(`)/'/g;
13311:                 $name =~ s/(`)/'/g;
13312:                 $confhash{$newid}{'custom'}{$name} = $value;
13313:             }
13314:         } else {
13315:             my $error = &mt('Failed to acquire unique ID for new external tool');   
13316:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13317:         }
13318:     }
13319:     if (ref($domconfig{$action}) eq 'HASH') {
13320:         my %deletions;
13321:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
13322:         if (@todelete) {
13323:             map { $deletions{$_} = 1; } @todelete;
13324:         }
13325:         my %customadds;
13326:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
13327:         if (@newcustom) {
13328:             map { $customadds{$_} = 1; } @newcustom;
13329:         } 
13330:         my %imgdeletions;
13331:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
13332:         if (@todeleteimages) {
13333:             map { $imgdeletions{$_} = 1; } @todeleteimages;
13334:         }
13335:         my $maxnum = $env{'form.ltitools_maxnum'};
13336:         for (my $i=0; $i<=$maxnum; $i++) {
13337:             my $itemid = $env{'form.ltitools_id_'.$i};
13338:             $itemid =~ s/\D+//g;
13339:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
13340:                 if ($deletions{$itemid}) {
13341:                     if ($domconfig{$action}{$itemid}{'image'}) {
13342:                         #FIXME need to obsolete item in RES space
13343:                     }
13344:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
13345:                     next;
13346:                 } else {
13347:                     my $newpos = $env{'form.ltitools_'.$itemid};
13348:                     $newpos =~ s/\D+//g;
13349:                     foreach my $item ('title','url','lifetime') {
13350:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
13351:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
13352:                             $changes{$itemid} = 1;
13353:                         }
13354:                     }
13355:                     foreach my $item ('key','secret') {
13356:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
13357:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
13358:                             $changes{$itemid} = 1;
13359:                         }
13360:                     }
13361:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
13362:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
13363:                     }
13364:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
13365:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
13366:                     }
13367:                     if ($env{'form.ltitools_sigmethod_'.$i} eq 'HMAC-SHA256') {
13368:                         $confhash{$itemid}{'sigmethod'} = $env{'form.ltitools_sigmethod_'.$i};
13369:                     } else {
13370:                         $confhash{$itemid}{'sigmethod'} = 'HMAC-SHA1'; 
13371:                     }
13372:                     if ($domconfig{$action}{$itemid}{'sigmethod'} eq '') {
13373:                         if ($confhash{$itemid}{'sigmethod'} ne 'HMAC-SHA1') {
13374:                             $changes{$itemid} = 1;
13375:                         }
13376:                     } elsif ($domconfig{$action}{$itemid}{'sigmethod'} ne $confhash{$itemid}{'sigmethod'}) {
13377:                         $changes{$itemid} = 1;
13378:                     }
13379:                     foreach my $size ('width','height') {
13380:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
13381:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
13382:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
13383:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
13384:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
13385:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
13386:                                     $changes{$itemid} = 1;
13387:                                 }
13388:                             } else {
13389:                                 $changes{$itemid} = 1;
13390:                             }
13391:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
13392:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
13393:                                 $changes{$itemid} = 1;
13394:                             }
13395:                         }
13396:                     }
13397:                     foreach my $item ('linktext','explanation') {
13398:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
13399:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
13400:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
13401:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
13402:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
13403:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
13404:                                     $changes{$itemid} = 1;
13405:                                 }
13406:                             } else {
13407:                                 $changes{$itemid} = 1;
13408:                             }
13409:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
13410:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
13411:                                 $changes{$itemid} = 1;
13412:                             }
13413:                         }
13414:                     }
13415:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
13416:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
13417:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
13418:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
13419:                     } else {
13420:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
13421:                     }
13422:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
13423:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
13424:                             $changes{$itemid} = 1;
13425:                         }
13426:                     } else {
13427:                         $changes{$itemid} = 1;
13428:                     }
13429:                     foreach my $extra ('passback','roster') {
13430:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
13431:                             $confhash{$itemid}{$extra} = 1;
13432:                             if ($env{'form.ltitools_'.$extra.'valid_'.$i} ne '') {
13433:                                 my $lifetime = $env{'form.ltitools_'.$extra.'valid_'.$i};
13434:                                 $lifetime =~ s/^\s+|\s+$//g;
13435:                                 if ($lifetime =~ /^\d+\.?\d*$/) {
13436:                                     $confhash{$itemid}{$extra.'valid'} = $lifetime;
13437:                                 }
13438:                             }
13439:                         }
13440:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
13441:                             $changes{$itemid} = 1;
13442:                         }
13443:                         if ($domconfig{$action}{$itemid}{$extra.'valid'} ne $confhash{$itemid}{$extra.'valid'}) {
13444:                             $changes{$itemid} = 1;
13445:                         }
13446:                     }
13447:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
13448:                     foreach my $item ('label','title','target','linktext','explanation','append') {
13449:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
13450:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
13451:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
13452:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
13453:                                     $changes{$itemid} = 1;
13454:                                 }
13455:                             } else {
13456:                                 $changes{$itemid} = 1;
13457:                             }
13458:                         }
13459:                     }
13460:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
13461:                     foreach my $field (@fields) {
13462:                         if ($possfield{$field}) {
13463:                             if ($field eq 'roles') {
13464:                                 foreach my $role (@courseroles) {
13465:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
13466:                                     if (($choice ne '') && ($posslti{$choice})) {
13467:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
13468:                                         if ($role eq 'cc') {
13469:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
13470:                                         }
13471:                                     }
13472:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
13473:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
13474:                                             $changes{$itemid} = 1;
13475:                                         }
13476:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
13477:                                         $changes{$itemid} = 1;
13478:                                     }
13479:                                 }
13480:                             } else {
13481:                                 $confhash{$itemid}{'fields'}{$field} = 1;
13482:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
13483:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
13484:                                         $changes{$itemid} = 1;
13485:                                     }
13486:                                 } else {
13487:                                     $changes{$itemid} = 1;
13488:                                 }
13489:                             }
13490:                         }
13491:                     }
13492:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
13493:                         if ($confhash{$itemid}{'fields'}{'user'}) {
13494:                             if ($env{'form.ltitools_userincdom_'.$i}) {
13495:                                 $confhash{$itemid}{'incdom'} = 1;
13496:                             }
13497:                             if ($domconfig{$action}{$itemid}{'incdom'} ne $confhash{$itemid}{'incdom'}) {
13498:                                 $changes{$itemid} = 1;
13499:                             }
13500:                         }
13501:                     }
13502:                     $allpos[$newpos] = $itemid;
13503:                 }
13504:                 if ($imgdeletions{$itemid}) {
13505:                     $changes{$itemid} = 1;
13506:                     #FIXME need to obsolete item in RES space
13507:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
13508:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
13509:                                                                  $itemid,$configuserok,$switchserver,
13510:                                                                  $author_ok);
13511:                     if ($imgurl) {
13512:                         $confhash{$itemid}{'image'} = $imgurl;
13513:                         $changes{$itemid} = 1;
13514:                     }
13515:                     if ($error) {
13516:                         &Apache::lonnet::logthis($error);
13517:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13518:                     }
13519:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
13520:                     $confhash{$itemid}{'image'} =
13521:                        $domconfig{$action}{$itemid}{'image'};
13522:                 }
13523:                 if ($customadds{$i}) {
13524:                     my $name = $env{'form.ltitools_custom_name_'.$i};
13525:                     $name =~ s/(`)/'/g;
13526:                     $name =~ s/^\s+//;
13527:                     $name =~ s/\s+$//;
13528:                     my $value = $env{'form.ltitools_custom_value_'.$i};
13529:                     $value =~ s/(`)/'/g;
13530:                     $value =~ s/^\s+//;
13531:                     $value =~ s/\s+$//;
13532:                     if ($name ne '') {
13533:                         $confhash{$itemid}{'custom'}{$name} = $value;
13534:                         $changes{$itemid} = 1;
13535:                     }
13536:                 }
13537:                 my %customdels;
13538:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
13539:                 if (@customdeletions) {
13540:                     $changes{$itemid} = 1;
13541:                 }
13542:                 map { $customdels{$_} = 1; } @customdeletions;
13543:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
13544:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
13545:                         unless ($customdels{$key}) {
13546:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
13547:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
13548:                             }
13549:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
13550:                                 $changes{$itemid} = 1;
13551:                             }
13552:                         }
13553:                     }
13554:                 }
13555:                 unless ($changes{$itemid}) {
13556:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
13557:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
13558:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
13559:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
13560:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
13561:                                         $changes{$itemid} = 1;
13562:                                         last;
13563:                                     }
13564:                                 }
13565:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
13566:                                 $changes{$itemid} = 1;
13567:                             }
13568:                         }
13569:                         last if ($changes{$itemid});
13570:                     }
13571:                 }
13572:             }
13573:         }
13574:     }
13575:     if (@allpos > 0) {
13576:         my $idx = 0;
13577:         foreach my $itemid (@allpos) {
13578:             if ($itemid ne '') {
13579:                 $confhash{$itemid}{'order'} = $idx;
13580:                 if (ref($domconfig{$action}) eq 'HASH') {
13581:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
13582:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
13583:                             $changes{$itemid} = 1;
13584:                         }
13585:                     }
13586:                 }
13587:                 $idx ++;
13588:             }
13589:         }
13590:     }
13591:     my %ltitoolshash = (
13592:                           $action => { %confhash }
13593:                        );
13594:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
13595:                                              $dom);
13596:     if ($putresult eq 'ok') {
13597:         my %ltienchash = (
13598:                              $action => { %encconfig }
13599:                          );
13600:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom,undef,1);
13601:         if (keys(%changes) > 0) {
13602:             my $cachetime = 24*60*60;
13603:             my %ltiall = %confhash;
13604:             foreach my $id (keys(%ltiall)) {
13605:                 if (ref($encconfig{$id}) eq 'HASH') {
13606:                     foreach my $item ('key','secret') {
13607:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
13608:                     }
13609:                 }
13610:             }
13611:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
13612:             if (ref($lastactref) eq 'HASH') {
13613:                 $lastactref->{'ltitools'} = 1;
13614:             }
13615:             $resulttext = &mt('Changes made:').'<ul>';
13616:             my %bynum;
13617:             foreach my $itemid (sort(keys(%changes))) {
13618:                 my $position = $confhash{$itemid}{'order'};
13619:                 $bynum{$position} = $itemid;
13620:             }
13621:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
13622:                 my $itemid = $bynum{$pos}; 
13623:                 if (ref($confhash{$itemid}) ne 'HASH') {
13624:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
13625:                 } else {
13626:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
13627:                     if ($confhash{$itemid}{'image'}) {
13628:                         $resulttext .= '&nbsp;'.
13629:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
13630:                                        ' alt="'.&mt('Tool Provider icon').'" />';
13631:                     }
13632:                     $resulttext .= '</li><ul>';
13633:                     my $position = $pos + 1;
13634:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
13635:                     foreach my $item ('version','msgtype','sigmethod','url','lifetime') {
13636:                         if ($confhash{$itemid}{$item} ne '') {
13637:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
13638:                         }
13639:                     }
13640:                     if ($encconfig{$itemid}{'key'} ne '') {
13641:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
13642:                     }
13643:                     if ($encconfig{$itemid}{'secret'} ne '') {
13644:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
13645:                         my $num = length($encconfig{$itemid}{'secret'});
13646:                         $resulttext .= ('*'x$num).'</li>';
13647:                     }
13648:                     $resulttext .= '<li>'.&mt('Configurable in course:');
13649:                     my @possconfig = ('label','title','target','linktext','explanation','append');
13650:                     my $numconfig = 0; 
13651:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') { 
13652:                         foreach my $item (@possconfig) {
13653:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
13654:                                 $numconfig ++;
13655:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
13656:                             }
13657:                         }
13658:                     }
13659:                     if (!$numconfig) {
13660:                         $resulttext .= '&nbsp;'.&mt('None');
13661:                     }
13662:                     $resulttext .= '</li>';
13663:                     foreach my $item ('passback','roster') {
13664:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
13665:                         if ($confhash{$itemid}{$item}) {
13666:                             $resulttext .= &mt('Yes');
13667:                             if ($confhash{$itemid}{$item.'valid'}) {
13668:                                 if ($item eq 'passback') {
13669:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,day] after launch',
13670:                                                            $confhash{$itemid}{$item.'valid'});
13671:                                 } else {
13672:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,second] after launch',
13673:                                                            $confhash{$itemid}{$item.'valid'});
13674:                                 }
13675:                             }
13676:                         } else {
13677:                             $resulttext .= &mt('No');
13678:                         }
13679:                         $resulttext .= '</li>';
13680:                     }
13681:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
13682:                         my $displaylist;
13683:                         if ($confhash{$itemid}{'display'}{'target'}) {
13684:                             $displaylist = &mt('Display target').':&nbsp;'.
13685:                                            $confhash{$itemid}{'display'}{'target'}.',';
13686:                         }
13687:                         foreach my $size ('width','height') { 
13688:                             if ($confhash{$itemid}{'display'}{$size}) {
13689:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
13690:                                                 $confhash{$itemid}{'display'}{$size}.',';
13691:                             }
13692:                         }
13693:                         if ($displaylist) {
13694:                             $displaylist =~ s/,$//;
13695:                             $resulttext .= '<li>'.$displaylist.'</li>';
13696:                         }
13697:                         foreach my $item ('linktext','explanation') {
13698:                             if ($confhash{$itemid}{'display'}{$item}) {
13699:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
13700:                             }
13701:                         }
13702:                     }
13703:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
13704:                         my $fieldlist;
13705:                         foreach my $field (@allfields) {
13706:                             if ($confhash{$itemid}{'fields'}{$field}) {
13707:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
13708:                             }
13709:                         }
13710:                         if ($fieldlist) {
13711:                             $fieldlist =~ s/,$//;
13712:                             if ($confhash{$itemid}{'fields'}{'user'}) {
13713:                                 if ($confhash{$itemid}{'incdom'}) {
13714:                                     $fieldlist .= ' ('.&mt('username:domain').')';
13715:                                 } else {
13716:                                     $fieldlist .= ' ('.&mt('username').')';
13717:                                 }
13718:                             }
13719:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
13720:                         }
13721:                     }
13722:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
13723:                         my $rolemaps;
13724:                         foreach my $role (@courseroles) {
13725:                             if ($confhash{$itemid}{'roles'}{$role}) {
13726:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
13727:                                              $confhash{$itemid}{'roles'}{$role}.',';
13728:                             }
13729:                         }
13730:                         if ($rolemaps) {
13731:                             $rolemaps =~ s/,$//; 
13732:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
13733:                         }
13734:                     }
13735:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
13736:                         my $customlist;
13737:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
13738:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
13739:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
13740:                             } 
13741:                         }
13742:                         if ($customlist) {
13743:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
13744:                         }
13745:                     } 
13746:                     $resulttext .= '</ul></li>';
13747:                 }
13748:             }
13749:             $resulttext .= '</ul>';
13750:         } else {
13751:             $resulttext = &mt('No changes made.');
13752:         }
13753:     } else {
13754:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
13755:     }
13756:     if ($errors) {
13757:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
13758:                        $errors.'</ul>';
13759:     }
13760:     return $resulttext;
13761: }
13762: 
13763: sub process_ltitools_image {
13764:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
13765:     my $filename = $env{'form.'.$caller.'.filename'};
13766:     my ($error,$url);
13767:     my ($width,$height) = (21,21);
13768:     if ($configuserok eq 'ok') {
13769:         if ($switchserver) {
13770:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
13771:                          $switchserver);
13772:         } elsif ($author_ok eq 'ok') {
13773:             my ($result,$imageurl,$madethumb) =
13774:                 &publishlogo($r,'upload',$caller,$dom,$confname,
13775:                              "ltitools/$itemid/icon",$width,$height);
13776:             if ($result eq 'ok') {
13777:                 if ($madethumb) {
13778:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
13779:                     my $imagethumb = "$path/tn-".$imagefile;
13780:                     $url = $imagethumb;
13781:                 } else {
13782:                     $url = $imageurl;
13783:                 }
13784:             } else {
13785:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
13786:             }
13787:         } else {
13788:             $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);
13789:         }
13790:     } else {
13791:         $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);
13792:     }
13793:     return ($url,$error);
13794: }
13795: 
13796: sub get_ltitools_id {
13797:     my ($cdom,$title) = @_;
13798:     # get lock on ltitools db
13799:     my $lockhash = {
13800:                       lock => $env{'user.name'}.
13801:                               ':'.$env{'user.domain'},
13802:                    };
13803:     my $tries = 0;
13804:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
13805:     my ($id,$error);
13806:  
13807:     while (($gotlock ne 'ok') && ($tries<10)) {
13808:         $tries ++;
13809:         sleep (0.1);
13810:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
13811:     }
13812:     if ($gotlock eq 'ok') {
13813:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
13814:         if ($currids{'lock'}) {
13815:             delete($currids{'lock'});
13816:             if (keys(%currids)) {
13817:                 my @curr = sort { $a <=> $b } keys(%currids);
13818:                 if ($curr[-1] =~ /^\d+$/) {
13819:                     $id = 1 + $curr[-1];
13820:                 }
13821:             } else {
13822:                 $id = 1;
13823:             }
13824:             if ($id) {
13825:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
13826:                     $error = 'nostore';
13827:                 }
13828:             } else {
13829:                 $error = 'nonumber';
13830:             }
13831:         }
13832:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
13833:     } else {
13834:         $error = 'nolock';
13835:     }
13836:     return ($id,$error);
13837: }
13838: 
13839: sub modify_proctoring {
13840:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
13841:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13842:     my (@allpos,%changes,%confhash,%encconfhash,$errors,$resulttext,%imgdeletions);
13843:     my $confname = $dom.'-domainconfig';
13844:     my $servadm = $r->dir_config('lonAdmEMail');
13845:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
13846:     my %providernames = &proctoring_providernames();
13847:     my $maxnum = scalar(keys(%providernames));
13848: 
13849:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
13850:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
13851:     if (ref($requref) eq 'HASH') {
13852:         %requserfields = %{$requref};
13853:     }
13854:     if (ref($opturef) eq 'HASH') {
13855:         %optuserfields = %{$opturef};
13856:     }
13857:     if (ref($defref) eq 'HASH') {
13858:         %defaults = %{$defref};
13859:     }
13860:     if (ref($extref) eq 'HASH') {
13861:         %extended = %{$extref};
13862:     }
13863:     if (ref($crsref) eq 'HASH') {
13864:         %crsconf = %{$crsref};
13865:     }
13866:     if (ref($rolesref) eq 'ARRAY') {
13867:         @courseroles = @{$rolesref};
13868:     }
13869:     if (ref($ltiref) eq 'ARRAY') {
13870:         @ltiroles = @{$ltiref};
13871:     }
13872: 
13873:     if (ref($domconfig{$action}) eq 'HASH') {
13874:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.proctoring_image_del');
13875:         if (@todeleteimages) {
13876:             map { $imgdeletions{$_} = 1; } @todeleteimages;
13877:         }
13878:     }
13879:     my %customadds;
13880:     my @newcustom = &Apache::loncommon::get_env_multiple('form.proctoring_customadd');
13881:     if (@newcustom) {
13882:         map { $customadds{$_} = 1; } @newcustom;
13883:     }
13884:     foreach my $provider (sort(keys(%providernames))) {
13885:         $confhash{$provider} = {};
13886:         my $pos = $env{'form.proctoring_pos_'.$provider};
13887:         $pos =~ s/\D+//g;
13888:         $allpos[$pos] = $provider;
13889:         my (%current,%currentenc);
13890:         my $showroles = 0;
13891:         if (ref($domconfig{$action}) eq 'HASH') {
13892:             if (ref($domconfig{$action}{$provider}) eq 'HASH') {
13893:                 %current = %{$domconfig{$action}{$provider}};
13894:                 foreach my $item ('key','secret') { 
13895:                     $currentenc{$item} = $current{$item};
13896:                     delete($current{$item});
13897:                 }
13898:             }
13899:         }
13900:         if ($env{'form.proctoring_available_'.$provider}) {
13901:             $confhash{$provider}{'available'} = 1;
13902:             unless ($current{'available'}) {
13903:                 $changes{$provider} = 1;
13904:             }
13905:         } else {
13906:             %{$confhash{$provider}} = %current;
13907:             %{$encconfhash{$provider}} = %currentenc;
13908:             $confhash{$provider}{'available'} = 0;
13909:             if ($current{'available'}) {
13910:                 $changes{$provider} = 1;
13911:             }
13912:         }
13913:         if ($confhash{$provider}{'available'}) {
13914:             foreach my $field ('lifetime','version','sigmethod','url','key','secret') {
13915:                 my $possval = $env{'form.proctoring_'.$provider.'_'.$field};
13916:                 if ($field eq 'lifetime') {
13917:                     if ($possval =~ /^\d+$/) {
13918:                         $confhash{$provider}{$field} = $possval;
13919:                     }
13920:                 } elsif ($field eq 'version') {
13921:                     if ($possval =~ /^\d+\.\d+$/) {
13922:                         $confhash{$provider}{$field} = $possval;
13923:                     }
13924:                 } elsif ($field eq 'sigmethod') {
13925:                     if ($possval =~ /^\QHMAC-SHA\E(1|256)$/) {
13926:                         $confhash{$provider}{$field} = $possval;
13927:                     }
13928:                 } elsif ($field eq 'url') {
13929:                     $confhash{$provider}{$field} = $possval;
13930:                 } elsif (($field eq 'key') || ($field eq 'secret')) {
13931:                     $encconfhash{$provider}{$field} = $possval;
13932:                     unless ($currentenc{$field} eq $possval) {
13933:                         $changes{$provider} = 1;
13934:                     }
13935:                 }
13936:                 unless (($field eq 'key') || ($field eq 'secret')) {
13937:                     unless ($current{$field} eq $confhash{$provider}{$field}) {
13938:                         $changes{$provider} = 1;
13939:                     }
13940:                 }
13941:             }
13942:             if ($imgdeletions{$provider}) {
13943:                 $changes{$provider} = 1;
13944:             } elsif ($env{'form.proctoring_image_'.$provider.'.filename'} ne '') {
13945:                 my ($imageurl,$error) =
13946:                     &process_proctoring_image($r,$dom,$confname,'proctoring_image_'.$provider,$provider,
13947:                                               $configuserok,$switchserver,$author_ok);
13948:                 if ($imageurl) {
13949:                     $confhash{$provider}{'image'} = $imageurl;
13950:                     $changes{$provider} = 1; 
13951:                 }
13952:                 if ($error) {
13953:                     &Apache::lonnet::logthis($error);
13954:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13955:                 }
13956:             } elsif (exists($current{'image'})) {
13957:                 $confhash{$provider}{'image'} = $current{'image'};
13958:             }
13959:             if (ref($requserfields{$provider}) eq 'ARRAY') {
13960:                 if (@{$requserfields{$provider}} > 0) {
13961:                     if (grep(/^user$/,@{$requserfields{$provider}})) {
13962:                         if ($env{'form.proctoring_userincdom_'.$provider}) {
13963:                             $confhash{$provider}{'incdom'} = 1;
13964:                         }
13965:                         unless ($current{'incdom'} eq $confhash{$provider}{'incdom'}) {
13966:                             $changes{$provider} = 1;
13967:                         }
13968:                     }
13969:                     if (grep(/^roles$/,@{$requserfields{$provider}})) {
13970:                         $showroles = 1;
13971:                     }
13972:                 }
13973:             }
13974:             $confhash{$provider}{'fields'} = [];
13975:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
13976:                 if (@{$optuserfields{$provider}} > 0) {
13977:                     my @optfields = &Apache::loncommon::get_env_multiple('form.proctoring_optional_'.$provider);
13978:                     foreach my $field (@{$optuserfields{$provider}}) {
13979:                         if (grep(/^\Q$field\E$/,@optfields)) {
13980:                             push(@{$confhash{$provider}{'fields'}},$field);
13981:                         }
13982:                     }
13983:                 }
13984:                 if (ref($current{'fields'}) eq 'ARRAY') {
13985:                     unless ($changes{$provider}) {
13986:                         my @new = sort(@{$confhash{$provider}{'fields'}});
13987:                         my @old = sort(@{$current{'fields'}}); 
13988:                         my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
13989:                         if (@diffs) {
13990:                             $changes{$provider} = 1;
13991:                         }
13992:                     }
13993:                 } elsif (@{$confhash{$provider}{'fields'}}) {
13994:                     $changes{$provider} = 1;
13995:                 }
13996:             }
13997:             if (ref($defaults{$provider}) eq 'ARRAY') {  
13998:                 if (@{$defaults{$provider}} > 0) {
13999:                     my %options;
14000:                     if (ref($extended{$provider}) eq 'HASH') {
14001:                         %options = %{$extended{$provider}};
14002:                     }
14003:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_defaults_'.$provider);
14004:                     foreach my $field (@{$defaults{$provider}}) {
14005:                         if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
14006:                             my $poss = $env{'form.proctoring_defaults_'.$field.'_'.$provider};
14007:                             if (grep(/^\Q$poss\E$/,@{$options{$field}})) {
14008:                                 push(@{$confhash{$provider}{'defaults'}},$poss); 
14009:                             }
14010:                         } elsif ((exists($options{$field})) && (ref($options{$field}) eq 'HASH')) {
14011:                             foreach my $inner (keys(%{$options{$field}})) {
14012:                                 if (ref($options{$field}{$inner}) eq 'ARRAY') {
14013:                                     my $poss = $env{'form.proctoring_'.$inner.'_'.$provider};
14014:                                     if (grep(/^\Q$poss\E$/,@{$options{$field}{$inner}})) {
14015:                                         $confhash{$provider}{'defaults'}{$inner} = $poss;
14016:                                     }
14017:                                 } else {
14018:                                     $confhash{$provider}{'defaults'}{$inner} = $env{'form.proctoring_'.$inner.'_'.$provider};
14019:                                 }
14020:                             }
14021:                         } else {
14022:                             if (grep(/^\Q$field\E$/,@checked)) {
14023:                                 push(@{$confhash{$provider}{'defaults'}},$field);
14024:                             }
14025:                         }
14026:                     }
14027:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
14028:                         if (ref($current{'defaults'}) eq 'ARRAY') {
14029:                             unless ($changes{$provider}) {
14030:                                 my @new = sort(@{$confhash{$provider}{'defaults'}});
14031:                                 my @old = sort(@{$current{'defaults'}});
14032:                                 my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
14033:                                 if (@diffs) {
14034:                                     $changes{$provider} = 1; 
14035:                                 }
14036:                             }
14037:                         } elsif (ref($current{'defaults'}) eq 'ARRAY') {
14038:                             if (@{$current{'defaults'}}) {
14039:                                 $changes{$provider} = 1;
14040:                             }
14041:                         }
14042:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
14043:                         if (ref($current{'defaults'}) eq 'HASH') {
14044:                             unless ($changes{$provider}) {
14045:                                 foreach my $key (keys(%{$confhash{$provider}{'defaults'}})) {
14046:                                     unless ($confhash{$provider}{'defaults'}{$key} eq $current{'defaults'}{$key}) {
14047:                                         $changes{$provider} = 1;
14048:                                         last;
14049:                                     }
14050:                                 }
14051:                             }
14052:                             unless ($changes{$provider}) {
14053:                                 foreach my $key (keys(%{$current{'defaults'}})) {
14054:                                     unless ($current{'defaults'}{$key} eq $confhash{$provider}{'defaults'}{$key}) {
14055:                                         $changes{$provider} = 1;
14056:                                         last;
14057:                                     }
14058:                                 }
14059:                             }
14060:                         } elsif (keys(%{$confhash{$provider}{'defaults'}})) {
14061:                             $changes{$provider} = 1;
14062:                         }
14063:                     }
14064:                 }
14065:             }
14066:             if (ref($crsconf{$provider}) eq 'ARRAY') {
14067:                 if (@{$crsconf{$provider}} > 0) {
14068:                     $confhash{$provider}{'crsconf'} = [];
14069:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_crsconf_'.$provider);
14070:                     foreach my $crsfield (@{$crsconf{$provider}}) {
14071:                         if (grep(/^\Q$crsfield\E$/,@checked)) {
14072:                             push(@{$confhash{$provider}{'crsconf'}},$crsfield);
14073:                         }
14074:                     }
14075:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
14076:                         unless ($changes{$provider}) {  
14077:                             my @new = sort(@{$confhash{$provider}{'crsconf'}});
14078:                             my @old = sort(@{$current{'crsconf'}});
14079:                             my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
14080:                             if (@diffs) {
14081:                                 $changes{$provider} = 1;
14082:                             }
14083:                         }
14084:                     } elsif (@{$confhash{$provider}{'crsconf'}}) {
14085:                         $changes{$provider} = 1;
14086:                     }
14087:                 }
14088:             }
14089:             if ($showroles) {
14090:                 $confhash{$provider}{'roles'} = {};
14091:                 foreach my $role (@courseroles) {
14092:                     my $poss = $env{'form.proctoring_roles_'.$role.'_'.$provider};
14093:                     if (grep(/^\Q$poss\E$/,@ltiroles)) {
14094:                         $confhash{$provider}{'roles'}{$role} = $poss;
14095:                     }
14096:                 }
14097:                 unless ($changes{$provider}) {
14098:                     if (ref($current{'roles'}) eq 'HASH') {
14099:                         foreach my $role (keys(%{$current{'roles'}})) {
14100:                             unless ($current{'roles'}{$role} eq $confhash{$provider}{'roles'}{$role}) {
14101:                                 $changes{$provider} = 1;
14102:                                 last
14103:                             }
14104:                         }
14105:                         unless ($changes{$provider}) {
14106:                             foreach my $role (keys(%{$confhash{$provider}{'roles'}})) {
14107:                                 unless ($confhash{$provider}{'roles'}{$role} eq $current{'roles'}{$role}) {
14108:                                     $changes{$provider} = 1;
14109:                                     last;
14110:                                 }
14111:                             }
14112:                         }
14113:                     } elsif (keys(%{$confhash{$provider}{'roles'}})) {
14114:                         $changes{$provider} = 1;
14115:                     }
14116:                 }
14117:             }
14118:             if (ref($current{'custom'}) eq 'HASH') {
14119:                 my @customdels = &Apache::loncommon::get_env_multiple('form.proctoring_customdel_'.$provider);
14120:                 foreach my $key (keys(%{$current{'custom'}})) {
14121:                     if (grep(/^\Q$key\E$/,@customdels)) {
14122:                         $changes{$provider} = 1;
14123:                     } else {
14124:                         $confhash{$provider}{'custom'}{$key} = $env{'form.proctoring_customval_'.$key.'_'.$provider};
14125:                         if ($confhash{$provider}{'custom'}{$key} ne $current{'custom'}{$key}) {
14126:                             $changes{$provider} = 1;
14127:                         }
14128:                     }
14129:                 }
14130:             }
14131:             if ($customadds{$provider}) {
14132:                 my $name = $env{'form.proctoring_custom_name_'.$provider};
14133:                 $name =~ s/(`)/'/g;
14134:                 $name =~ s/^\s+//;
14135:                 $name =~ s/\s+$//;
14136:                 my $value = $env{'form.proctoring_custom_value_'.$provider};
14137:                 $value =~ s/(`)/'/g;
14138:                 $value =~ s/^\s+//;
14139:                 $value =~ s/\s+$//;
14140:                 if ($name ne '') {
14141:                     $confhash{$provider}{'custom'}{$name} = $value;
14142:                     $changes{$provider} = 1;
14143:                 }
14144:             }
14145:         }
14146:     }
14147:     if (@allpos > 0) {
14148:         my $idx = 0;
14149:         foreach my $provider (@allpos) {
14150:             if ($provider ne '') {
14151:                 $confhash{$provider}{'order'} = $idx;
14152:                 unless ($changes{$provider}) {
14153:                     if (ref($domconfig{$action}) eq 'HASH') {
14154:                         if (ref($domconfig{$action}{$provider}) eq 'HASH') {
14155:                             if ($domconfig{$action}{$provider}{'order'} ne $idx) {
14156:                                 $changes{$provider} = 1;
14157:                             }
14158:                         }
14159:                     }
14160:                 }
14161:                 $idx ++;
14162:             }
14163:         }
14164:     }
14165:     my %proc_hash = (
14166:                           $action => { %confhash }
14167:                        );
14168:     my $putresult = &Apache::lonnet::put_dom('configuration',\%proc_hash,
14169:                                              $dom);
14170:     if ($putresult eq 'ok') {
14171:         my %proc_enchash = (
14172:                              $action => { %encconfhash }
14173:                          );
14174:         &Apache::lonnet::put_dom('encconfig',\%proc_enchash,$dom,undef,1);
14175:         if (keys(%changes) > 0) {
14176:             my $cachetime = 24*60*60;
14177:             my %procall = %confhash;
14178:             foreach my $provider (keys(%procall)) {
14179:                 if (ref($encconfhash{$provider}) eq 'HASH') {
14180:                     foreach my $key ('key','secret') {
14181:                         $procall{$provider}{$key} = $encconfhash{$provider}{$key};
14182:                     }
14183:                 }
14184:             }
14185:             &Apache::lonnet::do_cache_new('proctoring',$dom,\%procall,$cachetime);
14186:             if (ref($lastactref) eq 'HASH') {
14187:                 $lastactref->{'proctoring'} = 1;
14188:             }
14189:             $resulttext = &mt('Configuration for Provider(s) with changes:').'<ul>';
14190:             my %bynum;
14191:             foreach my $provider (sort(keys(%changes))) {
14192:                 my $position = $confhash{$provider}{'order'};
14193:                 $bynum{$position} = $provider;
14194:             }
14195:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
14196:                 my $provider = $bynum{$pos};
14197:                 my %lt = &proctoring_titles($provider);
14198:                 my %fieldtitles = &proctoring_fieldtitles($provider);
14199:                 if (!$confhash{$provider}{'available'}) {
14200:                     $resulttext .= '<li>'.&mt('Proctoring integration unavailable for: [_1]','<b>'.$providernames{$provider}.'</b>').'</li>';
14201:                 } else {
14202:                     $resulttext .= '<li>'.&mt('Proctoring integration available for: [_1]','<b>'.$providernames{$provider}.'</b>');
14203:                     if ($confhash{$provider}{'image'}) {
14204:                         $resulttext .= '&nbsp;'.
14205:                                        '<img src="'.$confhash{$provider}{'image'}.'"'.
14206:                                        ' alt="'.&mt('Proctoring icon').'" />';
14207:                     }
14208:                     $resulttext .= '<ul>';
14209:                     my $position = $pos + 1;
14210:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
14211:                     foreach my $key ('version','sigmethod','url','lifetime') {
14212:                         if ($confhash{$provider}{$key} ne '') {
14213:                             $resulttext .= '<li>'.$lt{$key}.':&nbsp;'.$confhash{$provider}{$key}.'</li>';
14214:                         }
14215:                     }
14216:                     if ($encconfhash{$provider}{'key'} ne '') {
14217:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfhash{$provider}{'key'}.'</li>';
14218:                     }
14219:                     if ($encconfhash{$provider}{'secret'} ne '') {
14220:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
14221:                         my $num = length($encconfhash{$provider}{'secret'});
14222:                         $resulttext .= ('*'x$num).'</li>';
14223:                     }
14224:                     my (@fields,$showroles);
14225:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
14226:                         push(@fields,@{$requserfields{$provider}});
14227:                     }
14228:                     if (ref($confhash{$provider}{'fields'}) eq 'ARRAY') {
14229:                         push(@fields,@{$confhash{$provider}{'fields'}});
14230:                     } elsif (ref($confhash{$provider}{'fields'}) eq 'HASH') {
14231:                         push(@fields,(keys(%{$confhash{$provider}{'fields'}})));
14232:                     }
14233:                     if (@fields) {
14234:                         if (grep(/^roles$/,@fields)) {
14235:                             $showroles = 1;
14236:                         }
14237:                         $resulttext .= '<li>'.$lt{'udsl'}.':&nbsp;"'.
14238:                                        join('", "', map { $lt{$_}; } @fields).'"</li>';
14239:                     }
14240:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
14241:                         if (grep(/^user$/,@{$requserfields{$provider}})) {
14242:                             if ($confhash{$provider}{'incdom'}) {
14243:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'uname:dom'}).'</li>';
14244:                             } else { 
14245:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'username'}).'</li>';
14246:                             }
14247:                         }
14248:                     }
14249:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
14250:                         if (@{$confhash{$provider}{'defaults'}} > 0) {
14251:                             $resulttext .= '<li>'.$lt{'defa'};
14252:                             foreach my $field (@{$confhash{$provider}{'defaults'}}) {
14253:                                 $resulttext .= ' "'.$fieldtitles{$field}.'",';
14254:                             }
14255:                             $resulttext =~ s/,$//;
14256:                             $resulttext .= '</li>';
14257:                         }
14258:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
14259:                         if (keys(%{$confhash{$provider}{'defaults'}})) {
14260:                             $resulttext .= '<li>'.$lt{'defa'}.':&nbsp;<ul>';
14261:                             foreach my $key (sort(keys(%{$confhash{$provider}{'defaults'}}))) {
14262:                                 if ($confhash{$provider}{'defaults'}{$key} ne '') {
14263:                                     $resulttext .= '<li>'.$fieldtitles{$key}.' = '.$confhash{$provider}{'defaults'}{$key}.'</li>';
14264:                                 }
14265:                             }
14266:                             $resulttext .= '</ul></li>';
14267:                         }
14268:                     }
14269:                     if (ref($crsconf{$provider}) eq 'ARRAY') {
14270:                         if (@{$crsconf{$provider}} > 0) {
14271:                             $resulttext .= '<li>'.&mt('Configurable in course:');
14272:                             my $numconfig = 0;
14273:                             if (ref($confhash{$provider}{'crsconf'}) eq 'ARRAY') {
14274:                                 if (@{$confhash{$provider}{'crsconf'}} > 0) {
14275:                                     foreach my $field (@{$confhash{$provider}{'crsconf'}}) {
14276:                                         $numconfig ++;
14277:                                         if ($provider eq 'examity') {
14278:                                             $resulttext .= ' "'.$lt{'crs'.$field}.'",';
14279:                                         } else {
14280:                                             $resulttext .= ' "'.$fieldtitles{$field}.'",';
14281:                                         }
14282:                                     }
14283:                                     $resulttext =~ s/,$//;
14284:                                 }
14285:                             }
14286:                             if (!$numconfig) {
14287:                                 $resulttext .= '&nbsp;'.&mt('None');
14288:                             }
14289:                             $resulttext .= '</li>';
14290:                         }
14291:                     }
14292:                     if ($showroles) {
14293:                         if (ref($confhash{$provider}{'roles'}) eq 'HASH') {
14294:                             my $rolemaps;
14295:                             foreach my $role (@courseroles) {
14296:                                 if ($confhash{$provider}{'roles'}{$role}) {
14297:                                     $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
14298:                                                  $confhash{$provider}{'roles'}{$role}.',';
14299:                                 }
14300:                             }
14301:                             if ($rolemaps) {
14302:                                 $rolemaps =~ s/,$//;
14303:                                 $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
14304:                             }
14305:                         }
14306:                     }
14307:                     if (ref($confhash{$provider}{'custom'}) eq 'HASH') {
14308:                         my $customlist;
14309:                         if (keys(%{$confhash{$provider}{'custom'}})) {
14310:                             foreach my $key (sort(keys(%{$confhash{$provider}{'custom'}}))) {
14311:                                 $customlist .= $key.'='.$confhash{$provider}{'custom'}{$key}.', ';
14312:                             }
14313:                             $customlist =~ s/,$//;
14314:                         }
14315:                         if ($customlist) {
14316:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
14317:                         }
14318:                     } 
14319:                     $resulttext .= '</ul></li>';
14320:                 }
14321:             }
14322:             $resulttext .= '</ul>';
14323:         } else {
14324:             $resulttext = &mt('No changes made.');
14325:         }
14326:     } else {
14327:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
14328:     }
14329:     if ($errors) {
14330:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
14331:                        $errors.'</ul>';
14332:     }
14333:     return $resulttext;
14334: }
14335: 
14336: sub process_proctoring_image {
14337:     my ($r,$dom,$confname,$caller,$provider,$configuserok,$switchserver,$author_ok) = @_;
14338:     my $filename = $env{'form.'.$caller.'.filename'};
14339:     my ($error,$url);
14340:     my ($width,$height) = (21,21);
14341:     if ($configuserok eq 'ok') {
14342:         if ($switchserver) {
14343:             $error = &mt('Upload of Remote Proctoring Provider icon is not permitted to this server: [_1]',
14344:                          $switchserver);
14345:         } elsif ($author_ok eq 'ok') {
14346:             my ($result,$imageurl,$madethumb) =
14347:                 &publishlogo($r,'upload',$caller,$dom,$confname,
14348:                              "proctoring/$provider/icon",$width,$height);
14349:             if ($result eq 'ok') {
14350:                 if ($madethumb) {
14351:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
14352:                     my $imagethumb = "$path/tn-".$imagefile;
14353:                     $url = $imagethumb;
14354:                 } else {
14355:                     $url = $imageurl;
14356:                 }
14357:             } else {
14358:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
14359:             }
14360:         } else {
14361:             $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);
14362:         }
14363:     } else {
14364:         $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);
14365:     }
14366:     return ($url,$error);
14367: }
14368: 
14369: sub modify_lti {
14370:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14371:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14372:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
14373:     my (%posslti,%posslticrs,%posscrstype);
14374:     my @courseroles = ('cc','in','ta','ep','st');
14375:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
14376:     my @lticourseroles = qw(Instructor TeachingAssistant Mentor Learner);
14377:     my @coursetypes = ('official','unofficial','community','textbook','placement');
14378:     my %coursetypetitles = &Apache::lonlocal::texthash (
14379:                                official   => 'Official',
14380:                                unofficial => 'Unofficial',
14381:                                community  => 'Community',
14382:                                textbook   => 'Textbook',
14383:                                placement  => 'Placement Test',
14384:     );
14385:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
14386:     my %lt = &lti_names();
14387:     map { $posslti{$_} = 1; } @ltiroles;
14388:     map { $posslticrs{$_} = 1; } @lticourseroles;
14389:     map { $posscrstype{$_} = 1; } @coursetypes;
14390: 
14391:     my %menutitles = &ltimenu_titles();
14392: 
14393:     my (@items,%deletions,%itemids);
14394:     if ($env{'form.lti_add'}) {
14395:         my $consumer = $env{'form.lti_consumer_add'};
14396:         $consumer =~ s/(`)/'/g;
14397:         ($newid,my $error) = &get_lti_id($dom,$consumer);
14398:         if ($newid) {
14399:             $itemids{'add'} = $newid;
14400:             push(@items,'add');
14401:             $changes{$newid} = 1;
14402:         } else {
14403:             my $error = &mt('Failed to acquire unique ID for new LTI configuration');
14404:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14405:         }
14406:     }
14407:     if (ref($domconfig{$action}) eq 'HASH') {
14408:         my @todelete = &Apache::loncommon::get_env_multiple('form.lti_del');
14409:         if (@todelete) {
14410:             map { $deletions{$_} = 1; } @todelete;
14411:         }
14412:         my $maxnum = $env{'form.lti_maxnum'};
14413:         for (my $i=0; $i<=$maxnum; $i++) {
14414:             my $itemid = $env{'form.lti_id_'.$i};
14415:             $itemid =~ s/\D+//g;
14416:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
14417:                 if ($deletions{$itemid}) {
14418:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'consumer'};
14419:                 } else {
14420:                    push(@items,$i);
14421:                    $itemids{$i} = $itemid;
14422:                 }
14423:             }
14424:         }
14425:     }
14426:     foreach my $idx (@items) {
14427:         my $itemid = $itemids{$idx};
14428:         next unless ($itemid);
14429:         my $position = $env{'form.lti_pos_'.$idx};
14430:         $position =~ s/\D+//g;
14431:         if ($position ne '') {
14432:             $allpos[$position] = $itemid;
14433:         }
14434:         foreach my $item ('consumer','key','secret','lifetime','requser') {
14435:             my $formitem = 'form.lti_'.$item.'_'.$idx;
14436:             $env{$formitem} =~ s/(`)/'/g;
14437:             if ($item eq 'lifetime') {
14438:                 $env{$formitem} =~ s/[^\d.]//g;
14439:             }
14440:             if ($env{$formitem} ne '') {
14441:                 if (($item eq 'key') || ($item eq 'secret')) {
14442:                     $encconfig{$itemid}{$item} = $env{$formitem};
14443:                 } else {
14444:                     $confhash{$itemid}{$item} = $env{$formitem};
14445:                     unless (($idx eq 'add') || ($changes{$itemid})) {
14446:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
14447:                             $changes{$itemid} = 1;
14448:                         }
14449:                     }
14450:                 }
14451:             }
14452:         }
14453:         if ($env{'form.lti_version_'.$idx} eq 'LTI-1p0') {
14454:             $confhash{$itemid}{'version'} = $env{'form.lti_version_'.$idx};
14455:         }
14456:         if ($confhash{$itemid}{'requser'}) {
14457:             if ($env{'form.lti_mapuser_'.$idx} eq 'sourcedid') {
14458:                 $confhash{$itemid}{'mapuser'} = 'lis_person_sourcedid'; 
14459:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'email') {
14460:                 $confhash{$itemid}{'mapuser'} = 'lis_person_contact_email_primary';
14461:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'other') {
14462:                 my $mapuser = $env{'form.lti_customuser_'.$idx};
14463:                 $mapuser =~ s/(`)/'/g;
14464:                 $mapuser =~ s/^\s+|\s+$//g; 
14465:                 $confhash{$itemid}{'mapuser'} = $mapuser; 
14466:             }
14467:             foreach my $ltirole (@lticourseroles) {
14468:                 my $possrole = $env{'form.lti_maprole_'.$ltirole.'_'.$idx};
14469:                 if (grep(/^\Q$possrole\E$/,@courseroles)) {
14470:                     $confhash{$itemid}{'maproles'}{$ltirole} = $possrole;
14471:                 }
14472:             }
14473:             my @possmakeuser = &Apache::loncommon::get_env_multiple('form.lti_makeuser_'.$idx);
14474:             my @makeuser;
14475:             foreach my $ltirole (sort(@possmakeuser)) {
14476:                 if ($posslti{$ltirole}) {
14477:                     push(@makeuser,$ltirole);
14478:                 }
14479:             }
14480:             $confhash{$itemid}{'makeuser'} = \@makeuser;
14481:             if (@makeuser) {
14482:                 my $lcauth = $env{'form.lti_lcauth_'.$idx};
14483:                 if ($lcauth =~ /^(internal|krb4|krb5|localauth)$/) {
14484:                     $confhash{$itemid}{'lcauth'} = $lcauth;
14485:                     if ($lcauth ne 'internal') {
14486:                         my $lcauthparm = $env{'form.lti_lcauthparm_'.$idx};
14487:                         $lcauthparm =~ s/^(\s+|\s+)$//g;
14488:                         $lcauthparm =~ s/`//g;
14489:                         if ($lcauthparm ne '') {
14490:                             $confhash{$itemid}{'lcauthparm'} = $lcauthparm;
14491:                         }
14492:                     }
14493:                 } else {
14494:                     $confhash{$itemid}{'lcauth'} = 'lti';
14495:                 }
14496:             }
14497:             my @possinstdata =  &Apache::loncommon::get_env_multiple('form.lti_instdata_'.$idx);
14498:             if (@possinstdata) {
14499:                 foreach my $field (@possinstdata) {
14500:                     if (exists($fieldtitles{$field})) {
14501:                         push(@{$confhash{$itemid}{'instdata'}});
14502:                     }
14503:                 }
14504:             }
14505:             if (($env{'form.lti_mapcrs_'.$idx} eq 'course_offering_sourcedid') ||
14506:                 ($env{'form.lti_mapcrs_'.$idx} eq 'context_id'))  {
14507:                 $confhash{$itemid}{'mapcrs'} = $env{'form.lti_mapcrs_'.$idx};
14508:             } elsif ($env{'form.lti_mapcrs_'.$idx} eq 'other') {
14509:                 my $mapcrs = $env{'form.lti_mapcrsfield_'.$idx}; 
14510:                 $mapcrs =~ s/(`)/'/g;
14511:                 $mapcrs =~ s/^\s+|\s+$//g;
14512:                 $confhash{$itemid}{'mapcrs'} = $mapcrs;
14513:             }
14514:             my @posstypes = &Apache::loncommon::get_env_multiple('form.lti_mapcrstype_'.$idx);
14515:             my @crstypes;
14516:             foreach my $type (sort(@posstypes)) {
14517:                 if ($posscrstype{$type}) {
14518:                     push(@crstypes,$type);
14519:                 }
14520:             }
14521:             $confhash{$itemid}{'mapcrstype'} = \@crstypes;
14522:             if ($env{'form.lti_makecrs_'.$idx}) {
14523:                 $confhash{$itemid}{'makecrs'} = 1;
14524:             }
14525:             my @possenroll = &Apache::loncommon::get_env_multiple('form.lti_selfenroll_'.$idx);
14526:             my @selfenroll;
14527:             foreach my $type (sort(@possenroll)) {
14528:                 if ($posslticrs{$type}) {
14529:                     push(@selfenroll,$type);
14530:                 }
14531:             }
14532:             $confhash{$itemid}{'selfenroll'} = \@selfenroll;
14533:             if ($env{'form.lti_crssec_'.$idx}) {
14534:                 if ($env{'form.lti_crssecsrc_'.$idx} eq 'course_section_sourcedid') {
14535:                     $confhash{$itemid}{'section'} = $env{'form.lti_crssecsrc_'.$idx};
14536:                 } elsif ($env{'form.lti_crssecsrc_'.$idx} eq 'other') {
14537:                     my $section = $env{'form.lti_customsection_'.$idx};
14538:                     $section =~ s/(`)/'/g;
14539:                     $section =~ s/^\s+|\s+$//g;
14540:                     if ($section ne '') {
14541:                         $confhash{$itemid}{'section'} = $section;
14542:                     }
14543:                 }
14544:             }
14545:             if ($env{'form.lti_callback_'.$idx}) {
14546:                 if ($env{'form.lti_callbackparam_'.$idx}) {
14547:                     my $callback = $env{'form.lti_callbackparam_'.$idx};
14548:                     $callback =~ s/^\s+|\s+$//g;
14549:                     $confhash{$itemid}{'callback'} = $callback;
14550:                 }
14551:             }
14552:             foreach my $field ('passback','roster','topmenu','inlinemenu') {
14553:                 if ($env{'form.lti_'.$field.'_'.$idx}) {
14554:                     $confhash{$itemid}{$field} = 1;
14555:                 }
14556:             }
14557:             if ($env{'form.lti_passback_'.$idx}) {
14558:                 if ($env{'form.lti_passbackformat_'.$idx} eq '1.0') {
14559:                     $confhash{$itemid}{'passbackformat'} = '1.0';
14560:                 } else {
14561:                     $confhash{$itemid}{'passbackformat'} = '1.1';
14562:                 }
14563:             }
14564:             if ($env{'form.lti_topmenu_'.$idx} || $env{'form.lti_inlinemenu_'.$idx}) {
14565:                 $confhash{$itemid}{lcmenu} = [];
14566:                 my @possmenu = &Apache::loncommon::get_env_multiple('form.lti_menuitem_'.$idx);
14567:                 foreach my $field (@possmenu) {
14568:                     if (exists($menutitles{$field})) {
14569:                         if ($field eq 'grades') {
14570:                             next unless ($env{'form.lti_inlinemenu_'.$idx});
14571:                         }
14572:                         push(@{$confhash{$itemid}{lcmenu}},$field);
14573:                     }
14574:                 }
14575:             }
14576:             unless (($idx eq 'add') || ($changes{$itemid})) {
14577:                 foreach my $field ('mapuser','mapcrs','makecrs','section','passback','roster','lcauth','lcauthparm','topmenu','inlinemenu','callback') {
14578:                     if ($domconfig{$action}{$itemid}{$field} ne $confhash{$itemid}{$field}) {
14579:                         $changes{$itemid} = 1;
14580:                     }
14581:                 }
14582:                 unless ($changes{$itemid}) {
14583:                     if ($domconfig{$action}{$itemid}{'passback'} eq $confhash{$itemid}{'passback'}) {
14584:                         if ($domconfig{$action}{$itemid}{'passbackformat'} ne $confhash{$itemid}{'passbackformat'}) {
14585:                             $changes{$itemid} = 1;
14586:                         }
14587:                     }
14588:                 }
14589:                 foreach my $field ('makeuser','mapcrstype','selfenroll','instdata','lcmenu') {
14590:                     unless ($changes{$itemid}) {
14591:                         if (ref($domconfig{$action}{$itemid}{$field}) eq 'ARRAY') {
14592:                             if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
14593:                                 my @diffs = &Apache::loncommon::compare_arrays($domconfig{$action}{$itemid}{$field},
14594:                                                                                $confhash{$itemid}{$field});
14595:                                 if (@diffs) {
14596:                                     $changes{$itemid} = 1;
14597:                                 }
14598:                             } elsif (@{$domconfig{$action}{$itemid}{$field}} > 0) {
14599:                                 $changes{$itemid} = 1;
14600:                             }
14601:                         } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
14602:                             if (@{$confhash{$itemid}{$field}} > 0) {
14603:                                 $changes{$itemid} = 1;
14604:                             }
14605:                         }
14606:                     }
14607:                 }
14608:                 unless ($changes{$itemid}) {
14609:                     if (ref($domconfig{$action}{$itemid}{'maproles'}) eq 'HASH') {
14610:                         if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
14611:                             foreach my $ltirole (keys(%{$domconfig{$action}{$itemid}{'maproles'}})) {
14612:                                 if ($domconfig{$action}{$itemid}{'maproles'}{$ltirole} ne 
14613:                                     $confhash{$itemid}{'maproles'}{$ltirole}) {
14614:                                     $changes{$itemid} = 1;
14615:                                     last;
14616:                                 }
14617:                             }
14618:                             unless ($changes{$itemid}) {
14619:                                 foreach my $ltirole (keys(%{$confhash{$itemid}{'maproles'}})) {
14620:                                     if ($confhash{$itemid}{'maproles'}{$ltirole} ne 
14621:                                         $domconfig{$action}{$itemid}{'maproles'}{$ltirole}) {
14622:                                         $changes{$itemid} = 1;
14623:                                         last;
14624:                                     }
14625:                                 }
14626:                             }
14627:                         } elsif (keys(%{$domconfig{$action}{$itemid}{'maproles'}}) > 0) {
14628:                             $changes{$itemid} = 1;
14629:                         }
14630:                     } elsif (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
14631:                         unless ($changes{$itemid}) {
14632:                             if (keys(%{$confhash{$itemid}{'maproles'}}) > 0) {
14633:                                 $changes{$itemid} = 1;
14634:                             }
14635:                         }
14636:                     }
14637:                 }
14638:             }
14639:         }
14640:     }
14641:     if (@allpos > 0) {
14642:         my $idx = 0;
14643:         foreach my $itemid (@allpos) {
14644:             if ($itemid ne '') {
14645:                 $confhash{$itemid}{'order'} = $idx;
14646:                 if (ref($domconfig{$action}) eq 'HASH') {
14647:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
14648:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
14649:                             $changes{$itemid} = 1;
14650:                         }
14651:                     }
14652:                 }
14653:                 $idx ++;
14654:             }
14655:         }
14656:     }
14657:     my %ltihash = (
14658:                           $action => { %confhash }
14659:                        );
14660:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltihash,
14661:                                              $dom);
14662:     if ($putresult eq 'ok') {
14663:         my %ltienchash = (
14664:                              $action => { %encconfig }
14665:                          );
14666:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom,undef,1);
14667:         if (keys(%changes) > 0) {
14668:             my $cachetime = 24*60*60;
14669:             my %ltiall = %confhash;
14670:             foreach my $id (keys(%ltiall)) {
14671:                 if (ref($encconfig{$id}) eq 'HASH') {
14672:                     foreach my $item ('key','secret') {
14673:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
14674:                     }
14675:                 }
14676:             }
14677:             &Apache::lonnet::do_cache_new('lti',$dom,\%ltiall,$cachetime);
14678:             if (ref($lastactref) eq 'HASH') {
14679:                 $lastactref->{'lti'} = 1;
14680:             }
14681:             $resulttext = &mt('Changes made:').'<ul>';
14682:             my %bynum;
14683:             foreach my $itemid (sort(keys(%changes))) {
14684:                 my $position = $confhash{$itemid}{'order'};
14685:                 $bynum{$position} = $itemid;
14686:             }
14687:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
14688:                 my $itemid = $bynum{$pos};
14689:                 if (ref($confhash{$itemid}) ne 'HASH') {
14690:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
14691:                 } else {
14692:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'consumer'}.'</b></li><ul>';
14693:                     my $position = $pos + 1;
14694:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
14695:                     foreach my $item ('version','lifetime') {
14696:                         if ($confhash{$itemid}{$item} ne '') {
14697:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
14698:                         }
14699:                     }
14700:                     if ($encconfig{$itemid}{'key'} ne '') {
14701:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
14702:                     }
14703:                     if ($encconfig{$itemid}{'secret'} ne '') {
14704:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
14705:                         my $num = length($encconfig{$itemid}{'secret'});
14706:                         $resulttext .= ('*'x$num).'</li>';
14707:                     }
14708:                     if ($confhash{$itemid}{'requser'}) {
14709:                         if ($confhash{$itemid}{'mapuser'}) {
14710:                             my $shownmapuser;
14711:                             if ($confhash{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
14712:                                 $shownmapuser = $lt{'sourcedid'}.' (lis_person_sourcedid)';
14713:                             } elsif ($confhash{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
14714:                                 $shownmapuser = $lt{'email'}.' (lis_person_contact_email_primary)';
14715:                             } else {
14716:                                 $shownmapuser = &mt('Other').' ('.$confhash{$itemid}{'mapuser'}.')';
14717:                             }
14718:                             $resulttext .= '<li>'.&mt('LON-CAPA username').': '.$shownmapuser.'</li>';
14719:                         }
14720:                         if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
14721:                             my $rolemaps;
14722:                             foreach my $role (@ltiroles) {
14723:                                 if ($confhash{$itemid}{'maproles'}{$role}) {
14724:                                     $rolemaps .= ('&nbsp;'x2).$role.'='.
14725:                                                  &Apache::lonnet::plaintext($confhash{$itemid}{'maproles'}{$role},
14726:                                                                             'Course').',';
14727:                                 }
14728:                             }
14729:                             if ($rolemaps) {
14730:                                 $rolemaps =~ s/,$//;
14731:                                 $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
14732:                             }
14733:                         }
14734:                         if (ref($confhash{$itemid}{'makeuser'}) eq 'ARRAY') {
14735:                             if (@{$confhash{$itemid}{'makeuser'}} > 0) { 
14736:                                 $resulttext .= '<li>'.&mt('Following roles may create user accounts: [_1]',
14737:                                                           join(', ',@{$confhash{$itemid}{'makeuser'}})).'<br />';
14738:                                 if ($confhash{$itemid}{'lcauth'} eq 'lti') {
14739:                                     $resulttext .= &mt('New users will only be able to authenticate via LTI').'</li>';
14740:                                 } else {
14741:                                     $resulttext .= &mt('New users will be assigned LON-CAPA authentication: [_1]',
14742:                                                        $confhash{$itemid}{'lcauth'});
14743:                                     if ($confhash{$itemid}{'lcauth'} eq 'internal') {
14744:                                         $resulttext .= '; '.&mt('a randomly generated password will be created');
14745:                                     } elsif ($confhash{$itemid}{'lcauth'} eq 'localauth') {
14746:                                         if ($confhash{$itemid}{'lcauthparm'} ne '') {
14747:                                             $resulttext .= ' '.&mt('with argument: [_1]',$confhash{$itemid}{'lcauthparm'});
14748:                                         }
14749:                                     } else {
14750:                                         $resulttext .= '; '.&mt('Kerberos domain: [_1]',$confhash{$itemid}{'lcauthparm'});
14751:                                     }
14752:                                 }
14753:                                 $resulttext .= '</li>';
14754:                             } else {
14755:                                 $resulttext .= '<li>'.&mt('User account creation not permitted.').'</li>';
14756:                             }
14757:                         }
14758:                         if (ref($confhash{$itemid}{'instdata'}) eq 'ARRAY') {
14759:                             if (@{$confhash{$itemid}{'instdata'}} > 0) {
14760:                                 $resulttext .= '<li>'.&mt('Institutional data will be used when creating a new user for: [_1]',
14761:                                                           join(', ',map { $fieldtitles{$_}; } @{$confhash{$itemid}{'instdata'}})).'</li>';
14762:                             } else {
14763:                                 $resulttext .= '<li>'.&mt('No institutional data used when creating a new user.').'</li>';
14764:                             }
14765:                         }
14766:                         if ($confhash{$itemid}{'mapcrs'}) {
14767:                             $resulttext .= '<li>'.&mt('Unique course identifier').': '.$confhash{$itemid}{'mapcrs'}.'</li>';
14768:                         }
14769:                         if (ref($confhash{$itemid}{'mapcrstype'}) eq 'ARRAY') {
14770:                             if (@{$confhash{$itemid}{'mapcrstype'}} > 0) {
14771:                                 $resulttext .= '<li>'.&mt('Mapping for the following LON-CAPA course types: [_1]',
14772:                                                join(', ',map { $coursetypetitles{$_}; } @coursetypes)).
14773:                                                '</li>';
14774:                             } else {
14775:                                 $resulttext .= '<li>'.&mt('No mapping to LON-CAPA courses').'</li>';
14776:                             }
14777:                         }
14778:                         if ($confhash{$itemid}{'makecrs'}) {
14779:                             $resulttext .= '<li>'.&mt('Instructor may create course (if absent).').'</li>';
14780:                         } else {
14781:                             $resulttext .= '<li>'.&mt('Instructor may not create course (if absent).').'</li>';
14782:                         }
14783:                         if (ref($confhash{$itemid}{'selfenroll'}) eq 'ARRAY') {
14784:                             if (@{$confhash{$itemid}{'selfenroll'}} > 0) {
14785:                                 $resulttext .= '<li>'.&mt('Self-enrollment for following roles: [_1]',
14786:                                                           join(', ',@{$confhash{$itemid}{'selfenroll'}})).
14787:                                                '</li>';
14788:                             } else {
14789:                                 $resulttext .= '<li>'.&mt('Self-enrollment not permitted').'</li>';
14790:                             }
14791:                         }
14792:                         if ($confhash{$itemid}{'section'}) {
14793:                             if ($confhash{$itemid}{'section'} eq 'course_section_sourcedid') {
14794:                                 $resulttext .= '<li>'.&mt('User section from standard field:').
14795:                                                      ' (course_section_sourcedid)'.'</li>';  
14796:                             } else {
14797:                                 $resulttext .= '<li>'.&mt('User section from:').' '.
14798:                                                       $confhash{$itemid}{'section'}.'</li>';
14799:                             }
14800:                         } else {
14801:                             $resulttext .= '<li>'.&mt('No section assignment').'</li>';
14802:                         }
14803:                         if ($confhash{$itemid}{'callback'}) {
14804:                             $resulttext .= '<li>'.&mt('Callback setting').': '.$confhash{$itemid}{'callback'}.'</li>';
14805:                         } else {
14806:                             $resulttext .= '<li>'.&mt('No callback to logout LON-CAPA session when user logs out of Comsumer');
14807:                         }
14808:                         foreach my $item ('passback','roster','topmenu','inlinemenu') {
14809:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
14810:                             if ($confhash{$itemid}{$item}) {
14811:                                 $resulttext .= &mt('Yes');
14812:                                 if ($item eq 'passback') {
14813:                                     if ($confhash{$itemid}{'passbackformat'} eq '1.0') {
14814:                                         $resulttext .= '&nbsp;('.&mt('Outcomes Extension (1.0)').')';
14815:                                     } elsif ($confhash{$itemid}{'passbackformat'} eq '1.1') {
14816:                                         $resulttext .= '&nbsp;('.&mt('Outcomes Service (1.1)').')';
14817:                                     }
14818:                                 }
14819:                             } else {
14820:                                 $resulttext .= &mt('No');
14821:                             }
14822:                             $resulttext .= '</li>';
14823:                         }
14824:                         if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
14825:                             if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
14826:                                 $resulttext .= '<li>'.&mt('Menu items:').' '.
14827:                                                join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>'; 
14828:                             } else {
14829:                                 $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>'; 
14830:                             }
14831:                         }
14832:                     }
14833:                     $resulttext .= '</ul></li>';
14834:                 }
14835:             }
14836:             $resulttext .= '</ul>';
14837:         } else {
14838:             $resulttext = &mt('No changes made.');
14839:         }
14840:     } else {
14841:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
14842:     }
14843:     if ($errors) {
14844:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
14845:                        $errors.'</ul>';
14846:     }
14847:     return $resulttext;
14848: }
14849: 
14850: sub get_lti_id {
14851:     my ($domain,$consumer) = @_;
14852:     # get lock on lti db
14853:     my $lockhash = {
14854:                       lock => $env{'user.name'}.
14855:                               ':'.$env{'user.domain'},
14856:                    };
14857:     my $tries = 0;
14858:     my $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
14859:     my ($id,$error);
14860: 
14861:     while (($gotlock ne 'ok') && ($tries<10)) {
14862:         $tries ++;
14863:         sleep (0.1);
14864:         $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
14865:     }
14866:     if ($gotlock eq 'ok') {
14867:         my %currids = &Apache::lonnet::dump_dom('lti',$domain);
14868:         if ($currids{'lock'}) {
14869:             delete($currids{'lock'});
14870:             if (keys(%currids)) {
14871:                 my @curr = sort { $a <=> $b } keys(%currids);
14872:                 if ($curr[-1] =~ /^\d+$/) {
14873:                     $id = 1 + $curr[-1];
14874:                 }
14875:             } else {
14876:                 $id = 1;
14877:             }
14878:             if ($id) {
14879:                 unless (&Apache::lonnet::newput_dom('lti',{ $id => $consumer },$domain) eq 'ok') {
14880:                     $error = 'nostore';
14881:                 }
14882:             } else {
14883:                 $error = 'nonumber';
14884:             }
14885:         }
14886:         my $dellockoutcome = &Apache::lonnet::del_dom('lti',['lock'],$domain);
14887:     } else {
14888:         $error = 'nolock';
14889:     }
14890:     return ($id,$error);
14891: }
14892: 
14893: sub modify_autoenroll {
14894:     my ($dom,$lastactref,%domconfig) = @_;
14895:     my ($resulttext,%changes);
14896:     my %currautoenroll;
14897:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
14898:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
14899:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
14900:         }
14901:     }
14902:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
14903:     my %title = ( run => 'Auto-enrollment active',
14904:                   sender => 'Sender for notification messages',
14905:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
14906:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
14907:     my @offon = ('off','on');
14908:     my $sender_uname = $env{'form.sender_uname'};
14909:     my $sender_domain = $env{'form.sender_domain'};
14910:     if ($sender_domain eq '') {
14911:         $sender_uname = '';
14912:     } elsif ($sender_uname eq '') {
14913:         $sender_domain = '';
14914:     }
14915:     my $coowners = $env{'form.autoassign_coowners'};
14916:     my $failsafe = $env{'form.autoenroll_failsafe'};
14917:     $failsafe =~ s{^\s+|\s+$}{}g;
14918:     if ($failsafe =~ /\D/) {
14919:         undef($failsafe);
14920:     }
14921:     my %autoenrollhash =  (
14922:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
14923:                                        'sender_uname' => $sender_uname,
14924:                                        'sender_domain' => $sender_domain,
14925:                                        'co-owners' => $coowners,
14926:                                        'autofailsafe' => $failsafe,
14927:                                 }
14928:                      );
14929:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
14930:                                              $dom);
14931:     if ($putresult eq 'ok') {
14932:         if (exists($currautoenroll{'run'})) {
14933:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
14934:                  $changes{'run'} = 1;
14935:              }
14936:         } elsif ($autorun) {
14937:             if ($env{'form.autoenroll_run'} ne '1') {
14938:                  $changes{'run'} = 1;
14939:             }
14940:         }
14941:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
14942:             $changes{'sender'} = 1;
14943:         }
14944:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
14945:             $changes{'sender'} = 1;
14946:         }
14947:         if ($currautoenroll{'co-owners'} ne '') {
14948:             if ($currautoenroll{'co-owners'} ne $coowners) {
14949:                 $changes{'coowners'} = 1;
14950:             }
14951:         } elsif ($coowners) {
14952:             $changes{'coowners'} = 1;
14953:         }
14954:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
14955:             $changes{'autofailsafe'} = 1;
14956:         }
14957:         if (keys(%changes) > 0) {
14958:             $resulttext = &mt('Changes made:').'<ul>';
14959:             if ($changes{'run'}) {
14960:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
14961:             }
14962:             if ($changes{'sender'}) {
14963:                 if ($sender_uname eq '' || $sender_domain eq '') {
14964:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
14965:                 } else {
14966:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
14967:                 }
14968:             }
14969:             if ($changes{'coowners'}) {
14970:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
14971:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
14972:                 if (ref($lastactref) eq 'HASH') {
14973:                     $lastactref->{'domainconfig'} = 1;
14974:                 }
14975:             }
14976:             if ($changes{'autofailsafe'}) {
14977:                 if ($failsafe ne '') {
14978:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
14979:                 } else {
14980:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
14981:                 }
14982:                 &Apache::lonnet::get_domain_defaults($dom,1);
14983:                 if (ref($lastactref) eq 'HASH') {
14984:                     $lastactref->{'domdefaults'} = 1;
14985:                 }
14986:             }
14987:             $resulttext .= '</ul>';
14988:         } else {
14989:             $resulttext = &mt('No changes made to auto-enrollment settings');
14990:         }
14991:     } else {
14992:         $resulttext = '<span class="LC_error">'.
14993: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
14994:     }
14995:     return $resulttext;
14996: }
14997: 
14998: sub modify_autoupdate {
14999:     my ($dom,%domconfig) = @_;
15000:     my ($resulttext,%currautoupdate,%fields,%changes);
15001:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
15002:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
15003:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
15004:         }
15005:     }
15006:     my @offon = ('off','on');
15007:     my %title = &Apache::lonlocal::texthash (
15008:                     run        => 'Auto-update:',
15009:                     classlists => 'Updates to user information in classlists?',
15010:                     unexpired  => 'Skip updates for users without active or future roles?',
15011:                     lastactive => 'Skip updates for inactive users?',
15012:                 );
15013:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
15014:     my %fieldtitles = &Apache::lonlocal::texthash (
15015:                         id => 'Student/Employee ID',
15016:                         permanentemail => 'E-mail address',
15017:                         lastname => 'Last Name',
15018:                         firstname => 'First Name',
15019:                         middlename => 'Middle Name',
15020:                         generation => 'Generation',
15021:                       );
15022:     $othertitle = &mt('All users');
15023:     if (keys(%{$usertypes}) >  0) {
15024:         $othertitle = &mt('Other users');
15025:     }
15026:     foreach my $key (keys(%env)) {
15027:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
15028:             my ($usertype,$item) = ($1,$2);
15029:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
15030:                 if ($usertype eq 'default') {   
15031:                     push(@{$fields{$1}},$2);
15032:                 } elsif (ref($types) eq 'ARRAY') {
15033:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
15034:                         push(@{$fields{$1}},$2);
15035:                     }
15036:                 }
15037:             }
15038:         }
15039:     }
15040:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
15041:     @lockablenames = sort(@lockablenames);
15042:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
15043:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
15044:         if (@changed) {
15045:             $changes{'lockablenames'} = 1;
15046:         }
15047:     } else {
15048:         if (@lockablenames) {
15049:             $changes{'lockablenames'} = 1;
15050:         }
15051:     }
15052:     my %updatehash = (
15053:                       autoupdate => { run => $env{'form.autoupdate_run'},
15054:                                       classlists => $env{'form.classlists'},
15055:                                       unexpired  => $env{'form.unexpired'},
15056:                                       fields => {%fields},
15057:                                       lockablenames => \@lockablenames,
15058:                                     }
15059:                      );
15060:     my $lastactivedays;
15061:     if ($env{'form.lastactive'}) {
15062:         $lastactivedays = $env{'form.lastactivedays'};
15063:         $lastactivedays =~ s/^\s+|\s+$//g;
15064:         unless ($lastactivedays =~ /^\d+$/) {
15065:             undef($lastactivedays);
15066:             $env{'form.lastactive'} = 0;
15067:         }
15068:     }
15069:     $updatehash{'autoupdate'}{'lastactive'} = $lastactivedays;
15070:     foreach my $key (keys(%currautoupdate)) {
15071:         if (($key eq 'run') || ($key eq 'classlists') || ($key eq 'unexpired') || ($key eq 'lastactive')) {
15072:             if (exists($updatehash{autoupdate}{$key})) {
15073:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
15074:                     $changes{$key} = 1;
15075:                 }
15076:             }
15077:         } elsif ($key eq 'fields') {
15078:             if (ref($currautoupdate{$key}) eq 'HASH') {
15079:                 foreach my $item (@{$types},'default') {
15080:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
15081:                         my $change = 0;
15082:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
15083:                             if (!exists($fields{$item})) {
15084:                                 $change = 1;
15085:                                 last;
15086:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
15087:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
15088:                                     $change = 1;
15089:                                     last;
15090:                                 }
15091:                             }
15092:                         }
15093:                         if ($change) {
15094:                             push(@{$changes{$key}},$item);
15095:                         }
15096:                     } 
15097:                 }
15098:             }
15099:         } elsif ($key eq 'lockablenames') {
15100:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
15101:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
15102:                 if (@changed) {
15103:                     $changes{'lockablenames'} = 1;
15104:                 }
15105:             } else {
15106:                 if (@lockablenames) {
15107:                     $changes{'lockablenames'} = 1;
15108:                 }
15109:             }
15110:         }
15111:     }
15112:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
15113:         if (@lockablenames) {
15114:             $changes{'lockablenames'} = 1;
15115:         }
15116:     }
15117:     unless (grep(/^unexpired$/,keys(%currautoupdate))) {
15118:         if ($updatehash{'autoupdate'}{'unexpired'}) {
15119:             $changes{'unexpired'} = 1;
15120:         }
15121:     }
15122:     unless (grep(/^lastactive$/,keys(%currautoupdate))) {
15123:         if ($updatehash{'autoupdate'}{'lastactive'} ne '') {
15124:             $changes{'lastactive'} = 1;
15125:         }
15126:     }
15127:     foreach my $item (@{$types},'default') {
15128:         if (defined($fields{$item})) {
15129:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
15130:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
15131:                     my $change = 0;
15132:                     if (ref($fields{$item}) eq 'ARRAY') {
15133:                         foreach my $type (@{$fields{$item}}) {
15134:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
15135:                                 $change = 1;
15136:                                 last;
15137:                             }
15138:                         }
15139:                     }
15140:                     if ($change) {
15141:                         push(@{$changes{'fields'}},$item);
15142:                     }
15143:                 } else {
15144:                     push(@{$changes{'fields'}},$item);
15145:                 }
15146:             } else {
15147:                 push(@{$changes{'fields'}},$item);
15148:             }
15149:         }
15150:     }
15151:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
15152:                                              $dom);
15153:     if ($putresult eq 'ok') {
15154:         if (keys(%changes) > 0) {
15155:             $resulttext = &mt('Changes made:').'<ul>';
15156:             foreach my $key (sort(keys(%changes))) {
15157:                 if ($key eq 'lockablenames') {
15158:                     $resulttext .= '<li>';
15159:                     if (@lockablenames) {
15160:                         $usertypes->{'default'} = $othertitle;
15161:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
15162:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
15163:                     } else {
15164:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
15165:                     }
15166:                     $resulttext .= '</li>';
15167:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
15168:                     foreach my $item (@{$changes{$key}}) {
15169:                         my @newvalues;
15170:                         foreach my $type (@{$fields{$item}}) {
15171:                             push(@newvalues,$fieldtitles{$type});
15172:                         }
15173:                         my $newvaluestr;
15174:                         if (@newvalues > 0) {
15175:                             $newvaluestr = join(', ',@newvalues);
15176:                         } else {
15177:                             $newvaluestr = &mt('none');
15178:                         }
15179:                         if ($item eq 'default') {
15180:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
15181:                         } else {
15182:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
15183:                         }
15184:                     }
15185:                 } else {
15186:                     my $newvalue;
15187:                     if ($key eq 'run') {
15188:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
15189:                     } elsif ($key eq 'lastactive') {
15190:                         $newvalue = $offon[$env{'form.lastactive'}];
15191:                         unless ($lastactivedays eq '') {
15192:                             $newvalue .= '; '.&mt('inactive = no activity in last [quant,_1,day]',$lastactivedays);
15193:                         }
15194:                     } else {
15195:                         $newvalue = $offon[$env{'form.'.$key}];
15196:                     }
15197:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
15198:                 }
15199:             }
15200:             $resulttext .= '</ul>';
15201:         } else {
15202:             $resulttext = &mt('No changes made to autoupdates');
15203:         }
15204:     } else {
15205:         $resulttext = '<span class="LC_error">'.
15206: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
15207:     }
15208:     return $resulttext;
15209: }
15210: 
15211: sub modify_autocreate {
15212:     my ($dom,%domconfig) = @_;
15213:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
15214:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
15215:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
15216:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
15217:         }
15218:     }
15219:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
15220:                  req => 'Auto-creation of validated requests for official courses',
15221:                  xmldc => 'Identity of course creator of courses from XML files',
15222:                );
15223:     my @types = ('xml','req');
15224:     foreach my $item (@types) {
15225:         $newvals{$item} = $env{'form.autocreate_'.$item};
15226:         $newvals{$item} =~ s/\D//g;
15227:         $newvals{$item} = 0 if ($newvals{$item} eq '');
15228:     }
15229:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
15230:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
15231:     unless (exists($domcoords{$newvals{'xmldc'}})) {
15232:         $newvals{'xmldc'} = '';
15233:     } 
15234:     %autocreatehash =  (
15235:                         autocreate => { xml => $newvals{'xml'},
15236:                                         req => $newvals{'req'},
15237:                                       }
15238:                        );
15239:     if ($newvals{'xmldc'} ne '') {
15240:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
15241:     }
15242:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
15243:                                              $dom);
15244:     if ($putresult eq 'ok') {
15245:         my @items = @types;
15246:         if ($newvals{'xml'}) {
15247:             push(@items,'xmldc');
15248:         }
15249:         foreach my $item (@items) {
15250:             if (exists($currautocreate{$item})) {
15251:                 if ($currautocreate{$item} ne $newvals{$item}) {
15252:                     $changes{$item} = 1;
15253:                 }
15254:             } elsif ($newvals{$item}) {
15255:                 $changes{$item} = 1;
15256:             }
15257:         }
15258:         if (keys(%changes) > 0) {
15259:             my @offon = ('off','on'); 
15260:             $resulttext = &mt('Changes made:').'<ul>';
15261:             foreach my $item (@types) {
15262:                 if ($changes{$item}) {
15263:                     my $newtxt = $offon[$newvals{$item}];
15264:                     $resulttext .= '<li>'.
15265:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
15266:                                        '<b>','</b>').
15267:                                    '</li>';
15268:                 }
15269:             }
15270:             if ($changes{'xmldc'}) {
15271:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
15272:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
15273:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
15274:             }
15275:             $resulttext .= '</ul>';
15276:         } else {
15277:             $resulttext = &mt('No changes made to auto-creation settings');
15278:         }
15279:     } else {
15280:         $resulttext = '<span class="LC_error">'.
15281:             &mt('An error occurred: [_1]',$putresult).'</span>';
15282:     }
15283:     return $resulttext;
15284: }
15285: 
15286: sub modify_directorysrch {
15287:     my ($dom,$lastactref,%domconfig) = @_;
15288:     my ($resulttext,%changes);
15289:     my %currdirsrch;
15290:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
15291:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
15292:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
15293:         }
15294:     }
15295:     my %title = ( available => 'Institutional directory search available',
15296:                   localonly => 'Other domains can search institution',
15297:                   lcavailable => 'LON-CAPA directory search available',
15298:                   lclocalonly => 'Other domains can search LON-CAPA domain',
15299:                   searchby => 'Search types',
15300:                   searchtypes => 'Search latitude');
15301:     my @offon = ('off','on');
15302:     my @otherdoms = ('Yes','No');
15303: 
15304:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
15305:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
15306:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
15307: 
15308:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
15309:     if (keys(%{$usertypes}) == 0) {
15310:         @cansearch = ('default');
15311:     } else {
15312:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
15313:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
15314:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
15315:                     push(@{$changes{'cansearch'}},$type);
15316:                 }
15317:             }
15318:             foreach my $type (@cansearch) {
15319:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
15320:                     push(@{$changes{'cansearch'}},$type);
15321:                 }
15322:             }
15323:         } else {
15324:             push(@{$changes{'cansearch'}},@cansearch);
15325:         }
15326:     }
15327: 
15328:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
15329:         foreach my $by (@{$currdirsrch{'searchby'}}) {
15330:             if (!grep(/^\Q$by\E$/,@searchby)) {
15331:                 push(@{$changes{'searchby'}},$by);
15332:             }
15333:         }
15334:         foreach my $by (@searchby) {
15335:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
15336:                 push(@{$changes{'searchby'}},$by);
15337:             }
15338:         }
15339:     } else {
15340:         push(@{$changes{'searchby'}},@searchby);
15341:     }
15342: 
15343:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
15344:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
15345:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
15346:                 push(@{$changes{'searchtypes'}},$type);
15347:             }
15348:         }
15349:         foreach my $type (@searchtypes) {
15350:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
15351:                 push(@{$changes{'searchtypes'}},$type);
15352:             }
15353:         }
15354:     } else {
15355:         if (exists($currdirsrch{'searchtypes'})) {
15356:             foreach my $type (@searchtypes) {  
15357:                 if ($type ne $currdirsrch{'searchtypes'}) { 
15358:                     push(@{$changes{'searchtypes'}},$type);
15359:                 }
15360:             }
15361:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
15362:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
15363:             }   
15364:         } else {
15365:             push(@{$changes{'searchtypes'}},@searchtypes); 
15366:         }
15367:     }
15368: 
15369:     my %dirsrch_hash =  (
15370:             directorysrch => { available => $env{'form.dirsrch_available'},
15371:                                cansearch => \@cansearch,
15372:                                localonly => $env{'form.dirsrch_instlocalonly'},
15373:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
15374:                                lcavailable => $env{'form.dirsrch_domavailable'},
15375:                                searchby => \@searchby,
15376:                                searchtypes => \@searchtypes,
15377:                              }
15378:             );
15379:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
15380:                                              $dom);
15381:     if ($putresult eq 'ok') {
15382:         if (exists($currdirsrch{'available'})) {
15383:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
15384:                  $changes{'available'} = 1;
15385:              }
15386:         } else {
15387:             if ($env{'form.dirsrch_available'} eq '1') {
15388:                 $changes{'available'} = 1;
15389:             }
15390:         }
15391:         if (exists($currdirsrch{'lcavailable'})) {
15392:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
15393:                 $changes{'lcavailable'} = 1;
15394:             }
15395:         } else {
15396:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
15397:                 $changes{'lcavailable'} = 1;
15398:             }
15399:         }
15400:         if (exists($currdirsrch{'localonly'})) {
15401:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
15402:                 $changes{'localonly'} = 1;
15403:             }
15404:         } else {
15405:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
15406:                 $changes{'localonly'} = 1;
15407:             }
15408:         }
15409:         if (exists($currdirsrch{'lclocalonly'})) {
15410:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
15411:                 $changes{'lclocalonly'} = 1;
15412:             }
15413:         } else {
15414:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
15415:                 $changes{'lclocalonly'} = 1;
15416:             }
15417:         }
15418:         if (keys(%changes) > 0) {
15419:             $resulttext = &mt('Changes made:').'<ul>';
15420:             if ($changes{'available'}) {
15421:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
15422:             }
15423:             if ($changes{'lcavailable'}) {
15424:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
15425:             }
15426:             if ($changes{'localonly'}) {
15427:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
15428:             }
15429:             if ($changes{'lclocalonly'}) {
15430:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
15431:             }
15432:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
15433:                 my $chgtext;
15434:                 if (ref($usertypes) eq 'HASH') {
15435:                     if (keys(%{$usertypes}) > 0) {
15436:                         foreach my $type (@{$types}) {
15437:                             if (grep(/^\Q$type\E$/,@cansearch)) {
15438:                                 $chgtext .= $usertypes->{$type}.'; ';
15439:                             }
15440:                         }
15441:                         if (grep(/^default$/,@cansearch)) {
15442:                             $chgtext .= $othertitle;
15443:                         } else {
15444:                             $chgtext =~ s/\; $//;
15445:                         }
15446:                         $resulttext .=
15447:                             '<li>'.
15448:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
15449:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
15450:                             '</li>';
15451:                     }
15452:                 }
15453:             }
15454:             if (ref($changes{'searchby'}) eq 'ARRAY') {
15455:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
15456:                 my $chgtext;
15457:                 foreach my $type (@{$titleorder}) {
15458:                     if (grep(/^\Q$type\E$/,@searchby)) {
15459:                         if (defined($searchtitles->{$type})) {
15460:                             $chgtext .= $searchtitles->{$type}.'; ';
15461:                         }
15462:                     }
15463:                 }
15464:                 $chgtext =~ s/\; $//;
15465:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
15466:             }
15467:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
15468:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
15469:                 my $chgtext;
15470:                 foreach my $type (@{$srchtypeorder}) {
15471:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
15472:                         if (defined($srchtypes_desc->{$type})) {
15473:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
15474:                         }
15475:                     }
15476:                 }
15477:                 $chgtext =~ s/\; $//;
15478:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
15479:             }
15480:             $resulttext .= '</ul>';
15481:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
15482:             if (ref($lastactref) eq 'HASH') {
15483:                 $lastactref->{'directorysrch'} = 1;
15484:             }
15485:         } else {
15486:             $resulttext = &mt('No changes made to directory search settings');
15487:         }
15488:     } else {
15489:         $resulttext = '<span class="LC_error">'.
15490:                       &mt('An error occurred: [_1]',$putresult).'</span>';
15491:     }
15492:     return $resulttext;
15493: }
15494: 
15495: sub modify_contacts {
15496:     my ($dom,$lastactref,%domconfig) = @_;
15497:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
15498:     if (ref($domconfig{'contacts'}) eq 'HASH') {
15499:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
15500:             $currsetting{$key} = $domconfig{'contacts'}{$key};
15501:         }
15502:     }
15503:     my (%others,%to,%bcc,%includestr,%includeloc);
15504:     my @contacts = ('supportemail','adminemail');
15505:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
15506:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
15507:     my @toggles = ('reporterrors','reportupdates','reportstatus');
15508:     my @lonstatus = ('threshold','sysmail','weights','excluded');
15509:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
15510:     foreach my $type (@mailings) {
15511:         @{$newsetting{$type}} = 
15512:             &Apache::loncommon::get_env_multiple('form.'.$type);
15513:         foreach my $item (@contacts) {
15514:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
15515:                 $contacts_hash{contacts}{$type}{$item} = 1;
15516:             } else {
15517:                 $contacts_hash{contacts}{$type}{$item} = 0;
15518:             }
15519:         }
15520:         $others{$type} = $env{'form.'.$type.'_others'};
15521:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
15522:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15523:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
15524:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
15525:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
15526:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
15527:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
15528:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
15529:             }
15530:         }
15531:     }
15532:     foreach my $item (@contacts) {
15533:         $to{$item} = $env{'form.'.$item};
15534:         $contacts_hash{'contacts'}{$item} = $to{$item};
15535:     }
15536:     foreach my $item (@toggles) {
15537:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
15538:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
15539:         }
15540:     }
15541:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
15542:     foreach my $item (@lonstatus) {
15543:         if ($item eq 'excluded') {
15544:             my (%serverhomes,@excluded);
15545:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
15546:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
15547:             if (@possexcluded) {
15548:                 foreach my $id (sort(@possexcluded)) {
15549:                     if ($serverhomes{$id}) {
15550:                         push(@excluded,$id);
15551:                     }
15552:                 }
15553:             }
15554:             if (@excluded) {
15555:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
15556:             }
15557:         } elsif ($item eq 'weights') {
15558:             foreach my $type ('E','W','N','U') {
15559:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
15560:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
15561:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
15562:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
15563:                             $env{'form.error'.$item.'_'.$type};
15564:                     }
15565:                 }
15566:             }
15567:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
15568:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
15569:             if ($env{'form.error'.$item} =~ /^\d+$/) {
15570:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
15571:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
15572:                 }
15573:             }
15574:         }
15575:     }
15576:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
15577:         foreach my $field (@{$fields}) {
15578:             if (ref($possoptions->{$field}) eq 'ARRAY') {
15579:                 my $value = $env{'form.helpform_'.$field};
15580:                 $value =~ s/^\s+|\s+$//g;
15581:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
15582:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
15583:                     if ($field eq 'screenshot') {
15584:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
15585:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
15586:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
15587:                         }
15588:                     }
15589:                 }
15590:             }
15591:         }
15592:     }
15593:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
15594:     my (@statuses,%usertypeshash,@overrides);
15595:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
15596:         @statuses = @{$types};
15597:         if (ref($usertypes) eq 'HASH') {
15598:             %usertypeshash = %{$usertypes};
15599:         }
15600:     }
15601:     if (@statuses) {
15602:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
15603:         foreach my $type (@possoverrides) {
15604:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
15605:                 push(@overrides,$type);
15606:             }
15607:         }
15608:         if (@overrides) {
15609:             foreach my $type (@overrides) {
15610:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
15611:                 foreach my $item (@contacts) {
15612:                     if (grep(/^\Q$item\E$/,@standard)) {
15613:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
15614:                         $newsetting{'override_'.$type}{$item} = 1;
15615:                     } else {
15616:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
15617:                         $newsetting{'override_'.$type}{$item} = 0;
15618:                     }
15619:                 }
15620:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
15621:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
15622:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
15623:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
15624:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
15625:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
15626:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
15627:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
15628:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
15629:                 }
15630:             }     
15631:         }
15632:     }
15633:     if (keys(%currsetting) > 0) {
15634:         foreach my $item (@contacts) {
15635:             if ($to{$item} ne $currsetting{$item}) {
15636:                 $changes{$item} = 1;
15637:             }
15638:         }
15639:         foreach my $type (@mailings) {
15640:             foreach my $item (@contacts) {
15641:                 if (ref($currsetting{$type}) eq 'HASH') {
15642:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
15643:                         push(@{$changes{$type}},$item);
15644:                     }
15645:                 } else {
15646:                     push(@{$changes{$type}},@{$newsetting{$type}});
15647:                 }
15648:             }
15649:             if ($others{$type} ne $currsetting{$type}{'others'}) {
15650:                 push(@{$changes{$type}},'others');
15651:             }
15652:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15653:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
15654:                     push(@{$changes{$type}},'bcc'); 
15655:                 }
15656:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
15657:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
15658:                     push(@{$changes{$type}},'include');
15659:                 }
15660:             }
15661:         }
15662:         if (ref($fields) eq 'ARRAY') {
15663:             if (ref($currsetting{'helpform'}) eq 'HASH') {
15664:                 foreach my $field (@{$fields}) {
15665:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
15666:                         push(@{$changes{'helpform'}},$field);
15667:                     }
15668:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
15669:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
15670:                             push(@{$changes{'helpform'}},'maxsize');
15671:                         }
15672:                     }
15673:                 }
15674:             } else {
15675:                 foreach my $field (@{$fields}) {
15676:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
15677:                         push(@{$changes{'helpform'}},$field);
15678:                     }
15679:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
15680:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
15681:                             push(@{$changes{'helpform'}},'maxsize');
15682:                         }
15683:                     }
15684:                 }
15685:             }
15686:         }
15687:         if (@statuses) {
15688:             if (ref($currsetting{'overrides'}) eq 'HASH') { 
15689:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
15690:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
15691:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
15692:                             foreach my $item (@contacts,'bcc','others','include') {
15693:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) { 
15694:                                     push(@{$changes{'overrides'}},$key);
15695:                                     last;
15696:                                 }
15697:                             }
15698:                         } else {
15699:                             push(@{$changes{'overrides'}},$key);
15700:                         }
15701:                     }
15702:                 }
15703:                 foreach my $key (@overrides) {
15704:                     unless (exists($currsetting{'overrides'}{$key})) {
15705:                         push(@{$changes{'overrides'}},$key);
15706:                     }
15707:                 }
15708:             } else {
15709:                 foreach my $key (@overrides) {
15710:                     push(@{$changes{'overrides'}},$key); 
15711:                 }
15712:             }
15713:         }
15714:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
15715:             foreach my $key ('excluded','weights','threshold','sysmail') {
15716:                 if ($key eq 'excluded') {
15717:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
15718:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
15719:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
15720:                             (@{$currsetting{'lonstatus'}{$key}})) {
15721:                             my @diffs =
15722:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
15723:                                                                    $currsetting{'lonstatus'}{$key});
15724:                             if (@diffs) {
15725:                                 push(@{$changes{'lonstatus'}},$key);
15726:                             }
15727:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
15728:                             push(@{$changes{'lonstatus'}},$key);
15729:                         }
15730:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
15731:                              (@{$currsetting{'lonstatus'}{$key}})) {
15732:                         push(@{$changes{'lonstatus'}},$key);
15733:                     }
15734:                 } elsif ($key eq 'weights') {
15735:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
15736:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
15737:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
15738:                             foreach my $type ('E','W','N','U') {
15739:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
15740:                                         $currsetting{'lonstatus'}{$key}{$type}) {
15741:                                     push(@{$changes{'lonstatus'}},$key);
15742:                                     last;
15743:                                 }
15744:                             }
15745:                         } else {
15746:                             foreach my $type ('E','W','N','U') {
15747:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
15748:                                     push(@{$changes{'lonstatus'}},$key);
15749:                                     last;
15750:                                 }
15751:                             }
15752:                         }
15753:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
15754:                         foreach my $type ('E','W','N','U') {
15755:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
15756:                                 push(@{$changes{'lonstatus'}},$key);
15757:                                 last;
15758:                             }
15759:                         }
15760:                     }
15761:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
15762:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
15763:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
15764:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
15765:                                 push(@{$changes{'lonstatus'}},$key);
15766:                             }
15767:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
15768:                             push(@{$changes{'lonstatus'}},$key);
15769:                         }
15770:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
15771:                         push(@{$changes{'lonstatus'}},$key);
15772:                     }
15773:                 }
15774:             }
15775:         } else {
15776:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
15777:                 foreach my $key ('excluded','weights','threshold','sysmail') {
15778:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
15779:                         push(@{$changes{'lonstatus'}},$key);
15780:                     }
15781:                 }
15782:             }
15783:         }
15784:     } else {
15785:         my %default;
15786:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
15787:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
15788:         $default{'errormail'} = 'adminemail';
15789:         $default{'packagesmail'} = 'adminemail';
15790:         $default{'helpdeskmail'} = 'supportemail';
15791:         $default{'otherdomsmail'} = 'supportemail';
15792:         $default{'lonstatusmail'} = 'adminemail';
15793:         $default{'requestsmail'} = 'adminemail';
15794:         $default{'updatesmail'} = 'adminemail';
15795:         $default{'hostipmail'} = 'adminemail';
15796:         foreach my $item (@contacts) {
15797:            if ($to{$item} ne $default{$item}) {
15798:                $changes{$item} = 1;
15799:            }
15800:         }
15801:         foreach my $type (@mailings) {
15802:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
15803:                 push(@{$changes{$type}},@{$newsetting{$type}});
15804:             }
15805:             if ($others{$type} ne '') {
15806:                 push(@{$changes{$type}},'others');
15807:             }
15808:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15809:                 if ($bcc{$type} ne '') {
15810:                     push(@{$changes{$type}},'bcc');
15811:                 }
15812:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
15813:                     push(@{$changes{$type}},'include');
15814:                 }
15815:             }
15816:         }
15817:         if (ref($fields) eq 'ARRAY') {
15818:             foreach my $field (@{$fields}) {
15819:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
15820:                     push(@{$changes{'helpform'}},$field);
15821:                 }
15822:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
15823:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
15824:                         push(@{$changes{'helpform'}},'maxsize');
15825:                     }
15826:                 }
15827:             }
15828:         }
15829:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
15830:             foreach my $key ('excluded','weights','threshold','sysmail') {
15831:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
15832:                     push(@{$changes{'lonstatus'}},$key);
15833:                 }
15834:             }
15835:         }
15836:     }
15837:     foreach my $item (@toggles) {
15838:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
15839:             $changes{$item} = 1;
15840:         } elsif ((!$env{'form.'.$item}) &&
15841:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
15842:             $changes{$item} = 1;
15843:         }
15844:     }
15845:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
15846:                                              $dom);
15847:     if ($putresult eq 'ok') {
15848:         if (keys(%changes) > 0) {
15849:             &Apache::loncommon::devalidate_domconfig_cache($dom);
15850:             if (ref($lastactref) eq 'HASH') {
15851:                 $lastactref->{'domainconfig'} = 1;
15852:             }
15853:             my ($titles,$short_titles)  = &contact_titles();
15854:             $resulttext = &mt('Changes made:').'<ul>';
15855:             foreach my $item (@contacts) {
15856:                 if ($changes{$item}) {
15857:                     $resulttext .= '<li>'.$titles->{$item}.
15858:                                     &mt(' set to: ').
15859:                                     '<span class="LC_cusr_emph">'.
15860:                                     $to{$item}.'</span></li>';
15861:                 }
15862:             }
15863:             foreach my $type (@mailings) {
15864:                 if (ref($changes{$type}) eq 'ARRAY') {
15865:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15866:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
15867:                     } else {
15868:                         $resulttext .= '<li>'.$titles->{$type}.': ';
15869:                     }
15870:                     my @text;
15871:                     foreach my $item (@{$newsetting{$type}}) {
15872:                         push(@text,$short_titles->{$item});
15873:                     }
15874:                     if ($others{$type} ne '') {
15875:                         push(@text,$others{$type});
15876:                     }
15877:                     if (@text) {
15878:                         $resulttext .= '<span class="LC_cusr_emph">'.
15879:                                        join(', ',@text).'</span>';
15880:                     }
15881:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15882:                         if ($bcc{$type} ne '') {
15883:                             my $bcctext;
15884:                             if (@text) {
15885:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
15886:                             } else {
15887:                                 $bcctext = '(Bcc)';
15888:                             }
15889:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
15890:                         } elsif (!@text) {
15891:                             $resulttext .= &mt('No one');
15892:                         }   
15893:                         if ($includestr{$type} ne '') {
15894:                             if ($includeloc{$type} eq 'b') {
15895:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
15896:                             } elsif ($includeloc{$type} eq 's') {
15897:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
15898:                             }
15899:                         }
15900:                     } elsif (!@text) {
15901:                         $resulttext .= &mt('No recipients');
15902:                     }
15903:                     $resulttext .= '</li>';
15904:                 }
15905:             }
15906:             if (ref($changes{'overrides'}) eq 'ARRAY') {
15907:                 my @deletions;
15908:                 foreach my $type (@{$changes{'overrides'}}) {
15909:                     if ($usertypeshash{$type}) {
15910:                         if (grep(/^\Q$type\E/,@overrides)) {
15911:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
15912:                                                       $usertypeshash{$type}).'<ul><li>';
15913:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
15914:                                 my @text;
15915:                                 foreach my $item (@contacts) {
15916:                                     if ($newsetting{'override_'.$type}{$item}) { 
15917:                                         push(@text,$short_titles->{$item});
15918:                                     }
15919:                                 }
15920:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
15921:                                     push(@text,$newsetting{'override_'.$type}{'others'});
15922:                                 }
15923:   
15924:                                 if (@text) {
15925:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
15926:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
15927:                                 }
15928:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
15929:                                     my $bcctext;
15930:                                     if (@text) {
15931:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
15932:                                     } else {
15933:                                         $bcctext = '(Bcc)';
15934:                                     }
15935:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
15936:                                 } elsif (!@text) {
15937:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
15938:                                 }
15939:                                 $resulttext .= '</li>';
15940:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
15941:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
15942:                                     if ($loc eq 'b') {
15943:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
15944:                                     } elsif ($loc eq 's') {
15945:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
15946:                                     }
15947:                                 }
15948:                             }
15949:                             $resulttext .= '</li></ul></li>';
15950:                         } else {
15951:                             push(@deletions,$usertypeshash{$type});
15952:                         }
15953:                     }
15954:                 }
15955:                 if (@deletions) {
15956:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
15957:                                               join(', ',@deletions)).'</li>';
15958:                 }
15959:             }
15960:             my @offon = ('off','on');
15961:             my $corelink = &core_link_msu();
15962:             if ($changes{'reporterrors'}) {
15963:                 $resulttext .= '<li>'.
15964:                                &mt('E-mail error reports to [_1] set to "'.
15965:                                    $offon[$env{'form.reporterrors'}].'".',
15966:                                    $corelink).
15967:                                '</li>';
15968:             }
15969:             if ($changes{'reportupdates'}) {
15970:                 $resulttext .= '<li>'.
15971:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
15972:                                     $offon[$env{'form.reportupdates'}].'".',
15973:                                     $corelink).
15974:                                 '</li>';
15975:             }
15976:             if ($changes{'reportstatus'}) {
15977:                 $resulttext .= '<li>'.
15978:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
15979:                                     $offon[$env{'form.reportstatus'}].'".',
15980:                                     $corelink).
15981:                                 '</li>';
15982:             }
15983:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
15984:                 $resulttext .= '<li>'.
15985:                                &mt('Nightly status check e-mail settings').':<ul>';
15986:                 my (%defval,%use_def,%shown);
15987:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
15988:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
15989:                 $defval{'weights'} =
15990:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
15991:                 $defval{'excluded'} = &mt('None');
15992:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
15993:                     foreach my $item ('threshold','sysmail','weights','excluded') {
15994:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
15995:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
15996:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
15997:                             } elsif ($item eq 'weights') {
15998:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
15999:                                     foreach my $type ('E','W','N','U') {
16000:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
16001:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
16002:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
16003:                                         } else {
16004:                                             $shown{$item} .= $lonstatus_defs->{$type};
16005:                                         }
16006:                                         $shown{$item} .= ', ';
16007:                                     }
16008:                                     $shown{$item} =~ s/, $//;
16009:                                 } else {
16010:                                     $shown{$item} = $defval{$item};
16011:                                 }
16012:                             } elsif ($item eq 'excluded') {
16013:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
16014:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
16015:                                 } else {
16016:                                     $shown{$item} = $defval{$item};
16017:                                 }
16018:                             }
16019:                         } else {
16020:                             $shown{$item} = $defval{$item};
16021:                         }
16022:                     }
16023:                 } else {
16024:                     foreach my $item ('threshold','weights','excluded','sysmail') {
16025:                         $shown{$item} = $defval{$item};
16026:                     }
16027:                 }
16028:                 foreach my $item ('threshold','weights','excluded','sysmail') {
16029:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
16030:                                           $shown{$item}).'</li>';
16031:                 }
16032:                 $resulttext .= '</ul></li>';
16033:             }
16034:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
16035:                 my (@optional,@required,@unused,$maxsizechg);
16036:                 foreach my $field (@{$changes{'helpform'}}) {
16037:                     if ($field eq 'maxsize') {
16038:                         $maxsizechg = 1;
16039:                         next;
16040:                     }
16041:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
16042:                         push(@optional,$field);
16043:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
16044:                         push(@unused,$field);
16045:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
16046:                         push(@required,$field);
16047:                     }
16048:                 }
16049:                 if (@optional) {
16050:                     $resulttext .= '<li>'.
16051:                                    &mt('Help form fields changed to "Optional": [_1].',
16052:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
16053:                                    '</li>';
16054:                 }
16055:                 if (@required) {
16056:                     $resulttext .= '<li>'.
16057:                                    &mt('Help form fields changed to "Required": [_1].',
16058:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
16059:                                    '</li>';
16060:                 }
16061:                 if (@unused) {
16062:                     $resulttext .= '<li>'.
16063:                                    &mt('Help form fields changed to "Not shown": [_1].',
16064:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
16065:                                    '</li>';
16066:                 }
16067:                 if ($maxsizechg) {
16068:                     $resulttext .= '<li>'.
16069:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
16070:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
16071:                                    '</li>';
16072:                 }
16073:             }
16074:             $resulttext .= '</ul>';
16075:         } else {
16076:             $resulttext = &mt('No changes made to contacts and form settings');
16077:         }
16078:     } else {
16079:         $resulttext = '<span class="LC_error">'.
16080:             &mt('An error occurred: [_1].',$putresult).'</span>';
16081:     }
16082:     return $resulttext;
16083: }
16084: 
16085: sub modify_privacy {
16086:     my ($dom,%domconfig) = @_;
16087:     my ($resulttext,%current,%changes);
16088:     if (ref($domconfig{'privacy'}) eq 'HASH') {
16089:         %current = %{$domconfig{'privacy'}};
16090:     }
16091:     my @fields = ('lastname','firstname','middlename','generation','permanentemail','id');
16092:     my @items = ('domain','author','course','community');
16093:     my %names = &Apache::lonlocal::texthash (
16094:                    domain => 'Assigned domain role(s)',
16095:                    author => 'Assigned co-author role(s)',
16096:                    course => 'Assigned course role(s)',
16097:                    community => 'Assigned community role',
16098:                 );
16099:     my %roles = &Apache::lonlocal::texthash (
16100:                    domain => 'Domain role',
16101:                    author => 'Co-author role',
16102:                    course => 'Course role',
16103:                    community => 'Community role',
16104:                 );
16105:     my %titles = &Apache::lonlocal::texthash (
16106:                   approval => 'Approval for role in different domain',
16107:                   othdom   => 'User information available in other domain',
16108:                   priv     => 'Information viewable by privileged user in same domain',
16109:                   unpriv   => 'Information viewable by unprivileged user in same domain',
16110:                   instdom  => 'Other domain shares institution/provider',
16111:                   extdom   => 'Other domain has different institution/provider',
16112:                   none     => 'Not allowed',
16113:                   user     => 'User authorizes',
16114:                   domain   => 'Domain Coordinator authorizes',
16115:                   auto     => 'Unrestricted',
16116:     );
16117:     my %fieldnames = &Apache::lonlocal::texthash (
16118:                         id => 'Student/Employee ID',
16119:                         permanentemail => 'E-mail address',
16120:                         lastname => 'Last Name',
16121:                         firstname => 'First Name',
16122:                         middlename => 'Middle Name',
16123:                         generation => 'Generation',
16124:     );
16125:     my ($othertitle,$usertypes,$types) =
16126:         &Apache::loncommon::sorted_inst_types($dom);
16127:     my (%by_ip,%by_location,@intdoms,@instdoms);
16128:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
16129: 
16130:     my %privacyhash = (
16131:                        'approval' => {
16132:                                        instdom => {},
16133:                                        extdom  => {},
16134:                                      },
16135:                        'othdom'   => {},
16136:                        'priv'     => {},
16137:                        'unpriv'   => {},
16138:                       );
16139:     foreach my $item (@items) {
16140:         if (@instdoms > 1) {
16141:             if ($env{'form.privacy_approval_instdom'.$item} =~ /^(none|user|domain|auto)$/) {
16142:                 $privacyhash{'approval'}{'instdom'}{$item} = $env{'form.privacy_approval_instdom_'.$item};
16143:             }
16144:             if (ref($current{'approval'}) eq 'HASH') {
16145:                 if (ref($current{'approval'}{'instdom'}) eq 'HASH') {
16146:                     unless ($privacyhash{'approval'}{'instdom'}{$item} eq $current{'approval'}{'instdom'}{$item}) {
16147:                         $changes{'approval'} = 1;
16148:                     }
16149:                 }
16150:             } elsif ($privacyhash{'approval'}{'instdom'}{$item} ne 'auto') {
16151:                 $changes{'approval'} = 1;
16152:             }
16153:         }
16154:         if (keys(%by_location) > 0) {
16155:             if ($env{'form.privacy_approval_extdom_'.$item} =~ /^(none|user|domain|auto)$/) {
16156:                 $privacyhash{'approval'}{'extdom'}{$item} = $env{'form.privacy_approval_extdom_'.$item};
16157:             }
16158:             if (ref($current{'approval'}) eq 'HASH') {
16159:                 if (ref($current{'approval'}{'extdom'}) eq 'HASH') {
16160:                     unless ($privacyhash{'approval'}{'extdom'}{$item} eq $current{'approval'}{'extdom'}{$item}) {
16161:                         $changes{'approval'} = 1;
16162:                     }
16163:                 }
16164:             } elsif ($privacyhash{'approval'}{'extdom'}{$item} ne 'auto') {
16165:                 $changes{'approval'} = 1;
16166:             }
16167:         }
16168:         foreach my $status ('priv','unpriv') {
16169:             my @possibles = sort(&Apache::loncommon::get_env_multiple('form.privacy_'.$status.'_'.$item));
16170:             my @newvalues;
16171:             foreach my $field (@possibles) {
16172:                 if (grep(/^\Q$field\E$/,@fields)) {
16173:                     $privacyhash{$status}{$item}{$field} = 1;
16174:                     push(@newvalues,$field);
16175:                 }
16176:             }
16177:             @newvalues = sort(@newvalues);
16178:             if (ref($current{$status}) eq 'HASH') {
16179:                 if (ref($current{$status}{$item}) eq 'HASH') {
16180:                     my @currvalues = sort(keys(%{$current{$status}{$item}}));
16181:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
16182:                     if (@diffs > 0) {
16183:                         $changes{$status} = 1;
16184:                     }
16185:                 }
16186:             } else {
16187:                 my @stdfields;
16188:                 foreach my $field (@fields) {
16189:                     if ($field eq 'id') {
16190:                         next if ($status eq 'unpriv');
16191:                         next if (($status eq 'priv') && ($item eq 'community'));
16192:                     }
16193:                     push(@stdfields,$field);
16194:                 }
16195:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
16196:                 if (@diffs > 0) {
16197:                     $changes{$status} = 1;
16198:                 }
16199:             }
16200:         }
16201:     }
16202:     if ((@instdoms > 1) || (keys(%by_location) > 0)) {
16203:         my @statuses;
16204:         if (ref($types) eq 'ARRAY') {
16205:             @statuses = @{$types};
16206:         }
16207:         foreach my $type (@statuses,'default') {
16208:             my @possfields = &Apache::loncommon::get_env_multiple('form.privacy_othdom_'.$type);
16209:             my @newvalues;
16210:             foreach my $field (sort(@possfields)) {
16211:                 if (grep(/^\Q$field\E$/,@fields)) {
16212:                     $privacyhash{'othdom'}{$type}{$field} = 1;
16213:                     push(@newvalues,$field);
16214:                 }
16215:             }
16216:             @newvalues = sort(@newvalues);
16217:             if (ref($current{'othdom'}) eq 'HASH') {
16218:                 if (ref($current{'othdom'}{$type}) eq 'HASH') {
16219:                     my @currvalues = sort(keys(%{$current{'othdom'}{$type}}));
16220:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
16221:                     if (@diffs > 0) {
16222:                         $changes{'othdom'} = 1;
16223:                     }
16224:                 }
16225:             } else {
16226:                 my @stdfields = ('lastname','firstname','middlename','generation','permanentemail');
16227:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
16228:                 if (@diffs > 0) {
16229:                     $changes{'othdom'} = 1;
16230:                 }
16231:             }
16232:         }
16233:     }
16234:     my %confighash = (
16235:                         privacy => \%privacyhash,
16236:                      );
16237:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
16238:     if ($putresult eq 'ok') {
16239:         if (keys(%changes) > 0) {
16240:             $resulttext = &mt('Changes made: ').'<ul>';
16241:             foreach my $key ('approval','othdom','priv','unpriv') {
16242:                 if ($changes{$key}) {
16243:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
16244:                     if ($key eq 'approval') {
16245:                         if (keys(%{$privacyhash{$key}{instdom}})) {
16246:                             $resulttext .= '<li>'.$titles{'instdom'}.'<ul>';
16247:                             foreach my $item (@items) {
16248:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{instdom}{$item}}.'</li>';
16249:                             }
16250:                             $resulttext .= '</ul></li>';
16251:                         }
16252:                         if (keys(%{$privacyhash{$key}{extdom}})) {
16253:                             $resulttext .= '<li>'.$titles{'extdom'}.'<ul>';
16254:                             foreach my $item (@items) {
16255:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{extdom}{$item}}.'</li>';
16256:                             }
16257:                             $resulttext .= '</ul></li>';
16258:                         }
16259:                     } elsif ($key eq 'othdom') {
16260:                         my @statuses;
16261:                         if (ref($types) eq 'ARRAY') {
16262:                             @statuses = @{$types};
16263:                         }
16264:                         if (ref($privacyhash{$key}) eq 'HASH') {
16265:                             foreach my $status (@statuses,'default') {
16266:                                 if ($status eq 'default') {
16267:                                     $resulttext .= '<li>'.$othertitle.': ';
16268:                                 } elsif (ref($usertypes) eq 'HASH') {
16269:                                     $resulttext .= '<li>'.$usertypes->{$status}.': ';
16270:                                 } else {
16271:                                     next;
16272:                                 }
16273:                                 if (ref($privacyhash{$key}{$status}) eq 'HASH') {
16274:                                     if (keys(%{$privacyhash{$key}{$status}})) {
16275:                                         $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$status}}))));
16276:                                     } else {
16277:                                         $resulttext .= &mt('none');
16278:                                     }
16279:                                 }
16280:                                 $resulttext .= '</li>';
16281:                             }
16282:                         }
16283:                     } else {
16284:                         foreach my $item (@items) {
16285:                             if (ref($privacyhash{$key}{$item}) eq 'HASH') {
16286:                                 $resulttext .= '<li>'.$names{$item}.': ';
16287:                                 if (keys(%{$privacyhash{$key}{$item}})) {
16288:                                     $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$item}}))));
16289:                                 } else {
16290:                                     $resulttext .= &mt('none');
16291:                                 }
16292:                                 $resulttext .= '</li>';
16293:                             }
16294:                         }
16295:                     }
16296:                     $resulttext .= '</ul></li>';
16297:                 }
16298:             }
16299:         } else {
16300:             $resulttext = &mt('No changes made to user information settings');
16301:         }
16302:     } else {
16303:         $resulttext = '<span class="LC_error">'.
16304:             &mt('An error occurred: [_1]',$putresult).'</span>';
16305:     }
16306:     return $resulttext;
16307: }
16308: 
16309: sub modify_passwords {
16310:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
16311:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
16312:         $updatedefaults,$updateconf);
16313:     my $customfn = 'resetpw.html';
16314:     if (ref($domconfig{'passwords'}) eq 'HASH') {
16315:         %current = %{$domconfig{'passwords'}};
16316:     }
16317:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16318:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16319:     if (ref($types) eq 'ARRAY') {
16320:         @oktypes = @{$types};
16321:     }
16322:     push(@oktypes,'default');
16323: 
16324:     my %titles = &Apache::lonlocal::texthash (
16325:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
16326:         intauth_check  => 'Check bcrypt cost if authenticated',
16327:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
16328:         permanent      => 'Permanent e-mail address',
16329:         critical       => 'Critical notification address',
16330:         notify         => 'Notification address',
16331:         min            => 'Minimum password length',
16332:         max            => 'Maximum password length',
16333:         chars          => 'Required characters',
16334:         expire         => 'Password expiration (days)',
16335:         numsaved       => 'Number of previous passwords to save',
16336:         reset          => 'Resetting Forgotten Password',
16337:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
16338:         rules          => 'Rules for LON-CAPA Passwords',
16339:         crsownerchg    => 'Course Owner Changing Student Passwords',
16340:         username       => 'Username',
16341:         email          => 'E-mail address',
16342:     );
16343: 
16344: #
16345: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
16346: #
16347:     my (%curr_defaults,%save_defaults);
16348:     if (ref($domconfig{'defaults'}) eq 'HASH') {
16349:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
16350:             if ($key =~ /^intauth_(cost|check|switch)$/) {
16351:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
16352:             } else {
16353:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
16354:             }
16355:         }
16356:     }
16357:     my %staticdefaults = (
16358:         'resetlink'      => 2,
16359:         'resetcase'      => \@oktypes,
16360:         'resetprelink'   => 'both',
16361:         'resetemail'     => ['critical','notify','permanent'],
16362:         'intauth_cost'   => 10,
16363:         'intauth_check'  => 0,
16364:         'intauth_switch' => 0,
16365:     );
16366:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
16367:     foreach my $type (@oktypes) {
16368:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
16369:     }
16370:     my $linklife = $env{'form.passwords_link'};
16371:     $linklife =~ s/^\s+|\s+$//g;
16372:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
16373:         $newvalues{'resetlink'} = $linklife;
16374:         if ($current{'resetlink'}) {
16375:             if ($current{'resetlink'} ne $linklife) {
16376:                 $changes{'reset'} = 1;
16377:             }
16378:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
16379:             if ($staticdefaults{'resetlink'} ne $linklife) {
16380:                 $changes{'reset'} = 1;
16381:             }
16382:         }
16383:     } elsif ($current{'resetlink'}) {
16384:         $changes{'reset'} = 1;
16385:     }
16386:     my @casesens;
16387:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
16388:     foreach my $case (sort(@posscase)) {
16389:         if (grep(/^\Q$case\E$/,@oktypes)) {
16390:             push(@casesens,$case);
16391:         }
16392:     }
16393:     $newvalues{'resetcase'} = \@casesens;
16394:     if (ref($current{'resetcase'}) eq 'ARRAY') {
16395:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
16396:         if (@diffs > 0) {
16397:             $changes{'reset'} = 1;
16398:         }
16399:     } elsif (!ref($domconfig{passwords}) eq 'HASH') {
16400:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
16401:         if (@diffs > 0) {
16402:             $changes{'reset'} = 1;
16403:         }
16404:     }
16405:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
16406:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
16407:         if (exists($current{'resetprelink'})) {
16408:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
16409:                 $changes{'reset'} = 1;
16410:             }
16411:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
16412:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
16413:                 $changes{'reset'} = 1;
16414:             }
16415:         }
16416:     } elsif ($current{'resetprelink'}) {
16417:         $changes{'reset'} = 1;
16418:     }
16419:     foreach my $type (@oktypes) {
16420:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
16421:         my @postlink;
16422:         foreach my $item (sort(@possplink)) {
16423:             if ($item =~ /^(email|username)$/) {
16424:                 push(@postlink,$item);
16425:             }
16426:         }
16427:         $newvalues{'resetpostlink'}{$type} = \@postlink;
16428:         unless ($changes{'reset'}) {
16429:             if (ref($current{'resetpostlink'}) eq 'HASH') {
16430:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
16431:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
16432:                     if (@diffs > 0) {
16433:                         $changes{'reset'} = 1;
16434:                     }
16435:                 } else {
16436:                     $changes{'reset'} = 1;
16437:                 }
16438:             } elsif (!ref($domconfig{passwords}) eq 'HASH') {
16439:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
16440:                 if (@diffs > 0) {
16441:                     $changes{'reset'} = 1;
16442:                 }
16443:             }
16444:         }
16445:     }
16446:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
16447:     my @resetemail;
16448:     foreach my $item (sort(@possemailsrc)) {
16449:         if ($item =~ /^(permanent|critical|notify)$/) {
16450:             push(@resetemail,$item);
16451:         }
16452:     }
16453:     $newvalues{'resetemail'} = \@resetemail;
16454:     unless ($changes{'reset'}) {
16455:         if (ref($current{'resetemail'}) eq 'ARRAY') {
16456:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
16457:             if (@diffs > 0) {
16458:                 $changes{'reset'} = 1;
16459:             }
16460:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
16461:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
16462:             if (@diffs > 0) {
16463:                 $changes{'reset'} = 1;
16464:             }
16465:         }
16466:     }
16467:     if ($env{'form.passwords_stdtext'} == 0) {
16468:         $newvalues{'resetremove'} = 1;
16469:         unless ($current{'resetremove'}) {
16470:             $changes{'reset'} = 1;
16471:         }
16472:     } elsif ($current{'resetremove'}) {
16473:         $changes{'reset'} = 1;
16474:     }
16475:     if ($env{'form.passwords_customfile.filename'} ne '') {
16476:         my $servadm = $r->dir_config('lonAdmEMail');
16477:         my ($configuserok,$author_ok,$switchserver) =
16478:             &config_check($dom,$confname,$servadm);
16479:         my $error;
16480:         if ($configuserok eq 'ok') {
16481:             if ($switchserver) {
16482:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
16483:             } else {
16484:                 if ($author_ok eq 'ok') {
16485:                     my ($result,$customurl) =
16486:                         &publishlogo($r,'upload','passwords_customfile',$dom,
16487:                                      $confname,'customtext/resetpw','','',$customfn);
16488:                     if ($result eq 'ok') {
16489:                         $newvalues{'resetcustom'} = $customurl;
16490:                         $changes{'reset'} = 1;
16491:                     } else {
16492:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
16493:                     }
16494:                 } else {
16495:                     $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);
16496:                 }
16497:             }
16498:         } else {
16499:             $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);
16500:         }
16501:         if ($error) {
16502:             &Apache::lonnet::logthis($error);
16503:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
16504:         }
16505:     } elsif ($current{'resetcustom'}) {
16506:         if ($env{'form.passwords_custom_del'}) {
16507:             $changes{'reset'} = 1;
16508:         } else {
16509:             $newvalues{'resetcustom'} = $current{'resetcustom'};
16510:         }
16511:     }
16512:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
16513:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
16514:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
16515:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
16516:             $changes{'intauth'} = 1;
16517:         }
16518:     } else {
16519:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
16520:     }
16521:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
16522:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
16523:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
16524:             $changes{'intauth'} = 1;
16525:         }
16526:     } else {
16527:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
16528:     }
16529:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
16530:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
16531:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
16532:             $changes{'intauth'} = 1;
16533:         }
16534:     } else {
16535:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
16536:     }
16537:     foreach my $item ('cost','check','switch') {
16538:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
16539:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
16540:             $updatedefaults = 1;
16541:         }
16542:     }
16543:     foreach my $rule ('min','max','expire','numsaved') {
16544:         $env{'form.passwords_'.$rule} =~ s/^\s+|\s+$//g;
16545:         my $ruleok;
16546:         if ($rule eq 'expire') {
16547:             if (($env{'form.passwords_'.$rule} =~ /^\d+(|\.\d*)$/) &&
16548:                 ($env{'form.passwords_'.$rule} ne '0')) {
16549:                 $ruleok = 1;
16550:             }
16551:         } elsif ($rule eq 'min') {
16552:             if ($env{'form.passwords_'.$rule} =~ /^\d+$/) {
16553:                 if ($env{'form.passwords_'.$rule} >= $Apache::lonnet::passwdmin) {
16554:                     $ruleok = 1;
16555:                 }
16556:             }
16557:         } elsif (($env{'form.passwords_'.$rule} =~ /^\d+$/) &&
16558:                  ($env{'form.passwords_'.$rule} ne '0')) {
16559:             $ruleok = 1;
16560:         }
16561:         if ($ruleok) {
16562:             $newvalues{$rule} = $env{'form.passwords_'.$rule};
16563:             if (exists($current{$rule})) {
16564:                 if ($newvalues{$rule} ne $current{$rule}) {
16565:                     $changes{'rules'} = 1;
16566:                 }
16567:             } elsif ($rule eq 'min') {
16568:                 if ($staticdefaults{$rule} ne $newvalues{$rule}) {
16569:                     $changes{'rules'} = 1;
16570:                 }
16571:             } else {
16572:                 $changes{'rules'} = 1;
16573:             }
16574:         } elsif (exists($current{$rule})) {
16575:             $changes{'rules'} = 1;
16576:         }
16577:     }
16578:     my @posschars = &Apache::loncommon::get_env_multiple('form.passwords_chars');
16579:     my @chars;
16580:     foreach my $item (sort(@posschars)) {
16581:         if ($item =~ /^(uc|lc|num|spec)$/) {
16582:             push(@chars,$item);
16583:         }
16584:     }
16585:     $newvalues{'chars'} = \@chars;
16586:     unless ($changes{'rules'}) {
16587:         if (ref($current{'chars'}) eq 'ARRAY') {
16588:             my @diffs = &Apache::loncommon::compare_arrays($current{'chars'},\@chars);
16589:             if (@diffs > 0) {
16590:                 $changes{'rules'} = 1;
16591:             }
16592:         } else {
16593:             if (@chars > 0) {
16594:                 $changes{'rules'} = 1;
16595:             }
16596:         }
16597:     }
16598:     my %crsownerchg = (
16599:                         by => [],
16600:                         for => [],
16601:                       );
16602:     foreach my $item ('by','for') {
16603:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
16604:         foreach my $type (sort(@posstypes)) {
16605:             if (grep(/^\Q$type\E$/,@oktypes)) {
16606:                 push(@{$crsownerchg{$item}},$type);
16607:             }
16608:         }
16609:     }
16610:     $newvalues{'crsownerchg'} = \%crsownerchg;
16611:     if (ref($current{'crsownerchg'}) eq 'HASH') {
16612:         foreach my $item ('by','for') {
16613:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
16614:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
16615:                 if (@diffs > 0) {
16616:                     $changes{'crsownerchg'} = 1;
16617:                     last;
16618:                 }
16619:             }
16620:         }
16621:     } elsif (!(ref($domconfig{passwords}) eq 'HASH')) {
16622:         foreach my $item ('by','for') {
16623:             if (@{$crsownerchg{$item}} > 0) {
16624:                 $changes{'crsownerchg'} = 1;
16625:                 last;
16626:             }
16627:         }
16628:     }
16629: 
16630:     my %confighash = (
16631:                         defaults  => \%save_defaults,
16632:                         passwords => \%newvalues,
16633:                      );
16634:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
16635: 
16636:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
16637:     if ($putresult eq 'ok') {
16638:         if (keys(%changes) > 0) {
16639:             $resulttext = &mt('Changes made: ').'<ul>';
16640:             foreach my $key ('reset','intauth','rules','crsownerchg') {
16641:                 if ($changes{$key}) {
16642:                     unless ($key eq 'intauth') {
16643:                         $updateconf = 1;
16644:                     }
16645:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
16646:                     if ($key eq 'reset') {
16647:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
16648:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
16649:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
16650:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
16651:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />';
16652:                             if (ref($confighash{'passwords'}{'recaptchakeys'}) eq 'HASH') {
16653:                                 $resulttext .= &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'public'}).'</br>'.
16654:                                                &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'private'}).'</li>';
16655:                             }
16656:                         } else {
16657:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
16658:                         }
16659:                         if ($confighash{'passwords'}{'resetlink'}) {
16660:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
16661:                         } else {
16662:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
16663:                                                   &mt('Will default to 2 hours').'</li>';
16664:                         }
16665:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
16666:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
16667:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
16668:                             } else {
16669:                                 my $casesens;
16670:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
16671:                                     if ($type eq 'default') {
16672:                                         $casesens .= $othertitle.', ';
16673:                                     } elsif ($usertypes->{$type} ne '') {
16674:                                         $casesens .= $usertypes->{$type}.', ';
16675:                                     }
16676:                                 }
16677:                                 $casesens =~ s/\Q, \E$//;
16678:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
16679:                             }
16680:                         } else {
16681:                             $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>';
16682:                         }
16683:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
16684:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
16685:                         } else {
16686:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
16687:                         }
16688:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
16689:                             my $output;
16690:                             if (ref($types) eq 'ARRAY') {
16691:                                 foreach my $type (@{$types}) {
16692:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
16693:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
16694:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
16695:                                         } else {
16696:                                             $output .= $usertypes->{$type}.' -- '.
16697:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
16698:                                         }
16699:                                     }
16700:                                 }
16701:                             }
16702:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
16703:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
16704:                                     $output .= $othertitle.' -- '.&mt('none');
16705:                                 } else {
16706:                                     $output .= $othertitle.' -- '.
16707:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
16708:                                 }
16709:                             }
16710:                             if ($output) {
16711:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
16712:                             } else {
16713:                                 $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>';
16714:                             }
16715:                         } else {
16716:                             $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>';
16717:                         }
16718:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
16719:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
16720:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
16721:                             } else {
16722:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
16723:                             }
16724:                         } else {
16725:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
16726:                         }
16727:                         if ($confighash{'passwords'}{'resetremove'}) {
16728:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
16729:                         } else {
16730:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
16731:                         }
16732:                         if ($confighash{'passwords'}{'resetcustom'}) {
16733:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
16734:                                                                             &mt('custom text'),600,500,undef,undef,
16735:                                                                             undef,undef,'background-color:#ffffff');
16736:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes: [_1]',$customlink).'</li>';
16737:                         } else {
16738:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
16739:                         }
16740:                     } elsif ($key eq 'intauth') {
16741:                         foreach my $item ('cost','switch','check') {
16742:                             my $value = $save_defaults{$key.'_'.$item};
16743:                             if ($item eq 'switch') {
16744:                                 my %optiondesc = &Apache::lonlocal::texthash (
16745:                                                      0 => 'No',
16746:                                                      1 => 'Yes',
16747:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
16748:                                                  );
16749:                                 if ($value =~ /^(0|1|2)$/) {
16750:                                     $value = $optiondesc{$value};
16751:                                 } else {
16752:                                     $value = &mt('none -- defaults to No');
16753:                                 }
16754:                             } elsif ($item eq 'check') {
16755:                                 my %optiondesc = &Apache::lonlocal::texthash (
16756:                                                      0 => 'No',
16757:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
16758:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
16759:                                                  );
16760:                                 if ($value =~ /^(0|1|2)$/) {
16761:                                     $value = $optiondesc{$value};
16762:                                 } else {
16763:                                     $value = &mt('none -- defaults to No');
16764:                                 }
16765:                             }
16766:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
16767:                         }
16768:                     } elsif ($key eq 'rules') {
16769:                         foreach my $rule ('min','max','expire','numsaved') {
16770:                             if ($confighash{'passwords'}{$rule} eq '') {
16771:                                 if ($rule eq 'min') {
16772:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
16773:                                                    ' '.&mt('Default of [_1] will be used',
16774:                                                            $Apache::lonnet::passwdmin).'</li>';
16775:                                 } else {
16776:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
16777:                                 }
16778:                             } else {
16779:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
16780:                             }
16781:                         }
16782:                         if (ref($confighash{'passwords'}{'chars'}) eq 'ARRAY') {
16783:                             if (@{$confighash{'passwords'}{'chars'}} > 0) {
16784:                                 my %rulenames = &Apache::lonlocal::texthash(
16785:                                                      uc => 'At least one upper case letter',
16786:                                                      lc => 'At least one lower case letter',
16787:                                                      num => 'At least one number',
16788:                                                      spec => 'At least one non-alphanumeric',
16789:                                                    );
16790:                                 my $needed = '<ul><li>'.
16791:                                              join('</li><li>',map {$rulenames{$_} } @{$confighash{'passwords'}{'chars'}}).
16792:                                              '</li></ul>'; 
16793:                                 $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
16794:                             } else {
16795:                                 $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
16796:                             }
16797:                         } else {
16798:                             $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
16799:                         }
16800:                     } elsif ($key eq 'crsownerchg') {
16801:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
16802:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
16803:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
16804:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
16805:                             } else {
16806:                                 my %crsownerstr;
16807:                                 foreach my $item ('by','for') {
16808:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
16809:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
16810:                                             if ($type eq 'default') {
16811:                                                 $crsownerstr{$item} .= $othertitle.', ';
16812:                                             } elsif ($usertypes->{$type} ne '') {
16813:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
16814:                                             }
16815:                                         }
16816:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
16817:                                     }
16818:                                 }
16819:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
16820:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
16821:                             }
16822:                         } else {
16823:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
16824:                         }
16825:                     }
16826:                     $resulttext .= '</ul></li>';
16827:                 }
16828:             }
16829:             $resulttext .= '</ul>';
16830:         } else {
16831:             $resulttext = &mt('No changes made to password settings');
16832:         }
16833:         my $cachetime = 24*60*60;
16834:         if ($updatedefaults) {
16835:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16836:             if (ref($lastactref) eq 'HASH') {
16837:                 $lastactref->{'domdefaults'} = 1;
16838:             }
16839:         }
16840:         if ($updateconf) {
16841:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
16842:             if (ref($lastactref) eq 'HASH') {
16843:                 $lastactref->{'passwdconf'} = 1;
16844:             }
16845:         }
16846:     } else {
16847:         $resulttext = '<span class="LC_error">'.
16848:             &mt('An error occurred: [_1]',$putresult).'</span>';
16849:     }
16850:     if ($errors) {
16851:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
16852:                        $errors.'</ul></p>';
16853:     }
16854:     return $resulttext;
16855: }
16856: 
16857: sub modify_usercreation {
16858:     my ($dom,%domconfig) = @_;
16859:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
16860:     my $warningmsg;
16861:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
16862:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
16863:             if ($key eq 'cancreate') {
16864:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
16865:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
16866:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
16867:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
16868:                         } else {
16869:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
16870:                         }
16871:                     }
16872:                 }
16873:             } elsif ($key eq 'email_rule') {
16874:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
16875:             } else {
16876:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
16877:             }
16878:         }
16879:     }
16880:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
16881:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
16882:     my @contexts = ('author','course','requestcrs');
16883:     foreach my $item(@contexts) {
16884:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
16885:     }
16886:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
16887:         foreach my $item (@contexts) {
16888:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
16889:                 push(@{$changes{'cancreate'}},$item);
16890:             }
16891:         }
16892:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
16893:         foreach my $item (@contexts) {
16894:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
16895:                 if ($cancreate{$item} ne 'any') {
16896:                     push(@{$changes{'cancreate'}},$item);
16897:                 }
16898:             } else {
16899:                 if ($cancreate{$item} ne 'none') {
16900:                     push(@{$changes{'cancreate'}},$item);
16901:                 }
16902:             }
16903:         }
16904:     } else {
16905:         foreach my $item (@contexts)  {
16906:             push(@{$changes{'cancreate'}},$item);
16907:         }
16908:     }
16909: 
16910:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
16911:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
16912:             if (!grep(/^\Q$type\E$/,@username_rule)) {
16913:                 push(@{$changes{'username_rule'}},$type);
16914:             }
16915:         }
16916:         foreach my $type (@username_rule) {
16917:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
16918:                 push(@{$changes{'username_rule'}},$type);
16919:             }
16920:         }
16921:     } else {
16922:         push(@{$changes{'username_rule'}},@username_rule);
16923:     }
16924: 
16925:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
16926:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
16927:             if (!grep(/^\Q$type\E$/,@id_rule)) {
16928:                 push(@{$changes{'id_rule'}},$type);
16929:             }
16930:         }
16931:         foreach my $type (@id_rule) {
16932:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
16933:                 push(@{$changes{'id_rule'}},$type);
16934:             }
16935:         }
16936:     } else {
16937:         push(@{$changes{'id_rule'}},@id_rule);
16938:     }
16939: 
16940:     my @authen_contexts = ('author','course','domain');
16941:     my @authtypes = ('int','krb4','krb5','loc','lti');
16942:     my %authhash;
16943:     foreach my $item (@authen_contexts) {
16944:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
16945:         foreach my $auth (@authtypes) {
16946:             if (grep(/^\Q$auth\E$/,@authallowed)) {
16947:                 $authhash{$item}{$auth} = 1;
16948:             } else {
16949:                 $authhash{$item}{$auth} = 0;
16950:             }
16951:         }
16952:     }
16953:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
16954:         foreach my $item (@authen_contexts) {
16955:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
16956:                 foreach my $auth (@authtypes) {
16957:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
16958:                         push(@{$changes{'authtypes'}},$item);
16959:                         last;
16960:                     }
16961:                 }
16962:             }
16963:         }
16964:     } else {
16965:         foreach my $item (@authen_contexts) {
16966:             push(@{$changes{'authtypes'}},$item);
16967:         }
16968:     }
16969: 
16970:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
16971:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
16972:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
16973:     $save_usercreate{'id_rule'} = \@id_rule;
16974:     $save_usercreate{'username_rule'} = \@username_rule,
16975:     $save_usercreate{'authtypes'} = \%authhash;
16976: 
16977:     my %usercreation_hash =  (
16978:         usercreation     => \%save_usercreate,
16979:     );
16980: 
16981:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
16982:                                              $dom);
16983: 
16984:     if ($putresult eq 'ok') {
16985:         if (keys(%changes) > 0) {
16986:             $resulttext = &mt('Changes made:').'<ul>';
16987:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
16988:                 my %lt = &usercreation_types();
16989:                 foreach my $type (@{$changes{'cancreate'}}) {
16990:                     my $chgtext = $lt{$type}.', ';
16991:                     if ($cancreate{$type} eq 'none') {
16992:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
16993:                     } elsif ($cancreate{$type} eq 'any') {
16994:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
16995:                     } elsif ($cancreate{$type} eq 'official') {
16996:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
16997:                     } elsif ($cancreate{$type} eq 'unofficial') {
16998:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
16999:                     }
17000:                     $resulttext .= '<li>'.$chgtext.'</li>';
17001:                 }
17002:             }
17003:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
17004:                 my ($rules,$ruleorder) = 
17005:                     &Apache::lonnet::inst_userrules($dom,'username');
17006:                 my $chgtext = '<ul>';
17007:                 foreach my $type (@username_rule) {
17008:                     if (ref($rules->{$type}) eq 'HASH') {
17009:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
17010:                     }
17011:                 }
17012:                 $chgtext .= '</ul>';
17013:                 if (@username_rule > 0) {
17014:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
17015:                 } else {
17016:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
17017:                 }
17018:             }
17019:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
17020:                 my ($idrules,$idruleorder) = 
17021:                     &Apache::lonnet::inst_userrules($dom,'id');
17022:                 my $chgtext = '<ul>';
17023:                 foreach my $type (@id_rule) {
17024:                     if (ref($idrules->{$type}) eq 'HASH') {
17025:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
17026:                     }
17027:                 }
17028:                 $chgtext .= '</ul>';
17029:                 if (@id_rule > 0) {
17030:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
17031:                 } else {
17032:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
17033:                 }
17034:             }
17035:             my %authname = &authtype_names();
17036:             my %context_title = &context_names();
17037:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
17038:                 my $chgtext = '<ul>';
17039:                 foreach my $type (@{$changes{'authtypes'}}) {
17040:                     my @allowed;
17041:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
17042:                     foreach my $auth (@authtypes) {
17043:                         if ($authhash{$type}{$auth}) {
17044:                             push(@allowed,$authname{$auth});
17045:                         }
17046:                     }
17047:                     if (@allowed > 0) {
17048:                         $chgtext .= join(', ',@allowed).'</li>';
17049:                     } else {
17050:                         $chgtext .= &mt('none').'</li>';
17051:                     }
17052:                 }
17053:                 $chgtext .= '</ul>';
17054:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
17055:                 $resulttext .= '</li>';
17056:             }
17057:             $resulttext .= '</ul>';
17058:         } else {
17059:             $resulttext = &mt('No changes made to user creation settings');
17060:         }
17061:     } else {
17062:         $resulttext = '<span class="LC_error">'.
17063:             &mt('An error occurred: [_1]',$putresult).'</span>';
17064:     }
17065:     if ($warningmsg ne '') {
17066:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
17067:     }
17068:     return $resulttext;
17069: }
17070: 
17071: sub modify_selfcreation {
17072:     my ($dom,$lastactref,%domconfig) = @_;
17073:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
17074:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
17075:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17076:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
17077:     if (ref($typesref) eq 'ARRAY') {
17078:         @types = @{$typesref};
17079:     }
17080:     if (ref($usertypesref) eq 'HASH') {
17081:         %usertypes = %{$usertypesref};
17082:     }
17083:     $usertypes{'default'} = $othertitle;
17084: #
17085: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
17086: #
17087:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
17088:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
17089:             if ($key eq 'cancreate') {
17090:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
17091:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
17092:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
17093:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
17094:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
17095:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
17096:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
17097:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
17098:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
17099:                         } else {
17100:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
17101:                         }
17102:                     }
17103:                 }
17104:             } elsif ($key eq 'email_rule') {
17105:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
17106:             } else {
17107:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
17108:             }
17109:         }
17110:     }
17111: #
17112: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
17113: #
17114:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
17115:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
17116:             if ($key eq 'selfcreate') {
17117:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
17118:             } else {
17119:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
17120:             }
17121:         }
17122:     }
17123: #
17124: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
17125: #
17126:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
17127:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
17128:             if ($key eq 'inststatusguest') {
17129:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
17130:             } else {
17131:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
17132:             }
17133:         }
17134:     }
17135: 
17136:     my @contexts = ('selfcreate');
17137:     @{$cancreate{'selfcreate'}} = ();
17138:     %{$cancreate{'emailusername'}} = ();
17139:     if (@types) {
17140:         @{$cancreate{'statustocreate'}} = ();
17141:     }
17142:     %{$cancreate{'selfcreateprocessing'}} = ();
17143:     %{$cancreate{'shibenv'}} = ();
17144:     %{$cancreate{'emailverified'}} = ();
17145:     %{$cancreate{'emailoptions'}} = ();
17146:     %{$cancreate{'emaildomain'}} = ();
17147:     my %selfcreatetypes = (
17148:                              sso   => 'users authenticated by institutional single sign on',
17149:                              login => 'users authenticated by institutional log-in',
17150:                              email => 'users verified by e-mail',
17151:                           );
17152: #
17153: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
17154: # is permitted.
17155: #
17156:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
17157: 
17158:     my (@statuses,%email_rule);
17159:     foreach my $item ('login','sso','email') {
17160:         if ($item eq 'email') {
17161:             if ($env{'form.cancreate_email'}) {
17162:                 if (@types) {
17163:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
17164:                     foreach my $status (@poss_statuses) {
17165:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
17166:                             push(@statuses,$status);
17167:                         }
17168:                     }
17169:                     $save_inststatus{'inststatusguest'} = \@statuses;
17170:                 } else {
17171:                     push(@statuses,'default');
17172:                 }
17173:                 if (@statuses) {
17174:                     my %curr_rule;
17175:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
17176:                         foreach my $type (@statuses) {
17177:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
17178:                         }
17179:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
17180:                         foreach my $type (@statuses) {
17181:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
17182:                         }
17183:                     }
17184:                     push(@{$cancreate{'selfcreate'}},'email');
17185:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
17186:                     my %curremaildom;
17187:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
17188:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
17189:                     }
17190:                     foreach my $type (@statuses) {
17191:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
17192:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
17193:                         }
17194:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
17195:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
17196:                         }
17197:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
17198: #
17199: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
17200: #
17201:                             my $chosen = $1;
17202:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
17203:                                 my $emaildom;
17204:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
17205:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type}; 
17206:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
17207:                                     if (ref($curremaildom{$type}) eq 'HASH') {
17208:                                         if (exists($curremaildom{$type}{$chosen})) {
17209:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
17210:                                                 push(@{$changes{'cancreate'}},'emaildomain');
17211:                                             }
17212:                                         } elsif ($emaildom ne '') {
17213:                                             push(@{$changes{'cancreate'}},'emaildomain');
17214:                                         }
17215:                                     } elsif ($emaildom ne '') {
17216:                                         push(@{$changes{'cancreate'}},'emaildomain');
17217:                                     } 
17218:                                 }
17219:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
17220:                             } elsif ($chosen eq 'custom') {
17221:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
17222:                                 $email_rule{$type} = [];
17223:                                 if (ref($emailrules) eq 'HASH') {
17224:                                     foreach my $rule (@possemail_rules) {
17225:                                         if (exists($emailrules->{$rule})) {
17226:                                             push(@{$email_rule{$type}},$rule);
17227:                                         }
17228:                                     }
17229:                                 }
17230:                                 if (@{$email_rule{$type}}) {
17231:                                     $cancreate{'emailoptions'}{$type} = 'custom';
17232:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
17233:                                         if (@{$curr_rule{$type}} > 0) {
17234:                                             foreach my $rule (@{$curr_rule{$type}}) {
17235:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
17236:                                                     push(@{$changes{'email_rule'}},$type);
17237:                                                 }
17238:                                             }
17239:                                         }
17240:                                         foreach my $type (@{$email_rule{$type}}) {
17241:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
17242:                                                 push(@{$changes{'email_rule'}},$type);
17243:                                             }
17244:                                         }
17245:                                     } else {
17246:                                         push(@{$changes{'email_rule'}},$type);
17247:                                     }
17248:                                 }
17249:                             } else {
17250:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
17251:                             }
17252:                         }
17253:                     }
17254:                     if (@types) {
17255:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
17256:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
17257:                             if (@changed) {
17258:                                 push(@{$changes{'inststatus'}},'inststatusguest');
17259:                             }
17260:                         } else {
17261:                             push(@{$changes{'inststatus'}},'inststatusguest');
17262:                         }
17263:                     }
17264:                 } else {
17265:                     delete($env{'form.cancreate_email'});
17266:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
17267:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
17268:                             push(@{$changes{'inststatus'}},'inststatusguest');
17269:                         }
17270:                     }
17271:                 }
17272:             } else {
17273:                 $save_inststatus{'inststatusguest'} = [];
17274:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
17275:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
17276:                         push(@{$changes{'inststatus'}},'inststatusguest');
17277:                     }
17278:                 }
17279:             }
17280:         } else {
17281:             if ($env{'form.cancreate_'.$item}) {
17282:                 push(@{$cancreate{'selfcreate'}},$item);
17283:             }
17284:         }
17285:     }
17286:     my (%userinfo,%savecaptcha);
17287:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
17288: #
17289: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
17290: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
17291: #
17292: 
17293:     if ($env{'form.cancreate_email'}) {
17294:         push(@contexts,'emailusername');
17295:         if (@statuses) {
17296:             foreach my $type (@statuses) {
17297:                 if (ref($infofields) eq 'ARRAY') {
17298:                     foreach my $field (@{$infofields}) {
17299:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
17300:                             $cancreate{'emailusername'}{$type}{$field} = $1;
17301:                         }
17302:                     }
17303:                 }
17304:             }
17305:         }
17306: #
17307: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
17308: # queued requests for self-creation of account verified by e-mail.
17309: #
17310: 
17311:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
17312:         @approvalnotify = sort(@approvalnotify);
17313:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
17314:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
17315:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
17316:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
17317:                     push(@{$changes{'cancreate'}},'notify');
17318:                 }
17319:             } else {
17320:                 if ($cancreate{'notify'}{'approval'}) {
17321:                     push(@{$changes{'cancreate'}},'notify');
17322:                 }
17323:             }
17324:         } elsif ($cancreate{'notify'}{'approval'}) {
17325:             push(@{$changes{'cancreate'}},'notify');
17326:         }
17327: 
17328:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
17329:     }
17330: #  
17331: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
17332: # institutional log-in.
17333: #
17334:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
17335:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
17336:                ($domdefaults{'auth_def'} eq 'localauth'))) {
17337:             $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.').' '.
17338:                           &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.');
17339:         }
17340:     }
17341:     my @fields = ('lastname','firstname','middlename','generation',
17342:                   'permanentemail','id');
17343:     my @shibfields = (@fields,'inststatus');
17344:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
17345: #
17346: # Where usernames may created for institutional log-in and/or institutional single sign on:
17347: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
17348: # may self-create accounts 
17349: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
17350: # which the user may supply, if institutional data is unavailable.
17351: #
17352:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
17353:         if (@types) {
17354:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
17355:             push(@contexts,'statustocreate');
17356:             foreach my $type (@types) {
17357:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
17358:                 foreach my $field (@fields) {
17359:                     if (grep(/^\Q$field\E$/,@modifiable)) {
17360:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
17361:                     } else {
17362:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
17363:                     }
17364:                 }
17365:             }
17366:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
17367:                 foreach my $type (@types) {
17368:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
17369:                         foreach my $field (@fields) {
17370:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
17371:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
17372:                                 push(@{$changes{'selfcreate'}},$type);
17373:                                 last;
17374:                             }
17375:                         }
17376:                     }
17377:                 }
17378:             } else {
17379:                 foreach my $type (@types) {
17380:                     push(@{$changes{'selfcreate'}},$type);
17381:                 }
17382:             }
17383:         }
17384:         foreach my $field (@shibfields) {
17385:             if ($env{'form.shibenv_'.$field} ne '') {
17386:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
17387:             }
17388:         }
17389:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
17390:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
17391:                 foreach my $field (@shibfields) {
17392:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
17393:                         push(@{$changes{'cancreate'}},'shibenv');
17394:                     }
17395:                 }
17396:             } else {
17397:                 foreach my $field (@shibfields) {
17398:                     if ($env{'form.shibenv_'.$field}) {
17399:                         push(@{$changes{'cancreate'}},'shibenv');
17400:                         last;
17401:                     }
17402:                 }
17403:             }
17404:         }
17405:     }
17406:     foreach my $item (@contexts) {
17407:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
17408:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
17409:                 if (ref($cancreate{$item}) eq 'ARRAY') {
17410:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
17411:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17412:                             push(@{$changes{'cancreate'}},$item);
17413:                         }
17414:                     }
17415:                 }
17416:             }
17417:             if (ref($cancreate{$item}) eq 'ARRAY') {
17418:                 foreach my $type (@{$cancreate{$item}}) {
17419:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
17420:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17421:                             push(@{$changes{'cancreate'}},$item);
17422:                         }
17423:                     }
17424:                 }
17425:             }
17426:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
17427:             if (ref($cancreate{$item}) eq 'HASH') {
17428:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
17429:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
17430:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
17431:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
17432:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17433:                                     push(@{$changes{'cancreate'}},$item);
17434:                                 }
17435:                             }
17436:                         }
17437:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
17438:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
17439:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17440:                                 push(@{$changes{'cancreate'}},$item);
17441:                             }
17442:                         }
17443:                     }
17444:                 }
17445:                 foreach my $type (keys(%{$cancreate{$item}})) {
17446:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
17447:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
17448:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
17449:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
17450:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17451:                                         push(@{$changes{'cancreate'}},$item);
17452:                                     }
17453:                                 }
17454:                             } else {
17455:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17456:                                     push(@{$changes{'cancreate'}},$item);
17457:                                 }
17458:                             }
17459:                         }
17460:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
17461:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
17462:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17463:                                 push(@{$changes{'cancreate'}},$item);
17464:                             }
17465:                         }
17466:                     }
17467:                 }
17468:             }
17469:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
17470:             if (ref($cancreate{$item}) eq 'ARRAY') {
17471:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
17472:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17473:                         push(@{$changes{'cancreate'}},$item);
17474:                     }
17475:                 }
17476:             }
17477:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
17478:             if (ref($cancreate{$item}) eq 'HASH') {
17479:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17480:                     push(@{$changes{'cancreate'}},$item);
17481:                 }
17482:             }
17483:         } elsif ($item eq 'emailusername') {
17484:             if (ref($cancreate{$item}) eq 'HASH') {
17485:                 foreach my $type (keys(%{$cancreate{$item}})) {
17486:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
17487:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
17488:                             if ($cancreate{$item}{$type}{$field}) {
17489:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
17490:                                     push(@{$changes{'cancreate'}},$item);
17491:                                 }
17492:                                 last;
17493:                             }
17494:                         }
17495:                     }
17496:                 }
17497:             }
17498:         }
17499:     }
17500: #
17501: # Populate %save_usercreate hash with updates to self-creation configuration.
17502: #
17503:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
17504:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
17505:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
17506:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
17507:     if (ref($cancreate{'notify'}) eq 'HASH') {
17508:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
17509:     }
17510:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
17511:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
17512:     }
17513:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
17514:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
17515:     }
17516:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
17517:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
17518:     }
17519:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
17520:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
17521:     }
17522:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
17523:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
17524:     }
17525:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
17526:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
17527:     }
17528:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
17529:     $save_usercreate{'email_rule'} = \%email_rule;
17530: 
17531:     my %userconfig_hash = (
17532:             usercreation     => \%save_usercreate,
17533:             usermodification => \%save_usermodify,
17534:             inststatus       => \%save_inststatus,
17535:     );
17536: 
17537:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
17538:                                              $dom);
17539: #
17540: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
17541: #
17542:     if ($putresult eq 'ok') {
17543:         if (keys(%changes) > 0) {
17544:             $resulttext = &mt('Changes made:').'<ul>';
17545:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
17546:                 my %lt = &selfcreation_types();
17547:                 foreach my $type (@{$changes{'cancreate'}}) {
17548:                     my $chgtext = '';
17549:                     if ($type eq 'selfcreate') {
17550:                         if (@{$cancreate{$type}} == 0) {
17551:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
17552:                         } else {
17553:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
17554:                                         '<ul>';
17555:                             foreach my $case (@{$cancreate{$type}}) {
17556:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
17557:                             }
17558:                             $chgtext .= '</ul>';
17559:                             if (ref($cancreate{$type}) eq 'ARRAY') {
17560:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
17561:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
17562:                                         if (@{$cancreate{'statustocreate'}} == 0) {
17563:                                             $chgtext .= '<span class="LC_warning">'.
17564:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
17565:                                                         '</span><br />';
17566:                                         }
17567:                                     }
17568:                                 }
17569:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
17570:                                     if (!@statuses) {
17571:                                         $chgtext .= '<span class="LC_warning">'.
17572:                                                     &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.").
17573:                                                     '</span><br />';
17574: 
17575:                                     }
17576:                                 }
17577:                             }
17578:                         }
17579:                     } elsif ($type eq 'shibenv') {
17580:                         if (keys(%{$cancreate{$type}}) == 0) {
17581:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
17582:                         } else {
17583:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
17584:                                         '<ul>';
17585:                             foreach my $field (@shibfields) {
17586:                                 next if ($cancreate{$type}{$field} eq '');
17587:                                 if ($field eq 'inststatus') {
17588:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
17589:                                 } else {
17590:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
17591:                                 }
17592:                             }
17593:                             $chgtext .= '</ul>';
17594:                         }
17595:                     } elsif ($type eq 'statustocreate') {
17596:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
17597:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
17598:                             if (@{$cancreate{'selfcreate'}} > 0) {
17599:                                 if (@{$cancreate{'statustocreate'}} == 0) {
17600:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
17601:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
17602:                                         $chgtext .= '<br />'.
17603:                                                     '<span class="LC_warning">'.
17604:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
17605:                                                     '</span>';
17606:                                     }
17607:                                 } elsif (keys(%usertypes) > 0) {
17608:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
17609:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
17610:                                     } else {
17611:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
17612:                                     }
17613:                                     $chgtext .= '<ul>';
17614:                                     foreach my $case (@{$cancreate{$type}}) {
17615:                                         if ($case eq 'default') {
17616:                                             $chgtext .= '<li>'.$othertitle.'</li>';
17617:                                         } else {
17618:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
17619:                                         }
17620:                                     }
17621:                                     $chgtext .= '</ul>';
17622:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
17623:                                         $chgtext .= '<span class="LC_warning">'.
17624:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
17625:                                                     '</span>';
17626:                                     }
17627:                                 }
17628:                             } else {
17629:                                 if (@{$cancreate{$type}} == 0) {
17630:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
17631:                                 } else {
17632:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
17633:                                 }
17634:                             }
17635:                             $chgtext .= '<br />';
17636:                         }
17637:                     } elsif ($type eq 'selfcreateprocessing') {
17638:                         my %choices = &Apache::lonlocal::texthash (
17639:                                                                     automatic => 'Automatic approval',
17640:                                                                     approval  => 'Queued for approval',
17641:                                                                   );
17642:                         if (@types) {
17643:                             if (@statuses) {
17644:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:'). 
17645:                                             '<ul>';
17646:                                 foreach my $status (@statuses) {
17647:                                     if ($status eq 'default') {
17648:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
17649:                                     } else {
17650:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
17651:                                     }
17652:                                 }
17653:                                 $chgtext .= '</ul>';
17654:                             }
17655:                         } else {
17656:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
17657:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
17658:                         }
17659:                     } elsif ($type eq 'emailverified') {
17660:                         my %options = &Apache::lonlocal::texthash (
17661:                                                                     all   => 'Same as e-mail',
17662:                                                                     first => 'Omit @domain',
17663:                                                                     free  => 'Free to choose',
17664:                                                                   );
17665:                         if (@types) {
17666:                             if (@statuses) {
17667:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
17668:                                             '<ul>';
17669:                                 foreach my $status (@statuses) {
17670:                                     if ($status eq 'default') {
17671:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
17672:                                     } else {
17673:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
17674:                                     }
17675:                                 }
17676:                                 $chgtext .= '</ul>';
17677:                             }
17678:                         } else {
17679:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
17680:                                             $options{$cancreate{'emailverified'}{'default'}});
17681:                         }
17682:                     } elsif ($type eq 'emailoptions') {
17683:                         my %options = &Apache::lonlocal::texthash (
17684:                                                                     any     => 'Any e-mail',
17685:                                                                     inst    => 'Institutional only',
17686:                                                                     noninst => 'Non-institutional only',
17687:                                                                     custom  => 'Custom restrictions',
17688:                                                                   );
17689:                         if (@types) {
17690:                             if (@statuses) {
17691:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
17692:                                             '<ul>';
17693:                                 foreach my $status (@statuses) {
17694:                                     if ($type eq 'default') {
17695:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
17696:                                     } else {
17697:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
17698:                                     }
17699:                                 }
17700:                                 $chgtext .= '</ul>';
17701:                             }
17702:                         } else {
17703:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
17704:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
17705:                             } else {
17706:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
17707:                                                 $options{$cancreate{'emailoptions'}{'default'}});
17708:                             }
17709:                         }
17710:                     } elsif ($type eq 'emaildomain') {
17711:                         my $output;
17712:                         if (@statuses) {
17713:                             foreach my $type (@statuses) {
17714:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
17715:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
17716:                                         if ($type eq 'default') {
17717:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
17718:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
17719:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
17720:                                             } else {
17721:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
17722:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
17723:                                             }
17724:                                         } else {
17725:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
17726:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
17727:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
17728:                                             } else {
17729:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
17730:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>'; 
17731:                                             }
17732:                                         }
17733:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
17734:                                         if ($type eq 'default') {
17735:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
17736:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
17737:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
17738:                                             } else {
17739:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
17740:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
17741:                                             }
17742:                                         } else {
17743:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
17744:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
17745:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
17746:                                             } else {
17747:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
17748:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';   
17749:                                             }
17750:                                         }
17751:                                     }
17752:                                 }
17753:                             }
17754:                         }
17755:                         if ($output ne '') {
17756:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
17757:                                         '<ul>'.$output.'</ul>';
17758:                         }
17759:                     } elsif ($type eq 'captcha') {
17760:                         if ($savecaptcha{$type} eq 'notused') {
17761:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
17762:                         } else {
17763:                             my %captchas = &captcha_phrases();
17764:                             if ($captchas{$savecaptcha{$type}}) {
17765:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
17766:                             } else {
17767:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
17768:                             }
17769:                         }
17770:                     } elsif ($type eq 'recaptchakeys') {
17771:                         my ($privkey,$pubkey);
17772:                         if (ref($savecaptcha{$type}) eq 'HASH') {
17773:                             $pubkey = $savecaptcha{$type}{'public'};
17774:                             $privkey = $savecaptcha{$type}{'private'};
17775:                         }
17776:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
17777:                         if (!$pubkey) {
17778:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
17779:                         } else {
17780:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
17781:                         }
17782:                         if (!$privkey) {
17783:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
17784:                         } else {
17785:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
17786:                         }
17787:                         $chgtext .= '</ul>';
17788:                     } elsif ($type eq 'recaptchaversion') {
17789:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
17790:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
17791:                         }
17792:                     } elsif ($type eq 'emailusername') {
17793:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
17794:                             if (@statuses) {
17795:                                 foreach my $type (@statuses) {
17796:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
17797:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
17798:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
17799:                                                     '<ul>';
17800:                                             foreach my $field (@{$infofields}) {
17801:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
17802:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
17803:                                                 }
17804:                                             }
17805:                                             $chgtext .= '</ul>';
17806:                                         } else {
17807:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
17808:                                         }
17809:                                     } else {
17810:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
17811:                                     }
17812:                                 }
17813:                             }
17814:                         }
17815:                     } elsif ($type eq 'notify') {
17816:                         my $numapprove = 0;
17817:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
17818:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
17819:                                 if ($cancreate{'notify'}{'approval'}) {
17820:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
17821:                                     $numapprove ++;
17822:                                 }
17823:                             }
17824:                         }
17825:                         unless ($numapprove) {
17826:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
17827:                         }
17828:                     }
17829:                     if ($chgtext) {
17830:                         $resulttext .= '<li>'.$chgtext.'</li>';
17831:                     }
17832:                 }
17833:             }
17834:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
17835:                 my ($emailrules,$emailruleorder) =
17836:                     &Apache::lonnet::inst_userrules($dom,'email');
17837:                 foreach my $type (@{$changes{'email_rule'}}) {
17838:                     if (ref($email_rule{$type}) eq 'ARRAY') {
17839:                         my $chgtext = '<ul>';
17840:                         foreach my $rule (@{$email_rule{$type}}) {
17841:                             if (ref($emailrules->{$rule}) eq 'HASH') {
17842:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
17843:                             }
17844:                         }
17845:                         $chgtext .= '</ul>';
17846:                         my $typename;
17847:                         if (@types) {
17848:                             if ($type eq 'default') {
17849:                                 $typename = $othertitle;
17850:                             } else {
17851:                                 $typename = $usertypes{$type};
17852:                             } 
17853:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
17854:                         }
17855:                         if (@{$email_rule{$type}} > 0) {
17856:                             $resulttext .= '<li>'.
17857:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
17858:                                                $usertypes{$type}).
17859:                                            $chgtext.
17860:                                            '</li>';
17861:                         } else {
17862:                             $resulttext .= '<li>'.
17863:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
17864:                                            '</li>'.
17865:                                            &mt('(Affiliation: [_1])',$typename);
17866:                         }
17867:                     }
17868:                 }
17869:             }
17870:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
17871:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
17872:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
17873:                         my $chgtext = '<ul>';
17874:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
17875:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
17876:                         }
17877:                         $chgtext .= '</ul>';
17878:                         $resulttext .= '<li>'.
17879:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
17880:                                           $chgtext.
17881:                                        '</li>';
17882:                     } else {
17883:                         $resulttext .= '<li>'.
17884:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
17885:                                        '</li>';
17886:                     }
17887:                 }
17888:             }
17889:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
17890:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
17891:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
17892:                 foreach my $type (@{$changes{'selfcreate'}}) {
17893:                     my $typename = $type;
17894:                     if (keys(%usertypes) > 0) {
17895:                         if ($usertypes{$type} ne '') {
17896:                             $typename = $usertypes{$type};
17897:                         }
17898:                     }
17899:                     my @modifiable;
17900:                     $resulttext .= '<li>'.
17901:                                     &mt('Self-creation of account by users with status: [_1]',
17902:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
17903:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
17904:                     foreach my $field (@fields) {
17905:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
17906:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
17907:                         }
17908:                     }
17909:                     if (@modifiable > 0) {
17910:                         $resulttext .= join(', ',@modifiable);
17911:                     } else {
17912:                         $resulttext .= &mt('none');
17913:                     }
17914:                     $resulttext .= '</li>';
17915:                 }
17916:                 $resulttext .= '</ul></li>';
17917:             }
17918:             $resulttext .= '</ul>';
17919:             my $cachetime = 24*60*60;
17920:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
17921:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17922:             if (ref($lastactref) eq 'HASH') {
17923:                 $lastactref->{'domdefaults'} = 1;
17924:             }
17925:         } else {
17926:             $resulttext = &mt('No changes made to self-creation settings');
17927:         }
17928:     } else {
17929:         $resulttext = '<span class="LC_error">'.
17930:             &mt('An error occurred: [_1]',$putresult).'</span>';
17931:     }
17932:     if ($warningmsg ne '') {
17933:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
17934:     }
17935:     return $resulttext;
17936: }
17937: 
17938: sub process_captcha {
17939:     my ($container,$changes,$newsettings,$currsettings) = @_;
17940:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH'));
17941:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
17942:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
17943:         $newsettings->{'captcha'} = 'original';
17944:     }
17945:     my %current;
17946:     if (ref($currsettings) eq 'HASH') {
17947:         %current = %{$currsettings};
17948:     }
17949:     if ($current{'captcha'} ne $newsettings->{'captcha'}) {
17950:         if ($container eq 'cancreate') {
17951:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
17952:                 push(@{$changes->{'cancreate'}},'captcha');
17953:             } elsif (!defined($changes->{'cancreate'})) {
17954:                 $changes->{'cancreate'} = ['captcha'];
17955:             }
17956:         } elsif ($container eq 'passwords') {
17957:             $changes->{'reset'} = 1;
17958:         } else {
17959:             $changes->{'captcha'} = 1;
17960:         }
17961:     }
17962:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
17963:     if ($newsettings->{'captcha'} eq 'recaptcha') {
17964:         $newpub = $env{'form.'.$container.'_recaptchapub'};
17965:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
17966:         $newpub =~ s/[^\w\-]//g;
17967:         $newpriv =~ s/[^\w\-]//g;
17968:         $newsettings->{'recaptchakeys'} = {
17969:                                              public  => $newpub,
17970:                                              private => $newpriv,
17971:                                           };
17972:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
17973:         $newversion =~ s/\D//g;
17974:         if ($newversion ne '2') {
17975:             $newversion = 1;
17976:         }
17977:         $newsettings->{'recaptchaversion'} = $newversion;
17978:     }
17979:     if (ref($current{'recaptchakeys'}) eq 'HASH') {
17980:         $currpub = $current{'recaptchakeys'}{'public'};
17981:         $currpriv = $current{'recaptchakeys'}{'private'};
17982:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
17983:             $newsettings->{'recaptchakeys'} = {
17984:                                                  public  => '',
17985:                                                  private => '',
17986:                                               }
17987:         }
17988:     }
17989:     if ($current{'captcha'} eq 'recaptcha') {
17990:         $currversion = $current{'recaptchaversion'};
17991:         if ($currversion ne '2') {
17992:             $currversion = 1;
17993:         }
17994:     }
17995:     if ($currversion ne $newversion) {
17996:         if ($container eq 'cancreate') {
17997:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
17998:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
17999:             } elsif (!defined($changes->{'cancreate'})) {
18000:                 $changes->{'cancreate'} = ['recaptchaversion'];
18001:             }
18002:         } elsif ($container eq 'passwords') {
18003:             $changes->{'reset'} = 1;
18004:         } else {
18005:             $changes->{'recaptchaversion'} = 1;
18006:         }
18007:     }
18008:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
18009:         if ($container eq 'cancreate') {
18010:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
18011:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
18012:             } elsif (!defined($changes->{'cancreate'})) {
18013:                 $changes->{'cancreate'} = ['recaptchakeys'];
18014:             }
18015:         } elsif ($container eq 'passwords') {
18016:             $changes->{'reset'} = 1;
18017:         } else {
18018:             $changes->{'recaptchakeys'} = 1;
18019:         }
18020:     }
18021:     return;
18022: }
18023: 
18024: sub modify_usermodification {
18025:     my ($dom,%domconfig) = @_;
18026:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
18027:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
18028:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
18029:             if ($key eq 'selfcreate') {
18030:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
18031:             } else {  
18032:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
18033:             }
18034:         }
18035:     }
18036:     my @contexts = ('author','course');
18037:     my %context_title = (
18038:                            author => 'In author context',
18039:                            course => 'In course context',
18040:                         );
18041:     my @fields = ('lastname','firstname','middlename','generation',
18042:                   'permanentemail','id');
18043:     my %roles = (
18044:                   author => ['ca','aa'],
18045:                   course => ['st','ep','ta','in','cr'],
18046:                 );
18047:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
18048:     foreach my $context (@contexts) {
18049:         foreach my $role (@{$roles{$context}}) {
18050:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
18051:             foreach my $item (@fields) {
18052:                 if (grep(/^\Q$item\E$/,@modifiable)) {
18053:                     $modifyhash{$context}{$role}{$item} = 1;
18054:                 } else {
18055:                     $modifyhash{$context}{$role}{$item} = 0;
18056:                 }
18057:             }
18058:         }
18059:         if (ref($curr_usermodification{$context}) eq 'HASH') {
18060:             foreach my $role (@{$roles{$context}}) {
18061:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
18062:                     foreach my $field (@fields) {
18063:                         if ($modifyhash{$context}{$role}{$field} ne 
18064:                                 $curr_usermodification{$context}{$role}{$field}) {
18065:                             push(@{$changes{$context}},$role);
18066:                             last;
18067:                         }
18068:                     }
18069:                 }
18070:             }
18071:         } else {
18072:             foreach my $context (@contexts) {
18073:                 foreach my $role (@{$roles{$context}}) {
18074:                     push(@{$changes{$context}},$role);
18075:                 }
18076:             }
18077:         }
18078:     }
18079:     my %usermodification_hash =  (
18080:                                    usermodification => \%modifyhash,
18081:                                  );
18082:     my $putresult = &Apache::lonnet::put_dom('configuration',
18083:                                              \%usermodification_hash,$dom);
18084:     if ($putresult eq 'ok') {
18085:         if (keys(%changes) > 0) {
18086:             $resulttext = &mt('Changes made: ').'<ul>';
18087:             foreach my $context (@contexts) {
18088:                 if (ref($changes{$context}) eq 'ARRAY') {
18089:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
18090:                     if (ref($changes{$context}) eq 'ARRAY') {
18091:                         foreach my $role (@{$changes{$context}}) {
18092:                             my $rolename;
18093:                             if ($role eq 'cr') {
18094:                                 $rolename = &mt('Custom');
18095:                             } else {
18096:                                 $rolename = &Apache::lonnet::plaintext($role);
18097:                             }
18098:                             my @modifiable;
18099:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
18100:                             foreach my $field (@fields) {
18101:                                 if ($modifyhash{$context}{$role}{$field}) {
18102:                                     push(@modifiable,$fieldtitles{$field});
18103:                                 }
18104:                             }
18105:                             if (@modifiable > 0) {
18106:                                 $resulttext .= join(', ',@modifiable);
18107:                             } else {
18108:                                 $resulttext .= &mt('none'); 
18109:                             }
18110:                             $resulttext .= '</li>';
18111:                         }
18112:                         $resulttext .= '</ul></li>';
18113:                     }
18114:                 }
18115:             }
18116:             $resulttext .= '</ul>';
18117:         } else {
18118:             $resulttext = &mt('No changes made to user modification settings');
18119:         }
18120:     } else {
18121:         $resulttext = '<span class="LC_error">'.
18122:             &mt('An error occurred: [_1]',$putresult).'</span>';
18123:     }
18124:     return $resulttext;
18125: }
18126: 
18127: sub modify_defaults {
18128:     my ($dom,$lastactref,%domconfig) = @_;
18129:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
18130:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18131:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
18132:                  'portal_def');
18133:     my @authtypes = ('internal','krb4','krb5','localauth','lti');
18134:     foreach my $item (@items) {
18135:         $newvalues{$item} = $env{'form.'.$item};
18136:         if ($item eq 'auth_def') {
18137:             if ($newvalues{$item} ne '') {
18138:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
18139:                     push(@errors,$item);
18140:                 }
18141:             }
18142:         } elsif ($item eq 'lang_def') {
18143:             if ($newvalues{$item} ne '') {
18144:                 if ($newvalues{$item} =~ /^(\w+)/) {
18145:                     my $langcode = $1;
18146:                     if ($langcode ne 'x_chef') {
18147:                         if (code2language($langcode) eq '') {
18148:                             push(@errors,$item);
18149:                         }
18150:                     }
18151:                 } else {
18152:                     push(@errors,$item);
18153:                 }
18154:             }
18155:         } elsif ($item eq 'timezone_def') {
18156:             if ($newvalues{$item} ne '') {
18157:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
18158:                     push(@errors,$item);   
18159:                 }
18160:             }
18161:         } elsif ($item eq 'datelocale_def') {
18162:             if ($newvalues{$item} ne '') {
18163:                 my @datelocale_ids = DateTime::Locale->ids();
18164:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
18165:                     push(@errors,$item);
18166:                 }
18167:             }
18168:         } elsif ($item eq 'portal_def') {
18169:             if ($newvalues{$item} ne '') {
18170:                 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])\/?$/) {
18171:                     push(@errors,$item);
18172:                 }
18173:             }
18174:         }
18175:         if (grep(/^\Q$item\E$/,@errors)) {
18176:             $newvalues{$item} = $domdefaults{$item};
18177:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
18178:             $changes{$item} = 1;
18179:         }
18180:         $domdefaults{$item} = $newvalues{$item};
18181:     }
18182:     my %staticdefaults = (
18183:                            'intauth_cost'   => 10,
18184:                            'intauth_check'  => 0,
18185:                            'intauth_switch' => 0,
18186:                          );
18187:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
18188:         if (exists($domdefaults{$item})) {
18189:             $newvalues{$item} = $domdefaults{$item};
18190:         } else {
18191:             $newvalues{$item} = $staticdefaults{$item};
18192:         }
18193:     }
18194:     my %defaults_hash = (
18195:                          defaults => \%newvalues,
18196:                         );
18197:     my $title = &defaults_titles();
18198: 
18199:     my $currinststatus;
18200:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
18201:         $currinststatus = $domconfig{'inststatus'};
18202:     } else {
18203:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
18204:         $currinststatus = {
18205:                              inststatustypes => $usertypes,
18206:                              inststatusorder => $types,
18207:                              inststatusguest => [],
18208:                           };
18209:     }
18210:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
18211:     my @allpos;
18212:     my %alltypes;
18213:     my @inststatusguest;
18214:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
18215:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
18216:             unless (grep(/^\Q$type\E$/,@todelete)) {
18217:                 push(@inststatusguest,$type);
18218:             }
18219:         }
18220:     }
18221:     my ($currtitles,$currorder);
18222:     if (ref($currinststatus) eq 'HASH') {
18223:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
18224:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
18225:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
18226:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
18227:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
18228:                     }
18229:                 }
18230:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
18231:                     my $position = $env{'form.inststatus_pos_'.$type};
18232:                     $position =~ s/\D+//g;
18233:                     $allpos[$position] = $type;
18234:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
18235:                     $alltypes{$type} =~ s/`//g;
18236:                 }
18237:             }
18238:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
18239:             $currtitles =~ s/,$//;
18240:         }
18241:     }
18242:     if ($env{'form.addinststatus'}) {
18243:         my $newtype = $env{'form.addinststatus'};
18244:         $newtype =~ s/\W//g;
18245:         unless (exists($alltypes{$newtype})) {
18246:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
18247:             $alltypes{$newtype} =~ s/`//g; 
18248:             my $position = $env{'form.addinststatus_pos'};
18249:             $position =~ s/\D+//g;
18250:             if ($position ne '') {
18251:                 $allpos[$position] = $newtype;
18252:             }
18253:         }
18254:     }
18255:     my @orderedstatus;
18256:     foreach my $type (@allpos) {
18257:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
18258:             push(@orderedstatus,$type);
18259:         }
18260:     }
18261:     foreach my $type (keys(%alltypes)) {
18262:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
18263:             delete($alltypes{$type});
18264:         }
18265:     }
18266:     $defaults_hash{'inststatus'} = {
18267:                                      inststatustypes => \%alltypes,
18268:                                      inststatusorder => \@orderedstatus,
18269:                                      inststatusguest => \@inststatusguest,
18270:                                    };
18271:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
18272:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
18273:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
18274:         }
18275:     }
18276:     if ($currorder ne join(',',@orderedstatus)) {
18277:         $changes{'inststatus'}{'inststatusorder'} = 1;
18278:     }
18279:     my $newtitles;
18280:     foreach my $item (@orderedstatus) {
18281:         $newtitles .= $alltypes{$item}.',';
18282:     }
18283:     $newtitles =~ s/,$//;
18284:     if ($currtitles ne $newtitles) {
18285:         $changes{'inststatus'}{'inststatustypes'} = 1;
18286:     }
18287:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
18288:                                              $dom);
18289:     if ($putresult eq 'ok') {
18290:         if (keys(%changes) > 0) {
18291:             $resulttext = &mt('Changes made:').'<ul>';
18292:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
18293:             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";
18294:             foreach my $item (sort(keys(%changes))) {
18295:                 if ($item eq 'inststatus') {
18296:                     if (ref($changes{'inststatus'}) eq 'HASH') {
18297:                         if (@orderedstatus) {
18298:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
18299:                             foreach my $type (@orderedstatus) { 
18300:                                 $resulttext .= $alltypes{$type}.', ';
18301:                             }
18302:                             $resulttext =~ s/, $//;
18303:                             $resulttext .= '</li>';
18304:                         } else {
18305:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>'; 
18306:                         }
18307:                     }
18308:                 } else {
18309:                     my $value = $env{'form.'.$item};
18310:                     if ($value eq '') {
18311:                         $value = &mt('none');
18312:                     } elsif ($item eq 'auth_def') {
18313:                         my %authnames = &authtype_names();
18314:                         my %shortauth = (
18315:                                           internal   => 'int',
18316:                                           krb4       => 'krb4',
18317:                                           krb5       => 'krb5',
18318:                                           localauth  => 'loc',
18319:                                           lti        => 'lti',
18320:                         );
18321:                         $value = $authnames{$shortauth{$value}};
18322:                     }
18323:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
18324:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
18325:                 }
18326:             }
18327:             $resulttext .= '</ul>';
18328:             $mailmsgtext .= "\n";
18329:             my $cachetime = 24*60*60;
18330:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18331:             if (ref($lastactref) eq 'HASH') {
18332:                 $lastactref->{'domdefaults'} = 1;
18333:             }
18334:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
18335:                 my $notify = 1;
18336:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
18337:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
18338:                         $notify = 0;
18339:                     }
18340:                 }
18341:                 if ($notify) {
18342:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
18343:                                                "LON-CAPA Domain Settings Change - $dom",
18344:                                                $mailmsgtext);
18345:                 }
18346:             }
18347:         } else {
18348:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
18349:         }
18350:     } else {
18351:         $resulttext = '<span class="LC_error">'.
18352:             &mt('An error occurred: [_1]',$putresult).'</span>';
18353:     }
18354:     if (@errors > 0) {
18355:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
18356:         foreach my $item (@errors) {
18357:             $resulttext .= ' "'.$title->{$item}.'",';
18358:         }
18359:         $resulttext =~ s/,$//;
18360:     }
18361:     return $resulttext;
18362: }
18363: 
18364: sub modify_scantron {
18365:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
18366:     my ($resulttext,%confhash,%changes,$errors);
18367:     my $custom = 'custom.tab';
18368:     my $default = 'default.tab';
18369:     my $servadm = $r->dir_config('lonAdmEMail');
18370:     my ($configuserok,$author_ok,$switchserver) =
18371:         &config_check($dom,$confname,$servadm);
18372:     if ($env{'form.scantronformat.filename'} ne '') {
18373:         my $error;
18374:         if ($configuserok eq 'ok') {
18375:             if ($switchserver) {
18376:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
18377:             } else {
18378:                 if ($author_ok eq 'ok') {
18379:                     my ($result,$scantronurl) =
18380:                         &publishlogo($r,'upload','scantronformat',$dom,
18381:                                      $confname,'scantron','','',$custom);
18382:                     if ($result eq 'ok') {
18383:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
18384:                         $changes{'scantronformat'} = 1;
18385:                     } else {
18386:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
18387:                     }
18388:                 } else {
18389:                     $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);
18390:                 }
18391:             }
18392:         } else {
18393:             $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);
18394:         }
18395:         if ($error) {
18396:             &Apache::lonnet::logthis($error);
18397:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
18398:         }
18399:     }
18400:     if (ref($domconfig{'scantron'}) eq 'HASH') {
18401:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
18402:             if ($env{'form.scantronformat_del'}) {
18403:                 $confhash{'scantron'}{'scantronformat'} = '';
18404:                 $changes{'scantronformat'} = 1;
18405:             } else {
18406:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
18407:             }
18408:         }
18409:     }
18410:     my @options = ('hdr','pad','rem');
18411:     my @fields = &scantroncsv_fields();
18412:     my %titles = &scantronconfig_titles();
18413:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
18414:     my ($newdat,$currdat,%newcol,%currcol);
18415:     if (grep(/^dat$/,@formats)) {
18416:         $confhash{'scantron'}{config}{dat} = 1;
18417:         $newdat = 1;
18418:     } else {
18419:         $newdat = 0;
18420:     }
18421:     if (grep(/^csv$/,@formats)) {
18422:         my %bynum;
18423:         foreach my $field (@fields) {
18424:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
18425:                 my $posscol = $1;
18426:                 if (($posscol < 20) && (!$bynum{$posscol})) {
18427:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
18428:                     $bynum{$posscol} = $field;
18429:                     $newcol{$field} = $posscol;
18430:                 }
18431:             }
18432:         }
18433:         if (keys(%newcol)) {
18434:             foreach my $option (@options) {
18435:                 if ($env{'form.scantroncsv_'.$option}) {
18436:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
18437:                 }
18438:             }
18439:         }
18440:     }
18441:     $currdat = 1;
18442:     if (ref($domconfig{'scantron'}) eq 'HASH') {
18443:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
18444:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
18445:                 $currdat = 0;
18446:             }
18447:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
18448:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
18449:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
18450:                 }
18451:             }
18452:         }
18453:     }
18454:     if ($currdat != $newdat) {
18455:         $changes{'config'} = 1;
18456:     } else {
18457:         foreach my $field (@fields) {
18458:             if ($currcol{$field} ne '') {
18459:                 if ($currcol{$field} ne $newcol{$field}) {
18460:                     $changes{'config'} = 1;
18461:                     last;
18462:                 }
18463:             } elsif ($newcol{$field} ne '') {
18464:                 $changes{'config'} = 1;
18465:                 last;
18466:             }
18467:         }
18468:     }
18469:     if (keys(%confhash) > 0) {
18470:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
18471:                                                  $dom);
18472:         if ($putresult eq 'ok') {
18473:             if (keys(%changes) > 0) {
18474:                 if (ref($confhash{'scantron'}) eq 'HASH') {
18475:                     $resulttext = &mt('Changes made:').'<ul>';
18476:                     if ($changes{'scantronformat'}) {
18477:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
18478:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
18479:                         } else {
18480:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
18481:                         }
18482:                     }
18483:                     if ($changes{'config'}) {
18484:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
18485:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
18486:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
18487:                             }
18488:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
18489:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
18490:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
18491:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
18492:                                         foreach my $field (@fields) {
18493:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
18494:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
18495:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
18496:                                             }
18497:                                         }
18498:                                         $resulttext .= '</ul></li>';
18499:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
18500:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
18501:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
18502:                                                 foreach my $option (@options) {
18503:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
18504:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
18505:                                                     }
18506:                                                 }
18507:                                                 $resulttext .= '</ul></li>';
18508:                                             }
18509:                                         }
18510:                                     }
18511:                                 }
18512:                             }
18513:                         } else {
18514:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
18515:                         }
18516:                     }
18517:                     $resulttext .= '</ul>';
18518:                 } else {
18519:                     $resulttext = &mt('Changes made to bubblesheet format file.');
18520:                 }
18521:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
18522:                 if (ref($lastactref) eq 'HASH') {
18523:                     $lastactref->{'domainconfig'} = 1;
18524:                 }
18525:             } else {
18526:                 $resulttext = &mt('No changes made to bubblesheet format settings');
18527:             }
18528:         } else {
18529:             $resulttext = '<span class="LC_error">'.
18530:                 &mt('An error occurred: [_1]',$putresult).'</span>';
18531:         }
18532:     } else {
18533:         $resulttext = &mt('No changes made to bubblesheet format file'); 
18534:     }
18535:     if ($errors) {
18536:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
18537:                        $errors.'</ul></p>';
18538:     }
18539:     return $resulttext;
18540: }
18541: 
18542: sub modify_coursecategories {
18543:     my ($dom,$lastactref,%domconfig) = @_;
18544:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
18545:         $cathash);
18546:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
18547:     my @catitems = ('unauth','auth');
18548:     my @cattypes = ('std','domonly','codesrch','none');
18549:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
18550:         $cathash = $domconfig{'coursecategories'}{'cats'};
18551:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
18552:             $changes{'togglecats'} = 1;
18553:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
18554:         }
18555:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
18556:             $changes{'categorize'} = 1;
18557:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
18558:         }
18559:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
18560:             $changes{'togglecatscomm'} = 1;
18561:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
18562:         }
18563:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
18564:             $changes{'categorizecomm'} = 1;
18565:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
18566: 
18567:         }
18568:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
18569:             $changes{'togglecatsplace'} = 1;
18570:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
18571:         }
18572:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
18573:             $changes{'categorizeplace'} = 1;
18574:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
18575:         }
18576:         foreach my $item (@catitems) {
18577:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
18578:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
18579:                     $changes{$item} = 1;
18580:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
18581:                 }
18582:             }
18583:         }
18584:     } else {
18585:         $changes{'togglecats'} = 1;
18586:         $changes{'categorize'} = 1;
18587:         $changes{'togglecatscomm'} = 1;
18588:         $changes{'categorizecomm'} = 1;
18589:         $changes{'togglecatsplace'} = 1;
18590:         $changes{'categorizeplace'} = 1;
18591:         $domconfig{'coursecategories'} = {
18592:                                              togglecats => $env{'form.togglecats'},
18593:                                              categorize => $env{'form.categorize'},
18594:                                              togglecatscomm => $env{'form.togglecatscomm'},
18595:                                              categorizecomm => $env{'form.categorizecomm'},
18596:                                              togglecatsplace => $env{'form.togglecatsplace'},
18597:                                              categorizeplace => $env{'form.categorizeplace'},
18598:                                          };
18599:         foreach my $item (@catitems) {
18600:             if ($env{'form.coursecat_'.$item} ne 'std') {
18601:                 $changes{$item} = 1;
18602:             }
18603:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
18604:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
18605:             }
18606:         }
18607:     }
18608:     if (ref($cathash) eq 'HASH') {
18609:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
18610:             push (@deletecategory,'instcode::0');
18611:         }
18612:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
18613:             push(@deletecategory,'communities::0');
18614:         }
18615:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
18616:             push(@deletecategory,'placement::0');
18617:         }
18618:     }
18619:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
18620:     if (ref($cathash) eq 'HASH') {
18621:         if (@deletecategory > 0) {
18622:             #FIXME Need to remove category from all courses using a deleted category 
18623:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
18624:             foreach my $item (@deletecategory) {
18625:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
18626:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
18627:                     $deletions{$item} = 1;
18628:                     &recurse_cat_deletes($item,$cathash,\%deletions);
18629:                 }
18630:             }
18631:         }
18632:         foreach my $item (keys(%{$cathash})) {
18633:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
18634:             if ($cathash->{$item} ne $env{'form.'.$item}) {
18635:                 $reorderings{$item} = 1;
18636:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
18637:             }
18638:             if ($env{'form.addcategory_name_'.$item} ne '') {
18639:                 my $newcat = $env{'form.addcategory_name_'.$item};
18640:                 my $newdepth = $depth+1;
18641:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
18642:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
18643:                 $adds{$newitem} = 1; 
18644:             }
18645:             if ($env{'form.subcat_'.$item} ne '') {
18646:                 my $newcat = $env{'form.subcat_'.$item};
18647:                 my $newdepth = $depth+1;
18648:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
18649:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
18650:                 $adds{$newitem} = 1;
18651:             }
18652:         }
18653:     }
18654:     if ($env{'form.instcode'} eq '1') {
18655:         if (ref($cathash) eq 'HASH') {
18656:             my $newitem = 'instcode::0';
18657:             if ($cathash->{$newitem} eq '') {  
18658:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
18659:                 $adds{$newitem} = 1;
18660:             }
18661:         } else {
18662:             my $newitem = 'instcode::0';
18663:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
18664:             $adds{$newitem} = 1;
18665:         }
18666:     }
18667:     if ($env{'form.communities'} eq '1') {
18668:         if (ref($cathash) eq 'HASH') {
18669:             my $newitem = 'communities::0';
18670:             if ($cathash->{$newitem} eq '') {
18671:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
18672:                 $adds{$newitem} = 1;
18673:             }
18674:         } else {
18675:             my $newitem = 'communities::0';
18676:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
18677:             $adds{$newitem} = 1;
18678:         }
18679:     }
18680:     if ($env{'form.placement'} eq '1') {
18681:         if (ref($cathash) eq 'HASH') {
18682:             my $newitem = 'placement::0';
18683:             if ($cathash->{$newitem} eq '') {
18684:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
18685:                 $adds{$newitem} = 1;
18686:             }
18687:         } else {
18688:             my $newitem = 'placement::0';
18689:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
18690:             $adds{$newitem} = 1;
18691:         }
18692:     }
18693:     if ($env{'form.addcategory_name'} ne '') {
18694:         if (($env{'form.addcategory_name'} ne 'instcode') &&
18695:             ($env{'form.addcategory_name'} ne 'communities') &&
18696:             ($env{'form.addcategory_name'} ne 'placement')) {
18697:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
18698:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
18699:             $adds{$newitem} = 1;
18700:         }
18701:     }
18702:     my $putresult;
18703:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
18704:         if (keys(%deletions) > 0) {
18705:             foreach my $key (keys(%deletions)) {
18706:                 if ($predelallitems{$key} ne '') {
18707:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
18708:                 }
18709:             }
18710:         }
18711:         my (@chkcats,@chktrails,%chkallitems);
18712:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
18713:         if (ref($chkcats[0]) eq 'ARRAY') {
18714:             my $depth = 0;
18715:             my $chg = 0;
18716:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
18717:                 my $name = $chkcats[0][$i];
18718:                 my $item;
18719:                 if ($name eq '') {
18720:                     $chg ++;
18721:                 } else {
18722:                     $item = &escape($name).'::0';
18723:                     if ($chg) {
18724:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
18725:                     }
18726:                     $depth ++; 
18727:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
18728:                     $depth --;
18729:                 }
18730:             }
18731:         }
18732:     }
18733:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
18734:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
18735:         if ($putresult eq 'ok') {
18736:             my %title = (
18737:                          togglecats     => 'Show/Hide a course in catalog',
18738:                          categorize     => 'Assign a category to a course',
18739:                          togglecatscomm => 'Show/Hide a community in catalog',
18740:                          categorizecomm => 'Assign a category to a community',
18741:                         );
18742:             my %level = (
18743:                          dom  => 'set in Domain ("Modify Course/Community")',
18744:                          crs  => 'set in Course ("Course Configuration")',
18745:                          comm => 'set in Community ("Community Configuration")',
18746:                          none     => 'No catalog',
18747:                          std      => 'Standard catalog',
18748:                          domonly  => 'Domain-only catalog',
18749:                          codesrch => 'Code search form',
18750:                         );
18751:             $resulttext = &mt('Changes made:').'<ul>';
18752:             if ($changes{'togglecats'}) {
18753:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
18754:             }
18755:             if ($changes{'categorize'}) {
18756:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
18757:             }
18758:             if ($changes{'togglecatscomm'}) {
18759:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
18760:             }
18761:             if ($changes{'categorizecomm'}) {
18762:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
18763:             }
18764:             if ($changes{'unauth'}) {
18765:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
18766:             }
18767:             if ($changes{'auth'}) {
18768:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
18769:             }
18770:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
18771:                 my $cathash;
18772:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
18773:                     $cathash = $domconfig{'coursecategories'}{'cats'};
18774:                 } else {
18775:                     $cathash = {};
18776:                 } 
18777:                 my (@cats,@trails,%allitems);
18778:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
18779:                 if (keys(%deletions) > 0) {
18780:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
18781:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
18782:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
18783:                     }
18784:                     $resulttext .= '</ul></li>';
18785:                 }
18786:                 if (keys(%reorderings) > 0) {
18787:                     my %sort_by_trail;
18788:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
18789:                     foreach my $key (keys(%reorderings)) {
18790:                         if ($allitems{$key} ne '') {
18791:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
18792:                         }
18793:                     }
18794:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
18795:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
18796:                     }
18797:                     $resulttext .= '</ul></li>';
18798:                 }
18799:                 if (keys(%adds) > 0) {
18800:                     my %sort_by_trail;
18801:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
18802:                     foreach my $key (keys(%adds)) {
18803:                         if ($allitems{$key} ne '') {
18804:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
18805:                         }
18806:                     }
18807:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
18808:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
18809:                     }
18810:                     $resulttext .= '</ul></li>';
18811:                 }
18812:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
18813:                 if (ref($lastactref) eq 'HASH') {
18814:                     $lastactref->{'cats'} = 1;
18815:                 }
18816:             }
18817:             $resulttext .= '</ul>';
18818:             if ($changes{'unauth'} || $changes{'auth'}) {
18819:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
18820:                 if ($changes{'auth'}) {
18821:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
18822:                 }
18823:                 if ($changes{'unauth'}) {
18824:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
18825:                 }
18826:                 my $cachetime = 24*60*60;
18827:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18828:                 if (ref($lastactref) eq 'HASH') {
18829:                     $lastactref->{'domdefaults'} = 1;
18830:                 }
18831:             }
18832:         } else {
18833:             $resulttext = '<span class="LC_error">'.
18834:                           &mt('An error occurred: [_1]',$putresult).'</span>';
18835:         }
18836:     } else {
18837:         $resulttext = &mt('No changes made to course and community categories');
18838:     }
18839:     return $resulttext;
18840: }
18841: 
18842: sub modify_serverstatuses {
18843:     my ($dom,%domconfig) = @_;
18844:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
18845:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
18846:         %currserverstatus = %{$domconfig{'serverstatuses'}};
18847:     }
18848:     my @pages = &serverstatus_pages();
18849:     foreach my $type (@pages) {
18850:         $newserverstatus{$type}{'namedusers'} = '';
18851:         $newserverstatus{$type}{'machines'} = '';
18852:         if (defined($env{'form.'.$type.'_namedusers'})) {
18853:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
18854:             my @okusers;
18855:             foreach my $user (@users) {
18856:                 my ($uname,$udom) = split(/:/,$user);
18857:                 if (($udom =~ /^$match_domain$/) &&   
18858:                     (&Apache::lonnet::domain($udom)) &&
18859:                     ($uname =~ /^$match_username$/)) {
18860:                     if (!grep(/^\Q$user\E/,@okusers)) {
18861:                         push(@okusers,$user);
18862:                     }
18863:                 }
18864:             }
18865:             if (@okusers > 0) {
18866:                  @okusers = sort(@okusers);
18867:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
18868:             }
18869:         }
18870:         if (defined($env{'form.'.$type.'_machines'})) {
18871:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
18872:             my @okmachines;
18873:             foreach my $ip (@machines) {
18874:                 my @parts = split(/\./,$ip);
18875:                 next if (@parts < 4);
18876:                 my $badip = 0;
18877:                 for (my $i=0; $i<4; $i++) {
18878:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
18879:                         $badip = 1;
18880:                         last;
18881:                     }
18882:                 }
18883:                 if (!$badip) {
18884:                     push(@okmachines,$ip);     
18885:                 }
18886:             }
18887:             @okmachines = sort(@okmachines);
18888:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
18889:         }
18890:     }
18891:     my %serverstatushash =  (
18892:                                 serverstatuses => \%newserverstatus,
18893:                             );
18894:     foreach my $type (@pages) {
18895:         foreach my $setting ('namedusers','machines') {
18896:             my (@current,@new);
18897:             if (ref($currserverstatus{$type}) eq 'HASH') {
18898:                 if ($currserverstatus{$type}{$setting} ne '') { 
18899:                     @current = split(/,/,$currserverstatus{$type}{$setting});
18900:                 }
18901:             }
18902:             if ($newserverstatus{$type}{$setting} ne '') {
18903:                 @new = split(/,/,$newserverstatus{$type}{$setting});
18904:             }
18905:             if (@current > 0) {
18906:                 if (@new > 0) {
18907:                     foreach my $item (@current) {
18908:                         if (!grep(/^\Q$item\E$/,@new)) {
18909:                             $changes{$type}{$setting} = 1;
18910:                             last;
18911:                         }
18912:                     }
18913:                     foreach my $item (@new) {
18914:                         if (!grep(/^\Q$item\E$/,@current)) {
18915:                             $changes{$type}{$setting} = 1;
18916:                             last;
18917:                         }
18918:                     }
18919:                 } else {
18920:                     $changes{$type}{$setting} = 1;
18921:                 }
18922:             } elsif (@new > 0) {
18923:                 $changes{$type}{$setting} = 1;
18924:             }
18925:         }
18926:     }
18927:     if (keys(%changes) > 0) {
18928:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
18929:         my $putresult = &Apache::lonnet::put_dom('configuration',
18930:                                                  \%serverstatushash,$dom);
18931:         if ($putresult eq 'ok') {
18932:             $resulttext .= &mt('Changes made:').'<ul>';
18933:             foreach my $type (@pages) {
18934:                 if (ref($changes{$type}) eq 'HASH') {
18935:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
18936:                     if ($changes{$type}{'namedusers'}) {
18937:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
18938:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
18939:                         } else {
18940:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
18941:                         }
18942:                     }
18943:                     if ($changes{$type}{'machines'}) {
18944:                         if ($newserverstatus{$type}{'machines'} eq '') {
18945:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
18946:                         } else {
18947:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
18948:                         }
18949: 
18950:                     }
18951:                     $resulttext .= '</ul></li>';
18952:                 }
18953:             }
18954:             $resulttext .= '</ul>';
18955:         } else {
18956:             $resulttext = '<span class="LC_error">'.
18957:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
18958: 
18959:         }
18960:     } else {
18961:         $resulttext = &mt('No changes made to access to server status pages');
18962:     }
18963:     return $resulttext;
18964: }
18965: 
18966: sub modify_helpsettings {
18967:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
18968:     my ($resulttext,$errors,%changes,%helphash);
18969:     my %defaultchecked = ('submitbugs' => 'on');
18970:     my @offon = ('off','on');
18971:     my @toggles = ('submitbugs');
18972:     my %current = ('submitbugs' => '',
18973:                    'adhoc'      => {},
18974:                   );
18975:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
18976:         %current = %{$domconfig{'helpsettings'}};
18977:     }
18978:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18979:     foreach my $item (@toggles) {
18980:         if ($defaultchecked{$item} eq 'on') { 
18981:             if ($current{$item} eq '') {
18982:                 if ($env{'form.'.$item} eq '0') {
18983:                     $changes{$item} = 1;
18984:                 }
18985:             } elsif ($current{$item} ne $env{'form.'.$item}) {
18986:                 $changes{$item} = 1;
18987:             }
18988:         } elsif ($defaultchecked{$item} eq 'off') {
18989:             if ($current{$item} eq '') {
18990:                 if ($env{'form.'.$item} eq '1') {
18991:                     $changes{$item} = 1;
18992:                 }
18993:             } elsif ($current{$item} ne $env{'form.'.$item}) {
18994:                 $changes{$item} = 1;
18995:             }
18996:         }
18997:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
18998:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
18999:         }
19000:     }
19001:     my $maxnum = $env{'form.helproles_maxnum'};
19002:     my $confname = $dom.'-domainconfig';
19003:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
19004:     my (@allpos,%newsettings,%changedprivs,$newrole);
19005:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
19006:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
19007:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
19008:     my %lt = &Apache::lonlocal::texthash(
19009:                     s      => 'system',
19010:                     d      => 'domain',
19011:                     order  => 'Display order',
19012:                     access => 'Role usage',
19013:                     all    => 'All with domain helpdesk or helpdesk assistant role',
19014:                     dh     => 'All with domain helpdesk role',
19015:                     da     => 'All with domain helpdesk assistant role',
19016:                     none   => 'None',
19017:                     status => 'Determined based on institutional status',
19018:                     inc    => 'Include all, but exclude specific personnel',
19019:                     exc    => 'Exclude all, but include specific personnel',
19020:     );
19021:     for (my $num=0; $num<=$maxnum; $num++) {
19022:         my ($prefix,$identifier,$rolename,%curr);
19023:         if ($num == $maxnum) {
19024:             next unless ($env{'form.newcusthelp'} == $maxnum);
19025:             $identifier = 'custhelp'.$num;
19026:             $prefix = 'helproles_'.$num;
19027:             $rolename = $env{'form.custhelpname'.$num};
19028:             $rolename=~s/[^A-Za-z0-9]//gs;
19029:             next if ($rolename eq '');
19030:             next if (exists($existing{'rolesdef_'.$rolename}));
19031:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
19032:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
19033:                                                      $newprivs{'c'},$confname,$dom);
19034:             if ($result ne 'ok') {
19035:                 $errors .= '<li><span class="LC_error">'.
19036:                            &mt('An error occurred storing the new custom role: [_1]',
19037:                            $result).'</span></li>';
19038:                 next;
19039:             } else {
19040:                 $changedprivs{$rolename} = \%newprivs;
19041:                 $newrole = $rolename;
19042:             }
19043:         } else {
19044:             $prefix = 'helproles_'.$num;
19045:             $rolename = $env{'form.'.$prefix};
19046:             next if ($rolename eq '');
19047:             next unless (exists($existing{'rolesdef_'.$rolename}));
19048:             $identifier = 'custhelp'.$num;
19049:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
19050:             my %currprivs;
19051:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
19052:                 split(/\_/,$existing{'rolesdef_'.$rolename});
19053:             foreach my $level ('c','d','s') {
19054:                 if ($newprivs{$level} ne $currprivs{$level}) {
19055:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
19056:                                                              $newprivs{'c'},$confname,$dom);
19057:                     if ($result ne 'ok') {
19058:                         $errors .= '<li><span class="LC_error">'.
19059:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
19060:                                        $rolename,$result).'</span></li>';
19061:                     } else {
19062:                         $changedprivs{$rolename} = \%newprivs;
19063:                     }
19064:                     last;
19065:                 }
19066:             }
19067:             if (ref($current{'adhoc'}) eq 'HASH') {
19068:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
19069:                     %curr = %{$current{'adhoc'}{$rolename}};
19070:                 }
19071:             }
19072:         }
19073:         my $newpos = $env{'form.'.$prefix.'_pos'};
19074:         $newpos =~ s/\D+//g;
19075:         $allpos[$newpos] = $rolename;
19076:         my $newdesc = $env{'form.'.$prefix.'_desc'};
19077:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
19078:         if ($curr{'desc'}) {
19079:             if ($curr{'desc'} ne $newdesc) {
19080:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
19081:                 $newsettings{$rolename}{'desc'} = $newdesc;
19082:             }
19083:         } elsif ($newdesc ne '') {
19084:             $changes{'customrole'}{$rolename}{'desc'} = 1;
19085:             $newsettings{$rolename}{'desc'} = $newdesc;
19086:         }
19087:         my $access = $env{'form.'.$prefix.'_access'};
19088:         if (grep(/^\Q$access\E$/,@accesstypes)) {
19089:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
19090:             if ($access eq 'status') {
19091:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
19092:                 if (scalar(@statuses) == 0) {
19093:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
19094:                 } else {
19095:                     my (@shownstatus,$numtypes);
19096:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
19097:                     if (ref($types) eq 'ARRAY') {
19098:                         $numtypes = scalar(@{$types});
19099:                         foreach my $type (sort(@statuses)) {
19100:                             if ($type eq 'default') {
19101:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
19102:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
19103:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
19104:                                 push(@shownstatus,$usertypes->{$type});
19105:                             }
19106:                         }
19107:                     }
19108:                     if (grep(/^default$/,@statuses)) {
19109:                         push(@shownstatus,$othertitle);
19110:                     }
19111:                     if (scalar(@shownstatus) == 1+$numtypes) {
19112:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
19113:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
19114:                     } else {
19115:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
19116:                         if (ref($curr{'status'}) eq 'ARRAY') {
19117:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
19118:                             if (@diffs) {
19119:                                 $changes{'customrole'}{$rolename}{$access} = 1;
19120:                             }
19121:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
19122:                             $changes{'customrole'}{$rolename}{$access} = 1;
19123:                         }
19124:                     }
19125:                 }
19126:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
19127:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
19128:                 my @newspecstaff;
19129:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
19130:                 foreach my $person (sort(@personnel)) {
19131:                     if ($domhelpdesk{$person}) {
19132:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
19133:                     }
19134:                 }
19135:                 if (ref($curr{$access}) eq 'ARRAY') {
19136:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
19137:                     if (@diffs) {
19138:                         $changes{'customrole'}{$rolename}{$access} = 1;
19139:                     }
19140:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
19141:                     $changes{'customrole'}{$rolename}{$access} = 1;
19142:                 }
19143:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
19144:                     my ($uname,$udom) = split(/:/,$person);
19145:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
19146:                 }
19147:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
19148:             }
19149:         } else {
19150:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
19151:         }
19152:         unless ($curr{'access'} eq $access) {
19153:             $changes{'customrole'}{$rolename}{'access'} = 1;
19154:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
19155:         }
19156:     }
19157:     if (@allpos > 0) {
19158:         my $idx = 0;
19159:         foreach my $rolename (@allpos) {
19160:             if ($rolename ne '') {
19161:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
19162:                 if (ref($current{'adhoc'}) eq 'HASH') {
19163:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
19164:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
19165:                             $changes{'customrole'}{$rolename}{'order'} = 1;
19166:                             $newsettings{$rolename}{'order'} = $idx+1;
19167:                         }
19168:                     }
19169:                 }
19170:                 $idx ++;
19171:             }
19172:         }
19173:     }
19174:     my $putresult;
19175:     if (keys(%changes) > 0) {
19176:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
19177:         if ($putresult eq 'ok') {
19178:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
19179:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
19180:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
19181:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
19182:                 }
19183:             }
19184:             my $cachetime = 24*60*60;
19185:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19186:             if (ref($lastactref) eq 'HASH') {
19187:                 $lastactref->{'domdefaults'} = 1;
19188:             }
19189:         } else {
19190:             $errors .= '<li><span class="LC_error">'.
19191:                        &mt('An error occurred storing the settings: [_1]',
19192:                            $putresult).'</span></li>';
19193:         }
19194:     }
19195:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
19196:         $resulttext = &mt('Changes made:').'<ul>';
19197:         my (%shownprivs,@levelorder);
19198:         @levelorder = ('c','d','s');
19199:         if ((keys(%changes)) && ($putresult eq 'ok')) {
19200:             foreach my $item (sort(keys(%changes))) {
19201:                 if ($item eq 'submitbugs') {
19202:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
19203:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
19204:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
19205:                 } elsif ($item eq 'customrole') {
19206:                     if (ref($changes{'customrole'}) eq 'HASH') {
19207:                         my @keyorder = ('order','desc','access','status','exc','inc');
19208:                         my %keytext = &Apache::lonlocal::texthash(
19209:                                                                    order  => 'Order',
19210:                                                                    desc   => 'Role description',
19211:                                                                    access => 'Role usage',
19212:                                                                    status => 'Allowed institutional types',
19213:                                                                    exc    => 'Allowed personnel',
19214:                                                                    inc    => 'Disallowed personnel',
19215:                         );
19216:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
19217:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
19218:                                 if ($role eq $newrole) {
19219:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
19220:                                                               $role).'<ul>';
19221:                                 } else {
19222:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
19223:                                                               $role).'<ul>';
19224:                                 }
19225:                                 foreach my $key (@keyorder) {
19226:                                     if ($changes{'customrole'}{$role}{$key}) {
19227:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
19228:                                                                   $keytext{$key},$newsettings{$role}{$key}).
19229:                                                        '</li>';
19230:                                     }
19231:                                 }
19232:                                 if (ref($changedprivs{$role}) eq 'HASH') {
19233:                                     $shownprivs{$role} = 1;
19234:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
19235:                                     foreach my $level (@levelorder) {
19236:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
19237:                                             next if ($item eq '');
19238:                                             my ($priv) = split(/\&/,$item,2);
19239:                                             if (&Apache::lonnet::plaintext($priv)) {
19240:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
19241:                                                 unless ($level eq 'c') {
19242:                                                     $resulttext .= ' ('.$lt{$level}.')';
19243:                                                 }
19244:                                                 $resulttext .= '</li>';
19245:                                             }
19246:                                         }
19247:                                     }
19248:                                     $resulttext .= '</ul>';
19249:                                 }
19250:                                 $resulttext .= '</ul></li>';
19251:                             }
19252:                         }
19253:                     }
19254:                 }
19255:             }
19256:         }
19257:         if (keys(%changedprivs)) {
19258:             foreach my $role (sort(keys(%changedprivs))) {
19259:                 unless ($shownprivs{$role}) {
19260:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
19261:                                               $role).'<ul>'.
19262:                                    '<li>'.&mt('Privileges set to :').'<ul>';
19263:                     foreach my $level (@levelorder) {
19264:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
19265:                             next if ($item eq '');
19266:                             my ($priv) = split(/\&/,$item,2);
19267:                             if (&Apache::lonnet::plaintext($priv)) {
19268:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
19269:                                 unless ($level eq 'c') {
19270:                                     $resulttext .= ' ('.$lt{$level}.')';
19271:                                 }
19272:                                 $resulttext .= '</li>';
19273:                             }
19274:                         }
19275:                     }
19276:                     $resulttext .= '</ul></li></ul></li>';
19277:                 }
19278:             }
19279:         }
19280:         $resulttext .= '</ul>';
19281:     } else {
19282:         $resulttext = &mt('No changes made to help settings');
19283:     }
19284:     if ($errors) {
19285:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
19286:                                     $errors.'</ul>';
19287:     }
19288:     return $resulttext;
19289: }
19290: 
19291: sub modify_coursedefaults {
19292:     my ($dom,$lastactref,%domconfig) = @_;
19293:     my ($resulttext,$errors,%changes,%defaultshash);
19294:     my %defaultchecked = (
19295:                            'canuse_pdfforms' => 'off',
19296:                            'uselcmath'       => 'on',
19297:                            'usejsme'         => 'on'
19298:                          );
19299:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
19300:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
19301:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
19302:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
19303:                    'mysqltables_textbook','mysqltables_placement');
19304:     my @types = ('official','unofficial','community','textbook','placement');
19305:     my %staticdefaults = (
19306:                            anonsurvey_threshold => 10,
19307:                            uploadquota          => 500,
19308:                            postsubmit           => 60,
19309:                            mysqltables          => 172800,
19310:                          );
19311:     my %texoptions = (
19312:                         MathJax  => 'MathJax',
19313:                         mimetex  => &mt('Convert to Images'),
19314:                         tth      => &mt('TeX to HTML'),
19315:                      );
19316:     $defaultshash{'coursedefaults'} = {};
19317: 
19318:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
19319:         if ($domconfig{'coursedefaults'} eq '') {
19320:             $domconfig{'coursedefaults'} = {};
19321:         }
19322:     }
19323: 
19324:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
19325:         foreach my $item (@toggles) {
19326:             if ($defaultchecked{$item} eq 'on') {
19327:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
19328:                     ($env{'form.'.$item} eq '0')) {
19329:                     $changes{$item} = 1;
19330:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
19331:                     $changes{$item} = 1;
19332:                 }
19333:             } elsif ($defaultchecked{$item} eq 'off') {
19334:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
19335:                     ($env{'form.'.$item} eq '1')) {
19336:                     $changes{$item} = 1;
19337:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
19338:                     $changes{$item} = 1;
19339:                 }
19340:             }
19341:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
19342:         }
19343:         foreach my $item (@numbers) {
19344:             my ($currdef,$newdef);
19345:             $newdef = $env{'form.'.$item};
19346:             if ($item eq 'anonsurvey_threshold') {
19347:                 $currdef = $domconfig{'coursedefaults'}{$item};
19348:                 $newdef =~ s/\D//g;
19349:                 if ($newdef eq '' || $newdef < 1) {
19350:                     $newdef = 1;
19351:                 }
19352:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
19353:             } else {
19354:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
19355:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
19356:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
19357:                 }
19358:                 $newdef =~ s/[^\w.\-]//g;
19359:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
19360:             }
19361:             if ($currdef ne $newdef) {
19362:                 if ($item eq 'anonsurvey_threshold') {
19363:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
19364:                         $changes{$item} = 1;
19365:                     }
19366:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
19367:                     my $setting = $1;
19368:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
19369:                         $changes{$setting} = 1;
19370:                     }
19371:                 }
19372:             }
19373:         }
19374:         my $texengine;
19375:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
19376:             $texengine = $env{'form.texengine'};
19377:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
19378:             if ($currdef eq '') {
19379:                 unless ($texengine eq $Apache::lonnet::deftex) {
19380:                     $changes{'texengine'} = 1;
19381:                 }
19382:             } elsif ($currdef ne $texengine) {
19383:                 $changes{'texengine'} = 1;
19384:             }
19385:         }
19386:         if ($texengine ne '') {
19387:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
19388:         }
19389:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
19390:         my @currclonecode;
19391:         if (ref($currclone) eq 'HASH') {
19392:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
19393:                 @currclonecode = @{$currclone->{'instcode'}};
19394:             }
19395:         }
19396:         my $newclone;
19397:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
19398:             $newclone = $env{'form.canclone'};
19399:         }
19400:         if ($newclone eq 'instcode') {
19401:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
19402:             my (%codedefaults,@code_order,@clonecode);
19403:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
19404:                                                     \@code_order);
19405:             foreach my $item (@code_order) {
19406:                 if (grep(/^\Q$item\E$/,@newcodes)) {
19407:                     push(@clonecode,$item);
19408:                 }
19409:             }
19410:             if (@clonecode) {
19411:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
19412:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
19413:                 if (@diffs) {
19414:                     $changes{'canclone'} = 1;
19415:                 }
19416:             } else {
19417:                 $newclone eq '';
19418:             }
19419:         } elsif ($newclone ne '') {
19420:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
19421:         }
19422:         if ($newclone ne $currclone) {
19423:             $changes{'canclone'} = 1;
19424:         }
19425:         my %credits;
19426:         foreach my $type (@types) {
19427:             unless ($type eq 'community') {
19428:                 $credits{$type} = $env{'form.'.$type.'_credits'};
19429:                 $credits{$type} =~ s/[^\d.]+//g;
19430:             }
19431:         }
19432:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
19433:             ($env{'form.coursecredits'} eq '1')) {
19434:             $changes{'coursecredits'} = 1;
19435:             foreach my $type (keys(%credits)) {
19436:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
19437:             }
19438:         } else {
19439:             if ($env{'form.coursecredits'} eq '1') {
19440:                 foreach my $type (@types) {
19441:                     unless ($type eq 'community') {
19442:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
19443:                             $changes{'coursecredits'} = 1;
19444:                         }
19445:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
19446:                     }
19447:                 }
19448:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
19449:                 foreach my $type (@types) {
19450:                     unless ($type eq 'community') {
19451:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
19452:                             $changes{'coursecredits'} = 1;
19453:                             last;
19454:                         }
19455:                     }
19456:                 }
19457:             }
19458:         }
19459:         if ($env{'form.postsubmit'} eq '1') {
19460:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
19461:             my %currtimeout;
19462:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
19463:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
19464:                     $changes{'postsubmit'} = 1;
19465:                 }
19466:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
19467:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
19468:                 }
19469:             } else {
19470:                 $changes{'postsubmit'} = 1;
19471:             }
19472:             foreach my $type (@types) {
19473:                 my $timeout = $env{'form.'.$type.'_timeout'};
19474:                 $timeout =~ s/\D//g;
19475:                 if ($timeout == $staticdefaults{'postsubmit'}) {
19476:                     $timeout = '';
19477:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
19478:                     $timeout = '0';
19479:                 }
19480:                 unless ($timeout eq '') {
19481:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
19482:                 }
19483:                 if (exists($currtimeout{$type})) {
19484:                     if ($timeout ne $currtimeout{$type}) {
19485:                         $changes{'postsubmit'} = 1;
19486:                     }
19487:                 } elsif ($timeout ne '') {
19488:                     $changes{'postsubmit'} = 1;
19489:                 }
19490:             }
19491:         } else {
19492:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
19493:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
19494:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
19495:                     $changes{'postsubmit'} = 1;
19496:                 }
19497:             } else {
19498:                 $changes{'postsubmit'} = 1;
19499:             }
19500:         }
19501:     }
19502:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
19503:                                              $dom);
19504:     if ($putresult eq 'ok') {
19505:         if (keys(%changes) > 0) {
19506:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19507:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
19508:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
19509:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'})) {
19510:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme','texengine') {
19511:                     if ($changes{$item}) {
19512:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
19513:                     }
19514:                 }
19515:                 if ($changes{'coursecredits'}) {
19516:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
19517:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
19518:                             $domdefaults{$type.'credits'} =
19519:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
19520:                         }
19521:                     }
19522:                 }
19523:                 if ($changes{'postsubmit'}) {
19524:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
19525:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
19526:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
19527:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
19528:                                 $domdefaults{$type.'postsubtimeout'} =
19529:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
19530:                             }
19531:                         }
19532:                     }
19533:                 }
19534:                 if ($changes{'uploadquota'}) {
19535:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
19536:                         foreach my $type (@types) {
19537:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
19538:                         }
19539:                     }
19540:                 }
19541:                 if ($changes{'canclone'}) {
19542:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
19543:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
19544:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
19545:                             if (@clonecodes) {
19546:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
19547:                             }
19548:                         }
19549:                     } else {
19550:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
19551:                     }
19552:                 }
19553:                 my $cachetime = 24*60*60;
19554:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19555:                 if (ref($lastactref) eq 'HASH') {
19556:                     $lastactref->{'domdefaults'} = 1;
19557:                 }
19558:             }
19559:             $resulttext = &mt('Changes made:').'<ul>';
19560:             foreach my $item (sort(keys(%changes))) {
19561:                 if ($item eq 'canuse_pdfforms') {
19562:                     if ($env{'form.'.$item} eq '1') {
19563:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
19564:                     } else {
19565:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
19566:                     }
19567:                 } elsif ($item eq 'uselcmath') {
19568:                     if ($env{'form.'.$item} eq '1') {
19569:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
19570:                     } else {
19571:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
19572:                     }
19573:                 } elsif ($item eq 'usejsme') {
19574:                     if ($env{'form.'.$item} eq '1') {
19575:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
19576:                     } else {
19577:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
19578:                     }
19579:                 } elsif ($item eq 'texengine') {
19580:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
19581:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
19582:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
19583:                     }
19584:                 } elsif ($item eq 'anonsurvey_threshold') {
19585:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
19586:                 } elsif ($item eq 'uploadquota') {
19587:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
19588:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
19589:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
19590:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
19591:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
19592:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
19593:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
19594:                                        '</ul>'.
19595:                                        '</li>';
19596:                     } else {
19597:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
19598:                     }
19599:                 } elsif ($item eq 'mysqltables') {
19600:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
19601:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
19602:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
19603:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
19604:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
19605:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
19606:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
19607:                                        '</ul>'.
19608:                                        '</li>';
19609:                     } else {
19610:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
19611:                     }
19612:                 } elsif ($item eq 'postsubmit') {
19613:                     if ($domdefaults{'postsubmit'} eq 'off') {
19614:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
19615:                     } else {
19616:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
19617:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
19618:                             $resulttext .= &mt('durations:').'<ul>';
19619:                             foreach my $type (@types) {
19620:                                 $resulttext .= '<li>';
19621:                                 my $timeout;
19622:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
19623:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
19624:                                 }
19625:                                 my $display;
19626:                                 if ($timeout eq '0') {
19627:                                     $display = &mt('unlimited');
19628:                                 } elsif ($timeout eq '') {
19629:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
19630:                                 } else {
19631:                                     $display = &mt('[quant,_1,second]',$timeout);
19632:                                 }
19633:                                 if ($type eq 'community') {
19634:                                     $resulttext .= &mt('Communities');
19635:                                 } elsif ($type eq 'official') {
19636:                                     $resulttext .= &mt('Official courses');
19637:                                 } elsif ($type eq 'unofficial') {
19638:                                     $resulttext .= &mt('Unofficial courses');
19639:                                 } elsif ($type eq 'textbook') {
19640:                                     $resulttext .= &mt('Textbook courses');
19641:                                 } elsif ($type eq 'placement') {
19642:                                     $resulttext .= &mt('Placement tests');
19643:                                 }
19644:                                 $resulttext .= ' -- '.$display.'</li>';
19645:                             }
19646:                             $resulttext .= '</ul>';
19647:                         }
19648:                         $resulttext .= '</li>';
19649:                     }
19650:                 } elsif ($item eq 'coursecredits') {
19651:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
19652:                         if (($domdefaults{'officialcredits'} eq '') &&
19653:                             ($domdefaults{'unofficialcredits'} eq '') &&
19654:                             ($domdefaults{'textbookcredits'} eq '')) {
19655:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
19656:                         } else {
19657:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
19658:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
19659:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
19660:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
19661:                                            '</ul>'.
19662:                                            '</li>';
19663:                         }
19664:                     } else {
19665:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
19666:                     }
19667:                 } elsif ($item eq 'canclone') {
19668:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
19669:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
19670:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
19671:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
19672:                         }
19673:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
19674:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
19675:                     } else {
19676:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
19677:                     }
19678:                 }
19679:             }
19680:             $resulttext .= '</ul>';
19681:         } else {
19682:             $resulttext = &mt('No changes made to course defaults');
19683:         }
19684:     } else {
19685:         $resulttext = '<span class="LC_error">'.
19686:             &mt('An error occurred: [_1]',$putresult).'</span>';
19687:     }
19688:     return $resulttext;
19689: }
19690: 
19691: sub modify_selfenrollment {
19692:     my ($dom,$lastactref,%domconfig) = @_;
19693:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
19694:     my @types = ('official','unofficial','community','textbook','placement');
19695:     my %titles = &tool_titles();
19696:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
19697:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
19698:     $ordered{'default'} = ['types','registered','approval','limit'];
19699: 
19700:     my (%roles,%shown,%toplevel);
19701:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
19702: 
19703:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
19704:         if ($domconfig{'selfenrollment'} eq '') {
19705:             $domconfig{'selfenrollment'} = {};
19706:         }
19707:     }
19708:     %toplevel = (
19709:                   admin      => 'Configuration Rights',
19710:                   default    => 'Default settings',
19711:                   validation => 'Validation of self-enrollment requests',
19712:                 );
19713:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
19714: 
19715:     if (ref($ordered{'admin'}) eq 'ARRAY') {
19716:         foreach my $item (@{$ordered{'admin'}}) {
19717:             foreach my $type (@types) {
19718:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
19719:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
19720:                 } else {
19721:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
19722:                 }
19723:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
19724:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
19725:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
19726:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
19727:                             push(@{$changes{'admin'}{$type}},$item);
19728:                         }
19729:                     } else {
19730:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
19731:                             push(@{$changes{'admin'}{$type}},$item);
19732:                         }
19733:                     }
19734:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
19735:                     push(@{$changes{'admin'}{$type}},$item);
19736:                 }
19737:             }
19738:         }
19739:     }
19740: 
19741:     foreach my $item (@{$ordered{'default'}}) {
19742:         foreach my $type (@types) {
19743:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
19744:             if ($item eq 'types') {
19745:                 unless (($value eq 'all') || ($value eq 'dom')) {
19746:                     $value = '';
19747:                 }
19748:             } elsif ($item eq 'registered') {
19749:                 unless ($value eq '1') {
19750:                     $value = 0;
19751:                 }
19752:             } elsif ($item eq 'approval') {
19753:                 unless ($value =~ /^[012]$/) {
19754:                     $value = 0;
19755:                 }
19756:             } else {
19757:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
19758:                     $value = 'none';
19759:                 }
19760:             }
19761:             $selfenrollhash{'default'}{$type}{$item} = $value;
19762:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
19763:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
19764:                     if ($selfenrollhash{'default'}{$type}{$item} ne
19765:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
19766:                          push(@{$changes{'default'}{$type}},$item);
19767:                     }
19768:                 } else {
19769:                     push(@{$changes{'default'}{$type}},$item);
19770:                 }
19771:             } else {
19772:                 push(@{$changes{'default'}{$type}},$item);
19773:             }
19774:             if ($item eq 'limit') {
19775:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
19776:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
19777:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
19778:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
19779:                     }
19780:                 } else {
19781:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
19782:                 }
19783:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
19784:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
19785:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
19786:                          push(@{$changes{'default'}{$type}},'cap');
19787:                     }
19788:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
19789:                     push(@{$changes{'default'}{$type}},'cap');
19790:                 }
19791:             }
19792:         }
19793:     }
19794: 
19795:     foreach my $item (@{$itemsref}) {
19796:         if ($item eq 'fields') {
19797:             my @changed;
19798:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
19799:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
19800:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
19801:             }
19802:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
19803:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
19804:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
19805:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
19806:                 } else {
19807:                     @changed = @{$selfenrollhash{'validation'}{$item}};
19808:                 }
19809:             } else {
19810:                 @changed = @{$selfenrollhash{'validation'}{$item}};
19811:             }
19812:             if (@changed) {
19813:                 if ($selfenrollhash{'validation'}{$item}) { 
19814:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
19815:                 } else {
19816:                     $changes{'validation'}{$item} = &mt('None');
19817:                 }
19818:             }
19819:         } else {
19820:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
19821:             if ($item eq 'markup') {
19822:                if ($env{'form.selfenroll_validation_'.$item}) {
19823:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
19824:                }
19825:             }
19826:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
19827:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
19828:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
19829:                 }
19830:             }
19831:         }
19832:     }
19833: 
19834:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
19835:                                              $dom);
19836:     if ($putresult eq 'ok') {
19837:         if (keys(%changes) > 0) {
19838:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19839:             $resulttext = &mt('Changes made:').'<ul>';
19840:             foreach my $key ('admin','default','validation') {
19841:                 if (ref($changes{$key}) eq 'HASH') {
19842:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
19843:                     if ($key eq 'validation') {
19844:                         foreach my $item (@{$itemsref}) {
19845:                             if (exists($changes{$key}{$item})) {
19846:                                 if ($item eq 'markup') {
19847:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
19848:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
19849:                                 } else {  
19850:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
19851:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
19852:                                 }
19853:                             }
19854:                         }
19855:                     } else {
19856:                         foreach my $type (@types) {
19857:                             if ($type eq 'community') {
19858:                                 $roles{'1'} = &mt('Community personnel');
19859:                             } else {
19860:                                 $roles{'1'} = &mt('Course personnel');
19861:                             }
19862:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
19863:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
19864:                                     if ($key eq 'admin') {
19865:                                         my @mgrdc = ();
19866:                                         if (ref($ordered{$key}) eq 'ARRAY') {
19867:                                             foreach my $item (@{$ordered{'admin'}}) {
19868:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
19869:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
19870:                                                         push(@mgrdc,$item);
19871:                                                     }
19872:                                                 }
19873:                                             }
19874:                                             if (@mgrdc) {
19875:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
19876:                                             } else {
19877:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
19878:                                             }
19879:                                         }
19880:                                     } else {
19881:                                         if (ref($ordered{$key}) eq 'ARRAY') {
19882:                                             foreach my $item (@{$ordered{$key}}) {
19883:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
19884:                                                     $domdefaults{$type.'selfenroll'.$item} =
19885:                                                         $selfenrollhash{$key}{$type}{$item};
19886:                                                 }
19887:                                             }
19888:                                         }
19889:                                     }
19890:                                 }
19891:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
19892:                                 foreach my $item (@{$ordered{$key}}) {
19893:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
19894:                                         $resulttext .= '<li>';
19895:                                         if ($key eq 'admin') {
19896:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
19897:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
19898:                                         } else {
19899:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
19900:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
19901:                                         }
19902:                                         $resulttext .= '</li>';
19903:                                     }
19904:                                 }
19905:                                 $resulttext .= '</ul></li>';
19906:                             }
19907:                         }
19908:                         $resulttext .= '</ul></li>'; 
19909:                     }
19910:                 }
19911:             }
19912:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
19913:                 my $cachetime = 24*60*60;
19914:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19915:                 if (ref($lastactref) eq 'HASH') {
19916:                     $lastactref->{'domdefaults'} = 1;
19917:                 }
19918:             }
19919:             $resulttext .= '</ul>';
19920:         } else {
19921:             $resulttext = &mt('No changes made to self-enrollment settings');
19922:         }
19923:     } else {
19924:         $resulttext = '<span class="LC_error">'.
19925:             &mt('An error occurred: [_1]',$putresult).'</span>';
19926:     }
19927:     return $resulttext;
19928: }
19929: 
19930: sub modify_wafproxy {
19931:     my ($dom,$action,$lastactref,%domconfig) = @_;
19932:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
19933:     my (%othercontrol,%canset,%values,%curralias,%currvalue,@warnings,%wafproxy,
19934:         %changes,%expirecache);
19935:     foreach my $server (sort(keys(%servers))) {
19936:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
19937:         if ($serverhome eq $server) {
19938:             my $serverdom = &Apache::lonnet::host_domain($server);
19939:             if ($serverdom eq $dom) {
19940:                 $canset{$server} = 1;
19941:             }
19942:         }
19943:     }
19944:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
19945:         %{$values{$dom}} = ();
19946:         if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
19947:             %curralias = %{$domconfig{'wafproxy'}{'alias'}};
19948:         }
19949:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
19950:             $currvalue{$item} = $domconfig{'wafproxy'}{$item};
19951:         }
19952:     }
19953:     my $output;
19954:     if (keys(%canset)) {
19955:         %{$wafproxy{'alias'}} = ();
19956:         foreach my $key (sort(keys(%canset))) {
19957:             if ($env{'form.wafproxy_'.$dom}) {
19958:                 $wafproxy{'alias'}{$key} = $env{'form.wafproxy_alias_'.$key};
19959:                 $wafproxy{'alias'}{$key} =~ s/^\s+|\s+$//g;
19960:                 if ($wafproxy{'alias'}{$key} ne $curralias{$key}) {
19961:                     $changes{'alias'} = 1;
19962:                 }
19963:             } else {
19964:                 $wafproxy{'alias'}{$key} = '';
19965:                 if ($curralias{$key}) {
19966:                     $changes{'alias'} = 1;
19967:                 }
19968:             }
19969:             if ($wafproxy{'alias'}{$key} eq '') {
19970:                 if ($curralias{$key}) {
19971:                     $expirecache{$key} = 1;
19972:                 }
19973:                 delete($wafproxy{'alias'}{$key});
19974:             }
19975:         }
19976:         unless (keys(%{$wafproxy{'alias'}})) {
19977:             delete($wafproxy{'alias'});
19978:         }
19979:         # Localization for values in %warn occus in &mt() calls separately.
19980:         my %warn = (
19981:                      trusted => 'trusted IP range(s)',
19982:                      vpnint => 'internal IP range(s) for VPN sessions(s)',
19983:                      vpnext => 'IP range(s) for backend WAF connections',
19984:                    );
19985:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
19986:             my $possible = $env{'form.wafproxy_'.$item};
19987:             $possible =~ s/^\s+|\s+$//g;
19988:             if ($possible ne '') {
19989:                 if ($item eq 'remoteip') {
19990:                     if ($possible =~ /^[mhn]$/) {
19991:                         $wafproxy{$item} = $possible;
19992:                     }
19993:                 } elsif ($item eq 'ipheader') {
19994:                     if ($wafproxy{'remoteip'} eq 'h') {
19995:                         $wafproxy{$item} = $possible;
19996:                     }
19997:                 } elsif ($item eq 'sslopt') {
19998:                     if ($possible =~ /^0|1$/) {
19999:                         $wafproxy{$item} = $possible;
20000:                     }
20001:                 } else {
20002:                     my (@ok,$count);
20003:                     if (($item eq 'vpnint') || ($item eq 'vpnext')) {
20004:                         unless ($env{'form.wafproxy_vpnaccess'}) {
20005:                             $possible = '';
20006:                         }
20007:                     } elsif ($item eq 'trusted') {
20008:                         unless ($wafproxy{'remoteip'} eq 'h') {
20009:                             $possible = '';
20010:                         }
20011:                     }
20012:                     unless ($possible eq '') {
20013:                         $possible =~ s/[\r\n]+/\s/g;
20014:                         $possible =~ s/\s*-\s*/-/g;
20015:                         $possible =~ s/\s+/,/g;
20016:                     }
20017:                     $count = 0;
20018:                     if ($possible ne '') {
20019:                         foreach my $poss (split(/\,/,$possible)) {
20020:                             $count ++;
20021:                             if (&validate_ip_pattern($poss)) {
20022:                                 push(@ok,$poss);
20023:                             }
20024:                         }
20025:                         if (@ok) {
20026:                             $wafproxy{$item} = join(',',@ok);
20027:                         }
20028:                         my $diff = $count - scalar(@ok);
20029:                         if ($diff) {
20030:                             push(@warnings,'<li>'.
20031:                                  &mt('[quant,_1,IP] invalid and excluded from saved value for [_2]',
20032:                                      $diff,$warn{$item}).
20033:                                  '</li>');
20034:                         }
20035:                     }
20036:                 }
20037:                 if ($wafproxy{$item} ne $currvalue{$item}) {
20038:                     $changes{$item} = 1;
20039:                 }
20040:             } elsif ($currvalue{$item}) {
20041:                 $changes{$item} = 1;
20042:             } 
20043:         }
20044:     } else {
20045:         if (keys(%curralias)) {
20046:             $changes{'alias'} = 1;
20047:         } 
20048:         if (keys(%currvalue)) {
20049:             foreach my $key (keys(%currvalue)) {
20050:                 $changes{$key} = 1;
20051:             }
20052:         }
20053:     }
20054:     if (keys(%changes)) {
20055:         my %defaultshash = (
20056:                               wafproxy => \%wafproxy,
20057:                            ); 
20058:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
20059:                                                  $dom);
20060:         if ($putresult eq 'ok') {
20061:             my $cachetime = 24*60*60;
20062:             my (%domdefaults,$updatedomdefs);
20063:             foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
20064:                 if ($changes{$item}) {
20065:                     unless ($updatedomdefs) {
20066:                         %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
20067:                         $updatedomdefs = 1;
20068:                     }
20069:                     if ($wafproxy{$item}) {
20070:                         $domdefaults{'waf_'.$item} = $wafproxy{$item};
20071:                     } elsif (exists($domdefaults{'waf_'.$item})) {
20072:                         delete($domdefaults{'waf_'.$item});
20073:                     } 
20074:                 }
20075:             }
20076:             if ($updatedomdefs) {
20077:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20078:                 if (ref($lastactref) eq 'HASH') {
20079:                     $lastactref->{'domdefaults'} = 1;
20080:                 }
20081:             }
20082:             if ((exists($wafproxy{'alias'})) || (keys(%expirecache))) {
20083:                 my %updates = %expirecache;
20084:                 foreach my $key (keys(%expirecache)) {
20085:                     &Apache::lonnet::devalidate_cache_new('proxyalias',$key);
20086:                 }
20087:                 if (ref($wafproxy{'alias'}) eq 'HASH') {
20088:                     my $cachetime = 24*60*60;
20089:                     foreach my $key (keys(%{$wafproxy{'alias'}})) {
20090:                         $updates{$key} = 1;
20091:                         &Apache::lonnet::do_cache_new('proxyalias',$key,$wafproxy{'alias'}{$key},
20092:                                                       $cachetime);
20093:                     }
20094:                 }
20095:                 if (ref($lastactref) eq 'HASH') {
20096:                     $lastactref->{'proxyalias'} = \%updates;
20097:                 }
20098:             }
20099:             $output = &mt('Changes were made to Web Application Firewall/Reverse Proxy').'<ul>';
20100:             foreach my $item ('alias','remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
20101:                 if ($changes{$item}) {
20102:                     if ($item eq 'alias') {
20103:                         my $numaliased = 0;
20104:                         if (ref($wafproxy{'alias'}) eq 'HASH') {
20105:                             my $shown;
20106:                             if (keys(%{$wafproxy{'alias'}})) {
20107:                                 foreach my $server (sort(keys(%{$wafproxy{'alias'}}))) {
20108:                                     $shown .= '<li>'.&mt('[_1] aliased by [_2]',
20109:                                                          &Apache::lonnet::hostname($server),
20110:                                                          $wafproxy{'alias'}{$server}).'</li>';
20111:                                     $numaliased ++;
20112:                                 }
20113:                                 if ($numaliased) {
20114:                                     $output .= '<li>'.&mt('Aliases for hostnames set to: [_1]',
20115:                                                           '<ul>'.$shown.'</ul>').'</li>';
20116:                                 }
20117:                             }
20118:                         }
20119:                         unless ($numaliased) {
20120:                             $output .= '<li>'.&mt('Aliases deleted for hostnames').'</li>';
20121:                         }
20122:                     } else {
20123:                         if ($item eq 'remoteip') {
20124:                             my %ip_methods = &remoteip_methods();
20125:                             if ($wafproxy{$item} =~ /^[mh]$/) {
20126:                                 $output .= '<li>'.&mt("Method for determining user's IP set to: [_1]",
20127:                                                       $ip_methods{$wafproxy{$item}}).'</li>';
20128:                             } else {
20129:                                 if (($env{'form.wafproxy_'.$dom}) && (ref($wafproxy{'alias'}) eq 'HASH')) {
20130:                                     $output .= '<li>'.&mt("No method in use to get user's real IP (will report IP used by WAF).").
20131:                                                '</li>';
20132:                                 } else {
20133:                                     $output .= '<li>'.&mt('WAF/Reverse Proxy not in use').'</li>';
20134:                                 }
20135:                             }
20136:                         } elsif ($item eq 'ipheader') {
20137:                             if ($wafproxy{$item}) {
20138:                                 $output .= '<li>'.&mt('Request header with remote IP set to: [_1]',
20139:                                                       $wafproxy{$item}).'</li>';
20140:                             } else {
20141:                                 $output .= '<li>'.&mt('Request header with remote IP deleted').'</li>';
20142:                             }
20143:                         } elsif ($item eq 'trusted') {
20144:                             if ($wafproxy{$item}) {
20145:                                 $output .= '<li>'.&mt('Trusted IP range(s) set to: [_1]',
20146:                                                       $wafproxy{$item}).'</li>';
20147:                             } else {
20148:                                 $output .= '<li>'.&mt('Trusted IP range(s) deleted').'</li>';
20149:                             }
20150:                         } elsif ($item eq 'vpnint') {
20151:                             if ($wafproxy{$item}) {
20152:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions set to: [_1]',
20153:                                                        $wafproxy{$item}).'</li>';
20154:                             } else {
20155:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions deleted').'</li>';
20156:                             }
20157:                         } elsif ($item eq 'vpnext') {
20158:                             if ($wafproxy{$item}) {
20159:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections set to: [_1]',
20160:                                                        $wafproxy{$item}).'</li>';
20161:                             } else {
20162:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections deleted').'</li>';
20163:                             }
20164:                         } elsif ($item eq 'sslopt') {
20165:                             if ($wafproxy{$item}) {
20166:                                 $output .= '<li>'.&mt('WAF/Reverse Proxy expected to forward requests to https on LON-CAPA node, regardless of original protocol in web browser (http or https).').'</li>';
20167:                             } else {
20168:                                 $output .= '<li>'.&mt('WAF/Reverse Proxy expected to preserve original protocol in web browser (either http or https) when forwarding to LON-CAPA node.').'</li>';
20169:                             }
20170:                         }
20171:                     }
20172:                 }
20173:             }
20174:         } else {
20175:             $output = '<span class="LC_error">'.
20176:                       &mt('An error occurred: [_1]',$putresult).'</span>';
20177:         }
20178:     } elsif (keys(%canset)) {
20179:         $output = &mt('No changes made to Web Application Firewall/Reverse Proxy settings');
20180:     }
20181:     if (@warnings) {
20182:         $output .= '<br />'.&mt('Warnings:').'<ul>'.
20183:                        join("\n",@warnings).'</ul>';
20184:     }
20185:     return $output;
20186: }
20187: 
20188: sub validate_ip_pattern {
20189:     my ($pattern) = @_;
20190:     if ($pattern =~ /^([^-]+)\-([^-]+)$/) {
20191:         my ($start,$end) = ($1,$2);
20192:         if ((&Net::CIDR::cidrvalidate($start)) && (&Net::CIDR::cidrvalidate($end))) {
20193:             return 1;
20194:         }
20195:     } elsif (&Net::CIDR::cidrvalidate($pattern)) {
20196:         return 1;
20197:     }
20198:     return
20199: }
20200: 
20201: sub modify_usersessions {
20202:     my ($dom,$lastactref,%domconfig) = @_;
20203:     my @hostingtypes = ('version','excludedomain','includedomain');
20204:     my @offloadtypes = ('primary','default');
20205:     my %types = (
20206:                   remote => \@hostingtypes,
20207:                   hosted => \@hostingtypes,
20208:                   spares => \@offloadtypes,
20209:                 );
20210:     my @prefixes = ('remote','hosted','spares');
20211:     my @lcversions = &Apache::lonnet::all_loncaparevs();
20212:     my (%by_ip,%by_location,@intdoms,@instdoms);
20213:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
20214:     my @locations = sort(keys(%by_location));
20215:     my (%defaultshash,%changes);
20216:     foreach my $prefix (@prefixes) {
20217:         $defaultshash{'usersessions'}{$prefix} = {};
20218:     }
20219:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20220:     my $resulttext;
20221:     my %iphost = &Apache::lonnet::get_iphost();
20222:     foreach my $prefix (@prefixes) {
20223:         next if ($prefix eq 'spares');
20224:         foreach my $type (@{$types{$prefix}}) {
20225:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
20226:             if ($type eq 'version') {
20227:                 my $value = $env{'form.'.$prefix.'_'.$type};
20228:                 my $okvalue;
20229:                 if ($value ne '') {
20230:                     if (grep(/^\Q$value\E$/,@lcversions)) {
20231:                         $okvalue = $value;
20232:                     }
20233:                 }
20234:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
20235:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
20236:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
20237:                             if ($inuse == 0) {
20238:                                 $changes{$prefix}{$type} = 1;
20239:                             } else {
20240:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
20241:                                     $changes{$prefix}{$type} = 1;
20242:                                 }
20243:                                 if ($okvalue ne '') {
20244:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
20245:                                 } 
20246:                             }
20247:                         } else {
20248:                             if (($inuse == 1) && ($okvalue ne '')) {
20249:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
20250:                                 $changes{$prefix}{$type} = 1;
20251:                             }
20252:                         }
20253:                     } else {
20254:                         if (($inuse == 1) && ($okvalue ne '')) {
20255:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
20256:                             $changes{$prefix}{$type} = 1;
20257:                         }
20258:                     }
20259:                 } else {
20260:                     if (($inuse == 1) && ($okvalue ne '')) {
20261:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
20262:                         $changes{$prefix}{$type} = 1;
20263:                     }
20264:                 }
20265:             } else {
20266:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
20267:                 my @okvals;
20268:                 foreach my $val (@vals) {
20269:                     if ($val =~ /:/) {
20270:                         my @items = split(/:/,$val);
20271:                         foreach my $item (@items) {
20272:                             if (ref($by_location{$item}) eq 'ARRAY') {
20273:                                 push(@okvals,$item);
20274:                             }
20275:                         }
20276:                     } else {
20277:                         if (ref($by_location{$val}) eq 'ARRAY') {
20278:                             push(@okvals,$val);
20279:                         }
20280:                     }
20281:                 }
20282:                 @okvals = sort(@okvals);
20283:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
20284:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
20285:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
20286:                             if ($inuse == 0) {
20287:                                 $changes{$prefix}{$type} = 1; 
20288:                             } else {
20289:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
20290:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
20291:                                 if (@changed > 0) {
20292:                                     $changes{$prefix}{$type} = 1;
20293:                                 }
20294:                             }
20295:                         } else {
20296:                             if ($inuse == 1) {
20297:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
20298:                                 $changes{$prefix}{$type} = 1;
20299:                             }
20300:                         } 
20301:                     } else {
20302:                         if ($inuse == 1) {
20303:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
20304:                             $changes{$prefix}{$type} = 1;
20305:                         }
20306:                     }
20307:                 } else {
20308:                     if ($inuse == 1) {
20309:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
20310:                         $changes{$prefix}{$type} = 1;
20311:                     }
20312:                 }
20313:             }
20314:         }
20315:     }
20316: 
20317:     my @alldoms = &Apache::lonnet::all_domains();
20318:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
20319:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
20320:     my $savespares;
20321: 
20322:     foreach my $lonhost (sort(keys(%servers))) {
20323:         my $serverhomeID =
20324:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
20325:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
20326:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
20327:         my %spareschg;
20328:         foreach my $type (@{$types{'spares'}}) {
20329:             my @okspares;
20330:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
20331:             foreach my $server (@checked) {
20332:                 if (&Apache::lonnet::hostname($server) ne '') {
20333:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
20334:                         unless (grep(/^\Q$server\E$/,@okspares)) {
20335:                             push(@okspares,$server);
20336:                         }
20337:                     }
20338:                 }
20339:             }
20340:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
20341:             my $newspare;
20342:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
20343:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
20344:                     $newspare = $new;
20345:                 }
20346:             }
20347:             my @spares;
20348:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
20349:                 @spares = sort(@okspares,$newspare);
20350:             } else {
20351:                 @spares = sort(@okspares);
20352:             }
20353:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
20354:             if (ref($spareid{$lonhost}) eq 'HASH') {
20355:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
20356:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
20357:                     if (@diffs > 0) {
20358:                         $spareschg{$type} = 1;
20359:                     }
20360:                 }
20361:             }
20362:         }
20363:         if (keys(%spareschg) > 0) {
20364:             $changes{'spares'}{$lonhost} = \%spareschg;
20365:         }
20366:     }
20367:     $defaultshash{'usersessions'}{'offloadnow'} = {};
20368:     $defaultshash{'usersessions'}{'offloadoth'} = {};
20369:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
20370:     my @okoffload;
20371:     if (@offloadnow) {
20372:         foreach my $server (@offloadnow) {
20373:             if (&Apache::lonnet::hostname($server) ne '') {
20374:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
20375:                     push(@okoffload,$server);
20376:                 }
20377:             }
20378:         }
20379:         if (@okoffload) {
20380:             foreach my $lonhost (@okoffload) {
20381:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
20382:             }
20383:         }
20384:     }
20385:     my @offloadoth = &Apache::loncommon::get_env_multiple('form.offloadoth');
20386:     my @okoffloadoth;
20387:     if (@offloadoth) {
20388:         foreach my $server (@offloadoth) {
20389:             if (&Apache::lonnet::hostname($server) ne '') {
20390:                 unless (grep(/^\Q$server\E$/,@okoffloadoth)) {
20391:                     push(@okoffloadoth,$server);
20392:                 }
20393:             }
20394:         }
20395:         if (@okoffloadoth) {
20396:             foreach my $lonhost (@okoffloadoth) {
20397:                 $defaultshash{'usersessions'}{'offloadoth'}{$lonhost} = 1;
20398:             }
20399:         }
20400:     }
20401:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
20402:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
20403:             if (ref($changes{'spares'}) eq 'HASH') {
20404:                 if (keys(%{$changes{'spares'}}) > 0) {
20405:                     $savespares = 1;
20406:                 }
20407:             }
20408:         } else {
20409:             $savespares = 1;
20410:         }
20411:         foreach my $offload ('offloadnow','offloadoth') {
20412:             if (ref($domconfig{'usersessions'}{$offload}) eq 'HASH') {
20413:                 foreach my $lonhost (keys(%{$domconfig{'usersessions'}{$offload}})) {
20414:                     unless ($defaultshash{'usersessions'}{$offload}{$lonhost}) {
20415:                         $changes{$offload} = 1;
20416:                         last;
20417:                     }
20418:                 }
20419:                 unless ($changes{$offload}) {
20420:                     foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{$offload}})) {
20421:                         unless ($domconfig{'usersessions'}{$offload}{$lonhost}) {
20422:                             $changes{$offload} = 1;
20423:                             last;
20424:                         }
20425:                     }
20426:                 }
20427:             } else {
20428:                 if (($offload eq 'offloadnow') && (@okoffload)) {
20429:                      $changes{'offloadnow'} = 1;
20430:                 }
20431:                 if (($offload eq 'offloadoth') && (@okoffloadoth)) {
20432:                     $changes{'offloadoth'} = 1;
20433:                 }
20434:             } 
20435:         }
20436:     } else {
20437:         if (@okoffload) {
20438:             $changes{'offloadnow'} = 1;
20439:         }
20440:         if (@okoffloadoth) {
20441:             $changes{'offloadoth'} = 1;
20442:         }
20443:     }
20444:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
20445:     if ((keys(%changes) > 0) || ($savespares)) {
20446:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
20447:                                                  $dom);
20448:         if ($putresult eq 'ok') {
20449:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
20450:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
20451:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
20452:                 }
20453:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
20454:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
20455:                 }
20456:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
20457:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
20458:                 }
20459:                 if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
20460:                     $domdefaults{'offloadoth'} = $defaultshash{'usersessions'}{'offloadoth'};
20461:                 }
20462:             }
20463:             my $cachetime = 24*60*60;
20464:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20465:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
20466:             if (ref($lastactref) eq 'HASH') {
20467:                 $lastactref->{'domdefaults'} = 1;
20468:                 $lastactref->{'usersessions'} = 1;
20469:             }
20470:             if (keys(%changes) > 0) {
20471:                 my %lt = &usersession_titles();
20472:                 $resulttext = &mt('Changes made:').'<ul>';
20473:                 foreach my $prefix (@prefixes) {
20474:                     if (ref($changes{$prefix}) eq 'HASH') {
20475:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
20476:                         if ($prefix eq 'spares') {
20477:                             if (ref($changes{$prefix}) eq 'HASH') {
20478:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
20479:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
20480:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
20481:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
20482:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
20483:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
20484:                                         foreach my $type (@{$types{$prefix}}) {
20485:                                             if ($changes{$prefix}{$lonhost}{$type}) {
20486:                                                 my $offloadto = &mt('None');
20487:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
20488:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
20489:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
20490:                                                     }
20491:                                                 }
20492:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
20493:                                             }
20494:                                         }
20495:                                     }
20496:                                     $resulttext .= '</li>';
20497:                                 }
20498:                             }
20499:                         } else {
20500:                             foreach my $type (@{$types{$prefix}}) {
20501:                                 if (defined($changes{$prefix}{$type})) {
20502:                                     my ($newvalue,$notinuse);
20503:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
20504:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
20505:                                             if ($type eq 'version') {
20506:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
20507:                                             } else {
20508:                                                 if (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
20509:                                                     if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
20510:                                                         $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
20511:                                                     }
20512:                                                 } else {
20513:                                                     $notinuse = 1;
20514:                                                 }
20515:                                             }
20516:                                         }
20517:                                     }
20518:                                     if ($newvalue eq '') {
20519:                                         if ($type eq 'version') {
20520:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
20521:                                         } elsif ($notinuse) {
20522:                                             $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
20523:                                         } else {
20524:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
20525:                                         }
20526:                                     } else {
20527:                                         if ($type eq 'version') {
20528:                                             $newvalue .= ' '.&mt('(or later)');
20529:                                         }
20530:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
20531:                                     }
20532:                                 }
20533:                             }
20534:                         }
20535:                         $resulttext .= '</ul>';
20536:                     }
20537:                 }
20538:                 if ($changes{'offloadnow'}) {
20539:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
20540:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
20541:                             $resulttext .= '<li>'.&mt('Switch any active user on next access, for server(s):').'<ul>';
20542:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
20543:                                 $resulttext .= '<li>'.$lonhost.'</li>';
20544:                             }
20545:                             $resulttext .= '</ul>';
20546:                         } else {
20547:                             $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.');
20548:                         }
20549:                     } else {
20550:                         $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.').'</li>';
20551:                     }
20552:                 }
20553:                 if ($changes{'offloadoth'}) {
20554:                     if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
20555:                         if (keys(%{$defaultshash{'usersessions'}{'offloadoth'}}) > 0) {
20556:                             $resulttext .= '<li>'.&mt('Switch other institutions on next access, for server(s):').'<ul>';
20557:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadoth'}}))) {
20558:                                 $resulttext .= '<li>'.$lonhost.'</li>';
20559:                             }
20560:                             $resulttext .= '</ul>';
20561:                         } else {
20562:                             $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.');
20563:                         }
20564:                     } else {
20565:                         $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.').'</li>';
20566:                     }
20567:                 }
20568:                 $resulttext .= '</ul>';
20569:             } else {
20570:                 $resulttext = $nochgmsg;
20571:             }
20572:         } else {
20573:             $resulttext = '<span class="LC_error">'.
20574:                           &mt('An error occurred: [_1]',$putresult).'</span>';
20575:         }
20576:     } else {
20577:         $resulttext = $nochgmsg;
20578:     }
20579:     return $resulttext;
20580: }
20581: 
20582: sub modify_ssl {
20583:     my ($dom,$lastactref,%domconfig) = @_;
20584:     my (%by_ip,%by_location,@intdoms,@instdoms);
20585:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
20586:     my @locations = sort(keys(%by_location));
20587:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
20588:     my (%defaultshash,%changes);
20589:     my $action = 'ssl';
20590:     my @prefixes = ('connto','connfrom','replication');
20591:     foreach my $prefix (@prefixes) {
20592:         $defaultshash{$action}{$prefix} = {};
20593:     }
20594:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20595:     my $resulttext;
20596:     my %iphost = &Apache::lonnet::get_iphost();
20597:     my @reptypes = ('certreq','nocertreq');
20598:     my @connecttypes = ('dom','intdom','other');
20599:     my %types = (
20600:                   connto      => \@connecttypes,
20601:                   connfrom    => \@connecttypes,
20602:                   replication => \@reptypes,
20603:                 );
20604:     foreach my $prefix (sort(keys(%types))) {
20605:         foreach my $type (@{$types{$prefix}}) {
20606:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
20607:                 my $value = 'yes';
20608:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
20609:                     $value = $env{'form.'.$prefix.'_'.$type};
20610:                 }
20611:                 if (ref($domconfig{$action}) eq 'HASH') {
20612:                     if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
20613:                         if ($domconfig{$action}{$prefix}{$type} ne '') {
20614:                             if ($value ne $domconfig{$action}{$prefix}{$type}) {
20615:                                 $changes{$prefix}{$type} = 1;
20616:                             }
20617:                             $defaultshash{$action}{$prefix}{$type} = $value;
20618:                         } else {
20619:                             $defaultshash{$action}{$prefix}{$type} = $value;
20620:                             $changes{$prefix}{$type} = 1;
20621:                         }
20622:                     } else {
20623:                         $defaultshash{$action}{$prefix}{$type} = $value;
20624:                         $changes{$prefix}{$type} = 1;
20625:                     }
20626:                 } else {
20627:                     $defaultshash{$action}{$prefix}{$type} = $value;
20628:                     $changes{$prefix}{$type} = 1;
20629:                 }
20630:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
20631:                     delete($changes{$prefix}{$type});
20632:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
20633:                     delete($changes{$prefix}{$type});
20634:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
20635:                     delete($changes{$prefix}{$type});
20636:                 }
20637:             } elsif ($prefix eq 'replication') {
20638:                 if (@locations > 0) {
20639:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
20640:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
20641:                     my @okvals;
20642:                     foreach my $val (@vals) {
20643:                         if ($val =~ /:/) {
20644:                             my @items = split(/:/,$val);
20645:                             foreach my $item (@items) {
20646:                                 if (ref($by_location{$item}) eq 'ARRAY') {
20647:                                     push(@okvals,$item);
20648:                                 }
20649:                             }
20650:                         } else {
20651:                             if (ref($by_location{$val}) eq 'ARRAY') {
20652:                                 push(@okvals,$val);
20653:                             }
20654:                         }
20655:                     }
20656:                     @okvals = sort(@okvals);
20657:                     if (ref($domconfig{$action}) eq 'HASH') {
20658:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
20659:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
20660:                                 if ($inuse == 0) {
20661:                                     $changes{$prefix}{$type} = 1;
20662:                                 } else {
20663:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
20664:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
20665:                                     if (@changed > 0) {
20666:                                         $changes{$prefix}{$type} = 1;
20667:                                     }
20668:                                 }
20669:                             } else {
20670:                                 if ($inuse == 1) {
20671:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
20672:                                     $changes{$prefix}{$type} = 1;
20673:                                 }
20674:                             }
20675:                         } else {
20676:                             if ($inuse == 1) {
20677:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
20678:                                 $changes{$prefix}{$type} = 1;
20679:                             }
20680:                         }
20681:                     } else {
20682:                         if ($inuse == 1) {
20683:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
20684:                             $changes{$prefix}{$type} = 1;
20685:                         }
20686:                     }
20687:                 }
20688:             }
20689:         }
20690:     }
20691:     if (keys(%changes)) {
20692:         foreach my $prefix (keys(%changes)) {
20693:             if (ref($changes{$prefix}) eq 'HASH') {
20694:                 if (scalar(keys(%{$changes{$prefix}})) == 0) {
20695:                     delete($changes{$prefix});
20696:                 }
20697:             } else {
20698:                 delete($changes{$prefix});
20699:             }
20700:         }
20701:     }
20702:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
20703:     if (keys(%changes) > 0) {
20704:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
20705:                                                  $dom);
20706:         if ($putresult eq 'ok') {
20707:             if (ref($defaultshash{$action}) eq 'HASH') {
20708:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
20709:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
20710:                 }
20711:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
20712:                     $domdefaults{'connto'} = $defaultshash{$action}{'connto'};
20713:                 }
20714:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
20715:                     $domdefaults{'connfrom'} = $defaultshash{$action}{'connfrom'};
20716:                 }
20717:             }
20718:             my $cachetime = 24*60*60;
20719:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20720:             if (ref($lastactref) eq 'HASH') {
20721:                 $lastactref->{'domdefaults'} = 1;
20722:             }
20723:             if (keys(%changes) > 0) {
20724:                 my %titles = &ssl_titles();
20725:                 $resulttext = &mt('Changes made:').'<ul>';
20726:                 foreach my $prefix (@prefixes) {
20727:                     if (ref($changes{$prefix}) eq 'HASH') {
20728:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
20729:                         foreach my $type (@{$types{$prefix}}) {
20730:                             if (defined($changes{$prefix}{$type})) {
20731:                                 my ($newvalue,$notinuse);
20732:                                 if (ref($defaultshash{$action}) eq 'HASH') {
20733:                                     if (ref($defaultshash{$action}{$prefix})) {
20734:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
20735:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
20736:                                         } else {
20737:                                             if (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
20738:                                                 if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
20739:                                                     $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
20740:                                                 }
20741:                                             } else {
20742:                                                 $notinuse = 1;
20743:                                             }
20744:                                         }
20745:                                     }
20746:                                     if ($notinuse) {
20747:                                         $resulttext .= '<li>'.&mt('[_1] set to: not in use',$titles{$type}).'</li>';
20748:                                     } elsif ($newvalue eq '') {
20749:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
20750:                                     } else {
20751:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
20752:                                     }
20753:                                 }
20754:                             }
20755:                         }
20756:                         $resulttext .= '</ul>';
20757:                     }
20758:                 }
20759:             } else {
20760:                 $resulttext = $nochgmsg;
20761:             }
20762:         } else {
20763:             $resulttext = '<span class="LC_error">'.
20764:                           &mt('An error occurred: [_1]',$putresult).'</span>';
20765:         }
20766:     } else {
20767:         $resulttext = $nochgmsg;
20768:     }
20769:     return $resulttext;
20770: }
20771: 
20772: sub modify_trust {
20773:     my ($dom,$lastactref,%domconfig) = @_;
20774:     my (%by_ip,%by_location,@intdoms,@instdoms);
20775:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
20776:     my @locations = sort(keys(%by_location));
20777:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
20778:     my @types = ('exc','inc');
20779:     my (%defaultshash,%changes);
20780:     foreach my $prefix (@prefixes) {
20781:         $defaultshash{'trust'}{$prefix} = {};
20782:     }
20783:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20784:     my $resulttext;
20785:     foreach my $prefix (@prefixes) {
20786:         foreach my $type (@types) {
20787:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
20788:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
20789:             my @okvals;
20790:             foreach my $val (@vals) {
20791:                 if ($val =~ /:/) {
20792:                     my @items = split(/:/,$val);
20793:                     foreach my $item (@items) {
20794:                         if (ref($by_location{$item}) eq 'ARRAY') {
20795:                             push(@okvals,$item);
20796:                         }
20797:                     }
20798:                 } else {
20799:                     if (ref($by_location{$val}) eq 'ARRAY') {
20800:                         push(@okvals,$val);
20801:                     }
20802:                 }
20803:             }
20804:             @okvals = sort(@okvals);
20805:             if (ref($domconfig{'trust'}) eq 'HASH') {
20806:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
20807:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
20808:                         if ($inuse == 0) {
20809:                             $changes{$prefix}{$type} = 1;
20810:                         } else {
20811:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
20812:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
20813:                             if (@changed > 0) {
20814:                                 $changes{$prefix}{$type} = 1;
20815:                             }
20816:                         }
20817:                     } else {
20818:                         if ($inuse == 1) {
20819:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
20820:                             $changes{$prefix}{$type} = 1;
20821:                         }
20822:                     }
20823:                 } else {
20824:                     if ($inuse == 1) {
20825:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
20826:                         $changes{$prefix}{$type} = 1;
20827:                     }
20828:                 }
20829:             } else {
20830:                 if ($inuse == 1) {
20831:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
20832:                     $changes{$prefix}{$type} = 1;
20833:                 }
20834:             }
20835:         }
20836:     }
20837:     my $nochgmsg = &mt('No changes made to trust settings.');
20838:     if (keys(%changes) > 0) {
20839:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
20840:                                                  $dom);
20841:         if ($putresult eq 'ok') {
20842:             if (ref($defaultshash{'trust'}) eq 'HASH') {
20843:                 foreach my $prefix (@prefixes) {
20844:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
20845:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
20846:                     }
20847:                 }
20848:             }
20849:             my $cachetime = 24*60*60;
20850:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20851:             if (ref($lastactref) eq 'HASH') {
20852:                 $lastactref->{'domdefaults'} = 1;
20853:             }
20854:             if (keys(%changes) > 0) {
20855:                 my %lt = &trust_titles();
20856:                 $resulttext = &mt('Changes made:').'<ul>';
20857:                 foreach my $prefix (@prefixes) {
20858:                     if (ref($changes{$prefix}) eq 'HASH') {
20859:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
20860:                         foreach my $type (@types) {
20861:                             if (defined($changes{$prefix}{$type})) {
20862:                                 my ($newvalue,$notinuse);
20863:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
20864:                                     if (ref($defaultshash{'trust'}{$prefix})) {
20865:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
20866:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
20867:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
20868:                                             }
20869:                                         } else {
20870:                                             $notinuse = 1;
20871:                                         }
20872:                                     }
20873:                                 }
20874:                                 if ($notinuse) {
20875:                                     $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
20876:                                 } elsif ($newvalue eq '') {
20877:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
20878:                                 } else {
20879:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
20880:                                 }
20881:                             }
20882:                         }
20883:                         $resulttext .= '</ul>';
20884:                     }
20885:                 }
20886:                 $resulttext .= '</ul>';
20887:             } else {
20888:                 $resulttext = $nochgmsg;
20889:             }
20890:         } else {
20891:             $resulttext = '<span class="LC_error">'.
20892:                           &mt('An error occurred: [_1]',$putresult).'</span>';
20893:         }
20894:     } else {
20895:         $resulttext = $nochgmsg;
20896:     }
20897:     return $resulttext;
20898: }
20899: 
20900: sub modify_loadbalancing {
20901:     my ($dom,%domconfig) = @_;
20902:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
20903:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
20904:     my ($othertitle,$usertypes,$types) =
20905:         &Apache::loncommon::sorted_inst_types($dom);
20906:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
20907:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
20908:     my @sparestypes = ('primary','default');
20909:     my %typetitles = &sparestype_titles();
20910:     my $resulttext;
20911:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
20912:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
20913:         %existing = %{$domconfig{'loadbalancing'}};
20914:     }
20915:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
20916:                               \%currtargets,\%currrules,\%currcookies);
20917:     my ($saveloadbalancing,%defaultshash,%changes);
20918:     my ($alltypes,$othertypes,$titles) =
20919:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
20920:     my %ruletitles = &offloadtype_text();
20921:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
20922:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
20923:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
20924:         if ($balancer eq '') {
20925:             next;
20926:         }
20927:         if (!exists($servers{$balancer})) {
20928:             if (exists($currbalancer{$balancer})) {
20929:                 push(@{$changes{'delete'}},$balancer);
20930:             }
20931:             next;
20932:         }
20933:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
20934:             push(@{$changes{'delete'}},$balancer);
20935:             next;
20936:         }
20937:         if (!exists($currbalancer{$balancer})) {
20938:             push(@{$changes{'add'}},$balancer);
20939:         }
20940:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
20941:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
20942:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
20943:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
20944:             $saveloadbalancing = 1;
20945:         }
20946:         foreach my $sparetype (@sparestypes) {
20947:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
20948:             my @offloadto;
20949:             foreach my $target (@targets) {
20950:                 if (($servers{$target}) && ($target ne $balancer)) {
20951:                     if ($sparetype eq 'default') {
20952:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
20953:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
20954:                         }
20955:                     }
20956:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
20957:                         push(@offloadto,$target);
20958:                     }
20959:                 }
20960:             }
20961:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
20962:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
20963:                     push(@offloadto,$balancer);
20964:                 }
20965:             }
20966:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
20967:         }
20968:         if ($env{'form.loadbalancing_cookie_'.$i}) {
20969:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
20970:             if (exists($currbalancer{$balancer})) { 
20971:                 unless ($currcookies{$balancer}) {
20972:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
20973:                 }
20974:             }
20975:         } elsif (exists($currbalancer{$balancer})) {
20976:             if ($currcookies{$balancer}) {
20977:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
20978:             }
20979:         }
20980:         if (ref($currtargets{$balancer}) eq 'HASH') {
20981:             foreach my $sparetype (@sparestypes) {
20982:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
20983:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
20984:                     if (@targetdiffs > 0) {
20985:                         $changes{'curr'}{$balancer}{'targets'} = 1;
20986:                     }
20987:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
20988:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
20989:                         $changes{'curr'}{$balancer}{'targets'} = 1;
20990:                     }
20991:                 }
20992:             }
20993:         } else {
20994:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
20995:                 foreach my $sparetype (@sparestypes) {
20996:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
20997:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
20998:                             $changes{'curr'}{$balancer}{'targets'} = 1;
20999:                         }
21000:                     }
21001:                 }
21002:             }
21003:         }
21004:         my $ishomedom;
21005:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
21006:             $ishomedom = 1;
21007:         }
21008:         if (ref($alltypes) eq 'ARRAY') {
21009:             foreach my $type (@{$alltypes}) {
21010:                 my $rule;
21011:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
21012:                          (!$ishomedom)) {
21013:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
21014:                 }
21015:                 if ($rule eq 'specific') {
21016:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
21017:                     if (exists($servers{$specifiedhost})) {
21018:                         $rule = $specifiedhost;
21019:                     }
21020:                 }
21021:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
21022:                 if (ref($currrules{$balancer}) eq 'HASH') {
21023:                     if ($rule ne $currrules{$balancer}{$type}) {
21024:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
21025:                     }
21026:                 } elsif ($rule ne '') {
21027:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
21028:                 }
21029:             }
21030:         }
21031:     }
21032:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
21033:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
21034:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
21035:             $defaultshash{'loadbalancing'} = {};
21036:         }
21037:         my $putresult = &Apache::lonnet::put_dom('configuration',
21038:                                                  \%defaultshash,$dom);
21039:         if ($putresult eq 'ok') {
21040:             if (keys(%changes) > 0) {
21041:                 my %toupdate;
21042:                 if (ref($changes{'delete'}) eq 'ARRAY') {
21043:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
21044:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
21045:                         $toupdate{$balancer} = 1;
21046:                     }
21047:                 }
21048:                 if (ref($changes{'add'}) eq 'ARRAY') {
21049:                     foreach my $balancer (sort(@{$changes{'add'}})) {
21050:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
21051:                         $toupdate{$balancer} = 1;
21052:                     }
21053:                 }
21054:                 if (ref($changes{'curr'}) eq 'HASH') {
21055:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
21056:                         $toupdate{$balancer} = 1;
21057:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
21058:                             if ($changes{'curr'}{$balancer}{'targets'}) {
21059:                                 my %offloadstr;
21060:                                 foreach my $sparetype (@sparestypes) {
21061:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
21062:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
21063:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
21064:                                         }
21065:                                     }
21066:                                 }
21067:                                 if (keys(%offloadstr) == 0) {
21068:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
21069:                                 } else {
21070:                                     my $showoffload;
21071:                                     foreach my $sparetype (@sparestypes) {
21072:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
21073:                                         if (defined($offloadstr{$sparetype})) {
21074:                                             $showoffload .= $offloadstr{$sparetype};
21075:                                         } else {
21076:                                             $showoffload .= &mt('None');
21077:                                         }
21078:                                         $showoffload .= ('&nbsp;'x3);
21079:                                     }
21080:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
21081:                                 }
21082:                             }
21083:                         }
21084:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
21085:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
21086:                                 foreach my $type (@{$alltypes}) {
21087:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
21088:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
21089:                                         my $balancetext;
21090:                                         if ($rule eq '') {
21091:                                             $balancetext =  $ruletitles{'default'};
21092:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
21093:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
21094:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
21095:                                                 foreach my $sparetype (@sparestypes) {
21096:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
21097:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
21098:                                                     }
21099:                                                 }
21100:                                                 foreach my $item (@{$alltypes}) {
21101:                                                     next if ($item =~  /^_LC_ipchange/);
21102:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
21103:                                                     if ($hasrule eq 'homeserver') {
21104:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
21105:                                                     } else {
21106:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
21107:                                                             if ($servers{$hasrule}) {
21108:                                                                 $toupdate{$hasrule} = 1;
21109:                                                             }
21110:                                                         }
21111:                                                     }
21112:                                                 }
21113:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
21114:                                                     $balancetext =  $ruletitles{$rule};
21115:                                                 } else {
21116:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
21117:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
21118:                                                     if ($receiver) {
21119:                                                         $toupdate{$receiver};
21120:                                                     }
21121:                                                 }
21122:                                             } else {
21123:                                                 $balancetext =  $ruletitles{$rule};
21124:                                             }
21125:                                         } else {
21126:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
21127:                                         }
21128:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
21129:                                     }
21130:                                 }
21131:                             }
21132:                         }
21133:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
21134:                             $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
21135:                                                       $balancer).'</li>'; 
21136:                         }
21137:                     }
21138:                 }
21139:                 if (keys(%toupdate)) {
21140:                     my %thismachine;
21141:                     my $updatedhere;
21142:                     my $cachetime = 60*60*24;
21143:                     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
21144:                     foreach my $lonhost (keys(%toupdate)) {
21145:                         if ($thismachine{$lonhost}) {
21146:                             unless ($updatedhere) {
21147:                                 &Apache::lonnet::do_cache_new('loadbalancing',$dom,
21148:                                                               $defaultshash{'loadbalancing'},
21149:                                                               $cachetime);
21150:                                 $updatedhere = 1;
21151:                             }
21152:                         } else {
21153:                             my $cachekey = &escape('loadbalancing').':'.&escape($dom);
21154:                             &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
21155:                         }
21156:                     }
21157:                 }
21158:                 if ($resulttext ne '') {
21159:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
21160:                 } else {
21161:                     $resulttext = $nochgmsg;
21162:                 }
21163:             } else {
21164:                 $resulttext = $nochgmsg;
21165:             }
21166:         } else {
21167:             $resulttext = '<span class="LC_error">'.
21168:                           &mt('An error occurred: [_1]',$putresult).'</span>';
21169:         }
21170:     } else {
21171:         $resulttext = $nochgmsg;
21172:     }
21173:     return $resulttext;
21174: }
21175: 
21176: sub recurse_check {
21177:     my ($chkcats,$categories,$depth,$name) = @_;
21178:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
21179:         my $chg = 0;
21180:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
21181:             my $category = $chkcats->[$depth]{$name}[$j];
21182:             my $item;
21183:             if ($category eq '') {
21184:                 $chg ++;
21185:             } else {
21186:                 my $deeper = $depth + 1;
21187:                 $item = &escape($category).':'.&escape($name).':'.$depth;
21188:                 if ($chg) {
21189:                     $categories->{$item} -= $chg;
21190:                 }
21191:                 &recurse_check($chkcats,$categories,$deeper,$category);
21192:                 $deeper --;
21193:             }
21194:         }
21195:     }
21196:     return;
21197: }
21198: 
21199: sub recurse_cat_deletes {
21200:     my ($item,$coursecategories,$deletions) = @_;
21201:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
21202:     my $subdepth = $depth + 1;
21203:     if (ref($coursecategories) eq 'HASH') {
21204:         foreach my $subitem (keys(%{$coursecategories})) {
21205:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
21206:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
21207:                 delete($coursecategories->{$subitem});
21208:                 $deletions->{$subitem} = 1;
21209:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
21210:             }
21211:         }
21212:     }
21213:     return;
21214: }
21215: 
21216: sub active_dc_picker {
21217:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
21218:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
21219:     my @domcoord = keys(%domcoords);
21220:     if (keys(%currhash)) {
21221:         foreach my $dc (keys(%currhash)) {
21222:             unless (exists($domcoords{$dc})) {
21223:                 push(@domcoord,$dc);
21224:             }
21225:         }
21226:     }
21227:     @domcoord = sort(@domcoord);
21228:     my $numdcs = scalar(@domcoord);
21229:     my $rows = 0;
21230:     my $table;
21231:     if ($numdcs > 1) {
21232:         $table = '<table>';
21233:         for (my $i=0; $i<@domcoord; $i++) {
21234:             my $rem = $i%($numinrow);
21235:             if ($rem == 0) {
21236:                 if ($i > 0) {
21237:                     $table .= '</tr>';
21238:                 }
21239:                 $table .= '<tr>';
21240:                 $rows ++;
21241:             }
21242:             my $check = '';
21243:             if ($inputtype eq 'radio') {
21244:                 if (keys(%currhash) == 0) {
21245:                     if (!$i) {
21246:                         $check = ' checked="checked"';
21247:                     }
21248:                 } elsif (exists($currhash{$domcoord[$i]})) {
21249:                     $check = ' checked="checked"';
21250:                 }
21251:             } else {
21252:                 if (exists($currhash{$domcoord[$i]})) {
21253:                     $check = ' checked="checked"';
21254:                 }
21255:             }
21256:             if ($i == @domcoord - 1) {
21257:                 my $colsleft = $numinrow - $rem;
21258:                 if ($colsleft > 1) {
21259:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
21260:                 } else {
21261:                     $table .= '<td class="LC_left_item">';
21262:                 }
21263:             } else {
21264:                 $table .= '<td class="LC_left_item">';
21265:             }
21266:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
21267:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
21268:             $table .= '<span class="LC_nobreak"><label>'.
21269:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
21270:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
21271:             if ($user ne $dcname.':'.$dcdom) {
21272:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
21273:             }
21274:             $table .= '</label></span></td>';
21275:         }
21276:         $table .= '</tr></table>';
21277:     } elsif ($numdcs == 1) {
21278:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
21279:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
21280:         if ($inputtype eq 'radio') {
21281:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
21282:             if ($user ne $dcname.':'.$dcdom) {
21283:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
21284:             }
21285:         } else {
21286:             my $check;
21287:             if (exists($currhash{$domcoord[0]})) {
21288:                 $check = ' checked="checked"';
21289:             }
21290:             $table = '<span class="LC_nobreak"><label>'.
21291:                      '<input type="checkbox" name="'.$name.'" '.
21292:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
21293:             if ($user ne $dcname.':'.$dcdom) {
21294:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
21295:             }
21296:             $table .= '</label></span>';
21297:             $rows ++;
21298:         }
21299:     }
21300:     return ($numdcs,$table,$rows);
21301: }
21302: 
21303: sub usersession_titles {
21304:     return &Apache::lonlocal::texthash(
21305:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
21306:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
21307:                spares => 'Servers offloaded to, when busy',
21308:                version => 'LON-CAPA version requirement',
21309:                excludedomain => 'Allow all, but exclude specific domains',
21310:                includedomain => 'Deny all, but include specific domains',
21311:                primary => 'Primary (checked first)',
21312:                default => 'Default',
21313:            );
21314: }
21315: 
21316: sub id_for_thisdom {
21317:     my (%servers) = @_;
21318:     my %altids;
21319:     foreach my $server (keys(%servers)) {
21320:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
21321:         if ($serverhome ne $server) {
21322:             $altids{$serverhome} = $server;
21323:         }
21324:     }
21325:     return %altids;
21326: }
21327: 
21328: sub count_servers {
21329:     my ($currbalancer,%servers) = @_;
21330:     my (@spares,$numspares);
21331:     foreach my $lonhost (sort(keys(%servers))) {
21332:         next if ($currbalancer eq $lonhost);
21333:         push(@spares,$lonhost);
21334:     }
21335:     if ($currbalancer) {
21336:         $numspares = scalar(@spares);
21337:     } else {
21338:         $numspares = scalar(@spares) - 1;
21339:     }
21340:     return ($numspares,@spares);
21341: }
21342: 
21343: sub lonbalance_targets_js {
21344:     my ($dom,$types,$servers,$settings) = @_;
21345:     my $select = &mt('Select');
21346:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
21347:     if (ref($servers) eq 'HASH') {
21348:         $alltargets = join("','",sort(keys(%{$servers})));
21349:         my @homedoms;
21350:         foreach my $server (sort(keys(%{$servers}))) {
21351:             if (&Apache::lonnet::host_domain($server) eq $dom) {
21352:                 push(@homedoms,'1');
21353:             } else {
21354:                 push(@homedoms,'0');
21355:             }
21356:         }
21357:         $allishome = join("','",@homedoms);
21358:     }
21359:     if (ref($types) eq 'ARRAY') {
21360:         if (@{$types} > 0) {
21361:             @alltypes = @{$types};
21362:         }
21363:     }
21364:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
21365:     $allinsttypes = join("','",@alltypes);
21366:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
21367:     if (ref($settings) eq 'HASH') {
21368:         %existing = %{$settings};
21369:     }
21370:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
21371:                               \%currtargets,\%currrules,\%currcookies);
21372:     my $balancers = join("','",sort(keys(%currbalancer)));
21373:     return <<"END";
21374: 
21375: <script type="text/javascript">
21376: // <![CDATA[
21377: 
21378: currBalancers = new Array('$balancers');
21379: 
21380: function toggleTargets(balnum) {
21381:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
21382:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
21383:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
21384:     var prevbalancer = prevhostitem.value;
21385:     var baltotal = document.getElementById('loadbalancing_total').value;
21386:     prevhostitem.value = balancer;
21387:     if (prevbalancer != '') {
21388:         var prevIdx = currBalancers.indexOf(prevbalancer);
21389:         if (prevIdx != -1) {
21390:             currBalancers.splice(prevIdx,1);
21391:         }
21392:     }
21393:     if (balancer == '') {
21394:         hideSpares(balnum);
21395:     } else {
21396:         var currIdx = currBalancers.indexOf(balancer);
21397:         if (currIdx == -1) {
21398:             currBalancers.push(balancer);
21399:         }
21400:         var homedoms = new Array('$allishome');
21401:         var ishomedom = homedoms[lonhostitem.selectedIndex];
21402:         showSpares(balancer,ishomedom,balnum);
21403:     }
21404:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
21405:     return;
21406: }
21407: 
21408: function showSpares(balancer,ishomedom,balnum) {
21409:     var alltargets = new Array('$alltargets');
21410:     var insttypes = new Array('$allinsttypes');
21411:     var offloadtypes = new Array('primary','default');
21412: 
21413:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
21414:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
21415:  
21416:     for (var i=0; i<offloadtypes.length; i++) {
21417:         var count = 0;
21418:         for (var j=0; j<alltargets.length; j++) {
21419:             if (alltargets[j] != balancer) {
21420:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
21421:                 item.value = alltargets[j];
21422:                 item.style.textAlign='left';
21423:                 item.style.textFace='normal';
21424:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
21425:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
21426:                     item.disabled = '';
21427:                 } else {
21428:                     item.disabled = 'disabled';
21429:                     item.checked = false;
21430:                 }
21431:                 count ++;
21432:             }
21433:         }
21434:     }
21435:     for (var k=0; k<insttypes.length; k++) {
21436:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
21437:             if (ishomedom == 1) {
21438:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
21439:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
21440:             } else {
21441:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
21442:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
21443:             }
21444:         } else {
21445:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
21446:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
21447:         }
21448:         if ((insttypes[k] != '_LC_external') && 
21449:             ((insttypes[k] != '_LC_internetdom') ||
21450:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
21451:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
21452:             item.options.length = 0;
21453:             item.options[0] = new Option("","",true,true);
21454:             var idx = 0;
21455:             for (var m=0; m<alltargets.length; m++) {
21456:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
21457:                     idx ++;
21458:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
21459:                 }
21460:             }
21461:         }
21462:     }
21463:     return;
21464: }
21465: 
21466: function hideSpares(balnum) {
21467:     var alltargets = new Array('$alltargets');
21468:     var insttypes = new Array('$allinsttypes');
21469:     var offloadtypes = new Array('primary','default');
21470: 
21471:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
21472:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
21473: 
21474:     var total = alltargets.length - 1;
21475:     for (var i=0; i<offloadtypes; i++) {
21476:         for (var j=0; j<total; j++) {
21477:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
21478:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
21479:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
21480:         }
21481:     }
21482:     for (var k=0; k<insttypes.length; k++) {
21483:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
21484:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
21485:         if (insttypes[k] != '_LC_external') {
21486:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
21487:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
21488:         }
21489:     }
21490:     return;
21491: }
21492: 
21493: function checkOffloads(item,balnum,type) {
21494:     var alltargets = new Array('$alltargets');
21495:     var offloadtypes = new Array('primary','default');
21496:     if (item.checked) {
21497:         var total = alltargets.length - 1;
21498:         var other;
21499:         if (type == offloadtypes[0]) {
21500:             other = offloadtypes[1];
21501:         } else {
21502:             other = offloadtypes[0];
21503:         }
21504:         for (var i=0; i<total; i++) {
21505:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
21506:             if (server == item.value) {
21507:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
21508:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
21509:                 }
21510:             }
21511:         }
21512:     }
21513:     return;
21514: }
21515: 
21516: function singleServerToggle(balnum,type) {
21517:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
21518:     if (offloadtoSelIdx == 0) {
21519:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
21520:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
21521: 
21522:     } else {
21523:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
21524:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
21525:     }
21526:     return;
21527: }
21528: 
21529: function balanceruleChange(formname,balnum,type) {
21530:     if (type == '_LC_external') {
21531:         return;
21532:     }
21533:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
21534:     for (var i=0; i<typesRules.length; i++) {
21535:         if (formname.elements[typesRules[i]].checked) {
21536:             if (formname.elements[typesRules[i]].value != 'specific') {
21537:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
21538:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
21539:             } else {
21540:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
21541:             }
21542:         }
21543:     }
21544:     return;
21545: }
21546: 
21547: function balancerDeleteChange(balnum) {
21548:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
21549:     var baltotal = document.getElementById('loadbalancing_total').value;
21550:     var addtarget;
21551:     var removetarget;
21552:     var action = 'delete';
21553:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
21554:         var lonhost = hostitem.value;
21555:         var currIdx = currBalancers.indexOf(lonhost);
21556:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
21557:             if (currIdx != -1) {
21558:                 currBalancers.splice(currIdx,1);
21559:             }
21560:             addtarget = lonhost;
21561:         } else {
21562:             if (currIdx == -1) {
21563:                 currBalancers.push(lonhost);
21564:             }
21565:             removetarget = lonhost;
21566:             action = 'undelete';
21567:         }
21568:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
21569:     }
21570:     return;
21571: }
21572: 
21573: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
21574:     if (baltotal > 1) {
21575:         var offloadtypes = new Array('primary','default');
21576:         var alltargets = new Array('$alltargets');
21577:         var insttypes = new Array('$allinsttypes');
21578:         for (var i=0; i<baltotal; i++) {
21579:             if (i != balnum) {
21580:                 for (var j=0; j<offloadtypes.length; j++) {
21581:                     var total = alltargets.length - 1;
21582:                     for (var k=0; k<total; k++) {
21583:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
21584:                         var server = serveritem.value;
21585:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
21586:                             if (server == addtarget) {
21587:                                 serveritem.disabled = '';
21588:                             }
21589:                         }
21590:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
21591:                             if (server == removetarget) {
21592:                                 serveritem.disabled = 'disabled';
21593:                                 serveritem.checked = false;
21594:                             }
21595:                         }
21596:                     }
21597:                 }
21598:                 for (var j=0; j<insttypes.length; j++) {
21599:                     if (insttypes[j] != '_LC_external') {
21600:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
21601:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
21602:                             var currSel = singleserver.selectedIndex;
21603:                             var currVal = singleserver.options[currSel].value;
21604:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
21605:                                 var numoptions = singleserver.options.length;
21606:                                 var needsnew = 1;
21607:                                 for (var k=0; k<numoptions; k++) {
21608:                                     if (singleserver.options[k] == addtarget) {
21609:                                         needsnew = 0;
21610:                                         break;
21611:                                     }
21612:                                 }
21613:                                 if (needsnew == 1) {
21614:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
21615:                                 }
21616:                             }
21617:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
21618:                                 singleserver.options.length = 0;
21619:                                 if ((currVal) && (currVal != removetarget)) {
21620:                                     singleserver.options[0] = new Option("","",false,false);
21621:                                 } else {
21622:                                     singleserver.options[0] = new Option("","",true,true);
21623:                                 }
21624:                                 var idx = 0;
21625:                                 for (var m=0; m<alltargets.length; m++) {
21626:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
21627:                                         idx ++;
21628:                                         if (currVal == alltargets[m]) {
21629:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
21630:                                         } else {
21631:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
21632:                                         }
21633:                                     }
21634:                                 }
21635:                             }
21636:                         }
21637:                     }
21638:                 }
21639:             }
21640:         }
21641:     }
21642:     return;
21643: }
21644: 
21645: // ]]>
21646: </script>
21647: 
21648: END
21649: }
21650: 
21651: 
21652: sub new_spares_js {
21653:     my @sparestypes = ('primary','default');
21654:     my $types = join("','",@sparestypes);
21655:     my $select = &mt('Select');
21656:     return <<"END";
21657: 
21658: <script type="text/javascript">
21659: // <![CDATA[
21660: 
21661: function updateNewSpares(formname,lonhost) {
21662:     var types = new Array('$types');
21663:     var include = new Array();
21664:     var exclude = new Array();
21665:     for (var i=0; i<types.length; i++) {
21666:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
21667:         for (var j=0; j<spareboxes.length; j++) {
21668:             if (formname.elements[spareboxes[j]].checked) {
21669:                 exclude.push(formname.elements[spareboxes[j]].value);
21670:             } else {
21671:                 include.push(formname.elements[spareboxes[j]].value);
21672:             }
21673:         }
21674:     }
21675:     for (var i=0; i<types.length; i++) {
21676:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
21677:         var selIdx = newSpare.selectedIndex;
21678:         var currnew = newSpare.options[selIdx].value;
21679:         var okSpares = new Array();
21680:         for (var j=0; j<newSpare.options.length; j++) {
21681:             var possible = newSpare.options[j].value;
21682:             if (possible != '') {
21683:                 if (exclude.indexOf(possible) == -1) {
21684:                     okSpares.push(possible);
21685:                 } else {
21686:                     if (currnew == possible) {
21687:                         selIdx = 0;
21688:                     }
21689:                 }
21690:             }
21691:         }
21692:         for (var k=0; k<include.length; k++) {
21693:             if (okSpares.indexOf(include[k]) == -1) {
21694:                 okSpares.push(include[k]);
21695:             }
21696:         }
21697:         okSpares.sort();
21698:         newSpare.options.length = 0;
21699:         if (selIdx == 0) {
21700:             newSpare.options[0] = new Option("$select","",true,true);
21701:         } else {
21702:             newSpare.options[0] = new Option("$select","",false,false);
21703:         }
21704:         for (var m=0; m<okSpares.length; m++) {
21705:             var idx = m+1;
21706:             var selThis = 0;
21707:             if (selIdx != 0) {
21708:                 if (okSpares[m] == currnew) {
21709:                     selThis = 1;
21710:                 }
21711:             }
21712:             if (selThis == 1) {
21713:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
21714:             } else {
21715:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
21716:             }
21717:         }
21718:     }
21719:     return;
21720: }
21721: 
21722: function checkNewSpares(lonhost,type) {
21723:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
21724:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
21725:     if (chosen != '') {
21726:         var othertype;
21727:         var othernewSpare;
21728:         if (type == 'primary') {
21729:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
21730:         }
21731:         if (type == 'default') {
21732:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
21733:         }
21734:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
21735:             othernewSpare.selectedIndex = 0;
21736:         }
21737:     }
21738:     return;
21739: }
21740: 
21741: // ]]>
21742: </script>
21743: 
21744: END
21745: 
21746: }
21747: 
21748: sub common_domprefs_js {
21749:     return <<"END";
21750: 
21751: <script type="text/javascript">
21752: // <![CDATA[
21753: 
21754: function getIndicesByName(formname,item) {
21755:     var group = new Array();
21756:     for (var i=0;i<formname.elements.length;i++) {
21757:         if (formname.elements[i].name == item) {
21758:             group.push(formname.elements[i].id);
21759:         }
21760:     }
21761:     return group;
21762: }
21763: 
21764: // ]]>
21765: </script>
21766: 
21767: END
21768: 
21769: }
21770: 
21771: sub recaptcha_js {
21772:     my %lt = &captcha_phrases();
21773:     return <<"END";
21774: 
21775: <script type="text/javascript">
21776: // <![CDATA[
21777: 
21778: function updateCaptcha(caller,context) {
21779:     var privitem;
21780:     var pubitem;
21781:     var privtext;
21782:     var pubtext;
21783:     var versionitem;
21784:     var versiontext;
21785:     if (document.getElementById(context+'_recaptchapub')) {
21786:         pubitem = document.getElementById(context+'_recaptchapub');
21787:     } else {
21788:         return;
21789:     }
21790:     if (document.getElementById(context+'_recaptchapriv')) {
21791:         privitem = document.getElementById(context+'_recaptchapriv');
21792:     } else {
21793:         return;
21794:     }
21795:     if (document.getElementById(context+'_recaptchapubtxt')) {
21796:         pubtext = document.getElementById(context+'_recaptchapubtxt');
21797:     } else {
21798:         return;
21799:     }
21800:     if (document.getElementById(context+'_recaptchaprivtxt')) {
21801:         privtext = document.getElementById(context+'_recaptchaprivtxt');
21802:     } else {
21803:         return;
21804:     }
21805:     if (document.getElementById(context+'_recaptchaversion')) {
21806:         versionitem = document.getElementById(context+'_recaptchaversion');
21807:     } else {
21808:         return;
21809:     }
21810:     if (document.getElementById(context+'_recaptchavertxt')) {
21811:         versiontext = document.getElementById(context+'_recaptchavertxt');
21812:     } else {
21813:         return;
21814:     }
21815:     if (caller.checked) {
21816:         if (caller.value == 'recaptcha') {
21817:             pubitem.type = 'text';
21818:             privitem.type = 'text';
21819:             pubitem.size = '40';
21820:             privitem.size = '40';
21821:             pubtext.innerHTML = "$lt{'pub'}";
21822:             privtext.innerHTML = "$lt{'priv'}";
21823:             versionitem.type = 'text';
21824:             versionitem.size = '3';
21825:             versiontext.innerHTML = "$lt{'ver'}";
21826:         } else {
21827:             pubitem.type = 'hidden';
21828:             privitem.type = 'hidden';
21829:             versionitem.type = 'hidden';
21830:             pubtext.innerHTML = '';
21831:             privtext.innerHTML = '';
21832:             versiontext.innerHTML = '';
21833:         }
21834:     }
21835:     return;
21836: }
21837: 
21838: // ]]>
21839: </script>
21840: 
21841: END
21842: 
21843: }
21844: 
21845: sub toggle_display_js {
21846:     return <<"END";
21847: 
21848: <script type="text/javascript">
21849: // <![CDATA[
21850: 
21851: function toggleDisplay(domForm,caller) {
21852:     if (document.getElementById(caller)) {
21853:         var divitem = document.getElementById(caller);
21854:         var optionsElement = domForm.coursecredits;
21855:         var checkval = 1;
21856:         var dispval = 'block';
21857:         var selfcreateRegExp = /^cancreate_emailverified/;
21858:         if (caller == 'emailoptions') {
21859:             optionsElement = domForm.cancreate_email;
21860:         }
21861:         if (caller == 'studentsubmission') {
21862:             optionsElement = domForm.postsubmit;
21863:         }
21864:         if (caller == 'cloneinstcode') {
21865:             optionsElement = domForm.canclone;
21866:             checkval = 'instcode';
21867:         }
21868:         if (selfcreateRegExp.test(caller)) {
21869:             optionsElement = domForm.elements[caller];
21870:             checkval = 'other';
21871:             dispval = 'inline'
21872:         }
21873:         if (optionsElement.length) {
21874:             var currval;
21875:             for (var i=0; i<optionsElement.length; i++) {
21876:                 if (optionsElement[i].checked) {
21877:                    currval = optionsElement[i].value;
21878:                 }
21879:             }
21880:             if (currval == checkval) {
21881:                 divitem.style.display = dispval;
21882:             } else {
21883:                 divitem.style.display = 'none';
21884:             }
21885:         }
21886:     }
21887:     return;
21888: }
21889: 
21890: // ]]>
21891: </script>
21892: 
21893: END
21894: 
21895: }
21896: 
21897: sub captcha_phrases {
21898:     return &Apache::lonlocal::texthash (
21899:                  priv => 'Private key',
21900:                  pub  => 'Public key',
21901:                  original  => 'original (CAPTCHA)',
21902:                  recaptcha => 'successor (ReCAPTCHA)',
21903:                  notused   => 'unused',
21904:                  ver => 'ReCAPTCHA version (1 or 2)',
21905:     );
21906: }
21907: 
21908: sub devalidate_remote_domconfs {
21909:     my ($dom,$cachekeys) = @_;
21910:     return unless (ref($cachekeys) eq 'HASH');
21911:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
21912:     my %thismachine;
21913:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
21914:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions',
21915:                       'directorysrch','passwdconf','cats','proxyalias');
21916:     if (keys(%servers)) {
21917:         foreach my $server (keys(%servers)) {
21918:             next if ($thismachine{$server});
21919:             my @cached;
21920:             foreach my $name (@posscached) {
21921:                 if ($cachekeys->{$name}) {
21922:                     if ($name eq 'proxyalias') {
21923:                         if (ref($cachekeys->{$name}) eq 'HASH') {  
21924:                             foreach my $key (keys(%{$cachekeys->{$name}})) {
21925:                                 push(@cached,&escape($name).':'.&escape($key));
21926:                             }
21927:                         }
21928:                     } else {
21929:                         push(@cached,&escape($name).':'.&escape($dom));
21930:                     }
21931:                 }
21932:             }
21933:             if (@cached) {
21934:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
21935:             }
21936:         }
21937:     }
21938:     return;
21939: }
21940: 
21941: 1;

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