File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.286: download - view: text, annotated - select for diffs
Wed Jan 18 21:24:39 2017 UTC (7 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Helpdesk web form
  - Domain configuration to specify which form fields besides E-mail address,
    Subject, and Description should be shown (some can be required fields).
  - Domain configuration to send submitted form to specified e-mail address(es)
    for user from other domains -- if no recipient set in user's domain for help).
  - Domain configuration to automatically include a specified string in the e-mail
    sent -- either prepended to the subject, or to the body.
  - If form is visited by a logged-in user with a role selected in an official
    course, the user can change the course code in the linked select boxes,
    in order to ask about a different course.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.286 2017/01/18 21:24:39 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 => "Replicating domain's published content",
  503:                               col2 => 'Rules'}],
  504:                   print => \&print_ssl,
  505:                   modify => \&modify_ssl,
  506:                  },
  507:         'trust' =>
  508:                  {text   => 'Trust Settings',
  509:                   help   => 'Domain_Configuration_Trust',
  510:                   header => [{col1 => "Access to this domain's content by others",
  511:                               col2 => 'Rules'},
  512:                              {col1 => "Access to other domain's content by this domain",
  513:                               col2 => 'Rules'},
  514:                              {col1 => "Enrollment in this domain's courses by others",
  515:                               col2 => 'Rules',},
  516:                              {col1 => "Co-author roles in this domain for others",
  517:                               col2 => 'Rules',},
  518:                              {col1 => "Co-author roles for this domain's users elsewhere",
  519:                               col2 => 'Rules',},
  520:                              {col1 => "Domain roles in this domain assignable to others",
  521:                               col2 => 'Rules'},
  522:                              {col1 => "Course catalog for this domain displayed elsewhere",
  523:                               col2 => 'Rules'},
  524:                              {col1 => "Requests for creation of courses in this domain by others",
  525:                               col2 => 'Rules'},
  526:                              {col1 => "Users in other domains can send messages to this domain",
  527:                               col2 => 'Rules'},],
  528:                   print => \&print_trust,
  529:                   modify => \&modify_trust,
  530:                  },
  531:     );
  532:     if (keys(%servers) > 1) {
  533:         $prefs{'login'}  = { text   => 'Log-in page options',
  534:                              help   => 'Domain_Configuration_Login_Page',
  535:                             header => [{col1 => 'Log-in Service',
  536:                                         col2 => 'Server Setting',},
  537:                                        {col1 => 'Log-in Page Items',
  538:                                         col2 => ''},
  539:                                        {col1 => 'Log-in Help',
  540:                                         col2 => 'Value'},
  541:                                        {col1 => 'Custom HTML in document head',
  542:                                         col2 => 'Value'}],
  543:                             print => \&print_login,
  544:                             modify => \&modify_login,
  545:                            };
  546:     }
  547: 
  548:     my @roles = ('student','coordinator','author','admin');
  549:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  550:     &Apache::lonhtmlcommon::add_breadcrumb
  551:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  552:       text=>"Settings to display/modify"});
  553:     my $confname = $dom.'-domainconfig';
  554: 
  555:     if ($phase eq 'process') {
  556:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  557:                                                               \%prefs,\%domconfig,$confname,\@roles);
  558:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  559:             $r->rflush();
  560:             &devalidate_remote_domconfs($dom,$result);
  561:         }
  562:     } elsif ($phase eq 'display') {
  563:         my $js = &recaptcha_js().
  564:                  &toggle_display_js();
  565:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  566:             my ($othertitle,$usertypes,$types) =
  567:                 &Apache::loncommon::sorted_inst_types($dom);
  568:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  569:                                           $domconfig{'loadbalancing'}).
  570:                    &new_spares_js().
  571:                    &common_domprefs_js().
  572:                    &Apache::loncommon::javascript_array_indexof();
  573:         }
  574:         if (grep(/^requestcourses$/,@actions)) {
  575:             my $javascript_validations;
  576:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  577:             $js .= <<END;
  578: <script type="text/javascript">
  579: $javascript_validations
  580: </script>
  581: $coursebrowserjs
  582: END
  583:         }
  584:         if (grep(/^contacts$/,@actions)) {
  585:             $js .= &contacts_javascript();
  586:         }
  587:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  588:     } else {
  589: # check if domconfig user exists for the domain.
  590:         my $servadm = $r->dir_config('lonAdmEMail');
  591:         my ($configuserok,$author_ok,$switchserver) =
  592:             &config_check($dom,$confname,$servadm);
  593:         unless ($configuserok eq 'ok') {
  594:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  595:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  596:                           $confname).
  597:                       '<br />'
  598:             );
  599:             if ($switchserver) {
  600:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  601:                           '<br />'.
  602:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  603:                           '<br />'.
  604:                           &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).
  605:                           '<br />'.
  606:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  607:                 );
  608:             } else {
  609:                 $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.').
  610:                           '<br />'.
  611:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  612:                 );
  613:             }
  614:             $r->print(&Apache::loncommon::end_page());
  615:             return OK;
  616:         }
  617:         if (keys(%domconfig) == 0) {
  618:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  619:             my @ids=&Apache::lonnet::current_machine_ids();
  620:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  621:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  622:                 my @loginimages = ('img','logo','domlogo','login');
  623:                 my $custom_img_count = 0;
  624:                 foreach my $img (@loginimages) {
  625:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  626:                         $custom_img_count ++;
  627:                     }
  628:                 }
  629:                 foreach my $role (@roles) {
  630:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  631:                         $custom_img_count ++;
  632:                     }
  633:                 }
  634:                 if ($custom_img_count > 0) {
  635:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  636:                     my $switch_server = &check_switchserver($dom,$confname);
  637:                     $r->print(
  638:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  639:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  640:     &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 />'.
  641:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  642:                     if ($switch_server) {
  643:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  644:                     }
  645:                     $r->print(&Apache::loncommon::end_page());
  646:                     return OK;
  647:                 }
  648:             }
  649:         }
  650:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  651:     }
  652:     return OK;
  653: }
  654: 
  655: sub process_changes {
  656:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  657:     my %domconfig;
  658:     if (ref($values) eq 'HASH') {
  659:         %domconfig = %{$values};
  660:     }
  661:     my $output;
  662:     if ($action eq 'login') {
  663:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  664:     } elsif ($action eq 'rolecolors') {
  665:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  666:                                      $lastactref,%domconfig);
  667:     } elsif ($action eq 'quotas') {
  668:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  669:     } elsif ($action eq 'autoenroll') {
  670:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  671:     } elsif ($action eq 'autoupdate') {
  672:         $output = &modify_autoupdate($dom,%domconfig);
  673:     } elsif ($action eq 'autocreate') {
  674:         $output = &modify_autocreate($dom,%domconfig);
  675:     } elsif ($action eq 'directorysrch') {
  676:         $output = &modify_directorysrch($dom,%domconfig);
  677:     } elsif ($action eq 'usercreation') {
  678:         $output = &modify_usercreation($dom,%domconfig);
  679:     } elsif ($action eq 'selfcreation') {
  680:         $output = &modify_selfcreation($dom,%domconfig);
  681:     } elsif ($action eq 'usermodification') {
  682:         $output = &modify_usermodification($dom,%domconfig);
  683:     } elsif ($action eq 'contacts') {
  684:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  685:     } elsif ($action eq 'defaults') {
  686:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  687:     } elsif ($action eq 'scantron') {
  688:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  689:     } elsif ($action eq 'coursecategories') {
  690:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  691:     } elsif ($action eq 'serverstatuses') {
  692:         $output = &modify_serverstatuses($dom,%domconfig);
  693:     } elsif ($action eq 'requestcourses') {
  694:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  695:     } elsif ($action eq 'requestauthor') {
  696:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  697:     } elsif ($action eq 'helpsettings') {
  698:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  699:     } elsif ($action eq 'coursedefaults') {
  700:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  701:     } elsif ($action eq 'selfenrollment') {
  702:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  703:     } elsif ($action eq 'usersessions') {
  704:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  705:     } elsif ($action eq 'loadbalancing') {
  706:         $output = &modify_loadbalancing($dom,%domconfig);
  707:     } elsif ($action eq 'ltitools') {
  708:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  709:     } elsif ($action eq 'ssl') {
  710:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  711:     } elsif ($action eq 'trust') {
  712:         $output = &modify_trust($dom,$lastactref,%domconfig);
  713:     }
  714:     return $output;
  715: }
  716: 
  717: sub print_config_box {
  718:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  719:     my $rowtotal = 0;
  720:     my $output;
  721:     if ($action eq 'coursecategories') {
  722:         $output = &coursecategories_javascript($settings);
  723:     } elsif ($action eq 'defaults') {
  724:         $output = &defaults_javascript($settings); 
  725:     } elsif ($action eq 'helpsettings') {
  726:         my (%privs,%levelscurrent);
  727:         my %full=();
  728:         my %levels=(
  729:                      course => {},
  730:                      domain => {},
  731:                      system => {},
  732:                    );
  733:         my $context = 'domain';
  734:         my $crstype = 'Course';
  735:         my $formname = 'display';
  736:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  737:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  738:         $output =
  739:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full, 
  740:                                                       \@templateroles);
  741:     }
  742:     $output .=
  743:          '<table class="LC_nested_outer">
  744:           <tr>
  745:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  746:            &mt($item->{text}).'&nbsp;'.
  747:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  748:           '</tr>';
  749:     $rowtotal ++;
  750:     my $numheaders = 1;
  751:     if (ref($item->{'header'}) eq 'ARRAY') {
  752:         $numheaders = scalar(@{$item->{'header'}});
  753:     }
  754:     if ($numheaders > 1) {
  755:         my $colspan = '';
  756:         my $rightcolspan = '';
  757:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  758:             ($action eq 'directorysrch') ||
  759:             (($action eq 'login') && ($numheaders < 4))) {
  760:             $colspan = ' colspan="2"';
  761:         }
  762:         if ($action eq 'usersessions') {
  763:             $rightcolspan = ' colspan="3"'; 
  764:         }
  765:         $output .= '
  766:           <tr>
  767:            <td>
  768:             <table class="LC_nested">
  769:              <tr class="LC_info_row">
  770:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  771:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  772:              </tr>';
  773:         $rowtotal ++;
  774:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  775:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  776:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  777:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  778:             ($action eq 'contacts')) {
  779:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  780:         } elsif ($action eq 'coursecategories') {
  781:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  782:         } elsif ($action eq 'login') {
  783:             if ($numheaders == 4) {
  784:                 $colspan = ' colspan="2"';
  785:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  786:             } else {
  787:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  788:             }
  789:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  790:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  791:         } elsif ($action eq 'rolecolors') {
  792:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  793:         }
  794:         $output .= '
  795:            </table>
  796:           </td>
  797:          </tr>
  798:          <tr>
  799:            <td>
  800:             <table class="LC_nested">
  801:              <tr class="LC_info_row">
  802:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  803:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  804:              </tr>';
  805:             $rowtotal ++;
  806:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  807:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  808:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
  809:             ($action eq 'ssl') || ($action eq 'trust') || ($action eq 'contacts')) {
  810:             if ($action eq 'coursecategories') {
  811:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  812:                 $colspan = ' colspan="2"';
  813:             } elsif ($action eq 'trust') {
  814:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
  815:             } else {
  816:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  817:             }
  818:             if ($action eq 'trust') {
  819:                 $output .= '
  820:             </table>
  821:           </td>
  822:          </tr>';
  823:                 my @trusthdrs = qw(2 3 4 5 6 7);
  824:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
  825:                 for (my $i=0; $i<@trusthdrs; $i++) {
  826:                     $output .= '
  827:          <tr>
  828:            <td>
  829:             <table class="LC_nested">
  830:              <tr class="LC_info_row">
  831:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
  832:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
  833:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
  834:             </table>
  835:           </td>
  836:          </tr>';
  837:                 }
  838:                 $output .= '
  839:          <tr>
  840:            <td>
  841:             <table class="LC_nested">
  842:              <tr class="LC_info_row">
  843:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
  844:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
  845:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  846:             } else {
  847:                 $output .= '
  848:            </table>
  849:           </td>
  850:          </tr>
  851:          <tr>
  852:            <td>
  853:             <table class="LC_nested">
  854:              <tr class="LC_info_row">
  855:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  856:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  857:              </tr>'."\n";
  858:                 if ($action eq 'coursecategories') {
  859:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  860:                 } else {
  861:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  862:                 }
  863:             }
  864:             $rowtotal ++;
  865:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  866:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  867:                  ($action eq 'helpsettings')) {
  868:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  869:         } elsif ($action eq 'login') {
  870:             if ($numheaders == 4) {
  871:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  872:            </table>
  873:           </td>
  874:          </tr>
  875:          <tr>
  876:            <td>
  877:             <table class="LC_nested">
  878:              <tr class="LC_info_row">
  879:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  880:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  881:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  882:                 $rowtotal ++;
  883:             } else {
  884:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  885:             }
  886:             $output .= '
  887:            </table>
  888:           </td>
  889:          </tr>
  890:          <tr>
  891:            <td>
  892:             <table class="LC_nested">
  893:              <tr class="LC_info_row">';
  894:             if ($numheaders == 4) {
  895:                 $output .= '
  896:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  897:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  898:              </tr>';
  899:             } else {
  900:                 $output .= '
  901:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  902:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  903:              </tr>';
  904:             }
  905:             $rowtotal ++;
  906:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  907:         } elsif ($action eq 'requestcourses') {
  908:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  909:             $rowtotal ++;
  910:             $output .= &print_studentcode($settings,\$rowtotal).'
  911:            </table>
  912:           </td>
  913:          </tr>
  914:          <tr>
  915:            <td>
  916:             <table class="LC_nested">
  917:              <tr class="LC_info_row">
  918:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  919:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  920:                        &textbookcourses_javascript($settings).
  921:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  922:             </table>
  923:            </td>
  924:           </tr>
  925:          <tr>
  926:            <td>
  927:             <table class="LC_nested">
  928:              <tr class="LC_info_row">
  929:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  930:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  931:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  932:             </table>
  933:            </td>
  934:           </tr>
  935:           <tr>
  936:            <td>
  937:             <table class="LC_nested">
  938:              <tr class="LC_info_row">
  939:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  940:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  941:              </tr>'.
  942:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  943:         } elsif ($action eq 'requestauthor') {
  944:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  945:             $rowtotal ++;
  946:         } elsif ($action eq 'rolecolors') {
  947:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  948:            </table>
  949:           </td>
  950:          </tr>
  951:          <tr>
  952:            <td>
  953:             <table class="LC_nested">
  954:              <tr class="LC_info_row">
  955:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  956:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  957:               <td class="LC_right_item" valign="top">'.
  958:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  959:              </tr>'.
  960:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  961:            </table>
  962:           </td>
  963:          </tr>
  964:          <tr>
  965:            <td>
  966:             <table class="LC_nested">
  967:              <tr class="LC_info_row">
  968:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  969:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  970:              </tr>'.
  971:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  972:             $rowtotal += 2;
  973:         }
  974:     } else {
  975:         $output .= '
  976:           <tr>
  977:            <td>
  978:             <table class="LC_nested">
  979:              <tr class="LC_info_row">';
  980:         if ($action eq 'login') {
  981:             $output .= '  
  982:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  983:         } elsif ($action eq 'serverstatuses') {
  984:             $output .= '
  985:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  986:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  987: 
  988:         } else {
  989:             $output .= '
  990:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  991:         }
  992:         if (defined($item->{'header'}->[0]->{'col3'})) {
  993:             $output .= '<td class="LC_left_item" valign="top">'.
  994:                        &mt($item->{'header'}->[0]->{'col2'});
  995:             if ($action eq 'serverstatuses') {
  996:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  997:             } 
  998:         } else {
  999:             $output .= '<td class="LC_right_item" valign="top">'.
 1000:                        &mt($item->{'header'}->[0]->{'col2'});
 1001:         }
 1002:         $output .= '</td>';
 1003:         if ($item->{'header'}->[0]->{'col3'}) {
 1004:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1005:                 $output .= '<td class="LC_left_item" valign="top">'.
 1006:                             &mt($item->{'header'}->[0]->{'col3'});
 1007:             } else {
 1008:                 $output .= '<td class="LC_right_item" valign="top">'.
 1009:                            &mt($item->{'header'}->[0]->{'col3'});
 1010:             }
 1011:             if ($action eq 'serverstatuses') {
 1012:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1013:             }
 1014:             $output .= '</td>';
 1015:         }
 1016:         if ($item->{'header'}->[0]->{'col4'}) {
 1017:             $output .= '<td class="LC_right_item" valign="top">'.
 1018:                        &mt($item->{'header'}->[0]->{'col4'});
 1019:         }
 1020:         $output .= '</tr>';
 1021:         $rowtotal ++;
 1022:         if ($action eq 'quotas') {
 1023:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1024:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1025:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1026:                  ($action eq 'ltitools')) {
 1027:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1028:         } elsif ($action eq 'scantron') {
 1029:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 1030:         }
 1031:     }
 1032:     $output .= '
 1033:    </table>
 1034:   </td>
 1035:  </tr>
 1036: </table><br />';
 1037:     return ($output,$rowtotal);
 1038: }
 1039: 
 1040: sub print_login {
 1041:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1042:     my ($css_class,$datatable);
 1043:     my %choices = &login_choices();
 1044: 
 1045:     if ($caller eq 'service') {
 1046:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1047:         my $choice = $choices{'disallowlogin'};
 1048:         $css_class = ' class="LC_odd_row"';
 1049:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1050:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1051:                       '<th>'.$choices{'server'}.'</th>'.
 1052:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1053:                       '<th>'.$choices{'custompath'}.'</th>'.
 1054:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1055:         my %disallowed;
 1056:         if (ref($settings) eq 'HASH') {
 1057:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1058:                %disallowed = %{$settings->{'loginvia'}};
 1059:             }
 1060:         }
 1061:         foreach my $lonhost (sort(keys(%servers))) {
 1062:             my $direct = 'selected="selected"';
 1063:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1064:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1065:                     $direct = '';
 1066:                 }
 1067:             }
 1068:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1069:                           '<td><select name="'.$lonhost.'_server">'.
 1070:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1071:                           '</option>';
 1072:             foreach my $hostid (sort(keys(%servers))) {
 1073:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1074:                 my $selected = '';
 1075:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1076:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1077:                         $selected = 'selected="selected"';
 1078:                     }
 1079:                 }
 1080:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1081:                               $servers{$hostid}.'</option>';
 1082:             }
 1083:             $datatable .= '</select></td>'.
 1084:                           '<td><select name="'.$lonhost.'_serverpath">';
 1085:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1086:                 my $pathname = $path;
 1087:                 if ($path eq 'custom') {
 1088:                     $pathname = &mt('Custom Path').' ->';
 1089:                 }
 1090:                 my $selected = '';
 1091:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1092:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1093:                         $selected = 'selected="selected"';
 1094:                     }
 1095:                 } elsif ($path eq '') {
 1096:                     $selected = 'selected="selected"';
 1097:                 }
 1098:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1099:             }
 1100:             $datatable .= '</select></td>';
 1101:             my ($custom,$exempt);
 1102:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1103:                 $custom = $disallowed{$lonhost}{'custompath'};
 1104:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1105:             }
 1106:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1107:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1108:                           '</tr>';
 1109:         }
 1110:         $datatable .= '</table></td></tr>';
 1111:         return $datatable;
 1112:     } elsif ($caller eq 'page') {
 1113:         my %defaultchecked = ( 
 1114:                                'coursecatalog' => 'on',
 1115:                                'helpdesk'      => 'on',
 1116:                                'adminmail'     => 'off',
 1117:                                'newuser'       => 'off',
 1118:                              );
 1119:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1120:         my (%checkedon,%checkedoff);
 1121:         foreach my $item (@toggles) {
 1122:             if ($defaultchecked{$item} eq 'on') { 
 1123:                 $checkedon{$item} = ' checked="checked" ';
 1124:                 $checkedoff{$item} = ' ';
 1125:             } elsif ($defaultchecked{$item} eq 'off') {
 1126:                 $checkedoff{$item} = ' checked="checked" ';
 1127:                 $checkedon{$item} = ' ';
 1128:             }
 1129:         }
 1130:         my @images = ('img','logo','domlogo','login');
 1131:         my @logintext = ('textcol','bgcol');
 1132:         my @bgs = ('pgbg','mainbg','sidebg');
 1133:         my @links = ('link','alink','vlink');
 1134:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1135:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1136:         my (%is_custom,%designs);
 1137:         my %defaults = (
 1138:                        font => $defaultdesign{'login.font'},
 1139:                        );
 1140:         foreach my $item (@images) {
 1141:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1142:             $defaults{'showlogo'}{$item} = 1;
 1143:         }
 1144:         foreach my $item (@bgs) {
 1145:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1146:         }
 1147:         foreach my $item (@logintext) {
 1148:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1149:         }
 1150:         foreach my $item (@links) {
 1151:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1152:         }
 1153:         if (ref($settings) eq 'HASH') {
 1154:             foreach my $item (@toggles) {
 1155:                 if ($settings->{$item} eq '1') {
 1156:                     $checkedon{$item} =  ' checked="checked" ';
 1157:                     $checkedoff{$item} = ' ';
 1158:                 } elsif ($settings->{$item} eq '0') {
 1159:                     $checkedoff{$item} =  ' checked="checked" ';
 1160:                     $checkedon{$item} = ' ';
 1161:                 }
 1162:             }
 1163:             foreach my $item (@images) {
 1164:                 if (defined($settings->{$item})) {
 1165:                     $designs{$item} = $settings->{$item};
 1166:                     $is_custom{$item} = 1;
 1167:                 }
 1168:                 if (defined($settings->{'showlogo'}{$item})) {
 1169:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1170:                 }
 1171:             }
 1172:             foreach my $item (@logintext) {
 1173:                 if ($settings->{$item} ne '') {
 1174:                     $designs{'logintext'}{$item} = $settings->{$item};
 1175:                     $is_custom{$item} = 1;
 1176:                 }
 1177:             }
 1178:             if ($settings->{'font'} ne '') {
 1179:                 $designs{'font'} = $settings->{'font'};
 1180:                 $is_custom{'font'} = 1;
 1181:             }
 1182:             foreach my $item (@bgs) {
 1183:                 if ($settings->{$item} ne '') {
 1184:                     $designs{'bgs'}{$item} = $settings->{$item};
 1185:                     $is_custom{$item} = 1;
 1186:                 }
 1187:             }
 1188:             foreach my $item (@links) {
 1189:                 if ($settings->{$item} ne '') {
 1190:                     $designs{'links'}{$item} = $settings->{$item};
 1191:                     $is_custom{$item} = 1;
 1192:                 }
 1193:             }
 1194:         } else {
 1195:             if ($designhash{$dom.'.login.font'} ne '') {
 1196:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1197:                 $is_custom{'font'} = 1;
 1198:             }
 1199:             foreach my $item (@images) {
 1200:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1201:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1202:                     $is_custom{$item} = 1;
 1203:                 }
 1204:             }
 1205:             foreach my $item (@bgs) {
 1206:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1207:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1208:                     $is_custom{$item} = 1;
 1209:                 }
 1210:             }
 1211:             foreach my $item (@links) {
 1212:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1213:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1214:                     $is_custom{$item} = 1;
 1215:                 }
 1216:             }
 1217:         }
 1218:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1219:                                                       logo => 'Institution Logo',
 1220:                                                       domlogo => 'Domain Logo',
 1221:                                                       login => 'Login box');
 1222:         my $itemcount = 1;
 1223:         foreach my $item (@toggles) {
 1224:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1225:             $datatable .=  
 1226:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1227:                 '</td><td>'.
 1228:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1229:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1230:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1231:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1232:                 '</tr>';
 1233:             $itemcount ++;
 1234:         }
 1235:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1236:         $datatable .= '</tr></table></td></tr>';
 1237:     } elsif ($caller eq 'help') {
 1238:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1239:         my $switchserver = &check_switchserver($dom,$confname);
 1240:         my $itemcount = 1;
 1241:         $defaulturl = '/adm/loginproblems.html';
 1242:         $defaulttype = 'default';
 1243:         %lt = &Apache::lonlocal::texthash (
 1244:                      del     => 'Delete?',
 1245:                      rep     => 'Replace:',
 1246:                      upl     => 'Upload:',
 1247:                      default => 'Default',
 1248:                      custom  => 'Custom',
 1249:                                              );
 1250:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1251:         my @currlangs;
 1252:         if (ref($settings) eq 'HASH') {
 1253:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1254:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1255:                     next if ($settings->{'helpurl'}{$key} eq '');
 1256:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1257:                     $type{$key} = 'custom';
 1258:                     unless ($key eq 'nolang') {
 1259:                         push(@currlangs,$key);
 1260:                     }
 1261:                 }
 1262:             } elsif ($settings->{'helpurl'} ne '') {
 1263:                 $type{'nolang'} = 'custom';
 1264:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1265:             }
 1266:         }
 1267:         foreach my $lang ('nolang',sort(@currlangs)) {
 1268:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1269:             $datatable .= '<tr'.$css_class.'>';
 1270:             if ($url{$lang} eq '') {
 1271:                 $url{$lang} = $defaulturl;
 1272:             }
 1273:             if ($type{$lang} eq '') {
 1274:                 $type{$lang} = $defaulttype;
 1275:             }
 1276:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1277:             if ($lang eq 'nolang') {
 1278:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1279:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1280:             } else {
 1281:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1282:                                   $langchoices{$lang},
 1283:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1284:             }
 1285:             $datatable .= '</span></td>'."\n".
 1286:                           '<td class="LC_left_item">';
 1287:             if ($type{$lang} eq 'custom') {
 1288:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1289:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1290:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1291:             } else {
 1292:                 $datatable .= $lt{'upl'};
 1293:             }
 1294:             $datatable .='<br />';
 1295:             if ($switchserver) {
 1296:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1297:             } else {
 1298:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1299:             }
 1300:             $datatable .= '</td></tr>';
 1301:             $itemcount ++;
 1302:         }
 1303:         my @addlangs;
 1304:         foreach my $lang (sort(keys(%langchoices))) {
 1305:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1306:             push(@addlangs,$lang);
 1307:         }
 1308:         if (@addlangs > 0) {
 1309:             my %toadd;
 1310:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1311:             $toadd{''} = &mt('Select');
 1312:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1313:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1314:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1315:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1316:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1317:             if ($switchserver) {
 1318:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1319:             } else {
 1320:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1321:             }
 1322:             $datatable .= '</td></tr>';
 1323:             $itemcount ++;
 1324:         }
 1325:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1326:     } elsif ($caller eq 'headtag') {
 1327:         my %domservers = &Apache::lonnet::get_servers($dom);
 1328:         my $choice = $choices{'headtag'};
 1329:         $css_class = ' class="LC_odd_row"';
 1330:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1331:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1332:                       '<th>'.$choices{'current'}.'</th>'.
 1333:                       '<th>'.$choices{'action'}.'</th>'.
 1334:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1335:         my (%currurls,%currexempt);
 1336:         if (ref($settings) eq 'HASH') {
 1337:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1338:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1339:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1340:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1341:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1342:                     }
 1343:                 }
 1344:             }
 1345:         }
 1346:         my %lt = &Apache::lonlocal::texthash(
 1347:                                                del  => 'Delete?',
 1348:                                                rep  => 'Replace:',
 1349:                                                upl  => 'Upload:',
 1350:                                                curr => 'View contents',
 1351:                                                none => 'None',
 1352:         );
 1353:         my $switchserver = &check_switchserver($dom,$confname);
 1354:         foreach my $lonhost (sort(keys(%domservers))) {
 1355:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1356:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1357:             if ($currurls{$lonhost}) {
 1358:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1359:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1360:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1361:                               '">'.$lt{'curr'}.'</a></td>'.
 1362:                               '<td><span class="LC_nobreak"><label>'.
 1363:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1364:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1365:             } else {
 1366:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1367:             }
 1368:             $datatable .='<br />';
 1369:             if ($switchserver) {
 1370:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1371:             } else {
 1372:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1373:             }
 1374:             $datatable .= '</td><td><input type="textbox" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1375:         }
 1376:         $datatable .= '</table></td></tr>';
 1377:     }
 1378:     return $datatable;
 1379: }
 1380: 
 1381: sub login_choices {
 1382:     my %choices =
 1383:         &Apache::lonlocal::texthash (
 1384:             coursecatalog => 'Display Course/Community Catalog link?',
 1385:             adminmail     => "Display Administrator's E-mail Address?",
 1386:             helpdesk      => 'Display "Contact Helpdesk" link',
 1387:             disallowlogin => "Login page requests redirected",
 1388:             hostid        => "Server",
 1389:             server        => "Redirect to:",
 1390:             serverpath    => "Path",
 1391:             custompath    => "Custom", 
 1392:             exempt        => "Exempt IP(s)",
 1393:             directlogin   => "No redirect",
 1394:             newuser       => "Link to create a user account",
 1395:             img           => "Header",
 1396:             logo          => "Main Logo",
 1397:             domlogo       => "Domain Logo",
 1398:             login         => "Log-in Header", 
 1399:             textcol       => "Text color",
 1400:             bgcol         => "Box color",
 1401:             bgs           => "Background colors",
 1402:             links         => "Link colors",
 1403:             font          => "Font color",
 1404:             pgbg          => "Header",
 1405:             mainbg        => "Page",
 1406:             sidebg        => "Login box",
 1407:             link          => "Link",
 1408:             alink         => "Active link",
 1409:             vlink         => "Visited link",
 1410:             headtag       => "Custom markup",
 1411:             action        => "Action",
 1412:             current       => "Current",
 1413:         );
 1414:     return %choices;
 1415: }
 1416: 
 1417: sub print_rolecolors {
 1418:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1419:     my %choices = &color_font_choices();
 1420:     my @bgs = ('pgbg','tabbg','sidebg');
 1421:     my @links = ('link','alink','vlink');
 1422:     my @images = ('img');
 1423:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1424:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1425:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1426:     my (%is_custom,%designs);
 1427:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1428:     if (ref($settings) eq 'HASH') {
 1429:         if (ref($settings->{$role}) eq 'HASH') {
 1430:             if ($settings->{$role}->{'img'} ne '') {
 1431:                 $designs{'img'} = $settings->{$role}->{'img'};
 1432:                 $is_custom{'img'} = 1;
 1433:             }
 1434:             if ($settings->{$role}->{'font'} ne '') {
 1435:                 $designs{'font'} = $settings->{$role}->{'font'};
 1436:                 $is_custom{'font'} = 1;
 1437:             }
 1438:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1439:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1440:                 $is_custom{'fontmenu'} = 1;
 1441:             }
 1442:             foreach my $item (@bgs) {
 1443:                 if ($settings->{$role}->{$item} ne '') {
 1444:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1445:                     $is_custom{$item} = 1;
 1446:                 }
 1447:             }
 1448:             foreach my $item (@links) {
 1449:                 if ($settings->{$role}->{$item} ne '') {
 1450:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1451:                     $is_custom{$item} = 1;
 1452:                 }
 1453:             }
 1454:         }
 1455:     } else {
 1456:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1457:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1458:             $is_custom{'img'} = 1;
 1459:         }
 1460:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1461:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1462:             $is_custom{'fontmenu'} = 1; 
 1463:         }
 1464:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1465:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1466:             $is_custom{'font'} = 1;
 1467:         }
 1468:         foreach my $item (@bgs) {
 1469:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1470:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1471:                 $is_custom{$item} = 1;
 1472:             
 1473:             }
 1474:         }
 1475:         foreach my $item (@links) {
 1476:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1477:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1478:                 $is_custom{$item} = 1;
 1479:             }
 1480:         }
 1481:     }
 1482:     my $itemcount = 1;
 1483:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1484:     $datatable .= '</tr></table></td></tr>';
 1485:     return $datatable;
 1486: }
 1487: 
 1488: sub role_defaults {
 1489:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1490:     my %defaults;
 1491:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1492:         return %defaults;
 1493:     }
 1494:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1495:     if ($role eq 'login') {
 1496:         %defaults = (
 1497:                        font => $defaultdesign{$role.'.font'},
 1498:                     );
 1499:         if (ref($logintext) eq 'ARRAY') {
 1500:             foreach my $item (@{$logintext}) {
 1501:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1502:             }
 1503:         }
 1504:         foreach my $item (@{$images}) {
 1505:             $defaults{'showlogo'}{$item} = 1;
 1506:         }
 1507:     } else {
 1508:         %defaults = (
 1509:                        img => $defaultdesign{$role.'.img'},
 1510:                        font => $defaultdesign{$role.'.font'},
 1511:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1512:                     );
 1513:     }
 1514:     foreach my $item (@{$bgs}) {
 1515:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1516:     }
 1517:     foreach my $item (@{$links}) {
 1518:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1519:     }
 1520:     foreach my $item (@{$images}) {
 1521:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1522:     }
 1523:     return %defaults;
 1524: }
 1525: 
 1526: sub display_color_options {
 1527:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1528:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1529:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1530:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1531:     my $datatable = '<tr'.$css_class.'>'.
 1532:         '<td>'.$choices->{'font'}.'</td>';
 1533:     if (!$is_custom->{'font'}) {
 1534:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1535:     } else {
 1536:         $datatable .= '<td>&nbsp;</td>';
 1537:     }
 1538:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1539: 
 1540:     $datatable .= '<td><span class="LC_nobreak">'.
 1541:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1542:                   ' value="'.$current_color.'" />&nbsp;'.
 1543:                   '&nbsp;</td></tr>';
 1544:     unless ($role eq 'login') { 
 1545:         $datatable .= '<tr'.$css_class.'>'.
 1546:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1547:         if (!$is_custom->{'fontmenu'}) {
 1548:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1549:         } else {
 1550:             $datatable .= '<td>&nbsp;</td>';
 1551:         }
 1552: 	$current_color = $designs->{'fontmenu'} ?
 1553: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1554:         $datatable .= '<td><span class="LC_nobreak">'.
 1555:                       '<input class="colorchooser" type="text" size="10" name="'
 1556: 		      .$role.'_fontmenu"'.
 1557:                       ' value="'.$current_color.'" />&nbsp;'.
 1558:                       '&nbsp;</td></tr>';
 1559:     }
 1560:     my $switchserver = &check_switchserver($dom,$confname);
 1561:     foreach my $img (@{$images}) {
 1562: 	$itemcount ++;
 1563:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1564:         $datatable .= '<tr'.$css_class.'>'.
 1565:                       '<td>'.$choices->{$img};
 1566:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1567:         if ($role eq 'login') {
 1568:             if ($img eq 'login') {
 1569:                 $login_hdr_pick =
 1570:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1571:                 $logincolors =
 1572:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1573:                                        $designs,$defaults);
 1574:             } elsif ($img ne 'domlogo') {
 1575:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1576:             }
 1577:         }
 1578:         $datatable .= '</td>';
 1579:         if ($designs->{$img} ne '') {
 1580:             $imgfile = $designs->{$img};
 1581: 	    $img_import = ($imgfile =~ m{^/adm/});
 1582:         } else {
 1583:             $imgfile = $defaults->{$img};
 1584:         }
 1585:         if ($imgfile) {
 1586:             my ($showfile,$fullsize);
 1587:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1588:                 my $urldir = $1;
 1589:                 my $filename = $2;
 1590:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1591:                 if (@info) {
 1592:                     my $thumbfile = 'tn-'.$filename;
 1593:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1594:                     if (@thumb) {
 1595:                         $showfile = $urldir.'/'.$thumbfile;
 1596:                     } else {
 1597:                         $showfile = $imgfile;
 1598:                     }
 1599:                 } else {
 1600:                     $showfile = '';
 1601:                 }
 1602:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1603:                 $showfile = $imgfile;
 1604:                 my $imgdir = $1;
 1605:                 my $filename = $2;
 1606:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1607:                     $showfile = "/$imgdir/tn-".$filename;
 1608:                 } else {
 1609:                     my $input = $londocroot.$imgfile;
 1610:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1611:                     if (!-e $output) {
 1612:                         my ($width,$height) = &thumb_dimensions();
 1613:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1614:                         if ($fullwidth ne '' && $fullheight ne '') {
 1615:                             if ($fullwidth > $width && $fullheight > $height) { 
 1616:                                 my $size = $width.'x'.$height;
 1617:                                 system("convert -sample $size $input $output");
 1618:                                 $showfile = "/$imgdir/tn-".$filename;
 1619:                             }
 1620:                         }
 1621:                     }
 1622:                 }
 1623:             }
 1624:             if ($showfile) {
 1625:                 if ($showfile =~ m{^/(adm|res)/}) {
 1626:                     if ($showfile =~ m{^/res/}) {
 1627:                         my $local_showfile =
 1628:                             &Apache::lonnet::filelocation('',$showfile);
 1629:                         &Apache::lonnet::repcopy($local_showfile);
 1630:                     }
 1631:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1632:                 }
 1633:                 if ($imgfile) {
 1634:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1635:                         if ($imgfile =~ m{^/res/}) {
 1636:                             my $local_imgfile =
 1637:                                 &Apache::lonnet::filelocation('',$imgfile);
 1638:                             &Apache::lonnet::repcopy($local_imgfile);
 1639:                         }
 1640:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1641:                     } else {
 1642:                         $fullsize = $imgfile;
 1643:                     }
 1644:                 }
 1645:                 $datatable .= '<td>';
 1646:                 if ($img eq 'login') {
 1647:                     $datatable .= $login_hdr_pick;
 1648:                 } 
 1649:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1650:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1651:             } else {
 1652:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1653:                               &mt('Upload:').'<br />';
 1654:             }
 1655:         } else {
 1656:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1657:                           &mt('Upload:').'<br />';
 1658:         }
 1659:         if ($switchserver) {
 1660:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1661:         } else {
 1662:             if ($img ne 'login') { # suppress file selection for Log-in header
 1663:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1664:             }
 1665:         }
 1666:         $datatable .= '</td></tr>';
 1667:     }
 1668:     $itemcount ++;
 1669:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1670:     $datatable .= '<tr'.$css_class.'>'.
 1671:                   '<td>'.$choices->{'bgs'}.'</td>';
 1672:     my $bgs_def;
 1673:     foreach my $item (@{$bgs}) {
 1674:         if (!$is_custom->{$item}) {
 1675:             $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>';
 1676:         }
 1677:     }
 1678:     if ($bgs_def) {
 1679:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1680:     } else {
 1681:         $datatable .= '<td>&nbsp;</td>';
 1682:     }
 1683:     $datatable .= '<td class="LC_right_item">'.
 1684:                   '<table border="0"><tr>';
 1685: 
 1686:     foreach my $item (@{$bgs}) {
 1687:         $datatable .= '<td align="center">'.$choices->{$item};
 1688: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1689:         if ($designs->{'bgs'}{$item}) {
 1690:             $datatable .= '&nbsp;';
 1691:         }
 1692:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1693:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1694:     }
 1695:     $datatable .= '</tr></table></td></tr>';
 1696:     $itemcount ++;
 1697:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1698:     $datatable .= '<tr'.$css_class.'>'.
 1699:                   '<td>'.$choices->{'links'}.'</td>';
 1700:     my $links_def;
 1701:     foreach my $item (@{$links}) {
 1702:         if (!$is_custom->{$item}) {
 1703:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1704:         }
 1705:     }
 1706:     if ($links_def) {
 1707:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1708:     } else {
 1709:         $datatable .= '<td>&nbsp;</td>';
 1710:     }
 1711:     $datatable .= '<td class="LC_right_item">'.
 1712:                   '<table border="0"><tr>';
 1713:     foreach my $item (@{$links}) {
 1714: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1715:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1716:         if ($designs->{'links'}{$item}) {
 1717:             $datatable.='&nbsp;';
 1718:         }
 1719:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1720:                       '" /></td>';
 1721:     }
 1722:     $$rowtotal += $itemcount;
 1723:     return $datatable;
 1724: }
 1725: 
 1726: sub logo_display_options {
 1727:     my ($img,$defaults,$designs) = @_;
 1728:     my $checkedon;
 1729:     if (ref($defaults) eq 'HASH') {
 1730:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1731:             if ($defaults->{'showlogo'}{$img}) {
 1732:                 $checkedon = 'checked="checked" ';     
 1733:             }
 1734:         } 
 1735:     }
 1736:     if (ref($designs) eq 'HASH') {
 1737:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1738:             if (defined($designs->{'showlogo'}{$img})) {
 1739:                 if ($designs->{'showlogo'}{$img} == 0) {
 1740:                     $checkedon = '';
 1741:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1742:                     $checkedon = 'checked="checked" ';
 1743:                 }
 1744:             }
 1745:         }
 1746:     }
 1747:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1748:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1749:            &mt('show').'</label>'."\n";
 1750: }
 1751: 
 1752: sub login_header_options  {
 1753:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1754:     my $output = '';
 1755:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1756:         $output .= &mt('Text default(s):').'<br />';
 1757:         if (!$is_custom->{'textcol'}) {
 1758:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1759:                        '&nbsp;&nbsp;&nbsp;';
 1760:         }
 1761:         if (!$is_custom->{'bgcol'}) {
 1762:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1763:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1764:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1765:         }
 1766:         $output .= '<br />';
 1767:     }
 1768:     $output .='<br />';
 1769:     return $output;
 1770: }
 1771: 
 1772: sub login_text_colors {
 1773:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1774:     my $color_menu = '<table border="0"><tr>';
 1775:     foreach my $item (@{$logintext}) {
 1776:         $color_menu .= '<td align="center">'.$choices->{$item};
 1777:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1778:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1779:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1780:     }
 1781:     $color_menu .= '</tr></table><br />';
 1782:     return $color_menu;
 1783: }
 1784: 
 1785: sub image_changes {
 1786:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1787:     my $output;
 1788:     if ($img eq 'login') {
 1789:             # suppress image for Log-in header
 1790:     } elsif (!$is_custom) {
 1791:         if ($img ne 'domlogo') {
 1792:             $output .= &mt('Default image:').'<br />';
 1793:         } else {
 1794:             $output .= &mt('Default in use:').'<br />';
 1795:         }
 1796:     }
 1797:     if ($img eq 'login') { # suppress image for Log-in header
 1798:         $output .= '<td>'.$logincolors;
 1799:     } else {
 1800:         if ($img_import) {
 1801:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1802:         }
 1803:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1804:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1805:         if ($is_custom) {
 1806:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1807:                        '<input type="checkbox" name="'.
 1808:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1809:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1810:         } else {
 1811:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1812:         }
 1813:     }
 1814:     return $output;
 1815: }
 1816: 
 1817: sub print_quotas {
 1818:     my ($dom,$settings,$rowtotal,$action) = @_;
 1819:     my $context;
 1820:     if ($action eq 'quotas') {
 1821:         $context = 'tools';
 1822:     } else {
 1823:         $context = $action;
 1824:     }
 1825:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1826:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1827:     my $typecount = 0;
 1828:     my ($css_class,%titles);
 1829:     if ($context eq 'requestcourses') {
 1830:         @usertools = ('official','unofficial','community','textbook','placement');
 1831:         @options =('norequest','approval','validate','autolimit');
 1832:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1833:         %titles = &courserequest_titles();
 1834:     } elsif ($context eq 'requestauthor') {
 1835:         @usertools = ('author');
 1836:         @options = ('norequest','approval','automatic');
 1837:         %titles = &authorrequest_titles();
 1838:     } else {
 1839:         @usertools = ('aboutme','blog','webdav','portfolio');
 1840:         %titles = &tool_titles();
 1841:     }
 1842:     if (ref($types) eq 'ARRAY') {
 1843:         foreach my $type (@{$types}) {
 1844:             my ($currdefquota,$currauthorquota);
 1845:             unless (($context eq 'requestcourses') ||
 1846:                     ($context eq 'requestauthor')) {
 1847:                 if (ref($settings) eq 'HASH') {
 1848:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1849:                         $currdefquota = $settings->{defaultquota}->{$type};
 1850:                     } else {
 1851:                         $currdefquota = $settings->{$type};
 1852:                     }
 1853:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1854:                         $currauthorquota = $settings->{authorquota}->{$type};
 1855:                     }
 1856:                 }
 1857:             }
 1858:             if (defined($usertypes->{$type})) {
 1859:                 $typecount ++;
 1860:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1861:                 $datatable .= '<tr'.$css_class.'>'.
 1862:                               '<td>'.$usertypes->{$type}.'</td>'.
 1863:                               '<td class="LC_left_item">';
 1864:                 if ($context eq 'requestcourses') {
 1865:                     $datatable .= '<table><tr>';
 1866:                 }
 1867:                 my %cell;  
 1868:                 foreach my $item (@usertools) {
 1869:                     if ($context eq 'requestcourses') {
 1870:                         my ($curroption,$currlimit);
 1871:                         if (ref($settings) eq 'HASH') {
 1872:                             if (ref($settings->{$item}) eq 'HASH') {
 1873:                                 $curroption = $settings->{$item}->{$type};
 1874:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1875:                                     $currlimit = $1; 
 1876:                                 }
 1877:                             }
 1878:                         }
 1879:                         if (!$curroption) {
 1880:                             $curroption = 'norequest';
 1881:                         }
 1882:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1883:                         foreach my $option (@options) {
 1884:                             my $val = $option;
 1885:                             if ($option eq 'norequest') {
 1886:                                 $val = 0;  
 1887:                             }
 1888:                             if ($option eq 'validate') {
 1889:                                 my $canvalidate = 0;
 1890:                                 if (ref($validations{$item}) eq 'HASH') { 
 1891:                                     if ($validations{$item}{$type}) {
 1892:                                         $canvalidate = 1;
 1893:                                     }
 1894:                                 }
 1895:                                 next if (!$canvalidate);
 1896:                             }
 1897:                             my $checked = '';
 1898:                             if ($option eq $curroption) {
 1899:                                 $checked = ' checked="checked"';
 1900:                             } elsif ($option eq 'autolimit') {
 1901:                                 if ($curroption =~ /^autolimit/) {
 1902:                                     $checked = ' checked="checked"';
 1903:                                 }                       
 1904:                             } 
 1905:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1906:                                   '<input type="radio" name="crsreq_'.$item.
 1907:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1908:                                   $titles{$option}.'</label>';
 1909:                             if ($option eq 'autolimit') {
 1910:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1911:                                                 $item.'_limit_'.$type.'" size="1" '.
 1912:                                                 'value="'.$currlimit.'" />';
 1913:                             }
 1914:                             $cell{$item} .= '</span> ';
 1915:                             if ($option eq 'autolimit') {
 1916:                                 $cell{$item} .= $titles{'unlimited'};
 1917:                             }
 1918:                         }
 1919:                     } elsif ($context eq 'requestauthor') {
 1920:                         my $curroption;
 1921:                         if (ref($settings) eq 'HASH') {
 1922:                             $curroption = $settings->{$type};
 1923:                         }
 1924:                         if (!$curroption) {
 1925:                             $curroption = 'norequest';
 1926:                         }
 1927:                         foreach my $option (@options) {
 1928:                             my $val = $option;
 1929:                             if ($option eq 'norequest') {
 1930:                                 $val = 0;
 1931:                             }
 1932:                             my $checked = '';
 1933:                             if ($option eq $curroption) {
 1934:                                 $checked = ' checked="checked"';
 1935:                             }
 1936:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1937:                                   '<input type="radio" name="authorreq_'.$type.
 1938:                                   '" value="'.$val.'"'.$checked.' />'.
 1939:                                   $titles{$option}.'</label></span>&nbsp; ';
 1940:                         }
 1941:                     } else {
 1942:                         my $checked = 'checked="checked" ';
 1943:                         if (ref($settings) eq 'HASH') {
 1944:                             if (ref($settings->{$item}) eq 'HASH') {
 1945:                                 if ($settings->{$item}->{$type} == 0) {
 1946:                                     $checked = '';
 1947:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1948:                                     $checked =  'checked="checked" ';
 1949:                                 }
 1950:                             }
 1951:                         }
 1952:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1953:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1954:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1955:                                       '</label></span>&nbsp; ';
 1956:                     }
 1957:                 }
 1958:                 if ($context eq 'requestcourses') {
 1959:                     $datatable .= '</tr><tr>';
 1960:                     foreach my $item (@usertools) {
 1961:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1962:                     }
 1963:                     $datatable .= '</tr></table>';
 1964:                 }
 1965:                 $datatable .= '</td>';
 1966:                 unless (($context eq 'requestcourses') ||
 1967:                         ($context eq 'requestauthor')) {
 1968:                     $datatable .= 
 1969:                               '<td class="LC_right_item">'.
 1970:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1971:                               '<input type="text" name="quota_'.$type.
 1972:                               '" value="'.$currdefquota.
 1973:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1974:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1975:                               '<input type="text" name="authorquota_'.$type.
 1976:                               '" value="'.$currauthorquota.
 1977:                               '" size="5" /></span></td>';
 1978:                 }
 1979:                 $datatable .= '</tr>';
 1980:             }
 1981:         }
 1982:     }
 1983:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1984:         $defaultquota = '20';
 1985:         $authorquota = '500';
 1986:         if (ref($settings) eq 'HASH') {
 1987:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1988:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1989:             } elsif (defined($settings->{'default'})) {
 1990:                 $defaultquota = $settings->{'default'};
 1991:             }
 1992:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1993:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1994:             }
 1995:         }
 1996:     }
 1997:     $typecount ++;
 1998:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1999:     $datatable .= '<tr'.$css_class.'>'.
 2000:                   '<td>'.$othertitle.'</td>'.
 2001:                   '<td class="LC_left_item">';
 2002:     if ($context eq 'requestcourses') {
 2003:         $datatable .= '<table><tr>';
 2004:     }
 2005:     my %defcell;
 2006:     foreach my $item (@usertools) {
 2007:         if ($context eq 'requestcourses') {
 2008:             my ($curroption,$currlimit);
 2009:             if (ref($settings) eq 'HASH') {
 2010:                 if (ref($settings->{$item}) eq 'HASH') {
 2011:                     $curroption = $settings->{$item}->{'default'};
 2012:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2013:                         $currlimit = $1;
 2014:                     }
 2015:                 }
 2016:             }
 2017:             if (!$curroption) {
 2018:                 $curroption = 'norequest';
 2019:             }
 2020:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2021:             foreach my $option (@options) {
 2022:                 my $val = $option;
 2023:                 if ($option eq 'norequest') {
 2024:                     $val = 0;
 2025:                 }
 2026:                 if ($option eq 'validate') {
 2027:                     my $canvalidate = 0;
 2028:                     if (ref($validations{$item}) eq 'HASH') {
 2029:                         if ($validations{$item}{'default'}) {
 2030:                             $canvalidate = 1;
 2031:                         }
 2032:                     }
 2033:                     next if (!$canvalidate);
 2034:                 }
 2035:                 my $checked = '';
 2036:                 if ($option eq $curroption) {
 2037:                     $checked = ' checked="checked"';
 2038:                 } elsif ($option eq 'autolimit') {
 2039:                     if ($curroption =~ /^autolimit/) {
 2040:                         $checked = ' checked="checked"';
 2041:                     }
 2042:                 }
 2043:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2044:                                   '<input type="radio" name="crsreq_'.$item.
 2045:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2046:                                   $titles{$option}.'</label>';
 2047:                 if ($option eq 'autolimit') {
 2048:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2049:                                        $item.'_limit_default" size="1" '.
 2050:                                        'value="'.$currlimit.'" />';
 2051:                 }
 2052:                 $defcell{$item} .= '</span> ';
 2053:                 if ($option eq 'autolimit') {
 2054:                     $defcell{$item} .= $titles{'unlimited'};
 2055:                 }
 2056:             }
 2057:         } elsif ($context eq 'requestauthor') {
 2058:             my $curroption;
 2059:             if (ref($settings) eq 'HASH') {
 2060:                 $curroption = $settings->{'default'};
 2061:             }
 2062:             if (!$curroption) {
 2063:                 $curroption = 'norequest';
 2064:             }
 2065:             foreach my $option (@options) {
 2066:                 my $val = $option;
 2067:                 if ($option eq 'norequest') {
 2068:                     $val = 0;
 2069:                 }
 2070:                 my $checked = '';
 2071:                 if ($option eq $curroption) {
 2072:                     $checked = ' checked="checked"';
 2073:                 }
 2074:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2075:                               '<input type="radio" name="authorreq_default"'.
 2076:                               ' value="'.$val.'"'.$checked.' />'.
 2077:                               $titles{$option}.'</label></span>&nbsp; ';
 2078:             }
 2079:         } else {
 2080:             my $checked = 'checked="checked" ';
 2081:             if (ref($settings) eq 'HASH') {
 2082:                 if (ref($settings->{$item}) eq 'HASH') {
 2083:                     if ($settings->{$item}->{'default'} == 0) {
 2084:                         $checked = '';
 2085:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2086:                         $checked = 'checked="checked" ';
 2087:                     }
 2088:                 }
 2089:             }
 2090:             $datatable .= '<span class="LC_nobreak"><label>'.
 2091:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2092:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2093:                           '</label></span>&nbsp; ';
 2094:         }
 2095:     }
 2096:     if ($context eq 'requestcourses') {
 2097:         $datatable .= '</tr><tr>';
 2098:         foreach my $item (@usertools) {
 2099:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2100:         }
 2101:         $datatable .= '</tr></table>';
 2102:     }
 2103:     $datatable .= '</td>';
 2104:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2105:         $datatable .= '<td class="LC_right_item">'.
 2106:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2107:                       '<input type="text" name="defaultquota" value="'.
 2108:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2109:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2110:                       '<input type="text" name="authorquota" value="'.
 2111:                       $authorquota.'" size="5" /></span></td>';
 2112:     }
 2113:     $datatable .= '</tr>';
 2114:     $typecount ++;
 2115:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2116:     $datatable .= '<tr'.$css_class.'>'.
 2117:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2118:     if ($context eq 'requestcourses') {
 2119:         $datatable .= &mt('(overrides affiliation, if set)').
 2120:                       '</td>'.
 2121:                       '<td class="LC_left_item">'.
 2122:                       '<table><tr>';
 2123:     } else {
 2124:         $datatable .= &mt('(overrides affiliation, if checked)').
 2125:                       '</td>'.
 2126:                       '<td class="LC_left_item" colspan="2">'.
 2127:                       '<br />';
 2128:     }
 2129:     my %advcell;
 2130:     foreach my $item (@usertools) {
 2131:         if ($context eq 'requestcourses') {
 2132:             my ($curroption,$currlimit);
 2133:             if (ref($settings) eq 'HASH') {
 2134:                 if (ref($settings->{$item}) eq 'HASH') {
 2135:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2136:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2137:                         $currlimit = $1;
 2138:                     }
 2139:                 }
 2140:             }
 2141:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2142:             my $checked = '';
 2143:             if ($curroption eq '') {
 2144:                 $checked = ' checked="checked"';
 2145:             }
 2146:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2147:                                '<input type="radio" name="crsreq_'.$item.
 2148:                                '__LC_adv" value=""'.$checked.' />'.
 2149:                                &mt('No override set').'</label></span>&nbsp; ';
 2150:             foreach my $option (@options) {
 2151:                 my $val = $option;
 2152:                 if ($option eq 'norequest') {
 2153:                     $val = 0;
 2154:                 }
 2155:                 if ($option eq 'validate') {
 2156:                     my $canvalidate = 0;
 2157:                     if (ref($validations{$item}) eq 'HASH') {
 2158:                         if ($validations{$item}{'_LC_adv'}) {
 2159:                             $canvalidate = 1;
 2160:                         }
 2161:                     }
 2162:                     next if (!$canvalidate);
 2163:                 }
 2164:                 my $checked = '';
 2165:                 if ($val eq $curroption) {
 2166:                     $checked = ' checked="checked"';
 2167:                 } elsif ($option eq 'autolimit') {
 2168:                     if ($curroption =~ /^autolimit/) {
 2169:                         $checked = ' checked="checked"';
 2170:                     }
 2171:                 }
 2172:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2173:                                   '<input type="radio" name="crsreq_'.$item.
 2174:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2175:                                   $titles{$option}.'</label>';
 2176:                 if ($option eq 'autolimit') {
 2177:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2178:                                        $item.'_limit__LC_adv" size="1" '.
 2179:                                        'value="'.$currlimit.'" />';
 2180:                 }
 2181:                 $advcell{$item} .= '</span> ';
 2182:                 if ($option eq 'autolimit') {
 2183:                     $advcell{$item} .= $titles{'unlimited'};
 2184:                 }
 2185:             }
 2186:         } elsif ($context eq 'requestauthor') {
 2187:             my $curroption;
 2188:             if (ref($settings) eq 'HASH') {
 2189:                 $curroption = $settings->{'_LC_adv'};
 2190:             }
 2191:             my $checked = '';
 2192:             if ($curroption eq '') {
 2193:                 $checked = ' checked="checked"';
 2194:             }
 2195:             $datatable .= '<span class="LC_nobreak"><label>'.
 2196:                           '<input type="radio" name="authorreq__LC_adv"'.
 2197:                           ' value=""'.$checked.' />'.
 2198:                           &mt('No override set').'</label></span>&nbsp; ';
 2199:             foreach my $option (@options) {
 2200:                 my $val = $option;
 2201:                 if ($option eq 'norequest') {
 2202:                     $val = 0;
 2203:                 }
 2204:                 my $checked = '';
 2205:                 if ($val eq $curroption) {
 2206:                     $checked = ' checked="checked"';
 2207:                 }
 2208:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2209:                               '<input type="radio" name="authorreq__LC_adv"'.
 2210:                               ' value="'.$val.'"'.$checked.' />'.
 2211:                               $titles{$option}.'</label></span>&nbsp; ';
 2212:             }
 2213:         } else {
 2214:             my $checked = 'checked="checked" ';
 2215:             if (ref($settings) eq 'HASH') {
 2216:                 if (ref($settings->{$item}) eq 'HASH') {
 2217:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2218:                         $checked = '';
 2219:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2220:                         $checked = 'checked="checked" ';
 2221:                     }
 2222:                 }
 2223:             }
 2224:             $datatable .= '<span class="LC_nobreak"><label>'.
 2225:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2226:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2227:                           '</label></span>&nbsp; ';
 2228:         }
 2229:     }
 2230:     if ($context eq 'requestcourses') {
 2231:         $datatable .= '</tr><tr>';
 2232:         foreach my $item (@usertools) {
 2233:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2234:         }
 2235:         $datatable .= '</tr></table>';
 2236:     }
 2237:     $datatable .= '</td></tr>';
 2238:     $$rowtotal += $typecount;
 2239:     return $datatable;
 2240: }
 2241: 
 2242: sub print_requestmail {
 2243:     my ($dom,$action,$settings,$rowtotal) = @_;
 2244:     my ($now,$datatable,%currapp);
 2245:     $now = time;
 2246:     if (ref($settings) eq 'HASH') {
 2247:         if (ref($settings->{'notify'}) eq 'HASH') {
 2248:             if ($settings->{'notify'}{'approval'} ne '') {
 2249:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2250:             }
 2251:         }
 2252:     }
 2253:     my $numinrow = 2;
 2254:     my $css_class;
 2255:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2256:     my $text;
 2257:     if ($action eq 'requestcourses') {
 2258:         $text = &mt('Receive notification of course requests requiring approval');
 2259:     } elsif ($action eq 'requestauthor') {
 2260:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2261:     } else {
 2262:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2263:     }
 2264:     $datatable = '<tr'.$css_class.'>'.
 2265:                  ' <td>'.$text.'</td>'.
 2266:                  ' <td class="LC_left_item">';
 2267:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2268:                                                  $action.'notifyapproval',%currapp);
 2269:     if ($numdc > 0) {
 2270:         $datatable .= $table;
 2271:     } else {
 2272:         $datatable .= &mt('There are no active Domain Coordinators');
 2273:     }
 2274:     $datatable .='</td></tr>';
 2275:     return $datatable;
 2276: }
 2277: 
 2278: sub print_studentcode {
 2279:     my ($settings,$rowtotal) = @_;
 2280:     my $rownum = 0; 
 2281:     my ($output,%current);
 2282:     my @crstypes = ('official','unofficial','community','textbook','placement');
 2283:     if (ref($settings) eq 'HASH') {
 2284:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2285:             foreach my $type (@crstypes) {
 2286:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2287:             }
 2288:         }
 2289:     }
 2290:     $output .= '<tr>'.
 2291:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2292:                '<td class="LC_left_item">';
 2293:     foreach my $type (@crstypes) {
 2294:         my $check = ' ';
 2295:         if ($current{$type}) {
 2296:             $check = ' checked="checked" ';
 2297:         }
 2298:         $output .= '<span class="LC_nobreak"><label>'.
 2299:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2300:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2301:     }
 2302:     $output .= '</td></tr>';
 2303:     $$rowtotal ++;
 2304:     return $output;
 2305: }
 2306: 
 2307: sub print_textbookcourses {
 2308:     my ($dom,$type,$settings,$rowtotal) = @_;
 2309:     my $rownum = 0;
 2310:     my $css_class;
 2311:     my $itemcount = 1;
 2312:     my $maxnum = 0;
 2313:     my $bookshash;
 2314:     if (ref($settings) eq 'HASH') {
 2315:         $bookshash = $settings->{$type};
 2316:     }
 2317:     my %ordered;
 2318:     if (ref($bookshash) eq 'HASH') {
 2319:         foreach my $item (keys(%{$bookshash})) {
 2320:             if (ref($bookshash->{$item}) eq 'HASH') {
 2321:                 my $num = $bookshash->{$item}{'order'};
 2322:                 $ordered{$num} = $item;
 2323:             }
 2324:         }
 2325:     }
 2326:     my $confname = $dom.'-domainconfig';
 2327:     my $switchserver = &check_switchserver($dom,$confname);
 2328:     my $maxnum = scalar(keys(%ordered));
 2329:     my $datatable;
 2330:     if (keys(%ordered)) {
 2331:         my @items = sort { $a <=> $b } keys(%ordered);
 2332:         for (my $i=0; $i<@items; $i++) {
 2333:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2334:             my $key = $ordered{$items[$i]};
 2335:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2336:             my $coursetitle = $coursehash{'description'};
 2337:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2338:             if (ref($bookshash->{$key}) eq 'HASH') {
 2339:                 $subject = $bookshash->{$key}->{'subject'};
 2340:                 $title = $bookshash->{$key}->{'title'};
 2341:                 if ($type eq 'textbooks') {
 2342:                     $publisher = $bookshash->{$key}->{'publisher'};
 2343:                     $author = $bookshash->{$key}->{'author'};
 2344:                     $image = $bookshash->{$key}->{'image'};
 2345:                     if ($image ne '') {
 2346:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2347:                         my $imagethumb = "$path/tn-".$imagefile;
 2348:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2349:                     }
 2350:                 }
 2351:             }
 2352:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2353:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2354:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2355:             for (my $k=0; $k<=$maxnum; $k++) {
 2356:                 my $vpos = $k+1;
 2357:                 my $selstr;
 2358:                 if ($k == $i) {
 2359:                     $selstr = ' selected="selected" ';
 2360:                 }
 2361:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2362:             }
 2363:             $datatable .= '</select>'.('&nbsp;'x2).
 2364:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2365:                 &mt('Delete?').'</label></span></td>'.
 2366:                 '<td colspan="2">'.
 2367:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2368:                 ('&nbsp;'x2).
 2369:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2370:             if ($type eq 'textbooks') {
 2371:                 $datatable .= ('&nbsp;'x2).
 2372:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2373:                               ('&nbsp;'x2).
 2374:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2375:                               ('&nbsp;'x2).
 2376:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2377:                 if ($image) {
 2378:                     $datatable .= $imgsrc.
 2379:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2380:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2381:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2382:                 }
 2383:                 if ($switchserver) {
 2384:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2385:                 } else {
 2386:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2387:                 }
 2388:             }
 2389:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2390:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2391:                           $coursetitle.'</span></td></tr>'."\n";
 2392:             $itemcount ++;
 2393:         }
 2394:     }
 2395:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2396:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2397:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2398:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2399:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2400:     for (my $k=0; $k<$maxnum+1; $k++) {
 2401:         my $vpos = $k+1;
 2402:         my $selstr;
 2403:         if ($k == $maxnum) {
 2404:             $selstr = ' selected="selected" ';
 2405:         }
 2406:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2407:     }
 2408:     $datatable .= '</select>&nbsp;'."\n".
 2409:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2410:                   '<td colspan="2">'.
 2411:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2412:                   ('&nbsp;'x2).
 2413:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2414:                   ('&nbsp;'x2);
 2415:     if ($type eq 'textbooks') {
 2416:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2417:                       ('&nbsp;'x2).
 2418:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2419:                       ('&nbsp;'x2).
 2420:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2421:         if ($switchserver) {
 2422:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2423:         } else {
 2424:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2425:         }
 2426:     }
 2427:     $datatable .= '</span>'."\n".
 2428:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2429:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2430:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2431:                   &Apache::loncommon::selectcourse_link
 2432:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2433:                   '</span></td>'."\n".
 2434:                   '</tr>'."\n";
 2435:     $itemcount ++;
 2436:     return $datatable;
 2437: }
 2438: 
 2439: sub textbookcourses_javascript {
 2440:     my ($settings) = @_;
 2441:     return unless(ref($settings) eq 'HASH');
 2442:     my (%ordered,%total,%jstext);
 2443:     foreach my $type ('textbooks','templates') {
 2444:         $total{$type} = 0;
 2445:         if (ref($settings->{$type}) eq 'HASH') {
 2446:             foreach my $item (keys(%{$settings->{$type}})) {
 2447:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2448:                     my $num = $settings->{$type}->{$item}{'order'};
 2449:                     $ordered{$type}{$num} = $item;
 2450:                 }
 2451:             }
 2452:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2453:         }
 2454:         my @jsarray = ();
 2455:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2456:             push(@jsarray,$ordered{$type}{$item});
 2457:         }
 2458:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2459:     }
 2460:     return <<"ENDSCRIPT";
 2461: <script type="text/javascript">
 2462: // <![CDATA[
 2463: function reorderBooks(form,item,caller) {
 2464:     var changedVal;
 2465: $jstext{'textbooks'};
 2466: $jstext{'templates'};
 2467:     var newpos;
 2468:     var maxh;
 2469:     if (caller == 'textbooks') {  
 2470:         newpos = 'textbooks_addbook_pos';
 2471:         maxh = 1 + $total{'textbooks'};
 2472:     } else {
 2473:         newpos = 'templates_addbook_pos';
 2474:         maxh = 1 + $total{'templates'};
 2475:     }
 2476:     var current = new Array;
 2477:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2478:     if (item == newpos) {
 2479:         changedVal = newitemVal;
 2480:     } else {
 2481:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2482:         current[newitemVal] = newpos;
 2483:     }
 2484:     if (caller == 'textbooks') {
 2485:         for (var i=0; i<textbooks.length; i++) {
 2486:             var elementName = 'textbooks_'+textbooks[i];
 2487:             if (elementName != item) {
 2488:                 if (form.elements[elementName]) {
 2489:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2490:                     current[currVal] = elementName;
 2491:                 }
 2492:             }
 2493:         }
 2494:     }
 2495:     if (caller == 'templates') {
 2496:         for (var i=0; i<templates.length; i++) {
 2497:             var elementName = 'templates_'+templates[i];
 2498:             if (elementName != item) {
 2499:                 if (form.elements[elementName]) {
 2500:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2501:                     current[currVal] = elementName;
 2502:                 }
 2503:             }
 2504:         }
 2505:     }
 2506:     var oldVal;
 2507:     for (var j=0; j<maxh; j++) {
 2508:         if (current[j] == undefined) {
 2509:             oldVal = j;
 2510:         }
 2511:     }
 2512:     if (oldVal < changedVal) {
 2513:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2514:            var elementName = current[k];
 2515:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2516:         }
 2517:     } else {
 2518:         for (var k=changedVal; k<oldVal; k++) {
 2519:             var elementName = current[k];
 2520:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2521:         }
 2522:     }
 2523:     return;
 2524: }
 2525: 
 2526: // ]]>
 2527: </script>
 2528: 
 2529: ENDSCRIPT
 2530: }
 2531: 
 2532: sub ltitools_javascript {
 2533:     my ($settings) = @_;
 2534:     return unless(ref($settings) eq 'HASH');
 2535:     my (%ordered,$total,%jstext);
 2536:     $total = 0;
 2537:     foreach my $item (keys(%{$settings})) {
 2538:         if (ref($settings->{$item}) eq 'HASH') {
 2539:             my $num = $settings->{$item}{'order'};
 2540:             $ordered{$num} = $item;
 2541:         }
 2542:     }
 2543:     $total = scalar(keys(%{$settings}));
 2544:     my @jsarray = ();
 2545:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2546:         push(@jsarray,$ordered{$item});
 2547:     }
 2548:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2549:     return <<"ENDSCRIPT";
 2550: <script type="text/javascript">
 2551: // <![CDATA[
 2552: function reorderLTI(form,item) {
 2553:     var changedVal;
 2554: $jstext
 2555:     var newpos = 'ltitools_add_pos';
 2556:     var maxh = 1 + $total;
 2557:     var current = new Array;
 2558:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2559:     if (item == newpos) {
 2560:         changedVal = newitemVal;
 2561:     } else {
 2562:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2563:         current[newitemVal] = newpos;
 2564:     }
 2565:     for (var i=0; i<ltitools.length; i++) {
 2566:         var elementName = 'ltitools_'+ltitools[i];
 2567:         if (elementName != item) {
 2568:             if (form.elements[elementName]) {
 2569:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2570:                 current[currVal] = elementName;
 2571:             }
 2572:         }
 2573:     }
 2574:     var oldVal;
 2575:     for (var j=0; j<maxh; j++) {
 2576:         if (current[j] == undefined) {
 2577:             oldVal = j;
 2578:         }
 2579:     }
 2580:     if (oldVal < changedVal) {
 2581:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2582:            var elementName = current[k];
 2583:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2584:         }
 2585:     } else {
 2586:         for (var k=changedVal; k<oldVal; k++) {
 2587:             var elementName = current[k];
 2588:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2589:         }
 2590:     }
 2591:     return;
 2592: }
 2593: 
 2594: // ]]>
 2595: </script>
 2596: 
 2597: ENDSCRIPT
 2598: }
 2599: 
 2600: sub print_autoenroll {
 2601:     my ($dom,$settings,$rowtotal) = @_;
 2602:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2603:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2604:     if (ref($settings) eq 'HASH') {
 2605:         if (exists($settings->{'run'})) {
 2606:             if ($settings->{'run'} eq '0') {
 2607:                 $runoff = ' checked="checked" ';
 2608:                 $runon = ' ';
 2609:             } else {
 2610:                 $runon = ' checked="checked" ';
 2611:                 $runoff = ' ';
 2612:             }
 2613:         } else {
 2614:             if ($autorun) {
 2615:                 $runon = ' checked="checked" ';
 2616:                 $runoff = ' ';
 2617:             } else {
 2618:                 $runoff = ' checked="checked" ';
 2619:                 $runon = ' ';
 2620:             }
 2621:         }
 2622:         if (exists($settings->{'co-owners'})) {
 2623:             if ($settings->{'co-owners'} eq '0') {
 2624:                 $coownersoff = ' checked="checked" ';
 2625:                 $coownerson = ' ';
 2626:             } else {
 2627:                 $coownerson = ' checked="checked" ';
 2628:                 $coownersoff = ' ';
 2629:             }
 2630:         } else {
 2631:             $coownersoff = ' checked="checked" ';
 2632:             $coownerson = ' ';
 2633:         }
 2634:         if (exists($settings->{'sender_domain'})) {
 2635:             $defdom = $settings->{'sender_domain'};
 2636:         }
 2637:         if (exists($settings->{'autofailsafe'})) {
 2638:             $failsafe = $settings->{'autofailsafe'};
 2639:         }
 2640:     } else {
 2641:         if ($autorun) {
 2642:             $runon = ' checked="checked" ';
 2643:             $runoff = ' ';
 2644:         } else {
 2645:             $runoff = ' checked="checked" ';
 2646:             $runon = ' ';
 2647:         }
 2648:     }
 2649:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2650:     my $notif_sender;
 2651:     if (ref($settings) eq 'HASH') {
 2652:         $notif_sender = $settings->{'sender_uname'};
 2653:     }
 2654:     my $datatable='<tr class="LC_odd_row">'.
 2655:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2656:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2657:                   '<input type="radio" name="autoenroll_run"'.
 2658:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2659:                   '<label><input type="radio" name="autoenroll_run"'.
 2660:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2661:                   '</tr><tr>'.
 2662:                   '<td>'.&mt('Notification messages - sender').
 2663:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2664:                   &mt('username').':&nbsp;'.
 2665:                   '<input type="text" name="sender_uname" value="'.
 2666:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2667:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2668:                   '<tr class="LC_odd_row">'.
 2669:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2670:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2671:                   '<input type="radio" name="autoassign_coowners"'.
 2672:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2673:                   '<label><input type="radio" name="autoassign_coowners"'.
 2674:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2675:                   '</tr><tr>'.
 2676:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2677:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2678:                   '<input type="text" name="autoenroll_failsafe"'.
 2679:                   ' value="'.$failsafe.'" size="4" /></td></tr>';
 2680:     $$rowtotal += 4;
 2681:     return $datatable;
 2682: }
 2683: 
 2684: sub print_autoupdate {
 2685:     my ($position,$dom,$settings,$rowtotal) = @_;
 2686:     my $datatable;
 2687:     if ($position eq 'top') {
 2688:         my $updateon = ' ';
 2689:         my $updateoff = ' checked="checked" ';
 2690:         my $classlistson = ' ';
 2691:         my $classlistsoff = ' checked="checked" ';
 2692:         if (ref($settings) eq 'HASH') {
 2693:             if ($settings->{'run'} eq '1') {
 2694:                 $updateon = $updateoff;
 2695:                 $updateoff = ' ';
 2696:             }
 2697:             if ($settings->{'classlists'} eq '1') {
 2698:                 $classlistson = $classlistsoff;
 2699:                 $classlistsoff = ' ';
 2700:             }
 2701:         }
 2702:         my %title = (
 2703:                    run => 'Auto-update active?',
 2704:                    classlists => 'Update information in classlists?',
 2705:                     );
 2706:         $datatable = '<tr class="LC_odd_row">'. 
 2707:                   '<td>'.&mt($title{'run'}).'</td>'.
 2708:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2709:                   '<input type="radio" name="autoupdate_run"'.
 2710:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2711:                   '<label><input type="radio" name="autoupdate_run"'.
 2712:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2713:                   '</tr><tr>'.
 2714:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2715:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2716:                   '<label><input type="radio" name="classlists"'.
 2717:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2718:                   '<label><input type="radio" name="classlists"'.
 2719:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2720:                   '</tr>';
 2721:         $$rowtotal += 2;
 2722:     } elsif ($position eq 'middle') {
 2723:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2724:         my $numinrow = 3;
 2725:         my $locknamesettings;
 2726:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2727:                                      $dom,$numinrow,$othertitle,
 2728:                                     'lockablenames');
 2729:         $$rowtotal ++;
 2730:     } else {
 2731:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2732:         my @fields = ('lastname','firstname','middlename','generation',
 2733:                       'permanentemail','id');
 2734:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2735:         my $numrows = 0;
 2736:         if (ref($types) eq 'ARRAY') {
 2737:             if (@{$types} > 0) {
 2738:                 $datatable = 
 2739:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2740:                                          \@fields,$types,\$numrows);
 2741:                     $$rowtotal += @{$types}; 
 2742:             }
 2743:         }
 2744:         $datatable .= 
 2745:             &usertype_update_row($settings,{'default' => $othertitle},
 2746:                                  \%fieldtitles,\@fields,['default'],
 2747:                                  \$numrows);
 2748:         $$rowtotal ++;     
 2749:     }
 2750:     return $datatable;
 2751: }
 2752: 
 2753: sub print_autocreate {
 2754:     my ($dom,$settings,$rowtotal) = @_;
 2755:     my (%createon,%createoff,%currhash);
 2756:     my @types = ('xml','req');
 2757:     if (ref($settings) eq 'HASH') {
 2758:         foreach my $item (@types) {
 2759:             $createoff{$item} = ' checked="checked" ';
 2760:             $createon{$item} = ' ';
 2761:             if (exists($settings->{$item})) {
 2762:                 if ($settings->{$item}) {
 2763:                     $createon{$item} = ' checked="checked" ';
 2764:                     $createoff{$item} = ' ';
 2765:                 }
 2766:             }
 2767:         }
 2768:         if ($settings->{'xmldc'} ne '') {
 2769:             $currhash{$settings->{'xmldc'}} = 1;
 2770:         }
 2771:     } else {
 2772:         foreach my $item (@types) {
 2773:             $createoff{$item} = ' checked="checked" ';
 2774:             $createon{$item} = ' ';
 2775:         }
 2776:     }
 2777:     $$rowtotal += 2;
 2778:     my $numinrow = 2;
 2779:     my $datatable='<tr class="LC_odd_row">'.
 2780:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2781:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2782:                   '<input type="radio" name="autocreate_xml"'.
 2783:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2784:                   '<label><input type="radio" name="autocreate_xml"'.
 2785:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2786:                   '</td></tr><tr>'.
 2787:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2788:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2789:                   '<input type="radio" name="autocreate_req"'.
 2790:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2791:                   '<label><input type="radio" name="autocreate_req"'.
 2792:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2793:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2794:                                                    'autocreate_xmldc',%currhash);
 2795:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2796:     if ($numdc > 1) {
 2797:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2798:                       '</td><td class="LC_left_item">';
 2799:     } else {
 2800:         $datatable .= &mt('Course creation processed as:').
 2801:                       '</td><td class="LC_right_item">';
 2802:     }
 2803:     $datatable .= $dctable.'</td></tr>';
 2804:     $$rowtotal += $rows;
 2805:     return $datatable;
 2806: }
 2807: 
 2808: sub print_directorysrch {
 2809:     my ($position,$dom,$settings,$rowtotal) = @_;
 2810:     my $datatable;
 2811:     if ($position eq 'top') {
 2812:         my $instsrchon = ' ';
 2813:         my $instsrchoff = ' checked="checked" ';
 2814:         my ($exacton,$containson,$beginson);
 2815:         my $instlocalon = ' ';
 2816:         my $instlocaloff = ' checked="checked" ';
 2817:         if (ref($settings) eq 'HASH') {
 2818:             if ($settings->{'available'} eq '1') {
 2819:                 $instsrchon = $instsrchoff;
 2820:                 $instsrchoff = ' ';
 2821:             }
 2822:             if ($settings->{'localonly'} eq '1') {
 2823:                 $instlocalon = $instlocaloff;
 2824:                 $instlocaloff = ' ';
 2825:             }
 2826:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2827:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 2828:                     if ($type eq 'exact') {
 2829:                         $exacton = ' checked="checked" ';
 2830:                     } elsif ($type eq 'contains') {
 2831:                         $containson = ' checked="checked" ';
 2832:                     } elsif ($type eq 'begins') {
 2833:                         $beginson = ' checked="checked" ';
 2834:                     }
 2835:                 }
 2836:             } else {
 2837:                 if ($settings->{'searchtypes'} eq 'exact') {
 2838:                     $exacton = ' checked="checked" ';
 2839:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 2840:                     $containson = ' checked="checked" ';
 2841:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 2842:                     $exacton = ' checked="checked" ';
 2843:                     $containson = ' checked="checked" ';
 2844:                 }
 2845:             }
 2846:         }
 2847:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2848:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2849: 
 2850:         my $numinrow = 4;
 2851:         my $cansrchrow = 0;
 2852:         $datatable='<tr class="LC_odd_row">'.
 2853:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 2854:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2855:                    '<input type="radio" name="dirsrch_available"'.
 2856:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2857:                    '<label><input type="radio" name="dirsrch_available"'.
 2858:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2859:                    '</tr><tr>'.
 2860:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 2861:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2862:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 2863:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2864:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 2865:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2866:                    '</tr>';
 2867:         $$rowtotal += 2;
 2868:         if (ref($usertypes) eq 'HASH') {
 2869:             if (keys(%{$usertypes}) > 0) {
 2870:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2871:                                              $numinrow,$othertitle,'cansearch');
 2872:                 $cansrchrow = 1;
 2873:             }
 2874:         }
 2875:         if ($cansrchrow) {
 2876:             $$rowtotal ++;
 2877:             $datatable .= '<tr>';
 2878:         } else {
 2879:             $datatable .= '<tr class="LC_odd_row">';
 2880:         }
 2881:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2882:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2883:         foreach my $title (@{$titleorder}) {
 2884:             if (defined($searchtitles->{$title})) {
 2885:                 my $check = ' ';
 2886:                 if (ref($settings) eq 'HASH') {
 2887:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2888:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2889:                             $check = ' checked="checked" ';
 2890:                         }
 2891:                     }
 2892:                 }
 2893:                 $datatable .= '<td class="LC_left_item">'.
 2894:                               '<span class="LC_nobreak"><label>'.
 2895:                               '<input type="checkbox" name="searchby" '.
 2896:                               'value="'.$title.'"'.$check.'/>'.
 2897:                               $searchtitles->{$title}.'</label></span></td>';
 2898:             }
 2899:         }
 2900:         $datatable .= '</tr></table></td></tr>';
 2901:         $$rowtotal ++;
 2902:         if ($cansrchrow) {
 2903:             $datatable .= '<tr class="LC_odd_row">';
 2904:         } else {
 2905:             $datatable .= '<tr>';
 2906:         }
 2907:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2908:                       '<td class="LC_left_item" colspan="2">'.
 2909:                       '<span class="LC_nobreak"><label>'.
 2910:                       '<input type="checkbox" name="searchtypes" '.
 2911:                       $exacton.' value="exact" />'.&mt('Exact match').
 2912:                       '</label>&nbsp;'.
 2913:                       '<label><input type="checkbox" name="searchtypes" '.
 2914:                       $beginson.' value="begins" />'.&mt('Begins with').
 2915:                       '</label>&nbsp;'.
 2916:                       '<label><input type="checkbox" name="searchtypes" '.
 2917:                       $containson.' value="contains" />'.&mt('Contains').
 2918:                       '</label></span></td></tr>';
 2919:         $$rowtotal ++;
 2920:     } else {
 2921:         my $domsrchon = ' checked="checked" ';
 2922:         my $domsrchoff = ' ';
 2923:         my $domlocalon = ' ';
 2924:         my $domlocaloff = ' checked="checked" ';
 2925:         if (ref($settings) eq 'HASH') {
 2926:             if ($settings->{'lclocalonly'} eq '1') {
 2927:                 $domlocalon = $domlocaloff;
 2928:                 $domlocaloff = ' ';
 2929:             }
 2930:             if ($settings->{'lcavailable'} eq '0') {
 2931:                 $domsrchoff = $domsrchon;
 2932:                 $domsrchon = ' ';
 2933:             }
 2934:         }
 2935:         $datatable='<tr class="LC_odd_row">'.
 2936:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 2937:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2938:                       '<input type="radio" name="dirsrch_domavailable"'.
 2939:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2940:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 2941:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2942:                       '</tr><tr>'.
 2943:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 2944:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2945:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 2946:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2947:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 2948:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2949:                       '</tr>';
 2950:         $$rowtotal += 2;
 2951:     }
 2952:     return $datatable;
 2953: }
 2954: 
 2955: sub print_contacts {
 2956:     my ($position,$dom,$settings,$rowtotal) = @_;
 2957:     my $datatable;
 2958:     my @contacts = ('adminemail','supportemail');
 2959:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 2960:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings);
 2961:     foreach my $type (@mailings) {
 2962:         $otheremails{$type} = '';
 2963:     }
 2964:     if ($position eq 'top') {
 2965:         if (ref($settings) eq 'HASH') {
 2966:             foreach my $item (@contacts) {
 2967:                 if (exists($settings->{$item})) {
 2968:                     $to{$item} = $settings->{$item};
 2969:                 }
 2970:             }
 2971:         }
 2972:     } elsif ($position eq 'middle') {
 2973:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 2974:                      'updatesmail','idconflictsmail');
 2975:     } else {
 2976:         @mailings = ('helpdeskmail','otherdomsmail');
 2977:         $bccemails{'helpdeskmail'} = '';
 2978:         $bccemails{'otherdomsmail'} = '';
 2979:         $includestr{'helpdeskmail'} = '';
 2980:         $includestr{'otherdomsmail'} = '';
 2981:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 2982:     }
 2983:     if (ref($settings) eq 'HASH') {
 2984:         unless ($position eq 'top') {
 2985:             foreach my $type (@mailings) {
 2986:                 if (exists($settings->{$type})) {
 2987:                     if (ref($settings->{$type}) eq 'HASH') {
 2988:                         foreach my $item (@contacts) {
 2989:                             if ($settings->{$type}{$item}) {
 2990:                                 $checked{$type}{$item} = ' checked="checked" ';
 2991:                             }
 2992:                         }
 2993:                         $otheremails{$type} = $settings->{$type}{'others'};
 2994:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 2995:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 2996:                             if ($settings->{$type}{'include'} ne '') {
 2997:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 2998:                                 $includestr{$type} = &unescape($includestr{$type});
 2999:                             }
 3000:                         }
 3001:                     }
 3002:                 } elsif ($type eq 'lonstatusmail') {
 3003:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3004:                 }
 3005:             }
 3006:         }
 3007:         if ($position eq 'bottom') {
 3008:             foreach my $type (@mailings) {
 3009:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 3010:                 if ($settings->{$type}{'include'} ne '') {
 3011:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3012:                     $includestr{$type} = &unescape($includestr{$type});
 3013:                 }
 3014:             }
 3015:             if (ref($settings->{'helpform'}) eq 'HASH') {
 3016:                 if (ref($fields) eq 'ARRAY') {
 3017:                     foreach my $field (@{$fields}) {
 3018:                         $currfield{$field} = $settings->{'helpform'}{$field};
 3019:                     }
 3020:                 }
 3021:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 3022:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 3023:                 } else {
 3024:                     $maxsize = '1.0'; 
 3025:                 }
 3026:             } else {
 3027:                 if (ref($fields) eq 'ARRAY') {
 3028:                     foreach my $field (@{$fields}) {
 3029:                         $currfield{$field} = 'yes';
 3030:                     }
 3031:                 }
 3032:                 $maxsize = '1.0';
 3033:             }
 3034:         }
 3035:     } else {
 3036:         if ($position eq 'top') {
 3037:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 3038:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 3039:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 3040:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 3041:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 3042:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 3043:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 3044:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 3045:         } elsif ($position eq 'bottom') {
 3046:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 3047:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 3048:             if (ref($fields) eq 'ARRAY') {
 3049:                 foreach my $field (@{$fields}) {
 3050:                     $currfield{$field} = 'yes';
 3051:                 }
 3052:             }
 3053:             $maxsize = '1.0';
 3054:         }
 3055:     }
 3056:     my ($titles,$short_titles) = &contact_titles();
 3057:     my $rownum = 0;
 3058:     my $css_class;
 3059:     if ($position eq 'top') {
 3060:         foreach my $item (@contacts) {
 3061:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3062:             $datatable .= '<tr'.$css_class.'>'. 
 3063:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3064:                           '</span></td><td class="LC_right_item">'.
 3065:                           '<input type="text" name="'.$item.'" value="'.
 3066:                           $to{$item}.'" /></td></tr>';
 3067:             $rownum ++;
 3068:         }
 3069:     } else {
 3070:         foreach my $type (@mailings) {
 3071:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3072:             $datatable .= '<tr'.$css_class.'>'.
 3073:                           '<td><span class="LC_nobreak">'.
 3074:                           $titles->{$type}.': </span></td>'.
 3075:                           '<td class="LC_left_item">';
 3076:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3077:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3078:             }
 3079:             $datatable .= '<span class="LC_nobreak">';
 3080:             foreach my $item (@contacts) {
 3081:                 $datatable .= '<label>'.
 3082:                               '<input type="checkbox" name="'.$type.'"'.
 3083:                               $checked{$type}{$item}.
 3084:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3085:                               '</label>&nbsp;';
 3086:             }
 3087:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3088:                           '<input type="text" name="'.$type.'_others" '.
 3089:                           'value="'.$otheremails{$type}.'"  />';
 3090:             my %locchecked;
 3091:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3092:                 foreach my $loc ('s','b') {
 3093:                     if ($includeloc{$type} eq $loc) {
 3094:                         $locchecked{$loc} = ' checked="checked"';
 3095:                         last;
 3096:                     }
 3097:                 }
 3098:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3099:                               '<input type="text" name="'.$type.'_bcc" '.
 3100:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3101:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3102:                               &mt('Text automatically added to e-mail:').' '.
 3103:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br >'.
 3104:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3105:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3106:                               ('&nbsp;'x2).
 3107:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3108:                               '</span></fieldset>';
 3109:             }
 3110:             $datatable .= '</td></tr>'."\n";
 3111:             $rownum ++;
 3112:         }
 3113:     }
 3114:     if ($position eq 'middle') {
 3115:         my %choices;
 3116:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 3117:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3118:                                     &mt('LON-CAPA core group - MSU'),600,500));
 3119:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3120:                                         &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3121:                                         &mt('LON-CAPA core group - MSU'),600,500));
 3122:         my @toggles = ('reporterrors','reportupdates');
 3123:         my %defaultchecked = ('reporterrors'  => 'on',
 3124:                               'reportupdates' => 'on');
 3125:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3126:                                                    \%choices,$rownum);
 3127:         $datatable .= $reports;
 3128:     } elsif ($position eq 'bottom') {
 3129:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3130:         $datatable .= '<tr'.$css_class.'>'.
 3131:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3132:                       &mt('(e-mail, subject, and description always shown)').
 3133:                       '</td><td class="LC_left_item">';
 3134:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') && 
 3135:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3136:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3137:             foreach my $field (@{$fields}) {
 3138:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3139:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3140:                     $datatable .= ' '.&mt('(logged-in users)');
 3141:                 }
 3142:                 $datatable .='</td><td>';
 3143:                 my $clickaction;
 3144:                 if ($field eq 'screenshot') {
 3145:                     $clickaction = ' onclick="screenshotSize(this);"';
 3146:                 }
 3147:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3148:                     foreach my $option (@{$possoptions->{$field}}) {
 3149:                         my $checked;
 3150:                         if ($currfield{$field} eq $option) {
 3151:                             $checked = ' checked="checked"';
 3152:                         }
 3153:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3154:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3155:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3156:                                       '</label></span>'.('&nbsp;'x2);
 3157:                     }
 3158:                 }
 3159:                 if ($field eq 'screenshot') {
 3160:                     my $display;
 3161:                     if ($currfield{$field} eq 'no') {
 3162:                         $display = ' style="display:none"';
 3163:                     }
 3164:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.' />'.
 3165:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3166:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3167:                 }
 3168:                 $datatable .= '</td></tr>';
 3169:             }
 3170:             $datatable .= '</table>';
 3171:         }
 3172:         $datatable .= '</td></tr>'."\n";
 3173:         $rownum ++;
 3174:     }
 3175:     $$rowtotal += $rownum;
 3176:     return $datatable;
 3177: }
 3178: 
 3179: sub contacts_javascript {
 3180:     return <<"ENDSCRIPT";
 3181: 
 3182: <script type="text/javascript">
 3183: // <![CDATA[
 3184: 
 3185: function screenshotSize(field) {
 3186:     if (document.getElementById('help_screenshotsize')) {
 3187:         if (field.value == 'no') {
 3188:             document.getElementById('help_screenshotsize').style.display="none"; 
 3189:         } else {
 3190:             document.getElementById('help_screenshotsize').style.display="";
 3191:         }
 3192:     }
 3193:     return;
 3194: }
 3195: 
 3196: // ]]>
 3197: </script>
 3198: 
 3199: ENDSCRIPT
 3200: }
 3201: 
 3202: sub print_helpsettings {
 3203:     my ($position,$dom,$settings,$rowtotal) = @_;
 3204:     my $confname = $dom.'-domainconfig';
 3205:     my $formname = 'display';
 3206:     my ($datatable,$itemcount);
 3207:     if ($position eq 'top') {
 3208:         $itemcount = 1;
 3209:         my (%choices,%defaultchecked,@toggles);
 3210:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3211:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3212:                                      &mt('LON-CAPA bug tracker'),600,500));
 3213:         %defaultchecked = ('submitbugs' => 'on');
 3214:         @toggles = ('submitbugs');
 3215:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3216:                                                      \%choices,$itemcount);
 3217:         $$rowtotal ++;
 3218:     } else {
 3219:         my $css_class;
 3220:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3221:         my (%customroles,%ordered,%current);
 3222:         if (ref($settings->{'adhoc'}) eq 'HASH') {
 3223:             %current = %{$settings->{'adhoc'}};
 3224:         }
 3225:         my $count = 0;
 3226:         foreach my $key (sort(keys(%existing))) {
 3227:             if ($key=~/^rolesdef\_(\w+)$/) {
 3228:                 my $rolename = $1;
 3229:                 my (%privs,$order);
 3230:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3231:                 $customroles{$rolename} = \%privs;
 3232:                 if (ref($current{$rolename}) eq 'HASH') {
 3233:                     $order = $current{$rolename}{'order'};
 3234:                 }
 3235:                 if ($order eq '') {
 3236:                     $order = $count;
 3237:                 }
 3238:                 $ordered{$order} = $rolename;
 3239:                 $count++;
 3240:             }
 3241:         }
 3242:         my $maxnum = scalar(keys(%ordered));
 3243:         my @roles_by_num = ();
 3244:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3245:             push(@roles_by_num,$item);
 3246:         }
 3247:         my $context = 'domprefs';
 3248:         my $crstype = 'Course';
 3249:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3250:         my @accesstypes = ('all','none');
 3251:         my ($numstatustypes,@jsarray);
 3252:         if (ref($types) eq 'ARRAY') {
 3253:             if (@{$types} > 0) {
 3254:                 $numstatustypes = scalar(@{$types});
 3255:                 push(@accesstypes,'status');
 3256:                 @jsarray = ('bystatus');
 3257:             }
 3258:         }
 3259:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh']);
 3260:         if (keys(%domhelpdesk)) {
 3261:             push(@accesstypes,('inc','exc'));
 3262:             push(@jsarray,('notinc','notexc'));
 3263:         }
 3264:         my $hiddenstr = join("','",@jsarray);
 3265:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3266:         my $context = 'domprefs';
 3267:         my $crstype = 'Course';
 3268:         my $prefix = 'helproles_';
 3269:         my $add_class = 'LC_hidden';
 3270:         foreach my $num (@roles_by_num) {
 3271:             my $role = $ordered{$num};
 3272:             my ($desc,$access,@statuses);
 3273:             if (ref($current{$role}) eq 'HASH') {
 3274:                 $desc = $current{$role}{'desc'};
 3275:                 $access = $current{$role}{'access'};
 3276:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3277:                     @statuses = @{$current{$role}{'insttypes'}};
 3278:                 }
 3279:             }
 3280:             if ($desc eq '') {
 3281:                 $desc = $role;
 3282:             }
 3283:             my $identifier = 'custhelp'.$num;
 3284:             my %full=();
 3285:             my %levels= (
 3286:                          course => {},
 3287:                          domain => {},
 3288:                          system => {},
 3289:                         );
 3290:             my %levelscurrent=(
 3291:                                course => {},
 3292:                                domain => {},
 3293:                                system => {},
 3294:                               );
 3295:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3296:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3297:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3298:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3299:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 3300:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3301:             for (my $k=0; $k<=$maxnum; $k++) {
 3302:                 my $vpos = $k+1;
 3303:                 my $selstr;
 3304:                 if ($k == $num) {
 3305:                     $selstr = ' selected="selected" ';
 3306:                 }
 3307:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3308:             }
 3309:             $datatable .= '</select>'.('&nbsp;'x2).
 3310:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3311:                           '</td>'.
 3312:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3313:                           &mt('Name shown to users:').
 3314:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3315:                           '</fieldset>'.
 3316:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3317:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3318:                           '<fieldset>'.
 3319:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3320:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3321:                                                                    \%levelscurrent,$identifier,
 3322:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3323:                           '</fieldset></td>';
 3324:             $itemcount ++;
 3325:         }
 3326:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3327:         my $newcust = 'custhelp'.$count;
 3328:         my (%privs,%levelscurrent);
 3329:         my %full=();
 3330:         my %levels= (
 3331:                      course => {},
 3332:                      domain => {},
 3333:                      system => {},
 3334:                     );
 3335:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3336:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3337:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3338:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 3339:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3340:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3341:         for (my $k=0; $k<$maxnum+1; $k++) {
 3342:             my $vpos = $k+1;
 3343:             my $selstr;
 3344:             if ($k == $maxnum) {
 3345:                 $selstr = ' selected="selected" ';
 3346:             }
 3347:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3348:         }
 3349:         $datatable .= '</select>&nbsp;'."\n".
 3350:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3351:                       '</label></span></td>'.
 3352:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3353:                       '<span class="LC_nobreak">'.
 3354:                       &mt('Internal name:').
 3355:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3356:                       '</span>'.('&nbsp;'x4).
 3357:                       '<span class="LC_nobreak">'.
 3358:                       &mt('Name shown to users:').
 3359:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3360:                       '</span></fieldset>'.
 3361:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3362:                                              $usertypes,$types,\%domhelpdesk).
 3363:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3364:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3365:                                                                 \@templateroles,$newcust).
 3366:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3367:                                                                \%levelscurrent,$newcust).
 3368:                       '</fieldset></td></tr>';
 3369:         $count ++;
 3370:         $$rowtotal += $count;
 3371:     }
 3372:     return $datatable;
 3373: }
 3374: 
 3375: sub adhocbutton {
 3376:     my ($prefix,$num,$field,$visibility) = @_;
 3377:     my %lt = &Apache::lonlocal::texthash(
 3378:                                           show => 'Show details',
 3379:                                           hide => 'Hide details',
 3380:                                         );
 3381:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3382:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3383:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3384:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3385: }
 3386: 
 3387: sub helpsettings_javascript {
 3388:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3389:     return unless(ref($roles_by_num) eq 'ARRAY');
 3390:     my %html_js_lt = &Apache::lonlocal::texthash(
 3391:                                           show => 'Show details',
 3392:                                           hide => 'Hide details',
 3393:                                         );
 3394:     &html_escape(\%html_js_lt);
 3395:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3396:     return <<"ENDSCRIPT";
 3397: <script type="text/javascript">
 3398: // <![CDATA[
 3399: 
 3400: function reorderHelpRoles(form,item) {
 3401:     var changedVal;
 3402: $jstext
 3403:     var newpos = 'helproles_${total}_pos';
 3404:     var maxh = 1 + $total;
 3405:     var current = new Array();
 3406:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3407:     if (item == newpos) {
 3408:         changedVal = newitemVal;
 3409:     } else {
 3410:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3411:         current[newitemVal] = newpos;
 3412:     }
 3413:     for (var i=0; i<helproles.length; i++) {
 3414:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3415:         if (elementName != item) {
 3416:             if (form.elements[elementName]) {
 3417:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3418:                 current[currVal] = elementName;
 3419:             }
 3420:         }
 3421:     }
 3422:     var oldVal;
 3423:     for (var j=0; j<maxh; j++) {
 3424:         if (current[j] == undefined) {
 3425:             oldVal = j;
 3426:         }
 3427:     }
 3428:     if (oldVal < changedVal) {
 3429:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3430:            var elementName = current[k];
 3431:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3432:         }
 3433:     } else {
 3434:         for (var k=changedVal; k<oldVal; k++) {
 3435:             var elementName = current[k];
 3436:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3437:         }
 3438:     }
 3439:     return;
 3440: }
 3441: 
 3442: function helpdeskAccess(num) {
 3443:     var curraccess = null;
 3444:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3445:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3446:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3447:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3448:             }
 3449:         }
 3450:     }
 3451:     var shown = Array();
 3452:     var hidden = Array();
 3453:     if (curraccess == 'none') {
 3454:         hidden = Array('$hiddenstr');
 3455:     } else {
 3456:         if (curraccess == 'status') {
 3457:             shown = Array('bystatus');
 3458:             hidden = Array('notinc','notexc');
 3459:         } else {
 3460:             if (curraccess == 'exc') {
 3461:                 shown = Array('notexc');
 3462:                 hidden = Array('notinc','bystatus');
 3463:             }
 3464:             if (curraccess == 'inc') {
 3465:                 shown = Array('notinc');
 3466:                 hidden = Array('notexc','bystatus');
 3467:             }
 3468:             if (curraccess == 'all') {
 3469:                 hidden = Array('notinc','notexc','bystatus');
 3470:             }
 3471:         }
 3472:     }
 3473:     if (hidden.length > 0) {
 3474:         for (var i=0; i<hidden.length; i++) {
 3475:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3476:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3477:             }
 3478:         }
 3479:     }
 3480:     if (shown.length > 0) {
 3481:         for (var i=0; i<shown.length; i++) {
 3482:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3483:                 if (shown[i] == 'privs') {
 3484:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3485:                 } else {
 3486:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3487:                 }
 3488:             }
 3489:         }
 3490:     }
 3491:     return;
 3492: }
 3493: 
 3494: function toggleHelpdeskItem(num,field) {
 3495:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3496:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3497:             document.getElementById('helproles_'+num+'_'+field).className = 
 3498:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3499:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3500:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3501:             }
 3502:         } else {
 3503:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3504:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3505:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3506:             }
 3507:         }
 3508:     }
 3509:     return;
 3510: }
 3511: 
 3512: // ]]>
 3513: </script>
 3514: 
 3515: ENDSCRIPT
 3516: }
 3517: 
 3518: sub helpdeskroles_access {
 3519:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3520:         $usertypes,$types,$domhelpdesk) = @_;
 3521:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3522:     my %lt = &Apache::lonlocal::texthash(
 3523:                     'rou'    => 'Role usage',
 3524:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3525:                     'all'    => 'All',
 3526:                     'none'   => 'None',
 3527:                     'status' => 'Determined based on institutional status',
 3528:                     'inc'    => 'Include all, but exclude specific personnel',
 3529:                     'exc'    => 'Exclude all, but include specific personnel',
 3530:                   );
 3531:     my %usecheck = (
 3532:                      all => ' checked="checked"',
 3533:                    );
 3534:     my %displaydiv = (
 3535:                       status => 'none',
 3536:                       inc    => 'none',
 3537:                       exc    => 'none',
 3538:                       priv   => 'block',
 3539:                      );
 3540:     my $output;
 3541:     if (ref($current) eq 'HASH') {
 3542:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3543:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3544:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3545:                 delete($usecheck{'all'});
 3546:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3547:                     my $access = $1;
 3548:                     $displaydiv{$access} = 'inline';
 3549:                 } elsif ($current->{access} eq 'none') {
 3550:                     $displaydiv{'priv'} = 'none';
 3551:                 }
 3552:             }
 3553:         }
 3554:     }
 3555:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3556:               '<p>'.$lt{'whi'}.'</p>';
 3557:     foreach my $access (@{$accesstypes}) {
 3558:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3559:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3560:                    $lt{$access}.'</label>';
 3561:         if ($access eq 'status') {
 3562:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3563:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3564:                                                                  $othertitle,$usertypes,$types).
 3565:                        '</div>';
 3566:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3567:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3568:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3569:                        '</div>';
 3570:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3571:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3572:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3573:                        '</div>';
 3574:         }
 3575:         $output .= '</p>';
 3576:     }
 3577:     $output .= '</fieldset>';
 3578:     return $output;
 3579: }
 3580: 
 3581: sub radiobutton_prefs {
 3582:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3583:         $additional,$align) = @_;
 3584:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3585:                    (ref($choices) eq 'HASH'));
 3586: 
 3587:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3588: 
 3589:     foreach my $item (@{$toggles}) {
 3590:         if ($defaultchecked->{$item} eq 'on') {
 3591:             $checkedon{$item} = ' checked="checked" ';
 3592:             $checkedoff{$item} = ' ';
 3593:         } elsif ($defaultchecked->{$item} eq 'off') {
 3594:             $checkedoff{$item} = ' checked="checked" ';
 3595:             $checkedon{$item} = ' ';
 3596:         }
 3597:     }
 3598:     if (ref($settings) eq 'HASH') {
 3599:         foreach my $item (@{$toggles}) {
 3600:             if ($settings->{$item} eq '1') {
 3601:                 $checkedon{$item} =  ' checked="checked" ';
 3602:                 $checkedoff{$item} = ' ';
 3603:             } elsif ($settings->{$item} eq '0') {
 3604:                 $checkedoff{$item} =  ' checked="checked" ';
 3605:                 $checkedon{$item} = ' ';
 3606:             }
 3607:         }
 3608:     }
 3609:     if ($onclick) {
 3610:         $onclick = ' onclick="'.$onclick.'"';
 3611:     }
 3612:     foreach my $item (@{$toggles}) {
 3613:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3614:         $datatable .=
 3615:             '<tr'.$css_class.'><td valign="top">'.
 3616:             '<span class="LC_nobreak">'.$choices->{$item}.
 3617:             '</span></td>';
 3618:         if ($align eq 'left') {
 3619:             $datatable .= '<td class="LC_left_item">';
 3620:         } else {
 3621:             $datatable .= '<td class="LC_right_item">';
 3622:         }
 3623:         $datatable .= 
 3624:             '<span class="LC_nobreak">'.
 3625:             '<label><input type="radio" name="'.
 3626:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3627:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3628:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3629:             '</span>'.$additional.
 3630:             '</td>'.
 3631:             '</tr>';
 3632:         $itemcount ++;
 3633:     }
 3634:     return ($datatable,$itemcount);
 3635: }
 3636: 
 3637: sub print_ltitools {
 3638:     my ($dom,$settings,$rowtotal) = @_;
 3639:     my $rownum = 0;
 3640:     my $css_class;
 3641:     my $itemcount = 1;
 3642:     my $maxnum = 0;
 3643:     my %ordered;
 3644:     if (ref($settings) eq 'HASH') {
 3645:         foreach my $item (keys(%{$settings})) {
 3646:             if (ref($settings->{$item}) eq 'HASH') {
 3647:                 my $num = $settings->{$item}{'order'};
 3648:                 $ordered{$num} = $item;
 3649:             }
 3650:         }
 3651:     }
 3652:     my $confname = $dom.'-domainconfig';
 3653:     my $switchserver = &check_switchserver($dom,$confname);
 3654:     my $maxnum = scalar(keys(%ordered));
 3655:     my $datatable = &ltitools_javascript($settings);
 3656:     my %lt = &ltitools_names();
 3657:     my @courseroles = ('cc','in','ta','ep','st');
 3658:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 3659:     my @fields = ('fullname','firstname','lastname','email','user','roles');
 3660:     if (keys(%ordered)) {
 3661:         my @items = sort { $a <=> $b } keys(%ordered);
 3662:         for (my $i=0; $i<@items; $i++) {
 3663:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3664:             my $item = $ordered{$items[$i]};
 3665:             my ($title,$key,$secret,$url,$imgsrc,$version);
 3666:             if (ref($settings->{$item}) eq 'HASH') {
 3667:                 $title = $settings->{$item}->{'title'};
 3668:                 $url = $settings->{$item}->{'url'};
 3669:                 $key = $settings->{$item}->{'key'};
 3670:                 $secret = $settings->{$item}->{'secret'};
 3671:                 my $image = $settings->{$item}->{'image'};
 3672:                 if ($image ne '') {
 3673:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 3674:                 }
 3675:             }
 3676:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_".$item."'".');"';
 3677:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3678:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 3679:             for (my $k=0; $k<=$maxnum; $k++) {
 3680:                 my $vpos = $k+1;
 3681:                 my $selstr;
 3682:                 if ($k == $i) {
 3683:                     $selstr = ' selected="selected" ';
 3684:                 }
 3685:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3686:             }
 3687:             $datatable .= '</select>'.('&nbsp;'x2).
 3688:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 3689:                 &mt('Delete?').'</label></span></td>'.
 3690:                 '<td colspan="2">'.
 3691:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3692:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 3693:                 ('&nbsp;'x2).
 3694:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 3695:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 3696:                 ('&nbsp;'x2).
 3697:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 3698:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3699:                 '<br /><br />'.
 3700:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_url_'.$i.'"'.
 3701:                 ' value="'.$url.'" /></span>'.
 3702:                 ('&nbsp;'x2).
 3703:                 '<span class="LC_nobreak">'.$lt{'key'}.
 3704:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 3705:                 ('&nbsp;'x2).
 3706:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 3707:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 3708:                 '<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>'.
 3709:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 3710:                 '</fieldset>'.
 3711:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3712:                 '<span class="LC_nobreak">'.&mt('Display target:');
 3713:             my %currdisp;
 3714:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 3715:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 3716:                     $currdisp{'window'} = ' checked="checked"';
 3717:                 } else {
 3718:                     $currdisp{'iframe'} = ' checked="checked"';
 3719:                 }
 3720:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 3721:                     $currdisp{'width'} = $1;
 3722:                 }
 3723:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 3724:                      $currdisp{'height'} = $1;
 3725:                 }
 3726:             } else {
 3727:                 $currdisp{'iframe'} = ' checked="checked"';
 3728:             }
 3729:             foreach my $disp ('iframe','window') {
 3730:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 3731:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 3732:             }
 3733:             $datatable .= ('&nbsp;'x4);
 3734:             foreach my $dimen ('width','height') {
 3735:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3736:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 3737:                               ('&nbsp;'x2);
 3738:             }
 3739:             $datatable .= '<br />';
 3740:             foreach my $extra ('passback','roster') {
 3741:                 my $checkedon = '';
 3742:                 my $checkedoff = ' checked="checked"';
 3743:                 if ($settings->{$item}->{$extra}) {
 3744:                     $checkedon = $checkedoff;
 3745:                     $checkedoff = '';
 3746:                 }
 3747:                 $datatable .= $lt{$extra}.'&nbsp;'.
 3748:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.' />'.
 3749:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 3750:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.' />'.
 3751:                               &mt('No').'</label>'.('&nbsp;'x4);
 3752:             }
 3753:             $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 3754:             if ($imgsrc) {
 3755:                 $datatable .= $imgsrc.
 3756:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 3757:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 3758:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 3759:             } else {
 3760:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3761:             }
 3762:             if ($switchserver) {
 3763:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3764:             } else {
 3765:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 3766:             }
 3767:             $datatable .= '</span></fieldset>';
 3768:             my (%checkedfields,%rolemaps);
 3769:             if (ref($settings->{$item}) eq 'HASH') {
 3770:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 3771:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 3772:                 }
 3773:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 3774:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 3775:                     $checkedfields{'roles'} = 1;
 3776:                 }
 3777:             }
 3778:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3779:                           '<span class="LC_nobreak">';
 3780:             foreach my $field (@fields) {
 3781:                 my $checked;
 3782:                 if ($checkedfields{$field}) {
 3783:                     $checked = ' checked="checked"';
 3784:                 }
 3785:                 $datatable .= '<label>'.
 3786:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$checked.' />'.
 3787:                               $lt{$field}.'</label>'.('&nbsp;' x2);
 3788:             }
 3789:             $datatable .= '</span></fieldset>'.
 3790:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 3791:             foreach my $role (@courseroles) {
 3792:                 my ($selected,$selectnone);
 3793:                 if (!$rolemaps{$role}) {
 3794:                     $selectnone = ' selected="selected"';
 3795:                 }
 3796:                 $datatable .= '<td align="center">'. 
 3797:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 3798:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 3799:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 3800:                 foreach my $ltirole (@ltiroles) {
 3801:                     unless ($selectnone) {
 3802:                         if ($rolemaps{$role} eq $ltirole) {
 3803:                             $selected = ' selected="selected"';
 3804:                         } else {
 3805:                             $selected = '';
 3806:                         }
 3807:                     }
 3808:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 3809:                 }
 3810:                 $datatable .= '</select></td>';
 3811:             }
 3812:             $datatable .= '</tr></table></fieldset>';
 3813:             my %courseconfig;
 3814:             if (ref($settings->{$item}) eq 'HASH') {
 3815:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 3816:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 3817:                 }
 3818:             }
 3819:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 3820:             foreach my $item ('label','title','target') {
 3821:                 my $checked;
 3822:                 if ($courseconfig{$item}) {
 3823:                     $checked = ' checked="checked"';
 3824:                 }
 3825:                 $datatable .= '<label>'.
 3826:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 3827:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 3828:             }
 3829:             $datatable .= '</span></fieldset>'.
 3830:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 3831:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 3832:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 3833:                 my %custom = %{$settings->{$item}->{'custom'}};
 3834:                 if (keys(%custom) > 0) {
 3835:                     foreach my $key (sort(keys(%custom))) {
 3836:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 3837:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 3838:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 3839:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 3840:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 3841:                     }
 3842:                 }
 3843:             }
 3844:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 3845:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 3846:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 3847:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 3848:             $datatable .= '</table></fieldset></td></tr>'."\n";
 3849:             $itemcount ++;
 3850:         }
 3851:     }
 3852:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3853:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_add_pos'".');"';
 3854:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 3855:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 3856:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 3857:     for (my $k=0; $k<$maxnum+1; $k++) {
 3858:         my $vpos = $k+1;
 3859:         my $selstr;
 3860:         if ($k == $maxnum) {
 3861:             $selstr = ' selected="selected" ';
 3862:         }
 3863:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3864:     }
 3865:     $datatable .= '</select>&nbsp;'."\n".
 3866:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</td>'."\n".
 3867:                   '<td colspan="2">'.
 3868:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3869:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_add_title" value="" /></span> '."\n".
 3870:                   ('&nbsp;'x2).
 3871:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 3872:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 3873:                   ('&nbsp;'x2).
 3874:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 3875:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3876:                   '<br />'.
 3877:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_add_url" value="" /></span> '."\n".
 3878:                   ('&nbsp;'x2).
 3879:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 3880:                   ('&nbsp;'x2).
 3881:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 3882:                   '<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".
 3883:                   '</fieldset>'.
 3884:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3885:                   '<span class="LC_nobreak">'.&mt('Display target:');
 3886:     my %defaultdisp;
 3887:     $defaultdisp{'iframe'} = ' checked="checked"';
 3888:     foreach my $disp ('iframe','window') {
 3889:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 3890:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 3891:     }
 3892:     $datatable .= ('&nbsp;'x4);
 3893:     foreach my $dimen ('width','height') {
 3894:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3895:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 3896:                       ('&nbsp;'x2);
 3897:     }
 3898:     $datatable .= '<br />';
 3899:     foreach my $extra ('passback','roster') {
 3900:         $datatable .= $lt{$extra}.'&nbsp;'.
 3901:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="1" />'.
 3902:                       &mt('Yes').'</label>'.('&nbsp;'x2).
 3903:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="0" checked="checked" />'.
 3904:                       &mt('No').'</label>'.('&nbsp;'x4);
 3905:     }
 3906:     $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 3907:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3908:     if ($switchserver) {
 3909:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3910:     } else {
 3911:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 3912:     }
 3913:     $datatable .= '</span></fieldset>'.
 3914:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3915:                   '<span class="LC_nobreak">';
 3916:     foreach my $field (@fields) {
 3917:         $datatable .= '<label>'.
 3918:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'" />'.
 3919:                       $lt{$field}.'</label>'.('&nbsp;' x2);
 3920:     }
 3921:     $datatable .= '</span></fieldset>'.
 3922:                   '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 3923:     foreach my $role (@courseroles) {
 3924:         my ($checked,$checkednone);
 3925:         $datatable .= '<td align="center">'.
 3926:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 3927:                       '<select name="ltitools_add_roles_'.$role.'">'.
 3928:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 3929:         foreach my $ltirole (@ltiroles) {
 3930:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 3931:         }
 3932:         $datatable .= '</select></td>';
 3933:     }
 3934:     $datatable .= '</tr></table></fieldset>'.
 3935:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 3936:     foreach my $item ('label','title','target') {
 3937:          $datatable .= '<label>'.
 3938:                        '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 3939:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 3940:     }
 3941:     $datatable .= '</span></fieldset>'.
 3942:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 3943:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 3944:                   '<tr><td><span class="LC_nobreak">'.
 3945:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 3946:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 3947:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 3948:                   '</table></fieldset></td></tr>'."\n".
 3949:                   '</td>'."\n".
 3950:                   '</tr>'."\n";
 3951:     $itemcount ++;
 3952:     return $datatable;
 3953: }
 3954: 
 3955: sub ltitools_names {
 3956:     my %lt = &Apache::lonlocal::texthash(
 3957:                                           'title'     => 'Title',
 3958:                                           'version'   => 'Version',
 3959:                                           'msgtype'   => 'Message Type',
 3960:                                           'url'       => 'URL',
 3961:                                           'key'       => 'Key',
 3962:                                           'secret'    => 'Secret',
 3963:                                           'icon'      => 'Icon',   
 3964:                                           'user'      => 'Username:domain',
 3965:                                           'fullname'  => 'Full Name',
 3966:                                           'firstname' => 'First Name',
 3967:                                           'lastname'  => 'Last Name',
 3968:                                           'email'     => 'E-mail',
 3969:                                           'roles'     => 'Role',
 3970:                                           'window'    => 'Window/Tab',
 3971:                                           'iframe'    => 'iFrame',
 3972:                                           'height'    => 'Height',
 3973:                                           'width'     => 'Width',
 3974:                                           'passback'  => 'Tool can return grades:',
 3975:                                           'roster'    => 'Tool can retrieve roster:',
 3976:                                           'crstarget' => 'Display target',
 3977:                                           'crslabel'  => 'Course label',
 3978:                                           'crstitle'  => 'Course title', 
 3979:                                         );
 3980:     return %lt;
 3981: }
 3982: 
 3983: sub print_coursedefaults {
 3984:     my ($position,$dom,$settings,$rowtotal) = @_;
 3985:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 3986:     my $itemcount = 1;
 3987:     my %choices =  &Apache::lonlocal::texthash (
 3988:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 3989:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 3990:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 3991:         coursecredits        => 'Credits can be specified for courses',
 3992:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 3993:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 3994:         postsubmit           => 'Disable submit button/keypress following student submission',
 3995:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 3996:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 3997:     );
 3998:     my %staticdefaults = (
 3999:                            anonsurvey_threshold => 10,
 4000:                            uploadquota          => 500,
 4001:                            postsubmit           => 60,
 4002:                            mysqltables          => 172800,
 4003:                          );
 4004:     if ($position eq 'top') {
 4005:         %defaultchecked = (
 4006:                             'canuse_pdfforms' => 'off',
 4007:                             'uselcmath'       => 'on',
 4008:                             'usejsme'         => 'on',
 4009:                             'canclone'        => 'none', 
 4010:                           );
 4011:         @toggles = ('canuse_pdfforms','uselcmath','usejsme');
 4012:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4013:                                                      \%choices,$itemcount);
 4014:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4015:         $datatable .=
 4016:             '<tr'.$css_class.'><td valign="top">'.
 4017:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 4018:             '</span></td><td class="LC_left_item">';
 4019:         my $currcanclone = 'none';
 4020:         my $onclick;
 4021:         my @cloneoptions = ('none','domain');
 4022:         my %clonetitles = (
 4023:                              none     => 'No additional course requesters',
 4024:                              domain   => "Any course requester in course's domain",
 4025:                              instcode => 'Course requests for official courses ...',
 4026:                           );
 4027:         my (%codedefaults,@code_order,@posscodes);
 4028:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 4029:                                                     \@code_order) eq 'ok') {
 4030:             if (@code_order > 0) {
 4031:                 push(@cloneoptions,'instcode');
 4032:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 4033:             }
 4034:         }
 4035:         if (ref($settings) eq 'HASH') {
 4036:             if ($settings->{'canclone'}) {
 4037:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 4038:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 4039:                         if (@code_order > 0) {
 4040:                             $currcanclone = 'instcode';
 4041:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 4042:                         }
 4043:                     }
 4044:                 } elsif ($settings->{'canclone'} eq 'domain') {
 4045:                     $currcanclone = $settings->{'canclone'};
 4046:                 }
 4047:             }
 4048:         } 
 4049:         foreach my $option (@cloneoptions) {
 4050:             my ($checked,$additional);
 4051:             if ($currcanclone eq $option) {
 4052:                 $checked = ' checked="checked"';
 4053:             }
 4054:             if ($option eq 'instcode') {
 4055:                 if (@code_order) {
 4056:                     my $show = 'none';
 4057:                     if ($checked) {
 4058:                         $show = 'block';
 4059:                     }
 4060:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 4061:                                   &mt('Institutional codes for new and cloned course have identical:').
 4062:                                   '<br />';
 4063:                     foreach my $item (@code_order) {
 4064:                         my $codechk;
 4065:                         if ($checked) {
 4066:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 4067:                                 $codechk = ' checked="checked"';
 4068:                             }
 4069:                         }
 4070:                         $additional .= '<label>'.
 4071:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 4072:                                        $item.'</label>';
 4073:                     }
 4074:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 4075:                 }
 4076:             }
 4077:             $datatable .=
 4078:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 4079:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 4080:                 '</label>&nbsp;'.$additional.'</span><br />';
 4081:         }
 4082:         $datatable .= '</td>'.
 4083:                       '</tr>';
 4084:         $itemcount ++;
 4085:     } else {
 4086:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4087:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 4088:         my $currusecredits = 0;
 4089:         my $postsubmitclient = 1;
 4090:         my @types = ('official','unofficial','community','textbook','placement');
 4091:         if (ref($settings) eq 'HASH') {
 4092:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 4093:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 4094:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 4095:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 4096:                 }
 4097:             }
 4098:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 4099:                 foreach my $type (@types) {
 4100:                     next if ($type eq 'community');
 4101:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 4102:                     if ($defcredits{$type} ne '') {
 4103:                         $currusecredits = 1;
 4104:                     }
 4105:                 }
 4106:             }
 4107:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 4108:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 4109:                     $postsubmitclient = 0;
 4110:                     foreach my $type (@types) {
 4111:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4112:                     }
 4113:                 } else {
 4114:                     foreach my $type (@types) {
 4115:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 4116:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 4117:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type}; 
 4118:                             } else {
 4119:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4120:                             }
 4121:                         } else {
 4122:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4123:                         }
 4124:                     }
 4125:                 }
 4126:             } else {
 4127:                 foreach my $type (@types) {
 4128:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4129:                 }
 4130:             }
 4131:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 4132:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 4133:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 4134:                 }
 4135:             } else {
 4136:                 foreach my $type (@types) {
 4137:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 4138:                 }
 4139:             }
 4140:         } else {
 4141:             foreach my $type (@types) {
 4142:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4143:             }
 4144:         }
 4145:         if (!$currdefresponder) {
 4146:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 4147:         } elsif ($currdefresponder < 1) {
 4148:             $currdefresponder = 1;
 4149:         }
 4150:         foreach my $type (@types) {
 4151:             if ($curruploadquota{$type} eq '') {
 4152:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 4153:             }
 4154:         }
 4155:         $datatable .=
 4156:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4157:                 $choices{'anonsurvey_threshold'}.
 4158:                 '</span></td>'.
 4159:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 4160:                 '<input type="text" name="anonsurvey_threshold"'.
 4161:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 4162:                 '</td></tr>'."\n";
 4163:         $itemcount ++;
 4164:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4165:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4166:                       $choices{'uploadquota'}.
 4167:                       '</span></td>'.
 4168:                       '<td align="right" class="LC_right_item">'.
 4169:                       '<table><tr>';
 4170:         foreach my $type (@types) {
 4171:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4172:                            '<input type="text" name="uploadquota_'.$type.'"'.
 4173:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 4174:         }
 4175:         $datatable .= '</tr></table></td></tr>'."\n";
 4176:         $itemcount ++;
 4177:         my $onclick = "toggleDisplay(this.form,'credits');";
 4178:         my $display = 'none';
 4179:         if ($currusecredits) {
 4180:             $display = 'block';
 4181:         }
 4182:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 4183:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 4184:         foreach my $type (@types) {
 4185:             next if ($type eq 'community');
 4186:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4187:                            '<input type="text" name="'.$type.'_credits"'.
 4188:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 4189:         }
 4190:         $additional .= '</tr></table></div>'."\n";
 4191:         %defaultchecked = ('coursecredits' => 'off');
 4192:         @toggles = ('coursecredits');
 4193:         my $current = {
 4194:                         'coursecredits' => $currusecredits,
 4195:                       };
 4196:         (my $table,$itemcount) =
 4197:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4198:                                \%choices,$itemcount,$onclick,$additional,'left');
 4199:         $datatable .= $table;
 4200:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 4201:         my $display = 'none';
 4202:         if ($postsubmitclient) {
 4203:             $display = 'block';
 4204:         }
 4205:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 4206:                       &mt('Number of seconds submit is disabled').'<br />'.
 4207:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 4208:                       '<table><tr>';
 4209:         foreach my $type (@types) {
 4210:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4211:                            '<input type="text" name="'.$type.'_timeout" value="'.
 4212:                            $deftimeout{$type}.'" size="5" /></td>';
 4213:         }
 4214:         $additional .= '</tr></table></div>'."\n";
 4215:         %defaultchecked = ('postsubmit' => 'on');
 4216:         @toggles = ('postsubmit');
 4217:         $current = {
 4218:                        'postsubmit' => $postsubmitclient,
 4219:                    };
 4220:         ($table,$itemcount) =
 4221:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4222:                                \%choices,$itemcount,$onclick,$additional,'left');
 4223:         $datatable .= $table;
 4224:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4225:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4226:                       $choices{'mysqltables'}.
 4227:                       '</span></td>'.
 4228:                       '<td align="right" class="LC_right_item">'.
 4229:                       '<table><tr>';
 4230:         foreach my $type (@types) {
 4231:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4232:                            '<input type="text" name="mysqltables_'.$type.'"'.
 4233:                            ' value="'.$currmysql{$type}.'" size="5" /></td>';
 4234:         }
 4235:         $datatable .= '</tr></table></td></tr>'."\n";
 4236:         $itemcount ++;
 4237: 
 4238:     }
 4239:     $$rowtotal += $itemcount;
 4240:     return $datatable;
 4241: }
 4242: 
 4243: sub print_selfenrollment {
 4244:     my ($position,$dom,$settings,$rowtotal) = @_;
 4245:     my ($css_class,$datatable);
 4246:     my $itemcount = 1;
 4247:     my @types = ('official','unofficial','community','textbook','placement');
 4248:     if (($position eq 'top') || ($position eq 'middle')) {
 4249:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 4250:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 4251:         my @rows;
 4252:         my $key;
 4253:         if ($position eq 'top') {
 4254:             $key = 'admin'; 
 4255:             if (ref($rowsref) eq 'ARRAY') {
 4256:                 @rows = @{$rowsref};
 4257:             }
 4258:         } elsif ($position eq 'middle') {
 4259:             $key = 'default';
 4260:             @rows = ('types','registered','approval','limit');
 4261:         }
 4262:         foreach my $row (@rows) {
 4263:             if (defined($titlesref->{$row})) {
 4264:                 $itemcount ++;
 4265:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4266:                 $datatable .= '<tr'.$css_class.'>'.
 4267:                               '<td>'.$titlesref->{$row}.'</td>'.
 4268:                               '<td class="LC_left_item">'.
 4269:                               '<table><tr>';
 4270:                 my (%current,%currentcap);
 4271:                 if (ref($settings) eq 'HASH') {
 4272:                     if (ref($settings->{$key}) eq 'HASH') {
 4273:                         foreach my $type (@types) {
 4274:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4275:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 4276:                             }
 4277:                             if (($row eq 'limit') && ($key eq 'default')) {
 4278:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4279:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 4280:                                 }
 4281:                             }
 4282:                         }
 4283:                     }
 4284:                 }
 4285:                 my %roles = (
 4286:                              '0' => &Apache::lonnet::plaintext('dc'),
 4287:                             ); 
 4288:             
 4289:                 foreach my $type (@types) {
 4290:                     unless (($row eq 'registered') && ($key eq 'default')) {
 4291:                         $datatable .= '<th>'.&mt($type).'</th>';
 4292:                     }
 4293:                 }
 4294:                 unless (($row eq 'registered') && ($key eq 'default')) {
 4295:                     $datatable .= '</tr><tr>';
 4296:                 }
 4297:                 foreach my $type (@types) {
 4298:                     if ($type eq 'community') {
 4299:                         $roles{'1'} = &mt('Community personnel');
 4300:                     } else {
 4301:                         $roles{'1'} = &mt('Course personnel');
 4302:                     }
 4303:                     $datatable .= '<td style="vertical-align: top">';
 4304:                     if ($position eq 'top') {
 4305:                         my %checked;
 4306:                         if ($current{$type} eq '0') {
 4307:                             $checked{'0'} = ' checked="checked"';
 4308:                         } else {
 4309:                             $checked{'1'} = ' checked="checked"';
 4310:                         }
 4311:                         foreach my $role ('1','0') {
 4312:                             $datatable .= '<span class="LC_nobreak"><label>'.
 4313:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 4314:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 4315:                                           $roles{$role}.'</label></span> ';
 4316:                         }
 4317:                     } else {
 4318:                         if ($row eq 'types') {
 4319:                             my %checked;
 4320:                             if ($current{$type} =~ /^(all|dom)$/) {
 4321:                                 $checked{$1} = ' checked="checked"';
 4322:                             } else {
 4323:                                 $checked{''} = ' checked="checked"';
 4324:                             }
 4325:                             foreach my $val ('','dom','all') {
 4326:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4327:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4328:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4329:                             }
 4330:                         } elsif ($row eq 'registered') {
 4331:                             my %checked;
 4332:                             if ($current{$type} eq '1') {
 4333:                                 $checked{'1'} = ' checked="checked"';
 4334:                             } else {
 4335:                                 $checked{'0'} = ' checked="checked"';
 4336:                             }
 4337:                             foreach my $val ('0','1') {
 4338:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4339:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4340:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4341:                             }
 4342:                         } elsif ($row eq 'approval') {
 4343:                             my %checked;
 4344:                             if ($current{$type} =~ /^([12])$/) {
 4345:                                 $checked{$1} = ' checked="checked"';
 4346:                             } else {
 4347:                                 $checked{'0'} = ' checked="checked"';
 4348:                             }
 4349:                             for my $val (0..2) {
 4350:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4351:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4352:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4353:                             }
 4354:                         } elsif ($row eq 'limit') {
 4355:                             my %checked;
 4356:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 4357:                                 $checked{$1} = ' checked="checked"';
 4358:                             } else {
 4359:                                 $checked{'none'} = ' checked="checked"';
 4360:                             }
 4361:                             my $cap;
 4362:                             if ($currentcap{$type} =~ /^\d+$/) {
 4363:                                 $cap = $currentcap{$type};
 4364:                             }
 4365:                             foreach my $val ('none','allstudents','selfenrolled') {
 4366:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4367:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4368:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4369:                             }
 4370:                             $datatable .= '<br />'.
 4371:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 4372:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 4373:                                           '</span>'; 
 4374:                         }
 4375:                     }
 4376:                     $datatable .= '</td>';
 4377:                 }
 4378:                 $datatable .= '</tr>';
 4379:             }
 4380:             $datatable .= '</table></td></tr>';
 4381:         }
 4382:     } elsif ($position eq 'bottom') {
 4383:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 4384:     }
 4385:     $$rowtotal += $itemcount;
 4386:     return $datatable;
 4387: }
 4388: 
 4389: sub print_validation_rows {
 4390:     my ($caller,$dom,$settings,$rowtotal) = @_;
 4391:     my ($itemsref,$namesref,$fieldsref);
 4392:     if ($caller eq 'selfenroll') { 
 4393:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 4394:     } elsif ($caller eq 'requestcourses') {
 4395:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 4396:     }
 4397:     my %currvalidation;
 4398:     if (ref($settings) eq 'HASH') {
 4399:         if (ref($settings->{'validation'}) eq 'HASH') {
 4400:             %currvalidation = %{$settings->{'validation'}};
 4401:         }
 4402:     }
 4403:     my $datatable;
 4404:     my $itemcount = 0;
 4405:     foreach my $item (@{$itemsref}) {
 4406:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4407:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4408:                       $namesref->{$item}.
 4409:                       '</span></td>'.
 4410:                       '<td class="LC_left_item">';
 4411:         if (($item eq 'url') || ($item eq 'button')) {
 4412:             $datatable .= '<span class="LC_nobreak">'.
 4413:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 4414:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 4415:         } elsif ($item eq 'fields') {
 4416:             my @currfields;
 4417:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 4418:                 @currfields = @{$currvalidation{$item}};
 4419:             }
 4420:             foreach my $field (@{$fieldsref}) {
 4421:                 my $check = '';
 4422:                 if (grep(/^\Q$field\E$/,@currfields)) {
 4423:                     $check = ' checked="checked"';
 4424:                 }
 4425:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4426:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 4427:                               ' value="'.$field.'"'.$check.' />'.$field.
 4428:                               '</label></span> ';
 4429:             }
 4430:         } elsif ($item eq 'markup') {
 4431:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 4432:                            $currvalidation{$item}.
 4433:                               '</textarea>';
 4434:         }
 4435:         $datatable .= '</td></tr>'."\n";
 4436:         if (ref($rowtotal)) {
 4437:             $itemcount ++;
 4438:         }
 4439:     }
 4440:     if ($caller eq 'requestcourses') {
 4441:         my %currhash;
 4442:         if (ref($settings) eq 'HASH') {
 4443:             if (ref($settings->{'validation'}) eq 'HASH') {
 4444:                 if ($settings->{'validation'}{'dc'} ne '') {
 4445:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 4446:                 }
 4447:             }
 4448:         }
 4449:         my $numinrow = 2;
 4450:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4451:                                                        'validationdc',%currhash);
 4452:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4453:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 4454:         if ($numdc > 1) {
 4455:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 4456:         } else {
 4457:             $datatable .=  &mt('Course creation processed as: ');
 4458:         }
 4459:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 4460:         $itemcount ++;
 4461:     }
 4462:     if (ref($rowtotal)) {
 4463:         $$rowtotal += $itemcount;
 4464:     }
 4465:     return $datatable;
 4466: }
 4467: 
 4468: sub print_usersessions {
 4469:     my ($position,$dom,$settings,$rowtotal) = @_;
 4470:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 4471:     my (%by_ip,%by_location,@intdoms,@instdoms);
 4472:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4473: 
 4474:     my @alldoms = &Apache::lonnet::all_domains();
 4475:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4476:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4477:     my %altids = &id_for_thisdom(%servers);
 4478:     if ($position eq 'top') {
 4479:         if (keys(%serverhomes) > 1) {
 4480:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 4481:             my $curroffloadnow;
 4482:             if (ref($settings) eq 'HASH') {
 4483:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 4484:                     $curroffloadnow = $settings->{'offloadnow'};
 4485:                 }
 4486:             }
 4487:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 4488:         } else {
 4489:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4490:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 4491:                           '</td></tr>';
 4492:         }
 4493:     } else {
 4494:         my %titles = &usersession_titles();
 4495:         my ($prefix,@types);
 4496:         if ($position eq 'bottom') {
 4497:             $prefix = 'remote';
 4498:             @types = ('version','excludedomain','includedomain');
 4499:         } else {
 4500:             $prefix = 'hosted';
 4501:             @types = ('excludedomain','includedomain');
 4502:         }
 4503:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4504:     }
 4505:     $$rowtotal += $itemcount;
 4506:     return $datatable;
 4507: }
 4508: 
 4509: sub rules_by_location {
 4510:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 4511:     my ($datatable,$itemcount,$css_class);
 4512:     if (keys(%{$by_location}) == 0) {
 4513:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4514:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 4515:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 4516:                      '</td></tr>';
 4517:         $itemcount = 1;
 4518:     } else {
 4519:         $itemcount = 0;
 4520:         my $numinrow = 5;
 4521:         my (%current,%checkedon,%checkedoff);
 4522:         my @locations = sort(keys(%{$by_location}));
 4523:         foreach my $type (@{$types}) {
 4524:             $checkedon{$type} = '';
 4525:             $checkedoff{$type} = ' checked="checked"';
 4526:         }
 4527:         if (ref($settings) eq 'HASH') {
 4528:             if (ref($settings->{$prefix}) eq 'HASH') {
 4529:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 4530:                     $current{$key} = $settings->{$prefix}{$key};
 4531:                     if ($key eq 'version') {
 4532:                         if ($current{$key} ne '') {
 4533:                             $checkedon{$key} = ' checked="checked"';
 4534:                             $checkedoff{$key} = '';
 4535:                         }
 4536:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 4537:                         $checkedon{$key} = ' checked="checked"';
 4538:                         $checkedoff{$key} = '';
 4539:                     }
 4540:                 }
 4541:             }
 4542:         }
 4543:         foreach my $type (@{$types}) {
 4544:             next if ($type ne 'version' && !@locations);
 4545:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4546:             $datatable .= '<tr'.$css_class.'>
 4547:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 4548:                            <span class="LC_nobreak">&nbsp;
 4549:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 4550:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 4551:             if ($type eq 'version') {
 4552:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 4553:                 my $selector = '<select name="'.$prefix.'_version">';
 4554:                 foreach my $version (@lcversions) {
 4555:                     my $selected = '';
 4556:                     if ($current{'version'} eq $version) {
 4557:                         $selected = ' selected="selected"';
 4558:                     }
 4559:                     $selector .= ' <option value="'.$version.'"'.
 4560:                                  $selected.'>'.$version.'</option>';
 4561:                 }
 4562:                 $selector .= '</select> ';
 4563:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 4564:             } else {
 4565:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 4566:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 4567:                              ' />'.('&nbsp;'x2).
 4568:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 4569:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 4570:                              "\n".
 4571:                              '</div><div><table>';
 4572:                 my $rem;
 4573:                 for (my $i=0; $i<@locations; $i++) {
 4574:                     my ($showloc,$value,$checkedtype);
 4575:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 4576:                         my $ip = $by_location->{$locations[$i]}->[0];
 4577:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4578:                             $value = join(':',@{$by_ip->{$ip}});
 4579:                             $showloc = join(', ',@{$by_ip->{$ip}});
 4580:                             if (ref($current{$type}) eq 'ARRAY') {
 4581:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 4582:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 4583:                                         $checkedtype = ' checked="checked"';
 4584:                                         last;
 4585:                                     }
 4586:                                 }
 4587:                             }
 4588:                         }
 4589:                     }
 4590:                     $rem = $i%($numinrow);
 4591:                     if ($rem == 0) {
 4592:                         if ($i > 0) {
 4593:                             $datatable .= '</tr>';
 4594:                         }
 4595:                         $datatable .= '<tr>';
 4596:                     }
 4597:                     $datatable .= '<td class="LC_left_item">'.
 4598:                                   '<span class="LC_nobreak"><label>'.
 4599:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 4600:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 4601:                                   '</label></span></td>';
 4602:                 }
 4603:                 $rem = @locations%($numinrow);
 4604:                 my $colsleft = $numinrow - $rem;
 4605:                 if ($colsleft > 1 ) {
 4606:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4607:                                   '&nbsp;</td>';
 4608:                 } elsif ($colsleft == 1) {
 4609:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4610:                 }
 4611:                 $datatable .= '</tr></table>';
 4612:             }
 4613:             $datatable .= '</td></tr>';
 4614:             $itemcount ++;
 4615:         }
 4616:     }
 4617:     return ($datatable,$itemcount);
 4618: }
 4619: 
 4620: sub print_ssl {
 4621:     my ($position,$dom,$settings,$rowtotal) = @_;
 4622:     my ($css_class,$datatable);
 4623:     my $itemcount = 1;
 4624:     if ($position eq 'top') {
 4625:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 4626:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 4627:         my $same_institution;
 4628:         if ($intdom ne '') {
 4629:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 4630:             if (ref($internet_names) eq 'ARRAY') {
 4631:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 4632:                     $same_institution = 1;
 4633:                 }
 4634:             }
 4635:         }
 4636:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4637:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 4638:         if ($same_institution) {
 4639:             my %domservers = &Apache::lonnet::get_servers($dom);
 4640:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 4641:         } else {
 4642:             $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.");
 4643:         }
 4644:         $datatable .= '</td></tr>';
 4645:         $itemcount ++;
 4646:     } else {
 4647:         my %titles = &ssl_titles();
 4648:         my (%by_ip,%by_location,@intdoms,@instdoms);
 4649:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4650:         my @alldoms = &Apache::lonnet::all_domains();
 4651:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4652:         my @domservers = &Apache::lonnet::get_servers($dom);
 4653:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4654:         my %altids = &id_for_thisdom(%servers);
 4655:         if ($position eq 'middle') {
 4656:             foreach my $type ('dom','intdom','other') {
 4657:                 my %checked;
 4658:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4659:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 4660:                               '<td class="LC_right_item">';
 4661:                 my $skip; 
 4662:                 if ($type eq 'dom') {
 4663:                     unless (keys(%servers) > 1) {
 4664:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 4665:                         $skip = 1;
 4666:                     }
 4667:                 }
 4668:                 if ($type eq 'intdom') {
 4669:                     unless (@instdoms > 1) {
 4670:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 4671:                         $skip = 1;
 4672:                     } 
 4673:                 } elsif ($type eq 'other') {
 4674:                     if (keys(%by_location) == 0) {
 4675:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 4676:                         $skip = 1;
 4677:                     }
 4678:                 }
 4679:                 unless ($skip) {
 4680:                     $checked{'yes'} = ' checked="checked"'; 
 4681:                     if (ref($settings) eq 'HASH') {
 4682:                         if (ref($settings->{'connect'}) eq 'HASH') {
 4683:                             if ($settings->{'connect'}->{$type} =~ /^(no|req)$/) {
 4684:                                 $checked{$1} = $checked{'yes'};
 4685:                                 delete($checked{'yes'}); 
 4686:                             }
 4687:                         }
 4688:                     }
 4689:                     foreach my $option ('no','yes','req') {
 4690:                         $datatable .= '<span class="LC_nobreak"><label>'.
 4691:                                       '<input type="radio" name="connect_'.$type.'" '.
 4692:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 4693:                                       '</label></span>'.('&nbsp;'x2);
 4694:                     }
 4695:                 }
 4696:                 $datatable .= '</td></tr>';
 4697:                 $itemcount ++; 
 4698:             }
 4699:         } else {
 4700:             my $prefix = 'replication';
 4701:             my @types = ('certreq','nocertreq');
 4702:             if (keys(%by_location) == 0) {
 4703:                 $datatable .= '<tr'.$css_class.'><td>'.
 4704:                               &mt('Nothing to set here, as there are no other institutions').
 4705:                               '</td></tr>';
 4706:                 $itemcount ++;
 4707:             } else {
 4708:                 ($datatable,$itemcount) = 
 4709:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4710:             }
 4711:         }
 4712:     }
 4713:     $$rowtotal += $itemcount;
 4714:     return $datatable;
 4715: }
 4716: 
 4717: sub ssl_titles {
 4718:     return &Apache::lonlocal::texthash (
 4719:                dom           => 'LON-CAPA servers/VMs from same domain',
 4720:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 4721:                other         => 'External LON-CAPA servers/VMs',
 4722:                connect       => 'Connections to other servers',
 4723:                replication   => 'Replicating content to other institutions',
 4724:                certreq       => 'Client certificate required, but specific domains exempt',
 4725:                nocertreq     => 'No client certificate required, except for specific domains',
 4726:                no            => 'SSL not used',
 4727:                yes           => 'SSL Optional (used if available)',
 4728:                req           => 'SSL Required',
 4729:     );
 4730: }
 4731: 
 4732: sub print_trust {
 4733:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 4734:     my ($css_class,$datatable,%checked,%choices);
 4735:     my (%by_ip,%by_location,@intdoms,@instdoms);
 4736:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4737:     my $itemcount = 1;
 4738:     my %titles = &trust_titles();
 4739:     my @types = ('exc','inc');
 4740:     if ($prefix eq 'top') {
 4741:         $prefix = 'content';
 4742:     } elsif ($prefix eq 'bottom') {
 4743:         $prefix = 'msg';
 4744:     }
 4745:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4746:     $$rowtotal += $itemcount;
 4747:     return $datatable;
 4748: }
 4749: 
 4750: sub trust_titles {
 4751:     return &Apache::lonlocal::texthash(
 4752:                content  => "Access to this domain's content by others",
 4753:                shared   => "Access to other domain's content by this domain",
 4754:                enroll   => "Enrollment in this domain's courses by others", 
 4755:                othcoau  => "Co-author roles in this domain for others",
 4756:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 4757:                domroles => "Domain roles in this domain assignable to others",
 4758:                catalog  => "Course Catalog for this domain displayed elsewhere",
 4759:                reqcrs   => "Requests for creation of courses in this domain by others",
 4760:                msg      => "Users in other domains can send messages to this domain",
 4761:                exc      => "Allow all, but exclude specific domains",
 4762:                inc      => "Deny all, but include specific domains",
 4763:            );
 4764: } 
 4765: 
 4766: sub build_location_hashes {
 4767:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 4768:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 4769:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 4770:     my %iphost = &Apache::lonnet::get_iphost();
 4771:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 4772:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 4773:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 4774:         foreach my $id (@{$iphost{$primary_ip}}) {
 4775:             my $intdom = &Apache::lonnet::internet_dom($id);
 4776:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 4777:                 push(@{$intdoms},$intdom);
 4778:             }
 4779:         }
 4780:     }
 4781:     foreach my $ip (keys(%iphost)) {
 4782:         if (ref($iphost{$ip}) eq 'ARRAY') {
 4783:             foreach my $id (@{$iphost{$ip}}) {
 4784:                 my $location = &Apache::lonnet::internet_dom($id);
 4785:                 if ($location) {
 4786:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 4787:                         my $dom = &Apache::lonnet::host_domain($id);
 4788:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 4789:                             push(@{$instdoms},$dom);
 4790:                         }
 4791:                         next;
 4792:                     }
 4793:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4794:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 4795:                             push(@{$by_ip->{$ip}},$location);
 4796:                         }
 4797:                     } else {
 4798:                         $by_ip->{$ip} = [$location];
 4799:                     }
 4800:                 }
 4801:             }
 4802:         }
 4803:     }
 4804:     foreach my $ip (sort(keys(%{$by_ip}))) {
 4805:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4806:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 4807:             my $first = $by_ip->{$ip}->[0];
 4808:             if (ref($by_location->{$first}) eq 'ARRAY') {
 4809:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 4810:                     push(@{$by_location->{$first}},$ip);
 4811:                 }
 4812:             } else {
 4813:                 $by_location->{$first} = [$ip];
 4814:             }
 4815:         }
 4816:     }
 4817:     return;
 4818: }
 4819: 
 4820: sub current_offloads_to {
 4821:     my ($dom,$settings,$servers) = @_;
 4822:     my (%spareid,%otherdomconfigs);
 4823:     if (ref($servers) eq 'HASH') {
 4824:         foreach my $lonhost (sort(keys(%{$servers}))) {
 4825:             my $gotspares;
 4826:             if (ref($settings) eq 'HASH') {
 4827:                 if (ref($settings->{'spares'}) eq 'HASH') {
 4828:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 4829:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 4830:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 4831:                         $gotspares = 1;
 4832:                     }
 4833:                 }
 4834:             }
 4835:             unless ($gotspares) {
 4836:                 my $gotspares;
 4837:                 my $serverhomeID =
 4838:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 4839:                 my $serverhomedom =
 4840:                     &Apache::lonnet::host_domain($serverhomeID);
 4841:                 if ($serverhomedom ne $dom) {
 4842:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 4843:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4844:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4845:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4846:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4847:                                 $gotspares = 1;
 4848:                             }
 4849:                         }
 4850:                     } else {
 4851:                         $otherdomconfigs{$serverhomedom} =
 4852:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 4853:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 4854:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4855:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4856:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 4857:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4858:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4859:                                         $gotspares = 1;
 4860:                                     }
 4861:                                 }
 4862:                             }
 4863:                         }
 4864:                     }
 4865:                 }
 4866:             }
 4867:             unless ($gotspares) {
 4868:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4869:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4870:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4871:                } else {
 4872:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 4873:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 4874:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4875:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4876:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4877:                     } else {
 4878:                         my %what = (
 4879:                              spareid => 1,
 4880:                         );
 4881:                         my ($result,$returnhash) = 
 4882:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 4883:                         if ($result eq 'ok') { 
 4884:                             if (ref($returnhash) eq 'HASH') {
 4885:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 4886:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 4887:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 4888:                                 }
 4889:                             }
 4890:                         }
 4891:                     }
 4892:                 }
 4893:             }
 4894:         }
 4895:     }
 4896:     return %spareid;
 4897: }
 4898: 
 4899: sub spares_row {
 4900:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 4901:     my $css_class;
 4902:     my $numinrow = 4;
 4903:     my $itemcount = 1;
 4904:     my $datatable;
 4905:     my %typetitles = &sparestype_titles();
 4906:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4907:         foreach my $server (sort(keys(%{$servers}))) {
 4908:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 4909:             my ($othercontrol,$serverdom);
 4910:             if ($serverhome ne $server) {
 4911:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 4912:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4913:             } else {
 4914:                 $serverdom = &Apache::lonnet::host_domain($server);
 4915:                 if ($serverdom ne $dom) {
 4916:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4917:                 }
 4918:             }
 4919:             next unless (ref($spareid->{$server}) eq 'HASH');
 4920:             my $checkednow;
 4921:             if (ref($curroffloadnow) eq 'HASH') {
 4922:                 if ($curroffloadnow->{$server}) {
 4923:                     $checkednow = ' checked="checked"';
 4924:                 }
 4925:             }
 4926:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4927:             $datatable .= '<tr'.$css_class.'>
 4928:                            <td rowspan="2">
 4929:                             <span class="LC_nobreak">'.
 4930:                           &mt('[_1] when busy, offloads to:'
 4931:                               ,'<b>'.$server.'</b>').'</span><br />'.
 4932:                           '<span class="LC_nobreak">'."\n". 
 4933:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 4934:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 4935:                           "\n";
 4936:             my (%current,%canselect);
 4937:             my @choices = 
 4938:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 4939:             foreach my $type ('primary','default') {
 4940:                 if (ref($spareid->{$server}) eq 'HASH') {
 4941:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 4942:                         my @spares = @{$spareid->{$server}{$type}};
 4943:                         if (@spares > 0) {
 4944:                             if ($othercontrol) {
 4945:                                 $current{$type} = join(', ',@spares);
 4946:                             } else {
 4947:                                 $current{$type} .= '<table>';
 4948:                                 my $numspares = scalar(@spares);
 4949:                                 for (my $i=0;  $i<@spares; $i++) {
 4950:                                     my $rem = $i%($numinrow);
 4951:                                     if ($rem == 0) {
 4952:                                         if ($i > 0) {
 4953:                                             $current{$type} .= '</tr>';
 4954:                                         }
 4955:                                         $current{$type} .= '<tr>';
 4956:                                     }
 4957:                                     $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;'.
 4958:                                                        $spareid->{$server}{$type}[$i].
 4959:                                                        '</label></td>'."\n";
 4960:                                 }
 4961:                                 my $rem = @spares%($numinrow);
 4962:                                 my $colsleft = $numinrow - $rem;
 4963:                                 if ($colsleft > 1 ) {
 4964:                                     $current{$type} .= '<td colspan="'.$colsleft.
 4965:                                                        '" class="LC_left_item">'.
 4966:                                                        '&nbsp;</td>';
 4967:                                 } elsif ($colsleft == 1) {
 4968:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 4969:                                 }
 4970:                                 $current{$type} .= '</tr></table>';
 4971:                             }
 4972:                         }
 4973:                     }
 4974:                     if ($current{$type} eq '') {
 4975:                         $current{$type} = &mt('None specified');
 4976:                     }
 4977:                     if ($othercontrol) {
 4978:                         if ($type eq 'primary') {
 4979:                             $canselect{$type} = $othercontrol;
 4980:                         }
 4981:                     } else {
 4982:                         $canselect{$type} = 
 4983:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 4984:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 4985:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 4986:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 4987:                         if (@choices > 0) {
 4988:                             foreach my $lonhost (@choices) {
 4989:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 4990:                             }
 4991:                         }
 4992:                         $canselect{$type} .= '</select>'."\n";
 4993:                     }
 4994:                 } else {
 4995:                     $current{$type} = &mt('Could not be determined');
 4996:                     if ($type eq 'primary') {
 4997:                         $canselect{$type} =  $othercontrol;
 4998:                     }
 4999:                 }
 5000:                 if ($type eq 'default') {
 5001:                     $datatable .= '<tr'.$css_class.'>';
 5002:                 }
 5003:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 5004:                               '<td>'.$current{$type}.'</td>'."\n".
 5005:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 5006:             }
 5007:             $itemcount ++;
 5008:         }
 5009:     }
 5010:     $$rowtotal += $itemcount;
 5011:     return $datatable;
 5012: }
 5013: 
 5014: sub possible_newspares {
 5015:     my ($server,$currspares,$serverhomes,$altids) = @_;
 5016:     my $serverhostname = &Apache::lonnet::hostname($server);
 5017:     my %excluded;
 5018:     if ($serverhostname ne '') {
 5019:         %excluded = (
 5020:                        $serverhostname => 1,
 5021:                     );
 5022:     }
 5023:     if (ref($currspares) eq 'HASH') {
 5024:         foreach my $type (keys(%{$currspares})) {
 5025:             if (ref($currspares->{$type}) eq 'ARRAY') {
 5026:                 if (@{$currspares->{$type}} > 0) {
 5027:                     foreach my $curr (@{$currspares->{$type}}) {
 5028:                         my $hostname = &Apache::lonnet::hostname($curr);
 5029:                         $excluded{$hostname} = 1;
 5030:                     }
 5031:                 }
 5032:             }
 5033:         }
 5034:     }
 5035:     my @choices;
 5036:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5037:         if (keys(%{$serverhomes}) > 1) {
 5038:             foreach my $name (sort(keys(%{$serverhomes}))) {
 5039:                 unless ($excluded{$name}) {
 5040:                     if (exists($altids->{$serverhomes->{$name}})) {
 5041:                         push(@choices,$altids->{$serverhomes->{$name}});
 5042:                     } else {
 5043:                         push(@choices,$serverhomes->{$name});
 5044:                     }
 5045:                 }
 5046:             }
 5047:         }
 5048:     }
 5049:     return sort(@choices);
 5050: }
 5051: 
 5052: sub print_loadbalancing {
 5053:     my ($dom,$settings,$rowtotal) = @_;
 5054:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5055:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5056:     my $numinrow = 1;
 5057:     my $datatable;
 5058:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5059:     my (%currbalancer,%currtargets,%currrules,%existing);
 5060:     if (ref($settings) eq 'HASH') {
 5061:         %existing = %{$settings};
 5062:     }
 5063:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 5064:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 5065:                                   \%currtargets,\%currrules);
 5066:     } else {
 5067:         return;
 5068:     }
 5069:     my ($othertitle,$usertypes,$types) =
 5070:         &Apache::loncommon::sorted_inst_types($dom);
 5071:     my $rownum = 8;
 5072:     if (ref($types) eq 'ARRAY') {
 5073:         $rownum += scalar(@{$types});
 5074:     }
 5075:     my @css_class = ('LC_odd_row','LC_even_row');
 5076:     my $balnum = 0;
 5077:     my $islast;
 5078:     my (@toshow,$disabledtext);
 5079:     if (keys(%currbalancer) > 0) {
 5080:         @toshow = sort(keys(%currbalancer));
 5081:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 5082:             push(@toshow,'');
 5083:         }
 5084:     } else {
 5085:         @toshow = ('');
 5086:         $disabledtext = &mt('No existing load balancer');
 5087:     }
 5088:     foreach my $lonhost (@toshow) {
 5089:         if ($balnum == scalar(@toshow)-1) {
 5090:             $islast = 1;
 5091:         } else {
 5092:             $islast = 0;
 5093:         }
 5094:         my $cssidx = $balnum%2;
 5095:         my $targets_div_style = 'display: none';
 5096:         my $disabled_div_style = 'display: block';
 5097:         my $homedom_div_style = 'display: none';
 5098:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 5099:                       '<td rowspan="'.$rownum.'" valign="top">'.
 5100:                       '<p>';
 5101:         if ($lonhost eq '') {
 5102:             $datatable .= '<span class="LC_nobreak">';
 5103:             if (keys(%currbalancer) > 0) {
 5104:                 $datatable .= &mt('Add balancer:');
 5105:             } else {
 5106:                 $datatable .= &mt('Enable balancer:');
 5107:             }
 5108:             $datatable .= '&nbsp;'.
 5109:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 5110:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 5111:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 5112:                           '<option value="" selected="selected">'.&mt('None').
 5113:                           '</option>'."\n";
 5114:             foreach my $server (sort(keys(%servers))) {
 5115:                 next if ($currbalancer{$server});
 5116:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 5117:             }
 5118:             $datatable .=
 5119:                 '</select>'."\n".
 5120:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 5121:         } else {
 5122:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 5123:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 5124:                            &mt('Stop balancing').'</label>'.
 5125:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 5126:             $targets_div_style = 'display: block';
 5127:             $disabled_div_style = 'display: none';
 5128:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 5129:                 $homedom_div_style = 'display: block';
 5130:             }
 5131:         }
 5132:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 5133:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 5134:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 5135:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 5136:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 5137:         my @sparestypes = ('primary','default');
 5138:         my %typetitles = &sparestype_titles();
 5139:         my %hostherechecked = (
 5140:                                   no => ' checked="checked"',
 5141:                               );
 5142:         foreach my $sparetype (@sparestypes) {
 5143:             my $targettable;
 5144:             for (my $i=0; $i<$numspares; $i++) {
 5145:                 my $checked;
 5146:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 5147:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5148:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5149:                             $checked = ' checked="checked"';
 5150:                         }
 5151:                     }
 5152:                 }
 5153:                 my ($chkboxval,$disabled);
 5154:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 5155:                     $chkboxval = $spares[$i];
 5156:                 }
 5157:                 if (exists($currbalancer{$spares[$i]})) {
 5158:                     $disabled = ' disabled="disabled"';
 5159:                 }
 5160:                 $targettable .=
 5161:                     '<td><span class="LC_nobreak"><label>'.
 5162:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 5163:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 5164:                     '</span></label></span></td>';
 5165:                 my $rem = $i%($numinrow);
 5166:                 if ($rem == 0) {
 5167:                     if (($i > 0) && ($i < $numspares-1)) {
 5168:                         $targettable .= '</tr>';
 5169:                     }
 5170:                     if ($i < $numspares-1) {
 5171:                         $targettable .= '<tr>';
 5172:                     }
 5173:                 }
 5174:             }
 5175:             if ($targettable ne '') {
 5176:                 my $rem = $numspares%($numinrow);
 5177:                 my $colsleft = $numinrow - $rem;
 5178:                 if ($colsleft > 1 ) {
 5179:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5180:                                     '&nbsp;</td>';
 5181:                 } elsif ($colsleft == 1) {
 5182:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 5183:                 }
 5184:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 5185:                                '<table><tr>'.$targettable.'</tr></table><br />';
 5186:             }
 5187:             $hostherechecked{$sparetype} = '';
 5188:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 5189:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5190:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5191:                         $hostherechecked{$sparetype} = ' checked="checked"';
 5192:                         $hostherechecked{'no'} = '';
 5193:                     }
 5194:                 }
 5195:             }
 5196:         }
 5197:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 5198:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 5199:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 5200:         foreach my $sparetype (@sparestypes) {
 5201:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 5202:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 5203:                           '</i></label><br />';
 5204:         }
 5205:         $datatable .= '</div></td></tr>'.
 5206:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 5207:                                            $othertitle,$usertypes,$types,\%servers,
 5208:                                            \%currbalancer,$lonhost,
 5209:                                            $targets_div_style,$homedom_div_style,
 5210:                                            $css_class[$cssidx],$balnum,$islast);
 5211:         $$rowtotal += $rownum;
 5212:         $balnum ++;
 5213:     }
 5214:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 5215:     return $datatable;
 5216: }
 5217: 
 5218: sub get_loadbalancers_config {
 5219:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 5220:     return unless ((ref($servers) eq 'HASH') &&
 5221:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 5222:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 5223:     if (keys(%{$existing}) > 0) {
 5224:         my $oldlonhost;
 5225:         foreach my $key (sort(keys(%{$existing}))) {
 5226:             if ($key eq 'lonhost') {
 5227:                 $oldlonhost = $existing->{'lonhost'};
 5228:                 $currbalancer->{$oldlonhost} = 1;
 5229:             } elsif ($key eq 'targets') {
 5230:                 if ($oldlonhost) {
 5231:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 5232:                 }
 5233:             } elsif ($key eq 'rules') {
 5234:                 if ($oldlonhost) {
 5235:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 5236:                 }
 5237:             } elsif (ref($existing->{$key}) eq 'HASH') {
 5238:                 $currbalancer->{$key} = 1;
 5239:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 5240:                 $currrules->{$key} = $existing->{$key}{'rules'};
 5241:             }
 5242:         }
 5243:     } else {
 5244:         my ($balancerref,$targetsref) =
 5245:                 &Apache::lonnet::get_lonbalancer_config($servers);
 5246:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 5247:             foreach my $server (sort(keys(%{$balancerref}))) {
 5248:                 $currbalancer->{$server} = 1;
 5249:                 $currtargets->{$server} = $targetsref->{$server};
 5250:             }
 5251:         }
 5252:     }
 5253:     return;
 5254: }
 5255: 
 5256: sub loadbalancing_rules {
 5257:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 5258:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 5259:         $css_class,$balnum,$islast) = @_;
 5260:     my $output;
 5261:     my $num = 0;
 5262:     my ($alltypes,$othertypes,$titles) =
 5263:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 5264:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 5265:         foreach my $type (@{$alltypes}) {
 5266:             $num ++;
 5267:             my $current;
 5268:             if (ref($currrules) eq 'HASH') {
 5269:                 $current = $currrules->{$type};
 5270:             }
 5271:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5272:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 5273:                     $current = '';
 5274:                 }
 5275:             }
 5276:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 5277:                                              $servers,$currbalancer,$lonhost,$dom,
 5278:                                              $targets_div_style,$homedom_div_style,
 5279:                                              $css_class,$balnum,$num,$islast);
 5280:         }
 5281:     }
 5282:     return $output;
 5283: }
 5284: 
 5285: sub loadbalancing_titles {
 5286:     my ($dom,$intdom,$usertypes,$types) = @_;
 5287:     my %othertypes = (
 5288:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 5289:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 5290:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 5291:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 5292:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 5293:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 5294:                      );
 5295:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 5296:     if (ref($types) eq 'ARRAY') {
 5297:         unshift(@alltypes,@{$types},'default');
 5298:     }
 5299:     my %titles;
 5300:     foreach my $type (@alltypes) {
 5301:         if ($type =~ /^_LC_/) {
 5302:             $titles{$type} = $othertypes{$type};
 5303:         } elsif ($type eq 'default') {
 5304:             $titles{$type} = &mt('All users from [_1]',$dom);
 5305:             if (ref($types) eq 'ARRAY') {
 5306:                 if (@{$types} > 0) {
 5307:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 5308:                 }
 5309:             }
 5310:         } elsif (ref($usertypes) eq 'HASH') {
 5311:             $titles{$type} = $usertypes->{$type};
 5312:         }
 5313:     }
 5314:     return (\@alltypes,\%othertypes,\%titles);
 5315: }
 5316: 
 5317: sub loadbalance_rule_row {
 5318:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 5319:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 5320:     my @rulenames;
 5321:     my %ruletitles = &offloadtype_text();
 5322:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 5323:         @rulenames = ('balancer','offloadedto','specific');
 5324:     } else {
 5325:         @rulenames = ('default','homeserver');
 5326:         if ($type eq '_LC_external') {
 5327:             push(@rulenames,'externalbalancer');
 5328:         } else {
 5329:             push(@rulenames,'specific');
 5330:         }
 5331:         push(@rulenames,'none');
 5332:     }
 5333:     my $style = $targets_div_style;
 5334:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5335:         $style = $homedom_div_style;
 5336:     }
 5337:     my $space;
 5338:     if ($islast && $num == 1) {
 5339:         $space = '<div display="inline-block">&nbsp;</div>';
 5340:     }
 5341:     my $output =
 5342:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 5343:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 5344:         '<td valaign="top">'.$space.
 5345:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 5346:     for (my $i=0; $i<@rulenames; $i++) {
 5347:         my $rule = $rulenames[$i];
 5348:         my ($checked,$extra);
 5349:         if ($rulenames[$i] eq 'default') {
 5350:             $rule = '';
 5351:         }
 5352:         if ($rulenames[$i] eq 'specific') {
 5353:             if (ref($servers) eq 'HASH') {
 5354:                 my $default;
 5355:                 if (($current ne '') && (exists($servers->{$current}))) {
 5356:                     $checked = ' checked="checked"';
 5357:                 }
 5358:                 unless ($checked) {
 5359:                     $default = ' selected="selected"';
 5360:                 }
 5361:                 $extra =
 5362:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5363:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5364:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 5365:                     '<option value=""'.$default.'></option>'."\n";
 5366:                 foreach my $server (sort(keys(%{$servers}))) {
 5367:                     if (ref($currbalancer) eq 'HASH') {
 5368:                         next if (exists($currbalancer->{$server}));
 5369:                     }
 5370:                     my $selected;
 5371:                     if ($server eq $current) {
 5372:                         $selected = ' selected="selected"';
 5373:                     }
 5374:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 5375:                 }
 5376:                 $extra .= '</select>';
 5377:             }
 5378:         } elsif ($rule eq $current) {
 5379:             $checked = ' checked="checked"';
 5380:         }
 5381:         $output .= '<span class="LC_nobreak"><label>'.
 5382:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 5383:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 5384:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 5385:                    ')"'.$checked.' />&nbsp;';
 5386:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 5387:             $output .= $ruletitles{'particular'};
 5388:         } else {
 5389:             $output .= $ruletitles{$rulenames[$i]};
 5390:         }
 5391:         $output .= '</label>'.$extra.'</span><br />'."\n";
 5392:     }
 5393:     $output .= '</div></td></tr>'."\n";
 5394:     return $output;
 5395: }
 5396: 
 5397: sub offloadtype_text {
 5398:     my %ruletitles = &Apache::lonlocal::texthash (
 5399:            'default'          => 'Offloads to default destinations',
 5400:            'homeserver'       => "Offloads to user's home server",
 5401:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 5402:            'specific'         => 'Offloads to specific server',
 5403:            'none'             => 'No offload',
 5404:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 5405:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 5406:            'particular'       => 'Session hosted (after re-auth) on server:',
 5407:     );
 5408:     return %ruletitles;
 5409: }
 5410: 
 5411: sub sparestype_titles {
 5412:     my %typestitles = &Apache::lonlocal::texthash (
 5413:                           'primary' => 'primary',
 5414:                           'default' => 'default',
 5415:                       );
 5416:     return %typestitles;
 5417: }
 5418: 
 5419: sub contact_titles {
 5420:     my %titles = &Apache::lonlocal::texthash (
 5421:                    'supportemail'    => 'Support E-mail address',
 5422:                    'adminemail'      => 'Default Server Admin E-mail address',
 5423:                    'errormail'       => 'Error reports to be e-mailed to',
 5424:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 5425:                    'helpdeskmail'    => "Helpdesk requests for this domain's users",
 5426:                    'otherdomsmail'   => 'Helpdesk requests for other (unconfigured) domains',  
 5427:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 5428:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 5429:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 5430:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 5431:                  );
 5432:     my %short_titles = &Apache::lonlocal::texthash (
 5433:                            adminemail   => 'Admin E-mail address',
 5434:                            supportemail => 'Support E-mail',
 5435:                        );   
 5436:     return (\%titles,\%short_titles);
 5437: }
 5438: 
 5439: sub helpform_fields {
 5440:     my %titles =  &Apache::lonlocal::texthash (
 5441:                        'username'   => 'Name',
 5442:                        'user'       => 'Username/domain',
 5443:                        'phone'      => 'Phone',
 5444:                        'cc'         => 'Cc e-mail',
 5445:                        'course'     => 'Course Details',
 5446:                        'section'    => 'Sections',
 5447:                        'screenshot' => 'File upload', 
 5448:     );
 5449:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 5450:     my %possoptions = (
 5451:                         username     => ['yes','no','req'],
 5452:                         phone        => ['yes','no','req'],   
 5453:                         user         => ['yes','no'],
 5454:                         cc           => ['yes','no'], 
 5455:                         course       => ['yes','no'],
 5456:                         section      => ['yes','no'],
 5457:                         screenshot   => ['yes','no'],
 5458:                       );
 5459:     my %fieldoptions = &Apache::lonlocal::texthash (
 5460:                          'yes'  => 'Optional',
 5461:                          'req'  => 'Required',
 5462:                          'no'   => "Not shown",
 5463:     );
 5464:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 5465: }
 5466: 
 5467: sub tool_titles {
 5468:     my %titles = &Apache::lonlocal::texthash (
 5469:                      aboutme    => 'Personal web page',
 5470:                      blog       => 'Blog',
 5471:                      webdav     => 'WebDAV',
 5472:                      portfolio  => 'Portfolio',
 5473:                      official   => 'Official courses (with institutional codes)',
 5474:                      unofficial => 'Unofficial courses',
 5475:                      community  => 'Communities',
 5476:                      textbook   => 'Textbook courses',
 5477:                      placement  => 'Placement tests',
 5478:                  );
 5479:     return %titles;
 5480: }
 5481: 
 5482: sub courserequest_titles {
 5483:     my %titles = &Apache::lonlocal::texthash (
 5484:                                    official   => 'Official',
 5485:                                    unofficial => 'Unofficial',
 5486:                                    community  => 'Communities',
 5487:                                    textbook   => 'Textbook',
 5488:                                    placement  => 'Placement tests',
 5489:                                    norequest  => 'Not allowed',
 5490:                                    approval   => 'Approval by Dom. Coord.',
 5491:                                    validate   => 'With validation',
 5492:                                    autolimit  => 'Numerical limit',
 5493:                                    unlimited  => '(blank for unlimited)',
 5494:                  );
 5495:     return %titles;
 5496: }
 5497: 
 5498: sub authorrequest_titles {
 5499:     my %titles = &Apache::lonlocal::texthash (
 5500:                                    norequest  => 'Not allowed',
 5501:                                    approval   => 'Approval by Dom. Coord.',
 5502:                                    automatic  => 'Automatic approval',
 5503:                  );
 5504:     return %titles;
 5505: }
 5506: 
 5507: sub courserequest_conditions {
 5508:     my %conditions = &Apache::lonlocal::texthash (
 5509:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 5510:        validate   => '(Processing of request subject to institutional validation).',
 5511:                  );
 5512:     return %conditions;
 5513: }
 5514: 
 5515: 
 5516: sub print_usercreation {
 5517:     my ($position,$dom,$settings,$rowtotal) = @_;
 5518:     my $numinrow = 4;
 5519:     my $datatable;
 5520:     if ($position eq 'top') {
 5521:         $$rowtotal ++;
 5522:         my $rowcount = 0;
 5523:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 5524:         if (ref($rules) eq 'HASH') {
 5525:             if (keys(%{$rules}) > 0) {
 5526:                 $datatable .= &user_formats_row('username',$settings,$rules,
 5527:                                                 $ruleorder,$numinrow,$rowcount);
 5528:                 $$rowtotal ++;
 5529:                 $rowcount ++;
 5530:             }
 5531:         }
 5532:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 5533:         if (ref($idrules) eq 'HASH') {
 5534:             if (keys(%{$idrules}) > 0) {
 5535:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 5536:                                                 $idruleorder,$numinrow,$rowcount);
 5537:                 $$rowtotal ++;
 5538:                 $rowcount ++;
 5539:             }
 5540:         }
 5541:         if ($rowcount == 0) {
 5542:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 5543:             $$rowtotal ++;
 5544:             $rowcount ++;
 5545:         }
 5546:     } elsif ($position eq 'middle') {
 5547:         my @creators = ('author','course','requestcrs');
 5548:         my ($rules,$ruleorder) =
 5549:             &Apache::lonnet::inst_userrules($dom,'username');
 5550:         my %lt = &usercreation_types();
 5551:         my %checked;
 5552:         if (ref($settings) eq 'HASH') {
 5553:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 5554:                 foreach my $item (@creators) {
 5555:                     $checked{$item} = $settings->{'cancreate'}{$item};
 5556:                 }
 5557:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 5558:                 foreach my $item (@creators) {
 5559:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 5560:                         $checked{$item} = 'none';
 5561:                     }
 5562:                 }
 5563:             }
 5564:         }
 5565:         my $rownum = 0;
 5566:         foreach my $item (@creators) {
 5567:             $rownum ++;
 5568:             if ($checked{$item} eq '') {
 5569:                 $checked{$item} = 'any';
 5570:             }
 5571:             my $css_class;
 5572:             if ($rownum%2) {
 5573:                 $css_class = '';
 5574:             } else {
 5575:                 $css_class = ' class="LC_odd_row" ';
 5576:             }
 5577:             $datatable .= '<tr'.$css_class.'>'.
 5578:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 5579:                          '</span></td><td align="right">';
 5580:             my @options = ('any');
 5581:             if (ref($rules) eq 'HASH') {
 5582:                 if (keys(%{$rules}) > 0) {
 5583:                     push(@options,('official','unofficial'));
 5584:                 }
 5585:             }
 5586:             push(@options,'none');
 5587:             foreach my $option (@options) {
 5588:                 my $type = 'radio';
 5589:                 my $check = ' ';
 5590:                 if ($checked{$item} eq $option) {
 5591:                     $check = ' checked="checked" ';
 5592:                 } 
 5593:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5594:                               '<input type="'.$type.'" name="can_createuser_'.
 5595:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 5596:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 5597:             }
 5598:             $datatable .= '</td></tr>';
 5599:         }
 5600:     } else {
 5601:         my @contexts = ('author','course','domain');
 5602:         my @authtypes = ('int','krb4','krb5','loc');
 5603:         my %checked;
 5604:         if (ref($settings) eq 'HASH') {
 5605:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 5606:                 foreach my $item (@contexts) {
 5607:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 5608:                         foreach my $auth (@authtypes) {
 5609:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 5610:                                 $checked{$item}{$auth} = ' checked="checked" ';
 5611:                             }
 5612:                         }
 5613:                     }
 5614:                 }
 5615:             }
 5616:         } else {
 5617:             foreach my $item (@contexts) {
 5618:                 foreach my $auth (@authtypes) {
 5619:                     $checked{$item}{$auth} = ' checked="checked" ';
 5620:                 }
 5621:             }
 5622:         }
 5623:         my %title = &context_names();
 5624:         my %authname = &authtype_names();
 5625:         my $rownum = 0;
 5626:         my $css_class; 
 5627:         foreach my $item (@contexts) {
 5628:             if ($rownum%2) {
 5629:                 $css_class = '';
 5630:             } else {
 5631:                 $css_class = ' class="LC_odd_row" ';
 5632:             }
 5633:             $datatable .=   '<tr'.$css_class.'>'.
 5634:                             '<td>'.$title{$item}.
 5635:                             '</td><td class="LC_left_item">'.
 5636:                             '<span class="LC_nobreak">';
 5637:             foreach my $auth (@authtypes) {
 5638:                 $datatable .= '<label>'. 
 5639:                               '<input type="checkbox" name="'.$item.'_auth" '.
 5640:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 5641:                               $authname{$auth}.'</label>&nbsp;';
 5642:             }
 5643:             $datatable .= '</span></td></tr>';
 5644:             $rownum ++;
 5645:         }
 5646:         $$rowtotal += $rownum;
 5647:     }
 5648:     return $datatable;
 5649: }
 5650: 
 5651: sub print_selfcreation {
 5652:     my ($position,$dom,$settings,$rowtotal) = @_;
 5653:     my (@selfcreate,$createsettings,$processing,$datatable);
 5654:     if (ref($settings) eq 'HASH') {
 5655:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 5656:             $createsettings = $settings->{'cancreate'};
 5657:             if (ref($createsettings) eq 'HASH') {
 5658:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 5659:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 5660:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 5661:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 5662:                         @selfcreate = ('email','login','sso');
 5663:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 5664:                         @selfcreate = ($createsettings->{'selfcreate'});
 5665:                     }
 5666:                 }
 5667:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 5668:                     $processing = $createsettings->{'selfcreateprocessing'};
 5669:                 }
 5670:             }
 5671:         }
 5672:     }
 5673:     my %radiohash;
 5674:     my $numinrow = 4;
 5675:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 5676:     if ($position eq 'top') {
 5677:         my %choices = &Apache::lonlocal::texthash (
 5678:                                                       cancreate_login      => 'Institutional Login',
 5679:                                                       cancreate_sso        => 'Institutional Single Sign On',
 5680:                                                   );
 5681:         my @toggles = sort(keys(%choices));
 5682:         my %defaultchecked = (
 5683:                                'cancreate_login' => 'off',
 5684:                                'cancreate_sso'   => 'off',
 5685:                              );
 5686:         my ($onclick,$itemcount);
 5687:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5688:                                                      \%choices,$itemcount,$onclick);
 5689:         $$rowtotal += $itemcount;
 5690:         
 5691:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5692: 
 5693:         if (ref($usertypes) eq 'HASH') {
 5694:             if (keys(%{$usertypes}) > 0) {
 5695:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 5696:                                              $dom,$numinrow,$othertitle,
 5697:                                              'statustocreate',$$rowtotal);
 5698:                 $$rowtotal ++;
 5699:             }
 5700:         }
 5701:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 5702:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5703:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 5704:         my $rem;
 5705:         my $numperrow = 2;
 5706:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 5707:         $datatable .= '<tr'.$css_class.'>'.
 5708:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 5709:                      '<td class="LC_left_item">'."\n".
 5710:                      '<table><tr><td>'."\n";
 5711:         for (my $i=0; $i<@fields; $i++) {
 5712:             $rem = $i%($numperrow);
 5713:             if ($rem == 0) {
 5714:                 if ($i > 0) {
 5715:                     $datatable .= '</tr>';
 5716:                 }
 5717:                 $datatable .= '<tr>';
 5718:             }
 5719:             my $currval;
 5720:             if (ref($createsettings) eq 'HASH') {
 5721:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 5722:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 5723:                 }
 5724:             }
 5725:             $datatable .= '<td class="LC_left_item">'.
 5726:                           '<span class="LC_nobreak">'.
 5727:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 5728:                           'value="'.$currval.'" size="10" />&nbsp;'.
 5729:                           $fieldtitles{$fields[$i]}.'</span></td>';
 5730:         }
 5731:         my $colsleft = $numperrow - $rem;
 5732:         if ($colsleft > 1 ) {
 5733:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5734:                          '&nbsp;</td>';
 5735:         } elsif ($colsleft == 1) {
 5736:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5737:         }
 5738:         $datatable .= '</tr></table></td></tr>';
 5739:         $$rowtotal ++;
 5740:     } elsif ($position eq 'middle') {
 5741:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 5742:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5743:         $usertypes->{'default'} = $othertitle;
 5744:         if (ref($types) eq 'ARRAY') {
 5745:             push(@{$types},'default');
 5746:             $usertypes->{'default'} = $othertitle;
 5747:             foreach my $status (@{$types}) {
 5748:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 5749:                                                        $numinrow,$$rowtotal,$usertypes);
 5750:                 $$rowtotal ++;
 5751:             }
 5752:         }
 5753:     } else {
 5754:         my %choices = &Apache::lonlocal::texthash (
 5755:                                                       cancreate_email => 'E-mail address as username',
 5756:                                                   );
 5757:         my @toggles = sort(keys(%choices));
 5758:         my %defaultchecked = (
 5759:                                'cancreate_email' => 'off',
 5760:                              );
 5761:         my $itemcount = 0;
 5762:         my $display = 'none';
 5763:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 5764:             $display = 'block';
 5765:         }
 5766:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 5767:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 5768:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5769:         my $usertypes = {};
 5770:         my $order = [];
 5771:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 5772:             $usertypes = $domdefaults{'inststatustypes'};
 5773:             $order = $domdefaults{'inststatusguest'};
 5774:         }
 5775:         if (ref($order) eq 'ARRAY') {
 5776:             push(@{$order},'default');
 5777:             if (@{$order} > 1) {
 5778:                 $usertypes->{'default'} = &mt('Other users');
 5779:                 $additional .= '<table><tr>';
 5780:                 foreach my $status (@{$order}) {
 5781:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 5782:                 }
 5783:                 $additional .= '</tr><tr>';
 5784:                 foreach my $status (@{$order}) {
 5785:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 5786:                 }
 5787:                 $additional .= '</tr></table>';
 5788:             } else {
 5789:                 $usertypes->{'default'} = &mt('All users');
 5790:                 $additional .= &email_as_username($rowtotal,$processing);
 5791:             }
 5792:         }
 5793:         $additional .= '</div>'."\n";
 5794: 
 5795:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5796:                                                      \%choices,$$rowtotal,$onclick,$additional);
 5797:         $$rowtotal ++;
 5798:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 5799:         $$rowtotal ++;
 5800:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 5801:         $numinrow = 1;
 5802:         if (ref($order) eq 'ARRAY') {
 5803:             foreach my $status (@{$order}) {
 5804:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 5805:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 5806:                 $$rowtotal ++;
 5807:             }
 5808:         }
 5809:         my ($emailrules,$emailruleorder) =
 5810:             &Apache::lonnet::inst_userrules($dom,'email');
 5811:         if (ref($emailrules) eq 'HASH') {
 5812:             if (keys(%{$emailrules}) > 0) {
 5813:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 5814:                                                 $emailruleorder,$numinrow,$$rowtotal);
 5815:                 $$rowtotal ++;
 5816:             }
 5817:         }
 5818:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 5819:     }
 5820:     return $datatable;
 5821: }
 5822: 
 5823: sub email_as_username {
 5824:     my ($rowtotal,$processing,$type) = @_;
 5825:     my %choices =
 5826:         &Apache::lonlocal::texthash (
 5827:                                       automatic => 'Automatic approval',
 5828:                                       approval  => 'Queued for approval',
 5829:                                     );
 5830:     my $output;
 5831:     foreach my $option ('automatic','approval') {
 5832:         my $checked;
 5833:         if (ref($processing) eq 'HASH') {
 5834:             if ($type eq '') {   
 5835:                 if (!exists($processing->{'default'})) {
 5836:                     if ($option eq 'automatic') {
 5837:                         $checked = ' checked="checked"';
 5838:                     }
 5839:                 } else {
 5840:                     if ($processing->{'default'} eq $option) {
 5841:                         $checked = ' checked="checked"';
 5842:                     }
 5843:                 }
 5844:             } else {
 5845:                 if (!exists($processing->{$type})) {
 5846:                     if ($option eq 'automatic') {
 5847:                         $checked = ' checked="checked"';
 5848:                     }
 5849:                 } else {
 5850:                     if ($processing->{$type} eq $option) {
 5851:                         $checked = ' checked="checked"';
 5852:                     }
 5853:                 }
 5854:             }
 5855:         } elsif ($option eq 'automatic') {
 5856:             $checked = ' checked="checked"'; 
 5857:         }
 5858:         my $name = 'cancreate_emailprocess';
 5859:         if (($type ne '') && ($type ne 'default')) {
 5860:             $name .= '_'.$type;
 5861:         }
 5862:         $output .= '<span class="LC_nobreak"><label>'.
 5863:                    '<input type="radio" name="'.$name.'"'.
 5864:                    $checked.' value="'.$option.'" />'.
 5865:                    $choices{$option}.'</label></span>';
 5866:         if ($type eq '') {
 5867:             $output .= '&nbsp;';
 5868:         } else {
 5869:             $output .= '<br />';
 5870:         }
 5871:     }
 5872:     $$rowtotal ++;
 5873:     return $output;
 5874: }
 5875: 
 5876: sub captcha_choice {
 5877:     my ($context,$settings,$itemcount) = @_;
 5878:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 5879:         $vertext,$currver);
 5880:     my %lt = &captcha_phrases();
 5881:     $keyentry = 'hidden';
 5882:     if ($context eq 'cancreate') {
 5883:         $rowname = &mt('CAPTCHA validation');
 5884:     } elsif ($context eq 'login') {
 5885:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 5886:     }
 5887:     if (ref($settings) eq 'HASH') {
 5888:         if ($settings->{'captcha'}) {
 5889:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 5890:         } else {
 5891:             $checked{'original'} = ' checked="checked"';
 5892:         }
 5893:         if ($settings->{'captcha'} eq 'recaptcha') {
 5894:             $pubtext = $lt{'pub'};
 5895:             $privtext = $lt{'priv'};
 5896:             $keyentry = 'text';
 5897:             $vertext = $lt{'ver'};
 5898:             $currver = $settings->{'recaptchaversion'};
 5899:             if ($currver ne '2') {
 5900:                 $currver = 1;
 5901:             }
 5902:         }
 5903:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 5904:             $currpub = $settings->{'recaptchakeys'}{'public'};
 5905:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 5906:         }
 5907:     } else {
 5908:         $checked{'original'} = ' checked="checked"';
 5909:     }
 5910:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5911:     my $output = '<tr'.$css_class.'>'.
 5912:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 5913:                  '<table><tr><td>'."\n";
 5914:     foreach my $option ('original','recaptcha','notused') {
 5915:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 5916:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 5917:                    $lt{$option}.'</label></span>';
 5918:         unless ($option eq 'notused') {
 5919:             $output .= ('&nbsp;'x2)."\n";
 5920:         }
 5921:     }
 5922: #
 5923: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 5924: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 5925: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 5926: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 5927: #
 5928:     $output .= '</td></tr>'."\n".
 5929:                '<tr><td>'."\n".
 5930:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 5931:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 5932:                $currpub.'" size="40" /></span><br />'."\n".
 5933:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 5934:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 5935:                $currpriv.'" size="40" /></span><br />'.
 5936:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 5937:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 5938:                $currver.'" size="3" /></span><br />'.
 5939:                '</td></tr></table>'."\n".
 5940:                '</td></tr>';
 5941:     return $output;
 5942: }
 5943: 
 5944: sub user_formats_row {
 5945:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 5946:     my $output;
 5947:     my %text = (
 5948:                    'username' => 'new usernames',
 5949:                    'id'       => 'IDs',
 5950:                    'email'    => 'self-created accounts (e-mail)',
 5951:                );
 5952:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5953:     $output = '<tr '.$css_class.'>'.
 5954:               '<td><span class="LC_nobreak">';
 5955:     if ($type eq 'email') {
 5956:         $output .= &mt("Formats disallowed for $text{$type}: ");
 5957:     } else {
 5958:         $output .= &mt("Format rules to check for $text{$type}: ");
 5959:     }
 5960:     $output .= '</span></td>'.
 5961:                '<td class="LC_left_item" colspan="2"><table>';
 5962:     my $rem;
 5963:     if (ref($ruleorder) eq 'ARRAY') {
 5964:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 5965:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 5966:                 my $rem = $i%($numinrow);
 5967:                 if ($rem == 0) {
 5968:                     if ($i > 0) {
 5969:                         $output .= '</tr>';
 5970:                     }
 5971:                     $output .= '<tr>';
 5972:                 }
 5973:                 my $check = ' ';
 5974:                 if (ref($settings) eq 'HASH') {
 5975:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 5976:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 5977:                             $check = ' checked="checked" ';
 5978:                         }
 5979:                     }
 5980:                 }
 5981:                 $output .= '<td class="LC_left_item">'.
 5982:                            '<span class="LC_nobreak"><label>'.
 5983:                            '<input type="checkbox" name="'.$type.'_rule" '.
 5984:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 5985:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 5986:             }
 5987:         }
 5988:         $rem = @{$ruleorder}%($numinrow);
 5989:     }
 5990:     my $colsleft = $numinrow - $rem;
 5991:     if ($colsleft > 1 ) {
 5992:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5993:                    '&nbsp;</td>';
 5994:     } elsif ($colsleft == 1) {
 5995:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5996:     }
 5997:     $output .= '</tr></table></td></tr>';
 5998:     return $output;
 5999: }
 6000: 
 6001: sub usercreation_types {
 6002:     my %lt = &Apache::lonlocal::texthash (
 6003:                     author     => 'When adding a co-author',
 6004:                     course     => 'When adding a user to a course',
 6005:                     requestcrs => 'When requesting a course',
 6006:                     any        => 'Any',
 6007:                     official   => 'Institutional only ',
 6008:                     unofficial => 'Non-institutional only',
 6009:                     none       => 'None',
 6010:     );
 6011:     return %lt;
 6012: }
 6013: 
 6014: sub selfcreation_types {
 6015:     my %lt = &Apache::lonlocal::texthash (
 6016:                     selfcreate => 'User creates own account',
 6017:                     any        => 'Any',
 6018:                     official   => 'Institutional only ',
 6019:                     unofficial => 'Non-institutional only',
 6020:                     email      => 'E-mail address',
 6021:                     login      => 'Institutional Login',
 6022:                     sso        => 'SSO',
 6023:              );
 6024: }
 6025: 
 6026: sub authtype_names {
 6027:     my %lt = &Apache::lonlocal::texthash(
 6028:                       int    => 'Internal',
 6029:                       krb4   => 'Kerberos 4',
 6030:                       krb5   => 'Kerberos 5',
 6031:                       loc    => 'Local',
 6032:                   );
 6033:     return %lt;
 6034: }
 6035: 
 6036: sub context_names {
 6037:     my %context_title = &Apache::lonlocal::texthash(
 6038:        author => 'Creating users when an Author',
 6039:        course => 'Creating users when in a course',
 6040:        domain => 'Creating users when a Domain Coordinator',
 6041:     );
 6042:     return %context_title;
 6043: }
 6044: 
 6045: sub print_usermodification {
 6046:     my ($position,$dom,$settings,$rowtotal) = @_;
 6047:     my $numinrow = 4;
 6048:     my ($context,$datatable,$rowcount);
 6049:     if ($position eq 'top') {
 6050:         $rowcount = 0;
 6051:         $context = 'author'; 
 6052:         foreach my $role ('ca','aa') {
 6053:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6054:                                                    $numinrow,$rowcount);
 6055:             $$rowtotal ++;
 6056:             $rowcount ++;
 6057:         }
 6058:     } elsif ($position eq 'bottom') {
 6059:         $context = 'course';
 6060:         $rowcount = 0;
 6061:         foreach my $role ('st','ep','ta','in','cr') {
 6062:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6063:                                                    $numinrow,$rowcount);
 6064:             $$rowtotal ++;
 6065:             $rowcount ++;
 6066:         }
 6067:     }
 6068:     return $datatable;
 6069: }
 6070: 
 6071: sub print_defaults {
 6072:     my ($position,$dom,$settings,$rowtotal) = @_;
 6073:     my $rownum = 0;
 6074:     my ($datatable,$css_class);
 6075:     if ($position eq 'top') {
 6076:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 6077:                      'datelocale_def','portal_def');
 6078:         my %defaults;
 6079:         if (ref($settings) eq 'HASH') {
 6080:             %defaults = %{$settings};
 6081:         } else {
 6082:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6083:             foreach my $item (@items) {
 6084:                 $defaults{$item} = $domdefaults{$item};
 6085:             }
 6086:         }
 6087:         my $titles = &defaults_titles($dom);
 6088:         foreach my $item (@items) {
 6089:             if ($rownum%2) {
 6090:                 $css_class = '';
 6091:             } else {
 6092:                 $css_class = ' class="LC_odd_row" ';
 6093:             }
 6094:             $datatable .= '<tr'.$css_class.'>'.
 6095:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6096:                           '</span></td><td class="LC_right_item" colspan="3">';
 6097:             if ($item eq 'auth_def') {
 6098:                 my @authtypes = ('internal','krb4','krb5','localauth');
 6099:                 my %shortauth = (
 6100:                                  internal => 'int',
 6101:                                  krb4 => 'krb4',
 6102:                                  krb5 => 'krb5',
 6103:                                  localauth  => 'loc'
 6104:                                 );
 6105:                 my %authnames = &authtype_names();
 6106:                 foreach my $auth (@authtypes) {
 6107:                     my $checked = ' ';
 6108:                     if ($defaults{$item} eq $auth) {
 6109:                         $checked = ' checked="checked" ';
 6110:                     }
 6111:                     $datatable .= '<label><input type="radio" name="'.$item.
 6112:                                   '" value="'.$auth.'"'.$checked.'/>'.
 6113:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 6114:                 }
 6115:             } elsif ($item eq 'timezone_def') {
 6116:                 my $includeempty = 1;
 6117:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 6118:             } elsif ($item eq 'datelocale_def') {
 6119:                 my $includeempty = 1;
 6120:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 6121:             } elsif ($item eq 'lang_def') {
 6122:                 my $includeempty = 1;
 6123:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 6124:             } else {
 6125:                 my $size;
 6126:                 if ($item eq 'portal_def') {
 6127:                     $size = ' size="25"';
 6128:                 }
 6129:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6130:                               $defaults{$item}.'"'.$size.' />';
 6131:             }
 6132:             $datatable .= '</td></tr>';
 6133:             $rownum ++;
 6134:         }
 6135:     } else {
 6136:         my (%defaults);
 6137:         if (ref($settings) eq 'HASH') {
 6138:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 6139:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 6140:                 my $maxnum = @{$settings->{'inststatusorder'}};
 6141:                 for (my $i=0; $i<$maxnum; $i++) {
 6142:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 6143:                     my $item = $settings->{'inststatusorder'}->[$i];
 6144:                     my $title = $settings->{'inststatustypes'}->{$item};
 6145:                     my $guestok;
 6146:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 6147:                         $guestok = 1;
 6148:                     }
 6149:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 6150:                     $datatable .= '<tr'.$css_class.'>'.
 6151:                                   '<td><span class="LC_nobreak">'.
 6152:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 6153:                     for (my $k=0; $k<=$maxnum; $k++) {
 6154:                         my $vpos = $k+1;
 6155:                         my $selstr;
 6156:                         if ($k == $i) {
 6157:                             $selstr = ' selected="selected" ';
 6158:                         }
 6159:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6160:                     }
 6161:                     my ($checkedon,$checkedoff);
 6162:                     $checkedoff = ' checked="checked"';
 6163:                     if ($guestok) {
 6164:                         $checkedon = $checkedoff;
 6165:                         $checkedoff = ''; 
 6166:                     }
 6167:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 6168:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 6169:                                   &mt('delete').'</span></td>'.
 6170:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 6171:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 6172:                                   '</span></td>'.
 6173:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6174:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 6175:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 6176:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 6177:                                   &mt('No').'</label></span></td></tr>';
 6178:                 }
 6179:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 6180:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 6181:                 $datatable .= '<tr '.$css_class.'>'.
 6182:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 6183:                 for (my $k=0; $k<=$maxnum; $k++) {
 6184:                     my $vpos = $k+1;
 6185:                     my $selstr;
 6186:                     if ($k == $maxnum) {
 6187:                         $selstr = ' selected="selected" ';
 6188:                     }
 6189:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6190:                 }
 6191:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 6192:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 6193:                               '&nbsp;'.&mt('(new)').
 6194:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 6195:                               &mt('Name displayed:').
 6196:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 6197:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 6198:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 6199:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 6200:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 6201:                               &mt('No').'</label></span></td></tr>';
 6202:                               '</tr>'."\n";
 6203:                 $rownum ++;
 6204:             }
 6205:         }
 6206:     }
 6207:     $$rowtotal += $rownum;
 6208:     return $datatable;
 6209: }
 6210: 
 6211: sub get_languages_hash {
 6212:     my %langchoices;
 6213:     foreach my $id (&Apache::loncommon::languageids()) {
 6214:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 6215:         if ($code ne '') {
 6216:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 6217:         }
 6218:     }
 6219:     return %langchoices;
 6220: }
 6221: 
 6222: sub defaults_titles {
 6223:     my ($dom) = @_;
 6224:     my %titles = &Apache::lonlocal::texthash (
 6225:                    'auth_def'      => 'Default authentication type',
 6226:                    'auth_arg_def'  => 'Default authentication argument',
 6227:                    'lang_def'      => 'Default language',
 6228:                    'timezone_def'  => 'Default timezone',
 6229:                    'datelocale_def' => 'Default locale for dates',
 6230:                    'portal_def'     => 'Portal/Default URL',
 6231:                  );
 6232:     if ($dom) {
 6233:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 6234:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 6235:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 6236:         $protocol = 'http' if ($protocol ne 'https');
 6237:         if ($uint_dom) {
 6238:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 6239:                                          $uint_dom);
 6240:         }
 6241:     }
 6242:     return (\%titles);
 6243: }
 6244: 
 6245: sub print_scantronformat {
 6246:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 6247:     my $itemcount = 1;
 6248:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 6249:         %confhash);
 6250:     my $switchserver = &check_switchserver($dom,$confname);
 6251:     my %lt = &Apache::lonlocal::texthash (
 6252:                 default => 'Default bubblesheet format file error',
 6253:                 custom  => 'Custom bubblesheet format file error',
 6254:              );
 6255:     my %scantronfiles = (
 6256:         default => 'default.tab',
 6257:         custom => 'custom.tab',
 6258:     );
 6259:     foreach my $key (keys(%scantronfiles)) {
 6260:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 6261:                               .$scantronfiles{$key};
 6262:     }
 6263:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 6264:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 6265:         if (!$switchserver) {
 6266:             my $servadm = $r->dir_config('lonAdmEMail');
 6267:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 6268:             if ($configuserok eq 'ok') {
 6269:                 if ($author_ok eq 'ok') {
 6270:                     my %legacyfile = (
 6271:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 6272:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 6273:                     );
 6274:                     my %md5chk;
 6275:                     foreach my $type (keys(%legacyfile)) {
 6276:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 6277:                         chomp($md5chk{$type});
 6278:                     }
 6279:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 6280:                         foreach my $type (keys(%legacyfile)) {
 6281:                             ($scantronurls{$type},my $error) = 
 6282:                                 &legacy_scantronformat($r,$dom,$confname,
 6283:                                                  $type,$legacyfile{$type},
 6284:                                                  $scantronurls{$type},
 6285:                                                  $scantronfiles{$type});
 6286:                             if ($error ne '') {
 6287:                                 $error{$type} = $error;
 6288:                             }
 6289:                         }
 6290:                         if (keys(%error) == 0) {
 6291:                             $is_custom = 1;
 6292:                             $confhash{'scantron'}{'scantronformat'} = 
 6293:                                 $scantronurls{'custom'};
 6294:                             my $putresult = 
 6295:                                 &Apache::lonnet::put_dom('configuration',
 6296:                                                          \%confhash,$dom);
 6297:                             if ($putresult ne 'ok') {
 6298:                                 $error{'custom'} = 
 6299:                                     '<span class="LC_error">'.
 6300:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6301:                             }
 6302:                         }
 6303:                     } else {
 6304:                         ($scantronurls{'default'},my $error) =
 6305:                             &legacy_scantronformat($r,$dom,$confname,
 6306:                                           'default',$legacyfile{'default'},
 6307:                                           $scantronurls{'default'},
 6308:                                           $scantronfiles{'default'});
 6309:                         if ($error eq '') {
 6310:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 6311:                             my $putresult =
 6312:                                 &Apache::lonnet::put_dom('configuration',
 6313:                                                          \%confhash,$dom);
 6314:                             if ($putresult ne 'ok') {
 6315:                                 $error{'default'} =
 6316:                                     '<span class="LC_error">'.
 6317:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6318:                             }
 6319:                         } else {
 6320:                             $error{'default'} = $error;
 6321:                         }
 6322:                     }
 6323:                 }
 6324:             }
 6325:         } else {
 6326:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 6327:         }
 6328:     }
 6329:     if (ref($settings) eq 'HASH') {
 6330:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 6331:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 6332:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 6333:                 $scantronurl = '';
 6334:             } else {
 6335:                 $scantronurl = $settings->{'scantronformat'};
 6336:             }
 6337:             $is_custom = 1;
 6338:         } else {
 6339:             $scantronurl = $scantronurls{'default'};
 6340:         }
 6341:     } else {
 6342:         if ($is_custom) {
 6343:             $scantronurl = $scantronurls{'custom'};
 6344:         } else {
 6345:             $scantronurl = $scantronurls{'default'};
 6346:         }
 6347:     }
 6348:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6349:     $datatable .= '<tr'.$css_class.'>';
 6350:     if (!$is_custom) {
 6351:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 6352:                       '<span class="LC_nobreak">';
 6353:         if ($scantronurl) {
 6354:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 6355:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 6356:         } else {
 6357:             $datatable = &mt('File unavailable for display');
 6358:         }
 6359:         $datatable .= '</span></td>';
 6360:         if (keys(%error) == 0) { 
 6361:             $datatable .= '<td valign="bottom">';
 6362:             if (!$switchserver) {
 6363:                 $datatable .= &mt('Upload:').'<br />';
 6364:             }
 6365:         } else {
 6366:             my $errorstr;
 6367:             foreach my $key (sort(keys(%error))) {
 6368:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6369:             }
 6370:             $datatable .= '<td>'.$errorstr;
 6371:         }
 6372:     } else {
 6373:         if (keys(%error) > 0) {
 6374:             my $errorstr;
 6375:             foreach my $key (sort(keys(%error))) {
 6376:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6377:             } 
 6378:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 6379:         } elsif ($scantronurl) {
 6380:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 6381:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 6382:             $datatable .= '<td><span class="LC_nobreak">'.
 6383:                           $link.
 6384:                           '<label><input type="checkbox" name="scantronformat_del"'.
 6385:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 6386:                           '<td><span class="LC_nobreak">&nbsp;'.
 6387:                           &mt('Replace:').'</span><br />';
 6388:         }
 6389:     }
 6390:     if (keys(%error) == 0) {
 6391:         if ($switchserver) {
 6392:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 6393:         } else {
 6394:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 6395:                          '<input type="file" name="scantronformat" /></span>';
 6396:         }
 6397:     }
 6398:     $datatable .= '</td></tr>';
 6399:     $$rowtotal ++;
 6400:     return $datatable;
 6401: }
 6402: 
 6403: sub legacy_scantronformat {
 6404:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 6405:     my ($url,$error);
 6406:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 6407:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 6408:         (my $result,$url) =
 6409:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 6410:                          '','',$newfile);
 6411:         if ($result ne 'ok') {
 6412:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 6413:         }
 6414:     }
 6415:     return ($url,$error);
 6416: }
 6417: 
 6418: sub print_coursecategories {
 6419:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 6420:     my $datatable;
 6421:     if ($position eq 'top') {
 6422:         my (%checked);
 6423:         my @catitems = ('unauth','auth');
 6424:         my @cattypes = ('std','domonly','codesrch','none');
 6425:         $checked{'unauth'} = 'std';
 6426:         $checked{'auth'} = 'std';
 6427:         if (ref($settings) eq 'HASH') {
 6428:             foreach my $type (@cattypes) {
 6429:                 if ($type eq $settings->{'unauth'}) {
 6430:                     $checked{'unauth'} = $type;
 6431:                 }
 6432:                 if ($type eq $settings->{'auth'}) {
 6433:                     $checked{'auth'} = $type;
 6434:                 }
 6435:             }
 6436:         }
 6437:         my %lt = &Apache::lonlocal::texthash (
 6438:                                                unauth   => 'Catalog type for unauthenticated users',
 6439:                                                auth     => 'Catalog type for authenticated users',
 6440:                                                none     => 'No catalog',
 6441:                                                std      => 'Standard catalog',
 6442:                                                domonly  => 'Domain-only catalog',
 6443:                                                codesrch => "Code search form",
 6444:                                              );
 6445:        my $itemcount = 0;
 6446:        foreach my $item (@catitems) {
 6447:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 6448:            $datatable .= '<tr '.$css_class.'>'.
 6449:                          '<td>'.$lt{$item}.'</td>'.
 6450:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 6451:            foreach my $type (@cattypes) {
 6452:                my $ischecked;
 6453:                if ($checked{$item} eq $type) {
 6454:                    $ischecked=' checked="checked"';
 6455:                }
 6456:                $datatable .= '<label>'.
 6457:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 6458:                              ' />'.$lt{$type}.'</label>&nbsp;';
 6459:            }
 6460:            $datatable .= '</td></tr>';
 6461:            $itemcount ++;
 6462:         }
 6463:         $$rowtotal += $itemcount;
 6464:     } elsif ($position eq 'middle') {
 6465:         my $toggle_cats_crs = ' ';
 6466:         my $toggle_cats_dom = ' checked="checked" ';
 6467:         my $can_cat_crs = ' ';
 6468:         my $can_cat_dom = ' checked="checked" ';
 6469:         my $toggle_catscomm_comm = ' ';
 6470:         my $toggle_catscomm_dom = ' checked="checked" ';
 6471:         my $can_catcomm_comm = ' ';
 6472:         my $can_catcomm_dom = ' checked="checked" ';
 6473:         my $toggle_catsplace_place = ' ';
 6474:         my $toggle_catsplace_dom = ' checked="checked" ';
 6475:         my $can_catplace_place = ' ';
 6476:         my $can_catplace_dom = ' checked="checked" ';
 6477: 
 6478:         if (ref($settings) eq 'HASH') {
 6479:             if ($settings->{'togglecats'} eq 'crs') {
 6480:                 $toggle_cats_crs = $toggle_cats_dom;
 6481:                 $toggle_cats_dom = ' ';
 6482:             }
 6483:             if ($settings->{'categorize'} eq 'crs') {
 6484:                 $can_cat_crs = $can_cat_dom;
 6485:                 $can_cat_dom = ' ';
 6486:             }
 6487:             if ($settings->{'togglecatscomm'} eq 'comm') {
 6488:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 6489:                 $toggle_catscomm_dom = ' ';
 6490:             }
 6491:             if ($settings->{'categorizecomm'} eq 'comm') {
 6492:                 $can_catcomm_comm = $can_catcomm_dom;
 6493:                 $can_catcomm_dom = ' ';
 6494:             }
 6495:             if ($settings->{'togglecatsplace'} eq 'place') {
 6496:                 $toggle_catsplace_place = $toggle_catsplace_dom;
 6497:                 $toggle_catsplace_dom = ' ';
 6498:             }
 6499:             if ($settings->{'categorizeplace'} eq 'place') {
 6500:                 $can_catplace_place = $can_catplace_dom;
 6501:                 $can_catplace_dom = ' ';
 6502:             }
 6503:         }
 6504:         my %title = &Apache::lonlocal::texthash (
 6505:                      togglecats      => 'Show/Hide a course in catalog',
 6506:                      togglecatscomm  => 'Show/Hide a community in catalog',
 6507:                      togglecatsplace => 'Show/Hide a placement test in catalog',
 6508:                      categorize      => 'Assign a category to a course',
 6509:                      categorizecomm  => 'Assign a category to a community',
 6510:                      categorizeplace => 'Assign a category to a placement test',
 6511:                     );
 6512:         my %level = &Apache::lonlocal::texthash (
 6513:                      dom   => 'Set in Domain',
 6514:                      crs   => 'Set in Course',
 6515:                      comm  => 'Set in Community',
 6516:                      place => 'Set in Placement Test',
 6517:                     );
 6518:         $datatable = '<tr class="LC_odd_row">'.
 6519:                   '<td>'.$title{'togglecats'}.'</td>'.
 6520:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6521:                   '<input type="radio" name="togglecats"'.
 6522:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6523:                   '<label><input type="radio" name="togglecats"'.
 6524:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6525:                   '</tr><tr>'.
 6526:                   '<td>'.$title{'categorize'}.'</td>'.
 6527:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6528:                   '<label><input type="radio" name="categorize"'.
 6529:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6530:                   '<label><input type="radio" name="categorize"'.
 6531:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6532:                   '</tr><tr class="LC_odd_row">'.
 6533:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 6534:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6535:                   '<input type="radio" name="togglecatscomm"'.
 6536:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6537:                   '<label><input type="radio" name="togglecatscomm"'.
 6538:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6539:                   '</tr><tr>'.
 6540:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 6541:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6542:                   '<label><input type="radio" name="categorizecomm"'.
 6543:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6544:                   '<label><input type="radio" name="categorizecomm"'.
 6545:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6546:                   '</tr><tr>'.
 6547:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
 6548:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6549:                   '<input type="radio" name="togglecatsplace"'.
 6550:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6551:                   '<label><input type="radio" name="togglecatscomm"'.
 6552:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
 6553:                   '</tr><tr>'.
 6554:                   '<td>'.$title{'categorizeplace'}.'</td>'.
 6555:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6556:                   '<label><input type="radio" name="categorizeplace"'.
 6557:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6558:                   '<label><input type="radio" name="categorizeplace"'.
 6559:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
 6560:                   '</tr>';
 6561:         $$rowtotal += 6;
 6562:     } else {
 6563:         my $css_class;
 6564:         my $itemcount = 1;
 6565:         my $cathash; 
 6566:         if (ref($settings) eq 'HASH') {
 6567:             $cathash = $settings->{'cats'};
 6568:         }
 6569:         if (ref($cathash) eq 'HASH') {
 6570:             my (@cats,@trails,%allitems,%idx,@jsarray);
 6571:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 6572:                                                    \%allitems,\%idx,\@jsarray);
 6573:             my $maxdepth = scalar(@cats);
 6574:             my $colattrib = '';
 6575:             if ($maxdepth > 2) {
 6576:                 $colattrib = ' colspan="2" ';
 6577:             }
 6578:             my @path;
 6579:             if (@cats > 0) {
 6580:                 if (ref($cats[0]) eq 'ARRAY') {
 6581:                     my $numtop = @{$cats[0]};
 6582:                     my $maxnum = $numtop;
 6583:                     my %default_names = (
 6584:                           instcode    => &mt('Official courses'),
 6585:                           communities => &mt('Communities'),
 6586:                           placement   => &mt('Placement Tests'),
 6587:                     );
 6588: 
 6589:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 6590:                         ($cathash->{'instcode::0'} eq '') ||
 6591:                         (!grep(/^communities$/,@{$cats[0]})) || 
 6592:                         ($cathash->{'communities::0'} eq '') ||
 6593:                         (!grep(/^placement$/,@{$cats[0]})) ||
 6594:                         ($cathash->{'placement::0'} eq '')) {
 6595:                         $maxnum ++;
 6596:                     }
 6597:                     my $lastidx;
 6598:                     for (my $i=0; $i<$numtop; $i++) {
 6599:                         my $parent = $cats[0][$i];
 6600:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6601:                         my $item = &escape($parent).'::0';
 6602:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 6603:                         $lastidx = $idx{$item};
 6604:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6605:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 6606:                         for (my $k=0; $k<=$maxnum; $k++) {
 6607:                             my $vpos = $k+1;
 6608:                             my $selstr;
 6609:                             if ($k == $i) {
 6610:                                 $selstr = ' selected="selected" ';
 6611:                             }
 6612:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6613:                         }
 6614:                         $datatable .= '</select></span></td><td>';
 6615:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
 6616:                             $datatable .=  '<span class="LC_nobreak">'
 6617:                                            .$default_names{$parent}.'</span>';
 6618:                             if ($parent eq 'instcode') {
 6619:                                 $datatable .= '<br /><span class="LC_nobreak">('
 6620:                                               .&mt('with institutional codes')
 6621:                                               .')</span></td><td'.$colattrib.'>';
 6622:                             } else {
 6623:                                 $datatable .= '<table><tr><td>';
 6624:                             }
 6625:                             $datatable .= '<span class="LC_nobreak">'
 6626:                                           .'<label><input type="radio" name="'
 6627:                                           .$parent.'" value="1" checked="checked" />'
 6628:                                           .&mt('Display').'</label>';
 6629:                             if ($parent eq 'instcode') {
 6630:                                 $datatable .= '&nbsp;';
 6631:                             } else {
 6632:                                 $datatable .= '</span></td></tr><tr><td>'
 6633:                                               .'<span class="LC_nobreak">';
 6634:                             }
 6635:                             $datatable .= '<label><input type="radio" name="'
 6636:                                           .$parent.'" value="0" />'
 6637:                                           .&mt('Do not display').'</label></span>';
 6638:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
 6639:                                 $datatable .= '</td></tr></table>';
 6640:                             }
 6641:                             $datatable .= '</td>';
 6642:                         } else {
 6643:                             $datatable .= $parent
 6644:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 6645:                                           .'<input type="checkbox" name="deletecategory" '
 6646:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 6647:                         }
 6648:                         my $depth = 1;
 6649:                         push(@path,$parent);
 6650:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 6651:                         pop(@path);
 6652:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 6653:                         $itemcount ++;
 6654:                     }
 6655:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6656:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 6657:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 6658:                     for (my $k=0; $k<=$maxnum; $k++) {
 6659:                         my $vpos = $k+1;
 6660:                         my $selstr;
 6661:                         if ($k == $numtop) {
 6662:                             $selstr = ' selected="selected" ';
 6663:                         }
 6664:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6665:                     }
 6666:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 6667:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 6668:                                   .'</tr>'."\n";
 6669:                     $itemcount ++;
 6670:                     foreach my $default ('instcode','communities','placement') {
 6671:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 6672:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6673:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 6674:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 6675:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 6676:                             for (my $k=0; $k<=$maxnum; $k++) {
 6677:                                 my $vpos = $k+1;
 6678:                                 my $selstr;
 6679:                                 if ($k == $maxnum) {
 6680:                                     $selstr = ' selected="selected" ';
 6681:                                 }
 6682:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6683:                             }
 6684:                             $datatable .= '</select></span></td>'.
 6685:                                           '<td><span class="LC_nobreak">'.
 6686:                                           $default_names{$default}.'</span>';
 6687:                             if ($default eq 'instcode') {
 6688:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 6689:                                               .&mt('with institutional codes').')</span>';
 6690:                             }
 6691:                             $datatable .= '</td>'
 6692:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 6693:                                           .&mt('Display').'</label>&nbsp;'
 6694:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 6695:                                           .&mt('Do not display').'</label></span></td></tr>';
 6696:                         }
 6697:                     }
 6698:                 }
 6699:             } else {
 6700:                 $datatable .= &initialize_categories($itemcount);
 6701:             }
 6702:         } else {
 6703:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 6704:                           .&initialize_categories($itemcount);
 6705:         }
 6706:         $$rowtotal += $itemcount;
 6707:     }
 6708:     return $datatable;
 6709: }
 6710: 
 6711: sub print_serverstatuses {
 6712:     my ($dom,$settings,$rowtotal) = @_;
 6713:     my $datatable;
 6714:     my @pages = &serverstatus_pages();
 6715:     my (%namedaccess,%machineaccess);
 6716:     foreach my $type (@pages) {
 6717:         $namedaccess{$type} = '';
 6718:         $machineaccess{$type}= '';
 6719:     }
 6720:     if (ref($settings) eq 'HASH') {
 6721:         foreach my $type (@pages) {
 6722:             if (exists($settings->{$type})) {
 6723:                 if (ref($settings->{$type}) eq 'HASH') {
 6724:                     foreach my $key (keys(%{$settings->{$type}})) {
 6725:                         if ($key eq 'namedusers') {
 6726:                             $namedaccess{$type} = $settings->{$type}->{$key};
 6727:                         } elsif ($key eq 'machines') {
 6728:                             $machineaccess{$type} = $settings->{$type}->{$key};
 6729:                         }
 6730:                     }
 6731:                 }
 6732:             }
 6733:         }
 6734:     }
 6735:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 6736:     my $rownum = 0;
 6737:     my $css_class;
 6738:     foreach my $type (@pages) {
 6739:         $rownum ++;
 6740:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 6741:         $datatable .= '<tr'.$css_class.'>'.
 6742:                       '<td><span class="LC_nobreak">'.
 6743:                       $titles->{$type}.'</span></td>'.
 6744:                       '<td class="LC_left_item">'.
 6745:                       '<input type="text" name="'.$type.'_namedusers" '.
 6746:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 6747:                       '<td class="LC_right_item">'.
 6748:                       '<span class="LC_nobreak">'.
 6749:                       '<input type="text" name="'.$type.'_machines" '.
 6750:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 6751:                       '</td></tr>'."\n";
 6752:     }
 6753:     $$rowtotal += $rownum;
 6754:     return $datatable;
 6755: }
 6756: 
 6757: sub serverstatus_pages {
 6758:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 6759:             'checksums','clusterstatus','certstatus','metadata_keywords',
 6760:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
 6761:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
 6762: }
 6763: 
 6764: sub defaults_javascript {
 6765:     my ($settings) = @_;
 6766:     return unless (ref($settings) eq 'HASH'); 
 6767:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 6768:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 6769:         if ($maxnum eq '') {
 6770:             $maxnum = 0;
 6771:         }
 6772:         $maxnum ++;
 6773:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 6774:         return <<"ENDSCRIPT";
 6775: <script type="text/javascript">
 6776: // <![CDATA[
 6777: function reorderTypes(form,caller) {
 6778:     var changedVal;
 6779: $jstext 
 6780:     var newpos = 'addinststatus_pos';
 6781:     var current = new Array;
 6782:     var maxh = $maxnum;
 6783:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6784:     var oldVal;
 6785:     if (caller == newpos) {
 6786:         changedVal = newitemVal;
 6787:     } else {
 6788:         var curritem = 'inststatus_pos_'+caller;
 6789:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 6790:         current[newitemVal] = newpos;
 6791:     }
 6792:     for (var i=0; i<inststatuses.length; i++) {
 6793:         if (inststatuses[i] != caller) {
 6794:             var elementName = 'inststatus_pos_'+inststatuses[i];
 6795:             if (form.elements[elementName]) {
 6796:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 6797:                 current[currVal] = elementName;
 6798:             }
 6799:         }
 6800:     }
 6801:     for (var j=0; j<maxh; j++) {
 6802:         if (current[j] == undefined) {
 6803:             oldVal = j;
 6804:         }
 6805:     }
 6806:     if (oldVal < changedVal) {
 6807:         for (var k=oldVal+1; k<=changedVal ; k++) {
 6808:            var elementName = current[k];
 6809:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 6810:         }
 6811:     } else {
 6812:         for (var k=changedVal; k<oldVal; k++) {
 6813:             var elementName = current[k];
 6814:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 6815:         }
 6816:     }
 6817:     return;
 6818: }
 6819: 
 6820: // ]]>
 6821: </script>
 6822: 
 6823: ENDSCRIPT
 6824:     }
 6825: }
 6826: 
 6827: sub coursecategories_javascript {
 6828:     my ($settings) = @_;
 6829:     my ($output,$jstext,$cathash);
 6830:     if (ref($settings) eq 'HASH') {
 6831:         $cathash = $settings->{'cats'};
 6832:     }
 6833:     if (ref($cathash) eq 'HASH') {
 6834:         my (@cats,@jsarray,%idx);
 6835:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 6836:         if (@jsarray > 0) {
 6837:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 6838:             for (my $i=0; $i<@jsarray; $i++) {
 6839:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 6840:                     my $catstr = join('","',@{$jsarray[$i]});
 6841:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 6842:                 }
 6843:             }
 6844:         }
 6845:     } else {
 6846:         $jstext  = '    var categories = Array(1);'."\n".
 6847:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 6848:     }
 6849:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 6850:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 6851:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
 6852:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 6853:     &js_escape(\$instcode_reserved);
 6854:     &js_escape(\$communities_reserved);
 6855:     &js_escape(\$placement_reserved);
 6856:     &js_escape(\$choose_again);
 6857:     $output = <<"ENDSCRIPT";
 6858: <script type="text/javascript">
 6859: // <![CDATA[
 6860: function reorderCats(form,parent,item,idx) {
 6861:     var changedVal;
 6862: $jstext
 6863:     var newpos = 'addcategory_pos';
 6864:     if (parent == '') {
 6865:         var has_instcode = 0;
 6866:         var maxtop = categories[idx].length;
 6867:         for (var j=0; j<maxtop; j++) {
 6868:             if (categories[idx][j] == 'instcode::0') {
 6869:                 has_instcode == 1;
 6870:             }
 6871:         }
 6872:         if (has_instcode == 0) {
 6873:             categories[idx][maxtop] = 'instcode_pos';
 6874:         }
 6875:     } else {
 6876:         newpos += '_'+parent;
 6877:     }
 6878:     var maxh = 1 + categories[idx].length;
 6879:     var current = new Array;
 6880:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6881:     if (item == newpos) {
 6882:         changedVal = newitemVal;
 6883:     } else {
 6884:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 6885:         current[newitemVal] = newpos;
 6886:     }
 6887:     for (var i=0; i<categories[idx].length; i++) {
 6888:         var elementName = categories[idx][i];
 6889:         if (elementName != item) {
 6890:             if (form.elements[elementName]) {
 6891:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 6892:                 current[currVal] = elementName;
 6893:             }
 6894:         }
 6895:     }
 6896:     var oldVal;
 6897:     for (var j=0; j<maxh; j++) {
 6898:         if (current[j] == undefined) {
 6899:             oldVal = j;
 6900:         }
 6901:     }
 6902:     if (oldVal < changedVal) {
 6903:         for (var k=oldVal+1; k<=changedVal ; k++) {
 6904:            var elementName = current[k];
 6905:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 6906:         }
 6907:     } else {
 6908:         for (var k=changedVal; k<oldVal; k++) {
 6909:             var elementName = current[k];
 6910:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 6911:         }
 6912:     }
 6913:     return;
 6914: }
 6915: 
 6916: function categoryCheck(form) {
 6917:     if (form.elements['addcategory_name'].value == 'instcode') {
 6918:         alert('$instcode_reserved\\n$choose_again');
 6919:         return false;
 6920:     }
 6921:     if (form.elements['addcategory_name'].value == 'communities') {
 6922:         alert('$communities_reserved\\n$choose_again');
 6923:         return false;
 6924:     }
 6925:     if (form.elements['addcategory_name'].value == 'placement') {
 6926:         alert('$placement_reserved\\n$choose_again');
 6927:         return false;
 6928:     }
 6929:     return true;
 6930: }
 6931: 
 6932: // ]]>
 6933: </script>
 6934: 
 6935: ENDSCRIPT
 6936:     return $output;
 6937: }
 6938: 
 6939: sub initialize_categories {
 6940:     my ($itemcount) = @_;
 6941:     my ($datatable,$css_class,$chgstr);
 6942:     my %default_names = (
 6943:                       instcode    => 'Official courses (with institutional codes)',
 6944:                       communities => 'Communities',
 6945:                       placement   => 'Placement Tests',
 6946:                         );
 6947:     my $select0 = ' selected="selected"';
 6948:     my $select1 = '';
 6949:     foreach my $default ('instcode','communities','placement') {
 6950:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6951:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 6952:         if (($default eq 'communities') || ($default eq 'placement')) {
 6953:             $select1 = $select0;
 6954:             $select0 = '';
 6955:         }
 6956:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6957:                      .'<select name="'.$default.'_pos">'
 6958:                      .'<option value="0"'.$select0.'>1</option>'
 6959:                      .'<option value="1"'.$select1.'>2</option>'
 6960:                      .'<option value="2">3</option></select>&nbsp;'
 6961:                      .$default_names{$default}
 6962:                      .'</span></td><td><span class="LC_nobreak">'
 6963:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 6964:                      .&mt('Display').'</label>&nbsp;<label>'
 6965:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 6966:                  .'</label></span></td></tr>';
 6967:         $itemcount ++;
 6968:     }
 6969:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6970:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 6971:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6972:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 6973:                   .'<option value="0">1</option>'
 6974:                   .'<option value="1">2</option>'
 6975:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 6976:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 6977:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 6978:     return $datatable;
 6979: }
 6980: 
 6981: sub build_category_rows {
 6982:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 6983:     my ($text,$name,$item,$chgstr);
 6984:     if (ref($cats) eq 'ARRAY') {
 6985:         my $maxdepth = scalar(@{$cats});
 6986:         if (ref($cats->[$depth]) eq 'HASH') {
 6987:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 6988:                 my $numchildren = @{$cats->[$depth]{$parent}};
 6989:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6990:                 $text .= '<td><table class="LC_data_table">';
 6991:                 my ($idxnum,$parent_name,$parent_item);
 6992:                 my $higher = $depth - 1;
 6993:                 if ($higher == 0) {
 6994:                     $parent_name = &escape($parent).'::'.$higher;
 6995:                 } else {
 6996:                     if (ref($path) eq 'ARRAY') {
 6997:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 6998:                     }
 6999:                 }
 7000:                 $parent_item = 'addcategory_pos_'.$parent_name;
 7001:                 for (my $j=0; $j<=$numchildren; $j++) {
 7002:                     if ($j < $numchildren) {
 7003:                         $name = $cats->[$depth]{$parent}[$j];
 7004:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 7005:                         $idxnum = $idx->{$item};
 7006:                     } else {
 7007:                         $name = $parent_name;
 7008:                         $item = $parent_item;
 7009:                     }
 7010:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 7011:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 7012:                     for (my $i=0; $i<=$numchildren; $i++) {
 7013:                         my $vpos = $i+1;
 7014:                         my $selstr;
 7015:                         if ($j == $i) {
 7016:                             $selstr = ' selected="selected" ';
 7017:                         }
 7018:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 7019:                     }
 7020:                     $text .= '</select>&nbsp;';
 7021:                     if ($j < $numchildren) {
 7022:                         my $deeper = $depth+1;
 7023:                         $text .= $name.'&nbsp;'
 7024:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 7025:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 7026:                         if(ref($path) eq 'ARRAY') {
 7027:                             push(@{$path},$name);
 7028:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 7029:                             pop(@{$path});
 7030:                         }
 7031:                     } else {
 7032:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 7033:                         if ($j == $numchildren) {
 7034:                             $text .= $name;
 7035:                         } else {
 7036:                             $text .= $item;
 7037:                         }
 7038:                         $text .= '" value="" />';
 7039:                     }
 7040:                     $text .= '</td></tr>';
 7041:                 }
 7042:                 $text .= '</table></td>';
 7043:             } else {
 7044:                 my $higher = $depth-1;
 7045:                 if ($higher == 0) {
 7046:                     $name = &escape($parent).'::'.$higher;
 7047:                 } else {
 7048:                     if (ref($path) eq 'ARRAY') {
 7049:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7050:                     }
 7051:                 }
 7052:                 my $colspan;
 7053:                 if ($parent ne 'instcode') {
 7054:                     $colspan = $maxdepth - $depth - 1;
 7055:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 7056:                 }
 7057:             }
 7058:         }
 7059:     }
 7060:     return $text;
 7061: }
 7062: 
 7063: sub modifiable_userdata_row {
 7064:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 7065:     my ($role,$rolename,$statustype);
 7066:     $role = $item;
 7067:     if ($context eq 'cancreate') {
 7068:         if ($item =~ /^emailusername_(.+)$/) {
 7069:             $statustype = $1;
 7070:             $role = 'emailusername';
 7071:             if (ref($usertypes) eq 'HASH') {
 7072:                 if ($usertypes->{$statustype}) {
 7073:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 7074:                 } else {
 7075:                     $rolename = &mt('Data provided by user');
 7076:                 }
 7077:             }
 7078:         }
 7079:     } elsif ($context eq 'selfcreate') {
 7080:         if (ref($usertypes) eq 'HASH') {
 7081:             $rolename = $usertypes->{$role};
 7082:         } else {
 7083:             $rolename = $role;
 7084:         }
 7085:     } else {
 7086:         if ($role eq 'cr') {
 7087:             $rolename = &mt('Custom role');
 7088:         } else {
 7089:             $rolename = &Apache::lonnet::plaintext($role);
 7090:         }
 7091:     }
 7092:     my (@fields,%fieldtitles);
 7093:     if (ref($fieldsref) eq 'ARRAY') {
 7094:         @fields = @{$fieldsref};
 7095:     } else {
 7096:         @fields = ('lastname','firstname','middlename','generation',
 7097:                    'permanentemail','id');
 7098:     }
 7099:     if ((ref($titlesref) eq 'HASH')) {
 7100:         %fieldtitles = %{$titlesref};
 7101:     } else {
 7102:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7103:     }
 7104:     my $output;
 7105:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 7106:     $output = '<tr '.$css_class.'>'.
 7107:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 7108:               '<td class="LC_left_item" colspan="2"><table>';
 7109:     my $rem;
 7110:     my %checks;
 7111:     if (ref($settings) eq 'HASH') {
 7112:         if (ref($settings->{$context}) eq 'HASH') {
 7113:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 7114:                 my $hashref = $settings->{$context}->{$role};
 7115:                 if ($role eq 'emailusername') {
 7116:                     if ($statustype) {
 7117:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 7118:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 7119:                             if (ref($hashref) eq 'HASH') { 
 7120:                                 foreach my $field (@fields) {
 7121:                                     if ($hashref->{$field}) {
 7122:                                         $checks{$field} = $hashref->{$field};
 7123:                                     }
 7124:                                 }
 7125:                             }
 7126:                         }
 7127:                     }
 7128:                 } else {
 7129:                     if (ref($hashref) eq 'HASH') {
 7130:                         foreach my $field (@fields) {
 7131:                             if ($hashref->{$field}) {
 7132:                                 $checks{$field} = ' checked="checked" ';
 7133:                             }
 7134:                         }
 7135:                     }
 7136:                 }
 7137:             }
 7138:         }
 7139:     }
 7140:      
 7141:     for (my $i=0; $i<@fields; $i++) {
 7142:         my $rem = $i%($numinrow);
 7143:         if ($rem == 0) {
 7144:             if ($i > 0) {
 7145:                 $output .= '</tr>';
 7146:             }
 7147:             $output .= '<tr>';
 7148:         }
 7149:         my $check = ' ';
 7150:         unless ($role eq 'emailusername') {
 7151:             if (exists($checks{$fields[$i]})) {
 7152:                 $check = $checks{$fields[$i]}
 7153:             } else {
 7154:                 if ($role eq 'st') {
 7155:                     if (ref($settings) ne 'HASH') {
 7156:                         $check = ' checked="checked" '; 
 7157:                     }
 7158:                 }
 7159:             }
 7160:         }
 7161:         $output .= '<td class="LC_left_item">'.
 7162:                    '<span class="LC_nobreak">';
 7163:         if ($role eq 'emailusername') {
 7164:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 7165:                 $checks{$fields[$i]} = 'omit';
 7166:             }
 7167:             foreach my $option ('required','optional','omit') {
 7168:                 my $checked='';
 7169:                 if ($checks{$fields[$i]} eq $option) {
 7170:                     $checked='checked="checked" ';
 7171:                 }
 7172:                 $output .= '<label>'.
 7173:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 7174:                            &mt($option).'</label>'.('&nbsp;' x2);
 7175:             }
 7176:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 7177:         } else {
 7178:             $output .= '<label>'.
 7179:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 7180:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 7181:                        '</label>';
 7182:         }
 7183:         $output .= '</span></td>';
 7184:         $rem = @fields%($numinrow);
 7185:     }
 7186:     my $colsleft = $numinrow - $rem;
 7187:     if ($colsleft > 1 ) {
 7188:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7189:                    '&nbsp;</td>';
 7190:     } elsif ($colsleft == 1) {
 7191:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7192:     }
 7193:     $output .= '</tr></table></td></tr>';
 7194:     return $output;
 7195: }
 7196: 
 7197: sub insttypes_row {
 7198:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 7199:     my %lt = &Apache::lonlocal::texthash (
 7200:                       cansearch => 'Users allowed to search',
 7201:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 7202:                       lockablenames => 'User preference to lock name',
 7203:              );
 7204:     my $showdom;
 7205:     if ($context eq 'cansearch') {
 7206:         $showdom = ' ('.$dom.')';
 7207:     }
 7208:     my $class = 'LC_left_item';
 7209:     if ($context eq 'statustocreate') {
 7210:         $class = 'LC_right_item';
 7211:     }
 7212:     my $css_class = ' class="LC_odd_row"';
 7213:     if ($rownum ne '') { 
 7214:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 7215:     }
 7216:     my $output = '<tr'.$css_class.'>'.
 7217:                  '<td>'.$lt{$context}.$showdom.
 7218:                  '</td><td class="'.$class.'" colspan="2"><table>';
 7219:     my $rem;
 7220:     if (ref($types) eq 'ARRAY') {
 7221:         for (my $i=0; $i<@{$types}; $i++) {
 7222:             if (defined($usertypes->{$types->[$i]})) {
 7223:                 my $rem = $i%($numinrow);
 7224:                 if ($rem == 0) {
 7225:                     if ($i > 0) {
 7226:                         $output .= '</tr>';
 7227:                     }
 7228:                     $output .= '<tr>';
 7229:                 }
 7230:                 my $check = ' ';
 7231:                 if (ref($settings) eq 'HASH') {
 7232:                     if (ref($settings->{$context}) eq 'ARRAY') {
 7233:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 7234:                             $check = ' checked="checked" ';
 7235:                         }
 7236:                     } elsif ($context eq 'statustocreate') {
 7237:                         $check = ' checked="checked" ';
 7238:                     }
 7239:                 }
 7240:                 $output .= '<td class="LC_left_item">'.
 7241:                            '<span class="LC_nobreak"><label>'.
 7242:                            '<input type="checkbox" name="'.$context.'" '.
 7243:                            'value="'.$types->[$i].'"'.$check.'/>'.
 7244:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 7245:             }
 7246:         }
 7247:         $rem = @{$types}%($numinrow);
 7248:     }
 7249:     my $colsleft = $numinrow - $rem;
 7250:     if (($rem == 0) && (@{$types} > 0)) {
 7251:         $output .= '<tr>';
 7252:     }
 7253:     if ($colsleft > 1) {
 7254:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 7255:     } else {
 7256:         $output .= '<td class="LC_left_item">';
 7257:     }
 7258:     my $defcheck = ' ';
 7259:     if (ref($settings) eq 'HASH') {  
 7260:         if (ref($settings->{$context}) eq 'ARRAY') {
 7261:             if (grep(/^default$/,@{$settings->{$context}})) {
 7262:                 $defcheck = ' checked="checked" ';
 7263:             }
 7264:         } elsif ($context eq 'statustocreate') {
 7265:             $defcheck = ' checked="checked" ';
 7266:         }
 7267:     }
 7268:     $output .= '<span class="LC_nobreak"><label>'.
 7269:                '<input type="checkbox" name="'.$context.'" '.
 7270:                'value="default"'.$defcheck.'/>'.
 7271:                $othertitle.'</label></span></td>'.
 7272:                '</tr></table></td></tr>';
 7273:     return $output;
 7274: }
 7275: 
 7276: sub sorted_searchtitles {
 7277:     my %searchtitles = &Apache::lonlocal::texthash(
 7278:                          'uname' => 'username',
 7279:                          'lastname' => 'last name',
 7280:                          'lastfirst' => 'last name, first name',
 7281:                      );
 7282:     my @titleorder = ('uname','lastname','lastfirst');
 7283:     return (\%searchtitles,\@titleorder);
 7284: }
 7285: 
 7286: sub sorted_searchtypes {
 7287:     my %srchtypes_desc = (
 7288:                            exact    => 'is exact match',
 7289:                            contains => 'contains ..',
 7290:                            begins   => 'begins with ..',
 7291:                          );
 7292:     my @srchtypeorder = ('exact','begins','contains');
 7293:     return (\%srchtypes_desc,\@srchtypeorder);
 7294: }
 7295: 
 7296: sub usertype_update_row {
 7297:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 7298:     my $datatable;
 7299:     my $numinrow = 4;
 7300:     foreach my $type (@{$types}) {
 7301:         if (defined($usertypes->{$type})) {
 7302:             $$rownums ++;
 7303:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 7304:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 7305:                           '</td><td class="LC_left_item"><table>';
 7306:             for (my $i=0; $i<@{$fields}; $i++) {
 7307:                 my $rem = $i%($numinrow);
 7308:                 if ($rem == 0) {
 7309:                     if ($i > 0) {
 7310:                         $datatable .= '</tr>';
 7311:                     }
 7312:                     $datatable .= '<tr>';
 7313:                 }
 7314:                 my $check = ' ';
 7315:                 if (ref($settings) eq 'HASH') {
 7316:                     if (ref($settings->{'fields'}) eq 'HASH') {
 7317:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 7318:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 7319:                                 $check = ' checked="checked" ';
 7320:                             }
 7321:                         }
 7322:                     }
 7323:                 }
 7324: 
 7325:                 if ($i == @{$fields}-1) {
 7326:                     my $colsleft = $numinrow - $rem;
 7327:                     if ($colsleft > 1) {
 7328:                         $datatable .= '<td colspan="'.$colsleft.'">';
 7329:                     } else {
 7330:                         $datatable .= '<td>';
 7331:                     }
 7332:                 } else {
 7333:                     $datatable .= '<td>';
 7334:                 }
 7335:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7336:                               '<input type="checkbox" name="updateable_'.$type.
 7337:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 7338:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 7339:             }
 7340:             $datatable .= '</tr></table></td></tr>';
 7341:         }
 7342:     }
 7343:     return $datatable;
 7344: }
 7345: 
 7346: sub modify_login {
 7347:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 7348:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 7349:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 7350:     %title = ( coursecatalog => 'Display course catalog',
 7351:                adminmail => 'Display administrator E-mail address',
 7352:                helpdesk  => 'Display "Contact Helpdesk" link',
 7353:                newuser => 'Link for visitors to create a user account',
 7354:                loginheader => 'Log-in box header');
 7355:     @offon = ('off','on');
 7356:     if (ref($domconfig{login}) eq 'HASH') {
 7357:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 7358:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 7359:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 7360:             }
 7361:         }
 7362:     }
 7363:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 7364:                                            \%domconfig,\%loginhash);
 7365:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7366:     foreach my $item (@toggles) {
 7367:         $loginhash{login}{$item} = $env{'form.'.$item};
 7368:     }
 7369:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 7370:     if (ref($colchanges{'login'}) eq 'HASH') {  
 7371:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 7372:                                          \%loginhash);
 7373:     }
 7374: 
 7375:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7376:     my %domservers = &Apache::lonnet::get_servers($dom);
 7377:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 7378:     if (keys(%servers) > 1) {
 7379:         foreach my $lonhost (keys(%servers)) {
 7380:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 7381:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 7382:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 7383:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 7384:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 7385:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7386:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7387:                         $changes{'loginvia'}{$lonhost} = 1;
 7388:                     } else {
 7389:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 7390:                         $changes{'loginvia'}{$lonhost} = 1;
 7391:                     }
 7392:                 } else {
 7393:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7394:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7395:                         $changes{'loginvia'}{$lonhost} = 1;
 7396:                     }
 7397:                 }
 7398:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 7399:                     foreach my $item (@loginvia_attribs) {
 7400:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 7401:                     }
 7402:                 } else {
 7403:                     foreach my $item (@loginvia_attribs) {
 7404:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7405:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7406:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 7407:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7408:                                 $new = '/';
 7409:                             }
 7410:                         }
 7411:                         if (($item eq 'custompath') && 
 7412:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7413:                             $new = '';
 7414:                         }
 7415:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 7416:                             $changes{'loginvia'}{$lonhost} = 1;
 7417:                         }
 7418:                         if ($item eq 'exempt') {
 7419:                             $new = &check_exempt_addresses($new);
 7420:                         }
 7421:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7422:                     }
 7423:                 }
 7424:             } else {
 7425:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7426:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7427:                     $changes{'loginvia'}{$lonhost} = 1;
 7428:                     foreach my $item (@loginvia_attribs) {
 7429:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7430:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7431:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7432:                                 $new = '/';
 7433:                             }
 7434:                         }
 7435:                         if (($item eq 'custompath') && 
 7436:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7437:                             $new = '';
 7438:                         }
 7439:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7440:                     }
 7441:                 }
 7442:             }
 7443:         }
 7444:     }
 7445: 
 7446:     my $servadm = $r->dir_config('lonAdmEMail');
 7447:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 7448:     if (ref($domconfig{'login'}) eq 'HASH') {
 7449:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 7450:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 7451:                 if ($lang eq 'nolang') {
 7452:                     push(@currlangs,$lang);
 7453:                 } elsif (defined($langchoices{$lang})) {
 7454:                     push(@currlangs,$lang);
 7455:                 } else {
 7456:                     next;
 7457:                 }
 7458:             }
 7459:         }
 7460:     }
 7461:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 7462:     if (@currlangs > 0) {
 7463:         foreach my $lang (@currlangs) {
 7464:             if (grep(/^\Q$lang\E$/,@delurls)) {
 7465:                 $changes{'helpurl'}{$lang} = 1;
 7466:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 7467:                 $changes{'helpurl'}{$lang} = 1;
 7468:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 7469:                 push(@newlangs,$lang);
 7470:             } else {
 7471:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7472:             }
 7473:         }
 7474:     }
 7475:     unless (grep(/^nolang$/,@currlangs)) {
 7476:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 7477:             $changes{'helpurl'}{'nolang'} = 1;
 7478:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 7479:             push(@newlangs,'nolang');
 7480:         }
 7481:     }
 7482:     if ($env{'form.loginhelpurl_add_lang'}) {
 7483:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 7484:             ($env{'form.loginhelpurl_add_file.filename'})) {
 7485:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 7486:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 7487:         }
 7488:     }
 7489:     if ((@newlangs > 0) || ($addedfile)) {
 7490:         my $error;
 7491:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7492:         if ($configuserok eq 'ok') {
 7493:             if ($switchserver) {
 7494:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 7495:             } elsif ($author_ok eq 'ok') {
 7496:                 my @allnew = @newlangs;
 7497:                 if ($addedfile ne '') {
 7498:                     push(@allnew,$addedfile);
 7499:                 }
 7500:                 foreach my $lang (@allnew) {
 7501:                     my $formelem = 'loginhelpurl_'.$lang;
 7502:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 7503:                         $formelem = 'loginhelpurl_add_file';
 7504:                     }
 7505:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7506:                                                                "help/$lang",'','',$newfile{$lang});
 7507:                     if ($result eq 'ok') {
 7508:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 7509:                         $changes{'helpurl'}{$lang} = 1;
 7510:                     } else {
 7511:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 7512:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7513:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 7514:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 7515:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7516:                         }
 7517:                     }
 7518:                 }
 7519:             } else {
 7520:                 $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);
 7521:             }
 7522:         } else {
 7523:             $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);
 7524:         }
 7525:         if ($error) {
 7526:             &Apache::lonnet::logthis($error);
 7527:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7528:         }
 7529:     }
 7530: 
 7531:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 7532:     if (ref($domconfig{'login'}) eq 'HASH') {
 7533:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 7534:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 7535:                 if ($domservers{$lonhost}) {
 7536:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7537:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 7538:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 7539:                     }
 7540:                 }
 7541:             }
 7542:         }
 7543:     }
 7544:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 7545:     foreach my $lonhost (sort(keys(%domservers))) {
 7546:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7547:             $changes{'headtag'}{$lonhost} = 1;
 7548:         } else {
 7549:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 7550:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 7551:             }
 7552:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 7553:                 push(@newhosts,$lonhost);
 7554:             } elsif ($currheadtagurls{$lonhost}) {
 7555:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 7556:                 if ($currexempt{$lonhost}) {
 7557:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) { 
 7558:                         $changes{'headtag'}{$lonhost} = 1;
 7559:                     }
 7560:                 } elsif ($possexempt{$lonhost}) {
 7561:                     $changes{'headtag'}{$lonhost} = 1;
 7562:                 }
 7563:                 if ($possexempt{$lonhost}) {
 7564:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7565:                 }
 7566:             }
 7567:         }
 7568:     }
 7569:     if (@newhosts) {
 7570:         my $error;
 7571:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7572:         if ($configuserok eq 'ok') {
 7573:             if ($switchserver) {
 7574:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 7575:             } elsif ($author_ok eq 'ok') {
 7576:                 foreach my $lonhost (@newhosts) {
 7577:                     my $formelem = 'loginheadtag_'.$lonhost;
 7578:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7579:                                                                           "login/headtag/$lonhost",'','',
 7580:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 7581:                     if ($result eq 'ok') {
 7582:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 7583:                         $changes{'headtag'}{$lonhost} = 1;
 7584:                         if ($possexempt{$lonhost}) {
 7585:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7586:                         }
 7587:                     } else {
 7588:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 7589:                                            $newheadtagurls{$lonhost},$result);
 7590:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7591:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 7592:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 7593:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 7594:                         }
 7595:                     }
 7596:                 }
 7597:             } else {
 7598:                 $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);
 7599:             }
 7600:         } else {
 7601:             $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);
 7602:         }
 7603:         if ($error) {
 7604:             &Apache::lonnet::logthis($error);
 7605:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7606:         }
 7607:     }
 7608:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 7609: 
 7610:     my $defaulthelpfile = '/adm/loginproblems.html';
 7611:     my $defaulttext = &mt('Default in use');
 7612: 
 7613:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 7614:                                              $dom);
 7615:     if ($putresult eq 'ok') {
 7616:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7617:         my %defaultchecked = (
 7618:                     'coursecatalog' => 'on',
 7619:                     'helpdesk'      => 'on',
 7620:                     'adminmail'     => 'off',
 7621:                     'newuser'       => 'off',
 7622:         );
 7623:         if (ref($domconfig{'login'}) eq 'HASH') {
 7624:             foreach my $item (@toggles) {
 7625:                 if ($defaultchecked{$item} eq 'on') { 
 7626:                     if (($domconfig{'login'}{$item} eq '0') &&
 7627:                         ($env{'form.'.$item} eq '1')) {
 7628:                         $changes{$item} = 1;
 7629:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7630:                               $domconfig{'login'}{$item} eq '1') &&
 7631:                              ($env{'form.'.$item} eq '0')) {
 7632:                         $changes{$item} = 1;
 7633:                     }
 7634:                 } elsif ($defaultchecked{$item} eq 'off') {
 7635:                     if (($domconfig{'login'}{$item} eq '1') &&
 7636:                         ($env{'form.'.$item} eq '0')) {
 7637:                         $changes{$item} = 1;
 7638:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7639:                               $domconfig{'login'}{$item} eq '0') &&
 7640:                              ($env{'form.'.$item} eq '1')) {
 7641:                         $changes{$item} = 1;
 7642:                     }
 7643:                 }
 7644:             }
 7645:         }
 7646:         if (keys(%changes) > 0 || $colchgtext) {
 7647:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7648:             if (ref($lastactref) eq 'HASH') {
 7649:                 $lastactref->{'domainconfig'} = 1;
 7650:             }
 7651:             $resulttext = &mt('Changes made:').'<ul>';
 7652:             foreach my $item (sort(keys(%changes))) {
 7653:                 if ($item eq 'loginvia') {
 7654:                     if (ref($changes{$item}) eq 'HASH') {
 7655:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 7656:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7657:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 7658:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 7659:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 7660:                                     $protocol = 'http' if ($protocol ne 'https');
 7661:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 7662: 
 7663:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 7664:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 7665:                                     } else {
 7666:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 7667:                                     }
 7668:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 7669:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 7670:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 7671:                                     }
 7672:                                     $resulttext .= '</li>';
 7673:                                 } else {
 7674:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 7675:                                 }
 7676:                             } else {
 7677:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 7678:                             }
 7679:                         }
 7680:                         $resulttext .= '</ul></li>';
 7681:                     }
 7682:                 } elsif ($item eq 'helpurl') {
 7683:                     if (ref($changes{$item}) eq 'HASH') {
 7684:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 7685:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 7686:                                 my ($chg,$link);
 7687:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 7688:                                 if ($lang eq 'nolang') {
 7689:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 7690:                                 } else {
 7691:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 7692:                                 }
 7693:                                 $resulttext .= '<li>'.$chg.'</li>';
 7694:                             } else {
 7695:                                 my $chg;
 7696:                                 if ($lang eq 'nolang') {
 7697:                                     $chg = &mt('custom log-in help file for no preferred language');
 7698:                                 } else {
 7699:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 7700:                                 }
 7701:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 7702:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 7703:                                                       '?inhibitmenu=yes',$chg,600,500).
 7704:                                                '</li>';
 7705:                             }
 7706:                         }
 7707:                     }
 7708:                 } elsif ($item eq 'headtag') {
 7709:                     if (ref($changes{$item}) eq 'HASH') {
 7710:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7711:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7712:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 7713:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7714:                                 $resulttext .= '<li><a href="'.
 7715:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 7716:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 7717:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 7718:                                 if ($possexempt{$lonhost}) {
 7719:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 7720:                                 } else {
 7721:                                     $resulttext .= &mt('included for any client IP');
 7722:                                 }
 7723:                                 $resulttext .= '</li>';
 7724:                             }
 7725:                         }
 7726:                     }
 7727:                 } elsif ($item eq 'captcha') {
 7728:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7729:                         my $chgtxt;
 7730:                         if ($loginhash{'login'}{$item} eq 'notused') {
 7731:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 7732:                         } else {
 7733:                             my %captchas = &captcha_phrases();
 7734:                             if ($captchas{$loginhash{'login'}{$item}}) {
 7735:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 7736:                             } else {
 7737:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 7738:                             }
 7739:                         }
 7740:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7741:                     }
 7742:                 } elsif ($item eq 'recaptchakeys') {
 7743:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7744:                         my ($privkey,$pubkey);
 7745:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 7746:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 7747:                             $privkey = $loginhash{'login'}{$item}{'private'};
 7748:                         }
 7749:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 7750:                         if (!$pubkey) {
 7751:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 7752:                         } else {
 7753:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 7754:                         }
 7755:                         if (!$privkey) {
 7756:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 7757:                         } else {
 7758:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 7759:                         }
 7760:                         $chgtxt .= '</ul>';
 7761:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7762:                     }
 7763:                 } elsif ($item eq 'recaptchaversion') {
 7764:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7765:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 7766:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 7767:                                            '</li>';
 7768:                         }
 7769:                     }
 7770:                 } else {
 7771:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 7772:                 }
 7773:             }
 7774:             $resulttext .= $colchgtext.'</ul>';
 7775:         } else {
 7776:             $resulttext = &mt('No changes made to log-in page settings');
 7777:         }
 7778:     } else {
 7779:         $resulttext = '<span class="LC_error">'.
 7780: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7781:     }
 7782:     if ($errors) {
 7783:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7784:                        $errors.'</ul>';
 7785:     }
 7786:     return $resulttext;
 7787: }
 7788: 
 7789: 
 7790: sub check_exempt_addresses {
 7791:     my ($iplist) = @_;
 7792:     $iplist =~ s/^\s+//;
 7793:     $iplist =~ s/\s+$//;
 7794:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 7795:     my (@okips,$new);
 7796:     foreach my $ip (@poss_ips) {
 7797:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 7798:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 7799:                 push(@okips,$ip);
 7800:             }
 7801:         }
 7802:     }
 7803:     if (@okips > 0) {
 7804:         $new = join(',',@okips);
 7805:     } else {
 7806:         $new = '';
 7807:     }
 7808:     return $new;
 7809: }
 7810: 
 7811: sub color_font_choices {
 7812:     my %choices =
 7813:         &Apache::lonlocal::texthash (
 7814:             img => "Header",
 7815:             bgs => "Background colors",
 7816:             links => "Link colors",
 7817:             images => "Images",
 7818:             font => "Font color",
 7819:             fontmenu => "Font menu",
 7820:             pgbg => "Page",
 7821:             tabbg => "Header",
 7822:             sidebg => "Border",
 7823:             link => "Link",
 7824:             alink => "Active link",
 7825:             vlink => "Visited link",
 7826:         );
 7827:     return %choices;
 7828: }
 7829: 
 7830: sub modify_rolecolors {
 7831:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 7832:     my ($resulttext,%rolehash);
 7833:     $rolehash{'rolecolors'} = {};
 7834:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 7835:         if ($domconfig{'rolecolors'} eq '') {
 7836:             $domconfig{'rolecolors'} = {};
 7837:         }
 7838:     }
 7839:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 7840:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 7841:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 7842:                                              $dom);
 7843:     if ($putresult eq 'ok') {
 7844:         if (keys(%changes) > 0) {
 7845:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7846:             if (ref($lastactref) eq 'HASH') {
 7847:                 $lastactref->{'domainconfig'} = 1;
 7848:             }
 7849:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 7850:                                              $rolehash{'rolecolors'});
 7851:         } else {
 7852:             $resulttext = &mt('No changes made to default color schemes');
 7853:         }
 7854:     } else {
 7855:         $resulttext = '<span class="LC_error">'.
 7856: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7857:     }
 7858:     if ($errors) {
 7859:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7860:                        $errors.'</ul>';
 7861:     }
 7862:     return $resulttext;
 7863: }
 7864: 
 7865: sub modify_colors {
 7866:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 7867:     my (%changes,%choices);
 7868:     my @bgs;
 7869:     my @links = ('link','alink','vlink');
 7870:     my @logintext;
 7871:     my @images;
 7872:     my $servadm = $r->dir_config('lonAdmEMail');
 7873:     my $errors;
 7874:     my %defaults;
 7875:     foreach my $role (@{$roles}) {
 7876:         if ($role eq 'login') {
 7877:             %choices = &login_choices();
 7878:             @logintext = ('textcol','bgcol');
 7879:         } else {
 7880:             %choices = &color_font_choices();
 7881:         }
 7882:         if ($role eq 'login') {
 7883:             @images = ('img','logo','domlogo','login');
 7884:             @bgs = ('pgbg','mainbg','sidebg');
 7885:         } else {
 7886:             @images = ('img');
 7887:             @bgs = ('pgbg','tabbg','sidebg');
 7888:         }
 7889:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 7890:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 7891:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 7892:         }
 7893:         if ($role eq 'login') {
 7894:             foreach my $item (@logintext) {
 7895:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7896:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7897:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7898:                 }
 7899:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 7900:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7901:                 }
 7902:             }
 7903:         } else {
 7904:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 7905:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 7906:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 7907:             }
 7908:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 7909:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 7910:             }
 7911:         }
 7912:         foreach my $item (@bgs) {
 7913:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7914:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7915:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7916:             }
 7917:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 7918:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7919:             }
 7920:         }
 7921:         foreach my $item (@links) {
 7922:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7923:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7924:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7925:             }
 7926:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 7927:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7928:             }
 7929:         }
 7930:         my ($configuserok,$author_ok,$switchserver) = 
 7931:             &config_check($dom,$confname,$servadm);
 7932:         my ($width,$height) = &thumb_dimensions();
 7933:         if (ref($domconfig->{$role}) ne 'HASH') {
 7934:             $domconfig->{$role} = {};
 7935:         }
 7936:         foreach my $img (@images) {
 7937:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 7938:                 if (defined($env{'form.login_showlogo_'.$img})) {
 7939:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 7940:                 } else { 
 7941:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 7942:                 }
 7943:             } 
 7944: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 7945: 		 && !defined($domconfig->{$role}{$img})
 7946: 		 && !$env{'form.'.$role.'_del_'.$img}
 7947: 		 && $env{'form.'.$role.'_import_'.$img}) {
 7948: 		# import the old configured image from the .tab setting
 7949: 		# if they haven't provided a new one 
 7950: 		$domconfig->{$role}{$img} = 
 7951: 		    $env{'form.'.$role.'_import_'.$img};
 7952: 	    }
 7953:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 7954:                 my $error;
 7955:                 if ($configuserok eq 'ok') {
 7956:                     if ($switchserver) {
 7957:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 7958:                     } else {
 7959:                         if ($author_ok eq 'ok') {
 7960:                             my ($result,$logourl) = 
 7961:                                 &publishlogo($r,'upload',$role.'_'.$img,
 7962:                                            $dom,$confname,$img,$width,$height);
 7963:                             if ($result eq 'ok') {
 7964:                                 $confhash->{$role}{$img} = $logourl;
 7965:                                 $changes{$role}{'images'}{$img} = 1;
 7966:                             } else {
 7967:                                 $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);
 7968:                             }
 7969:                         } else {
 7970:                             $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);
 7971:                         }
 7972:                     }
 7973:                 } else {
 7974:                     $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);
 7975:                 }
 7976:                 if ($error) {
 7977:                     &Apache::lonnet::logthis($error);
 7978:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7979:                 }
 7980:             } elsif ($domconfig->{$role}{$img} ne '') {
 7981:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 7982:                     my $error;
 7983:                     if ($configuserok eq 'ok') {
 7984: # is confname an author?
 7985:                         if ($switchserver eq '') {
 7986:                             if ($author_ok eq 'ok') {
 7987:                                 my ($result,$logourl) = 
 7988:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 7989:                                             $dom,$confname,$img,$width,$height);
 7990:                                 if ($result eq 'ok') {
 7991:                                     $confhash->{$role}{$img} = $logourl;
 7992: 				    $changes{$role}{'images'}{$img} = 1;
 7993:                                 }
 7994:                             }
 7995:                         }
 7996:                     }
 7997:                 }
 7998:             }
 7999:         }
 8000:         if (ref($domconfig) eq 'HASH') {
 8001:             if (ref($domconfig->{$role}) eq 'HASH') {
 8002:                 foreach my $img (@images) {
 8003:                     if ($domconfig->{$role}{$img} ne '') {
 8004:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8005:                             $confhash->{$role}{$img} = '';
 8006:                             $changes{$role}{'images'}{$img} = 1;
 8007:                         } else {
 8008:                             if ($confhash->{$role}{$img} eq '') {
 8009:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 8010:                             }
 8011:                         }
 8012:                     } else {
 8013:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8014:                             $confhash->{$role}{$img} = '';
 8015:                             $changes{$role}{'images'}{$img} = 1;
 8016:                         } 
 8017:                     }
 8018:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 8019:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 8020:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 8021:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 8022:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 8023:                             }
 8024:                         } else {
 8025:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8026:                                 $changes{$role}{'showlogo'}{$img} = 1;
 8027:                             }
 8028:                         }
 8029:                     }
 8030:                 }
 8031:                 if ($domconfig->{$role}{'font'} ne '') {
 8032:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 8033:                         $changes{$role}{'font'} = 1;
 8034:                     }
 8035:                 } else {
 8036:                     if ($confhash->{$role}{'font'}) {
 8037:                         $changes{$role}{'font'} = 1;
 8038:                     }
 8039:                 }
 8040:                 if ($role ne 'login') {
 8041:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 8042:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 8043:                             $changes{$role}{'fontmenu'} = 1;
 8044:                         }
 8045:                     } else {
 8046:                         if ($confhash->{$role}{'fontmenu'}) {
 8047:                             $changes{$role}{'fontmenu'} = 1;
 8048:                         }
 8049:                     }
 8050:                 }
 8051:                 foreach my $item (@bgs) {
 8052:                     if ($domconfig->{$role}{$item} ne '') {
 8053:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8054:                             $changes{$role}{'bgs'}{$item} = 1;
 8055:                         } 
 8056:                     } else {
 8057:                         if ($confhash->{$role}{$item}) {
 8058:                             $changes{$role}{'bgs'}{$item} = 1;
 8059:                         }
 8060:                     }
 8061:                 }
 8062:                 foreach my $item (@links) {
 8063:                     if ($domconfig->{$role}{$item} ne '') {
 8064:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8065:                             $changes{$role}{'links'}{$item} = 1;
 8066:                         }
 8067:                     } else {
 8068:                         if ($confhash->{$role}{$item}) {
 8069:                             $changes{$role}{'links'}{$item} = 1;
 8070:                         }
 8071:                     }
 8072:                 }
 8073:                 foreach my $item (@logintext) {
 8074:                     if ($domconfig->{$role}{$item} ne '') {
 8075:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8076:                             $changes{$role}{'logintext'}{$item} = 1;
 8077:                         }
 8078:                     } else {
 8079:                         if ($confhash->{$role}{$item}) {
 8080:                             $changes{$role}{'logintext'}{$item} = 1;
 8081:                         }
 8082:                     }
 8083:                 }
 8084:             } else {
 8085:                 &default_change_checker($role,\@images,\@links,\@bgs,
 8086:                                         \@logintext,$confhash,\%changes); 
 8087:             }
 8088:         } else {
 8089:             &default_change_checker($role,\@images,\@links,\@bgs,
 8090:                                     \@logintext,$confhash,\%changes); 
 8091:         }
 8092:     }
 8093:     return ($errors,%changes);
 8094: }
 8095: 
 8096: sub config_check {
 8097:     my ($dom,$confname,$servadm) = @_;
 8098:     my ($configuserok,$author_ok,$switchserver,%currroles);
 8099:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 8100:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 8101:                                                    $confname,$servadm);
 8102:     if ($configuserok eq 'ok') {
 8103:         $switchserver = &check_switchserver($dom,$confname);
 8104:         if ($switchserver eq '') {
 8105:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 8106:         }
 8107:     }
 8108:     return ($configuserok,$author_ok,$switchserver);
 8109: }
 8110: 
 8111: sub default_change_checker {
 8112:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 8113:     foreach my $item (@{$links}) {
 8114:         if ($confhash->{$role}{$item}) {
 8115:             $changes->{$role}{'links'}{$item} = 1;
 8116:         }
 8117:     }
 8118:     foreach my $item (@{$bgs}) {
 8119:         if ($confhash->{$role}{$item}) {
 8120:             $changes->{$role}{'bgs'}{$item} = 1;
 8121:         }
 8122:     }
 8123:     foreach my $item (@{$logintext}) {
 8124:         if ($confhash->{$role}{$item}) {
 8125:             $changes->{$role}{'logintext'}{$item} = 1;
 8126:         }
 8127:     }
 8128:     foreach my $img (@{$images}) {
 8129:         if ($env{'form.'.$role.'_del_'.$img}) {
 8130:             $confhash->{$role}{$img} = '';
 8131:             $changes->{$role}{'images'}{$img} = 1;
 8132:         }
 8133:         if ($role eq 'login') {
 8134:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8135:                 $changes->{$role}{'showlogo'}{$img} = 1;
 8136:             }
 8137:         }
 8138:     }
 8139:     if ($confhash->{$role}{'font'}) {
 8140:         $changes->{$role}{'font'} = 1;
 8141:     }
 8142: }
 8143: 
 8144: sub display_colorchgs {
 8145:     my ($dom,$changes,$roles,$confhash) = @_;
 8146:     my (%choices,$resulttext);
 8147:     if (!grep(/^login$/,@{$roles})) {
 8148:         $resulttext = &mt('Changes made:').'<br />';
 8149:     }
 8150:     foreach my $role (@{$roles}) {
 8151:         if ($role eq 'login') {
 8152:             %choices = &login_choices();
 8153:         } else {
 8154:             %choices = &color_font_choices();
 8155:         }
 8156:         if (ref($changes->{$role}) eq 'HASH') {
 8157:             if ($role ne 'login') {
 8158:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 8159:             }
 8160:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 8161:                 if ($role ne 'login') {
 8162:                     $resulttext .= '<ul>';
 8163:                 }
 8164:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 8165:                     if ($role ne 'login') {
 8166:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 8167:                     }
 8168:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 8169:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 8170:                             if ($confhash->{$role}{$key}{$item}) {
 8171:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 8172:                             } else {
 8173:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 8174:                             }
 8175:                         } elsif ($confhash->{$role}{$item} eq '') {
 8176:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 8177:                         } else {
 8178:                             my $newitem = $confhash->{$role}{$item};
 8179:                             if ($key eq 'images') {
 8180:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 8181:                             }
 8182:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 8183:                         }
 8184:                     }
 8185:                     if ($role ne 'login') {
 8186:                         $resulttext .= '</ul></li>';
 8187:                     }
 8188:                 } else {
 8189:                     if ($confhash->{$role}{$key} eq '') {
 8190:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 8191:                     } else {
 8192:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 8193:                     }
 8194:                 }
 8195:                 if ($role ne 'login') {
 8196:                     $resulttext .= '</ul>';
 8197:                 }
 8198:             }
 8199:         }
 8200:     }
 8201:     return $resulttext;
 8202: }
 8203: 
 8204: sub thumb_dimensions {
 8205:     return ('200','50');
 8206: }
 8207: 
 8208: sub check_dimensions {
 8209:     my ($inputfile) = @_;
 8210:     my ($fullwidth,$fullheight);
 8211:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 8212:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 8213:             my $imageinfo = <PIPE>;
 8214:             if (!close(PIPE)) {
 8215:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 8216:             }
 8217:             chomp($imageinfo);
 8218:             my ($fullsize) = 
 8219:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 8220:             if ($fullsize) {
 8221:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 8222:             }
 8223:         }
 8224:     }
 8225:     return ($fullwidth,$fullheight);
 8226: }
 8227: 
 8228: sub check_configuser {
 8229:     my ($uhome,$dom,$confname,$servadm) = @_;
 8230:     my ($configuserok,%currroles);
 8231:     if ($uhome eq 'no_host') {
 8232:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 8233:         my $configpass = &LONCAPA::Enrollment::create_password();
 8234:         $configuserok = 
 8235:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 8236:                              $configpass,'','','','','',undef,$servadm);
 8237:     } else {
 8238:         $configuserok = 'ok';
 8239:         %currroles = 
 8240:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 8241:     }
 8242:     return ($configuserok,%currroles);
 8243: }
 8244: 
 8245: sub check_authorstatus {
 8246:     my ($dom,$confname,%currroles) = @_;
 8247:     my $author_ok;
 8248:     if (!$currroles{':'.$dom.':au'}) {
 8249:         my $start = time;
 8250:         my $end = 0;
 8251:         $author_ok = 
 8252:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 8253:                                         'au',$end,$start,'','','domconfig');
 8254:     } else {
 8255:         $author_ok = 'ok';
 8256:     }
 8257:     return $author_ok;
 8258: }
 8259: 
 8260: sub publishlogo {
 8261:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 8262:     my ($output,$fname,$logourl,$madethumb);
 8263:     if ($action eq 'upload') {
 8264:         $fname=$env{'form.'.$formname.'.filename'};
 8265:         chop($env{'form.'.$formname});
 8266:     } else {
 8267:         ($fname) = ($formname =~ /([^\/]+)$/);
 8268:     }
 8269:     if ($savefileas ne '') {
 8270:         $fname = $savefileas;
 8271:     }
 8272:     $fname=&Apache::lonnet::clean_filename($fname);
 8273: # See if there is anything left
 8274:     unless ($fname) { return ('error: no uploaded file'); }
 8275:     $fname="$subdir/$fname";
 8276:     my $docroot=$r->dir_config('lonDocRoot');
 8277:     my $filepath="$docroot/priv";
 8278:     my $relpath = "$dom/$confname";
 8279:     my ($fnamepath,$file,$fetchthumb);
 8280:     $file=$fname;
 8281:     if ($fname=~m|/|) {
 8282:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 8283:     }
 8284:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 8285:     my $count;
 8286:     for ($count=5;$count<=$#parts;$count++) {
 8287:         $filepath.="/$parts[$count]";
 8288:         if ((-e $filepath)!=1) {
 8289:             mkdir($filepath,02770);
 8290:         }
 8291:     }
 8292:     # Check for bad extension and disallow upload
 8293:     if ($file=~/\.(\w+)$/ &&
 8294:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 8295:         $output = 
 8296:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 8297:     } elsif ($file=~/\.(\w+)$/ &&
 8298:         !defined(&Apache::loncommon::fileembstyle($1))) {
 8299:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 8300:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 8301:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 8302:     } elsif (-d "$filepath/$file") {
 8303:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 8304:     } else {
 8305:         my $source = $filepath.'/'.$file;
 8306:         my $logfile;
 8307:         if (!open($logfile,">>$source".'.log')) {
 8308:             return (&mt('No write permission to Authoring Space'));
 8309:         }
 8310:         print $logfile
 8311: "\n================= Publish ".localtime()." ================\n".
 8312: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 8313: # Save the file
 8314:         if (!open(FH,'>'.$source)) {
 8315:             &Apache::lonnet::logthis('Failed to create '.$source);
 8316:             return (&mt('Failed to create file'));
 8317:         }
 8318:         if ($action eq 'upload') {
 8319:             if (!print FH ($env{'form.'.$formname})) {
 8320:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 8321:                 return (&mt('Failed to write file'));
 8322:             }
 8323:         } else {
 8324:             my $original = &Apache::lonnet::filelocation('',$formname);
 8325:             if(!copy($original,$source)) {
 8326:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 8327:                 return (&mt('Failed to write file'));
 8328:             }
 8329:         }
 8330:         close(FH);
 8331:         chmod(0660, $source); # Permissions to rw-rw---.
 8332: 
 8333:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 8334:         my $copyfile=$targetdir.'/'.$file;
 8335: 
 8336:         my @parts=split(/\//,$targetdir);
 8337:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 8338:         for (my $count=5;$count<=$#parts;$count++) {
 8339:             $path.="/$parts[$count]";
 8340:             if (!-e $path) {
 8341:                 print $logfile "\nCreating directory ".$path;
 8342:                 mkdir($path,02770);
 8343:             }
 8344:         }
 8345:         my $versionresult;
 8346:         if (-e $copyfile) {
 8347:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 8348:         } else {
 8349:             $versionresult = 'ok';
 8350:         }
 8351:         if ($versionresult eq 'ok') {
 8352:             if (copy($source,$copyfile)) {
 8353:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 8354:                 $output = 'ok';
 8355:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 8356:                 push(@{$modified_urls},[$copyfile,$source]);
 8357:                 my $metaoutput = 
 8358:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 8359:                 unless ($registered_cleanup) {
 8360:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8361:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8362:                     $registered_cleanup=1;
 8363:                 }
 8364:             } else {
 8365:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 8366:                 $output = &mt('Failed to copy file to RES space').", $!";
 8367:             }
 8368:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 8369:                 my $inputfile = $filepath.'/'.$file;
 8370:                 my $outfile = $filepath.'/'.'tn-'.$file;
 8371:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 8372:                 if ($fullwidth ne '' && $fullheight ne '') { 
 8373:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 8374:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 8375:                         system("convert -sample $thumbsize $inputfile $outfile");
 8376:                         chmod(0660, $filepath.'/tn-'.$file);
 8377:                         if (-e $outfile) {
 8378:                             my $copyfile=$targetdir.'/tn-'.$file;
 8379:                             if (copy($outfile,$copyfile)) {
 8380:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 8381:                                 my $thumb_metaoutput = 
 8382:                                     &write_metadata($dom,$confname,$formname,
 8383:                                                     $targetdir,'tn-'.$file,$logfile);
 8384:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 8385:                                 unless ($registered_cleanup) {
 8386:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8387:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8388:                                     $registered_cleanup=1;
 8389:                                 }
 8390:                                 $madethumb = 1;
 8391:                             } else {
 8392:                                 print $logfile "\nUnable to write ".$copyfile.
 8393:                                                ':'.$!."\n";
 8394:                             }
 8395:                         }
 8396:                     }
 8397:                 }
 8398:             }
 8399:         } else {
 8400:             $output = $versionresult;
 8401:         }
 8402:     }
 8403:     return ($output,$logourl,$madethumb);
 8404: }
 8405: 
 8406: sub logo_versioning {
 8407:     my ($targetdir,$file,$logfile) = @_;
 8408:     my $target = $targetdir.'/'.$file;
 8409:     my ($maxversion,$fn,$extn,$output);
 8410:     $maxversion = 0;
 8411:     if ($file =~ /^(.+)\.(\w+)$/) {
 8412:         $fn=$1;
 8413:         $extn=$2;
 8414:     }
 8415:     opendir(DIR,$targetdir);
 8416:     while (my $filename=readdir(DIR)) {
 8417:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 8418:             $maxversion=($1>$maxversion)?$1:$maxversion;
 8419:         }
 8420:     }
 8421:     $maxversion++;
 8422:     print $logfile "\nCreating old version ".$maxversion."\n";
 8423:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 8424:     if (copy($target,$copyfile)) {
 8425:         print $logfile "Copied old target to ".$copyfile."\n";
 8426:         $copyfile=$copyfile.'.meta';
 8427:         if (copy($target.'.meta',$copyfile)) {
 8428:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 8429:             $output = 'ok';
 8430:         } else {
 8431:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 8432:             $output = &mt('Failed to copy old meta').", $!, ";
 8433:         }
 8434:     } else {
 8435:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 8436:         $output = &mt('Failed to copy old target').", $!, ";
 8437:     }
 8438:     return $output;
 8439: }
 8440: 
 8441: sub write_metadata {
 8442:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 8443:     my (%metadatafields,%metadatakeys,$output);
 8444:     $metadatafields{'title'}=$formname;
 8445:     $metadatafields{'creationdate'}=time;
 8446:     $metadatafields{'lastrevisiondate'}=time;
 8447:     $metadatafields{'copyright'}='public';
 8448:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 8449:                                          $env{'user.domain'};
 8450:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 8451:     $metadatafields{'domain'}=$dom;
 8452:     {
 8453:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 8454:         my $mfh;
 8455:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 8456:             foreach (sort(keys(%metadatafields))) {
 8457:                 unless ($_=~/\./) {
 8458:                     my $unikey=$_;
 8459:                     $unikey=~/^([A-Za-z]+)/;
 8460:                     my $tag=$1;
 8461:                     $tag=~tr/A-Z/a-z/;
 8462:                     print $mfh "\n\<$tag";
 8463:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 8464:                         my $value=$metadatafields{$unikey.'.'.$_};
 8465:                         $value=~s/\"/\'\'/g;
 8466:                         print $mfh ' '.$_.'="'.$value.'"';
 8467:                     }
 8468:                     print $mfh '>'.
 8469:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 8470:                             .'</'.$tag.'>';
 8471:                 }
 8472:             }
 8473:             $output = 'ok';
 8474:             print $logfile "\nWrote metadata";
 8475:             close($mfh);
 8476:         } else {
 8477:             print $logfile "\nFailed to open metadata file";
 8478:             $output = &mt('Could not write metadata');
 8479:         }
 8480:     }
 8481:     return $output;
 8482: }
 8483: 
 8484: sub notifysubscribed {
 8485:     foreach my $targetsource (@{$modified_urls}){
 8486:         next unless (ref($targetsource) eq 'ARRAY');
 8487:         my ($target,$source)=@{$targetsource};
 8488:         if ($source ne '') {
 8489:             if (open(my $logfh,'>>'.$source.'.log')) {
 8490:                 print $logfh "\nCleanup phase: Notifications\n";
 8491:                 my @subscribed=&subscribed_hosts($target);
 8492:                 foreach my $subhost (@subscribed) {
 8493:                     print $logfh "\nNotifying host ".$subhost.':';
 8494:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 8495:                     print $logfh $reply;
 8496:                 }
 8497:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 8498:                 foreach my $subhost (@subscribedmeta) {
 8499:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 8500:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 8501:                                                         $subhost);
 8502:                     print $logfh $reply;
 8503:                 }
 8504:                 print $logfh "\n============ Done ============\n";
 8505:                 close($logfh);
 8506:             }
 8507:         }
 8508:     }
 8509:     return OK;
 8510: }
 8511: 
 8512: sub subscribed_hosts {
 8513:     my ($target) = @_;
 8514:     my @subscribed;
 8515:     if (open(my $fh,"<$target.subscription")) {
 8516:         while (my $subline=<$fh>) {
 8517:             if ($subline =~ /^($match_lonid):/) {
 8518:                 my $host = $1;
 8519:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 8520:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 8521:                         push(@subscribed,$host);
 8522:                     }
 8523:                 }
 8524:             }
 8525:         }
 8526:     }
 8527:     return @subscribed;
 8528: }
 8529: 
 8530: sub check_switchserver {
 8531:     my ($dom,$confname) = @_;
 8532:     my ($allowed,$switchserver);
 8533:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 8534:     if ($home eq 'no_host') {
 8535:         $home = &Apache::lonnet::domain($dom,'primary');
 8536:     }
 8537:     my @ids=&Apache::lonnet::current_machine_ids();
 8538:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 8539:     if (!$allowed) {
 8540: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 8541:     }
 8542:     return $switchserver;
 8543: }
 8544: 
 8545: sub modify_quotas {
 8546:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 8547:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 8548:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 8549:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 8550:         $validationfieldsref);
 8551:     if ($action eq 'quotas') {
 8552:         $context = 'tools'; 
 8553:     } else {
 8554:         $context = $action;
 8555:     }
 8556:     if ($context eq 'requestcourses') {
 8557:         @usertools = ('official','unofficial','community','textbook','placement');
 8558:         @options =('norequest','approval','validate','autolimit');
 8559:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 8560:         %titles = &courserequest_titles();
 8561:         $toolregexp = join('|',@usertools);
 8562:         %conditions = &courserequest_conditions();
 8563:         $confname = $dom.'-domainconfig';
 8564:         my $servadm = $r->dir_config('lonAdmEMail');
 8565:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8566:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 8567:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 8568:     } elsif ($context eq 'requestauthor') {
 8569:         @usertools = ('author');
 8570:         %titles = &authorrequest_titles();
 8571:     } else {
 8572:         @usertools = ('aboutme','blog','webdav','portfolio');
 8573:         %titles = &tool_titles();
 8574:     }
 8575:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8576:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8577:     foreach my $key (keys(%env)) {
 8578:         if ($context eq 'requestcourses') {
 8579:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 8580:                 my $item = $1;
 8581:                 my $type = $2;
 8582:                 if ($type =~ /^limit_(.+)/) {
 8583:                     $limithash{$item}{$1} = $env{$key};
 8584:                 } else {
 8585:                     $confhash{$item}{$type} = $env{$key};
 8586:                 }
 8587:             }
 8588:         } elsif ($context eq 'requestauthor') {
 8589:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 8590:                 $confhash{$1} = $env{$key};
 8591:             }
 8592:         } else {
 8593:             if ($key =~ /^form\.quota_(.+)$/) {
 8594:                 $confhash{'defaultquota'}{$1} = $env{$key};
 8595:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 8596:                 $confhash{'authorquota'}{$1} = $env{$key};
 8597:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 8598:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 8599:             }
 8600:         }
 8601:     }
 8602:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 8603:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 8604:         @approvalnotify = sort(@approvalnotify);
 8605:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 8606:         my @crstypes = ('official','unofficial','community','textbook','placement');
 8607:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 8608:         foreach my $type (@hasuniquecode) {
 8609:             if (grep(/^\Q$type\E$/,@crstypes)) {
 8610:                 $confhash{'uniquecode'}{$type} = 1;
 8611:             }
 8612:         }
 8613:         my (%newbook,%allpos);
 8614:         if ($context eq 'requestcourses') {
 8615:             foreach my $type ('textbooks','templates') {
 8616:                 @{$allpos{$type}} = (); 
 8617:                 my $invalid;
 8618:                 if ($type eq 'textbooks') {
 8619:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 8620:                 } else {
 8621:                     $invalid = &mt('Invalid LON-CAPA course for template');
 8622:                 }
 8623:                 if ($env{'form.'.$type.'_addbook'}) {
 8624:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 8625:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 8626:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 8627:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 8628:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8629:                         } else {
 8630:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 8631:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 8632:                             $position =~ s/\D+//g;
 8633:                             if ($position ne '') {
 8634:                                 $allpos{$type}[$position] = $newbook{$type};
 8635:                             }
 8636:                         }
 8637:                     } else {
 8638:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8639:                     }
 8640:                 }
 8641:             } 
 8642:         }
 8643:         if (ref($domconfig{$action}) eq 'HASH') {
 8644:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 8645:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 8646:                     $changes{'notify'}{'approval'} = 1;
 8647:                 }
 8648:             } else {
 8649:                 if ($confhash{'notify'}{'approval'}) {
 8650:                     $changes{'notify'}{'approval'} = 1;
 8651:                 }
 8652:             }
 8653:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 8654:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 8655:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 8656:                         unless ($confhash{'uniquecode'}{$crstype}) {
 8657:                             $changes{'uniquecode'} = 1;
 8658:                         }
 8659:                     }
 8660:                     unless ($changes{'uniquecode'}) {
 8661:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 8662:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 8663:                                 $changes{'uniquecode'} = 1;
 8664:                             }
 8665:                         }
 8666:                     }
 8667:                } else {
 8668:                    $changes{'uniquecode'} = 1;
 8669:                }
 8670:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 8671:                 $changes{'uniquecode'} = 1;
 8672:             }
 8673:             if ($context eq 'requestcourses') {
 8674:                 foreach my $type ('textbooks','templates') {
 8675:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8676:                         my %deletions;
 8677:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 8678:                         if (@todelete) {
 8679:                             map { $deletions{$_} = 1; } @todelete;
 8680:                         }
 8681:                         my %imgdeletions;
 8682:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 8683:                         if (@todeleteimages) {
 8684:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 8685:                         }
 8686:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 8687:                         for (my $i=0; $i<=$maxnum; $i++) {
 8688:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 8689:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 8690:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 8691:                                 if ($deletions{$key}) {
 8692:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 8693:                                         #FIXME need to obsolete item in RES space
 8694:                                     }
 8695:                                     next;
 8696:                                 } else {
 8697:                                     my $newpos = $env{'form.'.$itemid};
 8698:                                     $newpos =~ s/\D+//g;
 8699:                                     foreach my $item ('subject','title','publisher','author') {
 8700:                                         next if ((($item eq 'author') || ($item eq 'publisher')) && 
 8701:                                                  ($type eq 'templates'));
 8702:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 8703:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 8704:                                             $changes{$type}{$key} = 1;
 8705:                                         }
 8706:                                     }
 8707:                                     $allpos{$type}[$newpos] = $key;
 8708:                                 }
 8709:                                 if ($imgdeletions{$key}) {
 8710:                                     $changes{$type}{$key} = 1;
 8711:                                     #FIXME need to obsolete item in RES space
 8712:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 8713:                                     my ($cdom,$cnum) = split(/_/,$key);
 8714:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 8715:                                                                                   $cdom,$cnum,$type,$configuserok,
 8716:                                                                                   $switchserver,$author_ok);
 8717:                                     if ($imgurl) {
 8718:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 8719:                                         $changes{$type}{$key} = 1; 
 8720:                                     }
 8721:                                     if ($error) {
 8722:                                         &Apache::lonnet::logthis($error);
 8723:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8724:                                     } 
 8725:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 8726:                                     $confhash{$type}{$key}{'image'} = 
 8727:                                         $domconfig{$action}{$type}{$key}{'image'};
 8728:                                 }
 8729:                             }
 8730:                         }
 8731:                     }
 8732:                 }
 8733:             }
 8734:         } else {
 8735:             if ($confhash{'notify'}{'approval'}) {
 8736:                 $changes{'notify'}{'approval'} = 1;
 8737:             }
 8738:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 8739:                 $changes{'uniquecode'} = 1;
 8740:             }
 8741:         }
 8742:         if ($context eq 'requestcourses') {
 8743:             foreach my $type ('textbooks','templates') {
 8744:                 if ($newbook{$type}) {
 8745:                     $changes{$type}{$newbook{$type}} = 1;
 8746:                     foreach my $item ('subject','title','publisher','author') {
 8747:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8748:                                  ($type eq 'template'));
 8749:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 8750:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 8751:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 8752:                         }
 8753:                     }
 8754:                     if ($type eq 'textbooks') {
 8755:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 8756:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 8757:                             my ($imageurl,$error) =
 8758:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 8759:                                                         $configuserok,$switchserver,$author_ok);
 8760:                             if ($imageurl) {
 8761:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 8762:                             }
 8763:                             if ($error) {
 8764:                                 &Apache::lonnet::logthis($error);
 8765:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8766:                             }
 8767:                         }
 8768:                     }
 8769:                 }
 8770:                 if (@{$allpos{$type}} > 0) {
 8771:                     my $idx = 0;
 8772:                     foreach my $item (@{$allpos{$type}}) {
 8773:                         if ($item ne '') {
 8774:                             $confhash{$type}{$item}{'order'} = $idx;
 8775:                             if (ref($domconfig{$action}) eq 'HASH') {
 8776:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8777:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 8778:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 8779:                                             $changes{$type}{$item} = 1;
 8780:                                         }
 8781:                                     }
 8782:                                 }
 8783:                             }
 8784:                             $idx ++;
 8785:                         }
 8786:                     }
 8787:                 }
 8788:             }
 8789:             if (ref($validationitemsref) eq 'ARRAY') {
 8790:                 foreach my $item (@{$validationitemsref}) {
 8791:                     if ($item eq 'fields') {
 8792:                         my @changed;
 8793:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 8794:                         if (@{$confhash{'validation'}{$item}} > 0) {
 8795:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 8796:                         }
 8797:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8798:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8799:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 8800:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 8801:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 8802:                                 } else {
 8803:                                     @changed = @{$confhash{'validation'}{$item}};
 8804:                                 }
 8805:                             } else {
 8806:                                 @changed = @{$confhash{'validation'}{$item}};
 8807:                             }
 8808:                         } else {
 8809:                             @changed = @{$confhash{'validation'}{$item}};
 8810:                         }
 8811:                         if (@changed) {
 8812:                             if ($confhash{'validation'}{$item}) {
 8813:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 8814:                             } else {
 8815:                                 $changes{'validation'}{$item} = &mt('None');
 8816:                             }
 8817:                         }
 8818:                     } else {
 8819:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 8820:                         if ($item eq 'markup') {
 8821:                             if ($env{'form.requestcourses_validation_'.$item}) {
 8822:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 8823:                             }
 8824:                         }
 8825:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8826:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8827:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 8828:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8829:                                 }
 8830:                             } else {
 8831:                                 if ($confhash{'validation'}{$item} ne '') {
 8832:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8833:                                 }
 8834:                             }
 8835:                         } else {
 8836:                             if ($confhash{'validation'}{$item} ne '') {
 8837:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8838:                             }
 8839:                         }
 8840:                     }
 8841:                 }
 8842:             }
 8843:             if ($env{'form.validationdc'}) {
 8844:                 my $newval = $env{'form.validationdc'};
 8845:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 8846:                 if (exists($domcoords{$newval})) {
 8847:                     $confhash{'validation'}{'dc'} = $newval;
 8848:                 }
 8849:             }
 8850:             if (ref($confhash{'validation'}) eq 'HASH') {
 8851:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8852:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8853:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8854:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8855:                                 if ($confhash{'validation'}{'dc'} eq '') {
 8856:                                     $changes{'validation'}{'dc'} = &mt('None');
 8857:                                 } else {
 8858:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8859:                                 }
 8860:                             }
 8861:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 8862:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8863:                         }
 8864:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 8865:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8866:                     }
 8867:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 8868:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8869:                 }  
 8870:             } else {
 8871:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8872:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8873:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8874:                             $changes{'validation'}{'dc'} = &mt('None');
 8875:                         }
 8876:                     }
 8877:                 }
 8878:             }
 8879:         }
 8880:     } else {
 8881:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 8882:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 8883:     }
 8884:     foreach my $item (@usertools) {
 8885:         foreach my $type (@{$types},'default','_LC_adv') {
 8886:             my $unset; 
 8887:             if ($context eq 'requestcourses') {
 8888:                 $unset = '0';
 8889:                 if ($type eq '_LC_adv') {
 8890:                     $unset = '';
 8891:                 }
 8892:                 if ($confhash{$item}{$type} eq 'autolimit') {
 8893:                     $confhash{$item}{$type} .= '=';
 8894:                     unless ($limithash{$item}{$type} =~ /\D/) {
 8895:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 8896:                     }
 8897:                 }
 8898:             } elsif ($context eq 'requestauthor') {
 8899:                 $unset = '0';
 8900:                 if ($type eq '_LC_adv') {
 8901:                     $unset = '';
 8902:                 }
 8903:             } else {
 8904:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 8905:                     $confhash{$item}{$type} = 1;
 8906:                 } else {
 8907:                     $confhash{$item}{$type} = 0;
 8908:                 }
 8909:             }
 8910:             if (ref($domconfig{$action}) eq 'HASH') {
 8911:                 if ($action eq 'requestauthor') {
 8912:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 8913:                         $changes{$type} = 1;
 8914:                     }
 8915:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 8916:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 8917:                         $changes{$item}{$type} = 1;
 8918:                     }
 8919:                 } else {
 8920:                     if ($context eq 'requestcourses') {
 8921:                         if ($confhash{$item}{$type} ne $unset) {
 8922:                             $changes{$item}{$type} = 1;
 8923:                         }
 8924:                     } else {
 8925:                         if (!$confhash{$item}{$type}) {
 8926:                             $changes{$item}{$type} = 1;
 8927:                         }
 8928:                     }
 8929:                 }
 8930:             } else {
 8931:                 if ($context eq 'requestcourses') {
 8932:                     if ($confhash{$item}{$type} ne $unset) {
 8933:                         $changes{$item}{$type} = 1;
 8934:                     }
 8935:                 } elsif ($context eq 'requestauthor') {
 8936:                     if ($confhash{$type} ne $unset) {
 8937:                         $changes{$type} = 1;
 8938:                     }
 8939:                 } else {
 8940:                     if (!$confhash{$item}{$type}) {
 8941:                         $changes{$item}{$type} = 1;
 8942:                     }
 8943:                 }
 8944:             }
 8945:         }
 8946:     }
 8947:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 8948:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 8949:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 8950:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 8951:                     if (exists($confhash{'defaultquota'}{$key})) {
 8952:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 8953:                             $changes{'defaultquota'}{$key} = 1;
 8954:                         }
 8955:                     } else {
 8956:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 8957:                     }
 8958:                 }
 8959:             } else {
 8960:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 8961:                     if (exists($confhash{'defaultquota'}{$key})) {
 8962:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 8963:                             $changes{'defaultquota'}{$key} = 1;
 8964:                         }
 8965:                     } else {
 8966:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 8967:                     }
 8968:                 }
 8969:             }
 8970:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 8971:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 8972:                     if (exists($confhash{'authorquota'}{$key})) {
 8973:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 8974:                             $changes{'authorquota'}{$key} = 1;
 8975:                         }
 8976:                     } else {
 8977:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 8978:                     }
 8979:                 }
 8980:             }
 8981:         }
 8982:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 8983:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 8984:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 8985:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 8986:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 8987:                             $changes{'defaultquota'}{$key} = 1;
 8988:                         }
 8989:                     } else {
 8990:                         if (!exists($domconfig{'quotas'}{$key})) {
 8991:                             $changes{'defaultquota'}{$key} = 1;
 8992:                         }
 8993:                     }
 8994:                 } else {
 8995:                     $changes{'defaultquota'}{$key} = 1;
 8996:                 }
 8997:             }
 8998:         }
 8999:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 9000:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 9001:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9002:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9003:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 9004:                             $changes{'authorquota'}{$key} = 1;
 9005:                         }
 9006:                     } else {
 9007:                         $changes{'authorquota'}{$key} = 1;
 9008:                     }
 9009:                 } else {
 9010:                     $changes{'authorquota'}{$key} = 1;
 9011:                 }
 9012:             }
 9013:         }
 9014:     }
 9015: 
 9016:     if ($context eq 'requestauthor') {
 9017:         $domdefaults{'requestauthor'} = \%confhash;
 9018:     } else {
 9019:         foreach my $key (keys(%confhash)) {
 9020:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 9021:                 $domdefaults{$key} = $confhash{$key};
 9022:             }
 9023:         }
 9024:     }
 9025: 
 9026:     my %quotahash = (
 9027:                       $action => { %confhash }
 9028:                     );
 9029:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 9030:                                              $dom);
 9031:     if ($putresult eq 'ok') {
 9032:         if (keys(%changes) > 0) {
 9033:             my $cachetime = 24*60*60;
 9034:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9035:             if (ref($lastactref) eq 'HASH') {
 9036:                 $lastactref->{'domdefaults'} = 1;
 9037:             }
 9038:             $resulttext = &mt('Changes made:').'<ul>';
 9039:             unless (($context eq 'requestcourses') ||
 9040:                     ($context eq 'requestauthor')) {
 9041:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 9042:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 9043:                     foreach my $type (@{$types},'default') {
 9044:                         if (defined($changes{'defaultquota'}{$type})) {
 9045:                             my $typetitle = $usertypes->{$type};
 9046:                             if ($type eq 'default') {
 9047:                                 $typetitle = $othertitle;
 9048:                             }
 9049:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 9050:                         }
 9051:                     }
 9052:                     $resulttext .= '</ul></li>';
 9053:                 }
 9054:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 9055:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 9056:                     foreach my $type (@{$types},'default') {
 9057:                         if (defined($changes{'authorquota'}{$type})) {
 9058:                             my $typetitle = $usertypes->{$type};
 9059:                             if ($type eq 'default') {
 9060:                                 $typetitle = $othertitle;
 9061:                             }
 9062:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 9063:                         }
 9064:                     }
 9065:                     $resulttext .= '</ul></li>';
 9066:                 }
 9067:             }
 9068:             my %newenv;
 9069:             foreach my $item (@usertools) {
 9070:                 my (%haschgs,%inconf);
 9071:                 if ($context eq 'requestauthor') {
 9072:                     %haschgs = %changes;
 9073:                     %inconf = %confhash;
 9074:                 } else {
 9075:                     if (ref($changes{$item}) eq 'HASH') {
 9076:                         %haschgs = %{$changes{$item}};
 9077:                     }
 9078:                     if (ref($confhash{$item}) eq 'HASH') {
 9079:                         %inconf = %{$confhash{$item}};
 9080:                     }
 9081:                 }
 9082:                 if (keys(%haschgs) > 0) {
 9083:                     my $newacc = 
 9084:                         &Apache::lonnet::usertools_access($env{'user.name'},
 9085:                                                           $env{'user.domain'},
 9086:                                                           $item,'reload',$context);
 9087:                     if (($context eq 'requestcourses') ||
 9088:                         ($context eq 'requestauthor')) {
 9089:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 9090:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 9091:                         }
 9092:                     } else {
 9093:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 9094:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 9095:                         }
 9096:                     }
 9097:                     unless ($context eq 'requestauthor') {
 9098:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 9099:                     }
 9100:                     foreach my $type (@{$types},'default','_LC_adv') {
 9101:                         if ($haschgs{$type}) {
 9102:                             my $typetitle = $usertypes->{$type};
 9103:                             if ($type eq 'default') {
 9104:                                 $typetitle = $othertitle;
 9105:                             } elsif ($type eq '_LC_adv') {
 9106:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 9107:                             }
 9108:                             if ($inconf{$type}) {
 9109:                                 if ($context eq 'requestcourses') {
 9110:                                     my $cond;
 9111:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 9112:                                         if ($1 eq '') {
 9113:                                             $cond = &mt('(Automatic processing of any request).');
 9114:                                         } else {
 9115:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 9116:                                         }
 9117:                                     } else { 
 9118:                                         $cond = $conditions{$inconf{$type}};
 9119:                                     }
 9120:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 9121:                                 } elsif ($context eq 'requestauthor') {
 9122:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 9123:                                                              $titles{$inconf{$type}},$typetitle);
 9124: 
 9125:                                 } else {
 9126:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 9127:                                 }
 9128:                             } else {
 9129:                                 if ($type eq '_LC_adv') {
 9130:                                     if ($inconf{$type} eq '0') {
 9131:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9132:                                     } else { 
 9133:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 9134:                                     }
 9135:                                 } else {
 9136:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9137:                                 }
 9138:                             }
 9139:                         }
 9140:                     }
 9141:                     unless ($context eq 'requestauthor') {
 9142:                         $resulttext .= '</ul></li>';
 9143:                     }
 9144:                 }
 9145:             }
 9146:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 9147:                 if (ref($changes{'notify'}) eq 'HASH') {
 9148:                     if ($changes{'notify'}{'approval'}) {
 9149:                         if (ref($confhash{'notify'}) eq 'HASH') {
 9150:                             if ($confhash{'notify'}{'approval'}) {
 9151:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 9152:                             } else {
 9153:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 9154:                             }
 9155:                         }
 9156:                     }
 9157:                 }
 9158:             }
 9159:             if ($action eq 'requestcourses') {
 9160:                 my @offon = ('off','on');
 9161:                 if ($changes{'uniquecode'}) {
 9162:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9163:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 9164:                         $resulttext .= '<li>'.
 9165:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 9166:                                        '</li>';
 9167:                     } else {
 9168:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 9169:                                        '</li>';
 9170:                     }
 9171:                 }
 9172:                 foreach my $type ('textbooks','templates') {
 9173:                     if (ref($changes{$type}) eq 'HASH') {
 9174:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 9175:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 9176:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 9177:                             my $coursetitle = $coursehash{'description'};
 9178:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 9179:                             $resulttext .= '<li>';
 9180:                             foreach my $item ('subject','title','publisher','author') {
 9181:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9182:                                          ($type eq 'templates'));
 9183:                                 my $name = $item.':';
 9184:                                 $name =~ s/^(\w)/\U$1/;
 9185:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 9186:                             }
 9187:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 9188:                             if ($type eq 'textbooks') {
 9189:                                 if ($confhash{$type}{$key}{'image'}) {
 9190:                                     $resulttext .= ' '.&mt('Image: [_1]',
 9191:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 9192:                                                    ' alt="Textbook cover" />').'<br />';
 9193:                                 }
 9194:                             }
 9195:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 9196:                         }
 9197:                         $resulttext .= '</ul></li>';
 9198:                     }
 9199:                 }
 9200:                 if (ref($changes{'validation'}) eq 'HASH') {
 9201:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 9202:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 9203:                         foreach my $item (@{$validationitemsref}) {
 9204:                             if (exists($changes{'validation'}{$item})) {
 9205:                                 if ($item eq 'markup') {
 9206:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9207:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 9208:                                 } else {
 9209:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9210:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 9211:                                 }
 9212:                             }
 9213:                         }
 9214:                         if (exists($changes{'validation'}{'dc'})) {
 9215:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 9216:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 9217:                         }
 9218:                     }
 9219:                 }
 9220:             }
 9221:             $resulttext .= '</ul>';
 9222:             if (keys(%newenv)) {
 9223:                 &Apache::lonnet::appenv(\%newenv);
 9224:             }
 9225:         } else {
 9226:             if ($context eq 'requestcourses') {
 9227:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 9228:             } elsif ($context eq 'requestauthor') {
 9229:                 $resulttext = &mt('No changes made to rights to request author space.');
 9230:             } else {
 9231:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 9232:             }
 9233:         }
 9234:     } else {
 9235:         $resulttext = '<span class="LC_error">'.
 9236: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9237:     }
 9238:     if ($errors) {
 9239:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 9240:                        '<ul>'.$errors.'</ul></p>';
 9241:     }
 9242:     return $resulttext;
 9243: }
 9244: 
 9245: sub process_textbook_image {
 9246:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 9247:     my $filename = $env{'form.'.$caller.'.filename'};
 9248:     my ($error,$url);
 9249:     my ($width,$height) = (50,50);
 9250:     if ($configuserok eq 'ok') {
 9251:         if ($switchserver) {
 9252:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 9253:                          $switchserver);
 9254:         } elsif ($author_ok eq 'ok') {
 9255:             my ($result,$imageurl) =
 9256:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9257:                              "$type/$dom/$cnum/cover",$width,$height);
 9258:             if ($result eq 'ok') {
 9259:                 $url = $imageurl;
 9260:             } else {
 9261:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9262:             }
 9263:         } else {
 9264:             $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);
 9265:         }
 9266:     } else {
 9267:         $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);
 9268:     }
 9269:     return ($url,$error);
 9270: }
 9271: 
 9272: sub modify_ltitools {
 9273:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 9274:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9275:     my ($newid,@allpos,%changes,%confhash,$errors,$resulttext);
 9276:     my $confname = $dom.'-domainconfig';
 9277:     my $servadm = $r->dir_config('lonAdmEMail');
 9278:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9279:     my (%posslti,%possfield);
 9280:     my @courseroles = ('cc','in','ta','ep','st');
 9281:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 9282:     map { $posslti{$_} = 1; } @ltiroles;
 9283:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
 9284:     map { $possfield{$_} = 1; } @allfields;
 9285:     my %lt = &ltitools_names(); 
 9286:     if ($env{'form.ltitools_add'}) {
 9287:         my $title = $env{'form.ltitools_add_title'};
 9288:         $title =~ s/(`)/'/g;
 9289:         ($newid,my $error) = &get_ltitools_id($dom,$title);
 9290:         if ($newid) {
 9291:             my $position = $env{'form.ltitools_add_pos'};
 9292:             $position =~ s/\D+//g;
 9293:             if ($position ne '') {
 9294:                 $allpos[$position] = $newid;
 9295:             }
 9296:             $changes{$newid} = 1;
 9297:             foreach my $item ('title','url','key','secret') {
 9298:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
 9299:                 if ($env{'form.ltitools_add_'.$item}) {
 9300:                     $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
 9301:                 }
 9302:             }
 9303:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
 9304:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
 9305:             }
 9306:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
 9307:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
 9308:             }
 9309:             foreach my $item ('width','height') {
 9310:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
 9311:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
 9312:                 if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
 9313:                     $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
 9314:                 }
 9315:             }
 9316:             if ($env{'form.ltitools_add_target'} eq 'window') {
 9317:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
 9318:             } else {
 9319:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
 9320:             }
 9321:             foreach my $item ('passback','roster') {
 9322:                 if ($env{'form.ltitools_add_'.$item}) {
 9323:                     $confhash{$newid}{$item} = 1;
 9324:                 }
 9325:             }
 9326:             if ($env{'form.ltitools_add_image.filename'} ne '') {
 9327:                 my ($imageurl,$error) =
 9328:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$dom,
 9329:                                             $configuserok,$switchserver,$author_ok);
 9330:                 if ($imageurl) {
 9331:                     $confhash{$newid}{'image'} = $imageurl;
 9332:                 }
 9333:                 if ($error) {
 9334:                     &Apache::lonnet::logthis($error);
 9335:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9336:                 }
 9337:             }
 9338:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
 9339:             foreach my $field (@fields) {
 9340:                 if ($possfield{$field}) {
 9341:                     if ($field eq 'roles') {
 9342:                         foreach my $role (@courseroles) {
 9343:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
 9344:                             if (($choice ne '') && ($posslti{$choice})) {
 9345:                                 $confhash{$newid}{'roles'}{$role} = $choice;
 9346:                                 if ($role eq 'cc') {
 9347:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
 9348:                                 }
 9349:                             }
 9350:                         }
 9351:                     } else {
 9352:                         $confhash{$newid}{'fields'}{$field} = 1;
 9353:                     }
 9354:                 }
 9355:             }
 9356:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
 9357:             foreach my $item (@courseconfig) {
 9358:                 $confhash{$newid}{'crsconf'}{$item} = 1;
 9359:             }
 9360:             if ($env{'form.ltitools_add_custom'}) {
 9361:                 my $name = $env{'form.ltitools_add_custom_name'};
 9362:                 my $value = $env{'form.ltitools_add_custom_value'};
 9363:                 $value =~ s/(`)/'/g;
 9364:                 $name =~ s/(`)/'/g;
 9365:                 $confhash{$newid}{'custom'}{$name} = $value;
 9366:             }
 9367:         } else {
 9368:             my $error = &mt('Failed to acquire unique ID for new external tool');   
 9369:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9370:         }
 9371:     }
 9372:     if (ref($domconfig{$action}) eq 'HASH') {
 9373:         my %deletions;
 9374:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
 9375:         if (@todelete) {
 9376:             map { $deletions{$_} = 1; } @todelete;
 9377:         }
 9378:         my %customadds;
 9379:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
 9380:         if (@newcustom) {
 9381:             map { $customadds{$_} = 1; } @newcustom;
 9382:         } 
 9383:         my %imgdeletions;
 9384:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
 9385:         if (@todeleteimages) {
 9386:             map { $imgdeletions{$_} = 1; } @todeleteimages;
 9387:         }
 9388:         my $maxnum = $env{'form.ltitools_maxnum'};
 9389:         for (my $i=0; $i<=$maxnum; $i++) {
 9390:             my $itemid = $env{'form.ltitools_id_'.$i};
 9391:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
 9392:                 if ($deletions{$itemid}) {
 9393:                     if ($domconfig{$action}{$itemid}{'image'}) {
 9394:                         #FIXME need to obsolete item in RES space
 9395:                     }
 9396:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
 9397:                     next;
 9398:                 } else {
 9399:                     my $newpos = $env{'form.ltitools_'.$itemid};
 9400:                     $newpos =~ s/\D+//g;
 9401:                     foreach my $item ('title','url','key','secret') {
 9402:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9403:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
 9404:                             $changes{$itemid} = 1;
 9405:                         }
 9406:                     }
 9407:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
 9408:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
 9409:                     }
 9410:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
 9411:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
 9412:                     }
 9413:                     foreach my $size ('width','height') {
 9414:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
 9415:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
 9416:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
 9417:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
 9418:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9419:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
 9420:                                     $changes{$itemid} = 1;
 9421:                                 }
 9422:                             } else {
 9423:                                 $changes{$itemid} = 1;
 9424:                             }
 9425:                         }
 9426:                     }
 9427:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
 9428:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
 9429:                     } else {
 9430:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
 9431:                     }
 9432:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9433:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
 9434:                             $changes{$itemid} = 1;
 9435:                         }
 9436:                     } else {
 9437:                         $changes{$itemid} = 1;
 9438:                     }
 9439:                     foreach my $extra ('passback','roster') {
 9440:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
 9441:                             $confhash{$itemid}{$extra} = 1;
 9442:                         }
 9443:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
 9444:                             $changes{$itemid} = 1;
 9445:                         }
 9446:                     }
 9447:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
 9448:                     foreach my $item ('label','title','target') {
 9449:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
 9450:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
 9451:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
 9452:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
 9453:                                     $changes{$itemid} = 1;
 9454:                                 }
 9455:                             } else {
 9456:                                 $changes{$itemid} = 1;
 9457:                             }
 9458:                         }
 9459:                     }
 9460:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
 9461:                     foreach my $field (@fields) {
 9462:                         if ($possfield{$field}) {
 9463:                             if ($field eq 'roles') {
 9464:                                 foreach my $role (@courseroles) {
 9465:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
 9466:                                     if (($choice ne '') && ($posslti{$choice})) {
 9467:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
 9468:                                         if ($role eq 'cc') {
 9469:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
 9470:                                         }
 9471:                                     }
 9472:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
 9473:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
 9474:                                             $changes{$itemid} = 1;
 9475:                                         }
 9476:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
 9477:                                         $changes{$itemid} = 1;
 9478:                                     }
 9479:                                 }
 9480:                             } else {
 9481:                                 $confhash{$itemid}{'fields'}{$field} = 1;
 9482:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
 9483:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
 9484:                                         $changes{$itemid} = 1;
 9485:                                     }
 9486:                                 } else {
 9487:                                     $changes{$itemid} = 1;
 9488:                                 }
 9489:                             }
 9490:                         }
 9491:                     }
 9492:                     $allpos[$newpos] = $itemid;
 9493:                 }
 9494:                 if ($imgdeletions{$itemid}) {
 9495:                     $changes{$itemid} = 1;
 9496:                     #FIXME need to obsolete item in RES space
 9497:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
 9498:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
 9499:                                                                  $itemid,$configuserok,$switchserver,
 9500:                                                                  $author_ok);
 9501:                     if ($imgurl) {
 9502:                         $confhash{$itemid}{'image'} = $imgurl;
 9503:                         $changes{$itemid} = 1;
 9504:                     }
 9505:                     if ($error) {
 9506:                         &Apache::lonnet::logthis($error);
 9507:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9508:                     }
 9509:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
 9510:                     $confhash{$itemid}{'image'} =
 9511:                        $domconfig{$action}{$itemid}{'image'};
 9512:                 }
 9513:                 if ($customadds{$i}) {
 9514:                     my $name = $env{'form.ltitools_custom_name_'.$i};
 9515:                     $name =~ s/(`)/'/g;
 9516:                     $name =~ s/^\s+//;
 9517:                     $name =~ s/\s+$//;
 9518:                     my $value = $env{'form.ltitools_custom_value_'.$i};
 9519:                     $value =~ s/(`)/'/g;
 9520:                     $value =~ s/^\s+//;
 9521:                     $value =~ s/\s+$//;
 9522:                     if ($name ne '') {
 9523:                         $confhash{$itemid}{'custom'}{$name} = $value;
 9524:                         $changes{$itemid} = 1;
 9525:                     }
 9526:                 }
 9527:                 my %customdels;
 9528:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
 9529:                 if (@customdeletions) {
 9530:                     $changes{$itemid} = 1;
 9531:                 }
 9532:                 map { $customdels{$_} = 1; } @customdeletions;
 9533:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
 9534:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
 9535:                         unless ($customdels{$key}) {
 9536:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
 9537:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
 9538:                             }
 9539:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
 9540:                                 $changes{$itemid} = 1;
 9541:                             }
 9542:                         }
 9543:                     }
 9544:                 }
 9545:                 unless ($changes{$itemid}) {
 9546:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
 9547:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
 9548:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
 9549:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
 9550:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
 9551:                                         $changes{$itemid} = 1;
 9552:                                         last;
 9553:                                     }
 9554:                                 }
 9555:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
 9556:                                 $changes{$itemid} = 1;
 9557:                             }
 9558:                         }
 9559:                         last if ($changes{$itemid});
 9560:                     }
 9561:                 }
 9562:             }
 9563:         }
 9564:     }
 9565:     if (@allpos > 0) {
 9566:         my $idx = 0;
 9567:         foreach my $itemid (@allpos) {
 9568:             if ($itemid ne '') {
 9569:                 $confhash{$itemid}{'order'} = $idx;
 9570:                 if (ref($domconfig{$action}) eq 'HASH') {
 9571:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
 9572:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
 9573:                             $changes{$itemid} = 1;
 9574:                         }
 9575:                     }
 9576:                 }
 9577:                 $idx ++;
 9578:             }
 9579:         }
 9580:     }
 9581:     my %ltitoolshash = (
 9582:                           $action => { %confhash }
 9583:                        );
 9584:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
 9585:                                              $dom);
 9586:     if ($putresult eq 'ok') {
 9587:         if (keys(%changes) > 0) {
 9588:             my $cachetime = 24*60*60;
 9589:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%confhash,$cachetime);
 9590:             if (ref($lastactref) eq 'HASH') {
 9591:                 $lastactref->{'ltitools'} = 1;
 9592:             }
 9593:             $resulttext = &mt('Changes made:').'<ul>';
 9594:             my %bynum;
 9595:             foreach my $itemid (sort(keys(%changes))) {
 9596:                 my $position = $confhash{$itemid}{'order'};
 9597:                 $bynum{$position} = $itemid;
 9598:             }
 9599:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
 9600:                 my $itemid = $bynum{$pos}; 
 9601:                 if (ref($confhash{$itemid}) ne 'HASH') {
 9602:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
 9603:                 } else {
 9604:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
 9605:                     if ($confhash{$itemid}{'image'}) {
 9606:                         $resulttext .= '&nbsp;'.
 9607:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
 9608:                                        ' alt="'.&mt('Tool Provider icon').'" />';
 9609:                     }
 9610:                     $resulttext .= '</li><ul>';
 9611:                     my $position = $pos + 1;
 9612:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
 9613:                     foreach my $item ('version','msgtype','url','key') {
 9614:                         if ($confhash{$itemid}{$item} ne '') {
 9615:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
 9616:                         }
 9617:                     }
 9618:                     if ($confhash{$itemid}{'secret'} ne '') {
 9619:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
 9620:                         my $num = length($confhash{$itemid}{'secret'});
 9621:                         $resulttext .= ('*'x$num).'</li>';
 9622:                     }
 9623:                     $resulttext .= '<li>'.&mt('Configurable in course:');
 9624:                     my @possconfig = ('label','title','target');
 9625:                     my $numconfig = 0; 
 9626:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') { 
 9627:                         foreach my $item (@possconfig) {
 9628:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
 9629:                                 $numconfig ++;
 9630:                                 $resulttext .= ' '.$lt{'crs'.$item};
 9631:                             }
 9632:                         }
 9633:                     }
 9634:                     if (!$numconfig) {
 9635:                         $resulttext .= &mt('None');
 9636:                     }
 9637:                     $resulttext .= '</li>';
 9638:                     foreach my $item ('passback','roster') {
 9639:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
 9640:                         if ($confhash{$itemid}{$item}) {
 9641:                             $resulttext .= &mt('Yes');
 9642:                         } else {
 9643:                             $resulttext .= &mt('No');
 9644:                         }
 9645:                         $resulttext .= '</li>';
 9646:                     }
 9647:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
 9648:                         my $displaylist;
 9649:                         if ($confhash{$itemid}{'display'}{'target'}) {
 9650:                             $displaylist = &mt('Display target').':&nbsp;'.
 9651:                                            $confhash{$itemid}{'display'}{'target'}.',';
 9652:                         }
 9653:                         foreach my $size ('width','height') { 
 9654:                             if ($confhash{$itemid}{'display'}{$size}) {
 9655:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
 9656:                                                 $confhash{$itemid}{'display'}{$size}.',';
 9657:                             }
 9658:                         }
 9659:                         if ($displaylist) {
 9660:                             $displaylist =~ s/,$//;
 9661:                             $resulttext .= '<li>'.$displaylist.'</li>';
 9662:                         }
 9663:                     } 
 9664:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
 9665:                         my $fieldlist;
 9666:                         foreach my $field (@allfields) {
 9667:                             if ($confhash{$itemid}{'fields'}{$field}) {
 9668:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
 9669:                             }
 9670:                         }
 9671:                         if ($fieldlist) {
 9672:                             $fieldlist =~ s/,$//;
 9673:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
 9674:                         }
 9675:                     }
 9676:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
 9677:                         my $rolemaps;
 9678:                         foreach my $role (@courseroles) {
 9679:                             if ($confhash{$itemid}{'roles'}{$role}) {
 9680:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
 9681:                                              $confhash{$itemid}{'roles'}{$role}.',';
 9682:                             }
 9683:                         }
 9684:                         if ($rolemaps) {
 9685:                             $rolemaps =~ s/,$//; 
 9686:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
 9687:                         }
 9688:                     }
 9689:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
 9690:                         my $customlist;
 9691:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
 9692:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
 9693:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
 9694:                             } 
 9695:                         }
 9696:                         if ($customlist) {
 9697:                             $resulttext .= '<li>'.&mt('Custom items').':'.$customlist.'</li>';
 9698:                         }
 9699:                     } 
 9700:                     $resulttext .= '</ul></li>';
 9701:                 }
 9702:             }
 9703:             $resulttext .= '</ul>';
 9704:         } else {
 9705:             $resulttext = &mt('No changes made.');
 9706:         }
 9707:     } else {
 9708:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
 9709:     }
 9710:     if ($errors) {
 9711:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9712:                        $errors.'</ul>';
 9713:     }
 9714:     return $resulttext;
 9715: }
 9716: 
 9717: sub process_ltitools_image {
 9718:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
 9719:     my $filename = $env{'form.'.$caller.'.filename'};
 9720:     my ($error,$url);
 9721:     my ($width,$height) = (21,21);
 9722:     if ($configuserok eq 'ok') {
 9723:         if ($switchserver) {
 9724:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
 9725:                          $switchserver);
 9726:         } elsif ($author_ok eq 'ok') {
 9727:             my ($result,$imageurl,$madethumb) =
 9728:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9729:                              "ltitools/$itemid/icon",$width,$height);
 9730:             if ($result eq 'ok') {
 9731:                 if ($madethumb) {
 9732:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
 9733:                     my $imagethumb = "$path/tn-".$imagefile;
 9734:                     $url = $imagethumb;
 9735:                 } else {
 9736:                     $url = $imageurl;
 9737:                 }
 9738:             } else {
 9739:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9740:             }
 9741:         } else {
 9742:             $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);
 9743:         }
 9744:     } else {
 9745:         $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);
 9746:     }
 9747:     return ($url,$error);
 9748: }
 9749: 
 9750: sub get_ltitools_id {
 9751:     my ($cdom,$title) = @_;
 9752:     # get lock on ltitools db
 9753:     my $lockhash = {
 9754:                       lock => $env{'user.name'}.
 9755:                               ':'.$env{'user.domain'},
 9756:                    };
 9757:     my $tries = 0;
 9758:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
 9759:     my ($id,$error);
 9760:  
 9761:     while (($gotlock ne 'ok') && ($tries<10)) {
 9762:         $tries ++;
 9763:         sleep (0.1);
 9764:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
 9765:     }
 9766:     if ($gotlock eq 'ok') {
 9767:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
 9768:         if ($currids{'lock'}) {
 9769:             delete($currids{'lock'});
 9770:             if (keys(%currids)) {
 9771:                 my @curr = sort { $a <=> $b } keys(%currids);
 9772:                 if ($curr[-1] =~ /^\d+$/) {
 9773:                     $id = 1 + $curr[-1];
 9774:                 }
 9775:             } else {
 9776:                 $id = 1;
 9777:             }
 9778:             if ($id) {
 9779:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
 9780:                     $error = 'nostore';
 9781:                 }
 9782:             } else {
 9783:                 $error = 'nonumber';
 9784:             }
 9785:         }
 9786:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
 9787:     } else {
 9788:         $error = 'nolock';
 9789:     }
 9790:     return ($id,$error);
 9791: }
 9792: 
 9793: sub modify_autoenroll {
 9794:     my ($dom,$lastactref,%domconfig) = @_;
 9795:     my ($resulttext,%changes);
 9796:     my %currautoenroll;
 9797:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 9798:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 9799:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 9800:         }
 9801:     }
 9802:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 9803:     my %title = ( run => 'Auto-enrollment active',
 9804:                   sender => 'Sender for notification messages',
 9805:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
 9806:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
 9807:     my @offon = ('off','on');
 9808:     my $sender_uname = $env{'form.sender_uname'};
 9809:     my $sender_domain = $env{'form.sender_domain'};
 9810:     if ($sender_domain eq '') {
 9811:         $sender_uname = '';
 9812:     } elsif ($sender_uname eq '') {
 9813:         $sender_domain = '';
 9814:     }
 9815:     my $coowners = $env{'form.autoassign_coowners'};
 9816:     my $failsafe = $env{'form.autoenroll_failsafe'};
 9817:     $failsafe =~ s{^\s+|\s+$}{}g;
 9818:     if ($failsafe =~ /\D/) {
 9819:         undef($failsafe);
 9820:     }
 9821:     my %autoenrollhash =  (
 9822:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 9823:                                        'sender_uname' => $sender_uname,
 9824:                                        'sender_domain' => $sender_domain,
 9825:                                        'co-owners' => $coowners,
 9826:                                        'autofailsafe' => $failsafe,
 9827:                                 }
 9828:                      );
 9829:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 9830:                                              $dom);
 9831:     if ($putresult eq 'ok') {
 9832:         if (exists($currautoenroll{'run'})) {
 9833:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 9834:                  $changes{'run'} = 1;
 9835:              }
 9836:         } elsif ($autorun) {
 9837:             if ($env{'form.autoenroll_run'} ne '1') {
 9838:                  $changes{'run'} = 1;
 9839:             }
 9840:         }
 9841:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 9842:             $changes{'sender'} = 1;
 9843:         }
 9844:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 9845:             $changes{'sender'} = 1;
 9846:         }
 9847:         if ($currautoenroll{'co-owners'} ne '') {
 9848:             if ($currautoenroll{'co-owners'} ne $coowners) {
 9849:                 $changes{'coowners'} = 1;
 9850:             }
 9851:         } elsif ($coowners) {
 9852:             $changes{'coowners'} = 1;
 9853:         }
 9854:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
 9855:             $changes{'autofailsafe'} = 1;
 9856:         }
 9857:         if (keys(%changes) > 0) {
 9858:             $resulttext = &mt('Changes made:').'<ul>';
 9859:             if ($changes{'run'}) {
 9860:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 9861:             }
 9862:             if ($changes{'sender'}) {
 9863:                 if ($sender_uname eq '' || $sender_domain eq '') {
 9864:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 9865:                 } else {
 9866:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 9867:                 }
 9868:             }
 9869:             if ($changes{'coowners'}) {
 9870:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 9871:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9872:                 if (ref($lastactref) eq 'HASH') {
 9873:                     $lastactref->{'domainconfig'} = 1;
 9874:                 }
 9875:             }
 9876:             if ($changes{'autofailsafe'}) {
 9877:                 if ($failsafe ne '') {
 9878:                     $resulttext .= '<li>'.&mt("$title{'failsafe'} set to [_1]",$failsafe).'</li>';
 9879:                 } else {
 9880:                     $resulttext .= '<li>'.&mt("$title{'failsafe'} deleted");
 9881:                 }
 9882:                 &Apache::lonnet::get_domain_defaults($dom,1);
 9883:                 if (ref($lastactref) eq 'HASH') {
 9884:                     $lastactref->{'domdefaults'} = 1;
 9885:                 }
 9886:             }
 9887:             $resulttext .= '</ul>';
 9888:         } else {
 9889:             $resulttext = &mt('No changes made to auto-enrollment settings');
 9890:         }
 9891:     } else {
 9892:         $resulttext = '<span class="LC_error">'.
 9893: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9894:     }
 9895:     return $resulttext;
 9896: }
 9897: 
 9898: sub modify_autoupdate {
 9899:     my ($dom,%domconfig) = @_;
 9900:     my ($resulttext,%currautoupdate,%fields,%changes);
 9901:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 9902:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 9903:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 9904:         }
 9905:     }
 9906:     my @offon = ('off','on');
 9907:     my %title = &Apache::lonlocal::texthash (
 9908:                    run => 'Auto-update:',
 9909:                    classlists => 'Updates to user information in classlists?'
 9910:                 );
 9911:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9912:     my %fieldtitles = &Apache::lonlocal::texthash (
 9913:                         id => 'Student/Employee ID',
 9914:                         permanentemail => 'E-mail address',
 9915:                         lastname => 'Last Name',
 9916:                         firstname => 'First Name',
 9917:                         middlename => 'Middle Name',
 9918:                         generation => 'Generation',
 9919:                       );
 9920:     $othertitle = &mt('All users');
 9921:     if (keys(%{$usertypes}) >  0) {
 9922:         $othertitle = &mt('Other users');
 9923:     }
 9924:     foreach my $key (keys(%env)) {
 9925:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 9926:             my ($usertype,$item) = ($1,$2);
 9927:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 9928:                 if ($usertype eq 'default') {   
 9929:                     push(@{$fields{$1}},$2);
 9930:                 } elsif (ref($types) eq 'ARRAY') {
 9931:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 9932:                         push(@{$fields{$1}},$2);
 9933:                     }
 9934:                 }
 9935:             }
 9936:         }
 9937:     }
 9938:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 9939:     @lockablenames = sort(@lockablenames);
 9940:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 9941:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 9942:         if (@changed) {
 9943:             $changes{'lockablenames'} = 1;
 9944:         }
 9945:     } else {
 9946:         if (@lockablenames) {
 9947:             $changes{'lockablenames'} = 1;
 9948:         }
 9949:     }
 9950:     my %updatehash = (
 9951:                       autoupdate => { run => $env{'form.autoupdate_run'},
 9952:                                       classlists => $env{'form.classlists'},
 9953:                                       fields => {%fields},
 9954:                                       lockablenames => \@lockablenames,
 9955:                                     }
 9956:                      );
 9957:     foreach my $key (keys(%currautoupdate)) {
 9958:         if (($key eq 'run') || ($key eq 'classlists')) {
 9959:             if (exists($updatehash{autoupdate}{$key})) {
 9960:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 9961:                     $changes{$key} = 1;
 9962:                 }
 9963:             }
 9964:         } elsif ($key eq 'fields') {
 9965:             if (ref($currautoupdate{$key}) eq 'HASH') {
 9966:                 foreach my $item (@{$types},'default') {
 9967:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 9968:                         my $change = 0;
 9969:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 9970:                             if (!exists($fields{$item})) {
 9971:                                 $change = 1;
 9972:                                 last;
 9973:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 9974:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 9975:                                     $change = 1;
 9976:                                     last;
 9977:                                 }
 9978:                             }
 9979:                         }
 9980:                         if ($change) {
 9981:                             push(@{$changes{$key}},$item);
 9982:                         }
 9983:                     } 
 9984:                 }
 9985:             }
 9986:         } elsif ($key eq 'lockablenames') {
 9987:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 9988:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 9989:                 if (@changed) {
 9990:                     $changes{'lockablenames'} = 1;
 9991:                 }
 9992:             } else {
 9993:                 if (@lockablenames) {
 9994:                     $changes{'lockablenames'} = 1;
 9995:                 }
 9996:             }
 9997:         }
 9998:     }
 9999:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
10000:         if (@lockablenames) {
10001:             $changes{'lockablenames'} = 1;
10002:         }
10003:     }
10004:     foreach my $item (@{$types},'default') {
10005:         if (defined($fields{$item})) {
10006:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
10007:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
10008:                     my $change = 0;
10009:                     if (ref($fields{$item}) eq 'ARRAY') {
10010:                         foreach my $type (@{$fields{$item}}) {
10011:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
10012:                                 $change = 1;
10013:                                 last;
10014:                             }
10015:                         }
10016:                     }
10017:                     if ($change) {
10018:                         push(@{$changes{'fields'}},$item);
10019:                     }
10020:                 } else {
10021:                     push(@{$changes{'fields'}},$item);
10022:                 }
10023:             } else {
10024:                 push(@{$changes{'fields'}},$item);
10025:             }
10026:         }
10027:     }
10028:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
10029:                                              $dom);
10030:     if ($putresult eq 'ok') {
10031:         if (keys(%changes) > 0) {
10032:             $resulttext = &mt('Changes made:').'<ul>';
10033:             foreach my $key (sort(keys(%changes))) {
10034:                 if ($key eq 'lockablenames') {
10035:                     $resulttext .= '<li>';
10036:                     if (@lockablenames) {
10037:                         $usertypes->{'default'} = $othertitle;
10038:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
10039:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
10040:                     } else {
10041:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
10042:                     }
10043:                     $resulttext .= '</li>';
10044:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
10045:                     foreach my $item (@{$changes{$key}}) {
10046:                         my @newvalues;
10047:                         foreach my $type (@{$fields{$item}}) {
10048:                             push(@newvalues,$fieldtitles{$type});
10049:                         }
10050:                         my $newvaluestr;
10051:                         if (@newvalues > 0) {
10052:                             $newvaluestr = join(', ',@newvalues);
10053:                         } else {
10054:                             $newvaluestr = &mt('none');
10055:                         }
10056:                         if ($item eq 'default') {
10057:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
10058:                         } else {
10059:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
10060:                         }
10061:                     }
10062:                 } else {
10063:                     my $newvalue;
10064:                     if ($key eq 'run') {
10065:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
10066:                     } else {
10067:                         $newvalue = $offon[$env{'form.'.$key}];
10068:                     }
10069:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
10070:                 }
10071:             }
10072:             $resulttext .= '</ul>';
10073:         } else {
10074:             $resulttext = &mt('No changes made to autoupdates');
10075:         }
10076:     } else {
10077:         $resulttext = '<span class="LC_error">'.
10078: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10079:     }
10080:     return $resulttext;
10081: }
10082: 
10083: sub modify_autocreate {
10084:     my ($dom,%domconfig) = @_;
10085:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
10086:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
10087:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
10088:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
10089:         }
10090:     }
10091:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
10092:                  req => 'Auto-creation of validated requests for official courses',
10093:                  xmldc => 'Identity of course creator of courses from XML files',
10094:                );
10095:     my @types = ('xml','req');
10096:     foreach my $item (@types) {
10097:         $newvals{$item} = $env{'form.autocreate_'.$item};
10098:         $newvals{$item} =~ s/\D//g;
10099:         $newvals{$item} = 0 if ($newvals{$item} eq '');
10100:     }
10101:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
10102:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
10103:     unless (exists($domcoords{$newvals{'xmldc'}})) {
10104:         $newvals{'xmldc'} = '';
10105:     } 
10106:     %autocreatehash =  (
10107:                         autocreate => { xml => $newvals{'xml'},
10108:                                         req => $newvals{'req'},
10109:                                       }
10110:                        );
10111:     if ($newvals{'xmldc'} ne '') {
10112:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
10113:     }
10114:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
10115:                                              $dom);
10116:     if ($putresult eq 'ok') {
10117:         my @items = @types;
10118:         if ($newvals{'xml'}) {
10119:             push(@items,'xmldc');
10120:         }
10121:         foreach my $item (@items) {
10122:             if (exists($currautocreate{$item})) {
10123:                 if ($currautocreate{$item} ne $newvals{$item}) {
10124:                     $changes{$item} = 1;
10125:                 }
10126:             } elsif ($newvals{$item}) {
10127:                 $changes{$item} = 1;
10128:             }
10129:         }
10130:         if (keys(%changes) > 0) {
10131:             my @offon = ('off','on'); 
10132:             $resulttext = &mt('Changes made:').'<ul>';
10133:             foreach my $item (@types) {
10134:                 if ($changes{$item}) {
10135:                     my $newtxt = $offon[$newvals{$item}];
10136:                     $resulttext .= '<li>'.
10137:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
10138:                                        '<b>','</b>').
10139:                                    '</li>';
10140:                 }
10141:             }
10142:             if ($changes{'xmldc'}) {
10143:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
10144:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
10145:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
10146:             }
10147:             $resulttext .= '</ul>';
10148:         } else {
10149:             $resulttext = &mt('No changes made to auto-creation settings');
10150:         }
10151:     } else {
10152:         $resulttext = '<span class="LC_error">'.
10153:             &mt('An error occurred: [_1]',$putresult).'</span>';
10154:     }
10155:     return $resulttext;
10156: }
10157: 
10158: sub modify_directorysrch {
10159:     my ($dom,%domconfig) = @_;
10160:     my ($resulttext,%changes);
10161:     my %currdirsrch;
10162:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
10163:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
10164:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
10165:         }
10166:     }
10167:     my %title = ( available => 'Institutional directory search available',
10168:                   localonly => 'Other domains can search institution',
10169:                   lcavailable => 'LON-CAPA directory search available',
10170:                   lclocalonly => 'Other domains can search LON-CAPA domain', 
10171:                   searchby => 'Search types',
10172:                   searchtypes => 'Search latitude');
10173:     my @offon = ('off','on');
10174:     my @otherdoms = ('Yes','No');
10175: 
10176:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
10177:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
10178:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
10179: 
10180:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10181:     if (keys(%{$usertypes}) == 0) {
10182:         @cansearch = ('default');
10183:     } else {
10184:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
10185:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
10186:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
10187:                     push(@{$changes{'cansearch'}},$type);
10188:                 }
10189:             }
10190:             foreach my $type (@cansearch) {
10191:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
10192:                     push(@{$changes{'cansearch'}},$type);
10193:                 }
10194:             }
10195:         } else {
10196:             push(@{$changes{'cansearch'}},@cansearch);
10197:         }
10198:     }
10199: 
10200:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
10201:         foreach my $by (@{$currdirsrch{'searchby'}}) {
10202:             if (!grep(/^\Q$by\E$/,@searchby)) {
10203:                 push(@{$changes{'searchby'}},$by);
10204:             }
10205:         }
10206:         foreach my $by (@searchby) {
10207:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
10208:                 push(@{$changes{'searchby'}},$by);
10209:             }
10210:         }
10211:     } else {
10212:         push(@{$changes{'searchby'}},@searchby);
10213:     }
10214: 
10215:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
10216:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
10217:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
10218:                 push(@{$changes{'searchtypes'}},$type);
10219:             }
10220:         }
10221:         foreach my $type (@searchtypes) {
10222:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
10223:                 push(@{$changes{'searchtypes'}},$type);
10224:             }
10225:         }
10226:     } else {
10227:         if (exists($currdirsrch{'searchtypes'})) {
10228:             foreach my $type (@searchtypes) {  
10229:                 if ($type ne $currdirsrch{'searchtypes'}) { 
10230:                     push(@{$changes{'searchtypes'}},$type);
10231:                 }
10232:             }
10233:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
10234:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
10235:             }   
10236:         } else {
10237:             push(@{$changes{'searchtypes'}},@searchtypes); 
10238:         }
10239:     }
10240: 
10241:     my %dirsrch_hash =  (
10242:             directorysrch => { available => $env{'form.dirsrch_available'},
10243:                                cansearch => \@cansearch,
10244:                                localonly => $env{'form.dirsrch_instlocalonly'},
10245:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
10246:                                lcavailable => $env{'form.dirsrch_domavailable'},
10247:                                searchby => \@searchby,
10248:                                searchtypes => \@searchtypes,
10249:                              }
10250:             );
10251:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
10252:                                              $dom);
10253:     if ($putresult eq 'ok') {
10254:         if (exists($currdirsrch{'available'})) {
10255:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
10256:                  $changes{'available'} = 1;
10257:              }
10258:         } else {
10259:             if ($env{'form.dirsrch_available'} eq '1') {
10260:                 $changes{'available'} = 1;
10261:             }
10262:         }
10263:         if (exists($currdirsrch{'lcavailable'})) {
10264:              if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
10265:                  $changes{'lcavailable'} = 1;
10266:              }
10267:         } else {
10268:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
10269:                 $changes{'lcavailable'} = 1;
10270:             }
10271:         }
10272:         if (exists($currdirsrch{'localonly'})) {
10273:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
10274:                  $changes{'localonly'} = 1;
10275:              }
10276:         } else {
10277:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
10278:                 $changes{'localonly'} = 1;
10279:             }
10280:         }
10281:         if (exists($currdirsrch{'lclocalonly'})) {
10282:              if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
10283:                  $changes{'lclocalonly'} = 1;
10284:              }
10285:         } else {
10286:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
10287:                 $changes{'lclocalonly'} = 1;
10288:             }
10289:         }
10290:         if (keys(%changes) > 0) {
10291:             $resulttext = &mt('Changes made:').'<ul>';
10292:             if ($changes{'available'}) {
10293:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
10294:             }
10295:             if ($changes{'lcavailable'}) {
10296:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
10297:             }
10298:             if ($changes{'localonly'}) {
10299:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
10300:             }
10301:             if ($changes{'lclocalonly'}) {
10302:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
10303:             }   
10304:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
10305:                 my $chgtext;
10306:                 if (ref($usertypes) eq 'HASH') {
10307:                     if (keys(%{$usertypes}) > 0) {
10308:                         foreach my $type (@{$types}) {
10309:                             if (grep(/^\Q$type\E$/,@cansearch)) {
10310:                                 $chgtext .= $usertypes->{$type}.'; ';
10311:                             }
10312:                         }
10313:                         if (grep(/^default$/,@cansearch)) {
10314:                             $chgtext .= $othertitle;
10315:                         } else {
10316:                             $chgtext =~ s/\; $//;
10317:                         }
10318:                         $resulttext .=
10319:                             '<li>'.
10320:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
10321:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
10322:                             '</li>';
10323:                     }
10324:                 }
10325:             }
10326:             if (ref($changes{'searchby'}) eq 'ARRAY') {
10327:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
10328:                 my $chgtext;
10329:                 foreach my $type (@{$titleorder}) {
10330:                     if (grep(/^\Q$type\E$/,@searchby)) {
10331:                         if (defined($searchtitles->{$type})) {
10332:                             $chgtext .= $searchtitles->{$type}.'; ';
10333:                         }
10334:                     }
10335:                 }
10336:                 $chgtext =~ s/\; $//;
10337:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
10338:             }
10339:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
10340:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
10341:                 my $chgtext;
10342:                 foreach my $type (@{$srchtypeorder}) {
10343:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
10344:                         if (defined($srchtypes_desc->{$type})) {
10345:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
10346:                         }
10347:                     }
10348:                 }
10349:                 $chgtext =~ s/\; $//;
10350:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
10351:             }
10352:             $resulttext .= '</ul>';
10353:         } else {
10354:             $resulttext = &mt('No changes made to directory search settings');
10355:         }
10356:     } else {
10357:         $resulttext = '<span class="LC_error">'.
10358:                       &mt('An error occurred: [_1]',$putresult).'</span>';
10359:     }
10360:     return $resulttext;
10361: }
10362: 
10363: sub modify_contacts {
10364:     my ($dom,$lastactref,%domconfig) = @_;
10365:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
10366:     if (ref($domconfig{'contacts'}) eq 'HASH') {
10367:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
10368:             $currsetting{$key} = $domconfig{'contacts'}{$key};
10369:         }
10370:     }
10371:     my (%others,%to,%bcc,%includestr,%includeloc);
10372:     my @contacts = ('supportemail','adminemail');
10373:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
10374:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail');
10375:     my @toggles = ('reporterrors','reportupdates');
10376:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
10377:     foreach my $type (@mailings) {
10378:         @{$newsetting{$type}} = 
10379:             &Apache::loncommon::get_env_multiple('form.'.$type);
10380:         foreach my $item (@contacts) {
10381:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
10382:                 $contacts_hash{contacts}{$type}{$item} = 1;
10383:             } else {
10384:                 $contacts_hash{contacts}{$type}{$item} = 0;
10385:             }
10386:         }  
10387:         $others{$type} = $env{'form.'.$type.'_others'};
10388:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
10389:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10390:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
10391:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
10392:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
10393:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
10394:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
10395:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
10396:             }
10397:         }
10398:     }
10399:     foreach my $item (@contacts) {
10400:         $to{$item} = $env{'form.'.$item};
10401:         $contacts_hash{'contacts'}{$item} = $to{$item};
10402:     }
10403:     foreach my $item (@toggles) {
10404:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
10405:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
10406:         }
10407:     }
10408:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
10409:         foreach my $field (@{$fields}) {
10410:             if (ref($possoptions->{$field}) eq 'ARRAY') {
10411:                 my $value = $env{'form.helpform_'.$field};
10412:                 $value =~ s/^\s+|\s+$//g;
10413:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
10414:                     $contacts_hash{contacts}{'helpform'}{$field} = $value;
10415:                     if ($field eq 'screenshot') {
10416:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
10417:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
10418:                             $contacts_hash{contacts}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
10419:                         }
10420:                     }
10421:                 }
10422:             }
10423:         }
10424:     }
10425:     if (keys(%currsetting) > 0) {
10426:         foreach my $item (@contacts) {
10427:             if ($to{$item} ne $currsetting{$item}) {
10428:                 $changes{$item} = 1;
10429:             }
10430:         }
10431:         foreach my $type (@mailings) {
10432:             foreach my $item (@contacts) {
10433:                 if (ref($currsetting{$type}) eq 'HASH') {
10434:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
10435:                         push(@{$changes{$type}},$item);
10436:                     }
10437:                 } else {
10438:                     push(@{$changes{$type}},@{$newsetting{$type}});
10439:                 }
10440:             }
10441:             if ($others{$type} ne $currsetting{$type}{'others'}) {
10442:                 push(@{$changes{$type}},'others');
10443:             }
10444:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {   
10445:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
10446:                     push(@{$changes{$type}},'bcc'); 
10447:                 }
10448:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
10449:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
10450:                     push(@{$changes{$type}},'include');
10451:                 }
10452:             }
10453:         }
10454:         if (ref($fields) eq 'ARRAY') {
10455:             if (ref($currsetting{'helpform'}) eq 'HASH') {
10456:                 foreach my $field (@{$fields}) {
10457:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
10458:                         push(@{$changes{'helpform'}},$field);
10459:                     }
10460:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10461:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
10462:                             push(@{$changes{'helpform'}},'maxsize');
10463:                         }
10464:                     }
10465:                 }
10466:             } else {
10467:                 foreach my $field (@{$fields}) {
10468:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10469:                         push(@{$changes{'helpform'}},$field);
10470:                     }
10471:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10472:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10473:                             push(@{$changes{'helpform'}},'maxsize');
10474:                         }
10475:                     }
10476:                 }
10477:             }
10478:         }
10479:     } else {
10480:         my %default;
10481:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
10482:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
10483:         $default{'errormail'} = 'adminemail';
10484:         $default{'packagesmail'} = 'adminemail';
10485:         $default{'helpdeskmail'} = 'supportemail';
10486:         $default{'otherdomsmail'} = 'supportemail';
10487:         $default{'lonstatusmail'} = 'adminemail';
10488:         $default{'requestsmail'} = 'adminemail';
10489:         $default{'updatesmail'} = 'adminemail';
10490:         foreach my $item (@contacts) {
10491:            if ($to{$item} ne $default{$item}) {
10492:                $changes{$item} = 1;
10493:            }
10494:         }
10495:         foreach my $type (@mailings) {
10496:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
10497:                 push(@{$changes{$type}},@{$newsetting{$type}});
10498:             }
10499:             if ($others{$type} ne '') {
10500:                 push(@{$changes{$type}},'others');
10501:             }
10502:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10503:                 if ($bcc{$type} ne '') {
10504:                     push(@{$changes{$type}},'bcc');
10505:                 }
10506:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
10507:                     push(@{$changes{$type}},'include');
10508:                 }
10509:             }
10510:         }
10511:         if (ref($fields) eq 'ARRAY') {
10512:             foreach my $field (@{$fields}) {
10513:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10514:                     push(@{$changes{'helpform'}},$field);
10515:                 }
10516:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10517:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10518:                         push(@{$changes{'helpform'}},'maxsize');
10519:                     }
10520:                 }
10521:             }
10522:         }   
10523:     }
10524:     foreach my $item (@toggles) {
10525:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
10526:             $changes{$item} = 1;
10527:         } elsif ((!$env{'form.'.$item}) &&
10528:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
10529:             $changes{$item} = 1;
10530:         }
10531:     }
10532:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
10533:                                              $dom);
10534:     if ($putresult eq 'ok') {
10535:         if (keys(%changes) > 0) {
10536:             &Apache::loncommon::devalidate_domconfig_cache($dom);
10537:             if (ref($lastactref) eq 'HASH') {
10538:                 $lastactref->{'domainconfig'} = 1;
10539:             }
10540:             my ($titles,$short_titles)  = &contact_titles();
10541:             $resulttext = &mt('Changes made:').'<ul>';
10542:             foreach my $item (@contacts) {
10543:                 if ($changes{$item}) {
10544:                     $resulttext .= '<li>'.$titles->{$item}.
10545:                                     &mt(' set to: ').
10546:                                     '<span class="LC_cusr_emph">'.
10547:                                     $to{$item}.'</span></li>';
10548:                 }
10549:             }
10550:             foreach my $type (@mailings) {
10551:                 if (ref($changes{$type}) eq 'ARRAY') {
10552:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10553:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': '; 
10554:                     } else {
10555:                         $resulttext .= '<li>'.$titles->{$type}.': ';
10556:                     }
10557:                     my @text;
10558:                     foreach my $item (@{$newsetting{$type}}) {
10559:                         push(@text,$short_titles->{$item});
10560:                     }
10561:                     if ($others{$type} ne '') {
10562:                         push(@text,$others{$type});
10563:                     }
10564:                     if (@text) {
10565:                         $resulttext .= '<span class="LC_cusr_emph">'.
10566:                                        join(', ',@text).'</span>';
10567:                     }
10568:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10569:                         if ($bcc{$type} ne '') {
10570:                             my $bcctext;
10571:                             if (@text) {
10572:                                 $bcctext = '&nbsp;'.&mt('with Bcc to'); 
10573:                             } else {
10574:                                 $bcctext = '(Bcc)';
10575:                             }
10576:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
10577:                         } elsif (!@text) {
10578:                             $resulttext .= &mt('No one');
10579:                         }   
10580:                         if ($includestr{$type} ne '') { 
10581:                             if ($includeloc{$type} eq 'b') {
10582:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
10583:                             } elsif ($includeloc{$type} eq 's') {
10584:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
10585:                             }
10586:                         }
10587:                     } elsif (!@text) {
10588:                         $resulttext .= &mt('No recipients');
10589:                     }
10590:                     $resulttext .= '</li>';
10591:                 }
10592:             }
10593:             my @offon = ('off','on');
10594:             if ($changes{'reporterrors'}) {
10595:                 $resulttext .= '<li>'.
10596:                                &mt('E-mail error reports to [_1] set to "'.
10597:                                    $offon[$env{'form.reporterrors'}].'".',
10598:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10599:                                        &mt('LON-CAPA core group - MSU'),600,500)).
10600:                                '</li>';
10601:             }
10602:             if ($changes{'reportupdates'}) {
10603:                 $resulttext .= '<li>'.
10604:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
10605:                                     $offon[$env{'form.reportupdates'}].'".',
10606:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10607:                                         &mt('LON-CAPA core group - MSU'),600,500)).
10608:                                 '</li>';
10609:             }
10610:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
10611:                 my (@optional,@required,@unused,$maxsizechg);
10612:                 foreach my $field (@{$changes{'helpform'}}) {
10613:                     if ($field eq 'maxsize') {
10614:                         $maxsizechg = 1;
10615:                         next;
10616:                     }
10617:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
10618:                         push(@optional,$field);    
10619:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
10620:                         push(@unused,$field);
10621:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
10622:                         push(@required,$field);   
10623:                     }
10624:                 }
10625:                 if (@optional) {
10626:                     $resulttext .= '<li>'.
10627:                                    &mt('Help form fields changed to "Optional": [_1].',
10628:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
10629:                                    '</li>';
10630:                 }
10631:                 if (@required) {
10632:                     $resulttext .= '<li>'.
10633:                                    &mt('Help form fields changed to "Required": [_1].',
10634:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
10635:                                    '</li>';
10636:                 }
10637:                 if (@unused) {
10638:                     $resulttext .= '<li>'.
10639:                                    &mt('Help form fields changed to "Not shown": [_1].',
10640:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
10641:                                    '</li>';
10642:                 }
10643:                 if ($maxsizechg) {
10644:                     $resulttext .= '<li>'.
10645:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
10646:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
10647:                                    '</li>';
10648: 
10649:                 }
10650:             }
10651:             $resulttext .= '</ul>';
10652:         } else {
10653:             $resulttext = &mt('No changes made to contact information');
10654:         }
10655:     } else {
10656:         $resulttext = '<span class="LC_error">'.
10657:             &mt('An error occurred: [_1].',$putresult).'</span>';
10658:     }
10659:     return $resulttext;
10660: }
10661: 
10662: sub modify_usercreation {
10663:     my ($dom,%domconfig) = @_;
10664:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
10665:     my $warningmsg;
10666:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
10667:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
10668:             if ($key eq 'cancreate') {
10669:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
10670:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
10671:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
10672:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
10673:                             ($item eq 'recaptchaversion')) {
10674:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10675:                         } else {
10676:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10677:                         }
10678:                     }
10679:                 }
10680:             } elsif ($key eq 'email_rule') {
10681:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
10682:             } else {
10683:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
10684:             }
10685:         }
10686:     }
10687:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
10688:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
10689:     my @contexts = ('author','course','requestcrs');
10690:     foreach my $item(@contexts) {
10691:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
10692:     }
10693:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
10694:         foreach my $item (@contexts) {
10695:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
10696:                 push(@{$changes{'cancreate'}},$item);
10697:             }
10698:         }
10699:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
10700:         foreach my $item (@contexts) {
10701:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
10702:                 if ($cancreate{$item} ne 'any') {
10703:                     push(@{$changes{'cancreate'}},$item);
10704:                 }
10705:             } else {
10706:                 if ($cancreate{$item} ne 'none') {
10707:                     push(@{$changes{'cancreate'}},$item);
10708:                 }
10709:             }
10710:         }
10711:     } else {
10712:         foreach my $item (@contexts)  {
10713:             push(@{$changes{'cancreate'}},$item);
10714:         }
10715:     }
10716: 
10717:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
10718:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
10719:             if (!grep(/^\Q$type\E$/,@username_rule)) {
10720:                 push(@{$changes{'username_rule'}},$type);
10721:             }
10722:         }
10723:         foreach my $type (@username_rule) {
10724:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
10725:                 push(@{$changes{'username_rule'}},$type);
10726:             }
10727:         }
10728:     } else {
10729:         push(@{$changes{'username_rule'}},@username_rule);
10730:     }
10731: 
10732:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
10733:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
10734:             if (!grep(/^\Q$type\E$/,@id_rule)) {
10735:                 push(@{$changes{'id_rule'}},$type);
10736:             }
10737:         }
10738:         foreach my $type (@id_rule) {
10739:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
10740:                 push(@{$changes{'id_rule'}},$type);
10741:             }
10742:         }
10743:     } else {
10744:         push(@{$changes{'id_rule'}},@id_rule);
10745:     }
10746: 
10747:     my @authen_contexts = ('author','course','domain');
10748:     my @authtypes = ('int','krb4','krb5','loc');
10749:     my %authhash;
10750:     foreach my $item (@authen_contexts) {
10751:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
10752:         foreach my $auth (@authtypes) {
10753:             if (grep(/^\Q$auth\E$/,@authallowed)) {
10754:                 $authhash{$item}{$auth} = 1;
10755:             } else {
10756:                 $authhash{$item}{$auth} = 0;
10757:             }
10758:         }
10759:     }
10760:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
10761:         foreach my $item (@authen_contexts) {
10762:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
10763:                 foreach my $auth (@authtypes) {
10764:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
10765:                         push(@{$changes{'authtypes'}},$item);
10766:                         last;
10767:                     }
10768:                 }
10769:             }
10770:         }
10771:     } else {
10772:         foreach my $item (@authen_contexts) {
10773:             push(@{$changes{'authtypes'}},$item);
10774:         }
10775:     }
10776: 
10777:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
10778:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
10779:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
10780:     $save_usercreate{'id_rule'} = \@id_rule;
10781:     $save_usercreate{'username_rule'} = \@username_rule,
10782:     $save_usercreate{'authtypes'} = \%authhash;
10783: 
10784:     my %usercreation_hash =  (
10785:         usercreation     => \%save_usercreate,
10786:     );
10787: 
10788:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
10789:                                              $dom);
10790: 
10791:     if ($putresult eq 'ok') {
10792:         if (keys(%changes) > 0) {
10793:             $resulttext = &mt('Changes made:').'<ul>';
10794:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
10795:                 my %lt = &usercreation_types();
10796:                 foreach my $type (@{$changes{'cancreate'}}) {
10797:                     my $chgtext = $lt{$type}.', ';
10798:                     if ($cancreate{$type} eq 'none') {
10799:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
10800:                     } elsif ($cancreate{$type} eq 'any') {
10801:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
10802:                     } elsif ($cancreate{$type} eq 'official') {
10803:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
10804:                     } elsif ($cancreate{$type} eq 'unofficial') {
10805:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
10806:                     }
10807:                     $resulttext .= '<li>'.$chgtext.'</li>';
10808:                 }
10809:             }
10810:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
10811:                 my ($rules,$ruleorder) = 
10812:                     &Apache::lonnet::inst_userrules($dom,'username');
10813:                 my $chgtext = '<ul>';
10814:                 foreach my $type (@username_rule) {
10815:                     if (ref($rules->{$type}) eq 'HASH') {
10816:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
10817:                     }
10818:                 }
10819:                 $chgtext .= '</ul>';
10820:                 if (@username_rule > 0) {
10821:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
10822:                 } else {
10823:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
10824:                 }
10825:             }
10826:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
10827:                 my ($idrules,$idruleorder) = 
10828:                     &Apache::lonnet::inst_userrules($dom,'id');
10829:                 my $chgtext = '<ul>';
10830:                 foreach my $type (@id_rule) {
10831:                     if (ref($idrules->{$type}) eq 'HASH') {
10832:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
10833:                     }
10834:                 }
10835:                 $chgtext .= '</ul>';
10836:                 if (@id_rule > 0) {
10837:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
10838:                 } else {
10839:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
10840:                 }
10841:             }
10842:             my %authname = &authtype_names();
10843:             my %context_title = &context_names();
10844:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
10845:                 my $chgtext = '<ul>';
10846:                 foreach my $type (@{$changes{'authtypes'}}) {
10847:                     my @allowed;
10848:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
10849:                     foreach my $auth (@authtypes) {
10850:                         if ($authhash{$type}{$auth}) {
10851:                             push(@allowed,$authname{$auth});
10852:                         }
10853:                     }
10854:                     if (@allowed > 0) {
10855:                         $chgtext .= join(', ',@allowed).'</li>';
10856:                     } else {
10857:                         $chgtext .= &mt('none').'</li>';
10858:                     }
10859:                 }
10860:                 $chgtext .= '</ul>';
10861:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
10862:                 $resulttext .= '</li>';
10863:             }
10864:             $resulttext .= '</ul>';
10865:         } else {
10866:             $resulttext = &mt('No changes made to user creation settings');
10867:         }
10868:     } else {
10869:         $resulttext = '<span class="LC_error">'.
10870:             &mt('An error occurred: [_1]',$putresult).'</span>';
10871:     }
10872:     if ($warningmsg ne '') {
10873:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
10874:     }
10875:     return $resulttext;
10876: }
10877: 
10878: sub modify_selfcreation {
10879:     my ($dom,%domconfig) = @_;
10880:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
10881:     my (%save_usercreate,%save_usermodify);
10882:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10883:     if (ref($types) eq 'ARRAY') {
10884:         $usertypes->{'default'} = $othertitle;
10885:         push(@{$types},'default');
10886:     }
10887: #
10888: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
10889: #
10890:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
10891:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
10892:             if ($key eq 'cancreate') {
10893:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
10894:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
10895:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
10896:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
10897:                             ($item eq 'recaptchaversion') ||
10898:                             ($item eq 'emailusername') || ($item eq 'notify') ||
10899:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
10900:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10901:                         } else {
10902:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10903:                         }
10904:                     }
10905:                 }
10906:             } elsif ($key eq 'email_rule') {
10907:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
10908:             } else {
10909:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
10910:             }
10911:         }
10912:     }
10913: #
10914: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
10915: #
10916:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
10917:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
10918:             if ($key eq 'selfcreate') {
10919:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
10920:             } else {
10921:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
10922:             }
10923:         }
10924:     }
10925: 
10926:     my @contexts = ('selfcreate');
10927:     @{$cancreate{'selfcreate'}} = ();
10928:     %{$cancreate{'emailusername'}} = ();
10929:     @{$cancreate{'statustocreate'}} = ();
10930:     %{$cancreate{'selfcreateprocessing'}} = ();
10931:     %{$cancreate{'shibenv'}} = ();
10932:     my %selfcreatetypes = (
10933:                              sso   => 'users authenticated by institutional single sign on',
10934:                              login => 'users authenticated by institutional log-in',
10935:                              email => 'users who provide a valid e-mail address for use as username',
10936:                           );
10937: #
10938: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
10939: # is permitted.
10940: #
10941: 
10942:     my @statuses;
10943:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
10944:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
10945:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
10946:         }
10947:     }
10948:     push(@statuses,'default');
10949: 
10950:     foreach my $item ('login','sso','email') {
10951:         if ($item eq 'email') {
10952:             if ($env{'form.cancreate_email'}) {
10953:                 push(@{$cancreate{'selfcreate'}},'email');
10954:                 push(@contexts,'selfcreateprocessing');
10955:                 foreach my $type (@statuses) {
10956:                     if ($type eq 'default') {
10957:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
10958:                     } else { 
10959:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
10960:                     }
10961:                 }
10962:             }
10963:         } else {
10964:             if ($env{'form.cancreate_'.$item}) {
10965:                 push(@{$cancreate{'selfcreate'}},$item);
10966:             }
10967:         }
10968:     }
10969:     my (@email_rule,%userinfo,%savecaptcha);
10970:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
10971: #
10972: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
10973: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
10974: #
10975: 
10976:     if ($env{'form.cancreate_email'}) {
10977:         push(@contexts,'emailusername');
10978:         if (ref($types) eq 'ARRAY') {
10979:             foreach my $type (@{$types}) {
10980:                 if (ref($infofields) eq 'ARRAY') {
10981:                     foreach my $field (@{$infofields}) {
10982:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
10983:                             $cancreate{'emailusername'}{$type}{$field} = $1;
10984:                         }
10985:                     }
10986:                 }
10987:             }
10988:         }
10989: #
10990: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
10991: # queued requests for self-creation of account using e-mail address as username
10992: #
10993: 
10994:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
10995:         @approvalnotify = sort(@approvalnotify);
10996:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
10997:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
10998:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
10999:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
11000:                     push(@{$changes{'cancreate'}},'notify');
11001:                 }
11002:             } else {
11003:                 if ($cancreate{'notify'}{'approval'}) {
11004:                     push(@{$changes{'cancreate'}},'notify');
11005:                 }
11006:             }
11007:         } elsif ($cancreate{'notify'}{'approval'}) {
11008:             push(@{$changes{'cancreate'}},'notify');
11009:         }
11010: 
11011: #
11012: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
11013: #
11014:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
11015:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
11016:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
11017:             if (@{$curr_usercreation{'email_rule'}} > 0) {
11018:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
11019:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
11020:                         push(@{$changes{'email_rule'}},$type);
11021:                     }
11022:                 }
11023:             }
11024:             if (@email_rule > 0) {
11025:                 foreach my $type (@email_rule) {
11026:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
11027:                         push(@{$changes{'email_rule'}},$type);
11028:                     }
11029:                 }
11030:             }
11031:         } elsif (@email_rule > 0) {
11032:             push(@{$changes{'email_rule'}},@email_rule);
11033:         }
11034:     }
11035: #  
11036: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
11037: # institutional log-in.
11038: #
11039:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
11040:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11041:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
11042:                ($domdefaults{'auth_def'} eq 'localauth'))) {
11043:             $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.').' '.
11044:                           &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.');
11045:         }
11046:     }
11047:     my @fields = ('lastname','firstname','middlename','generation',
11048:                   'permanentemail','id');
11049:     my @shibfields = (@fields,'inststatus');
11050:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11051: #
11052: # Where usernames may created for institutional log-in and/or institutional single sign on:
11053: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
11054: # may self-create accounts 
11055: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
11056: # which the user may supply, if institutional data is unavailable.
11057: #
11058:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
11059:         if (ref($types) eq 'ARRAY') {
11060:             if (@{$types} > 1) {
11061:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
11062:                 push(@contexts,'statustocreate');
11063:             } else {
11064:                 undef($cancreate{'statustocreate'});
11065:             } 
11066:             foreach my $type (@{$types}) {
11067:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
11068:                 foreach my $field (@fields) {
11069:                     if (grep(/^\Q$field\E$/,@modifiable)) {
11070:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
11071:                     } else {
11072:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
11073:                     }
11074:                 }
11075:             }
11076:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
11077:                 foreach my $type (@{$types}) {
11078:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
11079:                         foreach my $field (@fields) {
11080:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
11081:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
11082:                                 push(@{$changes{'selfcreate'}},$type);
11083:                                 last;
11084:                             }
11085:                         }
11086:                     }
11087:                 }
11088:             } else {
11089:                 foreach my $type (@{$types}) {
11090:                     push(@{$changes{'selfcreate'}},$type);
11091:                 }
11092:             }
11093:         }
11094:         foreach my $field (@shibfields) {
11095:             if ($env{'form.shibenv_'.$field} ne '') {
11096:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
11097:             }
11098:         }
11099:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11100:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
11101:                 foreach my $field (@shibfields) {
11102:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
11103:                         push(@{$changes{'cancreate'}},'shibenv');
11104:                     }
11105:                 }
11106:             } else {
11107:                 foreach my $field (@shibfields) {
11108:                     if ($env{'form.shibenv_'.$field}) {
11109:                         push(@{$changes{'cancreate'}},'shibenv');
11110:                         last;
11111:                     }
11112:                 }
11113:             }
11114:         }
11115:     }
11116:     foreach my $item (@contexts) {
11117:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
11118:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
11119:                 if (ref($cancreate{$item}) eq 'ARRAY') {
11120:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
11121:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11122:                             push(@{$changes{'cancreate'}},$item);
11123:                         }
11124:                     }
11125:                 }
11126:             }
11127:             if (ref($cancreate{$item}) eq 'ARRAY') {
11128:                 foreach my $type (@{$cancreate{$item}}) {
11129:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
11130:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11131:                             push(@{$changes{'cancreate'}},$item);
11132:                         }
11133:                     }
11134:                 }
11135:             }
11136:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
11137:             if (ref($cancreate{$item}) eq 'HASH') {
11138:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
11139:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
11140:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
11141:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
11142:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11143:                                     push(@{$changes{'cancreate'}},$item);
11144:                                 }
11145:                             }
11146:                         }
11147:                     } elsif ($item eq 'selfcreateprocessing') {
11148:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
11149:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11150:                                 push(@{$changes{'cancreate'}},$item);
11151:                             }
11152:                         }
11153:                     } else {
11154:                         if (!$cancreate{$item}{$curr}) {
11155:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11156:                                 push(@{$changes{'cancreate'}},$item);
11157:                             }
11158:                         }
11159:                     }
11160:                 }
11161:                 foreach my $field (keys(%{$cancreate{$item}})) {
11162:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
11163:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
11164:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
11165:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
11166:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11167:                                         push(@{$changes{'cancreate'}},$item);
11168:                                     }
11169:                                 }
11170:                             } else {
11171:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11172:                                     push(@{$changes{'cancreate'}},$item);
11173:                                 }
11174:                             }
11175:                         }
11176:                     } elsif ($item eq 'selfcreateprocessing') {
11177:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
11178:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11179:                                 push(@{$changes{'cancreate'}},$item);
11180:                             }
11181:                         }
11182:                     } else {
11183:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
11184:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11185:                                 push(@{$changes{'cancreate'}},$item);
11186:                             }
11187:                         }
11188:                     }
11189:                 }
11190:             }
11191:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
11192:             if (ref($cancreate{$item}) eq 'ARRAY') {
11193:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
11194:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11195:                         push(@{$changes{'cancreate'}},$item);
11196:                     }
11197:                 }
11198:             } elsif (ref($cancreate{$item}) eq 'HASH') {
11199:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
11200:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11201:                         push(@{$changes{'cancreate'}},$item);
11202:                     }
11203:                 }
11204:             }
11205:         } elsif ($item eq 'emailusername') {
11206:             if (ref($cancreate{$item}) eq 'HASH') {
11207:                 foreach my $type (keys(%{$cancreate{$item}})) {
11208:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
11209:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
11210:                             if ($cancreate{$item}{$type}{$field}) {
11211:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11212:                                     push(@{$changes{'cancreate'}},$item);
11213:                                 }
11214:                                 last;
11215:                             }
11216:                         }
11217:                     }
11218:                 }
11219:             }
11220:         }
11221:     }
11222: #
11223: # Populate %save_usercreate hash with updates to self-creation configuration.
11224: #
11225:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
11226:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
11227:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
11228:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
11229:     if (ref($cancreate{'notify'}) eq 'HASH') {
11230:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
11231:     }
11232:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
11233:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
11234:     }
11235:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11236:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
11237:     }
11238:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
11239:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
11240:     }
11241:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
11242:     $save_usercreate{'emailrule'} = \@email_rule;
11243: 
11244:     my %userconfig_hash = (
11245:             usercreation     => \%save_usercreate,
11246:             usermodification => \%save_usermodify,
11247:     );
11248:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
11249:                                              $dom);
11250: #
11251: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
11252: #
11253:     if ($putresult eq 'ok') {
11254:         if (keys(%changes) > 0) {
11255:             $resulttext = &mt('Changes made:').'<ul>';
11256:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11257:                 my %lt = &selfcreation_types();
11258:                 foreach my $type (@{$changes{'cancreate'}}) {
11259:                     my $chgtext;
11260:                     if ($type eq 'selfcreate') {
11261:                         if (@{$cancreate{$type}} == 0) {
11262:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
11263:                         } else {
11264:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
11265:                                         '<ul>';
11266:                             foreach my $case (@{$cancreate{$type}}) {
11267:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
11268:                             }
11269:                             $chgtext .= '</ul>';
11270:                             if (ref($cancreate{$type}) eq 'ARRAY') {
11271:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
11272:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11273:                                         if (@{$cancreate{'statustocreate'}} == 0) {
11274:                                             $chgtext .= '<br />'.
11275:                                                         '<span class="LC_warning">'.
11276:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
11277:                                                         '</span>';
11278:                                         }
11279:                                     }
11280:                                 }
11281:                             }
11282:                         }
11283:                     } elsif ($type eq 'shibenv') {
11284:                         if (keys(%{$cancreate{$type}}) == 0) {
11285:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
11286:                         } else {
11287:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
11288:                                         '<ul>';
11289:                             foreach my $field (@shibfields) {
11290:                                 next if ($cancreate{$type}{$field} eq '');
11291:                                 if ($field eq 'inststatus') {
11292:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
11293:                                 } else {
11294:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
11295:                                 }
11296:                             }
11297:                             $chgtext .= '</ul>';
11298:                         }  
11299:                     } elsif ($type eq 'statustocreate') {
11300:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
11301:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
11302:                             if (@{$cancreate{'selfcreate'}} > 0) {
11303:                                 if (@{$cancreate{'statustocreate'}} == 0) {
11304:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
11305:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
11306:                                         $chgtext .= '<br />'.
11307:                                                     '<span class="LC_warning">'.
11308:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
11309:                                                     '</span>';
11310:                                     }
11311:                                 } elsif (ref($usertypes) eq 'HASH') {
11312:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
11313:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
11314:                                     } else {
11315:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
11316:                                     }
11317:                                     $chgtext .= '<ul>';
11318:                                     foreach my $case (@{$cancreate{$type}}) {
11319:                                         if ($case eq 'default') {
11320:                                             $chgtext .= '<li>'.$othertitle.'</li>';
11321:                                         } else {
11322:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
11323:                                         }
11324:                                     }
11325:                                     $chgtext .= '</ul>';
11326:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
11327:                                         $chgtext .= '<br /><span class="LC_warning">'.
11328:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
11329:                                                     '</span>';
11330:                                     }
11331:                                 }
11332:                             } else {
11333:                                 if (@{$cancreate{$type}} == 0) {
11334:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
11335:                                 } else {
11336:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
11337:                                 }
11338:                             }
11339:                         }
11340:                     } elsif ($type eq 'selfcreateprocessing') {
11341:                         my %choices = &Apache::lonlocal::texthash (
11342:                                                                     automatic => 'Automatic approval',
11343:                                                                     approval  => 'Queued for approval',
11344:                                                                   );
11345:                         if (@statuses > 1) {
11346:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
11347:                                         '<ul>';
11348:                            foreach my $type (@statuses) {
11349:                                if ($type eq 'default') {
11350:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
11351:                                } else {
11352:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
11353:                                }
11354:                            }
11355:                            $chgtext .= '</ul>';
11356:                         } else {
11357:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
11358:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
11359:                         }
11360:                     } elsif ($type eq 'captcha') {
11361:                         if ($savecaptcha{$type} eq 'notused') {
11362:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
11363:                         } else {
11364:                             my %captchas = &captcha_phrases();
11365:                             if ($captchas{$savecaptcha{$type}}) {
11366:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
11367:                             } else {
11368:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
11369:                             }
11370:                         }
11371:                     } elsif ($type eq 'recaptchakeys') {
11372:                         my ($privkey,$pubkey);
11373:                         if (ref($savecaptcha{$type}) eq 'HASH') {
11374:                             $pubkey = $savecaptcha{$type}{'public'};
11375:                             $privkey = $savecaptcha{$type}{'private'};
11376:                         }
11377:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
11378:                         if (!$pubkey) {
11379:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
11380:                         } else {
11381:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
11382:                         }
11383:                         if (!$privkey) {
11384:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
11385:                         } else {
11386:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
11387:                         }
11388:                         $chgtext .= '</ul>';
11389:                     } elsif ($type eq 'recaptchaversion') {
11390:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
11391:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
11392:                         }
11393:                     } elsif ($type eq 'emailusername') {
11394:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
11395:                             if (ref($types) eq 'ARRAY') {
11396:                                 foreach my $type (@{$types}) {
11397:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
11398:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
11399:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
11400:                                                     '<ul>';
11401:                                             foreach my $field (@{$infofields}) {
11402:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
11403:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
11404:                                                 }
11405:                                             }
11406:                                             $chgtext .= '</ul>';
11407:                                         } else {
11408:                                             $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 />';
11409:                                         }
11410:                                     } else {
11411:                                         $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 />';
11412:                                     }
11413:                                 }
11414:                             }
11415:                         }
11416:                     } elsif ($type eq 'notify') {
11417:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
11418:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
11419:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
11420:                                 if ($cancreate{'notify'}{'approval'}) {
11421:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
11422:                                 }
11423:                             }
11424:                         }
11425:                     }
11426:                     if ($chgtext) {
11427:                         $resulttext .= '<li>'.$chgtext.'</li>';
11428:                     }
11429:                 }
11430:             }
11431:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
11432:                 my ($emailrules,$emailruleorder) =
11433:                     &Apache::lonnet::inst_userrules($dom,'email');
11434:                 my $chgtext = '<ul>';
11435:                 foreach my $type (@email_rule) {
11436:                     if (ref($emailrules->{$type}) eq 'HASH') {
11437:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
11438:                     }
11439:                 }
11440:                 $chgtext .= '</ul>';
11441:                 if (@email_rule > 0) {
11442:                     $resulttext .= '<li>'.
11443:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
11444:                                        $chgtext.
11445:                                    '</li>';
11446:                 } else {
11447:                     $resulttext .= '<li>'.
11448:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
11449:                                    '</li>';
11450:                 }
11451:             }
11452:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
11453:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
11454:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11455:                 foreach my $type (@{$changes{'selfcreate'}}) {
11456:                     my $typename = $type;
11457:                     if (ref($usertypes) eq 'HASH') {
11458:                         if ($usertypes->{$type} ne '') {
11459:                             $typename = $usertypes->{$type};
11460:                         }
11461:                     }
11462:                     my @modifiable;
11463:                     $resulttext .= '<li>'.
11464:                                     &mt('Self-creation of account by users with status: [_1]',
11465:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
11466:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
11467:                     foreach my $field (@fields) {
11468:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
11469:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
11470:                         }
11471:                     }
11472:                     if (@modifiable > 0) {
11473:                         $resulttext .= join(', ',@modifiable);
11474:                     } else {
11475:                         $resulttext .= &mt('none');
11476:                     }
11477:                     $resulttext .= '</li>';
11478:                 }
11479:                 $resulttext .= '</ul></li>';
11480:             }
11481:             $resulttext .= '</ul>';
11482:         } else {
11483:             $resulttext = &mt('No changes made to self-creation settings');
11484:         }
11485:     } else {
11486:         $resulttext = '<span class="LC_error">'.
11487:             &mt('An error occurred: [_1]',$putresult).'</span>';
11488:     }
11489:     if ($warningmsg ne '') {
11490:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11491:     }
11492:     return $resulttext;
11493: }
11494: 
11495: sub process_captcha {
11496:     my ($container,$changes,$newsettings,$current) = @_;
11497:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
11498:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
11499:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
11500:         $newsettings->{'captcha'} = 'original';
11501:     }
11502:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
11503:         if ($container eq 'cancreate') {
11504:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11505:                 push(@{$changes->{'cancreate'}},'captcha');
11506:             } elsif (!defined($changes->{'cancreate'})) {
11507:                 $changes->{'cancreate'} = ['captcha'];
11508:             }
11509:         } else {
11510:             $changes->{'captcha'} = 1;
11511:         }
11512:     }
11513:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
11514:     if ($newsettings->{'captcha'} eq 'recaptcha') {
11515:         $newpub = $env{'form.'.$container.'_recaptchapub'};
11516:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
11517:         $newpub =~ s/[^\w\-]//g;
11518:         $newpriv =~ s/[^\w\-]//g;
11519:         $newsettings->{'recaptchakeys'} = {
11520:                                              public  => $newpub,
11521:                                              private => $newpriv,
11522:                                           };
11523:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
11524:         $newversion =~ s/\D//g;
11525:         if ($newversion ne '2') {
11526:             $newversion = 1;
11527:         }
11528:         $newsettings->{'recaptchaversion'} = $newversion;
11529:     }
11530:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
11531:         $currpub = $current->{'recaptchakeys'}{'public'};
11532:         $currpriv = $current->{'recaptchakeys'}{'private'};
11533:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
11534:             $newsettings->{'recaptchakeys'} = {
11535:                                                  public  => '',
11536:                                                  private => '',
11537:                                               }
11538:         }
11539:     }
11540:     if ($current->{'captcha'} eq 'recaptcha') {
11541:         $currversion = $current->{'recaptchaversion'};
11542:         if ($currversion ne '2') {
11543:             $currversion = 1;
11544:         }
11545:     }
11546:     if ($currversion ne $newversion) {
11547:         if ($container eq 'cancreate') {
11548:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11549:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
11550:             } elsif (!defined($changes->{'cancreate'})) {
11551:                 $changes->{'cancreate'} = ['recaptchaversion'];
11552:             }
11553:         } else {
11554:             $changes->{'recaptchaversion'} = 1;
11555:         }
11556:     }
11557:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
11558:         if ($container eq 'cancreate') {
11559:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11560:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
11561:             } elsif (!defined($changes->{'cancreate'})) {
11562:                 $changes->{'cancreate'} = ['recaptchakeys'];
11563:             }
11564:         } else {
11565:             $changes->{'recaptchakeys'} = 1;
11566:         }
11567:     }
11568:     return;
11569: }
11570: 
11571: sub modify_usermodification {
11572:     my ($dom,%domconfig) = @_;
11573:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
11574:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11575:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11576:             if ($key eq 'selfcreate') {
11577:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
11578:             } else {  
11579:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
11580:             }
11581:         }
11582:     }
11583:     my @contexts = ('author','course');
11584:     my %context_title = (
11585:                            author => 'In author context',
11586:                            course => 'In course context',
11587:                         );
11588:     my @fields = ('lastname','firstname','middlename','generation',
11589:                   'permanentemail','id');
11590:     my %roles = (
11591:                   author => ['ca','aa'],
11592:                   course => ['st','ep','ta','in','cr'],
11593:                 );
11594:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11595:     foreach my $context (@contexts) {
11596:         foreach my $role (@{$roles{$context}}) {
11597:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
11598:             foreach my $item (@fields) {
11599:                 if (grep(/^\Q$item\E$/,@modifiable)) {
11600:                     $modifyhash{$context}{$role}{$item} = 1;
11601:                 } else {
11602:                     $modifyhash{$context}{$role}{$item} = 0;
11603:                 }
11604:             }
11605:         }
11606:         if (ref($curr_usermodification{$context}) eq 'HASH') {
11607:             foreach my $role (@{$roles{$context}}) {
11608:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
11609:                     foreach my $field (@fields) {
11610:                         if ($modifyhash{$context}{$role}{$field} ne 
11611:                                 $curr_usermodification{$context}{$role}{$field}) {
11612:                             push(@{$changes{$context}},$role);
11613:                             last;
11614:                         }
11615:                     }
11616:                 }
11617:             }
11618:         } else {
11619:             foreach my $context (@contexts) {
11620:                 foreach my $role (@{$roles{$context}}) {
11621:                     push(@{$changes{$context}},$role);
11622:                 }
11623:             }
11624:         }
11625:     }
11626:     my %usermodification_hash =  (
11627:                                    usermodification => \%modifyhash,
11628:                                  );
11629:     my $putresult = &Apache::lonnet::put_dom('configuration',
11630:                                              \%usermodification_hash,$dom);
11631:     if ($putresult eq 'ok') {
11632:         if (keys(%changes) > 0) {
11633:             $resulttext = &mt('Changes made: ').'<ul>';
11634:             foreach my $context (@contexts) {
11635:                 if (ref($changes{$context}) eq 'ARRAY') {
11636:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
11637:                     if (ref($changes{$context}) eq 'ARRAY') {
11638:                         foreach my $role (@{$changes{$context}}) {
11639:                             my $rolename;
11640:                             if ($role eq 'cr') {
11641:                                 $rolename = &mt('Custom');
11642:                             } else {
11643:                                 $rolename = &Apache::lonnet::plaintext($role);
11644:                             }
11645:                             my @modifiable;
11646:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
11647:                             foreach my $field (@fields) {
11648:                                 if ($modifyhash{$context}{$role}{$field}) {
11649:                                     push(@modifiable,$fieldtitles{$field});
11650:                                 }
11651:                             }
11652:                             if (@modifiable > 0) {
11653:                                 $resulttext .= join(', ',@modifiable);
11654:                             } else {
11655:                                 $resulttext .= &mt('none'); 
11656:                             }
11657:                             $resulttext .= '</li>';
11658:                         }
11659:                         $resulttext .= '</ul></li>';
11660:                     }
11661:                 }
11662:             }
11663:             $resulttext .= '</ul>';
11664:         } else {
11665:             $resulttext = &mt('No changes made to user modification settings');
11666:         }
11667:     } else {
11668:         $resulttext = '<span class="LC_error">'.
11669:             &mt('An error occurred: [_1]',$putresult).'</span>';
11670:     }
11671:     return $resulttext;
11672: }
11673: 
11674: sub modify_defaults {
11675:     my ($dom,$lastactref,%domconfig) = @_;
11676:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
11677:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11678:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
11679:     my @authtypes = ('internal','krb4','krb5','localauth');
11680:     foreach my $item (@items) {
11681:         $newvalues{$item} = $env{'form.'.$item};
11682:         if ($item eq 'auth_def') {
11683:             if ($newvalues{$item} ne '') {
11684:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
11685:                     push(@errors,$item);
11686:                 }
11687:             }
11688:         } elsif ($item eq 'lang_def') {
11689:             if ($newvalues{$item} ne '') {
11690:                 if ($newvalues{$item} =~ /^(\w+)/) {
11691:                     my $langcode = $1;
11692:                     if ($langcode ne 'x_chef') {
11693:                         if (code2language($langcode) eq '') {
11694:                             push(@errors,$item);
11695:                         }
11696:                     }
11697:                 } else {
11698:                     push(@errors,$item);
11699:                 }
11700:             }
11701:         } elsif ($item eq 'timezone_def') {
11702:             if ($newvalues{$item} ne '') {
11703:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
11704:                     push(@errors,$item);   
11705:                 }
11706:             }
11707:         } elsif ($item eq 'datelocale_def') {
11708:             if ($newvalues{$item} ne '') {
11709:                 my @datelocale_ids = DateTime::Locale->ids();
11710:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
11711:                     push(@errors,$item);
11712:                 }
11713:             }
11714:         } elsif ($item eq 'portal_def') {
11715:             if ($newvalues{$item} ne '') {
11716:                 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])\/?$/) {
11717:                     push(@errors,$item);
11718:                 }
11719:             }
11720:         }
11721:         if (grep(/^\Q$item\E$/,@errors)) {
11722:             $newvalues{$item} = $domdefaults{$item};
11723:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
11724:             $changes{$item} = 1;
11725:         }
11726:         $domdefaults{$item} = $newvalues{$item};
11727:     }
11728:     my %defaults_hash = (
11729:                          defaults => \%newvalues,
11730:                         );
11731:     my $title = &defaults_titles();
11732: 
11733:     my $currinststatus;
11734:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
11735:         $currinststatus = $domconfig{'inststatus'};
11736:     } else {
11737:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11738:         $currinststatus = {
11739:                              inststatustypes => $usertypes,
11740:                              inststatusorder => $types,
11741:                              inststatusguest => [],
11742:                           };
11743:     }
11744:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
11745:     my @allpos;
11746:     my %guests;
11747:     my %alltypes;
11748:     my ($currtitles,$currguests,$currorder);
11749:     if (ref($currinststatus) eq 'HASH') {
11750:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
11751:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
11752:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
11753:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
11754:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
11755:                     }
11756:                 }
11757:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
11758:                     my $position = $env{'form.inststatus_pos_'.$type};
11759:                     $position =~ s/\D+//g;
11760:                     $allpos[$position] = $type;
11761:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
11762:                     $alltypes{$type} =~ s/`//g;
11763:                     if ($env{'form.inststatus_guest_'.$type}) {
11764:                         $guests{$type} = 1;
11765:                     }
11766:                 }
11767:             }
11768:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
11769:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
11770:             }
11771:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
11772:             $currtitles =~ s/,$//;
11773:         }
11774:     }
11775:     if ($env{'form.addinststatus'}) {
11776:         my $newtype = $env{'form.addinststatus'};
11777:         $newtype =~ s/\W//g;
11778:         unless (exists($alltypes{$newtype})) {
11779:             if ($env{'form.addinststatus_guest'}) {
11780:                 $guests{$newtype} = 1;
11781:             }
11782:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
11783:             $alltypes{$newtype} =~ s/`//g; 
11784:             my $position = $env{'form.addinststatus_pos'};
11785:             $position =~ s/\D+//g;
11786:             if ($position ne '') {
11787:                 $allpos[$position] = $newtype;
11788:             }
11789:         }
11790:     }
11791:     my (@orderedstatus,@orderedguests);
11792:     foreach my $type (@allpos) {
11793:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
11794:             push(@orderedstatus,$type);
11795:             if ($guests{$type}) {
11796:                 push(@orderedguests,$type);
11797:             }
11798:         }
11799:     }
11800:     foreach my $type (keys(%alltypes)) {
11801:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
11802:             delete($alltypes{$type});
11803:         }
11804:     }
11805:     $defaults_hash{'inststatus'} = {
11806:                                      inststatustypes => \%alltypes,
11807:                                      inststatusorder => \@orderedstatus,
11808:                                      inststatusguest => \@orderedguests,
11809:                                    };
11810:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
11811:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
11812:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
11813:         }
11814:     }
11815:     if ($currorder ne join(',',@orderedstatus)) {
11816:         $changes{'inststatus'}{'inststatusorder'} = 1;
11817:     }
11818:     if ($currguests ne join(',',@orderedguests)) {
11819:         $changes{'inststatus'}{'inststatusguest'} = 1;
11820:     }
11821:     my $newtitles;
11822:     foreach my $item (@orderedstatus) {
11823:         $newtitles .= $alltypes{$item}.',';
11824:     }
11825:     $newtitles =~ s/,$//;
11826:     if ($currtitles ne $newtitles) {
11827:         $changes{'inststatus'}{'inststatustypes'} = 1;
11828:     }
11829:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
11830:                                              $dom);
11831:     if ($putresult eq 'ok') {
11832:         if (keys(%changes) > 0) {
11833:             $resulttext = &mt('Changes made:').'<ul>';
11834:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
11835:             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";
11836:             foreach my $item (sort(keys(%changes))) {
11837:                 if ($item eq 'inststatus') {
11838:                     if (ref($changes{'inststatus'}) eq 'HASH') {
11839:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
11840:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
11841:                             foreach my $type (@orderedstatus) { 
11842:                                 $resulttext .= $alltypes{$type}.', ';
11843:                             }
11844:                             $resulttext =~ s/, $//;
11845:                             $resulttext .= '</li>';
11846:                         }
11847:                         if ($changes{'inststatus'}{'inststatusguest'}) {
11848:                             $resulttext .= '<li>'; 
11849:                             if (@orderedguests) {
11850:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
11851:                                 foreach my $type (@orderedguests) {
11852:                                     $resulttext .= $alltypes{$type}.', ';
11853:                                 }
11854:                                 $resulttext =~ s/, $//;
11855:                             } else {
11856:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
11857:                             }
11858:                             $resulttext .= '</li>';
11859:                         }
11860:                     }
11861:                 } else {
11862:                     my $value = $env{'form.'.$item};
11863:                     if ($value eq '') {
11864:                         $value = &mt('none');
11865:                     } elsif ($item eq 'auth_def') {
11866:                         my %authnames = &authtype_names();
11867:                         my %shortauth = (
11868:                                           internal   => 'int',
11869:                                           krb4       => 'krb4',
11870:                                           krb5       => 'krb5',
11871:                                           localauth  => 'loc',
11872:                         );
11873:                         $value = $authnames{$shortauth{$value}};
11874:                     }
11875:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
11876:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
11877:                 }
11878:             }
11879:             $resulttext .= '</ul>';
11880:             $mailmsgtext .= "\n";
11881:             my $cachetime = 24*60*60;
11882:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11883:             if (ref($lastactref) eq 'HASH') {
11884:                 $lastactref->{'domdefaults'} = 1;
11885:             }
11886:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
11887:                 my $notify = 1;
11888:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
11889:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
11890:                         $notify = 0;
11891:                     }
11892:                 }
11893:                 if ($notify) {
11894:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
11895:                                                "LON-CAPA Domain Settings Change - $dom",
11896:                                                $mailmsgtext);
11897:                 }
11898:             }
11899:         } else {
11900:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
11901:         }
11902:     } else {
11903:         $resulttext = '<span class="LC_error">'.
11904:             &mt('An error occurred: [_1]',$putresult).'</span>';
11905:     }
11906:     if (@errors > 0) {
11907:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
11908:         foreach my $item (@errors) {
11909:             $resulttext .= ' "'.$title->{$item}.'",';
11910:         }
11911:         $resulttext =~ s/,$//;
11912:     }
11913:     return $resulttext;
11914: }
11915: 
11916: sub modify_scantron {
11917:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
11918:     my ($resulttext,%confhash,%changes,$errors);
11919:     my $custom = 'custom.tab';
11920:     my $default = 'default.tab';
11921:     my $servadm = $r->dir_config('lonAdmEMail');
11922:     my ($configuserok,$author_ok,$switchserver) = 
11923:         &config_check($dom,$confname,$servadm);
11924:     if ($env{'form.scantronformat.filename'} ne '') {
11925:         my $error;
11926:         if ($configuserok eq 'ok') {
11927:             if ($switchserver) {
11928:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
11929:             } else {
11930:                 if ($author_ok eq 'ok') {
11931:                     my ($result,$scantronurl) =
11932:                         &publishlogo($r,'upload','scantronformat',$dom,
11933:                                      $confname,'scantron','','',$custom);
11934:                     if ($result eq 'ok') {
11935:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
11936:                         $changes{'scantronformat'} = 1;
11937:                     } else {
11938:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
11939:                     }
11940:                 } else {
11941:                     $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);
11942:                 }
11943:             }
11944:         } else {
11945:             $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);
11946:         }
11947:         if ($error) {
11948:             &Apache::lonnet::logthis($error);
11949:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11950:         }
11951:     }
11952:     if (ref($domconfig{'scantron'}) eq 'HASH') {
11953:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
11954:             if ($env{'form.scantronformat_del'}) {
11955:                 $confhash{'scantron'}{'scantronformat'} = '';
11956:                 $changes{'scantronformat'} = 1;
11957:             }
11958:         }
11959:     }
11960:     if (keys(%confhash) > 0) {
11961:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
11962:                                                  $dom);
11963:         if ($putresult eq 'ok') {
11964:             if (keys(%changes) > 0) {
11965:                 if (ref($confhash{'scantron'}) eq 'HASH') {
11966:                     $resulttext = &mt('Changes made:').'<ul>';
11967:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
11968:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
11969:                     } else {
11970:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
11971:                     }
11972:                     $resulttext .= '</ul>';
11973:                 } else {
11974:                     $resulttext = &mt('Changes made to bubblesheet format file.');
11975:                 }
11976:                 $resulttext .= '</ul>';
11977:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
11978:                 if (ref($lastactref) eq 'HASH') {
11979:                     $lastactref->{'domainconfig'} = 1;
11980:                 }
11981:             } else {
11982:                 $resulttext = &mt('No changes made to bubblesheet format file');
11983:             }
11984:         } else {
11985:             $resulttext = '<span class="LC_error">'.
11986:                 &mt('An error occurred: [_1]',$putresult).'</span>';
11987:         }
11988:     } else {
11989:         $resulttext = &mt('No changes made to bubblesheet format file'); 
11990:     }
11991:     if ($errors) {
11992:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
11993:                        $errors.'</ul>';
11994:     }
11995:     return $resulttext;
11996: }
11997: 
11998: sub modify_coursecategories {
11999:     my ($dom,$lastactref,%domconfig) = @_;
12000:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
12001:         $cathash);
12002:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
12003:     my @catitems = ('unauth','auth');
12004:     my @cattypes = ('std','domonly','codesrch','none');
12005:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12006:         $cathash = $domconfig{'coursecategories'}{'cats'};
12007:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
12008:             $changes{'togglecats'} = 1;
12009:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
12010:         }
12011:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
12012:             $changes{'categorize'} = 1;
12013:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
12014:         }
12015:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
12016:             $changes{'togglecatscomm'} = 1;
12017:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
12018:         }
12019:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
12020:             $changes{'categorizecomm'} = 1;
12021:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
12022: 
12023:         }
12024:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
12025:             $changes{'togglecatsplace'} = 1;
12026:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
12027:         }
12028:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
12029:             $changes{'categorizeplace'} = 1;
12030:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
12031:         }
12032:         foreach my $item (@catitems) {
12033:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12034:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
12035:                     $changes{$item} = 1;
12036:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12037:                 }
12038:             }
12039:         }
12040:     } else {
12041:         $changes{'togglecats'} = 1;
12042:         $changes{'categorize'} = 1;
12043:         $changes{'togglecatscomm'} = 1;
12044:         $changes{'categorizecomm'} = 1;
12045:         $changes{'togglecatsplace'} = 1;
12046:         $changes{'categorizeplace'} = 1;
12047:         $domconfig{'coursecategories'} = {
12048:                                              togglecats => $env{'form.togglecats'},
12049:                                              categorize => $env{'form.categorize'},
12050:                                              togglecatscomm => $env{'form.togglecatscomm'},
12051:                                              categorizecomm => $env{'form.categorizecomm'},
12052:                                              togglecatsplace => $env{'form.togglecatsplace'},
12053:                                              categorizeplace => $env{'form.categorizeplace'},
12054:                                          };
12055:         foreach my $item (@catitems) {
12056:             if ($env{'form.coursecat_'.$item} ne 'std') {
12057:                 $changes{$item} = 1;
12058:             }
12059:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12060:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12061:             }
12062:         }
12063:     }
12064:     if (ref($cathash) eq 'HASH') {
12065:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
12066:             push (@deletecategory,'instcode::0');
12067:         }
12068:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
12069:             push(@deletecategory,'communities::0');
12070:         }
12071:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
12072:             push(@deletecategory,'placement::0');
12073:         }
12074:     }
12075:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
12076:     if (ref($cathash) eq 'HASH') {
12077:         if (@deletecategory > 0) {
12078:             #FIXME Need to remove category from all courses using a deleted category 
12079:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
12080:             foreach my $item (@deletecategory) {
12081:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
12082:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
12083:                     $deletions{$item} = 1;
12084:                     &recurse_cat_deletes($item,$cathash,\%deletions);
12085:                 }
12086:             }
12087:         }
12088:         foreach my $item (keys(%{$cathash})) {
12089:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
12090:             if ($cathash->{$item} ne $env{'form.'.$item}) {
12091:                 $reorderings{$item} = 1;
12092:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
12093:             }
12094:             if ($env{'form.addcategory_name_'.$item} ne '') {
12095:                 my $newcat = $env{'form.addcategory_name_'.$item};
12096:                 my $newdepth = $depth+1;
12097:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
12098:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
12099:                 $adds{$newitem} = 1; 
12100:             }
12101:             if ($env{'form.subcat_'.$item} ne '') {
12102:                 my $newcat = $env{'form.subcat_'.$item};
12103:                 my $newdepth = $depth+1;
12104:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
12105:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
12106:                 $adds{$newitem} = 1;
12107:             }
12108:         }
12109:     }
12110:     if ($env{'form.instcode'} eq '1') {
12111:         if (ref($cathash) eq 'HASH') {
12112:             my $newitem = 'instcode::0';
12113:             if ($cathash->{$newitem} eq '') {  
12114:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
12115:                 $adds{$newitem} = 1;
12116:             }
12117:         } else {
12118:             my $newitem = 'instcode::0';
12119:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
12120:             $adds{$newitem} = 1;
12121:         }
12122:     }
12123:     if ($env{'form.communities'} eq '1') {
12124:         if (ref($cathash) eq 'HASH') {
12125:             my $newitem = 'communities::0';
12126:             if ($cathash->{$newitem} eq '') {
12127:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
12128:                 $adds{$newitem} = 1;
12129:             }
12130:         } else {
12131:             my $newitem = 'communities::0';
12132:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
12133:             $adds{$newitem} = 1;
12134:         }
12135:     }
12136:     if ($env{'form.placement'} eq '1') {
12137:         if (ref($cathash) eq 'HASH') {
12138:             my $newitem = 'placement::0';
12139:             if ($cathash->{$newitem} eq '') {
12140:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
12141:                 $adds{$newitem} = 1;
12142:             }
12143:         } else {
12144:             my $newitem = 'placement::0';
12145:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
12146:             $adds{$newitem} = 1;
12147:         }
12148:     }
12149:     if ($env{'form.addcategory_name'} ne '') {
12150:         if (($env{'form.addcategory_name'} ne 'instcode') &&
12151:             ($env{'form.addcategory_name'} ne 'communities') &&
12152:             ($env{'form.addcategory_name'} ne 'placement')) {
12153:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
12154:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
12155:             $adds{$newitem} = 1;
12156:         }
12157:     }
12158:     my $putresult;
12159:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12160:         if (keys(%deletions) > 0) {
12161:             foreach my $key (keys(%deletions)) {
12162:                 if ($predelallitems{$key} ne '') {
12163:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
12164:                 }
12165:             }
12166:         }
12167:         my (@chkcats,@chktrails,%chkallitems);
12168:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
12169:         if (ref($chkcats[0]) eq 'ARRAY') {
12170:             my $depth = 0;
12171:             my $chg = 0;
12172:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
12173:                 my $name = $chkcats[0][$i];
12174:                 my $item;
12175:                 if ($name eq '') {
12176:                     $chg ++;
12177:                 } else {
12178:                     $item = &escape($name).'::0';
12179:                     if ($chg) {
12180:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
12181:                     }
12182:                     $depth ++; 
12183:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
12184:                     $depth --;
12185:                 }
12186:             }
12187:         }
12188:     }
12189:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12190:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
12191:         if ($putresult eq 'ok') {
12192:             my %title = (
12193:                          togglecats     => 'Show/Hide a course in catalog',
12194:                          categorize     => 'Assign a category to a course',
12195:                          togglecatscomm => 'Show/Hide a community in catalog',
12196:                          categorizecomm => 'Assign a category to a community',
12197:                         );
12198:             my %level = (
12199:                          dom  => 'set in Domain ("Modify Course/Community")',
12200:                          crs  => 'set in Course ("Course Configuration")',
12201:                          comm => 'set in Community ("Community Configuration")',
12202:                          none     => 'No catalog',
12203:                          std      => 'Standard catalog',
12204:                          domonly  => 'Domain-only catalog',
12205:                          codesrch => 'Code search form',
12206:                         );
12207:             $resulttext = &mt('Changes made:').'<ul>';
12208:             if ($changes{'togglecats'}) {
12209:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
12210:             }
12211:             if ($changes{'categorize'}) {
12212:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
12213:             }
12214:             if ($changes{'togglecatscomm'}) {
12215:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
12216:             }
12217:             if ($changes{'categorizecomm'}) {
12218:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
12219:             }
12220:             if ($changes{'unauth'}) {
12221:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
12222:             }
12223:             if ($changes{'auth'}) {
12224:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
12225:             }
12226:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12227:                 my $cathash;
12228:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12229:                     $cathash = $domconfig{'coursecategories'}{'cats'};
12230:                 } else {
12231:                     $cathash = {};
12232:                 } 
12233:                 my (@cats,@trails,%allitems);
12234:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
12235:                 if (keys(%deletions) > 0) {
12236:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
12237:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
12238:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
12239:                     }
12240:                     $resulttext .= '</ul></li>';
12241:                 }
12242:                 if (keys(%reorderings) > 0) {
12243:                     my %sort_by_trail;
12244:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
12245:                     foreach my $key (keys(%reorderings)) {
12246:                         if ($allitems{$key} ne '') {
12247:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
12248:                         }
12249:                     }
12250:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
12251:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
12252:                     }
12253:                     $resulttext .= '</ul></li>';
12254:                 }
12255:                 if (keys(%adds) > 0) {
12256:                     my %sort_by_trail;
12257:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
12258:                     foreach my $key (keys(%adds)) {
12259:                         if ($allitems{$key} ne '') {
12260:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
12261:                         }
12262:                     }
12263:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
12264:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
12265:                     }
12266:                     $resulttext .= '</ul></li>';
12267:                 }
12268:             }
12269:             $resulttext .= '</ul>';
12270:             if ($changes{'unauth'} || $changes{'auth'}) {
12271:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
12272:                 if ($changes{'auth'}) {
12273:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
12274:                 }
12275:                 if ($changes{'unauth'}) {
12276:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
12277:                 }
12278:                 my $cachetime = 24*60*60;
12279:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12280:                 if (ref($lastactref) eq 'HASH') {
12281:                     $lastactref->{'domdefaults'} = 1;
12282:                 }
12283:             }
12284:         } else {
12285:             $resulttext = '<span class="LC_error">'.
12286:                           &mt('An error occurred: [_1]',$putresult).'</span>';
12287:         }
12288:     } else {
12289:         $resulttext = &mt('No changes made to course and community categories');
12290:     }
12291:     return $resulttext;
12292: }
12293: 
12294: sub modify_serverstatuses {
12295:     my ($dom,%domconfig) = @_;
12296:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
12297:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
12298:         %currserverstatus = %{$domconfig{'serverstatuses'}};
12299:     }
12300:     my @pages = &serverstatus_pages();
12301:     foreach my $type (@pages) {
12302:         $newserverstatus{$type}{'namedusers'} = '';
12303:         $newserverstatus{$type}{'machines'} = '';
12304:         if (defined($env{'form.'.$type.'_namedusers'})) {
12305:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
12306:             my @okusers;
12307:             foreach my $user (@users) {
12308:                 my ($uname,$udom) = split(/:/,$user);
12309:                 if (($udom =~ /^$match_domain$/) &&   
12310:                     (&Apache::lonnet::domain($udom)) &&
12311:                     ($uname =~ /^$match_username$/)) {
12312:                     if (!grep(/^\Q$user\E/,@okusers)) {
12313:                         push(@okusers,$user);
12314:                     }
12315:                 }
12316:             }
12317:             if (@okusers > 0) {
12318:                  @okusers = sort(@okusers);
12319:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
12320:             }
12321:         }
12322:         if (defined($env{'form.'.$type.'_machines'})) {
12323:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
12324:             my @okmachines;
12325:             foreach my $ip (@machines) {
12326:                 my @parts = split(/\./,$ip);
12327:                 next if (@parts < 4);
12328:                 my $badip = 0;
12329:                 for (my $i=0; $i<4; $i++) {
12330:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
12331:                         $badip = 1;
12332:                         last;
12333:                     }
12334:                 }
12335:                 if (!$badip) {
12336:                     push(@okmachines,$ip);     
12337:                 }
12338:             }
12339:             @okmachines = sort(@okmachines);
12340:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
12341:         }
12342:     }
12343:     my %serverstatushash =  (
12344:                                 serverstatuses => \%newserverstatus,
12345:                             );
12346:     foreach my $type (@pages) {
12347:         foreach my $setting ('namedusers','machines') {
12348:             my (@current,@new);
12349:             if (ref($currserverstatus{$type}) eq 'HASH') {
12350:                 if ($currserverstatus{$type}{$setting} ne '') { 
12351:                     @current = split(/,/,$currserverstatus{$type}{$setting});
12352:                 }
12353:             }
12354:             if ($newserverstatus{$type}{$setting} ne '') {
12355:                 @new = split(/,/,$newserverstatus{$type}{$setting});
12356:             }
12357:             if (@current > 0) {
12358:                 if (@new > 0) {
12359:                     foreach my $item (@current) {
12360:                         if (!grep(/^\Q$item\E$/,@new)) {
12361:                             $changes{$type}{$setting} = 1;
12362:                             last;
12363:                         }
12364:                     }
12365:                     foreach my $item (@new) {
12366:                         if (!grep(/^\Q$item\E$/,@current)) {
12367:                             $changes{$type}{$setting} = 1;
12368:                             last;
12369:                         }
12370:                     }
12371:                 } else {
12372:                     $changes{$type}{$setting} = 1;
12373:                 }
12374:             } elsif (@new > 0) {
12375:                 $changes{$type}{$setting} = 1;
12376:             }
12377:         }
12378:     }
12379:     if (keys(%changes) > 0) {
12380:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
12381:         my $putresult = &Apache::lonnet::put_dom('configuration',
12382:                                                  \%serverstatushash,$dom);
12383:         if ($putresult eq 'ok') {
12384:             $resulttext .= &mt('Changes made:').'<ul>';
12385:             foreach my $type (@pages) {
12386:                 if (ref($changes{$type}) eq 'HASH') {
12387:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
12388:                     if ($changes{$type}{'namedusers'}) {
12389:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
12390:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
12391:                         } else {
12392:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
12393:                         }
12394:                     }
12395:                     if ($changes{$type}{'machines'}) {
12396:                         if ($newserverstatus{$type}{'machines'} eq '') {
12397:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
12398:                         } else {
12399:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
12400:                         }
12401: 
12402:                     }
12403:                     $resulttext .= '</ul></li>';
12404:                 }
12405:             }
12406:             $resulttext .= '</ul>';
12407:         } else {
12408:             $resulttext = '<span class="LC_error">'.
12409:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
12410: 
12411:         }
12412:     } else {
12413:         $resulttext = &mt('No changes made to access to server status pages');
12414:     }
12415:     return $resulttext;
12416: }
12417: 
12418: sub modify_helpsettings {
12419:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12420:     my ($resulttext,$errors,%changes,%helphash);
12421:     my %defaultchecked = ('submitbugs' => 'on');
12422:     my @offon = ('off','on');
12423:     my @toggles = ('submitbugs');
12424:     my %current = ('submitbugs' => '',
12425:                    'adhoc'      => {},
12426:                   );
12427:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
12428:         %current = %{$domconfig{'helpsettings'}};
12429:     }
12430:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12431:     foreach my $item (@toggles) {
12432:         if ($defaultchecked{$item} eq 'on') { 
12433:             if ($current{$item} eq '') {
12434:                 if ($env{'form.'.$item} eq '0') {
12435:                     $changes{$item} = 1;
12436:                 }
12437:             } elsif ($current{$item} ne $env{'form.'.$item}) {
12438:                 $changes{$item} = 1;
12439:             }
12440:         } elsif ($defaultchecked{$item} eq 'off') {
12441:             if ($current{$item} eq '') {
12442:                 if ($env{'form.'.$item} eq '1') {
12443:                     $changes{$item} = 1;
12444:                 }
12445:             } elsif ($current{$item} ne $env{'form.'.$item}) {
12446:                 $changes{$item} = 1;
12447:             }
12448:         }
12449:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
12450:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
12451:         }
12452:     }
12453:     my $maxnum = $env{'form.helproles_maxnum'};
12454:     my $confname = $dom.'-domainconfig';
12455:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
12456:     my (@allpos,%newsettings,%changedprivs,$newrole);
12457:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12458:     my @accesstypes = ('all','none','status','inc','exc');
12459:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh']);
12460:     my %lt = &Apache::lonlocal::texthash(
12461:                     s      => 'system',
12462:                     d      => 'domain',
12463:                     order  => 'Display order',
12464:                     access => 'Role usage',
12465:                     all    => 'All',
12466:                     none   => 'None',
12467:                     status => 'Determined based on institutional status',
12468:                     inc    => 'Include all, but exclude specific personnel',
12469:                     exc    => 'Exclude all, but include specific personnel',
12470:     );
12471:     for (my $num=0; $num<=$maxnum; $num++) {
12472:         my ($prefix,$identifier,$rolename,%curr);
12473:         if ($num == $maxnum) {
12474:             next unless ($env{'form.newcusthelp'} == $maxnum);
12475:             $identifier = 'custhelp'.$num;
12476:             $prefix = 'helproles_'.$num;
12477:             $rolename = $env{'form.custhelpname'.$num};
12478:             $rolename=~s/[^A-Za-z0-9]//gs;
12479:             next if ($rolename eq '');
12480:             next if (exists($existing{'rolesdef_'.$rolename}));
12481:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12482:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12483:                                                      $newprivs{'c'},$confname,$dom);
12484:             if ($result ne 'ok') {
12485:                 $errors .= '<li><span class="LC_error">'.
12486:                            &mt('An error occurred storing the new custom role: [_1]',
12487:                            $result).'</span></li>';
12488:                 next;
12489:             } else {
12490:                 $changedprivs{$rolename} = \%newprivs;
12491:                 $newrole = $rolename;
12492:             }
12493:         } else {
12494:             $prefix = 'helproles_'.$num;
12495:             $rolename = $env{'form.'.$prefix};
12496:             next if ($rolename eq '');
12497:             next unless (exists($existing{'rolesdef_'.$rolename}));
12498:             $identifier = 'custhelp'.$num;
12499:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12500:             my %currprivs;
12501:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) = 
12502:                 split(/\_/,$existing{'rolesdef_'.$rolename});
12503:             foreach my $level ('c','d','s') {
12504:                 if ($newprivs{$level} ne $currprivs{$level}) {
12505:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12506:                                                              $newprivs{'c'},$confname,$dom);
12507:                     if ($result ne 'ok') {
12508:                         $errors .= '<li><span class="LC_error">'.
12509:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
12510:                                        $rolename,$result).'</span></li>';
12511:                     } else {
12512:                         $changedprivs{$rolename} = \%newprivs;
12513:                     }
12514:                     last;
12515:                 }
12516:             }
12517:             if (ref($current{'adhoc'}) eq 'HASH') {
12518:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12519:                     %curr = %{$current{'adhoc'}{$rolename}};
12520:                 }
12521:             }
12522:         }
12523:         my $newpos = $env{'form.'.$prefix.'_pos'};
12524:         $newpos =~ s/\D+//g;
12525:         $allpos[$newpos] = $rolename;
12526:         my $newdesc = $env{'form.'.$prefix.'_desc'};
12527:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
12528:         if ($curr{'desc'}) {
12529:             if ($curr{'desc'} ne $newdesc) {
12530:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
12531:                 $newsettings{$rolename}{'desc'} = $newdesc;
12532:             }
12533:         } elsif ($newdesc ne '') {
12534:             $changes{'customrole'}{$rolename}{'desc'} = 1;
12535:             $newsettings{$rolename}{'desc'} = $newdesc;
12536:         }
12537:         my $access = $env{'form.'.$prefix.'_access'};
12538:         if (grep(/^\Q$access\E$/,@accesstypes)) {
12539:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
12540:             if ($access eq 'status') {
12541:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
12542:                 if (scalar(@statuses) == 0) {
12543:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none'; 
12544:                 } else {
12545:                     my (@shownstatus,$numtypes);
12546:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12547:                     if (ref($types) eq 'ARRAY') {
12548:                         $numtypes = scalar(@{$types});
12549:                         foreach my $type (sort(@statuses)) {
12550:                             if ($type eq 'default') {
12551:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12552:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
12553:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12554:                                 push(@shownstatus,$usertypes->{$type});
12555:                             }
12556:                         }
12557:                     }
12558:                     if (grep(/^default$/,@statuses)) {
12559:                         push(@shownstatus,$othertitle);
12560:                     }
12561:                     if (scalar(@shownstatus) == 1+$numtypes) {
12562:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
12563:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
12564:                     } else {
12565:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
12566:                         if (ref($curr{'status'}) eq 'ARRAY') {
12567:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12568:                             if (@diffs) {
12569:                                 $changes{'customrole'}{$rolename}{$access} = 1;
12570:                             }
12571:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12572:                             $changes{'customrole'}{$rolename}{$access} = 1;
12573:                         }
12574:                     }
12575:                 }
12576:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
12577:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
12578:                 my @newspecstaff;
12579:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12580:                 foreach my $person (sort(@personnel)) {
12581:                     if ($domhelpdesk{$person}) {
12582:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
12583:                     }
12584:                 }
12585:                 if (ref($curr{$access}) eq 'ARRAY') {
12586:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12587:                     if (@diffs) {
12588:                         $changes{'customrole'}{$rolename}{$access} = 1;
12589:                     }
12590:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12591:                     $changes{'customrole'}{$rolename}{$access} = 1;
12592:                 }
12593:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12594:                     my ($uname,$udom) = split(/:/,$person);
12595:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
12596:                 }
12597:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
12598:             }
12599:         } else {
12600:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
12601:         }
12602:         unless ($curr{'access'} eq $access) {
12603:             $changes{'customrole'}{$rolename}{'access'} = 1;
12604:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
12605:         }
12606:     }
12607:     if (@allpos > 0) {
12608:         my $idx = 0;
12609:         foreach my $rolename (@allpos) {
12610:             if ($rolename ne '') {
12611:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
12612:                 if (ref($current{'adhoc'}) eq 'HASH') {
12613:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12614:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
12615:                             $changes{'customrole'}{$rolename}{'order'} = 1;
12616:                             $newsettings{$rolename}{'order'} = $idx+1; 
12617:                         }
12618:                     }
12619:                 }
12620:                 $idx ++;
12621:             }
12622:         }
12623:     }
12624:     my $putresult;
12625:     if (keys(%changes) > 0) {
12626:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
12627:         if ($putresult eq 'ok') {
12628:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
12629:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
12630:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
12631:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
12632:                 }
12633:             }
12634:             my $cachetime = 24*60*60;
12635:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12636:             if (ref($lastactref) eq 'HASH') {
12637:                 $lastactref->{'domdefaults'} = 1;
12638:             }
12639:         } else {
12640:             $errors .= '<li><span class="LC_error">'.
12641:                        &mt('An error occurred storing the settings: [_1]',
12642:                            $putresult).'</span></li>';
12643:         }
12644:     }
12645:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
12646:         $resulttext = &mt('Changes made:').'<ul>';
12647:         my (%shownprivs,@levelorder);
12648:         @levelorder = ('c','d','s');
12649:         if ((keys(%changes)) && ($putresult eq 'ok')) {
12650:             foreach my $item (sort(keys(%changes))) {
12651:                 if ($item eq 'submitbugs') {
12652:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
12653:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
12654:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
12655:                 } elsif ($item eq 'customrole') {
12656:                     if (ref($changes{'customrole'}) eq 'HASH') {
12657:                         my @keyorder = ('order','desc','access','status','exc','inc');
12658:                         my %keytext = &Apache::lonlocal::texthash(
12659:                                                                    order  => 'Order',
12660:                                                                    desc   => 'Role description',
12661:                                                                    access => 'Role usage',
12662:                                                                    status => 'Allowed instituional types',
12663:                                                                    exc    => 'Allowed personnel',
12664:                                                                    inc    => 'Disallowed personnel',
12665:                         );
12666:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
12667:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
12668:                                 if ($role eq $newrole) {
12669:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
12670:                                                               $role).'<ul>';
12671:                                 } else {
12672:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
12673:                                                               $role).'<ul>';
12674:                                 }
12675:                                 foreach my $key (@keyorder) {
12676:                                     if ($changes{'customrole'}{$role}{$key}) {
12677:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
12678:                                                                   $keytext{$key},$newsettings{$role}{$key}).
12679:                                                        '</li>';
12680:                                     }
12681:                                 }
12682:                                 if (ref($changedprivs{$role}) eq 'HASH') {
12683:                                     $shownprivs{$role} = 1;
12684:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
12685:                                     foreach my $level (@levelorder) {
12686:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
12687:                                             next if ($item eq '');
12688:                                             my ($priv) = split(/\&/,$item,2);
12689:                                             if (&Apache::lonnet::plaintext($priv)) {
12690:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
12691:                                                 unless ($level eq 'c') {
12692:                                                     $resulttext .= ' ('.$lt{$level}.')';
12693:                                                 }
12694:                                                 $resulttext .= '</li>';
12695:                                             }
12696:                                         }
12697:                                     }
12698:                                     $resulttext .= '</ul>';
12699:                                 }
12700:                                 $resulttext .= '</ul></li>';
12701:                             }
12702:                         }
12703:                     }
12704:                 }
12705:             }
12706:         }
12707:         if (keys(%changedprivs)) {
12708:             foreach my $role (sort(keys(%changedprivs))) {
12709:                 unless ($shownprivs{$role}) {
12710:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
12711:                                               $role).'<ul>'.
12712:                                    '<li>'.&mt('Privileges set to :').'<ul>';
12713:                     foreach my $level (@levelorder) {
12714:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
12715:                             next if ($item eq '');
12716:                             my ($priv) = split(/\&/,$item,2);
12717:                             if (&Apache::lonnet::plaintext($priv)) {
12718:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
12719:                                 unless ($level eq 'c') {
12720:                                     $resulttext .= ' ('.$lt{$level}.')';
12721:                                 }
12722:                                 $resulttext .= '</li>';
12723:                             }
12724:                         }
12725:                     }
12726:                     $resulttext .= '</ul></li></ul></li>';
12727:                 }
12728:             }
12729:         }
12730:         $resulttext .= '</ul>';
12731:     } else {
12732:         $resulttext = &mt('No changes made to help settings');
12733:     }
12734:     if ($errors) {
12735:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
12736:                                     $errors.'</ul>';
12737:     }
12738:     return $resulttext;
12739: }
12740: 
12741: sub modify_coursedefaults {
12742:     my ($dom,$lastactref,%domconfig) = @_;
12743:     my ($resulttext,$errors,%changes,%defaultshash);
12744:     my %defaultchecked = (
12745:                            'canuse_pdfforms' => 'off',
12746:                            'uselcmath'       => 'on',
12747:                            'usejsme'         => 'on'
12748:                          );
12749:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
12750:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
12751:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
12752:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
12753:                    'mysqltables_textbook','mysqltables_placement');
12754:     my @types = ('official','unofficial','community','textbook','placement');
12755:     my %staticdefaults = (
12756:                            anonsurvey_threshold => 10,
12757:                            uploadquota          => 500,
12758:                            postsubmit           => 60,
12759:                            mysqltables          => 172800,
12760:                          );
12761: 
12762:     $defaultshash{'coursedefaults'} = {};
12763: 
12764:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
12765:         if ($domconfig{'coursedefaults'} eq '') {
12766:             $domconfig{'coursedefaults'} = {};
12767:         }
12768:     }
12769: 
12770:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
12771:         foreach my $item (@toggles) {
12772:             if ($defaultchecked{$item} eq 'on') {
12773:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
12774:                     ($env{'form.'.$item} eq '0')) {
12775:                     $changes{$item} = 1;
12776:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
12777:                     $changes{$item} = 1;
12778:                 }
12779:             } elsif ($defaultchecked{$item} eq 'off') {
12780:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
12781:                     ($env{'form.'.$item} eq '1')) {
12782:                     $changes{$item} = 1;
12783:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
12784:                     $changes{$item} = 1;
12785:                 }
12786:             }
12787:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
12788:         }
12789:         foreach my $item (@numbers) {
12790:             my ($currdef,$newdef);
12791:             $newdef = $env{'form.'.$item};
12792:             if ($item eq 'anonsurvey_threshold') {
12793:                 $currdef = $domconfig{'coursedefaults'}{$item};
12794:                 $newdef =~ s/\D//g;
12795:                 if ($newdef eq '' || $newdef < 1) {
12796:                     $newdef = 1;
12797:                 }
12798:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
12799:             } else {
12800:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
12801:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
12802:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
12803:                 }
12804:                 $newdef =~ s/[^\w.\-]//g;
12805:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
12806:             }
12807:             if ($currdef ne $newdef) {
12808:                 my $staticdef;
12809:                 if ($item eq 'anonsurvey_threshold') {
12810:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
12811:                         $changes{$item} = 1;
12812:                     }
12813:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
12814:                     my $setting = $1; 
12815:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
12816:                         $changes{$setting} = 1;
12817:                     }
12818:                 }
12819:             }
12820:         }
12821:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
12822:         my @currclonecode;
12823:         if (ref($currclone) eq 'HASH') {
12824:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
12825:                 @currclonecode = @{$currclone->{'instcode'}};
12826:             }
12827:         }
12828:         my $newclone;
12829:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) { 
12830:             $newclone = $env{'form.canclone'};
12831:         }
12832:         if ($newclone eq 'instcode') {
12833:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
12834:             my (%codedefaults,@code_order,@clonecode);
12835:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
12836:                                                     \@code_order);
12837:             foreach my $item (@code_order) {
12838:                 if (grep(/^\Q$item\E$/,@newcodes)) {
12839:                     push(@clonecode,$item);
12840:                 }
12841:             }
12842:             if (@clonecode) {
12843:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
12844:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
12845:                 if (@diffs) {
12846:                     $changes{'canclone'} = 1;
12847:                 }
12848:             } else {
12849:                 $newclone eq '';
12850:             }
12851:         } elsif ($newclone ne '') {
12852:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone; 
12853:         } 
12854:         if ($newclone ne $currclone) {
12855:             $changes{'canclone'} = 1;
12856:         }
12857:         my %credits;
12858:         foreach my $type (@types) {
12859:             unless ($type eq 'community') {
12860:                 $credits{$type} = $env{'form.'.$type.'_credits'};
12861:                 $credits{$type} =~ s/[^\d.]+//g;
12862:             }
12863:         }
12864:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
12865:             ($env{'form.coursecredits'} eq '1')) {
12866:             $changes{'coursecredits'} = 1;
12867:             foreach my $type (keys(%credits)) {
12868:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
12869:             }
12870:         } else {
12871:             if ($env{'form.coursecredits'} eq '1') { 
12872:                 foreach my $type (@types) {
12873:                     unless ($type eq 'community') {
12874:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {        
12875:                             $changes{'coursecredits'} = 1;
12876:                         }
12877:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
12878:                     }
12879:                 }
12880:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
12881:                 foreach my $type (@types) {
12882:                     unless ($type eq 'community') {
12883:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
12884:                             $changes{'coursecredits'} = 1;
12885:                             last;
12886:                         }
12887:                     }
12888:                 }
12889:             }
12890:         }
12891:         if ($env{'form.postsubmit'} eq '1') {
12892:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
12893:             my %currtimeout;
12894:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
12895:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
12896:                     $changes{'postsubmit'} = 1;
12897:                 }
12898:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
12899:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
12900:                 }
12901:             } else {
12902:                 $changes{'postsubmit'} = 1;
12903:             }
12904:             foreach my $type (@types) {
12905:                 my $timeout = $env{'form.'.$type.'_timeout'};
12906:                 $timeout =~ s/\D//g;
12907:                 if ($timeout == $staticdefaults{'postsubmit'}) {
12908:                     $timeout = '';
12909:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
12910:                     $timeout = '0';
12911:                 }
12912:                 unless ($timeout eq '') {
12913:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
12914:                 }
12915:                 if (exists($currtimeout{$type})) {
12916:                     if ($timeout ne $currtimeout{$type}) {
12917:                         $changes{'postsubmit'} = 1; 
12918:                     }
12919:                 } elsif ($timeout ne '') {
12920:                     $changes{'postsubmit'} = 1;
12921:                 }
12922:             }
12923:         } else {
12924:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
12925:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
12926:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
12927:                     $changes{'postsubmit'} = 1;
12928:                 }
12929:             } else {
12930:                 $changes{'postsubmit'} = 1;
12931:             }
12932:         }
12933:     }
12934:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
12935:                                              $dom);
12936:     if ($putresult eq 'ok') {
12937:         if (keys(%changes) > 0) {
12938:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12939:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
12940:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
12941:                 ($changes{'canclone'}) || ($changes{'mysqltables'})) {
12942:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme') { 
12943:                     if ($changes{$item}) {
12944:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
12945:                     }
12946:                 } 
12947:                 if ($changes{'coursecredits'}) {
12948:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
12949:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
12950:                             $domdefaults{$type.'credits'} =
12951:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
12952:                         }
12953:                     }
12954:                 }
12955:                 if ($changes{'postsubmit'}) {
12956:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
12957:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
12958:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
12959:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
12960:                                 $domdefaults{$type.'postsubtimeout'} =
12961:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
12962:                             }
12963:                         }
12964:                     }
12965:                 }
12966:                 if ($changes{'uploadquota'}) {
12967:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
12968:                         foreach my $type (@types) {
12969:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
12970:                         }
12971:                     }
12972:                 }
12973:                 if ($changes{'canclone'}) {
12974:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
12975:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
12976:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
12977:                             if (@clonecodes) {
12978:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
12979:                             }
12980:                         }
12981:                     } else {
12982:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
12983:                     }
12984:                 }
12985:                 my $cachetime = 24*60*60;
12986:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12987:                 if (ref($lastactref) eq 'HASH') {
12988:                     $lastactref->{'domdefaults'} = 1;
12989:                 }
12990:             }
12991:             $resulttext = &mt('Changes made:').'<ul>';
12992:             foreach my $item (sort(keys(%changes))) {
12993:                 if ($item eq 'canuse_pdfforms') {
12994:                     if ($env{'form.'.$item} eq '1') {
12995:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
12996:                     } else {
12997:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
12998:                     }
12999:                 } elsif ($item eq 'uselcmath') {
13000:                     if ($env{'form.'.$item} eq '1') {
13001:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
13002:                     } else {
13003:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
13004:                     }
13005:                 } elsif ($item eq 'usejsme') {
13006:                     if ($env{'form.'.$item} eq '1') {
13007:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
13008:                     } else {
13009:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>'; 
13010:                     }
13011:                 } elsif ($item eq 'anonsurvey_threshold') {
13012:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
13013:                 } elsif ($item eq 'uploadquota') {
13014:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13015:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
13016:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
13017:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
13018:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
13019:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
13020:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
13021:                                        '</ul>'.
13022:                                        '</li>';
13023:                     } else {
13024:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
13025:                     }
13026:                 } elsif ($item eq 'mysqltables') {
13027:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
13028:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
13029:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
13030:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
13031:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
13032:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
13033:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
13034:                                        '</ul>'.
13035:                                        '</li>';
13036:                     } else {
13037:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
13038:                     }
13039:                 } elsif ($item eq 'postsubmit') {
13040:                     if ($domdefaults{'postsubmit'} eq 'off') {
13041:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
13042:                     } else {
13043:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
13044:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') { 
13045:                             $resulttext .= &mt('durations:').'<ul>';
13046:                             foreach my $type (@types) {
13047:                                 $resulttext .= '<li>';
13048:                                 my $timeout;
13049:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13050:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13051:                                 }
13052:                                 my $display;
13053:                                 if ($timeout eq '0') {
13054:                                     $display = &mt('unlimited');
13055:                                 } elsif ($timeout eq '') {
13056:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
13057:                                 } else {
13058:                                     $display = &mt('[quant,_1,second]',$timeout);
13059:                                 }
13060:                                 if ($type eq 'community') {
13061:                                     $resulttext .= &mt('Communities');
13062:                                 } elsif ($type eq 'official') {
13063:                                     $resulttext .= &mt('Official courses');
13064:                                 } elsif ($type eq 'unofficial') {
13065:                                     $resulttext .= &mt('Unofficial courses');
13066:                                 } elsif ($type eq 'textbook') {
13067:                                     $resulttext .= &mt('Textbook courses');
13068:                                 } elsif ($type eq 'placement') {
13069:                                     $resulttext .= &mt('Placement tests');
13070:                                 }
13071:                                 $resulttext .= ' -- '.$display.'</li>';
13072:                             }
13073:                             $resulttext .= '</ul>';
13074:                         }
13075:                         $resulttext .= '</li>';    
13076:                     }
13077:                 } elsif ($item eq 'coursecredits') {
13078:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13079:                         if (($domdefaults{'officialcredits'} eq '') &&
13080:                             ($domdefaults{'unofficialcredits'} eq '') &&
13081:                             ($domdefaults{'textbookcredits'} eq '')) {
13082:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13083:                         } else {
13084:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
13085:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
13086:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
13087:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
13088:                                            '</ul>'.
13089:                                            '</li>';
13090:                         }
13091:                     } else {
13092:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13093:                     }
13094:                 } elsif ($item eq 'canclone') {
13095:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13096:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13097:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
13098:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
13099:                         }
13100:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
13101:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
13102:                     } else {
13103:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>'; 
13104:                     }
13105:                 }
13106:             }
13107:             $resulttext .= '</ul>';
13108:         } else {
13109:             $resulttext = &mt('No changes made to course defaults');
13110:         }
13111:     } else {
13112:         $resulttext = '<span class="LC_error">'.
13113:             &mt('An error occurred: [_1]',$putresult).'</span>';
13114:     }
13115:     return $resulttext;
13116: }
13117: 
13118: sub modify_selfenrollment {
13119:     my ($dom,$lastactref,%domconfig) = @_;
13120:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
13121:     my @types = ('official','unofficial','community','textbook','placement');
13122:     my %titles = &tool_titles();
13123:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
13124:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
13125:     $ordered{'default'} = ['types','registered','approval','limit'];
13126: 
13127:     my (%roles,%shown,%toplevel);
13128:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
13129: 
13130:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
13131:         if ($domconfig{'selfenrollment'} eq '') {
13132:             $domconfig{'selfenrollment'} = {};
13133:         }
13134:     }
13135:     %toplevel = (
13136:                   admin      => 'Configuration Rights',
13137:                   default    => 'Default settings',
13138:                   validation => 'Validation of self-enrollment requests',
13139:                 );
13140:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
13141: 
13142:     if (ref($ordered{'admin'}) eq 'ARRAY') {
13143:         foreach my $item (@{$ordered{'admin'}}) {
13144:             foreach my $type (@types) {
13145:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
13146:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
13147:                 } else {
13148:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
13149:                 }
13150:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
13151:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
13152:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
13153:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
13154:                             push(@{$changes{'admin'}{$type}},$item);
13155:                         }
13156:                     } else {
13157:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
13158:                             push(@{$changes{'admin'}{$type}},$item);
13159:                         }
13160:                     }
13161:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
13162:                     push(@{$changes{'admin'}{$type}},$item);
13163:                 }
13164:             }
13165:         }
13166:     }
13167: 
13168:     foreach my $item (@{$ordered{'default'}}) {
13169:         foreach my $type (@types) {
13170:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
13171:             if ($item eq 'types') {
13172:                 unless (($value eq 'all') || ($value eq 'dom')) {
13173:                     $value = '';
13174:                 }
13175:             } elsif ($item eq 'registered') {
13176:                 unless ($value eq '1') {
13177:                     $value = 0;
13178:                 }
13179:             } elsif ($item eq 'approval') {
13180:                 unless ($value =~ /^[012]$/) {
13181:                     $value = 0;
13182:                 }
13183:             } else {
13184:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
13185:                     $value = 'none';
13186:                 }
13187:             }
13188:             $selfenrollhash{'default'}{$type}{$item} = $value;
13189:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
13190:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
13191:                     if ($selfenrollhash{'default'}{$type}{$item} ne
13192:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
13193:                          push(@{$changes{'default'}{$type}},$item);
13194:                     }
13195:                 } else {
13196:                     push(@{$changes{'default'}{$type}},$item);
13197:                 }
13198:             } else {
13199:                 push(@{$changes{'default'}{$type}},$item);
13200:             }
13201:             if ($item eq 'limit') {
13202:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
13203:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
13204:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
13205:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
13206:                     }
13207:                 } else {
13208:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
13209:                 }
13210:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
13211:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
13212:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
13213:                          push(@{$changes{'default'}{$type}},'cap');
13214:                     }
13215:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
13216:                     push(@{$changes{'default'}{$type}},'cap');
13217:                 }
13218:             }
13219:         }
13220:     }
13221: 
13222:     foreach my $item (@{$itemsref}) {
13223:         if ($item eq 'fields') {
13224:             my @changed;
13225:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
13226:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
13227:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
13228:             }
13229:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
13230:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
13231:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
13232:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
13233:                 } else {
13234:                     @changed = @{$selfenrollhash{'validation'}{$item}};
13235:                 }
13236:             } else {
13237:                 @changed = @{$selfenrollhash{'validation'}{$item}};
13238:             }
13239:             if (@changed) {
13240:                 if ($selfenrollhash{'validation'}{$item}) { 
13241:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
13242:                 } else {
13243:                     $changes{'validation'}{$item} = &mt('None');
13244:                 }
13245:             }
13246:         } else {
13247:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
13248:             if ($item eq 'markup') {
13249:                if ($env{'form.selfenroll_validation_'.$item}) {
13250:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
13251:                }
13252:             }
13253:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
13254:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
13255:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
13256:                 }
13257:             }
13258:         }
13259:     }
13260: 
13261:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
13262:                                              $dom);
13263:     if ($putresult eq 'ok') {
13264:         if (keys(%changes) > 0) {
13265:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13266:             $resulttext = &mt('Changes made:').'<ul>';
13267:             foreach my $key ('admin','default','validation') {
13268:                 if (ref($changes{$key}) eq 'HASH') {
13269:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
13270:                     if ($key eq 'validation') {
13271:                         foreach my $item (@{$itemsref}) {
13272:                             if (exists($changes{$key}{$item})) {
13273:                                 if ($item eq 'markup') {
13274:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
13275:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
13276:                                 } else {  
13277:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
13278:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
13279:                                 }
13280:                             }
13281:                         }
13282:                     } else {
13283:                         foreach my $type (@types) {
13284:                             if ($type eq 'community') {
13285:                                 $roles{'1'} = &mt('Community personnel');
13286:                             } else {
13287:                                 $roles{'1'} = &mt('Course personnel');
13288:                             }
13289:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
13290:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
13291:                                     if ($key eq 'admin') {
13292:                                         my @mgrdc = ();
13293:                                         if (ref($ordered{$key}) eq 'ARRAY') {
13294:                                             foreach my $item (@{$ordered{'admin'}}) {
13295:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
13296:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
13297:                                                         push(@mgrdc,$item);
13298:                                                     }
13299:                                                 }
13300:                                             }
13301:                                             if (@mgrdc) {
13302:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
13303:                                             } else {
13304:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
13305:                                             }
13306:                                         }
13307:                                     } else {
13308:                                         if (ref($ordered{$key}) eq 'ARRAY') {
13309:                                             foreach my $item (@{$ordered{$key}}) {
13310:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
13311:                                                     $domdefaults{$type.'selfenroll'.$item} =
13312:                                                         $selfenrollhash{$key}{$type}{$item};
13313:                                                 }
13314:                                             }
13315:                                         }
13316:                                     }
13317:                                 }
13318:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
13319:                                 foreach my $item (@{$ordered{$key}}) {
13320:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
13321:                                         $resulttext .= '<li>';
13322:                                         if ($key eq 'admin') {
13323:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
13324:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
13325:                                         } else {
13326:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
13327:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
13328:                                         }
13329:                                         $resulttext .= '</li>';
13330:                                     }
13331:                                 }
13332:                                 $resulttext .= '</ul></li>';
13333:                             }
13334:                         }
13335:                         $resulttext .= '</ul></li>'; 
13336:                     }
13337:                 }
13338:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
13339:                     my $cachetime = 24*60*60;
13340:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13341:                     if (ref($lastactref) eq 'HASH') {
13342:                         $lastactref->{'domdefaults'} = 1;
13343:                     }
13344:                 }
13345:             }
13346:             $resulttext .= '</ul>';
13347:         } else {
13348:             $resulttext = &mt('No changes made to self-enrollment settings');
13349:         }
13350:     } else {
13351:         $resulttext = '<span class="LC_error">'.
13352:             &mt('An error occurred: [_1]',$putresult).'</span>';
13353:     }
13354:     return $resulttext;
13355: }
13356: 
13357: sub modify_usersessions {
13358:     my ($dom,$lastactref,%domconfig) = @_;
13359:     my @hostingtypes = ('version','excludedomain','includedomain');
13360:     my @offloadtypes = ('primary','default');
13361:     my %types = (
13362:                   remote => \@hostingtypes,
13363:                   hosted => \@hostingtypes,
13364:                   spares => \@offloadtypes,
13365:                 );
13366:     my @prefixes = ('remote','hosted','spares');
13367:     my @lcversions = &Apache::lonnet::all_loncaparevs();
13368:     my (%by_ip,%by_location,@intdoms,@instdoms);
13369:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
13370:     my @locations = sort(keys(%by_location));
13371:     my (%defaultshash,%changes);
13372:     foreach my $prefix (@prefixes) {
13373:         $defaultshash{'usersessions'}{$prefix} = {};
13374:     }
13375:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13376:     my $resulttext;
13377:     my %iphost = &Apache::lonnet::get_iphost();
13378:     foreach my $prefix (@prefixes) {
13379:         next if ($prefix eq 'spares');
13380:         foreach my $type (@{$types{$prefix}}) {
13381:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
13382:             if ($type eq 'version') {
13383:                 my $value = $env{'form.'.$prefix.'_'.$type};
13384:                 my $okvalue;
13385:                 if ($value ne '') {
13386:                     if (grep(/^\Q$value\E$/,@lcversions)) {
13387:                         $okvalue = $value;
13388:                     }
13389:                 }
13390:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
13391:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
13392:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
13393:                             if ($inuse == 0) {
13394:                                 $changes{$prefix}{$type} = 1;
13395:                             } else {
13396:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
13397:                                     $changes{$prefix}{$type} = 1;
13398:                                 }
13399:                                 if ($okvalue ne '') {
13400:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13401:                                 } 
13402:                             }
13403:                         } else {
13404:                             if (($inuse == 1) && ($okvalue ne '')) {
13405:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13406:                                 $changes{$prefix}{$type} = 1;
13407:                             }
13408:                         }
13409:                     } else {
13410:                         if (($inuse == 1) && ($okvalue ne '')) {
13411:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13412:                             $changes{$prefix}{$type} = 1;
13413:                         }
13414:                     }
13415:                 } else {
13416:                     if (($inuse == 1) && ($okvalue ne '')) {
13417:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13418:                         $changes{$prefix}{$type} = 1;
13419:                     }
13420:                 }
13421:             } else {
13422:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
13423:                 my @okvals;
13424:                 foreach my $val (@vals) {
13425:                     if ($val =~ /:/) {
13426:                         my @items = split(/:/,$val);
13427:                         foreach my $item (@items) {
13428:                             if (ref($by_location{$item}) eq 'ARRAY') {
13429:                                 push(@okvals,$item);
13430:                             }
13431:                         }
13432:                     } else {
13433:                         if (ref($by_location{$val}) eq 'ARRAY') {
13434:                             push(@okvals,$val);
13435:                         }
13436:                     }
13437:                 }
13438:                 @okvals = sort(@okvals);
13439:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
13440:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
13441:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13442:                             if ($inuse == 0) {
13443:                                 $changes{$prefix}{$type} = 1; 
13444:                             } else {
13445:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13446:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
13447:                                 if (@changed > 0) {
13448:                                     $changes{$prefix}{$type} = 1;
13449:                                 }
13450:                             }
13451:                         } else {
13452:                             if ($inuse == 1) {
13453:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13454:                                 $changes{$prefix}{$type} = 1;
13455:                             }
13456:                         } 
13457:                     } else {
13458:                         if ($inuse == 1) {
13459:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13460:                             $changes{$prefix}{$type} = 1;
13461:                         }
13462:                     }
13463:                 } else {
13464:                     if ($inuse == 1) {
13465:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13466:                         $changes{$prefix}{$type} = 1;
13467:                     }
13468:                 }
13469:             }
13470:         }
13471:     }
13472: 
13473:     my @alldoms = &Apache::lonnet::all_domains();
13474:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13475:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
13476:     my $savespares;
13477: 
13478:     foreach my $lonhost (sort(keys(%servers))) {
13479:         my $serverhomeID =
13480:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
13481:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
13482:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
13483:         my %spareschg;
13484:         foreach my $type (@{$types{'spares'}}) {
13485:             my @okspares;
13486:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
13487:             foreach my $server (@checked) {
13488:                 if (&Apache::lonnet::hostname($server) ne '') {
13489:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
13490:                         unless (grep(/^\Q$server\E$/,@okspares)) {
13491:                             push(@okspares,$server);
13492:                         }
13493:                     }
13494:                 }
13495:             }
13496:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
13497:             my $newspare;
13498:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
13499:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
13500:                     $newspare = $new;
13501:                 }
13502:             }
13503:             my @spares;
13504:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
13505:                 @spares = sort(@okspares,$newspare);
13506:             } else {
13507:                 @spares = sort(@okspares);
13508:             }
13509:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
13510:             if (ref($spareid{$lonhost}) eq 'HASH') {
13511:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
13512:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
13513:                     if (@diffs > 0) {
13514:                         $spareschg{$type} = 1;
13515:                     }
13516:                 }
13517:             }
13518:         }
13519:         if (keys(%spareschg) > 0) {
13520:             $changes{'spares'}{$lonhost} = \%spareschg;
13521:         }
13522:     }
13523:     $defaultshash{'usersessions'}{'offloadnow'} = {};
13524:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
13525:     my @okoffload;
13526:     if (@offloadnow) {
13527:         foreach my $server (@offloadnow) {
13528:             if (&Apache::lonnet::hostname($server) ne '') {
13529:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
13530:                     push(@okoffload,$server);
13531:                 }
13532:             }
13533:         }
13534:         if (@okoffload) {
13535:             foreach my $lonhost (@okoffload) {
13536:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
13537:             }
13538:         }
13539:     }
13540:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
13541:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
13542:             if (ref($changes{'spares'}) eq 'HASH') {
13543:                 if (keys(%{$changes{'spares'}}) > 0) {
13544:                     $savespares = 1;
13545:                 }
13546:             }
13547:         } else {
13548:             $savespares = 1;
13549:         }
13550:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
13551:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
13552:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
13553:                     $changes{'offloadnow'} = 1;
13554:                     last;
13555:                 }
13556:             }
13557:             unless ($changes{'offloadnow'}) {
13558:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) { 
13559:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
13560:                         $changes{'offloadnow'} = 1;
13561:                         last;
13562:                     }
13563:                 }
13564:             }
13565:         } elsif (@okoffload) {
13566:             $changes{'offloadnow'} = 1;
13567:         }
13568:     } elsif (@okoffload) {
13569:         $changes{'offloadnow'} = 1;
13570:     }
13571:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
13572:     if ((keys(%changes) > 0) || ($savespares)) {
13573:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13574:                                                  $dom);
13575:         if ($putresult eq 'ok') {
13576:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13577:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
13578:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
13579:                 }
13580:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
13581:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
13582:                 }
13583:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13584:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
13585:                 }
13586:             }
13587:             my $cachetime = 24*60*60;
13588:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13589:             if (ref($lastactref) eq 'HASH') {
13590:                 $lastactref->{'domdefaults'} = 1;
13591:             }
13592:             if (keys(%changes) > 0) {
13593:                 my %lt = &usersession_titles();
13594:                 $resulttext = &mt('Changes made:').'<ul>';
13595:                 foreach my $prefix (@prefixes) {
13596:                     if (ref($changes{$prefix}) eq 'HASH') {
13597:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
13598:                         if ($prefix eq 'spares') {
13599:                             if (ref($changes{$prefix}) eq 'HASH') {
13600:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
13601:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
13602:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
13603:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
13604:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
13605:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
13606:                                         foreach my $type (@{$types{$prefix}}) {
13607:                                             if ($changes{$prefix}{$lonhost}{$type}) {
13608:                                                 my $offloadto = &mt('None');
13609:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
13610:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
13611:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
13612:                                                     }
13613:                                                 }
13614:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
13615:                                             }
13616:                                         }
13617:                                     }
13618:                                     $resulttext .= '</li>';
13619:                                 }
13620:                             }
13621:                         } else {
13622:                             foreach my $type (@{$types{$prefix}}) {
13623:                                 if (defined($changes{$prefix}{$type})) {
13624:                                     my $newvalue;
13625:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13626:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
13627:                                             if ($type eq 'version') {
13628:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
13629:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13630:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
13631:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
13632:                                                 }
13633:                                             }
13634:                                         }
13635:                                     }
13636:                                     if ($newvalue eq '') {
13637:                                         if ($type eq 'version') {
13638:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
13639:                                         } else {
13640:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
13641:                                         }
13642:                                     } else {
13643:                                         if ($type eq 'version') {
13644:                                             $newvalue .= ' '.&mt('(or later)'); 
13645:                                         }
13646:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
13647:                                     }
13648:                                 }
13649:                             }
13650:                         }
13651:                         $resulttext .= '</ul>';
13652:                     }
13653:                 }
13654:                 if ($changes{'offloadnow'}) {
13655:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13656:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
13657:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
13658:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
13659:                                 $resulttext .= '<li>'.$lonhost.'</li>';
13660:                             }
13661:                             $resulttext .= '</ul>';
13662:                         } else {
13663:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
13664:                         }
13665:                     } else {
13666:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
13667:                     }
13668:                 }
13669:                 $resulttext .= '</ul>';
13670:             } else {
13671:                 $resulttext = $nochgmsg;
13672:             }
13673:         } else {
13674:             $resulttext = '<span class="LC_error">'.
13675:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13676:         }
13677:     } else {
13678:         $resulttext = $nochgmsg;
13679:     }
13680:     return $resulttext;
13681: }
13682: 
13683: sub modify_ssl {
13684:     my ($dom,$lastactref,%domconfig) = @_;
13685:     my (%by_ip,%by_location,@intdoms,@instdoms);
13686:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
13687:     my @locations = sort(keys(%by_location));
13688:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13689:     my (%defaultshash,%changes);
13690:     my $action = 'ssl';
13691:     my @prefixes = ('connect','replication');
13692:     foreach my $prefix (@prefixes) {
13693:         $defaultshash{$action}{$prefix} = {};
13694:     }
13695:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13696:     my $resulttext;
13697:     my %iphost = &Apache::lonnet::get_iphost();
13698:     my @reptypes = ('certreq','nocertreq');
13699:     my @connecttypes = ('dom','intdom','other');
13700:     my %types = (
13701:                   connect      => \@connecttypes,
13702:                   replication  => \@reptypes,
13703:                 );
13704:     foreach my $prefix (sort(keys(%types))) {
13705:         foreach my $type (@{$types{$prefix}}) {
13706:             if ($prefix eq 'connect') {
13707:                 my $value = 'yes';
13708:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
13709:                     $value = $env{'form.'.$prefix.'_'.$type};
13710:                 }
13711:                 if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
13712:                     if ($domconfig{$action}{$prefix}{$type} ne '') {
13713:                         if ($value ne $domconfig{$action}{$prefix}{$type}) {
13714:                             $changes{$prefix}{$type} = 1;
13715:                         }
13716:                         $defaultshash{$action}{$prefix}{$type} = $value;
13717:                     } else {
13718:                         $defaultshash{$action}{$prefix}{$type} = $value;
13719:                         $changes{$prefix}{$type} = 1;
13720:                     }
13721:                 } else {
13722:                     $defaultshash{$action}{$prefix}{$type} = $value;
13723:                     $changes{$prefix}{$type} = 1;
13724:                 }
13725:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
13726:                     delete($changes{$prefix}{$type});
13727:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
13728:                     delete($changes{$prefix}{$type});
13729:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
13730:                     delete($changes{$prefix}{$type});
13731:                 }
13732:             } elsif ($prefix eq 'replication') {
13733:                 if (@locations > 0) {
13734:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
13735:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
13736:                     my @okvals;
13737:                     foreach my $val (@vals) {
13738:                         if ($val =~ /:/) {
13739:                             my @items = split(/:/,$val);
13740:                             foreach my $item (@items) {
13741:                                 if (ref($by_location{$item}) eq 'ARRAY') {
13742:                                     push(@okvals,$item);
13743:                                 }
13744:                             }
13745:                         } else {
13746:                             if (ref($by_location{$val}) eq 'ARRAY') {
13747:                                 push(@okvals,$val);
13748:                             }
13749:                         }
13750:                     }
13751:                     @okvals = sort(@okvals);
13752:                     if (ref($domconfig{$action}) eq 'HASH') {
13753:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
13754:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
13755:                                 if ($inuse == 0) {
13756:                                     $changes{$prefix}{$type} = 1;
13757:                                 } else {
13758:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
13759:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
13760:                                     if (@changed > 0) {
13761:                                         $changes{$prefix}{$type} = 1;
13762:                                     }
13763:                                 }
13764:                             } else {
13765:                                 if ($inuse == 1) {
13766:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
13767:                                     $changes{$prefix}{$type} = 1;
13768:                                 }
13769:                             }
13770:                         } else {
13771:                             if ($inuse == 1) {
13772:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
13773:                                 $changes{$prefix}{$type} = 1;
13774:                             }
13775:                         }
13776:                     } else {
13777:                         if ($inuse == 1) {
13778:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
13779:                             $changes{$prefix}{$type} = 1;
13780:                         }
13781:                     }
13782:                 }
13783:             }
13784:         }
13785:     }
13786:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
13787:     if (keys(%changes) > 0) {
13788:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13789:                                                  $dom);
13790:         if ($putresult eq 'ok') {
13791:             if (ref($defaultshash{$action}) eq 'HASH') {
13792:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
13793:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
13794:                 }
13795:                 if (ref($defaultshash{$action}{'connect'}) eq 'HASH') {
13796:                     $domdefaults{'connect'} = $domconfig{$action}{'connect'};
13797:                 }
13798:             }
13799:             my $cachetime = 24*60*60;
13800:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13801:             if (ref($lastactref) eq 'HASH') {
13802:                 $lastactref->{'domdefaults'} = 1;
13803:             }
13804:             if (keys(%changes) > 0) {
13805:                 my %titles = &ssl_titles();
13806:                 $resulttext = &mt('Changes made:').'<ul>';
13807:                 foreach my $prefix (@prefixes) {
13808:                     if (ref($changes{$prefix}) eq 'HASH') {
13809:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
13810:                         foreach my $type (@{$types{$prefix}}) {
13811:                             if (defined($changes{$prefix}{$type})) {
13812:                                 my $newvalue;
13813:                                 if (ref($defaultshash{$action}) eq 'HASH') {
13814:                                     if (ref($defaultshash{$action}{$prefix})) {
13815:                                         if ($prefix eq 'connect') {
13816:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
13817:                                         } elsif (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
13818:                                             if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
13819:                                                 $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
13820:                                             }
13821:                                         }
13822:                                     }
13823:                                     if ($newvalue eq '') {
13824:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
13825:                                     } else {
13826:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
13827:                                     }
13828:                                 }
13829:                             }
13830:                         }
13831:                         $resulttext .= '</ul>';
13832:                     }
13833:                 }
13834:             } else {
13835:                 $resulttext = $nochgmsg;
13836:             }
13837:         } else {
13838:             $resulttext = '<span class="LC_error">'.
13839:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13840:         }
13841:     } else {
13842:         $resulttext = $nochgmsg;
13843:     }
13844:     return $resulttext;
13845: }
13846: 
13847: sub modify_trust {
13848:     my ($dom,$lastactref,%domconfig) = @_;
13849:     my (%by_ip,%by_location,@intdoms,@instdoms);
13850:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
13851:     my @locations = sort(keys(%by_location));
13852:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
13853:     my @types = ('exc','inc');
13854:     my (%defaultshash,%changes);
13855:     foreach my $prefix (@prefixes) {
13856:         $defaultshash{'trust'}{$prefix} = {};
13857:     }
13858:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13859:     my $resulttext;
13860:     foreach my $prefix (@prefixes) {
13861:         foreach my $type (@types) {
13862:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
13863:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
13864:             my @okvals;
13865:             foreach my $val (@vals) {
13866:                 if ($val =~ /:/) {
13867:                     my @items = split(/:/,$val);
13868:                     foreach my $item (@items) {
13869:                         if (ref($by_location{$item}) eq 'ARRAY') {
13870:                             push(@okvals,$item);
13871:                         }
13872:                     }
13873:                 } else {
13874:                     if (ref($by_location{$val}) eq 'ARRAY') {
13875:                         push(@okvals,$val);
13876:                     }
13877:                 }
13878:             }
13879:             @okvals = sort(@okvals);
13880:             if (ref($domconfig{'trust'}) eq 'HASH') {
13881:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
13882:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
13883:                         if ($inuse == 0) {
13884:                             $changes{$prefix}{$type} = 1;
13885:                         } else {
13886:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
13887:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
13888:                             if (@changed > 0) {
13889:                                 $changes{$prefix}{$type} = 1;
13890:                             }
13891:                         }
13892:                     } else {
13893:                         if ($inuse == 1) {
13894:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
13895:                             $changes{$prefix}{$type} = 1;
13896:                         }
13897:                     }
13898:                 } else {
13899:                     if ($inuse == 1) {
13900:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
13901:                         $changes{$prefix}{$type} = 1;
13902:                     }
13903:                 }
13904:             } else {
13905:                 if ($inuse == 1) {
13906:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
13907:                     $changes{$prefix}{$type} = 1;
13908:                 }
13909:             }
13910:         }
13911:     }
13912:     my $nochgmsg = &mt('No changes made to trust settings.');
13913:     if (keys(%changes) > 0) {
13914:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13915:                                                  $dom);
13916:         if ($putresult eq 'ok') {
13917:             if (ref($defaultshash{'trust'}) eq 'HASH') {
13918:                 foreach my $prefix (@prefixes) {
13919:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
13920:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
13921:                     }
13922:                 }
13923:             }
13924:             my $cachetime = 24*60*60;
13925:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13926:             if (ref($lastactref) eq 'HASH') {
13927:                 $lastactref->{'domdefaults'} = 1;
13928:             }
13929:             if (keys(%changes) > 0) {
13930:                 my %lt = &trust_titles();
13931:                 $resulttext = &mt('Changes made:').'<ul>';
13932:                 foreach my $prefix (@prefixes) {
13933:                     if (ref($changes{$prefix}) eq 'HASH') {
13934:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
13935:                         foreach my $type (@types) {
13936:                             if (defined($changes{$prefix}{$type})) {
13937:                                 my $newvalue;
13938:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
13939:                                     if (ref($defaultshash{'trust'}{$prefix})) {
13940:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
13941:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
13942:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
13943:                                             }
13944:                                         }
13945:                                     }
13946:                                 }
13947:                                 if ($newvalue eq '') {
13948:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
13949:                                 } else {
13950:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
13951:                                 }
13952:                             }
13953:                         }
13954:                         $resulttext .= '</ul>';
13955:                     }
13956:                 }
13957:                 $resulttext .= '</ul>';
13958:             } else {
13959:                 $resulttext = $nochgmsg;
13960:             }
13961:         } else {
13962:             $resulttext = '<span class="LC_error">'.
13963:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13964:         }
13965:     } else {
13966:         $resulttext = $nochgmsg;
13967:     }
13968:     return $resulttext;
13969: }
13970: 
13971: sub modify_loadbalancing {
13972:     my ($dom,%domconfig) = @_;
13973:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
13974:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
13975:     my ($othertitle,$usertypes,$types) =
13976:         &Apache::loncommon::sorted_inst_types($dom);
13977:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13978:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
13979:     my @sparestypes = ('primary','default');
13980:     my %typetitles = &sparestype_titles();
13981:     my $resulttext;
13982:     my (%currbalancer,%currtargets,%currrules,%existing);
13983:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
13984:         %existing = %{$domconfig{'loadbalancing'}};
13985:     }
13986:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
13987:                               \%currtargets,\%currrules);
13988:     my ($saveloadbalancing,%defaultshash,%changes);
13989:     my ($alltypes,$othertypes,$titles) =
13990:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
13991:     my %ruletitles = &offloadtype_text();
13992:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
13993:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
13994:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
13995:         if ($balancer eq '') {
13996:             next;
13997:         }
13998:         if (!exists($servers{$balancer})) {
13999:             if (exists($currbalancer{$balancer})) {
14000:                 push(@{$changes{'delete'}},$balancer);
14001:             }
14002:             next;
14003:         }
14004:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
14005:             push(@{$changes{'delete'}},$balancer);
14006:             next;
14007:         }
14008:         if (!exists($currbalancer{$balancer})) {
14009:             push(@{$changes{'add'}},$balancer);
14010:         }
14011:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
14012:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
14013:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
14014:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
14015:             $saveloadbalancing = 1;
14016:         }
14017:         foreach my $sparetype (@sparestypes) {
14018:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
14019:             my @offloadto;
14020:             foreach my $target (@targets) {
14021:                 if (($servers{$target}) && ($target ne $balancer)) {
14022:                     if ($sparetype eq 'default') {
14023:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
14024:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
14025:                         }
14026:                     }
14027:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
14028:                         push(@offloadto,$target);
14029:                     }
14030:                 }
14031:             }
14032:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
14033:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
14034:                     push(@offloadto,$balancer);
14035:                 }
14036:             }
14037:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
14038:         }
14039:         if (ref($currtargets{$balancer}) eq 'HASH') {
14040:             foreach my $sparetype (@sparestypes) {
14041:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
14042:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
14043:                     if (@targetdiffs > 0) {
14044:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14045:                     }
14046:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14047:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14048:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14049:                     }
14050:                 }
14051:             }
14052:         } else {
14053:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
14054:                 foreach my $sparetype (@sparestypes) {
14055:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14056:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14057:                             $changes{'curr'}{$balancer}{'targets'} = 1;
14058:                         }
14059:                     }
14060:                 }
14061:             }
14062:         }
14063:         my $ishomedom;
14064:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
14065:             $ishomedom = 1;
14066:         }
14067:         if (ref($alltypes) eq 'ARRAY') {
14068:             foreach my $type (@{$alltypes}) {
14069:                 my $rule;
14070:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
14071:                          (!$ishomedom)) {
14072:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
14073:                 }
14074:                 if ($rule eq 'specific') {
14075:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
14076:                     if (exists($servers{$specifiedhost})) { 
14077:                         $rule = $specifiedhost;
14078:                     }
14079:                 }
14080:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
14081:                 if (ref($currrules{$balancer}) eq 'HASH') {
14082:                     if ($rule ne $currrules{$balancer}{$type}) {
14083:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14084:                     }
14085:                 } elsif ($rule ne '') {
14086:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14087:                 }
14088:             }
14089:         }
14090:     }
14091:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
14092:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
14093:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
14094:             $defaultshash{'loadbalancing'} = {};
14095:         }
14096:         my $putresult = &Apache::lonnet::put_dom('configuration',
14097:                                                  \%defaultshash,$dom);
14098:         if ($putresult eq 'ok') {
14099:             if (keys(%changes) > 0) {
14100:                 my %toupdate;
14101:                 if (ref($changes{'delete'}) eq 'ARRAY') {
14102:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
14103:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
14104:                         $toupdate{$balancer} = 1;
14105:                     }
14106:                 }
14107:                 if (ref($changes{'add'}) eq 'ARRAY') {
14108:                     foreach my $balancer (sort(@{$changes{'add'}})) {
14109:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
14110:                         $toupdate{$balancer} = 1;
14111:                     }
14112:                 }
14113:                 if (ref($changes{'curr'}) eq 'HASH') {
14114:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
14115:                         $toupdate{$balancer} = 1;
14116:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
14117:                             if ($changes{'curr'}{$balancer}{'targets'}) {
14118:                                 my %offloadstr;
14119:                                 foreach my $sparetype (@sparestypes) {
14120:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14121:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14122:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14123:                                         }
14124:                                     }
14125:                                 }
14126:                                 if (keys(%offloadstr) == 0) {
14127:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
14128:                                 } else {
14129:                                     my $showoffload;
14130:                                     foreach my $sparetype (@sparestypes) {
14131:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
14132:                                         if (defined($offloadstr{$sparetype})) {
14133:                                             $showoffload .= $offloadstr{$sparetype};
14134:                                         } else {
14135:                                             $showoffload .= &mt('None');
14136:                                         }
14137:                                         $showoffload .= ('&nbsp;'x3);
14138:                                     }
14139:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
14140:                                 }
14141:                             }
14142:                         }
14143:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
14144:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
14145:                                 foreach my $type (@{$alltypes}) {
14146:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
14147:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
14148:                                         my $balancetext;
14149:                                         if ($rule eq '') {
14150:                                             $balancetext =  $ruletitles{'default'};
14151:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
14152:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) { 
14153:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
14154:                                                 foreach my $sparetype (@sparestypes) {
14155:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14156:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14157:                                                     }
14158:                                                 }
14159:                                                 foreach my $item (@{$alltypes}) {
14160:                                                     next if ($item =~  /^_LC_ipchange/);
14161:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
14162:                                                     if ($hasrule eq 'homeserver') {
14163:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
14164:                                                     } else {
14165:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
14166:                                                             if ($servers{$hasrule}) {
14167:                                                                 $toupdate{$hasrule} = 1;
14168:                                                             }
14169:                                                         }
14170:                                                     }
14171:                                                 }
14172:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
14173:                                                     $balancetext =  $ruletitles{$rule};
14174:                                                 } else {
14175:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
14176:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
14177:                                                     if ($receiver) {
14178:                                                         $toupdate{$receiver};
14179:                                                     }
14180:                                                 }
14181:                                             } else {
14182:                                                 $balancetext =  $ruletitles{$rule};
14183:                                             }
14184:                                         } else {
14185:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
14186:                                         }
14187:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
14188:                                     }
14189:                                 }
14190:                             }
14191:                         }
14192:                         if (keys(%toupdate)) {
14193:                             my %thismachine;
14194:                             my $updatedhere;
14195:                             my $cachetime = 60*60*24;
14196:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
14197:                             foreach my $lonhost (keys(%toupdate)) {
14198:                                 if ($thismachine{$lonhost}) {
14199:                                     unless ($updatedhere) {
14200:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
14201:                                                                       $defaultshash{'loadbalancing'},
14202:                                                                       $cachetime);
14203:                                         $updatedhere = 1;
14204:                                     }
14205:                                 } else {
14206:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
14207:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
14208:                                 }
14209:                             }
14210:                         }
14211:                     }
14212:                 }
14213:                 if ($resulttext ne '') {
14214:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
14215:                 } else {
14216:                     $resulttext = $nochgmsg;
14217:                 }
14218:             } else {
14219:                 $resulttext = $nochgmsg;
14220:             }
14221:         } else {
14222:             $resulttext = '<span class="LC_error">'.
14223:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14224:         }
14225:     } else {
14226:         $resulttext = $nochgmsg;
14227:     }
14228:     return $resulttext;
14229: }
14230: 
14231: sub recurse_check {
14232:     my ($chkcats,$categories,$depth,$name) = @_;
14233:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
14234:         my $chg = 0;
14235:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
14236:             my $category = $chkcats->[$depth]{$name}[$j];
14237:             my $item;
14238:             if ($category eq '') {
14239:                 $chg ++;
14240:             } else {
14241:                 my $deeper = $depth + 1;
14242:                 $item = &escape($category).':'.&escape($name).':'.$depth;
14243:                 if ($chg) {
14244:                     $categories->{$item} -= $chg;
14245:                 }
14246:                 &recurse_check($chkcats,$categories,$deeper,$category);
14247:                 $deeper --;
14248:             }
14249:         }
14250:     }
14251:     return;
14252: }
14253: 
14254: sub recurse_cat_deletes {
14255:     my ($item,$coursecategories,$deletions) = @_;
14256:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
14257:     my $subdepth = $depth + 1;
14258:     if (ref($coursecategories) eq 'HASH') {
14259:         foreach my $subitem (keys(%{$coursecategories})) {
14260:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
14261:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
14262:                 delete($coursecategories->{$subitem});
14263:                 $deletions->{$subitem} = 1;
14264:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
14265:             }
14266:         }
14267:     }
14268:     return;
14269: }
14270: 
14271: sub active_dc_picker {
14272:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
14273:     my %domcoords = &get_active_dcs($dom);
14274:     my @domcoord = keys(%domcoords);
14275:     if (keys(%currhash)) {
14276:         foreach my $dc (keys(%currhash)) {
14277:             unless (exists($domcoords{$dc})) {
14278:                 push(@domcoord,$dc);
14279:             }
14280:         }
14281:     }
14282:     @domcoord = sort(@domcoord);
14283:     my $numdcs = scalar(@domcoord);
14284:     my $rows = 0;
14285:     my $table;
14286:     if ($numdcs > 1) {
14287:         $table = '<table>';
14288:         for (my $i=0; $i<@domcoord; $i++) {
14289:             my $rem = $i%($numinrow);
14290:             if ($rem == 0) {
14291:                 if ($i > 0) {
14292:                     $table .= '</tr>';
14293:                 }
14294:                 $table .= '<tr>';
14295:                 $rows ++;
14296:             }
14297:             my $check = '';
14298:             if ($inputtype eq 'radio') {
14299:                 if (keys(%currhash) == 0) {
14300:                     if (!$i) {
14301:                         $check = ' checked="checked"';
14302:                     }
14303:                 } elsif (exists($currhash{$domcoord[$i]})) {
14304:                     $check = ' checked="checked"';
14305:                 }
14306:             } else {
14307:                 if (exists($currhash{$domcoord[$i]})) {
14308:                     $check = ' checked="checked"';
14309:                 }
14310:             }
14311:             if ($i == @domcoord - 1) {
14312:                 my $colsleft = $numinrow - $rem;
14313:                 if ($colsleft > 1) {
14314:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
14315:                 } else {
14316:                     $table .= '<td class="LC_left_item">';
14317:                 }
14318:             } else {
14319:                 $table .= '<td class="LC_left_item">';
14320:             }
14321:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
14322:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14323:             $table .= '<span class="LC_nobreak"><label>'.
14324:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
14325:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
14326:             if ($user ne $dcname.':'.$dcdom) {
14327:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14328:             }
14329:             $table .= '</label></span></td>';
14330:         }
14331:         $table .= '</tr></table>';
14332:     } elsif ($numdcs == 1) {
14333:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
14334:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14335:         if ($inputtype eq 'radio') {
14336:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
14337:             if ($user ne $dcname.':'.$dcdom) {
14338:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14339:             }
14340:         } else {
14341:             my $check;
14342:             if (exists($currhash{$domcoord[0]})) {
14343:                 $check = ' checked="checked"';
14344:             }
14345:             $table = '<span class="LC_nobreak"><label>'.
14346:                      '<input type="checkbox" name="'.$name.'" '.
14347:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
14348:             if ($user ne $dcname.':'.$dcdom) {
14349:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14350:             }
14351:             $table .= '</label></span>';
14352:             $rows ++;
14353:         }
14354:     }
14355:     return ($numdcs,$table,$rows);
14356: }
14357: 
14358: sub usersession_titles {
14359:     return &Apache::lonlocal::texthash(
14360:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
14361:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
14362:                spares => 'Servers offloaded to, when busy',
14363:                version => 'LON-CAPA version requirement',
14364:                excludedomain => 'Allow all, but exclude specific domains',
14365:                includedomain => 'Deny all, but include specific domains',
14366:                primary => 'Primary (checked first)',
14367:                default => 'Default',
14368:            );
14369: }
14370: 
14371: sub id_for_thisdom {
14372:     my (%servers) = @_;
14373:     my %altids;
14374:     foreach my $server (keys(%servers)) {
14375:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
14376:         if ($serverhome ne $server) {
14377:             $altids{$serverhome} = $server;
14378:         }
14379:     }
14380:     return %altids;
14381: }
14382: 
14383: sub count_servers {
14384:     my ($currbalancer,%servers) = @_;
14385:     my (@spares,$numspares);
14386:     foreach my $lonhost (sort(keys(%servers))) {
14387:         next if ($currbalancer eq $lonhost);
14388:         push(@spares,$lonhost);
14389:     }
14390:     if ($currbalancer) {
14391:         $numspares = scalar(@spares);
14392:     } else {
14393:         $numspares = scalar(@spares) - 1;
14394:     }
14395:     return ($numspares,@spares);
14396: }
14397: 
14398: sub lonbalance_targets_js {
14399:     my ($dom,$types,$servers,$settings) = @_;
14400:     my $select = &mt('Select');
14401:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
14402:     if (ref($servers) eq 'HASH') {
14403:         $alltargets = join("','",sort(keys(%{$servers})));
14404:         my @homedoms;
14405:         foreach my $server (sort(keys(%{$servers}))) {
14406:             if (&Apache::lonnet::host_domain($server) eq $dom) {
14407:                 push(@homedoms,'1');
14408:             } else {
14409:                 push(@homedoms,'0');
14410:             }
14411:         }
14412:         $allishome = join("','",@homedoms);
14413:     }
14414:     if (ref($types) eq 'ARRAY') {
14415:         if (@{$types} > 0) {
14416:             @alltypes = @{$types};
14417:         }
14418:     }
14419:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
14420:     $allinsttypes = join("','",@alltypes);
14421:     my (%currbalancer,%currtargets,%currrules,%existing);
14422:     if (ref($settings) eq 'HASH') {
14423:         %existing = %{$settings};
14424:     }
14425:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
14426:                               \%currtargets,\%currrules);
14427:     my $balancers = join("','",sort(keys(%currbalancer)));
14428:     return <<"END";
14429: 
14430: <script type="text/javascript">
14431: // <![CDATA[
14432: 
14433: currBalancers = new Array('$balancers');
14434: 
14435: function toggleTargets(balnum) {
14436:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
14437:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
14438:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
14439:     var prevbalancer = prevhostitem.value;
14440:     var baltotal = document.getElementById('loadbalancing_total').value;
14441:     prevhostitem.value = balancer;
14442:     if (prevbalancer != '') {
14443:         var prevIdx = currBalancers.indexOf(prevbalancer);
14444:         if (prevIdx != -1) {
14445:             currBalancers.splice(prevIdx,1);
14446:         }
14447:     }
14448:     if (balancer == '') {
14449:         hideSpares(balnum);
14450:     } else {
14451:         var currIdx = currBalancers.indexOf(balancer);
14452:         if (currIdx == -1) {
14453:             currBalancers.push(balancer);
14454:         }
14455:         var homedoms = new Array('$allishome');
14456:         var ishomedom = homedoms[lonhostitem.selectedIndex];
14457:         showSpares(balancer,ishomedom,balnum);
14458:     }
14459:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
14460:     return;
14461: }
14462: 
14463: function showSpares(balancer,ishomedom,balnum) {
14464:     var alltargets = new Array('$alltargets');
14465:     var insttypes = new Array('$allinsttypes');
14466:     var offloadtypes = new Array('primary','default');
14467: 
14468:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
14469:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
14470:  
14471:     for (var i=0; i<offloadtypes.length; i++) {
14472:         var count = 0;
14473:         for (var j=0; j<alltargets.length; j++) {
14474:             if (alltargets[j] != balancer) {
14475:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
14476:                 item.value = alltargets[j];
14477:                 item.style.textAlign='left';
14478:                 item.style.textFace='normal';
14479:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
14480:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
14481:                     item.disabled = '';
14482:                 } else {
14483:                     item.disabled = 'disabled';
14484:                     item.checked = false;
14485:                 }
14486:                 count ++;
14487:             }
14488:         }
14489:     }
14490:     for (var k=0; k<insttypes.length; k++) {
14491:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
14492:             if (ishomedom == 1) {
14493:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
14494:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
14495:             } else {
14496:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
14497:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
14498:             }
14499:         } else {
14500:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
14501:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
14502:         }
14503:         if ((insttypes[k] != '_LC_external') && 
14504:             ((insttypes[k] != '_LC_internetdom') ||
14505:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
14506:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
14507:             item.options.length = 0;
14508:             item.options[0] = new Option("","",true,true);
14509:             var idx = 0;
14510:             for (var m=0; m<alltargets.length; m++) {
14511:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
14512:                     idx ++;
14513:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
14514:                 }
14515:             }
14516:         }
14517:     }
14518:     return;
14519: }
14520: 
14521: function hideSpares(balnum) {
14522:     var alltargets = new Array('$alltargets');
14523:     var insttypes = new Array('$allinsttypes');
14524:     var offloadtypes = new Array('primary','default');
14525: 
14526:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
14527:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
14528: 
14529:     var total = alltargets.length - 1;
14530:     for (var i=0; i<offloadtypes; i++) {
14531:         for (var j=0; j<total; j++) {
14532:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
14533:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
14534:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
14535:         }
14536:     }
14537:     for (var k=0; k<insttypes.length; k++) {
14538:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
14539:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
14540:         if (insttypes[k] != '_LC_external') {
14541:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
14542:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
14543:         }
14544:     }
14545:     return;
14546: }
14547: 
14548: function checkOffloads(item,balnum,type) {
14549:     var alltargets = new Array('$alltargets');
14550:     var offloadtypes = new Array('primary','default');
14551:     if (item.checked) {
14552:         var total = alltargets.length - 1;
14553:         var other;
14554:         if (type == offloadtypes[0]) {
14555:             other = offloadtypes[1];
14556:         } else {
14557:             other = offloadtypes[0];
14558:         }
14559:         for (var i=0; i<total; i++) {
14560:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
14561:             if (server == item.value) {
14562:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
14563:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
14564:                 }
14565:             }
14566:         }
14567:     }
14568:     return;
14569: }
14570: 
14571: function singleServerToggle(balnum,type) {
14572:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
14573:     if (offloadtoSelIdx == 0) {
14574:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
14575:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
14576: 
14577:     } else {
14578:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
14579:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
14580:     }
14581:     return;
14582: }
14583: 
14584: function balanceruleChange(formname,balnum,type) {
14585:     if (type == '_LC_external') {
14586:         return;
14587:     }
14588:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
14589:     for (var i=0; i<typesRules.length; i++) {
14590:         if (formname.elements[typesRules[i]].checked) {
14591:             if (formname.elements[typesRules[i]].value != 'specific') {
14592:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
14593:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
14594:             } else {
14595:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
14596:             }
14597:         }
14598:     }
14599:     return;
14600: }
14601: 
14602: function balancerDeleteChange(balnum) {
14603:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
14604:     var baltotal = document.getElementById('loadbalancing_total').value;
14605:     var addtarget;
14606:     var removetarget;
14607:     var action = 'delete';
14608:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
14609:         var lonhost = hostitem.value;
14610:         var currIdx = currBalancers.indexOf(lonhost);
14611:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
14612:             if (currIdx != -1) {
14613:                 currBalancers.splice(currIdx,1);
14614:             }
14615:             addtarget = lonhost;
14616:         } else {
14617:             if (currIdx == -1) {
14618:                 currBalancers.push(lonhost);
14619:             }
14620:             removetarget = lonhost;
14621:             action = 'undelete';
14622:         }
14623:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
14624:     }
14625:     return;
14626: }
14627: 
14628: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
14629:     if (baltotal > 1) {
14630:         var offloadtypes = new Array('primary','default');
14631:         var alltargets = new Array('$alltargets');
14632:         var insttypes = new Array('$allinsttypes');
14633:         for (var i=0; i<baltotal; i++) {
14634:             if (i != balnum) {
14635:                 for (var j=0; j<offloadtypes.length; j++) {
14636:                     var total = alltargets.length - 1;
14637:                     for (var k=0; k<total; k++) {
14638:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
14639:                         var server = serveritem.value;
14640:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
14641:                             if (server == addtarget) {
14642:                                 serveritem.disabled = '';
14643:                             }
14644:                         }
14645:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
14646:                             if (server == removetarget) {
14647:                                 serveritem.disabled = 'disabled';
14648:                                 serveritem.checked = false;
14649:                             }
14650:                         }
14651:                     }
14652:                 }
14653:                 for (var j=0; j<insttypes.length; j++) {
14654:                     if (insttypes[j] != '_LC_external') {
14655:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
14656:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
14657:                             var currSel = singleserver.selectedIndex;
14658:                             var currVal = singleserver.options[currSel].value;
14659:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
14660:                                 var numoptions = singleserver.options.length;
14661:                                 var needsnew = 1;
14662:                                 for (var k=0; k<numoptions; k++) {
14663:                                     if (singleserver.options[k] == addtarget) {
14664:                                         needsnew = 0;
14665:                                         break;
14666:                                     }
14667:                                 }
14668:                                 if (needsnew == 1) {
14669:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
14670:                                 }
14671:                             }
14672:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
14673:                                 singleserver.options.length = 0;
14674:                                 if ((currVal) && (currVal != removetarget)) {
14675:                                     singleserver.options[0] = new Option("","",false,false);
14676:                                 } else {
14677:                                     singleserver.options[0] = new Option("","",true,true);
14678:                                 }
14679:                                 var idx = 0;
14680:                                 for (var m=0; m<alltargets.length; m++) {
14681:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
14682:                                         idx ++;
14683:                                         if (currVal == alltargets[m]) {
14684:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
14685:                                         } else {
14686:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
14687:                                         }
14688:                                     }
14689:                                 }
14690:                             }
14691:                         }
14692:                     }
14693:                 }
14694:             }
14695:         }
14696:     }
14697:     return;
14698: }
14699: 
14700: // ]]>
14701: </script>
14702: 
14703: END
14704: }
14705: 
14706: sub new_spares_js {
14707:     my @sparestypes = ('primary','default');
14708:     my $types = join("','",@sparestypes);
14709:     my $select = &mt('Select');
14710:     return <<"END";
14711: 
14712: <script type="text/javascript">
14713: // <![CDATA[
14714: 
14715: function updateNewSpares(formname,lonhost) {
14716:     var types = new Array('$types');
14717:     var include = new Array();
14718:     var exclude = new Array();
14719:     for (var i=0; i<types.length; i++) {
14720:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
14721:         for (var j=0; j<spareboxes.length; j++) {
14722:             if (formname.elements[spareboxes[j]].checked) {
14723:                 exclude.push(formname.elements[spareboxes[j]].value);
14724:             } else {
14725:                 include.push(formname.elements[spareboxes[j]].value);
14726:             }
14727:         }
14728:     }
14729:     for (var i=0; i<types.length; i++) {
14730:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
14731:         var selIdx = newSpare.selectedIndex;
14732:         var currnew = newSpare.options[selIdx].value;
14733:         var okSpares = new Array();
14734:         for (var j=0; j<newSpare.options.length; j++) {
14735:             var possible = newSpare.options[j].value;
14736:             if (possible != '') {
14737:                 if (exclude.indexOf(possible) == -1) {
14738:                     okSpares.push(possible);
14739:                 } else {
14740:                     if (currnew == possible) {
14741:                         selIdx = 0;
14742:                     }
14743:                 }
14744:             }
14745:         }
14746:         for (var k=0; k<include.length; k++) {
14747:             if (okSpares.indexOf(include[k]) == -1) {
14748:                 okSpares.push(include[k]);
14749:             }
14750:         }
14751:         okSpares.sort();
14752:         newSpare.options.length = 0;
14753:         if (selIdx == 0) {
14754:             newSpare.options[0] = new Option("$select","",true,true);
14755:         } else {
14756:             newSpare.options[0] = new Option("$select","",false,false);
14757:         }
14758:         for (var m=0; m<okSpares.length; m++) {
14759:             var idx = m+1;
14760:             var selThis = 0;
14761:             if (selIdx != 0) {
14762:                 if (okSpares[m] == currnew) {
14763:                     selThis = 1;
14764:                 }
14765:             }
14766:             if (selThis == 1) {
14767:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
14768:             } else {
14769:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
14770:             }
14771:         }
14772:     }
14773:     return;
14774: }
14775: 
14776: function checkNewSpares(lonhost,type) {
14777:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
14778:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
14779:     if (chosen != '') { 
14780:         var othertype;
14781:         var othernewSpare;
14782:         if (type == 'primary') {
14783:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
14784:         }
14785:         if (type == 'default') {
14786:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
14787:         }
14788:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
14789:             othernewSpare.selectedIndex = 0;
14790:         }
14791:     }
14792:     return;
14793: }
14794: 
14795: // ]]>
14796: </script>
14797: 
14798: END
14799: 
14800: }
14801: 
14802: sub common_domprefs_js {
14803:     return <<"END";
14804: 
14805: <script type="text/javascript">
14806: // <![CDATA[
14807: 
14808: function getIndicesByName(formname,item) {
14809:     var group = new Array();
14810:     for (var i=0;i<formname.elements.length;i++) {
14811:         if (formname.elements[i].name == item) {
14812:             group.push(formname.elements[i].id);
14813:         }
14814:     }
14815:     return group;
14816: }
14817: 
14818: // ]]>
14819: </script>
14820: 
14821: END
14822: 
14823: }
14824: 
14825: sub recaptcha_js {
14826:     my %lt = &captcha_phrases();
14827:     return <<"END";
14828: 
14829: <script type="text/javascript">
14830: // <![CDATA[
14831: 
14832: function updateCaptcha(caller,context) {
14833:     var privitem;
14834:     var pubitem;
14835:     var privtext;
14836:     var pubtext;
14837:     var versionitem;
14838:     var versiontext;
14839:     if (document.getElementById(context+'_recaptchapub')) {
14840:         pubitem = document.getElementById(context+'_recaptchapub');
14841:     } else {
14842:         return;
14843:     }
14844:     if (document.getElementById(context+'_recaptchapriv')) {
14845:         privitem = document.getElementById(context+'_recaptchapriv');
14846:     } else {
14847:         return;
14848:     }
14849:     if (document.getElementById(context+'_recaptchapubtxt')) {
14850:         pubtext = document.getElementById(context+'_recaptchapubtxt');
14851:     } else {
14852:         return;
14853:     }
14854:     if (document.getElementById(context+'_recaptchaprivtxt')) {
14855:         privtext = document.getElementById(context+'_recaptchaprivtxt');
14856:     } else {
14857:         return;
14858:     }
14859:     if (document.getElementById(context+'_recaptchaversion')) {
14860:         versionitem = document.getElementById(context+'_recaptchaversion');
14861:     } else {
14862:         return;
14863:     }
14864:     if (document.getElementById(context+'_recaptchavertxt')) {
14865:         versiontext = document.getElementById(context+'_recaptchavertxt');
14866:     } else {
14867:         return;
14868:     }
14869:     if (caller.checked) {
14870:         if (caller.value == 'recaptcha') {
14871:             pubitem.type = 'text';
14872:             privitem.type = 'text';
14873:             pubitem.size = '40';
14874:             privitem.size = '40';
14875:             pubtext.innerHTML = "$lt{'pub'}";
14876:             privtext.innerHTML = "$lt{'priv'}";
14877:             versionitem.type = 'text';
14878:             versionitem.size = '3';
14879:             versiontext.innerHTML = "$lt{'ver'}"; 
14880:         } else {
14881:             pubitem.type = 'hidden';
14882:             privitem.type = 'hidden';
14883:             versionitem.type = 'hidden';
14884:             pubtext.innerHTML = '';
14885:             privtext.innerHTML = '';
14886:             versiontext.innerHTML = '';
14887:         }
14888:     }
14889:     return;
14890: }
14891: 
14892: // ]]>
14893: </script>
14894: 
14895: END
14896: 
14897: }
14898: 
14899: sub toggle_display_js {
14900:     return <<"END";
14901: 
14902: <script type="text/javascript">
14903: // <![CDATA[
14904: 
14905: function toggleDisplay(domForm,caller) {
14906:     if (document.getElementById(caller)) {
14907:         var divitem = document.getElementById(caller);
14908:         var optionsElement = domForm.coursecredits;
14909:         var checkval = 1;
14910:         var dispval = 'block';
14911:         if (caller == 'emailoptions') {
14912:             optionsElement = domForm.cancreate_email; 
14913:         }
14914:         if (caller == 'studentsubmission') {
14915:             optionsElement = domForm.postsubmit;
14916:         }
14917:         if (caller == 'cloneinstcode') {
14918:             optionsElement = domForm.canclone;
14919:             checkval = 'instcode';
14920:         }
14921:         if (optionsElement.length) {
14922:             var currval;
14923:             for (var i=0; i<optionsElement.length; i++) {
14924:                 if (optionsElement[i].checked) {
14925:                    currval = optionsElement[i].value;
14926:                 }
14927:             }
14928:             if (currval == checkval) {
14929:                 divitem.style.display = dispval;
14930:             } else {
14931:                 divitem.style.display = 'none';
14932:             }
14933:         }
14934:     }
14935:     return;
14936: }
14937: 
14938: // ]]>
14939: </script>
14940: 
14941: END
14942: 
14943: }
14944: 
14945: sub captcha_phrases {
14946:     return &Apache::lonlocal::texthash (
14947:                  priv => 'Private key',
14948:                  pub  => 'Public key',
14949:                  original  => 'original (CAPTCHA)',
14950:                  recaptcha => 'successor (ReCAPTCHA)',
14951:                  notused   => 'unused',
14952:                  ver => 'ReCAPTCHA version (1 or 2)', 
14953:     );
14954: }
14955: 
14956: sub devalidate_remote_domconfs {
14957:     my ($dom,$cachekeys) = @_;
14958:     return unless (ref($cachekeys) eq 'HASH');
14959:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14960:     my %thismachine;
14961:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
14962:     my @posscached = ('domainconfig','domdefaults','ltitools');
14963:     if (keys(%servers)) {
14964:         foreach my $server (keys(%servers)) {
14965:             next if ($thismachine{$server});
14966:             my @cached;
14967:             foreach my $name (@posscached) {
14968:                 if ($cachekeys->{$name}) {
14969:                     push(@cached,&escape($name).':'.&escape($dom));
14970:                 }
14971:             }
14972:             if (@cached) {
14973:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
14974:             }
14975:         }
14976:     }
14977:     return;
14978: }
14979: 
14980: 1;

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