File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.285: download - view: text, annotated - select for diffs
Mon Jan 2 19:44:06 2017 UTC (7 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Ad hoc roles for helpdesk personnel.
  - Domain configuration for:
    - which user(s) with dh role in domain may use each ad hoc role.
      - set by institutional status, or by include specific people, or by
        exclude specific people.
    - assign role description (displayed on Roles page, and at top left of
      page, when role active).
    - assign role order (numerical).
  - Course configuration (available for editing by course owner, and viewing
    by co-owner(s).
    - can override domain defaults for which dh role users can use ad hoc role(s)
      in course.
    - can override domain defaults set for course-level privileges for ad hoc
      role(s).

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.285 2017/01/02 19:44:06 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: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: #
   28: ###############################################################
   29: ##############################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::domainprefs.pm
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Handles configuration of a LON-CAPA domain.  
   40: 
   41: This is part of the LearningOnline Network with CAPA project
   42: described at http://www.lon-capa.org.
   43: 
   44: 
   45: =head1 OVERVIEW
   46: 
   47: Each institution using LON-CAPA will typically have a single domain designated 
   48: for use by individuals affiliated with the institution.  Accordingly, each domain
   49: may define a default set of logos and a color scheme which can be used to "brand"
   50: the LON-CAPA instance. In addition, an institution will typically have a language
   51: and timezone which are used for the majority of courses.
   52: 
   53: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   54: host of other domain-wide settings which determine the types of functionality
   55: available to users and courses in the domain.
   56: 
   57: There is also a mechanism to configure cataloging of courses in the domain, and
   58: controls on the operation of automated processes which govern such things as
   59: roster updates, user directory updates and processing of course requests.
   60: 
   61: The domain coordination manual which is built dynamically on install/update of 
   62: LON-CAPA from the relevant help items provides more information about domain 
   63: configuration.
   64: 
   65: Most of the domain settings are stored in the configuration.db GDBM file which is
   66: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   67: where $dom is the domain.  The configuration.db stores settings in a number of 
   68: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   69: the domain as files (e.g., image files for logos etc., or plain text files for
   70: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   71: session hosted on the primary library server in the domain, as these files are 
   72: stored in author space belonging to a special $dom-domainconfig user.   
   73: 
   74: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   75: the current settings, and provides an interface to make modifications.
   76: 
   77: =head1 SUBROUTINES
   78: 
   79: =over
   80: 
   81: =item print_quotas()
   82: 
   83: Inputs: 4 
   84: 
   85: $dom,$settings,$rowtotal,$action.
   86: 
   87: $dom is the domain, $settings is a reference to a hash of current settings for
   88: the current context, $rowtotal is a reference to the scalar used to record the 
   89: number of rows displayed on the page, and $action is the context (quotas, 
   90: requestcourses or requestauthor).
   91: 
   92: The print_quotas routine was orginally created to display/store information
   93: about default quota sizes for portfolio spaces for the different types of 
   94: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   95: but is now also used to manage availability of user tools: 
   96: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   97: used by course owners to request creation of a course, and to display/store
   98: default quota sizes for Authoring Spaces.
   99: 
  100: Outputs: 1
  101: 
  102: $datatable  - HTML containing form elements which allow settings to be changed. 
  103: 
  104: In the case of course requests, radio buttons are displayed for each institutional
  105: affiliate type (and also default, and _LC_adv) for each of the course types 
  106: (official, unofficial, community, textbook, and placement).  
  107: In each case the radio buttons allow the selection of one of four values:  
  108: 
  109: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  110: which have the following effects:
  111: 
  112: 0
  113: 
  114: =over
  115: 
  116: - course requests are not allowed for this course types/affiliation
  117: 
  118: =back
  119: 
  120: approval 
  121: 
  122: =over 
  123: 
  124: - course requests must be approved by a Doman Coordinator in the 
  125: course's domain
  126: 
  127: =back
  128: 
  129: validate 
  130: 
  131: =over
  132: 
  133: - an institutional validation (e.g., check requestor is instructor
  134: of record) needs to be passed before the course will be created.  The required
  135: validation is in localenroll.pm on the primary library server for the course 
  136: domain.
  137: 
  138: =back
  139: 
  140: autolimit 
  141: 
  142: =over
  143:  
  144: - course requests will be processed automatically up to a limit of
  145: N requests for the course type for the particular requestor.
  146: If N is undefined, there is no limit to the number of course requests
  147: which a course owner may submit and have processed automatically. 
  148: 
  149: =back
  150: 
  151: =item modify_quotas() 
  152: 
  153: =back
  154: 
  155: =cut
  156: 
  157: package Apache::domainprefs;
  158: 
  159: use strict;
  160: use Apache::Constants qw(:common :http);
  161: use Apache::lonnet;
  162: use Apache::loncommon();
  163: use Apache::lonhtmlcommon();
  164: use Apache::lonlocal;
  165: use Apache::lonmsg();
  166: use Apache::lonconfigsettings;
  167: use Apache::lonuserutils();
  168: use Apache::loncoursequeueadmin();
  169: use LONCAPA qw(:DEFAULT :match);
  170: use LONCAPA::Enrollment;
  171: use LONCAPA::lonauthcgi();
  172: use LONCAPA::SSL;
  173: use File::Copy;
  174: use Locale::Language;
  175: use DateTime::TimeZone;
  176: use DateTime::Locale;
  177: use Time::HiRes qw( sleep );
  178: 
  179: my $registered_cleanup;
  180: my $modified_urls;
  181: 
  182: sub handler {
  183:     my $r=shift;
  184:     if ($r->header_only) {
  185:         &Apache::loncommon::content_type($r,'text/html');
  186:         $r->send_http_header;
  187:         return OK;
  188:     }
  189: 
  190:     my $context = 'domain';
  191:     my $dom = $env{'request.role.domain'};
  192:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  193:     if (&Apache::lonnet::allowed('mau',$dom)) {
  194:         &Apache::loncommon::content_type($r,'text/html');
  195:         $r->send_http_header;
  196:     } else {
  197:         $env{'user.error.msg'}=
  198:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  199:         return HTTP_NOT_ACCEPTABLE;
  200:     }
  201: 
  202:     $registered_cleanup=0;
  203:     @{$modified_urls}=();
  204: 
  205:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  206:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  207:                                             ['phase','actions']);
  208:     my $phase = 'pickactions';
  209:     if ( exists($env{'form.phase'}) ) {
  210:         $phase = $env{'form.phase'};
  211:     }
  212:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  213:     my %domconfig =
  214:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  215:                 'quotas','autoenroll','autoupdate','autocreate',
  216:                 'directorysrch','usercreation','usermodification',
  217:                 'contacts','defaults','scantron','coursecategories',
  218:                 'serverstatuses','requestcourses','helpsettings',
  219:                 'coursedefaults','usersessions','loadbalancing',
  220:                 'requestauthor','selfenrollment','inststatus',
  221:                 'ltitools','ssl','trust'],$dom);
  222:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  223:                        'autoupdate','autocreate','directorysrch','contacts',
  224:                        'usercreation','selfcreation','usermodification','scantron',
  225:                        'requestcourses','requestauthor','coursecategories',
  226:                        'serverstatuses','helpsettings','coursedefaults',
  227:                        'ltitools','selfenrollment','usersessions','ssl','trust');
  228:     my %existing;
  229:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  230:         %existing = %{$domconfig{'loadbalancing'}};
  231:     }
  232:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  233:         push(@prefs_order,'loadbalancing');
  234:     }
  235:     my %prefs = (
  236:         'rolecolors' =>
  237:                    { text => 'Default color schemes',
  238:                      help => 'Domain_Configuration_Color_Schemes',
  239:                      header => [{col1 => 'Student Settings',
  240:                                  col2 => '',},
  241:                                 {col1 => 'Coordinator Settings',
  242:                                  col2 => '',},
  243:                                 {col1 => 'Author Settings',
  244:                                  col2 => '',},
  245:                                 {col1 => 'Administrator Settings',
  246:                                  col2 => '',}],
  247:                       print => \&print_rolecolors,
  248:                       modify => \&modify_rolecolors,
  249:                     },
  250:         'login' =>
  251:                     { text => 'Log-in page options',
  252:                       help => 'Domain_Configuration_Login_Page',
  253:                       header => [{col1 => 'Log-in Page Items',
  254:                                   col2 => '',},
  255:                                  {col1 => 'Log-in Help',
  256:                                   col2 => 'Value'},
  257:                                  {col1 => 'Custom HTML in document head',
  258:                                   col2 => 'Value'}],
  259:                       print => \&print_login,
  260:                       modify => \&modify_login,
  261:                     },
  262:         'defaults' => 
  263:                     { text => 'Default authentication/language/timezone/portal/types',
  264:                       help => 'Domain_Configuration_LangTZAuth',
  265:                       header => [{col1 => 'Setting',
  266:                                   col2 => 'Value'},
  267:                                  {col1 => 'Institutional user types',
  268:                                   col2 => 'Assignable to e-mail usernames'}],
  269:                       print => \&print_defaults,
  270:                       modify => \&modify_defaults,
  271:                     },
  272:         'quotas' => 
  273:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  274:                       help => 'Domain_Configuration_Quotas',
  275:                       header => [{col1 => 'User affiliation',
  276:                                   col2 => 'Available tools',
  277:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  278:                       print => \&print_quotas,
  279:                       modify => \&modify_quotas,
  280:                     },
  281:         'autoenroll' =>
  282:                    { text => 'Auto-enrollment settings',
  283:                      help => 'Domain_Configuration_Auto_Enrollment',
  284:                      header => [{col1 => 'Configuration setting',
  285:                                  col2 => 'Value(s)'}],
  286:                      print => \&print_autoenroll,
  287:                      modify => \&modify_autoenroll,
  288:                    },
  289:         'autoupdate' => 
  290:                    { text => 'Auto-update settings',
  291:                      help => 'Domain_Configuration_Auto_Updates',
  292:                      header => [{col1 => 'Setting',
  293:                                  col2 => 'Value',},
  294:                                 {col1 => 'Setting',
  295:                                  col2 => 'Affiliation'},
  296:                                 {col1 => 'User population',
  297:                                  col2 => 'Updatable user data'}],
  298:                      print => \&print_autoupdate,
  299:                      modify => \&modify_autoupdate,
  300:                   },
  301:         'autocreate' => 
  302:                   { text => 'Auto-course creation settings',
  303:                      help => 'Domain_Configuration_Auto_Creation',
  304:                      header => [{col1 => 'Configuration Setting',
  305:                                  col2 => 'Value',}],
  306:                      print => \&print_autocreate,
  307:                      modify => \&modify_autocreate,
  308:                   },
  309:         'directorysrch' => 
  310:                   { text => 'Directory searches',
  311:                     help => 'Domain_Configuration_InstDirectory_Search',
  312:                     header => [{col1 => 'Institutional Directory Setting',
  313:                                 col2 => 'Value',},
  314:                                {col1 => 'LON-CAPA Directory Setting',
  315:                                 col2 => 'Value',}],
  316:                     print => \&print_directorysrch,
  317:                     modify => \&modify_directorysrch,
  318:                   },
  319:         'contacts' =>
  320:                   { text => 'Contact Information',
  321:                     help => 'Domain_Configuration_Contact_Info',
  322:                     header => [{col1 => 'Setting',
  323:                                 col2 => 'Value',}],
  324:                     print => \&print_contacts,
  325:                     modify => \&modify_contacts,
  326:                   },
  327:         'usercreation' => 
  328:                   { text => 'User creation',
  329:                     help => 'Domain_Configuration_User_Creation',
  330:                     header => [{col1 => 'Format rule type',
  331:                                 col2 => 'Format rules in force'},
  332:                                {col1 => 'User account creation',
  333:                                 col2 => 'Usernames which may be created',},
  334:                                {col1 => 'Context',
  335:                                 col2 => 'Assignable authentication types'}],
  336:                     print => \&print_usercreation,
  337:                     modify => \&modify_usercreation,
  338:                   },
  339:         'selfcreation' => 
  340:                   { text => 'Users self-creating accounts',
  341:                     help => 'Domain_Configuration_Self_Creation', 
  342:                     header => [{col1 => 'Self-creation with institutional username',
  343:                                 col2 => 'Enabled?'},
  344:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  345:                                 col2 => 'Information user can enter'},
  346:                                {col1 => 'Self-creation with e-mail as username',
  347:                                 col2 => 'Settings'}],
  348:                     print => \&print_selfcreation,
  349:                     modify => \&modify_selfcreation,
  350:                   },
  351:         'usermodification' =>
  352:                   { text => 'User modification',
  353:                     help => 'Domain_Configuration_User_Modification',
  354:                     header => [{col1 => 'Target user has role',
  355:                                 col2 => 'User information updatable in author context'},
  356:                                {col1 => 'Target user has role',
  357:                                 col2 => 'User information updatable in course context'}],
  358:                     print => \&print_usermodification,
  359:                     modify => \&modify_usermodification,
  360:                   },
  361:         'scantron' =>
  362:                   { text => 'Bubblesheet format file',
  363:                     help => 'Domain_Configuration_Scantron_Format',
  364:                     header => [ {col1 => 'Item',
  365:                                  col2 => '',
  366:                               }],
  367:                     print => \&print_scantron,
  368:                     modify => \&modify_scantron,
  369:                   },
  370:         'requestcourses' => 
  371:                  {text => 'Request creation of courses',
  372:                   help => 'Domain_Configuration_Request_Courses',
  373:                   header => [{col1 => 'User affiliation',
  374:                               col2 => 'Availability/Processing of requests',},
  375:                              {col1 => 'Setting',
  376:                               col2 => 'Value'},
  377:                              {col1 => 'Available textbooks',
  378:                               col2 => ''},
  379:                              {col1 => 'Available templates',
  380:                               col2 => ''},
  381:                              {col1 => 'Validation (not official courses)',
  382:                               col2 => 'Value'},],
  383:                   print => \&print_quotas,
  384:                   modify => \&modify_quotas,
  385:                  },
  386:         'requestauthor' =>
  387:                  {text => 'Request Authoring Space',
  388:                   help => 'Domain_Configuration_Request_Author',
  389:                   header => [{col1 => 'User affiliation',
  390:                               col2 => 'Availability/Processing of requests',},
  391:                              {col1 => 'Setting',
  392:                               col2 => 'Value'}],
  393:                   print => \&print_quotas,
  394:                   modify => \&modify_quotas,
  395:                  },
  396:         'coursecategories' =>
  397:                   { text => 'Cataloging of courses/communities',
  398:                     help => 'Domain_Configuration_Cataloging_Courses',
  399:                     header => [{col1 => 'Catalog type/availability',
  400:                                 col2 => '',},
  401:                                {col1 => 'Category settings for standard catalog',
  402:                                 col2 => '',},
  403:                                {col1 => 'Categories',
  404:                                 col2 => '',
  405:                                }],
  406:                     print => \&print_coursecategories,
  407:                     modify => \&modify_coursecategories,
  408:                   },
  409:         'serverstatuses' =>
  410:                  {text   => 'Access to server status pages',
  411:                   help   => 'Domain_Configuration_Server_Status',
  412:                   header => [{col1 => 'Status Page',
  413:                               col2 => 'Other named users',
  414:                               col3 => 'Specific IPs',
  415:                             }],
  416:                   print => \&print_serverstatuses,
  417:                   modify => \&modify_serverstatuses,
  418:                  },
  419:         'helpsettings' =>
  420:                  {text   => 'Support settings',
  421:                   help   => 'Domain_Configuration_Help_Settings',
  422:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  423:                               col2 => 'Value'},
  424:                              {col1 => 'Helpdesk Roles',
  425:                               col2 => 'Settings'},],
  426:                   print  => \&print_helpsettings,
  427:                   modify => \&modify_helpsettings,
  428:                  },
  429:         'coursedefaults' => 
  430:                  {text => 'Course/Community defaults',
  431:                   help => 'Domain_Configuration_Course_Defaults',
  432:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  433:                               col2 => 'Value',},
  434:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  435:                               col2 => 'Value',},],
  436:                   print => \&print_coursedefaults,
  437:                   modify => \&modify_coursedefaults,
  438:                  },
  439:         'selfenrollment' => 
  440:                  {text   => 'Self-enrollment in Course/Community',
  441:                   help   => 'Domain_Configuration_Selfenrollment',
  442:                   header => [{col1 => 'Configuration Rights',
  443:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  444:                              {col1 => 'Defaults',
  445:                               col2 => 'Value'},
  446:                              {col1 => 'Self-enrollment validation (optional)',
  447:                               col2 => 'Value'},],
  448:                   print => \&print_selfenrollment,
  449:                   modify => \&modify_selfenrollment,
  450:                  },
  451:         'privacy' => 
  452:                  {text   => 'User Privacy',
  453:                   help   => 'Domain_Configuration_User_Privacy',
  454:                   header => [{col1 => 'Setting',
  455:                               col2 => 'Value',}],
  456:                   print => \&print_privacy,
  457:                   modify => \&modify_privacy,
  458:                  },
  459:         'usersessions' =>
  460:                  {text  => 'User session hosting/offloading',
  461:                   help  => 'Domain_Configuration_User_Sessions',
  462:                   header => [{col1 => 'Domain server',
  463:                               col2 => 'Servers to offload sessions to when busy'},
  464:                              {col1 => 'Hosting of users from other domains',
  465:                               col2 => 'Rules'},
  466:                              {col1 => "Hosting domain's own users elsewhere",
  467:                               col2 => 'Rules'}],
  468:                   print => \&print_usersessions,
  469:                   modify => \&modify_usersessions,
  470:                  },
  471:         'loadbalancing' =>
  472:                  {text  => 'Dedicated Load Balancer(s)',
  473:                   help  => 'Domain_Configuration_Load_Balancing',
  474:                   header => [{col1 => 'Balancers',
  475:                               col2 => 'Default destinations',
  476:                               col3 => 'User affiliation',
  477:                               col4 => 'Overrides'},
  478:                             ],
  479:                   print => \&print_loadbalancing,
  480:                   modify => \&modify_loadbalancing,
  481:                  },
  482:         'ltitools' => 
  483:                  {text => 'External Tools (LTI)',
  484:                   help => 'Domain_configuration_LTI_Tools',
  485:                   header => [{col1 => 'Setting',
  486:                               col2 => 'Value',}],
  487:                   print => \&print_ltitools,
  488:                   modify => \&modify_ltitools,
  489:                  },
  490:         'ssl' =>
  491:                  {text  => 'LON-CAPA Network (SSL)',
  492:                   help  => 'Domain_Configuration_Network_SSL',
  493:                   header => [{col1 => 'Server',
  494:                               col2 => 'Certificate Status'},
  495:                              {col1 => 'Connections to other servers',
  496:                               col2 => 'Rules'},
  497:                              {col1 => "Replicating domain's published content",
  498:                               col2 => 'Rules'}],
  499:                   print => \&print_ssl,
  500:                   modify => \&modify_ssl,
  501:                  },
  502:         'trust' =>
  503:                  {text   => 'Trust Settings',
  504:                   help   => 'Domain_Configuration_Trust',
  505:                   header => [{col1 => "Access to this domain's content by others",
  506:                               col2 => 'Rules'},
  507:                              {col1 => "Access to other domain's content by this domain",
  508:                               col2 => 'Rules'},
  509:                              {col1 => "Enrollment in this domain's courses by others",
  510:                               col2 => 'Rules',},
  511:                              {col1 => "Co-author roles in this domain for others",
  512:                               col2 => 'Rules',},
  513:                              {col1 => "Co-author roles for this domain's users elsewhere",
  514:                               col2 => 'Rules',},
  515:                              {col1 => "Domain roles in this domain assignable to others",
  516:                               col2 => 'Rules'},
  517:                              {col1 => "Course catalog for this domain displayed elsewhere",
  518:                               col2 => 'Rules'},
  519:                              {col1 => "Requests for creation of courses in this domain by others",
  520:                               col2 => 'Rules'},
  521:                              {col1 => "Users in other domains can send messages to this domain",
  522:                               col2 => 'Rules'},],
  523:                   print => \&print_trust,
  524:                   modify => \&modify_trust,
  525:                  },
  526:     );
  527:     if (keys(%servers) > 1) {
  528:         $prefs{'login'}  = { text   => 'Log-in page options',
  529:                              help   => 'Domain_Configuration_Login_Page',
  530:                             header => [{col1 => 'Log-in Service',
  531:                                         col2 => 'Server Setting',},
  532:                                        {col1 => 'Log-in Page Items',
  533:                                         col2 => ''},
  534:                                        {col1 => 'Log-in Help',
  535:                                         col2 => 'Value'},
  536:                                        {col1 => 'Custom HTML in document head',
  537:                                         col2 => 'Value'}],
  538:                             print => \&print_login,
  539:                             modify => \&modify_login,
  540:                            };
  541:     }
  542: 
  543:     my @roles = ('student','coordinator','author','admin');
  544:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  545:     &Apache::lonhtmlcommon::add_breadcrumb
  546:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  547:       text=>"Settings to display/modify"});
  548:     my $confname = $dom.'-domainconfig';
  549: 
  550:     if ($phase eq 'process') {
  551:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  552:                                                               \%prefs,\%domconfig,$confname,\@roles);
  553:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  554:             $r->rflush();
  555:             &devalidate_remote_domconfs($dom,$result);
  556:         }
  557:     } elsif ($phase eq 'display') {
  558:         my $js = &recaptcha_js().
  559:                  &toggle_display_js();
  560:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  561:             my ($othertitle,$usertypes,$types) =
  562:                 &Apache::loncommon::sorted_inst_types($dom);
  563:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  564:                                           $domconfig{'loadbalancing'}).
  565:                    &new_spares_js().
  566:                    &common_domprefs_js().
  567:                    &Apache::loncommon::javascript_array_indexof();
  568:         }
  569:         if (grep(/^requestcourses$/,@actions)) {
  570:             my $javascript_validations;
  571:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  572:             $js .= <<END;
  573: <script type="text/javascript">
  574: $javascript_validations
  575: </script>
  576: $coursebrowserjs
  577: END
  578:         }
  579:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  580:     } else {
  581: # check if domconfig user exists for the domain.
  582:         my $servadm = $r->dir_config('lonAdmEMail');
  583:         my ($configuserok,$author_ok,$switchserver) =
  584:             &config_check($dom,$confname,$servadm);
  585:         unless ($configuserok eq 'ok') {
  586:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  587:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  588:                           $confname).
  589:                       '<br />'
  590:             );
  591:             if ($switchserver) {
  592:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  593:                           '<br />'.
  594:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  595:                           '<br />'.
  596:                           &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).
  597:                           '<br />'.
  598:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  599:                 );
  600:             } else {
  601:                 $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.').
  602:                           '<br />'.
  603:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  604:                 );
  605:             }
  606:             $r->print(&Apache::loncommon::end_page());
  607:             return OK;
  608:         }
  609:         if (keys(%domconfig) == 0) {
  610:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  611:             my @ids=&Apache::lonnet::current_machine_ids();
  612:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  613:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  614:                 my @loginimages = ('img','logo','domlogo','login');
  615:                 my $custom_img_count = 0;
  616:                 foreach my $img (@loginimages) {
  617:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  618:                         $custom_img_count ++;
  619:                     }
  620:                 }
  621:                 foreach my $role (@roles) {
  622:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  623:                         $custom_img_count ++;
  624:                     }
  625:                 }
  626:                 if ($custom_img_count > 0) {
  627:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  628:                     my $switch_server = &check_switchserver($dom,$confname);
  629:                     $r->print(
  630:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  631:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  632:     &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 />'.
  633:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  634:                     if ($switch_server) {
  635:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  636:                     }
  637:                     $r->print(&Apache::loncommon::end_page());
  638:                     return OK;
  639:                 }
  640:             }
  641:         }
  642:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  643:     }
  644:     return OK;
  645: }
  646: 
  647: sub process_changes {
  648:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  649:     my %domconfig;
  650:     if (ref($values) eq 'HASH') {
  651:         %domconfig = %{$values};
  652:     }
  653:     my $output;
  654:     if ($action eq 'login') {
  655:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  656:     } elsif ($action eq 'rolecolors') {
  657:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  658:                                      $lastactref,%domconfig);
  659:     } elsif ($action eq 'quotas') {
  660:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  661:     } elsif ($action eq 'autoenroll') {
  662:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  663:     } elsif ($action eq 'autoupdate') {
  664:         $output = &modify_autoupdate($dom,%domconfig);
  665:     } elsif ($action eq 'autocreate') {
  666:         $output = &modify_autocreate($dom,%domconfig);
  667:     } elsif ($action eq 'directorysrch') {
  668:         $output = &modify_directorysrch($dom,%domconfig);
  669:     } elsif ($action eq 'usercreation') {
  670:         $output = &modify_usercreation($dom,%domconfig);
  671:     } elsif ($action eq 'selfcreation') {
  672:         $output = &modify_selfcreation($dom,%domconfig);
  673:     } elsif ($action eq 'usermodification') {
  674:         $output = &modify_usermodification($dom,%domconfig);
  675:     } elsif ($action eq 'contacts') {
  676:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  677:     } elsif ($action eq 'defaults') {
  678:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  679:     } elsif ($action eq 'scantron') {
  680:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  681:     } elsif ($action eq 'coursecategories') {
  682:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  683:     } elsif ($action eq 'serverstatuses') {
  684:         $output = &modify_serverstatuses($dom,%domconfig);
  685:     } elsif ($action eq 'requestcourses') {
  686:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  687:     } elsif ($action eq 'requestauthor') {
  688:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  689:     } elsif ($action eq 'helpsettings') {
  690:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  691:     } elsif ($action eq 'coursedefaults') {
  692:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  693:     } elsif ($action eq 'selfenrollment') {
  694:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  695:     } elsif ($action eq 'usersessions') {
  696:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  697:     } elsif ($action eq 'loadbalancing') {
  698:         $output = &modify_loadbalancing($dom,%domconfig);
  699:     } elsif ($action eq 'ltitools') {
  700:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  701:     } elsif ($action eq 'ssl') {
  702:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  703:     } elsif ($action eq 'trust') {
  704:         $output = &modify_trust($dom,$lastactref,%domconfig);
  705:     }
  706:     return $output;
  707: }
  708: 
  709: sub print_config_box {
  710:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  711:     my $rowtotal = 0;
  712:     my $output;
  713:     if ($action eq 'coursecategories') {
  714:         $output = &coursecategories_javascript($settings);
  715:     } elsif ($action eq 'defaults') {
  716:         $output = &defaults_javascript($settings); 
  717:     } elsif ($action eq 'helpsettings') {
  718:         my (%privs,%levelscurrent);
  719:         my %full=();
  720:         my %levels=(
  721:                      course => {},
  722:                      domain => {},
  723:                      system => {},
  724:                    );
  725:         my $context = 'domain';
  726:         my $crstype = 'Course';
  727:         my $formname = 'display';
  728:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  729:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  730:         $output =
  731:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full, 
  732:                                                       \@templateroles);
  733:     }
  734:     $output .=
  735:          '<table class="LC_nested_outer">
  736:           <tr>
  737:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  738:            &mt($item->{text}).'&nbsp;'.
  739:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  740:           '</tr>';
  741:     $rowtotal ++;
  742:     my $numheaders = 1;
  743:     if (ref($item->{'header'}) eq 'ARRAY') {
  744:         $numheaders = scalar(@{$item->{'header'}});
  745:     }
  746:     if ($numheaders > 1) {
  747:         my $colspan = '';
  748:         my $rightcolspan = '';
  749:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  750:             ($action eq 'directorysrch') ||
  751:             (($action eq 'login') && ($numheaders < 4))) {
  752:             $colspan = ' colspan="2"';
  753:         }
  754:         if ($action eq 'usersessions') {
  755:             $rightcolspan = ' colspan="3"'; 
  756:         }
  757:         $output .= '
  758:           <tr>
  759:            <td>
  760:             <table class="LC_nested">
  761:              <tr class="LC_info_row">
  762:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  763:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  764:              </tr>';
  765:         $rowtotal ++;
  766:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  767:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  768:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  769:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings')) {
  770:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  771:         } elsif ($action eq 'coursecategories') {
  772:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  773:         } elsif ($action eq 'login') {
  774:             if ($numheaders == 4) {
  775:                 $colspan = ' colspan="2"';
  776:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  777:             } else {
  778:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  779:             }
  780:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  781:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  782:         } elsif ($action eq 'rolecolors') {
  783:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  784:         }
  785:         $output .= '
  786:            </table>
  787:           </td>
  788:          </tr>
  789:          <tr>
  790:            <td>
  791:             <table class="LC_nested">
  792:              <tr class="LC_info_row">
  793:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  794:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  795:              </tr>';
  796:             $rowtotal ++;
  797:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  798:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  799:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
  800:             ($action eq 'ssl') || ($action eq 'trust')) {
  801:             if ($action eq 'coursecategories') {
  802:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  803:                 $colspan = ' colspan="2"';
  804:             } elsif ($action eq 'trust') {
  805:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
  806:             } else {
  807:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  808:             }
  809:             if ($action eq 'trust') {
  810:                 $output .= '
  811:             </table>
  812:           </td>
  813:          </tr>';
  814:                 my @trusthdrs = qw(2 3 4 5 6 7);
  815:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
  816:                 for (my $i=0; $i<@trusthdrs; $i++) {
  817:                     $output .= '
  818:          <tr>
  819:            <td>
  820:             <table class="LC_nested">
  821:              <tr class="LC_info_row">
  822:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
  823:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
  824:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
  825:             </table>
  826:           </td>
  827:          </tr>';
  828:                 }
  829:                 $output .= '
  830:          <tr>
  831:            <td>
  832:             <table class="LC_nested">
  833:              <tr class="LC_info_row">
  834:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
  835:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
  836:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  837:             } else {
  838:                 $output .= '
  839:            </table>
  840:           </td>
  841:          </tr>
  842:          <tr>
  843:            <td>
  844:             <table class="LC_nested">
  845:              <tr class="LC_info_row">
  846:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  847:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  848:              </tr>'."\n";
  849:                 if ($action eq 'coursecategories') {
  850:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  851:                 } else {
  852:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  853:                 }
  854:             }
  855:             $rowtotal ++;
  856:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  857:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  858:                  ($action eq 'helpsettings')) {
  859:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  860:         } elsif ($action eq 'login') {
  861:             if ($numheaders == 4) {
  862:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  863:            </table>
  864:           </td>
  865:          </tr>
  866:          <tr>
  867:            <td>
  868:             <table class="LC_nested">
  869:              <tr class="LC_info_row">
  870:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  871:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  872:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  873:                 $rowtotal ++;
  874:             } else {
  875:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  876:             }
  877:             $output .= '
  878:            </table>
  879:           </td>
  880:          </tr>
  881:          <tr>
  882:            <td>
  883:             <table class="LC_nested">
  884:              <tr class="LC_info_row">';
  885:             if ($numheaders == 4) {
  886:                 $output .= '
  887:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  888:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  889:              </tr>';
  890:             } else {
  891:                 $output .= '
  892:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  893:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  894:              </tr>';
  895:             }
  896:             $rowtotal ++;
  897:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  898:         } elsif ($action eq 'requestcourses') {
  899:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  900:             $rowtotal ++;
  901:             $output .= &print_studentcode($settings,\$rowtotal).'
  902:            </table>
  903:           </td>
  904:          </tr>
  905:          <tr>
  906:            <td>
  907:             <table class="LC_nested">
  908:              <tr class="LC_info_row">
  909:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  910:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  911:                        &textbookcourses_javascript($settings).
  912:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  913:             </table>
  914:            </td>
  915:           </tr>
  916:          <tr>
  917:            <td>
  918:             <table class="LC_nested">
  919:              <tr class="LC_info_row">
  920:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  921:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  922:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  923:             </table>
  924:            </td>
  925:           </tr>
  926:           <tr>
  927:            <td>
  928:             <table class="LC_nested">
  929:              <tr class="LC_info_row">
  930:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  931:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  932:              </tr>'.
  933:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  934:         } elsif ($action eq 'requestauthor') {
  935:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  936:             $rowtotal ++;
  937:         } elsif ($action eq 'rolecolors') {
  938:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  939:            </table>
  940:           </td>
  941:          </tr>
  942:          <tr>
  943:            <td>
  944:             <table class="LC_nested">
  945:              <tr class="LC_info_row">
  946:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  947:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  948:               <td class="LC_right_item" valign="top">'.
  949:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  950:              </tr>'.
  951:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  952:            </table>
  953:           </td>
  954:          </tr>
  955:          <tr>
  956:            <td>
  957:             <table class="LC_nested">
  958:              <tr class="LC_info_row">
  959:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  960:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  961:              </tr>'.
  962:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  963:             $rowtotal += 2;
  964:         }
  965:     } else {
  966:         $output .= '
  967:           <tr>
  968:            <td>
  969:             <table class="LC_nested">
  970:              <tr class="LC_info_row">';
  971:         if ($action eq 'login') {
  972:             $output .= '  
  973:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  974:         } elsif ($action eq 'serverstatuses') {
  975:             $output .= '
  976:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  977:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  978: 
  979:         } else {
  980:             $output .= '
  981:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  982:         }
  983:         if (defined($item->{'header'}->[0]->{'col3'})) {
  984:             $output .= '<td class="LC_left_item" valign="top">'.
  985:                        &mt($item->{'header'}->[0]->{'col2'});
  986:             if ($action eq 'serverstatuses') {
  987:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  988:             } 
  989:         } else {
  990:             $output .= '<td class="LC_right_item" valign="top">'.
  991:                        &mt($item->{'header'}->[0]->{'col2'});
  992:         }
  993:         $output .= '</td>';
  994:         if ($item->{'header'}->[0]->{'col3'}) {
  995:             if (defined($item->{'header'}->[0]->{'col4'})) {
  996:                 $output .= '<td class="LC_left_item" valign="top">'.
  997:                             &mt($item->{'header'}->[0]->{'col3'});
  998:             } else {
  999:                 $output .= '<td class="LC_right_item" valign="top">'.
 1000:                            &mt($item->{'header'}->[0]->{'col3'});
 1001:             }
 1002:             if ($action eq 'serverstatuses') {
 1003:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1004:             }
 1005:             $output .= '</td>';
 1006:         }
 1007:         if ($item->{'header'}->[0]->{'col4'}) {
 1008:             $output .= '<td class="LC_right_item" valign="top">'.
 1009:                        &mt($item->{'header'}->[0]->{'col4'});
 1010:         }
 1011:         $output .= '</tr>';
 1012:         $rowtotal ++;
 1013:         if ($action eq 'quotas') {
 1014:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1015:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1016:                  ($action eq 'contacts') || ($action eq 'serverstatuses') || 
 1017:                  ($action eq 'loadbalancing') || ($action eq 'ltitools')) {
 1018:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1019:         } elsif ($action eq 'scantron') {
 1020:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 1021:         }
 1022:     }
 1023:     $output .= '
 1024:    </table>
 1025:   </td>
 1026:  </tr>
 1027: </table><br />';
 1028:     return ($output,$rowtotal);
 1029: }
 1030: 
 1031: sub print_login {
 1032:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1033:     my ($css_class,$datatable);
 1034:     my %choices = &login_choices();
 1035: 
 1036:     if ($caller eq 'service') {
 1037:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1038:         my $choice = $choices{'disallowlogin'};
 1039:         $css_class = ' class="LC_odd_row"';
 1040:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1041:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1042:                       '<th>'.$choices{'server'}.'</th>'.
 1043:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1044:                       '<th>'.$choices{'custompath'}.'</th>'.
 1045:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1046:         my %disallowed;
 1047:         if (ref($settings) eq 'HASH') {
 1048:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1049:                %disallowed = %{$settings->{'loginvia'}};
 1050:             }
 1051:         }
 1052:         foreach my $lonhost (sort(keys(%servers))) {
 1053:             my $direct = 'selected="selected"';
 1054:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1055:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1056:                     $direct = '';
 1057:                 }
 1058:             }
 1059:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1060:                           '<td><select name="'.$lonhost.'_server">'.
 1061:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1062:                           '</option>';
 1063:             foreach my $hostid (sort(keys(%servers))) {
 1064:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1065:                 my $selected = '';
 1066:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1067:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1068:                         $selected = 'selected="selected"';
 1069:                     }
 1070:                 }
 1071:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1072:                               $servers{$hostid}.'</option>';
 1073:             }
 1074:             $datatable .= '</select></td>'.
 1075:                           '<td><select name="'.$lonhost.'_serverpath">';
 1076:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1077:                 my $pathname = $path;
 1078:                 if ($path eq 'custom') {
 1079:                     $pathname = &mt('Custom Path').' ->';
 1080:                 }
 1081:                 my $selected = '';
 1082:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1083:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1084:                         $selected = 'selected="selected"';
 1085:                     }
 1086:                 } elsif ($path eq '') {
 1087:                     $selected = 'selected="selected"';
 1088:                 }
 1089:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1090:             }
 1091:             $datatable .= '</select></td>';
 1092:             my ($custom,$exempt);
 1093:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1094:                 $custom = $disallowed{$lonhost}{'custompath'};
 1095:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1096:             }
 1097:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1098:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1099:                           '</tr>';
 1100:         }
 1101:         $datatable .= '</table></td></tr>';
 1102:         return $datatable;
 1103:     } elsif ($caller eq 'page') {
 1104:         my %defaultchecked = ( 
 1105:                                'coursecatalog' => 'on',
 1106:                                'helpdesk'      => 'on',
 1107:                                'adminmail'     => 'off',
 1108:                                'newuser'       => 'off',
 1109:                              );
 1110:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1111:         my (%checkedon,%checkedoff);
 1112:         foreach my $item (@toggles) {
 1113:             if ($defaultchecked{$item} eq 'on') { 
 1114:                 $checkedon{$item} = ' checked="checked" ';
 1115:                 $checkedoff{$item} = ' ';
 1116:             } elsif ($defaultchecked{$item} eq 'off') {
 1117:                 $checkedoff{$item} = ' checked="checked" ';
 1118:                 $checkedon{$item} = ' ';
 1119:             }
 1120:         }
 1121:         my @images = ('img','logo','domlogo','login');
 1122:         my @logintext = ('textcol','bgcol');
 1123:         my @bgs = ('pgbg','mainbg','sidebg');
 1124:         my @links = ('link','alink','vlink');
 1125:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1126:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1127:         my (%is_custom,%designs);
 1128:         my %defaults = (
 1129:                        font => $defaultdesign{'login.font'},
 1130:                        );
 1131:         foreach my $item (@images) {
 1132:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1133:             $defaults{'showlogo'}{$item} = 1;
 1134:         }
 1135:         foreach my $item (@bgs) {
 1136:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1137:         }
 1138:         foreach my $item (@logintext) {
 1139:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1140:         }
 1141:         foreach my $item (@links) {
 1142:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1143:         }
 1144:         if (ref($settings) eq 'HASH') {
 1145:             foreach my $item (@toggles) {
 1146:                 if ($settings->{$item} eq '1') {
 1147:                     $checkedon{$item} =  ' checked="checked" ';
 1148:                     $checkedoff{$item} = ' ';
 1149:                 } elsif ($settings->{$item} eq '0') {
 1150:                     $checkedoff{$item} =  ' checked="checked" ';
 1151:                     $checkedon{$item} = ' ';
 1152:                 }
 1153:             }
 1154:             foreach my $item (@images) {
 1155:                 if (defined($settings->{$item})) {
 1156:                     $designs{$item} = $settings->{$item};
 1157:                     $is_custom{$item} = 1;
 1158:                 }
 1159:                 if (defined($settings->{'showlogo'}{$item})) {
 1160:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1161:                 }
 1162:             }
 1163:             foreach my $item (@logintext) {
 1164:                 if ($settings->{$item} ne '') {
 1165:                     $designs{'logintext'}{$item} = $settings->{$item};
 1166:                     $is_custom{$item} = 1;
 1167:                 }
 1168:             }
 1169:             if ($settings->{'font'} ne '') {
 1170:                 $designs{'font'} = $settings->{'font'};
 1171:                 $is_custom{'font'} = 1;
 1172:             }
 1173:             foreach my $item (@bgs) {
 1174:                 if ($settings->{$item} ne '') {
 1175:                     $designs{'bgs'}{$item} = $settings->{$item};
 1176:                     $is_custom{$item} = 1;
 1177:                 }
 1178:             }
 1179:             foreach my $item (@links) {
 1180:                 if ($settings->{$item} ne '') {
 1181:                     $designs{'links'}{$item} = $settings->{$item};
 1182:                     $is_custom{$item} = 1;
 1183:                 }
 1184:             }
 1185:         } else {
 1186:             if ($designhash{$dom.'.login.font'} ne '') {
 1187:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1188:                 $is_custom{'font'} = 1;
 1189:             }
 1190:             foreach my $item (@images) {
 1191:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1192:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1193:                     $is_custom{$item} = 1;
 1194:                 }
 1195:             }
 1196:             foreach my $item (@bgs) {
 1197:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1198:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1199:                     $is_custom{$item} = 1;
 1200:                 }
 1201:             }
 1202:             foreach my $item (@links) {
 1203:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1204:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1205:                     $is_custom{$item} = 1;
 1206:                 }
 1207:             }
 1208:         }
 1209:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1210:                                                       logo => 'Institution Logo',
 1211:                                                       domlogo => 'Domain Logo',
 1212:                                                       login => 'Login box');
 1213:         my $itemcount = 1;
 1214:         foreach my $item (@toggles) {
 1215:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1216:             $datatable .=  
 1217:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1218:                 '</td><td>'.
 1219:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1220:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1221:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1222:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1223:                 '</tr>';
 1224:             $itemcount ++;
 1225:         }
 1226:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1227:         $datatable .= '</tr></table></td></tr>';
 1228:     } elsif ($caller eq 'help') {
 1229:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1230:         my $switchserver = &check_switchserver($dom,$confname);
 1231:         my $itemcount = 1;
 1232:         $defaulturl = '/adm/loginproblems.html';
 1233:         $defaulttype = 'default';
 1234:         %lt = &Apache::lonlocal::texthash (
 1235:                      del     => 'Delete?',
 1236:                      rep     => 'Replace:',
 1237:                      upl     => 'Upload:',
 1238:                      default => 'Default',
 1239:                      custom  => 'Custom',
 1240:                                              );
 1241:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1242:         my @currlangs;
 1243:         if (ref($settings) eq 'HASH') {
 1244:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1245:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1246:                     next if ($settings->{'helpurl'}{$key} eq '');
 1247:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1248:                     $type{$key} = 'custom';
 1249:                     unless ($key eq 'nolang') {
 1250:                         push(@currlangs,$key);
 1251:                     }
 1252:                 }
 1253:             } elsif ($settings->{'helpurl'} ne '') {
 1254:                 $type{'nolang'} = 'custom';
 1255:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1256:             }
 1257:         }
 1258:         foreach my $lang ('nolang',sort(@currlangs)) {
 1259:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1260:             $datatable .= '<tr'.$css_class.'>';
 1261:             if ($url{$lang} eq '') {
 1262:                 $url{$lang} = $defaulturl;
 1263:             }
 1264:             if ($type{$lang} eq '') {
 1265:                 $type{$lang} = $defaulttype;
 1266:             }
 1267:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1268:             if ($lang eq 'nolang') {
 1269:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1270:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1271:             } else {
 1272:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1273:                                   $langchoices{$lang},
 1274:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1275:             }
 1276:             $datatable .= '</span></td>'."\n".
 1277:                           '<td class="LC_left_item">';
 1278:             if ($type{$lang} eq 'custom') {
 1279:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1280:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1281:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1282:             } else {
 1283:                 $datatable .= $lt{'upl'};
 1284:             }
 1285:             $datatable .='<br />';
 1286:             if ($switchserver) {
 1287:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1288:             } else {
 1289:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1290:             }
 1291:             $datatable .= '</td></tr>';
 1292:             $itemcount ++;
 1293:         }
 1294:         my @addlangs;
 1295:         foreach my $lang (sort(keys(%langchoices))) {
 1296:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1297:             push(@addlangs,$lang);
 1298:         }
 1299:         if (@addlangs > 0) {
 1300:             my %toadd;
 1301:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1302:             $toadd{''} = &mt('Select');
 1303:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1304:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1305:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1306:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1307:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1308:             if ($switchserver) {
 1309:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1310:             } else {
 1311:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1312:             }
 1313:             $datatable .= '</td></tr>';
 1314:             $itemcount ++;
 1315:         }
 1316:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1317:     } elsif ($caller eq 'headtag') {
 1318:         my %domservers = &Apache::lonnet::get_servers($dom);
 1319:         my $choice = $choices{'headtag'};
 1320:         $css_class = ' class="LC_odd_row"';
 1321:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1322:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1323:                       '<th>'.$choices{'current'}.'</th>'.
 1324:                       '<th>'.$choices{'action'}.'</th>'.
 1325:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1326:         my (%currurls,%currexempt);
 1327:         if (ref($settings) eq 'HASH') {
 1328:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1329:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1330:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1331:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1332:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1333:                     }
 1334:                 }
 1335:             }
 1336:         }
 1337:         my %lt = &Apache::lonlocal::texthash(
 1338:                                                del  => 'Delete?',
 1339:                                                rep  => 'Replace:',
 1340:                                                upl  => 'Upload:',
 1341:                                                curr => 'View contents',
 1342:                                                none => 'None',
 1343:         );
 1344:         my $switchserver = &check_switchserver($dom,$confname);
 1345:         foreach my $lonhost (sort(keys(%domservers))) {
 1346:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1347:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1348:             if ($currurls{$lonhost}) {
 1349:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1350:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1351:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1352:                               '">'.$lt{'curr'}.'</a></td>'.
 1353:                               '<td><span class="LC_nobreak"><label>'.
 1354:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1355:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1356:             } else {
 1357:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1358:             }
 1359:             $datatable .='<br />';
 1360:             if ($switchserver) {
 1361:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1362:             } else {
 1363:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1364:             }
 1365:             $datatable .= '</td><td><input type="textbox" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1366:         }
 1367:         $datatable .= '</table></td></tr>';
 1368:     }
 1369:     return $datatable;
 1370: }
 1371: 
 1372: sub login_choices {
 1373:     my %choices =
 1374:         &Apache::lonlocal::texthash (
 1375:             coursecatalog => 'Display Course/Community Catalog link?',
 1376:             adminmail     => "Display Administrator's E-mail Address?",
 1377:             helpdesk      => 'Display "Contact Helpdesk" link',
 1378:             disallowlogin => "Login page requests redirected",
 1379:             hostid        => "Server",
 1380:             server        => "Redirect to:",
 1381:             serverpath    => "Path",
 1382:             custompath    => "Custom", 
 1383:             exempt        => "Exempt IP(s)",
 1384:             directlogin   => "No redirect",
 1385:             newuser       => "Link to create a user account",
 1386:             img           => "Header",
 1387:             logo          => "Main Logo",
 1388:             domlogo       => "Domain Logo",
 1389:             login         => "Log-in Header", 
 1390:             textcol       => "Text color",
 1391:             bgcol         => "Box color",
 1392:             bgs           => "Background colors",
 1393:             links         => "Link colors",
 1394:             font          => "Font color",
 1395:             pgbg          => "Header",
 1396:             mainbg        => "Page",
 1397:             sidebg        => "Login box",
 1398:             link          => "Link",
 1399:             alink         => "Active link",
 1400:             vlink         => "Visited link",
 1401:             headtag       => "Custom markup",
 1402:             action        => "Action",
 1403:             current       => "Current",
 1404:         );
 1405:     return %choices;
 1406: }
 1407: 
 1408: sub print_rolecolors {
 1409:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1410:     my %choices = &color_font_choices();
 1411:     my @bgs = ('pgbg','tabbg','sidebg');
 1412:     my @links = ('link','alink','vlink');
 1413:     my @images = ('img');
 1414:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1415:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1416:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1417:     my (%is_custom,%designs);
 1418:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1419:     if (ref($settings) eq 'HASH') {
 1420:         if (ref($settings->{$role}) eq 'HASH') {
 1421:             if ($settings->{$role}->{'img'} ne '') {
 1422:                 $designs{'img'} = $settings->{$role}->{'img'};
 1423:                 $is_custom{'img'} = 1;
 1424:             }
 1425:             if ($settings->{$role}->{'font'} ne '') {
 1426:                 $designs{'font'} = $settings->{$role}->{'font'};
 1427:                 $is_custom{'font'} = 1;
 1428:             }
 1429:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1430:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1431:                 $is_custom{'fontmenu'} = 1;
 1432:             }
 1433:             foreach my $item (@bgs) {
 1434:                 if ($settings->{$role}->{$item} ne '') {
 1435:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1436:                     $is_custom{$item} = 1;
 1437:                 }
 1438:             }
 1439:             foreach my $item (@links) {
 1440:                 if ($settings->{$role}->{$item} ne '') {
 1441:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1442:                     $is_custom{$item} = 1;
 1443:                 }
 1444:             }
 1445:         }
 1446:     } else {
 1447:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1448:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1449:             $is_custom{'img'} = 1;
 1450:         }
 1451:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1452:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1453:             $is_custom{'fontmenu'} = 1; 
 1454:         }
 1455:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1456:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1457:             $is_custom{'font'} = 1;
 1458:         }
 1459:         foreach my $item (@bgs) {
 1460:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1461:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1462:                 $is_custom{$item} = 1;
 1463:             
 1464:             }
 1465:         }
 1466:         foreach my $item (@links) {
 1467:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1468:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1469:                 $is_custom{$item} = 1;
 1470:             }
 1471:         }
 1472:     }
 1473:     my $itemcount = 1;
 1474:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1475:     $datatable .= '</tr></table></td></tr>';
 1476:     return $datatable;
 1477: }
 1478: 
 1479: sub role_defaults {
 1480:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1481:     my %defaults;
 1482:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1483:         return %defaults;
 1484:     }
 1485:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1486:     if ($role eq 'login') {
 1487:         %defaults = (
 1488:                        font => $defaultdesign{$role.'.font'},
 1489:                     );
 1490:         if (ref($logintext) eq 'ARRAY') {
 1491:             foreach my $item (@{$logintext}) {
 1492:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1493:             }
 1494:         }
 1495:         foreach my $item (@{$images}) {
 1496:             $defaults{'showlogo'}{$item} = 1;
 1497:         }
 1498:     } else {
 1499:         %defaults = (
 1500:                        img => $defaultdesign{$role.'.img'},
 1501:                        font => $defaultdesign{$role.'.font'},
 1502:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1503:                     );
 1504:     }
 1505:     foreach my $item (@{$bgs}) {
 1506:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1507:     }
 1508:     foreach my $item (@{$links}) {
 1509:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1510:     }
 1511:     foreach my $item (@{$images}) {
 1512:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1513:     }
 1514:     return %defaults;
 1515: }
 1516: 
 1517: sub display_color_options {
 1518:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1519:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1520:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1521:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1522:     my $datatable = '<tr'.$css_class.'>'.
 1523:         '<td>'.$choices->{'font'}.'</td>';
 1524:     if (!$is_custom->{'font'}) {
 1525:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1526:     } else {
 1527:         $datatable .= '<td>&nbsp;</td>';
 1528:     }
 1529:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1530: 
 1531:     $datatable .= '<td><span class="LC_nobreak">'.
 1532:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1533:                   ' value="'.$current_color.'" />&nbsp;'.
 1534:                   '&nbsp;</td></tr>';
 1535:     unless ($role eq 'login') { 
 1536:         $datatable .= '<tr'.$css_class.'>'.
 1537:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1538:         if (!$is_custom->{'fontmenu'}) {
 1539:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1540:         } else {
 1541:             $datatable .= '<td>&nbsp;</td>';
 1542:         }
 1543: 	$current_color = $designs->{'fontmenu'} ?
 1544: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1545:         $datatable .= '<td><span class="LC_nobreak">'.
 1546:                       '<input class="colorchooser" type="text" size="10" name="'
 1547: 		      .$role.'_fontmenu"'.
 1548:                       ' value="'.$current_color.'" />&nbsp;'.
 1549:                       '&nbsp;</td></tr>';
 1550:     }
 1551:     my $switchserver = &check_switchserver($dom,$confname);
 1552:     foreach my $img (@{$images}) {
 1553: 	$itemcount ++;
 1554:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1555:         $datatable .= '<tr'.$css_class.'>'.
 1556:                       '<td>'.$choices->{$img};
 1557:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1558:         if ($role eq 'login') {
 1559:             if ($img eq 'login') {
 1560:                 $login_hdr_pick =
 1561:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1562:                 $logincolors =
 1563:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1564:                                        $designs,$defaults);
 1565:             } elsif ($img ne 'domlogo') {
 1566:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1567:             }
 1568:         }
 1569:         $datatable .= '</td>';
 1570:         if ($designs->{$img} ne '') {
 1571:             $imgfile = $designs->{$img};
 1572: 	    $img_import = ($imgfile =~ m{^/adm/});
 1573:         } else {
 1574:             $imgfile = $defaults->{$img};
 1575:         }
 1576:         if ($imgfile) {
 1577:             my ($showfile,$fullsize);
 1578:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1579:                 my $urldir = $1;
 1580:                 my $filename = $2;
 1581:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1582:                 if (@info) {
 1583:                     my $thumbfile = 'tn-'.$filename;
 1584:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1585:                     if (@thumb) {
 1586:                         $showfile = $urldir.'/'.$thumbfile;
 1587:                     } else {
 1588:                         $showfile = $imgfile;
 1589:                     }
 1590:                 } else {
 1591:                     $showfile = '';
 1592:                 }
 1593:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1594:                 $showfile = $imgfile;
 1595:                 my $imgdir = $1;
 1596:                 my $filename = $2;
 1597:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1598:                     $showfile = "/$imgdir/tn-".$filename;
 1599:                 } else {
 1600:                     my $input = $londocroot.$imgfile;
 1601:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1602:                     if (!-e $output) {
 1603:                         my ($width,$height) = &thumb_dimensions();
 1604:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1605:                         if ($fullwidth ne '' && $fullheight ne '') {
 1606:                             if ($fullwidth > $width && $fullheight > $height) { 
 1607:                                 my $size = $width.'x'.$height;
 1608:                                 system("convert -sample $size $input $output");
 1609:                                 $showfile = "/$imgdir/tn-".$filename;
 1610:                             }
 1611:                         }
 1612:                     }
 1613:                 }
 1614:             }
 1615:             if ($showfile) {
 1616:                 if ($showfile =~ m{^/(adm|res)/}) {
 1617:                     if ($showfile =~ m{^/res/}) {
 1618:                         my $local_showfile =
 1619:                             &Apache::lonnet::filelocation('',$showfile);
 1620:                         &Apache::lonnet::repcopy($local_showfile);
 1621:                     }
 1622:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1623:                 }
 1624:                 if ($imgfile) {
 1625:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1626:                         if ($imgfile =~ m{^/res/}) {
 1627:                             my $local_imgfile =
 1628:                                 &Apache::lonnet::filelocation('',$imgfile);
 1629:                             &Apache::lonnet::repcopy($local_imgfile);
 1630:                         }
 1631:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1632:                     } else {
 1633:                         $fullsize = $imgfile;
 1634:                     }
 1635:                 }
 1636:                 $datatable .= '<td>';
 1637:                 if ($img eq 'login') {
 1638:                     $datatable .= $login_hdr_pick;
 1639:                 } 
 1640:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1641:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1642:             } else {
 1643:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1644:                               &mt('Upload:').'<br />';
 1645:             }
 1646:         } else {
 1647:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1648:                           &mt('Upload:').'<br />';
 1649:         }
 1650:         if ($switchserver) {
 1651:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1652:         } else {
 1653:             if ($img ne 'login') { # suppress file selection for Log-in header
 1654:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1655:             }
 1656:         }
 1657:         $datatable .= '</td></tr>';
 1658:     }
 1659:     $itemcount ++;
 1660:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1661:     $datatable .= '<tr'.$css_class.'>'.
 1662:                   '<td>'.$choices->{'bgs'}.'</td>';
 1663:     my $bgs_def;
 1664:     foreach my $item (@{$bgs}) {
 1665:         if (!$is_custom->{$item}) {
 1666:             $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>';
 1667:         }
 1668:     }
 1669:     if ($bgs_def) {
 1670:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1671:     } else {
 1672:         $datatable .= '<td>&nbsp;</td>';
 1673:     }
 1674:     $datatable .= '<td class="LC_right_item">'.
 1675:                   '<table border="0"><tr>';
 1676: 
 1677:     foreach my $item (@{$bgs}) {
 1678:         $datatable .= '<td align="center">'.$choices->{$item};
 1679: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1680:         if ($designs->{'bgs'}{$item}) {
 1681:             $datatable .= '&nbsp;';
 1682:         }
 1683:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1684:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1685:     }
 1686:     $datatable .= '</tr></table></td></tr>';
 1687:     $itemcount ++;
 1688:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1689:     $datatable .= '<tr'.$css_class.'>'.
 1690:                   '<td>'.$choices->{'links'}.'</td>';
 1691:     my $links_def;
 1692:     foreach my $item (@{$links}) {
 1693:         if (!$is_custom->{$item}) {
 1694:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1695:         }
 1696:     }
 1697:     if ($links_def) {
 1698:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1699:     } else {
 1700:         $datatable .= '<td>&nbsp;</td>';
 1701:     }
 1702:     $datatable .= '<td class="LC_right_item">'.
 1703:                   '<table border="0"><tr>';
 1704:     foreach my $item (@{$links}) {
 1705: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1706:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1707:         if ($designs->{'links'}{$item}) {
 1708:             $datatable.='&nbsp;';
 1709:         }
 1710:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1711:                       '" /></td>';
 1712:     }
 1713:     $$rowtotal += $itemcount;
 1714:     return $datatable;
 1715: }
 1716: 
 1717: sub logo_display_options {
 1718:     my ($img,$defaults,$designs) = @_;
 1719:     my $checkedon;
 1720:     if (ref($defaults) eq 'HASH') {
 1721:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1722:             if ($defaults->{'showlogo'}{$img}) {
 1723:                 $checkedon = 'checked="checked" ';     
 1724:             }
 1725:         } 
 1726:     }
 1727:     if (ref($designs) eq 'HASH') {
 1728:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1729:             if (defined($designs->{'showlogo'}{$img})) {
 1730:                 if ($designs->{'showlogo'}{$img} == 0) {
 1731:                     $checkedon = '';
 1732:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1733:                     $checkedon = 'checked="checked" ';
 1734:                 }
 1735:             }
 1736:         }
 1737:     }
 1738:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1739:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1740:            &mt('show').'</label>'."\n";
 1741: }
 1742: 
 1743: sub login_header_options  {
 1744:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1745:     my $output = '';
 1746:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1747:         $output .= &mt('Text default(s):').'<br />';
 1748:         if (!$is_custom->{'textcol'}) {
 1749:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1750:                        '&nbsp;&nbsp;&nbsp;';
 1751:         }
 1752:         if (!$is_custom->{'bgcol'}) {
 1753:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1754:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1755:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1756:         }
 1757:         $output .= '<br />';
 1758:     }
 1759:     $output .='<br />';
 1760:     return $output;
 1761: }
 1762: 
 1763: sub login_text_colors {
 1764:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1765:     my $color_menu = '<table border="0"><tr>';
 1766:     foreach my $item (@{$logintext}) {
 1767:         $color_menu .= '<td align="center">'.$choices->{$item};
 1768:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1769:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1770:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1771:     }
 1772:     $color_menu .= '</tr></table><br />';
 1773:     return $color_menu;
 1774: }
 1775: 
 1776: sub image_changes {
 1777:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1778:     my $output;
 1779:     if ($img eq 'login') {
 1780:             # suppress image for Log-in header
 1781:     } elsif (!$is_custom) {
 1782:         if ($img ne 'domlogo') {
 1783:             $output .= &mt('Default image:').'<br />';
 1784:         } else {
 1785:             $output .= &mt('Default in use:').'<br />';
 1786:         }
 1787:     }
 1788:     if ($img eq 'login') { # suppress image for Log-in header
 1789:         $output .= '<td>'.$logincolors;
 1790:     } else {
 1791:         if ($img_import) {
 1792:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1793:         }
 1794:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1795:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1796:         if ($is_custom) {
 1797:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1798:                        '<input type="checkbox" name="'.
 1799:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1800:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1801:         } else {
 1802:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1803:         }
 1804:     }
 1805:     return $output;
 1806: }
 1807: 
 1808: sub print_quotas {
 1809:     my ($dom,$settings,$rowtotal,$action) = @_;
 1810:     my $context;
 1811:     if ($action eq 'quotas') {
 1812:         $context = 'tools';
 1813:     } else {
 1814:         $context = $action;
 1815:     }
 1816:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1817:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1818:     my $typecount = 0;
 1819:     my ($css_class,%titles);
 1820:     if ($context eq 'requestcourses') {
 1821:         @usertools = ('official','unofficial','community','textbook','placement');
 1822:         @options =('norequest','approval','validate','autolimit');
 1823:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1824:         %titles = &courserequest_titles();
 1825:     } elsif ($context eq 'requestauthor') {
 1826:         @usertools = ('author');
 1827:         @options = ('norequest','approval','automatic');
 1828:         %titles = &authorrequest_titles();
 1829:     } else {
 1830:         @usertools = ('aboutme','blog','webdav','portfolio');
 1831:         %titles = &tool_titles();
 1832:     }
 1833:     if (ref($types) eq 'ARRAY') {
 1834:         foreach my $type (@{$types}) {
 1835:             my ($currdefquota,$currauthorquota);
 1836:             unless (($context eq 'requestcourses') ||
 1837:                     ($context eq 'requestauthor')) {
 1838:                 if (ref($settings) eq 'HASH') {
 1839:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1840:                         $currdefquota = $settings->{defaultquota}->{$type};
 1841:                     } else {
 1842:                         $currdefquota = $settings->{$type};
 1843:                     }
 1844:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1845:                         $currauthorquota = $settings->{authorquota}->{$type};
 1846:                     }
 1847:                 }
 1848:             }
 1849:             if (defined($usertypes->{$type})) {
 1850:                 $typecount ++;
 1851:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1852:                 $datatable .= '<tr'.$css_class.'>'.
 1853:                               '<td>'.$usertypes->{$type}.'</td>'.
 1854:                               '<td class="LC_left_item">';
 1855:                 if ($context eq 'requestcourses') {
 1856:                     $datatable .= '<table><tr>';
 1857:                 }
 1858:                 my %cell;  
 1859:                 foreach my $item (@usertools) {
 1860:                     if ($context eq 'requestcourses') {
 1861:                         my ($curroption,$currlimit);
 1862:                         if (ref($settings) eq 'HASH') {
 1863:                             if (ref($settings->{$item}) eq 'HASH') {
 1864:                                 $curroption = $settings->{$item}->{$type};
 1865:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1866:                                     $currlimit = $1; 
 1867:                                 }
 1868:                             }
 1869:                         }
 1870:                         if (!$curroption) {
 1871:                             $curroption = 'norequest';
 1872:                         }
 1873:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1874:                         foreach my $option (@options) {
 1875:                             my $val = $option;
 1876:                             if ($option eq 'norequest') {
 1877:                                 $val = 0;  
 1878:                             }
 1879:                             if ($option eq 'validate') {
 1880:                                 my $canvalidate = 0;
 1881:                                 if (ref($validations{$item}) eq 'HASH') { 
 1882:                                     if ($validations{$item}{$type}) {
 1883:                                         $canvalidate = 1;
 1884:                                     }
 1885:                                 }
 1886:                                 next if (!$canvalidate);
 1887:                             }
 1888:                             my $checked = '';
 1889:                             if ($option eq $curroption) {
 1890:                                 $checked = ' checked="checked"';
 1891:                             } elsif ($option eq 'autolimit') {
 1892:                                 if ($curroption =~ /^autolimit/) {
 1893:                                     $checked = ' checked="checked"';
 1894:                                 }                       
 1895:                             } 
 1896:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1897:                                   '<input type="radio" name="crsreq_'.$item.
 1898:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1899:                                   $titles{$option}.'</label>';
 1900:                             if ($option eq 'autolimit') {
 1901:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1902:                                                 $item.'_limit_'.$type.'" size="1" '.
 1903:                                                 'value="'.$currlimit.'" />';
 1904:                             }
 1905:                             $cell{$item} .= '</span> ';
 1906:                             if ($option eq 'autolimit') {
 1907:                                 $cell{$item} .= $titles{'unlimited'};
 1908:                             }
 1909:                         }
 1910:                     } elsif ($context eq 'requestauthor') {
 1911:                         my $curroption;
 1912:                         if (ref($settings) eq 'HASH') {
 1913:                             $curroption = $settings->{$type};
 1914:                         }
 1915:                         if (!$curroption) {
 1916:                             $curroption = 'norequest';
 1917:                         }
 1918:                         foreach my $option (@options) {
 1919:                             my $val = $option;
 1920:                             if ($option eq 'norequest') {
 1921:                                 $val = 0;
 1922:                             }
 1923:                             my $checked = '';
 1924:                             if ($option eq $curroption) {
 1925:                                 $checked = ' checked="checked"';
 1926:                             }
 1927:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1928:                                   '<input type="radio" name="authorreq_'.$type.
 1929:                                   '" value="'.$val.'"'.$checked.' />'.
 1930:                                   $titles{$option}.'</label></span>&nbsp; ';
 1931:                         }
 1932:                     } else {
 1933:                         my $checked = 'checked="checked" ';
 1934:                         if (ref($settings) eq 'HASH') {
 1935:                             if (ref($settings->{$item}) eq 'HASH') {
 1936:                                 if ($settings->{$item}->{$type} == 0) {
 1937:                                     $checked = '';
 1938:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1939:                                     $checked =  'checked="checked" ';
 1940:                                 }
 1941:                             }
 1942:                         }
 1943:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1944:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1945:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1946:                                       '</label></span>&nbsp; ';
 1947:                     }
 1948:                 }
 1949:                 if ($context eq 'requestcourses') {
 1950:                     $datatable .= '</tr><tr>';
 1951:                     foreach my $item (@usertools) {
 1952:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1953:                     }
 1954:                     $datatable .= '</tr></table>';
 1955:                 }
 1956:                 $datatable .= '</td>';
 1957:                 unless (($context eq 'requestcourses') ||
 1958:                         ($context eq 'requestauthor')) {
 1959:                     $datatable .= 
 1960:                               '<td class="LC_right_item">'.
 1961:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1962:                               '<input type="text" name="quota_'.$type.
 1963:                               '" value="'.$currdefquota.
 1964:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1965:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1966:                               '<input type="text" name="authorquota_'.$type.
 1967:                               '" value="'.$currauthorquota.
 1968:                               '" size="5" /></span></td>';
 1969:                 }
 1970:                 $datatable .= '</tr>';
 1971:             }
 1972:         }
 1973:     }
 1974:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1975:         $defaultquota = '20';
 1976:         $authorquota = '500';
 1977:         if (ref($settings) eq 'HASH') {
 1978:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1979:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1980:             } elsif (defined($settings->{'default'})) {
 1981:                 $defaultquota = $settings->{'default'};
 1982:             }
 1983:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1984:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1985:             }
 1986:         }
 1987:     }
 1988:     $typecount ++;
 1989:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1990:     $datatable .= '<tr'.$css_class.'>'.
 1991:                   '<td>'.$othertitle.'</td>'.
 1992:                   '<td class="LC_left_item">';
 1993:     if ($context eq 'requestcourses') {
 1994:         $datatable .= '<table><tr>';
 1995:     }
 1996:     my %defcell;
 1997:     foreach my $item (@usertools) {
 1998:         if ($context eq 'requestcourses') {
 1999:             my ($curroption,$currlimit);
 2000:             if (ref($settings) eq 'HASH') {
 2001:                 if (ref($settings->{$item}) eq 'HASH') {
 2002:                     $curroption = $settings->{$item}->{'default'};
 2003:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2004:                         $currlimit = $1;
 2005:                     }
 2006:                 }
 2007:             }
 2008:             if (!$curroption) {
 2009:                 $curroption = 'norequest';
 2010:             }
 2011:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2012:             foreach my $option (@options) {
 2013:                 my $val = $option;
 2014:                 if ($option eq 'norequest') {
 2015:                     $val = 0;
 2016:                 }
 2017:                 if ($option eq 'validate') {
 2018:                     my $canvalidate = 0;
 2019:                     if (ref($validations{$item}) eq 'HASH') {
 2020:                         if ($validations{$item}{'default'}) {
 2021:                             $canvalidate = 1;
 2022:                         }
 2023:                     }
 2024:                     next if (!$canvalidate);
 2025:                 }
 2026:                 my $checked = '';
 2027:                 if ($option eq $curroption) {
 2028:                     $checked = ' checked="checked"';
 2029:                 } elsif ($option eq 'autolimit') {
 2030:                     if ($curroption =~ /^autolimit/) {
 2031:                         $checked = ' checked="checked"';
 2032:                     }
 2033:                 }
 2034:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2035:                                   '<input type="radio" name="crsreq_'.$item.
 2036:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2037:                                   $titles{$option}.'</label>';
 2038:                 if ($option eq 'autolimit') {
 2039:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2040:                                        $item.'_limit_default" size="1" '.
 2041:                                        'value="'.$currlimit.'" />';
 2042:                 }
 2043:                 $defcell{$item} .= '</span> ';
 2044:                 if ($option eq 'autolimit') {
 2045:                     $defcell{$item} .= $titles{'unlimited'};
 2046:                 }
 2047:             }
 2048:         } elsif ($context eq 'requestauthor') {
 2049:             my $curroption;
 2050:             if (ref($settings) eq 'HASH') {
 2051:                 $curroption = $settings->{'default'};
 2052:             }
 2053:             if (!$curroption) {
 2054:                 $curroption = 'norequest';
 2055:             }
 2056:             foreach my $option (@options) {
 2057:                 my $val = $option;
 2058:                 if ($option eq 'norequest') {
 2059:                     $val = 0;
 2060:                 }
 2061:                 my $checked = '';
 2062:                 if ($option eq $curroption) {
 2063:                     $checked = ' checked="checked"';
 2064:                 }
 2065:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2066:                               '<input type="radio" name="authorreq_default"'.
 2067:                               ' value="'.$val.'"'.$checked.' />'.
 2068:                               $titles{$option}.'</label></span>&nbsp; ';
 2069:             }
 2070:         } else {
 2071:             my $checked = 'checked="checked" ';
 2072:             if (ref($settings) eq 'HASH') {
 2073:                 if (ref($settings->{$item}) eq 'HASH') {
 2074:                     if ($settings->{$item}->{'default'} == 0) {
 2075:                         $checked = '';
 2076:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2077:                         $checked = 'checked="checked" ';
 2078:                     }
 2079:                 }
 2080:             }
 2081:             $datatable .= '<span class="LC_nobreak"><label>'.
 2082:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2083:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2084:                           '</label></span>&nbsp; ';
 2085:         }
 2086:     }
 2087:     if ($context eq 'requestcourses') {
 2088:         $datatable .= '</tr><tr>';
 2089:         foreach my $item (@usertools) {
 2090:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2091:         }
 2092:         $datatable .= '</tr></table>';
 2093:     }
 2094:     $datatable .= '</td>';
 2095:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2096:         $datatable .= '<td class="LC_right_item">'.
 2097:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2098:                       '<input type="text" name="defaultquota" value="'.
 2099:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2100:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2101:                       '<input type="text" name="authorquota" value="'.
 2102:                       $authorquota.'" size="5" /></span></td>';
 2103:     }
 2104:     $datatable .= '</tr>';
 2105:     $typecount ++;
 2106:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2107:     $datatable .= '<tr'.$css_class.'>'.
 2108:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2109:     if ($context eq 'requestcourses') {
 2110:         $datatable .= &mt('(overrides affiliation, if set)').
 2111:                       '</td>'.
 2112:                       '<td class="LC_left_item">'.
 2113:                       '<table><tr>';
 2114:     } else {
 2115:         $datatable .= &mt('(overrides affiliation, if checked)').
 2116:                       '</td>'.
 2117:                       '<td class="LC_left_item" colspan="2">'.
 2118:                       '<br />';
 2119:     }
 2120:     my %advcell;
 2121:     foreach my $item (@usertools) {
 2122:         if ($context eq 'requestcourses') {
 2123:             my ($curroption,$currlimit);
 2124:             if (ref($settings) eq 'HASH') {
 2125:                 if (ref($settings->{$item}) eq 'HASH') {
 2126:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2127:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2128:                         $currlimit = $1;
 2129:                     }
 2130:                 }
 2131:             }
 2132:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2133:             my $checked = '';
 2134:             if ($curroption eq '') {
 2135:                 $checked = ' checked="checked"';
 2136:             }
 2137:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2138:                                '<input type="radio" name="crsreq_'.$item.
 2139:                                '__LC_adv" value=""'.$checked.' />'.
 2140:                                &mt('No override set').'</label></span>&nbsp; ';
 2141:             foreach my $option (@options) {
 2142:                 my $val = $option;
 2143:                 if ($option eq 'norequest') {
 2144:                     $val = 0;
 2145:                 }
 2146:                 if ($option eq 'validate') {
 2147:                     my $canvalidate = 0;
 2148:                     if (ref($validations{$item}) eq 'HASH') {
 2149:                         if ($validations{$item}{'_LC_adv'}) {
 2150:                             $canvalidate = 1;
 2151:                         }
 2152:                     }
 2153:                     next if (!$canvalidate);
 2154:                 }
 2155:                 my $checked = '';
 2156:                 if ($val eq $curroption) {
 2157:                     $checked = ' checked="checked"';
 2158:                 } elsif ($option eq 'autolimit') {
 2159:                     if ($curroption =~ /^autolimit/) {
 2160:                         $checked = ' checked="checked"';
 2161:                     }
 2162:                 }
 2163:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2164:                                   '<input type="radio" name="crsreq_'.$item.
 2165:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2166:                                   $titles{$option}.'</label>';
 2167:                 if ($option eq 'autolimit') {
 2168:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2169:                                        $item.'_limit__LC_adv" size="1" '.
 2170:                                        'value="'.$currlimit.'" />';
 2171:                 }
 2172:                 $advcell{$item} .= '</span> ';
 2173:                 if ($option eq 'autolimit') {
 2174:                     $advcell{$item} .= $titles{'unlimited'};
 2175:                 }
 2176:             }
 2177:         } elsif ($context eq 'requestauthor') {
 2178:             my $curroption;
 2179:             if (ref($settings) eq 'HASH') {
 2180:                 $curroption = $settings->{'_LC_adv'};
 2181:             }
 2182:             my $checked = '';
 2183:             if ($curroption eq '') {
 2184:                 $checked = ' checked="checked"';
 2185:             }
 2186:             $datatable .= '<span class="LC_nobreak"><label>'.
 2187:                           '<input type="radio" name="authorreq__LC_adv"'.
 2188:                           ' value=""'.$checked.' />'.
 2189:                           &mt('No override set').'</label></span>&nbsp; ';
 2190:             foreach my $option (@options) {
 2191:                 my $val = $option;
 2192:                 if ($option eq 'norequest') {
 2193:                     $val = 0;
 2194:                 }
 2195:                 my $checked = '';
 2196:                 if ($val eq $curroption) {
 2197:                     $checked = ' checked="checked"';
 2198:                 }
 2199:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2200:                               '<input type="radio" name="authorreq__LC_adv"'.
 2201:                               ' value="'.$val.'"'.$checked.' />'.
 2202:                               $titles{$option}.'</label></span>&nbsp; ';
 2203:             }
 2204:         } else {
 2205:             my $checked = 'checked="checked" ';
 2206:             if (ref($settings) eq 'HASH') {
 2207:                 if (ref($settings->{$item}) eq 'HASH') {
 2208:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2209:                         $checked = '';
 2210:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2211:                         $checked = 'checked="checked" ';
 2212:                     }
 2213:                 }
 2214:             }
 2215:             $datatable .= '<span class="LC_nobreak"><label>'.
 2216:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2217:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2218:                           '</label></span>&nbsp; ';
 2219:         }
 2220:     }
 2221:     if ($context eq 'requestcourses') {
 2222:         $datatable .= '</tr><tr>';
 2223:         foreach my $item (@usertools) {
 2224:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2225:         }
 2226:         $datatable .= '</tr></table>';
 2227:     }
 2228:     $datatable .= '</td></tr>';
 2229:     $$rowtotal += $typecount;
 2230:     return $datatable;
 2231: }
 2232: 
 2233: sub print_requestmail {
 2234:     my ($dom,$action,$settings,$rowtotal) = @_;
 2235:     my ($now,$datatable,%currapp);
 2236:     $now = time;
 2237:     if (ref($settings) eq 'HASH') {
 2238:         if (ref($settings->{'notify'}) eq 'HASH') {
 2239:             if ($settings->{'notify'}{'approval'} ne '') {
 2240:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2241:             }
 2242:         }
 2243:     }
 2244:     my $numinrow = 2;
 2245:     my $css_class;
 2246:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2247:     my $text;
 2248:     if ($action eq 'requestcourses') {
 2249:         $text = &mt('Receive notification of course requests requiring approval');
 2250:     } elsif ($action eq 'requestauthor') {
 2251:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2252:     } else {
 2253:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2254:     }
 2255:     $datatable = '<tr'.$css_class.'>'.
 2256:                  ' <td>'.$text.'</td>'.
 2257:                  ' <td class="LC_left_item">';
 2258:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2259:                                                  $action.'notifyapproval',%currapp);
 2260:     if ($numdc > 0) {
 2261:         $datatable .= $table;
 2262:     } else {
 2263:         $datatable .= &mt('There are no active Domain Coordinators');
 2264:     }
 2265:     $datatable .='</td></tr>';
 2266:     return $datatable;
 2267: }
 2268: 
 2269: sub print_studentcode {
 2270:     my ($settings,$rowtotal) = @_;
 2271:     my $rownum = 0; 
 2272:     my ($output,%current);
 2273:     my @crstypes = ('official','unofficial','community','textbook','placement');
 2274:     if (ref($settings) eq 'HASH') {
 2275:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2276:             foreach my $type (@crstypes) {
 2277:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2278:             }
 2279:         }
 2280:     }
 2281:     $output .= '<tr>'.
 2282:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2283:                '<td class="LC_left_item">';
 2284:     foreach my $type (@crstypes) {
 2285:         my $check = ' ';
 2286:         if ($current{$type}) {
 2287:             $check = ' checked="checked" ';
 2288:         }
 2289:         $output .= '<span class="LC_nobreak"><label>'.
 2290:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2291:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2292:     }
 2293:     $output .= '</td></tr>';
 2294:     $$rowtotal ++;
 2295:     return $output;
 2296: }
 2297: 
 2298: sub print_textbookcourses {
 2299:     my ($dom,$type,$settings,$rowtotal) = @_;
 2300:     my $rownum = 0;
 2301:     my $css_class;
 2302:     my $itemcount = 1;
 2303:     my $maxnum = 0;
 2304:     my $bookshash;
 2305:     if (ref($settings) eq 'HASH') {
 2306:         $bookshash = $settings->{$type};
 2307:     }
 2308:     my %ordered;
 2309:     if (ref($bookshash) eq 'HASH') {
 2310:         foreach my $item (keys(%{$bookshash})) {
 2311:             if (ref($bookshash->{$item}) eq 'HASH') {
 2312:                 my $num = $bookshash->{$item}{'order'};
 2313:                 $ordered{$num} = $item;
 2314:             }
 2315:         }
 2316:     }
 2317:     my $confname = $dom.'-domainconfig';
 2318:     my $switchserver = &check_switchserver($dom,$confname);
 2319:     my $maxnum = scalar(keys(%ordered));
 2320:     my $datatable;
 2321:     if (keys(%ordered)) {
 2322:         my @items = sort { $a <=> $b } keys(%ordered);
 2323:         for (my $i=0; $i<@items; $i++) {
 2324:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2325:             my $key = $ordered{$items[$i]};
 2326:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2327:             my $coursetitle = $coursehash{'description'};
 2328:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2329:             if (ref($bookshash->{$key}) eq 'HASH') {
 2330:                 $subject = $bookshash->{$key}->{'subject'};
 2331:                 $title = $bookshash->{$key}->{'title'};
 2332:                 if ($type eq 'textbooks') {
 2333:                     $publisher = $bookshash->{$key}->{'publisher'};
 2334:                     $author = $bookshash->{$key}->{'author'};
 2335:                     $image = $bookshash->{$key}->{'image'};
 2336:                     if ($image ne '') {
 2337:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2338:                         my $imagethumb = "$path/tn-".$imagefile;
 2339:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2340:                     }
 2341:                 }
 2342:             }
 2343:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2344:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2345:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2346:             for (my $k=0; $k<=$maxnum; $k++) {
 2347:                 my $vpos = $k+1;
 2348:                 my $selstr;
 2349:                 if ($k == $i) {
 2350:                     $selstr = ' selected="selected" ';
 2351:                 }
 2352:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2353:             }
 2354:             $datatable .= '</select>'.('&nbsp;'x2).
 2355:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2356:                 &mt('Delete?').'</label></span></td>'.
 2357:                 '<td colspan="2">'.
 2358:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2359:                 ('&nbsp;'x2).
 2360:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2361:             if ($type eq 'textbooks') {
 2362:                 $datatable .= ('&nbsp;'x2).
 2363:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2364:                               ('&nbsp;'x2).
 2365:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2366:                               ('&nbsp;'x2).
 2367:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2368:                 if ($image) {
 2369:                     $datatable .= $imgsrc.
 2370:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2371:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2372:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2373:                 }
 2374:                 if ($switchserver) {
 2375:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2376:                 } else {
 2377:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2378:                 }
 2379:             }
 2380:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2381:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2382:                           $coursetitle.'</span></td></tr>'."\n";
 2383:             $itemcount ++;
 2384:         }
 2385:     }
 2386:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2387:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2388:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2389:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2390:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2391:     for (my $k=0; $k<$maxnum+1; $k++) {
 2392:         my $vpos = $k+1;
 2393:         my $selstr;
 2394:         if ($k == $maxnum) {
 2395:             $selstr = ' selected="selected" ';
 2396:         }
 2397:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2398:     }
 2399:     $datatable .= '</select>&nbsp;'."\n".
 2400:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2401:                   '<td colspan="2">'.
 2402:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2403:                   ('&nbsp;'x2).
 2404:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2405:                   ('&nbsp;'x2);
 2406:     if ($type eq 'textbooks') {
 2407:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2408:                       ('&nbsp;'x2).
 2409:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2410:                       ('&nbsp;'x2).
 2411:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2412:         if ($switchserver) {
 2413:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2414:         } else {
 2415:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2416:         }
 2417:     }
 2418:     $datatable .= '</span>'."\n".
 2419:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2420:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2421:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2422:                   &Apache::loncommon::selectcourse_link
 2423:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2424:                   '</span></td>'."\n".
 2425:                   '</tr>'."\n";
 2426:     $itemcount ++;
 2427:     return $datatable;
 2428: }
 2429: 
 2430: sub textbookcourses_javascript {
 2431:     my ($settings) = @_;
 2432:     return unless(ref($settings) eq 'HASH');
 2433:     my (%ordered,%total,%jstext);
 2434:     foreach my $type ('textbooks','templates') {
 2435:         $total{$type} = 0;
 2436:         if (ref($settings->{$type}) eq 'HASH') {
 2437:             foreach my $item (keys(%{$settings->{$type}})) {
 2438:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2439:                     my $num = $settings->{$type}->{$item}{'order'};
 2440:                     $ordered{$type}{$num} = $item;
 2441:                 }
 2442:             }
 2443:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2444:         }
 2445:         my @jsarray = ();
 2446:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2447:             push(@jsarray,$ordered{$type}{$item});
 2448:         }
 2449:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2450:     }
 2451:     return <<"ENDSCRIPT";
 2452: <script type="text/javascript">
 2453: // <![CDATA[
 2454: function reorderBooks(form,item,caller) {
 2455:     var changedVal;
 2456: $jstext{'textbooks'};
 2457: $jstext{'templates'};
 2458:     var newpos;
 2459:     var maxh;
 2460:     if (caller == 'textbooks') {  
 2461:         newpos = 'textbooks_addbook_pos';
 2462:         maxh = 1 + $total{'textbooks'};
 2463:     } else {
 2464:         newpos = 'templates_addbook_pos';
 2465:         maxh = 1 + $total{'templates'};
 2466:     }
 2467:     var current = new Array;
 2468:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2469:     if (item == newpos) {
 2470:         changedVal = newitemVal;
 2471:     } else {
 2472:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2473:         current[newitemVal] = newpos;
 2474:     }
 2475:     if (caller == 'textbooks') {
 2476:         for (var i=0; i<textbooks.length; i++) {
 2477:             var elementName = 'textbooks_'+textbooks[i];
 2478:             if (elementName != item) {
 2479:                 if (form.elements[elementName]) {
 2480:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2481:                     current[currVal] = elementName;
 2482:                 }
 2483:             }
 2484:         }
 2485:     }
 2486:     if (caller == 'templates') {
 2487:         for (var i=0; i<templates.length; i++) {
 2488:             var elementName = 'templates_'+templates[i];
 2489:             if (elementName != item) {
 2490:                 if (form.elements[elementName]) {
 2491:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2492:                     current[currVal] = elementName;
 2493:                 }
 2494:             }
 2495:         }
 2496:     }
 2497:     var oldVal;
 2498:     for (var j=0; j<maxh; j++) {
 2499:         if (current[j] == undefined) {
 2500:             oldVal = j;
 2501:         }
 2502:     }
 2503:     if (oldVal < changedVal) {
 2504:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2505:            var elementName = current[k];
 2506:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2507:         }
 2508:     } else {
 2509:         for (var k=changedVal; k<oldVal; k++) {
 2510:             var elementName = current[k];
 2511:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2512:         }
 2513:     }
 2514:     return;
 2515: }
 2516: 
 2517: // ]]>
 2518: </script>
 2519: 
 2520: ENDSCRIPT
 2521: }
 2522: 
 2523: sub ltitools_javascript {
 2524:     my ($settings) = @_;
 2525:     return unless(ref($settings) eq 'HASH');
 2526:     my (%ordered,$total,%jstext);
 2527:     $total = 0;
 2528:     foreach my $item (keys(%{$settings})) {
 2529:         if (ref($settings->{$item}) eq 'HASH') {
 2530:             my $num = $settings->{$item}{'order'};
 2531:             $ordered{$num} = $item;
 2532:         }
 2533:     }
 2534:     $total = scalar(keys(%{$settings}));
 2535:     my @jsarray = ();
 2536:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2537:         push(@jsarray,$ordered{$item});
 2538:     }
 2539:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2540:     return <<"ENDSCRIPT";
 2541: <script type="text/javascript">
 2542: // <![CDATA[
 2543: function reorderLTI(form,item) {
 2544:     var changedVal;
 2545: $jstext
 2546:     var newpos = 'ltitools_add_pos';
 2547:     var maxh = 1 + $total;
 2548:     var current = new Array;
 2549:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2550:     if (item == newpos) {
 2551:         changedVal = newitemVal;
 2552:     } else {
 2553:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2554:         current[newitemVal] = newpos;
 2555:     }
 2556:     for (var i=0; i<ltitools.length; i++) {
 2557:         var elementName = 'ltitools_'+ltitools[i];
 2558:         if (elementName != item) {
 2559:             if (form.elements[elementName]) {
 2560:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2561:                 current[currVal] = elementName;
 2562:             }
 2563:         }
 2564:     }
 2565:     var oldVal;
 2566:     for (var j=0; j<maxh; j++) {
 2567:         if (current[j] == undefined) {
 2568:             oldVal = j;
 2569:         }
 2570:     }
 2571:     if (oldVal < changedVal) {
 2572:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2573:            var elementName = current[k];
 2574:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2575:         }
 2576:     } else {
 2577:         for (var k=changedVal; k<oldVal; k++) {
 2578:             var elementName = current[k];
 2579:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2580:         }
 2581:     }
 2582:     return;
 2583: }
 2584: 
 2585: // ]]>
 2586: </script>
 2587: 
 2588: ENDSCRIPT
 2589: }
 2590: 
 2591: sub print_autoenroll {
 2592:     my ($dom,$settings,$rowtotal) = @_;
 2593:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2594:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2595:     if (ref($settings) eq 'HASH') {
 2596:         if (exists($settings->{'run'})) {
 2597:             if ($settings->{'run'} eq '0') {
 2598:                 $runoff = ' checked="checked" ';
 2599:                 $runon = ' ';
 2600:             } else {
 2601:                 $runon = ' checked="checked" ';
 2602:                 $runoff = ' ';
 2603:             }
 2604:         } else {
 2605:             if ($autorun) {
 2606:                 $runon = ' checked="checked" ';
 2607:                 $runoff = ' ';
 2608:             } else {
 2609:                 $runoff = ' checked="checked" ';
 2610:                 $runon = ' ';
 2611:             }
 2612:         }
 2613:         if (exists($settings->{'co-owners'})) {
 2614:             if ($settings->{'co-owners'} eq '0') {
 2615:                 $coownersoff = ' checked="checked" ';
 2616:                 $coownerson = ' ';
 2617:             } else {
 2618:                 $coownerson = ' checked="checked" ';
 2619:                 $coownersoff = ' ';
 2620:             }
 2621:         } else {
 2622:             $coownersoff = ' checked="checked" ';
 2623:             $coownerson = ' ';
 2624:         }
 2625:         if (exists($settings->{'sender_domain'})) {
 2626:             $defdom = $settings->{'sender_domain'};
 2627:         }
 2628:         if (exists($settings->{'autofailsafe'})) {
 2629:             $failsafe = $settings->{'autofailsafe'};
 2630:         }
 2631:     } else {
 2632:         if ($autorun) {
 2633:             $runon = ' checked="checked" ';
 2634:             $runoff = ' ';
 2635:         } else {
 2636:             $runoff = ' checked="checked" ';
 2637:             $runon = ' ';
 2638:         }
 2639:     }
 2640:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2641:     my $notif_sender;
 2642:     if (ref($settings) eq 'HASH') {
 2643:         $notif_sender = $settings->{'sender_uname'};
 2644:     }
 2645:     my $datatable='<tr class="LC_odd_row">'.
 2646:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2647:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2648:                   '<input type="radio" name="autoenroll_run"'.
 2649:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2650:                   '<label><input type="radio" name="autoenroll_run"'.
 2651:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2652:                   '</tr><tr>'.
 2653:                   '<td>'.&mt('Notification messages - sender').
 2654:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2655:                   &mt('username').':&nbsp;'.
 2656:                   '<input type="text" name="sender_uname" value="'.
 2657:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2658:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2659:                   '<tr class="LC_odd_row">'.
 2660:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2661:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2662:                   '<input type="radio" name="autoassign_coowners"'.
 2663:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2664:                   '<label><input type="radio" name="autoassign_coowners"'.
 2665:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2666:                   '</tr><tr>'.
 2667:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2668:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2669:                   '<input type="text" name="autoenroll_failsafe"'.
 2670:                   ' value="'.$failsafe.'" size="4" /></td></tr>';
 2671:     $$rowtotal += 4;
 2672:     return $datatable;
 2673: }
 2674: 
 2675: sub print_autoupdate {
 2676:     my ($position,$dom,$settings,$rowtotal) = @_;
 2677:     my $datatable;
 2678:     if ($position eq 'top') {
 2679:         my $updateon = ' ';
 2680:         my $updateoff = ' checked="checked" ';
 2681:         my $classlistson = ' ';
 2682:         my $classlistsoff = ' checked="checked" ';
 2683:         if (ref($settings) eq 'HASH') {
 2684:             if ($settings->{'run'} eq '1') {
 2685:                 $updateon = $updateoff;
 2686:                 $updateoff = ' ';
 2687:             }
 2688:             if ($settings->{'classlists'} eq '1') {
 2689:                 $classlistson = $classlistsoff;
 2690:                 $classlistsoff = ' ';
 2691:             }
 2692:         }
 2693:         my %title = (
 2694:                    run => 'Auto-update active?',
 2695:                    classlists => 'Update information in classlists?',
 2696:                     );
 2697:         $datatable = '<tr class="LC_odd_row">'. 
 2698:                   '<td>'.&mt($title{'run'}).'</td>'.
 2699:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2700:                   '<input type="radio" name="autoupdate_run"'.
 2701:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2702:                   '<label><input type="radio" name="autoupdate_run"'.
 2703:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2704:                   '</tr><tr>'.
 2705:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2706:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2707:                   '<label><input type="radio" name="classlists"'.
 2708:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2709:                   '<label><input type="radio" name="classlists"'.
 2710:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2711:                   '</tr>';
 2712:         $$rowtotal += 2;
 2713:     } elsif ($position eq 'middle') {
 2714:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2715:         my $numinrow = 3;
 2716:         my $locknamesettings;
 2717:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2718:                                      $dom,$numinrow,$othertitle,
 2719:                                     'lockablenames');
 2720:         $$rowtotal ++;
 2721:     } else {
 2722:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2723:         my @fields = ('lastname','firstname','middlename','generation',
 2724:                       'permanentemail','id');
 2725:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2726:         my $numrows = 0;
 2727:         if (ref($types) eq 'ARRAY') {
 2728:             if (@{$types} > 0) {
 2729:                 $datatable = 
 2730:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2731:                                          \@fields,$types,\$numrows);
 2732:                     $$rowtotal += @{$types}; 
 2733:             }
 2734:         }
 2735:         $datatable .= 
 2736:             &usertype_update_row($settings,{'default' => $othertitle},
 2737:                                  \%fieldtitles,\@fields,['default'],
 2738:                                  \$numrows);
 2739:         $$rowtotal ++;     
 2740:     }
 2741:     return $datatable;
 2742: }
 2743: 
 2744: sub print_autocreate {
 2745:     my ($dom,$settings,$rowtotal) = @_;
 2746:     my (%createon,%createoff,%currhash);
 2747:     my @types = ('xml','req');
 2748:     if (ref($settings) eq 'HASH') {
 2749:         foreach my $item (@types) {
 2750:             $createoff{$item} = ' checked="checked" ';
 2751:             $createon{$item} = ' ';
 2752:             if (exists($settings->{$item})) {
 2753:                 if ($settings->{$item}) {
 2754:                     $createon{$item} = ' checked="checked" ';
 2755:                     $createoff{$item} = ' ';
 2756:                 }
 2757:             }
 2758:         }
 2759:         if ($settings->{'xmldc'} ne '') {
 2760:             $currhash{$settings->{'xmldc'}} = 1;
 2761:         }
 2762:     } else {
 2763:         foreach my $item (@types) {
 2764:             $createoff{$item} = ' checked="checked" ';
 2765:             $createon{$item} = ' ';
 2766:         }
 2767:     }
 2768:     $$rowtotal += 2;
 2769:     my $numinrow = 2;
 2770:     my $datatable='<tr class="LC_odd_row">'.
 2771:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2772:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2773:                   '<input type="radio" name="autocreate_xml"'.
 2774:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2775:                   '<label><input type="radio" name="autocreate_xml"'.
 2776:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2777:                   '</td></tr><tr>'.
 2778:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2779:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2780:                   '<input type="radio" name="autocreate_req"'.
 2781:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2782:                   '<label><input type="radio" name="autocreate_req"'.
 2783:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2784:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2785:                                                    'autocreate_xmldc',%currhash);
 2786:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2787:     if ($numdc > 1) {
 2788:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2789:                       '</td><td class="LC_left_item">';
 2790:     } else {
 2791:         $datatable .= &mt('Course creation processed as:').
 2792:                       '</td><td class="LC_right_item">';
 2793:     }
 2794:     $datatable .= $dctable.'</td></tr>';
 2795:     $$rowtotal += $rows;
 2796:     return $datatable;
 2797: }
 2798: 
 2799: sub print_directorysrch {
 2800:     my ($position,$dom,$settings,$rowtotal) = @_;
 2801:     my $datatable;
 2802:     if ($position eq 'top') {
 2803:         my $instsrchon = ' ';
 2804:         my $instsrchoff = ' checked="checked" ';
 2805:         my ($exacton,$containson,$beginson);
 2806:         my $instlocalon = ' ';
 2807:         my $instlocaloff = ' checked="checked" ';
 2808:         if (ref($settings) eq 'HASH') {
 2809:             if ($settings->{'available'} eq '1') {
 2810:                 $instsrchon = $instsrchoff;
 2811:                 $instsrchoff = ' ';
 2812:             }
 2813:             if ($settings->{'localonly'} eq '1') {
 2814:                 $instlocalon = $instlocaloff;
 2815:                 $instlocaloff = ' ';
 2816:             }
 2817:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2818:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 2819:                     if ($type eq 'exact') {
 2820:                         $exacton = ' checked="checked" ';
 2821:                     } elsif ($type eq 'contains') {
 2822:                         $containson = ' checked="checked" ';
 2823:                     } elsif ($type eq 'begins') {
 2824:                         $beginson = ' checked="checked" ';
 2825:                     }
 2826:                 }
 2827:             } else {
 2828:                 if ($settings->{'searchtypes'} eq 'exact') {
 2829:                     $exacton = ' checked="checked" ';
 2830:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 2831:                     $containson = ' checked="checked" ';
 2832:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 2833:                     $exacton = ' checked="checked" ';
 2834:                     $containson = ' checked="checked" ';
 2835:                 }
 2836:             }
 2837:         }
 2838:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2839:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2840: 
 2841:         my $numinrow = 4;
 2842:         my $cansrchrow = 0;
 2843:         $datatable='<tr class="LC_odd_row">'.
 2844:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 2845:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2846:                    '<input type="radio" name="dirsrch_available"'.
 2847:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2848:                    '<label><input type="radio" name="dirsrch_available"'.
 2849:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2850:                    '</tr><tr>'.
 2851:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 2852:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2853:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 2854:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2855:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 2856:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2857:                    '</tr>';
 2858:         $$rowtotal += 2;
 2859:         if (ref($usertypes) eq 'HASH') {
 2860:             if (keys(%{$usertypes}) > 0) {
 2861:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2862:                                              $numinrow,$othertitle,'cansearch');
 2863:                 $cansrchrow = 1;
 2864:             }
 2865:         }
 2866:         if ($cansrchrow) {
 2867:             $$rowtotal ++;
 2868:             $datatable .= '<tr>';
 2869:         } else {
 2870:             $datatable .= '<tr class="LC_odd_row">';
 2871:         }
 2872:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2873:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2874:         foreach my $title (@{$titleorder}) {
 2875:             if (defined($searchtitles->{$title})) {
 2876:                 my $check = ' ';
 2877:                 if (ref($settings) eq 'HASH') {
 2878:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2879:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2880:                             $check = ' checked="checked" ';
 2881:                         }
 2882:                     }
 2883:                 }
 2884:                 $datatable .= '<td class="LC_left_item">'.
 2885:                               '<span class="LC_nobreak"><label>'.
 2886:                               '<input type="checkbox" name="searchby" '.
 2887:                               'value="'.$title.'"'.$check.'/>'.
 2888:                               $searchtitles->{$title}.'</label></span></td>';
 2889:             }
 2890:         }
 2891:         $datatable .= '</tr></table></td></tr>';
 2892:         $$rowtotal ++;
 2893:         if ($cansrchrow) {
 2894:             $datatable .= '<tr class="LC_odd_row">';
 2895:         } else {
 2896:             $datatable .= '<tr>';
 2897:         }
 2898:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2899:                       '<td class="LC_left_item" colspan="2">'.
 2900:                       '<span class="LC_nobreak"><label>'.
 2901:                       '<input type="checkbox" name="searchtypes" '.
 2902:                       $exacton.' value="exact" />'.&mt('Exact match').
 2903:                       '</label>&nbsp;'.
 2904:                       '<label><input type="checkbox" name="searchtypes" '.
 2905:                       $beginson.' value="begins" />'.&mt('Begins with').
 2906:                       '</label>&nbsp;'.
 2907:                       '<label><input type="checkbox" name="searchtypes" '.
 2908:                       $containson.' value="contains" />'.&mt('Contains').
 2909:                       '</label></span></td></tr>';
 2910:         $$rowtotal ++;
 2911:     } else {
 2912:         my $domsrchon = ' checked="checked" ';
 2913:         my $domsrchoff = ' ';
 2914:         my $domlocalon = ' ';
 2915:         my $domlocaloff = ' checked="checked" ';
 2916:         if (ref($settings) eq 'HASH') {
 2917:             if ($settings->{'lclocalonly'} eq '1') {
 2918:                 $domlocalon = $domlocaloff;
 2919:                 $domlocaloff = ' ';
 2920:             }
 2921:             if ($settings->{'lcavailable'} eq '0') {
 2922:                 $domsrchoff = $domsrchon;
 2923:                 $domsrchon = ' ';
 2924:             }
 2925:         }
 2926:         $datatable='<tr class="LC_odd_row">'.
 2927:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 2928:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2929:                       '<input type="radio" name="dirsrch_domavailable"'.
 2930:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2931:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 2932:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2933:                       '</tr><tr>'.
 2934:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 2935:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2936:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 2937:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2938:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 2939:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2940:                       '</tr>';
 2941:         $$rowtotal += 2;
 2942:     }
 2943:     return $datatable;
 2944: }
 2945: 
 2946: sub print_contacts {
 2947:     my ($dom,$settings,$rowtotal) = @_;
 2948:     my $datatable;
 2949:     my @contacts = ('adminemail','supportemail');
 2950:     my (%checked,%to,%otheremails,%bccemails);
 2951:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2952:                     'requestsmail','updatesmail','idconflictsmail');
 2953:     foreach my $type (@mailings) {
 2954:         $otheremails{$type} = '';
 2955:     }
 2956:     $bccemails{'helpdeskmail'} = '';
 2957:     if (ref($settings) eq 'HASH') {
 2958:         foreach my $item (@contacts) {
 2959:             if (exists($settings->{$item})) {
 2960:                 $to{$item} = $settings->{$item};
 2961:             }
 2962:         }
 2963:         foreach my $type (@mailings) {
 2964:             if (exists($settings->{$type})) {
 2965:                 if (ref($settings->{$type}) eq 'HASH') {
 2966:                     foreach my $item (@contacts) {
 2967:                         if ($settings->{$type}{$item}) {
 2968:                             $checked{$type}{$item} = ' checked="checked" ';
 2969:                         }
 2970:                     }
 2971:                     $otheremails{$type} = $settings->{$type}{'others'};
 2972:                     if ($type eq 'helpdeskmail') {
 2973:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2974:                     }
 2975:                 }
 2976:             } elsif ($type eq 'lonstatusmail') {
 2977:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2978:             }
 2979:         }
 2980:     } else {
 2981:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2982:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2983:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2984:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2985:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2986:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2987:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2988:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2989:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2990:     }
 2991:     my ($titles,$short_titles) = &contact_titles();
 2992:     my $rownum = 0;
 2993:     my $css_class;
 2994:     foreach my $item (@contacts) {
 2995:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2996:         $datatable .= '<tr'.$css_class.'>'. 
 2997:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2998:                   '</span></td><td class="LC_right_item">'.
 2999:                   '<input type="text" name="'.$item.'" value="'.
 3000:                   $to{$item}.'" /></td></tr>';
 3001:         $rownum ++;
 3002:     }
 3003:     foreach my $type (@mailings) {
 3004:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3005:         $datatable .= '<tr'.$css_class.'>'.
 3006:                       '<td><span class="LC_nobreak">'.
 3007:                       $titles->{$type}.': </span></td>'.
 3008:                       '<td class="LC_left_item">'.
 3009:                       '<span class="LC_nobreak">';
 3010:         foreach my $item (@contacts) {
 3011:             $datatable .= '<label>'.
 3012:                           '<input type="checkbox" name="'.$type.'"'.
 3013:                           $checked{$type}{$item}.
 3014:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 3015:                           '</label>&nbsp;';
 3016:         }
 3017:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3018:                       '<input type="text" name="'.$type.'_others" '.
 3019:                       'value="'.$otheremails{$type}.'"  />';
 3020:         if ($type eq 'helpdeskmail') {
 3021:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3022:                           '<input type="text" name="'.$type.'_bcc" '.
 3023:                           'value="'.$bccemails{$type}.'"  />';
 3024:         }
 3025:         $datatable .= '</td></tr>'."\n";
 3026:         $rownum ++;
 3027:     }
 3028:     my %choices;
 3029:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 3030:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3031:                                    &mt('LON-CAPA core group - MSU'),600,500));
 3032:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3033:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3034:                                     &mt('LON-CAPA core group - MSU'),600,500));
 3035:     my @toggles = ('reporterrors','reportupdates');
 3036:     my %defaultchecked = ('reporterrors'  => 'on',
 3037:                           'reportupdates' => 'on');
 3038:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3039:                                                \%choices,$rownum);
 3040:     $datatable .= $reports;
 3041:     $$rowtotal += $rownum;
 3042:     return $datatable;
 3043: }
 3044: 
 3045: sub print_helpsettings {
 3046:     my ($position,$dom,$settings,$rowtotal) = @_;
 3047:     my $confname = $dom.'-domainconfig';
 3048:     my $formname = 'display';
 3049:     my ($datatable,$itemcount);
 3050:     if ($position eq 'top') {
 3051:         $itemcount = 1;
 3052:         my (%choices,%defaultchecked,@toggles);
 3053:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3054:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3055:                                      &mt('LON-CAPA bug tracker'),600,500));
 3056:         %defaultchecked = ('submitbugs' => 'on');
 3057:         @toggles = ('submitbugs');
 3058:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3059:                                                      \%choices,$itemcount);
 3060:         $$rowtotal ++;
 3061:     } else {
 3062:         my $css_class;
 3063:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3064:         my (%customroles,%ordered,%current);
 3065:         if (ref($settings->{'adhoc'}) eq 'HASH') {
 3066:             %current = %{$settings->{'adhoc'}};
 3067:         }
 3068:         my $count = 0;
 3069:         foreach my $key (sort(keys(%existing))) {
 3070:             if ($key=~/^rolesdef\_(\w+)$/) {
 3071:                 my $rolename = $1;
 3072:                 my (%privs,$order);
 3073:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3074:                 $customroles{$rolename} = \%privs;
 3075:                 if (ref($current{$rolename}) eq 'HASH') {
 3076:                     $order = $current{$rolename}{'order'};
 3077:                 }
 3078:                 if ($order eq '') {
 3079:                     $order = $count;
 3080:                 }
 3081:                 $ordered{$order} = $rolename;
 3082:                 $count++;
 3083:             }
 3084:         }
 3085:         my $maxnum = scalar(keys(%ordered));
 3086:         my @roles_by_num = ();
 3087:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3088:             push(@roles_by_num,$item);
 3089:         }
 3090:         my $context = 'domprefs';
 3091:         my $crstype = 'Course';
 3092:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3093:         my @accesstypes = ('all','none');
 3094:         my ($numstatustypes,@jsarray);
 3095:         if (ref($types) eq 'ARRAY') {
 3096:             if (@{$types} > 0) {
 3097:                 $numstatustypes = scalar(@{$types});
 3098:                 push(@accesstypes,'status');
 3099:                 @jsarray = ('bystatus');
 3100:             }
 3101:         }
 3102:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh']);
 3103:         if (keys(%domhelpdesk)) {
 3104:             push(@accesstypes,('inc','exc'));
 3105:             push(@jsarray,('notinc','notexc'));
 3106:         }
 3107:         my $hiddenstr = join("','",@jsarray);
 3108:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3109:         my $context = 'domprefs';
 3110:         my $crstype = 'Course';
 3111:         my $prefix = 'helproles_';
 3112:         my $add_class = 'LC_hidden';
 3113:         foreach my $num (@roles_by_num) {
 3114:             my $role = $ordered{$num};
 3115:             my ($desc,$access,@statuses);
 3116:             if (ref($current{$role}) eq 'HASH') {
 3117:                 $desc = $current{$role}{'desc'};
 3118:                 $access = $current{$role}{'access'};
 3119:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3120:                     @statuses = @{$current{$role}{'insttypes'}};
 3121:                 }
 3122:             }
 3123:             if ($desc eq '') {
 3124:                 $desc = $role;
 3125:             }
 3126:             my $identifier = 'custhelp'.$num;
 3127:             my %full=();
 3128:             my %levels= (
 3129:                          course => {},
 3130:                          domain => {},
 3131:                          system => {},
 3132:                         );
 3133:             my %levelscurrent=(
 3134:                                course => {},
 3135:                                domain => {},
 3136:                                system => {},
 3137:                               );
 3138:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3139:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3140:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3141:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3142:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 3143:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3144:             for (my $k=0; $k<=$maxnum; $k++) {
 3145:                 my $vpos = $k+1;
 3146:                 my $selstr;
 3147:                 if ($k == $num) {
 3148:                     $selstr = ' selected="selected" ';
 3149:                 }
 3150:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3151:             }
 3152:             $datatable .= '</select>'.('&nbsp;'x2).
 3153:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3154:                           '</td>'.
 3155:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3156:                           &mt('Name shown to users:').
 3157:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3158:                           '</fieldset>'.
 3159:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3160:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3161:                           '<fieldset>'.
 3162:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3163:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3164:                                                                    \%levelscurrent,$identifier,
 3165:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3166:                           '</fieldset></td>';
 3167:             $itemcount ++;
 3168:         }
 3169:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3170:         my $newcust = 'custhelp'.$count;
 3171:         my (%privs,%levelscurrent);
 3172:         my %full=();
 3173:         my %levels= (
 3174:                      course => {},
 3175:                      domain => {},
 3176:                      system => {},
 3177:                     );
 3178:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3179:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3180:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3181:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 3182:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3183:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3184:         for (my $k=0; $k<$maxnum+1; $k++) {
 3185:             my $vpos = $k+1;
 3186:             my $selstr;
 3187:             if ($k == $maxnum) {
 3188:                 $selstr = ' selected="selected" ';
 3189:             }
 3190:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3191:         }
 3192:         $datatable .= '</select>&nbsp;'."\n".
 3193:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3194:                       '</label></span></td>'.
 3195:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3196:                       '<span class="LC_nobreak">'.
 3197:                       &mt('Internal name:').
 3198:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3199:                       '</span>'.('&nbsp;'x4).
 3200:                       '<span class="LC_nobreak">'.
 3201:                       &mt('Name shown to users:').
 3202:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3203:                       '</span></fieldset>'.
 3204:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3205:                                              $usertypes,$types,\%domhelpdesk).
 3206:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3207:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3208:                                                                 \@templateroles,$newcust).
 3209:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3210:                                                                \%levelscurrent,$newcust).
 3211:                       '</fieldset></td></tr>';
 3212:         $count ++;
 3213:         $$rowtotal += $count;
 3214:     }
 3215:     return $datatable;
 3216: }
 3217: 
 3218: sub adhocbutton {
 3219:     my ($prefix,$num,$field,$visibility) = @_;
 3220:     my %lt = &Apache::lonlocal::texthash(
 3221:                                           show => 'Show details',
 3222:                                           hide => 'Hide details',
 3223:                                         );
 3224:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3225:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3226:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3227:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3228: }
 3229: 
 3230: sub helpsettings_javascript {
 3231:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3232:     return unless(ref($roles_by_num) eq 'ARRAY');
 3233:     my %html_js_lt = &Apache::lonlocal::texthash(
 3234:                                           show => 'Show details',
 3235:                                           hide => 'Hide details',
 3236:                                         );
 3237:     &html_escape(\%html_js_lt);
 3238:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3239:     return <<"ENDSCRIPT";
 3240: <script type="text/javascript">
 3241: // <![CDATA[
 3242: 
 3243: function reorderHelpRoles(form,item) {
 3244:     var changedVal;
 3245: $jstext
 3246:     var newpos = 'helproles_${total}_pos';
 3247:     var maxh = 1 + $total;
 3248:     var current = new Array();
 3249:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3250:     if (item == newpos) {
 3251:         changedVal = newitemVal;
 3252:     } else {
 3253:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3254:         current[newitemVal] = newpos;
 3255:     }
 3256:     for (var i=0; i<helproles.length; i++) {
 3257:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3258:         if (elementName != item) {
 3259:             if (form.elements[elementName]) {
 3260:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3261:                 current[currVal] = elementName;
 3262:             }
 3263:         }
 3264:     }
 3265:     var oldVal;
 3266:     for (var j=0; j<maxh; j++) {
 3267:         if (current[j] == undefined) {
 3268:             oldVal = j;
 3269:         }
 3270:     }
 3271:     if (oldVal < changedVal) {
 3272:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3273:            var elementName = current[k];
 3274:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3275:         }
 3276:     } else {
 3277:         for (var k=changedVal; k<oldVal; k++) {
 3278:             var elementName = current[k];
 3279:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3280:         }
 3281:     }
 3282:     return;
 3283: }
 3284: 
 3285: function helpdeskAccess(num) {
 3286:     var curraccess = null;
 3287:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3288:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3289:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3290:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3291:             }
 3292:         }
 3293:     }
 3294:     var shown = Array();
 3295:     var hidden = Array();
 3296:     if (curraccess == 'none') {
 3297:         hidden = Array('$hiddenstr');
 3298:     } else {
 3299:         if (curraccess == 'status') {
 3300:             shown = Array('bystatus');
 3301:             hidden = Array('notinc','notexc');
 3302:         } else {
 3303:             if (curraccess == 'exc') {
 3304:                 shown = Array('notexc');
 3305:                 hidden = Array('notinc','bystatus');
 3306:             }
 3307:             if (curraccess == 'inc') {
 3308:                 shown = Array('notinc');
 3309:                 hidden = Array('notexc','bystatus');
 3310:             }
 3311:             if (curraccess == 'all') {
 3312:                 hidden = Array('notinc','notexc','bystatus');
 3313:             }
 3314:         }
 3315:     }
 3316:     if (hidden.length > 0) {
 3317:         for (var i=0; i<hidden.length; i++) {
 3318:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3319:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3320:             }
 3321:         }
 3322:     }
 3323:     if (shown.length > 0) {
 3324:         for (var i=0; i<shown.length; i++) {
 3325:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3326:                 if (shown[i] == 'privs') {
 3327:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3328:                 } else {
 3329:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3330:                 }
 3331:             }
 3332:         }
 3333:     }
 3334:     return;
 3335: }
 3336: 
 3337: function toggleHelpdeskItem(num,field) {
 3338:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3339:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3340:             document.getElementById('helproles_'+num+'_'+field).className = 
 3341:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3342:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3343:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3344:             }
 3345:         } else {
 3346:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3347:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3348:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3349:             }
 3350:         }
 3351:     }
 3352:     return;
 3353: }
 3354: 
 3355: // ]]>
 3356: </script>
 3357: 
 3358: ENDSCRIPT
 3359: }
 3360: 
 3361: sub helpdeskroles_access {
 3362:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3363:         $usertypes,$types,$domhelpdesk) = @_;
 3364:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3365:     my %lt = &Apache::lonlocal::texthash(
 3366:                     'rou'    => 'Role usage',
 3367:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3368:                     'all'    => 'All',
 3369:                     'none'   => 'None',
 3370:                     'status' => 'Determined based on institutional status',
 3371:                     'inc'    => 'Include all, but exclude specific personnel',
 3372:                     'exc'    => 'Exclude all, but include specific personnel',
 3373:                   );
 3374:     my %usecheck = (
 3375:                      all => ' checked="checked"',
 3376:                    );
 3377:     my %displaydiv = (
 3378:                       status => 'none',
 3379:                       inc    => 'none',
 3380:                       exc    => 'none',
 3381:                       priv   => 'block',
 3382:                      );
 3383:     my $output;
 3384:     if (ref($current) eq 'HASH') {
 3385:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3386:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3387:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3388:                 delete($usecheck{'all'});
 3389:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3390:                     my $access = $1;
 3391:                     $displaydiv{$access} = 'inline';
 3392:                 } elsif ($current->{access} eq 'none') {
 3393:                     $displaydiv{'priv'} = 'none';
 3394:                 }
 3395:             }
 3396:         }
 3397:     }
 3398:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3399:               '<p>'.$lt{'whi'}.'</p>';
 3400:     foreach my $access (@{$accesstypes}) {
 3401:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3402:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3403:                    $lt{$access}.'</label>';
 3404:         if ($access eq 'status') {
 3405:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3406:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3407:                                                                  $othertitle,$usertypes,$types).
 3408:                        '</div>';
 3409:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3410:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3411:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3412:                        '</div>';
 3413:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3414:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3415:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3416:                        '</div>';
 3417:         }
 3418:         $output .= '</p>';
 3419:     }
 3420:     $output .= '</fieldset>';
 3421:     return $output;
 3422: }
 3423: 
 3424: sub radiobutton_prefs {
 3425:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3426:         $additional,$align) = @_;
 3427:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3428:                    (ref($choices) eq 'HASH'));
 3429: 
 3430:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3431: 
 3432:     foreach my $item (@{$toggles}) {
 3433:         if ($defaultchecked->{$item} eq 'on') {
 3434:             $checkedon{$item} = ' checked="checked" ';
 3435:             $checkedoff{$item} = ' ';
 3436:         } elsif ($defaultchecked->{$item} eq 'off') {
 3437:             $checkedoff{$item} = ' checked="checked" ';
 3438:             $checkedon{$item} = ' ';
 3439:         }
 3440:     }
 3441:     if (ref($settings) eq 'HASH') {
 3442:         foreach my $item (@{$toggles}) {
 3443:             if ($settings->{$item} eq '1') {
 3444:                 $checkedon{$item} =  ' checked="checked" ';
 3445:                 $checkedoff{$item} = ' ';
 3446:             } elsif ($settings->{$item} eq '0') {
 3447:                 $checkedoff{$item} =  ' checked="checked" ';
 3448:                 $checkedon{$item} = ' ';
 3449:             }
 3450:         }
 3451:     }
 3452:     if ($onclick) {
 3453:         $onclick = ' onclick="'.$onclick.'"';
 3454:     }
 3455:     foreach my $item (@{$toggles}) {
 3456:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3457:         $datatable .=
 3458:             '<tr'.$css_class.'><td valign="top">'.
 3459:             '<span class="LC_nobreak">'.$choices->{$item}.
 3460:             '</span></td>';
 3461:         if ($align eq 'left') {
 3462:             $datatable .= '<td class="LC_left_item">';
 3463:         } else {
 3464:             $datatable .= '<td class="LC_right_item">';
 3465:         }
 3466:         $datatable .= 
 3467:             '<span class="LC_nobreak">'.
 3468:             '<label><input type="radio" name="'.
 3469:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3470:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3471:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3472:             '</span>'.$additional.
 3473:             '</td>'.
 3474:             '</tr>';
 3475:         $itemcount ++;
 3476:     }
 3477:     return ($datatable,$itemcount);
 3478: }
 3479: 
 3480: sub print_ltitools {
 3481:     my ($dom,$settings,$rowtotal) = @_;
 3482:     my $rownum = 0;
 3483:     my $css_class;
 3484:     my $itemcount = 1;
 3485:     my $maxnum = 0;
 3486:     my %ordered;
 3487:     if (ref($settings) eq 'HASH') {
 3488:         foreach my $item (keys(%{$settings})) {
 3489:             if (ref($settings->{$item}) eq 'HASH') {
 3490:                 my $num = $settings->{$item}{'order'};
 3491:                 $ordered{$num} = $item;
 3492:             }
 3493:         }
 3494:     }
 3495:     my $confname = $dom.'-domainconfig';
 3496:     my $switchserver = &check_switchserver($dom,$confname);
 3497:     my $maxnum = scalar(keys(%ordered));
 3498:     my $datatable = &ltitools_javascript($settings);
 3499:     my %lt = &ltitools_names();
 3500:     my @courseroles = ('cc','in','ta','ep','st');
 3501:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 3502:     my @fields = ('fullname','firstname','lastname','email','user','roles');
 3503:     if (keys(%ordered)) {
 3504:         my @items = sort { $a <=> $b } keys(%ordered);
 3505:         for (my $i=0; $i<@items; $i++) {
 3506:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3507:             my $item = $ordered{$items[$i]};
 3508:             my ($title,$key,$secret,$url,$imgsrc,$version);
 3509:             if (ref($settings->{$item}) eq 'HASH') {
 3510:                 $title = $settings->{$item}->{'title'};
 3511:                 $url = $settings->{$item}->{'url'};
 3512:                 $key = $settings->{$item}->{'key'};
 3513:                 $secret = $settings->{$item}->{'secret'};
 3514:                 my $image = $settings->{$item}->{'image'};
 3515:                 if ($image ne '') {
 3516:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 3517:                 }
 3518:             }
 3519:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_".$item."'".');"';
 3520:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3521:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 3522:             for (my $k=0; $k<=$maxnum; $k++) {
 3523:                 my $vpos = $k+1;
 3524:                 my $selstr;
 3525:                 if ($k == $i) {
 3526:                     $selstr = ' selected="selected" ';
 3527:                 }
 3528:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3529:             }
 3530:             $datatable .= '</select>'.('&nbsp;'x2).
 3531:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 3532:                 &mt('Delete?').'</label></span></td>'.
 3533:                 '<td colspan="2">'.
 3534:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3535:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 3536:                 ('&nbsp;'x2).
 3537:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 3538:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 3539:                 ('&nbsp;'x2).
 3540:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 3541:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3542:                 '<br /><br />'.
 3543:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_url_'.$i.'"'.
 3544:                 ' value="'.$url.'" /></span>'.
 3545:                 ('&nbsp;'x2).
 3546:                 '<span class="LC_nobreak">'.$lt{'key'}.
 3547:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 3548:                 ('&nbsp;'x2).
 3549:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 3550:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 3551:                 '<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>'.
 3552:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 3553:                 '</fieldset>'.
 3554:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3555:                 '<span class="LC_nobreak">'.&mt('Display target:');
 3556:             my %currdisp;
 3557:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 3558:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 3559:                     $currdisp{'window'} = ' checked="checked"';
 3560:                 } else {
 3561:                     $currdisp{'iframe'} = ' checked="checked"';
 3562:                 }
 3563:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 3564:                     $currdisp{'width'} = $1;
 3565:                 }
 3566:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 3567:                      $currdisp{'height'} = $1;
 3568:                 }
 3569:             } else {
 3570:                 $currdisp{'iframe'} = ' checked="checked"';
 3571:             }
 3572:             foreach my $disp ('iframe','window') {
 3573:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 3574:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 3575:             }
 3576:             $datatable .= ('&nbsp;'x4);
 3577:             foreach my $dimen ('width','height') {
 3578:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3579:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 3580:                               ('&nbsp;'x2);
 3581:             }
 3582:             $datatable .= '<br />';
 3583:             foreach my $extra ('passback','roster') {
 3584:                 my $checkedon = '';
 3585:                 my $checkedoff = ' checked="checked"';
 3586:                 if ($settings->{$item}->{$extra}) {
 3587:                     $checkedon = $checkedoff;
 3588:                     $checkedoff = '';
 3589:                 }
 3590:                 $datatable .= $lt{$extra}.'&nbsp;'.
 3591:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.' />'.
 3592:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 3593:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.' />'.
 3594:                               &mt('No').'</label>'.('&nbsp;'x4);
 3595:             }
 3596:             $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 3597:             if ($imgsrc) {
 3598:                 $datatable .= $imgsrc.
 3599:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 3600:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 3601:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 3602:             } else {
 3603:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3604:             }
 3605:             if ($switchserver) {
 3606:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3607:             } else {
 3608:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 3609:             }
 3610:             $datatable .= '</span></fieldset>';
 3611:             my (%checkedfields,%rolemaps);
 3612:             if (ref($settings->{$item}) eq 'HASH') {
 3613:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 3614:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 3615:                 }
 3616:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 3617:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 3618:                     $checkedfields{'roles'} = 1;
 3619:                 }
 3620:             }
 3621:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3622:                           '<span class="LC_nobreak">';
 3623:             foreach my $field (@fields) {
 3624:                 my $checked;
 3625:                 if ($checkedfields{$field}) {
 3626:                     $checked = ' checked="checked"';
 3627:                 }
 3628:                 $datatable .= '<label>'.
 3629:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$checked.' />'.
 3630:                               $lt{$field}.'</label>'.('&nbsp;' x2);
 3631:             }
 3632:             $datatable .= '</span></fieldset>'.
 3633:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 3634:             foreach my $role (@courseroles) {
 3635:                 my ($selected,$selectnone);
 3636:                 if (!$rolemaps{$role}) {
 3637:                     $selectnone = ' selected="selected"';
 3638:                 }
 3639:                 $datatable .= '<td align="center">'. 
 3640:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 3641:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 3642:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 3643:                 foreach my $ltirole (@ltiroles) {
 3644:                     unless ($selectnone) {
 3645:                         if ($rolemaps{$role} eq $ltirole) {
 3646:                             $selected = ' selected="selected"';
 3647:                         } else {
 3648:                             $selected = '';
 3649:                         }
 3650:                     }
 3651:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 3652:                 }
 3653:                 $datatable .= '</select></td>';
 3654:             }
 3655:             $datatable .= '</tr></table></fieldset>';
 3656:             my %courseconfig;
 3657:             if (ref($settings->{$item}) eq 'HASH') {
 3658:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 3659:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 3660:                 }
 3661:             }
 3662:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 3663:             foreach my $item ('label','title','target') {
 3664:                 my $checked;
 3665:                 if ($courseconfig{$item}) {
 3666:                     $checked = ' checked="checked"';
 3667:                 }
 3668:                 $datatable .= '<label>'.
 3669:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 3670:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 3671:             }
 3672:             $datatable .= '</span></fieldset>'.
 3673:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 3674:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 3675:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 3676:                 my %custom = %{$settings->{$item}->{'custom'}};
 3677:                 if (keys(%custom) > 0) {
 3678:                     foreach my $key (sort(keys(%custom))) {
 3679:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 3680:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 3681:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 3682:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 3683:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 3684:                     }
 3685:                 }
 3686:             }
 3687:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 3688:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 3689:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 3690:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 3691:             $datatable .= '</table></fieldset></td></tr>'."\n";
 3692:             $itemcount ++;
 3693:         }
 3694:     }
 3695:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3696:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_add_pos'".');"';
 3697:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 3698:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 3699:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 3700:     for (my $k=0; $k<$maxnum+1; $k++) {
 3701:         my $vpos = $k+1;
 3702:         my $selstr;
 3703:         if ($k == $maxnum) {
 3704:             $selstr = ' selected="selected" ';
 3705:         }
 3706:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3707:     }
 3708:     $datatable .= '</select>&nbsp;'."\n".
 3709:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</td>'."\n".
 3710:                   '<td colspan="2">'.
 3711:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3712:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_add_title" value="" /></span> '."\n".
 3713:                   ('&nbsp;'x2).
 3714:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 3715:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 3716:                   ('&nbsp;'x2).
 3717:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 3718:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3719:                   '<br />'.
 3720:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_add_url" value="" /></span> '."\n".
 3721:                   ('&nbsp;'x2).
 3722:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 3723:                   ('&nbsp;'x2).
 3724:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 3725:                   '<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".
 3726:                   '</fieldset>'.
 3727:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3728:                   '<span class="LC_nobreak">'.&mt('Display target:');
 3729:     my %defaultdisp;
 3730:     $defaultdisp{'iframe'} = ' checked="checked"';
 3731:     foreach my $disp ('iframe','window') {
 3732:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 3733:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 3734:     }
 3735:     $datatable .= ('&nbsp;'x4);
 3736:     foreach my $dimen ('width','height') {
 3737:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3738:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 3739:                       ('&nbsp;'x2);
 3740:     }
 3741:     $datatable .= '<br />';
 3742:     foreach my $extra ('passback','roster') {
 3743:         $datatable .= $lt{$extra}.'&nbsp;'.
 3744:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="1" />'.
 3745:                       &mt('Yes').'</label>'.('&nbsp;'x2).
 3746:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="0" checked="checked" />'.
 3747:                       &mt('No').'</label>'.('&nbsp;'x4);
 3748:     }
 3749:     $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 3750:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3751:     if ($switchserver) {
 3752:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3753:     } else {
 3754:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 3755:     }
 3756:     $datatable .= '</span></fieldset>'.
 3757:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3758:                   '<span class="LC_nobreak">';
 3759:     foreach my $field (@fields) {
 3760:         $datatable .= '<label>'.
 3761:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'" />'.
 3762:                       $lt{$field}.'</label>'.('&nbsp;' x2);
 3763:     }
 3764:     $datatable .= '</span></fieldset>'.
 3765:                   '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 3766:     foreach my $role (@courseroles) {
 3767:         my ($checked,$checkednone);
 3768:         $datatable .= '<td align="center">'.
 3769:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 3770:                       '<select name="ltitools_add_roles_'.$role.'">'.
 3771:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 3772:         foreach my $ltirole (@ltiroles) {
 3773:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 3774:         }
 3775:         $datatable .= '</select></td>';
 3776:     }
 3777:     $datatable .= '</tr></table></fieldset>'.
 3778:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 3779:     foreach my $item ('label','title','target') {
 3780:          $datatable .= '<label>'.
 3781:                        '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 3782:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 3783:     }
 3784:     $datatable .= '</span></fieldset>'.
 3785:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 3786:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 3787:                   '<tr><td><span class="LC_nobreak">'.
 3788:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 3789:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 3790:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 3791:                   '</table></fieldset></td></tr>'."\n".
 3792:                   '</td>'."\n".
 3793:                   '</tr>'."\n";
 3794:     $itemcount ++;
 3795:     return $datatable;
 3796: }
 3797: 
 3798: sub ltitools_names {
 3799:     my %lt = &Apache::lonlocal::texthash(
 3800:                                           'title'     => 'Title',
 3801:                                           'version'   => 'Version',
 3802:                                           'msgtype'   => 'Message Type',
 3803:                                           'url'       => 'URL',
 3804:                                           'key'       => 'Key',
 3805:                                           'secret'    => 'Secret',
 3806:                                           'icon'      => 'Icon',   
 3807:                                           'user'      => 'Username:domain',
 3808:                                           'fullname'  => 'Full Name',
 3809:                                           'firstname' => 'First Name',
 3810:                                           'lastname'  => 'Last Name',
 3811:                                           'email'     => 'E-mail',
 3812:                                           'roles'     => 'Role',
 3813:                                           'window'    => 'Window/Tab',
 3814:                                           'iframe'    => 'iFrame',
 3815:                                           'height'    => 'Height',
 3816:                                           'width'     => 'Width',
 3817:                                           'passback'  => 'Tool can return grades:',
 3818:                                           'roster'    => 'Tool can retrieve roster:',
 3819:                                           'crstarget' => 'Display target',
 3820:                                           'crslabel'  => 'Course label',
 3821:                                           'crstitle'  => 'Course title', 
 3822:                                         );
 3823:     return %lt;
 3824: }
 3825: 
 3826: sub print_coursedefaults {
 3827:     my ($position,$dom,$settings,$rowtotal) = @_;
 3828:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 3829:     my $itemcount = 1;
 3830:     my %choices =  &Apache::lonlocal::texthash (
 3831:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 3832:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 3833:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 3834:         coursecredits        => 'Credits can be specified for courses',
 3835:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 3836:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 3837:         postsubmit           => 'Disable submit button/keypress following student submission',
 3838:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 3839:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 3840:     );
 3841:     my %staticdefaults = (
 3842:                            anonsurvey_threshold => 10,
 3843:                            uploadquota          => 500,
 3844:                            postsubmit           => 60,
 3845:                            mysqltables          => 172800,
 3846:                          );
 3847:     if ($position eq 'top') {
 3848:         %defaultchecked = (
 3849:                             'canuse_pdfforms' => 'off',
 3850:                             'uselcmath'       => 'on',
 3851:                             'usejsme'         => 'on',
 3852:                             'canclone'        => 'none', 
 3853:                           );
 3854:         @toggles = ('canuse_pdfforms','uselcmath','usejsme');
 3855:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3856:                                                      \%choices,$itemcount);
 3857:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3858:         $datatable .=
 3859:             '<tr'.$css_class.'><td valign="top">'.
 3860:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 3861:             '</span></td><td class="LC_left_item">';
 3862:         my $currcanclone = 'none';
 3863:         my $onclick;
 3864:         my @cloneoptions = ('none','domain');
 3865:         my %clonetitles = (
 3866:                              none     => 'No additional course requesters',
 3867:                              domain   => "Any course requester in course's domain",
 3868:                              instcode => 'Course requests for official courses ...',
 3869:                           );
 3870:         my (%codedefaults,@code_order,@posscodes);
 3871:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 3872:                                                     \@code_order) eq 'ok') {
 3873:             if (@code_order > 0) {
 3874:                 push(@cloneoptions,'instcode');
 3875:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 3876:             }
 3877:         }
 3878:         if (ref($settings) eq 'HASH') {
 3879:             if ($settings->{'canclone'}) {
 3880:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 3881:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 3882:                         if (@code_order > 0) {
 3883:                             $currcanclone = 'instcode';
 3884:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 3885:                         }
 3886:                     }
 3887:                 } elsif ($settings->{'canclone'} eq 'domain') {
 3888:                     $currcanclone = $settings->{'canclone'};
 3889:                 }
 3890:             }
 3891:         } 
 3892:         foreach my $option (@cloneoptions) {
 3893:             my ($checked,$additional);
 3894:             if ($currcanclone eq $option) {
 3895:                 $checked = ' checked="checked"';
 3896:             }
 3897:             if ($option eq 'instcode') {
 3898:                 if (@code_order) {
 3899:                     my $show = 'none';
 3900:                     if ($checked) {
 3901:                         $show = 'block';
 3902:                     }
 3903:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 3904:                                   &mt('Institutional codes for new and cloned course have identical:').
 3905:                                   '<br />';
 3906:                     foreach my $item (@code_order) {
 3907:                         my $codechk;
 3908:                         if ($checked) {
 3909:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 3910:                                 $codechk = ' checked="checked"';
 3911:                             }
 3912:                         }
 3913:                         $additional .= '<label>'.
 3914:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 3915:                                        $item.'</label>';
 3916:                     }
 3917:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 3918:                 }
 3919:             }
 3920:             $datatable .=
 3921:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 3922:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 3923:                 '</label>&nbsp;'.$additional.'</span><br />';
 3924:         }
 3925:         $datatable .= '</td>'.
 3926:                       '</tr>';
 3927:         $itemcount ++;
 3928:     } else {
 3929:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3930:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 3931:         my $currusecredits = 0;
 3932:         my $postsubmitclient = 1;
 3933:         my @types = ('official','unofficial','community','textbook','placement');
 3934:         if (ref($settings) eq 'HASH') {
 3935:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 3936:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 3937:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 3938:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 3939:                 }
 3940:             }
 3941:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 3942:                 foreach my $type (@types) {
 3943:                     next if ($type eq 'community');
 3944:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 3945:                     if ($defcredits{$type} ne '') {
 3946:                         $currusecredits = 1;
 3947:                     }
 3948:                 }
 3949:             }
 3950:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 3951:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 3952:                     $postsubmitclient = 0;
 3953:                     foreach my $type (@types) {
 3954:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3955:                     }
 3956:                 } else {
 3957:                     foreach my $type (@types) {
 3958:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 3959:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 3960:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type}; 
 3961:                             } else {
 3962:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3963:                             }
 3964:                         } else {
 3965:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3966:                         }
 3967:                     }
 3968:                 }
 3969:             } else {
 3970:                 foreach my $type (@types) {
 3971:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3972:                 }
 3973:             }
 3974:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 3975:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 3976:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 3977:                 }
 3978:             } else {
 3979:                 foreach my $type (@types) {
 3980:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 3981:                 }
 3982:             }
 3983:         } else {
 3984:             foreach my $type (@types) {
 3985:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3986:             }
 3987:         }
 3988:         if (!$currdefresponder) {
 3989:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 3990:         } elsif ($currdefresponder < 1) {
 3991:             $currdefresponder = 1;
 3992:         }
 3993:         foreach my $type (@types) {
 3994:             if ($curruploadquota{$type} eq '') {
 3995:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 3996:             }
 3997:         }
 3998:         $datatable .=
 3999:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4000:                 $choices{'anonsurvey_threshold'}.
 4001:                 '</span></td>'.
 4002:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 4003:                 '<input type="text" name="anonsurvey_threshold"'.
 4004:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 4005:                 '</td></tr>'."\n";
 4006:         $itemcount ++;
 4007:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4008:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4009:                       $choices{'uploadquota'}.
 4010:                       '</span></td>'.
 4011:                       '<td align="right" class="LC_right_item">'.
 4012:                       '<table><tr>';
 4013:         foreach my $type (@types) {
 4014:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4015:                            '<input type="text" name="uploadquota_'.$type.'"'.
 4016:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 4017:         }
 4018:         $datatable .= '</tr></table></td></tr>'."\n";
 4019:         $itemcount ++;
 4020:         my $onclick = "toggleDisplay(this.form,'credits');";
 4021:         my $display = 'none';
 4022:         if ($currusecredits) {
 4023:             $display = 'block';
 4024:         }
 4025:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 4026:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 4027:         foreach my $type (@types) {
 4028:             next if ($type eq 'community');
 4029:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4030:                            '<input type="text" name="'.$type.'_credits"'.
 4031:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 4032:         }
 4033:         $additional .= '</tr></table></div>'."\n";
 4034:         %defaultchecked = ('coursecredits' => 'off');
 4035:         @toggles = ('coursecredits');
 4036:         my $current = {
 4037:                         'coursecredits' => $currusecredits,
 4038:                       };
 4039:         (my $table,$itemcount) =
 4040:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4041:                                \%choices,$itemcount,$onclick,$additional,'left');
 4042:         $datatable .= $table;
 4043:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 4044:         my $display = 'none';
 4045:         if ($postsubmitclient) {
 4046:             $display = 'block';
 4047:         }
 4048:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 4049:                       &mt('Number of seconds submit is disabled').'<br />'.
 4050:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 4051:                       '<table><tr>';
 4052:         foreach my $type (@types) {
 4053:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4054:                            '<input type="text" name="'.$type.'_timeout" value="'.
 4055:                            $deftimeout{$type}.'" size="5" /></td>';
 4056:         }
 4057:         $additional .= '</tr></table></div>'."\n";
 4058:         %defaultchecked = ('postsubmit' => 'on');
 4059:         @toggles = ('postsubmit');
 4060:         $current = {
 4061:                        'postsubmit' => $postsubmitclient,
 4062:                    };
 4063:         ($table,$itemcount) =
 4064:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4065:                                \%choices,$itemcount,$onclick,$additional,'left');
 4066:         $datatable .= $table;
 4067:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4068:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4069:                       $choices{'mysqltables'}.
 4070:                       '</span></td>'.
 4071:                       '<td align="right" class="LC_right_item">'.
 4072:                       '<table><tr>';
 4073:         foreach my $type (@types) {
 4074:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4075:                            '<input type="text" name="mysqltables_'.$type.'"'.
 4076:                            ' value="'.$currmysql{$type}.'" size="5" /></td>';
 4077:         }
 4078:         $datatable .= '</tr></table></td></tr>'."\n";
 4079:         $itemcount ++;
 4080: 
 4081:     }
 4082:     $$rowtotal += $itemcount;
 4083:     return $datatable;
 4084: }
 4085: 
 4086: sub print_selfenrollment {
 4087:     my ($position,$dom,$settings,$rowtotal) = @_;
 4088:     my ($css_class,$datatable);
 4089:     my $itemcount = 1;
 4090:     my @types = ('official','unofficial','community','textbook','placement');
 4091:     if (($position eq 'top') || ($position eq 'middle')) {
 4092:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 4093:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 4094:         my @rows;
 4095:         my $key;
 4096:         if ($position eq 'top') {
 4097:             $key = 'admin'; 
 4098:             if (ref($rowsref) eq 'ARRAY') {
 4099:                 @rows = @{$rowsref};
 4100:             }
 4101:         } elsif ($position eq 'middle') {
 4102:             $key = 'default';
 4103:             @rows = ('types','registered','approval','limit');
 4104:         }
 4105:         foreach my $row (@rows) {
 4106:             if (defined($titlesref->{$row})) {
 4107:                 $itemcount ++;
 4108:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4109:                 $datatable .= '<tr'.$css_class.'>'.
 4110:                               '<td>'.$titlesref->{$row}.'</td>'.
 4111:                               '<td class="LC_left_item">'.
 4112:                               '<table><tr>';
 4113:                 my (%current,%currentcap);
 4114:                 if (ref($settings) eq 'HASH') {
 4115:                     if (ref($settings->{$key}) eq 'HASH') {
 4116:                         foreach my $type (@types) {
 4117:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4118:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 4119:                             }
 4120:                             if (($row eq 'limit') && ($key eq 'default')) {
 4121:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4122:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 4123:                                 }
 4124:                             }
 4125:                         }
 4126:                     }
 4127:                 }
 4128:                 my %roles = (
 4129:                              '0' => &Apache::lonnet::plaintext('dc'),
 4130:                             ); 
 4131:             
 4132:                 foreach my $type (@types) {
 4133:                     unless (($row eq 'registered') && ($key eq 'default')) {
 4134:                         $datatable .= '<th>'.&mt($type).'</th>';
 4135:                     }
 4136:                 }
 4137:                 unless (($row eq 'registered') && ($key eq 'default')) {
 4138:                     $datatable .= '</tr><tr>';
 4139:                 }
 4140:                 foreach my $type (@types) {
 4141:                     if ($type eq 'community') {
 4142:                         $roles{'1'} = &mt('Community personnel');
 4143:                     } else {
 4144:                         $roles{'1'} = &mt('Course personnel');
 4145:                     }
 4146:                     $datatable .= '<td style="vertical-align: top">';
 4147:                     if ($position eq 'top') {
 4148:                         my %checked;
 4149:                         if ($current{$type} eq '0') {
 4150:                             $checked{'0'} = ' checked="checked"';
 4151:                         } else {
 4152:                             $checked{'1'} = ' checked="checked"';
 4153:                         }
 4154:                         foreach my $role ('1','0') {
 4155:                             $datatable .= '<span class="LC_nobreak"><label>'.
 4156:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 4157:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 4158:                                           $roles{$role}.'</label></span> ';
 4159:                         }
 4160:                     } else {
 4161:                         if ($row eq 'types') {
 4162:                             my %checked;
 4163:                             if ($current{$type} =~ /^(all|dom)$/) {
 4164:                                 $checked{$1} = ' checked="checked"';
 4165:                             } else {
 4166:                                 $checked{''} = ' checked="checked"';
 4167:                             }
 4168:                             foreach my $val ('','dom','all') {
 4169:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4170:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4171:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4172:                             }
 4173:                         } elsif ($row eq 'registered') {
 4174:                             my %checked;
 4175:                             if ($current{$type} eq '1') {
 4176:                                 $checked{'1'} = ' checked="checked"';
 4177:                             } else {
 4178:                                 $checked{'0'} = ' checked="checked"';
 4179:                             }
 4180:                             foreach my $val ('0','1') {
 4181:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4182:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4183:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4184:                             }
 4185:                         } elsif ($row eq 'approval') {
 4186:                             my %checked;
 4187:                             if ($current{$type} =~ /^([12])$/) {
 4188:                                 $checked{$1} = ' checked="checked"';
 4189:                             } else {
 4190:                                 $checked{'0'} = ' checked="checked"';
 4191:                             }
 4192:                             for my $val (0..2) {
 4193:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4194:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4195:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4196:                             }
 4197:                         } elsif ($row eq 'limit') {
 4198:                             my %checked;
 4199:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 4200:                                 $checked{$1} = ' checked="checked"';
 4201:                             } else {
 4202:                                 $checked{'none'} = ' checked="checked"';
 4203:                             }
 4204:                             my $cap;
 4205:                             if ($currentcap{$type} =~ /^\d+$/) {
 4206:                                 $cap = $currentcap{$type};
 4207:                             }
 4208:                             foreach my $val ('none','allstudents','selfenrolled') {
 4209:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4210:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4211:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4212:                             }
 4213:                             $datatable .= '<br />'.
 4214:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 4215:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 4216:                                           '</span>'; 
 4217:                         }
 4218:                     }
 4219:                     $datatable .= '</td>';
 4220:                 }
 4221:                 $datatable .= '</tr>';
 4222:             }
 4223:             $datatable .= '</table></td></tr>';
 4224:         }
 4225:     } elsif ($position eq 'bottom') {
 4226:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 4227:     }
 4228:     $$rowtotal += $itemcount;
 4229:     return $datatable;
 4230: }
 4231: 
 4232: sub print_validation_rows {
 4233:     my ($caller,$dom,$settings,$rowtotal) = @_;
 4234:     my ($itemsref,$namesref,$fieldsref);
 4235:     if ($caller eq 'selfenroll') { 
 4236:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 4237:     } elsif ($caller eq 'requestcourses') {
 4238:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 4239:     }
 4240:     my %currvalidation;
 4241:     if (ref($settings) eq 'HASH') {
 4242:         if (ref($settings->{'validation'}) eq 'HASH') {
 4243:             %currvalidation = %{$settings->{'validation'}};
 4244:         }
 4245:     }
 4246:     my $datatable;
 4247:     my $itemcount = 0;
 4248:     foreach my $item (@{$itemsref}) {
 4249:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4250:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4251:                       $namesref->{$item}.
 4252:                       '</span></td>'.
 4253:                       '<td class="LC_left_item">';
 4254:         if (($item eq 'url') || ($item eq 'button')) {
 4255:             $datatable .= '<span class="LC_nobreak">'.
 4256:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 4257:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 4258:         } elsif ($item eq 'fields') {
 4259:             my @currfields;
 4260:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 4261:                 @currfields = @{$currvalidation{$item}};
 4262:             }
 4263:             foreach my $field (@{$fieldsref}) {
 4264:                 my $check = '';
 4265:                 if (grep(/^\Q$field\E$/,@currfields)) {
 4266:                     $check = ' checked="checked"';
 4267:                 }
 4268:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4269:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 4270:                               ' value="'.$field.'"'.$check.' />'.$field.
 4271:                               '</label></span> ';
 4272:             }
 4273:         } elsif ($item eq 'markup') {
 4274:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 4275:                            $currvalidation{$item}.
 4276:                               '</textarea>';
 4277:         }
 4278:         $datatable .= '</td></tr>'."\n";
 4279:         if (ref($rowtotal)) {
 4280:             $itemcount ++;
 4281:         }
 4282:     }
 4283:     if ($caller eq 'requestcourses') {
 4284:         my %currhash;
 4285:         if (ref($settings) eq 'HASH') {
 4286:             if (ref($settings->{'validation'}) eq 'HASH') {
 4287:                 if ($settings->{'validation'}{'dc'} ne '') {
 4288:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 4289:                 }
 4290:             }
 4291:         }
 4292:         my $numinrow = 2;
 4293:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4294:                                                        'validationdc',%currhash);
 4295:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4296:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 4297:         if ($numdc > 1) {
 4298:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 4299:         } else {
 4300:             $datatable .=  &mt('Course creation processed as: ');
 4301:         }
 4302:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 4303:         $itemcount ++;
 4304:     }
 4305:     if (ref($rowtotal)) {
 4306:         $$rowtotal += $itemcount;
 4307:     }
 4308:     return $datatable;
 4309: }
 4310: 
 4311: sub print_usersessions {
 4312:     my ($position,$dom,$settings,$rowtotal) = @_;
 4313:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 4314:     my (%by_ip,%by_location,@intdoms,@instdoms);
 4315:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4316: 
 4317:     my @alldoms = &Apache::lonnet::all_domains();
 4318:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4319:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4320:     my %altids = &id_for_thisdom(%servers);
 4321:     if ($position eq 'top') {
 4322:         if (keys(%serverhomes) > 1) {
 4323:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 4324:             my $curroffloadnow;
 4325:             if (ref($settings) eq 'HASH') {
 4326:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 4327:                     $curroffloadnow = $settings->{'offloadnow'};
 4328:                 }
 4329:             }
 4330:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 4331:         } else {
 4332:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4333:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 4334:                           '</td></tr>';
 4335:         }
 4336:     } else {
 4337:         my %titles = &usersession_titles();
 4338:         my ($prefix,@types);
 4339:         if ($position eq 'bottom') {
 4340:             $prefix = 'remote';
 4341:             @types = ('version','excludedomain','includedomain');
 4342:         } else {
 4343:             $prefix = 'hosted';
 4344:             @types = ('excludedomain','includedomain');
 4345:         }
 4346:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4347:     }
 4348:     $$rowtotal += $itemcount;
 4349:     return $datatable;
 4350: }
 4351: 
 4352: sub rules_by_location {
 4353:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 4354:     my ($datatable,$itemcount,$css_class);
 4355:     if (keys(%{$by_location}) == 0) {
 4356:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4357:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 4358:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 4359:                      '</td></tr>';
 4360:         $itemcount = 1;
 4361:     } else {
 4362:         $itemcount = 0;
 4363:         my $numinrow = 5;
 4364:         my (%current,%checkedon,%checkedoff);
 4365:         my @locations = sort(keys(%{$by_location}));
 4366:         foreach my $type (@{$types}) {
 4367:             $checkedon{$type} = '';
 4368:             $checkedoff{$type} = ' checked="checked"';
 4369:         }
 4370:         if (ref($settings) eq 'HASH') {
 4371:             if (ref($settings->{$prefix}) eq 'HASH') {
 4372:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 4373:                     $current{$key} = $settings->{$prefix}{$key};
 4374:                     if ($key eq 'version') {
 4375:                         if ($current{$key} ne '') {
 4376:                             $checkedon{$key} = ' checked="checked"';
 4377:                             $checkedoff{$key} = '';
 4378:                         }
 4379:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 4380:                         $checkedon{$key} = ' checked="checked"';
 4381:                         $checkedoff{$key} = '';
 4382:                     }
 4383:                 }
 4384:             }
 4385:         }
 4386:         foreach my $type (@{$types}) {
 4387:             next if ($type ne 'version' && !@locations);
 4388:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4389:             $datatable .= '<tr'.$css_class.'>
 4390:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 4391:                            <span class="LC_nobreak">&nbsp;
 4392:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 4393:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 4394:             if ($type eq 'version') {
 4395:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 4396:                 my $selector = '<select name="'.$prefix.'_version">';
 4397:                 foreach my $version (@lcversions) {
 4398:                     my $selected = '';
 4399:                     if ($current{'version'} eq $version) {
 4400:                         $selected = ' selected="selected"';
 4401:                     }
 4402:                     $selector .= ' <option value="'.$version.'"'.
 4403:                                  $selected.'>'.$version.'</option>';
 4404:                 }
 4405:                 $selector .= '</select> ';
 4406:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 4407:             } else {
 4408:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 4409:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 4410:                              ' />'.('&nbsp;'x2).
 4411:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 4412:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 4413:                              "\n".
 4414:                              '</div><div><table>';
 4415:                 my $rem;
 4416:                 for (my $i=0; $i<@locations; $i++) {
 4417:                     my ($showloc,$value,$checkedtype);
 4418:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 4419:                         my $ip = $by_location->{$locations[$i]}->[0];
 4420:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4421:                             $value = join(':',@{$by_ip->{$ip}});
 4422:                             $showloc = join(', ',@{$by_ip->{$ip}});
 4423:                             if (ref($current{$type}) eq 'ARRAY') {
 4424:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 4425:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 4426:                                         $checkedtype = ' checked="checked"';
 4427:                                         last;
 4428:                                     }
 4429:                                 }
 4430:                             }
 4431:                         }
 4432:                     }
 4433:                     $rem = $i%($numinrow);
 4434:                     if ($rem == 0) {
 4435:                         if ($i > 0) {
 4436:                             $datatable .= '</tr>';
 4437:                         }
 4438:                         $datatable .= '<tr>';
 4439:                     }
 4440:                     $datatable .= '<td class="LC_left_item">'.
 4441:                                   '<span class="LC_nobreak"><label>'.
 4442:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 4443:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 4444:                                   '</label></span></td>';
 4445:                 }
 4446:                 $rem = @locations%($numinrow);
 4447:                 my $colsleft = $numinrow - $rem;
 4448:                 if ($colsleft > 1 ) {
 4449:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4450:                                   '&nbsp;</td>';
 4451:                 } elsif ($colsleft == 1) {
 4452:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4453:                 }
 4454:                 $datatable .= '</tr></table>';
 4455:             }
 4456:             $datatable .= '</td></tr>';
 4457:             $itemcount ++;
 4458:         }
 4459:     }
 4460:     return ($datatable,$itemcount);
 4461: }
 4462: 
 4463: sub print_ssl {
 4464:     my ($position,$dom,$settings,$rowtotal) = @_;
 4465:     my ($css_class,$datatable);
 4466:     my $itemcount = 1;
 4467:     if ($position eq 'top') {
 4468:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 4469:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 4470:         my $same_institution;
 4471:         if ($intdom ne '') {
 4472:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 4473:             if (ref($internet_names) eq 'ARRAY') {
 4474:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 4475:                     $same_institution = 1;
 4476:                 }
 4477:             }
 4478:         }
 4479:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4480:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 4481:         if ($same_institution) {
 4482:             my %domservers = &Apache::lonnet::get_servers($dom);
 4483:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 4484:         } else {
 4485:             $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.");
 4486:         }
 4487:         $datatable .= '</td></tr>';
 4488:         $itemcount ++;
 4489:     } else {
 4490:         my %titles = &ssl_titles();
 4491:         my (%by_ip,%by_location,@intdoms,@instdoms);
 4492:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4493:         my @alldoms = &Apache::lonnet::all_domains();
 4494:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4495:         my @domservers = &Apache::lonnet::get_servers($dom);
 4496:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4497:         my %altids = &id_for_thisdom(%servers);
 4498:         if ($position eq 'middle') {
 4499:             foreach my $type ('dom','intdom','other') {
 4500:                 my %checked;
 4501:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4502:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 4503:                               '<td class="LC_right_item">';
 4504:                 my $skip; 
 4505:                 if ($type eq 'dom') {
 4506:                     unless (keys(%servers) > 1) {
 4507:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 4508:                         $skip = 1;
 4509:                     }
 4510:                 }
 4511:                 if ($type eq 'intdom') {
 4512:                     unless (@instdoms > 1) {
 4513:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 4514:                         $skip = 1;
 4515:                     } 
 4516:                 } elsif ($type eq 'other') {
 4517:                     if (keys(%by_location) == 0) {
 4518:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 4519:                         $skip = 1;
 4520:                     }
 4521:                 }
 4522:                 unless ($skip) {
 4523:                     $checked{'yes'} = ' checked="checked"'; 
 4524:                     if (ref($settings) eq 'HASH') {
 4525:                         if (ref($settings->{'connect'}) eq 'HASH') {
 4526:                             if ($settings->{'connect'}->{$type} =~ /^(no|req)$/) {
 4527:                                 $checked{$1} = $checked{'yes'};
 4528:                                 delete($checked{'yes'}); 
 4529:                             }
 4530:                         }
 4531:                     }
 4532:                     foreach my $option ('no','yes','req') {
 4533:                         $datatable .= '<span class="LC_nobreak"><label>'.
 4534:                                       '<input type="radio" name="connect_'.$type.'" '.
 4535:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 4536:                                       '</label></span>'.('&nbsp;'x2);
 4537:                     }
 4538:                 }
 4539:                 $datatable .= '</td></tr>';
 4540:                 $itemcount ++; 
 4541:             }
 4542:         } else {
 4543:             my $prefix = 'replication';
 4544:             my @types = ('certreq','nocertreq');
 4545:             if (keys(%by_location) == 0) {
 4546:                 $datatable .= '<tr'.$css_class.'><td>'.
 4547:                               &mt('Nothing to set here, as there are no other institutions').
 4548:                               '</td></tr>';
 4549:                 $itemcount ++;
 4550:             } else {
 4551:                 ($datatable,$itemcount) = 
 4552:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4553:             }
 4554:         }
 4555:     }
 4556:     $$rowtotal += $itemcount;
 4557:     return $datatable;
 4558: }
 4559: 
 4560: sub ssl_titles {
 4561:     return &Apache::lonlocal::texthash (
 4562:                dom           => 'LON-CAPA servers/VMs from same domain',
 4563:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 4564:                other         => 'External LON-CAPA servers/VMs',
 4565:                connect       => 'Connections to other servers',
 4566:                replication   => 'Replicating content to other institutions',
 4567:                certreq       => 'Client certificate required, but specific domains exempt',
 4568:                nocertreq     => 'No client certificate required, except for specific domains',
 4569:                no            => 'SSL not used',
 4570:                yes           => 'SSL Optional (used if available)',
 4571:                req           => 'SSL Required',
 4572:     );
 4573: }
 4574: 
 4575: sub print_trust {
 4576:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 4577:     my ($css_class,$datatable,%checked,%choices);
 4578:     my (%by_ip,%by_location,@intdoms,@instdoms);
 4579:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4580:     my $itemcount = 1;
 4581:     my %titles = &trust_titles();
 4582:     my @types = ('exc','inc');
 4583:     if ($prefix eq 'top') {
 4584:         $prefix = 'content';
 4585:     } elsif ($prefix eq 'bottom') {
 4586:         $prefix = 'msg';
 4587:     }
 4588:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4589:     $$rowtotal += $itemcount;
 4590:     return $datatable;
 4591: }
 4592: 
 4593: sub trust_titles {
 4594:     return &Apache::lonlocal::texthash(
 4595:                content  => "Access to this domain's content by others",
 4596:                shared   => "Access to other domain's content by this domain",
 4597:                enroll   => "Enrollment in this domain's courses by others", 
 4598:                othcoau  => "Co-author roles in this domain for others",
 4599:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 4600:                domroles => "Domain roles in this domain assignable to others",
 4601:                catalog  => "Course Catalog for this domain displayed elsewhere",
 4602:                reqcrs   => "Requests for creation of courses in this domain by others",
 4603:                msg      => "Users in other domains can send messages to this domain",
 4604:                exc      => "Allow all, but exclude specific domains",
 4605:                inc      => "Deny all, but include specific domains",
 4606:            );
 4607: } 
 4608: 
 4609: sub build_location_hashes {
 4610:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 4611:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 4612:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 4613:     my %iphost = &Apache::lonnet::get_iphost();
 4614:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 4615:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 4616:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 4617:         foreach my $id (@{$iphost{$primary_ip}}) {
 4618:             my $intdom = &Apache::lonnet::internet_dom($id);
 4619:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 4620:                 push(@{$intdoms},$intdom);
 4621:             }
 4622:         }
 4623:     }
 4624:     foreach my $ip (keys(%iphost)) {
 4625:         if (ref($iphost{$ip}) eq 'ARRAY') {
 4626:             foreach my $id (@{$iphost{$ip}}) {
 4627:                 my $location = &Apache::lonnet::internet_dom($id);
 4628:                 if ($location) {
 4629:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 4630:                         my $dom = &Apache::lonnet::host_domain($id);
 4631:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 4632:                             push(@{$instdoms},$dom);
 4633:                         }
 4634:                         next;
 4635:                     }
 4636:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4637:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 4638:                             push(@{$by_ip->{$ip}},$location);
 4639:                         }
 4640:                     } else {
 4641:                         $by_ip->{$ip} = [$location];
 4642:                     }
 4643:                 }
 4644:             }
 4645:         }
 4646:     }
 4647:     foreach my $ip (sort(keys(%{$by_ip}))) {
 4648:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4649:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 4650:             my $first = $by_ip->{$ip}->[0];
 4651:             if (ref($by_location->{$first}) eq 'ARRAY') {
 4652:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 4653:                     push(@{$by_location->{$first}},$ip);
 4654:                 }
 4655:             } else {
 4656:                 $by_location->{$first} = [$ip];
 4657:             }
 4658:         }
 4659:     }
 4660:     return;
 4661: }
 4662: 
 4663: sub current_offloads_to {
 4664:     my ($dom,$settings,$servers) = @_;
 4665:     my (%spareid,%otherdomconfigs);
 4666:     if (ref($servers) eq 'HASH') {
 4667:         foreach my $lonhost (sort(keys(%{$servers}))) {
 4668:             my $gotspares;
 4669:             if (ref($settings) eq 'HASH') {
 4670:                 if (ref($settings->{'spares'}) eq 'HASH') {
 4671:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 4672:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 4673:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 4674:                         $gotspares = 1;
 4675:                     }
 4676:                 }
 4677:             }
 4678:             unless ($gotspares) {
 4679:                 my $gotspares;
 4680:                 my $serverhomeID =
 4681:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 4682:                 my $serverhomedom =
 4683:                     &Apache::lonnet::host_domain($serverhomeID);
 4684:                 if ($serverhomedom ne $dom) {
 4685:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 4686:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4687:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4688:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4689:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4690:                                 $gotspares = 1;
 4691:                             }
 4692:                         }
 4693:                     } else {
 4694:                         $otherdomconfigs{$serverhomedom} =
 4695:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 4696:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 4697:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4698:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4699:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 4700:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4701:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4702:                                         $gotspares = 1;
 4703:                                     }
 4704:                                 }
 4705:                             }
 4706:                         }
 4707:                     }
 4708:                 }
 4709:             }
 4710:             unless ($gotspares) {
 4711:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4712:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4713:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4714:                } else {
 4715:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 4716:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 4717:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4718:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4719:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4720:                     } else {
 4721:                         my %what = (
 4722:                              spareid => 1,
 4723:                         );
 4724:                         my ($result,$returnhash) = 
 4725:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 4726:                         if ($result eq 'ok') { 
 4727:                             if (ref($returnhash) eq 'HASH') {
 4728:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 4729:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 4730:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 4731:                                 }
 4732:                             }
 4733:                         }
 4734:                     }
 4735:                 }
 4736:             }
 4737:         }
 4738:     }
 4739:     return %spareid;
 4740: }
 4741: 
 4742: sub spares_row {
 4743:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 4744:     my $css_class;
 4745:     my $numinrow = 4;
 4746:     my $itemcount = 1;
 4747:     my $datatable;
 4748:     my %typetitles = &sparestype_titles();
 4749:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4750:         foreach my $server (sort(keys(%{$servers}))) {
 4751:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 4752:             my ($othercontrol,$serverdom);
 4753:             if ($serverhome ne $server) {
 4754:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 4755:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4756:             } else {
 4757:                 $serverdom = &Apache::lonnet::host_domain($server);
 4758:                 if ($serverdom ne $dom) {
 4759:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4760:                 }
 4761:             }
 4762:             next unless (ref($spareid->{$server}) eq 'HASH');
 4763:             my $checkednow;
 4764:             if (ref($curroffloadnow) eq 'HASH') {
 4765:                 if ($curroffloadnow->{$server}) {
 4766:                     $checkednow = ' checked="checked"';
 4767:                 }
 4768:             }
 4769:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4770:             $datatable .= '<tr'.$css_class.'>
 4771:                            <td rowspan="2">
 4772:                             <span class="LC_nobreak">'.
 4773:                           &mt('[_1] when busy, offloads to:'
 4774:                               ,'<b>'.$server.'</b>').'</span><br />'.
 4775:                           '<span class="LC_nobreak">'."\n". 
 4776:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 4777:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 4778:                           "\n";
 4779:             my (%current,%canselect);
 4780:             my @choices = 
 4781:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 4782:             foreach my $type ('primary','default') {
 4783:                 if (ref($spareid->{$server}) eq 'HASH') {
 4784:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 4785:                         my @spares = @{$spareid->{$server}{$type}};
 4786:                         if (@spares > 0) {
 4787:                             if ($othercontrol) {
 4788:                                 $current{$type} = join(', ',@spares);
 4789:                             } else {
 4790:                                 $current{$type} .= '<table>';
 4791:                                 my $numspares = scalar(@spares);
 4792:                                 for (my $i=0;  $i<@spares; $i++) {
 4793:                                     my $rem = $i%($numinrow);
 4794:                                     if ($rem == 0) {
 4795:                                         if ($i > 0) {
 4796:                                             $current{$type} .= '</tr>';
 4797:                                         }
 4798:                                         $current{$type} .= '<tr>';
 4799:                                     }
 4800:                                     $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;'.
 4801:                                                        $spareid->{$server}{$type}[$i].
 4802:                                                        '</label></td>'."\n";
 4803:                                 }
 4804:                                 my $rem = @spares%($numinrow);
 4805:                                 my $colsleft = $numinrow - $rem;
 4806:                                 if ($colsleft > 1 ) {
 4807:                                     $current{$type} .= '<td colspan="'.$colsleft.
 4808:                                                        '" class="LC_left_item">'.
 4809:                                                        '&nbsp;</td>';
 4810:                                 } elsif ($colsleft == 1) {
 4811:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 4812:                                 }
 4813:                                 $current{$type} .= '</tr></table>';
 4814:                             }
 4815:                         }
 4816:                     }
 4817:                     if ($current{$type} eq '') {
 4818:                         $current{$type} = &mt('None specified');
 4819:                     }
 4820:                     if ($othercontrol) {
 4821:                         if ($type eq 'primary') {
 4822:                             $canselect{$type} = $othercontrol;
 4823:                         }
 4824:                     } else {
 4825:                         $canselect{$type} = 
 4826:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 4827:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 4828:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 4829:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 4830:                         if (@choices > 0) {
 4831:                             foreach my $lonhost (@choices) {
 4832:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 4833:                             }
 4834:                         }
 4835:                         $canselect{$type} .= '</select>'."\n";
 4836:                     }
 4837:                 } else {
 4838:                     $current{$type} = &mt('Could not be determined');
 4839:                     if ($type eq 'primary') {
 4840:                         $canselect{$type} =  $othercontrol;
 4841:                     }
 4842:                 }
 4843:                 if ($type eq 'default') {
 4844:                     $datatable .= '<tr'.$css_class.'>';
 4845:                 }
 4846:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 4847:                               '<td>'.$current{$type}.'</td>'."\n".
 4848:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 4849:             }
 4850:             $itemcount ++;
 4851:         }
 4852:     }
 4853:     $$rowtotal += $itemcount;
 4854:     return $datatable;
 4855: }
 4856: 
 4857: sub possible_newspares {
 4858:     my ($server,$currspares,$serverhomes,$altids) = @_;
 4859:     my $serverhostname = &Apache::lonnet::hostname($server);
 4860:     my %excluded;
 4861:     if ($serverhostname ne '') {
 4862:         %excluded = (
 4863:                        $serverhostname => 1,
 4864:                     );
 4865:     }
 4866:     if (ref($currspares) eq 'HASH') {
 4867:         foreach my $type (keys(%{$currspares})) {
 4868:             if (ref($currspares->{$type}) eq 'ARRAY') {
 4869:                 if (@{$currspares->{$type}} > 0) {
 4870:                     foreach my $curr (@{$currspares->{$type}}) {
 4871:                         my $hostname = &Apache::lonnet::hostname($curr);
 4872:                         $excluded{$hostname} = 1;
 4873:                     }
 4874:                 }
 4875:             }
 4876:         }
 4877:     }
 4878:     my @choices;
 4879:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4880:         if (keys(%{$serverhomes}) > 1) {
 4881:             foreach my $name (sort(keys(%{$serverhomes}))) {
 4882:                 unless ($excluded{$name}) {
 4883:                     if (exists($altids->{$serverhomes->{$name}})) {
 4884:                         push(@choices,$altids->{$serverhomes->{$name}});
 4885:                     } else {
 4886:                         push(@choices,$serverhomes->{$name});
 4887:                     }
 4888:                 }
 4889:             }
 4890:         }
 4891:     }
 4892:     return sort(@choices);
 4893: }
 4894: 
 4895: sub print_loadbalancing {
 4896:     my ($dom,$settings,$rowtotal) = @_;
 4897:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 4898:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 4899:     my $numinrow = 1;
 4900:     my $datatable;
 4901:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4902:     my (%currbalancer,%currtargets,%currrules,%existing);
 4903:     if (ref($settings) eq 'HASH') {
 4904:         %existing = %{$settings};
 4905:     }
 4906:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 4907:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 4908:                                   \%currtargets,\%currrules);
 4909:     } else {
 4910:         return;
 4911:     }
 4912:     my ($othertitle,$usertypes,$types) =
 4913:         &Apache::loncommon::sorted_inst_types($dom);
 4914:     my $rownum = 8;
 4915:     if (ref($types) eq 'ARRAY') {
 4916:         $rownum += scalar(@{$types});
 4917:     }
 4918:     my @css_class = ('LC_odd_row','LC_even_row');
 4919:     my $balnum = 0;
 4920:     my $islast;
 4921:     my (@toshow,$disabledtext);
 4922:     if (keys(%currbalancer) > 0) {
 4923:         @toshow = sort(keys(%currbalancer));
 4924:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 4925:             push(@toshow,'');
 4926:         }
 4927:     } else {
 4928:         @toshow = ('');
 4929:         $disabledtext = &mt('No existing load balancer');
 4930:     }
 4931:     foreach my $lonhost (@toshow) {
 4932:         if ($balnum == scalar(@toshow)-1) {
 4933:             $islast = 1;
 4934:         } else {
 4935:             $islast = 0;
 4936:         }
 4937:         my $cssidx = $balnum%2;
 4938:         my $targets_div_style = 'display: none';
 4939:         my $disabled_div_style = 'display: block';
 4940:         my $homedom_div_style = 'display: none';
 4941:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 4942:                       '<td rowspan="'.$rownum.'" valign="top">'.
 4943:                       '<p>';
 4944:         if ($lonhost eq '') {
 4945:             $datatable .= '<span class="LC_nobreak">';
 4946:             if (keys(%currbalancer) > 0) {
 4947:                 $datatable .= &mt('Add balancer:');
 4948:             } else {
 4949:                 $datatable .= &mt('Enable balancer:');
 4950:             }
 4951:             $datatable .= '&nbsp;'.
 4952:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 4953:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 4954:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 4955:                           '<option value="" selected="selected">'.&mt('None').
 4956:                           '</option>'."\n";
 4957:             foreach my $server (sort(keys(%servers))) {
 4958:                 next if ($currbalancer{$server});
 4959:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 4960:             }
 4961:             $datatable .=
 4962:                 '</select>'."\n".
 4963:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 4964:         } else {
 4965:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 4966:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 4967:                            &mt('Stop balancing').'</label>'.
 4968:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 4969:             $targets_div_style = 'display: block';
 4970:             $disabled_div_style = 'display: none';
 4971:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 4972:                 $homedom_div_style = 'display: block';
 4973:             }
 4974:         }
 4975:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 4976:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 4977:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 4978:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 4979:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 4980:         my @sparestypes = ('primary','default');
 4981:         my %typetitles = &sparestype_titles();
 4982:         my %hostherechecked = (
 4983:                                   no => ' checked="checked"',
 4984:                               );
 4985:         foreach my $sparetype (@sparestypes) {
 4986:             my $targettable;
 4987:             for (my $i=0; $i<$numspares; $i++) {
 4988:                 my $checked;
 4989:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 4990:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 4991:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 4992:                             $checked = ' checked="checked"';
 4993:                         }
 4994:                     }
 4995:                 }
 4996:                 my ($chkboxval,$disabled);
 4997:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 4998:                     $chkboxval = $spares[$i];
 4999:                 }
 5000:                 if (exists($currbalancer{$spares[$i]})) {
 5001:                     $disabled = ' disabled="disabled"';
 5002:                 }
 5003:                 $targettable .=
 5004:                     '<td><span class="LC_nobreak"><label>'.
 5005:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 5006:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 5007:                     '</span></label></span></td>';
 5008:                 my $rem = $i%($numinrow);
 5009:                 if ($rem == 0) {
 5010:                     if (($i > 0) && ($i < $numspares-1)) {
 5011:                         $targettable .= '</tr>';
 5012:                     }
 5013:                     if ($i < $numspares-1) {
 5014:                         $targettable .= '<tr>';
 5015:                     }
 5016:                 }
 5017:             }
 5018:             if ($targettable ne '') {
 5019:                 my $rem = $numspares%($numinrow);
 5020:                 my $colsleft = $numinrow - $rem;
 5021:                 if ($colsleft > 1 ) {
 5022:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5023:                                     '&nbsp;</td>';
 5024:                 } elsif ($colsleft == 1) {
 5025:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 5026:                 }
 5027:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 5028:                                '<table><tr>'.$targettable.'</tr></table><br />';
 5029:             }
 5030:             $hostherechecked{$sparetype} = '';
 5031:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 5032:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5033:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5034:                         $hostherechecked{$sparetype} = ' checked="checked"';
 5035:                         $hostherechecked{'no'} = '';
 5036:                     }
 5037:                 }
 5038:             }
 5039:         }
 5040:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 5041:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 5042:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 5043:         foreach my $sparetype (@sparestypes) {
 5044:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 5045:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 5046:                           '</i></label><br />';
 5047:         }
 5048:         $datatable .= '</div></td></tr>'.
 5049:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 5050:                                            $othertitle,$usertypes,$types,\%servers,
 5051:                                            \%currbalancer,$lonhost,
 5052:                                            $targets_div_style,$homedom_div_style,
 5053:                                            $css_class[$cssidx],$balnum,$islast);
 5054:         $$rowtotal += $rownum;
 5055:         $balnum ++;
 5056:     }
 5057:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 5058:     return $datatable;
 5059: }
 5060: 
 5061: sub get_loadbalancers_config {
 5062:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 5063:     return unless ((ref($servers) eq 'HASH') &&
 5064:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 5065:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 5066:     if (keys(%{$existing}) > 0) {
 5067:         my $oldlonhost;
 5068:         foreach my $key (sort(keys(%{$existing}))) {
 5069:             if ($key eq 'lonhost') {
 5070:                 $oldlonhost = $existing->{'lonhost'};
 5071:                 $currbalancer->{$oldlonhost} = 1;
 5072:             } elsif ($key eq 'targets') {
 5073:                 if ($oldlonhost) {
 5074:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 5075:                 }
 5076:             } elsif ($key eq 'rules') {
 5077:                 if ($oldlonhost) {
 5078:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 5079:                 }
 5080:             } elsif (ref($existing->{$key}) eq 'HASH') {
 5081:                 $currbalancer->{$key} = 1;
 5082:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 5083:                 $currrules->{$key} = $existing->{$key}{'rules'};
 5084:             }
 5085:         }
 5086:     } else {
 5087:         my ($balancerref,$targetsref) =
 5088:                 &Apache::lonnet::get_lonbalancer_config($servers);
 5089:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 5090:             foreach my $server (sort(keys(%{$balancerref}))) {
 5091:                 $currbalancer->{$server} = 1;
 5092:                 $currtargets->{$server} = $targetsref->{$server};
 5093:             }
 5094:         }
 5095:     }
 5096:     return;
 5097: }
 5098: 
 5099: sub loadbalancing_rules {
 5100:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 5101:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 5102:         $css_class,$balnum,$islast) = @_;
 5103:     my $output;
 5104:     my $num = 0;
 5105:     my ($alltypes,$othertypes,$titles) =
 5106:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 5107:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 5108:         foreach my $type (@{$alltypes}) {
 5109:             $num ++;
 5110:             my $current;
 5111:             if (ref($currrules) eq 'HASH') {
 5112:                 $current = $currrules->{$type};
 5113:             }
 5114:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5115:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 5116:                     $current = '';
 5117:                 }
 5118:             }
 5119:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 5120:                                              $servers,$currbalancer,$lonhost,$dom,
 5121:                                              $targets_div_style,$homedom_div_style,
 5122:                                              $css_class,$balnum,$num,$islast);
 5123:         }
 5124:     }
 5125:     return $output;
 5126: }
 5127: 
 5128: sub loadbalancing_titles {
 5129:     my ($dom,$intdom,$usertypes,$types) = @_;
 5130:     my %othertypes = (
 5131:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 5132:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 5133:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 5134:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 5135:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 5136:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 5137:                      );
 5138:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 5139:     if (ref($types) eq 'ARRAY') {
 5140:         unshift(@alltypes,@{$types},'default');
 5141:     }
 5142:     my %titles;
 5143:     foreach my $type (@alltypes) {
 5144:         if ($type =~ /^_LC_/) {
 5145:             $titles{$type} = $othertypes{$type};
 5146:         } elsif ($type eq 'default') {
 5147:             $titles{$type} = &mt('All users from [_1]',$dom);
 5148:             if (ref($types) eq 'ARRAY') {
 5149:                 if (@{$types} > 0) {
 5150:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 5151:                 }
 5152:             }
 5153:         } elsif (ref($usertypes) eq 'HASH') {
 5154:             $titles{$type} = $usertypes->{$type};
 5155:         }
 5156:     }
 5157:     return (\@alltypes,\%othertypes,\%titles);
 5158: }
 5159: 
 5160: sub loadbalance_rule_row {
 5161:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 5162:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 5163:     my @rulenames;
 5164:     my %ruletitles = &offloadtype_text();
 5165:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 5166:         @rulenames = ('balancer','offloadedto','specific');
 5167:     } else {
 5168:         @rulenames = ('default','homeserver');
 5169:         if ($type eq '_LC_external') {
 5170:             push(@rulenames,'externalbalancer');
 5171:         } else {
 5172:             push(@rulenames,'specific');
 5173:         }
 5174:         push(@rulenames,'none');
 5175:     }
 5176:     my $style = $targets_div_style;
 5177:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5178:         $style = $homedom_div_style;
 5179:     }
 5180:     my $space;
 5181:     if ($islast && $num == 1) {
 5182:         $space = '<div display="inline-block">&nbsp;</div>';
 5183:     }
 5184:     my $output =
 5185:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 5186:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 5187:         '<td valaign="top">'.$space.
 5188:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 5189:     for (my $i=0; $i<@rulenames; $i++) {
 5190:         my $rule = $rulenames[$i];
 5191:         my ($checked,$extra);
 5192:         if ($rulenames[$i] eq 'default') {
 5193:             $rule = '';
 5194:         }
 5195:         if ($rulenames[$i] eq 'specific') {
 5196:             if (ref($servers) eq 'HASH') {
 5197:                 my $default;
 5198:                 if (($current ne '') && (exists($servers->{$current}))) {
 5199:                     $checked = ' checked="checked"';
 5200:                 }
 5201:                 unless ($checked) {
 5202:                     $default = ' selected="selected"';
 5203:                 }
 5204:                 $extra =
 5205:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5206:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5207:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 5208:                     '<option value=""'.$default.'></option>'."\n";
 5209:                 foreach my $server (sort(keys(%{$servers}))) {
 5210:                     if (ref($currbalancer) eq 'HASH') {
 5211:                         next if (exists($currbalancer->{$server}));
 5212:                     }
 5213:                     my $selected;
 5214:                     if ($server eq $current) {
 5215:                         $selected = ' selected="selected"';
 5216:                     }
 5217:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 5218:                 }
 5219:                 $extra .= '</select>';
 5220:             }
 5221:         } elsif ($rule eq $current) {
 5222:             $checked = ' checked="checked"';
 5223:         }
 5224:         $output .= '<span class="LC_nobreak"><label>'.
 5225:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 5226:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 5227:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 5228:                    ')"'.$checked.' />&nbsp;';
 5229:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 5230:             $output .= $ruletitles{'particular'};
 5231:         } else {
 5232:             $output .= $ruletitles{$rulenames[$i]};
 5233:         }
 5234:         $output .= '</label>'.$extra.'</span><br />'."\n";
 5235:     }
 5236:     $output .= '</div></td></tr>'."\n";
 5237:     return $output;
 5238: }
 5239: 
 5240: sub offloadtype_text {
 5241:     my %ruletitles = &Apache::lonlocal::texthash (
 5242:            'default'          => 'Offloads to default destinations',
 5243:            'homeserver'       => "Offloads to user's home server",
 5244:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 5245:            'specific'         => 'Offloads to specific server',
 5246:            'none'             => 'No offload',
 5247:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 5248:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 5249:            'particular'       => 'Session hosted (after re-auth) on server:',
 5250:     );
 5251:     return %ruletitles;
 5252: }
 5253: 
 5254: sub sparestype_titles {
 5255:     my %typestitles = &Apache::lonlocal::texthash (
 5256:                           'primary' => 'primary',
 5257:                           'default' => 'default',
 5258:                       );
 5259:     return %typestitles;
 5260: }
 5261: 
 5262: sub contact_titles {
 5263:     my %titles = &Apache::lonlocal::texthash (
 5264:                    'supportemail' => 'Support E-mail address',
 5265:                    'adminemail'   => 'Default Server Admin E-mail address',
 5266:                    'errormail'    => 'Error reports to be e-mailed to',
 5267:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 5268:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 5269:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 5270:                    'requestsmail' => 'E-mail from course requests requiring approval',
 5271:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 5272:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 5273:                  );
 5274:     my %short_titles = &Apache::lonlocal::texthash (
 5275:                            adminemail   => 'Admin E-mail address',
 5276:                            supportemail => 'Support E-mail',
 5277:                        );   
 5278:     return (\%titles,\%short_titles);
 5279: }
 5280: 
 5281: sub tool_titles {
 5282:     my %titles = &Apache::lonlocal::texthash (
 5283:                      aboutme    => 'Personal web page',
 5284:                      blog       => 'Blog',
 5285:                      webdav     => 'WebDAV',
 5286:                      portfolio  => 'Portfolio',
 5287:                      official   => 'Official courses (with institutional codes)',
 5288:                      unofficial => 'Unofficial courses',
 5289:                      community  => 'Communities',
 5290:                      textbook   => 'Textbook courses',
 5291:                      placement  => 'Placement tests',
 5292:                  );
 5293:     return %titles;
 5294: }
 5295: 
 5296: sub courserequest_titles {
 5297:     my %titles = &Apache::lonlocal::texthash (
 5298:                                    official   => 'Official',
 5299:                                    unofficial => 'Unofficial',
 5300:                                    community  => 'Communities',
 5301:                                    textbook   => 'Textbook',
 5302:                                    placement  => 'Placement tests',
 5303:                                    norequest  => 'Not allowed',
 5304:                                    approval   => 'Approval by Dom. Coord.',
 5305:                                    validate   => 'With validation',
 5306:                                    autolimit  => 'Numerical limit',
 5307:                                    unlimited  => '(blank for unlimited)',
 5308:                  );
 5309:     return %titles;
 5310: }
 5311: 
 5312: sub authorrequest_titles {
 5313:     my %titles = &Apache::lonlocal::texthash (
 5314:                                    norequest  => 'Not allowed',
 5315:                                    approval   => 'Approval by Dom. Coord.',
 5316:                                    automatic  => 'Automatic approval',
 5317:                  );
 5318:     return %titles;
 5319: }
 5320: 
 5321: sub courserequest_conditions {
 5322:     my %conditions = &Apache::lonlocal::texthash (
 5323:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 5324:        validate   => '(Processing of request subject to institutional validation).',
 5325:                  );
 5326:     return %conditions;
 5327: }
 5328: 
 5329: 
 5330: sub print_usercreation {
 5331:     my ($position,$dom,$settings,$rowtotal) = @_;
 5332:     my $numinrow = 4;
 5333:     my $datatable;
 5334:     if ($position eq 'top') {
 5335:         $$rowtotal ++;
 5336:         my $rowcount = 0;
 5337:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 5338:         if (ref($rules) eq 'HASH') {
 5339:             if (keys(%{$rules}) > 0) {
 5340:                 $datatable .= &user_formats_row('username',$settings,$rules,
 5341:                                                 $ruleorder,$numinrow,$rowcount);
 5342:                 $$rowtotal ++;
 5343:                 $rowcount ++;
 5344:             }
 5345:         }
 5346:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 5347:         if (ref($idrules) eq 'HASH') {
 5348:             if (keys(%{$idrules}) > 0) {
 5349:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 5350:                                                 $idruleorder,$numinrow,$rowcount);
 5351:                 $$rowtotal ++;
 5352:                 $rowcount ++;
 5353:             }
 5354:         }
 5355:         if ($rowcount == 0) {
 5356:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 5357:             $$rowtotal ++;
 5358:             $rowcount ++;
 5359:         }
 5360:     } elsif ($position eq 'middle') {
 5361:         my @creators = ('author','course','requestcrs');
 5362:         my ($rules,$ruleorder) =
 5363:             &Apache::lonnet::inst_userrules($dom,'username');
 5364:         my %lt = &usercreation_types();
 5365:         my %checked;
 5366:         if (ref($settings) eq 'HASH') {
 5367:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 5368:                 foreach my $item (@creators) {
 5369:                     $checked{$item} = $settings->{'cancreate'}{$item};
 5370:                 }
 5371:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 5372:                 foreach my $item (@creators) {
 5373:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 5374:                         $checked{$item} = 'none';
 5375:                     }
 5376:                 }
 5377:             }
 5378:         }
 5379:         my $rownum = 0;
 5380:         foreach my $item (@creators) {
 5381:             $rownum ++;
 5382:             if ($checked{$item} eq '') {
 5383:                 $checked{$item} = 'any';
 5384:             }
 5385:             my $css_class;
 5386:             if ($rownum%2) {
 5387:                 $css_class = '';
 5388:             } else {
 5389:                 $css_class = ' class="LC_odd_row" ';
 5390:             }
 5391:             $datatable .= '<tr'.$css_class.'>'.
 5392:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 5393:                          '</span></td><td align="right">';
 5394:             my @options = ('any');
 5395:             if (ref($rules) eq 'HASH') {
 5396:                 if (keys(%{$rules}) > 0) {
 5397:                     push(@options,('official','unofficial'));
 5398:                 }
 5399:             }
 5400:             push(@options,'none');
 5401:             foreach my $option (@options) {
 5402:                 my $type = 'radio';
 5403:                 my $check = ' ';
 5404:                 if ($checked{$item} eq $option) {
 5405:                     $check = ' checked="checked" ';
 5406:                 } 
 5407:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5408:                               '<input type="'.$type.'" name="can_createuser_'.
 5409:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 5410:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 5411:             }
 5412:             $datatable .= '</td></tr>';
 5413:         }
 5414:     } else {
 5415:         my @contexts = ('author','course','domain');
 5416:         my @authtypes = ('int','krb4','krb5','loc');
 5417:         my %checked;
 5418:         if (ref($settings) eq 'HASH') {
 5419:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 5420:                 foreach my $item (@contexts) {
 5421:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 5422:                         foreach my $auth (@authtypes) {
 5423:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 5424:                                 $checked{$item}{$auth} = ' checked="checked" ';
 5425:                             }
 5426:                         }
 5427:                     }
 5428:                 }
 5429:             }
 5430:         } else {
 5431:             foreach my $item (@contexts) {
 5432:                 foreach my $auth (@authtypes) {
 5433:                     $checked{$item}{$auth} = ' checked="checked" ';
 5434:                 }
 5435:             }
 5436:         }
 5437:         my %title = &context_names();
 5438:         my %authname = &authtype_names();
 5439:         my $rownum = 0;
 5440:         my $css_class; 
 5441:         foreach my $item (@contexts) {
 5442:             if ($rownum%2) {
 5443:                 $css_class = '';
 5444:             } else {
 5445:                 $css_class = ' class="LC_odd_row" ';
 5446:             }
 5447:             $datatable .=   '<tr'.$css_class.'>'.
 5448:                             '<td>'.$title{$item}.
 5449:                             '</td><td class="LC_left_item">'.
 5450:                             '<span class="LC_nobreak">';
 5451:             foreach my $auth (@authtypes) {
 5452:                 $datatable .= '<label>'. 
 5453:                               '<input type="checkbox" name="'.$item.'_auth" '.
 5454:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 5455:                               $authname{$auth}.'</label>&nbsp;';
 5456:             }
 5457:             $datatable .= '</span></td></tr>';
 5458:             $rownum ++;
 5459:         }
 5460:         $$rowtotal += $rownum;
 5461:     }
 5462:     return $datatable;
 5463: }
 5464: 
 5465: sub print_selfcreation {
 5466:     my ($position,$dom,$settings,$rowtotal) = @_;
 5467:     my (@selfcreate,$createsettings,$processing,$datatable);
 5468:     if (ref($settings) eq 'HASH') {
 5469:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 5470:             $createsettings = $settings->{'cancreate'};
 5471:             if (ref($createsettings) eq 'HASH') {
 5472:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 5473:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 5474:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 5475:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 5476:                         @selfcreate = ('email','login','sso');
 5477:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 5478:                         @selfcreate = ($createsettings->{'selfcreate'});
 5479:                     }
 5480:                 }
 5481:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 5482:                     $processing = $createsettings->{'selfcreateprocessing'};
 5483:                 }
 5484:             }
 5485:         }
 5486:     }
 5487:     my %radiohash;
 5488:     my $numinrow = 4;
 5489:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 5490:     if ($position eq 'top') {
 5491:         my %choices = &Apache::lonlocal::texthash (
 5492:                                                       cancreate_login      => 'Institutional Login',
 5493:                                                       cancreate_sso        => 'Institutional Single Sign On',
 5494:                                                   );
 5495:         my @toggles = sort(keys(%choices));
 5496:         my %defaultchecked = (
 5497:                                'cancreate_login' => 'off',
 5498:                                'cancreate_sso'   => 'off',
 5499:                              );
 5500:         my ($onclick,$itemcount);
 5501:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5502:                                                      \%choices,$itemcount,$onclick);
 5503:         $$rowtotal += $itemcount;
 5504:         
 5505:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5506: 
 5507:         if (ref($usertypes) eq 'HASH') {
 5508:             if (keys(%{$usertypes}) > 0) {
 5509:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 5510:                                              $dom,$numinrow,$othertitle,
 5511:                                              'statustocreate',$$rowtotal);
 5512:                 $$rowtotal ++;
 5513:             }
 5514:         }
 5515:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 5516:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5517:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 5518:         my $rem;
 5519:         my $numperrow = 2;
 5520:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 5521:         $datatable .= '<tr'.$css_class.'>'.
 5522:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 5523:                      '<td class="LC_left_item">'."\n".
 5524:                      '<table><tr><td>'."\n";
 5525:         for (my $i=0; $i<@fields; $i++) {
 5526:             $rem = $i%($numperrow);
 5527:             if ($rem == 0) {
 5528:                 if ($i > 0) {
 5529:                     $datatable .= '</tr>';
 5530:                 }
 5531:                 $datatable .= '<tr>';
 5532:             }
 5533:             my $currval;
 5534:             if (ref($createsettings) eq 'HASH') {
 5535:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 5536:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 5537:                 }
 5538:             }
 5539:             $datatable .= '<td class="LC_left_item">'.
 5540:                           '<span class="LC_nobreak">'.
 5541:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 5542:                           'value="'.$currval.'" size="10" />&nbsp;'.
 5543:                           $fieldtitles{$fields[$i]}.'</span></td>';
 5544:         }
 5545:         my $colsleft = $numperrow - $rem;
 5546:         if ($colsleft > 1 ) {
 5547:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5548:                          '&nbsp;</td>';
 5549:         } elsif ($colsleft == 1) {
 5550:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5551:         }
 5552:         $datatable .= '</tr></table></td></tr>';
 5553:         $$rowtotal ++;
 5554:     } elsif ($position eq 'middle') {
 5555:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 5556:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5557:         $usertypes->{'default'} = $othertitle;
 5558:         if (ref($types) eq 'ARRAY') {
 5559:             push(@{$types},'default');
 5560:             $usertypes->{'default'} = $othertitle;
 5561:             foreach my $status (@{$types}) {
 5562:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 5563:                                                        $numinrow,$$rowtotal,$usertypes);
 5564:                 $$rowtotal ++;
 5565:             }
 5566:         }
 5567:     } else {
 5568:         my %choices = &Apache::lonlocal::texthash (
 5569:                                                       cancreate_email => 'E-mail address as username',
 5570:                                                   );
 5571:         my @toggles = sort(keys(%choices));
 5572:         my %defaultchecked = (
 5573:                                'cancreate_email' => 'off',
 5574:                              );
 5575:         my $itemcount = 0;
 5576:         my $display = 'none';
 5577:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 5578:             $display = 'block';
 5579:         }
 5580:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 5581:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 5582:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5583:         my $usertypes = {};
 5584:         my $order = [];
 5585:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 5586:             $usertypes = $domdefaults{'inststatustypes'};
 5587:             $order = $domdefaults{'inststatusguest'};
 5588:         }
 5589:         if (ref($order) eq 'ARRAY') {
 5590:             push(@{$order},'default');
 5591:             if (@{$order} > 1) {
 5592:                 $usertypes->{'default'} = &mt('Other users');
 5593:                 $additional .= '<table><tr>';
 5594:                 foreach my $status (@{$order}) {
 5595:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 5596:                 }
 5597:                 $additional .= '</tr><tr>';
 5598:                 foreach my $status (@{$order}) {
 5599:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 5600:                 }
 5601:                 $additional .= '</tr></table>';
 5602:             } else {
 5603:                 $usertypes->{'default'} = &mt('All users');
 5604:                 $additional .= &email_as_username($rowtotal,$processing);
 5605:             }
 5606:         }
 5607:         $additional .= '</div>'."\n";
 5608: 
 5609:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5610:                                                      \%choices,$$rowtotal,$onclick,$additional);
 5611:         $$rowtotal ++;
 5612:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 5613:         $$rowtotal ++;
 5614:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 5615:         $numinrow = 1;
 5616:         if (ref($order) eq 'ARRAY') {
 5617:             foreach my $status (@{$order}) {
 5618:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 5619:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 5620:                 $$rowtotal ++;
 5621:             }
 5622:         }
 5623:         my ($emailrules,$emailruleorder) =
 5624:             &Apache::lonnet::inst_userrules($dom,'email');
 5625:         if (ref($emailrules) eq 'HASH') {
 5626:             if (keys(%{$emailrules}) > 0) {
 5627:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 5628:                                                 $emailruleorder,$numinrow,$$rowtotal);
 5629:                 $$rowtotal ++;
 5630:             }
 5631:         }
 5632:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 5633:     }
 5634:     return $datatable;
 5635: }
 5636: 
 5637: sub email_as_username {
 5638:     my ($rowtotal,$processing,$type) = @_;
 5639:     my %choices =
 5640:         &Apache::lonlocal::texthash (
 5641:                                       automatic => 'Automatic approval',
 5642:                                       approval  => 'Queued for approval',
 5643:                                     );
 5644:     my $output;
 5645:     foreach my $option ('automatic','approval') {
 5646:         my $checked;
 5647:         if (ref($processing) eq 'HASH') {
 5648:             if ($type eq '') {   
 5649:                 if (!exists($processing->{'default'})) {
 5650:                     if ($option eq 'automatic') {
 5651:                         $checked = ' checked="checked"';
 5652:                     }
 5653:                 } else {
 5654:                     if ($processing->{'default'} eq $option) {
 5655:                         $checked = ' checked="checked"';
 5656:                     }
 5657:                 }
 5658:             } else {
 5659:                 if (!exists($processing->{$type})) {
 5660:                     if ($option eq 'automatic') {
 5661:                         $checked = ' checked="checked"';
 5662:                     }
 5663:                 } else {
 5664:                     if ($processing->{$type} eq $option) {
 5665:                         $checked = ' checked="checked"';
 5666:                     }
 5667:                 }
 5668:             }
 5669:         } elsif ($option eq 'automatic') {
 5670:             $checked = ' checked="checked"'; 
 5671:         }
 5672:         my $name = 'cancreate_emailprocess';
 5673:         if (($type ne '') && ($type ne 'default')) {
 5674:             $name .= '_'.$type;
 5675:         }
 5676:         $output .= '<span class="LC_nobreak"><label>'.
 5677:                    '<input type="radio" name="'.$name.'"'.
 5678:                    $checked.' value="'.$option.'" />'.
 5679:                    $choices{$option}.'</label></span>';
 5680:         if ($type eq '') {
 5681:             $output .= '&nbsp;';
 5682:         } else {
 5683:             $output .= '<br />';
 5684:         }
 5685:     }
 5686:     $$rowtotal ++;
 5687:     return $output;
 5688: }
 5689: 
 5690: sub captcha_choice {
 5691:     my ($context,$settings,$itemcount) = @_;
 5692:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 5693:         $vertext,$currver);
 5694:     my %lt = &captcha_phrases();
 5695:     $keyentry = 'hidden';
 5696:     if ($context eq 'cancreate') {
 5697:         $rowname = &mt('CAPTCHA validation');
 5698:     } elsif ($context eq 'login') {
 5699:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 5700:     }
 5701:     if (ref($settings) eq 'HASH') {
 5702:         if ($settings->{'captcha'}) {
 5703:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 5704:         } else {
 5705:             $checked{'original'} = ' checked="checked"';
 5706:         }
 5707:         if ($settings->{'captcha'} eq 'recaptcha') {
 5708:             $pubtext = $lt{'pub'};
 5709:             $privtext = $lt{'priv'};
 5710:             $keyentry = 'text';
 5711:             $vertext = $lt{'ver'};
 5712:             $currver = $settings->{'recaptchaversion'};
 5713:             if ($currver ne '2') {
 5714:                 $currver = 1;
 5715:             }
 5716:         }
 5717:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 5718:             $currpub = $settings->{'recaptchakeys'}{'public'};
 5719:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 5720:         }
 5721:     } else {
 5722:         $checked{'original'} = ' checked="checked"';
 5723:     }
 5724:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5725:     my $output = '<tr'.$css_class.'>'.
 5726:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 5727:                  '<table><tr><td>'."\n";
 5728:     foreach my $option ('original','recaptcha','notused') {
 5729:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 5730:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 5731:                    $lt{$option}.'</label></span>';
 5732:         unless ($option eq 'notused') {
 5733:             $output .= ('&nbsp;'x2)."\n";
 5734:         }
 5735:     }
 5736: #
 5737: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 5738: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 5739: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 5740: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 5741: #
 5742:     $output .= '</td></tr>'."\n".
 5743:                '<tr><td>'."\n".
 5744:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 5745:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 5746:                $currpub.'" size="40" /></span><br />'."\n".
 5747:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 5748:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 5749:                $currpriv.'" size="40" /></span><br />'.
 5750:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 5751:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 5752:                $currver.'" size="3" /></span><br />'.
 5753:                '</td></tr></table>'."\n".
 5754:                '</td></tr>';
 5755:     return $output;
 5756: }
 5757: 
 5758: sub user_formats_row {
 5759:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 5760:     my $output;
 5761:     my %text = (
 5762:                    'username' => 'new usernames',
 5763:                    'id'       => 'IDs',
 5764:                    'email'    => 'self-created accounts (e-mail)',
 5765:                );
 5766:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5767:     $output = '<tr '.$css_class.'>'.
 5768:               '<td><span class="LC_nobreak">';
 5769:     if ($type eq 'email') {
 5770:         $output .= &mt("Formats disallowed for $text{$type}: ");
 5771:     } else {
 5772:         $output .= &mt("Format rules to check for $text{$type}: ");
 5773:     }
 5774:     $output .= '</span></td>'.
 5775:                '<td class="LC_left_item" colspan="2"><table>';
 5776:     my $rem;
 5777:     if (ref($ruleorder) eq 'ARRAY') {
 5778:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 5779:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 5780:                 my $rem = $i%($numinrow);
 5781:                 if ($rem == 0) {
 5782:                     if ($i > 0) {
 5783:                         $output .= '</tr>';
 5784:                     }
 5785:                     $output .= '<tr>';
 5786:                 }
 5787:                 my $check = ' ';
 5788:                 if (ref($settings) eq 'HASH') {
 5789:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 5790:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 5791:                             $check = ' checked="checked" ';
 5792:                         }
 5793:                     }
 5794:                 }
 5795:                 $output .= '<td class="LC_left_item">'.
 5796:                            '<span class="LC_nobreak"><label>'.
 5797:                            '<input type="checkbox" name="'.$type.'_rule" '.
 5798:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 5799:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 5800:             }
 5801:         }
 5802:         $rem = @{$ruleorder}%($numinrow);
 5803:     }
 5804:     my $colsleft = $numinrow - $rem;
 5805:     if ($colsleft > 1 ) {
 5806:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5807:                    '&nbsp;</td>';
 5808:     } elsif ($colsleft == 1) {
 5809:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5810:     }
 5811:     $output .= '</tr></table></td></tr>';
 5812:     return $output;
 5813: }
 5814: 
 5815: sub usercreation_types {
 5816:     my %lt = &Apache::lonlocal::texthash (
 5817:                     author     => 'When adding a co-author',
 5818:                     course     => 'When adding a user to a course',
 5819:                     requestcrs => 'When requesting a course',
 5820:                     any        => 'Any',
 5821:                     official   => 'Institutional only ',
 5822:                     unofficial => 'Non-institutional only',
 5823:                     none       => 'None',
 5824:     );
 5825:     return %lt;
 5826: }
 5827: 
 5828: sub selfcreation_types {
 5829:     my %lt = &Apache::lonlocal::texthash (
 5830:                     selfcreate => 'User creates own account',
 5831:                     any        => 'Any',
 5832:                     official   => 'Institutional only ',
 5833:                     unofficial => 'Non-institutional only',
 5834:                     email      => 'E-mail address',
 5835:                     login      => 'Institutional Login',
 5836:                     sso        => 'SSO',
 5837:              );
 5838: }
 5839: 
 5840: sub authtype_names {
 5841:     my %lt = &Apache::lonlocal::texthash(
 5842:                       int    => 'Internal',
 5843:                       krb4   => 'Kerberos 4',
 5844:                       krb5   => 'Kerberos 5',
 5845:                       loc    => 'Local',
 5846:                   );
 5847:     return %lt;
 5848: }
 5849: 
 5850: sub context_names {
 5851:     my %context_title = &Apache::lonlocal::texthash(
 5852:        author => 'Creating users when an Author',
 5853:        course => 'Creating users when in a course',
 5854:        domain => 'Creating users when a Domain Coordinator',
 5855:     );
 5856:     return %context_title;
 5857: }
 5858: 
 5859: sub print_usermodification {
 5860:     my ($position,$dom,$settings,$rowtotal) = @_;
 5861:     my $numinrow = 4;
 5862:     my ($context,$datatable,$rowcount);
 5863:     if ($position eq 'top') {
 5864:         $rowcount = 0;
 5865:         $context = 'author'; 
 5866:         foreach my $role ('ca','aa') {
 5867:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 5868:                                                    $numinrow,$rowcount);
 5869:             $$rowtotal ++;
 5870:             $rowcount ++;
 5871:         }
 5872:     } elsif ($position eq 'bottom') {
 5873:         $context = 'course';
 5874:         $rowcount = 0;
 5875:         foreach my $role ('st','ep','ta','in','cr') {
 5876:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 5877:                                                    $numinrow,$rowcount);
 5878:             $$rowtotal ++;
 5879:             $rowcount ++;
 5880:         }
 5881:     }
 5882:     return $datatable;
 5883: }
 5884: 
 5885: sub print_defaults {
 5886:     my ($position,$dom,$settings,$rowtotal) = @_;
 5887:     my $rownum = 0;
 5888:     my ($datatable,$css_class);
 5889:     if ($position eq 'top') {
 5890:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 5891:                      'datelocale_def','portal_def');
 5892:         my %defaults;
 5893:         if (ref($settings) eq 'HASH') {
 5894:             %defaults = %{$settings};
 5895:         } else {
 5896:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 5897:             foreach my $item (@items) {
 5898:                 $defaults{$item} = $domdefaults{$item};
 5899:             }
 5900:         }
 5901:         my $titles = &defaults_titles($dom);
 5902:         foreach my $item (@items) {
 5903:             if ($rownum%2) {
 5904:                 $css_class = '';
 5905:             } else {
 5906:                 $css_class = ' class="LC_odd_row" ';
 5907:             }
 5908:             $datatable .= '<tr'.$css_class.'>'.
 5909:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 5910:                           '</span></td><td class="LC_right_item" colspan="3">';
 5911:             if ($item eq 'auth_def') {
 5912:                 my @authtypes = ('internal','krb4','krb5','localauth');
 5913:                 my %shortauth = (
 5914:                                  internal => 'int',
 5915:                                  krb4 => 'krb4',
 5916:                                  krb5 => 'krb5',
 5917:                                  localauth  => 'loc'
 5918:                                 );
 5919:                 my %authnames = &authtype_names();
 5920:                 foreach my $auth (@authtypes) {
 5921:                     my $checked = ' ';
 5922:                     if ($defaults{$item} eq $auth) {
 5923:                         $checked = ' checked="checked" ';
 5924:                     }
 5925:                     $datatable .= '<label><input type="radio" name="'.$item.
 5926:                                   '" value="'.$auth.'"'.$checked.'/>'.
 5927:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 5928:                 }
 5929:             } elsif ($item eq 'timezone_def') {
 5930:                 my $includeempty = 1;
 5931:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 5932:             } elsif ($item eq 'datelocale_def') {
 5933:                 my $includeempty = 1;
 5934:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 5935:             } elsif ($item eq 'lang_def') {
 5936:                 my $includeempty = 1;
 5937:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 5938:             } else {
 5939:                 my $size;
 5940:                 if ($item eq 'portal_def') {
 5941:                     $size = ' size="25"';
 5942:                 }
 5943:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 5944:                               $defaults{$item}.'"'.$size.' />';
 5945:             }
 5946:             $datatable .= '</td></tr>';
 5947:             $rownum ++;
 5948:         }
 5949:     } else {
 5950:         my (%defaults);
 5951:         if (ref($settings) eq 'HASH') {
 5952:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 5953:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 5954:                 my $maxnum = @{$settings->{'inststatusorder'}};
 5955:                 for (my $i=0; $i<$maxnum; $i++) {
 5956:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 5957:                     my $item = $settings->{'inststatusorder'}->[$i];
 5958:                     my $title = $settings->{'inststatustypes'}->{$item};
 5959:                     my $guestok;
 5960:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 5961:                         $guestok = 1;
 5962:                     }
 5963:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 5964:                     $datatable .= '<tr'.$css_class.'>'.
 5965:                                   '<td><span class="LC_nobreak">'.
 5966:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 5967:                     for (my $k=0; $k<=$maxnum; $k++) {
 5968:                         my $vpos = $k+1;
 5969:                         my $selstr;
 5970:                         if ($k == $i) {
 5971:                             $selstr = ' selected="selected" ';
 5972:                         }
 5973:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5974:                     }
 5975:                     my ($checkedon,$checkedoff);
 5976:                     $checkedoff = ' checked="checked"';
 5977:                     if ($guestok) {
 5978:                         $checkedon = $checkedoff;
 5979:                         $checkedoff = ''; 
 5980:                     }
 5981:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 5982:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 5983:                                   &mt('delete').'</span></td>'.
 5984:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 5985:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 5986:                                   '</span></td>'.
 5987:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 5988:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 5989:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 5990:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 5991:                                   &mt('No').'</label></span></td></tr>';
 5992:                 }
 5993:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 5994:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 5995:                 $datatable .= '<tr '.$css_class.'>'.
 5996:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 5997:                 for (my $k=0; $k<=$maxnum; $k++) {
 5998:                     my $vpos = $k+1;
 5999:                     my $selstr;
 6000:                     if ($k == $maxnum) {
 6001:                         $selstr = ' selected="selected" ';
 6002:                     }
 6003:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6004:                 }
 6005:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 6006:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 6007:                               '&nbsp;'.&mt('(new)').
 6008:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 6009:                               &mt('Name displayed:').
 6010:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 6011:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 6012:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 6013:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 6014:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 6015:                               &mt('No').'</label></span></td></tr>';
 6016:                               '</tr>'."\n";
 6017:                 $rownum ++;
 6018:             }
 6019:         }
 6020:     }
 6021:     $$rowtotal += $rownum;
 6022:     return $datatable;
 6023: }
 6024: 
 6025: sub get_languages_hash {
 6026:     my %langchoices;
 6027:     foreach my $id (&Apache::loncommon::languageids()) {
 6028:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 6029:         if ($code ne '') {
 6030:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 6031:         }
 6032:     }
 6033:     return %langchoices;
 6034: }
 6035: 
 6036: sub defaults_titles {
 6037:     my ($dom) = @_;
 6038:     my %titles = &Apache::lonlocal::texthash (
 6039:                    'auth_def'      => 'Default authentication type',
 6040:                    'auth_arg_def'  => 'Default authentication argument',
 6041:                    'lang_def'      => 'Default language',
 6042:                    'timezone_def'  => 'Default timezone',
 6043:                    'datelocale_def' => 'Default locale for dates',
 6044:                    'portal_def'     => 'Portal/Default URL',
 6045:                  );
 6046:     if ($dom) {
 6047:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 6048:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 6049:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 6050:         $protocol = 'http' if ($protocol ne 'https');
 6051:         if ($uint_dom) {
 6052:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 6053:                                          $uint_dom);
 6054:         }
 6055:     }
 6056:     return (\%titles);
 6057: }
 6058: 
 6059: sub print_scantronformat {
 6060:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 6061:     my $itemcount = 1;
 6062:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 6063:         %confhash);
 6064:     my $switchserver = &check_switchserver($dom,$confname);
 6065:     my %lt = &Apache::lonlocal::texthash (
 6066:                 default => 'Default bubblesheet format file error',
 6067:                 custom  => 'Custom bubblesheet format file error',
 6068:              );
 6069:     my %scantronfiles = (
 6070:         default => 'default.tab',
 6071:         custom => 'custom.tab',
 6072:     );
 6073:     foreach my $key (keys(%scantronfiles)) {
 6074:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 6075:                               .$scantronfiles{$key};
 6076:     }
 6077:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 6078:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 6079:         if (!$switchserver) {
 6080:             my $servadm = $r->dir_config('lonAdmEMail');
 6081:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 6082:             if ($configuserok eq 'ok') {
 6083:                 if ($author_ok eq 'ok') {
 6084:                     my %legacyfile = (
 6085:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 6086:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 6087:                     );
 6088:                     my %md5chk;
 6089:                     foreach my $type (keys(%legacyfile)) {
 6090:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 6091:                         chomp($md5chk{$type});
 6092:                     }
 6093:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 6094:                         foreach my $type (keys(%legacyfile)) {
 6095:                             ($scantronurls{$type},my $error) = 
 6096:                                 &legacy_scantronformat($r,$dom,$confname,
 6097:                                                  $type,$legacyfile{$type},
 6098:                                                  $scantronurls{$type},
 6099:                                                  $scantronfiles{$type});
 6100:                             if ($error ne '') {
 6101:                                 $error{$type} = $error;
 6102:                             }
 6103:                         }
 6104:                         if (keys(%error) == 0) {
 6105:                             $is_custom = 1;
 6106:                             $confhash{'scantron'}{'scantronformat'} = 
 6107:                                 $scantronurls{'custom'};
 6108:                             my $putresult = 
 6109:                                 &Apache::lonnet::put_dom('configuration',
 6110:                                                          \%confhash,$dom);
 6111:                             if ($putresult ne 'ok') {
 6112:                                 $error{'custom'} = 
 6113:                                     '<span class="LC_error">'.
 6114:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6115:                             }
 6116:                         }
 6117:                     } else {
 6118:                         ($scantronurls{'default'},my $error) =
 6119:                             &legacy_scantronformat($r,$dom,$confname,
 6120:                                           'default',$legacyfile{'default'},
 6121:                                           $scantronurls{'default'},
 6122:                                           $scantronfiles{'default'});
 6123:                         if ($error eq '') {
 6124:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 6125:                             my $putresult =
 6126:                                 &Apache::lonnet::put_dom('configuration',
 6127:                                                          \%confhash,$dom);
 6128:                             if ($putresult ne 'ok') {
 6129:                                 $error{'default'} =
 6130:                                     '<span class="LC_error">'.
 6131:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6132:                             }
 6133:                         } else {
 6134:                             $error{'default'} = $error;
 6135:                         }
 6136:                     }
 6137:                 }
 6138:             }
 6139:         } else {
 6140:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 6141:         }
 6142:     }
 6143:     if (ref($settings) eq 'HASH') {
 6144:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 6145:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 6146:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 6147:                 $scantronurl = '';
 6148:             } else {
 6149:                 $scantronurl = $settings->{'scantronformat'};
 6150:             }
 6151:             $is_custom = 1;
 6152:         } else {
 6153:             $scantronurl = $scantronurls{'default'};
 6154:         }
 6155:     } else {
 6156:         if ($is_custom) {
 6157:             $scantronurl = $scantronurls{'custom'};
 6158:         } else {
 6159:             $scantronurl = $scantronurls{'default'};
 6160:         }
 6161:     }
 6162:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6163:     $datatable .= '<tr'.$css_class.'>';
 6164:     if (!$is_custom) {
 6165:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 6166:                       '<span class="LC_nobreak">';
 6167:         if ($scantronurl) {
 6168:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 6169:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 6170:         } else {
 6171:             $datatable = &mt('File unavailable for display');
 6172:         }
 6173:         $datatable .= '</span></td>';
 6174:         if (keys(%error) == 0) { 
 6175:             $datatable .= '<td valign="bottom">';
 6176:             if (!$switchserver) {
 6177:                 $datatable .= &mt('Upload:').'<br />';
 6178:             }
 6179:         } else {
 6180:             my $errorstr;
 6181:             foreach my $key (sort(keys(%error))) {
 6182:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6183:             }
 6184:             $datatable .= '<td>'.$errorstr;
 6185:         }
 6186:     } else {
 6187:         if (keys(%error) > 0) {
 6188:             my $errorstr;
 6189:             foreach my $key (sort(keys(%error))) {
 6190:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6191:             } 
 6192:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 6193:         } elsif ($scantronurl) {
 6194:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 6195:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 6196:             $datatable .= '<td><span class="LC_nobreak">'.
 6197:                           $link.
 6198:                           '<label><input type="checkbox" name="scantronformat_del"'.
 6199:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 6200:                           '<td><span class="LC_nobreak">&nbsp;'.
 6201:                           &mt('Replace:').'</span><br />';
 6202:         }
 6203:     }
 6204:     if (keys(%error) == 0) {
 6205:         if ($switchserver) {
 6206:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 6207:         } else {
 6208:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 6209:                          '<input type="file" name="scantronformat" /></span>';
 6210:         }
 6211:     }
 6212:     $datatable .= '</td></tr>';
 6213:     $$rowtotal ++;
 6214:     return $datatable;
 6215: }
 6216: 
 6217: sub legacy_scantronformat {
 6218:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 6219:     my ($url,$error);
 6220:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 6221:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 6222:         (my $result,$url) =
 6223:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 6224:                          '','',$newfile);
 6225:         if ($result ne 'ok') {
 6226:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 6227:         }
 6228:     }
 6229:     return ($url,$error);
 6230: }
 6231: 
 6232: sub print_coursecategories {
 6233:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 6234:     my $datatable;
 6235:     if ($position eq 'top') {
 6236:         my (%checked);
 6237:         my @catitems = ('unauth','auth');
 6238:         my @cattypes = ('std','domonly','codesrch','none');
 6239:         $checked{'unauth'} = 'std';
 6240:         $checked{'auth'} = 'std';
 6241:         if (ref($settings) eq 'HASH') {
 6242:             foreach my $type (@cattypes) {
 6243:                 if ($type eq $settings->{'unauth'}) {
 6244:                     $checked{'unauth'} = $type;
 6245:                 }
 6246:                 if ($type eq $settings->{'auth'}) {
 6247:                     $checked{'auth'} = $type;
 6248:                 }
 6249:             }
 6250:         }
 6251:         my %lt = &Apache::lonlocal::texthash (
 6252:                                                unauth   => 'Catalog type for unauthenticated users',
 6253:                                                auth     => 'Catalog type for authenticated users',
 6254:                                                none     => 'No catalog',
 6255:                                                std      => 'Standard catalog',
 6256:                                                domonly  => 'Domain-only catalog',
 6257:                                                codesrch => "Code search form",
 6258:                                              );
 6259:        my $itemcount = 0;
 6260:        foreach my $item (@catitems) {
 6261:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 6262:            $datatable .= '<tr '.$css_class.'>'.
 6263:                          '<td>'.$lt{$item}.'</td>'.
 6264:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 6265:            foreach my $type (@cattypes) {
 6266:                my $ischecked;
 6267:                if ($checked{$item} eq $type) {
 6268:                    $ischecked=' checked="checked"';
 6269:                }
 6270:                $datatable .= '<label>'.
 6271:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 6272:                              ' />'.$lt{$type}.'</label>&nbsp;';
 6273:            }
 6274:            $datatable .= '</td></tr>';
 6275:            $itemcount ++;
 6276:         }
 6277:         $$rowtotal += $itemcount;
 6278:     } elsif ($position eq 'middle') {
 6279:         my $toggle_cats_crs = ' ';
 6280:         my $toggle_cats_dom = ' checked="checked" ';
 6281:         my $can_cat_crs = ' ';
 6282:         my $can_cat_dom = ' checked="checked" ';
 6283:         my $toggle_catscomm_comm = ' ';
 6284:         my $toggle_catscomm_dom = ' checked="checked" ';
 6285:         my $can_catcomm_comm = ' ';
 6286:         my $can_catcomm_dom = ' checked="checked" ';
 6287:         my $toggle_catsplace_place = ' ';
 6288:         my $toggle_catsplace_dom = ' checked="checked" ';
 6289:         my $can_catplace_place = ' ';
 6290:         my $can_catplace_dom = ' checked="checked" ';
 6291: 
 6292:         if (ref($settings) eq 'HASH') {
 6293:             if ($settings->{'togglecats'} eq 'crs') {
 6294:                 $toggle_cats_crs = $toggle_cats_dom;
 6295:                 $toggle_cats_dom = ' ';
 6296:             }
 6297:             if ($settings->{'categorize'} eq 'crs') {
 6298:                 $can_cat_crs = $can_cat_dom;
 6299:                 $can_cat_dom = ' ';
 6300:             }
 6301:             if ($settings->{'togglecatscomm'} eq 'comm') {
 6302:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 6303:                 $toggle_catscomm_dom = ' ';
 6304:             }
 6305:             if ($settings->{'categorizecomm'} eq 'comm') {
 6306:                 $can_catcomm_comm = $can_catcomm_dom;
 6307:                 $can_catcomm_dom = ' ';
 6308:             }
 6309:             if ($settings->{'togglecatsplace'} eq 'place') {
 6310:                 $toggle_catsplace_place = $toggle_catsplace_dom;
 6311:                 $toggle_catsplace_dom = ' ';
 6312:             }
 6313:             if ($settings->{'categorizeplace'} eq 'place') {
 6314:                 $can_catplace_place = $can_catplace_dom;
 6315:                 $can_catplace_dom = ' ';
 6316:             }
 6317:         }
 6318:         my %title = &Apache::lonlocal::texthash (
 6319:                      togglecats      => 'Show/Hide a course in catalog',
 6320:                      togglecatscomm  => 'Show/Hide a community in catalog',
 6321:                      togglecatsplace => 'Show/Hide a placement test in catalog',
 6322:                      categorize      => 'Assign a category to a course',
 6323:                      categorizecomm  => 'Assign a category to a community',
 6324:                      categorizeplace => 'Assign a category to a placement test',
 6325:                     );
 6326:         my %level = &Apache::lonlocal::texthash (
 6327:                      dom   => 'Set in Domain',
 6328:                      crs   => 'Set in Course',
 6329:                      comm  => 'Set in Community',
 6330:                      place => 'Set in Placement Test',
 6331:                     );
 6332:         $datatable = '<tr class="LC_odd_row">'.
 6333:                   '<td>'.$title{'togglecats'}.'</td>'.
 6334:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6335:                   '<input type="radio" name="togglecats"'.
 6336:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6337:                   '<label><input type="radio" name="togglecats"'.
 6338:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6339:                   '</tr><tr>'.
 6340:                   '<td>'.$title{'categorize'}.'</td>'.
 6341:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6342:                   '<label><input type="radio" name="categorize"'.
 6343:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6344:                   '<label><input type="radio" name="categorize"'.
 6345:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6346:                   '</tr><tr class="LC_odd_row">'.
 6347:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 6348:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6349:                   '<input type="radio" name="togglecatscomm"'.
 6350:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6351:                   '<label><input type="radio" name="togglecatscomm"'.
 6352:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6353:                   '</tr><tr>'.
 6354:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 6355:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6356:                   '<label><input type="radio" name="categorizecomm"'.
 6357:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6358:                   '<label><input type="radio" name="categorizecomm"'.
 6359:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6360:                   '</tr><tr>'.
 6361:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
 6362:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6363:                   '<input type="radio" name="togglecatsplace"'.
 6364:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6365:                   '<label><input type="radio" name="togglecatscomm"'.
 6366:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
 6367:                   '</tr><tr>'.
 6368:                   '<td>'.$title{'categorizeplace'}.'</td>'.
 6369:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6370:                   '<label><input type="radio" name="categorizeplace"'.
 6371:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6372:                   '<label><input type="radio" name="categorizeplace"'.
 6373:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
 6374:                   '</tr>';
 6375:         $$rowtotal += 6;
 6376:     } else {
 6377:         my $css_class;
 6378:         my $itemcount = 1;
 6379:         my $cathash; 
 6380:         if (ref($settings) eq 'HASH') {
 6381:             $cathash = $settings->{'cats'};
 6382:         }
 6383:         if (ref($cathash) eq 'HASH') {
 6384:             my (@cats,@trails,%allitems,%idx,@jsarray);
 6385:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 6386:                                                    \%allitems,\%idx,\@jsarray);
 6387:             my $maxdepth = scalar(@cats);
 6388:             my $colattrib = '';
 6389:             if ($maxdepth > 2) {
 6390:                 $colattrib = ' colspan="2" ';
 6391:             }
 6392:             my @path;
 6393:             if (@cats > 0) {
 6394:                 if (ref($cats[0]) eq 'ARRAY') {
 6395:                     my $numtop = @{$cats[0]};
 6396:                     my $maxnum = $numtop;
 6397:                     my %default_names = (
 6398:                           instcode    => &mt('Official courses'),
 6399:                           communities => &mt('Communities'),
 6400:                           placement   => &mt('Placement Tests'),
 6401:                     );
 6402: 
 6403:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 6404:                         ($cathash->{'instcode::0'} eq '') ||
 6405:                         (!grep(/^communities$/,@{$cats[0]})) || 
 6406:                         ($cathash->{'communities::0'} eq '') ||
 6407:                         (!grep(/^placement$/,@{$cats[0]})) ||
 6408:                         ($cathash->{'placement::0'} eq '')) {
 6409:                         $maxnum ++;
 6410:                     }
 6411:                     my $lastidx;
 6412:                     for (my $i=0; $i<$numtop; $i++) {
 6413:                         my $parent = $cats[0][$i];
 6414:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6415:                         my $item = &escape($parent).'::0';
 6416:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 6417:                         $lastidx = $idx{$item};
 6418:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6419:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 6420:                         for (my $k=0; $k<=$maxnum; $k++) {
 6421:                             my $vpos = $k+1;
 6422:                             my $selstr;
 6423:                             if ($k == $i) {
 6424:                                 $selstr = ' selected="selected" ';
 6425:                             }
 6426:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6427:                         }
 6428:                         $datatable .= '</select></span></td><td>';
 6429:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
 6430:                             $datatable .=  '<span class="LC_nobreak">'
 6431:                                            .$default_names{$parent}.'</span>';
 6432:                             if ($parent eq 'instcode') {
 6433:                                 $datatable .= '<br /><span class="LC_nobreak">('
 6434:                                               .&mt('with institutional codes')
 6435:                                               .')</span></td><td'.$colattrib.'>';
 6436:                             } else {
 6437:                                 $datatable .= '<table><tr><td>';
 6438:                             }
 6439:                             $datatable .= '<span class="LC_nobreak">'
 6440:                                           .'<label><input type="radio" name="'
 6441:                                           .$parent.'" value="1" checked="checked" />'
 6442:                                           .&mt('Display').'</label>';
 6443:                             if ($parent eq 'instcode') {
 6444:                                 $datatable .= '&nbsp;';
 6445:                             } else {
 6446:                                 $datatable .= '</span></td></tr><tr><td>'
 6447:                                               .'<span class="LC_nobreak">';
 6448:                             }
 6449:                             $datatable .= '<label><input type="radio" name="'
 6450:                                           .$parent.'" value="0" />'
 6451:                                           .&mt('Do not display').'</label></span>';
 6452:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
 6453:                                 $datatable .= '</td></tr></table>';
 6454:                             }
 6455:                             $datatable .= '</td>';
 6456:                         } else {
 6457:                             $datatable .= $parent
 6458:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 6459:                                           .'<input type="checkbox" name="deletecategory" '
 6460:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 6461:                         }
 6462:                         my $depth = 1;
 6463:                         push(@path,$parent);
 6464:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 6465:                         pop(@path);
 6466:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 6467:                         $itemcount ++;
 6468:                     }
 6469:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6470:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 6471:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 6472:                     for (my $k=0; $k<=$maxnum; $k++) {
 6473:                         my $vpos = $k+1;
 6474:                         my $selstr;
 6475:                         if ($k == $numtop) {
 6476:                             $selstr = ' selected="selected" ';
 6477:                         }
 6478:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6479:                     }
 6480:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 6481:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 6482:                                   .'</tr>'."\n";
 6483:                     $itemcount ++;
 6484:                     foreach my $default ('instcode','communities','placement') {
 6485:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 6486:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6487:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 6488:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 6489:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 6490:                             for (my $k=0; $k<=$maxnum; $k++) {
 6491:                                 my $vpos = $k+1;
 6492:                                 my $selstr;
 6493:                                 if ($k == $maxnum) {
 6494:                                     $selstr = ' selected="selected" ';
 6495:                                 }
 6496:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6497:                             }
 6498:                             $datatable .= '</select></span></td>'.
 6499:                                           '<td><span class="LC_nobreak">'.
 6500:                                           $default_names{$default}.'</span>';
 6501:                             if ($default eq 'instcode') {
 6502:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 6503:                                               .&mt('with institutional codes').')</span>';
 6504:                             }
 6505:                             $datatable .= '</td>'
 6506:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 6507:                                           .&mt('Display').'</label>&nbsp;'
 6508:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 6509:                                           .&mt('Do not display').'</label></span></td></tr>';
 6510:                         }
 6511:                     }
 6512:                 }
 6513:             } else {
 6514:                 $datatable .= &initialize_categories($itemcount);
 6515:             }
 6516:         } else {
 6517:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 6518:                           .&initialize_categories($itemcount);
 6519:         }
 6520:         $$rowtotal += $itemcount;
 6521:     }
 6522:     return $datatable;
 6523: }
 6524: 
 6525: sub print_serverstatuses {
 6526:     my ($dom,$settings,$rowtotal) = @_;
 6527:     my $datatable;
 6528:     my @pages = &serverstatus_pages();
 6529:     my (%namedaccess,%machineaccess);
 6530:     foreach my $type (@pages) {
 6531:         $namedaccess{$type} = '';
 6532:         $machineaccess{$type}= '';
 6533:     }
 6534:     if (ref($settings) eq 'HASH') {
 6535:         foreach my $type (@pages) {
 6536:             if (exists($settings->{$type})) {
 6537:                 if (ref($settings->{$type}) eq 'HASH') {
 6538:                     foreach my $key (keys(%{$settings->{$type}})) {
 6539:                         if ($key eq 'namedusers') {
 6540:                             $namedaccess{$type} = $settings->{$type}->{$key};
 6541:                         } elsif ($key eq 'machines') {
 6542:                             $machineaccess{$type} = $settings->{$type}->{$key};
 6543:                         }
 6544:                     }
 6545:                 }
 6546:             }
 6547:         }
 6548:     }
 6549:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 6550:     my $rownum = 0;
 6551:     my $css_class;
 6552:     foreach my $type (@pages) {
 6553:         $rownum ++;
 6554:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 6555:         $datatable .= '<tr'.$css_class.'>'.
 6556:                       '<td><span class="LC_nobreak">'.
 6557:                       $titles->{$type}.'</span></td>'.
 6558:                       '<td class="LC_left_item">'.
 6559:                       '<input type="text" name="'.$type.'_namedusers" '.
 6560:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 6561:                       '<td class="LC_right_item">'.
 6562:                       '<span class="LC_nobreak">'.
 6563:                       '<input type="text" name="'.$type.'_machines" '.
 6564:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 6565:                       '</td></tr>'."\n";
 6566:     }
 6567:     $$rowtotal += $rownum;
 6568:     return $datatable;
 6569: }
 6570: 
 6571: sub serverstatus_pages {
 6572:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 6573:             'checksums','clusterstatus','certstatus','metadata_keywords',
 6574:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
 6575:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
 6576: }
 6577: 
 6578: sub defaults_javascript {
 6579:     my ($settings) = @_;
 6580:     return unless (ref($settings) eq 'HASH'); 
 6581:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 6582:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 6583:         if ($maxnum eq '') {
 6584:             $maxnum = 0;
 6585:         }
 6586:         $maxnum ++;
 6587:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 6588:         return <<"ENDSCRIPT";
 6589: <script type="text/javascript">
 6590: // <![CDATA[
 6591: function reorderTypes(form,caller) {
 6592:     var changedVal;
 6593: $jstext 
 6594:     var newpos = 'addinststatus_pos';
 6595:     var current = new Array;
 6596:     var maxh = $maxnum;
 6597:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6598:     var oldVal;
 6599:     if (caller == newpos) {
 6600:         changedVal = newitemVal;
 6601:     } else {
 6602:         var curritem = 'inststatus_pos_'+caller;
 6603:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 6604:         current[newitemVal] = newpos;
 6605:     }
 6606:     for (var i=0; i<inststatuses.length; i++) {
 6607:         if (inststatuses[i] != caller) {
 6608:             var elementName = 'inststatus_pos_'+inststatuses[i];
 6609:             if (form.elements[elementName]) {
 6610:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 6611:                 current[currVal] = elementName;
 6612:             }
 6613:         }
 6614:     }
 6615:     for (var j=0; j<maxh; j++) {
 6616:         if (current[j] == undefined) {
 6617:             oldVal = j;
 6618:         }
 6619:     }
 6620:     if (oldVal < changedVal) {
 6621:         for (var k=oldVal+1; k<=changedVal ; k++) {
 6622:            var elementName = current[k];
 6623:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 6624:         }
 6625:     } else {
 6626:         for (var k=changedVal; k<oldVal; k++) {
 6627:             var elementName = current[k];
 6628:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 6629:         }
 6630:     }
 6631:     return;
 6632: }
 6633: 
 6634: // ]]>
 6635: </script>
 6636: 
 6637: ENDSCRIPT
 6638:     }
 6639: }
 6640: 
 6641: sub coursecategories_javascript {
 6642:     my ($settings) = @_;
 6643:     my ($output,$jstext,$cathash);
 6644:     if (ref($settings) eq 'HASH') {
 6645:         $cathash = $settings->{'cats'};
 6646:     }
 6647:     if (ref($cathash) eq 'HASH') {
 6648:         my (@cats,@jsarray,%idx);
 6649:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 6650:         if (@jsarray > 0) {
 6651:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 6652:             for (my $i=0; $i<@jsarray; $i++) {
 6653:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 6654:                     my $catstr = join('","',@{$jsarray[$i]});
 6655:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 6656:                 }
 6657:             }
 6658:         }
 6659:     } else {
 6660:         $jstext  = '    var categories = Array(1);'."\n".
 6661:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 6662:     }
 6663:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 6664:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 6665:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
 6666:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 6667:     &js_escape(\$instcode_reserved);
 6668:     &js_escape(\$communities_reserved);
 6669:     &js_escape(\$placement_reserved);
 6670:     &js_escape(\$choose_again);
 6671:     $output = <<"ENDSCRIPT";
 6672: <script type="text/javascript">
 6673: // <![CDATA[
 6674: function reorderCats(form,parent,item,idx) {
 6675:     var changedVal;
 6676: $jstext
 6677:     var newpos = 'addcategory_pos';
 6678:     if (parent == '') {
 6679:         var has_instcode = 0;
 6680:         var maxtop = categories[idx].length;
 6681:         for (var j=0; j<maxtop; j++) {
 6682:             if (categories[idx][j] == 'instcode::0') {
 6683:                 has_instcode == 1;
 6684:             }
 6685:         }
 6686:         if (has_instcode == 0) {
 6687:             categories[idx][maxtop] = 'instcode_pos';
 6688:         }
 6689:     } else {
 6690:         newpos += '_'+parent;
 6691:     }
 6692:     var maxh = 1 + categories[idx].length;
 6693:     var current = new Array;
 6694:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6695:     if (item == newpos) {
 6696:         changedVal = newitemVal;
 6697:     } else {
 6698:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 6699:         current[newitemVal] = newpos;
 6700:     }
 6701:     for (var i=0; i<categories[idx].length; i++) {
 6702:         var elementName = categories[idx][i];
 6703:         if (elementName != item) {
 6704:             if (form.elements[elementName]) {
 6705:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 6706:                 current[currVal] = elementName;
 6707:             }
 6708:         }
 6709:     }
 6710:     var oldVal;
 6711:     for (var j=0; j<maxh; j++) {
 6712:         if (current[j] == undefined) {
 6713:             oldVal = j;
 6714:         }
 6715:     }
 6716:     if (oldVal < changedVal) {
 6717:         for (var k=oldVal+1; k<=changedVal ; k++) {
 6718:            var elementName = current[k];
 6719:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 6720:         }
 6721:     } else {
 6722:         for (var k=changedVal; k<oldVal; k++) {
 6723:             var elementName = current[k];
 6724:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 6725:         }
 6726:     }
 6727:     return;
 6728: }
 6729: 
 6730: function categoryCheck(form) {
 6731:     if (form.elements['addcategory_name'].value == 'instcode') {
 6732:         alert('$instcode_reserved\\n$choose_again');
 6733:         return false;
 6734:     }
 6735:     if (form.elements['addcategory_name'].value == 'communities') {
 6736:         alert('$communities_reserved\\n$choose_again');
 6737:         return false;
 6738:     }
 6739:     if (form.elements['addcategory_name'].value == 'placement') {
 6740:         alert('$placement_reserved\\n$choose_again');
 6741:         return false;
 6742:     }
 6743:     return true;
 6744: }
 6745: 
 6746: // ]]>
 6747: </script>
 6748: 
 6749: ENDSCRIPT
 6750:     return $output;
 6751: }
 6752: 
 6753: sub initialize_categories {
 6754:     my ($itemcount) = @_;
 6755:     my ($datatable,$css_class,$chgstr);
 6756:     my %default_names = (
 6757:                       instcode    => 'Official courses (with institutional codes)',
 6758:                       communities => 'Communities',
 6759:                       placement   => 'Placement Tests',
 6760:                         );
 6761:     my $select0 = ' selected="selected"';
 6762:     my $select1 = '';
 6763:     foreach my $default ('instcode','communities','placement') {
 6764:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6765:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 6766:         if (($default eq 'communities') || ($default eq 'placement')) {
 6767:             $select1 = $select0;
 6768:             $select0 = '';
 6769:         }
 6770:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6771:                      .'<select name="'.$default.'_pos">'
 6772:                      .'<option value="0"'.$select0.'>1</option>'
 6773:                      .'<option value="1"'.$select1.'>2</option>'
 6774:                      .'<option value="2">3</option></select>&nbsp;'
 6775:                      .$default_names{$default}
 6776:                      .'</span></td><td><span class="LC_nobreak">'
 6777:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 6778:                      .&mt('Display').'</label>&nbsp;<label>'
 6779:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 6780:                  .'</label></span></td></tr>';
 6781:         $itemcount ++;
 6782:     }
 6783:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6784:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 6785:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6786:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 6787:                   .'<option value="0">1</option>'
 6788:                   .'<option value="1">2</option>'
 6789:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 6790:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 6791:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 6792:     return $datatable;
 6793: }
 6794: 
 6795: sub build_category_rows {
 6796:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 6797:     my ($text,$name,$item,$chgstr);
 6798:     if (ref($cats) eq 'ARRAY') {
 6799:         my $maxdepth = scalar(@{$cats});
 6800:         if (ref($cats->[$depth]) eq 'HASH') {
 6801:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 6802:                 my $numchildren = @{$cats->[$depth]{$parent}};
 6803:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6804:                 $text .= '<td><table class="LC_data_table">';
 6805:                 my ($idxnum,$parent_name,$parent_item);
 6806:                 my $higher = $depth - 1;
 6807:                 if ($higher == 0) {
 6808:                     $parent_name = &escape($parent).'::'.$higher;
 6809:                 } else {
 6810:                     if (ref($path) eq 'ARRAY') {
 6811:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 6812:                     }
 6813:                 }
 6814:                 $parent_item = 'addcategory_pos_'.$parent_name;
 6815:                 for (my $j=0; $j<=$numchildren; $j++) {
 6816:                     if ($j < $numchildren) {
 6817:                         $name = $cats->[$depth]{$parent}[$j];
 6818:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 6819:                         $idxnum = $idx->{$item};
 6820:                     } else {
 6821:                         $name = $parent_name;
 6822:                         $item = $parent_item;
 6823:                     }
 6824:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 6825:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 6826:                     for (my $i=0; $i<=$numchildren; $i++) {
 6827:                         my $vpos = $i+1;
 6828:                         my $selstr;
 6829:                         if ($j == $i) {
 6830:                             $selstr = ' selected="selected" ';
 6831:                         }
 6832:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 6833:                     }
 6834:                     $text .= '</select>&nbsp;';
 6835:                     if ($j < $numchildren) {
 6836:                         my $deeper = $depth+1;
 6837:                         $text .= $name.'&nbsp;'
 6838:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 6839:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 6840:                         if(ref($path) eq 'ARRAY') {
 6841:                             push(@{$path},$name);
 6842:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 6843:                             pop(@{$path});
 6844:                         }
 6845:                     } else {
 6846:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 6847:                         if ($j == $numchildren) {
 6848:                             $text .= $name;
 6849:                         } else {
 6850:                             $text .= $item;
 6851:                         }
 6852:                         $text .= '" value="" />';
 6853:                     }
 6854:                     $text .= '</td></tr>';
 6855:                 }
 6856:                 $text .= '</table></td>';
 6857:             } else {
 6858:                 my $higher = $depth-1;
 6859:                 if ($higher == 0) {
 6860:                     $name = &escape($parent).'::'.$higher;
 6861:                 } else {
 6862:                     if (ref($path) eq 'ARRAY') {
 6863:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 6864:                     }
 6865:                 }
 6866:                 my $colspan;
 6867:                 if ($parent ne 'instcode') {
 6868:                     $colspan = $maxdepth - $depth - 1;
 6869:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 6870:                 }
 6871:             }
 6872:         }
 6873:     }
 6874:     return $text;
 6875: }
 6876: 
 6877: sub modifiable_userdata_row {
 6878:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 6879:     my ($role,$rolename,$statustype);
 6880:     $role = $item;
 6881:     if ($context eq 'cancreate') {
 6882:         if ($item =~ /^emailusername_(.+)$/) {
 6883:             $statustype = $1;
 6884:             $role = 'emailusername';
 6885:             if (ref($usertypes) eq 'HASH') {
 6886:                 if ($usertypes->{$statustype}) {
 6887:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 6888:                 } else {
 6889:                     $rolename = &mt('Data provided by user');
 6890:                 }
 6891:             }
 6892:         }
 6893:     } elsif ($context eq 'selfcreate') {
 6894:         if (ref($usertypes) eq 'HASH') {
 6895:             $rolename = $usertypes->{$role};
 6896:         } else {
 6897:             $rolename = $role;
 6898:         }
 6899:     } else {
 6900:         if ($role eq 'cr') {
 6901:             $rolename = &mt('Custom role');
 6902:         } else {
 6903:             $rolename = &Apache::lonnet::plaintext($role);
 6904:         }
 6905:     }
 6906:     my (@fields,%fieldtitles);
 6907:     if (ref($fieldsref) eq 'ARRAY') {
 6908:         @fields = @{$fieldsref};
 6909:     } else {
 6910:         @fields = ('lastname','firstname','middlename','generation',
 6911:                    'permanentemail','id');
 6912:     }
 6913:     if ((ref($titlesref) eq 'HASH')) {
 6914:         %fieldtitles = %{$titlesref};
 6915:     } else {
 6916:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 6917:     }
 6918:     my $output;
 6919:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 6920:     $output = '<tr '.$css_class.'>'.
 6921:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 6922:               '<td class="LC_left_item" colspan="2"><table>';
 6923:     my $rem;
 6924:     my %checks;
 6925:     if (ref($settings) eq 'HASH') {
 6926:         if (ref($settings->{$context}) eq 'HASH') {
 6927:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 6928:                 my $hashref = $settings->{$context}->{$role};
 6929:                 if ($role eq 'emailusername') {
 6930:                     if ($statustype) {
 6931:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 6932:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 6933:                             if (ref($hashref) eq 'HASH') { 
 6934:                                 foreach my $field (@fields) {
 6935:                                     if ($hashref->{$field}) {
 6936:                                         $checks{$field} = $hashref->{$field};
 6937:                                     }
 6938:                                 }
 6939:                             }
 6940:                         }
 6941:                     }
 6942:                 } else {
 6943:                     if (ref($hashref) eq 'HASH') {
 6944:                         foreach my $field (@fields) {
 6945:                             if ($hashref->{$field}) {
 6946:                                 $checks{$field} = ' checked="checked" ';
 6947:                             }
 6948:                         }
 6949:                     }
 6950:                 }
 6951:             }
 6952:         }
 6953:     }
 6954:      
 6955:     for (my $i=0; $i<@fields; $i++) {
 6956:         my $rem = $i%($numinrow);
 6957:         if ($rem == 0) {
 6958:             if ($i > 0) {
 6959:                 $output .= '</tr>';
 6960:             }
 6961:             $output .= '<tr>';
 6962:         }
 6963:         my $check = ' ';
 6964:         unless ($role eq 'emailusername') {
 6965:             if (exists($checks{$fields[$i]})) {
 6966:                 $check = $checks{$fields[$i]}
 6967:             } else {
 6968:                 if ($role eq 'st') {
 6969:                     if (ref($settings) ne 'HASH') {
 6970:                         $check = ' checked="checked" '; 
 6971:                     }
 6972:                 }
 6973:             }
 6974:         }
 6975:         $output .= '<td class="LC_left_item">'.
 6976:                    '<span class="LC_nobreak">';
 6977:         if ($role eq 'emailusername') {
 6978:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 6979:                 $checks{$fields[$i]} = 'omit';
 6980:             }
 6981:             foreach my $option ('required','optional','omit') {
 6982:                 my $checked='';
 6983:                 if ($checks{$fields[$i]} eq $option) {
 6984:                     $checked='checked="checked" ';
 6985:                 }
 6986:                 $output .= '<label>'.
 6987:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 6988:                            &mt($option).'</label>'.('&nbsp;' x2);
 6989:             }
 6990:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 6991:         } else {
 6992:             $output .= '<label>'.
 6993:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 6994:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 6995:                        '</label>';
 6996:         }
 6997:         $output .= '</span></td>';
 6998:         $rem = @fields%($numinrow);
 6999:     }
 7000:     my $colsleft = $numinrow - $rem;
 7001:     if ($colsleft > 1 ) {
 7002:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7003:                    '&nbsp;</td>';
 7004:     } elsif ($colsleft == 1) {
 7005:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7006:     }
 7007:     $output .= '</tr></table></td></tr>';
 7008:     return $output;
 7009: }
 7010: 
 7011: sub insttypes_row {
 7012:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 7013:     my %lt = &Apache::lonlocal::texthash (
 7014:                       cansearch => 'Users allowed to search',
 7015:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 7016:                       lockablenames => 'User preference to lock name',
 7017:              );
 7018:     my $showdom;
 7019:     if ($context eq 'cansearch') {
 7020:         $showdom = ' ('.$dom.')';
 7021:     }
 7022:     my $class = 'LC_left_item';
 7023:     if ($context eq 'statustocreate') {
 7024:         $class = 'LC_right_item';
 7025:     }
 7026:     my $css_class = ' class="LC_odd_row"';
 7027:     if ($rownum ne '') { 
 7028:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 7029:     }
 7030:     my $output = '<tr'.$css_class.'>'.
 7031:                  '<td>'.$lt{$context}.$showdom.
 7032:                  '</td><td class="'.$class.'" colspan="2"><table>';
 7033:     my $rem;
 7034:     if (ref($types) eq 'ARRAY') {
 7035:         for (my $i=0; $i<@{$types}; $i++) {
 7036:             if (defined($usertypes->{$types->[$i]})) {
 7037:                 my $rem = $i%($numinrow);
 7038:                 if ($rem == 0) {
 7039:                     if ($i > 0) {
 7040:                         $output .= '</tr>';
 7041:                     }
 7042:                     $output .= '<tr>';
 7043:                 }
 7044:                 my $check = ' ';
 7045:                 if (ref($settings) eq 'HASH') {
 7046:                     if (ref($settings->{$context}) eq 'ARRAY') {
 7047:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 7048:                             $check = ' checked="checked" ';
 7049:                         }
 7050:                     } elsif ($context eq 'statustocreate') {
 7051:                         $check = ' checked="checked" ';
 7052:                     }
 7053:                 }
 7054:                 $output .= '<td class="LC_left_item">'.
 7055:                            '<span class="LC_nobreak"><label>'.
 7056:                            '<input type="checkbox" name="'.$context.'" '.
 7057:                            'value="'.$types->[$i].'"'.$check.'/>'.
 7058:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 7059:             }
 7060:         }
 7061:         $rem = @{$types}%($numinrow);
 7062:     }
 7063:     my $colsleft = $numinrow - $rem;
 7064:     if (($rem == 0) && (@{$types} > 0)) {
 7065:         $output .= '<tr>';
 7066:     }
 7067:     if ($colsleft > 1) {
 7068:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 7069:     } else {
 7070:         $output .= '<td class="LC_left_item">';
 7071:     }
 7072:     my $defcheck = ' ';
 7073:     if (ref($settings) eq 'HASH') {  
 7074:         if (ref($settings->{$context}) eq 'ARRAY') {
 7075:             if (grep(/^default$/,@{$settings->{$context}})) {
 7076:                 $defcheck = ' checked="checked" ';
 7077:             }
 7078:         } elsif ($context eq 'statustocreate') {
 7079:             $defcheck = ' checked="checked" ';
 7080:         }
 7081:     }
 7082:     $output .= '<span class="LC_nobreak"><label>'.
 7083:                '<input type="checkbox" name="'.$context.'" '.
 7084:                'value="default"'.$defcheck.'/>'.
 7085:                $othertitle.'</label></span></td>'.
 7086:                '</tr></table></td></tr>';
 7087:     return $output;
 7088: }
 7089: 
 7090: sub sorted_searchtitles {
 7091:     my %searchtitles = &Apache::lonlocal::texthash(
 7092:                          'uname' => 'username',
 7093:                          'lastname' => 'last name',
 7094:                          'lastfirst' => 'last name, first name',
 7095:                      );
 7096:     my @titleorder = ('uname','lastname','lastfirst');
 7097:     return (\%searchtitles,\@titleorder);
 7098: }
 7099: 
 7100: sub sorted_searchtypes {
 7101:     my %srchtypes_desc = (
 7102:                            exact    => 'is exact match',
 7103:                            contains => 'contains ..',
 7104:                            begins   => 'begins with ..',
 7105:                          );
 7106:     my @srchtypeorder = ('exact','begins','contains');
 7107:     return (\%srchtypes_desc,\@srchtypeorder);
 7108: }
 7109: 
 7110: sub usertype_update_row {
 7111:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 7112:     my $datatable;
 7113:     my $numinrow = 4;
 7114:     foreach my $type (@{$types}) {
 7115:         if (defined($usertypes->{$type})) {
 7116:             $$rownums ++;
 7117:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 7118:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 7119:                           '</td><td class="LC_left_item"><table>';
 7120:             for (my $i=0; $i<@{$fields}; $i++) {
 7121:                 my $rem = $i%($numinrow);
 7122:                 if ($rem == 0) {
 7123:                     if ($i > 0) {
 7124:                         $datatable .= '</tr>';
 7125:                     }
 7126:                     $datatable .= '<tr>';
 7127:                 }
 7128:                 my $check = ' ';
 7129:                 if (ref($settings) eq 'HASH') {
 7130:                     if (ref($settings->{'fields'}) eq 'HASH') {
 7131:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 7132:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 7133:                                 $check = ' checked="checked" ';
 7134:                             }
 7135:                         }
 7136:                     }
 7137:                 }
 7138: 
 7139:                 if ($i == @{$fields}-1) {
 7140:                     my $colsleft = $numinrow - $rem;
 7141:                     if ($colsleft > 1) {
 7142:                         $datatable .= '<td colspan="'.$colsleft.'">';
 7143:                     } else {
 7144:                         $datatable .= '<td>';
 7145:                     }
 7146:                 } else {
 7147:                     $datatable .= '<td>';
 7148:                 }
 7149:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7150:                               '<input type="checkbox" name="updateable_'.$type.
 7151:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 7152:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 7153:             }
 7154:             $datatable .= '</tr></table></td></tr>';
 7155:         }
 7156:     }
 7157:     return $datatable;
 7158: }
 7159: 
 7160: sub modify_login {
 7161:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 7162:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 7163:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 7164:     %title = ( coursecatalog => 'Display course catalog',
 7165:                adminmail => 'Display administrator E-mail address',
 7166:                helpdesk  => 'Display "Contact Helpdesk" link',
 7167:                newuser => 'Link for visitors to create a user account',
 7168:                loginheader => 'Log-in box header');
 7169:     @offon = ('off','on');
 7170:     if (ref($domconfig{login}) eq 'HASH') {
 7171:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 7172:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 7173:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 7174:             }
 7175:         }
 7176:     }
 7177:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 7178:                                            \%domconfig,\%loginhash);
 7179:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7180:     foreach my $item (@toggles) {
 7181:         $loginhash{login}{$item} = $env{'form.'.$item};
 7182:     }
 7183:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 7184:     if (ref($colchanges{'login'}) eq 'HASH') {  
 7185:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 7186:                                          \%loginhash);
 7187:     }
 7188: 
 7189:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7190:     my %domservers = &Apache::lonnet::get_servers($dom);
 7191:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 7192:     if (keys(%servers) > 1) {
 7193:         foreach my $lonhost (keys(%servers)) {
 7194:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 7195:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 7196:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 7197:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 7198:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 7199:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7200:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7201:                         $changes{'loginvia'}{$lonhost} = 1;
 7202:                     } else {
 7203:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 7204:                         $changes{'loginvia'}{$lonhost} = 1;
 7205:                     }
 7206:                 } else {
 7207:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7208:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7209:                         $changes{'loginvia'}{$lonhost} = 1;
 7210:                     }
 7211:                 }
 7212:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 7213:                     foreach my $item (@loginvia_attribs) {
 7214:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 7215:                     }
 7216:                 } else {
 7217:                     foreach my $item (@loginvia_attribs) {
 7218:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7219:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7220:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 7221:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7222:                                 $new = '/';
 7223:                             }
 7224:                         }
 7225:                         if (($item eq 'custompath') && 
 7226:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7227:                             $new = '';
 7228:                         }
 7229:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 7230:                             $changes{'loginvia'}{$lonhost} = 1;
 7231:                         }
 7232:                         if ($item eq 'exempt') {
 7233:                             $new = &check_exempt_addresses($new);
 7234:                         }
 7235:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7236:                     }
 7237:                 }
 7238:             } else {
 7239:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7240:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7241:                     $changes{'loginvia'}{$lonhost} = 1;
 7242:                     foreach my $item (@loginvia_attribs) {
 7243:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7244:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7245:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7246:                                 $new = '/';
 7247:                             }
 7248:                         }
 7249:                         if (($item eq 'custompath') && 
 7250:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7251:                             $new = '';
 7252:                         }
 7253:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7254:                     }
 7255:                 }
 7256:             }
 7257:         }
 7258:     }
 7259: 
 7260:     my $servadm = $r->dir_config('lonAdmEMail');
 7261:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 7262:     if (ref($domconfig{'login'}) eq 'HASH') {
 7263:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 7264:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 7265:                 if ($lang eq 'nolang') {
 7266:                     push(@currlangs,$lang);
 7267:                 } elsif (defined($langchoices{$lang})) {
 7268:                     push(@currlangs,$lang);
 7269:                 } else {
 7270:                     next;
 7271:                 }
 7272:             }
 7273:         }
 7274:     }
 7275:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 7276:     if (@currlangs > 0) {
 7277:         foreach my $lang (@currlangs) {
 7278:             if (grep(/^\Q$lang\E$/,@delurls)) {
 7279:                 $changes{'helpurl'}{$lang} = 1;
 7280:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 7281:                 $changes{'helpurl'}{$lang} = 1;
 7282:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 7283:                 push(@newlangs,$lang);
 7284:             } else {
 7285:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7286:             }
 7287:         }
 7288:     }
 7289:     unless (grep(/^nolang$/,@currlangs)) {
 7290:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 7291:             $changes{'helpurl'}{'nolang'} = 1;
 7292:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 7293:             push(@newlangs,'nolang');
 7294:         }
 7295:     }
 7296:     if ($env{'form.loginhelpurl_add_lang'}) {
 7297:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 7298:             ($env{'form.loginhelpurl_add_file.filename'})) {
 7299:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 7300:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 7301:         }
 7302:     }
 7303:     if ((@newlangs > 0) || ($addedfile)) {
 7304:         my $error;
 7305:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7306:         if ($configuserok eq 'ok') {
 7307:             if ($switchserver) {
 7308:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 7309:             } elsif ($author_ok eq 'ok') {
 7310:                 my @allnew = @newlangs;
 7311:                 if ($addedfile ne '') {
 7312:                     push(@allnew,$addedfile);
 7313:                 }
 7314:                 foreach my $lang (@allnew) {
 7315:                     my $formelem = 'loginhelpurl_'.$lang;
 7316:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 7317:                         $formelem = 'loginhelpurl_add_file';
 7318:                     }
 7319:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7320:                                                                "help/$lang",'','',$newfile{$lang});
 7321:                     if ($result eq 'ok') {
 7322:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 7323:                         $changes{'helpurl'}{$lang} = 1;
 7324:                     } else {
 7325:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 7326:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7327:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 7328:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 7329:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7330:                         }
 7331:                     }
 7332:                 }
 7333:             } else {
 7334:                 $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);
 7335:             }
 7336:         } else {
 7337:             $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);
 7338:         }
 7339:         if ($error) {
 7340:             &Apache::lonnet::logthis($error);
 7341:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7342:         }
 7343:     }
 7344: 
 7345:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 7346:     if (ref($domconfig{'login'}) eq 'HASH') {
 7347:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 7348:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 7349:                 if ($domservers{$lonhost}) {
 7350:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7351:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 7352:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 7353:                     }
 7354:                 }
 7355:             }
 7356:         }
 7357:     }
 7358:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 7359:     foreach my $lonhost (sort(keys(%domservers))) {
 7360:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7361:             $changes{'headtag'}{$lonhost} = 1;
 7362:         } else {
 7363:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 7364:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 7365:             }
 7366:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 7367:                 push(@newhosts,$lonhost);
 7368:             } elsif ($currheadtagurls{$lonhost}) {
 7369:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 7370:                 if ($currexempt{$lonhost}) {
 7371:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) { 
 7372:                         $changes{'headtag'}{$lonhost} = 1;
 7373:                     }
 7374:                 } elsif ($possexempt{$lonhost}) {
 7375:                     $changes{'headtag'}{$lonhost} = 1;
 7376:                 }
 7377:                 if ($possexempt{$lonhost}) {
 7378:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7379:                 }
 7380:             }
 7381:         }
 7382:     }
 7383:     if (@newhosts) {
 7384:         my $error;
 7385:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7386:         if ($configuserok eq 'ok') {
 7387:             if ($switchserver) {
 7388:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 7389:             } elsif ($author_ok eq 'ok') {
 7390:                 foreach my $lonhost (@newhosts) {
 7391:                     my $formelem = 'loginheadtag_'.$lonhost;
 7392:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7393:                                                                           "login/headtag/$lonhost",'','',
 7394:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 7395:                     if ($result eq 'ok') {
 7396:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 7397:                         $changes{'headtag'}{$lonhost} = 1;
 7398:                         if ($possexempt{$lonhost}) {
 7399:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7400:                         }
 7401:                     } else {
 7402:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 7403:                                            $newheadtagurls{$lonhost},$result);
 7404:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7405:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 7406:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 7407:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 7408:                         }
 7409:                     }
 7410:                 }
 7411:             } else {
 7412:                 $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);
 7413:             }
 7414:         } else {
 7415:             $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);
 7416:         }
 7417:         if ($error) {
 7418:             &Apache::lonnet::logthis($error);
 7419:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7420:         }
 7421:     }
 7422:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 7423: 
 7424:     my $defaulthelpfile = '/adm/loginproblems.html';
 7425:     my $defaulttext = &mt('Default in use');
 7426: 
 7427:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 7428:                                              $dom);
 7429:     if ($putresult eq 'ok') {
 7430:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7431:         my %defaultchecked = (
 7432:                     'coursecatalog' => 'on',
 7433:                     'helpdesk'      => 'on',
 7434:                     'adminmail'     => 'off',
 7435:                     'newuser'       => 'off',
 7436:         );
 7437:         if (ref($domconfig{'login'}) eq 'HASH') {
 7438:             foreach my $item (@toggles) {
 7439:                 if ($defaultchecked{$item} eq 'on') { 
 7440:                     if (($domconfig{'login'}{$item} eq '0') &&
 7441:                         ($env{'form.'.$item} eq '1')) {
 7442:                         $changes{$item} = 1;
 7443:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7444:                               $domconfig{'login'}{$item} eq '1') &&
 7445:                              ($env{'form.'.$item} eq '0')) {
 7446:                         $changes{$item} = 1;
 7447:                     }
 7448:                 } elsif ($defaultchecked{$item} eq 'off') {
 7449:                     if (($domconfig{'login'}{$item} eq '1') &&
 7450:                         ($env{'form.'.$item} eq '0')) {
 7451:                         $changes{$item} = 1;
 7452:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7453:                               $domconfig{'login'}{$item} eq '0') &&
 7454:                              ($env{'form.'.$item} eq '1')) {
 7455:                         $changes{$item} = 1;
 7456:                     }
 7457:                 }
 7458:             }
 7459:         }
 7460:         if (keys(%changes) > 0 || $colchgtext) {
 7461:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7462:             if (ref($lastactref) eq 'HASH') {
 7463:                 $lastactref->{'domainconfig'} = 1;
 7464:             }
 7465:             $resulttext = &mt('Changes made:').'<ul>';
 7466:             foreach my $item (sort(keys(%changes))) {
 7467:                 if ($item eq 'loginvia') {
 7468:                     if (ref($changes{$item}) eq 'HASH') {
 7469:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 7470:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7471:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 7472:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 7473:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 7474:                                     $protocol = 'http' if ($protocol ne 'https');
 7475:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 7476: 
 7477:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 7478:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 7479:                                     } else {
 7480:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 7481:                                     }
 7482:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 7483:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 7484:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 7485:                                     }
 7486:                                     $resulttext .= '</li>';
 7487:                                 } else {
 7488:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 7489:                                 }
 7490:                             } else {
 7491:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 7492:                             }
 7493:                         }
 7494:                         $resulttext .= '</ul></li>';
 7495:                     }
 7496:                 } elsif ($item eq 'helpurl') {
 7497:                     if (ref($changes{$item}) eq 'HASH') {
 7498:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 7499:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 7500:                                 my ($chg,$link);
 7501:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 7502:                                 if ($lang eq 'nolang') {
 7503:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 7504:                                 } else {
 7505:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 7506:                                 }
 7507:                                 $resulttext .= '<li>'.$chg.'</li>';
 7508:                             } else {
 7509:                                 my $chg;
 7510:                                 if ($lang eq 'nolang') {
 7511:                                     $chg = &mt('custom log-in help file for no preferred language');
 7512:                                 } else {
 7513:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 7514:                                 }
 7515:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 7516:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 7517:                                                       '?inhibitmenu=yes',$chg,600,500).
 7518:                                                '</li>';
 7519:                             }
 7520:                         }
 7521:                     }
 7522:                 } elsif ($item eq 'headtag') {
 7523:                     if (ref($changes{$item}) eq 'HASH') {
 7524:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7525:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7526:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 7527:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7528:                                 $resulttext .= '<li><a href="'.
 7529:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 7530:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 7531:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 7532:                                 if ($possexempt{$lonhost}) {
 7533:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 7534:                                 } else {
 7535:                                     $resulttext .= &mt('included for any client IP');
 7536:                                 }
 7537:                                 $resulttext .= '</li>';
 7538:                             }
 7539:                         }
 7540:                     }
 7541:                 } elsif ($item eq 'captcha') {
 7542:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7543:                         my $chgtxt;
 7544:                         if ($loginhash{'login'}{$item} eq 'notused') {
 7545:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 7546:                         } else {
 7547:                             my %captchas = &captcha_phrases();
 7548:                             if ($captchas{$loginhash{'login'}{$item}}) {
 7549:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 7550:                             } else {
 7551:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 7552:                             }
 7553:                         }
 7554:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7555:                     }
 7556:                 } elsif ($item eq 'recaptchakeys') {
 7557:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7558:                         my ($privkey,$pubkey);
 7559:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 7560:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 7561:                             $privkey = $loginhash{'login'}{$item}{'private'};
 7562:                         }
 7563:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 7564:                         if (!$pubkey) {
 7565:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 7566:                         } else {
 7567:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 7568:                         }
 7569:                         if (!$privkey) {
 7570:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 7571:                         } else {
 7572:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 7573:                         }
 7574:                         $chgtxt .= '</ul>';
 7575:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7576:                     }
 7577:                 } elsif ($item eq 'recaptchaversion') {
 7578:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7579:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 7580:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 7581:                                            '</li>';
 7582:                         }
 7583:                     }
 7584:                 } else {
 7585:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 7586:                 }
 7587:             }
 7588:             $resulttext .= $colchgtext.'</ul>';
 7589:         } else {
 7590:             $resulttext = &mt('No changes made to log-in page settings');
 7591:         }
 7592:     } else {
 7593:         $resulttext = '<span class="LC_error">'.
 7594: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7595:     }
 7596:     if ($errors) {
 7597:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7598:                        $errors.'</ul>';
 7599:     }
 7600:     return $resulttext;
 7601: }
 7602: 
 7603: 
 7604: sub check_exempt_addresses {
 7605:     my ($iplist) = @_;
 7606:     $iplist =~ s/^\s+//;
 7607:     $iplist =~ s/\s+$//;
 7608:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 7609:     my (@okips,$new);
 7610:     foreach my $ip (@poss_ips) {
 7611:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 7612:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 7613:                 push(@okips,$ip);
 7614:             }
 7615:         }
 7616:     }
 7617:     if (@okips > 0) {
 7618:         $new = join(',',@okips);
 7619:     } else {
 7620:         $new = '';
 7621:     }
 7622:     return $new;
 7623: }
 7624: 
 7625: sub color_font_choices {
 7626:     my %choices =
 7627:         &Apache::lonlocal::texthash (
 7628:             img => "Header",
 7629:             bgs => "Background colors",
 7630:             links => "Link colors",
 7631:             images => "Images",
 7632:             font => "Font color",
 7633:             fontmenu => "Font menu",
 7634:             pgbg => "Page",
 7635:             tabbg => "Header",
 7636:             sidebg => "Border",
 7637:             link => "Link",
 7638:             alink => "Active link",
 7639:             vlink => "Visited link",
 7640:         );
 7641:     return %choices;
 7642: }
 7643: 
 7644: sub modify_rolecolors {
 7645:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 7646:     my ($resulttext,%rolehash);
 7647:     $rolehash{'rolecolors'} = {};
 7648:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 7649:         if ($domconfig{'rolecolors'} eq '') {
 7650:             $domconfig{'rolecolors'} = {};
 7651:         }
 7652:     }
 7653:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 7654:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 7655:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 7656:                                              $dom);
 7657:     if ($putresult eq 'ok') {
 7658:         if (keys(%changes) > 0) {
 7659:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7660:             if (ref($lastactref) eq 'HASH') {
 7661:                 $lastactref->{'domainconfig'} = 1;
 7662:             }
 7663:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 7664:                                              $rolehash{'rolecolors'});
 7665:         } else {
 7666:             $resulttext = &mt('No changes made to default color schemes');
 7667:         }
 7668:     } else {
 7669:         $resulttext = '<span class="LC_error">'.
 7670: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7671:     }
 7672:     if ($errors) {
 7673:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7674:                        $errors.'</ul>';
 7675:     }
 7676:     return $resulttext;
 7677: }
 7678: 
 7679: sub modify_colors {
 7680:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 7681:     my (%changes,%choices);
 7682:     my @bgs;
 7683:     my @links = ('link','alink','vlink');
 7684:     my @logintext;
 7685:     my @images;
 7686:     my $servadm = $r->dir_config('lonAdmEMail');
 7687:     my $errors;
 7688:     my %defaults;
 7689:     foreach my $role (@{$roles}) {
 7690:         if ($role eq 'login') {
 7691:             %choices = &login_choices();
 7692:             @logintext = ('textcol','bgcol');
 7693:         } else {
 7694:             %choices = &color_font_choices();
 7695:         }
 7696:         if ($role eq 'login') {
 7697:             @images = ('img','logo','domlogo','login');
 7698:             @bgs = ('pgbg','mainbg','sidebg');
 7699:         } else {
 7700:             @images = ('img');
 7701:             @bgs = ('pgbg','tabbg','sidebg');
 7702:         }
 7703:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 7704:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 7705:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 7706:         }
 7707:         if ($role eq 'login') {
 7708:             foreach my $item (@logintext) {
 7709:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7710:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7711:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7712:                 }
 7713:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 7714:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7715:                 }
 7716:             }
 7717:         } else {
 7718:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 7719:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 7720:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 7721:             }
 7722:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 7723:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 7724:             }
 7725:         }
 7726:         foreach my $item (@bgs) {
 7727:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7728:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7729:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7730:             }
 7731:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 7732:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7733:             }
 7734:         }
 7735:         foreach my $item (@links) {
 7736:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7737:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7738:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7739:             }
 7740:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 7741:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7742:             }
 7743:         }
 7744:         my ($configuserok,$author_ok,$switchserver) = 
 7745:             &config_check($dom,$confname,$servadm);
 7746:         my ($width,$height) = &thumb_dimensions();
 7747:         if (ref($domconfig->{$role}) ne 'HASH') {
 7748:             $domconfig->{$role} = {};
 7749:         }
 7750:         foreach my $img (@images) {
 7751:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 7752:                 if (defined($env{'form.login_showlogo_'.$img})) {
 7753:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 7754:                 } else { 
 7755:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 7756:                 }
 7757:             } 
 7758: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 7759: 		 && !defined($domconfig->{$role}{$img})
 7760: 		 && !$env{'form.'.$role.'_del_'.$img}
 7761: 		 && $env{'form.'.$role.'_import_'.$img}) {
 7762: 		# import the old configured image from the .tab setting
 7763: 		# if they haven't provided a new one 
 7764: 		$domconfig->{$role}{$img} = 
 7765: 		    $env{'form.'.$role.'_import_'.$img};
 7766: 	    }
 7767:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 7768:                 my $error;
 7769:                 if ($configuserok eq 'ok') {
 7770:                     if ($switchserver) {
 7771:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 7772:                     } else {
 7773:                         if ($author_ok eq 'ok') {
 7774:                             my ($result,$logourl) = 
 7775:                                 &publishlogo($r,'upload',$role.'_'.$img,
 7776:                                            $dom,$confname,$img,$width,$height);
 7777:                             if ($result eq 'ok') {
 7778:                                 $confhash->{$role}{$img} = $logourl;
 7779:                                 $changes{$role}{'images'}{$img} = 1;
 7780:                             } else {
 7781:                                 $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);
 7782:                             }
 7783:                         } else {
 7784:                             $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);
 7785:                         }
 7786:                     }
 7787:                 } else {
 7788:                     $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);
 7789:                 }
 7790:                 if ($error) {
 7791:                     &Apache::lonnet::logthis($error);
 7792:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7793:                 }
 7794:             } elsif ($domconfig->{$role}{$img} ne '') {
 7795:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 7796:                     my $error;
 7797:                     if ($configuserok eq 'ok') {
 7798: # is confname an author?
 7799:                         if ($switchserver eq '') {
 7800:                             if ($author_ok eq 'ok') {
 7801:                                 my ($result,$logourl) = 
 7802:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 7803:                                             $dom,$confname,$img,$width,$height);
 7804:                                 if ($result eq 'ok') {
 7805:                                     $confhash->{$role}{$img} = $logourl;
 7806: 				    $changes{$role}{'images'}{$img} = 1;
 7807:                                 }
 7808:                             }
 7809:                         }
 7810:                     }
 7811:                 }
 7812:             }
 7813:         }
 7814:         if (ref($domconfig) eq 'HASH') {
 7815:             if (ref($domconfig->{$role}) eq 'HASH') {
 7816:                 foreach my $img (@images) {
 7817:                     if ($domconfig->{$role}{$img} ne '') {
 7818:                         if ($env{'form.'.$role.'_del_'.$img}) {
 7819:                             $confhash->{$role}{$img} = '';
 7820:                             $changes{$role}{'images'}{$img} = 1;
 7821:                         } else {
 7822:                             if ($confhash->{$role}{$img} eq '') {
 7823:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 7824:                             }
 7825:                         }
 7826:                     } else {
 7827:                         if ($env{'form.'.$role.'_del_'.$img}) {
 7828:                             $confhash->{$role}{$img} = '';
 7829:                             $changes{$role}{'images'}{$img} = 1;
 7830:                         } 
 7831:                     }
 7832:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 7833:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 7834:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 7835:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 7836:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 7837:                             }
 7838:                         } else {
 7839:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 7840:                                 $changes{$role}{'showlogo'}{$img} = 1;
 7841:                             }
 7842:                         }
 7843:                     }
 7844:                 }
 7845:                 if ($domconfig->{$role}{'font'} ne '') {
 7846:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 7847:                         $changes{$role}{'font'} = 1;
 7848:                     }
 7849:                 } else {
 7850:                     if ($confhash->{$role}{'font'}) {
 7851:                         $changes{$role}{'font'} = 1;
 7852:                     }
 7853:                 }
 7854:                 if ($role ne 'login') {
 7855:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 7856:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 7857:                             $changes{$role}{'fontmenu'} = 1;
 7858:                         }
 7859:                     } else {
 7860:                         if ($confhash->{$role}{'fontmenu'}) {
 7861:                             $changes{$role}{'fontmenu'} = 1;
 7862:                         }
 7863:                     }
 7864:                 }
 7865:                 foreach my $item (@bgs) {
 7866:                     if ($domconfig->{$role}{$item} ne '') {
 7867:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 7868:                             $changes{$role}{'bgs'}{$item} = 1;
 7869:                         } 
 7870:                     } else {
 7871:                         if ($confhash->{$role}{$item}) {
 7872:                             $changes{$role}{'bgs'}{$item} = 1;
 7873:                         }
 7874:                     }
 7875:                 }
 7876:                 foreach my $item (@links) {
 7877:                     if ($domconfig->{$role}{$item} ne '') {
 7878:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 7879:                             $changes{$role}{'links'}{$item} = 1;
 7880:                         }
 7881:                     } else {
 7882:                         if ($confhash->{$role}{$item}) {
 7883:                             $changes{$role}{'links'}{$item} = 1;
 7884:                         }
 7885:                     }
 7886:                 }
 7887:                 foreach my $item (@logintext) {
 7888:                     if ($domconfig->{$role}{$item} ne '') {
 7889:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 7890:                             $changes{$role}{'logintext'}{$item} = 1;
 7891:                         }
 7892:                     } else {
 7893:                         if ($confhash->{$role}{$item}) {
 7894:                             $changes{$role}{'logintext'}{$item} = 1;
 7895:                         }
 7896:                     }
 7897:                 }
 7898:             } else {
 7899:                 &default_change_checker($role,\@images,\@links,\@bgs,
 7900:                                         \@logintext,$confhash,\%changes); 
 7901:             }
 7902:         } else {
 7903:             &default_change_checker($role,\@images,\@links,\@bgs,
 7904:                                     \@logintext,$confhash,\%changes); 
 7905:         }
 7906:     }
 7907:     return ($errors,%changes);
 7908: }
 7909: 
 7910: sub config_check {
 7911:     my ($dom,$confname,$servadm) = @_;
 7912:     my ($configuserok,$author_ok,$switchserver,%currroles);
 7913:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 7914:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 7915:                                                    $confname,$servadm);
 7916:     if ($configuserok eq 'ok') {
 7917:         $switchserver = &check_switchserver($dom,$confname);
 7918:         if ($switchserver eq '') {
 7919:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 7920:         }
 7921:     }
 7922:     return ($configuserok,$author_ok,$switchserver);
 7923: }
 7924: 
 7925: sub default_change_checker {
 7926:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 7927:     foreach my $item (@{$links}) {
 7928:         if ($confhash->{$role}{$item}) {
 7929:             $changes->{$role}{'links'}{$item} = 1;
 7930:         }
 7931:     }
 7932:     foreach my $item (@{$bgs}) {
 7933:         if ($confhash->{$role}{$item}) {
 7934:             $changes->{$role}{'bgs'}{$item} = 1;
 7935:         }
 7936:     }
 7937:     foreach my $item (@{$logintext}) {
 7938:         if ($confhash->{$role}{$item}) {
 7939:             $changes->{$role}{'logintext'}{$item} = 1;
 7940:         }
 7941:     }
 7942:     foreach my $img (@{$images}) {
 7943:         if ($env{'form.'.$role.'_del_'.$img}) {
 7944:             $confhash->{$role}{$img} = '';
 7945:             $changes->{$role}{'images'}{$img} = 1;
 7946:         }
 7947:         if ($role eq 'login') {
 7948:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 7949:                 $changes->{$role}{'showlogo'}{$img} = 1;
 7950:             }
 7951:         }
 7952:     }
 7953:     if ($confhash->{$role}{'font'}) {
 7954:         $changes->{$role}{'font'} = 1;
 7955:     }
 7956: }
 7957: 
 7958: sub display_colorchgs {
 7959:     my ($dom,$changes,$roles,$confhash) = @_;
 7960:     my (%choices,$resulttext);
 7961:     if (!grep(/^login$/,@{$roles})) {
 7962:         $resulttext = &mt('Changes made:').'<br />';
 7963:     }
 7964:     foreach my $role (@{$roles}) {
 7965:         if ($role eq 'login') {
 7966:             %choices = &login_choices();
 7967:         } else {
 7968:             %choices = &color_font_choices();
 7969:         }
 7970:         if (ref($changes->{$role}) eq 'HASH') {
 7971:             if ($role ne 'login') {
 7972:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 7973:             }
 7974:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 7975:                 if ($role ne 'login') {
 7976:                     $resulttext .= '<ul>';
 7977:                 }
 7978:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 7979:                     if ($role ne 'login') {
 7980:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 7981:                     }
 7982:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 7983:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 7984:                             if ($confhash->{$role}{$key}{$item}) {
 7985:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 7986:                             } else {
 7987:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 7988:                             }
 7989:                         } elsif ($confhash->{$role}{$item} eq '') {
 7990:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 7991:                         } else {
 7992:                             my $newitem = $confhash->{$role}{$item};
 7993:                             if ($key eq 'images') {
 7994:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 7995:                             }
 7996:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 7997:                         }
 7998:                     }
 7999:                     if ($role ne 'login') {
 8000:                         $resulttext .= '</ul></li>';
 8001:                     }
 8002:                 } else {
 8003:                     if ($confhash->{$role}{$key} eq '') {
 8004:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 8005:                     } else {
 8006:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 8007:                     }
 8008:                 }
 8009:                 if ($role ne 'login') {
 8010:                     $resulttext .= '</ul>';
 8011:                 }
 8012:             }
 8013:         }
 8014:     }
 8015:     return $resulttext;
 8016: }
 8017: 
 8018: sub thumb_dimensions {
 8019:     return ('200','50');
 8020: }
 8021: 
 8022: sub check_dimensions {
 8023:     my ($inputfile) = @_;
 8024:     my ($fullwidth,$fullheight);
 8025:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 8026:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 8027:             my $imageinfo = <PIPE>;
 8028:             if (!close(PIPE)) {
 8029:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 8030:             }
 8031:             chomp($imageinfo);
 8032:             my ($fullsize) = 
 8033:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 8034:             if ($fullsize) {
 8035:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 8036:             }
 8037:         }
 8038:     }
 8039:     return ($fullwidth,$fullheight);
 8040: }
 8041: 
 8042: sub check_configuser {
 8043:     my ($uhome,$dom,$confname,$servadm) = @_;
 8044:     my ($configuserok,%currroles);
 8045:     if ($uhome eq 'no_host') {
 8046:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 8047:         my $configpass = &LONCAPA::Enrollment::create_password();
 8048:         $configuserok = 
 8049:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 8050:                              $configpass,'','','','','',undef,$servadm);
 8051:     } else {
 8052:         $configuserok = 'ok';
 8053:         %currroles = 
 8054:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 8055:     }
 8056:     return ($configuserok,%currroles);
 8057: }
 8058: 
 8059: sub check_authorstatus {
 8060:     my ($dom,$confname,%currroles) = @_;
 8061:     my $author_ok;
 8062:     if (!$currroles{':'.$dom.':au'}) {
 8063:         my $start = time;
 8064:         my $end = 0;
 8065:         $author_ok = 
 8066:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 8067:                                         'au',$end,$start,'','','domconfig');
 8068:     } else {
 8069:         $author_ok = 'ok';
 8070:     }
 8071:     return $author_ok;
 8072: }
 8073: 
 8074: sub publishlogo {
 8075:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 8076:     my ($output,$fname,$logourl,$madethumb);
 8077:     if ($action eq 'upload') {
 8078:         $fname=$env{'form.'.$formname.'.filename'};
 8079:         chop($env{'form.'.$formname});
 8080:     } else {
 8081:         ($fname) = ($formname =~ /([^\/]+)$/);
 8082:     }
 8083:     if ($savefileas ne '') {
 8084:         $fname = $savefileas;
 8085:     }
 8086:     $fname=&Apache::lonnet::clean_filename($fname);
 8087: # See if there is anything left
 8088:     unless ($fname) { return ('error: no uploaded file'); }
 8089:     $fname="$subdir/$fname";
 8090:     my $docroot=$r->dir_config('lonDocRoot');
 8091:     my $filepath="$docroot/priv";
 8092:     my $relpath = "$dom/$confname";
 8093:     my ($fnamepath,$file,$fetchthumb);
 8094:     $file=$fname;
 8095:     if ($fname=~m|/|) {
 8096:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 8097:     }
 8098:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 8099:     my $count;
 8100:     for ($count=5;$count<=$#parts;$count++) {
 8101:         $filepath.="/$parts[$count]";
 8102:         if ((-e $filepath)!=1) {
 8103:             mkdir($filepath,02770);
 8104:         }
 8105:     }
 8106:     # Check for bad extension and disallow upload
 8107:     if ($file=~/\.(\w+)$/ &&
 8108:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 8109:         $output = 
 8110:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 8111:     } elsif ($file=~/\.(\w+)$/ &&
 8112:         !defined(&Apache::loncommon::fileembstyle($1))) {
 8113:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 8114:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 8115:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 8116:     } elsif (-d "$filepath/$file") {
 8117:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 8118:     } else {
 8119:         my $source = $filepath.'/'.$file;
 8120:         my $logfile;
 8121:         if (!open($logfile,">>$source".'.log')) {
 8122:             return (&mt('No write permission to Authoring Space'));
 8123:         }
 8124:         print $logfile
 8125: "\n================= Publish ".localtime()." ================\n".
 8126: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 8127: # Save the file
 8128:         if (!open(FH,'>'.$source)) {
 8129:             &Apache::lonnet::logthis('Failed to create '.$source);
 8130:             return (&mt('Failed to create file'));
 8131:         }
 8132:         if ($action eq 'upload') {
 8133:             if (!print FH ($env{'form.'.$formname})) {
 8134:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 8135:                 return (&mt('Failed to write file'));
 8136:             }
 8137:         } else {
 8138:             my $original = &Apache::lonnet::filelocation('',$formname);
 8139:             if(!copy($original,$source)) {
 8140:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 8141:                 return (&mt('Failed to write file'));
 8142:             }
 8143:         }
 8144:         close(FH);
 8145:         chmod(0660, $source); # Permissions to rw-rw---.
 8146: 
 8147:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 8148:         my $copyfile=$targetdir.'/'.$file;
 8149: 
 8150:         my @parts=split(/\//,$targetdir);
 8151:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 8152:         for (my $count=5;$count<=$#parts;$count++) {
 8153:             $path.="/$parts[$count]";
 8154:             if (!-e $path) {
 8155:                 print $logfile "\nCreating directory ".$path;
 8156:                 mkdir($path,02770);
 8157:             }
 8158:         }
 8159:         my $versionresult;
 8160:         if (-e $copyfile) {
 8161:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 8162:         } else {
 8163:             $versionresult = 'ok';
 8164:         }
 8165:         if ($versionresult eq 'ok') {
 8166:             if (copy($source,$copyfile)) {
 8167:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 8168:                 $output = 'ok';
 8169:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 8170:                 push(@{$modified_urls},[$copyfile,$source]);
 8171:                 my $metaoutput = 
 8172:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 8173:                 unless ($registered_cleanup) {
 8174:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8175:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8176:                     $registered_cleanup=1;
 8177:                 }
 8178:             } else {
 8179:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 8180:                 $output = &mt('Failed to copy file to RES space').", $!";
 8181:             }
 8182:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 8183:                 my $inputfile = $filepath.'/'.$file;
 8184:                 my $outfile = $filepath.'/'.'tn-'.$file;
 8185:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 8186:                 if ($fullwidth ne '' && $fullheight ne '') { 
 8187:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 8188:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 8189:                         system("convert -sample $thumbsize $inputfile $outfile");
 8190:                         chmod(0660, $filepath.'/tn-'.$file);
 8191:                         if (-e $outfile) {
 8192:                             my $copyfile=$targetdir.'/tn-'.$file;
 8193:                             if (copy($outfile,$copyfile)) {
 8194:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 8195:                                 my $thumb_metaoutput = 
 8196:                                     &write_metadata($dom,$confname,$formname,
 8197:                                                     $targetdir,'tn-'.$file,$logfile);
 8198:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 8199:                                 unless ($registered_cleanup) {
 8200:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8201:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8202:                                     $registered_cleanup=1;
 8203:                                 }
 8204:                                 $madethumb = 1;
 8205:                             } else {
 8206:                                 print $logfile "\nUnable to write ".$copyfile.
 8207:                                                ':'.$!."\n";
 8208:                             }
 8209:                         }
 8210:                     }
 8211:                 }
 8212:             }
 8213:         } else {
 8214:             $output = $versionresult;
 8215:         }
 8216:     }
 8217:     return ($output,$logourl,$madethumb);
 8218: }
 8219: 
 8220: sub logo_versioning {
 8221:     my ($targetdir,$file,$logfile) = @_;
 8222:     my $target = $targetdir.'/'.$file;
 8223:     my ($maxversion,$fn,$extn,$output);
 8224:     $maxversion = 0;
 8225:     if ($file =~ /^(.+)\.(\w+)$/) {
 8226:         $fn=$1;
 8227:         $extn=$2;
 8228:     }
 8229:     opendir(DIR,$targetdir);
 8230:     while (my $filename=readdir(DIR)) {
 8231:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 8232:             $maxversion=($1>$maxversion)?$1:$maxversion;
 8233:         }
 8234:     }
 8235:     $maxversion++;
 8236:     print $logfile "\nCreating old version ".$maxversion."\n";
 8237:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 8238:     if (copy($target,$copyfile)) {
 8239:         print $logfile "Copied old target to ".$copyfile."\n";
 8240:         $copyfile=$copyfile.'.meta';
 8241:         if (copy($target.'.meta',$copyfile)) {
 8242:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 8243:             $output = 'ok';
 8244:         } else {
 8245:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 8246:             $output = &mt('Failed to copy old meta').", $!, ";
 8247:         }
 8248:     } else {
 8249:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 8250:         $output = &mt('Failed to copy old target').", $!, ";
 8251:     }
 8252:     return $output;
 8253: }
 8254: 
 8255: sub write_metadata {
 8256:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 8257:     my (%metadatafields,%metadatakeys,$output);
 8258:     $metadatafields{'title'}=$formname;
 8259:     $metadatafields{'creationdate'}=time;
 8260:     $metadatafields{'lastrevisiondate'}=time;
 8261:     $metadatafields{'copyright'}='public';
 8262:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 8263:                                          $env{'user.domain'};
 8264:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 8265:     $metadatafields{'domain'}=$dom;
 8266:     {
 8267:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 8268:         my $mfh;
 8269:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 8270:             foreach (sort(keys(%metadatafields))) {
 8271:                 unless ($_=~/\./) {
 8272:                     my $unikey=$_;
 8273:                     $unikey=~/^([A-Za-z]+)/;
 8274:                     my $tag=$1;
 8275:                     $tag=~tr/A-Z/a-z/;
 8276:                     print $mfh "\n\<$tag";
 8277:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 8278:                         my $value=$metadatafields{$unikey.'.'.$_};
 8279:                         $value=~s/\"/\'\'/g;
 8280:                         print $mfh ' '.$_.'="'.$value.'"';
 8281:                     }
 8282:                     print $mfh '>'.
 8283:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 8284:                             .'</'.$tag.'>';
 8285:                 }
 8286:             }
 8287:             $output = 'ok';
 8288:             print $logfile "\nWrote metadata";
 8289:             close($mfh);
 8290:         } else {
 8291:             print $logfile "\nFailed to open metadata file";
 8292:             $output = &mt('Could not write metadata');
 8293:         }
 8294:     }
 8295:     return $output;
 8296: }
 8297: 
 8298: sub notifysubscribed {
 8299:     foreach my $targetsource (@{$modified_urls}){
 8300:         next unless (ref($targetsource) eq 'ARRAY');
 8301:         my ($target,$source)=@{$targetsource};
 8302:         if ($source ne '') {
 8303:             if (open(my $logfh,'>>'.$source.'.log')) {
 8304:                 print $logfh "\nCleanup phase: Notifications\n";
 8305:                 my @subscribed=&subscribed_hosts($target);
 8306:                 foreach my $subhost (@subscribed) {
 8307:                     print $logfh "\nNotifying host ".$subhost.':';
 8308:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 8309:                     print $logfh $reply;
 8310:                 }
 8311:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 8312:                 foreach my $subhost (@subscribedmeta) {
 8313:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 8314:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 8315:                                                         $subhost);
 8316:                     print $logfh $reply;
 8317:                 }
 8318:                 print $logfh "\n============ Done ============\n";
 8319:                 close($logfh);
 8320:             }
 8321:         }
 8322:     }
 8323:     return OK;
 8324: }
 8325: 
 8326: sub subscribed_hosts {
 8327:     my ($target) = @_;
 8328:     my @subscribed;
 8329:     if (open(my $fh,"<$target.subscription")) {
 8330:         while (my $subline=<$fh>) {
 8331:             if ($subline =~ /^($match_lonid):/) {
 8332:                 my $host = $1;
 8333:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 8334:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 8335:                         push(@subscribed,$host);
 8336:                     }
 8337:                 }
 8338:             }
 8339:         }
 8340:     }
 8341:     return @subscribed;
 8342: }
 8343: 
 8344: sub check_switchserver {
 8345:     my ($dom,$confname) = @_;
 8346:     my ($allowed,$switchserver);
 8347:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 8348:     if ($home eq 'no_host') {
 8349:         $home = &Apache::lonnet::domain($dom,'primary');
 8350:     }
 8351:     my @ids=&Apache::lonnet::current_machine_ids();
 8352:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 8353:     if (!$allowed) {
 8354: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 8355:     }
 8356:     return $switchserver;
 8357: }
 8358: 
 8359: sub modify_quotas {
 8360:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 8361:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 8362:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 8363:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 8364:         $validationfieldsref);
 8365:     if ($action eq 'quotas') {
 8366:         $context = 'tools'; 
 8367:     } else {
 8368:         $context = $action;
 8369:     }
 8370:     if ($context eq 'requestcourses') {
 8371:         @usertools = ('official','unofficial','community','textbook','placement');
 8372:         @options =('norequest','approval','validate','autolimit');
 8373:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 8374:         %titles = &courserequest_titles();
 8375:         $toolregexp = join('|',@usertools);
 8376:         %conditions = &courserequest_conditions();
 8377:         $confname = $dom.'-domainconfig';
 8378:         my $servadm = $r->dir_config('lonAdmEMail');
 8379:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8380:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 8381:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 8382:     } elsif ($context eq 'requestauthor') {
 8383:         @usertools = ('author');
 8384:         %titles = &authorrequest_titles();
 8385:     } else {
 8386:         @usertools = ('aboutme','blog','webdav','portfolio');
 8387:         %titles = &tool_titles();
 8388:     }
 8389:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8390:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8391:     foreach my $key (keys(%env)) {
 8392:         if ($context eq 'requestcourses') {
 8393:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 8394:                 my $item = $1;
 8395:                 my $type = $2;
 8396:                 if ($type =~ /^limit_(.+)/) {
 8397:                     $limithash{$item}{$1} = $env{$key};
 8398:                 } else {
 8399:                     $confhash{$item}{$type} = $env{$key};
 8400:                 }
 8401:             }
 8402:         } elsif ($context eq 'requestauthor') {
 8403:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 8404:                 $confhash{$1} = $env{$key};
 8405:             }
 8406:         } else {
 8407:             if ($key =~ /^form\.quota_(.+)$/) {
 8408:                 $confhash{'defaultquota'}{$1} = $env{$key};
 8409:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 8410:                 $confhash{'authorquota'}{$1} = $env{$key};
 8411:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 8412:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 8413:             }
 8414:         }
 8415:     }
 8416:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 8417:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 8418:         @approvalnotify = sort(@approvalnotify);
 8419:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 8420:         my @crstypes = ('official','unofficial','community','textbook','placement');
 8421:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 8422:         foreach my $type (@hasuniquecode) {
 8423:             if (grep(/^\Q$type\E$/,@crstypes)) {
 8424:                 $confhash{'uniquecode'}{$type} = 1;
 8425:             }
 8426:         }
 8427:         my (%newbook,%allpos);
 8428:         if ($context eq 'requestcourses') {
 8429:             foreach my $type ('textbooks','templates') {
 8430:                 @{$allpos{$type}} = (); 
 8431:                 my $invalid;
 8432:                 if ($type eq 'textbooks') {
 8433:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 8434:                 } else {
 8435:                     $invalid = &mt('Invalid LON-CAPA course for template');
 8436:                 }
 8437:                 if ($env{'form.'.$type.'_addbook'}) {
 8438:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 8439:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 8440:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 8441:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 8442:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8443:                         } else {
 8444:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 8445:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 8446:                             $position =~ s/\D+//g;
 8447:                             if ($position ne '') {
 8448:                                 $allpos{$type}[$position] = $newbook{$type};
 8449:                             }
 8450:                         }
 8451:                     } else {
 8452:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8453:                     }
 8454:                 }
 8455:             } 
 8456:         }
 8457:         if (ref($domconfig{$action}) eq 'HASH') {
 8458:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 8459:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 8460:                     $changes{'notify'}{'approval'} = 1;
 8461:                 }
 8462:             } else {
 8463:                 if ($confhash{'notify'}{'approval'}) {
 8464:                     $changes{'notify'}{'approval'} = 1;
 8465:                 }
 8466:             }
 8467:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 8468:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 8469:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 8470:                         unless ($confhash{'uniquecode'}{$crstype}) {
 8471:                             $changes{'uniquecode'} = 1;
 8472:                         }
 8473:                     }
 8474:                     unless ($changes{'uniquecode'}) {
 8475:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 8476:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 8477:                                 $changes{'uniquecode'} = 1;
 8478:                             }
 8479:                         }
 8480:                     }
 8481:                } else {
 8482:                    $changes{'uniquecode'} = 1;
 8483:                }
 8484:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 8485:                 $changes{'uniquecode'} = 1;
 8486:             }
 8487:             if ($context eq 'requestcourses') {
 8488:                 foreach my $type ('textbooks','templates') {
 8489:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8490:                         my %deletions;
 8491:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 8492:                         if (@todelete) {
 8493:                             map { $deletions{$_} = 1; } @todelete;
 8494:                         }
 8495:                         my %imgdeletions;
 8496:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 8497:                         if (@todeleteimages) {
 8498:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 8499:                         }
 8500:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 8501:                         for (my $i=0; $i<=$maxnum; $i++) {
 8502:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 8503:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 8504:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 8505:                                 if ($deletions{$key}) {
 8506:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 8507:                                         #FIXME need to obsolete item in RES space
 8508:                                     }
 8509:                                     next;
 8510:                                 } else {
 8511:                                     my $newpos = $env{'form.'.$itemid};
 8512:                                     $newpos =~ s/\D+//g;
 8513:                                     foreach my $item ('subject','title','publisher','author') {
 8514:                                         next if ((($item eq 'author') || ($item eq 'publisher')) && 
 8515:                                                  ($type eq 'templates'));
 8516:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 8517:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 8518:                                             $changes{$type}{$key} = 1;
 8519:                                         }
 8520:                                     }
 8521:                                     $allpos{$type}[$newpos] = $key;
 8522:                                 }
 8523:                                 if ($imgdeletions{$key}) {
 8524:                                     $changes{$type}{$key} = 1;
 8525:                                     #FIXME need to obsolete item in RES space
 8526:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 8527:                                     my ($cdom,$cnum) = split(/_/,$key);
 8528:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 8529:                                                                                   $cdom,$cnum,$type,$configuserok,
 8530:                                                                                   $switchserver,$author_ok);
 8531:                                     if ($imgurl) {
 8532:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 8533:                                         $changes{$type}{$key} = 1; 
 8534:                                     }
 8535:                                     if ($error) {
 8536:                                         &Apache::lonnet::logthis($error);
 8537:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8538:                                     } 
 8539:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 8540:                                     $confhash{$type}{$key}{'image'} = 
 8541:                                         $domconfig{$action}{$type}{$key}{'image'};
 8542:                                 }
 8543:                             }
 8544:                         }
 8545:                     }
 8546:                 }
 8547:             }
 8548:         } else {
 8549:             if ($confhash{'notify'}{'approval'}) {
 8550:                 $changes{'notify'}{'approval'} = 1;
 8551:             }
 8552:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 8553:                 $changes{'uniquecode'} = 1;
 8554:             }
 8555:         }
 8556:         if ($context eq 'requestcourses') {
 8557:             foreach my $type ('textbooks','templates') {
 8558:                 if ($newbook{$type}) {
 8559:                     $changes{$type}{$newbook{$type}} = 1;
 8560:                     foreach my $item ('subject','title','publisher','author') {
 8561:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8562:                                  ($type eq 'template'));
 8563:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 8564:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 8565:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 8566:                         }
 8567:                     }
 8568:                     if ($type eq 'textbooks') {
 8569:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 8570:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 8571:                             my ($imageurl,$error) =
 8572:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 8573:                                                         $configuserok,$switchserver,$author_ok);
 8574:                             if ($imageurl) {
 8575:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 8576:                             }
 8577:                             if ($error) {
 8578:                                 &Apache::lonnet::logthis($error);
 8579:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8580:                             }
 8581:                         }
 8582:                     }
 8583:                 }
 8584:                 if (@{$allpos{$type}} > 0) {
 8585:                     my $idx = 0;
 8586:                     foreach my $item (@{$allpos{$type}}) {
 8587:                         if ($item ne '') {
 8588:                             $confhash{$type}{$item}{'order'} = $idx;
 8589:                             if (ref($domconfig{$action}) eq 'HASH') {
 8590:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8591:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 8592:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 8593:                                             $changes{$type}{$item} = 1;
 8594:                                         }
 8595:                                     }
 8596:                                 }
 8597:                             }
 8598:                             $idx ++;
 8599:                         }
 8600:                     }
 8601:                 }
 8602:             }
 8603:             if (ref($validationitemsref) eq 'ARRAY') {
 8604:                 foreach my $item (@{$validationitemsref}) {
 8605:                     if ($item eq 'fields') {
 8606:                         my @changed;
 8607:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 8608:                         if (@{$confhash{'validation'}{$item}} > 0) {
 8609:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 8610:                         }
 8611:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8612:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8613:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 8614:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 8615:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 8616:                                 } else {
 8617:                                     @changed = @{$confhash{'validation'}{$item}};
 8618:                                 }
 8619:                             } else {
 8620:                                 @changed = @{$confhash{'validation'}{$item}};
 8621:                             }
 8622:                         } else {
 8623:                             @changed = @{$confhash{'validation'}{$item}};
 8624:                         }
 8625:                         if (@changed) {
 8626:                             if ($confhash{'validation'}{$item}) {
 8627:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 8628:                             } else {
 8629:                                 $changes{'validation'}{$item} = &mt('None');
 8630:                             }
 8631:                         }
 8632:                     } else {
 8633:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 8634:                         if ($item eq 'markup') {
 8635:                             if ($env{'form.requestcourses_validation_'.$item}) {
 8636:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 8637:                             }
 8638:                         }
 8639:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8640:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8641:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 8642:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8643:                                 }
 8644:                             } else {
 8645:                                 if ($confhash{'validation'}{$item} ne '') {
 8646:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8647:                                 }
 8648:                             }
 8649:                         } else {
 8650:                             if ($confhash{'validation'}{$item} ne '') {
 8651:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8652:                             }
 8653:                         }
 8654:                     }
 8655:                 }
 8656:             }
 8657:             if ($env{'form.validationdc'}) {
 8658:                 my $newval = $env{'form.validationdc'};
 8659:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 8660:                 if (exists($domcoords{$newval})) {
 8661:                     $confhash{'validation'}{'dc'} = $newval;
 8662:                 }
 8663:             }
 8664:             if (ref($confhash{'validation'}) eq 'HASH') {
 8665:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8666:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8667:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8668:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8669:                                 if ($confhash{'validation'}{'dc'} eq '') {
 8670:                                     $changes{'validation'}{'dc'} = &mt('None');
 8671:                                 } else {
 8672:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8673:                                 }
 8674:                             }
 8675:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 8676:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8677:                         }
 8678:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 8679:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8680:                     }
 8681:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 8682:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8683:                 }  
 8684:             } else {
 8685:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8686:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8687:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8688:                             $changes{'validation'}{'dc'} = &mt('None');
 8689:                         }
 8690:                     }
 8691:                 }
 8692:             }
 8693:         }
 8694:     } else {
 8695:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 8696:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 8697:     }
 8698:     foreach my $item (@usertools) {
 8699:         foreach my $type (@{$types},'default','_LC_adv') {
 8700:             my $unset; 
 8701:             if ($context eq 'requestcourses') {
 8702:                 $unset = '0';
 8703:                 if ($type eq '_LC_adv') {
 8704:                     $unset = '';
 8705:                 }
 8706:                 if ($confhash{$item}{$type} eq 'autolimit') {
 8707:                     $confhash{$item}{$type} .= '=';
 8708:                     unless ($limithash{$item}{$type} =~ /\D/) {
 8709:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 8710:                     }
 8711:                 }
 8712:             } elsif ($context eq 'requestauthor') {
 8713:                 $unset = '0';
 8714:                 if ($type eq '_LC_adv') {
 8715:                     $unset = '';
 8716:                 }
 8717:             } else {
 8718:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 8719:                     $confhash{$item}{$type} = 1;
 8720:                 } else {
 8721:                     $confhash{$item}{$type} = 0;
 8722:                 }
 8723:             }
 8724:             if (ref($domconfig{$action}) eq 'HASH') {
 8725:                 if ($action eq 'requestauthor') {
 8726:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 8727:                         $changes{$type} = 1;
 8728:                     }
 8729:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 8730:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 8731:                         $changes{$item}{$type} = 1;
 8732:                     }
 8733:                 } else {
 8734:                     if ($context eq 'requestcourses') {
 8735:                         if ($confhash{$item}{$type} ne $unset) {
 8736:                             $changes{$item}{$type} = 1;
 8737:                         }
 8738:                     } else {
 8739:                         if (!$confhash{$item}{$type}) {
 8740:                             $changes{$item}{$type} = 1;
 8741:                         }
 8742:                     }
 8743:                 }
 8744:             } else {
 8745:                 if ($context eq 'requestcourses') {
 8746:                     if ($confhash{$item}{$type} ne $unset) {
 8747:                         $changes{$item}{$type} = 1;
 8748:                     }
 8749:                 } elsif ($context eq 'requestauthor') {
 8750:                     if ($confhash{$type} ne $unset) {
 8751:                         $changes{$type} = 1;
 8752:                     }
 8753:                 } else {
 8754:                     if (!$confhash{$item}{$type}) {
 8755:                         $changes{$item}{$type} = 1;
 8756:                     }
 8757:                 }
 8758:             }
 8759:         }
 8760:     }
 8761:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 8762:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 8763:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 8764:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 8765:                     if (exists($confhash{'defaultquota'}{$key})) {
 8766:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 8767:                             $changes{'defaultquota'}{$key} = 1;
 8768:                         }
 8769:                     } else {
 8770:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 8771:                     }
 8772:                 }
 8773:             } else {
 8774:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 8775:                     if (exists($confhash{'defaultquota'}{$key})) {
 8776:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 8777:                             $changes{'defaultquota'}{$key} = 1;
 8778:                         }
 8779:                     } else {
 8780:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 8781:                     }
 8782:                 }
 8783:             }
 8784:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 8785:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 8786:                     if (exists($confhash{'authorquota'}{$key})) {
 8787:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 8788:                             $changes{'authorquota'}{$key} = 1;
 8789:                         }
 8790:                     } else {
 8791:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 8792:                     }
 8793:                 }
 8794:             }
 8795:         }
 8796:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 8797:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 8798:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 8799:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 8800:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 8801:                             $changes{'defaultquota'}{$key} = 1;
 8802:                         }
 8803:                     } else {
 8804:                         if (!exists($domconfig{'quotas'}{$key})) {
 8805:                             $changes{'defaultquota'}{$key} = 1;
 8806:                         }
 8807:                     }
 8808:                 } else {
 8809:                     $changes{'defaultquota'}{$key} = 1;
 8810:                 }
 8811:             }
 8812:         }
 8813:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 8814:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 8815:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 8816:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 8817:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 8818:                             $changes{'authorquota'}{$key} = 1;
 8819:                         }
 8820:                     } else {
 8821:                         $changes{'authorquota'}{$key} = 1;
 8822:                     }
 8823:                 } else {
 8824:                     $changes{'authorquota'}{$key} = 1;
 8825:                 }
 8826:             }
 8827:         }
 8828:     }
 8829: 
 8830:     if ($context eq 'requestauthor') {
 8831:         $domdefaults{'requestauthor'} = \%confhash;
 8832:     } else {
 8833:         foreach my $key (keys(%confhash)) {
 8834:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 8835:                 $domdefaults{$key} = $confhash{$key};
 8836:             }
 8837:         }
 8838:     }
 8839: 
 8840:     my %quotahash = (
 8841:                       $action => { %confhash }
 8842:                     );
 8843:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 8844:                                              $dom);
 8845:     if ($putresult eq 'ok') {
 8846:         if (keys(%changes) > 0) {
 8847:             my $cachetime = 24*60*60;
 8848:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8849:             if (ref($lastactref) eq 'HASH') {
 8850:                 $lastactref->{'domdefaults'} = 1;
 8851:             }
 8852:             $resulttext = &mt('Changes made:').'<ul>';
 8853:             unless (($context eq 'requestcourses') ||
 8854:                     ($context eq 'requestauthor')) {
 8855:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 8856:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 8857:                     foreach my $type (@{$types},'default') {
 8858:                         if (defined($changes{'defaultquota'}{$type})) {
 8859:                             my $typetitle = $usertypes->{$type};
 8860:                             if ($type eq 'default') {
 8861:                                 $typetitle = $othertitle;
 8862:                             }
 8863:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 8864:                         }
 8865:                     }
 8866:                     $resulttext .= '</ul></li>';
 8867:                 }
 8868:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 8869:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 8870:                     foreach my $type (@{$types},'default') {
 8871:                         if (defined($changes{'authorquota'}{$type})) {
 8872:                             my $typetitle = $usertypes->{$type};
 8873:                             if ($type eq 'default') {
 8874:                                 $typetitle = $othertitle;
 8875:                             }
 8876:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 8877:                         }
 8878:                     }
 8879:                     $resulttext .= '</ul></li>';
 8880:                 }
 8881:             }
 8882:             my %newenv;
 8883:             foreach my $item (@usertools) {
 8884:                 my (%haschgs,%inconf);
 8885:                 if ($context eq 'requestauthor') {
 8886:                     %haschgs = %changes;
 8887:                     %inconf = %confhash;
 8888:                 } else {
 8889:                     if (ref($changes{$item}) eq 'HASH') {
 8890:                         %haschgs = %{$changes{$item}};
 8891:                     }
 8892:                     if (ref($confhash{$item}) eq 'HASH') {
 8893:                         %inconf = %{$confhash{$item}};
 8894:                     }
 8895:                 }
 8896:                 if (keys(%haschgs) > 0) {
 8897:                     my $newacc = 
 8898:                         &Apache::lonnet::usertools_access($env{'user.name'},
 8899:                                                           $env{'user.domain'},
 8900:                                                           $item,'reload',$context);
 8901:                     if (($context eq 'requestcourses') ||
 8902:                         ($context eq 'requestauthor')) {
 8903:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 8904:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 8905:                         }
 8906:                     } else {
 8907:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 8908:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 8909:                         }
 8910:                     }
 8911:                     unless ($context eq 'requestauthor') {
 8912:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 8913:                     }
 8914:                     foreach my $type (@{$types},'default','_LC_adv') {
 8915:                         if ($haschgs{$type}) {
 8916:                             my $typetitle = $usertypes->{$type};
 8917:                             if ($type eq 'default') {
 8918:                                 $typetitle = $othertitle;
 8919:                             } elsif ($type eq '_LC_adv') {
 8920:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 8921:                             }
 8922:                             if ($inconf{$type}) {
 8923:                                 if ($context eq 'requestcourses') {
 8924:                                     my $cond;
 8925:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 8926:                                         if ($1 eq '') {
 8927:                                             $cond = &mt('(Automatic processing of any request).');
 8928:                                         } else {
 8929:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 8930:                                         }
 8931:                                     } else { 
 8932:                                         $cond = $conditions{$inconf{$type}};
 8933:                                     }
 8934:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 8935:                                 } elsif ($context eq 'requestauthor') {
 8936:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 8937:                                                              $titles{$inconf{$type}},$typetitle);
 8938: 
 8939:                                 } else {
 8940:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 8941:                                 }
 8942:                             } else {
 8943:                                 if ($type eq '_LC_adv') {
 8944:                                     if ($inconf{$type} eq '0') {
 8945:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 8946:                                     } else { 
 8947:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 8948:                                     }
 8949:                                 } else {
 8950:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 8951:                                 }
 8952:                             }
 8953:                         }
 8954:                     }
 8955:                     unless ($context eq 'requestauthor') {
 8956:                         $resulttext .= '</ul></li>';
 8957:                     }
 8958:                 }
 8959:             }
 8960:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 8961:                 if (ref($changes{'notify'}) eq 'HASH') {
 8962:                     if ($changes{'notify'}{'approval'}) {
 8963:                         if (ref($confhash{'notify'}) eq 'HASH') {
 8964:                             if ($confhash{'notify'}{'approval'}) {
 8965:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 8966:                             } else {
 8967:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 8968:                             }
 8969:                         }
 8970:                     }
 8971:                 }
 8972:             }
 8973:             if ($action eq 'requestcourses') {
 8974:                 my @offon = ('off','on');
 8975:                 if ($changes{'uniquecode'}) {
 8976:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 8977:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 8978:                         $resulttext .= '<li>'.
 8979:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 8980:                                        '</li>';
 8981:                     } else {
 8982:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 8983:                                        '</li>';
 8984:                     }
 8985:                 }
 8986:                 foreach my $type ('textbooks','templates') {
 8987:                     if (ref($changes{$type}) eq 'HASH') {
 8988:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 8989:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 8990:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 8991:                             my $coursetitle = $coursehash{'description'};
 8992:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 8993:                             $resulttext .= '<li>';
 8994:                             foreach my $item ('subject','title','publisher','author') {
 8995:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8996:                                          ($type eq 'templates'));
 8997:                                 my $name = $item.':';
 8998:                                 $name =~ s/^(\w)/\U$1/;
 8999:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 9000:                             }
 9001:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 9002:                             if ($type eq 'textbooks') {
 9003:                                 if ($confhash{$type}{$key}{'image'}) {
 9004:                                     $resulttext .= ' '.&mt('Image: [_1]',
 9005:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 9006:                                                    ' alt="Textbook cover" />').'<br />';
 9007:                                 }
 9008:                             }
 9009:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 9010:                         }
 9011:                         $resulttext .= '</ul></li>';
 9012:                     }
 9013:                 }
 9014:                 if (ref($changes{'validation'}) eq 'HASH') {
 9015:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 9016:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 9017:                         foreach my $item (@{$validationitemsref}) {
 9018:                             if (exists($changes{'validation'}{$item})) {
 9019:                                 if ($item eq 'markup') {
 9020:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9021:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 9022:                                 } else {
 9023:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9024:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 9025:                                 }
 9026:                             }
 9027:                         }
 9028:                         if (exists($changes{'validation'}{'dc'})) {
 9029:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 9030:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 9031:                         }
 9032:                     }
 9033:                 }
 9034:             }
 9035:             $resulttext .= '</ul>';
 9036:             if (keys(%newenv)) {
 9037:                 &Apache::lonnet::appenv(\%newenv);
 9038:             }
 9039:         } else {
 9040:             if ($context eq 'requestcourses') {
 9041:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 9042:             } elsif ($context eq 'requestauthor') {
 9043:                 $resulttext = &mt('No changes made to rights to request author space.');
 9044:             } else {
 9045:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 9046:             }
 9047:         }
 9048:     } else {
 9049:         $resulttext = '<span class="LC_error">'.
 9050: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9051:     }
 9052:     if ($errors) {
 9053:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 9054:                        '<ul>'.$errors.'</ul></p>';
 9055:     }
 9056:     return $resulttext;
 9057: }
 9058: 
 9059: sub process_textbook_image {
 9060:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 9061:     my $filename = $env{'form.'.$caller.'.filename'};
 9062:     my ($error,$url);
 9063:     my ($width,$height) = (50,50);
 9064:     if ($configuserok eq 'ok') {
 9065:         if ($switchserver) {
 9066:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 9067:                          $switchserver);
 9068:         } elsif ($author_ok eq 'ok') {
 9069:             my ($result,$imageurl) =
 9070:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9071:                              "$type/$dom/$cnum/cover",$width,$height);
 9072:             if ($result eq 'ok') {
 9073:                 $url = $imageurl;
 9074:             } else {
 9075:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9076:             }
 9077:         } else {
 9078:             $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);
 9079:         }
 9080:     } else {
 9081:         $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);
 9082:     }
 9083:     return ($url,$error);
 9084: }
 9085: 
 9086: sub modify_ltitools {
 9087:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 9088:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9089:     my ($newid,@allpos,%changes,%confhash,$errors,$resulttext);
 9090:     my $confname = $dom.'-domainconfig';
 9091:     my $servadm = $r->dir_config('lonAdmEMail');
 9092:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9093:     my (%posslti,%possfield);
 9094:     my @courseroles = ('cc','in','ta','ep','st');
 9095:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 9096:     map { $posslti{$_} = 1; } @ltiroles;
 9097:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
 9098:     map { $possfield{$_} = 1; } @allfields;
 9099:     my %lt = &ltitools_names(); 
 9100:     if ($env{'form.ltitools_add'}) {
 9101:         my $title = $env{'form.ltitools_add_title'};
 9102:         $title =~ s/(`)/'/g;
 9103:         ($newid,my $error) = &get_ltitools_id($dom,$title);
 9104:         if ($newid) {
 9105:             my $position = $env{'form.ltitools_add_pos'};
 9106:             $position =~ s/\D+//g;
 9107:             if ($position ne '') {
 9108:                 $allpos[$position] = $newid;
 9109:             }
 9110:             $changes{$newid} = 1;
 9111:             foreach my $item ('title','url','key','secret') {
 9112:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
 9113:                 if ($env{'form.ltitools_add_'.$item}) {
 9114:                     $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
 9115:                 }
 9116:             }
 9117:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
 9118:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
 9119:             }
 9120:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
 9121:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
 9122:             }
 9123:             foreach my $item ('width','height') {
 9124:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
 9125:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
 9126:                 if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
 9127:                     $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
 9128:                 }
 9129:             }
 9130:             if ($env{'form.ltitools_add_target'} eq 'window') {
 9131:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
 9132:             } else {
 9133:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
 9134:             }
 9135:             foreach my $item ('passback','roster') {
 9136:                 if ($env{'form.ltitools_add_'.$item}) {
 9137:                     $confhash{$newid}{$item} = 1;
 9138:                 }
 9139:             }
 9140:             if ($env{'form.ltitools_add_image.filename'} ne '') {
 9141:                 my ($imageurl,$error) =
 9142:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$dom,
 9143:                                             $configuserok,$switchserver,$author_ok);
 9144:                 if ($imageurl) {
 9145:                     $confhash{$newid}{'image'} = $imageurl;
 9146:                 }
 9147:                 if ($error) {
 9148:                     &Apache::lonnet::logthis($error);
 9149:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9150:                 }
 9151:             }
 9152:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
 9153:             foreach my $field (@fields) {
 9154:                 if ($possfield{$field}) {
 9155:                     if ($field eq 'roles') {
 9156:                         foreach my $role (@courseroles) {
 9157:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
 9158:                             if (($choice ne '') && ($posslti{$choice})) {
 9159:                                 $confhash{$newid}{'roles'}{$role} = $choice;
 9160:                                 if ($role eq 'cc') {
 9161:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
 9162:                                 }
 9163:                             }
 9164:                         }
 9165:                     } else {
 9166:                         $confhash{$newid}{'fields'}{$field} = 1;
 9167:                     }
 9168:                 }
 9169:             }
 9170:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
 9171:             foreach my $item (@courseconfig) {
 9172:                 $confhash{$newid}{'crsconf'}{$item} = 1;
 9173:             }
 9174:             if ($env{'form.ltitools_add_custom'}) {
 9175:                 my $name = $env{'form.ltitools_add_custom_name'};
 9176:                 my $value = $env{'form.ltitools_add_custom_value'};
 9177:                 $value =~ s/(`)/'/g;
 9178:                 $name =~ s/(`)/'/g;
 9179:                 $confhash{$newid}{'custom'}{$name} = $value;
 9180:             }
 9181:         } else {
 9182:             my $error = &mt('Failed to acquire unique ID for new external tool');   
 9183:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9184:         }
 9185:     }
 9186:     if (ref($domconfig{$action}) eq 'HASH') {
 9187:         my %deletions;
 9188:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
 9189:         if (@todelete) {
 9190:             map { $deletions{$_} = 1; } @todelete;
 9191:         }
 9192:         my %customadds;
 9193:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
 9194:         if (@newcustom) {
 9195:             map { $customadds{$_} = 1; } @newcustom;
 9196:         } 
 9197:         my %imgdeletions;
 9198:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
 9199:         if (@todeleteimages) {
 9200:             map { $imgdeletions{$_} = 1; } @todeleteimages;
 9201:         }
 9202:         my $maxnum = $env{'form.ltitools_maxnum'};
 9203:         for (my $i=0; $i<=$maxnum; $i++) {
 9204:             my $itemid = $env{'form.ltitools_id_'.$i};
 9205:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
 9206:                 if ($deletions{$itemid}) {
 9207:                     if ($domconfig{$action}{$itemid}{'image'}) {
 9208:                         #FIXME need to obsolete item in RES space
 9209:                     }
 9210:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
 9211:                     next;
 9212:                 } else {
 9213:                     my $newpos = $env{'form.ltitools_'.$itemid};
 9214:                     $newpos =~ s/\D+//g;
 9215:                     foreach my $item ('title','url','key','secret') {
 9216:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9217:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
 9218:                             $changes{$itemid} = 1;
 9219:                         }
 9220:                     }
 9221:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
 9222:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
 9223:                     }
 9224:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
 9225:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
 9226:                     }
 9227:                     foreach my $size ('width','height') {
 9228:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
 9229:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
 9230:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
 9231:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
 9232:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9233:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
 9234:                                     $changes{$itemid} = 1;
 9235:                                 }
 9236:                             } else {
 9237:                                 $changes{$itemid} = 1;
 9238:                             }
 9239:                         }
 9240:                     }
 9241:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
 9242:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
 9243:                     } else {
 9244:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
 9245:                     }
 9246:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9247:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
 9248:                             $changes{$itemid} = 1;
 9249:                         }
 9250:                     } else {
 9251:                         $changes{$itemid} = 1;
 9252:                     }
 9253:                     foreach my $extra ('passback','roster') {
 9254:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
 9255:                             $confhash{$itemid}{$extra} = 1;
 9256:                         }
 9257:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
 9258:                             $changes{$itemid} = 1;
 9259:                         }
 9260:                     }
 9261:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
 9262:                     foreach my $item ('label','title','target') {
 9263:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
 9264:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
 9265:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
 9266:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
 9267:                                     $changes{$itemid} = 1;
 9268:                                 }
 9269:                             } else {
 9270:                                 $changes{$itemid} = 1;
 9271:                             }
 9272:                         }
 9273:                     }
 9274:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
 9275:                     foreach my $field (@fields) {
 9276:                         if ($possfield{$field}) {
 9277:                             if ($field eq 'roles') {
 9278:                                 foreach my $role (@courseroles) {
 9279:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
 9280:                                     if (($choice ne '') && ($posslti{$choice})) {
 9281:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
 9282:                                         if ($role eq 'cc') {
 9283:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
 9284:                                         }
 9285:                                     }
 9286:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
 9287:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
 9288:                                             $changes{$itemid} = 1;
 9289:                                         }
 9290:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
 9291:                                         $changes{$itemid} = 1;
 9292:                                     }
 9293:                                 }
 9294:                             } else {
 9295:                                 $confhash{$itemid}{'fields'}{$field} = 1;
 9296:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
 9297:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
 9298:                                         $changes{$itemid} = 1;
 9299:                                     }
 9300:                                 } else {
 9301:                                     $changes{$itemid} = 1;
 9302:                                 }
 9303:                             }
 9304:                         }
 9305:                     }
 9306:                     $allpos[$newpos] = $itemid;
 9307:                 }
 9308:                 if ($imgdeletions{$itemid}) {
 9309:                     $changes{$itemid} = 1;
 9310:                     #FIXME need to obsolete item in RES space
 9311:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
 9312:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
 9313:                                                                  $itemid,$configuserok,$switchserver,
 9314:                                                                  $author_ok);
 9315:                     if ($imgurl) {
 9316:                         $confhash{$itemid}{'image'} = $imgurl;
 9317:                         $changes{$itemid} = 1;
 9318:                     }
 9319:                     if ($error) {
 9320:                         &Apache::lonnet::logthis($error);
 9321:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9322:                     }
 9323:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
 9324:                     $confhash{$itemid}{'image'} =
 9325:                        $domconfig{$action}{$itemid}{'image'};
 9326:                 }
 9327:                 if ($customadds{$i}) {
 9328:                     my $name = $env{'form.ltitools_custom_name_'.$i};
 9329:                     $name =~ s/(`)/'/g;
 9330:                     $name =~ s/^\s+//;
 9331:                     $name =~ s/\s+$//;
 9332:                     my $value = $env{'form.ltitools_custom_value_'.$i};
 9333:                     $value =~ s/(`)/'/g;
 9334:                     $value =~ s/^\s+//;
 9335:                     $value =~ s/\s+$//;
 9336:                     if ($name ne '') {
 9337:                         $confhash{$itemid}{'custom'}{$name} = $value;
 9338:                         $changes{$itemid} = 1;
 9339:                     }
 9340:                 }
 9341:                 my %customdels;
 9342:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
 9343:                 if (@customdeletions) {
 9344:                     $changes{$itemid} = 1;
 9345:                 }
 9346:                 map { $customdels{$_} = 1; } @customdeletions;
 9347:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
 9348:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
 9349:                         unless ($customdels{$key}) {
 9350:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
 9351:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
 9352:                             }
 9353:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
 9354:                                 $changes{$itemid} = 1;
 9355:                             }
 9356:                         }
 9357:                     }
 9358:                 }
 9359:                 unless ($changes{$itemid}) {
 9360:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
 9361:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
 9362:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
 9363:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
 9364:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
 9365:                                         $changes{$itemid} = 1;
 9366:                                         last;
 9367:                                     }
 9368:                                 }
 9369:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
 9370:                                 $changes{$itemid} = 1;
 9371:                             }
 9372:                         }
 9373:                         last if ($changes{$itemid});
 9374:                     }
 9375:                 }
 9376:             }
 9377:         }
 9378:     }
 9379:     if (@allpos > 0) {
 9380:         my $idx = 0;
 9381:         foreach my $itemid (@allpos) {
 9382:             if ($itemid ne '') {
 9383:                 $confhash{$itemid}{'order'} = $idx;
 9384:                 if (ref($domconfig{$action}) eq 'HASH') {
 9385:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
 9386:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
 9387:                             $changes{$itemid} = 1;
 9388:                         }
 9389:                     }
 9390:                 }
 9391:                 $idx ++;
 9392:             }
 9393:         }
 9394:     }
 9395:     my %ltitoolshash = (
 9396:                           $action => { %confhash }
 9397:                        );
 9398:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
 9399:                                              $dom);
 9400:     if ($putresult eq 'ok') {
 9401:         if (keys(%changes) > 0) {
 9402:             my $cachetime = 24*60*60;
 9403:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%confhash,$cachetime);
 9404:             if (ref($lastactref) eq 'HASH') {
 9405:                 $lastactref->{'ltitools'} = 1;
 9406:             }
 9407:             $resulttext = &mt('Changes made:').'<ul>';
 9408:             my %bynum;
 9409:             foreach my $itemid (sort(keys(%changes))) {
 9410:                 my $position = $confhash{$itemid}{'order'};
 9411:                 $bynum{$position} = $itemid;
 9412:             }
 9413:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
 9414:                 my $itemid = $bynum{$pos}; 
 9415:                 if (ref($confhash{$itemid}) ne 'HASH') {
 9416:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
 9417:                 } else {
 9418:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
 9419:                     if ($confhash{$itemid}{'image'}) {
 9420:                         $resulttext .= '&nbsp;'.
 9421:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
 9422:                                        ' alt="'.&mt('Tool Provider icon').'" />';
 9423:                     }
 9424:                     $resulttext .= '</li><ul>';
 9425:                     my $position = $pos + 1;
 9426:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
 9427:                     foreach my $item ('version','msgtype','url','key') {
 9428:                         if ($confhash{$itemid}{$item} ne '') {
 9429:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
 9430:                         }
 9431:                     }
 9432:                     if ($confhash{$itemid}{'secret'} ne '') {
 9433:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
 9434:                         my $num = length($confhash{$itemid}{'secret'});
 9435:                         $resulttext .= ('*'x$num).'</li>';
 9436:                     }
 9437:                     $resulttext .= '<li>'.&mt('Configurable in course:');
 9438:                     my @possconfig = ('label','title','target');
 9439:                     my $numconfig = 0; 
 9440:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') { 
 9441:                         foreach my $item (@possconfig) {
 9442:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
 9443:                                 $numconfig ++;
 9444:                                 $resulttext .= ' '.$lt{'crs'.$item};
 9445:                             }
 9446:                         }
 9447:                     }
 9448:                     if (!$numconfig) {
 9449:                         $resulttext .= &mt('None');
 9450:                     }
 9451:                     $resulttext .= '</li>';
 9452:                     foreach my $item ('passback','roster') {
 9453:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
 9454:                         if ($confhash{$itemid}{$item}) {
 9455:                             $resulttext .= &mt('Yes');
 9456:                         } else {
 9457:                             $resulttext .= &mt('No');
 9458:                         }
 9459:                         $resulttext .= '</li>';
 9460:                     }
 9461:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
 9462:                         my $displaylist;
 9463:                         if ($confhash{$itemid}{'display'}{'target'}) {
 9464:                             $displaylist = &mt('Display target').':&nbsp;'.
 9465:                                            $confhash{$itemid}{'display'}{'target'}.',';
 9466:                         }
 9467:                         foreach my $size ('width','height') { 
 9468:                             if ($confhash{$itemid}{'display'}{$size}) {
 9469:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
 9470:                                                 $confhash{$itemid}{'display'}{$size}.',';
 9471:                             }
 9472:                         }
 9473:                         if ($displaylist) {
 9474:                             $displaylist =~ s/,$//;
 9475:                             $resulttext .= '<li>'.$displaylist.'</li>';
 9476:                         }
 9477:                     } 
 9478:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
 9479:                         my $fieldlist;
 9480:                         foreach my $field (@allfields) {
 9481:                             if ($confhash{$itemid}{'fields'}{$field}) {
 9482:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
 9483:                             }
 9484:                         }
 9485:                         if ($fieldlist) {
 9486:                             $fieldlist =~ s/,$//;
 9487:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
 9488:                         }
 9489:                     }
 9490:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
 9491:                         my $rolemaps;
 9492:                         foreach my $role (@courseroles) {
 9493:                             if ($confhash{$itemid}{'roles'}{$role}) {
 9494:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
 9495:                                              $confhash{$itemid}{'roles'}{$role}.',';
 9496:                             }
 9497:                         }
 9498:                         if ($rolemaps) {
 9499:                             $rolemaps =~ s/,$//; 
 9500:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
 9501:                         }
 9502:                     }
 9503:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
 9504:                         my $customlist;
 9505:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
 9506:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
 9507:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
 9508:                             } 
 9509:                         }
 9510:                         if ($customlist) {
 9511:                             $resulttext .= '<li>'.&mt('Custom items').':'.$customlist.'</li>';
 9512:                         }
 9513:                     } 
 9514:                     $resulttext .= '</ul></li>';
 9515:                 }
 9516:             }
 9517:             $resulttext .= '</ul>';
 9518:         } else {
 9519:             $resulttext = &mt('No changes made.');
 9520:         }
 9521:     } else {
 9522:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
 9523:     }
 9524:     if ($errors) {
 9525:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9526:                        $errors.'</ul>';
 9527:     }
 9528:     return $resulttext;
 9529: }
 9530: 
 9531: sub process_ltitools_image {
 9532:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
 9533:     my $filename = $env{'form.'.$caller.'.filename'};
 9534:     my ($error,$url);
 9535:     my ($width,$height) = (21,21);
 9536:     if ($configuserok eq 'ok') {
 9537:         if ($switchserver) {
 9538:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
 9539:                          $switchserver);
 9540:         } elsif ($author_ok eq 'ok') {
 9541:             my ($result,$imageurl,$madethumb) =
 9542:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9543:                              "ltitools/$itemid/icon",$width,$height);
 9544:             if ($result eq 'ok') {
 9545:                 if ($madethumb) {
 9546:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
 9547:                     my $imagethumb = "$path/tn-".$imagefile;
 9548:                     $url = $imagethumb;
 9549:                 } else {
 9550:                     $url = $imageurl;
 9551:                 }
 9552:             } else {
 9553:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9554:             }
 9555:         } else {
 9556:             $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);
 9557:         }
 9558:     } else {
 9559:         $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);
 9560:     }
 9561:     return ($url,$error);
 9562: }
 9563: 
 9564: sub get_ltitools_id {
 9565:     my ($cdom,$title) = @_;
 9566:     # get lock on ltitools db
 9567:     my $lockhash = {
 9568:                       lock => $env{'user.name'}.
 9569:                               ':'.$env{'user.domain'},
 9570:                    };
 9571:     my $tries = 0;
 9572:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
 9573:     my ($id,$error);
 9574:  
 9575:     while (($gotlock ne 'ok') && ($tries<10)) {
 9576:         $tries ++;
 9577:         sleep (0.1);
 9578:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
 9579:     }
 9580:     if ($gotlock eq 'ok') {
 9581:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
 9582:         if ($currids{'lock'}) {
 9583:             delete($currids{'lock'});
 9584:             if (keys(%currids)) {
 9585:                 my @curr = sort { $a <=> $b } keys(%currids);
 9586:                 if ($curr[-1] =~ /^\d+$/) {
 9587:                     $id = 1 + $curr[-1];
 9588:                 }
 9589:             } else {
 9590:                 $id = 1;
 9591:             }
 9592:             if ($id) {
 9593:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
 9594:                     $error = 'nostore';
 9595:                 }
 9596:             } else {
 9597:                 $error = 'nonumber';
 9598:             }
 9599:         }
 9600:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
 9601:     } else {
 9602:         $error = 'nolock';
 9603:     }
 9604:     return ($id,$error);
 9605: }
 9606: 
 9607: sub modify_autoenroll {
 9608:     my ($dom,$lastactref,%domconfig) = @_;
 9609:     my ($resulttext,%changes);
 9610:     my %currautoenroll;
 9611:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 9612:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 9613:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 9614:         }
 9615:     }
 9616:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 9617:     my %title = ( run => 'Auto-enrollment active',
 9618:                   sender => 'Sender for notification messages',
 9619:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
 9620:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
 9621:     my @offon = ('off','on');
 9622:     my $sender_uname = $env{'form.sender_uname'};
 9623:     my $sender_domain = $env{'form.sender_domain'};
 9624:     if ($sender_domain eq '') {
 9625:         $sender_uname = '';
 9626:     } elsif ($sender_uname eq '') {
 9627:         $sender_domain = '';
 9628:     }
 9629:     my $coowners = $env{'form.autoassign_coowners'};
 9630:     my $failsafe = $env{'form.autoenroll_failsafe'};
 9631:     $failsafe =~ s{^\s+|\s+$}{}g;
 9632:     if ($failsafe =~ /\D/) {
 9633:         undef($failsafe);
 9634:     }
 9635:     my %autoenrollhash =  (
 9636:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 9637:                                        'sender_uname' => $sender_uname,
 9638:                                        'sender_domain' => $sender_domain,
 9639:                                        'co-owners' => $coowners,
 9640:                                        'autofailsafe' => $failsafe,
 9641:                                 }
 9642:                      );
 9643:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 9644:                                              $dom);
 9645:     if ($putresult eq 'ok') {
 9646:         if (exists($currautoenroll{'run'})) {
 9647:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 9648:                  $changes{'run'} = 1;
 9649:              }
 9650:         } elsif ($autorun) {
 9651:             if ($env{'form.autoenroll_run'} ne '1') {
 9652:                  $changes{'run'} = 1;
 9653:             }
 9654:         }
 9655:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 9656:             $changes{'sender'} = 1;
 9657:         }
 9658:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 9659:             $changes{'sender'} = 1;
 9660:         }
 9661:         if ($currautoenroll{'co-owners'} ne '') {
 9662:             if ($currautoenroll{'co-owners'} ne $coowners) {
 9663:                 $changes{'coowners'} = 1;
 9664:             }
 9665:         } elsif ($coowners) {
 9666:             $changes{'coowners'} = 1;
 9667:         }
 9668:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
 9669:             $changes{'autofailsafe'} = 1;
 9670:         }
 9671:         if (keys(%changes) > 0) {
 9672:             $resulttext = &mt('Changes made:').'<ul>';
 9673:             if ($changes{'run'}) {
 9674:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 9675:             }
 9676:             if ($changes{'sender'}) {
 9677:                 if ($sender_uname eq '' || $sender_domain eq '') {
 9678:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 9679:                 } else {
 9680:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 9681:                 }
 9682:             }
 9683:             if ($changes{'coowners'}) {
 9684:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 9685:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9686:                 if (ref($lastactref) eq 'HASH') {
 9687:                     $lastactref->{'domainconfig'} = 1;
 9688:                 }
 9689:             }
 9690:             if ($changes{'autofailsafe'}) {
 9691:                 if ($failsafe ne '') {
 9692:                     $resulttext .= '<li>'.&mt("$title{'failsafe'} set to [_1]",$failsafe).'</li>';
 9693:                 } else {
 9694:                     $resulttext .= '<li>'.&mt("$title{'failsafe'} deleted");
 9695:                 }
 9696:                 &Apache::lonnet::get_domain_defaults($dom,1);
 9697:                 if (ref($lastactref) eq 'HASH') {
 9698:                     $lastactref->{'domdefaults'} = 1;
 9699:                 }
 9700:             }
 9701:             $resulttext .= '</ul>';
 9702:         } else {
 9703:             $resulttext = &mt('No changes made to auto-enrollment settings');
 9704:         }
 9705:     } else {
 9706:         $resulttext = '<span class="LC_error">'.
 9707: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9708:     }
 9709:     return $resulttext;
 9710: }
 9711: 
 9712: sub modify_autoupdate {
 9713:     my ($dom,%domconfig) = @_;
 9714:     my ($resulttext,%currautoupdate,%fields,%changes);
 9715:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 9716:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 9717:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 9718:         }
 9719:     }
 9720:     my @offon = ('off','on');
 9721:     my %title = &Apache::lonlocal::texthash (
 9722:                    run => 'Auto-update:',
 9723:                    classlists => 'Updates to user information in classlists?'
 9724:                 );
 9725:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9726:     my %fieldtitles = &Apache::lonlocal::texthash (
 9727:                         id => 'Student/Employee ID',
 9728:                         permanentemail => 'E-mail address',
 9729:                         lastname => 'Last Name',
 9730:                         firstname => 'First Name',
 9731:                         middlename => 'Middle Name',
 9732:                         generation => 'Generation',
 9733:                       );
 9734:     $othertitle = &mt('All users');
 9735:     if (keys(%{$usertypes}) >  0) {
 9736:         $othertitle = &mt('Other users');
 9737:     }
 9738:     foreach my $key (keys(%env)) {
 9739:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 9740:             my ($usertype,$item) = ($1,$2);
 9741:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 9742:                 if ($usertype eq 'default') {   
 9743:                     push(@{$fields{$1}},$2);
 9744:                 } elsif (ref($types) eq 'ARRAY') {
 9745:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 9746:                         push(@{$fields{$1}},$2);
 9747:                     }
 9748:                 }
 9749:             }
 9750:         }
 9751:     }
 9752:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 9753:     @lockablenames = sort(@lockablenames);
 9754:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 9755:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 9756:         if (@changed) {
 9757:             $changes{'lockablenames'} = 1;
 9758:         }
 9759:     } else {
 9760:         if (@lockablenames) {
 9761:             $changes{'lockablenames'} = 1;
 9762:         }
 9763:     }
 9764:     my %updatehash = (
 9765:                       autoupdate => { run => $env{'form.autoupdate_run'},
 9766:                                       classlists => $env{'form.classlists'},
 9767:                                       fields => {%fields},
 9768:                                       lockablenames => \@lockablenames,
 9769:                                     }
 9770:                      );
 9771:     foreach my $key (keys(%currautoupdate)) {
 9772:         if (($key eq 'run') || ($key eq 'classlists')) {
 9773:             if (exists($updatehash{autoupdate}{$key})) {
 9774:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 9775:                     $changes{$key} = 1;
 9776:                 }
 9777:             }
 9778:         } elsif ($key eq 'fields') {
 9779:             if (ref($currautoupdate{$key}) eq 'HASH') {
 9780:                 foreach my $item (@{$types},'default') {
 9781:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 9782:                         my $change = 0;
 9783:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 9784:                             if (!exists($fields{$item})) {
 9785:                                 $change = 1;
 9786:                                 last;
 9787:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 9788:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 9789:                                     $change = 1;
 9790:                                     last;
 9791:                                 }
 9792:                             }
 9793:                         }
 9794:                         if ($change) {
 9795:                             push(@{$changes{$key}},$item);
 9796:                         }
 9797:                     } 
 9798:                 }
 9799:             }
 9800:         } elsif ($key eq 'lockablenames') {
 9801:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 9802:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 9803:                 if (@changed) {
 9804:                     $changes{'lockablenames'} = 1;
 9805:                 }
 9806:             } else {
 9807:                 if (@lockablenames) {
 9808:                     $changes{'lockablenames'} = 1;
 9809:                 }
 9810:             }
 9811:         }
 9812:     }
 9813:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 9814:         if (@lockablenames) {
 9815:             $changes{'lockablenames'} = 1;
 9816:         }
 9817:     }
 9818:     foreach my $item (@{$types},'default') {
 9819:         if (defined($fields{$item})) {
 9820:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 9821:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 9822:                     my $change = 0;
 9823:                     if (ref($fields{$item}) eq 'ARRAY') {
 9824:                         foreach my $type (@{$fields{$item}}) {
 9825:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 9826:                                 $change = 1;
 9827:                                 last;
 9828:                             }
 9829:                         }
 9830:                     }
 9831:                     if ($change) {
 9832:                         push(@{$changes{'fields'}},$item);
 9833:                     }
 9834:                 } else {
 9835:                     push(@{$changes{'fields'}},$item);
 9836:                 }
 9837:             } else {
 9838:                 push(@{$changes{'fields'}},$item);
 9839:             }
 9840:         }
 9841:     }
 9842:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 9843:                                              $dom);
 9844:     if ($putresult eq 'ok') {
 9845:         if (keys(%changes) > 0) {
 9846:             $resulttext = &mt('Changes made:').'<ul>';
 9847:             foreach my $key (sort(keys(%changes))) {
 9848:                 if ($key eq 'lockablenames') {
 9849:                     $resulttext .= '<li>';
 9850:                     if (@lockablenames) {
 9851:                         $usertypes->{'default'} = $othertitle;
 9852:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 9853:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 9854:                     } else {
 9855:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 9856:                     }
 9857:                     $resulttext .= '</li>';
 9858:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 9859:                     foreach my $item (@{$changes{$key}}) {
 9860:                         my @newvalues;
 9861:                         foreach my $type (@{$fields{$item}}) {
 9862:                             push(@newvalues,$fieldtitles{$type});
 9863:                         }
 9864:                         my $newvaluestr;
 9865:                         if (@newvalues > 0) {
 9866:                             $newvaluestr = join(', ',@newvalues);
 9867:                         } else {
 9868:                             $newvaluestr = &mt('none');
 9869:                         }
 9870:                         if ($item eq 'default') {
 9871:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 9872:                         } else {
 9873:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 9874:                         }
 9875:                     }
 9876:                 } else {
 9877:                     my $newvalue;
 9878:                     if ($key eq 'run') {
 9879:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 9880:                     } else {
 9881:                         $newvalue = $offon[$env{'form.'.$key}];
 9882:                     }
 9883:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 9884:                 }
 9885:             }
 9886:             $resulttext .= '</ul>';
 9887:         } else {
 9888:             $resulttext = &mt('No changes made to autoupdates');
 9889:         }
 9890:     } else {
 9891:         $resulttext = '<span class="LC_error">'.
 9892: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9893:     }
 9894:     return $resulttext;
 9895: }
 9896: 
 9897: sub modify_autocreate {
 9898:     my ($dom,%domconfig) = @_;
 9899:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 9900:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 9901:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 9902:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 9903:         }
 9904:     }
 9905:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 9906:                  req => 'Auto-creation of validated requests for official courses',
 9907:                  xmldc => 'Identity of course creator of courses from XML files',
 9908:                );
 9909:     my @types = ('xml','req');
 9910:     foreach my $item (@types) {
 9911:         $newvals{$item} = $env{'form.autocreate_'.$item};
 9912:         $newvals{$item} =~ s/\D//g;
 9913:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 9914:     }
 9915:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 9916:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 9917:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 9918:         $newvals{'xmldc'} = '';
 9919:     } 
 9920:     %autocreatehash =  (
 9921:                         autocreate => { xml => $newvals{'xml'},
 9922:                                         req => $newvals{'req'},
 9923:                                       }
 9924:                        );
 9925:     if ($newvals{'xmldc'} ne '') {
 9926:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 9927:     }
 9928:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 9929:                                              $dom);
 9930:     if ($putresult eq 'ok') {
 9931:         my @items = @types;
 9932:         if ($newvals{'xml'}) {
 9933:             push(@items,'xmldc');
 9934:         }
 9935:         foreach my $item (@items) {
 9936:             if (exists($currautocreate{$item})) {
 9937:                 if ($currautocreate{$item} ne $newvals{$item}) {
 9938:                     $changes{$item} = 1;
 9939:                 }
 9940:             } elsif ($newvals{$item}) {
 9941:                 $changes{$item} = 1;
 9942:             }
 9943:         }
 9944:         if (keys(%changes) > 0) {
 9945:             my @offon = ('off','on'); 
 9946:             $resulttext = &mt('Changes made:').'<ul>';
 9947:             foreach my $item (@types) {
 9948:                 if ($changes{$item}) {
 9949:                     my $newtxt = $offon[$newvals{$item}];
 9950:                     $resulttext .= '<li>'.
 9951:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 9952:                                        '<b>','</b>').
 9953:                                    '</li>';
 9954:                 }
 9955:             }
 9956:             if ($changes{'xmldc'}) {
 9957:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 9958:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 9959:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 9960:             }
 9961:             $resulttext .= '</ul>';
 9962:         } else {
 9963:             $resulttext = &mt('No changes made to auto-creation settings');
 9964:         }
 9965:     } else {
 9966:         $resulttext = '<span class="LC_error">'.
 9967:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9968:     }
 9969:     return $resulttext;
 9970: }
 9971: 
 9972: sub modify_directorysrch {
 9973:     my ($dom,%domconfig) = @_;
 9974:     my ($resulttext,%changes);
 9975:     my %currdirsrch;
 9976:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 9977:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 9978:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 9979:         }
 9980:     }
 9981:     my %title = ( available => 'Institutional directory search available',
 9982:                   localonly => 'Other domains can search institution',
 9983:                   lcavailable => 'LON-CAPA directory search available',
 9984:                   lclocalonly => 'Other domains can search LON-CAPA domain', 
 9985:                   searchby => 'Search types',
 9986:                   searchtypes => 'Search latitude');
 9987:     my @offon = ('off','on');
 9988:     my @otherdoms = ('Yes','No');
 9989: 
 9990:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 9991:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 9992:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 9993: 
 9994:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9995:     if (keys(%{$usertypes}) == 0) {
 9996:         @cansearch = ('default');
 9997:     } else {
 9998:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 9999:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
10000:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
10001:                     push(@{$changes{'cansearch'}},$type);
10002:                 }
10003:             }
10004:             foreach my $type (@cansearch) {
10005:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
10006:                     push(@{$changes{'cansearch'}},$type);
10007:                 }
10008:             }
10009:         } else {
10010:             push(@{$changes{'cansearch'}},@cansearch);
10011:         }
10012:     }
10013: 
10014:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
10015:         foreach my $by (@{$currdirsrch{'searchby'}}) {
10016:             if (!grep(/^\Q$by\E$/,@searchby)) {
10017:                 push(@{$changes{'searchby'}},$by);
10018:             }
10019:         }
10020:         foreach my $by (@searchby) {
10021:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
10022:                 push(@{$changes{'searchby'}},$by);
10023:             }
10024:         }
10025:     } else {
10026:         push(@{$changes{'searchby'}},@searchby);
10027:     }
10028: 
10029:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
10030:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
10031:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
10032:                 push(@{$changes{'searchtypes'}},$type);
10033:             }
10034:         }
10035:         foreach my $type (@searchtypes) {
10036:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
10037:                 push(@{$changes{'searchtypes'}},$type);
10038:             }
10039:         }
10040:     } else {
10041:         if (exists($currdirsrch{'searchtypes'})) {
10042:             foreach my $type (@searchtypes) {  
10043:                 if ($type ne $currdirsrch{'searchtypes'}) { 
10044:                     push(@{$changes{'searchtypes'}},$type);
10045:                 }
10046:             }
10047:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
10048:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
10049:             }   
10050:         } else {
10051:             push(@{$changes{'searchtypes'}},@searchtypes); 
10052:         }
10053:     }
10054: 
10055:     my %dirsrch_hash =  (
10056:             directorysrch => { available => $env{'form.dirsrch_available'},
10057:                                cansearch => \@cansearch,
10058:                                localonly => $env{'form.dirsrch_instlocalonly'},
10059:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
10060:                                lcavailable => $env{'form.dirsrch_domavailable'},
10061:                                searchby => \@searchby,
10062:                                searchtypes => \@searchtypes,
10063:                              }
10064:             );
10065:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
10066:                                              $dom);
10067:     if ($putresult eq 'ok') {
10068:         if (exists($currdirsrch{'available'})) {
10069:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
10070:                  $changes{'available'} = 1;
10071:              }
10072:         } else {
10073:             if ($env{'form.dirsrch_available'} eq '1') {
10074:                 $changes{'available'} = 1;
10075:             }
10076:         }
10077:         if (exists($currdirsrch{'lcavailable'})) {
10078:              if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
10079:                  $changes{'lcavailable'} = 1;
10080:              }
10081:         } else {
10082:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
10083:                 $changes{'lcavailable'} = 1;
10084:             }
10085:         }
10086:         if (exists($currdirsrch{'localonly'})) {
10087:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
10088:                  $changes{'localonly'} = 1;
10089:              }
10090:         } else {
10091:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
10092:                 $changes{'localonly'} = 1;
10093:             }
10094:         }
10095:         if (exists($currdirsrch{'lclocalonly'})) {
10096:              if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
10097:                  $changes{'lclocalonly'} = 1;
10098:              }
10099:         } else {
10100:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
10101:                 $changes{'lclocalonly'} = 1;
10102:             }
10103:         }
10104:         if (keys(%changes) > 0) {
10105:             $resulttext = &mt('Changes made:').'<ul>';
10106:             if ($changes{'available'}) {
10107:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
10108:             }
10109:             if ($changes{'lcavailable'}) {
10110:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
10111:             }
10112:             if ($changes{'localonly'}) {
10113:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
10114:             }
10115:             if ($changes{'lclocalonly'}) {
10116:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
10117:             }   
10118:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
10119:                 my $chgtext;
10120:                 if (ref($usertypes) eq 'HASH') {
10121:                     if (keys(%{$usertypes}) > 0) {
10122:                         foreach my $type (@{$types}) {
10123:                             if (grep(/^\Q$type\E$/,@cansearch)) {
10124:                                 $chgtext .= $usertypes->{$type}.'; ';
10125:                             }
10126:                         }
10127:                         if (grep(/^default$/,@cansearch)) {
10128:                             $chgtext .= $othertitle;
10129:                         } else {
10130:                             $chgtext =~ s/\; $//;
10131:                         }
10132:                         $resulttext .=
10133:                             '<li>'.
10134:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
10135:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
10136:                             '</li>';
10137:                     }
10138:                 }
10139:             }
10140:             if (ref($changes{'searchby'}) eq 'ARRAY') {
10141:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
10142:                 my $chgtext;
10143:                 foreach my $type (@{$titleorder}) {
10144:                     if (grep(/^\Q$type\E$/,@searchby)) {
10145:                         if (defined($searchtitles->{$type})) {
10146:                             $chgtext .= $searchtitles->{$type}.'; ';
10147:                         }
10148:                     }
10149:                 }
10150:                 $chgtext =~ s/\; $//;
10151:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
10152:             }
10153:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
10154:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
10155:                 my $chgtext;
10156:                 foreach my $type (@{$srchtypeorder}) {
10157:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
10158:                         if (defined($srchtypes_desc->{$type})) {
10159:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
10160:                         }
10161:                     }
10162:                 }
10163:                 $chgtext =~ s/\; $//;
10164:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
10165:             }
10166:             $resulttext .= '</ul>';
10167:         } else {
10168:             $resulttext = &mt('No changes made to directory search settings');
10169:         }
10170:     } else {
10171:         $resulttext = '<span class="LC_error">'.
10172:                       &mt('An error occurred: [_1]',$putresult).'</span>';
10173:     }
10174:     return $resulttext;
10175: }
10176: 
10177: sub modify_contacts {
10178:     my ($dom,$lastactref,%domconfig) = @_;
10179:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
10180:     if (ref($domconfig{'contacts'}) eq 'HASH') {
10181:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
10182:             $currsetting{$key} = $domconfig{'contacts'}{$key};
10183:         }
10184:     }
10185:     my (%others,%to,%bcc);
10186:     my @contacts = ('supportemail','adminemail');
10187:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
10188:                     'requestsmail','updatesmail','idconflictsmail');
10189:     my @toggles = ('reporterrors','reportupdates');
10190:     foreach my $type (@mailings) {
10191:         @{$newsetting{$type}} = 
10192:             &Apache::loncommon::get_env_multiple('form.'.$type);
10193:         foreach my $item (@contacts) {
10194:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
10195:                 $contacts_hash{contacts}{$type}{$item} = 1;
10196:             } else {
10197:                 $contacts_hash{contacts}{$type}{$item} = 0;
10198:             }
10199:         }  
10200:         $others{$type} = $env{'form.'.$type.'_others'};
10201:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
10202:         if ($type eq 'helpdeskmail') {
10203:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
10204:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
10205:         }
10206:     }
10207:     foreach my $item (@contacts) {
10208:         $to{$item} = $env{'form.'.$item};
10209:         $contacts_hash{'contacts'}{$item} = $to{$item};
10210:     }
10211:     foreach my $item (@toggles) {
10212:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
10213:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
10214:         }
10215:     }
10216:     if (keys(%currsetting) > 0) {
10217:         foreach my $item (@contacts) {
10218:             if ($to{$item} ne $currsetting{$item}) {
10219:                 $changes{$item} = 1;
10220:             }
10221:         }
10222:         foreach my $type (@mailings) {
10223:             foreach my $item (@contacts) {
10224:                 if (ref($currsetting{$type}) eq 'HASH') {
10225:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
10226:                         push(@{$changes{$type}},$item);
10227:                     }
10228:                 } else {
10229:                     push(@{$changes{$type}},@{$newsetting{$type}});
10230:                 }
10231:             }
10232:             if ($others{$type} ne $currsetting{$type}{'others'}) {
10233:                 push(@{$changes{$type}},'others');
10234:             }
10235:             if ($type eq 'helpdeskmail') {   
10236:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
10237:                     push(@{$changes{$type}},'bcc'); 
10238:                 }
10239:             }
10240:         }
10241:     } else {
10242:         my %default;
10243:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
10244:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
10245:         $default{'errormail'} = 'adminemail';
10246:         $default{'packagesmail'} = 'adminemail';
10247:         $default{'helpdeskmail'} = 'supportemail';
10248:         $default{'lonstatusmail'} = 'adminemail';
10249:         $default{'requestsmail'} = 'adminemail';
10250:         $default{'updatesmail'} = 'adminemail';
10251:         foreach my $item (@contacts) {
10252:            if ($to{$item} ne $default{$item}) {
10253:               $changes{$item} = 1;
10254:            }
10255:         }
10256:         foreach my $type (@mailings) {
10257:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
10258:                
10259:                 push(@{$changes{$type}},@{$newsetting{$type}});
10260:             }
10261:             if ($others{$type} ne '') {
10262:                 push(@{$changes{$type}},'others');
10263:             }
10264:             if ($type eq 'helpdeskmail') {
10265:                 if ($bcc{$type} ne '') {
10266:                     push(@{$changes{$type}},'bcc');
10267:                 }
10268:             }
10269:         }
10270:     }
10271:     foreach my $item (@toggles) {
10272:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
10273:             $changes{$item} = 1;
10274:         } elsif ((!$env{'form.'.$item}) &&
10275:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
10276:             $changes{$item} = 1;
10277:         }
10278:     }
10279:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
10280:                                              $dom);
10281:     if ($putresult eq 'ok') {
10282:         if (keys(%changes) > 0) {
10283:             &Apache::loncommon::devalidate_domconfig_cache($dom);
10284:             if (ref($lastactref) eq 'HASH') {
10285:                 $lastactref->{'domainconfig'} = 1;
10286:             }
10287:             my ($titles,$short_titles)  = &contact_titles();
10288:             $resulttext = &mt('Changes made:').'<ul>';
10289:             foreach my $item (@contacts) {
10290:                 if ($changes{$item}) {
10291:                     $resulttext .= '<li>'.$titles->{$item}.
10292:                                     &mt(' set to: ').
10293:                                     '<span class="LC_cusr_emph">'.
10294:                                     $to{$item}.'</span></li>';
10295:                 }
10296:             }
10297:             foreach my $type (@mailings) {
10298:                 if (ref($changes{$type}) eq 'ARRAY') {
10299:                     $resulttext .= '<li>'.$titles->{$type}.': ';
10300:                     my @text;
10301:                     foreach my $item (@{$newsetting{$type}}) {
10302:                         push(@text,$short_titles->{$item});
10303:                     }
10304:                     if ($others{$type} ne '') {
10305:                         push(@text,$others{$type});
10306:                     }
10307:                     $resulttext .= '<span class="LC_cusr_emph">'.
10308:                                    join(', ',@text).'</span>';
10309:                     if ($type eq 'helpdeskmail') {
10310:                         if ($bcc{$type} ne '') {
10311:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
10312:                         }
10313:                     }
10314:                     $resulttext .= '</li>';
10315:                 }
10316:             }
10317:             my @offon = ('off','on');
10318:             if ($changes{'reporterrors'}) {
10319:                 $resulttext .= '<li>'.
10320:                                &mt('E-mail error reports to [_1] set to "'.
10321:                                    $offon[$env{'form.reporterrors'}].'".',
10322:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10323:                                        &mt('LON-CAPA core group - MSU'),600,500)).
10324:                                '</li>';
10325:             }
10326:             if ($changes{'reportupdates'}) {
10327:                 $resulttext .= '<li>'.
10328:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
10329:                                     $offon[$env{'form.reportupdates'}].'".',
10330:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10331:                                         &mt('LON-CAPA core group - MSU'),600,500)).
10332:                                 '</li>';
10333:             }
10334:             $resulttext .= '</ul>';
10335:         } else {
10336:             $resulttext = &mt('No changes made to contact information');
10337:         }
10338:     } else {
10339:         $resulttext = '<span class="LC_error">'.
10340:             &mt('An error occurred: [_1].',$putresult).'</span>';
10341:     }
10342:     return $resulttext;
10343: }
10344: 
10345: sub modify_usercreation {
10346:     my ($dom,%domconfig) = @_;
10347:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
10348:     my $warningmsg;
10349:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
10350:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
10351:             if ($key eq 'cancreate') {
10352:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
10353:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
10354:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
10355:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
10356:                             ($item eq 'recaptchaversion')) {
10357:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10358:                         } else {
10359:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10360:                         }
10361:                     }
10362:                 }
10363:             } elsif ($key eq 'email_rule') {
10364:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
10365:             } else {
10366:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
10367:             }
10368:         }
10369:     }
10370:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
10371:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
10372:     my @contexts = ('author','course','requestcrs');
10373:     foreach my $item(@contexts) {
10374:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
10375:     }
10376:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
10377:         foreach my $item (@contexts) {
10378:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
10379:                 push(@{$changes{'cancreate'}},$item);
10380:             }
10381:         }
10382:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
10383:         foreach my $item (@contexts) {
10384:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
10385:                 if ($cancreate{$item} ne 'any') {
10386:                     push(@{$changes{'cancreate'}},$item);
10387:                 }
10388:             } else {
10389:                 if ($cancreate{$item} ne 'none') {
10390:                     push(@{$changes{'cancreate'}},$item);
10391:                 }
10392:             }
10393:         }
10394:     } else {
10395:         foreach my $item (@contexts)  {
10396:             push(@{$changes{'cancreate'}},$item);
10397:         }
10398:     }
10399: 
10400:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
10401:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
10402:             if (!grep(/^\Q$type\E$/,@username_rule)) {
10403:                 push(@{$changes{'username_rule'}},$type);
10404:             }
10405:         }
10406:         foreach my $type (@username_rule) {
10407:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
10408:                 push(@{$changes{'username_rule'}},$type);
10409:             }
10410:         }
10411:     } else {
10412:         push(@{$changes{'username_rule'}},@username_rule);
10413:     }
10414: 
10415:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
10416:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
10417:             if (!grep(/^\Q$type\E$/,@id_rule)) {
10418:                 push(@{$changes{'id_rule'}},$type);
10419:             }
10420:         }
10421:         foreach my $type (@id_rule) {
10422:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
10423:                 push(@{$changes{'id_rule'}},$type);
10424:             }
10425:         }
10426:     } else {
10427:         push(@{$changes{'id_rule'}},@id_rule);
10428:     }
10429: 
10430:     my @authen_contexts = ('author','course','domain');
10431:     my @authtypes = ('int','krb4','krb5','loc');
10432:     my %authhash;
10433:     foreach my $item (@authen_contexts) {
10434:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
10435:         foreach my $auth (@authtypes) {
10436:             if (grep(/^\Q$auth\E$/,@authallowed)) {
10437:                 $authhash{$item}{$auth} = 1;
10438:             } else {
10439:                 $authhash{$item}{$auth} = 0;
10440:             }
10441:         }
10442:     }
10443:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
10444:         foreach my $item (@authen_contexts) {
10445:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
10446:                 foreach my $auth (@authtypes) {
10447:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
10448:                         push(@{$changes{'authtypes'}},$item);
10449:                         last;
10450:                     }
10451:                 }
10452:             }
10453:         }
10454:     } else {
10455:         foreach my $item (@authen_contexts) {
10456:             push(@{$changes{'authtypes'}},$item);
10457:         }
10458:     }
10459: 
10460:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
10461:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
10462:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
10463:     $save_usercreate{'id_rule'} = \@id_rule;
10464:     $save_usercreate{'username_rule'} = \@username_rule,
10465:     $save_usercreate{'authtypes'} = \%authhash;
10466: 
10467:     my %usercreation_hash =  (
10468:         usercreation     => \%save_usercreate,
10469:     );
10470: 
10471:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
10472:                                              $dom);
10473: 
10474:     if ($putresult eq 'ok') {
10475:         if (keys(%changes) > 0) {
10476:             $resulttext = &mt('Changes made:').'<ul>';
10477:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
10478:                 my %lt = &usercreation_types();
10479:                 foreach my $type (@{$changes{'cancreate'}}) {
10480:                     my $chgtext = $lt{$type}.', ';
10481:                     if ($cancreate{$type} eq 'none') {
10482:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
10483:                     } elsif ($cancreate{$type} eq 'any') {
10484:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
10485:                     } elsif ($cancreate{$type} eq 'official') {
10486:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
10487:                     } elsif ($cancreate{$type} eq 'unofficial') {
10488:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
10489:                     }
10490:                     $resulttext .= '<li>'.$chgtext.'</li>';
10491:                 }
10492:             }
10493:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
10494:                 my ($rules,$ruleorder) = 
10495:                     &Apache::lonnet::inst_userrules($dom,'username');
10496:                 my $chgtext = '<ul>';
10497:                 foreach my $type (@username_rule) {
10498:                     if (ref($rules->{$type}) eq 'HASH') {
10499:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
10500:                     }
10501:                 }
10502:                 $chgtext .= '</ul>';
10503:                 if (@username_rule > 0) {
10504:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
10505:                 } else {
10506:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
10507:                 }
10508:             }
10509:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
10510:                 my ($idrules,$idruleorder) = 
10511:                     &Apache::lonnet::inst_userrules($dom,'id');
10512:                 my $chgtext = '<ul>';
10513:                 foreach my $type (@id_rule) {
10514:                     if (ref($idrules->{$type}) eq 'HASH') {
10515:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
10516:                     }
10517:                 }
10518:                 $chgtext .= '</ul>';
10519:                 if (@id_rule > 0) {
10520:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
10521:                 } else {
10522:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
10523:                 }
10524:             }
10525:             my %authname = &authtype_names();
10526:             my %context_title = &context_names();
10527:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
10528:                 my $chgtext = '<ul>';
10529:                 foreach my $type (@{$changes{'authtypes'}}) {
10530:                     my @allowed;
10531:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
10532:                     foreach my $auth (@authtypes) {
10533:                         if ($authhash{$type}{$auth}) {
10534:                             push(@allowed,$authname{$auth});
10535:                         }
10536:                     }
10537:                     if (@allowed > 0) {
10538:                         $chgtext .= join(', ',@allowed).'</li>';
10539:                     } else {
10540:                         $chgtext .= &mt('none').'</li>';
10541:                     }
10542:                 }
10543:                 $chgtext .= '</ul>';
10544:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
10545:                 $resulttext .= '</li>';
10546:             }
10547:             $resulttext .= '</ul>';
10548:         } else {
10549:             $resulttext = &mt('No changes made to user creation settings');
10550:         }
10551:     } else {
10552:         $resulttext = '<span class="LC_error">'.
10553:             &mt('An error occurred: [_1]',$putresult).'</span>';
10554:     }
10555:     if ($warningmsg ne '') {
10556:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
10557:     }
10558:     return $resulttext;
10559: }
10560: 
10561: sub modify_selfcreation {
10562:     my ($dom,%domconfig) = @_;
10563:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
10564:     my (%save_usercreate,%save_usermodify);
10565:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10566:     if (ref($types) eq 'ARRAY') {
10567:         $usertypes->{'default'} = $othertitle;
10568:         push(@{$types},'default');
10569:     }
10570: #
10571: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
10572: #
10573:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
10574:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
10575:             if ($key eq 'cancreate') {
10576:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
10577:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
10578:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
10579:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
10580:                             ($item eq 'recaptchaversion') ||
10581:                             ($item eq 'emailusername') || ($item eq 'notify') ||
10582:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
10583:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10584:                         } else {
10585:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10586:                         }
10587:                     }
10588:                 }
10589:             } elsif ($key eq 'email_rule') {
10590:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
10591:             } else {
10592:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
10593:             }
10594:         }
10595:     }
10596: #
10597: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
10598: #
10599:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
10600:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
10601:             if ($key eq 'selfcreate') {
10602:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
10603:             } else {
10604:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
10605:             }
10606:         }
10607:     }
10608: 
10609:     my @contexts = ('selfcreate');
10610:     @{$cancreate{'selfcreate'}} = ();
10611:     %{$cancreate{'emailusername'}} = ();
10612:     @{$cancreate{'statustocreate'}} = ();
10613:     %{$cancreate{'selfcreateprocessing'}} = ();
10614:     %{$cancreate{'shibenv'}} = ();
10615:     my %selfcreatetypes = (
10616:                              sso   => 'users authenticated by institutional single sign on',
10617:                              login => 'users authenticated by institutional log-in',
10618:                              email => 'users who provide a valid e-mail address for use as username',
10619:                           );
10620: #
10621: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
10622: # is permitted.
10623: #
10624: 
10625:     my @statuses;
10626:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
10627:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
10628:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
10629:         }
10630:     }
10631:     push(@statuses,'default');
10632: 
10633:     foreach my $item ('login','sso','email') {
10634:         if ($item eq 'email') {
10635:             if ($env{'form.cancreate_email'}) {
10636:                 push(@{$cancreate{'selfcreate'}},'email');
10637:                 push(@contexts,'selfcreateprocessing');
10638:                 foreach my $type (@statuses) {
10639:                     if ($type eq 'default') {
10640:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
10641:                     } else { 
10642:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
10643:                     }
10644:                 }
10645:             }
10646:         } else {
10647:             if ($env{'form.cancreate_'.$item}) {
10648:                 push(@{$cancreate{'selfcreate'}},$item);
10649:             }
10650:         }
10651:     }
10652:     my (@email_rule,%userinfo,%savecaptcha);
10653:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
10654: #
10655: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
10656: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
10657: #
10658: 
10659:     if ($env{'form.cancreate_email'}) {
10660:         push(@contexts,'emailusername');
10661:         if (ref($types) eq 'ARRAY') {
10662:             foreach my $type (@{$types}) {
10663:                 if (ref($infofields) eq 'ARRAY') {
10664:                     foreach my $field (@{$infofields}) {
10665:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
10666:                             $cancreate{'emailusername'}{$type}{$field} = $1;
10667:                         }
10668:                     }
10669:                 }
10670:             }
10671:         }
10672: #
10673: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
10674: # queued requests for self-creation of account using e-mail address as username
10675: #
10676: 
10677:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
10678:         @approvalnotify = sort(@approvalnotify);
10679:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
10680:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
10681:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
10682:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
10683:                     push(@{$changes{'cancreate'}},'notify');
10684:                 }
10685:             } else {
10686:                 if ($cancreate{'notify'}{'approval'}) {
10687:                     push(@{$changes{'cancreate'}},'notify');
10688:                 }
10689:             }
10690:         } elsif ($cancreate{'notify'}{'approval'}) {
10691:             push(@{$changes{'cancreate'}},'notify');
10692:         }
10693: 
10694: #
10695: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
10696: #
10697:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
10698:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
10699:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
10700:             if (@{$curr_usercreation{'email_rule'}} > 0) {
10701:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
10702:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
10703:                         push(@{$changes{'email_rule'}},$type);
10704:                     }
10705:                 }
10706:             }
10707:             if (@email_rule > 0) {
10708:                 foreach my $type (@email_rule) {
10709:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
10710:                         push(@{$changes{'email_rule'}},$type);
10711:                     }
10712:                 }
10713:             }
10714:         } elsif (@email_rule > 0) {
10715:             push(@{$changes{'email_rule'}},@email_rule);
10716:         }
10717:     }
10718: #  
10719: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
10720: # institutional log-in.
10721: #
10722:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
10723:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10724:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
10725:                ($domdefaults{'auth_def'} eq 'localauth'))) {
10726:             $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.').' '.
10727:                           &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.');
10728:         }
10729:     }
10730:     my @fields = ('lastname','firstname','middlename','generation',
10731:                   'permanentemail','id');
10732:     my @shibfields = (@fields,'inststatus');
10733:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
10734: #
10735: # Where usernames may created for institutional log-in and/or institutional single sign on:
10736: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
10737: # may self-create accounts 
10738: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
10739: # which the user may supply, if institutional data is unavailable.
10740: #
10741:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
10742:         if (ref($types) eq 'ARRAY') {
10743:             if (@{$types} > 1) {
10744:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
10745:                 push(@contexts,'statustocreate');
10746:             } else {
10747:                 undef($cancreate{'statustocreate'});
10748:             } 
10749:             foreach my $type (@{$types}) {
10750:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
10751:                 foreach my $field (@fields) {
10752:                     if (grep(/^\Q$field\E$/,@modifiable)) {
10753:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
10754:                     } else {
10755:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
10756:                     }
10757:                 }
10758:             }
10759:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
10760:                 foreach my $type (@{$types}) {
10761:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
10762:                         foreach my $field (@fields) {
10763:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
10764:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
10765:                                 push(@{$changes{'selfcreate'}},$type);
10766:                                 last;
10767:                             }
10768:                         }
10769:                     }
10770:                 }
10771:             } else {
10772:                 foreach my $type (@{$types}) {
10773:                     push(@{$changes{'selfcreate'}},$type);
10774:                 }
10775:             }
10776:         }
10777:         foreach my $field (@shibfields) {
10778:             if ($env{'form.shibenv_'.$field} ne '') {
10779:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
10780:             }
10781:         }
10782:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
10783:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
10784:                 foreach my $field (@shibfields) {
10785:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
10786:                         push(@{$changes{'cancreate'}},'shibenv');
10787:                     }
10788:                 }
10789:             } else {
10790:                 foreach my $field (@shibfields) {
10791:                     if ($env{'form.shibenv_'.$field}) {
10792:                         push(@{$changes{'cancreate'}},'shibenv');
10793:                         last;
10794:                     }
10795:                 }
10796:             }
10797:         }
10798:     }
10799:     foreach my $item (@contexts) {
10800:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
10801:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
10802:                 if (ref($cancreate{$item}) eq 'ARRAY') {
10803:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
10804:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10805:                             push(@{$changes{'cancreate'}},$item);
10806:                         }
10807:                     }
10808:                 }
10809:             }
10810:             if (ref($cancreate{$item}) eq 'ARRAY') {
10811:                 foreach my $type (@{$cancreate{$item}}) {
10812:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
10813:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10814:                             push(@{$changes{'cancreate'}},$item);
10815:                         }
10816:                     }
10817:                 }
10818:             }
10819:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
10820:             if (ref($cancreate{$item}) eq 'HASH') {
10821:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
10822:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
10823:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
10824:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
10825:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10826:                                     push(@{$changes{'cancreate'}},$item);
10827:                                 }
10828:                             }
10829:                         }
10830:                     } elsif ($item eq 'selfcreateprocessing') {
10831:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
10832:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10833:                                 push(@{$changes{'cancreate'}},$item);
10834:                             }
10835:                         }
10836:                     } else {
10837:                         if (!$cancreate{$item}{$curr}) {
10838:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10839:                                 push(@{$changes{'cancreate'}},$item);
10840:                             }
10841:                         }
10842:                     }
10843:                 }
10844:                 foreach my $field (keys(%{$cancreate{$item}})) {
10845:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
10846:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
10847:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
10848:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
10849:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10850:                                         push(@{$changes{'cancreate'}},$item);
10851:                                     }
10852:                                 }
10853:                             } else {
10854:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10855:                                     push(@{$changes{'cancreate'}},$item);
10856:                                 }
10857:                             }
10858:                         }
10859:                     } elsif ($item eq 'selfcreateprocessing') {
10860:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
10861:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10862:                                 push(@{$changes{'cancreate'}},$item);
10863:                             }
10864:                         }
10865:                     } else {
10866:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
10867:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10868:                                 push(@{$changes{'cancreate'}},$item);
10869:                             }
10870:                         }
10871:                     }
10872:                 }
10873:             }
10874:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
10875:             if (ref($cancreate{$item}) eq 'ARRAY') {
10876:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
10877:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10878:                         push(@{$changes{'cancreate'}},$item);
10879:                     }
10880:                 }
10881:             } elsif (ref($cancreate{$item}) eq 'HASH') {
10882:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
10883:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10884:                         push(@{$changes{'cancreate'}},$item);
10885:                     }
10886:                 }
10887:             }
10888:         } elsif ($item eq 'emailusername') {
10889:             if (ref($cancreate{$item}) eq 'HASH') {
10890:                 foreach my $type (keys(%{$cancreate{$item}})) {
10891:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
10892:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
10893:                             if ($cancreate{$item}{$type}{$field}) {
10894:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
10895:                                     push(@{$changes{'cancreate'}},$item);
10896:                                 }
10897:                                 last;
10898:                             }
10899:                         }
10900:                     }
10901:                 }
10902:             }
10903:         }
10904:     }
10905: #
10906: # Populate %save_usercreate hash with updates to self-creation configuration.
10907: #
10908:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
10909:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
10910:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
10911:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
10912:     if (ref($cancreate{'notify'}) eq 'HASH') {
10913:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
10914:     }
10915:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
10916:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
10917:     }
10918:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
10919:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
10920:     }
10921:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
10922:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
10923:     }
10924:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
10925:     $save_usercreate{'emailrule'} = \@email_rule;
10926: 
10927:     my %userconfig_hash = (
10928:             usercreation     => \%save_usercreate,
10929:             usermodification => \%save_usermodify,
10930:     );
10931:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
10932:                                              $dom);
10933: #
10934: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
10935: #
10936:     if ($putresult eq 'ok') {
10937:         if (keys(%changes) > 0) {
10938:             $resulttext = &mt('Changes made:').'<ul>';
10939:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
10940:                 my %lt = &selfcreation_types();
10941:                 foreach my $type (@{$changes{'cancreate'}}) {
10942:                     my $chgtext;
10943:                     if ($type eq 'selfcreate') {
10944:                         if (@{$cancreate{$type}} == 0) {
10945:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
10946:                         } else {
10947:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
10948:                                         '<ul>';
10949:                             foreach my $case (@{$cancreate{$type}}) {
10950:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
10951:                             }
10952:                             $chgtext .= '</ul>';
10953:                             if (ref($cancreate{$type}) eq 'ARRAY') {
10954:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
10955:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
10956:                                         if (@{$cancreate{'statustocreate'}} == 0) {
10957:                                             $chgtext .= '<br />'.
10958:                                                         '<span class="LC_warning">'.
10959:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
10960:                                                         '</span>';
10961:                                         }
10962:                                     }
10963:                                 }
10964:                             }
10965:                         }
10966:                     } elsif ($type eq 'shibenv') {
10967:                         if (keys(%{$cancreate{$type}}) == 0) {
10968:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
10969:                         } else {
10970:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
10971:                                         '<ul>';
10972:                             foreach my $field (@shibfields) {
10973:                                 next if ($cancreate{$type}{$field} eq '');
10974:                                 if ($field eq 'inststatus') {
10975:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
10976:                                 } else {
10977:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
10978:                                 }
10979:                             }
10980:                             $chgtext .= '</ul>';
10981:                         }  
10982:                     } elsif ($type eq 'statustocreate') {
10983:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
10984:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
10985:                             if (@{$cancreate{'selfcreate'}} > 0) {
10986:                                 if (@{$cancreate{'statustocreate'}} == 0) {
10987:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
10988:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
10989:                                         $chgtext .= '<br />'.
10990:                                                     '<span class="LC_warning">'.
10991:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
10992:                                                     '</span>';
10993:                                     }
10994:                                 } elsif (ref($usertypes) eq 'HASH') {
10995:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
10996:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
10997:                                     } else {
10998:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
10999:                                     }
11000:                                     $chgtext .= '<ul>';
11001:                                     foreach my $case (@{$cancreate{$type}}) {
11002:                                         if ($case eq 'default') {
11003:                                             $chgtext .= '<li>'.$othertitle.'</li>';
11004:                                         } else {
11005:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
11006:                                         }
11007:                                     }
11008:                                     $chgtext .= '</ul>';
11009:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
11010:                                         $chgtext .= '<br /><span class="LC_warning">'.
11011:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
11012:                                                     '</span>';
11013:                                     }
11014:                                 }
11015:                             } else {
11016:                                 if (@{$cancreate{$type}} == 0) {
11017:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
11018:                                 } else {
11019:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
11020:                                 }
11021:                             }
11022:                         }
11023:                     } elsif ($type eq 'selfcreateprocessing') {
11024:                         my %choices = &Apache::lonlocal::texthash (
11025:                                                                     automatic => 'Automatic approval',
11026:                                                                     approval  => 'Queued for approval',
11027:                                                                   );
11028:                         if (@statuses > 1) {
11029:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
11030:                                         '<ul>';
11031:                            foreach my $type (@statuses) {
11032:                                if ($type eq 'default') {
11033:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
11034:                                } else {
11035:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
11036:                                }
11037:                            }
11038:                            $chgtext .= '</ul>';
11039:                         } else {
11040:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
11041:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
11042:                         }
11043:                     } elsif ($type eq 'captcha') {
11044:                         if ($savecaptcha{$type} eq 'notused') {
11045:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
11046:                         } else {
11047:                             my %captchas = &captcha_phrases();
11048:                             if ($captchas{$savecaptcha{$type}}) {
11049:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
11050:                             } else {
11051:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
11052:                             }
11053:                         }
11054:                     } elsif ($type eq 'recaptchakeys') {
11055:                         my ($privkey,$pubkey);
11056:                         if (ref($savecaptcha{$type}) eq 'HASH') {
11057:                             $pubkey = $savecaptcha{$type}{'public'};
11058:                             $privkey = $savecaptcha{$type}{'private'};
11059:                         }
11060:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
11061:                         if (!$pubkey) {
11062:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
11063:                         } else {
11064:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
11065:                         }
11066:                         if (!$privkey) {
11067:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
11068:                         } else {
11069:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
11070:                         }
11071:                         $chgtext .= '</ul>';
11072:                     } elsif ($type eq 'recaptchaversion') {
11073:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
11074:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
11075:                         }
11076:                     } elsif ($type eq 'emailusername') {
11077:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
11078:                             if (ref($types) eq 'ARRAY') {
11079:                                 foreach my $type (@{$types}) {
11080:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
11081:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
11082:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
11083:                                                     '<ul>';
11084:                                             foreach my $field (@{$infofields}) {
11085:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
11086:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
11087:                                                 }
11088:                                             }
11089:                                             $chgtext .= '</ul>';
11090:                                         } else {
11091:                                             $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 />';
11092:                                         }
11093:                                     } else {
11094:                                         $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 />';
11095:                                     }
11096:                                 }
11097:                             }
11098:                         }
11099:                     } elsif ($type eq 'notify') {
11100:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
11101:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
11102:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
11103:                                 if ($cancreate{'notify'}{'approval'}) {
11104:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
11105:                                 }
11106:                             }
11107:                         }
11108:                     }
11109:                     if ($chgtext) {
11110:                         $resulttext .= '<li>'.$chgtext.'</li>';
11111:                     }
11112:                 }
11113:             }
11114:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
11115:                 my ($emailrules,$emailruleorder) =
11116:                     &Apache::lonnet::inst_userrules($dom,'email');
11117:                 my $chgtext = '<ul>';
11118:                 foreach my $type (@email_rule) {
11119:                     if (ref($emailrules->{$type}) eq 'HASH') {
11120:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
11121:                     }
11122:                 }
11123:                 $chgtext .= '</ul>';
11124:                 if (@email_rule > 0) {
11125:                     $resulttext .= '<li>'.
11126:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
11127:                                        $chgtext.
11128:                                    '</li>';
11129:                 } else {
11130:                     $resulttext .= '<li>'.
11131:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
11132:                                    '</li>';
11133:                 }
11134:             }
11135:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
11136:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
11137:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11138:                 foreach my $type (@{$changes{'selfcreate'}}) {
11139:                     my $typename = $type;
11140:                     if (ref($usertypes) eq 'HASH') {
11141:                         if ($usertypes->{$type} ne '') {
11142:                             $typename = $usertypes->{$type};
11143:                         }
11144:                     }
11145:                     my @modifiable;
11146:                     $resulttext .= '<li>'.
11147:                                     &mt('Self-creation of account by users with status: [_1]',
11148:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
11149:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
11150:                     foreach my $field (@fields) {
11151:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
11152:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
11153:                         }
11154:                     }
11155:                     if (@modifiable > 0) {
11156:                         $resulttext .= join(', ',@modifiable);
11157:                     } else {
11158:                         $resulttext .= &mt('none');
11159:                     }
11160:                     $resulttext .= '</li>';
11161:                 }
11162:                 $resulttext .= '</ul></li>';
11163:             }
11164:             $resulttext .= '</ul>';
11165:         } else {
11166:             $resulttext = &mt('No changes made to self-creation settings');
11167:         }
11168:     } else {
11169:         $resulttext = '<span class="LC_error">'.
11170:             &mt('An error occurred: [_1]',$putresult).'</span>';
11171:     }
11172:     if ($warningmsg ne '') {
11173:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11174:     }
11175:     return $resulttext;
11176: }
11177: 
11178: sub process_captcha {
11179:     my ($container,$changes,$newsettings,$current) = @_;
11180:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
11181:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
11182:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
11183:         $newsettings->{'captcha'} = 'original';
11184:     }
11185:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
11186:         if ($container eq 'cancreate') {
11187:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11188:                 push(@{$changes->{'cancreate'}},'captcha');
11189:             } elsif (!defined($changes->{'cancreate'})) {
11190:                 $changes->{'cancreate'} = ['captcha'];
11191:             }
11192:         } else {
11193:             $changes->{'captcha'} = 1;
11194:         }
11195:     }
11196:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
11197:     if ($newsettings->{'captcha'} eq 'recaptcha') {
11198:         $newpub = $env{'form.'.$container.'_recaptchapub'};
11199:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
11200:         $newpub =~ s/[^\w\-]//g;
11201:         $newpriv =~ s/[^\w\-]//g;
11202:         $newsettings->{'recaptchakeys'} = {
11203:                                              public  => $newpub,
11204:                                              private => $newpriv,
11205:                                           };
11206:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
11207:         $newversion =~ s/\D//g;
11208:         if ($newversion ne '2') {
11209:             $newversion = 1;
11210:         }
11211:         $newsettings->{'recaptchaversion'} = $newversion;
11212:     }
11213:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
11214:         $currpub = $current->{'recaptchakeys'}{'public'};
11215:         $currpriv = $current->{'recaptchakeys'}{'private'};
11216:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
11217:             $newsettings->{'recaptchakeys'} = {
11218:                                                  public  => '',
11219:                                                  private => '',
11220:                                               }
11221:         }
11222:     }
11223:     if ($current->{'captcha'} eq 'recaptcha') {
11224:         $currversion = $current->{'recaptchaversion'};
11225:         if ($currversion ne '2') {
11226:             $currversion = 1;
11227:         }
11228:     }
11229:     if ($currversion ne $newversion) {
11230:         if ($container eq 'cancreate') {
11231:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11232:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
11233:             } elsif (!defined($changes->{'cancreate'})) {
11234:                 $changes->{'cancreate'} = ['recaptchaversion'];
11235:             }
11236:         } else {
11237:             $changes->{'recaptchaversion'} = 1;
11238:         }
11239:     }
11240:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
11241:         if ($container eq 'cancreate') {
11242:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11243:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
11244:             } elsif (!defined($changes->{'cancreate'})) {
11245:                 $changes->{'cancreate'} = ['recaptchakeys'];
11246:             }
11247:         } else {
11248:             $changes->{'recaptchakeys'} = 1;
11249:         }
11250:     }
11251:     return;
11252: }
11253: 
11254: sub modify_usermodification {
11255:     my ($dom,%domconfig) = @_;
11256:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
11257:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11258:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11259:             if ($key eq 'selfcreate') {
11260:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
11261:             } else {  
11262:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
11263:             }
11264:         }
11265:     }
11266:     my @contexts = ('author','course');
11267:     my %context_title = (
11268:                            author => 'In author context',
11269:                            course => 'In course context',
11270:                         );
11271:     my @fields = ('lastname','firstname','middlename','generation',
11272:                   'permanentemail','id');
11273:     my %roles = (
11274:                   author => ['ca','aa'],
11275:                   course => ['st','ep','ta','in','cr'],
11276:                 );
11277:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11278:     foreach my $context (@contexts) {
11279:         foreach my $role (@{$roles{$context}}) {
11280:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
11281:             foreach my $item (@fields) {
11282:                 if (grep(/^\Q$item\E$/,@modifiable)) {
11283:                     $modifyhash{$context}{$role}{$item} = 1;
11284:                 } else {
11285:                     $modifyhash{$context}{$role}{$item} = 0;
11286:                 }
11287:             }
11288:         }
11289:         if (ref($curr_usermodification{$context}) eq 'HASH') {
11290:             foreach my $role (@{$roles{$context}}) {
11291:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
11292:                     foreach my $field (@fields) {
11293:                         if ($modifyhash{$context}{$role}{$field} ne 
11294:                                 $curr_usermodification{$context}{$role}{$field}) {
11295:                             push(@{$changes{$context}},$role);
11296:                             last;
11297:                         }
11298:                     }
11299:                 }
11300:             }
11301:         } else {
11302:             foreach my $context (@contexts) {
11303:                 foreach my $role (@{$roles{$context}}) {
11304:                     push(@{$changes{$context}},$role);
11305:                 }
11306:             }
11307:         }
11308:     }
11309:     my %usermodification_hash =  (
11310:                                    usermodification => \%modifyhash,
11311:                                  );
11312:     my $putresult = &Apache::lonnet::put_dom('configuration',
11313:                                              \%usermodification_hash,$dom);
11314:     if ($putresult eq 'ok') {
11315:         if (keys(%changes) > 0) {
11316:             $resulttext = &mt('Changes made: ').'<ul>';
11317:             foreach my $context (@contexts) {
11318:                 if (ref($changes{$context}) eq 'ARRAY') {
11319:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
11320:                     if (ref($changes{$context}) eq 'ARRAY') {
11321:                         foreach my $role (@{$changes{$context}}) {
11322:                             my $rolename;
11323:                             if ($role eq 'cr') {
11324:                                 $rolename = &mt('Custom');
11325:                             } else {
11326:                                 $rolename = &Apache::lonnet::plaintext($role);
11327:                             }
11328:                             my @modifiable;
11329:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
11330:                             foreach my $field (@fields) {
11331:                                 if ($modifyhash{$context}{$role}{$field}) {
11332:                                     push(@modifiable,$fieldtitles{$field});
11333:                                 }
11334:                             }
11335:                             if (@modifiable > 0) {
11336:                                 $resulttext .= join(', ',@modifiable);
11337:                             } else {
11338:                                 $resulttext .= &mt('none'); 
11339:                             }
11340:                             $resulttext .= '</li>';
11341:                         }
11342:                         $resulttext .= '</ul></li>';
11343:                     }
11344:                 }
11345:             }
11346:             $resulttext .= '</ul>';
11347:         } else {
11348:             $resulttext = &mt('No changes made to user modification settings');
11349:         }
11350:     } else {
11351:         $resulttext = '<span class="LC_error">'.
11352:             &mt('An error occurred: [_1]',$putresult).'</span>';
11353:     }
11354:     return $resulttext;
11355: }
11356: 
11357: sub modify_defaults {
11358:     my ($dom,$lastactref,%domconfig) = @_;
11359:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
11360:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11361:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
11362:     my @authtypes = ('internal','krb4','krb5','localauth');
11363:     foreach my $item (@items) {
11364:         $newvalues{$item} = $env{'form.'.$item};
11365:         if ($item eq 'auth_def') {
11366:             if ($newvalues{$item} ne '') {
11367:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
11368:                     push(@errors,$item);
11369:                 }
11370:             }
11371:         } elsif ($item eq 'lang_def') {
11372:             if ($newvalues{$item} ne '') {
11373:                 if ($newvalues{$item} =~ /^(\w+)/) {
11374:                     my $langcode = $1;
11375:                     if ($langcode ne 'x_chef') {
11376:                         if (code2language($langcode) eq '') {
11377:                             push(@errors,$item);
11378:                         }
11379:                     }
11380:                 } else {
11381:                     push(@errors,$item);
11382:                 }
11383:             }
11384:         } elsif ($item eq 'timezone_def') {
11385:             if ($newvalues{$item} ne '') {
11386:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
11387:                     push(@errors,$item);   
11388:                 }
11389:             }
11390:         } elsif ($item eq 'datelocale_def') {
11391:             if ($newvalues{$item} ne '') {
11392:                 my @datelocale_ids = DateTime::Locale->ids();
11393:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
11394:                     push(@errors,$item);
11395:                 }
11396:             }
11397:         } elsif ($item eq 'portal_def') {
11398:             if ($newvalues{$item} ne '') {
11399:                 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])\/?$/) {
11400:                     push(@errors,$item);
11401:                 }
11402:             }
11403:         }
11404:         if (grep(/^\Q$item\E$/,@errors)) {
11405:             $newvalues{$item} = $domdefaults{$item};
11406:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
11407:             $changes{$item} = 1;
11408:         }
11409:         $domdefaults{$item} = $newvalues{$item};
11410:     }
11411:     my %defaults_hash = (
11412:                          defaults => \%newvalues,
11413:                         );
11414:     my $title = &defaults_titles();
11415: 
11416:     my $currinststatus;
11417:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
11418:         $currinststatus = $domconfig{'inststatus'};
11419:     } else {
11420:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11421:         $currinststatus = {
11422:                              inststatustypes => $usertypes,
11423:                              inststatusorder => $types,
11424:                              inststatusguest => [],
11425:                           };
11426:     }
11427:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
11428:     my @allpos;
11429:     my %guests;
11430:     my %alltypes;
11431:     my ($currtitles,$currguests,$currorder);
11432:     if (ref($currinststatus) eq 'HASH') {
11433:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
11434:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
11435:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
11436:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
11437:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
11438:                     }
11439:                 }
11440:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
11441:                     my $position = $env{'form.inststatus_pos_'.$type};
11442:                     $position =~ s/\D+//g;
11443:                     $allpos[$position] = $type;
11444:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
11445:                     $alltypes{$type} =~ s/`//g;
11446:                     if ($env{'form.inststatus_guest_'.$type}) {
11447:                         $guests{$type} = 1;
11448:                     }
11449:                 }
11450:             }
11451:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
11452:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
11453:             }
11454:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
11455:             $currtitles =~ s/,$//;
11456:         }
11457:     }
11458:     if ($env{'form.addinststatus'}) {
11459:         my $newtype = $env{'form.addinststatus'};
11460:         $newtype =~ s/\W//g;
11461:         unless (exists($alltypes{$newtype})) {
11462:             if ($env{'form.addinststatus_guest'}) {
11463:                 $guests{$newtype} = 1;
11464:             }
11465:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
11466:             $alltypes{$newtype} =~ s/`//g; 
11467:             my $position = $env{'form.addinststatus_pos'};
11468:             $position =~ s/\D+//g;
11469:             if ($position ne '') {
11470:                 $allpos[$position] = $newtype;
11471:             }
11472:         }
11473:     }
11474:     my (@orderedstatus,@orderedguests);
11475:     foreach my $type (@allpos) {
11476:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
11477:             push(@orderedstatus,$type);
11478:             if ($guests{$type}) {
11479:                 push(@orderedguests,$type);
11480:             }
11481:         }
11482:     }
11483:     foreach my $type (keys(%alltypes)) {
11484:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
11485:             delete($alltypes{$type});
11486:         }
11487:     }
11488:     $defaults_hash{'inststatus'} = {
11489:                                      inststatustypes => \%alltypes,
11490:                                      inststatusorder => \@orderedstatus,
11491:                                      inststatusguest => \@orderedguests,
11492:                                    };
11493:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
11494:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
11495:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
11496:         }
11497:     }
11498:     if ($currorder ne join(',',@orderedstatus)) {
11499:         $changes{'inststatus'}{'inststatusorder'} = 1;
11500:     }
11501:     if ($currguests ne join(',',@orderedguests)) {
11502:         $changes{'inststatus'}{'inststatusguest'} = 1;
11503:     }
11504:     my $newtitles;
11505:     foreach my $item (@orderedstatus) {
11506:         $newtitles .= $alltypes{$item}.',';
11507:     }
11508:     $newtitles =~ s/,$//;
11509:     if ($currtitles ne $newtitles) {
11510:         $changes{'inststatus'}{'inststatustypes'} = 1;
11511:     }
11512:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
11513:                                              $dom);
11514:     if ($putresult eq 'ok') {
11515:         if (keys(%changes) > 0) {
11516:             $resulttext = &mt('Changes made:').'<ul>';
11517:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
11518:             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";
11519:             foreach my $item (sort(keys(%changes))) {
11520:                 if ($item eq 'inststatus') {
11521:                     if (ref($changes{'inststatus'}) eq 'HASH') {
11522:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
11523:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
11524:                             foreach my $type (@orderedstatus) { 
11525:                                 $resulttext .= $alltypes{$type}.', ';
11526:                             }
11527:                             $resulttext =~ s/, $//;
11528:                             $resulttext .= '</li>';
11529:                         }
11530:                         if ($changes{'inststatus'}{'inststatusguest'}) {
11531:                             $resulttext .= '<li>'; 
11532:                             if (@orderedguests) {
11533:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
11534:                                 foreach my $type (@orderedguests) {
11535:                                     $resulttext .= $alltypes{$type}.', ';
11536:                                 }
11537:                                 $resulttext =~ s/, $//;
11538:                             } else {
11539:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
11540:                             }
11541:                             $resulttext .= '</li>';
11542:                         }
11543:                     }
11544:                 } else {
11545:                     my $value = $env{'form.'.$item};
11546:                     if ($value eq '') {
11547:                         $value = &mt('none');
11548:                     } elsif ($item eq 'auth_def') {
11549:                         my %authnames = &authtype_names();
11550:                         my %shortauth = (
11551:                                           internal   => 'int',
11552:                                           krb4       => 'krb4',
11553:                                           krb5       => 'krb5',
11554:                                           localauth  => 'loc',
11555:                         );
11556:                         $value = $authnames{$shortauth{$value}};
11557:                     }
11558:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
11559:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
11560:                 }
11561:             }
11562:             $resulttext .= '</ul>';
11563:             $mailmsgtext .= "\n";
11564:             my $cachetime = 24*60*60;
11565:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11566:             if (ref($lastactref) eq 'HASH') {
11567:                 $lastactref->{'domdefaults'} = 1;
11568:             }
11569:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
11570:                 my $notify = 1;
11571:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
11572:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
11573:                         $notify = 0;
11574:                     }
11575:                 }
11576:                 if ($notify) {
11577:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
11578:                                                "LON-CAPA Domain Settings Change - $dom",
11579:                                                $mailmsgtext);
11580:                 }
11581:             }
11582:         } else {
11583:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
11584:         }
11585:     } else {
11586:         $resulttext = '<span class="LC_error">'.
11587:             &mt('An error occurred: [_1]',$putresult).'</span>';
11588:     }
11589:     if (@errors > 0) {
11590:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
11591:         foreach my $item (@errors) {
11592:             $resulttext .= ' "'.$title->{$item}.'",';
11593:         }
11594:         $resulttext =~ s/,$//;
11595:     }
11596:     return $resulttext;
11597: }
11598: 
11599: sub modify_scantron {
11600:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
11601:     my ($resulttext,%confhash,%changes,$errors);
11602:     my $custom = 'custom.tab';
11603:     my $default = 'default.tab';
11604:     my $servadm = $r->dir_config('lonAdmEMail');
11605:     my ($configuserok,$author_ok,$switchserver) = 
11606:         &config_check($dom,$confname,$servadm);
11607:     if ($env{'form.scantronformat.filename'} ne '') {
11608:         my $error;
11609:         if ($configuserok eq 'ok') {
11610:             if ($switchserver) {
11611:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
11612:             } else {
11613:                 if ($author_ok eq 'ok') {
11614:                     my ($result,$scantronurl) =
11615:                         &publishlogo($r,'upload','scantronformat',$dom,
11616:                                      $confname,'scantron','','',$custom);
11617:                     if ($result eq 'ok') {
11618:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
11619:                         $changes{'scantronformat'} = 1;
11620:                     } else {
11621:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
11622:                     }
11623:                 } else {
11624:                     $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);
11625:                 }
11626:             }
11627:         } else {
11628:             $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);
11629:         }
11630:         if ($error) {
11631:             &Apache::lonnet::logthis($error);
11632:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11633:         }
11634:     }
11635:     if (ref($domconfig{'scantron'}) eq 'HASH') {
11636:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
11637:             if ($env{'form.scantronformat_del'}) {
11638:                 $confhash{'scantron'}{'scantronformat'} = '';
11639:                 $changes{'scantronformat'} = 1;
11640:             }
11641:         }
11642:     }
11643:     if (keys(%confhash) > 0) {
11644:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
11645:                                                  $dom);
11646:         if ($putresult eq 'ok') {
11647:             if (keys(%changes) > 0) {
11648:                 if (ref($confhash{'scantron'}) eq 'HASH') {
11649:                     $resulttext = &mt('Changes made:').'<ul>';
11650:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
11651:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
11652:                     } else {
11653:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
11654:                     }
11655:                     $resulttext .= '</ul>';
11656:                 } else {
11657:                     $resulttext = &mt('Changes made to bubblesheet format file.');
11658:                 }
11659:                 $resulttext .= '</ul>';
11660:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
11661:                 if (ref($lastactref) eq 'HASH') {
11662:                     $lastactref->{'domainconfig'} = 1;
11663:                 }
11664:             } else {
11665:                 $resulttext = &mt('No changes made to bubblesheet format file');
11666:             }
11667:         } else {
11668:             $resulttext = '<span class="LC_error">'.
11669:                 &mt('An error occurred: [_1]',$putresult).'</span>';
11670:         }
11671:     } else {
11672:         $resulttext = &mt('No changes made to bubblesheet format file'); 
11673:     }
11674:     if ($errors) {
11675:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
11676:                        $errors.'</ul>';
11677:     }
11678:     return $resulttext;
11679: }
11680: 
11681: sub modify_coursecategories {
11682:     my ($dom,$lastactref,%domconfig) = @_;
11683:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
11684:         $cathash);
11685:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
11686:     my @catitems = ('unauth','auth');
11687:     my @cattypes = ('std','domonly','codesrch','none');
11688:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
11689:         $cathash = $domconfig{'coursecategories'}{'cats'};
11690:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
11691:             $changes{'togglecats'} = 1;
11692:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
11693:         }
11694:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
11695:             $changes{'categorize'} = 1;
11696:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
11697:         }
11698:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
11699:             $changes{'togglecatscomm'} = 1;
11700:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
11701:         }
11702:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
11703:             $changes{'categorizecomm'} = 1;
11704:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
11705: 
11706:         }
11707:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
11708:             $changes{'togglecatsplace'} = 1;
11709:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
11710:         }
11711:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
11712:             $changes{'categorizeplace'} = 1;
11713:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
11714:         }
11715:         foreach my $item (@catitems) {
11716:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
11717:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
11718:                     $changes{$item} = 1;
11719:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
11720:                 }
11721:             }
11722:         }
11723:     } else {
11724:         $changes{'togglecats'} = 1;
11725:         $changes{'categorize'} = 1;
11726:         $changes{'togglecatscomm'} = 1;
11727:         $changes{'categorizecomm'} = 1;
11728:         $changes{'togglecatsplace'} = 1;
11729:         $changes{'categorizeplace'} = 1;
11730:         $domconfig{'coursecategories'} = {
11731:                                              togglecats => $env{'form.togglecats'},
11732:                                              categorize => $env{'form.categorize'},
11733:                                              togglecatscomm => $env{'form.togglecatscomm'},
11734:                                              categorizecomm => $env{'form.categorizecomm'},
11735:                                              togglecatsplace => $env{'form.togglecatsplace'},
11736:                                              categorizeplace => $env{'form.categorizeplace'},
11737:                                          };
11738:         foreach my $item (@catitems) {
11739:             if ($env{'form.coursecat_'.$item} ne 'std') {
11740:                 $changes{$item} = 1;
11741:             }
11742:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
11743:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
11744:             }
11745:         }
11746:     }
11747:     if (ref($cathash) eq 'HASH') {
11748:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
11749:             push (@deletecategory,'instcode::0');
11750:         }
11751:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
11752:             push(@deletecategory,'communities::0');
11753:         }
11754:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
11755:             push(@deletecategory,'placement::0');
11756:         }
11757:     }
11758:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
11759:     if (ref($cathash) eq 'HASH') {
11760:         if (@deletecategory > 0) {
11761:             #FIXME Need to remove category from all courses using a deleted category 
11762:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
11763:             foreach my $item (@deletecategory) {
11764:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
11765:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
11766:                     $deletions{$item} = 1;
11767:                     &recurse_cat_deletes($item,$cathash,\%deletions);
11768:                 }
11769:             }
11770:         }
11771:         foreach my $item (keys(%{$cathash})) {
11772:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
11773:             if ($cathash->{$item} ne $env{'form.'.$item}) {
11774:                 $reorderings{$item} = 1;
11775:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
11776:             }
11777:             if ($env{'form.addcategory_name_'.$item} ne '') {
11778:                 my $newcat = $env{'form.addcategory_name_'.$item};
11779:                 my $newdepth = $depth+1;
11780:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
11781:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
11782:                 $adds{$newitem} = 1; 
11783:             }
11784:             if ($env{'form.subcat_'.$item} ne '') {
11785:                 my $newcat = $env{'form.subcat_'.$item};
11786:                 my $newdepth = $depth+1;
11787:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
11788:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
11789:                 $adds{$newitem} = 1;
11790:             }
11791:         }
11792:     }
11793:     if ($env{'form.instcode'} eq '1') {
11794:         if (ref($cathash) eq 'HASH') {
11795:             my $newitem = 'instcode::0';
11796:             if ($cathash->{$newitem} eq '') {  
11797:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
11798:                 $adds{$newitem} = 1;
11799:             }
11800:         } else {
11801:             my $newitem = 'instcode::0';
11802:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
11803:             $adds{$newitem} = 1;
11804:         }
11805:     }
11806:     if ($env{'form.communities'} eq '1') {
11807:         if (ref($cathash) eq 'HASH') {
11808:             my $newitem = 'communities::0';
11809:             if ($cathash->{$newitem} eq '') {
11810:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
11811:                 $adds{$newitem} = 1;
11812:             }
11813:         } else {
11814:             my $newitem = 'communities::0';
11815:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
11816:             $adds{$newitem} = 1;
11817:         }
11818:     }
11819:     if ($env{'form.placement'} eq '1') {
11820:         if (ref($cathash) eq 'HASH') {
11821:             my $newitem = 'placement::0';
11822:             if ($cathash->{$newitem} eq '') {
11823:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
11824:                 $adds{$newitem} = 1;
11825:             }
11826:         } else {
11827:             my $newitem = 'placement::0';
11828:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
11829:             $adds{$newitem} = 1;
11830:         }
11831:     }
11832:     if ($env{'form.addcategory_name'} ne '') {
11833:         if (($env{'form.addcategory_name'} ne 'instcode') &&
11834:             ($env{'form.addcategory_name'} ne 'communities') &&
11835:             ($env{'form.addcategory_name'} ne 'placement')) {
11836:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
11837:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
11838:             $adds{$newitem} = 1;
11839:         }
11840:     }
11841:     my $putresult;
11842:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
11843:         if (keys(%deletions) > 0) {
11844:             foreach my $key (keys(%deletions)) {
11845:                 if ($predelallitems{$key} ne '') {
11846:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
11847:                 }
11848:             }
11849:         }
11850:         my (@chkcats,@chktrails,%chkallitems);
11851:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
11852:         if (ref($chkcats[0]) eq 'ARRAY') {
11853:             my $depth = 0;
11854:             my $chg = 0;
11855:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
11856:                 my $name = $chkcats[0][$i];
11857:                 my $item;
11858:                 if ($name eq '') {
11859:                     $chg ++;
11860:                 } else {
11861:                     $item = &escape($name).'::0';
11862:                     if ($chg) {
11863:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
11864:                     }
11865:                     $depth ++; 
11866:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
11867:                     $depth --;
11868:                 }
11869:             }
11870:         }
11871:     }
11872:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
11873:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
11874:         if ($putresult eq 'ok') {
11875:             my %title = (
11876:                          togglecats     => 'Show/Hide a course in catalog',
11877:                          categorize     => 'Assign a category to a course',
11878:                          togglecatscomm => 'Show/Hide a community in catalog',
11879:                          categorizecomm => 'Assign a category to a community',
11880:                         );
11881:             my %level = (
11882:                          dom  => 'set in Domain ("Modify Course/Community")',
11883:                          crs  => 'set in Course ("Course Configuration")',
11884:                          comm => 'set in Community ("Community Configuration")',
11885:                          none     => 'No catalog',
11886:                          std      => 'Standard catalog',
11887:                          domonly  => 'Domain-only catalog',
11888:                          codesrch => 'Code search form',
11889:                         );
11890:             $resulttext = &mt('Changes made:').'<ul>';
11891:             if ($changes{'togglecats'}) {
11892:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
11893:             }
11894:             if ($changes{'categorize'}) {
11895:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
11896:             }
11897:             if ($changes{'togglecatscomm'}) {
11898:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
11899:             }
11900:             if ($changes{'categorizecomm'}) {
11901:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
11902:             }
11903:             if ($changes{'unauth'}) {
11904:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
11905:             }
11906:             if ($changes{'auth'}) {
11907:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
11908:             }
11909:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
11910:                 my $cathash;
11911:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
11912:                     $cathash = $domconfig{'coursecategories'}{'cats'};
11913:                 } else {
11914:                     $cathash = {};
11915:                 } 
11916:                 my (@cats,@trails,%allitems);
11917:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
11918:                 if (keys(%deletions) > 0) {
11919:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
11920:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
11921:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
11922:                     }
11923:                     $resulttext .= '</ul></li>';
11924:                 }
11925:                 if (keys(%reorderings) > 0) {
11926:                     my %sort_by_trail;
11927:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
11928:                     foreach my $key (keys(%reorderings)) {
11929:                         if ($allitems{$key} ne '') {
11930:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
11931:                         }
11932:                     }
11933:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
11934:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
11935:                     }
11936:                     $resulttext .= '</ul></li>';
11937:                 }
11938:                 if (keys(%adds) > 0) {
11939:                     my %sort_by_trail;
11940:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
11941:                     foreach my $key (keys(%adds)) {
11942:                         if ($allitems{$key} ne '') {
11943:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
11944:                         }
11945:                     }
11946:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
11947:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
11948:                     }
11949:                     $resulttext .= '</ul></li>';
11950:                 }
11951:             }
11952:             $resulttext .= '</ul>';
11953:             if ($changes{'unauth'} || $changes{'auth'}) {
11954:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
11955:                 if ($changes{'auth'}) {
11956:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
11957:                 }
11958:                 if ($changes{'unauth'}) {
11959:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
11960:                 }
11961:                 my $cachetime = 24*60*60;
11962:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11963:                 if (ref($lastactref) eq 'HASH') {
11964:                     $lastactref->{'domdefaults'} = 1;
11965:                 }
11966:             }
11967:         } else {
11968:             $resulttext = '<span class="LC_error">'.
11969:                           &mt('An error occurred: [_1]',$putresult).'</span>';
11970:         }
11971:     } else {
11972:         $resulttext = &mt('No changes made to course and community categories');
11973:     }
11974:     return $resulttext;
11975: }
11976: 
11977: sub modify_serverstatuses {
11978:     my ($dom,%domconfig) = @_;
11979:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
11980:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
11981:         %currserverstatus = %{$domconfig{'serverstatuses'}};
11982:     }
11983:     my @pages = &serverstatus_pages();
11984:     foreach my $type (@pages) {
11985:         $newserverstatus{$type}{'namedusers'} = '';
11986:         $newserverstatus{$type}{'machines'} = '';
11987:         if (defined($env{'form.'.$type.'_namedusers'})) {
11988:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
11989:             my @okusers;
11990:             foreach my $user (@users) {
11991:                 my ($uname,$udom) = split(/:/,$user);
11992:                 if (($udom =~ /^$match_domain$/) &&   
11993:                     (&Apache::lonnet::domain($udom)) &&
11994:                     ($uname =~ /^$match_username$/)) {
11995:                     if (!grep(/^\Q$user\E/,@okusers)) {
11996:                         push(@okusers,$user);
11997:                     }
11998:                 }
11999:             }
12000:             if (@okusers > 0) {
12001:                  @okusers = sort(@okusers);
12002:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
12003:             }
12004:         }
12005:         if (defined($env{'form.'.$type.'_machines'})) {
12006:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
12007:             my @okmachines;
12008:             foreach my $ip (@machines) {
12009:                 my @parts = split(/\./,$ip);
12010:                 next if (@parts < 4);
12011:                 my $badip = 0;
12012:                 for (my $i=0; $i<4; $i++) {
12013:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
12014:                         $badip = 1;
12015:                         last;
12016:                     }
12017:                 }
12018:                 if (!$badip) {
12019:                     push(@okmachines,$ip);     
12020:                 }
12021:             }
12022:             @okmachines = sort(@okmachines);
12023:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
12024:         }
12025:     }
12026:     my %serverstatushash =  (
12027:                                 serverstatuses => \%newserverstatus,
12028:                             );
12029:     foreach my $type (@pages) {
12030:         foreach my $setting ('namedusers','machines') {
12031:             my (@current,@new);
12032:             if (ref($currserverstatus{$type}) eq 'HASH') {
12033:                 if ($currserverstatus{$type}{$setting} ne '') { 
12034:                     @current = split(/,/,$currserverstatus{$type}{$setting});
12035:                 }
12036:             }
12037:             if ($newserverstatus{$type}{$setting} ne '') {
12038:                 @new = split(/,/,$newserverstatus{$type}{$setting});
12039:             }
12040:             if (@current > 0) {
12041:                 if (@new > 0) {
12042:                     foreach my $item (@current) {
12043:                         if (!grep(/^\Q$item\E$/,@new)) {
12044:                             $changes{$type}{$setting} = 1;
12045:                             last;
12046:                         }
12047:                     }
12048:                     foreach my $item (@new) {
12049:                         if (!grep(/^\Q$item\E$/,@current)) {
12050:                             $changes{$type}{$setting} = 1;
12051:                             last;
12052:                         }
12053:                     }
12054:                 } else {
12055:                     $changes{$type}{$setting} = 1;
12056:                 }
12057:             } elsif (@new > 0) {
12058:                 $changes{$type}{$setting} = 1;
12059:             }
12060:         }
12061:     }
12062:     if (keys(%changes) > 0) {
12063:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
12064:         my $putresult = &Apache::lonnet::put_dom('configuration',
12065:                                                  \%serverstatushash,$dom);
12066:         if ($putresult eq 'ok') {
12067:             $resulttext .= &mt('Changes made:').'<ul>';
12068:             foreach my $type (@pages) {
12069:                 if (ref($changes{$type}) eq 'HASH') {
12070:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
12071:                     if ($changes{$type}{'namedusers'}) {
12072:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
12073:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
12074:                         } else {
12075:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
12076:                         }
12077:                     }
12078:                     if ($changes{$type}{'machines'}) {
12079:                         if ($newserverstatus{$type}{'machines'} eq '') {
12080:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
12081:                         } else {
12082:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
12083:                         }
12084: 
12085:                     }
12086:                     $resulttext .= '</ul></li>';
12087:                 }
12088:             }
12089:             $resulttext .= '</ul>';
12090:         } else {
12091:             $resulttext = '<span class="LC_error">'.
12092:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
12093: 
12094:         }
12095:     } else {
12096:         $resulttext = &mt('No changes made to access to server status pages');
12097:     }
12098:     return $resulttext;
12099: }
12100: 
12101: sub modify_helpsettings {
12102:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12103:     my ($resulttext,$errors,%changes,%helphash);
12104:     my %defaultchecked = ('submitbugs' => 'on');
12105:     my @offon = ('off','on');
12106:     my @toggles = ('submitbugs');
12107:     my %current = ('submitbugs' => '',
12108:                    'adhoc'      => {},
12109:                   );
12110:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
12111:         %current = %{$domconfig{'helpsettings'}};
12112:     }
12113:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12114:     foreach my $item (@toggles) {
12115:         if ($defaultchecked{$item} eq 'on') { 
12116:             if ($current{$item} eq '') {
12117:                 if ($env{'form.'.$item} eq '0') {
12118:                     $changes{$item} = 1;
12119:                 }
12120:             } elsif ($current{$item} ne $env{'form.'.$item}) {
12121:                 $changes{$item} = 1;
12122:             }
12123:         } elsif ($defaultchecked{$item} eq 'off') {
12124:             if ($current{$item} eq '') {
12125:                 if ($env{'form.'.$item} eq '1') {
12126:                     $changes{$item} = 1;
12127:                 }
12128:             } elsif ($current{$item} ne $env{'form.'.$item}) {
12129:                 $changes{$item} = 1;
12130:             }
12131:         }
12132:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
12133:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
12134:         }
12135:     }
12136:     my $maxnum = $env{'form.helproles_maxnum'};
12137:     my $confname = $dom.'-domainconfig';
12138:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
12139:     my (@allpos,%newsettings,%changedprivs,$newrole);
12140:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12141:     my @accesstypes = ('all','none','status','inc','exc');
12142:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh']);
12143:     my %lt = &Apache::lonlocal::texthash(
12144:                     s      => 'system',
12145:                     d      => 'domain',
12146:                     order  => 'Display order',
12147:                     access => 'Role usage',
12148:                     all    => 'All',
12149:                     none   => 'None',
12150:                     status => 'Determined based on institutional status',
12151:                     inc    => 'Include all, but exclude specific personnel',
12152:                     exc    => 'Exclude all, but include specific personnel',
12153:     );
12154:     for (my $num=0; $num<=$maxnum; $num++) {
12155:         my ($prefix,$identifier,$rolename,%curr);
12156:         if ($num == $maxnum) {
12157:             next unless ($env{'form.newcusthelp'} == $maxnum);
12158:             $identifier = 'custhelp'.$num;
12159:             $prefix = 'helproles_'.$num;
12160:             $rolename = $env{'form.custhelpname'.$num};
12161:             $rolename=~s/[^A-Za-z0-9]//gs;
12162:             next if ($rolename eq '');
12163:             next if (exists($existing{'rolesdef_'.$rolename}));
12164:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12165:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12166:                                                      $newprivs{'c'},$confname,$dom);
12167:             if ($result ne 'ok') {
12168:                 $errors .= '<li><span class="LC_error">'.
12169:                            &mt('An error occurred storing the new custom role: [_1]',
12170:                            $result).'</span></li>';
12171:                 next;
12172:             } else {
12173:                 $changedprivs{$rolename} = \%newprivs;
12174:                 $newrole = $rolename;
12175:             }
12176:         } else {
12177:             $prefix = 'helproles_'.$num;
12178:             $rolename = $env{'form.'.$prefix};
12179:             next if ($rolename eq '');
12180:             next unless (exists($existing{'rolesdef_'.$rolename}));
12181:             $identifier = 'custhelp'.$num;
12182:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12183:             my %currprivs;
12184:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) = 
12185:                 split(/\_/,$existing{'rolesdef_'.$rolename});
12186:             foreach my $level ('c','d','s') {
12187:                 if ($newprivs{$level} ne $currprivs{$level}) {
12188:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12189:                                                              $newprivs{'c'},$confname,$dom);
12190:                     if ($result ne 'ok') {
12191:                         $errors .= '<li><span class="LC_error">'.
12192:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
12193:                                        $rolename,$result).'</span></li>';
12194:                     } else {
12195:                         $changedprivs{$rolename} = \%newprivs;
12196:                     }
12197:                     last;
12198:                 }
12199:             }
12200:             if (ref($current{'adhoc'}) eq 'HASH') {
12201:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12202:                     %curr = %{$current{'adhoc'}{$rolename}};
12203:                 }
12204:             }
12205:         }
12206:         my $newpos = $env{'form.'.$prefix.'_pos'};
12207:         $newpos =~ s/\D+//g;
12208:         $allpos[$newpos] = $rolename;
12209:         my $newdesc = $env{'form.'.$prefix.'_desc'};
12210:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
12211:         if ($curr{'desc'}) {
12212:             if ($curr{'desc'} ne $newdesc) {
12213:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
12214:                 $newsettings{$rolename}{'desc'} = $newdesc;
12215:             }
12216:         } elsif ($newdesc ne '') {
12217:             $changes{'customrole'}{$rolename}{'desc'} = 1;
12218:             $newsettings{$rolename}{'desc'} = $newdesc;
12219:         }
12220:         my $access = $env{'form.'.$prefix.'_access'};
12221:         if (grep(/^\Q$access\E$/,@accesstypes)) {
12222:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
12223:             if ($access eq 'status') {
12224:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
12225:                 if (scalar(@statuses) == 0) {
12226:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none'; 
12227:                 } else {
12228:                     my (@shownstatus,$numtypes);
12229:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12230:                     if (ref($types) eq 'ARRAY') {
12231:                         $numtypes = scalar(@{$types});
12232:                         foreach my $type (sort(@statuses)) {
12233:                             if ($type eq 'default') {
12234:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12235:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
12236:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12237:                                 push(@shownstatus,$usertypes->{$type});
12238:                             }
12239:                         }
12240:                     }
12241:                     if (grep(/^default$/,@statuses)) {
12242:                         push(@shownstatus,$othertitle);
12243:                     }
12244:                     if (scalar(@shownstatus) == 1+$numtypes) {
12245:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
12246:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
12247:                     } else {
12248:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
12249:                         if (ref($curr{'status'}) eq 'ARRAY') {
12250:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12251:                             if (@diffs) {
12252:                                 $changes{'customrole'}{$rolename}{$access} = 1;
12253:                             }
12254:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12255:                             $changes{'customrole'}{$rolename}{$access} = 1;
12256:                         }
12257:                     }
12258:                 }
12259:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
12260:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
12261:                 my @newspecstaff;
12262:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12263:                 foreach my $person (sort(@personnel)) {
12264:                     if ($domhelpdesk{$person}) {
12265:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
12266:                     }
12267:                 }
12268:                 if (ref($curr{$access}) eq 'ARRAY') {
12269:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12270:                     if (@diffs) {
12271:                         $changes{'customrole'}{$rolename}{$access} = 1;
12272:                     }
12273:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12274:                     $changes{'customrole'}{$rolename}{$access} = 1;
12275:                 }
12276:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12277:                     my ($uname,$udom) = split(/:/,$person);
12278:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
12279:                 }
12280:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
12281:             }
12282:         } else {
12283:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
12284:         }
12285:         unless ($curr{'access'} eq $access) {
12286:             $changes{'customrole'}{$rolename}{'access'} = 1;
12287:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
12288:         }
12289:     }
12290:     if (@allpos > 0) {
12291:         my $idx = 0;
12292:         foreach my $rolename (@allpos) {
12293:             if ($rolename ne '') {
12294:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
12295:                 if (ref($current{'adhoc'}) eq 'HASH') {
12296:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12297:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
12298:                             $changes{'customrole'}{$rolename}{'order'} = 1;
12299:                             $newsettings{$rolename}{'order'} = $idx+1; 
12300:                         }
12301:                     }
12302:                 }
12303:                 $idx ++;
12304:             }
12305:         }
12306:     }
12307:     my $putresult;
12308:     if (keys(%changes) > 0) {
12309:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
12310:         if ($putresult eq 'ok') {
12311:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
12312:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
12313:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
12314:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
12315:                 }
12316:             }
12317:             my $cachetime = 24*60*60;
12318:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12319:             if (ref($lastactref) eq 'HASH') {
12320:                 $lastactref->{'domdefaults'} = 1;
12321:             }
12322:         } else {
12323:             $errors .= '<li><span class="LC_error">'.
12324:                        &mt('An error occurred storing the settings: [_1]',
12325:                            $putresult).'</span></li>';
12326:         }
12327:     }
12328:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
12329:         $resulttext = &mt('Changes made:').'<ul>';
12330:         my (%shownprivs,@levelorder);
12331:         @levelorder = ('c','d','s');
12332:         if ((keys(%changes)) && ($putresult eq 'ok')) {
12333:             foreach my $item (sort(keys(%changes))) {
12334:                 if ($item eq 'submitbugs') {
12335:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
12336:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
12337:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
12338:                 } elsif ($item eq 'customrole') {
12339:                     if (ref($changes{'customrole'}) eq 'HASH') {
12340:                         my @keyorder = ('order','desc','access','status','exc','inc');
12341:                         my %keytext = &Apache::lonlocal::texthash(
12342:                                                                    order  => 'Order',
12343:                                                                    desc   => 'Role description',
12344:                                                                    access => 'Role usage',
12345:                                                                    status => 'Allowed instituional types',
12346:                                                                    exc    => 'Allowed personnel',
12347:                                                                    inc    => 'Disallowed personnel',
12348:                         );
12349:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
12350:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
12351:                                 if ($role eq $newrole) {
12352:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
12353:                                                               $role).'<ul>';
12354:                                 } else {
12355:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
12356:                                                               $role).'<ul>';
12357:                                 }
12358:                                 foreach my $key (@keyorder) {
12359:                                     if ($changes{'customrole'}{$role}{$key}) {
12360:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
12361:                                                                   $keytext{$key},$newsettings{$role}{$key}).
12362:                                                        '</li>';
12363:                                     }
12364:                                 }
12365:                                 if (ref($changedprivs{$role}) eq 'HASH') {
12366:                                     $shownprivs{$role} = 1;
12367:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
12368:                                     foreach my $level (@levelorder) {
12369:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
12370:                                             next if ($item eq '');
12371:                                             my ($priv) = split(/\&/,$item,2);
12372:                                             if (&Apache::lonnet::plaintext($priv)) {
12373:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
12374:                                                 unless ($level eq 'c') {
12375:                                                     $resulttext .= ' ('.$lt{$level}.')';
12376:                                                 }
12377:                                                 $resulttext .= '</li>';
12378:                                             }
12379:                                         }
12380:                                     }
12381:                                     $resulttext .= '</ul>';
12382:                                 }
12383:                                 $resulttext .= '</ul></li>';
12384:                             }
12385:                         }
12386:                     }
12387:                 }
12388:             }
12389:         }
12390:         if (keys(%changedprivs)) {
12391:             foreach my $role (sort(keys(%changedprivs))) {
12392:                 unless ($shownprivs{$role}) {
12393:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
12394:                                               $role).'<ul>'.
12395:                                    '<li>'.&mt('Privileges set to :').'<ul>';
12396:                     foreach my $level (@levelorder) {
12397:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
12398:                             next if ($item eq '');
12399:                             my ($priv) = split(/\&/,$item,2);
12400:                             if (&Apache::lonnet::plaintext($priv)) {
12401:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
12402:                                 unless ($level eq 'c') {
12403:                                     $resulttext .= ' ('.$lt{$level}.')';
12404:                                 }
12405:                                 $resulttext .= '</li>';
12406:                             }
12407:                         }
12408:                     }
12409:                     $resulttext .= '</ul></li></ul></li>';
12410:                 }
12411:             }
12412:         }
12413:         $resulttext .= '</ul>';
12414:     } else {
12415:         $resulttext = &mt('No changes made to help settings');
12416:     }
12417:     if ($errors) {
12418:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
12419:                                     $errors.'</ul>';
12420:     }
12421:     return $resulttext;
12422: }
12423: 
12424: sub modify_coursedefaults {
12425:     my ($dom,$lastactref,%domconfig) = @_;
12426:     my ($resulttext,$errors,%changes,%defaultshash);
12427:     my %defaultchecked = (
12428:                            'canuse_pdfforms' => 'off',
12429:                            'uselcmath'       => 'on',
12430:                            'usejsme'         => 'on'
12431:                          );
12432:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
12433:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
12434:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
12435:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
12436:                    'mysqltables_textbook','mysqltables_placement');
12437:     my @types = ('official','unofficial','community','textbook','placement');
12438:     my %staticdefaults = (
12439:                            anonsurvey_threshold => 10,
12440:                            uploadquota          => 500,
12441:                            postsubmit           => 60,
12442:                            mysqltables          => 172800,
12443:                          );
12444: 
12445:     $defaultshash{'coursedefaults'} = {};
12446: 
12447:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
12448:         if ($domconfig{'coursedefaults'} eq '') {
12449:             $domconfig{'coursedefaults'} = {};
12450:         }
12451:     }
12452: 
12453:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
12454:         foreach my $item (@toggles) {
12455:             if ($defaultchecked{$item} eq 'on') {
12456:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
12457:                     ($env{'form.'.$item} eq '0')) {
12458:                     $changes{$item} = 1;
12459:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
12460:                     $changes{$item} = 1;
12461:                 }
12462:             } elsif ($defaultchecked{$item} eq 'off') {
12463:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
12464:                     ($env{'form.'.$item} eq '1')) {
12465:                     $changes{$item} = 1;
12466:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
12467:                     $changes{$item} = 1;
12468:                 }
12469:             }
12470:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
12471:         }
12472:         foreach my $item (@numbers) {
12473:             my ($currdef,$newdef);
12474:             $newdef = $env{'form.'.$item};
12475:             if ($item eq 'anonsurvey_threshold') {
12476:                 $currdef = $domconfig{'coursedefaults'}{$item};
12477:                 $newdef =~ s/\D//g;
12478:                 if ($newdef eq '' || $newdef < 1) {
12479:                     $newdef = 1;
12480:                 }
12481:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
12482:             } else {
12483:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
12484:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
12485:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
12486:                 }
12487:                 $newdef =~ s/[^\w.\-]//g;
12488:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
12489:             }
12490:             if ($currdef ne $newdef) {
12491:                 my $staticdef;
12492:                 if ($item eq 'anonsurvey_threshold') {
12493:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
12494:                         $changes{$item} = 1;
12495:                     }
12496:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
12497:                     my $setting = $1; 
12498:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
12499:                         $changes{$setting} = 1;
12500:                     }
12501:                 }
12502:             }
12503:         }
12504:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
12505:         my @currclonecode;
12506:         if (ref($currclone) eq 'HASH') {
12507:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
12508:                 @currclonecode = @{$currclone->{'instcode'}};
12509:             }
12510:         }
12511:         my $newclone;
12512:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) { 
12513:             $newclone = $env{'form.canclone'};
12514:         }
12515:         if ($newclone eq 'instcode') {
12516:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
12517:             my (%codedefaults,@code_order,@clonecode);
12518:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
12519:                                                     \@code_order);
12520:             foreach my $item (@code_order) {
12521:                 if (grep(/^\Q$item\E$/,@newcodes)) {
12522:                     push(@clonecode,$item);
12523:                 }
12524:             }
12525:             if (@clonecode) {
12526:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
12527:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
12528:                 if (@diffs) {
12529:                     $changes{'canclone'} = 1;
12530:                 }
12531:             } else {
12532:                 $newclone eq '';
12533:             }
12534:         } elsif ($newclone ne '') {
12535:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone; 
12536:         } 
12537:         if ($newclone ne $currclone) {
12538:             $changes{'canclone'} = 1;
12539:         }
12540:         my %credits;
12541:         foreach my $type (@types) {
12542:             unless ($type eq 'community') {
12543:                 $credits{$type} = $env{'form.'.$type.'_credits'};
12544:                 $credits{$type} =~ s/[^\d.]+//g;
12545:             }
12546:         }
12547:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
12548:             ($env{'form.coursecredits'} eq '1')) {
12549:             $changes{'coursecredits'} = 1;
12550:             foreach my $type (keys(%credits)) {
12551:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
12552:             }
12553:         } else {
12554:             if ($env{'form.coursecredits'} eq '1') { 
12555:                 foreach my $type (@types) {
12556:                     unless ($type eq 'community') {
12557:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {        
12558:                             $changes{'coursecredits'} = 1;
12559:                         }
12560:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
12561:                     }
12562:                 }
12563:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
12564:                 foreach my $type (@types) {
12565:                     unless ($type eq 'community') {
12566:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
12567:                             $changes{'coursecredits'} = 1;
12568:                             last;
12569:                         }
12570:                     }
12571:                 }
12572:             }
12573:         }
12574:         if ($env{'form.postsubmit'} eq '1') {
12575:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
12576:             my %currtimeout;
12577:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
12578:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
12579:                     $changes{'postsubmit'} = 1;
12580:                 }
12581:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
12582:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
12583:                 }
12584:             } else {
12585:                 $changes{'postsubmit'} = 1;
12586:             }
12587:             foreach my $type (@types) {
12588:                 my $timeout = $env{'form.'.$type.'_timeout'};
12589:                 $timeout =~ s/\D//g;
12590:                 if ($timeout == $staticdefaults{'postsubmit'}) {
12591:                     $timeout = '';
12592:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
12593:                     $timeout = '0';
12594:                 }
12595:                 unless ($timeout eq '') {
12596:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
12597:                 }
12598:                 if (exists($currtimeout{$type})) {
12599:                     if ($timeout ne $currtimeout{$type}) {
12600:                         $changes{'postsubmit'} = 1; 
12601:                     }
12602:                 } elsif ($timeout ne '') {
12603:                     $changes{'postsubmit'} = 1;
12604:                 }
12605:             }
12606:         } else {
12607:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
12608:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
12609:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
12610:                     $changes{'postsubmit'} = 1;
12611:                 }
12612:             } else {
12613:                 $changes{'postsubmit'} = 1;
12614:             }
12615:         }
12616:     }
12617:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
12618:                                              $dom);
12619:     if ($putresult eq 'ok') {
12620:         if (keys(%changes) > 0) {
12621:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12622:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
12623:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
12624:                 ($changes{'canclone'}) || ($changes{'mysqltables'})) {
12625:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme') { 
12626:                     if ($changes{$item}) {
12627:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
12628:                     }
12629:                 } 
12630:                 if ($changes{'coursecredits'}) {
12631:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
12632:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
12633:                             $domdefaults{$type.'credits'} =
12634:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
12635:                         }
12636:                     }
12637:                 }
12638:                 if ($changes{'postsubmit'}) {
12639:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
12640:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
12641:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
12642:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
12643:                                 $domdefaults{$type.'postsubtimeout'} =
12644:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
12645:                             }
12646:                         }
12647:                     }
12648:                 }
12649:                 if ($changes{'uploadquota'}) {
12650:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
12651:                         foreach my $type (@types) {
12652:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
12653:                         }
12654:                     }
12655:                 }
12656:                 if ($changes{'canclone'}) {
12657:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
12658:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
12659:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
12660:                             if (@clonecodes) {
12661:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
12662:                             }
12663:                         }
12664:                     } else {
12665:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
12666:                     }
12667:                 }
12668:                 my $cachetime = 24*60*60;
12669:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12670:                 if (ref($lastactref) eq 'HASH') {
12671:                     $lastactref->{'domdefaults'} = 1;
12672:                 }
12673:             }
12674:             $resulttext = &mt('Changes made:').'<ul>';
12675:             foreach my $item (sort(keys(%changes))) {
12676:                 if ($item eq 'canuse_pdfforms') {
12677:                     if ($env{'form.'.$item} eq '1') {
12678:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
12679:                     } else {
12680:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
12681:                     }
12682:                 } elsif ($item eq 'uselcmath') {
12683:                     if ($env{'form.'.$item} eq '1') {
12684:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
12685:                     } else {
12686:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
12687:                     }
12688:                 } elsif ($item eq 'usejsme') {
12689:                     if ($env{'form.'.$item} eq '1') {
12690:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
12691:                     } else {
12692:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>'; 
12693:                     }
12694:                 } elsif ($item eq 'anonsurvey_threshold') {
12695:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
12696:                 } elsif ($item eq 'uploadquota') {
12697:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
12698:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
12699:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
12700:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
12701:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
12702:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
12703:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
12704:                                        '</ul>'.
12705:                                        '</li>';
12706:                     } else {
12707:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
12708:                     }
12709:                 } elsif ($item eq 'mysqltables') {
12710:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
12711:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
12712:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
12713:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
12714:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
12715:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
12716:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
12717:                                        '</ul>'.
12718:                                        '</li>';
12719:                     } else {
12720:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
12721:                     }
12722:                 } elsif ($item eq 'postsubmit') {
12723:                     if ($domdefaults{'postsubmit'} eq 'off') {
12724:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
12725:                     } else {
12726:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
12727:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') { 
12728:                             $resulttext .= &mt('durations:').'<ul>';
12729:                             foreach my $type (@types) {
12730:                                 $resulttext .= '<li>';
12731:                                 my $timeout;
12732:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
12733:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
12734:                                 }
12735:                                 my $display;
12736:                                 if ($timeout eq '0') {
12737:                                     $display = &mt('unlimited');
12738:                                 } elsif ($timeout eq '') {
12739:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
12740:                                 } else {
12741:                                     $display = &mt('[quant,_1,second]',$timeout);
12742:                                 }
12743:                                 if ($type eq 'community') {
12744:                                     $resulttext .= &mt('Communities');
12745:                                 } elsif ($type eq 'official') {
12746:                                     $resulttext .= &mt('Official courses');
12747:                                 } elsif ($type eq 'unofficial') {
12748:                                     $resulttext .= &mt('Unofficial courses');
12749:                                 } elsif ($type eq 'textbook') {
12750:                                     $resulttext .= &mt('Textbook courses');
12751:                                 } elsif ($type eq 'placement') {
12752:                                     $resulttext .= &mt('Placement tests');
12753:                                 }
12754:                                 $resulttext .= ' -- '.$display.'</li>';
12755:                             }
12756:                             $resulttext .= '</ul>';
12757:                         }
12758:                         $resulttext .= '</li>';    
12759:                     }
12760:                 } elsif ($item eq 'coursecredits') {
12761:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
12762:                         if (($domdefaults{'officialcredits'} eq '') &&
12763:                             ($domdefaults{'unofficialcredits'} eq '') &&
12764:                             ($domdefaults{'textbookcredits'} eq '')) {
12765:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
12766:                         } else {
12767:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
12768:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
12769:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
12770:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
12771:                                            '</ul>'.
12772:                                            '</li>';
12773:                         }
12774:                     } else {
12775:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
12776:                     }
12777:                 } elsif ($item eq 'canclone') {
12778:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
12779:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
12780:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
12781:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
12782:                         }
12783:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
12784:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
12785:                     } else {
12786:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>'; 
12787:                     }
12788:                 }
12789:             }
12790:             $resulttext .= '</ul>';
12791:         } else {
12792:             $resulttext = &mt('No changes made to course defaults');
12793:         }
12794:     } else {
12795:         $resulttext = '<span class="LC_error">'.
12796:             &mt('An error occurred: [_1]',$putresult).'</span>';
12797:     }
12798:     return $resulttext;
12799: }
12800: 
12801: sub modify_selfenrollment {
12802:     my ($dom,$lastactref,%domconfig) = @_;
12803:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
12804:     my @types = ('official','unofficial','community','textbook','placement');
12805:     my %titles = &tool_titles();
12806:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
12807:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
12808:     $ordered{'default'} = ['types','registered','approval','limit'];
12809: 
12810:     my (%roles,%shown,%toplevel);
12811:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
12812: 
12813:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
12814:         if ($domconfig{'selfenrollment'} eq '') {
12815:             $domconfig{'selfenrollment'} = {};
12816:         }
12817:     }
12818:     %toplevel = (
12819:                   admin      => 'Configuration Rights',
12820:                   default    => 'Default settings',
12821:                   validation => 'Validation of self-enrollment requests',
12822:                 );
12823:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
12824: 
12825:     if (ref($ordered{'admin'}) eq 'ARRAY') {
12826:         foreach my $item (@{$ordered{'admin'}}) {
12827:             foreach my $type (@types) {
12828:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
12829:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
12830:                 } else {
12831:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
12832:                 }
12833:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
12834:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
12835:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
12836:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
12837:                             push(@{$changes{'admin'}{$type}},$item);
12838:                         }
12839:                     } else {
12840:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
12841:                             push(@{$changes{'admin'}{$type}},$item);
12842:                         }
12843:                     }
12844:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
12845:                     push(@{$changes{'admin'}{$type}},$item);
12846:                 }
12847:             }
12848:         }
12849:     }
12850: 
12851:     foreach my $item (@{$ordered{'default'}}) {
12852:         foreach my $type (@types) {
12853:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
12854:             if ($item eq 'types') {
12855:                 unless (($value eq 'all') || ($value eq 'dom')) {
12856:                     $value = '';
12857:                 }
12858:             } elsif ($item eq 'registered') {
12859:                 unless ($value eq '1') {
12860:                     $value = 0;
12861:                 }
12862:             } elsif ($item eq 'approval') {
12863:                 unless ($value =~ /^[012]$/) {
12864:                     $value = 0;
12865:                 }
12866:             } else {
12867:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
12868:                     $value = 'none';
12869:                 }
12870:             }
12871:             $selfenrollhash{'default'}{$type}{$item} = $value;
12872:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
12873:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
12874:                     if ($selfenrollhash{'default'}{$type}{$item} ne
12875:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
12876:                          push(@{$changes{'default'}{$type}},$item);
12877:                     }
12878:                 } else {
12879:                     push(@{$changes{'default'}{$type}},$item);
12880:                 }
12881:             } else {
12882:                 push(@{$changes{'default'}{$type}},$item);
12883:             }
12884:             if ($item eq 'limit') {
12885:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
12886:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
12887:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
12888:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
12889:                     }
12890:                 } else {
12891:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
12892:                 }
12893:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
12894:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
12895:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
12896:                          push(@{$changes{'default'}{$type}},'cap');
12897:                     }
12898:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
12899:                     push(@{$changes{'default'}{$type}},'cap');
12900:                 }
12901:             }
12902:         }
12903:     }
12904: 
12905:     foreach my $item (@{$itemsref}) {
12906:         if ($item eq 'fields') {
12907:             my @changed;
12908:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
12909:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
12910:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
12911:             }
12912:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
12913:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
12914:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
12915:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
12916:                 } else {
12917:                     @changed = @{$selfenrollhash{'validation'}{$item}};
12918:                 }
12919:             } else {
12920:                 @changed = @{$selfenrollhash{'validation'}{$item}};
12921:             }
12922:             if (@changed) {
12923:                 if ($selfenrollhash{'validation'}{$item}) { 
12924:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
12925:                 } else {
12926:                     $changes{'validation'}{$item} = &mt('None');
12927:                 }
12928:             }
12929:         } else {
12930:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
12931:             if ($item eq 'markup') {
12932:                if ($env{'form.selfenroll_validation_'.$item}) {
12933:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
12934:                }
12935:             }
12936:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
12937:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
12938:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
12939:                 }
12940:             }
12941:         }
12942:     }
12943: 
12944:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
12945:                                              $dom);
12946:     if ($putresult eq 'ok') {
12947:         if (keys(%changes) > 0) {
12948:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12949:             $resulttext = &mt('Changes made:').'<ul>';
12950:             foreach my $key ('admin','default','validation') {
12951:                 if (ref($changes{$key}) eq 'HASH') {
12952:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
12953:                     if ($key eq 'validation') {
12954:                         foreach my $item (@{$itemsref}) {
12955:                             if (exists($changes{$key}{$item})) {
12956:                                 if ($item eq 'markup') {
12957:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
12958:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
12959:                                 } else {  
12960:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
12961:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
12962:                                 }
12963:                             }
12964:                         }
12965:                     } else {
12966:                         foreach my $type (@types) {
12967:                             if ($type eq 'community') {
12968:                                 $roles{'1'} = &mt('Community personnel');
12969:                             } else {
12970:                                 $roles{'1'} = &mt('Course personnel');
12971:                             }
12972:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
12973:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
12974:                                     if ($key eq 'admin') {
12975:                                         my @mgrdc = ();
12976:                                         if (ref($ordered{$key}) eq 'ARRAY') {
12977:                                             foreach my $item (@{$ordered{'admin'}}) {
12978:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
12979:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
12980:                                                         push(@mgrdc,$item);
12981:                                                     }
12982:                                                 }
12983:                                             }
12984:                                             if (@mgrdc) {
12985:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
12986:                                             } else {
12987:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
12988:                                             }
12989:                                         }
12990:                                     } else {
12991:                                         if (ref($ordered{$key}) eq 'ARRAY') {
12992:                                             foreach my $item (@{$ordered{$key}}) {
12993:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
12994:                                                     $domdefaults{$type.'selfenroll'.$item} =
12995:                                                         $selfenrollhash{$key}{$type}{$item};
12996:                                                 }
12997:                                             }
12998:                                         }
12999:                                     }
13000:                                 }
13001:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
13002:                                 foreach my $item (@{$ordered{$key}}) {
13003:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
13004:                                         $resulttext .= '<li>';
13005:                                         if ($key eq 'admin') {
13006:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
13007:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
13008:                                         } else {
13009:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
13010:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
13011:                                         }
13012:                                         $resulttext .= '</li>';
13013:                                     }
13014:                                 }
13015:                                 $resulttext .= '</ul></li>';
13016:                             }
13017:                         }
13018:                         $resulttext .= '</ul></li>'; 
13019:                     }
13020:                 }
13021:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
13022:                     my $cachetime = 24*60*60;
13023:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13024:                     if (ref($lastactref) eq 'HASH') {
13025:                         $lastactref->{'domdefaults'} = 1;
13026:                     }
13027:                 }
13028:             }
13029:             $resulttext .= '</ul>';
13030:         } else {
13031:             $resulttext = &mt('No changes made to self-enrollment settings');
13032:         }
13033:     } else {
13034:         $resulttext = '<span class="LC_error">'.
13035:             &mt('An error occurred: [_1]',$putresult).'</span>';
13036:     }
13037:     return $resulttext;
13038: }
13039: 
13040: sub modify_usersessions {
13041:     my ($dom,$lastactref,%domconfig) = @_;
13042:     my @hostingtypes = ('version','excludedomain','includedomain');
13043:     my @offloadtypes = ('primary','default');
13044:     my %types = (
13045:                   remote => \@hostingtypes,
13046:                   hosted => \@hostingtypes,
13047:                   spares => \@offloadtypes,
13048:                 );
13049:     my @prefixes = ('remote','hosted','spares');
13050:     my @lcversions = &Apache::lonnet::all_loncaparevs();
13051:     my (%by_ip,%by_location,@intdoms,@instdoms);
13052:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
13053:     my @locations = sort(keys(%by_location));
13054:     my (%defaultshash,%changes);
13055:     foreach my $prefix (@prefixes) {
13056:         $defaultshash{'usersessions'}{$prefix} = {};
13057:     }
13058:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13059:     my $resulttext;
13060:     my %iphost = &Apache::lonnet::get_iphost();
13061:     foreach my $prefix (@prefixes) {
13062:         next if ($prefix eq 'spares');
13063:         foreach my $type (@{$types{$prefix}}) {
13064:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
13065:             if ($type eq 'version') {
13066:                 my $value = $env{'form.'.$prefix.'_'.$type};
13067:                 my $okvalue;
13068:                 if ($value ne '') {
13069:                     if (grep(/^\Q$value\E$/,@lcversions)) {
13070:                         $okvalue = $value;
13071:                     }
13072:                 }
13073:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
13074:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
13075:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
13076:                             if ($inuse == 0) {
13077:                                 $changes{$prefix}{$type} = 1;
13078:                             } else {
13079:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
13080:                                     $changes{$prefix}{$type} = 1;
13081:                                 }
13082:                                 if ($okvalue ne '') {
13083:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13084:                                 } 
13085:                             }
13086:                         } else {
13087:                             if (($inuse == 1) && ($okvalue ne '')) {
13088:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13089:                                 $changes{$prefix}{$type} = 1;
13090:                             }
13091:                         }
13092:                     } else {
13093:                         if (($inuse == 1) && ($okvalue ne '')) {
13094:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13095:                             $changes{$prefix}{$type} = 1;
13096:                         }
13097:                     }
13098:                 } else {
13099:                     if (($inuse == 1) && ($okvalue ne '')) {
13100:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13101:                         $changes{$prefix}{$type} = 1;
13102:                     }
13103:                 }
13104:             } else {
13105:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
13106:                 my @okvals;
13107:                 foreach my $val (@vals) {
13108:                     if ($val =~ /:/) {
13109:                         my @items = split(/:/,$val);
13110:                         foreach my $item (@items) {
13111:                             if (ref($by_location{$item}) eq 'ARRAY') {
13112:                                 push(@okvals,$item);
13113:                             }
13114:                         }
13115:                     } else {
13116:                         if (ref($by_location{$val}) eq 'ARRAY') {
13117:                             push(@okvals,$val);
13118:                         }
13119:                     }
13120:                 }
13121:                 @okvals = sort(@okvals);
13122:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
13123:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
13124:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13125:                             if ($inuse == 0) {
13126:                                 $changes{$prefix}{$type} = 1; 
13127:                             } else {
13128:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13129:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
13130:                                 if (@changed > 0) {
13131:                                     $changes{$prefix}{$type} = 1;
13132:                                 }
13133:                             }
13134:                         } else {
13135:                             if ($inuse == 1) {
13136:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13137:                                 $changes{$prefix}{$type} = 1;
13138:                             }
13139:                         } 
13140:                     } else {
13141:                         if ($inuse == 1) {
13142:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13143:                             $changes{$prefix}{$type} = 1;
13144:                         }
13145:                     }
13146:                 } else {
13147:                     if ($inuse == 1) {
13148:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13149:                         $changes{$prefix}{$type} = 1;
13150:                     }
13151:                 }
13152:             }
13153:         }
13154:     }
13155: 
13156:     my @alldoms = &Apache::lonnet::all_domains();
13157:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13158:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
13159:     my $savespares;
13160: 
13161:     foreach my $lonhost (sort(keys(%servers))) {
13162:         my $serverhomeID =
13163:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
13164:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
13165:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
13166:         my %spareschg;
13167:         foreach my $type (@{$types{'spares'}}) {
13168:             my @okspares;
13169:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
13170:             foreach my $server (@checked) {
13171:                 if (&Apache::lonnet::hostname($server) ne '') {
13172:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
13173:                         unless (grep(/^\Q$server\E$/,@okspares)) {
13174:                             push(@okspares,$server);
13175:                         }
13176:                     }
13177:                 }
13178:             }
13179:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
13180:             my $newspare;
13181:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
13182:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
13183:                     $newspare = $new;
13184:                 }
13185:             }
13186:             my @spares;
13187:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
13188:                 @spares = sort(@okspares,$newspare);
13189:             } else {
13190:                 @spares = sort(@okspares);
13191:             }
13192:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
13193:             if (ref($spareid{$lonhost}) eq 'HASH') {
13194:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
13195:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
13196:                     if (@diffs > 0) {
13197:                         $spareschg{$type} = 1;
13198:                     }
13199:                 }
13200:             }
13201:         }
13202:         if (keys(%spareschg) > 0) {
13203:             $changes{'spares'}{$lonhost} = \%spareschg;
13204:         }
13205:     }
13206:     $defaultshash{'usersessions'}{'offloadnow'} = {};
13207:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
13208:     my @okoffload;
13209:     if (@offloadnow) {
13210:         foreach my $server (@offloadnow) {
13211:             if (&Apache::lonnet::hostname($server) ne '') {
13212:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
13213:                     push(@okoffload,$server);
13214:                 }
13215:             }
13216:         }
13217:         if (@okoffload) {
13218:             foreach my $lonhost (@okoffload) {
13219:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
13220:             }
13221:         }
13222:     }
13223:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
13224:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
13225:             if (ref($changes{'spares'}) eq 'HASH') {
13226:                 if (keys(%{$changes{'spares'}}) > 0) {
13227:                     $savespares = 1;
13228:                 }
13229:             }
13230:         } else {
13231:             $savespares = 1;
13232:         }
13233:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
13234:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
13235:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
13236:                     $changes{'offloadnow'} = 1;
13237:                     last;
13238:                 }
13239:             }
13240:             unless ($changes{'offloadnow'}) {
13241:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) { 
13242:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
13243:                         $changes{'offloadnow'} = 1;
13244:                         last;
13245:                     }
13246:                 }
13247:             }
13248:         } elsif (@okoffload) {
13249:             $changes{'offloadnow'} = 1;
13250:         }
13251:     } elsif (@okoffload) {
13252:         $changes{'offloadnow'} = 1;
13253:     }
13254:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
13255:     if ((keys(%changes) > 0) || ($savespares)) {
13256:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13257:                                                  $dom);
13258:         if ($putresult eq 'ok') {
13259:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13260:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
13261:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
13262:                 }
13263:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
13264:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
13265:                 }
13266:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13267:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
13268:                 }
13269:             }
13270:             my $cachetime = 24*60*60;
13271:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13272:             if (ref($lastactref) eq 'HASH') {
13273:                 $lastactref->{'domdefaults'} = 1;
13274:             }
13275:             if (keys(%changes) > 0) {
13276:                 my %lt = &usersession_titles();
13277:                 $resulttext = &mt('Changes made:').'<ul>';
13278:                 foreach my $prefix (@prefixes) {
13279:                     if (ref($changes{$prefix}) eq 'HASH') {
13280:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
13281:                         if ($prefix eq 'spares') {
13282:                             if (ref($changes{$prefix}) eq 'HASH') {
13283:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
13284:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
13285:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
13286:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
13287:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
13288:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
13289:                                         foreach my $type (@{$types{$prefix}}) {
13290:                                             if ($changes{$prefix}{$lonhost}{$type}) {
13291:                                                 my $offloadto = &mt('None');
13292:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
13293:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
13294:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
13295:                                                     }
13296:                                                 }
13297:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
13298:                                             }
13299:                                         }
13300:                                     }
13301:                                     $resulttext .= '</li>';
13302:                                 }
13303:                             }
13304:                         } else {
13305:                             foreach my $type (@{$types{$prefix}}) {
13306:                                 if (defined($changes{$prefix}{$type})) {
13307:                                     my $newvalue;
13308:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13309:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
13310:                                             if ($type eq 'version') {
13311:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
13312:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13313:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
13314:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
13315:                                                 }
13316:                                             }
13317:                                         }
13318:                                     }
13319:                                     if ($newvalue eq '') {
13320:                                         if ($type eq 'version') {
13321:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
13322:                                         } else {
13323:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
13324:                                         }
13325:                                     } else {
13326:                                         if ($type eq 'version') {
13327:                                             $newvalue .= ' '.&mt('(or later)'); 
13328:                                         }
13329:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
13330:                                     }
13331:                                 }
13332:                             }
13333:                         }
13334:                         $resulttext .= '</ul>';
13335:                     }
13336:                 }
13337:                 if ($changes{'offloadnow'}) {
13338:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13339:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
13340:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
13341:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
13342:                                 $resulttext .= '<li>'.$lonhost.'</li>';
13343:                             }
13344:                             $resulttext .= '</ul>';
13345:                         } else {
13346:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
13347:                         }
13348:                     } else {
13349:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
13350:                     }
13351:                 }
13352:                 $resulttext .= '</ul>';
13353:             } else {
13354:                 $resulttext = $nochgmsg;
13355:             }
13356:         } else {
13357:             $resulttext = '<span class="LC_error">'.
13358:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13359:         }
13360:     } else {
13361:         $resulttext = $nochgmsg;
13362:     }
13363:     return $resulttext;
13364: }
13365: 
13366: sub modify_ssl {
13367:     my ($dom,$lastactref,%domconfig) = @_;
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 %servers = &Apache::lonnet::internet_dom_servers($dom);
13372:     my (%defaultshash,%changes);
13373:     my $action = 'ssl';
13374:     my @prefixes = ('connect','replication');
13375:     foreach my $prefix (@prefixes) {
13376:         $defaultshash{$action}{$prefix} = {};
13377:     }
13378:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13379:     my $resulttext;
13380:     my %iphost = &Apache::lonnet::get_iphost();
13381:     my @reptypes = ('certreq','nocertreq');
13382:     my @connecttypes = ('dom','intdom','other');
13383:     my %types = (
13384:                   connect      => \@connecttypes,
13385:                   replication  => \@reptypes,
13386:                 );
13387:     foreach my $prefix (sort(keys(%types))) {
13388:         foreach my $type (@{$types{$prefix}}) {
13389:             if ($prefix eq 'connect') {
13390:                 my $value = 'yes';
13391:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
13392:                     $value = $env{'form.'.$prefix.'_'.$type};
13393:                 }
13394:                 if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
13395:                     if ($domconfig{$action}{$prefix}{$type} ne '') {
13396:                         if ($value ne $domconfig{$action}{$prefix}{$type}) {
13397:                             $changes{$prefix}{$type} = 1;
13398:                         }
13399:                         $defaultshash{$action}{$prefix}{$type} = $value;
13400:                     } else {
13401:                         $defaultshash{$action}{$prefix}{$type} = $value;
13402:                         $changes{$prefix}{$type} = 1;
13403:                     }
13404:                 } else {
13405:                     $defaultshash{$action}{$prefix}{$type} = $value;
13406:                     $changes{$prefix}{$type} = 1;
13407:                 }
13408:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
13409:                     delete($changes{$prefix}{$type});
13410:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
13411:                     delete($changes{$prefix}{$type});
13412:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
13413:                     delete($changes{$prefix}{$type});
13414:                 }
13415:             } elsif ($prefix eq 'replication') {
13416:                 if (@locations > 0) {
13417:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
13418:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
13419:                     my @okvals;
13420:                     foreach my $val (@vals) {
13421:                         if ($val =~ /:/) {
13422:                             my @items = split(/:/,$val);
13423:                             foreach my $item (@items) {
13424:                                 if (ref($by_location{$item}) eq 'ARRAY') {
13425:                                     push(@okvals,$item);
13426:                                 }
13427:                             }
13428:                         } else {
13429:                             if (ref($by_location{$val}) eq 'ARRAY') {
13430:                                 push(@okvals,$val);
13431:                             }
13432:                         }
13433:                     }
13434:                     @okvals = sort(@okvals);
13435:                     if (ref($domconfig{$action}) eq 'HASH') {
13436:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
13437:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
13438:                                 if ($inuse == 0) {
13439:                                     $changes{$prefix}{$type} = 1;
13440:                                 } else {
13441:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
13442:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
13443:                                     if (@changed > 0) {
13444:                                         $changes{$prefix}{$type} = 1;
13445:                                     }
13446:                                 }
13447:                             } else {
13448:                                 if ($inuse == 1) {
13449:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
13450:                                     $changes{$prefix}{$type} = 1;
13451:                                 }
13452:                             }
13453:                         } else {
13454:                             if ($inuse == 1) {
13455:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
13456:                                 $changes{$prefix}{$type} = 1;
13457:                             }
13458:                         }
13459:                     } else {
13460:                         if ($inuse == 1) {
13461:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
13462:                             $changes{$prefix}{$type} = 1;
13463:                         }
13464:                     }
13465:                 }
13466:             }
13467:         }
13468:     }
13469:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
13470:     if (keys(%changes) > 0) {
13471:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13472:                                                  $dom);
13473:         if ($putresult eq 'ok') {
13474:             if (ref($defaultshash{$action}) eq 'HASH') {
13475:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
13476:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
13477:                 }
13478:                 if (ref($defaultshash{$action}{'connect'}) eq 'HASH') {
13479:                     $domdefaults{'connect'} = $domconfig{$action}{'connect'};
13480:                 }
13481:             }
13482:             my $cachetime = 24*60*60;
13483:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13484:             if (ref($lastactref) eq 'HASH') {
13485:                 $lastactref->{'domdefaults'} = 1;
13486:             }
13487:             if (keys(%changes) > 0) {
13488:                 my %titles = &ssl_titles();
13489:                 $resulttext = &mt('Changes made:').'<ul>';
13490:                 foreach my $prefix (@prefixes) {
13491:                     if (ref($changes{$prefix}) eq 'HASH') {
13492:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
13493:                         foreach my $type (@{$types{$prefix}}) {
13494:                             if (defined($changes{$prefix}{$type})) {
13495:                                 my $newvalue;
13496:                                 if (ref($defaultshash{$action}) eq 'HASH') {
13497:                                     if (ref($defaultshash{$action}{$prefix})) {
13498:                                         if ($prefix eq 'connect') {
13499:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
13500:                                         } elsif (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
13501:                                             if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
13502:                                                 $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
13503:                                             }
13504:                                         }
13505:                                     }
13506:                                     if ($newvalue eq '') {
13507:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
13508:                                     } else {
13509:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
13510:                                     }
13511:                                 }
13512:                             }
13513:                         }
13514:                         $resulttext .= '</ul>';
13515:                     }
13516:                 }
13517:             } else {
13518:                 $resulttext = $nochgmsg;
13519:             }
13520:         } else {
13521:             $resulttext = '<span class="LC_error">'.
13522:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13523:         }
13524:     } else {
13525:         $resulttext = $nochgmsg;
13526:     }
13527:     return $resulttext;
13528: }
13529: 
13530: sub modify_trust {
13531:     my ($dom,$lastactref,%domconfig) = @_;
13532:     my (%by_ip,%by_location,@intdoms,@instdoms);
13533:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
13534:     my @locations = sort(keys(%by_location));
13535:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
13536:     my @types = ('exc','inc');
13537:     my (%defaultshash,%changes);
13538:     foreach my $prefix (@prefixes) {
13539:         $defaultshash{'trust'}{$prefix} = {};
13540:     }
13541:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13542:     my $resulttext;
13543:     foreach my $prefix (@prefixes) {
13544:         foreach my $type (@types) {
13545:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
13546:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
13547:             my @okvals;
13548:             foreach my $val (@vals) {
13549:                 if ($val =~ /:/) {
13550:                     my @items = split(/:/,$val);
13551:                     foreach my $item (@items) {
13552:                         if (ref($by_location{$item}) eq 'ARRAY') {
13553:                             push(@okvals,$item);
13554:                         }
13555:                     }
13556:                 } else {
13557:                     if (ref($by_location{$val}) eq 'ARRAY') {
13558:                         push(@okvals,$val);
13559:                     }
13560:                 }
13561:             }
13562:             @okvals = sort(@okvals);
13563:             if (ref($domconfig{'trust'}) eq 'HASH') {
13564:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
13565:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
13566:                         if ($inuse == 0) {
13567:                             $changes{$prefix}{$type} = 1;
13568:                         } else {
13569:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
13570:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
13571:                             if (@changed > 0) {
13572:                                 $changes{$prefix}{$type} = 1;
13573:                             }
13574:                         }
13575:                     } else {
13576:                         if ($inuse == 1) {
13577:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
13578:                             $changes{$prefix}{$type} = 1;
13579:                         }
13580:                     }
13581:                 } else {
13582:                     if ($inuse == 1) {
13583:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
13584:                         $changes{$prefix}{$type} = 1;
13585:                     }
13586:                 }
13587:             } else {
13588:                 if ($inuse == 1) {
13589:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
13590:                     $changes{$prefix}{$type} = 1;
13591:                 }
13592:             }
13593:         }
13594:     }
13595:     my $nochgmsg = &mt('No changes made to trust settings.');
13596:     if (keys(%changes) > 0) {
13597:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13598:                                                  $dom);
13599:         if ($putresult eq 'ok') {
13600:             if (ref($defaultshash{'trust'}) eq 'HASH') {
13601:                 foreach my $prefix (@prefixes) {
13602:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
13603:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
13604:                     }
13605:                 }
13606:             }
13607:             my $cachetime = 24*60*60;
13608:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13609:             if (ref($lastactref) eq 'HASH') {
13610:                 $lastactref->{'domdefaults'} = 1;
13611:             }
13612:             if (keys(%changes) > 0) {
13613:                 my %lt = &trust_titles();
13614:                 $resulttext = &mt('Changes made:').'<ul>';
13615:                 foreach my $prefix (@prefixes) {
13616:                     if (ref($changes{$prefix}) eq 'HASH') {
13617:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
13618:                         foreach my $type (@types) {
13619:                             if (defined($changes{$prefix}{$type})) {
13620:                                 my $newvalue;
13621:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
13622:                                     if (ref($defaultshash{'trust'}{$prefix})) {
13623:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
13624:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
13625:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
13626:                                             }
13627:                                         }
13628:                                     }
13629:                                 }
13630:                                 if ($newvalue eq '') {
13631:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
13632:                                 } else {
13633:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
13634:                                 }
13635:                             }
13636:                         }
13637:                         $resulttext .= '</ul>';
13638:                     }
13639:                 }
13640:                 $resulttext .= '</ul>';
13641:             } else {
13642:                 $resulttext = $nochgmsg;
13643:             }
13644:         } else {
13645:             $resulttext = '<span class="LC_error">'.
13646:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13647:         }
13648:     } else {
13649:         $resulttext = $nochgmsg;
13650:     }
13651:     return $resulttext;
13652: }
13653: 
13654: sub modify_loadbalancing {
13655:     my ($dom,%domconfig) = @_;
13656:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
13657:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
13658:     my ($othertitle,$usertypes,$types) =
13659:         &Apache::loncommon::sorted_inst_types($dom);
13660:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13661:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
13662:     my @sparestypes = ('primary','default');
13663:     my %typetitles = &sparestype_titles();
13664:     my $resulttext;
13665:     my (%currbalancer,%currtargets,%currrules,%existing);
13666:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
13667:         %existing = %{$domconfig{'loadbalancing'}};
13668:     }
13669:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
13670:                               \%currtargets,\%currrules);
13671:     my ($saveloadbalancing,%defaultshash,%changes);
13672:     my ($alltypes,$othertypes,$titles) =
13673:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
13674:     my %ruletitles = &offloadtype_text();
13675:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
13676:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
13677:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
13678:         if ($balancer eq '') {
13679:             next;
13680:         }
13681:         if (!exists($servers{$balancer})) {
13682:             if (exists($currbalancer{$balancer})) {
13683:                 push(@{$changes{'delete'}},$balancer);
13684:             }
13685:             next;
13686:         }
13687:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
13688:             push(@{$changes{'delete'}},$balancer);
13689:             next;
13690:         }
13691:         if (!exists($currbalancer{$balancer})) {
13692:             push(@{$changes{'add'}},$balancer);
13693:         }
13694:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
13695:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
13696:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
13697:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
13698:             $saveloadbalancing = 1;
13699:         }
13700:         foreach my $sparetype (@sparestypes) {
13701:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
13702:             my @offloadto;
13703:             foreach my $target (@targets) {
13704:                 if (($servers{$target}) && ($target ne $balancer)) {
13705:                     if ($sparetype eq 'default') {
13706:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
13707:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
13708:                         }
13709:                     }
13710:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
13711:                         push(@offloadto,$target);
13712:                     }
13713:                 }
13714:             }
13715:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
13716:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
13717:                     push(@offloadto,$balancer);
13718:                 }
13719:             }
13720:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
13721:         }
13722:         if (ref($currtargets{$balancer}) eq 'HASH') {
13723:             foreach my $sparetype (@sparestypes) {
13724:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
13725:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
13726:                     if (@targetdiffs > 0) {
13727:                         $changes{'curr'}{$balancer}{'targets'} = 1;
13728:                     }
13729:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
13730:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
13731:                         $changes{'curr'}{$balancer}{'targets'} = 1;
13732:                     }
13733:                 }
13734:             }
13735:         } else {
13736:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
13737:                 foreach my $sparetype (@sparestypes) {
13738:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
13739:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
13740:                             $changes{'curr'}{$balancer}{'targets'} = 1;
13741:                         }
13742:                     }
13743:                 }
13744:             }
13745:         }
13746:         my $ishomedom;
13747:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
13748:             $ishomedom = 1;
13749:         }
13750:         if (ref($alltypes) eq 'ARRAY') {
13751:             foreach my $type (@{$alltypes}) {
13752:                 my $rule;
13753:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
13754:                          (!$ishomedom)) {
13755:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
13756:                 }
13757:                 if ($rule eq 'specific') {
13758:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
13759:                     if (exists($servers{$specifiedhost})) { 
13760:                         $rule = $specifiedhost;
13761:                     }
13762:                 }
13763:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
13764:                 if (ref($currrules{$balancer}) eq 'HASH') {
13765:                     if ($rule ne $currrules{$balancer}{$type}) {
13766:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
13767:                     }
13768:                 } elsif ($rule ne '') {
13769:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
13770:                 }
13771:             }
13772:         }
13773:     }
13774:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
13775:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
13776:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
13777:             $defaultshash{'loadbalancing'} = {};
13778:         }
13779:         my $putresult = &Apache::lonnet::put_dom('configuration',
13780:                                                  \%defaultshash,$dom);
13781:         if ($putresult eq 'ok') {
13782:             if (keys(%changes) > 0) {
13783:                 my %toupdate;
13784:                 if (ref($changes{'delete'}) eq 'ARRAY') {
13785:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
13786:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
13787:                         $toupdate{$balancer} = 1;
13788:                     }
13789:                 }
13790:                 if (ref($changes{'add'}) eq 'ARRAY') {
13791:                     foreach my $balancer (sort(@{$changes{'add'}})) {
13792:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
13793:                         $toupdate{$balancer} = 1;
13794:                     }
13795:                 }
13796:                 if (ref($changes{'curr'}) eq 'HASH') {
13797:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
13798:                         $toupdate{$balancer} = 1;
13799:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
13800:                             if ($changes{'curr'}{$balancer}{'targets'}) {
13801:                                 my %offloadstr;
13802:                                 foreach my $sparetype (@sparestypes) {
13803:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
13804:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
13805:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
13806:                                         }
13807:                                     }
13808:                                 }
13809:                                 if (keys(%offloadstr) == 0) {
13810:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
13811:                                 } else {
13812:                                     my $showoffload;
13813:                                     foreach my $sparetype (@sparestypes) {
13814:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
13815:                                         if (defined($offloadstr{$sparetype})) {
13816:                                             $showoffload .= $offloadstr{$sparetype};
13817:                                         } else {
13818:                                             $showoffload .= &mt('None');
13819:                                         }
13820:                                         $showoffload .= ('&nbsp;'x3);
13821:                                     }
13822:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
13823:                                 }
13824:                             }
13825:                         }
13826:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
13827:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
13828:                                 foreach my $type (@{$alltypes}) {
13829:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
13830:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
13831:                                         my $balancetext;
13832:                                         if ($rule eq '') {
13833:                                             $balancetext =  $ruletitles{'default'};
13834:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
13835:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) { 
13836:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
13837:                                                 foreach my $sparetype (@sparestypes) {
13838:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
13839:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
13840:                                                     }
13841:                                                 }
13842:                                                 foreach my $item (@{$alltypes}) {
13843:                                                     next if ($item =~  /^_LC_ipchange/);
13844:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
13845:                                                     if ($hasrule eq 'homeserver') {
13846:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
13847:                                                     } else {
13848:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
13849:                                                             if ($servers{$hasrule}) {
13850:                                                                 $toupdate{$hasrule} = 1;
13851:                                                             }
13852:                                                         }
13853:                                                     }
13854:                                                 }
13855:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
13856:                                                     $balancetext =  $ruletitles{$rule};
13857:                                                 } else {
13858:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
13859:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
13860:                                                     if ($receiver) {
13861:                                                         $toupdate{$receiver};
13862:                                                     }
13863:                                                 }
13864:                                             } else {
13865:                                                 $balancetext =  $ruletitles{$rule};
13866:                                             }
13867:                                         } else {
13868:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
13869:                                         }
13870:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
13871:                                     }
13872:                                 }
13873:                             }
13874:                         }
13875:                         if (keys(%toupdate)) {
13876:                             my %thismachine;
13877:                             my $updatedhere;
13878:                             my $cachetime = 60*60*24;
13879:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
13880:                             foreach my $lonhost (keys(%toupdate)) {
13881:                                 if ($thismachine{$lonhost}) {
13882:                                     unless ($updatedhere) {
13883:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
13884:                                                                       $defaultshash{'loadbalancing'},
13885:                                                                       $cachetime);
13886:                                         $updatedhere = 1;
13887:                                     }
13888:                                 } else {
13889:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
13890:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
13891:                                 }
13892:                             }
13893:                         }
13894:                     }
13895:                 }
13896:                 if ($resulttext ne '') {
13897:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
13898:                 } else {
13899:                     $resulttext = $nochgmsg;
13900:                 }
13901:             } else {
13902:                 $resulttext = $nochgmsg;
13903:             }
13904:         } else {
13905:             $resulttext = '<span class="LC_error">'.
13906:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13907:         }
13908:     } else {
13909:         $resulttext = $nochgmsg;
13910:     }
13911:     return $resulttext;
13912: }
13913: 
13914: sub recurse_check {
13915:     my ($chkcats,$categories,$depth,$name) = @_;
13916:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
13917:         my $chg = 0;
13918:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
13919:             my $category = $chkcats->[$depth]{$name}[$j];
13920:             my $item;
13921:             if ($category eq '') {
13922:                 $chg ++;
13923:             } else {
13924:                 my $deeper = $depth + 1;
13925:                 $item = &escape($category).':'.&escape($name).':'.$depth;
13926:                 if ($chg) {
13927:                     $categories->{$item} -= $chg;
13928:                 }
13929:                 &recurse_check($chkcats,$categories,$deeper,$category);
13930:                 $deeper --;
13931:             }
13932:         }
13933:     }
13934:     return;
13935: }
13936: 
13937: sub recurse_cat_deletes {
13938:     my ($item,$coursecategories,$deletions) = @_;
13939:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
13940:     my $subdepth = $depth + 1;
13941:     if (ref($coursecategories) eq 'HASH') {
13942:         foreach my $subitem (keys(%{$coursecategories})) {
13943:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
13944:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
13945:                 delete($coursecategories->{$subitem});
13946:                 $deletions->{$subitem} = 1;
13947:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
13948:             }
13949:         }
13950:     }
13951:     return;
13952: }
13953: 
13954: sub active_dc_picker {
13955:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
13956:     my %domcoords = &get_active_dcs($dom);
13957:     my @domcoord = keys(%domcoords);
13958:     if (keys(%currhash)) {
13959:         foreach my $dc (keys(%currhash)) {
13960:             unless (exists($domcoords{$dc})) {
13961:                 push(@domcoord,$dc);
13962:             }
13963:         }
13964:     }
13965:     @domcoord = sort(@domcoord);
13966:     my $numdcs = scalar(@domcoord);
13967:     my $rows = 0;
13968:     my $table;
13969:     if ($numdcs > 1) {
13970:         $table = '<table>';
13971:         for (my $i=0; $i<@domcoord; $i++) {
13972:             my $rem = $i%($numinrow);
13973:             if ($rem == 0) {
13974:                 if ($i > 0) {
13975:                     $table .= '</tr>';
13976:                 }
13977:                 $table .= '<tr>';
13978:                 $rows ++;
13979:             }
13980:             my $check = '';
13981:             if ($inputtype eq 'radio') {
13982:                 if (keys(%currhash) == 0) {
13983:                     if (!$i) {
13984:                         $check = ' checked="checked"';
13985:                     }
13986:                 } elsif (exists($currhash{$domcoord[$i]})) {
13987:                     $check = ' checked="checked"';
13988:                 }
13989:             } else {
13990:                 if (exists($currhash{$domcoord[$i]})) {
13991:                     $check = ' checked="checked"';
13992:                 }
13993:             }
13994:             if ($i == @domcoord - 1) {
13995:                 my $colsleft = $numinrow - $rem;
13996:                 if ($colsleft > 1) {
13997:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
13998:                 } else {
13999:                     $table .= '<td class="LC_left_item">';
14000:                 }
14001:             } else {
14002:                 $table .= '<td class="LC_left_item">';
14003:             }
14004:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
14005:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14006:             $table .= '<span class="LC_nobreak"><label>'.
14007:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
14008:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
14009:             if ($user ne $dcname.':'.$dcdom) {
14010:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14011:             }
14012:             $table .= '</label></span></td>';
14013:         }
14014:         $table .= '</tr></table>';
14015:     } elsif ($numdcs == 1) {
14016:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
14017:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14018:         if ($inputtype eq 'radio') {
14019:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
14020:             if ($user ne $dcname.':'.$dcdom) {
14021:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14022:             }
14023:         } else {
14024:             my $check;
14025:             if (exists($currhash{$domcoord[0]})) {
14026:                 $check = ' checked="checked"';
14027:             }
14028:             $table = '<span class="LC_nobreak"><label>'.
14029:                      '<input type="checkbox" name="'.$name.'" '.
14030:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
14031:             if ($user ne $dcname.':'.$dcdom) {
14032:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14033:             }
14034:             $table .= '</label></span>';
14035:             $rows ++;
14036:         }
14037:     }
14038:     return ($numdcs,$table,$rows);
14039: }
14040: 
14041: sub usersession_titles {
14042:     return &Apache::lonlocal::texthash(
14043:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
14044:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
14045:                spares => 'Servers offloaded to, when busy',
14046:                version => 'LON-CAPA version requirement',
14047:                excludedomain => 'Allow all, but exclude specific domains',
14048:                includedomain => 'Deny all, but include specific domains',
14049:                primary => 'Primary (checked first)',
14050:                default => 'Default',
14051:            );
14052: }
14053: 
14054: sub id_for_thisdom {
14055:     my (%servers) = @_;
14056:     my %altids;
14057:     foreach my $server (keys(%servers)) {
14058:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
14059:         if ($serverhome ne $server) {
14060:             $altids{$serverhome} = $server;
14061:         }
14062:     }
14063:     return %altids;
14064: }
14065: 
14066: sub count_servers {
14067:     my ($currbalancer,%servers) = @_;
14068:     my (@spares,$numspares);
14069:     foreach my $lonhost (sort(keys(%servers))) {
14070:         next if ($currbalancer eq $lonhost);
14071:         push(@spares,$lonhost);
14072:     }
14073:     if ($currbalancer) {
14074:         $numspares = scalar(@spares);
14075:     } else {
14076:         $numspares = scalar(@spares) - 1;
14077:     }
14078:     return ($numspares,@spares);
14079: }
14080: 
14081: sub lonbalance_targets_js {
14082:     my ($dom,$types,$servers,$settings) = @_;
14083:     my $select = &mt('Select');
14084:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
14085:     if (ref($servers) eq 'HASH') {
14086:         $alltargets = join("','",sort(keys(%{$servers})));
14087:         my @homedoms;
14088:         foreach my $server (sort(keys(%{$servers}))) {
14089:             if (&Apache::lonnet::host_domain($server) eq $dom) {
14090:                 push(@homedoms,'1');
14091:             } else {
14092:                 push(@homedoms,'0');
14093:             }
14094:         }
14095:         $allishome = join("','",@homedoms);
14096:     }
14097:     if (ref($types) eq 'ARRAY') {
14098:         if (@{$types} > 0) {
14099:             @alltypes = @{$types};
14100:         }
14101:     }
14102:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
14103:     $allinsttypes = join("','",@alltypes);
14104:     my (%currbalancer,%currtargets,%currrules,%existing);
14105:     if (ref($settings) eq 'HASH') {
14106:         %existing = %{$settings};
14107:     }
14108:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
14109:                               \%currtargets,\%currrules);
14110:     my $balancers = join("','",sort(keys(%currbalancer)));
14111:     return <<"END";
14112: 
14113: <script type="text/javascript">
14114: // <![CDATA[
14115: 
14116: currBalancers = new Array('$balancers');
14117: 
14118: function toggleTargets(balnum) {
14119:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
14120:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
14121:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
14122:     var prevbalancer = prevhostitem.value;
14123:     var baltotal = document.getElementById('loadbalancing_total').value;
14124:     prevhostitem.value = balancer;
14125:     if (prevbalancer != '') {
14126:         var prevIdx = currBalancers.indexOf(prevbalancer);
14127:         if (prevIdx != -1) {
14128:             currBalancers.splice(prevIdx,1);
14129:         }
14130:     }
14131:     if (balancer == '') {
14132:         hideSpares(balnum);
14133:     } else {
14134:         var currIdx = currBalancers.indexOf(balancer);
14135:         if (currIdx == -1) {
14136:             currBalancers.push(balancer);
14137:         }
14138:         var homedoms = new Array('$allishome');
14139:         var ishomedom = homedoms[lonhostitem.selectedIndex];
14140:         showSpares(balancer,ishomedom,balnum);
14141:     }
14142:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
14143:     return;
14144: }
14145: 
14146: function showSpares(balancer,ishomedom,balnum) {
14147:     var alltargets = new Array('$alltargets');
14148:     var insttypes = new Array('$allinsttypes');
14149:     var offloadtypes = new Array('primary','default');
14150: 
14151:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
14152:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
14153:  
14154:     for (var i=0; i<offloadtypes.length; i++) {
14155:         var count = 0;
14156:         for (var j=0; j<alltargets.length; j++) {
14157:             if (alltargets[j] != balancer) {
14158:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
14159:                 item.value = alltargets[j];
14160:                 item.style.textAlign='left';
14161:                 item.style.textFace='normal';
14162:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
14163:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
14164:                     item.disabled = '';
14165:                 } else {
14166:                     item.disabled = 'disabled';
14167:                     item.checked = false;
14168:                 }
14169:                 count ++;
14170:             }
14171:         }
14172:     }
14173:     for (var k=0; k<insttypes.length; k++) {
14174:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
14175:             if (ishomedom == 1) {
14176:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
14177:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
14178:             } else {
14179:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
14180:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
14181:             }
14182:         } else {
14183:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
14184:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
14185:         }
14186:         if ((insttypes[k] != '_LC_external') && 
14187:             ((insttypes[k] != '_LC_internetdom') ||
14188:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
14189:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
14190:             item.options.length = 0;
14191:             item.options[0] = new Option("","",true,true);
14192:             var idx = 0;
14193:             for (var m=0; m<alltargets.length; m++) {
14194:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
14195:                     idx ++;
14196:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
14197:                 }
14198:             }
14199:         }
14200:     }
14201:     return;
14202: }
14203: 
14204: function hideSpares(balnum) {
14205:     var alltargets = new Array('$alltargets');
14206:     var insttypes = new Array('$allinsttypes');
14207:     var offloadtypes = new Array('primary','default');
14208: 
14209:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
14210:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
14211: 
14212:     var total = alltargets.length - 1;
14213:     for (var i=0; i<offloadtypes; i++) {
14214:         for (var j=0; j<total; j++) {
14215:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
14216:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
14217:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
14218:         }
14219:     }
14220:     for (var k=0; k<insttypes.length; k++) {
14221:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
14222:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
14223:         if (insttypes[k] != '_LC_external') {
14224:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
14225:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
14226:         }
14227:     }
14228:     return;
14229: }
14230: 
14231: function checkOffloads(item,balnum,type) {
14232:     var alltargets = new Array('$alltargets');
14233:     var offloadtypes = new Array('primary','default');
14234:     if (item.checked) {
14235:         var total = alltargets.length - 1;
14236:         var other;
14237:         if (type == offloadtypes[0]) {
14238:             other = offloadtypes[1];
14239:         } else {
14240:             other = offloadtypes[0];
14241:         }
14242:         for (var i=0; i<total; i++) {
14243:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
14244:             if (server == item.value) {
14245:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
14246:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
14247:                 }
14248:             }
14249:         }
14250:     }
14251:     return;
14252: }
14253: 
14254: function singleServerToggle(balnum,type) {
14255:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
14256:     if (offloadtoSelIdx == 0) {
14257:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
14258:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
14259: 
14260:     } else {
14261:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
14262:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
14263:     }
14264:     return;
14265: }
14266: 
14267: function balanceruleChange(formname,balnum,type) {
14268:     if (type == '_LC_external') {
14269:         return;
14270:     }
14271:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
14272:     for (var i=0; i<typesRules.length; i++) {
14273:         if (formname.elements[typesRules[i]].checked) {
14274:             if (formname.elements[typesRules[i]].value != 'specific') {
14275:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
14276:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
14277:             } else {
14278:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
14279:             }
14280:         }
14281:     }
14282:     return;
14283: }
14284: 
14285: function balancerDeleteChange(balnum) {
14286:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
14287:     var baltotal = document.getElementById('loadbalancing_total').value;
14288:     var addtarget;
14289:     var removetarget;
14290:     var action = 'delete';
14291:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
14292:         var lonhost = hostitem.value;
14293:         var currIdx = currBalancers.indexOf(lonhost);
14294:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
14295:             if (currIdx != -1) {
14296:                 currBalancers.splice(currIdx,1);
14297:             }
14298:             addtarget = lonhost;
14299:         } else {
14300:             if (currIdx == -1) {
14301:                 currBalancers.push(lonhost);
14302:             }
14303:             removetarget = lonhost;
14304:             action = 'undelete';
14305:         }
14306:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
14307:     }
14308:     return;
14309: }
14310: 
14311: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
14312:     if (baltotal > 1) {
14313:         var offloadtypes = new Array('primary','default');
14314:         var alltargets = new Array('$alltargets');
14315:         var insttypes = new Array('$allinsttypes');
14316:         for (var i=0; i<baltotal; i++) {
14317:             if (i != balnum) {
14318:                 for (var j=0; j<offloadtypes.length; j++) {
14319:                     var total = alltargets.length - 1;
14320:                     for (var k=0; k<total; k++) {
14321:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
14322:                         var server = serveritem.value;
14323:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
14324:                             if (server == addtarget) {
14325:                                 serveritem.disabled = '';
14326:                             }
14327:                         }
14328:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
14329:                             if (server == removetarget) {
14330:                                 serveritem.disabled = 'disabled';
14331:                                 serveritem.checked = false;
14332:                             }
14333:                         }
14334:                     }
14335:                 }
14336:                 for (var j=0; j<insttypes.length; j++) {
14337:                     if (insttypes[j] != '_LC_external') {
14338:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
14339:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
14340:                             var currSel = singleserver.selectedIndex;
14341:                             var currVal = singleserver.options[currSel].value;
14342:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
14343:                                 var numoptions = singleserver.options.length;
14344:                                 var needsnew = 1;
14345:                                 for (var k=0; k<numoptions; k++) {
14346:                                     if (singleserver.options[k] == addtarget) {
14347:                                         needsnew = 0;
14348:                                         break;
14349:                                     }
14350:                                 }
14351:                                 if (needsnew == 1) {
14352:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
14353:                                 }
14354:                             }
14355:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
14356:                                 singleserver.options.length = 0;
14357:                                 if ((currVal) && (currVal != removetarget)) {
14358:                                     singleserver.options[0] = new Option("","",false,false);
14359:                                 } else {
14360:                                     singleserver.options[0] = new Option("","",true,true);
14361:                                 }
14362:                                 var idx = 0;
14363:                                 for (var m=0; m<alltargets.length; m++) {
14364:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
14365:                                         idx ++;
14366:                                         if (currVal == alltargets[m]) {
14367:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
14368:                                         } else {
14369:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
14370:                                         }
14371:                                     }
14372:                                 }
14373:                             }
14374:                         }
14375:                     }
14376:                 }
14377:             }
14378:         }
14379:     }
14380:     return;
14381: }
14382: 
14383: // ]]>
14384: </script>
14385: 
14386: END
14387: }
14388: 
14389: sub new_spares_js {
14390:     my @sparestypes = ('primary','default');
14391:     my $types = join("','",@sparestypes);
14392:     my $select = &mt('Select');
14393:     return <<"END";
14394: 
14395: <script type="text/javascript">
14396: // <![CDATA[
14397: 
14398: function updateNewSpares(formname,lonhost) {
14399:     var types = new Array('$types');
14400:     var include = new Array();
14401:     var exclude = new Array();
14402:     for (var i=0; i<types.length; i++) {
14403:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
14404:         for (var j=0; j<spareboxes.length; j++) {
14405:             if (formname.elements[spareboxes[j]].checked) {
14406:                 exclude.push(formname.elements[spareboxes[j]].value);
14407:             } else {
14408:                 include.push(formname.elements[spareboxes[j]].value);
14409:             }
14410:         }
14411:     }
14412:     for (var i=0; i<types.length; i++) {
14413:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
14414:         var selIdx = newSpare.selectedIndex;
14415:         var currnew = newSpare.options[selIdx].value;
14416:         var okSpares = new Array();
14417:         for (var j=0; j<newSpare.options.length; j++) {
14418:             var possible = newSpare.options[j].value;
14419:             if (possible != '') {
14420:                 if (exclude.indexOf(possible) == -1) {
14421:                     okSpares.push(possible);
14422:                 } else {
14423:                     if (currnew == possible) {
14424:                         selIdx = 0;
14425:                     }
14426:                 }
14427:             }
14428:         }
14429:         for (var k=0; k<include.length; k++) {
14430:             if (okSpares.indexOf(include[k]) == -1) {
14431:                 okSpares.push(include[k]);
14432:             }
14433:         }
14434:         okSpares.sort();
14435:         newSpare.options.length = 0;
14436:         if (selIdx == 0) {
14437:             newSpare.options[0] = new Option("$select","",true,true);
14438:         } else {
14439:             newSpare.options[0] = new Option("$select","",false,false);
14440:         }
14441:         for (var m=0; m<okSpares.length; m++) {
14442:             var idx = m+1;
14443:             var selThis = 0;
14444:             if (selIdx != 0) {
14445:                 if (okSpares[m] == currnew) {
14446:                     selThis = 1;
14447:                 }
14448:             }
14449:             if (selThis == 1) {
14450:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
14451:             } else {
14452:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
14453:             }
14454:         }
14455:     }
14456:     return;
14457: }
14458: 
14459: function checkNewSpares(lonhost,type) {
14460:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
14461:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
14462:     if (chosen != '') { 
14463:         var othertype;
14464:         var othernewSpare;
14465:         if (type == 'primary') {
14466:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
14467:         }
14468:         if (type == 'default') {
14469:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
14470:         }
14471:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
14472:             othernewSpare.selectedIndex = 0;
14473:         }
14474:     }
14475:     return;
14476: }
14477: 
14478: // ]]>
14479: </script>
14480: 
14481: END
14482: 
14483: }
14484: 
14485: sub common_domprefs_js {
14486:     return <<"END";
14487: 
14488: <script type="text/javascript">
14489: // <![CDATA[
14490: 
14491: function getIndicesByName(formname,item) {
14492:     var group = new Array();
14493:     for (var i=0;i<formname.elements.length;i++) {
14494:         if (formname.elements[i].name == item) {
14495:             group.push(formname.elements[i].id);
14496:         }
14497:     }
14498:     return group;
14499: }
14500: 
14501: // ]]>
14502: </script>
14503: 
14504: END
14505: 
14506: }
14507: 
14508: sub recaptcha_js {
14509:     my %lt = &captcha_phrases();
14510:     return <<"END";
14511: 
14512: <script type="text/javascript">
14513: // <![CDATA[
14514: 
14515: function updateCaptcha(caller,context) {
14516:     var privitem;
14517:     var pubitem;
14518:     var privtext;
14519:     var pubtext;
14520:     var versionitem;
14521:     var versiontext;
14522:     if (document.getElementById(context+'_recaptchapub')) {
14523:         pubitem = document.getElementById(context+'_recaptchapub');
14524:     } else {
14525:         return;
14526:     }
14527:     if (document.getElementById(context+'_recaptchapriv')) {
14528:         privitem = document.getElementById(context+'_recaptchapriv');
14529:     } else {
14530:         return;
14531:     }
14532:     if (document.getElementById(context+'_recaptchapubtxt')) {
14533:         pubtext = document.getElementById(context+'_recaptchapubtxt');
14534:     } else {
14535:         return;
14536:     }
14537:     if (document.getElementById(context+'_recaptchaprivtxt')) {
14538:         privtext = document.getElementById(context+'_recaptchaprivtxt');
14539:     } else {
14540:         return;
14541:     }
14542:     if (document.getElementById(context+'_recaptchaversion')) {
14543:         versionitem = document.getElementById(context+'_recaptchaversion');
14544:     } else {
14545:         return;
14546:     }
14547:     if (document.getElementById(context+'_recaptchavertxt')) {
14548:         versiontext = document.getElementById(context+'_recaptchavertxt');
14549:     } else {
14550:         return;
14551:     }
14552:     if (caller.checked) {
14553:         if (caller.value == 'recaptcha') {
14554:             pubitem.type = 'text';
14555:             privitem.type = 'text';
14556:             pubitem.size = '40';
14557:             privitem.size = '40';
14558:             pubtext.innerHTML = "$lt{'pub'}";
14559:             privtext.innerHTML = "$lt{'priv'}";
14560:             versionitem.type = 'text';
14561:             versionitem.size = '3';
14562:             versiontext.innerHTML = "$lt{'ver'}"; 
14563:         } else {
14564:             pubitem.type = 'hidden';
14565:             privitem.type = 'hidden';
14566:             versionitem.type = 'hidden';
14567:             pubtext.innerHTML = '';
14568:             privtext.innerHTML = '';
14569:             versiontext.innerHTML = '';
14570:         }
14571:     }
14572:     return;
14573: }
14574: 
14575: // ]]>
14576: </script>
14577: 
14578: END
14579: 
14580: }
14581: 
14582: sub toggle_display_js {
14583:     return <<"END";
14584: 
14585: <script type="text/javascript">
14586: // <![CDATA[
14587: 
14588: function toggleDisplay(domForm,caller) {
14589:     if (document.getElementById(caller)) {
14590:         var divitem = document.getElementById(caller);
14591:         var optionsElement = domForm.coursecredits;
14592:         var checkval = 1;
14593:         var dispval = 'block';
14594:         if (caller == 'emailoptions') {
14595:             optionsElement = domForm.cancreate_email; 
14596:         }
14597:         if (caller == 'studentsubmission') {
14598:             optionsElement = domForm.postsubmit;
14599:         }
14600:         if (caller == 'cloneinstcode') {
14601:             optionsElement = domForm.canclone;
14602:             checkval = 'instcode';
14603:         }
14604:         if (optionsElement.length) {
14605:             var currval;
14606:             for (var i=0; i<optionsElement.length; i++) {
14607:                 if (optionsElement[i].checked) {
14608:                    currval = optionsElement[i].value;
14609:                 }
14610:             }
14611:             if (currval == checkval) {
14612:                 divitem.style.display = dispval;
14613:             } else {
14614:                 divitem.style.display = 'none';
14615:             }
14616:         }
14617:     }
14618:     return;
14619: }
14620: 
14621: // ]]>
14622: </script>
14623: 
14624: END
14625: 
14626: }
14627: 
14628: sub captcha_phrases {
14629:     return &Apache::lonlocal::texthash (
14630:                  priv => 'Private key',
14631:                  pub  => 'Public key',
14632:                  original  => 'original (CAPTCHA)',
14633:                  recaptcha => 'successor (ReCAPTCHA)',
14634:                  notused   => 'unused',
14635:                  ver => 'ReCAPTCHA version (1 or 2)', 
14636:     );
14637: }
14638: 
14639: sub devalidate_remote_domconfs {
14640:     my ($dom,$cachekeys) = @_;
14641:     return unless (ref($cachekeys) eq 'HASH');
14642:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14643:     my %thismachine;
14644:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
14645:     my @posscached = ('domainconfig','domdefaults','ltitools');
14646:     if (keys(%servers)) {
14647:         foreach my $server (keys(%servers)) {
14648:             next if ($thismachine{$server});
14649:             my @cached;
14650:             foreach my $name (@posscached) {
14651:                 if ($cachekeys->{$name}) {
14652:                     push(@cached,&escape($name).':'.&escape($dom));
14653:                 }
14654:             }
14655:             if (@cached) {
14656:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
14657:             }
14658:         }
14659:     }
14660:     return;
14661: }
14662: 
14663: 1;

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