File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.293: download - view: text, annotated - select for diffs
Sat Feb 25 20:30:52 2017 UTC (7 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
LON-CAPA Network (SSL) domain configuration.
- Options for SSL usage can be set separately for outbound connections (lonc)
  and inbound connections (lond).

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.293 2017/02/25 20:30: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, and placement).  
  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: 
  180: my $registered_cleanup;
  181: my $modified_urls;
  182: 
  183: sub handler {
  184:     my $r=shift;
  185:     if ($r->header_only) {
  186:         &Apache::loncommon::content_type($r,'text/html');
  187:         $r->send_http_header;
  188:         return OK;
  189:     }
  190: 
  191:     my $context = 'domain';
  192:     my $dom = $env{'request.role.domain'};
  193:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  194:     if (&Apache::lonnet::allowed('mau',$dom)) {
  195:         &Apache::loncommon::content_type($r,'text/html');
  196:         $r->send_http_header;
  197:     } else {
  198:         $env{'user.error.msg'}=
  199:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  200:         return HTTP_NOT_ACCEPTABLE;
  201:     }
  202: 
  203:     $registered_cleanup=0;
  204:     @{$modified_urls}=();
  205: 
  206:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  207:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  208:                                             ['phase','actions']);
  209:     my $phase = 'pickactions';
  210:     if ( exists($env{'form.phase'}) ) {
  211:         $phase = $env{'form.phase'};
  212:     }
  213:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  214:     my %domconfig =
  215:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  216:                 'quotas','autoenroll','autoupdate','autocreate',
  217:                 'directorysrch','usercreation','usermodification',
  218:                 'contacts','defaults','scantron','coursecategories',
  219:                 'serverstatuses','requestcourses','helpsettings',
  220:                 'coursedefaults','usersessions','loadbalancing',
  221:                 'requestauthor','selfenrollment','inststatus',
  222:                 'ltitools','ssl','trust'],$dom);
  223:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  224:                        'autoupdate','autocreate','directorysrch','contacts',
  225:                        'usercreation','selfcreation','usermodification','scantron',
  226:                        'requestcourses','requestauthor','coursecategories',
  227:                        'serverstatuses','helpsettings','coursedefaults',
  228:                        'ltitools','selfenrollment','usersessions','ssl','trust');
  229:     my %existing;
  230:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  231:         %existing = %{$domconfig{'loadbalancing'}};
  232:     }
  233:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  234:         push(@prefs_order,'loadbalancing');
  235:     }
  236:     my %prefs = (
  237:         'rolecolors' =>
  238:                    { text => 'Default color schemes',
  239:                      help => 'Domain_Configuration_Color_Schemes',
  240:                      header => [{col1 => 'Student Settings',
  241:                                  col2 => '',},
  242:                                 {col1 => 'Coordinator Settings',
  243:                                  col2 => '',},
  244:                                 {col1 => 'Author Settings',
  245:                                  col2 => '',},
  246:                                 {col1 => 'Administrator Settings',
  247:                                  col2 => '',}],
  248:                       print => \&print_rolecolors,
  249:                       modify => \&modify_rolecolors,
  250:                     },
  251:         'login' =>
  252:                     { text => 'Log-in page options',
  253:                       help => 'Domain_Configuration_Login_Page',
  254:                       header => [{col1 => 'Log-in Page Items',
  255:                                   col2 => '',},
  256:                                  {col1 => 'Log-in Help',
  257:                                   col2 => 'Value'},
  258:                                  {col1 => 'Custom HTML in document head',
  259:                                   col2 => 'Value'}],
  260:                       print => \&print_login,
  261:                       modify => \&modify_login,
  262:                     },
  263:         'defaults' => 
  264:                     { text => 'Default authentication/language/timezone/portal/types',
  265:                       help => 'Domain_Configuration_LangTZAuth',
  266:                       header => [{col1 => 'Setting',
  267:                                   col2 => 'Value'},
  268:                                  {col1 => 'Institutional user types',
  269:                                   col2 => 'Assignable to e-mail usernames'}],
  270:                       print => \&print_defaults,
  271:                       modify => \&modify_defaults,
  272:                     },
  273:         'quotas' => 
  274:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  275:                       help => 'Domain_Configuration_Quotas',
  276:                       header => [{col1 => 'User affiliation',
  277:                                   col2 => 'Available tools',
  278:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  279:                       print => \&print_quotas,
  280:                       modify => \&modify_quotas,
  281:                     },
  282:         'autoenroll' =>
  283:                    { text => 'Auto-enrollment settings',
  284:                      help => 'Domain_Configuration_Auto_Enrollment',
  285:                      header => [{col1 => 'Configuration setting',
  286:                                  col2 => 'Value(s)'}],
  287:                      print => \&print_autoenroll,
  288:                      modify => \&modify_autoenroll,
  289:                    },
  290:         'autoupdate' => 
  291:                    { text => 'Auto-update settings',
  292:                      help => 'Domain_Configuration_Auto_Updates',
  293:                      header => [{col1 => 'Setting',
  294:                                  col2 => 'Value',},
  295:                                 {col1 => 'Setting',
  296:                                  col2 => 'Affiliation'},
  297:                                 {col1 => 'User population',
  298:                                  col2 => 'Updatable user data'}],
  299:                      print => \&print_autoupdate,
  300:                      modify => \&modify_autoupdate,
  301:                   },
  302:         'autocreate' => 
  303:                   { text => 'Auto-course creation settings',
  304:                      help => 'Domain_Configuration_Auto_Creation',
  305:                      header => [{col1 => 'Configuration Setting',
  306:                                  col2 => 'Value',}],
  307:                      print => \&print_autocreate,
  308:                      modify => \&modify_autocreate,
  309:                   },
  310:         'directorysrch' => 
  311:                   { text => 'Directory searches',
  312:                     help => 'Domain_Configuration_InstDirectory_Search',
  313:                     header => [{col1 => 'Institutional Directory Setting',
  314:                                 col2 => 'Value',},
  315:                                {col1 => 'LON-CAPA Directory Setting',
  316:                                 col2 => 'Value',}],
  317:                     print => \&print_directorysrch,
  318:                     modify => \&modify_directorysrch,
  319:                   },
  320:         'contacts' =>
  321:                   { text => 'E-mail addresses and helpform',
  322:                     help => 'Domain_Configuration_Contact_Info',
  323:                     header => [{col1 => 'Default e-mail addresses',
  324:                                 col2 => 'Value',},
  325:                                {col1 => 'Recipient(s) for notifications',
  326:                                 col2 => 'Value',},
  327:                                {col1 => 'Ask helpdesk form settings',
  328:                                 col2 => 'Value',},],
  329:                     print => \&print_contacts,
  330:                     modify => \&modify_contacts,
  331:                   },
  332:         'usercreation' => 
  333:                   { text => 'User creation',
  334:                     help => 'Domain_Configuration_User_Creation',
  335:                     header => [{col1 => 'Format rule type',
  336:                                 col2 => 'Format rules in force'},
  337:                                {col1 => 'User account creation',
  338:                                 col2 => 'Usernames which may be created',},
  339:                                {col1 => 'Context',
  340:                                 col2 => 'Assignable authentication types'}],
  341:                     print => \&print_usercreation,
  342:                     modify => \&modify_usercreation,
  343:                   },
  344:         'selfcreation' => 
  345:                   { text => 'Users self-creating accounts',
  346:                     help => 'Domain_Configuration_Self_Creation', 
  347:                     header => [{col1 => 'Self-creation with institutional username',
  348:                                 col2 => 'Enabled?'},
  349:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  350:                                 col2 => 'Information user can enter'},
  351:                                {col1 => 'Self-creation with e-mail as username',
  352:                                 col2 => 'Settings'}],
  353:                     print => \&print_selfcreation,
  354:                     modify => \&modify_selfcreation,
  355:                   },
  356:         'usermodification' =>
  357:                   { text => 'User modification',
  358:                     help => 'Domain_Configuration_User_Modification',
  359:                     header => [{col1 => 'Target user has role',
  360:                                 col2 => 'User information updatable in author context'},
  361:                                {col1 => 'Target user has role',
  362:                                 col2 => 'User information updatable in course context'}],
  363:                     print => \&print_usermodification,
  364:                     modify => \&modify_usermodification,
  365:                   },
  366:         'scantron' =>
  367:                   { text => 'Bubblesheet format file',
  368:                     help => 'Domain_Configuration_Scantron_Format',
  369:                     header => [ {col1 => 'Item',
  370:                                  col2 => '',
  371:                               }],
  372:                     print => \&print_scantron,
  373:                     modify => \&modify_scantron,
  374:                   },
  375:         'requestcourses' => 
  376:                  {text => 'Request creation of courses',
  377:                   help => 'Domain_Configuration_Request_Courses',
  378:                   header => [{col1 => 'User affiliation',
  379:                               col2 => 'Availability/Processing of requests',},
  380:                              {col1 => 'Setting',
  381:                               col2 => 'Value'},
  382:                              {col1 => 'Available textbooks',
  383:                               col2 => ''},
  384:                              {col1 => 'Available templates',
  385:                               col2 => ''},
  386:                              {col1 => 'Validation (not official courses)',
  387:                               col2 => 'Value'},],
  388:                   print => \&print_quotas,
  389:                   modify => \&modify_quotas,
  390:                  },
  391:         'requestauthor' =>
  392:                  {text => 'Request Authoring Space',
  393:                   help => 'Domain_Configuration_Request_Author',
  394:                   header => [{col1 => 'User affiliation',
  395:                               col2 => 'Availability/Processing of requests',},
  396:                              {col1 => 'Setting',
  397:                               col2 => 'Value'}],
  398:                   print => \&print_quotas,
  399:                   modify => \&modify_quotas,
  400:                  },
  401:         'coursecategories' =>
  402:                   { text => 'Cataloging of courses/communities',
  403:                     help => 'Domain_Configuration_Cataloging_Courses',
  404:                     header => [{col1 => 'Catalog type/availability',
  405:                                 col2 => '',},
  406:                                {col1 => 'Category settings for standard catalog',
  407:                                 col2 => '',},
  408:                                {col1 => 'Categories',
  409:                                 col2 => '',
  410:                                }],
  411:                     print => \&print_coursecategories,
  412:                     modify => \&modify_coursecategories,
  413:                   },
  414:         'serverstatuses' =>
  415:                  {text   => 'Access to server status pages',
  416:                   help   => 'Domain_Configuration_Server_Status',
  417:                   header => [{col1 => 'Status Page',
  418:                               col2 => 'Other named users',
  419:                               col3 => 'Specific IPs',
  420:                             }],
  421:                   print => \&print_serverstatuses,
  422:                   modify => \&modify_serverstatuses,
  423:                  },
  424:         'helpsettings' =>
  425:                  {text   => 'Support settings',
  426:                   help   => 'Domain_Configuration_Help_Settings',
  427:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  428:                               col2 => 'Value'},
  429:                              {col1 => 'Helpdesk Roles',
  430:                               col2 => 'Settings'},],
  431:                   print  => \&print_helpsettings,
  432:                   modify => \&modify_helpsettings,
  433:                  },
  434:         'coursedefaults' => 
  435:                  {text => 'Course/Community defaults',
  436:                   help => 'Domain_Configuration_Course_Defaults',
  437:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  438:                               col2 => 'Value',},
  439:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  440:                               col2 => 'Value',},],
  441:                   print => \&print_coursedefaults,
  442:                   modify => \&modify_coursedefaults,
  443:                  },
  444:         'selfenrollment' => 
  445:                  {text   => 'Self-enrollment in Course/Community',
  446:                   help   => 'Domain_Configuration_Selfenrollment',
  447:                   header => [{col1 => 'Configuration Rights',
  448:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  449:                              {col1 => 'Defaults',
  450:                               col2 => 'Value'},
  451:                              {col1 => 'Self-enrollment validation (optional)',
  452:                               col2 => 'Value'},],
  453:                   print => \&print_selfenrollment,
  454:                   modify => \&modify_selfenrollment,
  455:                  },
  456:         'privacy' => 
  457:                  {text   => 'User Privacy',
  458:                   help   => 'Domain_Configuration_User_Privacy',
  459:                   header => [{col1 => 'Setting',
  460:                               col2 => 'Value',}],
  461:                   print => \&print_privacy,
  462:                   modify => \&modify_privacy,
  463:                  },
  464:         'usersessions' =>
  465:                  {text  => 'User session hosting/offloading',
  466:                   help  => 'Domain_Configuration_User_Sessions',
  467:                   header => [{col1 => 'Domain server',
  468:                               col2 => 'Servers to offload sessions to when busy'},
  469:                              {col1 => 'Hosting of users from other domains',
  470:                               col2 => 'Rules'},
  471:                              {col1 => "Hosting domain's own users elsewhere",
  472:                               col2 => 'Rules'}],
  473:                   print => \&print_usersessions,
  474:                   modify => \&modify_usersessions,
  475:                  },
  476:         'loadbalancing' =>
  477:                  {text  => 'Dedicated Load Balancer(s)',
  478:                   help  => 'Domain_Configuration_Load_Balancing',
  479:                   header => [{col1 => 'Balancers',
  480:                               col2 => 'Default destinations',
  481:                               col3 => 'User affiliation',
  482:                               col4 => 'Overrides'},
  483:                             ],
  484:                   print => \&print_loadbalancing,
  485:                   modify => \&modify_loadbalancing,
  486:                  },
  487:         'ltitools' => 
  488:                  {text => 'External Tools (LTI)',
  489:                   help => 'Domain_configuration_LTI_Tools',
  490:                   header => [{col1 => 'Setting',
  491:                               col2 => 'Value',}],
  492:                   print => \&print_ltitools,
  493:                   modify => \&modify_ltitools,
  494:                  },
  495:         'ssl' =>
  496:                  {text  => 'LON-CAPA Network (SSL)',
  497:                   help  => 'Domain_Configuration_Network_SSL',
  498:                   header => [{col1 => 'Server',
  499:                               col2 => 'Certificate Status'},
  500:                              {col1 => 'Connections to other servers',
  501:                               col2 => 'Rules'},
  502:                              {col1 => 'Connections from other servers',
  503:                               col2 => 'Rules'},
  504:                              {col1 => "Replicating domain's published content",
  505:                               col2 => 'Rules'}],
  506:                   print => \&print_ssl,
  507:                   modify => \&modify_ssl,
  508:                  },
  509:         'trust' =>
  510:                  {text   => 'Trust Settings',
  511:                   help   => 'Domain_Configuration_Trust',
  512:                   header => [{col1 => "Access to this domain's content by others",
  513:                               col2 => 'Rules'},
  514:                              {col1 => "Access to other domain's content by this domain",
  515:                               col2 => 'Rules'},
  516:                              {col1 => "Enrollment in this domain's courses by others",
  517:                               col2 => 'Rules',},
  518:                              {col1 => "Co-author roles in this domain for others",
  519:                               col2 => 'Rules',},
  520:                              {col1 => "Co-author roles for this domain's users elsewhere",
  521:                               col2 => 'Rules',},
  522:                              {col1 => "Domain roles in this domain assignable to others",
  523:                               col2 => 'Rules'},
  524:                              {col1 => "Course catalog for this domain displayed elsewhere",
  525:                               col2 => 'Rules'},
  526:                              {col1 => "Requests for creation of courses in this domain by others",
  527:                               col2 => 'Rules'},
  528:                              {col1 => "Users in other domains can send messages to this domain",
  529:                               col2 => 'Rules'},],
  530:                   print => \&print_trust,
  531:                   modify => \&modify_trust,
  532:                  },
  533:     );
  534:     if (keys(%servers) > 1) {
  535:         $prefs{'login'}  = { text   => 'Log-in page options',
  536:                              help   => 'Domain_Configuration_Login_Page',
  537:                             header => [{col1 => 'Log-in Service',
  538:                                         col2 => 'Server Setting',},
  539:                                        {col1 => 'Log-in Page Items',
  540:                                         col2 => ''},
  541:                                        {col1 => 'Log-in Help',
  542:                                         col2 => 'Value'},
  543:                                        {col1 => 'Custom HTML in document head',
  544:                                         col2 => 'Value'}],
  545:                             print => \&print_login,
  546:                             modify => \&modify_login,
  547:                            };
  548:     }
  549: 
  550:     my @roles = ('student','coordinator','author','admin');
  551:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  552:     &Apache::lonhtmlcommon::add_breadcrumb
  553:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  554:       text=>"Settings to display/modify"});
  555:     my $confname = $dom.'-domainconfig';
  556: 
  557:     if ($phase eq 'process') {
  558:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  559:                                                               \%prefs,\%domconfig,$confname,\@roles);
  560:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  561:             $r->rflush();
  562:             &devalidate_remote_domconfs($dom,$result);
  563:         }
  564:     } elsif ($phase eq 'display') {
  565:         my $js = &recaptcha_js().
  566:                  &toggle_display_js();
  567:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  568:             my ($othertitle,$usertypes,$types) =
  569:                 &Apache::loncommon::sorted_inst_types($dom);
  570:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  571:                                           $domconfig{'loadbalancing'}).
  572:                    &new_spares_js().
  573:                    &common_domprefs_js().
  574:                    &Apache::loncommon::javascript_array_indexof();
  575:         }
  576:         if (grep(/^requestcourses$/,@actions)) {
  577:             my $javascript_validations;
  578:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  579:             $js .= <<END;
  580: <script type="text/javascript">
  581: $javascript_validations
  582: </script>
  583: $coursebrowserjs
  584: END
  585:         }
  586:         if (grep(/^contacts$/,@actions)) {
  587:             $js .= &contacts_javascript();
  588:         }
  589:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  590:     } else {
  591: # check if domconfig user exists for the domain.
  592:         my $servadm = $r->dir_config('lonAdmEMail');
  593:         my ($configuserok,$author_ok,$switchserver) =
  594:             &config_check($dom,$confname,$servadm);
  595:         unless ($configuserok eq 'ok') {
  596:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  597:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  598:                           $confname).
  599:                       '<br />'
  600:             );
  601:             if ($switchserver) {
  602:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  603:                           '<br />'.
  604:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  605:                           '<br />'.
  606:                           &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).
  607:                           '<br />'.
  608:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  609:                 );
  610:             } else {
  611:                 $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.').
  612:                           '<br />'.
  613:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  614:                 );
  615:             }
  616:             $r->print(&Apache::loncommon::end_page());
  617:             return OK;
  618:         }
  619:         if (keys(%domconfig) == 0) {
  620:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  621:             my @ids=&Apache::lonnet::current_machine_ids();
  622:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  623:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  624:                 my @loginimages = ('img','logo','domlogo','login');
  625:                 my $custom_img_count = 0;
  626:                 foreach my $img (@loginimages) {
  627:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  628:                         $custom_img_count ++;
  629:                     }
  630:                 }
  631:                 foreach my $role (@roles) {
  632:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  633:                         $custom_img_count ++;
  634:                     }
  635:                 }
  636:                 if ($custom_img_count > 0) {
  637:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  638:                     my $switch_server = &check_switchserver($dom,$confname);
  639:                     $r->print(
  640:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  641:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  642:     &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 />'.
  643:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  644:                     if ($switch_server) {
  645:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  646:                     }
  647:                     $r->print(&Apache::loncommon::end_page());
  648:                     return OK;
  649:                 }
  650:             }
  651:         }
  652:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  653:     }
  654:     return OK;
  655: }
  656: 
  657: sub process_changes {
  658:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  659:     my %domconfig;
  660:     if (ref($values) eq 'HASH') {
  661:         %domconfig = %{$values};
  662:     }
  663:     my $output;
  664:     if ($action eq 'login') {
  665:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  666:     } elsif ($action eq 'rolecolors') {
  667:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  668:                                      $lastactref,%domconfig);
  669:     } elsif ($action eq 'quotas') {
  670:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  671:     } elsif ($action eq 'autoenroll') {
  672:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  673:     } elsif ($action eq 'autoupdate') {
  674:         $output = &modify_autoupdate($dom,%domconfig);
  675:     } elsif ($action eq 'autocreate') {
  676:         $output = &modify_autocreate($dom,%domconfig);
  677:     } elsif ($action eq 'directorysrch') {
  678:         $output = &modify_directorysrch($dom,%domconfig);
  679:     } elsif ($action eq 'usercreation') {
  680:         $output = &modify_usercreation($dom,%domconfig);
  681:     } elsif ($action eq 'selfcreation') {
  682:         $output = &modify_selfcreation($dom,%domconfig);
  683:     } elsif ($action eq 'usermodification') {
  684:         $output = &modify_usermodification($dom,%domconfig);
  685:     } elsif ($action eq 'contacts') {
  686:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  687:     } elsif ($action eq 'defaults') {
  688:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  689:     } elsif ($action eq 'scantron') {
  690:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  691:     } elsif ($action eq 'coursecategories') {
  692:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  693:     } elsif ($action eq 'serverstatuses') {
  694:         $output = &modify_serverstatuses($dom,%domconfig);
  695:     } elsif ($action eq 'requestcourses') {
  696:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  697:     } elsif ($action eq 'requestauthor') {
  698:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  699:     } elsif ($action eq 'helpsettings') {
  700:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  701:     } elsif ($action eq 'coursedefaults') {
  702:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  703:     } elsif ($action eq 'selfenrollment') {
  704:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  705:     } elsif ($action eq 'usersessions') {
  706:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  707:     } elsif ($action eq 'loadbalancing') {
  708:         $output = &modify_loadbalancing($dom,%domconfig);
  709:     } elsif ($action eq 'ltitools') {
  710:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  711:     } elsif ($action eq 'ssl') {
  712:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  713:     } elsif ($action eq 'trust') {
  714:         $output = &modify_trust($dom,$lastactref,%domconfig);
  715:     }
  716:     return $output;
  717: }
  718: 
  719: sub print_config_box {
  720:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  721:     my $rowtotal = 0;
  722:     my $output;
  723:     if ($action eq 'coursecategories') {
  724:         $output = &coursecategories_javascript($settings);
  725:     } elsif ($action eq 'defaults') {
  726:         $output = &defaults_javascript($settings); 
  727:     } elsif ($action eq 'helpsettings') {
  728:         my (%privs,%levelscurrent);
  729:         my %full=();
  730:         my %levels=(
  731:                      course => {},
  732:                      domain => {},
  733:                      system => {},
  734:                    );
  735:         my $context = 'domain';
  736:         my $crstype = 'Course';
  737:         my $formname = 'display';
  738:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  739:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  740:         $output =
  741:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full, 
  742:                                                       \@templateroles);
  743:     }
  744:     $output .=
  745:          '<table class="LC_nested_outer">
  746:           <tr>
  747:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  748:            &mt($item->{text}).'&nbsp;'.
  749:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  750:           '</tr>';
  751:     $rowtotal ++;
  752:     my $numheaders = 1;
  753:     if (ref($item->{'header'}) eq 'ARRAY') {
  754:         $numheaders = scalar(@{$item->{'header'}});
  755:     }
  756:     if ($numheaders > 1) {
  757:         my $colspan = '';
  758:         my $rightcolspan = '';
  759:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  760:             ($action eq 'directorysrch') ||
  761:             (($action eq 'login') && ($numheaders < 4))) {
  762:             $colspan = ' colspan="2"';
  763:         }
  764:         if ($action eq 'usersessions') {
  765:             $rightcolspan = ' colspan="3"'; 
  766:         }
  767:         $output .= '
  768:           <tr>
  769:            <td>
  770:             <table class="LC_nested">
  771:              <tr class="LC_info_row">
  772:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  773:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  774:              </tr>';
  775:         $rowtotal ++;
  776:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  777:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  778:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  779:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  780:             ($action eq 'contacts')) {
  781:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  782:         } elsif ($action eq 'coursecategories') {
  783:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  784:         } elsif ($action eq 'login') {
  785:             if ($numheaders == 4) {
  786:                 $colspan = ' colspan="2"';
  787:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  788:             } else {
  789:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  790:             }
  791:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  792:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  793:         } elsif ($action eq 'rolecolors') {
  794:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  795:         }
  796:         $output .= '
  797:            </table>
  798:           </td>
  799:          </tr>
  800:          <tr>
  801:            <td>
  802:             <table class="LC_nested">
  803:              <tr class="LC_info_row">
  804:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  805:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  806:              </tr>';
  807:             $rowtotal ++;
  808:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  809:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  810:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
  811:             ($action eq 'trust') || ($action eq 'contacts')) {
  812:             if ($action eq 'coursecategories') {
  813:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  814:                 $colspan = ' colspan="2"';
  815:             } elsif ($action eq 'trust') {
  816:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
  817:             } else {
  818:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  819:             }
  820:             if ($action eq 'trust') {
  821:                 $output .= '
  822:             </table>
  823:           </td>
  824:          </tr>';
  825:                 my @trusthdrs = qw(2 3 4 5 6 7);
  826:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
  827:                 for (my $i=0; $i<@trusthdrs; $i++) {
  828:                     $output .= '
  829:          <tr>
  830:            <td>
  831:             <table class="LC_nested">
  832:              <tr class="LC_info_row">
  833:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
  834:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
  835:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
  836:             </table>
  837:           </td>
  838:          </tr>';
  839:                 }
  840:                 $output .= '
  841:          <tr>
  842:            <td>
  843:             <table class="LC_nested">
  844:              <tr class="LC_info_row">
  845:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
  846:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
  847:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  848:             } else {
  849:                 $output .= '
  850:            </table>
  851:           </td>
  852:          </tr>
  853:          <tr>
  854:            <td>
  855:             <table class="LC_nested">
  856:              <tr class="LC_info_row">
  857:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  858:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  859:              </tr>'."\n";
  860:                 if ($action eq 'coursecategories') {
  861:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  862:                 } else {
  863:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  864:                 }
  865:             }
  866:             $rowtotal ++;
  867:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  868:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  869:                  ($action eq 'helpsettings')) {
  870:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  871:         } elsif ($action eq 'ssl') {
  872:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
  873:             </table>
  874:           </td>
  875:          </tr>
  876:          <tr>
  877:            <td>
  878:             <table class="LC_nested">
  879:              <tr class="LC_info_row">
  880:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  881:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  882:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
  883:             </table>
  884:           </td>
  885:          </tr>
  886:          <tr>
  887:            <td>
  888:             <table class="LC_nested">
  889:              <tr class="LC_info_row">
  890:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  891:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
  892:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  893:         } elsif ($action eq 'login') {
  894:             if ($numheaders == 4) {
  895:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  896:            </table>
  897:           </td>
  898:          </tr>
  899:          <tr>
  900:            <td>
  901:             <table class="LC_nested">
  902:              <tr class="LC_info_row">
  903:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  904:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  905:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  906:                 $rowtotal ++;
  907:             } else {
  908:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  909:             }
  910:             $output .= '
  911:            </table>
  912:           </td>
  913:          </tr>
  914:          <tr>
  915:            <td>
  916:             <table class="LC_nested">
  917:              <tr class="LC_info_row">';
  918:             if ($numheaders == 4) {
  919:                 $output .= '
  920:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  921:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  922:              </tr>';
  923:             } else {
  924:                 $output .= '
  925:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  926:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  927:              </tr>';
  928:             }
  929:             $rowtotal ++;
  930:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  931:         } elsif ($action eq 'requestcourses') {
  932:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  933:             $rowtotal ++;
  934:             $output .= &print_studentcode($settings,\$rowtotal).'
  935:            </table>
  936:           </td>
  937:          </tr>
  938:          <tr>
  939:            <td>
  940:             <table class="LC_nested">
  941:              <tr class="LC_info_row">
  942:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  943:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  944:                        &textbookcourses_javascript($settings).
  945:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  946:             </table>
  947:            </td>
  948:           </tr>
  949:          <tr>
  950:            <td>
  951:             <table class="LC_nested">
  952:              <tr class="LC_info_row">
  953:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  954:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  955:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  956:             </table>
  957:            </td>
  958:           </tr>
  959:           <tr>
  960:            <td>
  961:             <table class="LC_nested">
  962:              <tr class="LC_info_row">
  963:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  964:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  965:              </tr>'.
  966:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  967:         } elsif ($action eq 'requestauthor') {
  968:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  969:             $rowtotal ++;
  970:         } elsif ($action eq 'rolecolors') {
  971:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  972:            </table>
  973:           </td>
  974:          </tr>
  975:          <tr>
  976:            <td>
  977:             <table class="LC_nested">
  978:              <tr class="LC_info_row">
  979:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  980:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  981:               <td class="LC_right_item" valign="top">'.
  982:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  983:              </tr>'.
  984:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  985:            </table>
  986:           </td>
  987:          </tr>
  988:          <tr>
  989:            <td>
  990:             <table class="LC_nested">
  991:              <tr class="LC_info_row">
  992:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  993:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  994:              </tr>'.
  995:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  996:             $rowtotal += 2;
  997:         }
  998:     } else {
  999:         $output .= '
 1000:           <tr>
 1001:            <td>
 1002:             <table class="LC_nested">
 1003:              <tr class="LC_info_row">';
 1004:         if ($action eq 'login') {
 1005:             $output .= '  
 1006:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1007:         } elsif ($action eq 'serverstatuses') {
 1008:             $output .= '
 1009:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1010:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1011: 
 1012:         } else {
 1013:             $output .= '
 1014:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1015:         }
 1016:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1017:             $output .= '<td class="LC_left_item" valign="top">'.
 1018:                        &mt($item->{'header'}->[0]->{'col2'});
 1019:             if ($action eq 'serverstatuses') {
 1020:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1021:             } 
 1022:         } else {
 1023:             $output .= '<td class="LC_right_item" valign="top">'.
 1024:                        &mt($item->{'header'}->[0]->{'col2'});
 1025:         }
 1026:         $output .= '</td>';
 1027:         if ($item->{'header'}->[0]->{'col3'}) {
 1028:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1029:                 $output .= '<td class="LC_left_item" valign="top">'.
 1030:                             &mt($item->{'header'}->[0]->{'col3'});
 1031:             } else {
 1032:                 $output .= '<td class="LC_right_item" valign="top">'.
 1033:                            &mt($item->{'header'}->[0]->{'col3'});
 1034:             }
 1035:             if ($action eq 'serverstatuses') {
 1036:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1037:             }
 1038:             $output .= '</td>';
 1039:         }
 1040:         if ($item->{'header'}->[0]->{'col4'}) {
 1041:             $output .= '<td class="LC_right_item" valign="top">'.
 1042:                        &mt($item->{'header'}->[0]->{'col4'});
 1043:         }
 1044:         $output .= '</tr>';
 1045:         $rowtotal ++;
 1046:         if ($action eq 'quotas') {
 1047:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1048:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1049:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1050:                  ($action eq 'ltitools')) {
 1051:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1052:         } elsif ($action eq 'scantron') {
 1053:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 1054:         }
 1055:     }
 1056:     $output .= '
 1057:    </table>
 1058:   </td>
 1059:  </tr>
 1060: </table><br />';
 1061:     return ($output,$rowtotal);
 1062: }
 1063: 
 1064: sub print_login {
 1065:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1066:     my ($css_class,$datatable);
 1067:     my %choices = &login_choices();
 1068: 
 1069:     if ($caller eq 'service') {
 1070:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1071:         my $choice = $choices{'disallowlogin'};
 1072:         $css_class = ' class="LC_odd_row"';
 1073:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1074:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1075:                       '<th>'.$choices{'server'}.'</th>'.
 1076:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1077:                       '<th>'.$choices{'custompath'}.'</th>'.
 1078:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1079:         my %disallowed;
 1080:         if (ref($settings) eq 'HASH') {
 1081:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1082:                %disallowed = %{$settings->{'loginvia'}};
 1083:             }
 1084:         }
 1085:         foreach my $lonhost (sort(keys(%servers))) {
 1086:             my $direct = 'selected="selected"';
 1087:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1088:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1089:                     $direct = '';
 1090:                 }
 1091:             }
 1092:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1093:                           '<td><select name="'.$lonhost.'_server">'.
 1094:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1095:                           '</option>';
 1096:             foreach my $hostid (sort(keys(%servers))) {
 1097:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1098:                 my $selected = '';
 1099:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1100:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1101:                         $selected = 'selected="selected"';
 1102:                     }
 1103:                 }
 1104:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1105:                               $servers{$hostid}.'</option>';
 1106:             }
 1107:             $datatable .= '</select></td>'.
 1108:                           '<td><select name="'.$lonhost.'_serverpath">';
 1109:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1110:                 my $pathname = $path;
 1111:                 if ($path eq 'custom') {
 1112:                     $pathname = &mt('Custom Path').' ->';
 1113:                 }
 1114:                 my $selected = '';
 1115:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1116:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1117:                         $selected = 'selected="selected"';
 1118:                     }
 1119:                 } elsif ($path eq '') {
 1120:                     $selected = 'selected="selected"';
 1121:                 }
 1122:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1123:             }
 1124:             $datatable .= '</select></td>';
 1125:             my ($custom,$exempt);
 1126:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1127:                 $custom = $disallowed{$lonhost}{'custompath'};
 1128:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1129:             }
 1130:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1131:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1132:                           '</tr>';
 1133:         }
 1134:         $datatable .= '</table></td></tr>';
 1135:         return $datatable;
 1136:     } elsif ($caller eq 'page') {
 1137:         my %defaultchecked = ( 
 1138:                                'coursecatalog' => 'on',
 1139:                                'helpdesk'      => 'on',
 1140:                                'adminmail'     => 'off',
 1141:                                'newuser'       => 'off',
 1142:                              );
 1143:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1144:         my (%checkedon,%checkedoff);
 1145:         foreach my $item (@toggles) {
 1146:             if ($defaultchecked{$item} eq 'on') { 
 1147:                 $checkedon{$item} = ' checked="checked" ';
 1148:                 $checkedoff{$item} = ' ';
 1149:             } elsif ($defaultchecked{$item} eq 'off') {
 1150:                 $checkedoff{$item} = ' checked="checked" ';
 1151:                 $checkedon{$item} = ' ';
 1152:             }
 1153:         }
 1154:         my @images = ('img','logo','domlogo','login');
 1155:         my @logintext = ('textcol','bgcol');
 1156:         my @bgs = ('pgbg','mainbg','sidebg');
 1157:         my @links = ('link','alink','vlink');
 1158:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1159:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1160:         my (%is_custom,%designs);
 1161:         my %defaults = (
 1162:                        font => $defaultdesign{'login.font'},
 1163:                        );
 1164:         foreach my $item (@images) {
 1165:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1166:             $defaults{'showlogo'}{$item} = 1;
 1167:         }
 1168:         foreach my $item (@bgs) {
 1169:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1170:         }
 1171:         foreach my $item (@logintext) {
 1172:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1173:         }
 1174:         foreach my $item (@links) {
 1175:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1176:         }
 1177:         if (ref($settings) eq 'HASH') {
 1178:             foreach my $item (@toggles) {
 1179:                 if ($settings->{$item} eq '1') {
 1180:                     $checkedon{$item} =  ' checked="checked" ';
 1181:                     $checkedoff{$item} = ' ';
 1182:                 } elsif ($settings->{$item} eq '0') {
 1183:                     $checkedoff{$item} =  ' checked="checked" ';
 1184:                     $checkedon{$item} = ' ';
 1185:                 }
 1186:             }
 1187:             foreach my $item (@images) {
 1188:                 if (defined($settings->{$item})) {
 1189:                     $designs{$item} = $settings->{$item};
 1190:                     $is_custom{$item} = 1;
 1191:                 }
 1192:                 if (defined($settings->{'showlogo'}{$item})) {
 1193:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1194:                 }
 1195:             }
 1196:             foreach my $item (@logintext) {
 1197:                 if ($settings->{$item} ne '') {
 1198:                     $designs{'logintext'}{$item} = $settings->{$item};
 1199:                     $is_custom{$item} = 1;
 1200:                 }
 1201:             }
 1202:             if ($settings->{'font'} ne '') {
 1203:                 $designs{'font'} = $settings->{'font'};
 1204:                 $is_custom{'font'} = 1;
 1205:             }
 1206:             foreach my $item (@bgs) {
 1207:                 if ($settings->{$item} ne '') {
 1208:                     $designs{'bgs'}{$item} = $settings->{$item};
 1209:                     $is_custom{$item} = 1;
 1210:                 }
 1211:             }
 1212:             foreach my $item (@links) {
 1213:                 if ($settings->{$item} ne '') {
 1214:                     $designs{'links'}{$item} = $settings->{$item};
 1215:                     $is_custom{$item} = 1;
 1216:                 }
 1217:             }
 1218:         } else {
 1219:             if ($designhash{$dom.'.login.font'} ne '') {
 1220:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1221:                 $is_custom{'font'} = 1;
 1222:             }
 1223:             foreach my $item (@images) {
 1224:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1225:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1226:                     $is_custom{$item} = 1;
 1227:                 }
 1228:             }
 1229:             foreach my $item (@bgs) {
 1230:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1231:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1232:                     $is_custom{$item} = 1;
 1233:                 }
 1234:             }
 1235:             foreach my $item (@links) {
 1236:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1237:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1238:                     $is_custom{$item} = 1;
 1239:                 }
 1240:             }
 1241:         }
 1242:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1243:                                                       logo => 'Institution Logo',
 1244:                                                       domlogo => 'Domain Logo',
 1245:                                                       login => 'Login box');
 1246:         my $itemcount = 1;
 1247:         foreach my $item (@toggles) {
 1248:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1249:             $datatable .=  
 1250:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1251:                 '</td><td>'.
 1252:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1253:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1254:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1255:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1256:                 '</tr>';
 1257:             $itemcount ++;
 1258:         }
 1259:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1260:         $datatable .= '</tr></table></td></tr>';
 1261:     } elsif ($caller eq 'help') {
 1262:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1263:         my $switchserver = &check_switchserver($dom,$confname);
 1264:         my $itemcount = 1;
 1265:         $defaulturl = '/adm/loginproblems.html';
 1266:         $defaulttype = 'default';
 1267:         %lt = &Apache::lonlocal::texthash (
 1268:                      del     => 'Delete?',
 1269:                      rep     => 'Replace:',
 1270:                      upl     => 'Upload:',
 1271:                      default => 'Default',
 1272:                      custom  => 'Custom',
 1273:                                              );
 1274:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1275:         my @currlangs;
 1276:         if (ref($settings) eq 'HASH') {
 1277:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1278:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1279:                     next if ($settings->{'helpurl'}{$key} eq '');
 1280:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1281:                     $type{$key} = 'custom';
 1282:                     unless ($key eq 'nolang') {
 1283:                         push(@currlangs,$key);
 1284:                     }
 1285:                 }
 1286:             } elsif ($settings->{'helpurl'} ne '') {
 1287:                 $type{'nolang'} = 'custom';
 1288:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1289:             }
 1290:         }
 1291:         foreach my $lang ('nolang',sort(@currlangs)) {
 1292:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1293:             $datatable .= '<tr'.$css_class.'>';
 1294:             if ($url{$lang} eq '') {
 1295:                 $url{$lang} = $defaulturl;
 1296:             }
 1297:             if ($type{$lang} eq '') {
 1298:                 $type{$lang} = $defaulttype;
 1299:             }
 1300:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1301:             if ($lang eq 'nolang') {
 1302:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1303:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1304:             } else {
 1305:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1306:                                   $langchoices{$lang},
 1307:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1308:             }
 1309:             $datatable .= '</span></td>'."\n".
 1310:                           '<td class="LC_left_item">';
 1311:             if ($type{$lang} eq 'custom') {
 1312:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1313:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1314:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1315:             } else {
 1316:                 $datatable .= $lt{'upl'};
 1317:             }
 1318:             $datatable .='<br />';
 1319:             if ($switchserver) {
 1320:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1321:             } else {
 1322:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1323:             }
 1324:             $datatable .= '</td></tr>';
 1325:             $itemcount ++;
 1326:         }
 1327:         my @addlangs;
 1328:         foreach my $lang (sort(keys(%langchoices))) {
 1329:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1330:             push(@addlangs,$lang);
 1331:         }
 1332:         if (@addlangs > 0) {
 1333:             my %toadd;
 1334:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1335:             $toadd{''} = &mt('Select');
 1336:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1337:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1338:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1339:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1340:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1341:             if ($switchserver) {
 1342:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1343:             } else {
 1344:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1345:             }
 1346:             $datatable .= '</td></tr>';
 1347:             $itemcount ++;
 1348:         }
 1349:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1350:     } elsif ($caller eq 'headtag') {
 1351:         my %domservers = &Apache::lonnet::get_servers($dom);
 1352:         my $choice = $choices{'headtag'};
 1353:         $css_class = ' class="LC_odd_row"';
 1354:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1355:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1356:                       '<th>'.$choices{'current'}.'</th>'.
 1357:                       '<th>'.$choices{'action'}.'</th>'.
 1358:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1359:         my (%currurls,%currexempt);
 1360:         if (ref($settings) eq 'HASH') {
 1361:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1362:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1363:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1364:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1365:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1366:                     }
 1367:                 }
 1368:             }
 1369:         }
 1370:         my %lt = &Apache::lonlocal::texthash(
 1371:                                                del  => 'Delete?',
 1372:                                                rep  => 'Replace:',
 1373:                                                upl  => 'Upload:',
 1374:                                                curr => 'View contents',
 1375:                                                none => 'None',
 1376:         );
 1377:         my $switchserver = &check_switchserver($dom,$confname);
 1378:         foreach my $lonhost (sort(keys(%domservers))) {
 1379:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1380:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1381:             if ($currurls{$lonhost}) {
 1382:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1383:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1384:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1385:                               '">'.$lt{'curr'}.'</a></td>'.
 1386:                               '<td><span class="LC_nobreak"><label>'.
 1387:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1388:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1389:             } else {
 1390:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1391:             }
 1392:             $datatable .='<br />';
 1393:             if ($switchserver) {
 1394:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1395:             } else {
 1396:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1397:             }
 1398:             $datatable .= '</td><td><input type="textbox" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1399:         }
 1400:         $datatable .= '</table></td></tr>';
 1401:     }
 1402:     return $datatable;
 1403: }
 1404: 
 1405: sub login_choices {
 1406:     my %choices =
 1407:         &Apache::lonlocal::texthash (
 1408:             coursecatalog => 'Display Course/Community Catalog link?',
 1409:             adminmail     => "Display Administrator's E-mail Address?",
 1410:             helpdesk      => 'Display "Contact Helpdesk" link',
 1411:             disallowlogin => "Login page requests redirected",
 1412:             hostid        => "Server",
 1413:             server        => "Redirect to:",
 1414:             serverpath    => "Path",
 1415:             custompath    => "Custom", 
 1416:             exempt        => "Exempt IP(s)",
 1417:             directlogin   => "No redirect",
 1418:             newuser       => "Link to create a user account",
 1419:             img           => "Header",
 1420:             logo          => "Main Logo",
 1421:             domlogo       => "Domain Logo",
 1422:             login         => "Log-in Header", 
 1423:             textcol       => "Text color",
 1424:             bgcol         => "Box color",
 1425:             bgs           => "Background colors",
 1426:             links         => "Link colors",
 1427:             font          => "Font color",
 1428:             pgbg          => "Header",
 1429:             mainbg        => "Page",
 1430:             sidebg        => "Login box",
 1431:             link          => "Link",
 1432:             alink         => "Active link",
 1433:             vlink         => "Visited link",
 1434:             headtag       => "Custom markup",
 1435:             action        => "Action",
 1436:             current       => "Current",
 1437:         );
 1438:     return %choices;
 1439: }
 1440: 
 1441: sub print_rolecolors {
 1442:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1443:     my %choices = &color_font_choices();
 1444:     my @bgs = ('pgbg','tabbg','sidebg');
 1445:     my @links = ('link','alink','vlink');
 1446:     my @images = ('img');
 1447:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1448:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1449:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1450:     my (%is_custom,%designs);
 1451:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1452:     if (ref($settings) eq 'HASH') {
 1453:         if (ref($settings->{$role}) eq 'HASH') {
 1454:             if ($settings->{$role}->{'img'} ne '') {
 1455:                 $designs{'img'} = $settings->{$role}->{'img'};
 1456:                 $is_custom{'img'} = 1;
 1457:             }
 1458:             if ($settings->{$role}->{'font'} ne '') {
 1459:                 $designs{'font'} = $settings->{$role}->{'font'};
 1460:                 $is_custom{'font'} = 1;
 1461:             }
 1462:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1463:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1464:                 $is_custom{'fontmenu'} = 1;
 1465:             }
 1466:             foreach my $item (@bgs) {
 1467:                 if ($settings->{$role}->{$item} ne '') {
 1468:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1469:                     $is_custom{$item} = 1;
 1470:                 }
 1471:             }
 1472:             foreach my $item (@links) {
 1473:                 if ($settings->{$role}->{$item} ne '') {
 1474:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1475:                     $is_custom{$item} = 1;
 1476:                 }
 1477:             }
 1478:         }
 1479:     } else {
 1480:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1481:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1482:             $is_custom{'img'} = 1;
 1483:         }
 1484:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1485:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1486:             $is_custom{'fontmenu'} = 1; 
 1487:         }
 1488:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1489:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1490:             $is_custom{'font'} = 1;
 1491:         }
 1492:         foreach my $item (@bgs) {
 1493:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1494:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1495:                 $is_custom{$item} = 1;
 1496:             
 1497:             }
 1498:         }
 1499:         foreach my $item (@links) {
 1500:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1501:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1502:                 $is_custom{$item} = 1;
 1503:             }
 1504:         }
 1505:     }
 1506:     my $itemcount = 1;
 1507:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1508:     $datatable .= '</tr></table></td></tr>';
 1509:     return $datatable;
 1510: }
 1511: 
 1512: sub role_defaults {
 1513:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1514:     my %defaults;
 1515:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1516:         return %defaults;
 1517:     }
 1518:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1519:     if ($role eq 'login') {
 1520:         %defaults = (
 1521:                        font => $defaultdesign{$role.'.font'},
 1522:                     );
 1523:         if (ref($logintext) eq 'ARRAY') {
 1524:             foreach my $item (@{$logintext}) {
 1525:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1526:             }
 1527:         }
 1528:         foreach my $item (@{$images}) {
 1529:             $defaults{'showlogo'}{$item} = 1;
 1530:         }
 1531:     } else {
 1532:         %defaults = (
 1533:                        img => $defaultdesign{$role.'.img'},
 1534:                        font => $defaultdesign{$role.'.font'},
 1535:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1536:                     );
 1537:     }
 1538:     foreach my $item (@{$bgs}) {
 1539:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1540:     }
 1541:     foreach my $item (@{$links}) {
 1542:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1543:     }
 1544:     foreach my $item (@{$images}) {
 1545:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1546:     }
 1547:     return %defaults;
 1548: }
 1549: 
 1550: sub display_color_options {
 1551:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1552:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1553:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1554:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1555:     my $datatable = '<tr'.$css_class.'>'.
 1556:         '<td>'.$choices->{'font'}.'</td>';
 1557:     if (!$is_custom->{'font'}) {
 1558:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1559:     } else {
 1560:         $datatable .= '<td>&nbsp;</td>';
 1561:     }
 1562:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1563: 
 1564:     $datatable .= '<td><span class="LC_nobreak">'.
 1565:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1566:                   ' value="'.$current_color.'" />&nbsp;'.
 1567:                   '&nbsp;</td></tr>';
 1568:     unless ($role eq 'login') { 
 1569:         $datatable .= '<tr'.$css_class.'>'.
 1570:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1571:         if (!$is_custom->{'fontmenu'}) {
 1572:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1573:         } else {
 1574:             $datatable .= '<td>&nbsp;</td>';
 1575:         }
 1576: 	$current_color = $designs->{'fontmenu'} ?
 1577: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1578:         $datatable .= '<td><span class="LC_nobreak">'.
 1579:                       '<input class="colorchooser" type="text" size="10" name="'
 1580: 		      .$role.'_fontmenu"'.
 1581:                       ' value="'.$current_color.'" />&nbsp;'.
 1582:                       '&nbsp;</td></tr>';
 1583:     }
 1584:     my $switchserver = &check_switchserver($dom,$confname);
 1585:     foreach my $img (@{$images}) {
 1586: 	$itemcount ++;
 1587:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1588:         $datatable .= '<tr'.$css_class.'>'.
 1589:                       '<td>'.$choices->{$img};
 1590:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1591:         if ($role eq 'login') {
 1592:             if ($img eq 'login') {
 1593:                 $login_hdr_pick =
 1594:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1595:                 $logincolors =
 1596:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1597:                                        $designs,$defaults);
 1598:             } elsif ($img ne 'domlogo') {
 1599:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1600:             }
 1601:         }
 1602:         $datatable .= '</td>';
 1603:         if ($designs->{$img} ne '') {
 1604:             $imgfile = $designs->{$img};
 1605: 	    $img_import = ($imgfile =~ m{^/adm/});
 1606:         } else {
 1607:             $imgfile = $defaults->{$img};
 1608:         }
 1609:         if ($imgfile) {
 1610:             my ($showfile,$fullsize);
 1611:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1612:                 my $urldir = $1;
 1613:                 my $filename = $2;
 1614:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1615:                 if (@info) {
 1616:                     my $thumbfile = 'tn-'.$filename;
 1617:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1618:                     if (@thumb) {
 1619:                         $showfile = $urldir.'/'.$thumbfile;
 1620:                     } else {
 1621:                         $showfile = $imgfile;
 1622:                     }
 1623:                 } else {
 1624:                     $showfile = '';
 1625:                 }
 1626:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1627:                 $showfile = $imgfile;
 1628:                 my $imgdir = $1;
 1629:                 my $filename = $2;
 1630:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1631:                     $showfile = "/$imgdir/tn-".$filename;
 1632:                 } else {
 1633:                     my $input = $londocroot.$imgfile;
 1634:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1635:                     if (!-e $output) {
 1636:                         my ($width,$height) = &thumb_dimensions();
 1637:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1638:                         if ($fullwidth ne '' && $fullheight ne '') {
 1639:                             if ($fullwidth > $width && $fullheight > $height) { 
 1640:                                 my $size = $width.'x'.$height;
 1641:                                 system("convert -sample $size $input $output");
 1642:                                 $showfile = "/$imgdir/tn-".$filename;
 1643:                             }
 1644:                         }
 1645:                     }
 1646:                 }
 1647:             }
 1648:             if ($showfile) {
 1649:                 if ($showfile =~ m{^/(adm|res)/}) {
 1650:                     if ($showfile =~ m{^/res/}) {
 1651:                         my $local_showfile =
 1652:                             &Apache::lonnet::filelocation('',$showfile);
 1653:                         &Apache::lonnet::repcopy($local_showfile);
 1654:                     }
 1655:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1656:                 }
 1657:                 if ($imgfile) {
 1658:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1659:                         if ($imgfile =~ m{^/res/}) {
 1660:                             my $local_imgfile =
 1661:                                 &Apache::lonnet::filelocation('',$imgfile);
 1662:                             &Apache::lonnet::repcopy($local_imgfile);
 1663:                         }
 1664:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1665:                     } else {
 1666:                         $fullsize = $imgfile;
 1667:                     }
 1668:                 }
 1669:                 $datatable .= '<td>';
 1670:                 if ($img eq 'login') {
 1671:                     $datatable .= $login_hdr_pick;
 1672:                 } 
 1673:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1674:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1675:             } else {
 1676:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1677:                               &mt('Upload:').'<br />';
 1678:             }
 1679:         } else {
 1680:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1681:                           &mt('Upload:').'<br />';
 1682:         }
 1683:         if ($switchserver) {
 1684:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1685:         } else {
 1686:             if ($img ne 'login') { # suppress file selection for Log-in header
 1687:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1688:             }
 1689:         }
 1690:         $datatable .= '</td></tr>';
 1691:     }
 1692:     $itemcount ++;
 1693:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1694:     $datatable .= '<tr'.$css_class.'>'.
 1695:                   '<td>'.$choices->{'bgs'}.'</td>';
 1696:     my $bgs_def;
 1697:     foreach my $item (@{$bgs}) {
 1698:         if (!$is_custom->{$item}) {
 1699:             $bgs_def .= '<td><span class="LC_nobreak">'.$choices->{$item}.'</span>&nbsp;<span id="css_default_'.$role.'_'.$item.'" style="background-color: '.$defaults->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span><br />'.$defaults->{'bgs'}{$item}.'</td>';
 1700:         }
 1701:     }
 1702:     if ($bgs_def) {
 1703:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1704:     } else {
 1705:         $datatable .= '<td>&nbsp;</td>';
 1706:     }
 1707:     $datatable .= '<td class="LC_right_item">'.
 1708:                   '<table border="0"><tr>';
 1709: 
 1710:     foreach my $item (@{$bgs}) {
 1711:         $datatable .= '<td align="center">'.$choices->{$item};
 1712: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1713:         if ($designs->{'bgs'}{$item}) {
 1714:             $datatable .= '&nbsp;';
 1715:         }
 1716:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1717:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1718:     }
 1719:     $datatable .= '</tr></table></td></tr>';
 1720:     $itemcount ++;
 1721:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1722:     $datatable .= '<tr'.$css_class.'>'.
 1723:                   '<td>'.$choices->{'links'}.'</td>';
 1724:     my $links_def;
 1725:     foreach my $item (@{$links}) {
 1726:         if (!$is_custom->{$item}) {
 1727:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1728:         }
 1729:     }
 1730:     if ($links_def) {
 1731:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1732:     } else {
 1733:         $datatable .= '<td>&nbsp;</td>';
 1734:     }
 1735:     $datatable .= '<td class="LC_right_item">'.
 1736:                   '<table border="0"><tr>';
 1737:     foreach my $item (@{$links}) {
 1738: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1739:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1740:         if ($designs->{'links'}{$item}) {
 1741:             $datatable.='&nbsp;';
 1742:         }
 1743:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1744:                       '" /></td>';
 1745:     }
 1746:     $$rowtotal += $itemcount;
 1747:     return $datatable;
 1748: }
 1749: 
 1750: sub logo_display_options {
 1751:     my ($img,$defaults,$designs) = @_;
 1752:     my $checkedon;
 1753:     if (ref($defaults) eq 'HASH') {
 1754:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1755:             if ($defaults->{'showlogo'}{$img}) {
 1756:                 $checkedon = 'checked="checked" ';     
 1757:             }
 1758:         } 
 1759:     }
 1760:     if (ref($designs) eq 'HASH') {
 1761:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1762:             if (defined($designs->{'showlogo'}{$img})) {
 1763:                 if ($designs->{'showlogo'}{$img} == 0) {
 1764:                     $checkedon = '';
 1765:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1766:                     $checkedon = 'checked="checked" ';
 1767:                 }
 1768:             }
 1769:         }
 1770:     }
 1771:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1772:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1773:            &mt('show').'</label>'."\n";
 1774: }
 1775: 
 1776: sub login_header_options  {
 1777:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1778:     my $output = '';
 1779:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1780:         $output .= &mt('Text default(s):').'<br />';
 1781:         if (!$is_custom->{'textcol'}) {
 1782:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1783:                        '&nbsp;&nbsp;&nbsp;';
 1784:         }
 1785:         if (!$is_custom->{'bgcol'}) {
 1786:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1787:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1788:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1789:         }
 1790:         $output .= '<br />';
 1791:     }
 1792:     $output .='<br />';
 1793:     return $output;
 1794: }
 1795: 
 1796: sub login_text_colors {
 1797:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1798:     my $color_menu = '<table border="0"><tr>';
 1799:     foreach my $item (@{$logintext}) {
 1800:         $color_menu .= '<td align="center">'.$choices->{$item};
 1801:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1802:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1803:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1804:     }
 1805:     $color_menu .= '</tr></table><br />';
 1806:     return $color_menu;
 1807: }
 1808: 
 1809: sub image_changes {
 1810:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1811:     my $output;
 1812:     if ($img eq 'login') {
 1813:             # suppress image for Log-in header
 1814:     } elsif (!$is_custom) {
 1815:         if ($img ne 'domlogo') {
 1816:             $output .= &mt('Default image:').'<br />';
 1817:         } else {
 1818:             $output .= &mt('Default in use:').'<br />';
 1819:         }
 1820:     }
 1821:     if ($img eq 'login') { # suppress image for Log-in header
 1822:         $output .= '<td>'.$logincolors;
 1823:     } else {
 1824:         if ($img_import) {
 1825:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1826:         }
 1827:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1828:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1829:         if ($is_custom) {
 1830:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1831:                        '<input type="checkbox" name="'.
 1832:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1833:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1834:         } else {
 1835:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1836:         }
 1837:     }
 1838:     return $output;
 1839: }
 1840: 
 1841: sub print_quotas {
 1842:     my ($dom,$settings,$rowtotal,$action) = @_;
 1843:     my $context;
 1844:     if ($action eq 'quotas') {
 1845:         $context = 'tools';
 1846:     } else {
 1847:         $context = $action;
 1848:     }
 1849:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1850:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1851:     my $typecount = 0;
 1852:     my ($css_class,%titles);
 1853:     if ($context eq 'requestcourses') {
 1854:         @usertools = ('official','unofficial','community','textbook','placement');
 1855:         @options =('norequest','approval','validate','autolimit');
 1856:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1857:         %titles = &courserequest_titles();
 1858:     } elsif ($context eq 'requestauthor') {
 1859:         @usertools = ('author');
 1860:         @options = ('norequest','approval','automatic');
 1861:         %titles = &authorrequest_titles();
 1862:     } else {
 1863:         @usertools = ('aboutme','blog','webdav','portfolio');
 1864:         %titles = &tool_titles();
 1865:     }
 1866:     if (ref($types) eq 'ARRAY') {
 1867:         foreach my $type (@{$types}) {
 1868:             my ($currdefquota,$currauthorquota);
 1869:             unless (($context eq 'requestcourses') ||
 1870:                     ($context eq 'requestauthor')) {
 1871:                 if (ref($settings) eq 'HASH') {
 1872:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1873:                         $currdefquota = $settings->{defaultquota}->{$type};
 1874:                     } else {
 1875:                         $currdefquota = $settings->{$type};
 1876:                     }
 1877:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1878:                         $currauthorquota = $settings->{authorquota}->{$type};
 1879:                     }
 1880:                 }
 1881:             }
 1882:             if (defined($usertypes->{$type})) {
 1883:                 $typecount ++;
 1884:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1885:                 $datatable .= '<tr'.$css_class.'>'.
 1886:                               '<td>'.$usertypes->{$type}.'</td>'.
 1887:                               '<td class="LC_left_item">';
 1888:                 if ($context eq 'requestcourses') {
 1889:                     $datatable .= '<table><tr>';
 1890:                 }
 1891:                 my %cell;  
 1892:                 foreach my $item (@usertools) {
 1893:                     if ($context eq 'requestcourses') {
 1894:                         my ($curroption,$currlimit);
 1895:                         if (ref($settings) eq 'HASH') {
 1896:                             if (ref($settings->{$item}) eq 'HASH') {
 1897:                                 $curroption = $settings->{$item}->{$type};
 1898:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1899:                                     $currlimit = $1; 
 1900:                                 }
 1901:                             }
 1902:                         }
 1903:                         if (!$curroption) {
 1904:                             $curroption = 'norequest';
 1905:                         }
 1906:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1907:                         foreach my $option (@options) {
 1908:                             my $val = $option;
 1909:                             if ($option eq 'norequest') {
 1910:                                 $val = 0;  
 1911:                             }
 1912:                             if ($option eq 'validate') {
 1913:                                 my $canvalidate = 0;
 1914:                                 if (ref($validations{$item}) eq 'HASH') { 
 1915:                                     if ($validations{$item}{$type}) {
 1916:                                         $canvalidate = 1;
 1917:                                     }
 1918:                                 }
 1919:                                 next if (!$canvalidate);
 1920:                             }
 1921:                             my $checked = '';
 1922:                             if ($option eq $curroption) {
 1923:                                 $checked = ' checked="checked"';
 1924:                             } elsif ($option eq 'autolimit') {
 1925:                                 if ($curroption =~ /^autolimit/) {
 1926:                                     $checked = ' checked="checked"';
 1927:                                 }                       
 1928:                             } 
 1929:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1930:                                   '<input type="radio" name="crsreq_'.$item.
 1931:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1932:                                   $titles{$option}.'</label>';
 1933:                             if ($option eq 'autolimit') {
 1934:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1935:                                                 $item.'_limit_'.$type.'" size="1" '.
 1936:                                                 'value="'.$currlimit.'" />';
 1937:                             }
 1938:                             $cell{$item} .= '</span> ';
 1939:                             if ($option eq 'autolimit') {
 1940:                                 $cell{$item} .= $titles{'unlimited'};
 1941:                             }
 1942:                         }
 1943:                     } elsif ($context eq 'requestauthor') {
 1944:                         my $curroption;
 1945:                         if (ref($settings) eq 'HASH') {
 1946:                             $curroption = $settings->{$type};
 1947:                         }
 1948:                         if (!$curroption) {
 1949:                             $curroption = 'norequest';
 1950:                         }
 1951:                         foreach my $option (@options) {
 1952:                             my $val = $option;
 1953:                             if ($option eq 'norequest') {
 1954:                                 $val = 0;
 1955:                             }
 1956:                             my $checked = '';
 1957:                             if ($option eq $curroption) {
 1958:                                 $checked = ' checked="checked"';
 1959:                             }
 1960:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1961:                                   '<input type="radio" name="authorreq_'.$type.
 1962:                                   '" value="'.$val.'"'.$checked.' />'.
 1963:                                   $titles{$option}.'</label></span>&nbsp; ';
 1964:                         }
 1965:                     } else {
 1966:                         my $checked = 'checked="checked" ';
 1967:                         if (ref($settings) eq 'HASH') {
 1968:                             if (ref($settings->{$item}) eq 'HASH') {
 1969:                                 if ($settings->{$item}->{$type} == 0) {
 1970:                                     $checked = '';
 1971:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1972:                                     $checked =  'checked="checked" ';
 1973:                                 }
 1974:                             }
 1975:                         }
 1976:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1977:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1978:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1979:                                       '</label></span>&nbsp; ';
 1980:                     }
 1981:                 }
 1982:                 if ($context eq 'requestcourses') {
 1983:                     $datatable .= '</tr><tr>';
 1984:                     foreach my $item (@usertools) {
 1985:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1986:                     }
 1987:                     $datatable .= '</tr></table>';
 1988:                 }
 1989:                 $datatable .= '</td>';
 1990:                 unless (($context eq 'requestcourses') ||
 1991:                         ($context eq 'requestauthor')) {
 1992:                     $datatable .= 
 1993:                               '<td class="LC_right_item">'.
 1994:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1995:                               '<input type="text" name="quota_'.$type.
 1996:                               '" value="'.$currdefquota.
 1997:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1998:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1999:                               '<input type="text" name="authorquota_'.$type.
 2000:                               '" value="'.$currauthorquota.
 2001:                               '" size="5" /></span></td>';
 2002:                 }
 2003:                 $datatable .= '</tr>';
 2004:             }
 2005:         }
 2006:     }
 2007:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2008:         $defaultquota = '20';
 2009:         $authorquota = '500';
 2010:         if (ref($settings) eq 'HASH') {
 2011:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2012:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2013:             } elsif (defined($settings->{'default'})) {
 2014:                 $defaultquota = $settings->{'default'};
 2015:             }
 2016:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 2017:                 $authorquota = $settings->{'authorquota'}->{'default'};
 2018:             }
 2019:         }
 2020:     }
 2021:     $typecount ++;
 2022:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2023:     $datatable .= '<tr'.$css_class.'>'.
 2024:                   '<td>'.$othertitle.'</td>'.
 2025:                   '<td class="LC_left_item">';
 2026:     if ($context eq 'requestcourses') {
 2027:         $datatable .= '<table><tr>';
 2028:     }
 2029:     my %defcell;
 2030:     foreach my $item (@usertools) {
 2031:         if ($context eq 'requestcourses') {
 2032:             my ($curroption,$currlimit);
 2033:             if (ref($settings) eq 'HASH') {
 2034:                 if (ref($settings->{$item}) eq 'HASH') {
 2035:                     $curroption = $settings->{$item}->{'default'};
 2036:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2037:                         $currlimit = $1;
 2038:                     }
 2039:                 }
 2040:             }
 2041:             if (!$curroption) {
 2042:                 $curroption = 'norequest';
 2043:             }
 2044:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2045:             foreach my $option (@options) {
 2046:                 my $val = $option;
 2047:                 if ($option eq 'norequest') {
 2048:                     $val = 0;
 2049:                 }
 2050:                 if ($option eq 'validate') {
 2051:                     my $canvalidate = 0;
 2052:                     if (ref($validations{$item}) eq 'HASH') {
 2053:                         if ($validations{$item}{'default'}) {
 2054:                             $canvalidate = 1;
 2055:                         }
 2056:                     }
 2057:                     next if (!$canvalidate);
 2058:                 }
 2059:                 my $checked = '';
 2060:                 if ($option eq $curroption) {
 2061:                     $checked = ' checked="checked"';
 2062:                 } elsif ($option eq 'autolimit') {
 2063:                     if ($curroption =~ /^autolimit/) {
 2064:                         $checked = ' checked="checked"';
 2065:                     }
 2066:                 }
 2067:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2068:                                   '<input type="radio" name="crsreq_'.$item.
 2069:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2070:                                   $titles{$option}.'</label>';
 2071:                 if ($option eq 'autolimit') {
 2072:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2073:                                        $item.'_limit_default" size="1" '.
 2074:                                        'value="'.$currlimit.'" />';
 2075:                 }
 2076:                 $defcell{$item} .= '</span> ';
 2077:                 if ($option eq 'autolimit') {
 2078:                     $defcell{$item} .= $titles{'unlimited'};
 2079:                 }
 2080:             }
 2081:         } elsif ($context eq 'requestauthor') {
 2082:             my $curroption;
 2083:             if (ref($settings) eq 'HASH') {
 2084:                 $curroption = $settings->{'default'};
 2085:             }
 2086:             if (!$curroption) {
 2087:                 $curroption = 'norequest';
 2088:             }
 2089:             foreach my $option (@options) {
 2090:                 my $val = $option;
 2091:                 if ($option eq 'norequest') {
 2092:                     $val = 0;
 2093:                 }
 2094:                 my $checked = '';
 2095:                 if ($option eq $curroption) {
 2096:                     $checked = ' checked="checked"';
 2097:                 }
 2098:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2099:                               '<input type="radio" name="authorreq_default"'.
 2100:                               ' value="'.$val.'"'.$checked.' />'.
 2101:                               $titles{$option}.'</label></span>&nbsp; ';
 2102:             }
 2103:         } else {
 2104:             my $checked = 'checked="checked" ';
 2105:             if (ref($settings) eq 'HASH') {
 2106:                 if (ref($settings->{$item}) eq 'HASH') {
 2107:                     if ($settings->{$item}->{'default'} == 0) {
 2108:                         $checked = '';
 2109:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2110:                         $checked = 'checked="checked" ';
 2111:                     }
 2112:                 }
 2113:             }
 2114:             $datatable .= '<span class="LC_nobreak"><label>'.
 2115:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2116:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2117:                           '</label></span>&nbsp; ';
 2118:         }
 2119:     }
 2120:     if ($context eq 'requestcourses') {
 2121:         $datatable .= '</tr><tr>';
 2122:         foreach my $item (@usertools) {
 2123:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2124:         }
 2125:         $datatable .= '</tr></table>';
 2126:     }
 2127:     $datatable .= '</td>';
 2128:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2129:         $datatable .= '<td class="LC_right_item">'.
 2130:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2131:                       '<input type="text" name="defaultquota" value="'.
 2132:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2133:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2134:                       '<input type="text" name="authorquota" value="'.
 2135:                       $authorquota.'" size="5" /></span></td>';
 2136:     }
 2137:     $datatable .= '</tr>';
 2138:     $typecount ++;
 2139:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2140:     $datatable .= '<tr'.$css_class.'>'.
 2141:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2142:     if ($context eq 'requestcourses') {
 2143:         $datatable .= &mt('(overrides affiliation, if set)').
 2144:                       '</td>'.
 2145:                       '<td class="LC_left_item">'.
 2146:                       '<table><tr>';
 2147:     } else {
 2148:         $datatable .= &mt('(overrides affiliation, if checked)').
 2149:                       '</td>'.
 2150:                       '<td class="LC_left_item" colspan="2">'.
 2151:                       '<br />';
 2152:     }
 2153:     my %advcell;
 2154:     foreach my $item (@usertools) {
 2155:         if ($context eq 'requestcourses') {
 2156:             my ($curroption,$currlimit);
 2157:             if (ref($settings) eq 'HASH') {
 2158:                 if (ref($settings->{$item}) eq 'HASH') {
 2159:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2160:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2161:                         $currlimit = $1;
 2162:                     }
 2163:                 }
 2164:             }
 2165:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2166:             my $checked = '';
 2167:             if ($curroption eq '') {
 2168:                 $checked = ' checked="checked"';
 2169:             }
 2170:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2171:                                '<input type="radio" name="crsreq_'.$item.
 2172:                                '__LC_adv" value=""'.$checked.' />'.
 2173:                                &mt('No override set').'</label></span>&nbsp; ';
 2174:             foreach my $option (@options) {
 2175:                 my $val = $option;
 2176:                 if ($option eq 'norequest') {
 2177:                     $val = 0;
 2178:                 }
 2179:                 if ($option eq 'validate') {
 2180:                     my $canvalidate = 0;
 2181:                     if (ref($validations{$item}) eq 'HASH') {
 2182:                         if ($validations{$item}{'_LC_adv'}) {
 2183:                             $canvalidate = 1;
 2184:                         }
 2185:                     }
 2186:                     next if (!$canvalidate);
 2187:                 }
 2188:                 my $checked = '';
 2189:                 if ($val eq $curroption) {
 2190:                     $checked = ' checked="checked"';
 2191:                 } elsif ($option eq 'autolimit') {
 2192:                     if ($curroption =~ /^autolimit/) {
 2193:                         $checked = ' checked="checked"';
 2194:                     }
 2195:                 }
 2196:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2197:                                   '<input type="radio" name="crsreq_'.$item.
 2198:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2199:                                   $titles{$option}.'</label>';
 2200:                 if ($option eq 'autolimit') {
 2201:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2202:                                        $item.'_limit__LC_adv" size="1" '.
 2203:                                        'value="'.$currlimit.'" />';
 2204:                 }
 2205:                 $advcell{$item} .= '</span> ';
 2206:                 if ($option eq 'autolimit') {
 2207:                     $advcell{$item} .= $titles{'unlimited'};
 2208:                 }
 2209:             }
 2210:         } elsif ($context eq 'requestauthor') {
 2211:             my $curroption;
 2212:             if (ref($settings) eq 'HASH') {
 2213:                 $curroption = $settings->{'_LC_adv'};
 2214:             }
 2215:             my $checked = '';
 2216:             if ($curroption eq '') {
 2217:                 $checked = ' checked="checked"';
 2218:             }
 2219:             $datatable .= '<span class="LC_nobreak"><label>'.
 2220:                           '<input type="radio" name="authorreq__LC_adv"'.
 2221:                           ' value=""'.$checked.' />'.
 2222:                           &mt('No override set').'</label></span>&nbsp; ';
 2223:             foreach my $option (@options) {
 2224:                 my $val = $option;
 2225:                 if ($option eq 'norequest') {
 2226:                     $val = 0;
 2227:                 }
 2228:                 my $checked = '';
 2229:                 if ($val eq $curroption) {
 2230:                     $checked = ' checked="checked"';
 2231:                 }
 2232:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2233:                               '<input type="radio" name="authorreq__LC_adv"'.
 2234:                               ' value="'.$val.'"'.$checked.' />'.
 2235:                               $titles{$option}.'</label></span>&nbsp; ';
 2236:             }
 2237:         } else {
 2238:             my $checked = 'checked="checked" ';
 2239:             if (ref($settings) eq 'HASH') {
 2240:                 if (ref($settings->{$item}) eq 'HASH') {
 2241:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2242:                         $checked = '';
 2243:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2244:                         $checked = 'checked="checked" ';
 2245:                     }
 2246:                 }
 2247:             }
 2248:             $datatable .= '<span class="LC_nobreak"><label>'.
 2249:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2250:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2251:                           '</label></span>&nbsp; ';
 2252:         }
 2253:     }
 2254:     if ($context eq 'requestcourses') {
 2255:         $datatable .= '</tr><tr>';
 2256:         foreach my $item (@usertools) {
 2257:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2258:         }
 2259:         $datatable .= '</tr></table>';
 2260:     }
 2261:     $datatable .= '</td></tr>';
 2262:     $$rowtotal += $typecount;
 2263:     return $datatable;
 2264: }
 2265: 
 2266: sub print_requestmail {
 2267:     my ($dom,$action,$settings,$rowtotal) = @_;
 2268:     my ($now,$datatable,%currapp);
 2269:     $now = time;
 2270:     if (ref($settings) eq 'HASH') {
 2271:         if (ref($settings->{'notify'}) eq 'HASH') {
 2272:             if ($settings->{'notify'}{'approval'} ne '') {
 2273:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2274:             }
 2275:         }
 2276:     }
 2277:     my $numinrow = 2;
 2278:     my $css_class;
 2279:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2280:     my $text;
 2281:     if ($action eq 'requestcourses') {
 2282:         $text = &mt('Receive notification of course requests requiring approval');
 2283:     } elsif ($action eq 'requestauthor') {
 2284:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2285:     } else {
 2286:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2287:     }
 2288:     $datatable = '<tr'.$css_class.'>'.
 2289:                  ' <td>'.$text.'</td>'.
 2290:                  ' <td class="LC_left_item">';
 2291:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2292:                                                  $action.'notifyapproval',%currapp);
 2293:     if ($numdc > 0) {
 2294:         $datatable .= $table;
 2295:     } else {
 2296:         $datatable .= &mt('There are no active Domain Coordinators');
 2297:     }
 2298:     $datatable .='</td></tr>';
 2299:     return $datatable;
 2300: }
 2301: 
 2302: sub print_studentcode {
 2303:     my ($settings,$rowtotal) = @_;
 2304:     my $rownum = 0; 
 2305:     my ($output,%current);
 2306:     my @crstypes = ('official','unofficial','community','textbook','placement');
 2307:     if (ref($settings) eq 'HASH') {
 2308:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2309:             foreach my $type (@crstypes) {
 2310:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2311:             }
 2312:         }
 2313:     }
 2314:     $output .= '<tr>'.
 2315:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2316:                '<td class="LC_left_item">';
 2317:     foreach my $type (@crstypes) {
 2318:         my $check = ' ';
 2319:         if ($current{$type}) {
 2320:             $check = ' checked="checked" ';
 2321:         }
 2322:         $output .= '<span class="LC_nobreak"><label>'.
 2323:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2324:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2325:     }
 2326:     $output .= '</td></tr>';
 2327:     $$rowtotal ++;
 2328:     return $output;
 2329: }
 2330: 
 2331: sub print_textbookcourses {
 2332:     my ($dom,$type,$settings,$rowtotal) = @_;
 2333:     my $rownum = 0;
 2334:     my $css_class;
 2335:     my $itemcount = 1;
 2336:     my $maxnum = 0;
 2337:     my $bookshash;
 2338:     if (ref($settings) eq 'HASH') {
 2339:         $bookshash = $settings->{$type};
 2340:     }
 2341:     my %ordered;
 2342:     if (ref($bookshash) eq 'HASH') {
 2343:         foreach my $item (keys(%{$bookshash})) {
 2344:             if (ref($bookshash->{$item}) eq 'HASH') {
 2345:                 my $num = $bookshash->{$item}{'order'};
 2346:                 $ordered{$num} = $item;
 2347:             }
 2348:         }
 2349:     }
 2350:     my $confname = $dom.'-domainconfig';
 2351:     my $switchserver = &check_switchserver($dom,$confname);
 2352:     my $maxnum = scalar(keys(%ordered));
 2353:     my $datatable;
 2354:     if (keys(%ordered)) {
 2355:         my @items = sort { $a <=> $b } keys(%ordered);
 2356:         for (my $i=0; $i<@items; $i++) {
 2357:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2358:             my $key = $ordered{$items[$i]};
 2359:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2360:             my $coursetitle = $coursehash{'description'};
 2361:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2362:             if (ref($bookshash->{$key}) eq 'HASH') {
 2363:                 $subject = $bookshash->{$key}->{'subject'};
 2364:                 $title = $bookshash->{$key}->{'title'};
 2365:                 if ($type eq 'textbooks') {
 2366:                     $publisher = $bookshash->{$key}->{'publisher'};
 2367:                     $author = $bookshash->{$key}->{'author'};
 2368:                     $image = $bookshash->{$key}->{'image'};
 2369:                     if ($image ne '') {
 2370:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2371:                         my $imagethumb = "$path/tn-".$imagefile;
 2372:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2373:                     }
 2374:                 }
 2375:             }
 2376:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2377:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2378:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2379:             for (my $k=0; $k<=$maxnum; $k++) {
 2380:                 my $vpos = $k+1;
 2381:                 my $selstr;
 2382:                 if ($k == $i) {
 2383:                     $selstr = ' selected="selected" ';
 2384:                 }
 2385:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2386:             }
 2387:             $datatable .= '</select>'.('&nbsp;'x2).
 2388:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2389:                 &mt('Delete?').'</label></span></td>'.
 2390:                 '<td colspan="2">'.
 2391:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2392:                 ('&nbsp;'x2).
 2393:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2394:             if ($type eq 'textbooks') {
 2395:                 $datatable .= ('&nbsp;'x2).
 2396:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2397:                               ('&nbsp;'x2).
 2398:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2399:                               ('&nbsp;'x2).
 2400:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2401:                 if ($image) {
 2402:                     $datatable .= $imgsrc.
 2403:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2404:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2405:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2406:                 }
 2407:                 if ($switchserver) {
 2408:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2409:                 } else {
 2410:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2411:                 }
 2412:             }
 2413:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2414:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2415:                           $coursetitle.'</span></td></tr>'."\n";
 2416:             $itemcount ++;
 2417:         }
 2418:     }
 2419:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2420:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2421:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2422:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2423:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2424:     for (my $k=0; $k<$maxnum+1; $k++) {
 2425:         my $vpos = $k+1;
 2426:         my $selstr;
 2427:         if ($k == $maxnum) {
 2428:             $selstr = ' selected="selected" ';
 2429:         }
 2430:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2431:     }
 2432:     $datatable .= '</select>&nbsp;'."\n".
 2433:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2434:                   '<td colspan="2">'.
 2435:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2436:                   ('&nbsp;'x2).
 2437:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2438:                   ('&nbsp;'x2);
 2439:     if ($type eq 'textbooks') {
 2440:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2441:                       ('&nbsp;'x2).
 2442:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2443:                       ('&nbsp;'x2).
 2444:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2445:         if ($switchserver) {
 2446:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2447:         } else {
 2448:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2449:         }
 2450:     }
 2451:     $datatable .= '</span>'."\n".
 2452:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2453:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2454:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2455:                   &Apache::loncommon::selectcourse_link
 2456:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2457:                   '</span></td>'."\n".
 2458:                   '</tr>'."\n";
 2459:     $itemcount ++;
 2460:     return $datatable;
 2461: }
 2462: 
 2463: sub textbookcourses_javascript {
 2464:     my ($settings) = @_;
 2465:     return unless(ref($settings) eq 'HASH');
 2466:     my (%ordered,%total,%jstext);
 2467:     foreach my $type ('textbooks','templates') {
 2468:         $total{$type} = 0;
 2469:         if (ref($settings->{$type}) eq 'HASH') {
 2470:             foreach my $item (keys(%{$settings->{$type}})) {
 2471:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2472:                     my $num = $settings->{$type}->{$item}{'order'};
 2473:                     $ordered{$type}{$num} = $item;
 2474:                 }
 2475:             }
 2476:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2477:         }
 2478:         my @jsarray = ();
 2479:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2480:             push(@jsarray,$ordered{$type}{$item});
 2481:         }
 2482:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2483:     }
 2484:     return <<"ENDSCRIPT";
 2485: <script type="text/javascript">
 2486: // <![CDATA[
 2487: function reorderBooks(form,item,caller) {
 2488:     var changedVal;
 2489: $jstext{'textbooks'};
 2490: $jstext{'templates'};
 2491:     var newpos;
 2492:     var maxh;
 2493:     if (caller == 'textbooks') {  
 2494:         newpos = 'textbooks_addbook_pos';
 2495:         maxh = 1 + $total{'textbooks'};
 2496:     } else {
 2497:         newpos = 'templates_addbook_pos';
 2498:         maxh = 1 + $total{'templates'};
 2499:     }
 2500:     var current = new Array;
 2501:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2502:     if (item == newpos) {
 2503:         changedVal = newitemVal;
 2504:     } else {
 2505:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2506:         current[newitemVal] = newpos;
 2507:     }
 2508:     if (caller == 'textbooks') {
 2509:         for (var i=0; i<textbooks.length; i++) {
 2510:             var elementName = 'textbooks_'+textbooks[i];
 2511:             if (elementName != item) {
 2512:                 if (form.elements[elementName]) {
 2513:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2514:                     current[currVal] = elementName;
 2515:                 }
 2516:             }
 2517:         }
 2518:     }
 2519:     if (caller == 'templates') {
 2520:         for (var i=0; i<templates.length; i++) {
 2521:             var elementName = 'templates_'+templates[i];
 2522:             if (elementName != item) {
 2523:                 if (form.elements[elementName]) {
 2524:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2525:                     current[currVal] = elementName;
 2526:                 }
 2527:             }
 2528:         }
 2529:     }
 2530:     var oldVal;
 2531:     for (var j=0; j<maxh; j++) {
 2532:         if (current[j] == undefined) {
 2533:             oldVal = j;
 2534:         }
 2535:     }
 2536:     if (oldVal < changedVal) {
 2537:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2538:            var elementName = current[k];
 2539:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2540:         }
 2541:     } else {
 2542:         for (var k=changedVal; k<oldVal; k++) {
 2543:             var elementName = current[k];
 2544:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2545:         }
 2546:     }
 2547:     return;
 2548: }
 2549: 
 2550: // ]]>
 2551: </script>
 2552: 
 2553: ENDSCRIPT
 2554: }
 2555: 
 2556: sub ltitools_javascript {
 2557:     my ($settings) = @_;
 2558:     return unless(ref($settings) eq 'HASH');
 2559:     my (%ordered,$total,%jstext);
 2560:     $total = 0;
 2561:     foreach my $item (keys(%{$settings})) {
 2562:         if (ref($settings->{$item}) eq 'HASH') {
 2563:             my $num = $settings->{$item}{'order'};
 2564:             $ordered{$num} = $item;
 2565:         }
 2566:     }
 2567:     $total = scalar(keys(%{$settings}));
 2568:     my @jsarray = ();
 2569:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2570:         push(@jsarray,$ordered{$item});
 2571:     }
 2572:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2573:     return <<"ENDSCRIPT";
 2574: <script type="text/javascript">
 2575: // <![CDATA[
 2576: function reorderLTI(form,item) {
 2577:     var changedVal;
 2578: $jstext
 2579:     var newpos = 'ltitools_add_pos';
 2580:     var maxh = 1 + $total;
 2581:     var current = new Array;
 2582:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2583:     if (item == newpos) {
 2584:         changedVal = newitemVal;
 2585:     } else {
 2586:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2587:         current[newitemVal] = newpos;
 2588:     }
 2589:     for (var i=0; i<ltitools.length; i++) {
 2590:         var elementName = 'ltitools_'+ltitools[i];
 2591:         if (elementName != item) {
 2592:             if (form.elements[elementName]) {
 2593:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2594:                 current[currVal] = elementName;
 2595:             }
 2596:         }
 2597:     }
 2598:     var oldVal;
 2599:     for (var j=0; j<maxh; j++) {
 2600:         if (current[j] == undefined) {
 2601:             oldVal = j;
 2602:         }
 2603:     }
 2604:     if (oldVal < changedVal) {
 2605:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2606:            var elementName = current[k];
 2607:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2608:         }
 2609:     } else {
 2610:         for (var k=changedVal; k<oldVal; k++) {
 2611:             var elementName = current[k];
 2612:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2613:         }
 2614:     }
 2615:     return;
 2616: }
 2617: 
 2618: // ]]>
 2619: </script>
 2620: 
 2621: ENDSCRIPT
 2622: }
 2623: 
 2624: sub print_autoenroll {
 2625:     my ($dom,$settings,$rowtotal) = @_;
 2626:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2627:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2628:     if (ref($settings) eq 'HASH') {
 2629:         if (exists($settings->{'run'})) {
 2630:             if ($settings->{'run'} eq '0') {
 2631:                 $runoff = ' checked="checked" ';
 2632:                 $runon = ' ';
 2633:             } else {
 2634:                 $runon = ' checked="checked" ';
 2635:                 $runoff = ' ';
 2636:             }
 2637:         } else {
 2638:             if ($autorun) {
 2639:                 $runon = ' checked="checked" ';
 2640:                 $runoff = ' ';
 2641:             } else {
 2642:                 $runoff = ' checked="checked" ';
 2643:                 $runon = ' ';
 2644:             }
 2645:         }
 2646:         if (exists($settings->{'co-owners'})) {
 2647:             if ($settings->{'co-owners'} eq '0') {
 2648:                 $coownersoff = ' checked="checked" ';
 2649:                 $coownerson = ' ';
 2650:             } else {
 2651:                 $coownerson = ' checked="checked" ';
 2652:                 $coownersoff = ' ';
 2653:             }
 2654:         } else {
 2655:             $coownersoff = ' checked="checked" ';
 2656:             $coownerson = ' ';
 2657:         }
 2658:         if (exists($settings->{'sender_domain'})) {
 2659:             $defdom = $settings->{'sender_domain'};
 2660:         }
 2661:         if (exists($settings->{'autofailsafe'})) {
 2662:             $failsafe = $settings->{'autofailsafe'};
 2663:         }
 2664:     } else {
 2665:         if ($autorun) {
 2666:             $runon = ' checked="checked" ';
 2667:             $runoff = ' ';
 2668:         } else {
 2669:             $runoff = ' checked="checked" ';
 2670:             $runon = ' ';
 2671:         }
 2672:     }
 2673:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2674:     my $notif_sender;
 2675:     if (ref($settings) eq 'HASH') {
 2676:         $notif_sender = $settings->{'sender_uname'};
 2677:     }
 2678:     my $datatable='<tr class="LC_odd_row">'.
 2679:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2680:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2681:                   '<input type="radio" name="autoenroll_run"'.
 2682:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2683:                   '<label><input type="radio" name="autoenroll_run"'.
 2684:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2685:                   '</tr><tr>'.
 2686:                   '<td>'.&mt('Notification messages - sender').
 2687:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2688:                   &mt('username').':&nbsp;'.
 2689:                   '<input type="text" name="sender_uname" value="'.
 2690:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2691:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2692:                   '<tr class="LC_odd_row">'.
 2693:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2694:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2695:                   '<input type="radio" name="autoassign_coowners"'.
 2696:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2697:                   '<label><input type="radio" name="autoassign_coowners"'.
 2698:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2699:                   '</tr><tr>'.
 2700:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2701:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2702:                   '<input type="text" name="autoenroll_failsafe"'.
 2703:                   ' value="'.$failsafe.'" size="4" /></td></tr>';
 2704:     $$rowtotal += 4;
 2705:     return $datatable;
 2706: }
 2707: 
 2708: sub print_autoupdate {
 2709:     my ($position,$dom,$settings,$rowtotal) = @_;
 2710:     my $datatable;
 2711:     if ($position eq 'top') {
 2712:         my $updateon = ' ';
 2713:         my $updateoff = ' checked="checked" ';
 2714:         my $classlistson = ' ';
 2715:         my $classlistsoff = ' checked="checked" ';
 2716:         if (ref($settings) eq 'HASH') {
 2717:             if ($settings->{'run'} eq '1') {
 2718:                 $updateon = $updateoff;
 2719:                 $updateoff = ' ';
 2720:             }
 2721:             if ($settings->{'classlists'} eq '1') {
 2722:                 $classlistson = $classlistsoff;
 2723:                 $classlistsoff = ' ';
 2724:             }
 2725:         }
 2726:         my %title = (
 2727:                    run => 'Auto-update active?',
 2728:                    classlists => 'Update information in classlists?',
 2729:                     );
 2730:         $datatable = '<tr class="LC_odd_row">'. 
 2731:                   '<td>'.&mt($title{'run'}).'</td>'.
 2732:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2733:                   '<input type="radio" name="autoupdate_run"'.
 2734:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2735:                   '<label><input type="radio" name="autoupdate_run"'.
 2736:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2737:                   '</tr><tr>'.
 2738:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2739:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2740:                   '<label><input type="radio" name="classlists"'.
 2741:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2742:                   '<label><input type="radio" name="classlists"'.
 2743:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2744:                   '</tr>';
 2745:         $$rowtotal += 2;
 2746:     } elsif ($position eq 'middle') {
 2747:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2748:         my $numinrow = 3;
 2749:         my $locknamesettings;
 2750:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2751:                                      $dom,$numinrow,$othertitle,
 2752:                                     'lockablenames');
 2753:         $$rowtotal ++;
 2754:     } else {
 2755:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2756:         my @fields = ('lastname','firstname','middlename','generation',
 2757:                       'permanentemail','id');
 2758:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2759:         my $numrows = 0;
 2760:         if (ref($types) eq 'ARRAY') {
 2761:             if (@{$types} > 0) {
 2762:                 $datatable = 
 2763:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2764:                                          \@fields,$types,\$numrows);
 2765:                     $$rowtotal += @{$types}; 
 2766:             }
 2767:         }
 2768:         $datatable .= 
 2769:             &usertype_update_row($settings,{'default' => $othertitle},
 2770:                                  \%fieldtitles,\@fields,['default'],
 2771:                                  \$numrows);
 2772:         $$rowtotal ++;     
 2773:     }
 2774:     return $datatable;
 2775: }
 2776: 
 2777: sub print_autocreate {
 2778:     my ($dom,$settings,$rowtotal) = @_;
 2779:     my (%createon,%createoff,%currhash);
 2780:     my @types = ('xml','req');
 2781:     if (ref($settings) eq 'HASH') {
 2782:         foreach my $item (@types) {
 2783:             $createoff{$item} = ' checked="checked" ';
 2784:             $createon{$item} = ' ';
 2785:             if (exists($settings->{$item})) {
 2786:                 if ($settings->{$item}) {
 2787:                     $createon{$item} = ' checked="checked" ';
 2788:                     $createoff{$item} = ' ';
 2789:                 }
 2790:             }
 2791:         }
 2792:         if ($settings->{'xmldc'} ne '') {
 2793:             $currhash{$settings->{'xmldc'}} = 1;
 2794:         }
 2795:     } else {
 2796:         foreach my $item (@types) {
 2797:             $createoff{$item} = ' checked="checked" ';
 2798:             $createon{$item} = ' ';
 2799:         }
 2800:     }
 2801:     $$rowtotal += 2;
 2802:     my $numinrow = 2;
 2803:     my $datatable='<tr class="LC_odd_row">'.
 2804:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2805:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2806:                   '<input type="radio" name="autocreate_xml"'.
 2807:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2808:                   '<label><input type="radio" name="autocreate_xml"'.
 2809:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2810:                   '</td></tr><tr>'.
 2811:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2812:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2813:                   '<input type="radio" name="autocreate_req"'.
 2814:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2815:                   '<label><input type="radio" name="autocreate_req"'.
 2816:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2817:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2818:                                                    'autocreate_xmldc',%currhash);
 2819:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2820:     if ($numdc > 1) {
 2821:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2822:                       '</td><td class="LC_left_item">';
 2823:     } else {
 2824:         $datatable .= &mt('Course creation processed as:').
 2825:                       '</td><td class="LC_right_item">';
 2826:     }
 2827:     $datatable .= $dctable.'</td></tr>';
 2828:     $$rowtotal += $rows;
 2829:     return $datatable;
 2830: }
 2831: 
 2832: sub print_directorysrch {
 2833:     my ($position,$dom,$settings,$rowtotal) = @_;
 2834:     my $datatable;
 2835:     if ($position eq 'top') {
 2836:         my $instsrchon = ' ';
 2837:         my $instsrchoff = ' checked="checked" ';
 2838:         my ($exacton,$containson,$beginson);
 2839:         my $instlocalon = ' ';
 2840:         my $instlocaloff = ' checked="checked" ';
 2841:         if (ref($settings) eq 'HASH') {
 2842:             if ($settings->{'available'} eq '1') {
 2843:                 $instsrchon = $instsrchoff;
 2844:                 $instsrchoff = ' ';
 2845:             }
 2846:             if ($settings->{'localonly'} eq '1') {
 2847:                 $instlocalon = $instlocaloff;
 2848:                 $instlocaloff = ' ';
 2849:             }
 2850:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2851:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 2852:                     if ($type eq 'exact') {
 2853:                         $exacton = ' checked="checked" ';
 2854:                     } elsif ($type eq 'contains') {
 2855:                         $containson = ' checked="checked" ';
 2856:                     } elsif ($type eq 'begins') {
 2857:                         $beginson = ' checked="checked" ';
 2858:                     }
 2859:                 }
 2860:             } else {
 2861:                 if ($settings->{'searchtypes'} eq 'exact') {
 2862:                     $exacton = ' checked="checked" ';
 2863:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 2864:                     $containson = ' checked="checked" ';
 2865:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 2866:                     $exacton = ' checked="checked" ';
 2867:                     $containson = ' checked="checked" ';
 2868:                 }
 2869:             }
 2870:         }
 2871:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2872:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2873: 
 2874:         my $numinrow = 4;
 2875:         my $cansrchrow = 0;
 2876:         $datatable='<tr class="LC_odd_row">'.
 2877:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 2878:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2879:                    '<input type="radio" name="dirsrch_available"'.
 2880:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2881:                    '<label><input type="radio" name="dirsrch_available"'.
 2882:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2883:                    '</tr><tr>'.
 2884:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 2885:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2886:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 2887:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2888:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 2889:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2890:                    '</tr>';
 2891:         $$rowtotal += 2;
 2892:         if (ref($usertypes) eq 'HASH') {
 2893:             if (keys(%{$usertypes}) > 0) {
 2894:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2895:                                              $numinrow,$othertitle,'cansearch');
 2896:                 $cansrchrow = 1;
 2897:             }
 2898:         }
 2899:         if ($cansrchrow) {
 2900:             $$rowtotal ++;
 2901:             $datatable .= '<tr>';
 2902:         } else {
 2903:             $datatable .= '<tr class="LC_odd_row">';
 2904:         }
 2905:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2906:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2907:         foreach my $title (@{$titleorder}) {
 2908:             if (defined($searchtitles->{$title})) {
 2909:                 my $check = ' ';
 2910:                 if (ref($settings) eq 'HASH') {
 2911:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2912:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2913:                             $check = ' checked="checked" ';
 2914:                         }
 2915:                     }
 2916:                 }
 2917:                 $datatable .= '<td class="LC_left_item">'.
 2918:                               '<span class="LC_nobreak"><label>'.
 2919:                               '<input type="checkbox" name="searchby" '.
 2920:                               'value="'.$title.'"'.$check.'/>'.
 2921:                               $searchtitles->{$title}.'</label></span></td>';
 2922:             }
 2923:         }
 2924:         $datatable .= '</tr></table></td></tr>';
 2925:         $$rowtotal ++;
 2926:         if ($cansrchrow) {
 2927:             $datatable .= '<tr class="LC_odd_row">';
 2928:         } else {
 2929:             $datatable .= '<tr>';
 2930:         }
 2931:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2932:                       '<td class="LC_left_item" colspan="2">'.
 2933:                       '<span class="LC_nobreak"><label>'.
 2934:                       '<input type="checkbox" name="searchtypes" '.
 2935:                       $exacton.' value="exact" />'.&mt('Exact match').
 2936:                       '</label>&nbsp;'.
 2937:                       '<label><input type="checkbox" name="searchtypes" '.
 2938:                       $beginson.' value="begins" />'.&mt('Begins with').
 2939:                       '</label>&nbsp;'.
 2940:                       '<label><input type="checkbox" name="searchtypes" '.
 2941:                       $containson.' value="contains" />'.&mt('Contains').
 2942:                       '</label></span></td></tr>';
 2943:         $$rowtotal ++;
 2944:     } else {
 2945:         my $domsrchon = ' checked="checked" ';
 2946:         my $domsrchoff = ' ';
 2947:         my $domlocalon = ' ';
 2948:         my $domlocaloff = ' checked="checked" ';
 2949:         if (ref($settings) eq 'HASH') {
 2950:             if ($settings->{'lclocalonly'} eq '1') {
 2951:                 $domlocalon = $domlocaloff;
 2952:                 $domlocaloff = ' ';
 2953:             }
 2954:             if ($settings->{'lcavailable'} eq '0') {
 2955:                 $domsrchoff = $domsrchon;
 2956:                 $domsrchon = ' ';
 2957:             }
 2958:         }
 2959:         $datatable='<tr class="LC_odd_row">'.
 2960:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 2961:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2962:                       '<input type="radio" name="dirsrch_domavailable"'.
 2963:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2964:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 2965:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2966:                       '</tr><tr>'.
 2967:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 2968:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2969:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 2970:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2971:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 2972:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2973:                       '</tr>';
 2974:         $$rowtotal += 2;
 2975:     }
 2976:     return $datatable;
 2977: }
 2978: 
 2979: sub print_contacts {
 2980:     my ($position,$dom,$settings,$rowtotal) = @_;
 2981:     my $datatable;
 2982:     my @contacts = ('adminemail','supportemail');
 2983:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 2984:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings);
 2985:     if ($position eq 'top') {
 2986:         if (ref($settings) eq 'HASH') {
 2987:             foreach my $item (@contacts) {
 2988:                 if (exists($settings->{$item})) {
 2989:                     $to{$item} = $settings->{$item};
 2990:                 }
 2991:             }
 2992:         }
 2993:     } elsif ($position eq 'middle') {
 2994:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 2995:                      'updatesmail','idconflictsmail');
 2996:         foreach my $type (@mailings) {
 2997:             $otheremails{$type} = '';
 2998:         }
 2999:     } else {
 3000:         @mailings = ('helpdeskmail','otherdomsmail');
 3001:         foreach my $type (@mailings) {
 3002:             $otheremails{$type} = '';
 3003:         }
 3004:         $bccemails{'helpdeskmail'} = '';
 3005:         $bccemails{'otherdomsmail'} = '';
 3006:         $includestr{'helpdeskmail'} = '';
 3007:         $includestr{'otherdomsmail'} = '';
 3008:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 3009:     }
 3010:     if (ref($settings) eq 'HASH') {
 3011:         unless ($position eq 'top') {
 3012:             foreach my $type (@mailings) {
 3013:                 if (exists($settings->{$type})) {
 3014:                     if (ref($settings->{$type}) eq 'HASH') {
 3015:                         foreach my $item (@contacts) {
 3016:                             if ($settings->{$type}{$item}) {
 3017:                                 $checked{$type}{$item} = ' checked="checked" ';
 3018:                             }
 3019:                         }
 3020:                         $otheremails{$type} = $settings->{$type}{'others'};
 3021:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3022:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 3023:                             if ($settings->{$type}{'include'} ne '') {
 3024:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3025:                                 $includestr{$type} = &unescape($includestr{$type});
 3026:                             }
 3027:                         }
 3028:                     }
 3029:                 } elsif ($type eq 'lonstatusmail') {
 3030:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3031:                 }
 3032:             }
 3033:         }
 3034:         if ($position eq 'bottom') {
 3035:             foreach my $type (@mailings) {
 3036:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 3037:                 if ($settings->{$type}{'include'} ne '') {
 3038:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3039:                     $includestr{$type} = &unescape($includestr{$type});
 3040:                 }
 3041:             }
 3042:             if (ref($settings->{'helpform'}) eq 'HASH') {
 3043:                 if (ref($fields) eq 'ARRAY') {
 3044:                     foreach my $field (@{$fields}) {
 3045:                         $currfield{$field} = $settings->{'helpform'}{$field};
 3046:                     }
 3047:                 }
 3048:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 3049:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 3050:                 } else {
 3051:                     $maxsize = '1.0';
 3052:                 }
 3053:             } else {
 3054:                 if (ref($fields) eq 'ARRAY') {
 3055:                     foreach my $field (@{$fields}) {
 3056:                         $currfield{$field} = 'yes';
 3057:                     }
 3058:                 }
 3059:                 $maxsize = '1.0';
 3060:             }
 3061:         }
 3062:     } else {
 3063:         if ($position eq 'top') {
 3064:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 3065:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 3066:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 3067:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 3068:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3069:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 3070:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 3071:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 3072:         } elsif ($position eq 'bottom') {
 3073:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 3074:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 3075:             if (ref($fields) eq 'ARRAY') {
 3076:                 foreach my $field (@{$fields}) {
 3077:                     $currfield{$field} = 'yes';
 3078:                 }
 3079:             }
 3080:             $maxsize = '1.0';
 3081:         }
 3082:     }
 3083:     my ($titles,$short_titles) = &contact_titles();
 3084:     my $rownum = 0;
 3085:     my $css_class;
 3086:     if ($position eq 'top') {
 3087:         foreach my $item (@contacts) {
 3088:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3089:             $datatable .= '<tr'.$css_class.'>'. 
 3090:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3091:                           '</span></td><td class="LC_right_item">'.
 3092:                           '<input type="text" name="'.$item.'" value="'.
 3093:                           $to{$item}.'" /></td></tr>';
 3094:             $rownum ++;
 3095:         }
 3096:     } else {
 3097:         foreach my $type (@mailings) {
 3098:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3099:             $datatable .= '<tr'.$css_class.'>'.
 3100:                           '<td><span class="LC_nobreak">'.
 3101:                           $titles->{$type}.': </span></td>'.
 3102:                           '<td class="LC_left_item">';
 3103:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3104:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3105:             }
 3106:             $datatable .= '<span class="LC_nobreak">';
 3107:             foreach my $item (@contacts) {
 3108:                 $datatable .= '<label>'.
 3109:                               '<input type="checkbox" name="'.$type.'"'.
 3110:                               $checked{$type}{$item}.
 3111:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3112:                               '</label>&nbsp;';
 3113:             }
 3114:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3115:                           '<input type="text" name="'.$type.'_others" '.
 3116:                           'value="'.$otheremails{$type}.'"  />';
 3117:             my %locchecked;
 3118:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3119:                 foreach my $loc ('s','b') {
 3120:                     if ($includeloc{$type} eq $loc) {
 3121:                         $locchecked{$loc} = ' checked="checked"';
 3122:                         last;
 3123:                     }
 3124:                 }
 3125:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3126:                               '<input type="text" name="'.$type.'_bcc" '.
 3127:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3128:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3129:                               &mt('Text automatically added to e-mail:').' '.
 3130:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br >'.
 3131:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3132:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3133:                               ('&nbsp;'x2).
 3134:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3135:                               '</span></fieldset>';
 3136:             }
 3137:             $datatable .= '</td></tr>'."\n";
 3138:             $rownum ++;
 3139:         }
 3140:     }
 3141:     if ($position eq 'middle') {
 3142:         my %choices;
 3143:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 3144:                                        &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3145:                                        &mt('LON-CAPA core group - MSU'),600,500));
 3146:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3147:                                         &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3148:                                         &mt('LON-CAPA core group - MSU'),600,500));
 3149:         my @toggles = ('reporterrors','reportupdates');
 3150:         my %defaultchecked = ('reporterrors'  => 'on',
 3151:                               'reportupdates' => 'on');
 3152:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3153:                                                    \%choices,$rownum);
 3154:         $datatable .= $reports;
 3155:     } elsif ($position eq 'bottom') {
 3156:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3157:         $datatable .= '<tr'.$css_class.'>'.
 3158:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3159:                       &mt('(e-mail, subject, and description always shown)').
 3160:                       '</td><td class="LC_left_item">';
 3161:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3162:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3163:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3164:             foreach my $field (@{$fields}) {
 3165:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3166:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3167:                     $datatable .= ' '.&mt('(logged-in users)');
 3168:                 }
 3169:                 $datatable .='</td><td>';
 3170:                 my $clickaction;
 3171:                 if ($field eq 'screenshot') {
 3172:                     $clickaction = ' onclick="screenshotSize(this);"';
 3173:                 }
 3174:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3175:                     foreach my $option (@{$possoptions->{$field}}) {
 3176:                         my $checked;
 3177:                         if ($currfield{$field} eq $option) {
 3178:                             $checked = ' checked="checked"';
 3179:                         }
 3180:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3181:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3182:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3183:                                       '</label></span>'.('&nbsp;'x2);
 3184:                     }
 3185:                 }
 3186:                 if ($field eq 'screenshot') {
 3187:                     my $display;
 3188:                     if ($currfield{$field} eq 'no') {
 3189:                         $display = ' style="display:none"';
 3190:                     }
 3191:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.' />'.
 3192:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3193:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3194:                 }
 3195:                 $datatable .= '</td></tr>';
 3196:             }
 3197:             $datatable .= '</table>';
 3198:         }
 3199:         $datatable .= '</td></tr>'."\n";
 3200:         $rownum ++;
 3201:     }
 3202:     $$rowtotal += $rownum;
 3203:     return $datatable;
 3204: }
 3205: 
 3206: sub contacts_javascript {
 3207:     return <<"ENDSCRIPT";
 3208: 
 3209: <script type="text/javascript">
 3210: // <![CDATA[
 3211: 
 3212: function screenshotSize(field) {
 3213:     if (document.getElementById('help_screenshotsize')) {
 3214:         if (field.value == 'no') {
 3215:             document.getElementById('help_screenshotsize').style.display="none";
 3216:         } else {
 3217:             document.getElementById('help_screenshotsize').style.display="";
 3218:         }
 3219:     }
 3220:     return;
 3221: }
 3222: 
 3223: // ]]>
 3224: </script>
 3225: 
 3226: ENDSCRIPT
 3227: }
 3228: 
 3229: sub print_helpsettings {
 3230:     my ($position,$dom,$settings,$rowtotal) = @_;
 3231:     my $confname = $dom.'-domainconfig';
 3232:     my $formname = 'display';
 3233:     my ($datatable,$itemcount);
 3234:     if ($position eq 'top') {
 3235:         $itemcount = 1;
 3236:         my (%choices,%defaultchecked,@toggles);
 3237:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3238:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3239:                                      &mt('LON-CAPA bug tracker'),600,500));
 3240:         %defaultchecked = ('submitbugs' => 'on');
 3241:         @toggles = ('submitbugs');
 3242:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3243:                                                      \%choices,$itemcount);
 3244:         $$rowtotal ++;
 3245:     } else {
 3246:         my $css_class;
 3247:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3248:         my (%customroles,%ordered,%current);
 3249:         if (ref($settings->{'adhoc'}) eq 'HASH') {
 3250:             %current = %{$settings->{'adhoc'}};
 3251:         }
 3252:         my $count = 0;
 3253:         foreach my $key (sort(keys(%existing))) {
 3254:             if ($key=~/^rolesdef\_(\w+)$/) {
 3255:                 my $rolename = $1;
 3256:                 my (%privs,$order);
 3257:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3258:                 $customroles{$rolename} = \%privs;
 3259:                 if (ref($current{$rolename}) eq 'HASH') {
 3260:                     $order = $current{$rolename}{'order'};
 3261:                 }
 3262:                 if ($order eq '') {
 3263:                     $order = $count;
 3264:                 }
 3265:                 $ordered{$order} = $rolename;
 3266:                 $count++;
 3267:             }
 3268:         }
 3269:         my $maxnum = scalar(keys(%ordered));
 3270:         my @roles_by_num = ();
 3271:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3272:             push(@roles_by_num,$item);
 3273:         }
 3274:         my $context = 'domprefs';
 3275:         my $crstype = 'Course';
 3276:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3277:         my @accesstypes = ('all','dh','da','none');
 3278:         my ($numstatustypes,@jsarray);
 3279:         if (ref($types) eq 'ARRAY') {
 3280:             if (@{$types} > 0) {
 3281:                 $numstatustypes = scalar(@{$types});
 3282:                 push(@accesstypes,'status');
 3283:                 @jsarray = ('bystatus');
 3284:             }
 3285:         }
 3286:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3287:         if (keys(%domhelpdesk)) {
 3288:             push(@accesstypes,('inc','exc'));
 3289:             push(@jsarray,('notinc','notexc'));
 3290:         }
 3291:         my $hiddenstr = join("','",@jsarray);
 3292:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3293:         my $context = 'domprefs';
 3294:         my $crstype = 'Course';
 3295:         my $prefix = 'helproles_';
 3296:         my $add_class = 'LC_hidden';
 3297:         foreach my $num (@roles_by_num) {
 3298:             my $role = $ordered{$num};
 3299:             my ($desc,$access,@statuses);
 3300:             if (ref($current{$role}) eq 'HASH') {
 3301:                 $desc = $current{$role}{'desc'};
 3302:                 $access = $current{$role}{'access'};
 3303:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3304:                     @statuses = @{$current{$role}{'insttypes'}};
 3305:                 }
 3306:             }
 3307:             if ($desc eq '') {
 3308:                 $desc = $role;
 3309:             }
 3310:             my $identifier = 'custhelp'.$num;
 3311:             my %full=();
 3312:             my %levels= (
 3313:                          course => {},
 3314:                          domain => {},
 3315:                          system => {},
 3316:                         );
 3317:             my %levelscurrent=(
 3318:                                course => {},
 3319:                                domain => {},
 3320:                                system => {},
 3321:                               );
 3322:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3323:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3324:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3325:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3326:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 3327:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3328:             for (my $k=0; $k<=$maxnum; $k++) {
 3329:                 my $vpos = $k+1;
 3330:                 my $selstr;
 3331:                 if ($k == $num) {
 3332:                     $selstr = ' selected="selected" ';
 3333:                 }
 3334:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3335:             }
 3336:             $datatable .= '</select>'.('&nbsp;'x2).
 3337:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3338:                           '</td>'.
 3339:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3340:                           &mt('Name shown to users:').
 3341:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3342:                           '</fieldset>'.
 3343:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3344:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3345:                           '<fieldset>'.
 3346:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3347:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3348:                                                                    \%levelscurrent,$identifier,
 3349:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3350:                           '</fieldset></td>';
 3351:             $itemcount ++;
 3352:         }
 3353:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3354:         my $newcust = 'custhelp'.$count;
 3355:         my (%privs,%levelscurrent);
 3356:         my %full=();
 3357:         my %levels= (
 3358:                      course => {},
 3359:                      domain => {},
 3360:                      system => {},
 3361:                     );
 3362:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3363:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3364:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3365:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 3366:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3367:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3368:         for (my $k=0; $k<$maxnum+1; $k++) {
 3369:             my $vpos = $k+1;
 3370:             my $selstr;
 3371:             if ($k == $maxnum) {
 3372:                 $selstr = ' selected="selected" ';
 3373:             }
 3374:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3375:         }
 3376:         $datatable .= '</select>&nbsp;'."\n".
 3377:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3378:                       '</label></span></td>'.
 3379:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3380:                       '<span class="LC_nobreak">'.
 3381:                       &mt('Internal name:').
 3382:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3383:                       '</span>'.('&nbsp;'x4).
 3384:                       '<span class="LC_nobreak">'.
 3385:                       &mt('Name shown to users:').
 3386:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3387:                       '</span></fieldset>'.
 3388:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3389:                                              $usertypes,$types,\%domhelpdesk).
 3390:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3391:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3392:                                                                 \@templateroles,$newcust).
 3393:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3394:                                                                \%levelscurrent,$newcust).
 3395:                       '</fieldset></td></tr>';
 3396:         $count ++;
 3397:         $$rowtotal += $count;
 3398:     }
 3399:     return $datatable;
 3400: }
 3401: 
 3402: sub adhocbutton {
 3403:     my ($prefix,$num,$field,$visibility) = @_;
 3404:     my %lt = &Apache::lonlocal::texthash(
 3405:                                           show => 'Show details',
 3406:                                           hide => 'Hide details',
 3407:                                         );
 3408:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3409:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3410:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3411:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3412: }
 3413: 
 3414: sub helpsettings_javascript {
 3415:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3416:     return unless(ref($roles_by_num) eq 'ARRAY');
 3417:     my %html_js_lt = &Apache::lonlocal::texthash(
 3418:                                           show => 'Show details',
 3419:                                           hide => 'Hide details',
 3420:                                         );
 3421:     &html_escape(\%html_js_lt);
 3422:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3423:     return <<"ENDSCRIPT";
 3424: <script type="text/javascript">
 3425: // <![CDATA[
 3426: 
 3427: function reorderHelpRoles(form,item) {
 3428:     var changedVal;
 3429: $jstext
 3430:     var newpos = 'helproles_${total}_pos';
 3431:     var maxh = 1 + $total;
 3432:     var current = new Array();
 3433:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3434:     if (item == newpos) {
 3435:         changedVal = newitemVal;
 3436:     } else {
 3437:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3438:         current[newitemVal] = newpos;
 3439:     }
 3440:     for (var i=0; i<helproles.length; i++) {
 3441:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3442:         if (elementName != item) {
 3443:             if (form.elements[elementName]) {
 3444:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3445:                 current[currVal] = elementName;
 3446:             }
 3447:         }
 3448:     }
 3449:     var oldVal;
 3450:     for (var j=0; j<maxh; j++) {
 3451:         if (current[j] == undefined) {
 3452:             oldVal = j;
 3453:         }
 3454:     }
 3455:     if (oldVal < changedVal) {
 3456:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3457:            var elementName = current[k];
 3458:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3459:         }
 3460:     } else {
 3461:         for (var k=changedVal; k<oldVal; k++) {
 3462:             var elementName = current[k];
 3463:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3464:         }
 3465:     }
 3466:     return;
 3467: }
 3468: 
 3469: function helpdeskAccess(num) {
 3470:     var curraccess = null;
 3471:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3472:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3473:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3474:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3475:             }
 3476:         }
 3477:     }
 3478:     var shown = Array();
 3479:     var hidden = Array();
 3480:     if (curraccess == 'none') {
 3481:         hidden = Array('$hiddenstr');
 3482:     } else {
 3483:         if (curraccess == 'status') {
 3484:             shown = Array('bystatus');
 3485:             hidden = Array('notinc','notexc');
 3486:         } else {
 3487:             if (curraccess == 'exc') {
 3488:                 shown = Array('notexc');
 3489:                 hidden = Array('notinc','bystatus');
 3490:             }
 3491:             if (curraccess == 'inc') {
 3492:                 shown = Array('notinc');
 3493:                 hidden = Array('notexc','bystatus');
 3494:             }
 3495:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 3496:                 hidden = Array('notinc','notexc','bystatus');
 3497:             }
 3498:         }
 3499:     }
 3500:     if (hidden.length > 0) {
 3501:         for (var i=0; i<hidden.length; i++) {
 3502:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3503:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3504:             }
 3505:         }
 3506:     }
 3507:     if (shown.length > 0) {
 3508:         for (var i=0; i<shown.length; i++) {
 3509:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3510:                 if (shown[i] == 'privs') {
 3511:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3512:                 } else {
 3513:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3514:                 }
 3515:             }
 3516:         }
 3517:     }
 3518:     return;
 3519: }
 3520: 
 3521: function toggleHelpdeskItem(num,field) {
 3522:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3523:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3524:             document.getElementById('helproles_'+num+'_'+field).className =
 3525:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3526:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3527:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3528:             }
 3529:         } else {
 3530:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3531:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3532:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3533:             }
 3534:         }
 3535:     }
 3536:     return;
 3537: }
 3538: 
 3539: // ]]>
 3540: </script>
 3541: 
 3542: ENDSCRIPT
 3543: }
 3544: 
 3545: sub helpdeskroles_access {
 3546:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3547:         $usertypes,$types,$domhelpdesk) = @_;
 3548:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3549:     my %lt = &Apache::lonlocal::texthash(
 3550:                     'rou'    => 'Role usage',
 3551:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3552:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 3553:                     'dh'     => 'All with domain helpdesk role',
 3554:                     'da'     => 'All with domain helpdesk assistant role',
 3555:                     'none'   => 'None',
 3556:                     'status' => 'Determined based on institutional status',
 3557:                     'inc'    => 'Include all, but exclude specific personnel',
 3558:                     'exc'    => 'Exclude all, but include specific personnel',
 3559:                   );
 3560:     my %usecheck = (
 3561:                      all => ' checked="checked"',
 3562:                    );
 3563:     my %displaydiv = (
 3564:                       status => 'none',
 3565:                       inc    => 'none',
 3566:                       exc    => 'none',
 3567:                       priv   => 'block',
 3568:                      );
 3569:     my $output;
 3570:     if (ref($current) eq 'HASH') {
 3571:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3572:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3573:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3574:                 delete($usecheck{'all'});
 3575:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3576:                     my $access = $1;
 3577:                     $displaydiv{$access} = 'inline';
 3578:                 } elsif ($current->{access} eq 'none') {
 3579:                     $displaydiv{'priv'} = 'none';
 3580:                 }
 3581:             }
 3582:         }
 3583:     }
 3584:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3585:               '<p>'.$lt{'whi'}.'</p>';
 3586:     foreach my $access (@{$accesstypes}) {
 3587:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3588:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3589:                    $lt{$access}.'</label>';
 3590:         if ($access eq 'status') {
 3591:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3592:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3593:                                                                  $othertitle,$usertypes,$types).
 3594:                        '</div>';
 3595:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3596:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3597:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3598:                        '</div>';
 3599:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3600:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3601:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3602:                        '</div>';
 3603:         }
 3604:         $output .= '</p>';
 3605:     }
 3606:     $output .= '</fieldset>';
 3607:     return $output;
 3608: }
 3609: 
 3610: sub radiobutton_prefs {
 3611:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3612:         $additional,$align) = @_;
 3613:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3614:                    (ref($choices) eq 'HASH'));
 3615: 
 3616:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3617: 
 3618:     foreach my $item (@{$toggles}) {
 3619:         if ($defaultchecked->{$item} eq 'on') {
 3620:             $checkedon{$item} = ' checked="checked" ';
 3621:             $checkedoff{$item} = ' ';
 3622:         } elsif ($defaultchecked->{$item} eq 'off') {
 3623:             $checkedoff{$item} = ' checked="checked" ';
 3624:             $checkedon{$item} = ' ';
 3625:         }
 3626:     }
 3627:     if (ref($settings) eq 'HASH') {
 3628:         foreach my $item (@{$toggles}) {
 3629:             if ($settings->{$item} eq '1') {
 3630:                 $checkedon{$item} =  ' checked="checked" ';
 3631:                 $checkedoff{$item} = ' ';
 3632:             } elsif ($settings->{$item} eq '0') {
 3633:                 $checkedoff{$item} =  ' checked="checked" ';
 3634:                 $checkedon{$item} = ' ';
 3635:             }
 3636:         }
 3637:     }
 3638:     if ($onclick) {
 3639:         $onclick = ' onclick="'.$onclick.'"';
 3640:     }
 3641:     foreach my $item (@{$toggles}) {
 3642:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3643:         $datatable .=
 3644:             '<tr'.$css_class.'><td valign="top">'.
 3645:             '<span class="LC_nobreak">'.$choices->{$item}.
 3646:             '</span></td>';
 3647:         if ($align eq 'left') {
 3648:             $datatable .= '<td class="LC_left_item">';
 3649:         } else {
 3650:             $datatable .= '<td class="LC_right_item">';
 3651:         }
 3652:         $datatable .=
 3653:             '<span class="LC_nobreak">'.
 3654:             '<label><input type="radio" name="'.
 3655:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3656:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3657:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3658:             '</span>'.$additional.
 3659:             '</td>'.
 3660:             '</tr>';
 3661:         $itemcount ++;
 3662:     }
 3663:     return ($datatable,$itemcount);
 3664: }
 3665: 
 3666: sub print_ltitools {
 3667:     my ($dom,$settings,$rowtotal) = @_;
 3668:     my $rownum = 0;
 3669:     my $css_class;
 3670:     my $itemcount = 1;
 3671:     my $maxnum = 0;
 3672:     my %ordered;
 3673:     if (ref($settings) eq 'HASH') {
 3674:         foreach my $item (keys(%{$settings})) {
 3675:             if (ref($settings->{$item}) eq 'HASH') {
 3676:                 my $num = $settings->{$item}{'order'};
 3677:                 $ordered{$num} = $item;
 3678:             }
 3679:         }
 3680:     }
 3681:     my $confname = $dom.'-domainconfig';
 3682:     my $switchserver = &check_switchserver($dom,$confname);
 3683:     my $maxnum = scalar(keys(%ordered));
 3684:     my $datatable = &ltitools_javascript($settings);
 3685:     my %lt = &ltitools_names();
 3686:     my @courseroles = ('cc','in','ta','ep','st');
 3687:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 3688:     my @fields = ('fullname','firstname','lastname','email','user','roles');
 3689:     if (keys(%ordered)) {
 3690:         my @items = sort { $a <=> $b } keys(%ordered);
 3691:         for (my $i=0; $i<@items; $i++) {
 3692:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3693:             my $item = $ordered{$items[$i]};
 3694:             my ($title,$key,$secret,$url,$imgsrc,$version);
 3695:             if (ref($settings->{$item}) eq 'HASH') {
 3696:                 $title = $settings->{$item}->{'title'};
 3697:                 $url = $settings->{$item}->{'url'};
 3698:                 $key = $settings->{$item}->{'key'};
 3699:                 $secret = $settings->{$item}->{'secret'};
 3700:                 my $image = $settings->{$item}->{'image'};
 3701:                 if ($image ne '') {
 3702:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 3703:                 }
 3704:             }
 3705:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_".$item."'".');"';
 3706:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3707:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 3708:             for (my $k=0; $k<=$maxnum; $k++) {
 3709:                 my $vpos = $k+1;
 3710:                 my $selstr;
 3711:                 if ($k == $i) {
 3712:                     $selstr = ' selected="selected" ';
 3713:                 }
 3714:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3715:             }
 3716:             $datatable .= '</select>'.('&nbsp;'x2).
 3717:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 3718:                 &mt('Delete?').'</label></span></td>'.
 3719:                 '<td colspan="2">'.
 3720:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3721:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 3722:                 ('&nbsp;'x2).
 3723:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 3724:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 3725:                 ('&nbsp;'x2).
 3726:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 3727:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3728:                 '<br /><br />'.
 3729:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_url_'.$i.'"'.
 3730:                 ' value="'.$url.'" /></span>'.
 3731:                 ('&nbsp;'x2).
 3732:                 '<span class="LC_nobreak">'.$lt{'key'}.
 3733:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 3734:                 ('&nbsp;'x2).
 3735:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 3736:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 3737:                 '<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>'.
 3738:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 3739:                 '</fieldset>'.
 3740:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3741:                 '<span class="LC_nobreak">'.&mt('Display target:');
 3742:             my %currdisp;
 3743:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 3744:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 3745:                     $currdisp{'window'} = ' checked="checked"';
 3746:                 } else {
 3747:                     $currdisp{'iframe'} = ' checked="checked"';
 3748:                 }
 3749:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 3750:                     $currdisp{'width'} = $1;
 3751:                 }
 3752:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 3753:                      $currdisp{'height'} = $1;
 3754:                 }
 3755:             } else {
 3756:                 $currdisp{'iframe'} = ' checked="checked"';
 3757:             }
 3758:             foreach my $disp ('iframe','window') {
 3759:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 3760:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 3761:             }
 3762:             $datatable .= ('&nbsp;'x4);
 3763:             foreach my $dimen ('width','height') {
 3764:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3765:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 3766:                               ('&nbsp;'x2);
 3767:             }
 3768:             $datatable .= '<br />';
 3769:             foreach my $extra ('passback','roster') {
 3770:                 my $checkedon = '';
 3771:                 my $checkedoff = ' checked="checked"';
 3772:                 if ($settings->{$item}->{$extra}) {
 3773:                     $checkedon = $checkedoff;
 3774:                     $checkedoff = '';
 3775:                 }
 3776:                 $datatable .= $lt{$extra}.'&nbsp;'.
 3777:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.' />'.
 3778:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 3779:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.' />'.
 3780:                               &mt('No').'</label>'.('&nbsp;'x4);
 3781:             }
 3782:             $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 3783:             if ($imgsrc) {
 3784:                 $datatable .= $imgsrc.
 3785:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 3786:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 3787:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 3788:             } else {
 3789:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3790:             }
 3791:             if ($switchserver) {
 3792:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3793:             } else {
 3794:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 3795:             }
 3796:             $datatable .= '</span></fieldset>';
 3797:             my (%checkedfields,%rolemaps);
 3798:             if (ref($settings->{$item}) eq 'HASH') {
 3799:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 3800:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 3801:                 }
 3802:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 3803:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 3804:                     $checkedfields{'roles'} = 1;
 3805:                 }
 3806:             }
 3807:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3808:                           '<span class="LC_nobreak">';
 3809:             foreach my $field (@fields) {
 3810:                 my $checked;
 3811:                 if ($checkedfields{$field}) {
 3812:                     $checked = ' checked="checked"';
 3813:                 }
 3814:                 $datatable .= '<label>'.
 3815:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$checked.' />'.
 3816:                               $lt{$field}.'</label>'.('&nbsp;' x2);
 3817:             }
 3818:             $datatable .= '</span></fieldset>'.
 3819:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 3820:             foreach my $role (@courseroles) {
 3821:                 my ($selected,$selectnone);
 3822:                 if (!$rolemaps{$role}) {
 3823:                     $selectnone = ' selected="selected"';
 3824:                 }
 3825:                 $datatable .= '<td align="center">'. 
 3826:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 3827:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 3828:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 3829:                 foreach my $ltirole (@ltiroles) {
 3830:                     unless ($selectnone) {
 3831:                         if ($rolemaps{$role} eq $ltirole) {
 3832:                             $selected = ' selected="selected"';
 3833:                         } else {
 3834:                             $selected = '';
 3835:                         }
 3836:                     }
 3837:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 3838:                 }
 3839:                 $datatable .= '</select></td>';
 3840:             }
 3841:             $datatable .= '</tr></table></fieldset>';
 3842:             my %courseconfig;
 3843:             if (ref($settings->{$item}) eq 'HASH') {
 3844:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 3845:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 3846:                 }
 3847:             }
 3848:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 3849:             foreach my $item ('label','title','target') {
 3850:                 my $checked;
 3851:                 if ($courseconfig{$item}) {
 3852:                     $checked = ' checked="checked"';
 3853:                 }
 3854:                 $datatable .= '<label>'.
 3855:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 3856:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 3857:             }
 3858:             $datatable .= '</span></fieldset>'.
 3859:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 3860:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 3861:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 3862:                 my %custom = %{$settings->{$item}->{'custom'}};
 3863:                 if (keys(%custom) > 0) {
 3864:                     foreach my $key (sort(keys(%custom))) {
 3865:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 3866:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 3867:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 3868:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 3869:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 3870:                     }
 3871:                 }
 3872:             }
 3873:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 3874:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 3875:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 3876:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 3877:             $datatable .= '</table></fieldset></td></tr>'."\n";
 3878:             $itemcount ++;
 3879:         }
 3880:     }
 3881:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3882:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_add_pos'".');"';
 3883:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 3884:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 3885:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 3886:     for (my $k=0; $k<$maxnum+1; $k++) {
 3887:         my $vpos = $k+1;
 3888:         my $selstr;
 3889:         if ($k == $maxnum) {
 3890:             $selstr = ' selected="selected" ';
 3891:         }
 3892:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3893:     }
 3894:     $datatable .= '</select>&nbsp;'."\n".
 3895:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</td>'."\n".
 3896:                   '<td colspan="2">'.
 3897:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3898:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_add_title" value="" /></span> '."\n".
 3899:                   ('&nbsp;'x2).
 3900:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 3901:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 3902:                   ('&nbsp;'x2).
 3903:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 3904:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3905:                   '<br />'.
 3906:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_add_url" value="" /></span> '."\n".
 3907:                   ('&nbsp;'x2).
 3908:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 3909:                   ('&nbsp;'x2).
 3910:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 3911:                   '<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".
 3912:                   '</fieldset>'.
 3913:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3914:                   '<span class="LC_nobreak">'.&mt('Display target:');
 3915:     my %defaultdisp;
 3916:     $defaultdisp{'iframe'} = ' checked="checked"';
 3917:     foreach my $disp ('iframe','window') {
 3918:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 3919:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 3920:     }
 3921:     $datatable .= ('&nbsp;'x4);
 3922:     foreach my $dimen ('width','height') {
 3923:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3924:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 3925:                       ('&nbsp;'x2);
 3926:     }
 3927:     $datatable .= '<br />';
 3928:     foreach my $extra ('passback','roster') {
 3929:         $datatable .= $lt{$extra}.'&nbsp;'.
 3930:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="1" />'.
 3931:                       &mt('Yes').'</label>'.('&nbsp;'x2).
 3932:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="0" checked="checked" />'.
 3933:                       &mt('No').'</label>'.('&nbsp;'x4);
 3934:     }
 3935:     $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 3936:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3937:     if ($switchserver) {
 3938:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3939:     } else {
 3940:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 3941:     }
 3942:     $datatable .= '</span></fieldset>'.
 3943:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3944:                   '<span class="LC_nobreak">';
 3945:     foreach my $field (@fields) {
 3946:         $datatable .= '<label>'.
 3947:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'" />'.
 3948:                       $lt{$field}.'</label>'.('&nbsp;' x2);
 3949:     }
 3950:     $datatable .= '</span></fieldset>'.
 3951:                   '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 3952:     foreach my $role (@courseroles) {
 3953:         my ($checked,$checkednone);
 3954:         $datatable .= '<td align="center">'.
 3955:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 3956:                       '<select name="ltitools_add_roles_'.$role.'">'.
 3957:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 3958:         foreach my $ltirole (@ltiroles) {
 3959:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 3960:         }
 3961:         $datatable .= '</select></td>';
 3962:     }
 3963:     $datatable .= '</tr></table></fieldset>'.
 3964:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 3965:     foreach my $item ('label','title','target') {
 3966:          $datatable .= '<label>'.
 3967:                        '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 3968:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 3969:     }
 3970:     $datatable .= '</span></fieldset>'.
 3971:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 3972:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 3973:                   '<tr><td><span class="LC_nobreak">'.
 3974:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 3975:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 3976:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 3977:                   '</table></fieldset></td></tr>'."\n".
 3978:                   '</td>'."\n".
 3979:                   '</tr>'."\n";
 3980:     $itemcount ++;
 3981:     return $datatable;
 3982: }
 3983: 
 3984: sub ltitools_names {
 3985:     my %lt = &Apache::lonlocal::texthash(
 3986:                                           'title'     => 'Title',
 3987:                                           'version'   => 'Version',
 3988:                                           'msgtype'   => 'Message Type',
 3989:                                           'url'       => 'URL',
 3990:                                           'key'       => 'Key',
 3991:                                           'secret'    => 'Secret',
 3992:                                           'icon'      => 'Icon',   
 3993:                                           'user'      => 'Username:domain',
 3994:                                           'fullname'  => 'Full Name',
 3995:                                           'firstname' => 'First Name',
 3996:                                           'lastname'  => 'Last Name',
 3997:                                           'email'     => 'E-mail',
 3998:                                           'roles'     => 'Role',
 3999:                                           'window'    => 'Window/Tab',
 4000:                                           'iframe'    => 'iFrame',
 4001:                                           'height'    => 'Height',
 4002:                                           'width'     => 'Width',
 4003:                                           'passback'  => 'Tool can return grades:',
 4004:                                           'roster'    => 'Tool can retrieve roster:',
 4005:                                           'crstarget' => 'Display target',
 4006:                                           'crslabel'  => 'Course label',
 4007:                                           'crstitle'  => 'Course title', 
 4008:                                         );
 4009:     return %lt;
 4010: }
 4011: 
 4012: sub print_coursedefaults {
 4013:     my ($position,$dom,$settings,$rowtotal) = @_;
 4014:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 4015:     my $itemcount = 1;
 4016:     my %choices =  &Apache::lonlocal::texthash (
 4017:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 4018:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 4019:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 4020:         coursecredits        => 'Credits can be specified for courses',
 4021:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 4022:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 4023:         postsubmit           => 'Disable submit button/keypress following student submission',
 4024:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 4025:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 4026:     );
 4027:     my %staticdefaults = (
 4028:                            anonsurvey_threshold => 10,
 4029:                            uploadquota          => 500,
 4030:                            postsubmit           => 60,
 4031:                            mysqltables          => 172800,
 4032:                          );
 4033:     if ($position eq 'top') {
 4034:         %defaultchecked = (
 4035:                             'canuse_pdfforms' => 'off',
 4036:                             'uselcmath'       => 'on',
 4037:                             'usejsme'         => 'on',
 4038:                             'canclone'        => 'none',
 4039:                           );
 4040:         @toggles = ('canuse_pdfforms','uselcmath','usejsme');
 4041:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4042:                                                      \%choices,$itemcount);
 4043:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4044:         $datatable .=
 4045:             '<tr'.$css_class.'><td valign="top">'.
 4046:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 4047:             '</span></td><td class="LC_left_item">';
 4048:         my $currcanclone = 'none';
 4049:         my $onclick;
 4050:         my @cloneoptions = ('none','domain');
 4051:         my %clonetitles = (
 4052:                              none     => 'No additional course requesters',
 4053:                              domain   => "Any course requester in course's domain",
 4054:                              instcode => 'Course requests for official courses ...',
 4055:                           );
 4056:         my (%codedefaults,@code_order,@posscodes);
 4057:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 4058:                                                     \@code_order) eq 'ok') {
 4059:             if (@code_order > 0) {
 4060:                 push(@cloneoptions,'instcode');
 4061:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 4062:             }
 4063:         }
 4064:         if (ref($settings) eq 'HASH') {
 4065:             if ($settings->{'canclone'}) {
 4066:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 4067:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 4068:                         if (@code_order > 0) {
 4069:                             $currcanclone = 'instcode';
 4070:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 4071:                         }
 4072:                     }
 4073:                 } elsif ($settings->{'canclone'} eq 'domain') {
 4074:                     $currcanclone = $settings->{'canclone'};
 4075:                 }
 4076:             }
 4077:         }
 4078:         foreach my $option (@cloneoptions) {
 4079:             my ($checked,$additional);
 4080:             if ($currcanclone eq $option) {
 4081:                 $checked = ' checked="checked"';
 4082:             }
 4083:             if ($option eq 'instcode') {
 4084:                 if (@code_order) {
 4085:                     my $show = 'none';
 4086:                     if ($checked) {
 4087:                         $show = 'block';
 4088:                     }
 4089:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 4090:                                   &mt('Institutional codes for new and cloned course have identical:').
 4091:                                   '<br />';
 4092:                     foreach my $item (@code_order) {
 4093:                         my $codechk;
 4094:                         if ($checked) {
 4095:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 4096:                                 $codechk = ' checked="checked"';
 4097:                             }
 4098:                         }
 4099:                         $additional .= '<label>'.
 4100:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 4101:                                        $item.'</label>';
 4102:                     }
 4103:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 4104:                 }
 4105:             }
 4106:             $datatable .=
 4107:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 4108:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 4109:                 '</label>&nbsp;'.$additional.'</span><br />';
 4110:         }
 4111:         $datatable .= '</td>'.
 4112:                       '</tr>';
 4113:         $itemcount ++;
 4114:     } else {
 4115:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4116:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 4117:         my $currusecredits = 0;
 4118:         my $postsubmitclient = 1;
 4119:         my @types = ('official','unofficial','community','textbook','placement');
 4120:         if (ref($settings) eq 'HASH') {
 4121:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 4122:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 4123:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 4124:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 4125:                 }
 4126:             }
 4127:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 4128:                 foreach my $type (@types) {
 4129:                     next if ($type eq 'community');
 4130:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 4131:                     if ($defcredits{$type} ne '') {
 4132:                         $currusecredits = 1;
 4133:                     }
 4134:                 }
 4135:             }
 4136:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 4137:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 4138:                     $postsubmitclient = 0;
 4139:                     foreach my $type (@types) {
 4140:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4141:                     }
 4142:                 } else {
 4143:                     foreach my $type (@types) {
 4144:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 4145:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 4146:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 4147:                             } else {
 4148:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4149:                             }
 4150:                         } else {
 4151:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4152:                         }
 4153:                     }
 4154:                 }
 4155:             } else {
 4156:                 foreach my $type (@types) {
 4157:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4158:                 }
 4159:             }
 4160:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 4161:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 4162:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 4163:                 }
 4164:             } else {
 4165:                 foreach my $type (@types) {
 4166:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 4167:                 }
 4168:             }
 4169:         } else {
 4170:             foreach my $type (@types) {
 4171:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4172:             }
 4173:         }
 4174:         if (!$currdefresponder) {
 4175:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 4176:         } elsif ($currdefresponder < 1) {
 4177:             $currdefresponder = 1;
 4178:         }
 4179:         foreach my $type (@types) {
 4180:             if ($curruploadquota{$type} eq '') {
 4181:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 4182:             }
 4183:         }
 4184:         $datatable .=
 4185:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4186:                 $choices{'anonsurvey_threshold'}.
 4187:                 '</span></td>'.
 4188:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 4189:                 '<input type="text" name="anonsurvey_threshold"'.
 4190:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 4191:                 '</td></tr>'."\n";
 4192:         $itemcount ++;
 4193:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4194:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4195:                       $choices{'uploadquota'}.
 4196:                       '</span></td>'.
 4197:                       '<td align="right" class="LC_right_item">'.
 4198:                       '<table><tr>';
 4199:         foreach my $type (@types) {
 4200:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4201:                            '<input type="text" name="uploadquota_'.$type.'"'.
 4202:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 4203:         }
 4204:         $datatable .= '</tr></table></td></tr>'."\n";
 4205:         $itemcount ++;
 4206:         my $onclick = "toggleDisplay(this.form,'credits');";
 4207:         my $display = 'none';
 4208:         if ($currusecredits) {
 4209:             $display = 'block';
 4210:         }
 4211:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 4212:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 4213:         foreach my $type (@types) {
 4214:             next if ($type eq 'community');
 4215:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4216:                            '<input type="text" name="'.$type.'_credits"'.
 4217:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 4218:         }
 4219:         $additional .= '</tr></table></div>'."\n";
 4220:         %defaultchecked = ('coursecredits' => 'off');
 4221:         @toggles = ('coursecredits');
 4222:         my $current = {
 4223:                         'coursecredits' => $currusecredits,
 4224:                       };
 4225:         (my $table,$itemcount) =
 4226:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4227:                                \%choices,$itemcount,$onclick,$additional,'left');
 4228:         $datatable .= $table;
 4229:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 4230:         my $display = 'none';
 4231:         if ($postsubmitclient) {
 4232:             $display = 'block';
 4233:         }
 4234:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 4235:                       &mt('Number of seconds submit is disabled').'<br />'.
 4236:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 4237:                       '<table><tr>';
 4238:         foreach my $type (@types) {
 4239:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4240:                            '<input type="text" name="'.$type.'_timeout" value="'.
 4241:                            $deftimeout{$type}.'" size="5" /></td>';
 4242:         }
 4243:         $additional .= '</tr></table></div>'."\n";
 4244:         %defaultchecked = ('postsubmit' => 'on');
 4245:         @toggles = ('postsubmit');
 4246:         $current = {
 4247:                        'postsubmit' => $postsubmitclient,
 4248:                    };
 4249:         ($table,$itemcount) =
 4250:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4251:                                \%choices,$itemcount,$onclick,$additional,'left');
 4252:         $datatable .= $table;
 4253:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4254:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4255:                       $choices{'mysqltables'}.
 4256:                       '</span></td>'.
 4257:                       '<td align="right" class="LC_right_item">'.
 4258:                       '<table><tr>';
 4259:         foreach my $type (@types) {
 4260:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4261:                            '<input type="text" name="mysqltables_'.$type.'"'.
 4262:                            ' value="'.$currmysql{$type}.'" size="5" /></td>';
 4263:         }
 4264:         $datatable .= '</tr></table></td></tr>'."\n";
 4265:         $itemcount ++;
 4266: 
 4267:     }
 4268:     $$rowtotal += $itemcount;
 4269:     return $datatable;
 4270: }
 4271: 
 4272: sub print_selfenrollment {
 4273:     my ($position,$dom,$settings,$rowtotal) = @_;
 4274:     my ($css_class,$datatable);
 4275:     my $itemcount = 1;
 4276:     my @types = ('official','unofficial','community','textbook','placement');
 4277:     if (($position eq 'top') || ($position eq 'middle')) {
 4278:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 4279:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 4280:         my @rows;
 4281:         my $key;
 4282:         if ($position eq 'top') {
 4283:             $key = 'admin'; 
 4284:             if (ref($rowsref) eq 'ARRAY') {
 4285:                 @rows = @{$rowsref};
 4286:             }
 4287:         } elsif ($position eq 'middle') {
 4288:             $key = 'default';
 4289:             @rows = ('types','registered','approval','limit');
 4290:         }
 4291:         foreach my $row (@rows) {
 4292:             if (defined($titlesref->{$row})) {
 4293:                 $itemcount ++;
 4294:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4295:                 $datatable .= '<tr'.$css_class.'>'.
 4296:                               '<td>'.$titlesref->{$row}.'</td>'.
 4297:                               '<td class="LC_left_item">'.
 4298:                               '<table><tr>';
 4299:                 my (%current,%currentcap);
 4300:                 if (ref($settings) eq 'HASH') {
 4301:                     if (ref($settings->{$key}) eq 'HASH') {
 4302:                         foreach my $type (@types) {
 4303:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4304:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 4305:                             }
 4306:                             if (($row eq 'limit') && ($key eq 'default')) {
 4307:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4308:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 4309:                                 }
 4310:                             }
 4311:                         }
 4312:                     }
 4313:                 }
 4314:                 my %roles = (
 4315:                              '0' => &Apache::lonnet::plaintext('dc'),
 4316:                             ); 
 4317:             
 4318:                 foreach my $type (@types) {
 4319:                     unless (($row eq 'registered') && ($key eq 'default')) {
 4320:                         $datatable .= '<th>'.&mt($type).'</th>';
 4321:                     }
 4322:                 }
 4323:                 unless (($row eq 'registered') && ($key eq 'default')) {
 4324:                     $datatable .= '</tr><tr>';
 4325:                 }
 4326:                 foreach my $type (@types) {
 4327:                     if ($type eq 'community') {
 4328:                         $roles{'1'} = &mt('Community personnel');
 4329:                     } else {
 4330:                         $roles{'1'} = &mt('Course personnel');
 4331:                     }
 4332:                     $datatable .= '<td style="vertical-align: top">';
 4333:                     if ($position eq 'top') {
 4334:                         my %checked;
 4335:                         if ($current{$type} eq '0') {
 4336:                             $checked{'0'} = ' checked="checked"';
 4337:                         } else {
 4338:                             $checked{'1'} = ' checked="checked"';
 4339:                         }
 4340:                         foreach my $role ('1','0') {
 4341:                             $datatable .= '<span class="LC_nobreak"><label>'.
 4342:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 4343:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 4344:                                           $roles{$role}.'</label></span> ';
 4345:                         }
 4346:                     } else {
 4347:                         if ($row eq 'types') {
 4348:                             my %checked;
 4349:                             if ($current{$type} =~ /^(all|dom)$/) {
 4350:                                 $checked{$1} = ' checked="checked"';
 4351:                             } else {
 4352:                                 $checked{''} = ' checked="checked"';
 4353:                             }
 4354:                             foreach my $val ('','dom','all') {
 4355:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4356:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4357:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4358:                             }
 4359:                         } elsif ($row eq 'registered') {
 4360:                             my %checked;
 4361:                             if ($current{$type} eq '1') {
 4362:                                 $checked{'1'} = ' checked="checked"';
 4363:                             } else {
 4364:                                 $checked{'0'} = ' checked="checked"';
 4365:                             }
 4366:                             foreach my $val ('0','1') {
 4367:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4368:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4369:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4370:                             }
 4371:                         } elsif ($row eq 'approval') {
 4372:                             my %checked;
 4373:                             if ($current{$type} =~ /^([12])$/) {
 4374:                                 $checked{$1} = ' checked="checked"';
 4375:                             } else {
 4376:                                 $checked{'0'} = ' checked="checked"';
 4377:                             }
 4378:                             for my $val (0..2) {
 4379:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4380:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4381:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4382:                             }
 4383:                         } elsif ($row eq 'limit') {
 4384:                             my %checked;
 4385:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 4386:                                 $checked{$1} = ' checked="checked"';
 4387:                             } else {
 4388:                                 $checked{'none'} = ' checked="checked"';
 4389:                             }
 4390:                             my $cap;
 4391:                             if ($currentcap{$type} =~ /^\d+$/) {
 4392:                                 $cap = $currentcap{$type};
 4393:                             }
 4394:                             foreach my $val ('none','allstudents','selfenrolled') {
 4395:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4396:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4397:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4398:                             }
 4399:                             $datatable .= '<br />'.
 4400:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 4401:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 4402:                                           '</span>'; 
 4403:                         }
 4404:                     }
 4405:                     $datatable .= '</td>';
 4406:                 }
 4407:                 $datatable .= '</tr>';
 4408:             }
 4409:             $datatable .= '</table></td></tr>';
 4410:         }
 4411:     } elsif ($position eq 'bottom') {
 4412:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 4413:     }
 4414:     $$rowtotal += $itemcount;
 4415:     return $datatable;
 4416: }
 4417: 
 4418: sub print_validation_rows {
 4419:     my ($caller,$dom,$settings,$rowtotal) = @_;
 4420:     my ($itemsref,$namesref,$fieldsref);
 4421:     if ($caller eq 'selfenroll') { 
 4422:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 4423:     } elsif ($caller eq 'requestcourses') {
 4424:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 4425:     }
 4426:     my %currvalidation;
 4427:     if (ref($settings) eq 'HASH') {
 4428:         if (ref($settings->{'validation'}) eq 'HASH') {
 4429:             %currvalidation = %{$settings->{'validation'}};
 4430:         }
 4431:     }
 4432:     my $datatable;
 4433:     my $itemcount = 0;
 4434:     foreach my $item (@{$itemsref}) {
 4435:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4436:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4437:                       $namesref->{$item}.
 4438:                       '</span></td>'.
 4439:                       '<td class="LC_left_item">';
 4440:         if (($item eq 'url') || ($item eq 'button')) {
 4441:             $datatable .= '<span class="LC_nobreak">'.
 4442:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 4443:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 4444:         } elsif ($item eq 'fields') {
 4445:             my @currfields;
 4446:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 4447:                 @currfields = @{$currvalidation{$item}};
 4448:             }
 4449:             foreach my $field (@{$fieldsref}) {
 4450:                 my $check = '';
 4451:                 if (grep(/^\Q$field\E$/,@currfields)) {
 4452:                     $check = ' checked="checked"';
 4453:                 }
 4454:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4455:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 4456:                               ' value="'.$field.'"'.$check.' />'.$field.
 4457:                               '</label></span> ';
 4458:             }
 4459:         } elsif ($item eq 'markup') {
 4460:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 4461:                            $currvalidation{$item}.
 4462:                               '</textarea>';
 4463:         }
 4464:         $datatable .= '</td></tr>'."\n";
 4465:         if (ref($rowtotal)) {
 4466:             $itemcount ++;
 4467:         }
 4468:     }
 4469:     if ($caller eq 'requestcourses') {
 4470:         my %currhash;
 4471:         if (ref($settings) eq 'HASH') {
 4472:             if (ref($settings->{'validation'}) eq 'HASH') {
 4473:                 if ($settings->{'validation'}{'dc'} ne '') {
 4474:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 4475:                 }
 4476:             }
 4477:         }
 4478:         my $numinrow = 2;
 4479:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4480:                                                        'validationdc',%currhash);
 4481:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4482:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 4483:         if ($numdc > 1) {
 4484:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 4485:         } else {
 4486:             $datatable .=  &mt('Course creation processed as: ');
 4487:         }
 4488:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 4489:         $itemcount ++;
 4490:     }
 4491:     if (ref($rowtotal)) {
 4492:         $$rowtotal += $itemcount;
 4493:     }
 4494:     return $datatable;
 4495: }
 4496: 
 4497: sub print_usersessions {
 4498:     my ($position,$dom,$settings,$rowtotal) = @_;
 4499:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 4500:     my (%by_ip,%by_location,@intdoms,@instdoms);
 4501:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4502: 
 4503:     my @alldoms = &Apache::lonnet::all_domains();
 4504:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4505:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4506:     my %altids = &id_for_thisdom(%servers);
 4507:     if ($position eq 'top') {
 4508:         if (keys(%serverhomes) > 1) {
 4509:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 4510:             my $curroffloadnow;
 4511:             if (ref($settings) eq 'HASH') {
 4512:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 4513:                     $curroffloadnow = $settings->{'offloadnow'};
 4514:                 }
 4515:             }
 4516:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 4517:         } else {
 4518:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4519:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 4520:                           '</td></tr>';
 4521:         }
 4522:     } else {
 4523:         my %titles = &usersession_titles();
 4524:         my ($prefix,@types);
 4525:         if ($position eq 'bottom') {
 4526:             $prefix = 'remote';
 4527:             @types = ('version','excludedomain','includedomain');
 4528:         } else {
 4529:             $prefix = 'hosted';
 4530:             @types = ('excludedomain','includedomain');
 4531:         }
 4532:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4533:     }
 4534:     $$rowtotal += $itemcount;
 4535:     return $datatable;
 4536: }
 4537: 
 4538: sub rules_by_location {
 4539:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 4540:     my ($datatable,$itemcount,$css_class);
 4541:     if (keys(%{$by_location}) == 0) {
 4542:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4543:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 4544:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 4545:                      '</td></tr>';
 4546:         $itemcount = 1;
 4547:     } else {
 4548:         $itemcount = 0;
 4549:         my $numinrow = 5;
 4550:         my (%current,%checkedon,%checkedoff);
 4551:         my @locations = sort(keys(%{$by_location}));
 4552:         foreach my $type (@{$types}) {
 4553:             $checkedon{$type} = '';
 4554:             $checkedoff{$type} = ' checked="checked"';
 4555:         }
 4556:         if (ref($settings) eq 'HASH') {
 4557:             if (ref($settings->{$prefix}) eq 'HASH') {
 4558:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 4559:                     $current{$key} = $settings->{$prefix}{$key};
 4560:                     if ($key eq 'version') {
 4561:                         if ($current{$key} ne '') {
 4562:                             $checkedon{$key} = ' checked="checked"';
 4563:                             $checkedoff{$key} = '';
 4564:                         }
 4565:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 4566:                         $checkedon{$key} = ' checked="checked"';
 4567:                         $checkedoff{$key} = '';
 4568:                     }
 4569:                 }
 4570:             }
 4571:         }
 4572:         foreach my $type (@{$types}) {
 4573:             next if ($type ne 'version' && !@locations);
 4574:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4575:             $datatable .= '<tr'.$css_class.'>
 4576:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 4577:                            <span class="LC_nobreak">&nbsp;
 4578:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 4579:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 4580:             if ($type eq 'version') {
 4581:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 4582:                 my $selector = '<select name="'.$prefix.'_version">';
 4583:                 foreach my $version (@lcversions) {
 4584:                     my $selected = '';
 4585:                     if ($current{'version'} eq $version) {
 4586:                         $selected = ' selected="selected"';
 4587:                     }
 4588:                     $selector .= ' <option value="'.$version.'"'.
 4589:                                  $selected.'>'.$version.'</option>';
 4590:                 }
 4591:                 $selector .= '</select> ';
 4592:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 4593:             } else {
 4594:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 4595:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 4596:                              ' />'.('&nbsp;'x2).
 4597:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 4598:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 4599:                              "\n".
 4600:                              '</div><div><table>';
 4601:                 my $rem;
 4602:                 for (my $i=0; $i<@locations; $i++) {
 4603:                     my ($showloc,$value,$checkedtype);
 4604:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 4605:                         my $ip = $by_location->{$locations[$i]}->[0];
 4606:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4607:                             $value = join(':',@{$by_ip->{$ip}});
 4608:                             $showloc = join(', ',@{$by_ip->{$ip}});
 4609:                             if (ref($current{$type}) eq 'ARRAY') {
 4610:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 4611:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 4612:                                         $checkedtype = ' checked="checked"';
 4613:                                         last;
 4614:                                     }
 4615:                                 }
 4616:                             }
 4617:                         }
 4618:                     }
 4619:                     $rem = $i%($numinrow);
 4620:                     if ($rem == 0) {
 4621:                         if ($i > 0) {
 4622:                             $datatable .= '</tr>';
 4623:                         }
 4624:                         $datatable .= '<tr>';
 4625:                     }
 4626:                     $datatable .= '<td class="LC_left_item">'.
 4627:                                   '<span class="LC_nobreak"><label>'.
 4628:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 4629:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 4630:                                   '</label></span></td>';
 4631:                 }
 4632:                 $rem = @locations%($numinrow);
 4633:                 my $colsleft = $numinrow - $rem;
 4634:                 if ($colsleft > 1 ) {
 4635:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4636:                                   '&nbsp;</td>';
 4637:                 } elsif ($colsleft == 1) {
 4638:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4639:                 }
 4640:                 $datatable .= '</tr></table>';
 4641:             }
 4642:             $datatable .= '</td></tr>';
 4643:             $itemcount ++;
 4644:         }
 4645:     }
 4646:     return ($datatable,$itemcount);
 4647: }
 4648: 
 4649: sub print_ssl {
 4650:     my ($position,$dom,$settings,$rowtotal) = @_;
 4651:     my ($css_class,$datatable);
 4652:     my $itemcount = 1;
 4653:     if ($position eq 'top') {
 4654:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 4655:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 4656:         my $same_institution;
 4657:         if ($intdom ne '') {
 4658:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 4659:             if (ref($internet_names) eq 'ARRAY') {
 4660:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 4661:                     $same_institution = 1;
 4662:                 }
 4663:             }
 4664:         }
 4665:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4666:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 4667:         if ($same_institution) {
 4668:             my %domservers = &Apache::lonnet::get_servers($dom);
 4669:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 4670:         } else {
 4671:             $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.");
 4672:         }
 4673:         $datatable .= '</td></tr>';
 4674:         $itemcount ++;
 4675:     } else {
 4676:         my %titles = &ssl_titles();
 4677:         my (%by_ip,%by_location,@intdoms,@instdoms);
 4678:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4679:         my @alldoms = &Apache::lonnet::all_domains();
 4680:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4681:         my @domservers = &Apache::lonnet::get_servers($dom);
 4682:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4683:         my %altids = &id_for_thisdom(%servers);
 4684:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 4685:             my $legacy;
 4686:             unless (ref($settings) eq 'HASH') {
 4687:                 my $name;
 4688:                 if ($position eq 'connto') {
 4689:                     $name = 'loncAllowInsecure';
 4690:                 } else {
 4691:                     $name = 'londAllowInsecure';
 4692:                 }
 4693:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 4694:                 my @ids=&Apache::lonnet::current_machine_ids();
 4695:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 4696:                     my %what = (
 4697:                                    $name => 1,
 4698:                                );
 4699:                     my ($result,$returnhash) =
 4700:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 4701:                     if ($result eq 'ok') {
 4702:                         if (ref($returnhash) eq 'HASH') {
 4703:                             $legacy = $returnhash->{$name};
 4704:                         }
 4705:                     }
 4706:                 } else {
 4707:                     $legacy = $Apache::lonnet::perlvar{$name};
 4708:                 }
 4709:             }
 4710:             foreach my $type ('dom','intdom','other') {
 4711:                 my %checked;
 4712:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4713:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 4714:                               '<td class="LC_right_item">';
 4715:                 my $skip; 
 4716:                 if ($type eq 'dom') {
 4717:                     unless (keys(%servers) > 1) {
 4718:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 4719:                         $skip = 1;
 4720:                     }
 4721:                 }
 4722:                 if ($type eq 'intdom') {
 4723:                     unless (@instdoms > 1) {
 4724:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 4725:                         $skip = 1;
 4726:                     } 
 4727:                 } elsif ($type eq 'other') {
 4728:                     if (keys(%by_location) == 0) {
 4729:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 4730:                         $skip = 1;
 4731:                     }
 4732:                 }
 4733:                 unless ($skip) {
 4734:                     $checked{'yes'} = ' checked="checked"'; 
 4735:                     if (ref($settings) eq 'HASH') {
 4736:                         if (ref($settings->{$position}) eq 'HASH') {
 4737:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 4738:                                 $checked{$1} = $checked{'yes'};
 4739:                                 delete($checked{'yes'}); 
 4740:                             }
 4741:                         }
 4742:                     } else {
 4743:                         if ($legacy == 0) {
 4744:                             $checked{'req'} = $checked{'yes'};
 4745:                             delete($checked{'yes'});    
 4746:                         }
 4747:                     }
 4748:                     foreach my $option ('no','yes','req') {
 4749:                         $datatable .= '<span class="LC_nobreak"><label>'.
 4750:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 4751:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 4752:                                       '</label></span>'.('&nbsp;'x2);
 4753:                     }
 4754:                 }
 4755:                 $datatable .= '</td></tr>';
 4756:                 $itemcount ++; 
 4757:             }
 4758:         } else {
 4759:             my $prefix = 'replication';
 4760:             my @types = ('certreq','nocertreq');
 4761:             if (keys(%by_location) == 0) {
 4762:                 $datatable .= '<tr'.$css_class.'><td>'.
 4763:                               &mt('Nothing to set here, as there are no other institutions').
 4764:                               '</td></tr>';
 4765:                 $itemcount ++;
 4766:             } else {
 4767:                 ($datatable,$itemcount) = 
 4768:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4769:             }
 4770:         }
 4771:     }
 4772:     $$rowtotal += $itemcount;
 4773:     return $datatable;
 4774: }
 4775: 
 4776: sub ssl_titles {
 4777:     return &Apache::lonlocal::texthash (
 4778:                dom           => 'LON-CAPA servers/VMs from same domain',
 4779:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 4780:                other         => 'External LON-CAPA servers/VMs',
 4781:                connto        => 'Connections to other servers',
 4782:                connfrom      => 'Connections from other servers',
 4783:                replication   => 'Replicating content to other institutions',
 4784:                certreq       => 'Client certificate required, but specific domains exempt',
 4785:                nocertreq     => 'No client certificate required, except for specific domains',
 4786:                no            => 'SSL not used',
 4787:                yes           => 'SSL Optional (used if available)',
 4788:                req           => 'SSL Required',
 4789:     );
 4790: }
 4791: 
 4792: sub print_trust {
 4793:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 4794:     my ($css_class,$datatable,%checked,%choices);
 4795:     my (%by_ip,%by_location,@intdoms,@instdoms);
 4796:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4797:     my $itemcount = 1;
 4798:     my %titles = &trust_titles();
 4799:     my @types = ('exc','inc');
 4800:     if ($prefix eq 'top') {
 4801:         $prefix = 'content';
 4802:     } elsif ($prefix eq 'bottom') {
 4803:         $prefix = 'msg';
 4804:     }
 4805:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4806:     $$rowtotal += $itemcount;
 4807:     return $datatable;
 4808: }
 4809: 
 4810: sub trust_titles {
 4811:     return &Apache::lonlocal::texthash(
 4812:                content  => "Access to this domain's content by others",
 4813:                shared   => "Access to other domain's content by this domain",
 4814:                enroll   => "Enrollment in this domain's courses by others", 
 4815:                othcoau  => "Co-author roles in this domain for others",
 4816:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 4817:                domroles => "Domain roles in this domain assignable to others",
 4818:                catalog  => "Course Catalog for this domain displayed elsewhere",
 4819:                reqcrs   => "Requests for creation of courses in this domain by others",
 4820:                msg      => "Users in other domains can send messages to this domain",
 4821:                exc      => "Allow all, but exclude specific domains",
 4822:                inc      => "Deny all, but include specific domains",
 4823:            );
 4824: } 
 4825: 
 4826: sub build_location_hashes {
 4827:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 4828:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 4829:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 4830:     my %iphost = &Apache::lonnet::get_iphost();
 4831:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 4832:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 4833:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 4834:         foreach my $id (@{$iphost{$primary_ip}}) {
 4835:             my $intdom = &Apache::lonnet::internet_dom($id);
 4836:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 4837:                 push(@{$intdoms},$intdom);
 4838:             }
 4839:         }
 4840:     }
 4841:     foreach my $ip (keys(%iphost)) {
 4842:         if (ref($iphost{$ip}) eq 'ARRAY') {
 4843:             foreach my $id (@{$iphost{$ip}}) {
 4844:                 my $location = &Apache::lonnet::internet_dom($id);
 4845:                 if ($location) {
 4846:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 4847:                         my $dom = &Apache::lonnet::host_domain($id);
 4848:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 4849:                             push(@{$instdoms},$dom);
 4850:                         }
 4851:                         next;
 4852:                     }
 4853:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4854:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 4855:                             push(@{$by_ip->{$ip}},$location);
 4856:                         }
 4857:                     } else {
 4858:                         $by_ip->{$ip} = [$location];
 4859:                     }
 4860:                 }
 4861:             }
 4862:         }
 4863:     }
 4864:     foreach my $ip (sort(keys(%{$by_ip}))) {
 4865:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4866:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 4867:             my $first = $by_ip->{$ip}->[0];
 4868:             if (ref($by_location->{$first}) eq 'ARRAY') {
 4869:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 4870:                     push(@{$by_location->{$first}},$ip);
 4871:                 }
 4872:             } else {
 4873:                 $by_location->{$first} = [$ip];
 4874:             }
 4875:         }
 4876:     }
 4877:     return;
 4878: }
 4879: 
 4880: sub current_offloads_to {
 4881:     my ($dom,$settings,$servers) = @_;
 4882:     my (%spareid,%otherdomconfigs);
 4883:     if (ref($servers) eq 'HASH') {
 4884:         foreach my $lonhost (sort(keys(%{$servers}))) {
 4885:             my $gotspares;
 4886:             if (ref($settings) eq 'HASH') {
 4887:                 if (ref($settings->{'spares'}) eq 'HASH') {
 4888:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 4889:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 4890:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 4891:                         $gotspares = 1;
 4892:                     }
 4893:                 }
 4894:             }
 4895:             unless ($gotspares) {
 4896:                 my $gotspares;
 4897:                 my $serverhomeID =
 4898:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 4899:                 my $serverhomedom =
 4900:                     &Apache::lonnet::host_domain($serverhomeID);
 4901:                 if ($serverhomedom ne $dom) {
 4902:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 4903:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4904:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4905:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4906:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4907:                                 $gotspares = 1;
 4908:                             }
 4909:                         }
 4910:                     } else {
 4911:                         $otherdomconfigs{$serverhomedom} =
 4912:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 4913:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 4914:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4915:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4916:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 4917:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4918:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4919:                                         $gotspares = 1;
 4920:                                     }
 4921:                                 }
 4922:                             }
 4923:                         }
 4924:                     }
 4925:                 }
 4926:             }
 4927:             unless ($gotspares) {
 4928:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4929:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4930:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4931:                } else {
 4932:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 4933:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 4934:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4935:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4936:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4937:                     } else {
 4938:                         my %what = (
 4939:                              spareid => 1,
 4940:                         );
 4941:                         my ($result,$returnhash) = 
 4942:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 4943:                         if ($result eq 'ok') { 
 4944:                             if (ref($returnhash) eq 'HASH') {
 4945:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 4946:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 4947:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 4948:                                 }
 4949:                             }
 4950:                         }
 4951:                     }
 4952:                 }
 4953:             }
 4954:         }
 4955:     }
 4956:     return %spareid;
 4957: }
 4958: 
 4959: sub spares_row {
 4960:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 4961:     my $css_class;
 4962:     my $numinrow = 4;
 4963:     my $itemcount = 1;
 4964:     my $datatable;
 4965:     my %typetitles = &sparestype_titles();
 4966:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4967:         foreach my $server (sort(keys(%{$servers}))) {
 4968:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 4969:             my ($othercontrol,$serverdom);
 4970:             if ($serverhome ne $server) {
 4971:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 4972:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4973:             } else {
 4974:                 $serverdom = &Apache::lonnet::host_domain($server);
 4975:                 if ($serverdom ne $dom) {
 4976:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4977:                 }
 4978:             }
 4979:             next unless (ref($spareid->{$server}) eq 'HASH');
 4980:             my $checkednow;
 4981:             if (ref($curroffloadnow) eq 'HASH') {
 4982:                 if ($curroffloadnow->{$server}) {
 4983:                     $checkednow = ' checked="checked"';
 4984:                 }
 4985:             }
 4986:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4987:             $datatable .= '<tr'.$css_class.'>
 4988:                            <td rowspan="2">
 4989:                             <span class="LC_nobreak">'.
 4990:                           &mt('[_1] when busy, offloads to:'
 4991:                               ,'<b>'.$server.'</b>').'</span><br />'.
 4992:                           '<span class="LC_nobreak">'."\n".
 4993:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 4994:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 4995:                           "\n";
 4996:             my (%current,%canselect);
 4997:             my @choices = 
 4998:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 4999:             foreach my $type ('primary','default') {
 5000:                 if (ref($spareid->{$server}) eq 'HASH') {
 5001:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 5002:                         my @spares = @{$spareid->{$server}{$type}};
 5003:                         if (@spares > 0) {
 5004:                             if ($othercontrol) {
 5005:                                 $current{$type} = join(', ',@spares);
 5006:                             } else {
 5007:                                 $current{$type} .= '<table>';
 5008:                                 my $numspares = scalar(@spares);
 5009:                                 for (my $i=0;  $i<@spares; $i++) {
 5010:                                     my $rem = $i%($numinrow);
 5011:                                     if ($rem == 0) {
 5012:                                         if ($i > 0) {
 5013:                                             $current{$type} .= '</tr>';
 5014:                                         }
 5015:                                         $current{$type} .= '<tr>';
 5016:                                     }
 5017:                                     $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;'.
 5018:                                                        $spareid->{$server}{$type}[$i].
 5019:                                                        '</label></td>'."\n";
 5020:                                 }
 5021:                                 my $rem = @spares%($numinrow);
 5022:                                 my $colsleft = $numinrow - $rem;
 5023:                                 if ($colsleft > 1 ) {
 5024:                                     $current{$type} .= '<td colspan="'.$colsleft.
 5025:                                                        '" class="LC_left_item">'.
 5026:                                                        '&nbsp;</td>';
 5027:                                 } elsif ($colsleft == 1) {
 5028:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 5029:                                 }
 5030:                                 $current{$type} .= '</tr></table>';
 5031:                             }
 5032:                         }
 5033:                     }
 5034:                     if ($current{$type} eq '') {
 5035:                         $current{$type} = &mt('None specified');
 5036:                     }
 5037:                     if ($othercontrol) {
 5038:                         if ($type eq 'primary') {
 5039:                             $canselect{$type} = $othercontrol;
 5040:                         }
 5041:                     } else {
 5042:                         $canselect{$type} = 
 5043:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 5044:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 5045:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 5046:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 5047:                         if (@choices > 0) {
 5048:                             foreach my $lonhost (@choices) {
 5049:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 5050:                             }
 5051:                         }
 5052:                         $canselect{$type} .= '</select>'."\n";
 5053:                     }
 5054:                 } else {
 5055:                     $current{$type} = &mt('Could not be determined');
 5056:                     if ($type eq 'primary') {
 5057:                         $canselect{$type} =  $othercontrol;
 5058:                     }
 5059:                 }
 5060:                 if ($type eq 'default') {
 5061:                     $datatable .= '<tr'.$css_class.'>';
 5062:                 }
 5063:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 5064:                               '<td>'.$current{$type}.'</td>'."\n".
 5065:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 5066:             }
 5067:             $itemcount ++;
 5068:         }
 5069:     }
 5070:     $$rowtotal += $itemcount;
 5071:     return $datatable;
 5072: }
 5073: 
 5074: sub possible_newspares {
 5075:     my ($server,$currspares,$serverhomes,$altids) = @_;
 5076:     my $serverhostname = &Apache::lonnet::hostname($server);
 5077:     my %excluded;
 5078:     if ($serverhostname ne '') {
 5079:         %excluded = (
 5080:                        $serverhostname => 1,
 5081:                     );
 5082:     }
 5083:     if (ref($currspares) eq 'HASH') {
 5084:         foreach my $type (keys(%{$currspares})) {
 5085:             if (ref($currspares->{$type}) eq 'ARRAY') {
 5086:                 if (@{$currspares->{$type}} > 0) {
 5087:                     foreach my $curr (@{$currspares->{$type}}) {
 5088:                         my $hostname = &Apache::lonnet::hostname($curr);
 5089:                         $excluded{$hostname} = 1;
 5090:                     }
 5091:                 }
 5092:             }
 5093:         }
 5094:     }
 5095:     my @choices;
 5096:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5097:         if (keys(%{$serverhomes}) > 1) {
 5098:             foreach my $name (sort(keys(%{$serverhomes}))) {
 5099:                 unless ($excluded{$name}) {
 5100:                     if (exists($altids->{$serverhomes->{$name}})) {
 5101:                         push(@choices,$altids->{$serverhomes->{$name}});
 5102:                     } else {
 5103:                         push(@choices,$serverhomes->{$name});
 5104:                     }
 5105:                 }
 5106:             }
 5107:         }
 5108:     }
 5109:     return sort(@choices);
 5110: }
 5111: 
 5112: sub print_loadbalancing {
 5113:     my ($dom,$settings,$rowtotal) = @_;
 5114:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5115:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5116:     my $numinrow = 1;
 5117:     my $datatable;
 5118:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5119:     my (%currbalancer,%currtargets,%currrules,%existing);
 5120:     if (ref($settings) eq 'HASH') {
 5121:         %existing = %{$settings};
 5122:     }
 5123:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 5124:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 5125:                                   \%currtargets,\%currrules);
 5126:     } else {
 5127:         return;
 5128:     }
 5129:     my ($othertitle,$usertypes,$types) =
 5130:         &Apache::loncommon::sorted_inst_types($dom);
 5131:     my $rownum = 8;
 5132:     if (ref($types) eq 'ARRAY') {
 5133:         $rownum += scalar(@{$types});
 5134:     }
 5135:     my @css_class = ('LC_odd_row','LC_even_row');
 5136:     my $balnum = 0;
 5137:     my $islast;
 5138:     my (@toshow,$disabledtext);
 5139:     if (keys(%currbalancer) > 0) {
 5140:         @toshow = sort(keys(%currbalancer));
 5141:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 5142:             push(@toshow,'');
 5143:         }
 5144:     } else {
 5145:         @toshow = ('');
 5146:         $disabledtext = &mt('No existing load balancer');
 5147:     }
 5148:     foreach my $lonhost (@toshow) {
 5149:         if ($balnum == scalar(@toshow)-1) {
 5150:             $islast = 1;
 5151:         } else {
 5152:             $islast = 0;
 5153:         }
 5154:         my $cssidx = $balnum%2;
 5155:         my $targets_div_style = 'display: none';
 5156:         my $disabled_div_style = 'display: block';
 5157:         my $homedom_div_style = 'display: none';
 5158:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 5159:                       '<td rowspan="'.$rownum.'" valign="top">'.
 5160:                       '<p>';
 5161:         if ($lonhost eq '') {
 5162:             $datatable .= '<span class="LC_nobreak">';
 5163:             if (keys(%currbalancer) > 0) {
 5164:                 $datatable .= &mt('Add balancer:');
 5165:             } else {
 5166:                 $datatable .= &mt('Enable balancer:');
 5167:             }
 5168:             $datatable .= '&nbsp;'.
 5169:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 5170:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 5171:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 5172:                           '<option value="" selected="selected">'.&mt('None').
 5173:                           '</option>'."\n";
 5174:             foreach my $server (sort(keys(%servers))) {
 5175:                 next if ($currbalancer{$server});
 5176:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 5177:             }
 5178:             $datatable .=
 5179:                 '</select>'."\n".
 5180:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 5181:         } else {
 5182:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 5183:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 5184:                            &mt('Stop balancing').'</label>'.
 5185:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 5186:             $targets_div_style = 'display: block';
 5187:             $disabled_div_style = 'display: none';
 5188:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 5189:                 $homedom_div_style = 'display: block';
 5190:             }
 5191:         }
 5192:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 5193:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 5194:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 5195:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 5196:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 5197:         my @sparestypes = ('primary','default');
 5198:         my %typetitles = &sparestype_titles();
 5199:         my %hostherechecked = (
 5200:                                   no => ' checked="checked"',
 5201:                               );
 5202:         foreach my $sparetype (@sparestypes) {
 5203:             my $targettable;
 5204:             for (my $i=0; $i<$numspares; $i++) {
 5205:                 my $checked;
 5206:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 5207:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5208:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5209:                             $checked = ' checked="checked"';
 5210:                         }
 5211:                     }
 5212:                 }
 5213:                 my ($chkboxval,$disabled);
 5214:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 5215:                     $chkboxval = $spares[$i];
 5216:                 }
 5217:                 if (exists($currbalancer{$spares[$i]})) {
 5218:                     $disabled = ' disabled="disabled"';
 5219:                 }
 5220:                 $targettable .=
 5221:                     '<td><span class="LC_nobreak"><label>'.
 5222:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 5223:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 5224:                     '</span></label></span></td>';
 5225:                 my $rem = $i%($numinrow);
 5226:                 if ($rem == 0) {
 5227:                     if (($i > 0) && ($i < $numspares-1)) {
 5228:                         $targettable .= '</tr>';
 5229:                     }
 5230:                     if ($i < $numspares-1) {
 5231:                         $targettable .= '<tr>';
 5232:                     }
 5233:                 }
 5234:             }
 5235:             if ($targettable ne '') {
 5236:                 my $rem = $numspares%($numinrow);
 5237:                 my $colsleft = $numinrow - $rem;
 5238:                 if ($colsleft > 1 ) {
 5239:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5240:                                     '&nbsp;</td>';
 5241:                 } elsif ($colsleft == 1) {
 5242:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 5243:                 }
 5244:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 5245:                                '<table><tr>'.$targettable.'</tr></table><br />';
 5246:             }
 5247:             $hostherechecked{$sparetype} = '';
 5248:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 5249:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5250:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5251:                         $hostherechecked{$sparetype} = ' checked="checked"';
 5252:                         $hostherechecked{'no'} = '';
 5253:                     }
 5254:                 }
 5255:             }
 5256:         }
 5257:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 5258:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 5259:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 5260:         foreach my $sparetype (@sparestypes) {
 5261:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 5262:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 5263:                           '</i></label><br />';
 5264:         }
 5265:         $datatable .= '</div></td></tr>'.
 5266:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 5267:                                            $othertitle,$usertypes,$types,\%servers,
 5268:                                            \%currbalancer,$lonhost,
 5269:                                            $targets_div_style,$homedom_div_style,
 5270:                                            $css_class[$cssidx],$balnum,$islast);
 5271:         $$rowtotal += $rownum;
 5272:         $balnum ++;
 5273:     }
 5274:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 5275:     return $datatable;
 5276: }
 5277: 
 5278: sub get_loadbalancers_config {
 5279:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 5280:     return unless ((ref($servers) eq 'HASH') &&
 5281:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 5282:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 5283:     if (keys(%{$existing}) > 0) {
 5284:         my $oldlonhost;
 5285:         foreach my $key (sort(keys(%{$existing}))) {
 5286:             if ($key eq 'lonhost') {
 5287:                 $oldlonhost = $existing->{'lonhost'};
 5288:                 $currbalancer->{$oldlonhost} = 1;
 5289:             } elsif ($key eq 'targets') {
 5290:                 if ($oldlonhost) {
 5291:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 5292:                 }
 5293:             } elsif ($key eq 'rules') {
 5294:                 if ($oldlonhost) {
 5295:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 5296:                 }
 5297:             } elsif (ref($existing->{$key}) eq 'HASH') {
 5298:                 $currbalancer->{$key} = 1;
 5299:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 5300:                 $currrules->{$key} = $existing->{$key}{'rules'};
 5301:             }
 5302:         }
 5303:     } else {
 5304:         my ($balancerref,$targetsref) =
 5305:                 &Apache::lonnet::get_lonbalancer_config($servers);
 5306:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 5307:             foreach my $server (sort(keys(%{$balancerref}))) {
 5308:                 $currbalancer->{$server} = 1;
 5309:                 $currtargets->{$server} = $targetsref->{$server};
 5310:             }
 5311:         }
 5312:     }
 5313:     return;
 5314: }
 5315: 
 5316: sub loadbalancing_rules {
 5317:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 5318:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 5319:         $css_class,$balnum,$islast) = @_;
 5320:     my $output;
 5321:     my $num = 0;
 5322:     my ($alltypes,$othertypes,$titles) =
 5323:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 5324:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 5325:         foreach my $type (@{$alltypes}) {
 5326:             $num ++;
 5327:             my $current;
 5328:             if (ref($currrules) eq 'HASH') {
 5329:                 $current = $currrules->{$type};
 5330:             }
 5331:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5332:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 5333:                     $current = '';
 5334:                 }
 5335:             }
 5336:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 5337:                                              $servers,$currbalancer,$lonhost,$dom,
 5338:                                              $targets_div_style,$homedom_div_style,
 5339:                                              $css_class,$balnum,$num,$islast);
 5340:         }
 5341:     }
 5342:     return $output;
 5343: }
 5344: 
 5345: sub loadbalancing_titles {
 5346:     my ($dom,$intdom,$usertypes,$types) = @_;
 5347:     my %othertypes = (
 5348:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 5349:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 5350:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 5351:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 5352:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 5353:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 5354:                      );
 5355:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 5356:     if (ref($types) eq 'ARRAY') {
 5357:         unshift(@alltypes,@{$types},'default');
 5358:     }
 5359:     my %titles;
 5360:     foreach my $type (@alltypes) {
 5361:         if ($type =~ /^_LC_/) {
 5362:             $titles{$type} = $othertypes{$type};
 5363:         } elsif ($type eq 'default') {
 5364:             $titles{$type} = &mt('All users from [_1]',$dom);
 5365:             if (ref($types) eq 'ARRAY') {
 5366:                 if (@{$types} > 0) {
 5367:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 5368:                 }
 5369:             }
 5370:         } elsif (ref($usertypes) eq 'HASH') {
 5371:             $titles{$type} = $usertypes->{$type};
 5372:         }
 5373:     }
 5374:     return (\@alltypes,\%othertypes,\%titles);
 5375: }
 5376: 
 5377: sub loadbalance_rule_row {
 5378:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 5379:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 5380:     my @rulenames;
 5381:     my %ruletitles = &offloadtype_text();
 5382:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 5383:         @rulenames = ('balancer','offloadedto','specific');
 5384:     } else {
 5385:         @rulenames = ('default','homeserver');
 5386:         if ($type eq '_LC_external') {
 5387:             push(@rulenames,'externalbalancer');
 5388:         } else {
 5389:             push(@rulenames,'specific');
 5390:         }
 5391:         push(@rulenames,'none');
 5392:     }
 5393:     my $style = $targets_div_style;
 5394:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5395:         $style = $homedom_div_style;
 5396:     }
 5397:     my $space;
 5398:     if ($islast && $num == 1) {
 5399:         $space = '<div display="inline-block">&nbsp;</div>';
 5400:     }
 5401:     my $output =
 5402:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 5403:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 5404:         '<td valaign="top">'.$space.
 5405:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 5406:     for (my $i=0; $i<@rulenames; $i++) {
 5407:         my $rule = $rulenames[$i];
 5408:         my ($checked,$extra);
 5409:         if ($rulenames[$i] eq 'default') {
 5410:             $rule = '';
 5411:         }
 5412:         if ($rulenames[$i] eq 'specific') {
 5413:             if (ref($servers) eq 'HASH') {
 5414:                 my $default;
 5415:                 if (($current ne '') && (exists($servers->{$current}))) {
 5416:                     $checked = ' checked="checked"';
 5417:                 }
 5418:                 unless ($checked) {
 5419:                     $default = ' selected="selected"';
 5420:                 }
 5421:                 $extra =
 5422:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5423:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5424:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 5425:                     '<option value=""'.$default.'></option>'."\n";
 5426:                 foreach my $server (sort(keys(%{$servers}))) {
 5427:                     if (ref($currbalancer) eq 'HASH') {
 5428:                         next if (exists($currbalancer->{$server}));
 5429:                     }
 5430:                     my $selected;
 5431:                     if ($server eq $current) {
 5432:                         $selected = ' selected="selected"';
 5433:                     }
 5434:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 5435:                 }
 5436:                 $extra .= '</select>';
 5437:             }
 5438:         } elsif ($rule eq $current) {
 5439:             $checked = ' checked="checked"';
 5440:         }
 5441:         $output .= '<span class="LC_nobreak"><label>'.
 5442:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 5443:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 5444:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 5445:                    ')"'.$checked.' />&nbsp;';
 5446:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 5447:             $output .= $ruletitles{'particular'};
 5448:         } else {
 5449:             $output .= $ruletitles{$rulenames[$i]};
 5450:         }
 5451:         $output .= '</label>'.$extra.'</span><br />'."\n";
 5452:     }
 5453:     $output .= '</div></td></tr>'."\n";
 5454:     return $output;
 5455: }
 5456: 
 5457: sub offloadtype_text {
 5458:     my %ruletitles = &Apache::lonlocal::texthash (
 5459:            'default'          => 'Offloads to default destinations',
 5460:            'homeserver'       => "Offloads to user's home server",
 5461:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 5462:            'specific'         => 'Offloads to specific server',
 5463:            'none'             => 'No offload',
 5464:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 5465:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 5466:            'particular'       => 'Session hosted (after re-auth) on server:',
 5467:     );
 5468:     return %ruletitles;
 5469: }
 5470: 
 5471: sub sparestype_titles {
 5472:     my %typestitles = &Apache::lonlocal::texthash (
 5473:                           'primary' => 'primary',
 5474:                           'default' => 'default',
 5475:                       );
 5476:     return %typestitles;
 5477: }
 5478: 
 5479: sub contact_titles {
 5480:     my %titles = &Apache::lonlocal::texthash (
 5481:                    'supportemail'    => 'Support E-mail address',
 5482:                    'adminemail'      => 'Default Server Admin E-mail address',
 5483:                    'errormail'       => 'Error reports to be e-mailed to',
 5484:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 5485:                    'helpdeskmail'    => "Helpdesk requests for this domain's users",
 5486:                    'otherdomsmail'   => 'Helpdesk requests for other (unconfigured) domains',
 5487:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 5488:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 5489:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 5490:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 5491:                  );
 5492:     my %short_titles = &Apache::lonlocal::texthash (
 5493:                            adminemail   => 'Admin E-mail address',
 5494:                            supportemail => 'Support E-mail',
 5495:                        );   
 5496:     return (\%titles,\%short_titles);
 5497: }
 5498: 
 5499: sub helpform_fields {
 5500:     my %titles =  &Apache::lonlocal::texthash (
 5501:                        'username'   => 'Name',
 5502:                        'user'       => 'Username/domain',
 5503:                        'phone'      => 'Phone',
 5504:                        'cc'         => 'Cc e-mail',
 5505:                        'course'     => 'Course Details',
 5506:                        'section'    => 'Sections',
 5507:                        'screenshot' => 'File upload',
 5508:     );
 5509:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 5510:     my %possoptions = (
 5511:                         username     => ['yes','no','req'],
 5512:                         phone        => ['yes','no','req'],
 5513:                         user         => ['yes','no'],
 5514:                         cc           => ['yes','no'],
 5515:                         course       => ['yes','no'],
 5516:                         section      => ['yes','no'],
 5517:                         screenshot   => ['yes','no'],
 5518:                       );
 5519:     my %fieldoptions = &Apache::lonlocal::texthash (
 5520:                          'yes'  => 'Optional',
 5521:                          'req'  => 'Required',
 5522:                          'no'   => "Not shown",
 5523:     );
 5524:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 5525: }
 5526: 
 5527: sub tool_titles {
 5528:     my %titles = &Apache::lonlocal::texthash (
 5529:                      aboutme    => 'Personal web page',
 5530:                      blog       => 'Blog',
 5531:                      webdav     => 'WebDAV',
 5532:                      portfolio  => 'Portfolio',
 5533:                      official   => 'Official courses (with institutional codes)',
 5534:                      unofficial => 'Unofficial courses',
 5535:                      community  => 'Communities',
 5536:                      textbook   => 'Textbook courses',
 5537:                      placement  => 'Placement tests',
 5538:                  );
 5539:     return %titles;
 5540: }
 5541: 
 5542: sub courserequest_titles {
 5543:     my %titles = &Apache::lonlocal::texthash (
 5544:                                    official   => 'Official',
 5545:                                    unofficial => 'Unofficial',
 5546:                                    community  => 'Communities',
 5547:                                    textbook   => 'Textbook',
 5548:                                    placement  => 'Placement tests',
 5549:                                    norequest  => 'Not allowed',
 5550:                                    approval   => 'Approval by Dom. Coord.',
 5551:                                    validate   => 'With validation',
 5552:                                    autolimit  => 'Numerical limit',
 5553:                                    unlimited  => '(blank for unlimited)',
 5554:                  );
 5555:     return %titles;
 5556: }
 5557: 
 5558: sub authorrequest_titles {
 5559:     my %titles = &Apache::lonlocal::texthash (
 5560:                                    norequest  => 'Not allowed',
 5561:                                    approval   => 'Approval by Dom. Coord.',
 5562:                                    automatic  => 'Automatic approval',
 5563:                  );
 5564:     return %titles;
 5565: }
 5566: 
 5567: sub courserequest_conditions {
 5568:     my %conditions = &Apache::lonlocal::texthash (
 5569:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 5570:        validate   => '(Processing of request subject to institutional validation).',
 5571:                  );
 5572:     return %conditions;
 5573: }
 5574: 
 5575: 
 5576: sub print_usercreation {
 5577:     my ($position,$dom,$settings,$rowtotal) = @_;
 5578:     my $numinrow = 4;
 5579:     my $datatable;
 5580:     if ($position eq 'top') {
 5581:         $$rowtotal ++;
 5582:         my $rowcount = 0;
 5583:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 5584:         if (ref($rules) eq 'HASH') {
 5585:             if (keys(%{$rules}) > 0) {
 5586:                 $datatable .= &user_formats_row('username',$settings,$rules,
 5587:                                                 $ruleorder,$numinrow,$rowcount);
 5588:                 $$rowtotal ++;
 5589:                 $rowcount ++;
 5590:             }
 5591:         }
 5592:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 5593:         if (ref($idrules) eq 'HASH') {
 5594:             if (keys(%{$idrules}) > 0) {
 5595:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 5596:                                                 $idruleorder,$numinrow,$rowcount);
 5597:                 $$rowtotal ++;
 5598:                 $rowcount ++;
 5599:             }
 5600:         }
 5601:         if ($rowcount == 0) {
 5602:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 5603:             $$rowtotal ++;
 5604:             $rowcount ++;
 5605:         }
 5606:     } elsif ($position eq 'middle') {
 5607:         my @creators = ('author','course','requestcrs');
 5608:         my ($rules,$ruleorder) =
 5609:             &Apache::lonnet::inst_userrules($dom,'username');
 5610:         my %lt = &usercreation_types();
 5611:         my %checked;
 5612:         if (ref($settings) eq 'HASH') {
 5613:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 5614:                 foreach my $item (@creators) {
 5615:                     $checked{$item} = $settings->{'cancreate'}{$item};
 5616:                 }
 5617:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 5618:                 foreach my $item (@creators) {
 5619:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 5620:                         $checked{$item} = 'none';
 5621:                     }
 5622:                 }
 5623:             }
 5624:         }
 5625:         my $rownum = 0;
 5626:         foreach my $item (@creators) {
 5627:             $rownum ++;
 5628:             if ($checked{$item} eq '') {
 5629:                 $checked{$item} = 'any';
 5630:             }
 5631:             my $css_class;
 5632:             if ($rownum%2) {
 5633:                 $css_class = '';
 5634:             } else {
 5635:                 $css_class = ' class="LC_odd_row" ';
 5636:             }
 5637:             $datatable .= '<tr'.$css_class.'>'.
 5638:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 5639:                          '</span></td><td align="right">';
 5640:             my @options = ('any');
 5641:             if (ref($rules) eq 'HASH') {
 5642:                 if (keys(%{$rules}) > 0) {
 5643:                     push(@options,('official','unofficial'));
 5644:                 }
 5645:             }
 5646:             push(@options,'none');
 5647:             foreach my $option (@options) {
 5648:                 my $type = 'radio';
 5649:                 my $check = ' ';
 5650:                 if ($checked{$item} eq $option) {
 5651:                     $check = ' checked="checked" ';
 5652:                 } 
 5653:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5654:                               '<input type="'.$type.'" name="can_createuser_'.
 5655:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 5656:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 5657:             }
 5658:             $datatable .= '</td></tr>';
 5659:         }
 5660:     } else {
 5661:         my @contexts = ('author','course','domain');
 5662:         my @authtypes = ('int','krb4','krb5','loc');
 5663:         my %checked;
 5664:         if (ref($settings) eq 'HASH') {
 5665:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 5666:                 foreach my $item (@contexts) {
 5667:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 5668:                         foreach my $auth (@authtypes) {
 5669:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 5670:                                 $checked{$item}{$auth} = ' checked="checked" ';
 5671:                             }
 5672:                         }
 5673:                     }
 5674:                 }
 5675:             }
 5676:         } else {
 5677:             foreach my $item (@contexts) {
 5678:                 foreach my $auth (@authtypes) {
 5679:                     $checked{$item}{$auth} = ' checked="checked" ';
 5680:                 }
 5681:             }
 5682:         }
 5683:         my %title = &context_names();
 5684:         my %authname = &authtype_names();
 5685:         my $rownum = 0;
 5686:         my $css_class; 
 5687:         foreach my $item (@contexts) {
 5688:             if ($rownum%2) {
 5689:                 $css_class = '';
 5690:             } else {
 5691:                 $css_class = ' class="LC_odd_row" ';
 5692:             }
 5693:             $datatable .=   '<tr'.$css_class.'>'.
 5694:                             '<td>'.$title{$item}.
 5695:                             '</td><td class="LC_left_item">'.
 5696:                             '<span class="LC_nobreak">';
 5697:             foreach my $auth (@authtypes) {
 5698:                 $datatable .= '<label>'. 
 5699:                               '<input type="checkbox" name="'.$item.'_auth" '.
 5700:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 5701:                               $authname{$auth}.'</label>&nbsp;';
 5702:             }
 5703:             $datatable .= '</span></td></tr>';
 5704:             $rownum ++;
 5705:         }
 5706:         $$rowtotal += $rownum;
 5707:     }
 5708:     return $datatable;
 5709: }
 5710: 
 5711: sub print_selfcreation {
 5712:     my ($position,$dom,$settings,$rowtotal) = @_;
 5713:     my (@selfcreate,$createsettings,$processing,$datatable);
 5714:     if (ref($settings) eq 'HASH') {
 5715:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 5716:             $createsettings = $settings->{'cancreate'};
 5717:             if (ref($createsettings) eq 'HASH') {
 5718:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 5719:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 5720:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 5721:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 5722:                         @selfcreate = ('email','login','sso');
 5723:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 5724:                         @selfcreate = ($createsettings->{'selfcreate'});
 5725:                     }
 5726:                 }
 5727:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 5728:                     $processing = $createsettings->{'selfcreateprocessing'};
 5729:                 }
 5730:             }
 5731:         }
 5732:     }
 5733:     my %radiohash;
 5734:     my $numinrow = 4;
 5735:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 5736:     if ($position eq 'top') {
 5737:         my %choices = &Apache::lonlocal::texthash (
 5738:                                                       cancreate_login      => 'Institutional Login',
 5739:                                                       cancreate_sso        => 'Institutional Single Sign On',
 5740:                                                   );
 5741:         my @toggles = sort(keys(%choices));
 5742:         my %defaultchecked = (
 5743:                                'cancreate_login' => 'off',
 5744:                                'cancreate_sso'   => 'off',
 5745:                              );
 5746:         my ($onclick,$itemcount);
 5747:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5748:                                                      \%choices,$itemcount,$onclick);
 5749:         $$rowtotal += $itemcount;
 5750:         
 5751:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5752: 
 5753:         if (ref($usertypes) eq 'HASH') {
 5754:             if (keys(%{$usertypes}) > 0) {
 5755:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 5756:                                              $dom,$numinrow,$othertitle,
 5757:                                              'statustocreate',$$rowtotal);
 5758:                 $$rowtotal ++;
 5759:             }
 5760:         }
 5761:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 5762:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5763:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 5764:         my $rem;
 5765:         my $numperrow = 2;
 5766:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 5767:         $datatable .= '<tr'.$css_class.'>'.
 5768:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 5769:                      '<td class="LC_left_item">'."\n".
 5770:                      '<table><tr><td>'."\n";
 5771:         for (my $i=0; $i<@fields; $i++) {
 5772:             $rem = $i%($numperrow);
 5773:             if ($rem == 0) {
 5774:                 if ($i > 0) {
 5775:                     $datatable .= '</tr>';
 5776:                 }
 5777:                 $datatable .= '<tr>';
 5778:             }
 5779:             my $currval;
 5780:             if (ref($createsettings) eq 'HASH') {
 5781:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 5782:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 5783:                 }
 5784:             }
 5785:             $datatable .= '<td class="LC_left_item">'.
 5786:                           '<span class="LC_nobreak">'.
 5787:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 5788:                           'value="'.$currval.'" size="10" />&nbsp;'.
 5789:                           $fieldtitles{$fields[$i]}.'</span></td>';
 5790:         }
 5791:         my $colsleft = $numperrow - $rem;
 5792:         if ($colsleft > 1 ) {
 5793:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5794:                          '&nbsp;</td>';
 5795:         } elsif ($colsleft == 1) {
 5796:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5797:         }
 5798:         $datatable .= '</tr></table></td></tr>';
 5799:         $$rowtotal ++;
 5800:     } elsif ($position eq 'middle') {
 5801:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 5802:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5803:         $usertypes->{'default'} = $othertitle;
 5804:         if (ref($types) eq 'ARRAY') {
 5805:             push(@{$types},'default');
 5806:             $usertypes->{'default'} = $othertitle;
 5807:             foreach my $status (@{$types}) {
 5808:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 5809:                                                        $numinrow,$$rowtotal,$usertypes);
 5810:                 $$rowtotal ++;
 5811:             }
 5812:         }
 5813:     } else {
 5814:         my %choices = &Apache::lonlocal::texthash (
 5815:                                                       cancreate_email => 'E-mail address as username',
 5816:                                                   );
 5817:         my @toggles = sort(keys(%choices));
 5818:         my %defaultchecked = (
 5819:                                'cancreate_email' => 'off',
 5820:                              );
 5821:         my $itemcount = 0;
 5822:         my $display = 'none';
 5823:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 5824:             $display = 'block';
 5825:         }
 5826:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 5827:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 5828:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5829:         my $usertypes = {};
 5830:         my $order = [];
 5831:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 5832:             $usertypes = $domdefaults{'inststatustypes'};
 5833:             $order = $domdefaults{'inststatusguest'};
 5834:         }
 5835:         if (ref($order) eq 'ARRAY') {
 5836:             push(@{$order},'default');
 5837:             if (@{$order} > 1) {
 5838:                 $usertypes->{'default'} = &mt('Other users');
 5839:                 $additional .= '<table><tr>';
 5840:                 foreach my $status (@{$order}) {
 5841:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 5842:                 }
 5843:                 $additional .= '</tr><tr>';
 5844:                 foreach my $status (@{$order}) {
 5845:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 5846:                 }
 5847:                 $additional .= '</tr></table>';
 5848:             } else {
 5849:                 $usertypes->{'default'} = &mt('All users');
 5850:                 $additional .= &email_as_username($rowtotal,$processing);
 5851:             }
 5852:         }
 5853:         $additional .= '</div>'."\n";
 5854: 
 5855:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5856:                                                      \%choices,$$rowtotal,$onclick,$additional);
 5857:         $$rowtotal ++;
 5858:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 5859:         $$rowtotal ++;
 5860:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 5861:         $numinrow = 1;
 5862:         if (ref($order) eq 'ARRAY') {
 5863:             foreach my $status (@{$order}) {
 5864:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 5865:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 5866:                 $$rowtotal ++;
 5867:             }
 5868:         }
 5869:         my ($emailrules,$emailruleorder) =
 5870:             &Apache::lonnet::inst_userrules($dom,'email');
 5871:         if (ref($emailrules) eq 'HASH') {
 5872:             if (keys(%{$emailrules}) > 0) {
 5873:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 5874:                                                 $emailruleorder,$numinrow,$$rowtotal);
 5875:                 $$rowtotal ++;
 5876:             }
 5877:         }
 5878:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 5879:     }
 5880:     return $datatable;
 5881: }
 5882: 
 5883: sub email_as_username {
 5884:     my ($rowtotal,$processing,$type) = @_;
 5885:     my %choices =
 5886:         &Apache::lonlocal::texthash (
 5887:                                       automatic => 'Automatic approval',
 5888:                                       approval  => 'Queued for approval',
 5889:                                     );
 5890:     my $output;
 5891:     foreach my $option ('automatic','approval') {
 5892:         my $checked;
 5893:         if (ref($processing) eq 'HASH') {
 5894:             if ($type eq '') {   
 5895:                 if (!exists($processing->{'default'})) {
 5896:                     if ($option eq 'automatic') {
 5897:                         $checked = ' checked="checked"';
 5898:                     }
 5899:                 } else {
 5900:                     if ($processing->{'default'} eq $option) {
 5901:                         $checked = ' checked="checked"';
 5902:                     }
 5903:                 }
 5904:             } else {
 5905:                 if (!exists($processing->{$type})) {
 5906:                     if ($option eq 'automatic') {
 5907:                         $checked = ' checked="checked"';
 5908:                     }
 5909:                 } else {
 5910:                     if ($processing->{$type} eq $option) {
 5911:                         $checked = ' checked="checked"';
 5912:                     }
 5913:                 }
 5914:             }
 5915:         } elsif ($option eq 'automatic') {
 5916:             $checked = ' checked="checked"'; 
 5917:         }
 5918:         my $name = 'cancreate_emailprocess';
 5919:         if (($type ne '') && ($type ne 'default')) {
 5920:             $name .= '_'.$type;
 5921:         }
 5922:         $output .= '<span class="LC_nobreak"><label>'.
 5923:                    '<input type="radio" name="'.$name.'"'.
 5924:                    $checked.' value="'.$option.'" />'.
 5925:                    $choices{$option}.'</label></span>';
 5926:         if ($type eq '') {
 5927:             $output .= '&nbsp;';
 5928:         } else {
 5929:             $output .= '<br />';
 5930:         }
 5931:     }
 5932:     $$rowtotal ++;
 5933:     return $output;
 5934: }
 5935: 
 5936: sub captcha_choice {
 5937:     my ($context,$settings,$itemcount) = @_;
 5938:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 5939:         $vertext,$currver);
 5940:     my %lt = &captcha_phrases();
 5941:     $keyentry = 'hidden';
 5942:     if ($context eq 'cancreate') {
 5943:         $rowname = &mt('CAPTCHA validation');
 5944:     } elsif ($context eq 'login') {
 5945:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 5946:     }
 5947:     if (ref($settings) eq 'HASH') {
 5948:         if ($settings->{'captcha'}) {
 5949:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 5950:         } else {
 5951:             $checked{'original'} = ' checked="checked"';
 5952:         }
 5953:         if ($settings->{'captcha'} eq 'recaptcha') {
 5954:             $pubtext = $lt{'pub'};
 5955:             $privtext = $lt{'priv'};
 5956:             $keyentry = 'text';
 5957:             $vertext = $lt{'ver'};
 5958:             $currver = $settings->{'recaptchaversion'};
 5959:             if ($currver ne '2') {
 5960:                 $currver = 1;
 5961:             }
 5962:         }
 5963:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 5964:             $currpub = $settings->{'recaptchakeys'}{'public'};
 5965:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 5966:         }
 5967:     } else {
 5968:         $checked{'original'} = ' checked="checked"';
 5969:     }
 5970:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5971:     my $output = '<tr'.$css_class.'>'.
 5972:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 5973:                  '<table><tr><td>'."\n";
 5974:     foreach my $option ('original','recaptcha','notused') {
 5975:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 5976:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 5977:                    $lt{$option}.'</label></span>';
 5978:         unless ($option eq 'notused') {
 5979:             $output .= ('&nbsp;'x2)."\n";
 5980:         }
 5981:     }
 5982: #
 5983: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 5984: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 5985: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 5986: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 5987: #
 5988:     $output .= '</td></tr>'."\n".
 5989:                '<tr><td>'."\n".
 5990:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 5991:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 5992:                $currpub.'" size="40" /></span><br />'."\n".
 5993:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 5994:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 5995:                $currpriv.'" size="40" /></span><br />'.
 5996:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 5997:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 5998:                $currver.'" size="3" /></span><br />'.
 5999:                '</td></tr></table>'."\n".
 6000:                '</td></tr>';
 6001:     return $output;
 6002: }
 6003: 
 6004: sub user_formats_row {
 6005:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 6006:     my $output;
 6007:     my %text = (
 6008:                    'username' => 'new usernames',
 6009:                    'id'       => 'IDs',
 6010:                    'email'    => 'self-created accounts (e-mail)',
 6011:                );
 6012:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 6013:     $output = '<tr '.$css_class.'>'.
 6014:               '<td><span class="LC_nobreak">';
 6015:     if ($type eq 'email') {
 6016:         $output .= &mt("Formats disallowed for $text{$type}: ");
 6017:     } else {
 6018:         $output .= &mt("Format rules to check for $text{$type}: ");
 6019:     }
 6020:     $output .= '</span></td>'.
 6021:                '<td class="LC_left_item" colspan="2"><table>';
 6022:     my $rem;
 6023:     if (ref($ruleorder) eq 'ARRAY') {
 6024:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 6025:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 6026:                 my $rem = $i%($numinrow);
 6027:                 if ($rem == 0) {
 6028:                     if ($i > 0) {
 6029:                         $output .= '</tr>';
 6030:                     }
 6031:                     $output .= '<tr>';
 6032:                 }
 6033:                 my $check = ' ';
 6034:                 if (ref($settings) eq 'HASH') {
 6035:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 6036:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 6037:                             $check = ' checked="checked" ';
 6038:                         }
 6039:                     }
 6040:                 }
 6041:                 $output .= '<td class="LC_left_item">'.
 6042:                            '<span class="LC_nobreak"><label>'.
 6043:                            '<input type="checkbox" name="'.$type.'_rule" '.
 6044:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 6045:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 6046:             }
 6047:         }
 6048:         $rem = @{$ruleorder}%($numinrow);
 6049:     }
 6050:     my $colsleft = $numinrow - $rem;
 6051:     if ($colsleft > 1 ) {
 6052:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6053:                    '&nbsp;</td>';
 6054:     } elsif ($colsleft == 1) {
 6055:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 6056:     }
 6057:     $output .= '</tr></table></td></tr>';
 6058:     return $output;
 6059: }
 6060: 
 6061: sub usercreation_types {
 6062:     my %lt = &Apache::lonlocal::texthash (
 6063:                     author     => 'When adding a co-author',
 6064:                     course     => 'When adding a user to a course',
 6065:                     requestcrs => 'When requesting a course',
 6066:                     any        => 'Any',
 6067:                     official   => 'Institutional only ',
 6068:                     unofficial => 'Non-institutional only',
 6069:                     none       => 'None',
 6070:     );
 6071:     return %lt;
 6072: }
 6073: 
 6074: sub selfcreation_types {
 6075:     my %lt = &Apache::lonlocal::texthash (
 6076:                     selfcreate => 'User creates own account',
 6077:                     any        => 'Any',
 6078:                     official   => 'Institutional only ',
 6079:                     unofficial => 'Non-institutional only',
 6080:                     email      => 'E-mail address',
 6081:                     login      => 'Institutional Login',
 6082:                     sso        => 'SSO',
 6083:              );
 6084: }
 6085: 
 6086: sub authtype_names {
 6087:     my %lt = &Apache::lonlocal::texthash(
 6088:                       int    => 'Internal',
 6089:                       krb4   => 'Kerberos 4',
 6090:                       krb5   => 'Kerberos 5',
 6091:                       loc    => 'Local',
 6092:                   );
 6093:     return %lt;
 6094: }
 6095: 
 6096: sub context_names {
 6097:     my %context_title = &Apache::lonlocal::texthash(
 6098:        author => 'Creating users when an Author',
 6099:        course => 'Creating users when in a course',
 6100:        domain => 'Creating users when a Domain Coordinator',
 6101:     );
 6102:     return %context_title;
 6103: }
 6104: 
 6105: sub print_usermodification {
 6106:     my ($position,$dom,$settings,$rowtotal) = @_;
 6107:     my $numinrow = 4;
 6108:     my ($context,$datatable,$rowcount);
 6109:     if ($position eq 'top') {
 6110:         $rowcount = 0;
 6111:         $context = 'author'; 
 6112:         foreach my $role ('ca','aa') {
 6113:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6114:                                                    $numinrow,$rowcount);
 6115:             $$rowtotal ++;
 6116:             $rowcount ++;
 6117:         }
 6118:     } elsif ($position eq 'bottom') {
 6119:         $context = 'course';
 6120:         $rowcount = 0;
 6121:         foreach my $role ('st','ep','ta','in','cr') {
 6122:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6123:                                                    $numinrow,$rowcount);
 6124:             $$rowtotal ++;
 6125:             $rowcount ++;
 6126:         }
 6127:     }
 6128:     return $datatable;
 6129: }
 6130: 
 6131: sub print_defaults {
 6132:     my ($position,$dom,$settings,$rowtotal) = @_;
 6133:     my $rownum = 0;
 6134:     my ($datatable,$css_class);
 6135:     if ($position eq 'top') {
 6136:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 6137:                      'datelocale_def','portal_def');
 6138:         my %defaults;
 6139:         if (ref($settings) eq 'HASH') {
 6140:             %defaults = %{$settings};
 6141:         } else {
 6142:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6143:             foreach my $item (@items) {
 6144:                 $defaults{$item} = $domdefaults{$item};
 6145:             }
 6146:         }
 6147:         my $titles = &defaults_titles($dom);
 6148:         foreach my $item (@items) {
 6149:             if ($rownum%2) {
 6150:                 $css_class = '';
 6151:             } else {
 6152:                 $css_class = ' class="LC_odd_row" ';
 6153:             }
 6154:             $datatable .= '<tr'.$css_class.'>'.
 6155:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6156:                           '</span></td><td class="LC_right_item" colspan="3">';
 6157:             if ($item eq 'auth_def') {
 6158:                 my @authtypes = ('internal','krb4','krb5','localauth');
 6159:                 my %shortauth = (
 6160:                                  internal => 'int',
 6161:                                  krb4 => 'krb4',
 6162:                                  krb5 => 'krb5',
 6163:                                  localauth  => 'loc'
 6164:                                 );
 6165:                 my %authnames = &authtype_names();
 6166:                 foreach my $auth (@authtypes) {
 6167:                     my $checked = ' ';
 6168:                     if ($defaults{$item} eq $auth) {
 6169:                         $checked = ' checked="checked" ';
 6170:                     }
 6171:                     $datatable .= '<label><input type="radio" name="'.$item.
 6172:                                   '" value="'.$auth.'"'.$checked.'/>'.
 6173:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 6174:                 }
 6175:             } elsif ($item eq 'timezone_def') {
 6176:                 my $includeempty = 1;
 6177:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 6178:             } elsif ($item eq 'datelocale_def') {
 6179:                 my $includeempty = 1;
 6180:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 6181:             } elsif ($item eq 'lang_def') {
 6182:                 my $includeempty = 1;
 6183:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 6184:             } else {
 6185:                 my $size;
 6186:                 if ($item eq 'portal_def') {
 6187:                     $size = ' size="25"';
 6188:                 }
 6189:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6190:                               $defaults{$item}.'"'.$size.' />';
 6191:             }
 6192:             $datatable .= '</td></tr>';
 6193:             $rownum ++;
 6194:         }
 6195:     } else {
 6196:         my (%defaults);
 6197:         if (ref($settings) eq 'HASH') {
 6198:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 6199:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 6200:                 my $maxnum = @{$settings->{'inststatusorder'}};
 6201:                 for (my $i=0; $i<$maxnum; $i++) {
 6202:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 6203:                     my $item = $settings->{'inststatusorder'}->[$i];
 6204:                     my $title = $settings->{'inststatustypes'}->{$item};
 6205:                     my $guestok;
 6206:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 6207:                         $guestok = 1;
 6208:                     }
 6209:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 6210:                     $datatable .= '<tr'.$css_class.'>'.
 6211:                                   '<td><span class="LC_nobreak">'.
 6212:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 6213:                     for (my $k=0; $k<=$maxnum; $k++) {
 6214:                         my $vpos = $k+1;
 6215:                         my $selstr;
 6216:                         if ($k == $i) {
 6217:                             $selstr = ' selected="selected" ';
 6218:                         }
 6219:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6220:                     }
 6221:                     my ($checkedon,$checkedoff);
 6222:                     $checkedoff = ' checked="checked"';
 6223:                     if ($guestok) {
 6224:                         $checkedon = $checkedoff;
 6225:                         $checkedoff = ''; 
 6226:                     }
 6227:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 6228:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 6229:                                   &mt('delete').'</span></td>'.
 6230:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 6231:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 6232:                                   '</span></td>'.
 6233:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6234:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 6235:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 6236:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 6237:                                   &mt('No').'</label></span></td></tr>';
 6238:                 }
 6239:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 6240:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 6241:                 $datatable .= '<tr '.$css_class.'>'.
 6242:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 6243:                 for (my $k=0; $k<=$maxnum; $k++) {
 6244:                     my $vpos = $k+1;
 6245:                     my $selstr;
 6246:                     if ($k == $maxnum) {
 6247:                         $selstr = ' selected="selected" ';
 6248:                     }
 6249:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6250:                 }
 6251:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 6252:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 6253:                               '&nbsp;'.&mt('(new)').
 6254:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 6255:                               &mt('Name displayed:').
 6256:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 6257:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 6258:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 6259:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 6260:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 6261:                               &mt('No').'</label></span></td></tr>';
 6262:                               '</tr>'."\n";
 6263:                 $rownum ++;
 6264:             }
 6265:         }
 6266:     }
 6267:     $$rowtotal += $rownum;
 6268:     return $datatable;
 6269: }
 6270: 
 6271: sub get_languages_hash {
 6272:     my %langchoices;
 6273:     foreach my $id (&Apache::loncommon::languageids()) {
 6274:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 6275:         if ($code ne '') {
 6276:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 6277:         }
 6278:     }
 6279:     return %langchoices;
 6280: }
 6281: 
 6282: sub defaults_titles {
 6283:     my ($dom) = @_;
 6284:     my %titles = &Apache::lonlocal::texthash (
 6285:                    'auth_def'      => 'Default authentication type',
 6286:                    'auth_arg_def'  => 'Default authentication argument',
 6287:                    'lang_def'      => 'Default language',
 6288:                    'timezone_def'  => 'Default timezone',
 6289:                    'datelocale_def' => 'Default locale for dates',
 6290:                    'portal_def'     => 'Portal/Default URL',
 6291:                  );
 6292:     if ($dom) {
 6293:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 6294:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 6295:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 6296:         $protocol = 'http' if ($protocol ne 'https');
 6297:         if ($uint_dom) {
 6298:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 6299:                                          $uint_dom);
 6300:         }
 6301:     }
 6302:     return (\%titles);
 6303: }
 6304: 
 6305: sub print_scantronformat {
 6306:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 6307:     my $itemcount = 1;
 6308:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 6309:         %confhash);
 6310:     my $switchserver = &check_switchserver($dom,$confname);
 6311:     my %lt = &Apache::lonlocal::texthash (
 6312:                 default => 'Default bubblesheet format file error',
 6313:                 custom  => 'Custom bubblesheet format file error',
 6314:              );
 6315:     my %scantronfiles = (
 6316:         default => 'default.tab',
 6317:         custom => 'custom.tab',
 6318:     );
 6319:     foreach my $key (keys(%scantronfiles)) {
 6320:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 6321:                               .$scantronfiles{$key};
 6322:     }
 6323:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 6324:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 6325:         if (!$switchserver) {
 6326:             my $servadm = $r->dir_config('lonAdmEMail');
 6327:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 6328:             if ($configuserok eq 'ok') {
 6329:                 if ($author_ok eq 'ok') {
 6330:                     my %legacyfile = (
 6331:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 6332:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 6333:                     );
 6334:                     my %md5chk;
 6335:                     foreach my $type (keys(%legacyfile)) {
 6336:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 6337:                         chomp($md5chk{$type});
 6338:                     }
 6339:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 6340:                         foreach my $type (keys(%legacyfile)) {
 6341:                             ($scantronurls{$type},my $error) = 
 6342:                                 &legacy_scantronformat($r,$dom,$confname,
 6343:                                                  $type,$legacyfile{$type},
 6344:                                                  $scantronurls{$type},
 6345:                                                  $scantronfiles{$type});
 6346:                             if ($error ne '') {
 6347:                                 $error{$type} = $error;
 6348:                             }
 6349:                         }
 6350:                         if (keys(%error) == 0) {
 6351:                             $is_custom = 1;
 6352:                             $confhash{'scantron'}{'scantronformat'} = 
 6353:                                 $scantronurls{'custom'};
 6354:                             my $putresult = 
 6355:                                 &Apache::lonnet::put_dom('configuration',
 6356:                                                          \%confhash,$dom);
 6357:                             if ($putresult ne 'ok') {
 6358:                                 $error{'custom'} = 
 6359:                                     '<span class="LC_error">'.
 6360:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6361:                             }
 6362:                         }
 6363:                     } else {
 6364:                         ($scantronurls{'default'},my $error) =
 6365:                             &legacy_scantronformat($r,$dom,$confname,
 6366:                                           'default',$legacyfile{'default'},
 6367:                                           $scantronurls{'default'},
 6368:                                           $scantronfiles{'default'});
 6369:                         if ($error eq '') {
 6370:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 6371:                             my $putresult =
 6372:                                 &Apache::lonnet::put_dom('configuration',
 6373:                                                          \%confhash,$dom);
 6374:                             if ($putresult ne 'ok') {
 6375:                                 $error{'default'} =
 6376:                                     '<span class="LC_error">'.
 6377:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6378:                             }
 6379:                         } else {
 6380:                             $error{'default'} = $error;
 6381:                         }
 6382:                     }
 6383:                 }
 6384:             }
 6385:         } else {
 6386:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 6387:         }
 6388:     }
 6389:     if (ref($settings) eq 'HASH') {
 6390:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 6391:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 6392:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 6393:                 $scantronurl = '';
 6394:             } else {
 6395:                 $scantronurl = $settings->{'scantronformat'};
 6396:             }
 6397:             $is_custom = 1;
 6398:         } else {
 6399:             $scantronurl = $scantronurls{'default'};
 6400:         }
 6401:     } else {
 6402:         if ($is_custom) {
 6403:             $scantronurl = $scantronurls{'custom'};
 6404:         } else {
 6405:             $scantronurl = $scantronurls{'default'};
 6406:         }
 6407:     }
 6408:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6409:     $datatable .= '<tr'.$css_class.'>';
 6410:     if (!$is_custom) {
 6411:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 6412:                       '<span class="LC_nobreak">';
 6413:         if ($scantronurl) {
 6414:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 6415:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 6416:         } else {
 6417:             $datatable = &mt('File unavailable for display');
 6418:         }
 6419:         $datatable .= '</span></td>';
 6420:         if (keys(%error) == 0) { 
 6421:             $datatable .= '<td valign="bottom">';
 6422:             if (!$switchserver) {
 6423:                 $datatable .= &mt('Upload:').'<br />';
 6424:             }
 6425:         } else {
 6426:             my $errorstr;
 6427:             foreach my $key (sort(keys(%error))) {
 6428:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6429:             }
 6430:             $datatable .= '<td>'.$errorstr;
 6431:         }
 6432:     } else {
 6433:         if (keys(%error) > 0) {
 6434:             my $errorstr;
 6435:             foreach my $key (sort(keys(%error))) {
 6436:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6437:             } 
 6438:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 6439:         } elsif ($scantronurl) {
 6440:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 6441:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 6442:             $datatable .= '<td><span class="LC_nobreak">'.
 6443:                           $link.
 6444:                           '<label><input type="checkbox" name="scantronformat_del"'.
 6445:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 6446:                           '<td><span class="LC_nobreak">&nbsp;'.
 6447:                           &mt('Replace:').'</span><br />';
 6448:         }
 6449:     }
 6450:     if (keys(%error) == 0) {
 6451:         if ($switchserver) {
 6452:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 6453:         } else {
 6454:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 6455:                          '<input type="file" name="scantronformat" /></span>';
 6456:         }
 6457:     }
 6458:     $datatable .= '</td></tr>';
 6459:     $$rowtotal ++;
 6460:     return $datatable;
 6461: }
 6462: 
 6463: sub legacy_scantronformat {
 6464:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 6465:     my ($url,$error);
 6466:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 6467:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 6468:         (my $result,$url) =
 6469:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 6470:                          '','',$newfile);
 6471:         if ($result ne 'ok') {
 6472:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 6473:         }
 6474:     }
 6475:     return ($url,$error);
 6476: }
 6477: 
 6478: sub print_coursecategories {
 6479:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 6480:     my $datatable;
 6481:     if ($position eq 'top') {
 6482:         my (%checked);
 6483:         my @catitems = ('unauth','auth');
 6484:         my @cattypes = ('std','domonly','codesrch','none');
 6485:         $checked{'unauth'} = 'std';
 6486:         $checked{'auth'} = 'std';
 6487:         if (ref($settings) eq 'HASH') {
 6488:             foreach my $type (@cattypes) {
 6489:                 if ($type eq $settings->{'unauth'}) {
 6490:                     $checked{'unauth'} = $type;
 6491:                 }
 6492:                 if ($type eq $settings->{'auth'}) {
 6493:                     $checked{'auth'} = $type;
 6494:                 }
 6495:             }
 6496:         }
 6497:         my %lt = &Apache::lonlocal::texthash (
 6498:                                                unauth   => 'Catalog type for unauthenticated users',
 6499:                                                auth     => 'Catalog type for authenticated users',
 6500:                                                none     => 'No catalog',
 6501:                                                std      => 'Standard catalog',
 6502:                                                domonly  => 'Domain-only catalog',
 6503:                                                codesrch => "Code search form",
 6504:                                              );
 6505:        my $itemcount = 0;
 6506:        foreach my $item (@catitems) {
 6507:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 6508:            $datatable .= '<tr '.$css_class.'>'.
 6509:                          '<td>'.$lt{$item}.'</td>'.
 6510:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 6511:            foreach my $type (@cattypes) {
 6512:                my $ischecked;
 6513:                if ($checked{$item} eq $type) {
 6514:                    $ischecked=' checked="checked"';
 6515:                }
 6516:                $datatable .= '<label>'.
 6517:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 6518:                              ' />'.$lt{$type}.'</label>&nbsp;';
 6519:            }
 6520:            $datatable .= '</td></tr>';
 6521:            $itemcount ++;
 6522:         }
 6523:         $$rowtotal += $itemcount;
 6524:     } elsif ($position eq 'middle') {
 6525:         my $toggle_cats_crs = ' ';
 6526:         my $toggle_cats_dom = ' checked="checked" ';
 6527:         my $can_cat_crs = ' ';
 6528:         my $can_cat_dom = ' checked="checked" ';
 6529:         my $toggle_catscomm_comm = ' ';
 6530:         my $toggle_catscomm_dom = ' checked="checked" ';
 6531:         my $can_catcomm_comm = ' ';
 6532:         my $can_catcomm_dom = ' checked="checked" ';
 6533:         my $toggle_catsplace_place = ' ';
 6534:         my $toggle_catsplace_dom = ' checked="checked" ';
 6535:         my $can_catplace_place = ' ';
 6536:         my $can_catplace_dom = ' checked="checked" ';
 6537: 
 6538:         if (ref($settings) eq 'HASH') {
 6539:             if ($settings->{'togglecats'} eq 'crs') {
 6540:                 $toggle_cats_crs = $toggle_cats_dom;
 6541:                 $toggle_cats_dom = ' ';
 6542:             }
 6543:             if ($settings->{'categorize'} eq 'crs') {
 6544:                 $can_cat_crs = $can_cat_dom;
 6545:                 $can_cat_dom = ' ';
 6546:             }
 6547:             if ($settings->{'togglecatscomm'} eq 'comm') {
 6548:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 6549:                 $toggle_catscomm_dom = ' ';
 6550:             }
 6551:             if ($settings->{'categorizecomm'} eq 'comm') {
 6552:                 $can_catcomm_comm = $can_catcomm_dom;
 6553:                 $can_catcomm_dom = ' ';
 6554:             }
 6555:             if ($settings->{'togglecatsplace'} eq 'place') {
 6556:                 $toggle_catsplace_place = $toggle_catsplace_dom;
 6557:                 $toggle_catsplace_dom = ' ';
 6558:             }
 6559:             if ($settings->{'categorizeplace'} eq 'place') {
 6560:                 $can_catplace_place = $can_catplace_dom;
 6561:                 $can_catplace_dom = ' ';
 6562:             }
 6563:         }
 6564:         my %title = &Apache::lonlocal::texthash (
 6565:                      togglecats      => 'Show/Hide a course in catalog',
 6566:                      togglecatscomm  => 'Show/Hide a community in catalog',
 6567:                      togglecatsplace => 'Show/Hide a placement test in catalog',
 6568:                      categorize      => 'Assign a category to a course',
 6569:                      categorizecomm  => 'Assign a category to a community',
 6570:                      categorizeplace => 'Assign a category to a placement test',
 6571:                     );
 6572:         my %level = &Apache::lonlocal::texthash (
 6573:                      dom   => 'Set in Domain',
 6574:                      crs   => 'Set in Course',
 6575:                      comm  => 'Set in Community',
 6576:                      place => 'Set in Placement Test',
 6577:                     );
 6578:         $datatable = '<tr class="LC_odd_row">'.
 6579:                   '<td>'.$title{'togglecats'}.'</td>'.
 6580:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6581:                   '<input type="radio" name="togglecats"'.
 6582:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6583:                   '<label><input type="radio" name="togglecats"'.
 6584:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6585:                   '</tr><tr>'.
 6586:                   '<td>'.$title{'categorize'}.'</td>'.
 6587:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6588:                   '<label><input type="radio" name="categorize"'.
 6589:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6590:                   '<label><input type="radio" name="categorize"'.
 6591:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6592:                   '</tr><tr class="LC_odd_row">'.
 6593:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 6594:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6595:                   '<input type="radio" name="togglecatscomm"'.
 6596:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6597:                   '<label><input type="radio" name="togglecatscomm"'.
 6598:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6599:                   '</tr><tr>'.
 6600:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 6601:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6602:                   '<label><input type="radio" name="categorizecomm"'.
 6603:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6604:                   '<label><input type="radio" name="categorizecomm"'.
 6605:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6606:                   '</tr><tr>'.
 6607:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
 6608:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6609:                   '<input type="radio" name="togglecatsplace"'.
 6610:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6611:                   '<label><input type="radio" name="togglecatscomm"'.
 6612:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
 6613:                   '</tr><tr>'.
 6614:                   '<td>'.$title{'categorizeplace'}.'</td>'.
 6615:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6616:                   '<label><input type="radio" name="categorizeplace"'.
 6617:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6618:                   '<label><input type="radio" name="categorizeplace"'.
 6619:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
 6620:                   '</tr>';
 6621:         $$rowtotal += 6;
 6622:     } else {
 6623:         my $css_class;
 6624:         my $itemcount = 1;
 6625:         my $cathash; 
 6626:         if (ref($settings) eq 'HASH') {
 6627:             $cathash = $settings->{'cats'};
 6628:         }
 6629:         if (ref($cathash) eq 'HASH') {
 6630:             my (@cats,@trails,%allitems,%idx,@jsarray);
 6631:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 6632:                                                    \%allitems,\%idx,\@jsarray);
 6633:             my $maxdepth = scalar(@cats);
 6634:             my $colattrib = '';
 6635:             if ($maxdepth > 2) {
 6636:                 $colattrib = ' colspan="2" ';
 6637:             }
 6638:             my @path;
 6639:             if (@cats > 0) {
 6640:                 if (ref($cats[0]) eq 'ARRAY') {
 6641:                     my $numtop = @{$cats[0]};
 6642:                     my $maxnum = $numtop;
 6643:                     my %default_names = (
 6644:                           instcode    => &mt('Official courses'),
 6645:                           communities => &mt('Communities'),
 6646:                           placement   => &mt('Placement Tests'),
 6647:                     );
 6648: 
 6649:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 6650:                         ($cathash->{'instcode::0'} eq '') ||
 6651:                         (!grep(/^communities$/,@{$cats[0]})) || 
 6652:                         ($cathash->{'communities::0'} eq '') ||
 6653:                         (!grep(/^placement$/,@{$cats[0]})) ||
 6654:                         ($cathash->{'placement::0'} eq '')) {
 6655:                         $maxnum ++;
 6656:                     }
 6657:                     my $lastidx;
 6658:                     for (my $i=0; $i<$numtop; $i++) {
 6659:                         my $parent = $cats[0][$i];
 6660:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6661:                         my $item = &escape($parent).'::0';
 6662:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 6663:                         $lastidx = $idx{$item};
 6664:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6665:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 6666:                         for (my $k=0; $k<=$maxnum; $k++) {
 6667:                             my $vpos = $k+1;
 6668:                             my $selstr;
 6669:                             if ($k == $i) {
 6670:                                 $selstr = ' selected="selected" ';
 6671:                             }
 6672:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6673:                         }
 6674:                         $datatable .= '</select></span></td><td>';
 6675:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
 6676:                             $datatable .=  '<span class="LC_nobreak">'
 6677:                                            .$default_names{$parent}.'</span>';
 6678:                             if ($parent eq 'instcode') {
 6679:                                 $datatable .= '<br /><span class="LC_nobreak">('
 6680:                                               .&mt('with institutional codes')
 6681:                                               .')</span></td><td'.$colattrib.'>';
 6682:                             } else {
 6683:                                 $datatable .= '<table><tr><td>';
 6684:                             }
 6685:                             $datatable .= '<span class="LC_nobreak">'
 6686:                                           .'<label><input type="radio" name="'
 6687:                                           .$parent.'" value="1" checked="checked" />'
 6688:                                           .&mt('Display').'</label>';
 6689:                             if ($parent eq 'instcode') {
 6690:                                 $datatable .= '&nbsp;';
 6691:                             } else {
 6692:                                 $datatable .= '</span></td></tr><tr><td>'
 6693:                                               .'<span class="LC_nobreak">';
 6694:                             }
 6695:                             $datatable .= '<label><input type="radio" name="'
 6696:                                           .$parent.'" value="0" />'
 6697:                                           .&mt('Do not display').'</label></span>';
 6698:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
 6699:                                 $datatable .= '</td></tr></table>';
 6700:                             }
 6701:                             $datatable .= '</td>';
 6702:                         } else {
 6703:                             $datatable .= $parent
 6704:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 6705:                                           .'<input type="checkbox" name="deletecategory" '
 6706:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 6707:                         }
 6708:                         my $depth = 1;
 6709:                         push(@path,$parent);
 6710:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 6711:                         pop(@path);
 6712:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 6713:                         $itemcount ++;
 6714:                     }
 6715:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6716:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 6717:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 6718:                     for (my $k=0; $k<=$maxnum; $k++) {
 6719:                         my $vpos = $k+1;
 6720:                         my $selstr;
 6721:                         if ($k == $numtop) {
 6722:                             $selstr = ' selected="selected" ';
 6723:                         }
 6724:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6725:                     }
 6726:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 6727:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 6728:                                   .'</tr>'."\n";
 6729:                     $itemcount ++;
 6730:                     foreach my $default ('instcode','communities','placement') {
 6731:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 6732:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6733:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 6734:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 6735:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 6736:                             for (my $k=0; $k<=$maxnum; $k++) {
 6737:                                 my $vpos = $k+1;
 6738:                                 my $selstr;
 6739:                                 if ($k == $maxnum) {
 6740:                                     $selstr = ' selected="selected" ';
 6741:                                 }
 6742:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6743:                             }
 6744:                             $datatable .= '</select></span></td>'.
 6745:                                           '<td><span class="LC_nobreak">'.
 6746:                                           $default_names{$default}.'</span>';
 6747:                             if ($default eq 'instcode') {
 6748:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 6749:                                               .&mt('with institutional codes').')</span>';
 6750:                             }
 6751:                             $datatable .= '</td>'
 6752:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 6753:                                           .&mt('Display').'</label>&nbsp;'
 6754:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 6755:                                           .&mt('Do not display').'</label></span></td></tr>';
 6756:                         }
 6757:                     }
 6758:                 }
 6759:             } else {
 6760:                 $datatable .= &initialize_categories($itemcount);
 6761:             }
 6762:         } else {
 6763:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 6764:                           .&initialize_categories($itemcount);
 6765:         }
 6766:         $$rowtotal += $itemcount;
 6767:     }
 6768:     return $datatable;
 6769: }
 6770: 
 6771: sub print_serverstatuses {
 6772:     my ($dom,$settings,$rowtotal) = @_;
 6773:     my $datatable;
 6774:     my @pages = &serverstatus_pages();
 6775:     my (%namedaccess,%machineaccess);
 6776:     foreach my $type (@pages) {
 6777:         $namedaccess{$type} = '';
 6778:         $machineaccess{$type}= '';
 6779:     }
 6780:     if (ref($settings) eq 'HASH') {
 6781:         foreach my $type (@pages) {
 6782:             if (exists($settings->{$type})) {
 6783:                 if (ref($settings->{$type}) eq 'HASH') {
 6784:                     foreach my $key (keys(%{$settings->{$type}})) {
 6785:                         if ($key eq 'namedusers') {
 6786:                             $namedaccess{$type} = $settings->{$type}->{$key};
 6787:                         } elsif ($key eq 'machines') {
 6788:                             $machineaccess{$type} = $settings->{$type}->{$key};
 6789:                         }
 6790:                     }
 6791:                 }
 6792:             }
 6793:         }
 6794:     }
 6795:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 6796:     my $rownum = 0;
 6797:     my $css_class;
 6798:     foreach my $type (@pages) {
 6799:         $rownum ++;
 6800:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 6801:         $datatable .= '<tr'.$css_class.'>'.
 6802:                       '<td><span class="LC_nobreak">'.
 6803:                       $titles->{$type}.'</span></td>'.
 6804:                       '<td class="LC_left_item">'.
 6805:                       '<input type="text" name="'.$type.'_namedusers" '.
 6806:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 6807:                       '<td class="LC_right_item">'.
 6808:                       '<span class="LC_nobreak">'.
 6809:                       '<input type="text" name="'.$type.'_machines" '.
 6810:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 6811:                       '</td></tr>'."\n";
 6812:     }
 6813:     $$rowtotal += $rownum;
 6814:     return $datatable;
 6815: }
 6816: 
 6817: sub serverstatus_pages {
 6818:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 6819:             'checksums','clusterstatus','certstatus','metadata_keywords',
 6820:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
 6821:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
 6822: }
 6823: 
 6824: sub defaults_javascript {
 6825:     my ($settings) = @_;
 6826:     return unless (ref($settings) eq 'HASH');
 6827:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 6828:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 6829:         if ($maxnum eq '') {
 6830:             $maxnum = 0;
 6831:         }
 6832:         $maxnum ++;
 6833:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 6834:         return <<"ENDSCRIPT";
 6835: <script type="text/javascript">
 6836: // <![CDATA[
 6837: function reorderTypes(form,caller) {
 6838:     var changedVal;
 6839: $jstext 
 6840:     var newpos = 'addinststatus_pos';
 6841:     var current = new Array;
 6842:     var maxh = $maxnum;
 6843:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6844:     var oldVal;
 6845:     if (caller == newpos) {
 6846:         changedVal = newitemVal;
 6847:     } else {
 6848:         var curritem = 'inststatus_pos_'+caller;
 6849:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 6850:         current[newitemVal] = newpos;
 6851:     }
 6852:     for (var i=0; i<inststatuses.length; i++) {
 6853:         if (inststatuses[i] != caller) {
 6854:             var elementName = 'inststatus_pos_'+inststatuses[i];
 6855:             if (form.elements[elementName]) {
 6856:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 6857:                 current[currVal] = elementName;
 6858:             }
 6859:         }
 6860:     }
 6861:     for (var j=0; j<maxh; j++) {
 6862:         if (current[j] == undefined) {
 6863:             oldVal = j;
 6864:         }
 6865:     }
 6866:     if (oldVal < changedVal) {
 6867:         for (var k=oldVal+1; k<=changedVal ; k++) {
 6868:            var elementName = current[k];
 6869:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 6870:         }
 6871:     } else {
 6872:         for (var k=changedVal; k<oldVal; k++) {
 6873:             var elementName = current[k];
 6874:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 6875:         }
 6876:     }
 6877:     return;
 6878: }
 6879: 
 6880: // ]]>
 6881: </script>
 6882: 
 6883: ENDSCRIPT
 6884:     }
 6885: }
 6886: 
 6887: sub coursecategories_javascript {
 6888:     my ($settings) = @_;
 6889:     my ($output,$jstext,$cathash);
 6890:     if (ref($settings) eq 'HASH') {
 6891:         $cathash = $settings->{'cats'};
 6892:     }
 6893:     if (ref($cathash) eq 'HASH') {
 6894:         my (@cats,@jsarray,%idx);
 6895:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 6896:         if (@jsarray > 0) {
 6897:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 6898:             for (my $i=0; $i<@jsarray; $i++) {
 6899:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 6900:                     my $catstr = join('","',@{$jsarray[$i]});
 6901:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 6902:                 }
 6903:             }
 6904:         }
 6905:     } else {
 6906:         $jstext  = '    var categories = Array(1);'."\n".
 6907:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 6908:     }
 6909:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 6910:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 6911:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
 6912:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 6913:     &js_escape(\$instcode_reserved);
 6914:     &js_escape(\$communities_reserved);
 6915:     &js_escape(\$placement_reserved);
 6916:     &js_escape(\$choose_again);
 6917:     $output = <<"ENDSCRIPT";
 6918: <script type="text/javascript">
 6919: // <![CDATA[
 6920: function reorderCats(form,parent,item,idx) {
 6921:     var changedVal;
 6922: $jstext
 6923:     var newpos = 'addcategory_pos';
 6924:     if (parent == '') {
 6925:         var has_instcode = 0;
 6926:         var maxtop = categories[idx].length;
 6927:         for (var j=0; j<maxtop; j++) {
 6928:             if (categories[idx][j] == 'instcode::0') {
 6929:                 has_instcode == 1;
 6930:             }
 6931:         }
 6932:         if (has_instcode == 0) {
 6933:             categories[idx][maxtop] = 'instcode_pos';
 6934:         }
 6935:     } else {
 6936:         newpos += '_'+parent;
 6937:     }
 6938:     var maxh = 1 + categories[idx].length;
 6939:     var current = new Array;
 6940:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6941:     if (item == newpos) {
 6942:         changedVal = newitemVal;
 6943:     } else {
 6944:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 6945:         current[newitemVal] = newpos;
 6946:     }
 6947:     for (var i=0; i<categories[idx].length; i++) {
 6948:         var elementName = categories[idx][i];
 6949:         if (elementName != item) {
 6950:             if (form.elements[elementName]) {
 6951:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 6952:                 current[currVal] = elementName;
 6953:             }
 6954:         }
 6955:     }
 6956:     var oldVal;
 6957:     for (var j=0; j<maxh; j++) {
 6958:         if (current[j] == undefined) {
 6959:             oldVal = j;
 6960:         }
 6961:     }
 6962:     if (oldVal < changedVal) {
 6963:         for (var k=oldVal+1; k<=changedVal ; k++) {
 6964:            var elementName = current[k];
 6965:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 6966:         }
 6967:     } else {
 6968:         for (var k=changedVal; k<oldVal; k++) {
 6969:             var elementName = current[k];
 6970:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 6971:         }
 6972:     }
 6973:     return;
 6974: }
 6975: 
 6976: function categoryCheck(form) {
 6977:     if (form.elements['addcategory_name'].value == 'instcode') {
 6978:         alert('$instcode_reserved\\n$choose_again');
 6979:         return false;
 6980:     }
 6981:     if (form.elements['addcategory_name'].value == 'communities') {
 6982:         alert('$communities_reserved\\n$choose_again');
 6983:         return false;
 6984:     }
 6985:     if (form.elements['addcategory_name'].value == 'placement') {
 6986:         alert('$placement_reserved\\n$choose_again');
 6987:         return false;
 6988:     }
 6989:     return true;
 6990: }
 6991: 
 6992: // ]]>
 6993: </script>
 6994: 
 6995: ENDSCRIPT
 6996:     return $output;
 6997: }
 6998: 
 6999: sub initialize_categories {
 7000:     my ($itemcount) = @_;
 7001:     my ($datatable,$css_class,$chgstr);
 7002:     my %default_names = (
 7003:                       instcode    => 'Official courses (with institutional codes)',
 7004:                       communities => 'Communities',
 7005:                       placement   => 'Placement Tests',
 7006:                         );
 7007:     my $select0 = ' selected="selected"';
 7008:     my $select1 = '';
 7009:     foreach my $default ('instcode','communities','placement') {
 7010:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7011:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 7012:         if (($default eq 'communities') || ($default eq 'placement')) {
 7013:             $select1 = $select0;
 7014:             $select0 = '';
 7015:         }
 7016:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7017:                      .'<select name="'.$default.'_pos">'
 7018:                      .'<option value="0"'.$select0.'>1</option>'
 7019:                      .'<option value="1"'.$select1.'>2</option>'
 7020:                      .'<option value="2">3</option></select>&nbsp;'
 7021:                      .$default_names{$default}
 7022:                      .'</span></td><td><span class="LC_nobreak">'
 7023:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 7024:                      .&mt('Display').'</label>&nbsp;<label>'
 7025:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 7026:                  .'</label></span></td></tr>';
 7027:         $itemcount ++;
 7028:     }
 7029:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7030:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 7031:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7032:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 7033:                   .'<option value="0">1</option>'
 7034:                   .'<option value="1">2</option>'
 7035:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 7036:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 7037:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 7038:     return $datatable;
 7039: }
 7040: 
 7041: sub build_category_rows {
 7042:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 7043:     my ($text,$name,$item,$chgstr);
 7044:     if (ref($cats) eq 'ARRAY') {
 7045:         my $maxdepth = scalar(@{$cats});
 7046:         if (ref($cats->[$depth]) eq 'HASH') {
 7047:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 7048:                 my $numchildren = @{$cats->[$depth]{$parent}};
 7049:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7050:                 $text .= '<td><table class="LC_data_table">';
 7051:                 my ($idxnum,$parent_name,$parent_item);
 7052:                 my $higher = $depth - 1;
 7053:                 if ($higher == 0) {
 7054:                     $parent_name = &escape($parent).'::'.$higher;
 7055:                 } else {
 7056:                     if (ref($path) eq 'ARRAY') {
 7057:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7058:                     }
 7059:                 }
 7060:                 $parent_item = 'addcategory_pos_'.$parent_name;
 7061:                 for (my $j=0; $j<=$numchildren; $j++) {
 7062:                     if ($j < $numchildren) {
 7063:                         $name = $cats->[$depth]{$parent}[$j];
 7064:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 7065:                         $idxnum = $idx->{$item};
 7066:                     } else {
 7067:                         $name = $parent_name;
 7068:                         $item = $parent_item;
 7069:                     }
 7070:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 7071:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 7072:                     for (my $i=0; $i<=$numchildren; $i++) {
 7073:                         my $vpos = $i+1;
 7074:                         my $selstr;
 7075:                         if ($j == $i) {
 7076:                             $selstr = ' selected="selected" ';
 7077:                         }
 7078:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 7079:                     }
 7080:                     $text .= '</select>&nbsp;';
 7081:                     if ($j < $numchildren) {
 7082:                         my $deeper = $depth+1;
 7083:                         $text .= $name.'&nbsp;'
 7084:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 7085:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 7086:                         if(ref($path) eq 'ARRAY') {
 7087:                             push(@{$path},$name);
 7088:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 7089:                             pop(@{$path});
 7090:                         }
 7091:                     } else {
 7092:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 7093:                         if ($j == $numchildren) {
 7094:                             $text .= $name;
 7095:                         } else {
 7096:                             $text .= $item;
 7097:                         }
 7098:                         $text .= '" value="" />';
 7099:                     }
 7100:                     $text .= '</td></tr>';
 7101:                 }
 7102:                 $text .= '</table></td>';
 7103:             } else {
 7104:                 my $higher = $depth-1;
 7105:                 if ($higher == 0) {
 7106:                     $name = &escape($parent).'::'.$higher;
 7107:                 } else {
 7108:                     if (ref($path) eq 'ARRAY') {
 7109:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7110:                     }
 7111:                 }
 7112:                 my $colspan;
 7113:                 if ($parent ne 'instcode') {
 7114:                     $colspan = $maxdepth - $depth - 1;
 7115:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 7116:                 }
 7117:             }
 7118:         }
 7119:     }
 7120:     return $text;
 7121: }
 7122: 
 7123: sub modifiable_userdata_row {
 7124:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 7125:     my ($role,$rolename,$statustype);
 7126:     $role = $item;
 7127:     if ($context eq 'cancreate') {
 7128:         if ($item =~ /^emailusername_(.+)$/) {
 7129:             $statustype = $1;
 7130:             $role = 'emailusername';
 7131:             if (ref($usertypes) eq 'HASH') {
 7132:                 if ($usertypes->{$statustype}) {
 7133:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 7134:                 } else {
 7135:                     $rolename = &mt('Data provided by user');
 7136:                 }
 7137:             }
 7138:         }
 7139:     } elsif ($context eq 'selfcreate') {
 7140:         if (ref($usertypes) eq 'HASH') {
 7141:             $rolename = $usertypes->{$role};
 7142:         } else {
 7143:             $rolename = $role;
 7144:         }
 7145:     } else {
 7146:         if ($role eq 'cr') {
 7147:             $rolename = &mt('Custom role');
 7148:         } else {
 7149:             $rolename = &Apache::lonnet::plaintext($role);
 7150:         }
 7151:     }
 7152:     my (@fields,%fieldtitles);
 7153:     if (ref($fieldsref) eq 'ARRAY') {
 7154:         @fields = @{$fieldsref};
 7155:     } else {
 7156:         @fields = ('lastname','firstname','middlename','generation',
 7157:                    'permanentemail','id');
 7158:     }
 7159:     if ((ref($titlesref) eq 'HASH')) {
 7160:         %fieldtitles = %{$titlesref};
 7161:     } else {
 7162:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7163:     }
 7164:     my $output;
 7165:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 7166:     $output = '<tr '.$css_class.'>'.
 7167:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 7168:               '<td class="LC_left_item" colspan="2"><table>';
 7169:     my $rem;
 7170:     my %checks;
 7171:     if (ref($settings) eq 'HASH') {
 7172:         if (ref($settings->{$context}) eq 'HASH') {
 7173:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 7174:                 my $hashref = $settings->{$context}->{$role};
 7175:                 if ($role eq 'emailusername') {
 7176:                     if ($statustype) {
 7177:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 7178:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 7179:                             if (ref($hashref) eq 'HASH') { 
 7180:                                 foreach my $field (@fields) {
 7181:                                     if ($hashref->{$field}) {
 7182:                                         $checks{$field} = $hashref->{$field};
 7183:                                     }
 7184:                                 }
 7185:                             }
 7186:                         }
 7187:                     }
 7188:                 } else {
 7189:                     if (ref($hashref) eq 'HASH') {
 7190:                         foreach my $field (@fields) {
 7191:                             if ($hashref->{$field}) {
 7192:                                 $checks{$field} = ' checked="checked" ';
 7193:                             }
 7194:                         }
 7195:                     }
 7196:                 }
 7197:             }
 7198:         }
 7199:     }
 7200:      
 7201:     for (my $i=0; $i<@fields; $i++) {
 7202:         my $rem = $i%($numinrow);
 7203:         if ($rem == 0) {
 7204:             if ($i > 0) {
 7205:                 $output .= '</tr>';
 7206:             }
 7207:             $output .= '<tr>';
 7208:         }
 7209:         my $check = ' ';
 7210:         unless ($role eq 'emailusername') {
 7211:             if (exists($checks{$fields[$i]})) {
 7212:                 $check = $checks{$fields[$i]}
 7213:             } else {
 7214:                 if ($role eq 'st') {
 7215:                     if (ref($settings) ne 'HASH') {
 7216:                         $check = ' checked="checked" '; 
 7217:                     }
 7218:                 }
 7219:             }
 7220:         }
 7221:         $output .= '<td class="LC_left_item">'.
 7222:                    '<span class="LC_nobreak">';
 7223:         if ($role eq 'emailusername') {
 7224:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 7225:                 $checks{$fields[$i]} = 'omit';
 7226:             }
 7227:             foreach my $option ('required','optional','omit') {
 7228:                 my $checked='';
 7229:                 if ($checks{$fields[$i]} eq $option) {
 7230:                     $checked='checked="checked" ';
 7231:                 }
 7232:                 $output .= '<label>'.
 7233:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 7234:                            &mt($option).'</label>'.('&nbsp;' x2);
 7235:             }
 7236:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 7237:         } else {
 7238:             $output .= '<label>'.
 7239:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 7240:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 7241:                        '</label>';
 7242:         }
 7243:         $output .= '</span></td>';
 7244:         $rem = @fields%($numinrow);
 7245:     }
 7246:     my $colsleft = $numinrow - $rem;
 7247:     if ($colsleft > 1 ) {
 7248:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7249:                    '&nbsp;</td>';
 7250:     } elsif ($colsleft == 1) {
 7251:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7252:     }
 7253:     $output .= '</tr></table></td></tr>';
 7254:     return $output;
 7255: }
 7256: 
 7257: sub insttypes_row {
 7258:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 7259:     my %lt = &Apache::lonlocal::texthash (
 7260:                       cansearch => 'Users allowed to search',
 7261:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 7262:                       lockablenames => 'User preference to lock name',
 7263:              );
 7264:     my $showdom;
 7265:     if ($context eq 'cansearch') {
 7266:         $showdom = ' ('.$dom.')';
 7267:     }
 7268:     my $class = 'LC_left_item';
 7269:     if ($context eq 'statustocreate') {
 7270:         $class = 'LC_right_item';
 7271:     }
 7272:     my $css_class = ' class="LC_odd_row"';
 7273:     if ($rownum ne '') { 
 7274:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 7275:     }
 7276:     my $output = '<tr'.$css_class.'>'.
 7277:                  '<td>'.$lt{$context}.$showdom.
 7278:                  '</td><td class="'.$class.'" colspan="2"><table>';
 7279:     my $rem;
 7280:     if (ref($types) eq 'ARRAY') {
 7281:         for (my $i=0; $i<@{$types}; $i++) {
 7282:             if (defined($usertypes->{$types->[$i]})) {
 7283:                 my $rem = $i%($numinrow);
 7284:                 if ($rem == 0) {
 7285:                     if ($i > 0) {
 7286:                         $output .= '</tr>';
 7287:                     }
 7288:                     $output .= '<tr>';
 7289:                 }
 7290:                 my $check = ' ';
 7291:                 if (ref($settings) eq 'HASH') {
 7292:                     if (ref($settings->{$context}) eq 'ARRAY') {
 7293:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 7294:                             $check = ' checked="checked" ';
 7295:                         }
 7296:                     } elsif ($context eq 'statustocreate') {
 7297:                         $check = ' checked="checked" ';
 7298:                     }
 7299:                 }
 7300:                 $output .= '<td class="LC_left_item">'.
 7301:                            '<span class="LC_nobreak"><label>'.
 7302:                            '<input type="checkbox" name="'.$context.'" '.
 7303:                            'value="'.$types->[$i].'"'.$check.'/>'.
 7304:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 7305:             }
 7306:         }
 7307:         $rem = @{$types}%($numinrow);
 7308:     }
 7309:     my $colsleft = $numinrow - $rem;
 7310:     if (($rem == 0) && (@{$types} > 0)) {
 7311:         $output .= '<tr>';
 7312:     }
 7313:     if ($colsleft > 1) {
 7314:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 7315:     } else {
 7316:         $output .= '<td class="LC_left_item">';
 7317:     }
 7318:     my $defcheck = ' ';
 7319:     if (ref($settings) eq 'HASH') {  
 7320:         if (ref($settings->{$context}) eq 'ARRAY') {
 7321:             if (grep(/^default$/,@{$settings->{$context}})) {
 7322:                 $defcheck = ' checked="checked" ';
 7323:             }
 7324:         } elsif ($context eq 'statustocreate') {
 7325:             $defcheck = ' checked="checked" ';
 7326:         }
 7327:     }
 7328:     $output .= '<span class="LC_nobreak"><label>'.
 7329:                '<input type="checkbox" name="'.$context.'" '.
 7330:                'value="default"'.$defcheck.'/>'.
 7331:                $othertitle.'</label></span></td>'.
 7332:                '</tr></table></td></tr>';
 7333:     return $output;
 7334: }
 7335: 
 7336: sub sorted_searchtitles {
 7337:     my %searchtitles = &Apache::lonlocal::texthash(
 7338:                          'uname' => 'username',
 7339:                          'lastname' => 'last name',
 7340:                          'lastfirst' => 'last name, first name',
 7341:                      );
 7342:     my @titleorder = ('uname','lastname','lastfirst');
 7343:     return (\%searchtitles,\@titleorder);
 7344: }
 7345: 
 7346: sub sorted_searchtypes {
 7347:     my %srchtypes_desc = (
 7348:                            exact    => 'is exact match',
 7349:                            contains => 'contains ..',
 7350:                            begins   => 'begins with ..',
 7351:                          );
 7352:     my @srchtypeorder = ('exact','begins','contains');
 7353:     return (\%srchtypes_desc,\@srchtypeorder);
 7354: }
 7355: 
 7356: sub usertype_update_row {
 7357:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 7358:     my $datatable;
 7359:     my $numinrow = 4;
 7360:     foreach my $type (@{$types}) {
 7361:         if (defined($usertypes->{$type})) {
 7362:             $$rownums ++;
 7363:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 7364:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 7365:                           '</td><td class="LC_left_item"><table>';
 7366:             for (my $i=0; $i<@{$fields}; $i++) {
 7367:                 my $rem = $i%($numinrow);
 7368:                 if ($rem == 0) {
 7369:                     if ($i > 0) {
 7370:                         $datatable .= '</tr>';
 7371:                     }
 7372:                     $datatable .= '<tr>';
 7373:                 }
 7374:                 my $check = ' ';
 7375:                 if (ref($settings) eq 'HASH') {
 7376:                     if (ref($settings->{'fields'}) eq 'HASH') {
 7377:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 7378:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 7379:                                 $check = ' checked="checked" ';
 7380:                             }
 7381:                         }
 7382:                     }
 7383:                 }
 7384: 
 7385:                 if ($i == @{$fields}-1) {
 7386:                     my $colsleft = $numinrow - $rem;
 7387:                     if ($colsleft > 1) {
 7388:                         $datatable .= '<td colspan="'.$colsleft.'">';
 7389:                     } else {
 7390:                         $datatable .= '<td>';
 7391:                     }
 7392:                 } else {
 7393:                     $datatable .= '<td>';
 7394:                 }
 7395:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7396:                               '<input type="checkbox" name="updateable_'.$type.
 7397:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 7398:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 7399:             }
 7400:             $datatable .= '</tr></table></td></tr>';
 7401:         }
 7402:     }
 7403:     return $datatable;
 7404: }
 7405: 
 7406: sub modify_login {
 7407:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 7408:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 7409:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 7410:     %title = ( coursecatalog => 'Display course catalog',
 7411:                adminmail => 'Display administrator E-mail address',
 7412:                helpdesk  => 'Display "Contact Helpdesk" link',
 7413:                newuser => 'Link for visitors to create a user account',
 7414:                loginheader => 'Log-in box header');
 7415:     @offon = ('off','on');
 7416:     if (ref($domconfig{login}) eq 'HASH') {
 7417:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 7418:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 7419:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 7420:             }
 7421:         }
 7422:     }
 7423:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 7424:                                            \%domconfig,\%loginhash);
 7425:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7426:     foreach my $item (@toggles) {
 7427:         $loginhash{login}{$item} = $env{'form.'.$item};
 7428:     }
 7429:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 7430:     if (ref($colchanges{'login'}) eq 'HASH') {  
 7431:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 7432:                                          \%loginhash);
 7433:     }
 7434: 
 7435:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7436:     my %domservers = &Apache::lonnet::get_servers($dom);
 7437:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 7438:     if (keys(%servers) > 1) {
 7439:         foreach my $lonhost (keys(%servers)) {
 7440:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 7441:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 7442:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 7443:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 7444:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 7445:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7446:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7447:                         $changes{'loginvia'}{$lonhost} = 1;
 7448:                     } else {
 7449:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 7450:                         $changes{'loginvia'}{$lonhost} = 1;
 7451:                     }
 7452:                 } else {
 7453:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7454:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7455:                         $changes{'loginvia'}{$lonhost} = 1;
 7456:                     }
 7457:                 }
 7458:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 7459:                     foreach my $item (@loginvia_attribs) {
 7460:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 7461:                     }
 7462:                 } else {
 7463:                     foreach my $item (@loginvia_attribs) {
 7464:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7465:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7466:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 7467:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7468:                                 $new = '/';
 7469:                             }
 7470:                         }
 7471:                         if (($item eq 'custompath') && 
 7472:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7473:                             $new = '';
 7474:                         }
 7475:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 7476:                             $changes{'loginvia'}{$lonhost} = 1;
 7477:                         }
 7478:                         if ($item eq 'exempt') {
 7479:                             $new = &check_exempt_addresses($new);
 7480:                         }
 7481:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7482:                     }
 7483:                 }
 7484:             } else {
 7485:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7486:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7487:                     $changes{'loginvia'}{$lonhost} = 1;
 7488:                     foreach my $item (@loginvia_attribs) {
 7489:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7490:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7491:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7492:                                 $new = '/';
 7493:                             }
 7494:                         }
 7495:                         if (($item eq 'custompath') && 
 7496:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7497:                             $new = '';
 7498:                         }
 7499:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7500:                     }
 7501:                 }
 7502:             }
 7503:         }
 7504:     }
 7505: 
 7506:     my $servadm = $r->dir_config('lonAdmEMail');
 7507:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 7508:     if (ref($domconfig{'login'}) eq 'HASH') {
 7509:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 7510:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 7511:                 if ($lang eq 'nolang') {
 7512:                     push(@currlangs,$lang);
 7513:                 } elsif (defined($langchoices{$lang})) {
 7514:                     push(@currlangs,$lang);
 7515:                 } else {
 7516:                     next;
 7517:                 }
 7518:             }
 7519:         }
 7520:     }
 7521:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 7522:     if (@currlangs > 0) {
 7523:         foreach my $lang (@currlangs) {
 7524:             if (grep(/^\Q$lang\E$/,@delurls)) {
 7525:                 $changes{'helpurl'}{$lang} = 1;
 7526:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 7527:                 $changes{'helpurl'}{$lang} = 1;
 7528:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 7529:                 push(@newlangs,$lang);
 7530:             } else {
 7531:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7532:             }
 7533:         }
 7534:     }
 7535:     unless (grep(/^nolang$/,@currlangs)) {
 7536:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 7537:             $changes{'helpurl'}{'nolang'} = 1;
 7538:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 7539:             push(@newlangs,'nolang');
 7540:         }
 7541:     }
 7542:     if ($env{'form.loginhelpurl_add_lang'}) {
 7543:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 7544:             ($env{'form.loginhelpurl_add_file.filename'})) {
 7545:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 7546:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 7547:         }
 7548:     }
 7549:     if ((@newlangs > 0) || ($addedfile)) {
 7550:         my $error;
 7551:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7552:         if ($configuserok eq 'ok') {
 7553:             if ($switchserver) {
 7554:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 7555:             } elsif ($author_ok eq 'ok') {
 7556:                 my @allnew = @newlangs;
 7557:                 if ($addedfile ne '') {
 7558:                     push(@allnew,$addedfile);
 7559:                 }
 7560:                 foreach my $lang (@allnew) {
 7561:                     my $formelem = 'loginhelpurl_'.$lang;
 7562:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 7563:                         $formelem = 'loginhelpurl_add_file';
 7564:                     }
 7565:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7566:                                                                "help/$lang",'','',$newfile{$lang});
 7567:                     if ($result eq 'ok') {
 7568:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 7569:                         $changes{'helpurl'}{$lang} = 1;
 7570:                     } else {
 7571:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 7572:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7573:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 7574:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 7575:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7576:                         }
 7577:                     }
 7578:                 }
 7579:             } else {
 7580:                 $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);
 7581:             }
 7582:         } else {
 7583:             $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);
 7584:         }
 7585:         if ($error) {
 7586:             &Apache::lonnet::logthis($error);
 7587:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7588:         }
 7589:     }
 7590: 
 7591:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 7592:     if (ref($domconfig{'login'}) eq 'HASH') {
 7593:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 7594:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 7595:                 if ($domservers{$lonhost}) {
 7596:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7597:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 7598:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 7599:                     }
 7600:                 }
 7601:             }
 7602:         }
 7603:     }
 7604:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 7605:     foreach my $lonhost (sort(keys(%domservers))) {
 7606:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7607:             $changes{'headtag'}{$lonhost} = 1;
 7608:         } else {
 7609:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 7610:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 7611:             }
 7612:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 7613:                 push(@newhosts,$lonhost);
 7614:             } elsif ($currheadtagurls{$lonhost}) {
 7615:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 7616:                 if ($currexempt{$lonhost}) {
 7617:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 7618:                         $changes{'headtag'}{$lonhost} = 1;
 7619:                     }
 7620:                 } elsif ($possexempt{$lonhost}) {
 7621:                     $changes{'headtag'}{$lonhost} = 1;
 7622:                 }
 7623:                 if ($possexempt{$lonhost}) {
 7624:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7625:                 }
 7626:             }
 7627:         }
 7628:     }
 7629:     if (@newhosts) {
 7630:         my $error;
 7631:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7632:         if ($configuserok eq 'ok') {
 7633:             if ($switchserver) {
 7634:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 7635:             } elsif ($author_ok eq 'ok') {
 7636:                 foreach my $lonhost (@newhosts) {
 7637:                     my $formelem = 'loginheadtag_'.$lonhost;
 7638:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7639:                                                                           "login/headtag/$lonhost",'','',
 7640:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 7641:                     if ($result eq 'ok') {
 7642:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 7643:                         $changes{'headtag'}{$lonhost} = 1;
 7644:                         if ($possexempt{$lonhost}) {
 7645:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7646:                         }
 7647:                     } else {
 7648:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 7649:                                            $newheadtagurls{$lonhost},$result);
 7650:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7651:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 7652:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 7653:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 7654:                         }
 7655:                     }
 7656:                 }
 7657:             } else {
 7658:                 $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);
 7659:             }
 7660:         } else {
 7661:             $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);
 7662:         }
 7663:         if ($error) {
 7664:             &Apache::lonnet::logthis($error);
 7665:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7666:         }
 7667:     }
 7668:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 7669: 
 7670:     my $defaulthelpfile = '/adm/loginproblems.html';
 7671:     my $defaulttext = &mt('Default in use');
 7672: 
 7673:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 7674:                                              $dom);
 7675:     if ($putresult eq 'ok') {
 7676:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7677:         my %defaultchecked = (
 7678:                     'coursecatalog' => 'on',
 7679:                     'helpdesk'      => 'on',
 7680:                     'adminmail'     => 'off',
 7681:                     'newuser'       => 'off',
 7682:         );
 7683:         if (ref($domconfig{'login'}) eq 'HASH') {
 7684:             foreach my $item (@toggles) {
 7685:                 if ($defaultchecked{$item} eq 'on') { 
 7686:                     if (($domconfig{'login'}{$item} eq '0') &&
 7687:                         ($env{'form.'.$item} eq '1')) {
 7688:                         $changes{$item} = 1;
 7689:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7690:                               $domconfig{'login'}{$item} eq '1') &&
 7691:                              ($env{'form.'.$item} eq '0')) {
 7692:                         $changes{$item} = 1;
 7693:                     }
 7694:                 } elsif ($defaultchecked{$item} eq 'off') {
 7695:                     if (($domconfig{'login'}{$item} eq '1') &&
 7696:                         ($env{'form.'.$item} eq '0')) {
 7697:                         $changes{$item} = 1;
 7698:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7699:                               $domconfig{'login'}{$item} eq '0') &&
 7700:                              ($env{'form.'.$item} eq '1')) {
 7701:                         $changes{$item} = 1;
 7702:                     }
 7703:                 }
 7704:             }
 7705:         }
 7706:         if (keys(%changes) > 0 || $colchgtext) {
 7707:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7708:             if (ref($lastactref) eq 'HASH') {
 7709:                 $lastactref->{'domainconfig'} = 1;
 7710:             }
 7711:             $resulttext = &mt('Changes made:').'<ul>';
 7712:             foreach my $item (sort(keys(%changes))) {
 7713:                 if ($item eq 'loginvia') {
 7714:                     if (ref($changes{$item}) eq 'HASH') {
 7715:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 7716:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7717:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 7718:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 7719:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 7720:                                     $protocol = 'http' if ($protocol ne 'https');
 7721:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 7722: 
 7723:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 7724:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 7725:                                     } else {
 7726:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 7727:                                     }
 7728:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 7729:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 7730:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 7731:                                     }
 7732:                                     $resulttext .= '</li>';
 7733:                                 } else {
 7734:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 7735:                                 }
 7736:                             } else {
 7737:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 7738:                             }
 7739:                         }
 7740:                         $resulttext .= '</ul></li>';
 7741:                     }
 7742:                 } elsif ($item eq 'helpurl') {
 7743:                     if (ref($changes{$item}) eq 'HASH') {
 7744:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 7745:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 7746:                                 my ($chg,$link);
 7747:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 7748:                                 if ($lang eq 'nolang') {
 7749:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 7750:                                 } else {
 7751:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 7752:                                 }
 7753:                                 $resulttext .= '<li>'.$chg.'</li>';
 7754:                             } else {
 7755:                                 my $chg;
 7756:                                 if ($lang eq 'nolang') {
 7757:                                     $chg = &mt('custom log-in help file for no preferred language');
 7758:                                 } else {
 7759:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 7760:                                 }
 7761:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 7762:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 7763:                                                       '?inhibitmenu=yes',$chg,600,500).
 7764:                                                '</li>';
 7765:                             }
 7766:                         }
 7767:                     }
 7768:                 } elsif ($item eq 'headtag') {
 7769:                     if (ref($changes{$item}) eq 'HASH') {
 7770:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7771:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7772:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 7773:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7774:                                 $resulttext .= '<li><a href="'.
 7775:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 7776:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 7777:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 7778:                                 if ($possexempt{$lonhost}) {
 7779:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 7780:                                 } else {
 7781:                                     $resulttext .= &mt('included for any client IP');
 7782:                                 }
 7783:                                 $resulttext .= '</li>';
 7784:                             }
 7785:                         }
 7786:                     }
 7787:                 } elsif ($item eq 'captcha') {
 7788:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7789:                         my $chgtxt;
 7790:                         if ($loginhash{'login'}{$item} eq 'notused') {
 7791:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 7792:                         } else {
 7793:                             my %captchas = &captcha_phrases();
 7794:                             if ($captchas{$loginhash{'login'}{$item}}) {
 7795:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 7796:                             } else {
 7797:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 7798:                             }
 7799:                         }
 7800:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7801:                     }
 7802:                 } elsif ($item eq 'recaptchakeys') {
 7803:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7804:                         my ($privkey,$pubkey);
 7805:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 7806:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 7807:                             $privkey = $loginhash{'login'}{$item}{'private'};
 7808:                         }
 7809:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 7810:                         if (!$pubkey) {
 7811:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 7812:                         } else {
 7813:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 7814:                         }
 7815:                         if (!$privkey) {
 7816:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 7817:                         } else {
 7818:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 7819:                         }
 7820:                         $chgtxt .= '</ul>';
 7821:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7822:                     }
 7823:                 } elsif ($item eq 'recaptchaversion') {
 7824:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7825:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 7826:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 7827:                                            '</li>';
 7828:                         }
 7829:                     }
 7830:                 } else {
 7831:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 7832:                 }
 7833:             }
 7834:             $resulttext .= $colchgtext.'</ul>';
 7835:         } else {
 7836:             $resulttext = &mt('No changes made to log-in page settings');
 7837:         }
 7838:     } else {
 7839:         $resulttext = '<span class="LC_error">'.
 7840: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7841:     }
 7842:     if ($errors) {
 7843:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7844:                        $errors.'</ul>';
 7845:     }
 7846:     return $resulttext;
 7847: }
 7848: 
 7849: sub check_exempt_addresses {
 7850:     my ($iplist) = @_;
 7851:     $iplist =~ s/^\s+//;
 7852:     $iplist =~ s/\s+$//;
 7853:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 7854:     my (@okips,$new);
 7855:     foreach my $ip (@poss_ips) {
 7856:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 7857:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 7858:                 push(@okips,$ip);
 7859:             }
 7860:         }
 7861:     }
 7862:     if (@okips > 0) {
 7863:         $new = join(',',@okips);
 7864:     } else {
 7865:         $new = '';
 7866:     }
 7867:     return $new;
 7868: }
 7869: 
 7870: sub color_font_choices {
 7871:     my %choices =
 7872:         &Apache::lonlocal::texthash (
 7873:             img => "Header",
 7874:             bgs => "Background colors",
 7875:             links => "Link colors",
 7876:             images => "Images",
 7877:             font => "Font color",
 7878:             fontmenu => "Font menu",
 7879:             pgbg => "Page",
 7880:             tabbg => "Header",
 7881:             sidebg => "Border",
 7882:             link => "Link",
 7883:             alink => "Active link",
 7884:             vlink => "Visited link",
 7885:         );
 7886:     return %choices;
 7887: }
 7888: 
 7889: sub modify_rolecolors {
 7890:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 7891:     my ($resulttext,%rolehash);
 7892:     $rolehash{'rolecolors'} = {};
 7893:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 7894:         if ($domconfig{'rolecolors'} eq '') {
 7895:             $domconfig{'rolecolors'} = {};
 7896:         }
 7897:     }
 7898:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 7899:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 7900:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 7901:                                              $dom);
 7902:     if ($putresult eq 'ok') {
 7903:         if (keys(%changes) > 0) {
 7904:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7905:             if (ref($lastactref) eq 'HASH') {
 7906:                 $lastactref->{'domainconfig'} = 1;
 7907:             }
 7908:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 7909:                                              $rolehash{'rolecolors'});
 7910:         } else {
 7911:             $resulttext = &mt('No changes made to default color schemes');
 7912:         }
 7913:     } else {
 7914:         $resulttext = '<span class="LC_error">'.
 7915: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7916:     }
 7917:     if ($errors) {
 7918:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7919:                        $errors.'</ul>';
 7920:     }
 7921:     return $resulttext;
 7922: }
 7923: 
 7924: sub modify_colors {
 7925:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 7926:     my (%changes,%choices);
 7927:     my @bgs;
 7928:     my @links = ('link','alink','vlink');
 7929:     my @logintext;
 7930:     my @images;
 7931:     my $servadm = $r->dir_config('lonAdmEMail');
 7932:     my $errors;
 7933:     my %defaults;
 7934:     foreach my $role (@{$roles}) {
 7935:         if ($role eq 'login') {
 7936:             %choices = &login_choices();
 7937:             @logintext = ('textcol','bgcol');
 7938:         } else {
 7939:             %choices = &color_font_choices();
 7940:         }
 7941:         if ($role eq 'login') {
 7942:             @images = ('img','logo','domlogo','login');
 7943:             @bgs = ('pgbg','mainbg','sidebg');
 7944:         } else {
 7945:             @images = ('img');
 7946:             @bgs = ('pgbg','tabbg','sidebg');
 7947:         }
 7948:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 7949:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 7950:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 7951:         }
 7952:         if ($role eq 'login') {
 7953:             foreach my $item (@logintext) {
 7954:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7955:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7956:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7957:                 }
 7958:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 7959:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7960:                 }
 7961:             }
 7962:         } else {
 7963:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 7964:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 7965:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 7966:             }
 7967:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 7968:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 7969:             }
 7970:         }
 7971:         foreach my $item (@bgs) {
 7972:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7973:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7974:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7975:             }
 7976:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 7977:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7978:             }
 7979:         }
 7980:         foreach my $item (@links) {
 7981:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7982:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7983:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7984:             }
 7985:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 7986:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7987:             }
 7988:         }
 7989:         my ($configuserok,$author_ok,$switchserver) = 
 7990:             &config_check($dom,$confname,$servadm);
 7991:         my ($width,$height) = &thumb_dimensions();
 7992:         if (ref($domconfig->{$role}) ne 'HASH') {
 7993:             $domconfig->{$role} = {};
 7994:         }
 7995:         foreach my $img (@images) {
 7996:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 7997:                 if (defined($env{'form.login_showlogo_'.$img})) {
 7998:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 7999:                 } else { 
 8000:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 8001:                 }
 8002:             } 
 8003: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 8004: 		 && !defined($domconfig->{$role}{$img})
 8005: 		 && !$env{'form.'.$role.'_del_'.$img}
 8006: 		 && $env{'form.'.$role.'_import_'.$img}) {
 8007: 		# import the old configured image from the .tab setting
 8008: 		# if they haven't provided a new one 
 8009: 		$domconfig->{$role}{$img} = 
 8010: 		    $env{'form.'.$role.'_import_'.$img};
 8011: 	    }
 8012:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 8013:                 my $error;
 8014:                 if ($configuserok eq 'ok') {
 8015:                     if ($switchserver) {
 8016:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 8017:                     } else {
 8018:                         if ($author_ok eq 'ok') {
 8019:                             my ($result,$logourl) = 
 8020:                                 &publishlogo($r,'upload',$role.'_'.$img,
 8021:                                            $dom,$confname,$img,$width,$height);
 8022:                             if ($result eq 'ok') {
 8023:                                 $confhash->{$role}{$img} = $logourl;
 8024:                                 $changes{$role}{'images'}{$img} = 1;
 8025:                             } else {
 8026:                                 $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);
 8027:                             }
 8028:                         } else {
 8029:                             $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);
 8030:                         }
 8031:                     }
 8032:                 } else {
 8033:                     $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);
 8034:                 }
 8035:                 if ($error) {
 8036:                     &Apache::lonnet::logthis($error);
 8037:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8038:                 }
 8039:             } elsif ($domconfig->{$role}{$img} ne '') {
 8040:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 8041:                     my $error;
 8042:                     if ($configuserok eq 'ok') {
 8043: # is confname an author?
 8044:                         if ($switchserver eq '') {
 8045:                             if ($author_ok eq 'ok') {
 8046:                                 my ($result,$logourl) = 
 8047:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 8048:                                             $dom,$confname,$img,$width,$height);
 8049:                                 if ($result eq 'ok') {
 8050:                                     $confhash->{$role}{$img} = $logourl;
 8051: 				    $changes{$role}{'images'}{$img} = 1;
 8052:                                 }
 8053:                             }
 8054:                         }
 8055:                     }
 8056:                 }
 8057:             }
 8058:         }
 8059:         if (ref($domconfig) eq 'HASH') {
 8060:             if (ref($domconfig->{$role}) eq 'HASH') {
 8061:                 foreach my $img (@images) {
 8062:                     if ($domconfig->{$role}{$img} ne '') {
 8063:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8064:                             $confhash->{$role}{$img} = '';
 8065:                             $changes{$role}{'images'}{$img} = 1;
 8066:                         } else {
 8067:                             if ($confhash->{$role}{$img} eq '') {
 8068:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 8069:                             }
 8070:                         }
 8071:                     } else {
 8072:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8073:                             $confhash->{$role}{$img} = '';
 8074:                             $changes{$role}{'images'}{$img} = 1;
 8075:                         } 
 8076:                     }
 8077:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 8078:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 8079:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 8080:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 8081:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 8082:                             }
 8083:                         } else {
 8084:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8085:                                 $changes{$role}{'showlogo'}{$img} = 1;
 8086:                             }
 8087:                         }
 8088:                     }
 8089:                 }
 8090:                 if ($domconfig->{$role}{'font'} ne '') {
 8091:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 8092:                         $changes{$role}{'font'} = 1;
 8093:                     }
 8094:                 } else {
 8095:                     if ($confhash->{$role}{'font'}) {
 8096:                         $changes{$role}{'font'} = 1;
 8097:                     }
 8098:                 }
 8099:                 if ($role ne 'login') {
 8100:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 8101:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 8102:                             $changes{$role}{'fontmenu'} = 1;
 8103:                         }
 8104:                     } else {
 8105:                         if ($confhash->{$role}{'fontmenu'}) {
 8106:                             $changes{$role}{'fontmenu'} = 1;
 8107:                         }
 8108:                     }
 8109:                 }
 8110:                 foreach my $item (@bgs) {
 8111:                     if ($domconfig->{$role}{$item} ne '') {
 8112:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8113:                             $changes{$role}{'bgs'}{$item} = 1;
 8114:                         } 
 8115:                     } else {
 8116:                         if ($confhash->{$role}{$item}) {
 8117:                             $changes{$role}{'bgs'}{$item} = 1;
 8118:                         }
 8119:                     }
 8120:                 }
 8121:                 foreach my $item (@links) {
 8122:                     if ($domconfig->{$role}{$item} ne '') {
 8123:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8124:                             $changes{$role}{'links'}{$item} = 1;
 8125:                         }
 8126:                     } else {
 8127:                         if ($confhash->{$role}{$item}) {
 8128:                             $changes{$role}{'links'}{$item} = 1;
 8129:                         }
 8130:                     }
 8131:                 }
 8132:                 foreach my $item (@logintext) {
 8133:                     if ($domconfig->{$role}{$item} ne '') {
 8134:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8135:                             $changes{$role}{'logintext'}{$item} = 1;
 8136:                         }
 8137:                     } else {
 8138:                         if ($confhash->{$role}{$item}) {
 8139:                             $changes{$role}{'logintext'}{$item} = 1;
 8140:                         }
 8141:                     }
 8142:                 }
 8143:             } else {
 8144:                 &default_change_checker($role,\@images,\@links,\@bgs,
 8145:                                         \@logintext,$confhash,\%changes); 
 8146:             }
 8147:         } else {
 8148:             &default_change_checker($role,\@images,\@links,\@bgs,
 8149:                                     \@logintext,$confhash,\%changes); 
 8150:         }
 8151:     }
 8152:     return ($errors,%changes);
 8153: }
 8154: 
 8155: sub config_check {
 8156:     my ($dom,$confname,$servadm) = @_;
 8157:     my ($configuserok,$author_ok,$switchserver,%currroles);
 8158:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 8159:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 8160:                                                    $confname,$servadm);
 8161:     if ($configuserok eq 'ok') {
 8162:         $switchserver = &check_switchserver($dom,$confname);
 8163:         if ($switchserver eq '') {
 8164:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 8165:         }
 8166:     }
 8167:     return ($configuserok,$author_ok,$switchserver);
 8168: }
 8169: 
 8170: sub default_change_checker {
 8171:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 8172:     foreach my $item (@{$links}) {
 8173:         if ($confhash->{$role}{$item}) {
 8174:             $changes->{$role}{'links'}{$item} = 1;
 8175:         }
 8176:     }
 8177:     foreach my $item (@{$bgs}) {
 8178:         if ($confhash->{$role}{$item}) {
 8179:             $changes->{$role}{'bgs'}{$item} = 1;
 8180:         }
 8181:     }
 8182:     foreach my $item (@{$logintext}) {
 8183:         if ($confhash->{$role}{$item}) {
 8184:             $changes->{$role}{'logintext'}{$item} = 1;
 8185:         }
 8186:     }
 8187:     foreach my $img (@{$images}) {
 8188:         if ($env{'form.'.$role.'_del_'.$img}) {
 8189:             $confhash->{$role}{$img} = '';
 8190:             $changes->{$role}{'images'}{$img} = 1;
 8191:         }
 8192:         if ($role eq 'login') {
 8193:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8194:                 $changes->{$role}{'showlogo'}{$img} = 1;
 8195:             }
 8196:         }
 8197:     }
 8198:     if ($confhash->{$role}{'font'}) {
 8199:         $changes->{$role}{'font'} = 1;
 8200:     }
 8201: }
 8202: 
 8203: sub display_colorchgs {
 8204:     my ($dom,$changes,$roles,$confhash) = @_;
 8205:     my (%choices,$resulttext);
 8206:     if (!grep(/^login$/,@{$roles})) {
 8207:         $resulttext = &mt('Changes made:').'<br />';
 8208:     }
 8209:     foreach my $role (@{$roles}) {
 8210:         if ($role eq 'login') {
 8211:             %choices = &login_choices();
 8212:         } else {
 8213:             %choices = &color_font_choices();
 8214:         }
 8215:         if (ref($changes->{$role}) eq 'HASH') {
 8216:             if ($role ne 'login') {
 8217:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 8218:             }
 8219:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 8220:                 if ($role ne 'login') {
 8221:                     $resulttext .= '<ul>';
 8222:                 }
 8223:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 8224:                     if ($role ne 'login') {
 8225:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 8226:                     }
 8227:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 8228:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 8229:                             if ($confhash->{$role}{$key}{$item}) {
 8230:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 8231:                             } else {
 8232:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 8233:                             }
 8234:                         } elsif ($confhash->{$role}{$item} eq '') {
 8235:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 8236:                         } else {
 8237:                             my $newitem = $confhash->{$role}{$item};
 8238:                             if ($key eq 'images') {
 8239:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 8240:                             }
 8241:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 8242:                         }
 8243:                     }
 8244:                     if ($role ne 'login') {
 8245:                         $resulttext .= '</ul></li>';
 8246:                     }
 8247:                 } else {
 8248:                     if ($confhash->{$role}{$key} eq '') {
 8249:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 8250:                     } else {
 8251:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 8252:                     }
 8253:                 }
 8254:                 if ($role ne 'login') {
 8255:                     $resulttext .= '</ul>';
 8256:                 }
 8257:             }
 8258:         }
 8259:     }
 8260:     return $resulttext;
 8261: }
 8262: 
 8263: sub thumb_dimensions {
 8264:     return ('200','50');
 8265: }
 8266: 
 8267: sub check_dimensions {
 8268:     my ($inputfile) = @_;
 8269:     my ($fullwidth,$fullheight);
 8270:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 8271:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 8272:             my $imageinfo = <PIPE>;
 8273:             if (!close(PIPE)) {
 8274:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 8275:             }
 8276:             chomp($imageinfo);
 8277:             my ($fullsize) = 
 8278:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 8279:             if ($fullsize) {
 8280:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 8281:             }
 8282:         }
 8283:     }
 8284:     return ($fullwidth,$fullheight);
 8285: }
 8286: 
 8287: sub check_configuser {
 8288:     my ($uhome,$dom,$confname,$servadm) = @_;
 8289:     my ($configuserok,%currroles);
 8290:     if ($uhome eq 'no_host') {
 8291:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 8292:         my $configpass = &LONCAPA::Enrollment::create_password();
 8293:         $configuserok = 
 8294:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 8295:                              $configpass,'','','','','',undef,$servadm);
 8296:     } else {
 8297:         $configuserok = 'ok';
 8298:         %currroles = 
 8299:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 8300:     }
 8301:     return ($configuserok,%currroles);
 8302: }
 8303: 
 8304: sub check_authorstatus {
 8305:     my ($dom,$confname,%currroles) = @_;
 8306:     my $author_ok;
 8307:     if (!$currroles{':'.$dom.':au'}) {
 8308:         my $start = time;
 8309:         my $end = 0;
 8310:         $author_ok = 
 8311:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 8312:                                         'au',$end,$start,'','','domconfig');
 8313:     } else {
 8314:         $author_ok = 'ok';
 8315:     }
 8316:     return $author_ok;
 8317: }
 8318: 
 8319: sub publishlogo {
 8320:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 8321:     my ($output,$fname,$logourl,$madethumb);
 8322:     if ($action eq 'upload') {
 8323:         $fname=$env{'form.'.$formname.'.filename'};
 8324:         chop($env{'form.'.$formname});
 8325:     } else {
 8326:         ($fname) = ($formname =~ /([^\/]+)$/);
 8327:     }
 8328:     if ($savefileas ne '') {
 8329:         $fname = $savefileas;
 8330:     }
 8331:     $fname=&Apache::lonnet::clean_filename($fname);
 8332: # See if there is anything left
 8333:     unless ($fname) { return ('error: no uploaded file'); }
 8334:     $fname="$subdir/$fname";
 8335:     my $docroot=$r->dir_config('lonDocRoot');
 8336:     my $filepath="$docroot/priv";
 8337:     my $relpath = "$dom/$confname";
 8338:     my ($fnamepath,$file,$fetchthumb);
 8339:     $file=$fname;
 8340:     if ($fname=~m|/|) {
 8341:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 8342:     }
 8343:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 8344:     my $count;
 8345:     for ($count=5;$count<=$#parts;$count++) {
 8346:         $filepath.="/$parts[$count]";
 8347:         if ((-e $filepath)!=1) {
 8348:             mkdir($filepath,02770);
 8349:         }
 8350:     }
 8351:     # Check for bad extension and disallow upload
 8352:     if ($file=~/\.(\w+)$/ &&
 8353:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 8354:         $output = 
 8355:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 8356:     } elsif ($file=~/\.(\w+)$/ &&
 8357:         !defined(&Apache::loncommon::fileembstyle($1))) {
 8358:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 8359:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 8360:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 8361:     } elsif (-d "$filepath/$file") {
 8362:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 8363:     } else {
 8364:         my $source = $filepath.'/'.$file;
 8365:         my $logfile;
 8366:         if (!open($logfile,">>$source".'.log')) {
 8367:             return (&mt('No write permission to Authoring Space'));
 8368:         }
 8369:         print $logfile
 8370: "\n================= Publish ".localtime()." ================\n".
 8371: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 8372: # Save the file
 8373:         if (!open(FH,'>'.$source)) {
 8374:             &Apache::lonnet::logthis('Failed to create '.$source);
 8375:             return (&mt('Failed to create file'));
 8376:         }
 8377:         if ($action eq 'upload') {
 8378:             if (!print FH ($env{'form.'.$formname})) {
 8379:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 8380:                 return (&mt('Failed to write file'));
 8381:             }
 8382:         } else {
 8383:             my $original = &Apache::lonnet::filelocation('',$formname);
 8384:             if(!copy($original,$source)) {
 8385:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 8386:                 return (&mt('Failed to write file'));
 8387:             }
 8388:         }
 8389:         close(FH);
 8390:         chmod(0660, $source); # Permissions to rw-rw---.
 8391: 
 8392:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 8393:         my $copyfile=$targetdir.'/'.$file;
 8394: 
 8395:         my @parts=split(/\//,$targetdir);
 8396:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 8397:         for (my $count=5;$count<=$#parts;$count++) {
 8398:             $path.="/$parts[$count]";
 8399:             if (!-e $path) {
 8400:                 print $logfile "\nCreating directory ".$path;
 8401:                 mkdir($path,02770);
 8402:             }
 8403:         }
 8404:         my $versionresult;
 8405:         if (-e $copyfile) {
 8406:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 8407:         } else {
 8408:             $versionresult = 'ok';
 8409:         }
 8410:         if ($versionresult eq 'ok') {
 8411:             if (copy($source,$copyfile)) {
 8412:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 8413:                 $output = 'ok';
 8414:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 8415:                 push(@{$modified_urls},[$copyfile,$source]);
 8416:                 my $metaoutput = 
 8417:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 8418:                 unless ($registered_cleanup) {
 8419:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8420:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8421:                     $registered_cleanup=1;
 8422:                 }
 8423:             } else {
 8424:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 8425:                 $output = &mt('Failed to copy file to RES space').", $!";
 8426:             }
 8427:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 8428:                 my $inputfile = $filepath.'/'.$file;
 8429:                 my $outfile = $filepath.'/'.'tn-'.$file;
 8430:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 8431:                 if ($fullwidth ne '' && $fullheight ne '') { 
 8432:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 8433:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 8434:                         system("convert -sample $thumbsize $inputfile $outfile");
 8435:                         chmod(0660, $filepath.'/tn-'.$file);
 8436:                         if (-e $outfile) {
 8437:                             my $copyfile=$targetdir.'/tn-'.$file;
 8438:                             if (copy($outfile,$copyfile)) {
 8439:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 8440:                                 my $thumb_metaoutput = 
 8441:                                     &write_metadata($dom,$confname,$formname,
 8442:                                                     $targetdir,'tn-'.$file,$logfile);
 8443:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 8444:                                 unless ($registered_cleanup) {
 8445:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8446:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8447:                                     $registered_cleanup=1;
 8448:                                 }
 8449:                                 $madethumb = 1;
 8450:                             } else {
 8451:                                 print $logfile "\nUnable to write ".$copyfile.
 8452:                                                ':'.$!."\n";
 8453:                             }
 8454:                         }
 8455:                     }
 8456:                 }
 8457:             }
 8458:         } else {
 8459:             $output = $versionresult;
 8460:         }
 8461:     }
 8462:     return ($output,$logourl,$madethumb);
 8463: }
 8464: 
 8465: sub logo_versioning {
 8466:     my ($targetdir,$file,$logfile) = @_;
 8467:     my $target = $targetdir.'/'.$file;
 8468:     my ($maxversion,$fn,$extn,$output);
 8469:     $maxversion = 0;
 8470:     if ($file =~ /^(.+)\.(\w+)$/) {
 8471:         $fn=$1;
 8472:         $extn=$2;
 8473:     }
 8474:     opendir(DIR,$targetdir);
 8475:     while (my $filename=readdir(DIR)) {
 8476:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 8477:             $maxversion=($1>$maxversion)?$1:$maxversion;
 8478:         }
 8479:     }
 8480:     $maxversion++;
 8481:     print $logfile "\nCreating old version ".$maxversion."\n";
 8482:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 8483:     if (copy($target,$copyfile)) {
 8484:         print $logfile "Copied old target to ".$copyfile."\n";
 8485:         $copyfile=$copyfile.'.meta';
 8486:         if (copy($target.'.meta',$copyfile)) {
 8487:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 8488:             $output = 'ok';
 8489:         } else {
 8490:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 8491:             $output = &mt('Failed to copy old meta').", $!, ";
 8492:         }
 8493:     } else {
 8494:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 8495:         $output = &mt('Failed to copy old target').", $!, ";
 8496:     }
 8497:     return $output;
 8498: }
 8499: 
 8500: sub write_metadata {
 8501:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 8502:     my (%metadatafields,%metadatakeys,$output);
 8503:     $metadatafields{'title'}=$formname;
 8504:     $metadatafields{'creationdate'}=time;
 8505:     $metadatafields{'lastrevisiondate'}=time;
 8506:     $metadatafields{'copyright'}='public';
 8507:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 8508:                                          $env{'user.domain'};
 8509:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 8510:     $metadatafields{'domain'}=$dom;
 8511:     {
 8512:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 8513:         my $mfh;
 8514:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 8515:             foreach (sort(keys(%metadatafields))) {
 8516:                 unless ($_=~/\./) {
 8517:                     my $unikey=$_;
 8518:                     $unikey=~/^([A-Za-z]+)/;
 8519:                     my $tag=$1;
 8520:                     $tag=~tr/A-Z/a-z/;
 8521:                     print $mfh "\n\<$tag";
 8522:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 8523:                         my $value=$metadatafields{$unikey.'.'.$_};
 8524:                         $value=~s/\"/\'\'/g;
 8525:                         print $mfh ' '.$_.'="'.$value.'"';
 8526:                     }
 8527:                     print $mfh '>'.
 8528:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 8529:                             .'</'.$tag.'>';
 8530:                 }
 8531:             }
 8532:             $output = 'ok';
 8533:             print $logfile "\nWrote metadata";
 8534:             close($mfh);
 8535:         } else {
 8536:             print $logfile "\nFailed to open metadata file";
 8537:             $output = &mt('Could not write metadata');
 8538:         }
 8539:     }
 8540:     return $output;
 8541: }
 8542: 
 8543: sub notifysubscribed {
 8544:     foreach my $targetsource (@{$modified_urls}){
 8545:         next unless (ref($targetsource) eq 'ARRAY');
 8546:         my ($target,$source)=@{$targetsource};
 8547:         if ($source ne '') {
 8548:             if (open(my $logfh,'>>'.$source.'.log')) {
 8549:                 print $logfh "\nCleanup phase: Notifications\n";
 8550:                 my @subscribed=&subscribed_hosts($target);
 8551:                 foreach my $subhost (@subscribed) {
 8552:                     print $logfh "\nNotifying host ".$subhost.':';
 8553:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 8554:                     print $logfh $reply;
 8555:                 }
 8556:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 8557:                 foreach my $subhost (@subscribedmeta) {
 8558:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 8559:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 8560:                                                         $subhost);
 8561:                     print $logfh $reply;
 8562:                 }
 8563:                 print $logfh "\n============ Done ============\n";
 8564:                 close($logfh);
 8565:             }
 8566:         }
 8567:     }
 8568:     return OK;
 8569: }
 8570: 
 8571: sub subscribed_hosts {
 8572:     my ($target) = @_;
 8573:     my @subscribed;
 8574:     if (open(my $fh,"<$target.subscription")) {
 8575:         while (my $subline=<$fh>) {
 8576:             if ($subline =~ /^($match_lonid):/) {
 8577:                 my $host = $1;
 8578:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 8579:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 8580:                         push(@subscribed,$host);
 8581:                     }
 8582:                 }
 8583:             }
 8584:         }
 8585:     }
 8586:     return @subscribed;
 8587: }
 8588: 
 8589: sub check_switchserver {
 8590:     my ($dom,$confname) = @_;
 8591:     my ($allowed,$switchserver);
 8592:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 8593:     if ($home eq 'no_host') {
 8594:         $home = &Apache::lonnet::domain($dom,'primary');
 8595:     }
 8596:     my @ids=&Apache::lonnet::current_machine_ids();
 8597:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 8598:     if (!$allowed) {
 8599: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 8600:     }
 8601:     return $switchserver;
 8602: }
 8603: 
 8604: sub modify_quotas {
 8605:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 8606:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 8607:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 8608:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 8609:         $validationfieldsref);
 8610:     if ($action eq 'quotas') {
 8611:         $context = 'tools'; 
 8612:     } else {
 8613:         $context = $action;
 8614:     }
 8615:     if ($context eq 'requestcourses') {
 8616:         @usertools = ('official','unofficial','community','textbook','placement');
 8617:         @options =('norequest','approval','validate','autolimit');
 8618:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 8619:         %titles = &courserequest_titles();
 8620:         $toolregexp = join('|',@usertools);
 8621:         %conditions = &courserequest_conditions();
 8622:         $confname = $dom.'-domainconfig';
 8623:         my $servadm = $r->dir_config('lonAdmEMail');
 8624:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8625:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 8626:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 8627:     } elsif ($context eq 'requestauthor') {
 8628:         @usertools = ('author');
 8629:         %titles = &authorrequest_titles();
 8630:     } else {
 8631:         @usertools = ('aboutme','blog','webdav','portfolio');
 8632:         %titles = &tool_titles();
 8633:     }
 8634:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8635:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8636:     foreach my $key (keys(%env)) {
 8637:         if ($context eq 'requestcourses') {
 8638:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 8639:                 my $item = $1;
 8640:                 my $type = $2;
 8641:                 if ($type =~ /^limit_(.+)/) {
 8642:                     $limithash{$item}{$1} = $env{$key};
 8643:                 } else {
 8644:                     $confhash{$item}{$type} = $env{$key};
 8645:                 }
 8646:             }
 8647:         } elsif ($context eq 'requestauthor') {
 8648:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 8649:                 $confhash{$1} = $env{$key};
 8650:             }
 8651:         } else {
 8652:             if ($key =~ /^form\.quota_(.+)$/) {
 8653:                 $confhash{'defaultquota'}{$1} = $env{$key};
 8654:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 8655:                 $confhash{'authorquota'}{$1} = $env{$key};
 8656:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 8657:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 8658:             }
 8659:         }
 8660:     }
 8661:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 8662:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 8663:         @approvalnotify = sort(@approvalnotify);
 8664:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 8665:         my @crstypes = ('official','unofficial','community','textbook','placement');
 8666:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 8667:         foreach my $type (@hasuniquecode) {
 8668:             if (grep(/^\Q$type\E$/,@crstypes)) {
 8669:                 $confhash{'uniquecode'}{$type} = 1;
 8670:             }
 8671:         }
 8672:         my (%newbook,%allpos);
 8673:         if ($context eq 'requestcourses') {
 8674:             foreach my $type ('textbooks','templates') {
 8675:                 @{$allpos{$type}} = (); 
 8676:                 my $invalid;
 8677:                 if ($type eq 'textbooks') {
 8678:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 8679:                 } else {
 8680:                     $invalid = &mt('Invalid LON-CAPA course for template');
 8681:                 }
 8682:                 if ($env{'form.'.$type.'_addbook'}) {
 8683:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 8684:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 8685:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 8686:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 8687:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8688:                         } else {
 8689:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 8690:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 8691:                             $position =~ s/\D+//g;
 8692:                             if ($position ne '') {
 8693:                                 $allpos{$type}[$position] = $newbook{$type};
 8694:                             }
 8695:                         }
 8696:                     } else {
 8697:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8698:                     }
 8699:                 }
 8700:             } 
 8701:         }
 8702:         if (ref($domconfig{$action}) eq 'HASH') {
 8703:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 8704:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 8705:                     $changes{'notify'}{'approval'} = 1;
 8706:                 }
 8707:             } else {
 8708:                 if ($confhash{'notify'}{'approval'}) {
 8709:                     $changes{'notify'}{'approval'} = 1;
 8710:                 }
 8711:             }
 8712:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 8713:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 8714:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 8715:                         unless ($confhash{'uniquecode'}{$crstype}) {
 8716:                             $changes{'uniquecode'} = 1;
 8717:                         }
 8718:                     }
 8719:                     unless ($changes{'uniquecode'}) {
 8720:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 8721:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 8722:                                 $changes{'uniquecode'} = 1;
 8723:                             }
 8724:                         }
 8725:                     }
 8726:                } else {
 8727:                    $changes{'uniquecode'} = 1;
 8728:                }
 8729:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 8730:                 $changes{'uniquecode'} = 1;
 8731:             }
 8732:             if ($context eq 'requestcourses') {
 8733:                 foreach my $type ('textbooks','templates') {
 8734:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8735:                         my %deletions;
 8736:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 8737:                         if (@todelete) {
 8738:                             map { $deletions{$_} = 1; } @todelete;
 8739:                         }
 8740:                         my %imgdeletions;
 8741:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 8742:                         if (@todeleteimages) {
 8743:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 8744:                         }
 8745:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 8746:                         for (my $i=0; $i<=$maxnum; $i++) {
 8747:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 8748:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 8749:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 8750:                                 if ($deletions{$key}) {
 8751:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 8752:                                         #FIXME need to obsolete item in RES space
 8753:                                     }
 8754:                                     next;
 8755:                                 } else {
 8756:                                     my $newpos = $env{'form.'.$itemid};
 8757:                                     $newpos =~ s/\D+//g;
 8758:                                     foreach my $item ('subject','title','publisher','author') {
 8759:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8760:                                                  ($type eq 'templates'));
 8761:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 8762:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 8763:                                             $changes{$type}{$key} = 1;
 8764:                                         }
 8765:                                     }
 8766:                                     $allpos{$type}[$newpos] = $key;
 8767:                                 }
 8768:                                 if ($imgdeletions{$key}) {
 8769:                                     $changes{$type}{$key} = 1;
 8770:                                     #FIXME need to obsolete item in RES space
 8771:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 8772:                                     my ($cdom,$cnum) = split(/_/,$key);
 8773:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 8774:                                                                                   $cdom,$cnum,$type,$configuserok,
 8775:                                                                                   $switchserver,$author_ok);
 8776:                                     if ($imgurl) {
 8777:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 8778:                                         $changes{$type}{$key} = 1; 
 8779:                                     }
 8780:                                     if ($error) {
 8781:                                         &Apache::lonnet::logthis($error);
 8782:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8783:                                     } 
 8784:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 8785:                                     $confhash{$type}{$key}{'image'} = 
 8786:                                         $domconfig{$action}{$type}{$key}{'image'};
 8787:                                 }
 8788:                             }
 8789:                         }
 8790:                     }
 8791:                 }
 8792:             }
 8793:         } else {
 8794:             if ($confhash{'notify'}{'approval'}) {
 8795:                 $changes{'notify'}{'approval'} = 1;
 8796:             }
 8797:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 8798:                 $changes{'uniquecode'} = 1;
 8799:             }
 8800:         }
 8801:         if ($context eq 'requestcourses') {
 8802:             foreach my $type ('textbooks','templates') {
 8803:                 if ($newbook{$type}) {
 8804:                     $changes{$type}{$newbook{$type}} = 1;
 8805:                     foreach my $item ('subject','title','publisher','author') {
 8806:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8807:                                  ($type eq 'template'));
 8808:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 8809:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 8810:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 8811:                         }
 8812:                     }
 8813:                     if ($type eq 'textbooks') {
 8814:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 8815:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 8816:                             my ($imageurl,$error) =
 8817:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 8818:                                                         $configuserok,$switchserver,$author_ok);
 8819:                             if ($imageurl) {
 8820:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 8821:                             }
 8822:                             if ($error) {
 8823:                                 &Apache::lonnet::logthis($error);
 8824:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8825:                             }
 8826:                         }
 8827:                     }
 8828:                 }
 8829:                 if (@{$allpos{$type}} > 0) {
 8830:                     my $idx = 0;
 8831:                     foreach my $item (@{$allpos{$type}}) {
 8832:                         if ($item ne '') {
 8833:                             $confhash{$type}{$item}{'order'} = $idx;
 8834:                             if (ref($domconfig{$action}) eq 'HASH') {
 8835:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8836:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 8837:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 8838:                                             $changes{$type}{$item} = 1;
 8839:                                         }
 8840:                                     }
 8841:                                 }
 8842:                             }
 8843:                             $idx ++;
 8844:                         }
 8845:                     }
 8846:                 }
 8847:             }
 8848:             if (ref($validationitemsref) eq 'ARRAY') {
 8849:                 foreach my $item (@{$validationitemsref}) {
 8850:                     if ($item eq 'fields') {
 8851:                         my @changed;
 8852:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 8853:                         if (@{$confhash{'validation'}{$item}} > 0) {
 8854:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 8855:                         }
 8856:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8857:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8858:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 8859:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 8860:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 8861:                                 } else {
 8862:                                     @changed = @{$confhash{'validation'}{$item}};
 8863:                                 }
 8864:                             } else {
 8865:                                 @changed = @{$confhash{'validation'}{$item}};
 8866:                             }
 8867:                         } else {
 8868:                             @changed = @{$confhash{'validation'}{$item}};
 8869:                         }
 8870:                         if (@changed) {
 8871:                             if ($confhash{'validation'}{$item}) {
 8872:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 8873:                             } else {
 8874:                                 $changes{'validation'}{$item} = &mt('None');
 8875:                             }
 8876:                         }
 8877:                     } else {
 8878:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 8879:                         if ($item eq 'markup') {
 8880:                             if ($env{'form.requestcourses_validation_'.$item}) {
 8881:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 8882:                             }
 8883:                         }
 8884:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8885:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8886:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 8887:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8888:                                 }
 8889:                             } else {
 8890:                                 if ($confhash{'validation'}{$item} ne '') {
 8891:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8892:                                 }
 8893:                             }
 8894:                         } else {
 8895:                             if ($confhash{'validation'}{$item} ne '') {
 8896:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8897:                             }
 8898:                         }
 8899:                     }
 8900:                 }
 8901:             }
 8902:             if ($env{'form.validationdc'}) {
 8903:                 my $newval = $env{'form.validationdc'};
 8904:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 8905:                 if (exists($domcoords{$newval})) {
 8906:                     $confhash{'validation'}{'dc'} = $newval;
 8907:                 }
 8908:             }
 8909:             if (ref($confhash{'validation'}) eq 'HASH') {
 8910:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8911:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8912:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8913:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8914:                                 if ($confhash{'validation'}{'dc'} eq '') {
 8915:                                     $changes{'validation'}{'dc'} = &mt('None');
 8916:                                 } else {
 8917:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8918:                                 }
 8919:                             }
 8920:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 8921:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8922:                         }
 8923:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 8924:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8925:                     }
 8926:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 8927:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8928:                 }
 8929:             } else {
 8930:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8931:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8932:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8933:                             $changes{'validation'}{'dc'} = &mt('None');
 8934:                         }
 8935:                     }
 8936:                 }
 8937:             }
 8938:         }
 8939:     } else {
 8940:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 8941:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 8942:     }
 8943:     foreach my $item (@usertools) {
 8944:         foreach my $type (@{$types},'default','_LC_adv') {
 8945:             my $unset; 
 8946:             if ($context eq 'requestcourses') {
 8947:                 $unset = '0';
 8948:                 if ($type eq '_LC_adv') {
 8949:                     $unset = '';
 8950:                 }
 8951:                 if ($confhash{$item}{$type} eq 'autolimit') {
 8952:                     $confhash{$item}{$type} .= '=';
 8953:                     unless ($limithash{$item}{$type} =~ /\D/) {
 8954:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 8955:                     }
 8956:                 }
 8957:             } elsif ($context eq 'requestauthor') {
 8958:                 $unset = '0';
 8959:                 if ($type eq '_LC_adv') {
 8960:                     $unset = '';
 8961:                 }
 8962:             } else {
 8963:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 8964:                     $confhash{$item}{$type} = 1;
 8965:                 } else {
 8966:                     $confhash{$item}{$type} = 0;
 8967:                 }
 8968:             }
 8969:             if (ref($domconfig{$action}) eq 'HASH') {
 8970:                 if ($action eq 'requestauthor') {
 8971:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 8972:                         $changes{$type} = 1;
 8973:                     }
 8974:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 8975:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 8976:                         $changes{$item}{$type} = 1;
 8977:                     }
 8978:                 } else {
 8979:                     if ($context eq 'requestcourses') {
 8980:                         if ($confhash{$item}{$type} ne $unset) {
 8981:                             $changes{$item}{$type} = 1;
 8982:                         }
 8983:                     } else {
 8984:                         if (!$confhash{$item}{$type}) {
 8985:                             $changes{$item}{$type} = 1;
 8986:                         }
 8987:                     }
 8988:                 }
 8989:             } else {
 8990:                 if ($context eq 'requestcourses') {
 8991:                     if ($confhash{$item}{$type} ne $unset) {
 8992:                         $changes{$item}{$type} = 1;
 8993:                     }
 8994:                 } elsif ($context eq 'requestauthor') {
 8995:                     if ($confhash{$type} ne $unset) {
 8996:                         $changes{$type} = 1;
 8997:                     }
 8998:                 } else {
 8999:                     if (!$confhash{$item}{$type}) {
 9000:                         $changes{$item}{$type} = 1;
 9001:                     }
 9002:                 }
 9003:             }
 9004:         }
 9005:     }
 9006:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9007:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 9008:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9009:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 9010:                     if (exists($confhash{'defaultquota'}{$key})) {
 9011:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 9012:                             $changes{'defaultquota'}{$key} = 1;
 9013:                         }
 9014:                     } else {
 9015:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 9016:                     }
 9017:                 }
 9018:             } else {
 9019:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 9020:                     if (exists($confhash{'defaultquota'}{$key})) {
 9021:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 9022:                             $changes{'defaultquota'}{$key} = 1;
 9023:                         }
 9024:                     } else {
 9025:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 9026:                     }
 9027:                 }
 9028:             }
 9029:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9030:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 9031:                     if (exists($confhash{'authorquota'}{$key})) {
 9032:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 9033:                             $changes{'authorquota'}{$key} = 1;
 9034:                         }
 9035:                     } else {
 9036:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 9037:                     }
 9038:                 }
 9039:             }
 9040:         }
 9041:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 9042:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 9043:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9044:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9045:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 9046:                             $changes{'defaultquota'}{$key} = 1;
 9047:                         }
 9048:                     } else {
 9049:                         if (!exists($domconfig{'quotas'}{$key})) {
 9050:                             $changes{'defaultquota'}{$key} = 1;
 9051:                         }
 9052:                     }
 9053:                 } else {
 9054:                     $changes{'defaultquota'}{$key} = 1;
 9055:                 }
 9056:             }
 9057:         }
 9058:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 9059:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 9060:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9061:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9062:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 9063:                             $changes{'authorquota'}{$key} = 1;
 9064:                         }
 9065:                     } else {
 9066:                         $changes{'authorquota'}{$key} = 1;
 9067:                     }
 9068:                 } else {
 9069:                     $changes{'authorquota'}{$key} = 1;
 9070:                 }
 9071:             }
 9072:         }
 9073:     }
 9074: 
 9075:     if ($context eq 'requestauthor') {
 9076:         $domdefaults{'requestauthor'} = \%confhash;
 9077:     } else {
 9078:         foreach my $key (keys(%confhash)) {
 9079:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 9080:                 $domdefaults{$key} = $confhash{$key};
 9081:             }
 9082:         }
 9083:     }
 9084: 
 9085:     my %quotahash = (
 9086:                       $action => { %confhash }
 9087:                     );
 9088:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 9089:                                              $dom);
 9090:     if ($putresult eq 'ok') {
 9091:         if (keys(%changes) > 0) {
 9092:             my $cachetime = 24*60*60;
 9093:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9094:             if (ref($lastactref) eq 'HASH') {
 9095:                 $lastactref->{'domdefaults'} = 1;
 9096:             }
 9097:             $resulttext = &mt('Changes made:').'<ul>';
 9098:             unless (($context eq 'requestcourses') ||
 9099:                     ($context eq 'requestauthor')) {
 9100:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 9101:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 9102:                     foreach my $type (@{$types},'default') {
 9103:                         if (defined($changes{'defaultquota'}{$type})) {
 9104:                             my $typetitle = $usertypes->{$type};
 9105:                             if ($type eq 'default') {
 9106:                                 $typetitle = $othertitle;
 9107:                             }
 9108:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 9109:                         }
 9110:                     }
 9111:                     $resulttext .= '</ul></li>';
 9112:                 }
 9113:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 9114:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 9115:                     foreach my $type (@{$types},'default') {
 9116:                         if (defined($changes{'authorquota'}{$type})) {
 9117:                             my $typetitle = $usertypes->{$type};
 9118:                             if ($type eq 'default') {
 9119:                                 $typetitle = $othertitle;
 9120:                             }
 9121:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 9122:                         }
 9123:                     }
 9124:                     $resulttext .= '</ul></li>';
 9125:                 }
 9126:             }
 9127:             my %newenv;
 9128:             foreach my $item (@usertools) {
 9129:                 my (%haschgs,%inconf);
 9130:                 if ($context eq 'requestauthor') {
 9131:                     %haschgs = %changes;
 9132:                     %inconf = %confhash;
 9133:                 } else {
 9134:                     if (ref($changes{$item}) eq 'HASH') {
 9135:                         %haschgs = %{$changes{$item}};
 9136:                     }
 9137:                     if (ref($confhash{$item}) eq 'HASH') {
 9138:                         %inconf = %{$confhash{$item}};
 9139:                     }
 9140:                 }
 9141:                 if (keys(%haschgs) > 0) {
 9142:                     my $newacc = 
 9143:                         &Apache::lonnet::usertools_access($env{'user.name'},
 9144:                                                           $env{'user.domain'},
 9145:                                                           $item,'reload',$context);
 9146:                     if (($context eq 'requestcourses') ||
 9147:                         ($context eq 'requestauthor')) {
 9148:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 9149:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 9150:                         }
 9151:                     } else {
 9152:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 9153:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 9154:                         }
 9155:                     }
 9156:                     unless ($context eq 'requestauthor') {
 9157:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 9158:                     }
 9159:                     foreach my $type (@{$types},'default','_LC_adv') {
 9160:                         if ($haschgs{$type}) {
 9161:                             my $typetitle = $usertypes->{$type};
 9162:                             if ($type eq 'default') {
 9163:                                 $typetitle = $othertitle;
 9164:                             } elsif ($type eq '_LC_adv') {
 9165:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 9166:                             }
 9167:                             if ($inconf{$type}) {
 9168:                                 if ($context eq 'requestcourses') {
 9169:                                     my $cond;
 9170:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 9171:                                         if ($1 eq '') {
 9172:                                             $cond = &mt('(Automatic processing of any request).');
 9173:                                         } else {
 9174:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 9175:                                         }
 9176:                                     } else { 
 9177:                                         $cond = $conditions{$inconf{$type}};
 9178:                                     }
 9179:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 9180:                                 } elsif ($context eq 'requestauthor') {
 9181:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 9182:                                                              $titles{$inconf{$type}},$typetitle);
 9183: 
 9184:                                 } else {
 9185:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 9186:                                 }
 9187:                             } else {
 9188:                                 if ($type eq '_LC_adv') {
 9189:                                     if ($inconf{$type} eq '0') {
 9190:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9191:                                     } else { 
 9192:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 9193:                                     }
 9194:                                 } else {
 9195:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9196:                                 }
 9197:                             }
 9198:                         }
 9199:                     }
 9200:                     unless ($context eq 'requestauthor') {
 9201:                         $resulttext .= '</ul></li>';
 9202:                     }
 9203:                 }
 9204:             }
 9205:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 9206:                 if (ref($changes{'notify'}) eq 'HASH') {
 9207:                     if ($changes{'notify'}{'approval'}) {
 9208:                         if (ref($confhash{'notify'}) eq 'HASH') {
 9209:                             if ($confhash{'notify'}{'approval'}) {
 9210:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 9211:                             } else {
 9212:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 9213:                             }
 9214:                         }
 9215:                     }
 9216:                 }
 9217:             }
 9218:             if ($action eq 'requestcourses') {
 9219:                 my @offon = ('off','on');
 9220:                 if ($changes{'uniquecode'}) {
 9221:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9222:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 9223:                         $resulttext .= '<li>'.
 9224:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 9225:                                        '</li>';
 9226:                     } else {
 9227:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 9228:                                        '</li>';
 9229:                     }
 9230:                 }
 9231:                 foreach my $type ('textbooks','templates') {
 9232:                     if (ref($changes{$type}) eq 'HASH') {
 9233:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 9234:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 9235:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 9236:                             my $coursetitle = $coursehash{'description'};
 9237:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 9238:                             $resulttext .= '<li>';
 9239:                             foreach my $item ('subject','title','publisher','author') {
 9240:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9241:                                          ($type eq 'templates'));
 9242:                                 my $name = $item.':';
 9243:                                 $name =~ s/^(\w)/\U$1/;
 9244:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 9245:                             }
 9246:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 9247:                             if ($type eq 'textbooks') {
 9248:                                 if ($confhash{$type}{$key}{'image'}) {
 9249:                                     $resulttext .= ' '.&mt('Image: [_1]',
 9250:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 9251:                                                    ' alt="Textbook cover" />').'<br />';
 9252:                                 }
 9253:                             }
 9254:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 9255:                         }
 9256:                         $resulttext .= '</ul></li>';
 9257:                     }
 9258:                 }
 9259:                 if (ref($changes{'validation'}) eq 'HASH') {
 9260:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 9261:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 9262:                         foreach my $item (@{$validationitemsref}) {
 9263:                             if (exists($changes{'validation'}{$item})) {
 9264:                                 if ($item eq 'markup') {
 9265:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9266:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 9267:                                 } else {
 9268:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9269:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 9270:                                 }
 9271:                             }
 9272:                         }
 9273:                         if (exists($changes{'validation'}{'dc'})) {
 9274:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 9275:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 9276:                         }
 9277:                     }
 9278:                 }
 9279:             }
 9280:             $resulttext .= '</ul>';
 9281:             if (keys(%newenv)) {
 9282:                 &Apache::lonnet::appenv(\%newenv);
 9283:             }
 9284:         } else {
 9285:             if ($context eq 'requestcourses') {
 9286:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 9287:             } elsif ($context eq 'requestauthor') {
 9288:                 $resulttext = &mt('No changes made to rights to request author space.');
 9289:             } else {
 9290:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 9291:             }
 9292:         }
 9293:     } else {
 9294:         $resulttext = '<span class="LC_error">'.
 9295: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9296:     }
 9297:     if ($errors) {
 9298:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 9299:                        '<ul>'.$errors.'</ul></p>';
 9300:     }
 9301:     return $resulttext;
 9302: }
 9303: 
 9304: sub process_textbook_image {
 9305:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 9306:     my $filename = $env{'form.'.$caller.'.filename'};
 9307:     my ($error,$url);
 9308:     my ($width,$height) = (50,50);
 9309:     if ($configuserok eq 'ok') {
 9310:         if ($switchserver) {
 9311:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 9312:                          $switchserver);
 9313:         } elsif ($author_ok eq 'ok') {
 9314:             my ($result,$imageurl) =
 9315:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9316:                              "$type/$dom/$cnum/cover",$width,$height);
 9317:             if ($result eq 'ok') {
 9318:                 $url = $imageurl;
 9319:             } else {
 9320:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9321:             }
 9322:         } else {
 9323:             $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);
 9324:         }
 9325:     } else {
 9326:         $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);
 9327:     }
 9328:     return ($url,$error);
 9329: }
 9330: 
 9331: sub modify_ltitools {
 9332:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 9333:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9334:     my ($newid,@allpos,%changes,%confhash,$errors,$resulttext);
 9335:     my $confname = $dom.'-domainconfig';
 9336:     my $servadm = $r->dir_config('lonAdmEMail');
 9337:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9338:     my (%posslti,%possfield);
 9339:     my @courseroles = ('cc','in','ta','ep','st');
 9340:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 9341:     map { $posslti{$_} = 1; } @ltiroles;
 9342:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
 9343:     map { $possfield{$_} = 1; } @allfields;
 9344:     my %lt = &ltitools_names(); 
 9345:     if ($env{'form.ltitools_add'}) {
 9346:         my $title = $env{'form.ltitools_add_title'};
 9347:         $title =~ s/(`)/'/g;
 9348:         ($newid,my $error) = &get_ltitools_id($dom,$title);
 9349:         if ($newid) {
 9350:             my $position = $env{'form.ltitools_add_pos'};
 9351:             $position =~ s/\D+//g;
 9352:             if ($position ne '') {
 9353:                 $allpos[$position] = $newid;
 9354:             }
 9355:             $changes{$newid} = 1;
 9356:             foreach my $item ('title','url','key','secret') {
 9357:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
 9358:                 if ($env{'form.ltitools_add_'.$item}) {
 9359:                     $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
 9360:                 }
 9361:             }
 9362:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
 9363:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
 9364:             }
 9365:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
 9366:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
 9367:             }
 9368:             foreach my $item ('width','height') {
 9369:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
 9370:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
 9371:                 if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
 9372:                     $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
 9373:                 }
 9374:             }
 9375:             if ($env{'form.ltitools_add_target'} eq 'window') {
 9376:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
 9377:             } else {
 9378:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
 9379:             }
 9380:             foreach my $item ('passback','roster') {
 9381:                 if ($env{'form.ltitools_add_'.$item}) {
 9382:                     $confhash{$newid}{$item} = 1;
 9383:                 }
 9384:             }
 9385:             if ($env{'form.ltitools_add_image.filename'} ne '') {
 9386:                 my ($imageurl,$error) =
 9387:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$dom,
 9388:                                             $configuserok,$switchserver,$author_ok);
 9389:                 if ($imageurl) {
 9390:                     $confhash{$newid}{'image'} = $imageurl;
 9391:                 }
 9392:                 if ($error) {
 9393:                     &Apache::lonnet::logthis($error);
 9394:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9395:                 }
 9396:             }
 9397:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
 9398:             foreach my $field (@fields) {
 9399:                 if ($possfield{$field}) {
 9400:                     if ($field eq 'roles') {
 9401:                         foreach my $role (@courseroles) {
 9402:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
 9403:                             if (($choice ne '') && ($posslti{$choice})) {
 9404:                                 $confhash{$newid}{'roles'}{$role} = $choice;
 9405:                                 if ($role eq 'cc') {
 9406:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
 9407:                                 }
 9408:                             }
 9409:                         }
 9410:                     } else {
 9411:                         $confhash{$newid}{'fields'}{$field} = 1;
 9412:                     }
 9413:                 }
 9414:             }
 9415:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
 9416:             foreach my $item (@courseconfig) {
 9417:                 $confhash{$newid}{'crsconf'}{$item} = 1;
 9418:             }
 9419:             if ($env{'form.ltitools_add_custom'}) {
 9420:                 my $name = $env{'form.ltitools_add_custom_name'};
 9421:                 my $value = $env{'form.ltitools_add_custom_value'};
 9422:                 $value =~ s/(`)/'/g;
 9423:                 $name =~ s/(`)/'/g;
 9424:                 $confhash{$newid}{'custom'}{$name} = $value;
 9425:             }
 9426:         } else {
 9427:             my $error = &mt('Failed to acquire unique ID for new external tool');   
 9428:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9429:         }
 9430:     }
 9431:     if (ref($domconfig{$action}) eq 'HASH') {
 9432:         my %deletions;
 9433:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
 9434:         if (@todelete) {
 9435:             map { $deletions{$_} = 1; } @todelete;
 9436:         }
 9437:         my %customadds;
 9438:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
 9439:         if (@newcustom) {
 9440:             map { $customadds{$_} = 1; } @newcustom;
 9441:         } 
 9442:         my %imgdeletions;
 9443:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
 9444:         if (@todeleteimages) {
 9445:             map { $imgdeletions{$_} = 1; } @todeleteimages;
 9446:         }
 9447:         my $maxnum = $env{'form.ltitools_maxnum'};
 9448:         for (my $i=0; $i<=$maxnum; $i++) {
 9449:             my $itemid = $env{'form.ltitools_id_'.$i};
 9450:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
 9451:                 if ($deletions{$itemid}) {
 9452:                     if ($domconfig{$action}{$itemid}{'image'}) {
 9453:                         #FIXME need to obsolete item in RES space
 9454:                     }
 9455:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
 9456:                     next;
 9457:                 } else {
 9458:                     my $newpos = $env{'form.ltitools_'.$itemid};
 9459:                     $newpos =~ s/\D+//g;
 9460:                     foreach my $item ('title','url','key','secret') {
 9461:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9462:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
 9463:                             $changes{$itemid} = 1;
 9464:                         }
 9465:                     }
 9466:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
 9467:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
 9468:                     }
 9469:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
 9470:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
 9471:                     }
 9472:                     foreach my $size ('width','height') {
 9473:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
 9474:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
 9475:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
 9476:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
 9477:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9478:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
 9479:                                     $changes{$itemid} = 1;
 9480:                                 }
 9481:                             } else {
 9482:                                 $changes{$itemid} = 1;
 9483:                             }
 9484:                         }
 9485:                     }
 9486:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
 9487:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
 9488:                     } else {
 9489:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
 9490:                     }
 9491:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9492:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
 9493:                             $changes{$itemid} = 1;
 9494:                         }
 9495:                     } else {
 9496:                         $changes{$itemid} = 1;
 9497:                     }
 9498:                     foreach my $extra ('passback','roster') {
 9499:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
 9500:                             $confhash{$itemid}{$extra} = 1;
 9501:                         }
 9502:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
 9503:                             $changes{$itemid} = 1;
 9504:                         }
 9505:                     }
 9506:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
 9507:                     foreach my $item ('label','title','target') {
 9508:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
 9509:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
 9510:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
 9511:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
 9512:                                     $changes{$itemid} = 1;
 9513:                                 }
 9514:                             } else {
 9515:                                 $changes{$itemid} = 1;
 9516:                             }
 9517:                         }
 9518:                     }
 9519:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
 9520:                     foreach my $field (@fields) {
 9521:                         if ($possfield{$field}) {
 9522:                             if ($field eq 'roles') {
 9523:                                 foreach my $role (@courseroles) {
 9524:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
 9525:                                     if (($choice ne '') && ($posslti{$choice})) {
 9526:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
 9527:                                         if ($role eq 'cc') {
 9528:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
 9529:                                         }
 9530:                                     }
 9531:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
 9532:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
 9533:                                             $changes{$itemid} = 1;
 9534:                                         }
 9535:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
 9536:                                         $changes{$itemid} = 1;
 9537:                                     }
 9538:                                 }
 9539:                             } else {
 9540:                                 $confhash{$itemid}{'fields'}{$field} = 1;
 9541:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
 9542:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
 9543:                                         $changes{$itemid} = 1;
 9544:                                     }
 9545:                                 } else {
 9546:                                     $changes{$itemid} = 1;
 9547:                                 }
 9548:                             }
 9549:                         }
 9550:                     }
 9551:                     $allpos[$newpos] = $itemid;
 9552:                 }
 9553:                 if ($imgdeletions{$itemid}) {
 9554:                     $changes{$itemid} = 1;
 9555:                     #FIXME need to obsolete item in RES space
 9556:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
 9557:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
 9558:                                                                  $itemid,$configuserok,$switchserver,
 9559:                                                                  $author_ok);
 9560:                     if ($imgurl) {
 9561:                         $confhash{$itemid}{'image'} = $imgurl;
 9562:                         $changes{$itemid} = 1;
 9563:                     }
 9564:                     if ($error) {
 9565:                         &Apache::lonnet::logthis($error);
 9566:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9567:                     }
 9568:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
 9569:                     $confhash{$itemid}{'image'} =
 9570:                        $domconfig{$action}{$itemid}{'image'};
 9571:                 }
 9572:                 if ($customadds{$i}) {
 9573:                     my $name = $env{'form.ltitools_custom_name_'.$i};
 9574:                     $name =~ s/(`)/'/g;
 9575:                     $name =~ s/^\s+//;
 9576:                     $name =~ s/\s+$//;
 9577:                     my $value = $env{'form.ltitools_custom_value_'.$i};
 9578:                     $value =~ s/(`)/'/g;
 9579:                     $value =~ s/^\s+//;
 9580:                     $value =~ s/\s+$//;
 9581:                     if ($name ne '') {
 9582:                         $confhash{$itemid}{'custom'}{$name} = $value;
 9583:                         $changes{$itemid} = 1;
 9584:                     }
 9585:                 }
 9586:                 my %customdels;
 9587:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
 9588:                 if (@customdeletions) {
 9589:                     $changes{$itemid} = 1;
 9590:                 }
 9591:                 map { $customdels{$_} = 1; } @customdeletions;
 9592:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
 9593:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
 9594:                         unless ($customdels{$key}) {
 9595:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
 9596:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
 9597:                             }
 9598:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
 9599:                                 $changes{$itemid} = 1;
 9600:                             }
 9601:                         }
 9602:                     }
 9603:                 }
 9604:                 unless ($changes{$itemid}) {
 9605:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
 9606:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
 9607:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
 9608:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
 9609:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
 9610:                                         $changes{$itemid} = 1;
 9611:                                         last;
 9612:                                     }
 9613:                                 }
 9614:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
 9615:                                 $changes{$itemid} = 1;
 9616:                             }
 9617:                         }
 9618:                         last if ($changes{$itemid});
 9619:                     }
 9620:                 }
 9621:             }
 9622:         }
 9623:     }
 9624:     if (@allpos > 0) {
 9625:         my $idx = 0;
 9626:         foreach my $itemid (@allpos) {
 9627:             if ($itemid ne '') {
 9628:                 $confhash{$itemid}{'order'} = $idx;
 9629:                 if (ref($domconfig{$action}) eq 'HASH') {
 9630:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
 9631:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
 9632:                             $changes{$itemid} = 1;
 9633:                         }
 9634:                     }
 9635:                 }
 9636:                 $idx ++;
 9637:             }
 9638:         }
 9639:     }
 9640:     my %ltitoolshash = (
 9641:                           $action => { %confhash }
 9642:                        );
 9643:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
 9644:                                              $dom);
 9645:     if ($putresult eq 'ok') {
 9646:         if (keys(%changes) > 0) {
 9647:             my $cachetime = 24*60*60;
 9648:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%confhash,$cachetime);
 9649:             if (ref($lastactref) eq 'HASH') {
 9650:                 $lastactref->{'ltitools'} = 1;
 9651:             }
 9652:             $resulttext = &mt('Changes made:').'<ul>';
 9653:             my %bynum;
 9654:             foreach my $itemid (sort(keys(%changes))) {
 9655:                 my $position = $confhash{$itemid}{'order'};
 9656:                 $bynum{$position} = $itemid;
 9657:             }
 9658:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
 9659:                 my $itemid = $bynum{$pos}; 
 9660:                 if (ref($confhash{$itemid}) ne 'HASH') {
 9661:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
 9662:                 } else {
 9663:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
 9664:                     if ($confhash{$itemid}{'image'}) {
 9665:                         $resulttext .= '&nbsp;'.
 9666:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
 9667:                                        ' alt="'.&mt('Tool Provider icon').'" />';
 9668:                     }
 9669:                     $resulttext .= '</li><ul>';
 9670:                     my $position = $pos + 1;
 9671:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
 9672:                     foreach my $item ('version','msgtype','url','key') {
 9673:                         if ($confhash{$itemid}{$item} ne '') {
 9674:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
 9675:                         }
 9676:                     }
 9677:                     if ($confhash{$itemid}{'secret'} ne '') {
 9678:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
 9679:                         my $num = length($confhash{$itemid}{'secret'});
 9680:                         $resulttext .= ('*'x$num).'</li>';
 9681:                     }
 9682:                     $resulttext .= '<li>'.&mt('Configurable in course:');
 9683:                     my @possconfig = ('label','title','target');
 9684:                     my $numconfig = 0; 
 9685:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') { 
 9686:                         foreach my $item (@possconfig) {
 9687:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
 9688:                                 $numconfig ++;
 9689:                                 $resulttext .= ' '.$lt{'crs'.$item};
 9690:                             }
 9691:                         }
 9692:                     }
 9693:                     if (!$numconfig) {
 9694:                         $resulttext .= &mt('None');
 9695:                     }
 9696:                     $resulttext .= '</li>';
 9697:                     foreach my $item ('passback','roster') {
 9698:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
 9699:                         if ($confhash{$itemid}{$item}) {
 9700:                             $resulttext .= &mt('Yes');
 9701:                         } else {
 9702:                             $resulttext .= &mt('No');
 9703:                         }
 9704:                         $resulttext .= '</li>';
 9705:                     }
 9706:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
 9707:                         my $displaylist;
 9708:                         if ($confhash{$itemid}{'display'}{'target'}) {
 9709:                             $displaylist = &mt('Display target').':&nbsp;'.
 9710:                                            $confhash{$itemid}{'display'}{'target'}.',';
 9711:                         }
 9712:                         foreach my $size ('width','height') { 
 9713:                             if ($confhash{$itemid}{'display'}{$size}) {
 9714:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
 9715:                                                 $confhash{$itemid}{'display'}{$size}.',';
 9716:                             }
 9717:                         }
 9718:                         if ($displaylist) {
 9719:                             $displaylist =~ s/,$//;
 9720:                             $resulttext .= '<li>'.$displaylist.'</li>';
 9721:                         }
 9722:                     } 
 9723:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
 9724:                         my $fieldlist;
 9725:                         foreach my $field (@allfields) {
 9726:                             if ($confhash{$itemid}{'fields'}{$field}) {
 9727:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
 9728:                             }
 9729:                         }
 9730:                         if ($fieldlist) {
 9731:                             $fieldlist =~ s/,$//;
 9732:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
 9733:                         }
 9734:                     }
 9735:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
 9736:                         my $rolemaps;
 9737:                         foreach my $role (@courseroles) {
 9738:                             if ($confhash{$itemid}{'roles'}{$role}) {
 9739:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
 9740:                                              $confhash{$itemid}{'roles'}{$role}.',';
 9741:                             }
 9742:                         }
 9743:                         if ($rolemaps) {
 9744:                             $rolemaps =~ s/,$//; 
 9745:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
 9746:                         }
 9747:                     }
 9748:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
 9749:                         my $customlist;
 9750:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
 9751:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
 9752:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
 9753:                             } 
 9754:                         }
 9755:                         if ($customlist) {
 9756:                             $resulttext .= '<li>'.&mt('Custom items').':'.$customlist.'</li>';
 9757:                         }
 9758:                     } 
 9759:                     $resulttext .= '</ul></li>';
 9760:                 }
 9761:             }
 9762:             $resulttext .= '</ul>';
 9763:         } else {
 9764:             $resulttext = &mt('No changes made.');
 9765:         }
 9766:     } else {
 9767:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
 9768:     }
 9769:     if ($errors) {
 9770:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9771:                        $errors.'</ul>';
 9772:     }
 9773:     return $resulttext;
 9774: }
 9775: 
 9776: sub process_ltitools_image {
 9777:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
 9778:     my $filename = $env{'form.'.$caller.'.filename'};
 9779:     my ($error,$url);
 9780:     my ($width,$height) = (21,21);
 9781:     if ($configuserok eq 'ok') {
 9782:         if ($switchserver) {
 9783:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
 9784:                          $switchserver);
 9785:         } elsif ($author_ok eq 'ok') {
 9786:             my ($result,$imageurl,$madethumb) =
 9787:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9788:                              "ltitools/$itemid/icon",$width,$height);
 9789:             if ($result eq 'ok') {
 9790:                 if ($madethumb) {
 9791:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
 9792:                     my $imagethumb = "$path/tn-".$imagefile;
 9793:                     $url = $imagethumb;
 9794:                 } else {
 9795:                     $url = $imageurl;
 9796:                 }
 9797:             } else {
 9798:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9799:             }
 9800:         } else {
 9801:             $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);
 9802:         }
 9803:     } else {
 9804:         $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);
 9805:     }
 9806:     return ($url,$error);
 9807: }
 9808: 
 9809: sub get_ltitools_id {
 9810:     my ($cdom,$title) = @_;
 9811:     # get lock on ltitools db
 9812:     my $lockhash = {
 9813:                       lock => $env{'user.name'}.
 9814:                               ':'.$env{'user.domain'},
 9815:                    };
 9816:     my $tries = 0;
 9817:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
 9818:     my ($id,$error);
 9819:  
 9820:     while (($gotlock ne 'ok') && ($tries<10)) {
 9821:         $tries ++;
 9822:         sleep (0.1);
 9823:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
 9824:     }
 9825:     if ($gotlock eq 'ok') {
 9826:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
 9827:         if ($currids{'lock'}) {
 9828:             delete($currids{'lock'});
 9829:             if (keys(%currids)) {
 9830:                 my @curr = sort { $a <=> $b } keys(%currids);
 9831:                 if ($curr[-1] =~ /^\d+$/) {
 9832:                     $id = 1 + $curr[-1];
 9833:                 }
 9834:             } else {
 9835:                 $id = 1;
 9836:             }
 9837:             if ($id) {
 9838:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
 9839:                     $error = 'nostore';
 9840:                 }
 9841:             } else {
 9842:                 $error = 'nonumber';
 9843:             }
 9844:         }
 9845:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
 9846:     } else {
 9847:         $error = 'nolock';
 9848:     }
 9849:     return ($id,$error);
 9850: }
 9851: 
 9852: sub modify_autoenroll {
 9853:     my ($dom,$lastactref,%domconfig) = @_;
 9854:     my ($resulttext,%changes);
 9855:     my %currautoenroll;
 9856:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 9857:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 9858:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 9859:         }
 9860:     }
 9861:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 9862:     my %title = ( run => 'Auto-enrollment active',
 9863:                   sender => 'Sender for notification messages',
 9864:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
 9865:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
 9866:     my @offon = ('off','on');
 9867:     my $sender_uname = $env{'form.sender_uname'};
 9868:     my $sender_domain = $env{'form.sender_domain'};
 9869:     if ($sender_domain eq '') {
 9870:         $sender_uname = '';
 9871:     } elsif ($sender_uname eq '') {
 9872:         $sender_domain = '';
 9873:     }
 9874:     my $coowners = $env{'form.autoassign_coowners'};
 9875:     my $failsafe = $env{'form.autoenroll_failsafe'};
 9876:     $failsafe =~ s{^\s+|\s+$}{}g;
 9877:     if ($failsafe =~ /\D/) {
 9878:         undef($failsafe);
 9879:     }
 9880:     my %autoenrollhash =  (
 9881:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 9882:                                        'sender_uname' => $sender_uname,
 9883:                                        'sender_domain' => $sender_domain,
 9884:                                        'co-owners' => $coowners,
 9885:                                        'autofailsafe' => $failsafe,
 9886:                                 }
 9887:                      );
 9888:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 9889:                                              $dom);
 9890:     if ($putresult eq 'ok') {
 9891:         if (exists($currautoenroll{'run'})) {
 9892:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 9893:                  $changes{'run'} = 1;
 9894:              }
 9895:         } elsif ($autorun) {
 9896:             if ($env{'form.autoenroll_run'} ne '1') {
 9897:                  $changes{'run'} = 1;
 9898:             }
 9899:         }
 9900:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 9901:             $changes{'sender'} = 1;
 9902:         }
 9903:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 9904:             $changes{'sender'} = 1;
 9905:         }
 9906:         if ($currautoenroll{'co-owners'} ne '') {
 9907:             if ($currautoenroll{'co-owners'} ne $coowners) {
 9908:                 $changes{'coowners'} = 1;
 9909:             }
 9910:         } elsif ($coowners) {
 9911:             $changes{'coowners'} = 1;
 9912:         }
 9913:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
 9914:             $changes{'autofailsafe'} = 1;
 9915:         }
 9916:         if (keys(%changes) > 0) {
 9917:             $resulttext = &mt('Changes made:').'<ul>';
 9918:             if ($changes{'run'}) {
 9919:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 9920:             }
 9921:             if ($changes{'sender'}) {
 9922:                 if ($sender_uname eq '' || $sender_domain eq '') {
 9923:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 9924:                 } else {
 9925:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 9926:                 }
 9927:             }
 9928:             if ($changes{'coowners'}) {
 9929:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 9930:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9931:                 if (ref($lastactref) eq 'HASH') {
 9932:                     $lastactref->{'domainconfig'} = 1;
 9933:                 }
 9934:             }
 9935:             if ($changes{'autofailsafe'}) {
 9936:                 if ($failsafe ne '') {
 9937:                     $resulttext .= '<li>'.&mt("$title{'failsafe'} set to [_1]",$failsafe).'</li>';
 9938:                 } else {
 9939:                     $resulttext .= '<li>'.&mt("$title{'failsafe'} deleted");
 9940:                 }
 9941:                 &Apache::lonnet::get_domain_defaults($dom,1);
 9942:                 if (ref($lastactref) eq 'HASH') {
 9943:                     $lastactref->{'domdefaults'} = 1;
 9944:                 }
 9945:             }
 9946:             $resulttext .= '</ul>';
 9947:         } else {
 9948:             $resulttext = &mt('No changes made to auto-enrollment settings');
 9949:         }
 9950:     } else {
 9951:         $resulttext = '<span class="LC_error">'.
 9952: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9953:     }
 9954:     return $resulttext;
 9955: }
 9956: 
 9957: sub modify_autoupdate {
 9958:     my ($dom,%domconfig) = @_;
 9959:     my ($resulttext,%currautoupdate,%fields,%changes);
 9960:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 9961:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 9962:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 9963:         }
 9964:     }
 9965:     my @offon = ('off','on');
 9966:     my %title = &Apache::lonlocal::texthash (
 9967:                    run => 'Auto-update:',
 9968:                    classlists => 'Updates to user information in classlists?'
 9969:                 );
 9970:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9971:     my %fieldtitles = &Apache::lonlocal::texthash (
 9972:                         id => 'Student/Employee ID',
 9973:                         permanentemail => 'E-mail address',
 9974:                         lastname => 'Last Name',
 9975:                         firstname => 'First Name',
 9976:                         middlename => 'Middle Name',
 9977:                         generation => 'Generation',
 9978:                       );
 9979:     $othertitle = &mt('All users');
 9980:     if (keys(%{$usertypes}) >  0) {
 9981:         $othertitle = &mt('Other users');
 9982:     }
 9983:     foreach my $key (keys(%env)) {
 9984:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 9985:             my ($usertype,$item) = ($1,$2);
 9986:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 9987:                 if ($usertype eq 'default') {   
 9988:                     push(@{$fields{$1}},$2);
 9989:                 } elsif (ref($types) eq 'ARRAY') {
 9990:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 9991:                         push(@{$fields{$1}},$2);
 9992:                     }
 9993:                 }
 9994:             }
 9995:         }
 9996:     }
 9997:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 9998:     @lockablenames = sort(@lockablenames);
 9999:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
10000:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10001:         if (@changed) {
10002:             $changes{'lockablenames'} = 1;
10003:         }
10004:     } else {
10005:         if (@lockablenames) {
10006:             $changes{'lockablenames'} = 1;
10007:         }
10008:     }
10009:     my %updatehash = (
10010:                       autoupdate => { run => $env{'form.autoupdate_run'},
10011:                                       classlists => $env{'form.classlists'},
10012:                                       fields => {%fields},
10013:                                       lockablenames => \@lockablenames,
10014:                                     }
10015:                      );
10016:     foreach my $key (keys(%currautoupdate)) {
10017:         if (($key eq 'run') || ($key eq 'classlists')) {
10018:             if (exists($updatehash{autoupdate}{$key})) {
10019:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
10020:                     $changes{$key} = 1;
10021:                 }
10022:             }
10023:         } elsif ($key eq 'fields') {
10024:             if (ref($currautoupdate{$key}) eq 'HASH') {
10025:                 foreach my $item (@{$types},'default') {
10026:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
10027:                         my $change = 0;
10028:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
10029:                             if (!exists($fields{$item})) {
10030:                                 $change = 1;
10031:                                 last;
10032:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
10033:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
10034:                                     $change = 1;
10035:                                     last;
10036:                                 }
10037:                             }
10038:                         }
10039:                         if ($change) {
10040:                             push(@{$changes{$key}},$item);
10041:                         }
10042:                     } 
10043:                 }
10044:             }
10045:         } elsif ($key eq 'lockablenames') {
10046:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
10047:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10048:                 if (@changed) {
10049:                     $changes{'lockablenames'} = 1;
10050:                 }
10051:             } else {
10052:                 if (@lockablenames) {
10053:                     $changes{'lockablenames'} = 1;
10054:                 }
10055:             }
10056:         }
10057:     }
10058:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
10059:         if (@lockablenames) {
10060:             $changes{'lockablenames'} = 1;
10061:         }
10062:     }
10063:     foreach my $item (@{$types},'default') {
10064:         if (defined($fields{$item})) {
10065:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
10066:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
10067:                     my $change = 0;
10068:                     if (ref($fields{$item}) eq 'ARRAY') {
10069:                         foreach my $type (@{$fields{$item}}) {
10070:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
10071:                                 $change = 1;
10072:                                 last;
10073:                             }
10074:                         }
10075:                     }
10076:                     if ($change) {
10077:                         push(@{$changes{'fields'}},$item);
10078:                     }
10079:                 } else {
10080:                     push(@{$changes{'fields'}},$item);
10081:                 }
10082:             } else {
10083:                 push(@{$changes{'fields'}},$item);
10084:             }
10085:         }
10086:     }
10087:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
10088:                                              $dom);
10089:     if ($putresult eq 'ok') {
10090:         if (keys(%changes) > 0) {
10091:             $resulttext = &mt('Changes made:').'<ul>';
10092:             foreach my $key (sort(keys(%changes))) {
10093:                 if ($key eq 'lockablenames') {
10094:                     $resulttext .= '<li>';
10095:                     if (@lockablenames) {
10096:                         $usertypes->{'default'} = $othertitle;
10097:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
10098:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
10099:                     } else {
10100:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
10101:                     }
10102:                     $resulttext .= '</li>';
10103:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
10104:                     foreach my $item (@{$changes{$key}}) {
10105:                         my @newvalues;
10106:                         foreach my $type (@{$fields{$item}}) {
10107:                             push(@newvalues,$fieldtitles{$type});
10108:                         }
10109:                         my $newvaluestr;
10110:                         if (@newvalues > 0) {
10111:                             $newvaluestr = join(', ',@newvalues);
10112:                         } else {
10113:                             $newvaluestr = &mt('none');
10114:                         }
10115:                         if ($item eq 'default') {
10116:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
10117:                         } else {
10118:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
10119:                         }
10120:                     }
10121:                 } else {
10122:                     my $newvalue;
10123:                     if ($key eq 'run') {
10124:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
10125:                     } else {
10126:                         $newvalue = $offon[$env{'form.'.$key}];
10127:                     }
10128:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
10129:                 }
10130:             }
10131:             $resulttext .= '</ul>';
10132:         } else {
10133:             $resulttext = &mt('No changes made to autoupdates');
10134:         }
10135:     } else {
10136:         $resulttext = '<span class="LC_error">'.
10137: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10138:     }
10139:     return $resulttext;
10140: }
10141: 
10142: sub modify_autocreate {
10143:     my ($dom,%domconfig) = @_;
10144:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
10145:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
10146:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
10147:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
10148:         }
10149:     }
10150:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
10151:                  req => 'Auto-creation of validated requests for official courses',
10152:                  xmldc => 'Identity of course creator of courses from XML files',
10153:                );
10154:     my @types = ('xml','req');
10155:     foreach my $item (@types) {
10156:         $newvals{$item} = $env{'form.autocreate_'.$item};
10157:         $newvals{$item} =~ s/\D//g;
10158:         $newvals{$item} = 0 if ($newvals{$item} eq '');
10159:     }
10160:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
10161:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
10162:     unless (exists($domcoords{$newvals{'xmldc'}})) {
10163:         $newvals{'xmldc'} = '';
10164:     } 
10165:     %autocreatehash =  (
10166:                         autocreate => { xml => $newvals{'xml'},
10167:                                         req => $newvals{'req'},
10168:                                       }
10169:                        );
10170:     if ($newvals{'xmldc'} ne '') {
10171:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
10172:     }
10173:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
10174:                                              $dom);
10175:     if ($putresult eq 'ok') {
10176:         my @items = @types;
10177:         if ($newvals{'xml'}) {
10178:             push(@items,'xmldc');
10179:         }
10180:         foreach my $item (@items) {
10181:             if (exists($currautocreate{$item})) {
10182:                 if ($currautocreate{$item} ne $newvals{$item}) {
10183:                     $changes{$item} = 1;
10184:                 }
10185:             } elsif ($newvals{$item}) {
10186:                 $changes{$item} = 1;
10187:             }
10188:         }
10189:         if (keys(%changes) > 0) {
10190:             my @offon = ('off','on'); 
10191:             $resulttext = &mt('Changes made:').'<ul>';
10192:             foreach my $item (@types) {
10193:                 if ($changes{$item}) {
10194:                     my $newtxt = $offon[$newvals{$item}];
10195:                     $resulttext .= '<li>'.
10196:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
10197:                                        '<b>','</b>').
10198:                                    '</li>';
10199:                 }
10200:             }
10201:             if ($changes{'xmldc'}) {
10202:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
10203:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
10204:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
10205:             }
10206:             $resulttext .= '</ul>';
10207:         } else {
10208:             $resulttext = &mt('No changes made to auto-creation settings');
10209:         }
10210:     } else {
10211:         $resulttext = '<span class="LC_error">'.
10212:             &mt('An error occurred: [_1]',$putresult).'</span>';
10213:     }
10214:     return $resulttext;
10215: }
10216: 
10217: sub modify_directorysrch {
10218:     my ($dom,%domconfig) = @_;
10219:     my ($resulttext,%changes);
10220:     my %currdirsrch;
10221:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
10222:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
10223:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
10224:         }
10225:     }
10226:     my %title = ( available => 'Institutional directory search available',
10227:                   localonly => 'Other domains can search institution',
10228:                   lcavailable => 'LON-CAPA directory search available',
10229:                   lclocalonly => 'Other domains can search LON-CAPA domain',
10230:                   searchby => 'Search types',
10231:                   searchtypes => 'Search latitude');
10232:     my @offon = ('off','on');
10233:     my @otherdoms = ('Yes','No');
10234: 
10235:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
10236:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
10237:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
10238: 
10239:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10240:     if (keys(%{$usertypes}) == 0) {
10241:         @cansearch = ('default');
10242:     } else {
10243:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
10244:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
10245:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
10246:                     push(@{$changes{'cansearch'}},$type);
10247:                 }
10248:             }
10249:             foreach my $type (@cansearch) {
10250:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
10251:                     push(@{$changes{'cansearch'}},$type);
10252:                 }
10253:             }
10254:         } else {
10255:             push(@{$changes{'cansearch'}},@cansearch);
10256:         }
10257:     }
10258: 
10259:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
10260:         foreach my $by (@{$currdirsrch{'searchby'}}) {
10261:             if (!grep(/^\Q$by\E$/,@searchby)) {
10262:                 push(@{$changes{'searchby'}},$by);
10263:             }
10264:         }
10265:         foreach my $by (@searchby) {
10266:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
10267:                 push(@{$changes{'searchby'}},$by);
10268:             }
10269:         }
10270:     } else {
10271:         push(@{$changes{'searchby'}},@searchby);
10272:     }
10273: 
10274:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
10275:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
10276:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
10277:                 push(@{$changes{'searchtypes'}},$type);
10278:             }
10279:         }
10280:         foreach my $type (@searchtypes) {
10281:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
10282:                 push(@{$changes{'searchtypes'}},$type);
10283:             }
10284:         }
10285:     } else {
10286:         if (exists($currdirsrch{'searchtypes'})) {
10287:             foreach my $type (@searchtypes) {  
10288:                 if ($type ne $currdirsrch{'searchtypes'}) { 
10289:                     push(@{$changes{'searchtypes'}},$type);
10290:                 }
10291:             }
10292:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
10293:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
10294:             }   
10295:         } else {
10296:             push(@{$changes{'searchtypes'}},@searchtypes); 
10297:         }
10298:     }
10299: 
10300:     my %dirsrch_hash =  (
10301:             directorysrch => { available => $env{'form.dirsrch_available'},
10302:                                cansearch => \@cansearch,
10303:                                localonly => $env{'form.dirsrch_instlocalonly'},
10304:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
10305:                                lcavailable => $env{'form.dirsrch_domavailable'},
10306:                                searchby => \@searchby,
10307:                                searchtypes => \@searchtypes,
10308:                              }
10309:             );
10310:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
10311:                                              $dom);
10312:     if ($putresult eq 'ok') {
10313:         if (exists($currdirsrch{'available'})) {
10314:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
10315:                  $changes{'available'} = 1;
10316:              }
10317:         } else {
10318:             if ($env{'form.dirsrch_available'} eq '1') {
10319:                 $changes{'available'} = 1;
10320:             }
10321:         }
10322:         if (exists($currdirsrch{'lcavailable'})) {
10323:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
10324:                 $changes{'lcavailable'} = 1;
10325:             }
10326:         } else {
10327:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
10328:                 $changes{'lcavailable'} = 1;
10329:             }
10330:         }
10331:         if (exists($currdirsrch{'localonly'})) {
10332:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
10333:                 $changes{'localonly'} = 1;
10334:             }
10335:         } else {
10336:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
10337:                 $changes{'localonly'} = 1;
10338:             }
10339:         }
10340:         if (exists($currdirsrch{'lclocalonly'})) {
10341:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
10342:                 $changes{'lclocalonly'} = 1;
10343:             }
10344:         } else {
10345:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
10346:                 $changes{'lclocalonly'} = 1;
10347:             }
10348:         }
10349:         if (keys(%changes) > 0) {
10350:             $resulttext = &mt('Changes made:').'<ul>';
10351:             if ($changes{'available'}) {
10352:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
10353:             }
10354:             if ($changes{'lcavailable'}) {
10355:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
10356:             }
10357:             if ($changes{'localonly'}) {
10358:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
10359:             }
10360:             if ($changes{'lclocalonly'}) {
10361:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
10362:             }
10363:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
10364:                 my $chgtext;
10365:                 if (ref($usertypes) eq 'HASH') {
10366:                     if (keys(%{$usertypes}) > 0) {
10367:                         foreach my $type (@{$types}) {
10368:                             if (grep(/^\Q$type\E$/,@cansearch)) {
10369:                                 $chgtext .= $usertypes->{$type}.'; ';
10370:                             }
10371:                         }
10372:                         if (grep(/^default$/,@cansearch)) {
10373:                             $chgtext .= $othertitle;
10374:                         } else {
10375:                             $chgtext =~ s/\; $//;
10376:                         }
10377:                         $resulttext .=
10378:                             '<li>'.
10379:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
10380:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
10381:                             '</li>';
10382:                     }
10383:                 }
10384:             }
10385:             if (ref($changes{'searchby'}) eq 'ARRAY') {
10386:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
10387:                 my $chgtext;
10388:                 foreach my $type (@{$titleorder}) {
10389:                     if (grep(/^\Q$type\E$/,@searchby)) {
10390:                         if (defined($searchtitles->{$type})) {
10391:                             $chgtext .= $searchtitles->{$type}.'; ';
10392:                         }
10393:                     }
10394:                 }
10395:                 $chgtext =~ s/\; $//;
10396:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
10397:             }
10398:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
10399:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
10400:                 my $chgtext;
10401:                 foreach my $type (@{$srchtypeorder}) {
10402:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
10403:                         if (defined($srchtypes_desc->{$type})) {
10404:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
10405:                         }
10406:                     }
10407:                 }
10408:                 $chgtext =~ s/\; $//;
10409:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
10410:             }
10411:             $resulttext .= '</ul>';
10412:         } else {
10413:             $resulttext = &mt('No changes made to directory search settings');
10414:         }
10415:     } else {
10416:         $resulttext = '<span class="LC_error">'.
10417:                       &mt('An error occurred: [_1]',$putresult).'</span>';
10418:     }
10419:     return $resulttext;
10420: }
10421: 
10422: sub modify_contacts {
10423:     my ($dom,$lastactref,%domconfig) = @_;
10424:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
10425:     if (ref($domconfig{'contacts'}) eq 'HASH') {
10426:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
10427:             $currsetting{$key} = $domconfig{'contacts'}{$key};
10428:         }
10429:     }
10430:     my (%others,%to,%bcc,%includestr,%includeloc);
10431:     my @contacts = ('supportemail','adminemail');
10432:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
10433:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail');
10434:     my @toggles = ('reporterrors','reportupdates');
10435:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
10436:     foreach my $type (@mailings) {
10437:         @{$newsetting{$type}} = 
10438:             &Apache::loncommon::get_env_multiple('form.'.$type);
10439:         foreach my $item (@contacts) {
10440:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
10441:                 $contacts_hash{contacts}{$type}{$item} = 1;
10442:             } else {
10443:                 $contacts_hash{contacts}{$type}{$item} = 0;
10444:             }
10445:         }
10446:         $others{$type} = $env{'form.'.$type.'_others'};
10447:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
10448:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10449:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
10450:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
10451:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
10452:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
10453:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
10454:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
10455:             }
10456:         }
10457:     }
10458:     foreach my $item (@contacts) {
10459:         $to{$item} = $env{'form.'.$item};
10460:         $contacts_hash{'contacts'}{$item} = $to{$item};
10461:     }
10462:     foreach my $item (@toggles) {
10463:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
10464:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
10465:         }
10466:     }
10467:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
10468:         foreach my $field (@{$fields}) {
10469:             if (ref($possoptions->{$field}) eq 'ARRAY') {
10470:                 my $value = $env{'form.helpform_'.$field};
10471:                 $value =~ s/^\s+|\s+$//g;
10472:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
10473:                     $contacts_hash{contacts}{'helpform'}{$field} = $value;
10474:                     if ($field eq 'screenshot') {
10475:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
10476:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
10477:                             $contacts_hash{contacts}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
10478:                         }
10479:                     }
10480:                 }
10481:             }
10482:         }
10483:     }
10484:     if (keys(%currsetting) > 0) {
10485:         foreach my $item (@contacts) {
10486:             if ($to{$item} ne $currsetting{$item}) {
10487:                 $changes{$item} = 1;
10488:             }
10489:         }
10490:         foreach my $type (@mailings) {
10491:             foreach my $item (@contacts) {
10492:                 if (ref($currsetting{$type}) eq 'HASH') {
10493:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
10494:                         push(@{$changes{$type}},$item);
10495:                     }
10496:                 } else {
10497:                     push(@{$changes{$type}},@{$newsetting{$type}});
10498:                 }
10499:             }
10500:             if ($others{$type} ne $currsetting{$type}{'others'}) {
10501:                 push(@{$changes{$type}},'others');
10502:             }
10503:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10504:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
10505:                     push(@{$changes{$type}},'bcc'); 
10506:                 }
10507:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
10508:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
10509:                     push(@{$changes{$type}},'include');
10510:                 }
10511:             }
10512:         }
10513:         if (ref($fields) eq 'ARRAY') {
10514:             if (ref($currsetting{'helpform'}) eq 'HASH') {
10515:                 foreach my $field (@{$fields}) {
10516:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
10517:                         push(@{$changes{'helpform'}},$field);
10518:                     }
10519:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10520:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
10521:                             push(@{$changes{'helpform'}},'maxsize');
10522:                         }
10523:                     }
10524:                 }
10525:             } else {
10526:                 foreach my $field (@{$fields}) {
10527:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10528:                         push(@{$changes{'helpform'}},$field);
10529:                     }
10530:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10531:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10532:                             push(@{$changes{'helpform'}},'maxsize');
10533:                         }
10534:                     }
10535:                 }
10536:             }
10537:         }
10538:     } else {
10539:         my %default;
10540:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
10541:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
10542:         $default{'errormail'} = 'adminemail';
10543:         $default{'packagesmail'} = 'adminemail';
10544:         $default{'helpdeskmail'} = 'supportemail';
10545:         $default{'otherdomsmail'} = 'supportemail';
10546:         $default{'lonstatusmail'} = 'adminemail';
10547:         $default{'requestsmail'} = 'adminemail';
10548:         $default{'updatesmail'} = 'adminemail';
10549:         foreach my $item (@contacts) {
10550:            if ($to{$item} ne $default{$item}) {
10551:                $changes{$item} = 1;
10552:            }
10553:         }
10554:         foreach my $type (@mailings) {
10555:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
10556:                 push(@{$changes{$type}},@{$newsetting{$type}});
10557:             }
10558:             if ($others{$type} ne '') {
10559:                 push(@{$changes{$type}},'others');
10560:             }
10561:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10562:                 if ($bcc{$type} ne '') {
10563:                     push(@{$changes{$type}},'bcc');
10564:                 }
10565:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
10566:                     push(@{$changes{$type}},'include');
10567:                 }
10568:             }
10569:         }
10570:         if (ref($fields) eq 'ARRAY') {
10571:             foreach my $field (@{$fields}) {
10572:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10573:                     push(@{$changes{'helpform'}},$field);
10574:                 }
10575:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10576:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10577:                         push(@{$changes{'helpform'}},'maxsize');
10578:                     }
10579:                 }
10580:             }
10581:         }
10582:     }
10583:     foreach my $item (@toggles) {
10584:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
10585:             $changes{$item} = 1;
10586:         } elsif ((!$env{'form.'.$item}) &&
10587:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
10588:             $changes{$item} = 1;
10589:         }
10590:     }
10591:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
10592:                                              $dom);
10593:     if ($putresult eq 'ok') {
10594:         if (keys(%changes) > 0) {
10595:             &Apache::loncommon::devalidate_domconfig_cache($dom);
10596:             if (ref($lastactref) eq 'HASH') {
10597:                 $lastactref->{'domainconfig'} = 1;
10598:             }
10599:             my ($titles,$short_titles)  = &contact_titles();
10600:             $resulttext = &mt('Changes made:').'<ul>';
10601:             foreach my $item (@contacts) {
10602:                 if ($changes{$item}) {
10603:                     $resulttext .= '<li>'.$titles->{$item}.
10604:                                     &mt(' set to: ').
10605:                                     '<span class="LC_cusr_emph">'.
10606:                                     $to{$item}.'</span></li>';
10607:                 }
10608:             }
10609:             foreach my $type (@mailings) {
10610:                 if (ref($changes{$type}) eq 'ARRAY') {
10611:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10612:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
10613:                     } else {
10614:                         $resulttext .= '<li>'.$titles->{$type}.': ';
10615:                     }
10616:                     my @text;
10617:                     foreach my $item (@{$newsetting{$type}}) {
10618:                         push(@text,$short_titles->{$item});
10619:                     }
10620:                     if ($others{$type} ne '') {
10621:                         push(@text,$others{$type});
10622:                     }
10623:                     if (@text) {
10624:                         $resulttext .= '<span class="LC_cusr_emph">'.
10625:                                        join(', ',@text).'</span>';
10626:                     }
10627:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10628:                         if ($bcc{$type} ne '') {
10629:                             my $bcctext;
10630:                             if (@text) {
10631:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
10632:                             } else {
10633:                                 $bcctext = '(Bcc)';
10634:                             }
10635:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
10636:                         } elsif (!@text) {
10637:                             $resulttext .= &mt('No one');
10638:                         }   
10639:                         if ($includestr{$type} ne '') {
10640:                             if ($includeloc{$type} eq 'b') {
10641:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
10642:                             } elsif ($includeloc{$type} eq 's') {
10643:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
10644:                             }
10645:                         }
10646:                     } elsif (!@text) {
10647:                         $resulttext .= &mt('No recipients');
10648:                     }
10649:                     $resulttext .= '</li>';
10650:                 }
10651:             }
10652:             my @offon = ('off','on');
10653:             if ($changes{'reporterrors'}) {
10654:                 $resulttext .= '<li>'.
10655:                                &mt('E-mail error reports to [_1] set to "'.
10656:                                    $offon[$env{'form.reporterrors'}].'".',
10657:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10658:                                        &mt('LON-CAPA core group - MSU'),600,500)).
10659:                                '</li>';
10660:             }
10661:             if ($changes{'reportupdates'}) {
10662:                 $resulttext .= '<li>'.
10663:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
10664:                                     $offon[$env{'form.reportupdates'}].'".',
10665:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10666:                                         &mt('LON-CAPA core group - MSU'),600,500)).
10667:                                 '</li>';
10668:             }
10669:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
10670:                 my (@optional,@required,@unused,$maxsizechg);
10671:                 foreach my $field (@{$changes{'helpform'}}) {
10672:                     if ($field eq 'maxsize') {
10673:                         $maxsizechg = 1;
10674:                         next;
10675:                     }
10676:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
10677:                         push(@optional,$field);
10678:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
10679:                         push(@unused,$field);
10680:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
10681:                         push(@required,$field);
10682:                     }
10683:                 }
10684:                 if (@optional) {
10685:                     $resulttext .= '<li>'.
10686:                                    &mt('Help form fields changed to "Optional": [_1].',
10687:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
10688:                                    '</li>';
10689:                 }
10690:                 if (@required) {
10691:                     $resulttext .= '<li>'.
10692:                                    &mt('Help form fields changed to "Required": [_1].',
10693:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
10694:                                    '</li>';
10695:                 }
10696:                 if (@unused) {
10697:                     $resulttext .= '<li>'.
10698:                                    &mt('Help form fields changed to "Not shown": [_1].',
10699:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
10700:                                    '</li>';
10701:                 }
10702:                 if ($maxsizechg) {
10703:                     $resulttext .= '<li>'.
10704:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
10705:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
10706:                                    '</li>';
10707: 
10708:                 }
10709:             }
10710:             $resulttext .= '</ul>';
10711:         } else {
10712:             $resulttext = &mt('No changes made to contacts and form settings');
10713:         }
10714:     } else {
10715:         $resulttext = '<span class="LC_error">'.
10716:             &mt('An error occurred: [_1].',$putresult).'</span>';
10717:     }
10718:     return $resulttext;
10719: }
10720: 
10721: sub modify_usercreation {
10722:     my ($dom,%domconfig) = @_;
10723:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
10724:     my $warningmsg;
10725:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
10726:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
10727:             if ($key eq 'cancreate') {
10728:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
10729:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
10730:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
10731:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
10732:                             ($item eq 'recaptchaversion')) {
10733:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10734:                         } else {
10735:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10736:                         }
10737:                     }
10738:                 }
10739:             } elsif ($key eq 'email_rule') {
10740:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
10741:             } else {
10742:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
10743:             }
10744:         }
10745:     }
10746:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
10747:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
10748:     my @contexts = ('author','course','requestcrs');
10749:     foreach my $item(@contexts) {
10750:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
10751:     }
10752:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
10753:         foreach my $item (@contexts) {
10754:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
10755:                 push(@{$changes{'cancreate'}},$item);
10756:             }
10757:         }
10758:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
10759:         foreach my $item (@contexts) {
10760:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
10761:                 if ($cancreate{$item} ne 'any') {
10762:                     push(@{$changes{'cancreate'}},$item);
10763:                 }
10764:             } else {
10765:                 if ($cancreate{$item} ne 'none') {
10766:                     push(@{$changes{'cancreate'}},$item);
10767:                 }
10768:             }
10769:         }
10770:     } else {
10771:         foreach my $item (@contexts)  {
10772:             push(@{$changes{'cancreate'}},$item);
10773:         }
10774:     }
10775: 
10776:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
10777:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
10778:             if (!grep(/^\Q$type\E$/,@username_rule)) {
10779:                 push(@{$changes{'username_rule'}},$type);
10780:             }
10781:         }
10782:         foreach my $type (@username_rule) {
10783:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
10784:                 push(@{$changes{'username_rule'}},$type);
10785:             }
10786:         }
10787:     } else {
10788:         push(@{$changes{'username_rule'}},@username_rule);
10789:     }
10790: 
10791:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
10792:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
10793:             if (!grep(/^\Q$type\E$/,@id_rule)) {
10794:                 push(@{$changes{'id_rule'}},$type);
10795:             }
10796:         }
10797:         foreach my $type (@id_rule) {
10798:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
10799:                 push(@{$changes{'id_rule'}},$type);
10800:             }
10801:         }
10802:     } else {
10803:         push(@{$changes{'id_rule'}},@id_rule);
10804:     }
10805: 
10806:     my @authen_contexts = ('author','course','domain');
10807:     my @authtypes = ('int','krb4','krb5','loc');
10808:     my %authhash;
10809:     foreach my $item (@authen_contexts) {
10810:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
10811:         foreach my $auth (@authtypes) {
10812:             if (grep(/^\Q$auth\E$/,@authallowed)) {
10813:                 $authhash{$item}{$auth} = 1;
10814:             } else {
10815:                 $authhash{$item}{$auth} = 0;
10816:             }
10817:         }
10818:     }
10819:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
10820:         foreach my $item (@authen_contexts) {
10821:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
10822:                 foreach my $auth (@authtypes) {
10823:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
10824:                         push(@{$changes{'authtypes'}},$item);
10825:                         last;
10826:                     }
10827:                 }
10828:             }
10829:         }
10830:     } else {
10831:         foreach my $item (@authen_contexts) {
10832:             push(@{$changes{'authtypes'}},$item);
10833:         }
10834:     }
10835: 
10836:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
10837:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
10838:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
10839:     $save_usercreate{'id_rule'} = \@id_rule;
10840:     $save_usercreate{'username_rule'} = \@username_rule,
10841:     $save_usercreate{'authtypes'} = \%authhash;
10842: 
10843:     my %usercreation_hash =  (
10844:         usercreation     => \%save_usercreate,
10845:     );
10846: 
10847:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
10848:                                              $dom);
10849: 
10850:     if ($putresult eq 'ok') {
10851:         if (keys(%changes) > 0) {
10852:             $resulttext = &mt('Changes made:').'<ul>';
10853:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
10854:                 my %lt = &usercreation_types();
10855:                 foreach my $type (@{$changes{'cancreate'}}) {
10856:                     my $chgtext = $lt{$type}.', ';
10857:                     if ($cancreate{$type} eq 'none') {
10858:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
10859:                     } elsif ($cancreate{$type} eq 'any') {
10860:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
10861:                     } elsif ($cancreate{$type} eq 'official') {
10862:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
10863:                     } elsif ($cancreate{$type} eq 'unofficial') {
10864:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
10865:                     }
10866:                     $resulttext .= '<li>'.$chgtext.'</li>';
10867:                 }
10868:             }
10869:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
10870:                 my ($rules,$ruleorder) = 
10871:                     &Apache::lonnet::inst_userrules($dom,'username');
10872:                 my $chgtext = '<ul>';
10873:                 foreach my $type (@username_rule) {
10874:                     if (ref($rules->{$type}) eq 'HASH') {
10875:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
10876:                     }
10877:                 }
10878:                 $chgtext .= '</ul>';
10879:                 if (@username_rule > 0) {
10880:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
10881:                 } else {
10882:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
10883:                 }
10884:             }
10885:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
10886:                 my ($idrules,$idruleorder) = 
10887:                     &Apache::lonnet::inst_userrules($dom,'id');
10888:                 my $chgtext = '<ul>';
10889:                 foreach my $type (@id_rule) {
10890:                     if (ref($idrules->{$type}) eq 'HASH') {
10891:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
10892:                     }
10893:                 }
10894:                 $chgtext .= '</ul>';
10895:                 if (@id_rule > 0) {
10896:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
10897:                 } else {
10898:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
10899:                 }
10900:             }
10901:             my %authname = &authtype_names();
10902:             my %context_title = &context_names();
10903:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
10904:                 my $chgtext = '<ul>';
10905:                 foreach my $type (@{$changes{'authtypes'}}) {
10906:                     my @allowed;
10907:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
10908:                     foreach my $auth (@authtypes) {
10909:                         if ($authhash{$type}{$auth}) {
10910:                             push(@allowed,$authname{$auth});
10911:                         }
10912:                     }
10913:                     if (@allowed > 0) {
10914:                         $chgtext .= join(', ',@allowed).'</li>';
10915:                     } else {
10916:                         $chgtext .= &mt('none').'</li>';
10917:                     }
10918:                 }
10919:                 $chgtext .= '</ul>';
10920:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
10921:                 $resulttext .= '</li>';
10922:             }
10923:             $resulttext .= '</ul>';
10924:         } else {
10925:             $resulttext = &mt('No changes made to user creation settings');
10926:         }
10927:     } else {
10928:         $resulttext = '<span class="LC_error">'.
10929:             &mt('An error occurred: [_1]',$putresult).'</span>';
10930:     }
10931:     if ($warningmsg ne '') {
10932:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
10933:     }
10934:     return $resulttext;
10935: }
10936: 
10937: sub modify_selfcreation {
10938:     my ($dom,%domconfig) = @_;
10939:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
10940:     my (%save_usercreate,%save_usermodify);
10941:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10942:     if (ref($types) eq 'ARRAY') {
10943:         $usertypes->{'default'} = $othertitle;
10944:         push(@{$types},'default');
10945:     }
10946: #
10947: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
10948: #
10949:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
10950:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
10951:             if ($key eq 'cancreate') {
10952:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
10953:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
10954:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
10955:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
10956:                             ($item eq 'recaptchaversion') ||
10957:                             ($item eq 'emailusername') || ($item eq 'notify') ||
10958:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
10959:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10960:                         } else {
10961:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10962:                         }
10963:                     }
10964:                 }
10965:             } elsif ($key eq 'email_rule') {
10966:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
10967:             } else {
10968:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
10969:             }
10970:         }
10971:     }
10972: #
10973: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
10974: #
10975:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
10976:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
10977:             if ($key eq 'selfcreate') {
10978:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
10979:             } else {
10980:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
10981:             }
10982:         }
10983:     }
10984: 
10985:     my @contexts = ('selfcreate');
10986:     @{$cancreate{'selfcreate'}} = ();
10987:     %{$cancreate{'emailusername'}} = ();
10988:     @{$cancreate{'statustocreate'}} = ();
10989:     %{$cancreate{'selfcreateprocessing'}} = ();
10990:     %{$cancreate{'shibenv'}} = ();
10991:     my %selfcreatetypes = (
10992:                              sso   => 'users authenticated by institutional single sign on',
10993:                              login => 'users authenticated by institutional log-in',
10994:                              email => 'users who provide a valid e-mail address for use as username',
10995:                           );
10996: #
10997: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
10998: # is permitted.
10999: #
11000: 
11001:     my @statuses;
11002:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
11003:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
11004:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
11005:         }
11006:     }
11007:     push(@statuses,'default');
11008: 
11009:     foreach my $item ('login','sso','email') {
11010:         if ($item eq 'email') {
11011:             if ($env{'form.cancreate_email'}) {
11012:                 push(@{$cancreate{'selfcreate'}},'email');
11013:                 push(@contexts,'selfcreateprocessing');
11014:                 foreach my $type (@statuses) {
11015:                     if ($type eq 'default') {
11016:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
11017:                     } else { 
11018:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
11019:                     }
11020:                 }
11021:             }
11022:         } else {
11023:             if ($env{'form.cancreate_'.$item}) {
11024:                 push(@{$cancreate{'selfcreate'}},$item);
11025:             }
11026:         }
11027:     }
11028:     my (@email_rule,%userinfo,%savecaptcha);
11029:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
11030: #
11031: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
11032: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
11033: #
11034: 
11035:     if ($env{'form.cancreate_email'}) {
11036:         push(@contexts,'emailusername');
11037:         if (ref($types) eq 'ARRAY') {
11038:             foreach my $type (@{$types}) {
11039:                 if (ref($infofields) eq 'ARRAY') {
11040:                     foreach my $field (@{$infofields}) {
11041:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
11042:                             $cancreate{'emailusername'}{$type}{$field} = $1;
11043:                         }
11044:                     }
11045:                 }
11046:             }
11047:         }
11048: #
11049: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
11050: # queued requests for self-creation of account using e-mail address as username
11051: #
11052: 
11053:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
11054:         @approvalnotify = sort(@approvalnotify);
11055:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
11056:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11057:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
11058:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
11059:                     push(@{$changes{'cancreate'}},'notify');
11060:                 }
11061:             } else {
11062:                 if ($cancreate{'notify'}{'approval'}) {
11063:                     push(@{$changes{'cancreate'}},'notify');
11064:                 }
11065:             }
11066:         } elsif ($cancreate{'notify'}{'approval'}) {
11067:             push(@{$changes{'cancreate'}},'notify');
11068:         }
11069: 
11070: #
11071: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
11072: #
11073:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
11074:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
11075:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
11076:             if (@{$curr_usercreation{'email_rule'}} > 0) {
11077:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
11078:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
11079:                         push(@{$changes{'email_rule'}},$type);
11080:                     }
11081:                 }
11082:             }
11083:             if (@email_rule > 0) {
11084:                 foreach my $type (@email_rule) {
11085:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
11086:                         push(@{$changes{'email_rule'}},$type);
11087:                     }
11088:                 }
11089:             }
11090:         } elsif (@email_rule > 0) {
11091:             push(@{$changes{'email_rule'}},@email_rule);
11092:         }
11093:     }
11094: #  
11095: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
11096: # institutional log-in.
11097: #
11098:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
11099:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11100:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
11101:                ($domdefaults{'auth_def'} eq 'localauth'))) {
11102:             $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.').' '.
11103:                           &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.');
11104:         }
11105:     }
11106:     my @fields = ('lastname','firstname','middlename','generation',
11107:                   'permanentemail','id');
11108:     my @shibfields = (@fields,'inststatus');
11109:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11110: #
11111: # Where usernames may created for institutional log-in and/or institutional single sign on:
11112: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
11113: # may self-create accounts 
11114: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
11115: # which the user may supply, if institutional data is unavailable.
11116: #
11117:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
11118:         if (ref($types) eq 'ARRAY') {
11119:             if (@{$types} > 1) {
11120:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
11121:                 push(@contexts,'statustocreate');
11122:             } else {
11123:                 undef($cancreate{'statustocreate'});
11124:             } 
11125:             foreach my $type (@{$types}) {
11126:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
11127:                 foreach my $field (@fields) {
11128:                     if (grep(/^\Q$field\E$/,@modifiable)) {
11129:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
11130:                     } else {
11131:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
11132:                     }
11133:                 }
11134:             }
11135:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
11136:                 foreach my $type (@{$types}) {
11137:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
11138:                         foreach my $field (@fields) {
11139:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
11140:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
11141:                                 push(@{$changes{'selfcreate'}},$type);
11142:                                 last;
11143:                             }
11144:                         }
11145:                     }
11146:                 }
11147:             } else {
11148:                 foreach my $type (@{$types}) {
11149:                     push(@{$changes{'selfcreate'}},$type);
11150:                 }
11151:             }
11152:         }
11153:         foreach my $field (@shibfields) {
11154:             if ($env{'form.shibenv_'.$field} ne '') {
11155:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
11156:             }
11157:         }
11158:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11159:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
11160:                 foreach my $field (@shibfields) {
11161:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
11162:                         push(@{$changes{'cancreate'}},'shibenv');
11163:                     }
11164:                 }
11165:             } else {
11166:                 foreach my $field (@shibfields) {
11167:                     if ($env{'form.shibenv_'.$field}) {
11168:                         push(@{$changes{'cancreate'}},'shibenv');
11169:                         last;
11170:                     }
11171:                 }
11172:             }
11173:         }
11174:     }
11175:     foreach my $item (@contexts) {
11176:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
11177:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
11178:                 if (ref($cancreate{$item}) eq 'ARRAY') {
11179:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
11180:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11181:                             push(@{$changes{'cancreate'}},$item);
11182:                         }
11183:                     }
11184:                 }
11185:             }
11186:             if (ref($cancreate{$item}) eq 'ARRAY') {
11187:                 foreach my $type (@{$cancreate{$item}}) {
11188:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
11189:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11190:                             push(@{$changes{'cancreate'}},$item);
11191:                         }
11192:                     }
11193:                 }
11194:             }
11195:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
11196:             if (ref($cancreate{$item}) eq 'HASH') {
11197:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
11198:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
11199:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
11200:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
11201:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11202:                                     push(@{$changes{'cancreate'}},$item);
11203:                                 }
11204:                             }
11205:                         }
11206:                     } elsif ($item eq 'selfcreateprocessing') {
11207:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
11208:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11209:                                 push(@{$changes{'cancreate'}},$item);
11210:                             }
11211:                         }
11212:                     } else {
11213:                         if (!$cancreate{$item}{$curr}) {
11214:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11215:                                 push(@{$changes{'cancreate'}},$item);
11216:                             }
11217:                         }
11218:                     }
11219:                 }
11220:                 foreach my $field (keys(%{$cancreate{$item}})) {
11221:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
11222:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
11223:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
11224:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
11225:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11226:                                         push(@{$changes{'cancreate'}},$item);
11227:                                     }
11228:                                 }
11229:                             } else {
11230:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11231:                                     push(@{$changes{'cancreate'}},$item);
11232:                                 }
11233:                             }
11234:                         }
11235:                     } elsif ($item eq 'selfcreateprocessing') {
11236:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
11237:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11238:                                 push(@{$changes{'cancreate'}},$item);
11239:                             }
11240:                         }
11241:                     } else {
11242:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
11243:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11244:                                 push(@{$changes{'cancreate'}},$item);
11245:                             }
11246:                         }
11247:                     }
11248:                 }
11249:             }
11250:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
11251:             if (ref($cancreate{$item}) eq 'ARRAY') {
11252:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
11253:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11254:                         push(@{$changes{'cancreate'}},$item);
11255:                     }
11256:                 }
11257:             } elsif (ref($cancreate{$item}) eq 'HASH') {
11258:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
11259:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11260:                         push(@{$changes{'cancreate'}},$item);
11261:                     }
11262:                 }
11263:             }
11264:         } elsif ($item eq 'emailusername') {
11265:             if (ref($cancreate{$item}) eq 'HASH') {
11266:                 foreach my $type (keys(%{$cancreate{$item}})) {
11267:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
11268:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
11269:                             if ($cancreate{$item}{$type}{$field}) {
11270:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11271:                                     push(@{$changes{'cancreate'}},$item);
11272:                                 }
11273:                                 last;
11274:                             }
11275:                         }
11276:                     }
11277:                 }
11278:             }
11279:         }
11280:     }
11281: #
11282: # Populate %save_usercreate hash with updates to self-creation configuration.
11283: #
11284:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
11285:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
11286:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
11287:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
11288:     if (ref($cancreate{'notify'}) eq 'HASH') {
11289:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
11290:     }
11291:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
11292:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
11293:     }
11294:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11295:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
11296:     }
11297:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
11298:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
11299:     }
11300:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
11301:     $save_usercreate{'emailrule'} = \@email_rule;
11302: 
11303:     my %userconfig_hash = (
11304:             usercreation     => \%save_usercreate,
11305:             usermodification => \%save_usermodify,
11306:     );
11307:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
11308:                                              $dom);
11309: #
11310: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
11311: #
11312:     if ($putresult eq 'ok') {
11313:         if (keys(%changes) > 0) {
11314:             $resulttext = &mt('Changes made:').'<ul>';
11315:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11316:                 my %lt = &selfcreation_types();
11317:                 foreach my $type (@{$changes{'cancreate'}}) {
11318:                     my $chgtext;
11319:                     if ($type eq 'selfcreate') {
11320:                         if (@{$cancreate{$type}} == 0) {
11321:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
11322:                         } else {
11323:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
11324:                                         '<ul>';
11325:                             foreach my $case (@{$cancreate{$type}}) {
11326:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
11327:                             }
11328:                             $chgtext .= '</ul>';
11329:                             if (ref($cancreate{$type}) eq 'ARRAY') {
11330:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
11331:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11332:                                         if (@{$cancreate{'statustocreate'}} == 0) {
11333:                                             $chgtext .= '<br />'.
11334:                                                         '<span class="LC_warning">'.
11335:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
11336:                                                         '</span>';
11337:                                         }
11338:                                     }
11339:                                 }
11340:                             }
11341:                         }
11342:                     } elsif ($type eq 'shibenv') {
11343:                         if (keys(%{$cancreate{$type}}) == 0) {
11344:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
11345:                         } else {
11346:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
11347:                                         '<ul>';
11348:                             foreach my $field (@shibfields) {
11349:                                 next if ($cancreate{$type}{$field} eq '');
11350:                                 if ($field eq 'inststatus') {
11351:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
11352:                                 } else {
11353:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
11354:                                 }
11355:                             }
11356:                             $chgtext .= '</ul>';
11357:                         }  
11358:                     } elsif ($type eq 'statustocreate') {
11359:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
11360:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
11361:                             if (@{$cancreate{'selfcreate'}} > 0) {
11362:                                 if (@{$cancreate{'statustocreate'}} == 0) {
11363:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
11364:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
11365:                                         $chgtext .= '<br />'.
11366:                                                     '<span class="LC_warning">'.
11367:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
11368:                                                     '</span>';
11369:                                     }
11370:                                 } elsif (ref($usertypes) eq 'HASH') {
11371:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
11372:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
11373:                                     } else {
11374:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
11375:                                     }
11376:                                     $chgtext .= '<ul>';
11377:                                     foreach my $case (@{$cancreate{$type}}) {
11378:                                         if ($case eq 'default') {
11379:                                             $chgtext .= '<li>'.$othertitle.'</li>';
11380:                                         } else {
11381:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
11382:                                         }
11383:                                     }
11384:                                     $chgtext .= '</ul>';
11385:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
11386:                                         $chgtext .= '<br /><span class="LC_warning">'.
11387:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
11388:                                                     '</span>';
11389:                                     }
11390:                                 }
11391:                             } else {
11392:                                 if (@{$cancreate{$type}} == 0) {
11393:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
11394:                                 } else {
11395:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
11396:                                 }
11397:                             }
11398:                         }
11399:                     } elsif ($type eq 'selfcreateprocessing') {
11400:                         my %choices = &Apache::lonlocal::texthash (
11401:                                                                     automatic => 'Automatic approval',
11402:                                                                     approval  => 'Queued for approval',
11403:                                                                   );
11404:                         if (@statuses > 1) {
11405:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
11406:                                         '<ul>';
11407:                            foreach my $type (@statuses) {
11408:                                if ($type eq 'default') {
11409:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
11410:                                } else {
11411:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
11412:                                }
11413:                            }
11414:                            $chgtext .= '</ul>';
11415:                         } else {
11416:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
11417:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
11418:                         }
11419:                     } elsif ($type eq 'captcha') {
11420:                         if ($savecaptcha{$type} eq 'notused') {
11421:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
11422:                         } else {
11423:                             my %captchas = &captcha_phrases();
11424:                             if ($captchas{$savecaptcha{$type}}) {
11425:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
11426:                             } else {
11427:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
11428:                             }
11429:                         }
11430:                     } elsif ($type eq 'recaptchakeys') {
11431:                         my ($privkey,$pubkey);
11432:                         if (ref($savecaptcha{$type}) eq 'HASH') {
11433:                             $pubkey = $savecaptcha{$type}{'public'};
11434:                             $privkey = $savecaptcha{$type}{'private'};
11435:                         }
11436:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
11437:                         if (!$pubkey) {
11438:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
11439:                         } else {
11440:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
11441:                         }
11442:                         if (!$privkey) {
11443:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
11444:                         } else {
11445:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
11446:                         }
11447:                         $chgtext .= '</ul>';
11448:                     } elsif ($type eq 'recaptchaversion') {
11449:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
11450:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
11451:                         }
11452:                     } elsif ($type eq 'emailusername') {
11453:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
11454:                             if (ref($types) eq 'ARRAY') {
11455:                                 foreach my $type (@{$types}) {
11456:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
11457:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
11458:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
11459:                                                     '<ul>';
11460:                                             foreach my $field (@{$infofields}) {
11461:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
11462:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
11463:                                                 }
11464:                                             }
11465:                                             $chgtext .= '</ul>';
11466:                                         } else {
11467:                                             $chgtext .= &mt('When self creating account with e-mail as username, no information besides e-mail address will be provided by [_1].',"'$usertypes->{$type}'").'<br />';
11468:                                         }
11469:                                     } else {
11470:                                         $chgtext .= &mt('When self creating account with e-mail as username, no information besides e-mail address will be provided by [_1].',"'$usertypes->{$type}'").'<br />';
11471:                                     }
11472:                                 }
11473:                             }
11474:                         }
11475:                     } elsif ($type eq 'notify') {
11476:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
11477:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
11478:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
11479:                                 if ($cancreate{'notify'}{'approval'}) {
11480:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
11481:                                 }
11482:                             }
11483:                         }
11484:                     }
11485:                     if ($chgtext) {
11486:                         $resulttext .= '<li>'.$chgtext.'</li>';
11487:                     }
11488:                 }
11489:             }
11490:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
11491:                 my ($emailrules,$emailruleorder) =
11492:                     &Apache::lonnet::inst_userrules($dom,'email');
11493:                 my $chgtext = '<ul>';
11494:                 foreach my $type (@email_rule) {
11495:                     if (ref($emailrules->{$type}) eq 'HASH') {
11496:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
11497:                     }
11498:                 }
11499:                 $chgtext .= '</ul>';
11500:                 if (@email_rule > 0) {
11501:                     $resulttext .= '<li>'.
11502:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
11503:                                        $chgtext.
11504:                                    '</li>';
11505:                 } else {
11506:                     $resulttext .= '<li>'.
11507:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
11508:                                    '</li>';
11509:                 }
11510:             }
11511:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
11512:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
11513:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11514:                 foreach my $type (@{$changes{'selfcreate'}}) {
11515:                     my $typename = $type;
11516:                     if (ref($usertypes) eq 'HASH') {
11517:                         if ($usertypes->{$type} ne '') {
11518:                             $typename = $usertypes->{$type};
11519:                         }
11520:                     }
11521:                     my @modifiable;
11522:                     $resulttext .= '<li>'.
11523:                                     &mt('Self-creation of account by users with status: [_1]',
11524:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
11525:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
11526:                     foreach my $field (@fields) {
11527:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
11528:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
11529:                         }
11530:                     }
11531:                     if (@modifiable > 0) {
11532:                         $resulttext .= join(', ',@modifiable);
11533:                     } else {
11534:                         $resulttext .= &mt('none');
11535:                     }
11536:                     $resulttext .= '</li>';
11537:                 }
11538:                 $resulttext .= '</ul></li>';
11539:             }
11540:             $resulttext .= '</ul>';
11541:         } else {
11542:             $resulttext = &mt('No changes made to self-creation settings');
11543:         }
11544:     } else {
11545:         $resulttext = '<span class="LC_error">'.
11546:             &mt('An error occurred: [_1]',$putresult).'</span>';
11547:     }
11548:     if ($warningmsg ne '') {
11549:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11550:     }
11551:     return $resulttext;
11552: }
11553: 
11554: sub process_captcha {
11555:     my ($container,$changes,$newsettings,$current) = @_;
11556:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
11557:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
11558:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
11559:         $newsettings->{'captcha'} = 'original';
11560:     }
11561:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
11562:         if ($container eq 'cancreate') {
11563:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11564:                 push(@{$changes->{'cancreate'}},'captcha');
11565:             } elsif (!defined($changes->{'cancreate'})) {
11566:                 $changes->{'cancreate'} = ['captcha'];
11567:             }
11568:         } else {
11569:             $changes->{'captcha'} = 1;
11570:         }
11571:     }
11572:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
11573:     if ($newsettings->{'captcha'} eq 'recaptcha') {
11574:         $newpub = $env{'form.'.$container.'_recaptchapub'};
11575:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
11576:         $newpub =~ s/[^\w\-]//g;
11577:         $newpriv =~ s/[^\w\-]//g;
11578:         $newsettings->{'recaptchakeys'} = {
11579:                                              public  => $newpub,
11580:                                              private => $newpriv,
11581:                                           };
11582:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
11583:         $newversion =~ s/\D//g;
11584:         if ($newversion ne '2') {
11585:             $newversion = 1;
11586:         }
11587:         $newsettings->{'recaptchaversion'} = $newversion;
11588:     }
11589:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
11590:         $currpub = $current->{'recaptchakeys'}{'public'};
11591:         $currpriv = $current->{'recaptchakeys'}{'private'};
11592:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
11593:             $newsettings->{'recaptchakeys'} = {
11594:                                                  public  => '',
11595:                                                  private => '',
11596:                                               }
11597:         }
11598:     }
11599:     if ($current->{'captcha'} eq 'recaptcha') {
11600:         $currversion = $current->{'recaptchaversion'};
11601:         if ($currversion ne '2') {
11602:             $currversion = 1;
11603:         }
11604:     }
11605:     if ($currversion ne $newversion) {
11606:         if ($container eq 'cancreate') {
11607:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11608:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
11609:             } elsif (!defined($changes->{'cancreate'})) {
11610:                 $changes->{'cancreate'} = ['recaptchaversion'];
11611:             }
11612:         } else {
11613:             $changes->{'recaptchaversion'} = 1;
11614:         }
11615:     }
11616:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
11617:         if ($container eq 'cancreate') {
11618:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11619:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
11620:             } elsif (!defined($changes->{'cancreate'})) {
11621:                 $changes->{'cancreate'} = ['recaptchakeys'];
11622:             }
11623:         } else {
11624:             $changes->{'recaptchakeys'} = 1;
11625:         }
11626:     }
11627:     return;
11628: }
11629: 
11630: sub modify_usermodification {
11631:     my ($dom,%domconfig) = @_;
11632:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
11633:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11634:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11635:             if ($key eq 'selfcreate') {
11636:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
11637:             } else {  
11638:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
11639:             }
11640:         }
11641:     }
11642:     my @contexts = ('author','course');
11643:     my %context_title = (
11644:                            author => 'In author context',
11645:                            course => 'In course context',
11646:                         );
11647:     my @fields = ('lastname','firstname','middlename','generation',
11648:                   'permanentemail','id');
11649:     my %roles = (
11650:                   author => ['ca','aa'],
11651:                   course => ['st','ep','ta','in','cr'],
11652:                 );
11653:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11654:     foreach my $context (@contexts) {
11655:         foreach my $role (@{$roles{$context}}) {
11656:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
11657:             foreach my $item (@fields) {
11658:                 if (grep(/^\Q$item\E$/,@modifiable)) {
11659:                     $modifyhash{$context}{$role}{$item} = 1;
11660:                 } else {
11661:                     $modifyhash{$context}{$role}{$item} = 0;
11662:                 }
11663:             }
11664:         }
11665:         if (ref($curr_usermodification{$context}) eq 'HASH') {
11666:             foreach my $role (@{$roles{$context}}) {
11667:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
11668:                     foreach my $field (@fields) {
11669:                         if ($modifyhash{$context}{$role}{$field} ne 
11670:                                 $curr_usermodification{$context}{$role}{$field}) {
11671:                             push(@{$changes{$context}},$role);
11672:                             last;
11673:                         }
11674:                     }
11675:                 }
11676:             }
11677:         } else {
11678:             foreach my $context (@contexts) {
11679:                 foreach my $role (@{$roles{$context}}) {
11680:                     push(@{$changes{$context}},$role);
11681:                 }
11682:             }
11683:         }
11684:     }
11685:     my %usermodification_hash =  (
11686:                                    usermodification => \%modifyhash,
11687:                                  );
11688:     my $putresult = &Apache::lonnet::put_dom('configuration',
11689:                                              \%usermodification_hash,$dom);
11690:     if ($putresult eq 'ok') {
11691:         if (keys(%changes) > 0) {
11692:             $resulttext = &mt('Changes made: ').'<ul>';
11693:             foreach my $context (@contexts) {
11694:                 if (ref($changes{$context}) eq 'ARRAY') {
11695:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
11696:                     if (ref($changes{$context}) eq 'ARRAY') {
11697:                         foreach my $role (@{$changes{$context}}) {
11698:                             my $rolename;
11699:                             if ($role eq 'cr') {
11700:                                 $rolename = &mt('Custom');
11701:                             } else {
11702:                                 $rolename = &Apache::lonnet::plaintext($role);
11703:                             }
11704:                             my @modifiable;
11705:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
11706:                             foreach my $field (@fields) {
11707:                                 if ($modifyhash{$context}{$role}{$field}) {
11708:                                     push(@modifiable,$fieldtitles{$field});
11709:                                 }
11710:                             }
11711:                             if (@modifiable > 0) {
11712:                                 $resulttext .= join(', ',@modifiable);
11713:                             } else {
11714:                                 $resulttext .= &mt('none'); 
11715:                             }
11716:                             $resulttext .= '</li>';
11717:                         }
11718:                         $resulttext .= '</ul></li>';
11719:                     }
11720:                 }
11721:             }
11722:             $resulttext .= '</ul>';
11723:         } else {
11724:             $resulttext = &mt('No changes made to user modification settings');
11725:         }
11726:     } else {
11727:         $resulttext = '<span class="LC_error">'.
11728:             &mt('An error occurred: [_1]',$putresult).'</span>';
11729:     }
11730:     return $resulttext;
11731: }
11732: 
11733: sub modify_defaults {
11734:     my ($dom,$lastactref,%domconfig) = @_;
11735:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
11736:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11737:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
11738:     my @authtypes = ('internal','krb4','krb5','localauth');
11739:     foreach my $item (@items) {
11740:         $newvalues{$item} = $env{'form.'.$item};
11741:         if ($item eq 'auth_def') {
11742:             if ($newvalues{$item} ne '') {
11743:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
11744:                     push(@errors,$item);
11745:                 }
11746:             }
11747:         } elsif ($item eq 'lang_def') {
11748:             if ($newvalues{$item} ne '') {
11749:                 if ($newvalues{$item} =~ /^(\w+)/) {
11750:                     my $langcode = $1;
11751:                     if ($langcode ne 'x_chef') {
11752:                         if (code2language($langcode) eq '') {
11753:                             push(@errors,$item);
11754:                         }
11755:                     }
11756:                 } else {
11757:                     push(@errors,$item);
11758:                 }
11759:             }
11760:         } elsif ($item eq 'timezone_def') {
11761:             if ($newvalues{$item} ne '') {
11762:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
11763:                     push(@errors,$item);   
11764:                 }
11765:             }
11766:         } elsif ($item eq 'datelocale_def') {
11767:             if ($newvalues{$item} ne '') {
11768:                 my @datelocale_ids = DateTime::Locale->ids();
11769:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
11770:                     push(@errors,$item);
11771:                 }
11772:             }
11773:         } elsif ($item eq 'portal_def') {
11774:             if ($newvalues{$item} ne '') {
11775:                 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])\/?$/) {
11776:                     push(@errors,$item);
11777:                 }
11778:             }
11779:         }
11780:         if (grep(/^\Q$item\E$/,@errors)) {
11781:             $newvalues{$item} = $domdefaults{$item};
11782:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
11783:             $changes{$item} = 1;
11784:         }
11785:         $domdefaults{$item} = $newvalues{$item};
11786:     }
11787:     my %defaults_hash = (
11788:                          defaults => \%newvalues,
11789:                         );
11790:     my $title = &defaults_titles();
11791: 
11792:     my $currinststatus;
11793:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
11794:         $currinststatus = $domconfig{'inststatus'};
11795:     } else {
11796:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11797:         $currinststatus = {
11798:                              inststatustypes => $usertypes,
11799:                              inststatusorder => $types,
11800:                              inststatusguest => [],
11801:                           };
11802:     }
11803:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
11804:     my @allpos;
11805:     my %guests;
11806:     my %alltypes;
11807:     my ($currtitles,$currguests,$currorder);
11808:     if (ref($currinststatus) eq 'HASH') {
11809:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
11810:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
11811:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
11812:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
11813:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
11814:                     }
11815:                 }
11816:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
11817:                     my $position = $env{'form.inststatus_pos_'.$type};
11818:                     $position =~ s/\D+//g;
11819:                     $allpos[$position] = $type;
11820:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
11821:                     $alltypes{$type} =~ s/`//g;
11822:                     if ($env{'form.inststatus_guest_'.$type}) {
11823:                         $guests{$type} = 1;
11824:                     }
11825:                 }
11826:             }
11827:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
11828:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
11829:             }
11830:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
11831:             $currtitles =~ s/,$//;
11832:         }
11833:     }
11834:     if ($env{'form.addinststatus'}) {
11835:         my $newtype = $env{'form.addinststatus'};
11836:         $newtype =~ s/\W//g;
11837:         unless (exists($alltypes{$newtype})) {
11838:             if ($env{'form.addinststatus_guest'}) {
11839:                 $guests{$newtype} = 1;
11840:             }
11841:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
11842:             $alltypes{$newtype} =~ s/`//g; 
11843:             my $position = $env{'form.addinststatus_pos'};
11844:             $position =~ s/\D+//g;
11845:             if ($position ne '') {
11846:                 $allpos[$position] = $newtype;
11847:             }
11848:         }
11849:     }
11850:     my (@orderedstatus,@orderedguests);
11851:     foreach my $type (@allpos) {
11852:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
11853:             push(@orderedstatus,$type);
11854:             if ($guests{$type}) {
11855:                 push(@orderedguests,$type);
11856:             }
11857:         }
11858:     }
11859:     foreach my $type (keys(%alltypes)) {
11860:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
11861:             delete($alltypes{$type});
11862:         }
11863:     }
11864:     $defaults_hash{'inststatus'} = {
11865:                                      inststatustypes => \%alltypes,
11866:                                      inststatusorder => \@orderedstatus,
11867:                                      inststatusguest => \@orderedguests,
11868:                                    };
11869:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
11870:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
11871:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
11872:         }
11873:     }
11874:     if ($currorder ne join(',',@orderedstatus)) {
11875:         $changes{'inststatus'}{'inststatusorder'} = 1;
11876:     }
11877:     if ($currguests ne join(',',@orderedguests)) {
11878:         $changes{'inststatus'}{'inststatusguest'} = 1;
11879:     }
11880:     my $newtitles;
11881:     foreach my $item (@orderedstatus) {
11882:         $newtitles .= $alltypes{$item}.',';
11883:     }
11884:     $newtitles =~ s/,$//;
11885:     if ($currtitles ne $newtitles) {
11886:         $changes{'inststatus'}{'inststatustypes'} = 1;
11887:     }
11888:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
11889:                                              $dom);
11890:     if ($putresult eq 'ok') {
11891:         if (keys(%changes) > 0) {
11892:             $resulttext = &mt('Changes made:').'<ul>';
11893:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
11894:             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";
11895:             foreach my $item (sort(keys(%changes))) {
11896:                 if ($item eq 'inststatus') {
11897:                     if (ref($changes{'inststatus'}) eq 'HASH') {
11898:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
11899:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
11900:                             foreach my $type (@orderedstatus) { 
11901:                                 $resulttext .= $alltypes{$type}.', ';
11902:                             }
11903:                             $resulttext =~ s/, $//;
11904:                             $resulttext .= '</li>';
11905:                         }
11906:                         if ($changes{'inststatus'}{'inststatusguest'}) {
11907:                             $resulttext .= '<li>'; 
11908:                             if (@orderedguests) {
11909:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
11910:                                 foreach my $type (@orderedguests) {
11911:                                     $resulttext .= $alltypes{$type}.', ';
11912:                                 }
11913:                                 $resulttext =~ s/, $//;
11914:                             } else {
11915:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
11916:                             }
11917:                             $resulttext .= '</li>';
11918:                         }
11919:                     }
11920:                 } else {
11921:                     my $value = $env{'form.'.$item};
11922:                     if ($value eq '') {
11923:                         $value = &mt('none');
11924:                     } elsif ($item eq 'auth_def') {
11925:                         my %authnames = &authtype_names();
11926:                         my %shortauth = (
11927:                                           internal   => 'int',
11928:                                           krb4       => 'krb4',
11929:                                           krb5       => 'krb5',
11930:                                           localauth  => 'loc',
11931:                         );
11932:                         $value = $authnames{$shortauth{$value}};
11933:                     }
11934:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
11935:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
11936:                 }
11937:             }
11938:             $resulttext .= '</ul>';
11939:             $mailmsgtext .= "\n";
11940:             my $cachetime = 24*60*60;
11941:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11942:             if (ref($lastactref) eq 'HASH') {
11943:                 $lastactref->{'domdefaults'} = 1;
11944:             }
11945:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
11946:                 my $notify = 1;
11947:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
11948:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
11949:                         $notify = 0;
11950:                     }
11951:                 }
11952:                 if ($notify) {
11953:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
11954:                                                "LON-CAPA Domain Settings Change - $dom",
11955:                                                $mailmsgtext);
11956:                 }
11957:             }
11958:         } else {
11959:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
11960:         }
11961:     } else {
11962:         $resulttext = '<span class="LC_error">'.
11963:             &mt('An error occurred: [_1]',$putresult).'</span>';
11964:     }
11965:     if (@errors > 0) {
11966:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
11967:         foreach my $item (@errors) {
11968:             $resulttext .= ' "'.$title->{$item}.'",';
11969:         }
11970:         $resulttext =~ s/,$//;
11971:     }
11972:     return $resulttext;
11973: }
11974: 
11975: sub modify_scantron {
11976:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
11977:     my ($resulttext,%confhash,%changes,$errors);
11978:     my $custom = 'custom.tab';
11979:     my $default = 'default.tab';
11980:     my $servadm = $r->dir_config('lonAdmEMail');
11981:     my ($configuserok,$author_ok,$switchserver) = 
11982:         &config_check($dom,$confname,$servadm);
11983:     if ($env{'form.scantronformat.filename'} ne '') {
11984:         my $error;
11985:         if ($configuserok eq 'ok') {
11986:             if ($switchserver) {
11987:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
11988:             } else {
11989:                 if ($author_ok eq 'ok') {
11990:                     my ($result,$scantronurl) =
11991:                         &publishlogo($r,'upload','scantronformat',$dom,
11992:                                      $confname,'scantron','','',$custom);
11993:                     if ($result eq 'ok') {
11994:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
11995:                         $changes{'scantronformat'} = 1;
11996:                     } else {
11997:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
11998:                     }
11999:                 } else {
12000:                     $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);
12001:                 }
12002:             }
12003:         } else {
12004:             $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);
12005:         }
12006:         if ($error) {
12007:             &Apache::lonnet::logthis($error);
12008:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12009:         }
12010:     }
12011:     if (ref($domconfig{'scantron'}) eq 'HASH') {
12012:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
12013:             if ($env{'form.scantronformat_del'}) {
12014:                 $confhash{'scantron'}{'scantronformat'} = '';
12015:                 $changes{'scantronformat'} = 1;
12016:             }
12017:         }
12018:     }
12019:     if (keys(%confhash) > 0) {
12020:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
12021:                                                  $dom);
12022:         if ($putresult eq 'ok') {
12023:             if (keys(%changes) > 0) {
12024:                 if (ref($confhash{'scantron'}) eq 'HASH') {
12025:                     $resulttext = &mt('Changes made:').'<ul>';
12026:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
12027:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
12028:                     } else {
12029:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
12030:                     }
12031:                     $resulttext .= '</ul>';
12032:                 } else {
12033:                     $resulttext = &mt('Changes made to bubblesheet format file.');
12034:                 }
12035:                 $resulttext .= '</ul>';
12036:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
12037:                 if (ref($lastactref) eq 'HASH') {
12038:                     $lastactref->{'domainconfig'} = 1;
12039:                 }
12040:             } else {
12041:                 $resulttext = &mt('No changes made to bubblesheet format file');
12042:             }
12043:         } else {
12044:             $resulttext = '<span class="LC_error">'.
12045:                 &mt('An error occurred: [_1]',$putresult).'</span>';
12046:         }
12047:     } else {
12048:         $resulttext = &mt('No changes made to bubblesheet format file'); 
12049:     }
12050:     if ($errors) {
12051:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12052:                        $errors.'</ul>';
12053:     }
12054:     return $resulttext;
12055: }
12056: 
12057: sub modify_coursecategories {
12058:     my ($dom,$lastactref,%domconfig) = @_;
12059:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
12060:         $cathash);
12061:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
12062:     my @catitems = ('unauth','auth');
12063:     my @cattypes = ('std','domonly','codesrch','none');
12064:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12065:         $cathash = $domconfig{'coursecategories'}{'cats'};
12066:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
12067:             $changes{'togglecats'} = 1;
12068:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
12069:         }
12070:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
12071:             $changes{'categorize'} = 1;
12072:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
12073:         }
12074:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
12075:             $changes{'togglecatscomm'} = 1;
12076:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
12077:         }
12078:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
12079:             $changes{'categorizecomm'} = 1;
12080:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
12081: 
12082:         }
12083:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
12084:             $changes{'togglecatsplace'} = 1;
12085:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
12086:         }
12087:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
12088:             $changes{'categorizeplace'} = 1;
12089:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
12090:         }
12091:         foreach my $item (@catitems) {
12092:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12093:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
12094:                     $changes{$item} = 1;
12095:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12096:                 }
12097:             }
12098:         }
12099:     } else {
12100:         $changes{'togglecats'} = 1;
12101:         $changes{'categorize'} = 1;
12102:         $changes{'togglecatscomm'} = 1;
12103:         $changes{'categorizecomm'} = 1;
12104:         $changes{'togglecatsplace'} = 1;
12105:         $changes{'categorizeplace'} = 1;
12106:         $domconfig{'coursecategories'} = {
12107:                                              togglecats => $env{'form.togglecats'},
12108:                                              categorize => $env{'form.categorize'},
12109:                                              togglecatscomm => $env{'form.togglecatscomm'},
12110:                                              categorizecomm => $env{'form.categorizecomm'},
12111:                                              togglecatsplace => $env{'form.togglecatsplace'},
12112:                                              categorizeplace => $env{'form.categorizeplace'},
12113:                                          };
12114:         foreach my $item (@catitems) {
12115:             if ($env{'form.coursecat_'.$item} ne 'std') {
12116:                 $changes{$item} = 1;
12117:             }
12118:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12119:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12120:             }
12121:         }
12122:     }
12123:     if (ref($cathash) eq 'HASH') {
12124:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
12125:             push (@deletecategory,'instcode::0');
12126:         }
12127:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
12128:             push(@deletecategory,'communities::0');
12129:         }
12130:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
12131:             push(@deletecategory,'placement::0');
12132:         }
12133:     }
12134:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
12135:     if (ref($cathash) eq 'HASH') {
12136:         if (@deletecategory > 0) {
12137:             #FIXME Need to remove category from all courses using a deleted category 
12138:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
12139:             foreach my $item (@deletecategory) {
12140:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
12141:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
12142:                     $deletions{$item} = 1;
12143:                     &recurse_cat_deletes($item,$cathash,\%deletions);
12144:                 }
12145:             }
12146:         }
12147:         foreach my $item (keys(%{$cathash})) {
12148:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
12149:             if ($cathash->{$item} ne $env{'form.'.$item}) {
12150:                 $reorderings{$item} = 1;
12151:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
12152:             }
12153:             if ($env{'form.addcategory_name_'.$item} ne '') {
12154:                 my $newcat = $env{'form.addcategory_name_'.$item};
12155:                 my $newdepth = $depth+1;
12156:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
12157:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
12158:                 $adds{$newitem} = 1; 
12159:             }
12160:             if ($env{'form.subcat_'.$item} ne '') {
12161:                 my $newcat = $env{'form.subcat_'.$item};
12162:                 my $newdepth = $depth+1;
12163:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
12164:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
12165:                 $adds{$newitem} = 1;
12166:             }
12167:         }
12168:     }
12169:     if ($env{'form.instcode'} eq '1') {
12170:         if (ref($cathash) eq 'HASH') {
12171:             my $newitem = 'instcode::0';
12172:             if ($cathash->{$newitem} eq '') {  
12173:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
12174:                 $adds{$newitem} = 1;
12175:             }
12176:         } else {
12177:             my $newitem = 'instcode::0';
12178:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
12179:             $adds{$newitem} = 1;
12180:         }
12181:     }
12182:     if ($env{'form.communities'} eq '1') {
12183:         if (ref($cathash) eq 'HASH') {
12184:             my $newitem = 'communities::0';
12185:             if ($cathash->{$newitem} eq '') {
12186:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
12187:                 $adds{$newitem} = 1;
12188:             }
12189:         } else {
12190:             my $newitem = 'communities::0';
12191:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
12192:             $adds{$newitem} = 1;
12193:         }
12194:     }
12195:     if ($env{'form.placement'} eq '1') {
12196:         if (ref($cathash) eq 'HASH') {
12197:             my $newitem = 'placement::0';
12198:             if ($cathash->{$newitem} eq '') {
12199:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
12200:                 $adds{$newitem} = 1;
12201:             }
12202:         } else {
12203:             my $newitem = 'placement::0';
12204:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
12205:             $adds{$newitem} = 1;
12206:         }
12207:     }
12208:     if ($env{'form.addcategory_name'} ne '') {
12209:         if (($env{'form.addcategory_name'} ne 'instcode') &&
12210:             ($env{'form.addcategory_name'} ne 'communities') &&
12211:             ($env{'form.addcategory_name'} ne 'placement')) {
12212:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
12213:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
12214:             $adds{$newitem} = 1;
12215:         }
12216:     }
12217:     my $putresult;
12218:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12219:         if (keys(%deletions) > 0) {
12220:             foreach my $key (keys(%deletions)) {
12221:                 if ($predelallitems{$key} ne '') {
12222:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
12223:                 }
12224:             }
12225:         }
12226:         my (@chkcats,@chktrails,%chkallitems);
12227:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
12228:         if (ref($chkcats[0]) eq 'ARRAY') {
12229:             my $depth = 0;
12230:             my $chg = 0;
12231:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
12232:                 my $name = $chkcats[0][$i];
12233:                 my $item;
12234:                 if ($name eq '') {
12235:                     $chg ++;
12236:                 } else {
12237:                     $item = &escape($name).'::0';
12238:                     if ($chg) {
12239:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
12240:                     }
12241:                     $depth ++; 
12242:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
12243:                     $depth --;
12244:                 }
12245:             }
12246:         }
12247:     }
12248:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12249:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
12250:         if ($putresult eq 'ok') {
12251:             my %title = (
12252:                          togglecats     => 'Show/Hide a course in catalog',
12253:                          categorize     => 'Assign a category to a course',
12254:                          togglecatscomm => 'Show/Hide a community in catalog',
12255:                          categorizecomm => 'Assign a category to a community',
12256:                         );
12257:             my %level = (
12258:                          dom  => 'set in Domain ("Modify Course/Community")',
12259:                          crs  => 'set in Course ("Course Configuration")',
12260:                          comm => 'set in Community ("Community Configuration")',
12261:                          none     => 'No catalog',
12262:                          std      => 'Standard catalog',
12263:                          domonly  => 'Domain-only catalog',
12264:                          codesrch => 'Code search form',
12265:                         );
12266:             $resulttext = &mt('Changes made:').'<ul>';
12267:             if ($changes{'togglecats'}) {
12268:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
12269:             }
12270:             if ($changes{'categorize'}) {
12271:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
12272:             }
12273:             if ($changes{'togglecatscomm'}) {
12274:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
12275:             }
12276:             if ($changes{'categorizecomm'}) {
12277:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
12278:             }
12279:             if ($changes{'unauth'}) {
12280:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
12281:             }
12282:             if ($changes{'auth'}) {
12283:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
12284:             }
12285:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12286:                 my $cathash;
12287:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12288:                     $cathash = $domconfig{'coursecategories'}{'cats'};
12289:                 } else {
12290:                     $cathash = {};
12291:                 } 
12292:                 my (@cats,@trails,%allitems);
12293:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
12294:                 if (keys(%deletions) > 0) {
12295:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
12296:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
12297:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
12298:                     }
12299:                     $resulttext .= '</ul></li>';
12300:                 }
12301:                 if (keys(%reorderings) > 0) {
12302:                     my %sort_by_trail;
12303:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
12304:                     foreach my $key (keys(%reorderings)) {
12305:                         if ($allitems{$key} ne '') {
12306:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
12307:                         }
12308:                     }
12309:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
12310:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
12311:                     }
12312:                     $resulttext .= '</ul></li>';
12313:                 }
12314:                 if (keys(%adds) > 0) {
12315:                     my %sort_by_trail;
12316:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
12317:                     foreach my $key (keys(%adds)) {
12318:                         if ($allitems{$key} ne '') {
12319:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
12320:                         }
12321:                     }
12322:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
12323:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
12324:                     }
12325:                     $resulttext .= '</ul></li>';
12326:                 }
12327:             }
12328:             $resulttext .= '</ul>';
12329:             if ($changes{'unauth'} || $changes{'auth'}) {
12330:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
12331:                 if ($changes{'auth'}) {
12332:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
12333:                 }
12334:                 if ($changes{'unauth'}) {
12335:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
12336:                 }
12337:                 my $cachetime = 24*60*60;
12338:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12339:                 if (ref($lastactref) eq 'HASH') {
12340:                     $lastactref->{'domdefaults'} = 1;
12341:                 }
12342:             }
12343:         } else {
12344:             $resulttext = '<span class="LC_error">'.
12345:                           &mt('An error occurred: [_1]',$putresult).'</span>';
12346:         }
12347:     } else {
12348:         $resulttext = &mt('No changes made to course and community categories');
12349:     }
12350:     return $resulttext;
12351: }
12352: 
12353: sub modify_serverstatuses {
12354:     my ($dom,%domconfig) = @_;
12355:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
12356:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
12357:         %currserverstatus = %{$domconfig{'serverstatuses'}};
12358:     }
12359:     my @pages = &serverstatus_pages();
12360:     foreach my $type (@pages) {
12361:         $newserverstatus{$type}{'namedusers'} = '';
12362:         $newserverstatus{$type}{'machines'} = '';
12363:         if (defined($env{'form.'.$type.'_namedusers'})) {
12364:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
12365:             my @okusers;
12366:             foreach my $user (@users) {
12367:                 my ($uname,$udom) = split(/:/,$user);
12368:                 if (($udom =~ /^$match_domain$/) &&   
12369:                     (&Apache::lonnet::domain($udom)) &&
12370:                     ($uname =~ /^$match_username$/)) {
12371:                     if (!grep(/^\Q$user\E/,@okusers)) {
12372:                         push(@okusers,$user);
12373:                     }
12374:                 }
12375:             }
12376:             if (@okusers > 0) {
12377:                  @okusers = sort(@okusers);
12378:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
12379:             }
12380:         }
12381:         if (defined($env{'form.'.$type.'_machines'})) {
12382:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
12383:             my @okmachines;
12384:             foreach my $ip (@machines) {
12385:                 my @parts = split(/\./,$ip);
12386:                 next if (@parts < 4);
12387:                 my $badip = 0;
12388:                 for (my $i=0; $i<4; $i++) {
12389:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
12390:                         $badip = 1;
12391:                         last;
12392:                     }
12393:                 }
12394:                 if (!$badip) {
12395:                     push(@okmachines,$ip);     
12396:                 }
12397:             }
12398:             @okmachines = sort(@okmachines);
12399:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
12400:         }
12401:     }
12402:     my %serverstatushash =  (
12403:                                 serverstatuses => \%newserverstatus,
12404:                             );
12405:     foreach my $type (@pages) {
12406:         foreach my $setting ('namedusers','machines') {
12407:             my (@current,@new);
12408:             if (ref($currserverstatus{$type}) eq 'HASH') {
12409:                 if ($currserverstatus{$type}{$setting} ne '') { 
12410:                     @current = split(/,/,$currserverstatus{$type}{$setting});
12411:                 }
12412:             }
12413:             if ($newserverstatus{$type}{$setting} ne '') {
12414:                 @new = split(/,/,$newserverstatus{$type}{$setting});
12415:             }
12416:             if (@current > 0) {
12417:                 if (@new > 0) {
12418:                     foreach my $item (@current) {
12419:                         if (!grep(/^\Q$item\E$/,@new)) {
12420:                             $changes{$type}{$setting} = 1;
12421:                             last;
12422:                         }
12423:                     }
12424:                     foreach my $item (@new) {
12425:                         if (!grep(/^\Q$item\E$/,@current)) {
12426:                             $changes{$type}{$setting} = 1;
12427:                             last;
12428:                         }
12429:                     }
12430:                 } else {
12431:                     $changes{$type}{$setting} = 1;
12432:                 }
12433:             } elsif (@new > 0) {
12434:                 $changes{$type}{$setting} = 1;
12435:             }
12436:         }
12437:     }
12438:     if (keys(%changes) > 0) {
12439:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
12440:         my $putresult = &Apache::lonnet::put_dom('configuration',
12441:                                                  \%serverstatushash,$dom);
12442:         if ($putresult eq 'ok') {
12443:             $resulttext .= &mt('Changes made:').'<ul>';
12444:             foreach my $type (@pages) {
12445:                 if (ref($changes{$type}) eq 'HASH') {
12446:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
12447:                     if ($changes{$type}{'namedusers'}) {
12448:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
12449:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
12450:                         } else {
12451:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
12452:                         }
12453:                     }
12454:                     if ($changes{$type}{'machines'}) {
12455:                         if ($newserverstatus{$type}{'machines'} eq '') {
12456:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
12457:                         } else {
12458:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
12459:                         }
12460: 
12461:                     }
12462:                     $resulttext .= '</ul></li>';
12463:                 }
12464:             }
12465:             $resulttext .= '</ul>';
12466:         } else {
12467:             $resulttext = '<span class="LC_error">'.
12468:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
12469: 
12470:         }
12471:     } else {
12472:         $resulttext = &mt('No changes made to access to server status pages');
12473:     }
12474:     return $resulttext;
12475: }
12476: 
12477: sub modify_helpsettings {
12478:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12479:     my ($resulttext,$errors,%changes,%helphash);
12480:     my %defaultchecked = ('submitbugs' => 'on');
12481:     my @offon = ('off','on');
12482:     my @toggles = ('submitbugs');
12483:     my %current = ('submitbugs' => '',
12484:                    'adhoc'      => {},
12485:                   );
12486:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
12487:         %current = %{$domconfig{'helpsettings'}};
12488:     }
12489:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12490:     foreach my $item (@toggles) {
12491:         if ($defaultchecked{$item} eq 'on') { 
12492:             if ($current{$item} eq '') {
12493:                 if ($env{'form.'.$item} eq '0') {
12494:                     $changes{$item} = 1;
12495:                 }
12496:             } elsif ($current{$item} ne $env{'form.'.$item}) {
12497:                 $changes{$item} = 1;
12498:             }
12499:         } elsif ($defaultchecked{$item} eq 'off') {
12500:             if ($current{$item} eq '') {
12501:                 if ($env{'form.'.$item} eq '1') {
12502:                     $changes{$item} = 1;
12503:                 }
12504:             } elsif ($current{$item} ne $env{'form.'.$item}) {
12505:                 $changes{$item} = 1;
12506:             }
12507:         }
12508:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
12509:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
12510:         }
12511:     }
12512:     my $maxnum = $env{'form.helproles_maxnum'};
12513:     my $confname = $dom.'-domainconfig';
12514:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
12515:     my (@allpos,%newsettings,%changedprivs,$newrole);
12516:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12517:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
12518:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
12519:     my %lt = &Apache::lonlocal::texthash(
12520:                     s      => 'system',
12521:                     d      => 'domain',
12522:                     order  => 'Display order',
12523:                     access => 'Role usage',
12524:                     all    => 'All with domain helpdesk or helpdesk assistant role',
12525:                     dh     => 'All with domain helpdesk role',
12526:                     da     => 'All with domain helpdesk assistant role',
12527:                     none   => 'None',
12528:                     status => 'Determined based on institutional status',
12529:                     inc    => 'Include all, but exclude specific personnel',
12530:                     exc    => 'Exclude all, but include specific personnel',
12531:     );
12532:     for (my $num=0; $num<=$maxnum; $num++) {
12533:         my ($prefix,$identifier,$rolename,%curr);
12534:         if ($num == $maxnum) {
12535:             next unless ($env{'form.newcusthelp'} == $maxnum);
12536:             $identifier = 'custhelp'.$num;
12537:             $prefix = 'helproles_'.$num;
12538:             $rolename = $env{'form.custhelpname'.$num};
12539:             $rolename=~s/[^A-Za-z0-9]//gs;
12540:             next if ($rolename eq '');
12541:             next if (exists($existing{'rolesdef_'.$rolename}));
12542:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12543:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12544:                                                      $newprivs{'c'},$confname,$dom);
12545:             if ($result ne 'ok') {
12546:                 $errors .= '<li><span class="LC_error">'.
12547:                            &mt('An error occurred storing the new custom role: [_1]',
12548:                            $result).'</span></li>';
12549:                 next;
12550:             } else {
12551:                 $changedprivs{$rolename} = \%newprivs;
12552:                 $newrole = $rolename;
12553:             }
12554:         } else {
12555:             $prefix = 'helproles_'.$num;
12556:             $rolename = $env{'form.'.$prefix};
12557:             next if ($rolename eq '');
12558:             next unless (exists($existing{'rolesdef_'.$rolename}));
12559:             $identifier = 'custhelp'.$num;
12560:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12561:             my %currprivs;
12562:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
12563:                 split(/\_/,$existing{'rolesdef_'.$rolename});
12564:             foreach my $level ('c','d','s') {
12565:                 if ($newprivs{$level} ne $currprivs{$level}) {
12566:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12567:                                                              $newprivs{'c'},$confname,$dom);
12568:                     if ($result ne 'ok') {
12569:                         $errors .= '<li><span class="LC_error">'.
12570:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
12571:                                        $rolename,$result).'</span></li>';
12572:                     } else {
12573:                         $changedprivs{$rolename} = \%newprivs;
12574:                     }
12575:                     last;
12576:                 }
12577:             }
12578:             if (ref($current{'adhoc'}) eq 'HASH') {
12579:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12580:                     %curr = %{$current{'adhoc'}{$rolename}};
12581:                 }
12582:             }
12583:         }
12584:         my $newpos = $env{'form.'.$prefix.'_pos'};
12585:         $newpos =~ s/\D+//g;
12586:         $allpos[$newpos] = $rolename;
12587:         my $newdesc = $env{'form.'.$prefix.'_desc'};
12588:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
12589:         if ($curr{'desc'}) {
12590:             if ($curr{'desc'} ne $newdesc) {
12591:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
12592:                 $newsettings{$rolename}{'desc'} = $newdesc;
12593:             }
12594:         } elsif ($newdesc ne '') {
12595:             $changes{'customrole'}{$rolename}{'desc'} = 1;
12596:             $newsettings{$rolename}{'desc'} = $newdesc;
12597:         }
12598:         my $access = $env{'form.'.$prefix.'_access'};
12599:         if (grep(/^\Q$access\E$/,@accesstypes)) {
12600:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
12601:             if ($access eq 'status') {
12602:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
12603:                 if (scalar(@statuses) == 0) {
12604:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
12605:                 } else {
12606:                     my (@shownstatus,$numtypes);
12607:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12608:                     if (ref($types) eq 'ARRAY') {
12609:                         $numtypes = scalar(@{$types});
12610:                         foreach my $type (sort(@statuses)) {
12611:                             if ($type eq 'default') {
12612:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12613:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
12614:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12615:                                 push(@shownstatus,$usertypes->{$type});
12616:                             }
12617:                         }
12618:                     }
12619:                     if (grep(/^default$/,@statuses)) {
12620:                         push(@shownstatus,$othertitle);
12621:                     }
12622:                     if (scalar(@shownstatus) == 1+$numtypes) {
12623:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
12624:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
12625:                     } else {
12626:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
12627:                         if (ref($curr{'status'}) eq 'ARRAY') {
12628:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12629:                             if (@diffs) {
12630:                                 $changes{'customrole'}{$rolename}{$access} = 1;
12631:                             }
12632:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12633:                             $changes{'customrole'}{$rolename}{$access} = 1;
12634:                         }
12635:                     }
12636:                 }
12637:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
12638:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
12639:                 my @newspecstaff;
12640:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12641:                 foreach my $person (sort(@personnel)) {
12642:                     if ($domhelpdesk{$person}) {
12643:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
12644:                     }
12645:                 }
12646:                 if (ref($curr{$access}) eq 'ARRAY') {
12647:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12648:                     if (@diffs) {
12649:                         $changes{'customrole'}{$rolename}{$access} = 1;
12650:                     }
12651:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12652:                     $changes{'customrole'}{$rolename}{$access} = 1;
12653:                 }
12654:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12655:                     my ($uname,$udom) = split(/:/,$person);
12656:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
12657:                 }
12658:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
12659:             }
12660:         } else {
12661:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
12662:         }
12663:         unless ($curr{'access'} eq $access) {
12664:             $changes{'customrole'}{$rolename}{'access'} = 1;
12665:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
12666:         }
12667:     }
12668:     if (@allpos > 0) {
12669:         my $idx = 0;
12670:         foreach my $rolename (@allpos) {
12671:             if ($rolename ne '') {
12672:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
12673:                 if (ref($current{'adhoc'}) eq 'HASH') {
12674:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12675:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
12676:                             $changes{'customrole'}{$rolename}{'order'} = 1;
12677:                             $newsettings{$rolename}{'order'} = $idx+1;
12678:                         }
12679:                     }
12680:                 }
12681:                 $idx ++;
12682:             }
12683:         }
12684:     }
12685:     my $putresult;
12686:     if (keys(%changes) > 0) {
12687:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
12688:         if ($putresult eq 'ok') {
12689:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
12690:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
12691:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
12692:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
12693:                 }
12694:             }
12695:             my $cachetime = 24*60*60;
12696:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12697:             if (ref($lastactref) eq 'HASH') {
12698:                 $lastactref->{'domdefaults'} = 1;
12699:             }
12700:         } else {
12701:             $errors .= '<li><span class="LC_error">'.
12702:                        &mt('An error occurred storing the settings: [_1]',
12703:                            $putresult).'</span></li>';
12704:         }
12705:     }
12706:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
12707:         $resulttext = &mt('Changes made:').'<ul>';
12708:         my (%shownprivs,@levelorder);
12709:         @levelorder = ('c','d','s');
12710:         if ((keys(%changes)) && ($putresult eq 'ok')) {
12711:             foreach my $item (sort(keys(%changes))) {
12712:                 if ($item eq 'submitbugs') {
12713:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
12714:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
12715:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
12716:                 } elsif ($item eq 'customrole') {
12717:                     if (ref($changes{'customrole'}) eq 'HASH') {
12718:                         my @keyorder = ('order','desc','access','status','exc','inc');
12719:                         my %keytext = &Apache::lonlocal::texthash(
12720:                                                                    order  => 'Order',
12721:                                                                    desc   => 'Role description',
12722:                                                                    access => 'Role usage',
12723:                                                                    status => 'Allowed instituional types',
12724:                                                                    exc    => 'Allowed personnel',
12725:                                                                    inc    => 'Disallowed personnel',
12726:                         );
12727:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
12728:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
12729:                                 if ($role eq $newrole) {
12730:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
12731:                                                               $role).'<ul>';
12732:                                 } else {
12733:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
12734:                                                               $role).'<ul>';
12735:                                 }
12736:                                 foreach my $key (@keyorder) {
12737:                                     if ($changes{'customrole'}{$role}{$key}) {
12738:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
12739:                                                                   $keytext{$key},$newsettings{$role}{$key}).
12740:                                                        '</li>';
12741:                                     }
12742:                                 }
12743:                                 if (ref($changedprivs{$role}) eq 'HASH') {
12744:                                     $shownprivs{$role} = 1;
12745:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
12746:                                     foreach my $level (@levelorder) {
12747:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
12748:                                             next if ($item eq '');
12749:                                             my ($priv) = split(/\&/,$item,2);
12750:                                             if (&Apache::lonnet::plaintext($priv)) {
12751:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
12752:                                                 unless ($level eq 'c') {
12753:                                                     $resulttext .= ' ('.$lt{$level}.')';
12754:                                                 }
12755:                                                 $resulttext .= '</li>';
12756:                                             }
12757:                                         }
12758:                                     }
12759:                                     $resulttext .= '</ul>';
12760:                                 }
12761:                                 $resulttext .= '</ul></li>';
12762:                             }
12763:                         }
12764:                     }
12765:                 }
12766:             }
12767:         }
12768:         if (keys(%changedprivs)) {
12769:             foreach my $role (sort(keys(%changedprivs))) {
12770:                 unless ($shownprivs{$role}) {
12771:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
12772:                                               $role).'<ul>'.
12773:                                    '<li>'.&mt('Privileges set to :').'<ul>';
12774:                     foreach my $level (@levelorder) {
12775:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
12776:                             next if ($item eq '');
12777:                             my ($priv) = split(/\&/,$item,2);
12778:                             if (&Apache::lonnet::plaintext($priv)) {
12779:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
12780:                                 unless ($level eq 'c') {
12781:                                     $resulttext .= ' ('.$lt{$level}.')';
12782:                                 }
12783:                                 $resulttext .= '</li>';
12784:                             }
12785:                         }
12786:                     }
12787:                     $resulttext .= '</ul></li></ul></li>';
12788:                 }
12789:             }
12790:         }
12791:         $resulttext .= '</ul>';
12792:     } else {
12793:         $resulttext = &mt('No changes made to help settings');
12794:     }
12795:     if ($errors) {
12796:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
12797:                                     $errors.'</ul>';
12798:     }
12799:     return $resulttext;
12800: }
12801: 
12802: sub modify_coursedefaults {
12803:     my ($dom,$lastactref,%domconfig) = @_;
12804:     my ($resulttext,$errors,%changes,%defaultshash);
12805:     my %defaultchecked = (
12806:                            'canuse_pdfforms' => 'off',
12807:                            'uselcmath'       => 'on',
12808:                            'usejsme'         => 'on'
12809:                          );
12810:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
12811:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
12812:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
12813:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
12814:                    'mysqltables_textbook','mysqltables_placement');
12815:     my @types = ('official','unofficial','community','textbook','placement');
12816:     my %staticdefaults = (
12817:                            anonsurvey_threshold => 10,
12818:                            uploadquota          => 500,
12819:                            postsubmit           => 60,
12820:                            mysqltables          => 172800,
12821:                          );
12822: 
12823:     $defaultshash{'coursedefaults'} = {};
12824: 
12825:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
12826:         if ($domconfig{'coursedefaults'} eq '') {
12827:             $domconfig{'coursedefaults'} = {};
12828:         }
12829:     }
12830: 
12831:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
12832:         foreach my $item (@toggles) {
12833:             if ($defaultchecked{$item} eq 'on') {
12834:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
12835:                     ($env{'form.'.$item} eq '0')) {
12836:                     $changes{$item} = 1;
12837:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
12838:                     $changes{$item} = 1;
12839:                 }
12840:             } elsif ($defaultchecked{$item} eq 'off') {
12841:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
12842:                     ($env{'form.'.$item} eq '1')) {
12843:                     $changes{$item} = 1;
12844:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
12845:                     $changes{$item} = 1;
12846:                 }
12847:             }
12848:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
12849:         }
12850:         foreach my $item (@numbers) {
12851:             my ($currdef,$newdef);
12852:             $newdef = $env{'form.'.$item};
12853:             if ($item eq 'anonsurvey_threshold') {
12854:                 $currdef = $domconfig{'coursedefaults'}{$item};
12855:                 $newdef =~ s/\D//g;
12856:                 if ($newdef eq '' || $newdef < 1) {
12857:                     $newdef = 1;
12858:                 }
12859:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
12860:             } else {
12861:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
12862:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
12863:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
12864:                 }
12865:                 $newdef =~ s/[^\w.\-]//g;
12866:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
12867:             }
12868:             if ($currdef ne $newdef) {
12869:                 my $staticdef;
12870:                 if ($item eq 'anonsurvey_threshold') {
12871:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
12872:                         $changes{$item} = 1;
12873:                     }
12874:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
12875:                     my $setting = $1;
12876:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
12877:                         $changes{$setting} = 1;
12878:                     }
12879:                 }
12880:             }
12881:         }
12882:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
12883:         my @currclonecode;
12884:         if (ref($currclone) eq 'HASH') {
12885:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
12886:                 @currclonecode = @{$currclone->{'instcode'}};
12887:             }
12888:         }
12889:         my $newclone;
12890:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
12891:             $newclone = $env{'form.canclone'};
12892:         }
12893:         if ($newclone eq 'instcode') {
12894:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
12895:             my (%codedefaults,@code_order,@clonecode);
12896:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
12897:                                                     \@code_order);
12898:             foreach my $item (@code_order) {
12899:                 if (grep(/^\Q$item\E$/,@newcodes)) {
12900:                     push(@clonecode,$item);
12901:                 }
12902:             }
12903:             if (@clonecode) {
12904:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
12905:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
12906:                 if (@diffs) {
12907:                     $changes{'canclone'} = 1;
12908:                 }
12909:             } else {
12910:                 $newclone eq '';
12911:             }
12912:         } elsif ($newclone ne '') {
12913:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
12914:         }
12915:         if ($newclone ne $currclone) {
12916:             $changes{'canclone'} = 1;
12917:         }
12918:         my %credits;
12919:         foreach my $type (@types) {
12920:             unless ($type eq 'community') {
12921:                 $credits{$type} = $env{'form.'.$type.'_credits'};
12922:                 $credits{$type} =~ s/[^\d.]+//g;
12923:             }
12924:         }
12925:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
12926:             ($env{'form.coursecredits'} eq '1')) {
12927:             $changes{'coursecredits'} = 1;
12928:             foreach my $type (keys(%credits)) {
12929:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
12930:             }
12931:         } else {
12932:             if ($env{'form.coursecredits'} eq '1') {
12933:                 foreach my $type (@types) {
12934:                     unless ($type eq 'community') {
12935:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
12936:                             $changes{'coursecredits'} = 1;
12937:                         }
12938:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
12939:                     }
12940:                 }
12941:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
12942:                 foreach my $type (@types) {
12943:                     unless ($type eq 'community') {
12944:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
12945:                             $changes{'coursecredits'} = 1;
12946:                             last;
12947:                         }
12948:                     }
12949:                 }
12950:             }
12951:         }
12952:         if ($env{'form.postsubmit'} eq '1') {
12953:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
12954:             my %currtimeout;
12955:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
12956:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
12957:                     $changes{'postsubmit'} = 1;
12958:                 }
12959:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
12960:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
12961:                 }
12962:             } else {
12963:                 $changes{'postsubmit'} = 1;
12964:             }
12965:             foreach my $type (@types) {
12966:                 my $timeout = $env{'form.'.$type.'_timeout'};
12967:                 $timeout =~ s/\D//g;
12968:                 if ($timeout == $staticdefaults{'postsubmit'}) {
12969:                     $timeout = '';
12970:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
12971:                     $timeout = '0';
12972:                 }
12973:                 unless ($timeout eq '') {
12974:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
12975:                 }
12976:                 if (exists($currtimeout{$type})) {
12977:                     if ($timeout ne $currtimeout{$type}) {
12978:                         $changes{'postsubmit'} = 1;
12979:                     }
12980:                 } elsif ($timeout ne '') {
12981:                     $changes{'postsubmit'} = 1;
12982:                 }
12983:             }
12984:         } else {
12985:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
12986:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
12987:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
12988:                     $changes{'postsubmit'} = 1;
12989:                 }
12990:             } else {
12991:                 $changes{'postsubmit'} = 1;
12992:             }
12993:         }
12994:     }
12995:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
12996:                                              $dom);
12997:     if ($putresult eq 'ok') {
12998:         if (keys(%changes) > 0) {
12999:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13000:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
13001:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
13002:                 ($changes{'canclone'}) || ($changes{'mysqltables'})) {
13003:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme') { 
13004:                     if ($changes{$item}) {
13005:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
13006:                     }
13007:                 }
13008:                 if ($changes{'coursecredits'}) {
13009:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13010:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
13011:                             $domdefaults{$type.'credits'} =
13012:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
13013:                         }
13014:                     }
13015:                 }
13016:                 if ($changes{'postsubmit'}) {
13017:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13018:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
13019:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13020:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
13021:                                 $domdefaults{$type.'postsubtimeout'} =
13022:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13023:                             }
13024:                         }
13025:                     }
13026:                 }
13027:                 if ($changes{'uploadquota'}) {
13028:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13029:                         foreach my $type (@types) {
13030:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
13031:                         }
13032:                     }
13033:                 }
13034:                 if ($changes{'canclone'}) {
13035:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13036:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13037:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
13038:                             if (@clonecodes) {
13039:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
13040:                             }
13041:                         }
13042:                     } else {
13043:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
13044:                     }
13045:                 }
13046:                 my $cachetime = 24*60*60;
13047:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13048:                 if (ref($lastactref) eq 'HASH') {
13049:                     $lastactref->{'domdefaults'} = 1;
13050:                 }
13051:             }
13052:             $resulttext = &mt('Changes made:').'<ul>';
13053:             foreach my $item (sort(keys(%changes))) {
13054:                 if ($item eq 'canuse_pdfforms') {
13055:                     if ($env{'form.'.$item} eq '1') {
13056:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
13057:                     } else {
13058:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
13059:                     }
13060:                 } elsif ($item eq 'uselcmath') {
13061:                     if ($env{'form.'.$item} eq '1') {
13062:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
13063:                     } else {
13064:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
13065:                     }
13066:                 } elsif ($item eq 'usejsme') {
13067:                     if ($env{'form.'.$item} eq '1') {
13068:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
13069:                     } else {
13070:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
13071:                     }
13072:                 } elsif ($item eq 'anonsurvey_threshold') {
13073:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
13074:                 } elsif ($item eq 'uploadquota') {
13075:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13076:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
13077:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
13078:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
13079:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
13080:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
13081:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
13082:                                        '</ul>'.
13083:                                        '</li>';
13084:                     } else {
13085:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
13086:                     }
13087:                 } elsif ($item eq 'mysqltables') {
13088:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
13089:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
13090:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
13091:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
13092:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
13093:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
13094:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
13095:                                        '</ul>'.
13096:                                        '</li>';
13097:                     } else {
13098:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
13099:                     }
13100:                 } elsif ($item eq 'postsubmit') {
13101:                     if ($domdefaults{'postsubmit'} eq 'off') {
13102:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
13103:                     } else {
13104:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
13105:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13106:                             $resulttext .= &mt('durations:').'<ul>';
13107:                             foreach my $type (@types) {
13108:                                 $resulttext .= '<li>';
13109:                                 my $timeout;
13110:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13111:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13112:                                 }
13113:                                 my $display;
13114:                                 if ($timeout eq '0') {
13115:                                     $display = &mt('unlimited');
13116:                                 } elsif ($timeout eq '') {
13117:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
13118:                                 } else {
13119:                                     $display = &mt('[quant,_1,second]',$timeout);
13120:                                 }
13121:                                 if ($type eq 'community') {
13122:                                     $resulttext .= &mt('Communities');
13123:                                 } elsif ($type eq 'official') {
13124:                                     $resulttext .= &mt('Official courses');
13125:                                 } elsif ($type eq 'unofficial') {
13126:                                     $resulttext .= &mt('Unofficial courses');
13127:                                 } elsif ($type eq 'textbook') {
13128:                                     $resulttext .= &mt('Textbook courses');
13129:                                 } elsif ($type eq 'placement') {
13130:                                     $resulttext .= &mt('Placement tests');
13131:                                 }
13132:                                 $resulttext .= ' -- '.$display.'</li>';
13133:                             }
13134:                             $resulttext .= '</ul>';
13135:                         }
13136:                         $resulttext .= '</li>';
13137:                     }
13138:                 } elsif ($item eq 'coursecredits') {
13139:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13140:                         if (($domdefaults{'officialcredits'} eq '') &&
13141:                             ($domdefaults{'unofficialcredits'} eq '') &&
13142:                             ($domdefaults{'textbookcredits'} eq '')) {
13143:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13144:                         } else {
13145:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
13146:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
13147:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
13148:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
13149:                                            '</ul>'.
13150:                                            '</li>';
13151:                         }
13152:                     } else {
13153:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13154:                     }
13155:                 } elsif ($item eq 'canclone') {
13156:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13157:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13158:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
13159:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
13160:                         }
13161:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
13162:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
13163:                     } else {
13164:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
13165:                     }
13166:                 }
13167:             }
13168:             $resulttext .= '</ul>';
13169:         } else {
13170:             $resulttext = &mt('No changes made to course defaults');
13171:         }
13172:     } else {
13173:         $resulttext = '<span class="LC_error">'.
13174:             &mt('An error occurred: [_1]',$putresult).'</span>';
13175:     }
13176:     return $resulttext;
13177: }
13178: 
13179: sub modify_selfenrollment {
13180:     my ($dom,$lastactref,%domconfig) = @_;
13181:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
13182:     my @types = ('official','unofficial','community','textbook','placement');
13183:     my %titles = &tool_titles();
13184:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
13185:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
13186:     $ordered{'default'} = ['types','registered','approval','limit'];
13187: 
13188:     my (%roles,%shown,%toplevel);
13189:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
13190: 
13191:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
13192:         if ($domconfig{'selfenrollment'} eq '') {
13193:             $domconfig{'selfenrollment'} = {};
13194:         }
13195:     }
13196:     %toplevel = (
13197:                   admin      => 'Configuration Rights',
13198:                   default    => 'Default settings',
13199:                   validation => 'Validation of self-enrollment requests',
13200:                 );
13201:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
13202: 
13203:     if (ref($ordered{'admin'}) eq 'ARRAY') {
13204:         foreach my $item (@{$ordered{'admin'}}) {
13205:             foreach my $type (@types) {
13206:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
13207:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
13208:                 } else {
13209:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
13210:                 }
13211:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
13212:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
13213:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
13214:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
13215:                             push(@{$changes{'admin'}{$type}},$item);
13216:                         }
13217:                     } else {
13218:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
13219:                             push(@{$changes{'admin'}{$type}},$item);
13220:                         }
13221:                     }
13222:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
13223:                     push(@{$changes{'admin'}{$type}},$item);
13224:                 }
13225:             }
13226:         }
13227:     }
13228: 
13229:     foreach my $item (@{$ordered{'default'}}) {
13230:         foreach my $type (@types) {
13231:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
13232:             if ($item eq 'types') {
13233:                 unless (($value eq 'all') || ($value eq 'dom')) {
13234:                     $value = '';
13235:                 }
13236:             } elsif ($item eq 'registered') {
13237:                 unless ($value eq '1') {
13238:                     $value = 0;
13239:                 }
13240:             } elsif ($item eq 'approval') {
13241:                 unless ($value =~ /^[012]$/) {
13242:                     $value = 0;
13243:                 }
13244:             } else {
13245:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
13246:                     $value = 'none';
13247:                 }
13248:             }
13249:             $selfenrollhash{'default'}{$type}{$item} = $value;
13250:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
13251:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
13252:                     if ($selfenrollhash{'default'}{$type}{$item} ne
13253:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
13254:                          push(@{$changes{'default'}{$type}},$item);
13255:                     }
13256:                 } else {
13257:                     push(@{$changes{'default'}{$type}},$item);
13258:                 }
13259:             } else {
13260:                 push(@{$changes{'default'}{$type}},$item);
13261:             }
13262:             if ($item eq 'limit') {
13263:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
13264:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
13265:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
13266:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
13267:                     }
13268:                 } else {
13269:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
13270:                 }
13271:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
13272:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
13273:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
13274:                          push(@{$changes{'default'}{$type}},'cap');
13275:                     }
13276:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
13277:                     push(@{$changes{'default'}{$type}},'cap');
13278:                 }
13279:             }
13280:         }
13281:     }
13282: 
13283:     foreach my $item (@{$itemsref}) {
13284:         if ($item eq 'fields') {
13285:             my @changed;
13286:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
13287:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
13288:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
13289:             }
13290:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
13291:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
13292:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
13293:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
13294:                 } else {
13295:                     @changed = @{$selfenrollhash{'validation'}{$item}};
13296:                 }
13297:             } else {
13298:                 @changed = @{$selfenrollhash{'validation'}{$item}};
13299:             }
13300:             if (@changed) {
13301:                 if ($selfenrollhash{'validation'}{$item}) { 
13302:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
13303:                 } else {
13304:                     $changes{'validation'}{$item} = &mt('None');
13305:                 }
13306:             }
13307:         } else {
13308:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
13309:             if ($item eq 'markup') {
13310:                if ($env{'form.selfenroll_validation_'.$item}) {
13311:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
13312:                }
13313:             }
13314:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
13315:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
13316:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
13317:                 }
13318:             }
13319:         }
13320:     }
13321: 
13322:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
13323:                                              $dom);
13324:     if ($putresult eq 'ok') {
13325:         if (keys(%changes) > 0) {
13326:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13327:             $resulttext = &mt('Changes made:').'<ul>';
13328:             foreach my $key ('admin','default','validation') {
13329:                 if (ref($changes{$key}) eq 'HASH') {
13330:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
13331:                     if ($key eq 'validation') {
13332:                         foreach my $item (@{$itemsref}) {
13333:                             if (exists($changes{$key}{$item})) {
13334:                                 if ($item eq 'markup') {
13335:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
13336:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
13337:                                 } else {  
13338:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
13339:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
13340:                                 }
13341:                             }
13342:                         }
13343:                     } else {
13344:                         foreach my $type (@types) {
13345:                             if ($type eq 'community') {
13346:                                 $roles{'1'} = &mt('Community personnel');
13347:                             } else {
13348:                                 $roles{'1'} = &mt('Course personnel');
13349:                             }
13350:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
13351:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
13352:                                     if ($key eq 'admin') {
13353:                                         my @mgrdc = ();
13354:                                         if (ref($ordered{$key}) eq 'ARRAY') {
13355:                                             foreach my $item (@{$ordered{'admin'}}) {
13356:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
13357:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
13358:                                                         push(@mgrdc,$item);
13359:                                                     }
13360:                                                 }
13361:                                             }
13362:                                             if (@mgrdc) {
13363:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
13364:                                             } else {
13365:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
13366:                                             }
13367:                                         }
13368:                                     } else {
13369:                                         if (ref($ordered{$key}) eq 'ARRAY') {
13370:                                             foreach my $item (@{$ordered{$key}}) {
13371:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
13372:                                                     $domdefaults{$type.'selfenroll'.$item} =
13373:                                                         $selfenrollhash{$key}{$type}{$item};
13374:                                                 }
13375:                                             }
13376:                                         }
13377:                                     }
13378:                                 }
13379:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
13380:                                 foreach my $item (@{$ordered{$key}}) {
13381:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
13382:                                         $resulttext .= '<li>';
13383:                                         if ($key eq 'admin') {
13384:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
13385:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
13386:                                         } else {
13387:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
13388:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
13389:                                         }
13390:                                         $resulttext .= '</li>';
13391:                                     }
13392:                                 }
13393:                                 $resulttext .= '</ul></li>';
13394:                             }
13395:                         }
13396:                         $resulttext .= '</ul></li>'; 
13397:                     }
13398:                 }
13399:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
13400:                     my $cachetime = 24*60*60;
13401:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13402:                     if (ref($lastactref) eq 'HASH') {
13403:                         $lastactref->{'domdefaults'} = 1;
13404:                     }
13405:                 }
13406:             }
13407:             $resulttext .= '</ul>';
13408:         } else {
13409:             $resulttext = &mt('No changes made to self-enrollment settings');
13410:         }
13411:     } else {
13412:         $resulttext = '<span class="LC_error">'.
13413:             &mt('An error occurred: [_1]',$putresult).'</span>';
13414:     }
13415:     return $resulttext;
13416: }
13417: 
13418: sub modify_usersessions {
13419:     my ($dom,$lastactref,%domconfig) = @_;
13420:     my @hostingtypes = ('version','excludedomain','includedomain');
13421:     my @offloadtypes = ('primary','default');
13422:     my %types = (
13423:                   remote => \@hostingtypes,
13424:                   hosted => \@hostingtypes,
13425:                   spares => \@offloadtypes,
13426:                 );
13427:     my @prefixes = ('remote','hosted','spares');
13428:     my @lcversions = &Apache::lonnet::all_loncaparevs();
13429:     my (%by_ip,%by_location,@intdoms,@instdoms);
13430:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
13431:     my @locations = sort(keys(%by_location));
13432:     my (%defaultshash,%changes);
13433:     foreach my $prefix (@prefixes) {
13434:         $defaultshash{'usersessions'}{$prefix} = {};
13435:     }
13436:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13437:     my $resulttext;
13438:     my %iphost = &Apache::lonnet::get_iphost();
13439:     foreach my $prefix (@prefixes) {
13440:         next if ($prefix eq 'spares');
13441:         foreach my $type (@{$types{$prefix}}) {
13442:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
13443:             if ($type eq 'version') {
13444:                 my $value = $env{'form.'.$prefix.'_'.$type};
13445:                 my $okvalue;
13446:                 if ($value ne '') {
13447:                     if (grep(/^\Q$value\E$/,@lcversions)) {
13448:                         $okvalue = $value;
13449:                     }
13450:                 }
13451:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
13452:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
13453:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
13454:                             if ($inuse == 0) {
13455:                                 $changes{$prefix}{$type} = 1;
13456:                             } else {
13457:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
13458:                                     $changes{$prefix}{$type} = 1;
13459:                                 }
13460:                                 if ($okvalue ne '') {
13461:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13462:                                 } 
13463:                             }
13464:                         } else {
13465:                             if (($inuse == 1) && ($okvalue ne '')) {
13466:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13467:                                 $changes{$prefix}{$type} = 1;
13468:                             }
13469:                         }
13470:                     } else {
13471:                         if (($inuse == 1) && ($okvalue ne '')) {
13472:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13473:                             $changes{$prefix}{$type} = 1;
13474:                         }
13475:                     }
13476:                 } else {
13477:                     if (($inuse == 1) && ($okvalue ne '')) {
13478:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13479:                         $changes{$prefix}{$type} = 1;
13480:                     }
13481:                 }
13482:             } else {
13483:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
13484:                 my @okvals;
13485:                 foreach my $val (@vals) {
13486:                     if ($val =~ /:/) {
13487:                         my @items = split(/:/,$val);
13488:                         foreach my $item (@items) {
13489:                             if (ref($by_location{$item}) eq 'ARRAY') {
13490:                                 push(@okvals,$item);
13491:                             }
13492:                         }
13493:                     } else {
13494:                         if (ref($by_location{$val}) eq 'ARRAY') {
13495:                             push(@okvals,$val);
13496:                         }
13497:                     }
13498:                 }
13499:                 @okvals = sort(@okvals);
13500:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
13501:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
13502:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13503:                             if ($inuse == 0) {
13504:                                 $changes{$prefix}{$type} = 1; 
13505:                             } else {
13506:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13507:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
13508:                                 if (@changed > 0) {
13509:                                     $changes{$prefix}{$type} = 1;
13510:                                 }
13511:                             }
13512:                         } else {
13513:                             if ($inuse == 1) {
13514:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13515:                                 $changes{$prefix}{$type} = 1;
13516:                             }
13517:                         } 
13518:                     } else {
13519:                         if ($inuse == 1) {
13520:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13521:                             $changes{$prefix}{$type} = 1;
13522:                         }
13523:                     }
13524:                 } else {
13525:                     if ($inuse == 1) {
13526:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13527:                         $changes{$prefix}{$type} = 1;
13528:                     }
13529:                 }
13530:             }
13531:         }
13532:     }
13533: 
13534:     my @alldoms = &Apache::lonnet::all_domains();
13535:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13536:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
13537:     my $savespares;
13538: 
13539:     foreach my $lonhost (sort(keys(%servers))) {
13540:         my $serverhomeID =
13541:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
13542:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
13543:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
13544:         my %spareschg;
13545:         foreach my $type (@{$types{'spares'}}) {
13546:             my @okspares;
13547:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
13548:             foreach my $server (@checked) {
13549:                 if (&Apache::lonnet::hostname($server) ne '') {
13550:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
13551:                         unless (grep(/^\Q$server\E$/,@okspares)) {
13552:                             push(@okspares,$server);
13553:                         }
13554:                     }
13555:                 }
13556:             }
13557:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
13558:             my $newspare;
13559:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
13560:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
13561:                     $newspare = $new;
13562:                 }
13563:             }
13564:             my @spares;
13565:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
13566:                 @spares = sort(@okspares,$newspare);
13567:             } else {
13568:                 @spares = sort(@okspares);
13569:             }
13570:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
13571:             if (ref($spareid{$lonhost}) eq 'HASH') {
13572:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
13573:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
13574:                     if (@diffs > 0) {
13575:                         $spareschg{$type} = 1;
13576:                     }
13577:                 }
13578:             }
13579:         }
13580:         if (keys(%spareschg) > 0) {
13581:             $changes{'spares'}{$lonhost} = \%spareschg;
13582:         }
13583:     }
13584:     $defaultshash{'usersessions'}{'offloadnow'} = {};
13585:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
13586:     my @okoffload;
13587:     if (@offloadnow) {
13588:         foreach my $server (@offloadnow) {
13589:             if (&Apache::lonnet::hostname($server) ne '') {
13590:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
13591:                     push(@okoffload,$server);
13592:                 }
13593:             }
13594:         }
13595:         if (@okoffload) {
13596:             foreach my $lonhost (@okoffload) {
13597:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
13598:             }
13599:         }
13600:     }
13601:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
13602:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
13603:             if (ref($changes{'spares'}) eq 'HASH') {
13604:                 if (keys(%{$changes{'spares'}}) > 0) {
13605:                     $savespares = 1;
13606:                 }
13607:             }
13608:         } else {
13609:             $savespares = 1;
13610:         }
13611:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
13612:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
13613:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
13614:                     $changes{'offloadnow'} = 1;
13615:                     last;
13616:                 }
13617:             }
13618:             unless ($changes{'offloadnow'}) {
13619:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
13620:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
13621:                         $changes{'offloadnow'} = 1;
13622:                         last;
13623:                     }
13624:                 }
13625:             }
13626:         } elsif (@okoffload) {
13627:             $changes{'offloadnow'} = 1;
13628:         }
13629:     } elsif (@okoffload) {
13630:         $changes{'offloadnow'} = 1;
13631:     }
13632:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
13633:     if ((keys(%changes) > 0) || ($savespares)) {
13634:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13635:                                                  $dom);
13636:         if ($putresult eq 'ok') {
13637:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13638:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
13639:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
13640:                 }
13641:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
13642:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
13643:                 }
13644:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13645:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
13646:                 }
13647:             }
13648:             my $cachetime = 24*60*60;
13649:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13650:             if (ref($lastactref) eq 'HASH') {
13651:                 $lastactref->{'domdefaults'} = 1;
13652:             }
13653:             if (keys(%changes) > 0) {
13654:                 my %lt = &usersession_titles();
13655:                 $resulttext = &mt('Changes made:').'<ul>';
13656:                 foreach my $prefix (@prefixes) {
13657:                     if (ref($changes{$prefix}) eq 'HASH') {
13658:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
13659:                         if ($prefix eq 'spares') {
13660:                             if (ref($changes{$prefix}) eq 'HASH') {
13661:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
13662:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
13663:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
13664:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
13665:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
13666:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
13667:                                         foreach my $type (@{$types{$prefix}}) {
13668:                                             if ($changes{$prefix}{$lonhost}{$type}) {
13669:                                                 my $offloadto = &mt('None');
13670:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
13671:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
13672:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
13673:                                                     }
13674:                                                 }
13675:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
13676:                                             }
13677:                                         }
13678:                                     }
13679:                                     $resulttext .= '</li>';
13680:                                 }
13681:                             }
13682:                         } else {
13683:                             foreach my $type (@{$types{$prefix}}) {
13684:                                 if (defined($changes{$prefix}{$type})) {
13685:                                     my $newvalue;
13686:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13687:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
13688:                                             if ($type eq 'version') {
13689:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
13690:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13691:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
13692:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
13693:                                                 }
13694:                                             }
13695:                                         }
13696:                                     }
13697:                                     if ($newvalue eq '') {
13698:                                         if ($type eq 'version') {
13699:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
13700:                                         } else {
13701:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
13702:                                         }
13703:                                     } else {
13704:                                         if ($type eq 'version') {
13705:                                             $newvalue .= ' '.&mt('(or later)'); 
13706:                                         }
13707:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
13708:                                     }
13709:                                 }
13710:                             }
13711:                         }
13712:                         $resulttext .= '</ul>';
13713:                     }
13714:                 }
13715:                 if ($changes{'offloadnow'}) {
13716:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13717:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
13718:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
13719:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
13720:                                 $resulttext .= '<li>'.$lonhost.'</li>';
13721:                             }
13722:                             $resulttext .= '</ul>';
13723:                         } else {
13724:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
13725:                         }
13726:                     } else {
13727:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
13728:                     }
13729:                 }
13730:                 $resulttext .= '</ul>';
13731:             } else {
13732:                 $resulttext = $nochgmsg;
13733:             }
13734:         } else {
13735:             $resulttext = '<span class="LC_error">'.
13736:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13737:         }
13738:     } else {
13739:         $resulttext = $nochgmsg;
13740:     }
13741:     return $resulttext;
13742: }
13743: 
13744: sub modify_ssl {
13745:     my ($dom,$lastactref,%domconfig) = @_;
13746:     my (%by_ip,%by_location,@intdoms,@instdoms);
13747:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
13748:     my @locations = sort(keys(%by_location));
13749:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13750:     my (%defaultshash,%changes);
13751:     my $action = 'ssl';
13752:     my @prefixes = ('connto','connfrom','replication');
13753:     foreach my $prefix (@prefixes) {
13754:         $defaultshash{$action}{$prefix} = {};
13755:     }
13756:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13757:     my $resulttext;
13758:     my %iphost = &Apache::lonnet::get_iphost();
13759:     my @reptypes = ('certreq','nocertreq');
13760:     my @connecttypes = ('dom','intdom','other');
13761:     my %types = (
13762:                   connto      => \@connecttypes,
13763:                   connfrom    => \@connecttypes,
13764:                   replication => \@reptypes,
13765:                 );
13766:     foreach my $prefix (sort(keys(%types))) {
13767:         foreach my $type (@{$types{$prefix}}) {
13768:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
13769:                 my $value = 'yes';
13770:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
13771:                     $value = $env{'form.'.$prefix.'_'.$type};
13772:                 }
13773:                 if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
13774:                     if ($domconfig{$action}{$prefix}{$type} ne '') {
13775:                         if ($value ne $domconfig{$action}{$prefix}{$type}) {
13776:                             $changes{$prefix}{$type} = 1;
13777:                         }
13778:                         $defaultshash{$action}{$prefix}{$type} = $value;
13779:                     } else {
13780:                         $defaultshash{$action}{$prefix}{$type} = $value;
13781:                         $changes{$prefix}{$type} = 1;
13782:                     }
13783:                 } else {
13784:                     $defaultshash{$action}{$prefix}{$type} = $value;
13785:                     $changes{$prefix}{$type} = 1;
13786:                 }
13787:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
13788:                     delete($changes{$prefix}{$type});
13789:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
13790:                     delete($changes{$prefix}{$type});
13791:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
13792:                     delete($changes{$prefix}{$type});
13793:                 }
13794:             } elsif ($prefix eq 'replication') {
13795:                 if (@locations > 0) {
13796:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
13797:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
13798:                     my @okvals;
13799:                     foreach my $val (@vals) {
13800:                         if ($val =~ /:/) {
13801:                             my @items = split(/:/,$val);
13802:                             foreach my $item (@items) {
13803:                                 if (ref($by_location{$item}) eq 'ARRAY') {
13804:                                     push(@okvals,$item);
13805:                                 }
13806:                             }
13807:                         } else {
13808:                             if (ref($by_location{$val}) eq 'ARRAY') {
13809:                                 push(@okvals,$val);
13810:                             }
13811:                         }
13812:                     }
13813:                     @okvals = sort(@okvals);
13814:                     if (ref($domconfig{$action}) eq 'HASH') {
13815:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
13816:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
13817:                                 if ($inuse == 0) {
13818:                                     $changes{$prefix}{$type} = 1;
13819:                                 } else {
13820:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
13821:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
13822:                                     if (@changed > 0) {
13823:                                         $changes{$prefix}{$type} = 1;
13824:                                     }
13825:                                 }
13826:                             } else {
13827:                                 if ($inuse == 1) {
13828:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
13829:                                     $changes{$prefix}{$type} = 1;
13830:                                 }
13831:                             }
13832:                         } else {
13833:                             if ($inuse == 1) {
13834:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
13835:                                 $changes{$prefix}{$type} = 1;
13836:                             }
13837:                         }
13838:                     } else {
13839:                         if ($inuse == 1) {
13840:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
13841:                             $changes{$prefix}{$type} = 1;
13842:                         }
13843:                     }
13844:                 }
13845:             }
13846:         }
13847:     }
13848:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
13849:     if (keys(%changes) > 0) {
13850:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13851:                                                  $dom);
13852:         if ($putresult eq 'ok') {
13853:             if (ref($defaultshash{$action}) eq 'HASH') {
13854:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
13855:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
13856:                 }
13857:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
13858:                     $domdefaults{'connto'} = $domconfig{$action}{'connto'};
13859:                 }
13860:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
13861:                     $domdefaults{'connfrom'} = $domconfig{$action}{'connfrom'};
13862:                 }
13863:             }
13864:             my $cachetime = 24*60*60;
13865:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13866:             if (ref($lastactref) eq 'HASH') {
13867:                 $lastactref->{'domdefaults'} = 1;
13868:             }
13869:             if (keys(%changes) > 0) {
13870:                 my %titles = &ssl_titles();
13871:                 $resulttext = &mt('Changes made:').'<ul>';
13872:                 foreach my $prefix (@prefixes) {
13873:                     if (ref($changes{$prefix}) eq 'HASH') {
13874:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
13875:                         foreach my $type (@{$types{$prefix}}) {
13876:                             if (defined($changes{$prefix}{$type})) {
13877:                                 my $newvalue;
13878:                                 if (ref($defaultshash{$action}) eq 'HASH') {
13879:                                     if (ref($defaultshash{$action}{$prefix})) {
13880:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
13881:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
13882:                                         } elsif (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
13883:                                             if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
13884:                                                 $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
13885:                                             }
13886:                                         }
13887:                                     }
13888:                                     if ($newvalue eq '') {
13889:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
13890:                                     } else {
13891:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
13892:                                     }
13893:                                 }
13894:                             }
13895:                         }
13896:                         $resulttext .= '</ul>';
13897:                     }
13898:                 }
13899:             } else {
13900:                 $resulttext = $nochgmsg;
13901:             }
13902:         } else {
13903:             $resulttext = '<span class="LC_error">'.
13904:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13905:         }
13906:     } else {
13907:         $resulttext = $nochgmsg;
13908:     }
13909:     return $resulttext;
13910: }
13911: 
13912: sub modify_trust {
13913:     my ($dom,$lastactref,%domconfig) = @_;
13914:     my (%by_ip,%by_location,@intdoms,@instdoms);
13915:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
13916:     my @locations = sort(keys(%by_location));
13917:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
13918:     my @types = ('exc','inc');
13919:     my (%defaultshash,%changes);
13920:     foreach my $prefix (@prefixes) {
13921:         $defaultshash{'trust'}{$prefix} = {};
13922:     }
13923:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13924:     my $resulttext;
13925:     foreach my $prefix (@prefixes) {
13926:         foreach my $type (@types) {
13927:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
13928:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
13929:             my @okvals;
13930:             foreach my $val (@vals) {
13931:                 if ($val =~ /:/) {
13932:                     my @items = split(/:/,$val);
13933:                     foreach my $item (@items) {
13934:                         if (ref($by_location{$item}) eq 'ARRAY') {
13935:                             push(@okvals,$item);
13936:                         }
13937:                     }
13938:                 } else {
13939:                     if (ref($by_location{$val}) eq 'ARRAY') {
13940:                         push(@okvals,$val);
13941:                     }
13942:                 }
13943:             }
13944:             @okvals = sort(@okvals);
13945:             if (ref($domconfig{'trust'}) eq 'HASH') {
13946:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
13947:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
13948:                         if ($inuse == 0) {
13949:                             $changes{$prefix}{$type} = 1;
13950:                         } else {
13951:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
13952:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
13953:                             if (@changed > 0) {
13954:                                 $changes{$prefix}{$type} = 1;
13955:                             }
13956:                         }
13957:                     } else {
13958:                         if ($inuse == 1) {
13959:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
13960:                             $changes{$prefix}{$type} = 1;
13961:                         }
13962:                     }
13963:                 } else {
13964:                     if ($inuse == 1) {
13965:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
13966:                         $changes{$prefix}{$type} = 1;
13967:                     }
13968:                 }
13969:             } else {
13970:                 if ($inuse == 1) {
13971:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
13972:                     $changes{$prefix}{$type} = 1;
13973:                 }
13974:             }
13975:         }
13976:     }
13977:     my $nochgmsg = &mt('No changes made to trust settings.');
13978:     if (keys(%changes) > 0) {
13979:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13980:                                                  $dom);
13981:         if ($putresult eq 'ok') {
13982:             if (ref($defaultshash{'trust'}) eq 'HASH') {
13983:                 foreach my $prefix (@prefixes) {
13984:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
13985:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
13986:                     }
13987:                 }
13988:             }
13989:             my $cachetime = 24*60*60;
13990:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13991:             if (ref($lastactref) eq 'HASH') {
13992:                 $lastactref->{'domdefaults'} = 1;
13993:             }
13994:             if (keys(%changes) > 0) {
13995:                 my %lt = &trust_titles();
13996:                 $resulttext = &mt('Changes made:').'<ul>';
13997:                 foreach my $prefix (@prefixes) {
13998:                     if (ref($changes{$prefix}) eq 'HASH') {
13999:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
14000:                         foreach my $type (@types) {
14001:                             if (defined($changes{$prefix}{$type})) {
14002:                                 my $newvalue;
14003:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
14004:                                     if (ref($defaultshash{'trust'}{$prefix})) {
14005:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
14006:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
14007:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
14008:                                             }
14009:                                         }
14010:                                     }
14011:                                 }
14012:                                 if ($newvalue eq '') {
14013:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
14014:                                 } else {
14015:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
14016:                                 }
14017:                             }
14018:                         }
14019:                         $resulttext .= '</ul>';
14020:                     }
14021:                 }
14022:                 $resulttext .= '</ul>';
14023:             } else {
14024:                 $resulttext = $nochgmsg;
14025:             }
14026:         } else {
14027:             $resulttext = '<span class="LC_error">'.
14028:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14029:         }
14030:     } else {
14031:         $resulttext = $nochgmsg;
14032:     }
14033:     return $resulttext;
14034: }
14035: 
14036: sub modify_loadbalancing {
14037:     my ($dom,%domconfig) = @_;
14038:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
14039:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
14040:     my ($othertitle,$usertypes,$types) =
14041:         &Apache::loncommon::sorted_inst_types($dom);
14042:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14043:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
14044:     my @sparestypes = ('primary','default');
14045:     my %typetitles = &sparestype_titles();
14046:     my $resulttext;
14047:     my (%currbalancer,%currtargets,%currrules,%existing);
14048:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
14049:         %existing = %{$domconfig{'loadbalancing'}};
14050:     }
14051:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
14052:                               \%currtargets,\%currrules);
14053:     my ($saveloadbalancing,%defaultshash,%changes);
14054:     my ($alltypes,$othertypes,$titles) =
14055:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
14056:     my %ruletitles = &offloadtype_text();
14057:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
14058:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
14059:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
14060:         if ($balancer eq '') {
14061:             next;
14062:         }
14063:         if (!exists($servers{$balancer})) {
14064:             if (exists($currbalancer{$balancer})) {
14065:                 push(@{$changes{'delete'}},$balancer);
14066:             }
14067:             next;
14068:         }
14069:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
14070:             push(@{$changes{'delete'}},$balancer);
14071:             next;
14072:         }
14073:         if (!exists($currbalancer{$balancer})) {
14074:             push(@{$changes{'add'}},$balancer);
14075:         }
14076:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
14077:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
14078:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
14079:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
14080:             $saveloadbalancing = 1;
14081:         }
14082:         foreach my $sparetype (@sparestypes) {
14083:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
14084:             my @offloadto;
14085:             foreach my $target (@targets) {
14086:                 if (($servers{$target}) && ($target ne $balancer)) {
14087:                     if ($sparetype eq 'default') {
14088:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
14089:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
14090:                         }
14091:                     }
14092:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
14093:                         push(@offloadto,$target);
14094:                     }
14095:                 }
14096:             }
14097:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
14098:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
14099:                     push(@offloadto,$balancer);
14100:                 }
14101:             }
14102:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
14103:         }
14104:         if (ref($currtargets{$balancer}) eq 'HASH') {
14105:             foreach my $sparetype (@sparestypes) {
14106:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
14107:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
14108:                     if (@targetdiffs > 0) {
14109:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14110:                     }
14111:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14112:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14113:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14114:                     }
14115:                 }
14116:             }
14117:         } else {
14118:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
14119:                 foreach my $sparetype (@sparestypes) {
14120:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14121:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14122:                             $changes{'curr'}{$balancer}{'targets'} = 1;
14123:                         }
14124:                     }
14125:                 }
14126:             }
14127:         }
14128:         my $ishomedom;
14129:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
14130:             $ishomedom = 1;
14131:         }
14132:         if (ref($alltypes) eq 'ARRAY') {
14133:             foreach my $type (@{$alltypes}) {
14134:                 my $rule;
14135:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
14136:                          (!$ishomedom)) {
14137:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
14138:                 }
14139:                 if ($rule eq 'specific') {
14140:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
14141:                     if (exists($servers{$specifiedhost})) {
14142:                         $rule = $specifiedhost;
14143:                     }
14144:                 }
14145:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
14146:                 if (ref($currrules{$balancer}) eq 'HASH') {
14147:                     if ($rule ne $currrules{$balancer}{$type}) {
14148:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14149:                     }
14150:                 } elsif ($rule ne '') {
14151:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14152:                 }
14153:             }
14154:         }
14155:     }
14156:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
14157:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
14158:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
14159:             $defaultshash{'loadbalancing'} = {};
14160:         }
14161:         my $putresult = &Apache::lonnet::put_dom('configuration',
14162:                                                  \%defaultshash,$dom);
14163:         if ($putresult eq 'ok') {
14164:             if (keys(%changes) > 0) {
14165:                 my %toupdate;
14166:                 if (ref($changes{'delete'}) eq 'ARRAY') {
14167:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
14168:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
14169:                         $toupdate{$balancer} = 1;
14170:                     }
14171:                 }
14172:                 if (ref($changes{'add'}) eq 'ARRAY') {
14173:                     foreach my $balancer (sort(@{$changes{'add'}})) {
14174:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
14175:                         $toupdate{$balancer} = 1;
14176:                     }
14177:                 }
14178:                 if (ref($changes{'curr'}) eq 'HASH') {
14179:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
14180:                         $toupdate{$balancer} = 1;
14181:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
14182:                             if ($changes{'curr'}{$balancer}{'targets'}) {
14183:                                 my %offloadstr;
14184:                                 foreach my $sparetype (@sparestypes) {
14185:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14186:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14187:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14188:                                         }
14189:                                     }
14190:                                 }
14191:                                 if (keys(%offloadstr) == 0) {
14192:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
14193:                                 } else {
14194:                                     my $showoffload;
14195:                                     foreach my $sparetype (@sparestypes) {
14196:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
14197:                                         if (defined($offloadstr{$sparetype})) {
14198:                                             $showoffload .= $offloadstr{$sparetype};
14199:                                         } else {
14200:                                             $showoffload .= &mt('None');
14201:                                         }
14202:                                         $showoffload .= ('&nbsp;'x3);
14203:                                     }
14204:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
14205:                                 }
14206:                             }
14207:                         }
14208:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
14209:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
14210:                                 foreach my $type (@{$alltypes}) {
14211:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
14212:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
14213:                                         my $balancetext;
14214:                                         if ($rule eq '') {
14215:                                             $balancetext =  $ruletitles{'default'};
14216:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
14217:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
14218:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
14219:                                                 foreach my $sparetype (@sparestypes) {
14220:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14221:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14222:                                                     }
14223:                                                 }
14224:                                                 foreach my $item (@{$alltypes}) {
14225:                                                     next if ($item =~  /^_LC_ipchange/);
14226:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
14227:                                                     if ($hasrule eq 'homeserver') {
14228:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
14229:                                                     } else {
14230:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
14231:                                                             if ($servers{$hasrule}) {
14232:                                                                 $toupdate{$hasrule} = 1;
14233:                                                             }
14234:                                                         }
14235:                                                     }
14236:                                                 }
14237:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
14238:                                                     $balancetext =  $ruletitles{$rule};
14239:                                                 } else {
14240:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
14241:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
14242:                                                     if ($receiver) {
14243:                                                         $toupdate{$receiver};
14244:                                                     }
14245:                                                 }
14246:                                             } else {
14247:                                                 $balancetext =  $ruletitles{$rule};
14248:                                             }
14249:                                         } else {
14250:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
14251:                                         }
14252:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
14253:                                     }
14254:                                 }
14255:                             }
14256:                         }
14257:                         if (keys(%toupdate)) {
14258:                             my %thismachine;
14259:                             my $updatedhere;
14260:                             my $cachetime = 60*60*24;
14261:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
14262:                             foreach my $lonhost (keys(%toupdate)) {
14263:                                 if ($thismachine{$lonhost}) {
14264:                                     unless ($updatedhere) {
14265:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
14266:                                                                       $defaultshash{'loadbalancing'},
14267:                                                                       $cachetime);
14268:                                         $updatedhere = 1;
14269:                                     }
14270:                                 } else {
14271:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
14272:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
14273:                                 }
14274:                             }
14275:                         }
14276:                     }
14277:                 }
14278:                 if ($resulttext ne '') {
14279:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
14280:                 } else {
14281:                     $resulttext = $nochgmsg;
14282:                 }
14283:             } else {
14284:                 $resulttext = $nochgmsg;
14285:             }
14286:         } else {
14287:             $resulttext = '<span class="LC_error">'.
14288:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14289:         }
14290:     } else {
14291:         $resulttext = $nochgmsg;
14292:     }
14293:     return $resulttext;
14294: }
14295: 
14296: sub recurse_check {
14297:     my ($chkcats,$categories,$depth,$name) = @_;
14298:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
14299:         my $chg = 0;
14300:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
14301:             my $category = $chkcats->[$depth]{$name}[$j];
14302:             my $item;
14303:             if ($category eq '') {
14304:                 $chg ++;
14305:             } else {
14306:                 my $deeper = $depth + 1;
14307:                 $item = &escape($category).':'.&escape($name).':'.$depth;
14308:                 if ($chg) {
14309:                     $categories->{$item} -= $chg;
14310:                 }
14311:                 &recurse_check($chkcats,$categories,$deeper,$category);
14312:                 $deeper --;
14313:             }
14314:         }
14315:     }
14316:     return;
14317: }
14318: 
14319: sub recurse_cat_deletes {
14320:     my ($item,$coursecategories,$deletions) = @_;
14321:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
14322:     my $subdepth = $depth + 1;
14323:     if (ref($coursecategories) eq 'HASH') {
14324:         foreach my $subitem (keys(%{$coursecategories})) {
14325:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
14326:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
14327:                 delete($coursecategories->{$subitem});
14328:                 $deletions->{$subitem} = 1;
14329:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
14330:             }
14331:         }
14332:     }
14333:     return;
14334: }
14335: 
14336: sub active_dc_picker {
14337:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
14338:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
14339:     my @domcoord = keys(%domcoords);
14340:     if (keys(%currhash)) {
14341:         foreach my $dc (keys(%currhash)) {
14342:             unless (exists($domcoords{$dc})) {
14343:                 push(@domcoord,$dc);
14344:             }
14345:         }
14346:     }
14347:     @domcoord = sort(@domcoord);
14348:     my $numdcs = scalar(@domcoord);
14349:     my $rows = 0;
14350:     my $table;
14351:     if ($numdcs > 1) {
14352:         $table = '<table>';
14353:         for (my $i=0; $i<@domcoord; $i++) {
14354:             my $rem = $i%($numinrow);
14355:             if ($rem == 0) {
14356:                 if ($i > 0) {
14357:                     $table .= '</tr>';
14358:                 }
14359:                 $table .= '<tr>';
14360:                 $rows ++;
14361:             }
14362:             my $check = '';
14363:             if ($inputtype eq 'radio') {
14364:                 if (keys(%currhash) == 0) {
14365:                     if (!$i) {
14366:                         $check = ' checked="checked"';
14367:                     }
14368:                 } elsif (exists($currhash{$domcoord[$i]})) {
14369:                     $check = ' checked="checked"';
14370:                 }
14371:             } else {
14372:                 if (exists($currhash{$domcoord[$i]})) {
14373:                     $check = ' checked="checked"';
14374:                 }
14375:             }
14376:             if ($i == @domcoord - 1) {
14377:                 my $colsleft = $numinrow - $rem;
14378:                 if ($colsleft > 1) {
14379:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
14380:                 } else {
14381:                     $table .= '<td class="LC_left_item">';
14382:                 }
14383:             } else {
14384:                 $table .= '<td class="LC_left_item">';
14385:             }
14386:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
14387:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14388:             $table .= '<span class="LC_nobreak"><label>'.
14389:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
14390:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
14391:             if ($user ne $dcname.':'.$dcdom) {
14392:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14393:             }
14394:             $table .= '</label></span></td>';
14395:         }
14396:         $table .= '</tr></table>';
14397:     } elsif ($numdcs == 1) {
14398:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
14399:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14400:         if ($inputtype eq 'radio') {
14401:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
14402:             if ($user ne $dcname.':'.$dcdom) {
14403:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14404:             }
14405:         } else {
14406:             my $check;
14407:             if (exists($currhash{$domcoord[0]})) {
14408:                 $check = ' checked="checked"';
14409:             }
14410:             $table = '<span class="LC_nobreak"><label>'.
14411:                      '<input type="checkbox" name="'.$name.'" '.
14412:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
14413:             if ($user ne $dcname.':'.$dcdom) {
14414:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14415:             }
14416:             $table .= '</label></span>';
14417:             $rows ++;
14418:         }
14419:     }
14420:     return ($numdcs,$table,$rows);
14421: }
14422: 
14423: sub usersession_titles {
14424:     return &Apache::lonlocal::texthash(
14425:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
14426:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
14427:                spares => 'Servers offloaded to, when busy',
14428:                version => 'LON-CAPA version requirement',
14429:                excludedomain => 'Allow all, but exclude specific domains',
14430:                includedomain => 'Deny all, but include specific domains',
14431:                primary => 'Primary (checked first)',
14432:                default => 'Default',
14433:            );
14434: }
14435: 
14436: sub id_for_thisdom {
14437:     my (%servers) = @_;
14438:     my %altids;
14439:     foreach my $server (keys(%servers)) {
14440:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
14441:         if ($serverhome ne $server) {
14442:             $altids{$serverhome} = $server;
14443:         }
14444:     }
14445:     return %altids;
14446: }
14447: 
14448: sub count_servers {
14449:     my ($currbalancer,%servers) = @_;
14450:     my (@spares,$numspares);
14451:     foreach my $lonhost (sort(keys(%servers))) {
14452:         next if ($currbalancer eq $lonhost);
14453:         push(@spares,$lonhost);
14454:     }
14455:     if ($currbalancer) {
14456:         $numspares = scalar(@spares);
14457:     } else {
14458:         $numspares = scalar(@spares) - 1;
14459:     }
14460:     return ($numspares,@spares);
14461: }
14462: 
14463: sub lonbalance_targets_js {
14464:     my ($dom,$types,$servers,$settings) = @_;
14465:     my $select = &mt('Select');
14466:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
14467:     if (ref($servers) eq 'HASH') {
14468:         $alltargets = join("','",sort(keys(%{$servers})));
14469:         my @homedoms;
14470:         foreach my $server (sort(keys(%{$servers}))) {
14471:             if (&Apache::lonnet::host_domain($server) eq $dom) {
14472:                 push(@homedoms,'1');
14473:             } else {
14474:                 push(@homedoms,'0');
14475:             }
14476:         }
14477:         $allishome = join("','",@homedoms);
14478:     }
14479:     if (ref($types) eq 'ARRAY') {
14480:         if (@{$types} > 0) {
14481:             @alltypes = @{$types};
14482:         }
14483:     }
14484:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
14485:     $allinsttypes = join("','",@alltypes);
14486:     my (%currbalancer,%currtargets,%currrules,%existing);
14487:     if (ref($settings) eq 'HASH') {
14488:         %existing = %{$settings};
14489:     }
14490:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
14491:                               \%currtargets,\%currrules);
14492:     my $balancers = join("','",sort(keys(%currbalancer)));
14493:     return <<"END";
14494: 
14495: <script type="text/javascript">
14496: // <![CDATA[
14497: 
14498: currBalancers = new Array('$balancers');
14499: 
14500: function toggleTargets(balnum) {
14501:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
14502:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
14503:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
14504:     var prevbalancer = prevhostitem.value;
14505:     var baltotal = document.getElementById('loadbalancing_total').value;
14506:     prevhostitem.value = balancer;
14507:     if (prevbalancer != '') {
14508:         var prevIdx = currBalancers.indexOf(prevbalancer);
14509:         if (prevIdx != -1) {
14510:             currBalancers.splice(prevIdx,1);
14511:         }
14512:     }
14513:     if (balancer == '') {
14514:         hideSpares(balnum);
14515:     } else {
14516:         var currIdx = currBalancers.indexOf(balancer);
14517:         if (currIdx == -1) {
14518:             currBalancers.push(balancer);
14519:         }
14520:         var homedoms = new Array('$allishome');
14521:         var ishomedom = homedoms[lonhostitem.selectedIndex];
14522:         showSpares(balancer,ishomedom,balnum);
14523:     }
14524:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
14525:     return;
14526: }
14527: 
14528: function showSpares(balancer,ishomedom,balnum) {
14529:     var alltargets = new Array('$alltargets');
14530:     var insttypes = new Array('$allinsttypes');
14531:     var offloadtypes = new Array('primary','default');
14532: 
14533:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
14534:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
14535:  
14536:     for (var i=0; i<offloadtypes.length; i++) {
14537:         var count = 0;
14538:         for (var j=0; j<alltargets.length; j++) {
14539:             if (alltargets[j] != balancer) {
14540:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
14541:                 item.value = alltargets[j];
14542:                 item.style.textAlign='left';
14543:                 item.style.textFace='normal';
14544:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
14545:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
14546:                     item.disabled = '';
14547:                 } else {
14548:                     item.disabled = 'disabled';
14549:                     item.checked = false;
14550:                 }
14551:                 count ++;
14552:             }
14553:         }
14554:     }
14555:     for (var k=0; k<insttypes.length; k++) {
14556:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
14557:             if (ishomedom == 1) {
14558:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
14559:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
14560:             } else {
14561:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
14562:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
14563:             }
14564:         } else {
14565:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
14566:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
14567:         }
14568:         if ((insttypes[k] != '_LC_external') && 
14569:             ((insttypes[k] != '_LC_internetdom') ||
14570:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
14571:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
14572:             item.options.length = 0;
14573:             item.options[0] = new Option("","",true,true);
14574:             var idx = 0;
14575:             for (var m=0; m<alltargets.length; m++) {
14576:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
14577:                     idx ++;
14578:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
14579:                 }
14580:             }
14581:         }
14582:     }
14583:     return;
14584: }
14585: 
14586: function hideSpares(balnum) {
14587:     var alltargets = new Array('$alltargets');
14588:     var insttypes = new Array('$allinsttypes');
14589:     var offloadtypes = new Array('primary','default');
14590: 
14591:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
14592:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
14593: 
14594:     var total = alltargets.length - 1;
14595:     for (var i=0; i<offloadtypes; i++) {
14596:         for (var j=0; j<total; j++) {
14597:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
14598:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
14599:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
14600:         }
14601:     }
14602:     for (var k=0; k<insttypes.length; k++) {
14603:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
14604:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
14605:         if (insttypes[k] != '_LC_external') {
14606:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
14607:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
14608:         }
14609:     }
14610:     return;
14611: }
14612: 
14613: function checkOffloads(item,balnum,type) {
14614:     var alltargets = new Array('$alltargets');
14615:     var offloadtypes = new Array('primary','default');
14616:     if (item.checked) {
14617:         var total = alltargets.length - 1;
14618:         var other;
14619:         if (type == offloadtypes[0]) {
14620:             other = offloadtypes[1];
14621:         } else {
14622:             other = offloadtypes[0];
14623:         }
14624:         for (var i=0; i<total; i++) {
14625:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
14626:             if (server == item.value) {
14627:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
14628:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
14629:                 }
14630:             }
14631:         }
14632:     }
14633:     return;
14634: }
14635: 
14636: function singleServerToggle(balnum,type) {
14637:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
14638:     if (offloadtoSelIdx == 0) {
14639:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
14640:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
14641: 
14642:     } else {
14643:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
14644:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
14645:     }
14646:     return;
14647: }
14648: 
14649: function balanceruleChange(formname,balnum,type) {
14650:     if (type == '_LC_external') {
14651:         return;
14652:     }
14653:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
14654:     for (var i=0; i<typesRules.length; i++) {
14655:         if (formname.elements[typesRules[i]].checked) {
14656:             if (formname.elements[typesRules[i]].value != 'specific') {
14657:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
14658:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
14659:             } else {
14660:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
14661:             }
14662:         }
14663:     }
14664:     return;
14665: }
14666: 
14667: function balancerDeleteChange(balnum) {
14668:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
14669:     var baltotal = document.getElementById('loadbalancing_total').value;
14670:     var addtarget;
14671:     var removetarget;
14672:     var action = 'delete';
14673:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
14674:         var lonhost = hostitem.value;
14675:         var currIdx = currBalancers.indexOf(lonhost);
14676:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
14677:             if (currIdx != -1) {
14678:                 currBalancers.splice(currIdx,1);
14679:             }
14680:             addtarget = lonhost;
14681:         } else {
14682:             if (currIdx == -1) {
14683:                 currBalancers.push(lonhost);
14684:             }
14685:             removetarget = lonhost;
14686:             action = 'undelete';
14687:         }
14688:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
14689:     }
14690:     return;
14691: }
14692: 
14693: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
14694:     if (baltotal > 1) {
14695:         var offloadtypes = new Array('primary','default');
14696:         var alltargets = new Array('$alltargets');
14697:         var insttypes = new Array('$allinsttypes');
14698:         for (var i=0; i<baltotal; i++) {
14699:             if (i != balnum) {
14700:                 for (var j=0; j<offloadtypes.length; j++) {
14701:                     var total = alltargets.length - 1;
14702:                     for (var k=0; k<total; k++) {
14703:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
14704:                         var server = serveritem.value;
14705:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
14706:                             if (server == addtarget) {
14707:                                 serveritem.disabled = '';
14708:                             }
14709:                         }
14710:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
14711:                             if (server == removetarget) {
14712:                                 serveritem.disabled = 'disabled';
14713:                                 serveritem.checked = false;
14714:                             }
14715:                         }
14716:                     }
14717:                 }
14718:                 for (var j=0; j<insttypes.length; j++) {
14719:                     if (insttypes[j] != '_LC_external') {
14720:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
14721:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
14722:                             var currSel = singleserver.selectedIndex;
14723:                             var currVal = singleserver.options[currSel].value;
14724:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
14725:                                 var numoptions = singleserver.options.length;
14726:                                 var needsnew = 1;
14727:                                 for (var k=0; k<numoptions; k++) {
14728:                                     if (singleserver.options[k] == addtarget) {
14729:                                         needsnew = 0;
14730:                                         break;
14731:                                     }
14732:                                 }
14733:                                 if (needsnew == 1) {
14734:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
14735:                                 }
14736:                             }
14737:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
14738:                                 singleserver.options.length = 0;
14739:                                 if ((currVal) && (currVal != removetarget)) {
14740:                                     singleserver.options[0] = new Option("","",false,false);
14741:                                 } else {
14742:                                     singleserver.options[0] = new Option("","",true,true);
14743:                                 }
14744:                                 var idx = 0;
14745:                                 for (var m=0; m<alltargets.length; m++) {
14746:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
14747:                                         idx ++;
14748:                                         if (currVal == alltargets[m]) {
14749:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
14750:                                         } else {
14751:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
14752:                                         }
14753:                                     }
14754:                                 }
14755:                             }
14756:                         }
14757:                     }
14758:                 }
14759:             }
14760:         }
14761:     }
14762:     return;
14763: }
14764: 
14765: // ]]>
14766: </script>
14767: 
14768: END
14769: }
14770: 
14771: sub new_spares_js {
14772:     my @sparestypes = ('primary','default');
14773:     my $types = join("','",@sparestypes);
14774:     my $select = &mt('Select');
14775:     return <<"END";
14776: 
14777: <script type="text/javascript">
14778: // <![CDATA[
14779: 
14780: function updateNewSpares(formname,lonhost) {
14781:     var types = new Array('$types');
14782:     var include = new Array();
14783:     var exclude = new Array();
14784:     for (var i=0; i<types.length; i++) {
14785:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
14786:         for (var j=0; j<spareboxes.length; j++) {
14787:             if (formname.elements[spareboxes[j]].checked) {
14788:                 exclude.push(formname.elements[spareboxes[j]].value);
14789:             } else {
14790:                 include.push(formname.elements[spareboxes[j]].value);
14791:             }
14792:         }
14793:     }
14794:     for (var i=0; i<types.length; i++) {
14795:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
14796:         var selIdx = newSpare.selectedIndex;
14797:         var currnew = newSpare.options[selIdx].value;
14798:         var okSpares = new Array();
14799:         for (var j=0; j<newSpare.options.length; j++) {
14800:             var possible = newSpare.options[j].value;
14801:             if (possible != '') {
14802:                 if (exclude.indexOf(possible) == -1) {
14803:                     okSpares.push(possible);
14804:                 } else {
14805:                     if (currnew == possible) {
14806:                         selIdx = 0;
14807:                     }
14808:                 }
14809:             }
14810:         }
14811:         for (var k=0; k<include.length; k++) {
14812:             if (okSpares.indexOf(include[k]) == -1) {
14813:                 okSpares.push(include[k]);
14814:             }
14815:         }
14816:         okSpares.sort();
14817:         newSpare.options.length = 0;
14818:         if (selIdx == 0) {
14819:             newSpare.options[0] = new Option("$select","",true,true);
14820:         } else {
14821:             newSpare.options[0] = new Option("$select","",false,false);
14822:         }
14823:         for (var m=0; m<okSpares.length; m++) {
14824:             var idx = m+1;
14825:             var selThis = 0;
14826:             if (selIdx != 0) {
14827:                 if (okSpares[m] == currnew) {
14828:                     selThis = 1;
14829:                 }
14830:             }
14831:             if (selThis == 1) {
14832:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
14833:             } else {
14834:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
14835:             }
14836:         }
14837:     }
14838:     return;
14839: }
14840: 
14841: function checkNewSpares(lonhost,type) {
14842:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
14843:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
14844:     if (chosen != '') { 
14845:         var othertype;
14846:         var othernewSpare;
14847:         if (type == 'primary') {
14848:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
14849:         }
14850:         if (type == 'default') {
14851:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
14852:         }
14853:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
14854:             othernewSpare.selectedIndex = 0;
14855:         }
14856:     }
14857:     return;
14858: }
14859: 
14860: // ]]>
14861: </script>
14862: 
14863: END
14864: 
14865: }
14866: 
14867: sub common_domprefs_js {
14868:     return <<"END";
14869: 
14870: <script type="text/javascript">
14871: // <![CDATA[
14872: 
14873: function getIndicesByName(formname,item) {
14874:     var group = new Array();
14875:     for (var i=0;i<formname.elements.length;i++) {
14876:         if (formname.elements[i].name == item) {
14877:             group.push(formname.elements[i].id);
14878:         }
14879:     }
14880:     return group;
14881: }
14882: 
14883: // ]]>
14884: </script>
14885: 
14886: END
14887: 
14888: }
14889: 
14890: sub recaptcha_js {
14891:     my %lt = &captcha_phrases();
14892:     return <<"END";
14893: 
14894: <script type="text/javascript">
14895: // <![CDATA[
14896: 
14897: function updateCaptcha(caller,context) {
14898:     var privitem;
14899:     var pubitem;
14900:     var privtext;
14901:     var pubtext;
14902:     var versionitem;
14903:     var versiontext;
14904:     if (document.getElementById(context+'_recaptchapub')) {
14905:         pubitem = document.getElementById(context+'_recaptchapub');
14906:     } else {
14907:         return;
14908:     }
14909:     if (document.getElementById(context+'_recaptchapriv')) {
14910:         privitem = document.getElementById(context+'_recaptchapriv');
14911:     } else {
14912:         return;
14913:     }
14914:     if (document.getElementById(context+'_recaptchapubtxt')) {
14915:         pubtext = document.getElementById(context+'_recaptchapubtxt');
14916:     } else {
14917:         return;
14918:     }
14919:     if (document.getElementById(context+'_recaptchaprivtxt')) {
14920:         privtext = document.getElementById(context+'_recaptchaprivtxt');
14921:     } else {
14922:         return;
14923:     }
14924:     if (document.getElementById(context+'_recaptchaversion')) {
14925:         versionitem = document.getElementById(context+'_recaptchaversion');
14926:     } else {
14927:         return;
14928:     }
14929:     if (document.getElementById(context+'_recaptchavertxt')) {
14930:         versiontext = document.getElementById(context+'_recaptchavertxt');
14931:     } else {
14932:         return;
14933:     }
14934:     if (caller.checked) {
14935:         if (caller.value == 'recaptcha') {
14936:             pubitem.type = 'text';
14937:             privitem.type = 'text';
14938:             pubitem.size = '40';
14939:             privitem.size = '40';
14940:             pubtext.innerHTML = "$lt{'pub'}";
14941:             privtext.innerHTML = "$lt{'priv'}";
14942:             versionitem.type = 'text';
14943:             versionitem.size = '3';
14944:             versiontext.innerHTML = "$lt{'ver'}";
14945:         } else {
14946:             pubitem.type = 'hidden';
14947:             privitem.type = 'hidden';
14948:             versionitem.type = 'hidden';
14949:             pubtext.innerHTML = '';
14950:             privtext.innerHTML = '';
14951:             versiontext.innerHTML = '';
14952:         }
14953:     }
14954:     return;
14955: }
14956: 
14957: // ]]>
14958: </script>
14959: 
14960: END
14961: 
14962: }
14963: 
14964: sub toggle_display_js {
14965:     return <<"END";
14966: 
14967: <script type="text/javascript">
14968: // <![CDATA[
14969: 
14970: function toggleDisplay(domForm,caller) {
14971:     if (document.getElementById(caller)) {
14972:         var divitem = document.getElementById(caller);
14973:         var optionsElement = domForm.coursecredits;
14974:         var checkval = 1;
14975:         var dispval = 'block';
14976:         if (caller == 'emailoptions') {
14977:             optionsElement = domForm.cancreate_email; 
14978:         }
14979:         if (caller == 'studentsubmission') {
14980:             optionsElement = domForm.postsubmit;
14981:         }
14982:         if (caller == 'cloneinstcode') {
14983:             optionsElement = domForm.canclone;
14984:             checkval = 'instcode';
14985:         }
14986:         if (optionsElement.length) {
14987:             var currval;
14988:             for (var i=0; i<optionsElement.length; i++) {
14989:                 if (optionsElement[i].checked) {
14990:                    currval = optionsElement[i].value;
14991:                 }
14992:             }
14993:             if (currval == checkval) {
14994:                 divitem.style.display = dispval;
14995:             } else {
14996:                 divitem.style.display = 'none';
14997:             }
14998:         }
14999:     }
15000:     return;
15001: }
15002: 
15003: // ]]>
15004: </script>
15005: 
15006: END
15007: 
15008: }
15009: 
15010: sub captcha_phrases {
15011:     return &Apache::lonlocal::texthash (
15012:                  priv => 'Private key',
15013:                  pub  => 'Public key',
15014:                  original  => 'original (CAPTCHA)',
15015:                  recaptcha => 'successor (ReCAPTCHA)',
15016:                  notused   => 'unused',
15017:                  ver => 'ReCAPTCHA version (1 or 2)',
15018:     );
15019: }
15020: 
15021: sub devalidate_remote_domconfs {
15022:     my ($dom,$cachekeys) = @_;
15023:     return unless (ref($cachekeys) eq 'HASH');
15024:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
15025:     my %thismachine;
15026:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
15027:     my @posscached = ('domainconfig','domdefaults','ltitools');
15028:     if (keys(%servers)) {
15029:         foreach my $server (keys(%servers)) {
15030:             next if ($thismachine{$server});
15031:             my @cached;
15032:             foreach my $name (@posscached) {
15033:                 if ($cachekeys->{$name}) {
15034:                     push(@cached,&escape($name).':'.&escape($dom));
15035:                 }
15036:             }
15037:             if (@cached) {
15038:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
15039:             }
15040:         }
15041:     }
15042:     return;
15043: }
15044: 
15045: 1;

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