File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.84.6.3: download - view: text, annotated - select for diffs
Fri May 22 22:29:56 2020 UTC (3 years, 11 months ago) by raeburn
Branches: version_2_11_2_uiuc
Diff to branchpoint 1.160.6.84: preferred, unified
- For 2.11.2 (modified)
  Required for changes in rev. 1.160.6.84.6.2

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.84.6.3 2020/05/22 22:29:56 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: ###############################################################
   30: ##############################################################
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::domainprefs.pm
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: Handles configuration of a LON-CAPA domain.  
   41: 
   42: This is part of the LearningOnline Network with CAPA project
   43: described at http://www.lon-capa.org.
   44: 
   45: 
   46: =head1 OVERVIEW
   47: 
   48: Each institution using LON-CAPA will typically have a single domain designated 
   49: for use by individuals affiliated with the institution.  Accordingly, each domain
   50: may define a default set of logos and a color scheme which can be used to "brand"
   51: the LON-CAPA instance. In addition, an institution will typically have a language
   52: and timezone which are used for the majority of courses.
   53: 
   54: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   55: host of other domain-wide settings which determine the types of functionality
   56: available to users and courses in the domain.
   57: 
   58: There is also a mechanism to configure cataloging of courses in the domain, and
   59: controls on the operation of automated processes which govern such things as
   60: roster updates, user directory updates and processing of course requests.
   61: 
   62: The domain coordination manual which is built dynamically on install/update of 
   63: LON-CAPA from the relevant help items provides more information about domain 
   64: configuration.
   65: 
   66: Most of the domain settings are stored in the configuration.db GDBM file which is
   67: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   68: where $dom is the domain.  The configuration.db stores settings in a number of 
   69: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   70: the domain as files (e.g., image files for logos etc., or plain text files for
   71: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   72: session hosted on the primary library server in the domain, as these files are 
   73: stored in author space belonging to a special $dom-domainconfig user.   
   74: 
   75: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   76: the current settings, and provides an interface to make modifications.
   77: 
   78: =head1 SUBROUTINES
   79: 
   80: =over
   81: 
   82: =item print_quotas()
   83: 
   84: Inputs: 4 
   85: 
   86: $dom,$settings,$rowtotal,$action.
   87: 
   88: $dom is the domain, $settings is a reference to a hash of current settings for
   89: the current context, $rowtotal is a reference to the scalar used to record the 
   90: number of rows displayed on the page, and $action is the context (quotas, 
   91: requestcourses or requestauthor).
   92: 
   93: The print_quotas routine was orginally created to display/store information
   94: about default quota sizes for portfolio spaces for the different types of 
   95: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   96: but is now also used to manage availability of user tools: 
   97: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   98: used by course owners to request creation of a course, and to display/store
   99: default quota sizes for Authoring Spaces.
  100: 
  101: Outputs: 1
  102: 
  103: $datatable  - HTML containing form elements which allow settings to be changed. 
  104: 
  105: In the case of course requests, radio buttons are displayed for each institutional
  106: affiliate type (and also default, and _LC_adv) for each of the course types 
  107: (official, unofficial, community, and textbook).  In each case the radio buttons 
  108: allow the selection of one of four values:
  109: 
  110: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  111: which have the following effects:
  112: 
  113: 0
  114: 
  115: =over
  116: 
  117: - course requests are not allowed for this course types/affiliation
  118: 
  119: =back
  120: 
  121: approval 
  122: 
  123: =over 
  124: 
  125: - course requests must be approved by a Doman Coordinator in the 
  126: course's domain
  127: 
  128: =back
  129: 
  130: validate 
  131: 
  132: =over
  133: 
  134: - an institutional validation (e.g., check requestor is instructor
  135: of record) needs to be passed before the course will be created.  The required
  136: validation is in localenroll.pm on the primary library server for the course 
  137: domain.
  138: 
  139: =back
  140: 
  141: autolimit 
  142: 
  143: =over
  144:  
  145: - course requests will be processed automatically up to a limit of
  146: N requests for the course type for the particular requestor.
  147: If N is undefined, there is no limit to the number of course requests
  148: which a course owner may submit and have processed automatically. 
  149: 
  150: =back
  151: 
  152: =item modify_quotas() 
  153: 
  154: =back
  155: 
  156: =cut
  157: 
  158: package Apache::domainprefs;
  159: 
  160: use strict;
  161: use Apache::Constants qw(:common :http);
  162: use Apache::lonnet;
  163: use Apache::loncommon();
  164: use Apache::lonhtmlcommon();
  165: use Apache::lonlocal;
  166: use Apache::lonmsg();
  167: use Apache::lonconfigsettings;
  168: use Apache::lonuserutils();
  169: use Apache::loncoursequeueadmin();
  170: use LONCAPA qw(:DEFAULT :match);
  171: use LONCAPA::Enrollment;
  172: use LONCAPA::lonauthcgi();
  173: use File::Copy;
  174: use Locale::Language;
  175: use DateTime::TimeZone;
  176: use DateTime::Locale;
  177: 
  178: my $registered_cleanup;
  179: my $modified_urls;
  180: 
  181: sub handler {
  182:     my $r=shift;
  183:     if ($r->header_only) {
  184:         &Apache::loncommon::content_type($r,'text/html');
  185:         $r->send_http_header;
  186:         return OK;
  187:     }
  188: 
  189:     my $context = 'domain';
  190:     my $dom = $env{'request.role.domain'};
  191:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  192:     if (&Apache::lonnet::allowed('mau',$dom)) {
  193:         &Apache::loncommon::content_type($r,'text/html');
  194:         $r->send_http_header;
  195:     } else {
  196:         $env{'user.error.msg'}=
  197:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  198:         return HTTP_NOT_ACCEPTABLE;
  199:     }
  200: 
  201:     $registered_cleanup=0;
  202:     @{$modified_urls}=();
  203: 
  204:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  205:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  206:                                             ['phase','actions']);
  207:     my $phase = 'pickactions';
  208:     if ( exists($env{'form.phase'}) ) {
  209:         $phase = $env{'form.phase'};
  210:     }
  211:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  212:     my %domconfig =
  213:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  214:                 'quotas','autoenroll','autoupdate','autocreate',
  215:                 'directorysrch','usercreation','usermodification',
  216:                 'contacts','defaults','scantron','coursecategories',
  217:                 'serverstatuses','requestcourses','helpsettings',
  218:                 'coursedefaults','usersessions','loadbalancing',
  219:                 'requestauthor','selfenrollment','inststatus',
  220:                 'ltitools'],$dom);
  221:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  222:         my %encconfig =
  223:             &Apache::lonnet::get_dom('encconfig',['ltitools'],$dom);
  224:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  225:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  226:                 if (ref($domconfig{'ltitools'}{$id}) eq 'HASH') {
  227:                     foreach my $item ('key','secret') {
  228:                         $domconfig{'ltitools'}{$id}{$item} = $encconfig{'ltitools'}{$id}{$item};
  229:                     }
  230:                 }
  231:             }
  232:         }
  233:     }
  234:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  235:                        'autoupdate','autocreate','directorysrch','contacts',
  236:                        'usercreation','selfcreation','usermodification','scantron',
  237:                        'requestcourses','requestauthor','coursecategories',
  238:                        'serverstatuses','helpsettings','coursedefaults',
  239:                        'ltitools','selfenrollment','usersessions');
  240:     my %existing;
  241:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  242:         %existing = %{$domconfig{'loadbalancing'}};
  243:     }
  244:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  245:         push(@prefs_order,'loadbalancing');
  246:     }
  247:     my %prefs = (
  248:         'rolecolors' =>
  249:                    { text => 'Default color schemes',
  250:                      help => 'Domain_Configuration_Color_Schemes',
  251:                      header => [{col1 => 'Student Settings',
  252:                                  col2 => '',},
  253:                                 {col1 => 'Coordinator Settings',
  254:                                  col2 => '',},
  255:                                 {col1 => 'Author Settings',
  256:                                  col2 => '',},
  257:                                 {col1 => 'Administrator Settings',
  258:                                  col2 => '',}],
  259:                       print => \&print_rolecolors,
  260:                       modify => \&modify_rolecolors,
  261:                     },
  262:         'login' =>
  263:                     { text => 'Log-in page options',
  264:                       help => 'Domain_Configuration_Login_Page',
  265:                       header => [{col1 => 'Log-in Page Items',
  266:                                   col2 => '',},
  267:                                  {col1 => 'Log-in Help',
  268:                                   col2 => 'Value'},
  269:                                  {col1 => 'Custom HTML in document head',
  270:                                   col2 => 'Value'}],
  271:                       print => \&print_login,
  272:                       modify => \&modify_login,
  273:                     },
  274:         'defaults' => 
  275:                     { text => 'Default authentication/language/timezone/portal/types',
  276:                       help => 'Domain_Configuration_LangTZAuth',
  277:                       header => [{col1 => 'Setting',
  278:                                   col2 => 'Value'},
  279:                                  {col1 => 'Internal Authentication',
  280:                                   col2 => 'Value'},
  281:                                  {col1 => 'Institutional user types',
  282:                                   col2 => 'Assignable to e-mail usernames'}],
  283:                       print => \&print_defaults,
  284:                       modify => \&modify_defaults,
  285:                     },
  286:         'quotas' => 
  287:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  288:                       help => 'Domain_Configuration_Quotas',
  289:                       header => [{col1 => 'User affiliation',
  290:                                   col2 => 'Available tools',
  291:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  292:                       print => \&print_quotas,
  293:                       modify => \&modify_quotas,
  294:                     },
  295:         'autoenroll' =>
  296:                    { text => 'Auto-enrollment settings',
  297:                      help => 'Domain_Configuration_Auto_Enrollment',
  298:                      header => [{col1 => 'Configuration setting',
  299:                                  col2 => 'Value(s)'}],
  300:                      print => \&print_autoenroll,
  301:                      modify => \&modify_autoenroll,
  302:                    },
  303:         'autoupdate' => 
  304:                    { text => 'Auto-update settings',
  305:                      help => 'Domain_Configuration_Auto_Updates',
  306:                      header => [{col1 => 'Setting',
  307:                                  col2 => 'Value',},
  308:                                 {col1 => 'Setting',
  309:                                  col2 => 'Affiliation'},
  310:                                 {col1 => 'User population',
  311:                                  col2 => 'Updatable user data'}],
  312:                      print => \&print_autoupdate,
  313:                      modify => \&modify_autoupdate,
  314:                   },
  315:         'autocreate' => 
  316:                   { text => 'Auto-course creation settings',
  317:                      help => 'Domain_Configuration_Auto_Creation',
  318:                      header => [{col1 => 'Configuration Setting',
  319:                                  col2 => 'Value',}],
  320:                      print => \&print_autocreate,
  321:                      modify => \&modify_autocreate,
  322:                   },
  323:         'directorysrch' => 
  324:                   { text => 'Directory searches',
  325:                     help => 'Domain_Configuration_InstDirectory_Search',
  326:                     header => [{col1 => 'Institutional Directory Setting',
  327:                                 col2 => 'Value',},
  328:                                {col1 => 'LON-CAPA Directory Setting',
  329:                                 col2 => 'Value',}],
  330:                     print => \&print_directorysrch,
  331:                     modify => \&modify_directorysrch,
  332:                   },
  333:         'contacts' =>
  334:                   { text => 'E-mail addresses and helpform',
  335:                     help => 'Domain_Configuration_Contact_Info',
  336:                     header => [{col1 => 'Default e-mail addresses',
  337:                                 col2 => 'Value',},
  338:                                {col1 => 'Recipient(s) for notifications',
  339:                                 col2 => 'Value',},
  340:                                {col1 => 'Ask helpdesk form settings',
  341:                                 col2 => 'Value',},],
  342:                     print => \&print_contacts,
  343:                     modify => \&modify_contacts,
  344:                   },
  345:         'usercreation' => 
  346:                   { text => 'User creation',
  347:                     help => 'Domain_Configuration_User_Creation',
  348:                     header => [{col1 => 'Format rule type',
  349:                                 col2 => 'Format rules in force'},
  350:                                {col1 => 'User account creation',
  351:                                 col2 => 'Usernames which may be created',},
  352:                                {col1 => 'Context',
  353:                                 col2 => 'Assignable authentication types'}],
  354:                     print => \&print_usercreation,
  355:                     modify => \&modify_usercreation,
  356:                   },
  357:         'selfcreation' => 
  358:                   { text => 'Users self-creating accounts',
  359:                     help => 'Domain_Configuration_Self_Creation', 
  360:                     header => [{col1 => 'Self-creation with institutional username',
  361:                                 col2 => 'Enabled?'},
  362:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  363:                                 col2 => 'Information user can enter'},
  364:                                {col1 => 'Self-creation with e-mail as username',
  365:                                 col2 => 'Settings'}],
  366:                     print => \&print_selfcreation,
  367:                     modify => \&modify_selfcreation,
  368:                   },
  369:         'usermodification' =>
  370:                   { text => 'User modification',
  371:                     help => 'Domain_Configuration_User_Modification',
  372:                     header => [{col1 => 'Target user has role',
  373:                                 col2 => 'User information updatable in author context'},
  374:                                {col1 => 'Target user has role',
  375:                                 col2 => 'User information updatable in course context'}],
  376:                     print => \&print_usermodification,
  377:                     modify => \&modify_usermodification,
  378:                   },
  379:         'scantron' =>
  380:                   { text => 'Bubblesheet format file',
  381:                     help => 'Domain_Configuration_Scantron_Format',
  382:                     header => [ {col1 => 'Item',
  383:                                  col2 => '',
  384:                               }],
  385:                     print => \&print_scantron,
  386:                     modify => \&modify_scantron,
  387:                   },
  388:         'requestcourses' => 
  389:                  {text => 'Request creation of courses',
  390:                   help => 'Domain_Configuration_Request_Courses',
  391:                   header => [{col1 => 'User affiliation',
  392:                               col2 => 'Availability/Processing of requests',},
  393:                              {col1 => 'Setting',
  394:                               col2 => 'Value'},
  395:                              {col1 => 'Available textbooks',
  396:                               col2 => ''},
  397:                              {col1 => 'Available templates',
  398:                               col2 => ''},
  399:                              {col1 => 'Validation (not official courses)',
  400:                               col2 => 'Value'},],
  401:                   print => \&print_quotas,
  402:                   modify => \&modify_quotas,
  403:                  },
  404:         'requestauthor' =>
  405:                  {text => 'Request Authoring Space',
  406:                   help => 'Domain_Configuration_Request_Author',
  407:                   header => [{col1 => 'User affiliation',
  408:                               col2 => 'Availability/Processing of requests',},
  409:                              {col1 => 'Setting',
  410:                               col2 => 'Value'}],
  411:                   print => \&print_quotas,
  412:                   modify => \&modify_quotas,
  413:                  },
  414:         'coursecategories' =>
  415:                   { text => 'Cataloging of courses/communities',
  416:                     help => 'Domain_Configuration_Cataloging_Courses',
  417:                     header => [{col1 => 'Catalog type/availability',
  418:                                 col2 => '',},
  419:                                {col1 => 'Category settings for standard catalog',
  420:                                 col2 => '',},
  421:                                {col1 => 'Categories',
  422:                                 col2 => '',
  423:                                }],
  424:                     print => \&print_coursecategories,
  425:                     modify => \&modify_coursecategories,
  426:                   },
  427:         'serverstatuses' =>
  428:                  {text   => 'Access to server status pages',
  429:                   help   => 'Domain_Configuration_Server_Status',
  430:                   header => [{col1 => 'Status Page',
  431:                               col2 => 'Other named users',
  432:                               col3 => 'Specific IPs',
  433:                             }],
  434:                   print => \&print_serverstatuses,
  435:                   modify => \&modify_serverstatuses,
  436:                  },
  437:         'helpsettings' =>
  438:                  {text   => 'Support settings',
  439:                   help   => 'Domain_Configuration_Help_Settings',
  440:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  441:                               col2 => 'Value'},
  442:                              {col1 => 'Helpdesk Roles',
  443:                               col2 => 'Settings'},],
  444:                   print  => \&print_helpsettings,
  445:                   modify => \&modify_helpsettings,
  446:                  },
  447:         'coursedefaults' => 
  448:                  {text => 'Course/Community defaults',
  449:                   help => 'Domain_Configuration_Course_Defaults',
  450:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  451:                               col2 => 'Value',},
  452:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  453:                               col2 => 'Value',},],
  454:                   print => \&print_coursedefaults,
  455:                   modify => \&modify_coursedefaults,
  456:                  },
  457:         'selfenrollment' => 
  458:                  {text   => 'Self-enrollment in Course/Community',
  459:                   help   => 'Domain_Configuration_Selfenrollment',
  460:                   header => [{col1 => 'Configuration Rights',
  461:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  462:                              {col1 => 'Defaults',
  463:                               col2 => 'Value'},
  464:                              {col1 => 'Self-enrollment validation (optional)',
  465:                               col2 => 'Value'},],
  466:                   print => \&print_selfenrollment,
  467:                   modify => \&modify_selfenrollment,
  468:                  },
  469:         'usersessions' =>
  470:                  {text  => 'User session hosting/offloading',
  471:                   help  => 'Domain_Configuration_User_Sessions',
  472:                   header => [{col1 => 'Domain server',
  473:                               col2 => 'Servers to offload sessions to when busy'},
  474:                              {col1 => 'Hosting of users from other domains',
  475:                               col2 => 'Rules'},
  476:                              {col1 => "Hosting domain's own users elsewhere",
  477:                               col2 => 'Rules'}],
  478:                   print => \&print_usersessions,
  479:                   modify => \&modify_usersessions,
  480:                  },
  481:         'loadbalancing' =>
  482:                  {text  => 'Dedicated Load Balancer(s)',
  483:                   help  => 'Domain_Configuration_Load_Balancing',
  484:                   header => [{col1 => 'Balancers',
  485:                               col2 => 'Default destinations',
  486:                               col3 => 'User affiliation',
  487:                               col4 => 'Overrides'},
  488:                             ],
  489:                   print => \&print_loadbalancing,
  490:                   modify => \&modify_loadbalancing,
  491:                  },
  492:         'ltitools' =>
  493:                  {text => 'External Tools (LTI)',
  494:                   help => 'Domain_Configuration_LTI_Tools',
  495:                   header => [{col1 => 'Setting',
  496:                               col2 => 'Value',}],
  497:                   print => \&print_ltitools,
  498:                   modify => \&modify_ltitools,
  499:                  },
  500:     );
  501:     if (keys(%servers) > 1) {
  502:         $prefs{'login'}  = { text   => 'Log-in page options',
  503:                              help   => 'Domain_Configuration_Login_Page',
  504:                             header => [{col1 => 'Log-in Service',
  505:                                         col2 => 'Server Setting',},
  506:                                        {col1 => 'Log-in Page Items',
  507:                                         col2 => ''},
  508:                                        {col1 => 'Log-in Help',
  509:                                         col2 => 'Value'},
  510:                                        {col1 => 'Custom HTML in document head',
  511:                                         col2 => 'Value'}],
  512:                             print => \&print_login,
  513:                             modify => \&modify_login,
  514:                            };
  515:     }
  516: 
  517:     my @roles = ('student','coordinator','author','admin');
  518:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  519:     &Apache::lonhtmlcommon::add_breadcrumb
  520:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  521:       text=>"Settings to display/modify"});
  522:     my $confname = $dom.'-domainconfig';
  523: 
  524:     if ($phase eq 'process') {
  525:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  526:                                                               \%prefs,\%domconfig,$confname,\@roles);
  527:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  528:             $r->rflush();
  529:             &devalidate_remote_domconfs($dom,$result);
  530:         }
  531:     } elsif ($phase eq 'display') {
  532:         my $js = &recaptcha_js().
  533:                  &toggle_display_js();
  534:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  535:             my ($othertitle,$usertypes,$types) =
  536:                 &Apache::loncommon::sorted_inst_types($dom);
  537:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  538:                                           $domconfig{'loadbalancing'}).
  539:                    &new_spares_js().
  540:                    &common_domprefs_js().
  541:                    &Apache::loncommon::javascript_array_indexof();
  542:         }
  543:         if (grep(/^requestcourses$/,@actions)) {
  544:             my $javascript_validations;
  545:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  546:             $js .= <<END;
  547: <script type="text/javascript">
  548: $javascript_validations
  549: </script>
  550: $coursebrowserjs
  551: END
  552:         }
  553:         if (grep(/^contacts$/,@actions)) {
  554:             $js .= &contacts_javascript();
  555:         }
  556:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  557:     } else {
  558: # check if domconfig user exists for the domain.
  559:         my $servadm = $r->dir_config('lonAdmEMail');
  560:         my ($configuserok,$author_ok,$switchserver) =
  561:             &config_check($dom,$confname,$servadm);
  562:         unless ($configuserok eq 'ok') {
  563:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  564:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  565:                           $confname).
  566:                       '<br />'
  567:             );
  568:             if ($switchserver) {
  569:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  570:                           '<br />'.
  571:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  572:                           '<br />'.
  573:                           &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).
  574:                           '<br />'.
  575:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  576:                 );
  577:             } else {
  578:                 $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.').
  579:                           '<br />'.
  580:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  581:                 );
  582:             }
  583:             $r->print(&Apache::loncommon::end_page());
  584:             return OK;
  585:         }
  586:         if (keys(%domconfig) == 0) {
  587:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  588:             my @ids=&Apache::lonnet::current_machine_ids();
  589:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  590:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  591:                 my @loginimages = ('img','logo','domlogo','login');
  592:                 my $custom_img_count = 0;
  593:                 foreach my $img (@loginimages) {
  594:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  595:                         $custom_img_count ++;
  596:                     }
  597:                 }
  598:                 foreach my $role (@roles) {
  599:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  600:                         $custom_img_count ++;
  601:                     }
  602:                 }
  603:                 if ($custom_img_count > 0) {
  604:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  605:                     my $switch_server = &check_switchserver($dom,$confname);
  606:                     $r->print(
  607:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  608:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  609:     &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 />'.
  610:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  611:                     if ($switch_server) {
  612:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  613:                     }
  614:                     $r->print(&Apache::loncommon::end_page());
  615:                     return OK;
  616:                 }
  617:             }
  618:         }
  619:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  620:     }
  621:     return OK;
  622: }
  623: 
  624: sub process_changes {
  625:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  626:     my %domconfig;
  627:     if (ref($values) eq 'HASH') {
  628:         %domconfig = %{$values};
  629:     }
  630:     my $output;
  631:     if ($action eq 'login') {
  632:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  633:     } elsif ($action eq 'rolecolors') {
  634:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  635:                                      $lastactref,%domconfig);
  636:     } elsif ($action eq 'quotas') {
  637:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  638:     } elsif ($action eq 'autoenroll') {
  639:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  640:     } elsif ($action eq 'autoupdate') {
  641:         $output = &modify_autoupdate($dom,%domconfig);
  642:     } elsif ($action eq 'autocreate') {
  643:         $output = &modify_autocreate($dom,%domconfig);
  644:     } elsif ($action eq 'directorysrch') {
  645:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  646:     } elsif ($action eq 'usercreation') {
  647:         $output = &modify_usercreation($dom,%domconfig);
  648:     } elsif ($action eq 'selfcreation') {
  649:         $output = &modify_selfcreation($dom,%domconfig);
  650:     } elsif ($action eq 'usermodification') {
  651:         $output = &modify_usermodification($dom,%domconfig);
  652:     } elsif ($action eq 'contacts') {
  653:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  654:     } elsif ($action eq 'defaults') {
  655:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  656:     } elsif ($action eq 'scantron') {
  657:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  658:     } elsif ($action eq 'coursecategories') {
  659:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  660:     } elsif ($action eq 'serverstatuses') {
  661:         $output = &modify_serverstatuses($dom,%domconfig);
  662:     } elsif ($action eq 'requestcourses') {
  663:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  664:     } elsif ($action eq 'requestauthor') {
  665:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  666:     } elsif ($action eq 'helpsettings') {
  667:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  668:     } elsif ($action eq 'coursedefaults') {
  669:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  670:     } elsif ($action eq 'selfenrollment') {
  671:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  672:     } elsif ($action eq 'usersessions') {
  673:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  674:     } elsif ($action eq 'loadbalancing') {
  675:         $output = &modify_loadbalancing($dom,%domconfig);
  676:     } elsif ($action eq 'ltitools') {
  677:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  678:     }
  679:     return $output;
  680: }
  681: 
  682: sub print_config_box {
  683:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  684:     my $rowtotal = 0;
  685:     my $output;
  686:     if ($action eq 'coursecategories') {
  687:         $output = &coursecategories_javascript($settings);
  688:     } elsif ($action eq 'defaults') {
  689:         $output = &defaults_javascript($settings); 
  690:     } elsif ($action eq 'helpsettings') {
  691:         my (%privs,%levelscurrent);
  692:         my %full=();
  693:         my %levels=(
  694:                      course => {},
  695:                      domain => {},
  696:                      system => {},
  697:                    );
  698:         my $context = 'domain';
  699:         my $crstype = 'Course';
  700:         my $formname = 'display';
  701:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  702:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  703:         $output =
  704:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full,
  705:                                                       \@templateroles);
  706:     } elsif ($action eq 'ltitools') {
  707:         $output .= &ltitools_javascript($settings);
  708:     }
  709:     $output .=
  710:          '<table class="LC_nested_outer">
  711:           <tr>
  712:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  713:            &mt($item->{text}).'&nbsp;'.
  714:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  715:           '</tr>';
  716:     $rowtotal ++;
  717:     my $numheaders = 1;
  718:     if (ref($item->{'header'}) eq 'ARRAY') {
  719:         $numheaders = scalar(@{$item->{'header'}});
  720:     }
  721:     if ($numheaders > 1) {
  722:         my $colspan = '';
  723:         my $rightcolspan = '';
  724:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  725:             ($action eq 'directorysrch') ||
  726:             (($action eq 'login') && ($numheaders < 4))) {
  727:             $colspan = ' colspan="2"';
  728:         }
  729:         if ($action eq 'usersessions') {
  730:             $rightcolspan = ' colspan="3"'; 
  731:         }
  732:         $output .= '
  733:           <tr>
  734:            <td>
  735:             <table class="LC_nested">
  736:              <tr class="LC_info_row">
  737:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  738:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  739:              </tr>';
  740:         $rowtotal ++;
  741:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  742:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  743:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'directorysrch') ||
  744:             ($action eq 'helpsettings') || ($action eq 'contacts')) {
  745:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  746:         } elsif ($action eq 'coursecategories') {
  747:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  748:         } elsif ($action eq 'login') {
  749:             if ($numheaders == 4) {
  750:                 $colspan = ' colspan="2"';
  751:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  752:             } else {
  753:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  754:             }
  755:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  756:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  757:         } elsif ($action eq 'rolecolors') {
  758:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  759:         }
  760:         $output .= '
  761:            </table>
  762:           </td>
  763:          </tr>
  764:          <tr>
  765:            <td>
  766:             <table class="LC_nested">
  767:              <tr class="LC_info_row">
  768:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  769:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  770:              </tr>';
  771:             $rowtotal ++;
  772:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  773:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  774:             ($action eq 'usersessions') || ($action eq 'coursecategories') ||
  775:             ($action eq 'contacts') || ($action eq 'defaults')) {
  776:             if ($action eq 'coursecategories') {
  777:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  778:                 $colspan = ' colspan="2"';
  779:             } else {
  780:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  781:             }
  782:             $output .= '
  783:            </table>
  784:           </td>
  785:          </tr>
  786:          <tr>
  787:            <td>
  788:             <table class="LC_nested">
  789:              <tr class="LC_info_row">
  790:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  791:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  792:              </tr>'."\n";
  793:             if ($action eq 'coursecategories') {
  794:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  795:             } else {
  796:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  797:             }
  798:             $rowtotal ++;
  799:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  800:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  801:                  ($action eq 'helpsettings')) {
  802:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  803:         } elsif ($action eq 'login') {
  804:             if ($numheaders == 4) {
  805:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  806:            </table>
  807:           </td>
  808:          </tr>
  809:          <tr>
  810:            <td>
  811:             <table class="LC_nested">
  812:              <tr class="LC_info_row">
  813:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  814:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  815:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  816:                 $rowtotal ++;
  817:             } else {
  818:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  819:             }
  820:             $output .= '
  821:            </table>
  822:           </td>
  823:          </tr>
  824:          <tr>
  825:            <td>
  826:             <table class="LC_nested">
  827:              <tr class="LC_info_row">';
  828:             if ($numheaders == 4) {
  829:                 $output .= '
  830:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  831:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  832:              </tr>';
  833:             } else {
  834:                 $output .= '
  835:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  836:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  837:              </tr>';
  838:             }
  839:             $rowtotal ++;
  840:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  841:         } elsif ($action eq 'requestcourses') {
  842:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  843:             $rowtotal ++;
  844:             $output .= &print_studentcode($settings,\$rowtotal).'
  845:            </table>
  846:           </td>
  847:          </tr>
  848:          <tr>
  849:            <td>
  850:             <table class="LC_nested">
  851:              <tr class="LC_info_row">
  852:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  853:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  854:                        &textbookcourses_javascript($settings).
  855:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  856:             </table>
  857:            </td>
  858:           </tr>
  859:          <tr>
  860:            <td>
  861:             <table class="LC_nested">
  862:              <tr class="LC_info_row">
  863:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  864:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  865:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  866:             </table>
  867:            </td>
  868:           </tr>
  869:           <tr>
  870:            <td>
  871:             <table class="LC_nested">
  872:              <tr class="LC_info_row">
  873:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  874:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  875:              </tr>'.
  876:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  877:         } elsif ($action eq 'requestauthor') {
  878:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  879:             $rowtotal ++;
  880:         } elsif ($action eq 'rolecolors') {
  881:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  882:            </table>
  883:           </td>
  884:          </tr>
  885:          <tr>
  886:            <td>
  887:             <table class="LC_nested">
  888:              <tr class="LC_info_row">
  889:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  890:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  891:               <td class="LC_right_item" valign="top">'.
  892:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  893:              </tr>'.
  894:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  895:            </table>
  896:           </td>
  897:          </tr>
  898:          <tr>
  899:            <td>
  900:             <table class="LC_nested">
  901:              <tr class="LC_info_row">
  902:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  903:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  904:              </tr>'.
  905:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  906:             $rowtotal += 2;
  907:         }
  908:     } else {
  909:         $output .= '
  910:           <tr>
  911:            <td>
  912:             <table class="LC_nested">
  913:              <tr class="LC_info_row">';
  914:         if ($action eq 'login') {
  915:             $output .= '  
  916:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  917:         } elsif ($action eq 'serverstatuses') {
  918:             $output .= '
  919:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  920:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  921: 
  922:         } else {
  923:             $output .= '
  924:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  925:         }
  926:         if (defined($item->{'header'}->[0]->{'col3'})) {
  927:             $output .= '<td class="LC_left_item" valign="top">'.
  928:                        &mt($item->{'header'}->[0]->{'col2'});
  929:             if ($action eq 'serverstatuses') {
  930:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  931:             } 
  932:         } else {
  933:             $output .= '<td class="LC_right_item" valign="top">'.
  934:                        &mt($item->{'header'}->[0]->{'col2'});
  935:         }
  936:         $output .= '</td>';
  937:         if ($item->{'header'}->[0]->{'col3'}) {
  938:             if (defined($item->{'header'}->[0]->{'col4'})) {
  939:                 $output .= '<td class="LC_left_item" valign="top">'.
  940:                             &mt($item->{'header'}->[0]->{'col3'});
  941:             } else {
  942:                 $output .= '<td class="LC_right_item" valign="top">'.
  943:                            &mt($item->{'header'}->[0]->{'col3'});
  944:             }
  945:             if ($action eq 'serverstatuses') {
  946:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  947:             }
  948:             $output .= '</td>';
  949:         }
  950:         if ($item->{'header'}->[0]->{'col4'}) {
  951:             $output .= '<td class="LC_right_item" valign="top">'.
  952:                        &mt($item->{'header'}->[0]->{'col4'});
  953:         }
  954:         $output .= '</tr>';
  955:         $rowtotal ++;
  956:         if ($action eq 'quotas') {
  957:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  958:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
  959:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') ||
  960:                  ($action eq 'ltitools')) {
  961:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  962:         } elsif ($action eq 'scantron') {
  963:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  964:         }
  965:     }
  966:     $output .= '
  967:    </table>
  968:   </td>
  969:  </tr>
  970: </table><br />';
  971:     return ($output,$rowtotal);
  972: }
  973: 
  974: sub print_login {
  975:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  976:     my ($css_class,$datatable);
  977:     my %choices = &login_choices();
  978: 
  979:     if ($caller eq 'service') {
  980:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  981:         my $choice = $choices{'disallowlogin'};
  982:         $css_class = ' class="LC_odd_row"';
  983:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  984:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  985:                       '<th>'.$choices{'server'}.'</th>'.
  986:                       '<th>'.$choices{'serverpath'}.'</th>'.
  987:                       '<th>'.$choices{'custompath'}.'</th>'.
  988:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  989:         my %disallowed;
  990:         if (ref($settings) eq 'HASH') {
  991:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  992:                %disallowed = %{$settings->{'loginvia'}};
  993:             }
  994:         }
  995:         foreach my $lonhost (sort(keys(%servers))) {
  996:             my $direct = 'selected="selected"';
  997:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  998:                 if ($disallowed{$lonhost}{'server'} ne '') {
  999:                     $direct = '';
 1000:                 }
 1001:             }
 1002:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1003:                           '<td><select name="'.$lonhost.'_server">'.
 1004:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1005:                           '</option>';
 1006:             foreach my $hostid (sort(keys(%servers))) {
 1007:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1008:                 my $selected = '';
 1009:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1010:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1011:                         $selected = 'selected="selected"';
 1012:                     }
 1013:                 }
 1014:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1015:                               $servers{$hostid}.'</option>';
 1016:             }
 1017:             $datatable .= '</select></td>'.
 1018:                           '<td><select name="'.$lonhost.'_serverpath">';
 1019:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1020:                 my $pathname = $path;
 1021:                 if ($path eq 'custom') {
 1022:                     $pathname = &mt('Custom Path').' ->';
 1023:                 }
 1024:                 my $selected = '';
 1025:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1026:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1027:                         $selected = 'selected="selected"';
 1028:                     }
 1029:                 } elsif ($path eq '') {
 1030:                     $selected = 'selected="selected"';
 1031:                 }
 1032:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1033:             }
 1034:             $datatable .= '</select></td>';
 1035:             my ($custom,$exempt);
 1036:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1037:                 $custom = $disallowed{$lonhost}{'custompath'};
 1038:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1039:             }
 1040:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1041:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1042:                           '</tr>';
 1043:         }
 1044:         $datatable .= '</table></td></tr>';
 1045:         return $datatable;
 1046:     } elsif ($caller eq 'page') {
 1047:         my %defaultchecked = ( 
 1048:                                'coursecatalog' => 'on',
 1049:                                'helpdesk'      => 'on',
 1050:                                'adminmail'     => 'off',
 1051:                                'newuser'       => 'off',
 1052:                              );
 1053:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1054:         my (%checkedon,%checkedoff);
 1055:         foreach my $item (@toggles) {
 1056:             if ($defaultchecked{$item} eq 'on') { 
 1057:                 $checkedon{$item} = ' checked="checked" ';
 1058:                 $checkedoff{$item} = ' ';
 1059:             } elsif ($defaultchecked{$item} eq 'off') {
 1060:                 $checkedoff{$item} = ' checked="checked" ';
 1061:                 $checkedon{$item} = ' ';
 1062:             }
 1063:         }
 1064:         my @images = ('img','logo','domlogo','login');
 1065:         my @logintext = ('textcol','bgcol');
 1066:         my @bgs = ('pgbg','mainbg','sidebg');
 1067:         my @links = ('link','alink','vlink');
 1068:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1069:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1070:         my (%is_custom,%designs);
 1071:         my %defaults = (
 1072:                        font => $defaultdesign{'login.font'},
 1073:                        );
 1074:         foreach my $item (@images) {
 1075:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1076:             $defaults{'showlogo'}{$item} = 1;
 1077:         }
 1078:         foreach my $item (@bgs) {
 1079:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1080:         }
 1081:         foreach my $item (@logintext) {
 1082:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1083:         }
 1084:         foreach my $item (@links) {
 1085:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1086:         }
 1087:         if (ref($settings) eq 'HASH') {
 1088:             foreach my $item (@toggles) {
 1089:                 if ($settings->{$item} eq '1') {
 1090:                     $checkedon{$item} =  ' checked="checked" ';
 1091:                     $checkedoff{$item} = ' ';
 1092:                 } elsif ($settings->{$item} eq '0') {
 1093:                     $checkedoff{$item} =  ' checked="checked" ';
 1094:                     $checkedon{$item} = ' ';
 1095:                 }
 1096:             }
 1097:             foreach my $item (@images) {
 1098:                 if (defined($settings->{$item})) {
 1099:                     $designs{$item} = $settings->{$item};
 1100:                     $is_custom{$item} = 1;
 1101:                 }
 1102:                 if (defined($settings->{'showlogo'}{$item})) {
 1103:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1104:                 }
 1105:             }
 1106:             foreach my $item (@logintext) {
 1107:                 if ($settings->{$item} ne '') {
 1108:                     $designs{'logintext'}{$item} = $settings->{$item};
 1109:                     $is_custom{$item} = 1;
 1110:                 }
 1111:             }
 1112:             if ($settings->{'font'} ne '') {
 1113:                 $designs{'font'} = $settings->{'font'};
 1114:                 $is_custom{'font'} = 1;
 1115:             }
 1116:             foreach my $item (@bgs) {
 1117:                 if ($settings->{$item} ne '') {
 1118:                     $designs{'bgs'}{$item} = $settings->{$item};
 1119:                     $is_custom{$item} = 1;
 1120:                 }
 1121:             }
 1122:             foreach my $item (@links) {
 1123:                 if ($settings->{$item} ne '') {
 1124:                     $designs{'links'}{$item} = $settings->{$item};
 1125:                     $is_custom{$item} = 1;
 1126:                 }
 1127:             }
 1128:         } else {
 1129:             if ($designhash{$dom.'.login.font'} ne '') {
 1130:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1131:                 $is_custom{'font'} = 1;
 1132:             }
 1133:             foreach my $item (@images) {
 1134:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1135:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1136:                     $is_custom{$item} = 1;
 1137:                 }
 1138:             }
 1139:             foreach my $item (@bgs) {
 1140:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1141:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1142:                     $is_custom{$item} = 1;
 1143:                 }
 1144:             }
 1145:             foreach my $item (@links) {
 1146:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1147:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1148:                     $is_custom{$item} = 1;
 1149:                 }
 1150:             }
 1151:         }
 1152:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1153:                                                       logo => 'Institution Logo',
 1154:                                                       domlogo => 'Domain Logo',
 1155:                                                       login => 'Login box');
 1156:         my $itemcount = 1;
 1157:         foreach my $item (@toggles) {
 1158:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1159:             $datatable .=  
 1160:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1161:                 '</td><td>'.
 1162:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1163:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1164:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1165:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1166:                 '</tr>';
 1167:             $itemcount ++;
 1168:         }
 1169:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1170:         $datatable .= '</tr></table></td></tr>';
 1171:     } elsif ($caller eq 'help') {
 1172:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1173:         my $switchserver = &check_switchserver($dom,$confname);
 1174:         my $itemcount = 1;
 1175:         $defaulturl = '/adm/loginproblems.html';
 1176:         $defaulttype = 'default';
 1177:         %lt = &Apache::lonlocal::texthash (
 1178:                      del     => 'Delete?',
 1179:                      rep     => 'Replace:',
 1180:                      upl     => 'Upload:',
 1181:                      default => 'Default',
 1182:                      custom  => 'Custom',
 1183:                                              );
 1184:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1185:         my @currlangs;
 1186:         if (ref($settings) eq 'HASH') {
 1187:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1188:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1189:                     next if ($settings->{'helpurl'}{$key} eq '');
 1190:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1191:                     $type{$key} = 'custom';
 1192:                     unless ($key eq 'nolang') {
 1193:                         push(@currlangs,$key);
 1194:                     }
 1195:                 }
 1196:             } elsif ($settings->{'helpurl'} ne '') {
 1197:                 $type{'nolang'} = 'custom';
 1198:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1199:             }
 1200:         }
 1201:         foreach my $lang ('nolang',sort(@currlangs)) {
 1202:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1203:             $datatable .= '<tr'.$css_class.'>';
 1204:             if ($url{$lang} eq '') {
 1205:                 $url{$lang} = $defaulturl;
 1206:             }
 1207:             if ($type{$lang} eq '') {
 1208:                 $type{$lang} = $defaulttype;
 1209:             }
 1210:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1211:             if ($lang eq 'nolang') {
 1212:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1213:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1214:             } else {
 1215:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1216:                                   $langchoices{$lang},
 1217:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1218:             }
 1219:             $datatable .= '</span></td>'."\n".
 1220:                           '<td class="LC_left_item">';
 1221:             if ($type{$lang} eq 'custom') {
 1222:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1223:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1224:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1225:             } else {
 1226:                 $datatable .= $lt{'upl'};
 1227:             }
 1228:             $datatable .='<br />';
 1229:             if ($switchserver) {
 1230:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1231:             } else {
 1232:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1233:             }
 1234:             $datatable .= '</td></tr>';
 1235:             $itemcount ++;
 1236:         }
 1237:         my @addlangs;
 1238:         foreach my $lang (sort(keys(%langchoices))) {
 1239:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1240:             push(@addlangs,$lang);
 1241:         }
 1242:         if (@addlangs > 0) {
 1243:             my %toadd;
 1244:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1245:             $toadd{''} = &mt('Select');
 1246:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1247:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1248:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1249:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1250:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1251:             if ($switchserver) {
 1252:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1253:             } else {
 1254:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1255:             }
 1256:             $datatable .= '</td></tr>';
 1257:             $itemcount ++;
 1258:         }
 1259:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1260:     } elsif ($caller eq 'headtag') {
 1261:         my %domservers = &Apache::lonnet::get_servers($dom);
 1262:         my $choice = $choices{'headtag'};
 1263:         $css_class = ' class="LC_odd_row"';
 1264:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1265:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1266:                       '<th>'.$choices{'current'}.'</th>'.
 1267:                       '<th>'.$choices{'action'}.'</th>'.
 1268:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1269:         my (%currurls,%currexempt);
 1270:         if (ref($settings) eq 'HASH') {
 1271:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1272:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1273:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1274:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1275:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1276:                     }
 1277:                 }
 1278:             }
 1279:         }
 1280:         my %lt = &Apache::lonlocal::texthash(
 1281:                                                del  => 'Delete?',
 1282:                                                rep  => 'Replace:',
 1283:                                                upl  => 'Upload:',
 1284:                                                curr => 'View contents',
 1285:                                                none => 'None',
 1286:         );
 1287:         my $switchserver = &check_switchserver($dom,$confname);
 1288:         foreach my $lonhost (sort(keys(%domservers))) {
 1289:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1290:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1291:             if ($currurls{$lonhost}) {
 1292:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1293:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1294:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1295:                               '">'.$lt{'curr'}.'</a></td>'.
 1296:                               '<td><span class="LC_nobreak"><label>'.
 1297:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1298:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1299:             } else {
 1300:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1301:             }
 1302:             $datatable .='<br />';
 1303:             if ($switchserver) {
 1304:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1305:             } else {
 1306:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1307:             }
 1308:             $datatable .= '</td><td><input type="textbox" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1309:         }
 1310:         $datatable .= '</table></td></tr>';
 1311:     }
 1312:     return $datatable;
 1313: }
 1314: 
 1315: sub login_choices {
 1316:     my %choices =
 1317:         &Apache::lonlocal::texthash (
 1318:             coursecatalog => 'Display Course/Community Catalog link?',
 1319:             adminmail     => "Display Administrator's E-mail Address?",
 1320:             helpdesk      => 'Display "Contact Helpdesk" link',
 1321:             disallowlogin => "Login page requests redirected",
 1322:             hostid        => "Server",
 1323:             server        => "Redirect to:",
 1324:             serverpath    => "Path",
 1325:             custompath    => "Custom", 
 1326:             exempt        => "Exempt IP(s)",
 1327:             directlogin   => "No redirect",
 1328:             newuser       => "Link to create a user account",
 1329:             img           => "Header",
 1330:             logo          => "Main Logo",
 1331:             domlogo       => "Domain Logo",
 1332:             login         => "Log-in Header", 
 1333:             textcol       => "Text color",
 1334:             bgcol         => "Box color",
 1335:             bgs           => "Background colors",
 1336:             links         => "Link colors",
 1337:             font          => "Font color",
 1338:             pgbg          => "Header",
 1339:             mainbg        => "Page",
 1340:             sidebg        => "Login box",
 1341:             link          => "Link",
 1342:             alink         => "Active link",
 1343:             vlink         => "Visited link",
 1344:             headtag       => "Custom markup",
 1345:             action        => "Action",
 1346:             current       => "Current",
 1347:         );
 1348:     return %choices;
 1349: }
 1350: 
 1351: sub print_rolecolors {
 1352:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1353:     my %choices = &color_font_choices();
 1354:     my @bgs = ('pgbg','tabbg','sidebg');
 1355:     my @links = ('link','alink','vlink');
 1356:     my @images = ('img');
 1357:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1358:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1359:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1360:     my (%is_custom,%designs);
 1361:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1362:     if (ref($settings) eq 'HASH') {
 1363:         if (ref($settings->{$role}) eq 'HASH') {
 1364:             if ($settings->{$role}->{'img'} ne '') {
 1365:                 $designs{'img'} = $settings->{$role}->{'img'};
 1366:                 $is_custom{'img'} = 1;
 1367:             }
 1368:             if ($settings->{$role}->{'font'} ne '') {
 1369:                 $designs{'font'} = $settings->{$role}->{'font'};
 1370:                 $is_custom{'font'} = 1;
 1371:             }
 1372:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1373:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1374:                 $is_custom{'fontmenu'} = 1;
 1375:             }
 1376:             foreach my $item (@bgs) {
 1377:                 if ($settings->{$role}->{$item} ne '') {
 1378:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1379:                     $is_custom{$item} = 1;
 1380:                 }
 1381:             }
 1382:             foreach my $item (@links) {
 1383:                 if ($settings->{$role}->{$item} ne '') {
 1384:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1385:                     $is_custom{$item} = 1;
 1386:                 }
 1387:             }
 1388:         }
 1389:     } else {
 1390:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1391:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1392:             $is_custom{'img'} = 1;
 1393:         }
 1394:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1395:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1396:             $is_custom{'fontmenu'} = 1; 
 1397:         }
 1398:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1399:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1400:             $is_custom{'font'} = 1;
 1401:         }
 1402:         foreach my $item (@bgs) {
 1403:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1404:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1405:                 $is_custom{$item} = 1;
 1406:             
 1407:             }
 1408:         }
 1409:         foreach my $item (@links) {
 1410:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1411:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1412:                 $is_custom{$item} = 1;
 1413:             }
 1414:         }
 1415:     }
 1416:     my $itemcount = 1;
 1417:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1418:     $datatable .= '</tr></table></td></tr>';
 1419:     return $datatable;
 1420: }
 1421: 
 1422: sub role_defaults {
 1423:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1424:     my %defaults;
 1425:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1426:         return %defaults;
 1427:     }
 1428:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1429:     if ($role eq 'login') {
 1430:         %defaults = (
 1431:                        font => $defaultdesign{$role.'.font'},
 1432:                     );
 1433:         if (ref($logintext) eq 'ARRAY') {
 1434:             foreach my $item (@{$logintext}) {
 1435:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1436:             }
 1437:         }
 1438:         foreach my $item (@{$images}) {
 1439:             $defaults{'showlogo'}{$item} = 1;
 1440:         }
 1441:     } else {
 1442:         %defaults = (
 1443:                        img => $defaultdesign{$role.'.img'},
 1444:                        font => $defaultdesign{$role.'.font'},
 1445:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1446:                     );
 1447:     }
 1448:     foreach my $item (@{$bgs}) {
 1449:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1450:     }
 1451:     foreach my $item (@{$links}) {
 1452:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1453:     }
 1454:     foreach my $item (@{$images}) {
 1455:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1456:     }
 1457:     return %defaults;
 1458: }
 1459: 
 1460: sub display_color_options {
 1461:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1462:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1463:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1464:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1465:     my $datatable = '<tr'.$css_class.'>'.
 1466:         '<td>'.$choices->{'font'}.'</td>';
 1467:     if (!$is_custom->{'font'}) {
 1468:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1469:     } else {
 1470:         $datatable .= '<td>&nbsp;</td>';
 1471:     }
 1472:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1473: 
 1474:     $datatable .= '<td><span class="LC_nobreak">'.
 1475:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1476:                   ' value="'.$current_color.'" />&nbsp;'.
 1477:                   '&nbsp;</td></tr>';
 1478:     unless ($role eq 'login') { 
 1479:         $datatable .= '<tr'.$css_class.'>'.
 1480:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1481:         if (!$is_custom->{'fontmenu'}) {
 1482:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1483:         } else {
 1484:             $datatable .= '<td>&nbsp;</td>';
 1485:         }
 1486: 	$current_color = $designs->{'fontmenu'} ?
 1487: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1488:         $datatable .= '<td><span class="LC_nobreak">'.
 1489:                       '<input class="colorchooser" type="text" size="10" name="'
 1490: 		      .$role.'_fontmenu"'.
 1491:                       ' value="'.$current_color.'" />&nbsp;'.
 1492:                       '&nbsp;</td></tr>';
 1493:     }
 1494:     my $switchserver = &check_switchserver($dom,$confname);
 1495:     foreach my $img (@{$images}) {
 1496: 	$itemcount ++;
 1497:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1498:         $datatable .= '<tr'.$css_class.'>'.
 1499:                       '<td>'.$choices->{$img};
 1500:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1501:         if ($role eq 'login') {
 1502:             if ($img eq 'login') {
 1503:                 $login_hdr_pick =
 1504:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1505:                 $logincolors =
 1506:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1507:                                        $designs,$defaults);
 1508:             } elsif ($img ne 'domlogo') {
 1509:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1510:             }
 1511:         }
 1512:         $datatable .= '</td>';
 1513:         if ($designs->{$img} ne '') {
 1514:             $imgfile = $designs->{$img};
 1515: 	    $img_import = ($imgfile =~ m{^/adm/});
 1516:         } else {
 1517:             $imgfile = $defaults->{$img};
 1518:         }
 1519:         if ($imgfile) {
 1520:             my ($showfile,$fullsize);
 1521:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1522:                 my $urldir = $1;
 1523:                 my $filename = $2;
 1524:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1525:                 if (@info) {
 1526:                     my $thumbfile = 'tn-'.$filename;
 1527:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1528:                     if (@thumb) {
 1529:                         $showfile = $urldir.'/'.$thumbfile;
 1530:                     } else {
 1531:                         $showfile = $imgfile;
 1532:                     }
 1533:                 } else {
 1534:                     $showfile = '';
 1535:                 }
 1536:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1537:                 $showfile = $imgfile;
 1538:                 my $imgdir = $1;
 1539:                 my $filename = $2;
 1540:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1541:                     $showfile = "/$imgdir/tn-".$filename;
 1542:                 } else {
 1543:                     my $input = $londocroot.$imgfile;
 1544:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1545:                     if (!-e $output) {
 1546:                         my ($width,$height) = &thumb_dimensions();
 1547:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1548:                         if ($fullwidth ne '' && $fullheight ne '') {
 1549:                             if ($fullwidth > $width && $fullheight > $height) { 
 1550:                                 my $size = $width.'x'.$height;
 1551:                                 system("convert -sample $size $input $output");
 1552:                                 $showfile = "/$imgdir/tn-".$filename;
 1553:                             }
 1554:                         }
 1555:                     }
 1556:                 }
 1557:             }
 1558:             if ($showfile) {
 1559:                 if ($showfile =~ m{^/(adm|res)/}) {
 1560:                     if ($showfile =~ m{^/res/}) {
 1561:                         my $local_showfile =
 1562:                             &Apache::lonnet::filelocation('',$showfile);
 1563:                         &Apache::lonnet::repcopy($local_showfile);
 1564:                     }
 1565:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1566:                 }
 1567:                 if ($imgfile) {
 1568:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1569:                         if ($imgfile =~ m{^/res/}) {
 1570:                             my $local_imgfile =
 1571:                                 &Apache::lonnet::filelocation('',$imgfile);
 1572:                             &Apache::lonnet::repcopy($local_imgfile);
 1573:                         }
 1574:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1575:                     } else {
 1576:                         $fullsize = $imgfile;
 1577:                     }
 1578:                 }
 1579:                 $datatable .= '<td>';
 1580:                 if ($img eq 'login') {
 1581:                     $datatable .= $login_hdr_pick;
 1582:                 } 
 1583:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1584:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1585:             } else {
 1586:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1587:                               &mt('Upload:').'<br />';
 1588:             }
 1589:         } else {
 1590:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1591:                           &mt('Upload:').'<br />';
 1592:         }
 1593:         if ($switchserver) {
 1594:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1595:         } else {
 1596:             if ($img ne 'login') { # suppress file selection for Log-in header
 1597:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1598:             }
 1599:         }
 1600:         $datatable .= '</td></tr>';
 1601:     }
 1602:     $itemcount ++;
 1603:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1604:     $datatable .= '<tr'.$css_class.'>'.
 1605:                   '<td>'.$choices->{'bgs'}.'</td>';
 1606:     my $bgs_def;
 1607:     foreach my $item (@{$bgs}) {
 1608:         if (!$is_custom->{$item}) {
 1609:             $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>';
 1610:         }
 1611:     }
 1612:     if ($bgs_def) {
 1613:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1614:     } else {
 1615:         $datatable .= '<td>&nbsp;</td>';
 1616:     }
 1617:     $datatable .= '<td class="LC_right_item">'.
 1618:                   '<table border="0"><tr>';
 1619: 
 1620:     foreach my $item (@{$bgs}) {
 1621:         $datatable .= '<td align="center">'.$choices->{$item};
 1622: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1623:         if ($designs->{'bgs'}{$item}) {
 1624:             $datatable .= '&nbsp;';
 1625:         }
 1626:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1627:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1628:     }
 1629:     $datatable .= '</tr></table></td></tr>';
 1630:     $itemcount ++;
 1631:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1632:     $datatable .= '<tr'.$css_class.'>'.
 1633:                   '<td>'.$choices->{'links'}.'</td>';
 1634:     my $links_def;
 1635:     foreach my $item (@{$links}) {
 1636:         if (!$is_custom->{$item}) {
 1637:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1638:         }
 1639:     }
 1640:     if ($links_def) {
 1641:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1642:     } else {
 1643:         $datatable .= '<td>&nbsp;</td>';
 1644:     }
 1645:     $datatable .= '<td class="LC_right_item">'.
 1646:                   '<table border="0"><tr>';
 1647:     foreach my $item (@{$links}) {
 1648: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1649:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1650:         if ($designs->{'links'}{$item}) {
 1651:             $datatable.='&nbsp;';
 1652:         }
 1653:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1654:                       '" /></td>';
 1655:     }
 1656:     $$rowtotal += $itemcount;
 1657:     return $datatable;
 1658: }
 1659: 
 1660: sub logo_display_options {
 1661:     my ($img,$defaults,$designs) = @_;
 1662:     my $checkedon;
 1663:     if (ref($defaults) eq 'HASH') {
 1664:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1665:             if ($defaults->{'showlogo'}{$img}) {
 1666:                 $checkedon = 'checked="checked" ';     
 1667:             }
 1668:         } 
 1669:     }
 1670:     if (ref($designs) eq 'HASH') {
 1671:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1672:             if (defined($designs->{'showlogo'}{$img})) {
 1673:                 if ($designs->{'showlogo'}{$img} == 0) {
 1674:                     $checkedon = '';
 1675:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1676:                     $checkedon = 'checked="checked" ';
 1677:                 }
 1678:             }
 1679:         }
 1680:     }
 1681:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1682:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1683:            &mt('show').'</label>'."\n";
 1684: }
 1685: 
 1686: sub login_header_options  {
 1687:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1688:     my $output = '';
 1689:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1690:         $output .= &mt('Text default(s):').'<br />';
 1691:         if (!$is_custom->{'textcol'}) {
 1692:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1693:                        '&nbsp;&nbsp;&nbsp;';
 1694:         }
 1695:         if (!$is_custom->{'bgcol'}) {
 1696:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1697:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1698:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1699:         }
 1700:         $output .= '<br />';
 1701:     }
 1702:     $output .='<br />';
 1703:     return $output;
 1704: }
 1705: 
 1706: sub login_text_colors {
 1707:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1708:     my $color_menu = '<table border="0"><tr>';
 1709:     foreach my $item (@{$logintext}) {
 1710:         $color_menu .= '<td align="center">'.$choices->{$item};
 1711:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1712:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1713:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1714:     }
 1715:     $color_menu .= '</tr></table><br />';
 1716:     return $color_menu;
 1717: }
 1718: 
 1719: sub image_changes {
 1720:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1721:     my $output;
 1722:     if ($img eq 'login') {
 1723:             # suppress image for Log-in header
 1724:     } elsif (!$is_custom) {
 1725:         if ($img ne 'domlogo') {
 1726:             $output .= &mt('Default image:').'<br />';
 1727:         } else {
 1728:             $output .= &mt('Default in use:').'<br />';
 1729:         }
 1730:     }
 1731:     if ($img eq 'login') { # suppress image for Log-in header
 1732:         $output .= '<td>'.$logincolors;
 1733:     } else {
 1734:         if ($img_import) {
 1735:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1736:         }
 1737:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1738:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1739:         if ($is_custom) {
 1740:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1741:                        '<input type="checkbox" name="'.
 1742:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1743:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1744:         } else {
 1745:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1746:         }
 1747:     }
 1748:     return $output;
 1749: }
 1750: 
 1751: sub print_quotas {
 1752:     my ($dom,$settings,$rowtotal,$action) = @_;
 1753:     my $context;
 1754:     if ($action eq 'quotas') {
 1755:         $context = 'tools';
 1756:     } else {
 1757:         $context = $action;
 1758:     }
 1759:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1760:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1761:     my $typecount = 0;
 1762:     my ($css_class,%titles);
 1763:     if ($context eq 'requestcourses') {
 1764:         @usertools = ('official','unofficial','community','textbook');
 1765:         @options =('norequest','approval','validate','autolimit');
 1766:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1767:         %titles = &courserequest_titles();
 1768:     } elsif ($context eq 'requestauthor') {
 1769:         @usertools = ('author');
 1770:         @options = ('norequest','approval','automatic');
 1771:         %titles = &authorrequest_titles();
 1772:     } else {
 1773:         @usertools = ('aboutme','blog','webdav','portfolio');
 1774:         %titles = &tool_titles();
 1775:     }
 1776:     if (ref($types) eq 'ARRAY') {
 1777:         foreach my $type (@{$types}) {
 1778:             my ($currdefquota,$currauthorquota);
 1779:             unless (($context eq 'requestcourses') ||
 1780:                     ($context eq 'requestauthor')) {
 1781:                 if (ref($settings) eq 'HASH') {
 1782:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1783:                         $currdefquota = $settings->{defaultquota}->{$type};
 1784:                     } else {
 1785:                         $currdefquota = $settings->{$type};
 1786:                     }
 1787:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1788:                         $currauthorquota = $settings->{authorquota}->{$type};
 1789:                     }
 1790:                 }
 1791:             }
 1792:             if (defined($usertypes->{$type})) {
 1793:                 $typecount ++;
 1794:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1795:                 $datatable .= '<tr'.$css_class.'>'.
 1796:                               '<td>'.$usertypes->{$type}.'</td>'.
 1797:                               '<td class="LC_left_item">';
 1798:                 if ($context eq 'requestcourses') {
 1799:                     $datatable .= '<table><tr>';
 1800:                 }
 1801:                 my %cell;  
 1802:                 foreach my $item (@usertools) {
 1803:                     if ($context eq 'requestcourses') {
 1804:                         my ($curroption,$currlimit);
 1805:                         if (ref($settings) eq 'HASH') {
 1806:                             if (ref($settings->{$item}) eq 'HASH') {
 1807:                                 $curroption = $settings->{$item}->{$type};
 1808:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1809:                                     $currlimit = $1; 
 1810:                                 }
 1811:                             }
 1812:                         }
 1813:                         if (!$curroption) {
 1814:                             $curroption = 'norequest';
 1815:                         }
 1816:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1817:                         foreach my $option (@options) {
 1818:                             my $val = $option;
 1819:                             if ($option eq 'norequest') {
 1820:                                 $val = 0;  
 1821:                             }
 1822:                             if ($option eq 'validate') {
 1823:                                 my $canvalidate = 0;
 1824:                                 if (ref($validations{$item}) eq 'HASH') { 
 1825:                                     if ($validations{$item}{$type}) {
 1826:                                         $canvalidate = 1;
 1827:                                     }
 1828:                                 }
 1829:                                 next if (!$canvalidate);
 1830:                             }
 1831:                             my $checked = '';
 1832:                             if ($option eq $curroption) {
 1833:                                 $checked = ' checked="checked"';
 1834:                             } elsif ($option eq 'autolimit') {
 1835:                                 if ($curroption =~ /^autolimit/) {
 1836:                                     $checked = ' checked="checked"';
 1837:                                 }                       
 1838:                             } 
 1839:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1840:                                   '<input type="radio" name="crsreq_'.$item.
 1841:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1842:                                   $titles{$option}.'</label>';
 1843:                             if ($option eq 'autolimit') {
 1844:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1845:                                                 $item.'_limit_'.$type.'" size="1" '.
 1846:                                                 'value="'.$currlimit.'" />';
 1847:                             }
 1848:                             $cell{$item} .= '</span> ';
 1849:                             if ($option eq 'autolimit') {
 1850:                                 $cell{$item} .= $titles{'unlimited'};
 1851:                             }
 1852:                         }
 1853:                     } elsif ($context eq 'requestauthor') {
 1854:                         my $curroption;
 1855:                         if (ref($settings) eq 'HASH') {
 1856:                             $curroption = $settings->{$type};
 1857:                         }
 1858:                         if (!$curroption) {
 1859:                             $curroption = 'norequest';
 1860:                         }
 1861:                         foreach my $option (@options) {
 1862:                             my $val = $option;
 1863:                             if ($option eq 'norequest') {
 1864:                                 $val = 0;
 1865:                             }
 1866:                             my $checked = '';
 1867:                             if ($option eq $curroption) {
 1868:                                 $checked = ' checked="checked"';
 1869:                             }
 1870:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1871:                                   '<input type="radio" name="authorreq_'.$type.
 1872:                                   '" value="'.$val.'"'.$checked.' />'.
 1873:                                   $titles{$option}.'</label></span>&nbsp; ';
 1874:                         }
 1875:                     } else {
 1876:                         my $checked = 'checked="checked" ';
 1877:                         if (ref($settings) eq 'HASH') {
 1878:                             if (ref($settings->{$item}) eq 'HASH') {
 1879:                                 if ($settings->{$item}->{$type} == 0) {
 1880:                                     $checked = '';
 1881:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1882:                                     $checked =  'checked="checked" ';
 1883:                                 }
 1884:                             }
 1885:                         }
 1886:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1887:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1888:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1889:                                       '</label></span>&nbsp; ';
 1890:                     }
 1891:                 }
 1892:                 if ($context eq 'requestcourses') {
 1893:                     $datatable .= '</tr><tr>';
 1894:                     foreach my $item (@usertools) {
 1895:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1896:                     }
 1897:                     $datatable .= '</tr></table>';
 1898:                 }
 1899:                 $datatable .= '</td>';
 1900:                 unless (($context eq 'requestcourses') ||
 1901:                         ($context eq 'requestauthor')) {
 1902:                     $datatable .= 
 1903:                               '<td class="LC_right_item">'.
 1904:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1905:                               '<input type="text" name="quota_'.$type.
 1906:                               '" value="'.$currdefquota.
 1907:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1908:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1909:                               '<input type="text" name="authorquota_'.$type.
 1910:                               '" value="'.$currauthorquota.
 1911:                               '" size="5" /></span></td>';
 1912:                 }
 1913:                 $datatable .= '</tr>';
 1914:             }
 1915:         }
 1916:     }
 1917:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1918:         $defaultquota = '20';
 1919:         $authorquota = '500';
 1920:         if (ref($settings) eq 'HASH') {
 1921:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1922:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1923:             } elsif (defined($settings->{'default'})) {
 1924:                 $defaultquota = $settings->{'default'};
 1925:             }
 1926:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1927:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1928:             }
 1929:         }
 1930:     }
 1931:     $typecount ++;
 1932:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1933:     $datatable .= '<tr'.$css_class.'>'.
 1934:                   '<td>'.$othertitle.'</td>'.
 1935:                   '<td class="LC_left_item">';
 1936:     if ($context eq 'requestcourses') {
 1937:         $datatable .= '<table><tr>';
 1938:     }
 1939:     my %defcell;
 1940:     foreach my $item (@usertools) {
 1941:         if ($context eq 'requestcourses') {
 1942:             my ($curroption,$currlimit);
 1943:             if (ref($settings) eq 'HASH') {
 1944:                 if (ref($settings->{$item}) eq 'HASH') {
 1945:                     $curroption = $settings->{$item}->{'default'};
 1946:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1947:                         $currlimit = $1;
 1948:                     }
 1949:                 }
 1950:             }
 1951:             if (!$curroption) {
 1952:                 $curroption = 'norequest';
 1953:             }
 1954:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1955:             foreach my $option (@options) {
 1956:                 my $val = $option;
 1957:                 if ($option eq 'norequest') {
 1958:                     $val = 0;
 1959:                 }
 1960:                 if ($option eq 'validate') {
 1961:                     my $canvalidate = 0;
 1962:                     if (ref($validations{$item}) eq 'HASH') {
 1963:                         if ($validations{$item}{'default'}) {
 1964:                             $canvalidate = 1;
 1965:                         }
 1966:                     }
 1967:                     next if (!$canvalidate);
 1968:                 }
 1969:                 my $checked = '';
 1970:                 if ($option eq $curroption) {
 1971:                     $checked = ' checked="checked"';
 1972:                 } elsif ($option eq 'autolimit') {
 1973:                     if ($curroption =~ /^autolimit/) {
 1974:                         $checked = ' checked="checked"';
 1975:                     }
 1976:                 }
 1977:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1978:                                   '<input type="radio" name="crsreq_'.$item.
 1979:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1980:                                   $titles{$option}.'</label>';
 1981:                 if ($option eq 'autolimit') {
 1982:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1983:                                        $item.'_limit_default" size="1" '.
 1984:                                        'value="'.$currlimit.'" />';
 1985:                 }
 1986:                 $defcell{$item} .= '</span> ';
 1987:                 if ($option eq 'autolimit') {
 1988:                     $defcell{$item} .= $titles{'unlimited'};
 1989:                 }
 1990:             }
 1991:         } elsif ($context eq 'requestauthor') {
 1992:             my $curroption;
 1993:             if (ref($settings) eq 'HASH') {
 1994:                 $curroption = $settings->{'default'};
 1995:             }
 1996:             if (!$curroption) {
 1997:                 $curroption = 'norequest';
 1998:             }
 1999:             foreach my $option (@options) {
 2000:                 my $val = $option;
 2001:                 if ($option eq 'norequest') {
 2002:                     $val = 0;
 2003:                 }
 2004:                 my $checked = '';
 2005:                 if ($option eq $curroption) {
 2006:                     $checked = ' checked="checked"';
 2007:                 }
 2008:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2009:                               '<input type="radio" name="authorreq_default"'.
 2010:                               ' value="'.$val.'"'.$checked.' />'.
 2011:                               $titles{$option}.'</label></span>&nbsp; ';
 2012:             }
 2013:         } else {
 2014:             my $checked = 'checked="checked" ';
 2015:             if (ref($settings) eq 'HASH') {
 2016:                 if (ref($settings->{$item}) eq 'HASH') {
 2017:                     if ($settings->{$item}->{'default'} == 0) {
 2018:                         $checked = '';
 2019:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2020:                         $checked = 'checked="checked" ';
 2021:                     }
 2022:                 }
 2023:             }
 2024:             $datatable .= '<span class="LC_nobreak"><label>'.
 2025:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2026:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2027:                           '</label></span>&nbsp; ';
 2028:         }
 2029:     }
 2030:     if ($context eq 'requestcourses') {
 2031:         $datatable .= '</tr><tr>';
 2032:         foreach my $item (@usertools) {
 2033:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2034:         }
 2035:         $datatable .= '</tr></table>';
 2036:     }
 2037:     $datatable .= '</td>';
 2038:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2039:         $datatable .= '<td class="LC_right_item">'.
 2040:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2041:                       '<input type="text" name="defaultquota" value="'.
 2042:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2043:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2044:                       '<input type="text" name="authorquota" value="'.
 2045:                       $authorquota.'" size="5" /></span></td>';
 2046:     }
 2047:     $datatable .= '</tr>';
 2048:     $typecount ++;
 2049:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2050:     $datatable .= '<tr'.$css_class.'>'.
 2051:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2052:     if ($context eq 'requestcourses') {
 2053:         $datatable .= &mt('(overrides affiliation, if set)').
 2054:                       '</td>'.
 2055:                       '<td class="LC_left_item">'.
 2056:                       '<table><tr>';
 2057:     } else {
 2058:         $datatable .= &mt('(overrides affiliation, if checked)').
 2059:                       '</td>'.
 2060:                       '<td class="LC_left_item" colspan="2">'.
 2061:                       '<br />';
 2062:     }
 2063:     my %advcell;
 2064:     foreach my $item (@usertools) {
 2065:         if ($context eq 'requestcourses') {
 2066:             my ($curroption,$currlimit);
 2067:             if (ref($settings) eq 'HASH') {
 2068:                 if (ref($settings->{$item}) eq 'HASH') {
 2069:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2070:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2071:                         $currlimit = $1;
 2072:                     }
 2073:                 }
 2074:             }
 2075:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2076:             my $checked = '';
 2077:             if ($curroption eq '') {
 2078:                 $checked = ' checked="checked"';
 2079:             }
 2080:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2081:                                '<input type="radio" name="crsreq_'.$item.
 2082:                                '__LC_adv" value=""'.$checked.' />'.
 2083:                                &mt('No override set').'</label></span>&nbsp; ';
 2084:             foreach my $option (@options) {
 2085:                 my $val = $option;
 2086:                 if ($option eq 'norequest') {
 2087:                     $val = 0;
 2088:                 }
 2089:                 if ($option eq 'validate') {
 2090:                     my $canvalidate = 0;
 2091:                     if (ref($validations{$item}) eq 'HASH') {
 2092:                         if ($validations{$item}{'_LC_adv'}) {
 2093:                             $canvalidate = 1;
 2094:                         }
 2095:                     }
 2096:                     next if (!$canvalidate);
 2097:                 }
 2098:                 my $checked = '';
 2099:                 if ($val eq $curroption) {
 2100:                     $checked = ' checked="checked"';
 2101:                 } elsif ($option eq 'autolimit') {
 2102:                     if ($curroption =~ /^autolimit/) {
 2103:                         $checked = ' checked="checked"';
 2104:                     }
 2105:                 }
 2106:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2107:                                   '<input type="radio" name="crsreq_'.$item.
 2108:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2109:                                   $titles{$option}.'</label>';
 2110:                 if ($option eq 'autolimit') {
 2111:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2112:                                        $item.'_limit__LC_adv" size="1" '.
 2113:                                        'value="'.$currlimit.'" />';
 2114:                 }
 2115:                 $advcell{$item} .= '</span> ';
 2116:                 if ($option eq 'autolimit') {
 2117:                     $advcell{$item} .= $titles{'unlimited'};
 2118:                 }
 2119:             }
 2120:         } elsif ($context eq 'requestauthor') {
 2121:             my $curroption;
 2122:             if (ref($settings) eq 'HASH') {
 2123:                 $curroption = $settings->{'_LC_adv'};
 2124:             }
 2125:             my $checked = '';
 2126:             if ($curroption eq '') {
 2127:                 $checked = ' checked="checked"';
 2128:             }
 2129:             $datatable .= '<span class="LC_nobreak"><label>'.
 2130:                           '<input type="radio" name="authorreq__LC_adv"'.
 2131:                           ' value=""'.$checked.' />'.
 2132:                           &mt('No override set').'</label></span>&nbsp; ';
 2133:             foreach my $option (@options) {
 2134:                 my $val = $option;
 2135:                 if ($option eq 'norequest') {
 2136:                     $val = 0;
 2137:                 }
 2138:                 my $checked = '';
 2139:                 if ($val eq $curroption) {
 2140:                     $checked = ' checked="checked"';
 2141:                 }
 2142:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2143:                               '<input type="radio" name="authorreq__LC_adv"'.
 2144:                               ' value="'.$val.'"'.$checked.' />'.
 2145:                               $titles{$option}.'</label></span>&nbsp; ';
 2146:             }
 2147:         } else {
 2148:             my $checked = 'checked="checked" ';
 2149:             if (ref($settings) eq 'HASH') {
 2150:                 if (ref($settings->{$item}) eq 'HASH') {
 2151:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2152:                         $checked = '';
 2153:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2154:                         $checked = 'checked="checked" ';
 2155:                     }
 2156:                 }
 2157:             }
 2158:             $datatable .= '<span class="LC_nobreak"><label>'.
 2159:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2160:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2161:                           '</label></span>&nbsp; ';
 2162:         }
 2163:     }
 2164:     if ($context eq 'requestcourses') {
 2165:         $datatable .= '</tr><tr>';
 2166:         foreach my $item (@usertools) {
 2167:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2168:         }
 2169:         $datatable .= '</tr></table>';
 2170:     }
 2171:     $datatable .= '</td></tr>';
 2172:     $$rowtotal += $typecount;
 2173:     return $datatable;
 2174: }
 2175: 
 2176: sub print_requestmail {
 2177:     my ($dom,$action,$settings,$rowtotal) = @_;
 2178:     my ($now,$datatable,%currapp);
 2179:     $now = time;
 2180:     if (ref($settings) eq 'HASH') {
 2181:         if (ref($settings->{'notify'}) eq 'HASH') {
 2182:             if ($settings->{'notify'}{'approval'} ne '') {
 2183:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2184:             }
 2185:         }
 2186:     }
 2187:     my $numinrow = 2;
 2188:     my $css_class;
 2189:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2190:     my $text;
 2191:     if ($action eq 'requestcourses') {
 2192:         $text = &mt('Receive notification of course requests requiring approval');
 2193:     } elsif ($action eq 'requestauthor') {
 2194:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2195:     } else {
 2196:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2197:     }
 2198:     $datatable = '<tr'.$css_class.'>'.
 2199:                  ' <td>'.$text.'</td>'.
 2200:                  ' <td class="LC_left_item">';
 2201:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2202:                                                  $action.'notifyapproval',%currapp);
 2203:     if ($numdc > 0) {
 2204:         $datatable .= $table;
 2205:     } else {
 2206:         $datatable .= &mt('There are no active Domain Coordinators');
 2207:     }
 2208:     $datatable .='</td></tr>';
 2209:     return $datatable;
 2210: }
 2211: 
 2212: sub print_studentcode {
 2213:     my ($settings,$rowtotal) = @_;
 2214:     my $rownum = 0; 
 2215:     my ($output,%current);
 2216:     my @crstypes = ('official','unofficial','community','textbook');
 2217:     if (ref($settings) eq 'HASH') {
 2218:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2219:             foreach my $type (@crstypes) {
 2220:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2221:             }
 2222:         }
 2223:     }
 2224:     $output .= '<tr>'.
 2225:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2226:                '<td class="LC_left_item">';
 2227:     foreach my $type (@crstypes) {
 2228:         my $check = ' ';
 2229:         if ($current{$type}) {
 2230:             $check = ' checked="checked" ';
 2231:         }
 2232:         $output .= '<span class="LC_nobreak"><label>'.
 2233:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2234:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2235:     }
 2236:     $output .= '</td></tr>';
 2237:     $$rowtotal ++;
 2238:     return $output;
 2239: }
 2240: 
 2241: sub print_textbookcourses {
 2242:     my ($dom,$type,$settings,$rowtotal) = @_;
 2243:     my $rownum = 0;
 2244:     my $css_class;
 2245:     my $itemcount = 1;
 2246:     my $maxnum = 0;
 2247:     my $bookshash;
 2248:     if (ref($settings) eq 'HASH') {
 2249:         $bookshash = $settings->{$type};
 2250:     }
 2251:     my %ordered;
 2252:     if (ref($bookshash) eq 'HASH') {
 2253:         foreach my $item (keys(%{$bookshash})) {
 2254:             if (ref($bookshash->{$item}) eq 'HASH') {
 2255:                 my $num = $bookshash->{$item}{'order'};
 2256:                 $ordered{$num} = $item;
 2257:             }
 2258:         }
 2259:     }
 2260:     my $confname = $dom.'-domainconfig';
 2261:     my $switchserver = &check_switchserver($dom,$confname);
 2262:     my $maxnum = scalar(keys(%ordered));
 2263:     my $datatable;
 2264:     if (keys(%ordered)) {
 2265:         my @items = sort { $a <=> $b } keys(%ordered);
 2266:         for (my $i=0; $i<@items; $i++) {
 2267:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2268:             my $key = $ordered{$items[$i]};
 2269:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2270:             my $coursetitle = $coursehash{'description'};
 2271:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2272:             if (ref($bookshash->{$key}) eq 'HASH') {
 2273:                 $subject = $bookshash->{$key}->{'subject'};
 2274:                 $title = $bookshash->{$key}->{'title'};
 2275:                 if ($type eq 'textbooks') {
 2276:                     $publisher = $bookshash->{$key}->{'publisher'};
 2277:                     $author = $bookshash->{$key}->{'author'};
 2278:                     $image = $bookshash->{$key}->{'image'};
 2279:                     if ($image ne '') {
 2280:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2281:                         my $imagethumb = "$path/tn-".$imagefile;
 2282:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2283:                     }
 2284:                 }
 2285:             }
 2286:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2287:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2288:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2289:             for (my $k=0; $k<=$maxnum; $k++) {
 2290:                 my $vpos = $k+1;
 2291:                 my $selstr;
 2292:                 if ($k == $i) {
 2293:                     $selstr = ' selected="selected" ';
 2294:                 }
 2295:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2296:             }
 2297:             $datatable .= '</select>'.('&nbsp;'x2).
 2298:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2299:                 &mt('Delete?').'</label></span></td>'.
 2300:                 '<td colspan="2">'.
 2301:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2302:                 ('&nbsp;'x2).
 2303:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2304:             if ($type eq 'textbooks') {
 2305:                 $datatable .= ('&nbsp;'x2).
 2306:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2307:                               ('&nbsp;'x2).
 2308:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2309:                               ('&nbsp;'x2).
 2310:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2311:                 if ($image) {
 2312:                     $datatable .= '<span class="LC_nobreak">'.
 2313:                                   $imgsrc.
 2314:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2315:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2316:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2317:                 }
 2318:                 if ($switchserver) {
 2319:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2320:                 } else {
 2321:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2322:                 }
 2323:             }
 2324:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2325:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2326:                           $coursetitle.'</span></td></tr>'."\n";
 2327:             $itemcount ++;
 2328:         }
 2329:     }
 2330:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2331:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2332:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2333:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2334:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2335:     for (my $k=0; $k<$maxnum+1; $k++) {
 2336:         my $vpos = $k+1;
 2337:         my $selstr;
 2338:         if ($k == $maxnum) {
 2339:             $selstr = ' selected="selected" ';
 2340:         }
 2341:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2342:     }
 2343:     $datatable .= '</select>&nbsp;'."\n".
 2344:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2345:                   '<td colspan="2">'.
 2346:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2347:                   ('&nbsp;'x2).
 2348:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2349:                   ('&nbsp;'x2);
 2350:     if ($type eq 'textbooks') {
 2351:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2352:                       ('&nbsp;'x2).
 2353:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2354:                       ('&nbsp;'x2).
 2355:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2356:         if ($switchserver) {
 2357:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2358:         } else {
 2359:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2360:         }
 2361:     }
 2362:     $datatable .= '</span>'."\n".
 2363:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2364:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2365:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2366:                   &Apache::loncommon::selectcourse_link
 2367:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2368:                   '</span></td>'."\n".
 2369:                   '</tr>'."\n";
 2370:     $itemcount ++;
 2371:     return $datatable;
 2372: }
 2373: 
 2374: sub textbookcourses_javascript {
 2375:     my ($settings) = @_;
 2376:     return unless(ref($settings) eq 'HASH');
 2377:     my (%ordered,%total,%jstext);
 2378:     foreach my $type ('textbooks','templates') {
 2379:         $total{$type} = 0;
 2380:         if (ref($settings->{$type}) eq 'HASH') {
 2381:             foreach my $item (keys(%{$settings->{$type}})) {
 2382:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2383:                     my $num = $settings->{$type}->{$item}{'order'};
 2384:                     $ordered{$type}{$num} = $item;
 2385:                 }
 2386:             }
 2387:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2388:         }
 2389:         my @jsarray = ();
 2390:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2391:             push(@jsarray,$ordered{$type}{$item});
 2392:         }
 2393:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2394:     }
 2395:     return <<"ENDSCRIPT";
 2396: <script type="text/javascript">
 2397: // <![CDATA[
 2398: function reorderBooks(form,item,caller) {
 2399:     var changedVal;
 2400: $jstext{'textbooks'};
 2401: $jstext{'templates'};
 2402:     var newpos;
 2403:     var maxh;
 2404:     if (caller == 'textbooks') {  
 2405:         newpos = 'textbooks_addbook_pos';
 2406:         maxh = 1 + $total{'textbooks'};
 2407:     } else {
 2408:         newpos = 'templates_addbook_pos';
 2409:         maxh = 1 + $total{'templates'};
 2410:     }
 2411:     var current = new Array;
 2412:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2413:     if (item == newpos) {
 2414:         changedVal = newitemVal;
 2415:     } else {
 2416:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2417:         current[newitemVal] = newpos;
 2418:     }
 2419:     if (caller == 'textbooks') {
 2420:         for (var i=0; i<textbooks.length; i++) {
 2421:             var elementName = 'textbooks_'+textbooks[i];
 2422:             if (elementName != item) {
 2423:                 if (form.elements[elementName]) {
 2424:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2425:                     current[currVal] = elementName;
 2426:                 }
 2427:             }
 2428:         }
 2429:     }
 2430:     if (caller == 'templates') {
 2431:         for (var i=0; i<templates.length; i++) {
 2432:             var elementName = 'templates_'+templates[i];
 2433:             if (elementName != item) {
 2434:                 if (form.elements[elementName]) {
 2435:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2436:                     current[currVal] = elementName;
 2437:                 }
 2438:             }
 2439:         }
 2440:     }
 2441:     var oldVal;
 2442:     for (var j=0; j<maxh; j++) {
 2443:         if (current[j] == undefined) {
 2444:             oldVal = j;
 2445:         }
 2446:     }
 2447:     if (oldVal < changedVal) {
 2448:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2449:            var elementName = current[k];
 2450:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2451:         }
 2452:     } else {
 2453:         for (var k=changedVal; k<oldVal; k++) {
 2454:             var elementName = current[k];
 2455:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2456:         }
 2457:     }
 2458:     return;
 2459: }
 2460: 
 2461: // ]]>
 2462: </script>
 2463: 
 2464: ENDSCRIPT
 2465: }
 2466: 
 2467: sub ltitools_javascript {
 2468:     my ($settings) = @_;
 2469:     my $togglejs = &ltitools_toggle_js();
 2470:     unless (ref($settings) eq 'HASH') {
 2471:         return $togglejs;
 2472:     }
 2473:     my (%ordered,$total,%jstext);
 2474:     $total = 0;
 2475:     foreach my $item (keys(%{$settings})) {
 2476:         if (ref($settings->{$item}) eq 'HASH') {
 2477:             my $num = $settings->{$item}{'order'};
 2478:             $ordered{$num} = $item;
 2479:         }
 2480:     }
 2481:     $total = scalar(keys(%{$settings}));
 2482:     my @jsarray = ();
 2483:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2484:         push(@jsarray,$ordered{$item});
 2485:     }
 2486:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2487:     return <<"ENDSCRIPT";
 2488: <script type="text/javascript">
 2489: // <![CDATA[
 2490: function reorderLTITools(form,item) {
 2491:     var changedVal;
 2492: $jstext
 2493:     var newpos = 'ltitools_add_pos';
 2494:     var maxh = 1 + $total;
 2495:     var current = new Array;
 2496:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2497:     if (item == newpos) {
 2498:         changedVal = newitemVal;
 2499:     } else {
 2500:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2501:         current[newitemVal] = newpos;
 2502:     }
 2503:     for (var i=0; i<ltitools.length; i++) {
 2504:         var elementName = 'ltitools_'+ltitools[i];
 2505:         if (elementName != item) {
 2506:             if (form.elements[elementName]) {
 2507:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2508:                 current[currVal] = elementName;
 2509:             }
 2510:         }
 2511:     }
 2512:     var oldVal;
 2513:     for (var j=0; j<maxh; j++) {
 2514:         if (current[j] == undefined) {
 2515:             oldVal = j;
 2516:         }
 2517:     }
 2518:     if (oldVal < changedVal) {
 2519:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2520:            var elementName = current[k];
 2521:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2522:         }
 2523:     } else {
 2524:         for (var k=changedVal; k<oldVal; k++) {
 2525:             var elementName = current[k];
 2526:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2527:         }
 2528:     }
 2529:     return;
 2530: }
 2531: 
 2532: // ]]>
 2533: </script>
 2534: 
 2535: $togglejs
 2536: 
 2537: ENDSCRIPT
 2538: }
 2539: 
 2540: sub ltitools_toggle_js {
 2541:     return <<"ENDSCRIPT";
 2542: <script type="text/javascript">
 2543: // <![CDATA[
 2544: 
 2545: function toggleLTITools(form,setting,item) {
 2546:     var radioname = '';
 2547:     var divid = '';
 2548:     if (setting == 'user') {
 2549:         divid = 'ltitools_'+setting+'_div_'+item;
 2550:         var checkid = 'ltitools_'+setting+'_field_'+item;
 2551:         if (document.getElementById(divid)) {
 2552:             if (document.getElementById(checkid)) {
 2553:                 if (document.getElementById(checkid).checked) {
 2554:                     document.getElementById(divid).style.display = 'inline-block';
 2555:                 } else {
 2556:                     document.getElementById(divid).style.display = 'none';
 2557:                 }
 2558:             }
 2559:         }
 2560:     }
 2561:     return;
 2562: }
 2563: // ]]>
 2564: </script>
 2565: 
 2566: ENDSCRIPT
 2567: }
 2568: 
 2569: sub print_autoenroll {
 2570:     my ($dom,$settings,$rowtotal) = @_;
 2571:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2572:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2573:     if (ref($settings) eq 'HASH') {
 2574:         if (exists($settings->{'run'})) {
 2575:             if ($settings->{'run'} eq '0') {
 2576:                 $runoff = ' checked="checked" ';
 2577:                 $runon = ' ';
 2578:             } else {
 2579:                 $runon = ' checked="checked" ';
 2580:                 $runoff = ' ';
 2581:             }
 2582:         } else {
 2583:             if ($autorun) {
 2584:                 $runon = ' checked="checked" ';
 2585:                 $runoff = ' ';
 2586:             } else {
 2587:                 $runoff = ' checked="checked" ';
 2588:                 $runon = ' ';
 2589:             }
 2590:         }
 2591:         if (exists($settings->{'co-owners'})) {
 2592:             if ($settings->{'co-owners'} eq '0') {
 2593:                 $coownersoff = ' checked="checked" ';
 2594:                 $coownerson = ' ';
 2595:             } else {
 2596:                 $coownerson = ' checked="checked" ';
 2597:                 $coownersoff = ' ';
 2598:             }
 2599:         } else {
 2600:             $coownersoff = ' checked="checked" ';
 2601:             $coownerson = ' ';
 2602:         }
 2603:         if (exists($settings->{'sender_domain'})) {
 2604:             $defdom = $settings->{'sender_domain'};
 2605:         }
 2606:         if (exists($settings->{'autofailsafe'})) {
 2607:             $failsafe = $settings->{'autofailsafe'};
 2608:         }
 2609:     } else {
 2610:         if ($autorun) {
 2611:             $runon = ' checked="checked" ';
 2612:             $runoff = ' ';
 2613:         } else {
 2614:             $runoff = ' checked="checked" ';
 2615:             $runon = ' ';
 2616:         }
 2617:     }
 2618:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2619:     my $notif_sender;
 2620:     if (ref($settings) eq 'HASH') {
 2621:         $notif_sender = $settings->{'sender_uname'};
 2622:     }
 2623:     my $datatable='<tr class="LC_odd_row">'.
 2624:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2625:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2626:                   '<input type="radio" name="autoenroll_run"'.
 2627:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2628:                   '<label><input type="radio" name="autoenroll_run"'.
 2629:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2630:                   '</tr><tr>'.
 2631:                   '<td>'.&mt('Notification messages - sender').
 2632:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2633:                   &mt('username').':&nbsp;'.
 2634:                   '<input type="text" name="sender_uname" value="'.
 2635:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2636:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2637:                   '<tr class="LC_odd_row">'.
 2638:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2639:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2640:                   '<input type="radio" name="autoassign_coowners"'.
 2641:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2642:                   '<label><input type="radio" name="autoassign_coowners"'.
 2643:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2644:                   '</tr><tr>'.
 2645:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2646:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2647:                   '<input type="text" name="autoenroll_failsafe"'.
 2648:                   ' value="'.$failsafe.'" size="4" /></td></tr>';
 2649:     $$rowtotal += 4;
 2650:     return $datatable;
 2651: }
 2652: 
 2653: sub print_autoupdate {
 2654:     my ($position,$dom,$settings,$rowtotal) = @_;
 2655:     my $datatable;
 2656:     if ($position eq 'top') {
 2657:         my $updateon = ' ';
 2658:         my $updateoff = ' checked="checked" ';
 2659:         my $classlistson = ' ';
 2660:         my $classlistsoff = ' checked="checked" ';
 2661:         if (ref($settings) eq 'HASH') {
 2662:             if ($settings->{'run'} eq '1') {
 2663:                 $updateon = $updateoff;
 2664:                 $updateoff = ' ';
 2665:             }
 2666:             if ($settings->{'classlists'} eq '1') {
 2667:                 $classlistson = $classlistsoff;
 2668:                 $classlistsoff = ' ';
 2669:             }
 2670:         }
 2671:         my %title = (
 2672:                    run => 'Auto-update active?',
 2673:                    classlists => 'Update information in classlists?',
 2674:                     );
 2675:         $datatable = '<tr class="LC_odd_row">'. 
 2676:                   '<td>'.&mt($title{'run'}).'</td>'.
 2677:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2678:                   '<input type="radio" name="autoupdate_run"'.
 2679:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2680:                   '<label><input type="radio" name="autoupdate_run"'.
 2681:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2682:                   '</tr><tr>'.
 2683:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2684:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2685:                   '<label><input type="radio" name="classlists"'.
 2686:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2687:                   '<label><input type="radio" name="classlists"'.
 2688:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2689:                   '</tr>';
 2690:         $$rowtotal += 2;
 2691:     } elsif ($position eq 'middle') {
 2692:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2693:         my $numinrow = 3;
 2694:         my $locknamesettings;
 2695:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2696:                                      $dom,$numinrow,$othertitle,
 2697:                                     'lockablenames');
 2698:         $$rowtotal ++;
 2699:     } else {
 2700:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2701:         my @fields = ('lastname','firstname','middlename','generation',
 2702:                       'permanentemail','id');
 2703:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2704:         my $numrows = 0;
 2705:         if (ref($types) eq 'ARRAY') {
 2706:             if (@{$types} > 0) {
 2707:                 $datatable = 
 2708:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2709:                                          \@fields,$types,\$numrows);
 2710:                     $$rowtotal += @{$types}; 
 2711:             }
 2712:         }
 2713:         $datatable .= 
 2714:             &usertype_update_row($settings,{'default' => $othertitle},
 2715:                                  \%fieldtitles,\@fields,['default'],
 2716:                                  \$numrows);
 2717:         $$rowtotal ++;     
 2718:     }
 2719:     return $datatable;
 2720: }
 2721: 
 2722: sub print_autocreate {
 2723:     my ($dom,$settings,$rowtotal) = @_;
 2724:     my (%createon,%createoff,%currhash);
 2725:     my @types = ('xml','req');
 2726:     if (ref($settings) eq 'HASH') {
 2727:         foreach my $item (@types) {
 2728:             $createoff{$item} = ' checked="checked" ';
 2729:             $createon{$item} = ' ';
 2730:             if (exists($settings->{$item})) {
 2731:                 if ($settings->{$item}) {
 2732:                     $createon{$item} = ' checked="checked" ';
 2733:                     $createoff{$item} = ' ';
 2734:                 }
 2735:             }
 2736:         }
 2737:         if ($settings->{'xmldc'} ne '') {
 2738:             $currhash{$settings->{'xmldc'}} = 1;
 2739:         }
 2740:     } else {
 2741:         foreach my $item (@types) {
 2742:             $createoff{$item} = ' checked="checked" ';
 2743:             $createon{$item} = ' ';
 2744:         }
 2745:     }
 2746:     $$rowtotal += 2;
 2747:     my $numinrow = 2;
 2748:     my $datatable='<tr class="LC_odd_row">'.
 2749:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2750:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2751:                   '<input type="radio" name="autocreate_xml"'.
 2752:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2753:                   '<label><input type="radio" name="autocreate_xml"'.
 2754:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2755:                   '</td></tr><tr>'.
 2756:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2757:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2758:                   '<input type="radio" name="autocreate_req"'.
 2759:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2760:                   '<label><input type="radio" name="autocreate_req"'.
 2761:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2762:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2763:                                                    'autocreate_xmldc',%currhash);
 2764:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2765:     if ($numdc > 1) {
 2766:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2767:                       '</td><td class="LC_left_item">';
 2768:     } else {
 2769:         $datatable .= &mt('Course creation processed as:').
 2770:                       '</td><td class="LC_right_item">';
 2771:     }
 2772:     $datatable .= $dctable.'</td></tr>';
 2773:     $$rowtotal += $rows;
 2774:     return $datatable;
 2775: }
 2776: 
 2777: sub print_directorysrch {
 2778:     my ($position,$dom,$settings,$rowtotal) = @_;
 2779:     my $datatable;
 2780:     if ($position eq 'top') {
 2781:         my $instsrchon = ' ';
 2782:         my $instsrchoff = ' checked="checked" ';
 2783:         my ($exacton,$containson,$beginson);
 2784:         my $instlocalon = ' ';
 2785:         my $instlocaloff = ' checked="checked" ';
 2786:         if (ref($settings) eq 'HASH') {
 2787:             if ($settings->{'available'} eq '1') {
 2788:                 $instsrchon = $instsrchoff;
 2789:                 $instsrchoff = ' ';
 2790:             }
 2791:             if ($settings->{'localonly'} eq '1') {
 2792:                 $instlocalon = $instlocaloff;
 2793:                 $instlocaloff = ' ';
 2794:             }
 2795:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2796:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 2797:                     if ($type eq 'exact') {
 2798:                         $exacton = ' checked="checked" ';
 2799:                     } elsif ($type eq 'contains') {
 2800:                         $containson = ' checked="checked" ';
 2801:                     } elsif ($type eq 'begins') {
 2802:                         $beginson = ' checked="checked" ';
 2803:                     }
 2804:                 }
 2805:             } else {
 2806:                 if ($settings->{'searchtypes'} eq 'exact') {
 2807:                     $exacton = ' checked="checked" ';
 2808:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 2809:                     $containson = ' checked="checked" ';
 2810:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 2811:                     $exacton = ' checked="checked" ';
 2812:                     $containson = ' checked="checked" ';
 2813:                 }
 2814:             }
 2815:         }
 2816:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2817:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2818: 
 2819:         my $numinrow = 4;
 2820:         my $cansrchrow = 0;
 2821:         $datatable='<tr class="LC_odd_row">'.
 2822:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 2823:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2824:                    '<input type="radio" name="dirsrch_available"'.
 2825:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2826:                    '<label><input type="radio" name="dirsrch_available"'.
 2827:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2828:                    '</tr><tr>'.
 2829:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 2830:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2831:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 2832:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2833:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 2834:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2835:                    '</tr>';
 2836:         $$rowtotal += 2;
 2837:         if (ref($usertypes) eq 'HASH') {
 2838:             if (keys(%{$usertypes}) > 0) {
 2839:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2840:                                              $numinrow,$othertitle,'cansearch');
 2841:                 $cansrchrow = 1;
 2842:             }
 2843:         }
 2844:         if ($cansrchrow) {
 2845:             $$rowtotal ++;
 2846:             $datatable .= '<tr>';
 2847:         } else {
 2848:             $datatable .= '<tr class="LC_odd_row">';
 2849:         }
 2850:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2851:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2852:         foreach my $title (@{$titleorder}) {
 2853:             if (defined($searchtitles->{$title})) {
 2854:                 my $check = ' ';
 2855:                 if (ref($settings) eq 'HASH') {
 2856:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2857:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2858:                             $check = ' checked="checked" ';
 2859:                         }
 2860:                     }
 2861:                 }
 2862:                 $datatable .= '<td class="LC_left_item">'.
 2863:                               '<span class="LC_nobreak"><label>'.
 2864:                               '<input type="checkbox" name="searchby" '.
 2865:                               'value="'.$title.'"'.$check.'/>'.
 2866:                               $searchtitles->{$title}.'</label></span></td>';
 2867:             }
 2868:         }
 2869:         $datatable .= '</tr></table></td></tr>';
 2870:         $$rowtotal ++;
 2871:         if ($cansrchrow) {
 2872:             $datatable .= '<tr class="LC_odd_row">';
 2873:         } else {
 2874:             $datatable .= '<tr>';
 2875:         }
 2876:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2877:                       '<td class="LC_left_item" colspan="2">'.
 2878:                       '<span class="LC_nobreak"><label>'.
 2879:                       '<input type="checkbox" name="searchtypes" '.
 2880:                       $exacton.' value="exact" />'.&mt('Exact match').
 2881:                       '</label>&nbsp;'.
 2882:                       '<label><input type="checkbox" name="searchtypes" '.
 2883:                       $beginson.' value="begins" />'.&mt('Begins with').
 2884:                       '</label>&nbsp;'.
 2885:                       '<label><input type="checkbox" name="searchtypes" '.
 2886:                       $containson.' value="contains" />'.&mt('Contains').
 2887:                       '</label></span></td></tr>';
 2888:         $$rowtotal ++;
 2889:     } else {
 2890:         my $domsrchon = ' checked="checked" ';
 2891:         my $domsrchoff = ' ';
 2892:         my $domlocalon = ' ';
 2893:         my $domlocaloff = ' checked="checked" ';
 2894:         if (ref($settings) eq 'HASH') {
 2895:             if ($settings->{'lclocalonly'} eq '1') {
 2896:                 $domlocalon = $domlocaloff;
 2897:                 $domlocaloff = ' ';
 2898:             }
 2899:             if ($settings->{'lcavailable'} eq '0') {
 2900:                 $domsrchoff = $domsrchon;
 2901:                 $domsrchon = ' ';
 2902:             }
 2903:         }
 2904:         $datatable='<tr class="LC_odd_row">'.
 2905:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 2906:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2907:                       '<input type="radio" name="dirsrch_domavailable"'.
 2908:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2909:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 2910:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2911:                       '</tr><tr>'.
 2912:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 2913:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2914:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 2915:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2916:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 2917:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2918:                       '</tr>';
 2919:         $$rowtotal += 2;
 2920:     }
 2921:     return $datatable;
 2922: }
 2923: 
 2924: sub print_contacts {
 2925:     my ($position,$dom,$settings,$rowtotal) = @_;
 2926:     my $datatable;
 2927:     my @contacts = ('adminemail','supportemail');
 2928:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 2929:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings);
 2930:     if ($position eq 'top') {
 2931:         if (ref($settings) eq 'HASH') {
 2932:             foreach my $item (@contacts) {
 2933:                 if (exists($settings->{$item})) {
 2934:                     $to{$item} = $settings->{$item};
 2935:                 }
 2936:             }
 2937:         }
 2938:     } elsif ($position eq 'middle') {
 2939:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 2940:                      'updatesmail','idconflictsmail');
 2941:         foreach my $type (@mailings) {
 2942:             $otheremails{$type} = '';
 2943:         }
 2944:     } else {
 2945:         @mailings = ('helpdeskmail','otherdomsmail');
 2946:         foreach my $type (@mailings) {
 2947:             $otheremails{$type} = '';
 2948:         }
 2949:         $bccemails{'helpdeskmail'} = '';
 2950:         $bccemails{'otherdomsmail'} = '';
 2951:         $includestr{'helpdeskmail'} = '';
 2952:         $includestr{'otherdomsmail'} = '';
 2953:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 2954:     }
 2955:     if (ref($settings) eq 'HASH') {
 2956:         unless ($position eq 'top') {
 2957:             foreach my $type (@mailings) {
 2958:                 if (exists($settings->{$type})) {
 2959:                     if (ref($settings->{$type}) eq 'HASH') {
 2960:                         foreach my $item (@contacts) {
 2961:                             if ($settings->{$type}{$item}) {
 2962:                                 $checked{$type}{$item} = ' checked="checked" ';
 2963:                             }
 2964:                         }
 2965:                         $otheremails{$type} = $settings->{$type}{'others'};
 2966:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 2967:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 2968:                             if ($settings->{$type}{'include'} ne '') {
 2969:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 2970:                                 $includestr{$type} = &unescape($includestr{$type});
 2971:                             }
 2972:                         }
 2973:                     }
 2974:                 } elsif ($type eq 'lonstatusmail') {
 2975:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2976:                 }
 2977:             }
 2978:         }
 2979:         if ($position eq 'bottom') {
 2980:             foreach my $type (@mailings) {
 2981:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 2982:                 if ($settings->{$type}{'include'} ne '') {
 2983:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 2984:                     $includestr{$type} = &unescape($includestr{$type});
 2985:                 }
 2986:             }
 2987:             if (ref($settings->{'helpform'}) eq 'HASH') {
 2988:                 if (ref($fields) eq 'ARRAY') {
 2989:                     foreach my $field (@{$fields}) {
 2990:                         $currfield{$field} = $settings->{'helpform'}{$field};
 2991:                     }
 2992:                 }
 2993:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 2994:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 2995:                 } else {
 2996:                     $maxsize = '1.0';
 2997:                 }
 2998:             } else {
 2999:                 if (ref($fields) eq 'ARRAY') {
 3000:                     foreach my $field (@{$fields}) {
 3001:                         $currfield{$field} = 'yes';
 3002:                     }
 3003:                 }
 3004:                 $maxsize = '1.0';
 3005:             }
 3006:         }
 3007:     } else {
 3008:         if ($position eq 'top') {
 3009:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 3010:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 3011:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 3012:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 3013:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3014:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 3015:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 3016:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 3017:         } elsif ($position eq 'bottom') {
 3018:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 3019:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 3020:             if (ref($fields) eq 'ARRAY') {
 3021:                 foreach my $field (@{$fields}) {
 3022:                     $currfield{$field} = 'yes';
 3023:                 }
 3024:             }
 3025:             $maxsize = '1.0';
 3026:         }
 3027:     }
 3028:     my ($titles,$short_titles) = &contact_titles();
 3029:     my $rownum = 0;
 3030:     my $css_class;
 3031:     if ($position eq 'top') {
 3032:         foreach my $item (@contacts) {
 3033:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3034:             $datatable .= '<tr'.$css_class.'>'. 
 3035:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3036:                           '</span></td><td class="LC_right_item">'.
 3037:                           '<input type="text" name="'.$item.'" value="'.
 3038:                           $to{$item}.'" /></td></tr>';
 3039:             $rownum ++;
 3040:         }
 3041:     } else {
 3042:         foreach my $type (@mailings) {
 3043:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3044:             $datatable .= '<tr'.$css_class.'>'.
 3045:                           '<td><span class="LC_nobreak">'.
 3046:                           $titles->{$type}.': </span></td>'.
 3047:                           '<td class="LC_left_item">';
 3048:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3049:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3050:             }
 3051:             $datatable .= '<span class="LC_nobreak">';
 3052:             foreach my $item (@contacts) {
 3053:                 $datatable .= '<label>'.
 3054:                               '<input type="checkbox" name="'.$type.'"'.
 3055:                               $checked{$type}{$item}.
 3056:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3057:                               '</label>&nbsp;';
 3058:             }
 3059:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3060:                           '<input type="text" name="'.$type.'_others" '.
 3061:                           'value="'.$otheremails{$type}.'"  />';
 3062:             my %locchecked;
 3063:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3064:                 foreach my $loc ('s','b') {
 3065:                     if ($includeloc{$type} eq $loc) {
 3066:                         $locchecked{$loc} = ' checked="checked"';
 3067:                         last;
 3068:                     }
 3069:                 }
 3070:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3071:                               '<input type="text" name="'.$type.'_bcc" '.
 3072:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3073:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3074:                               &mt('Text automatically added to e-mail:').' '.
 3075:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br >'.
 3076:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3077:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3078:                               ('&nbsp;'x2).
 3079:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3080:                               '</span></fieldset>';
 3081:             }
 3082:             $datatable .= '</td></tr>'."\n";
 3083:             $rownum ++;
 3084:         }
 3085:     }
 3086:     if ($position eq 'middle') {
 3087:         my %choices;
 3088:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 3089:                                        &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3090:                                        &mt('LON-CAPA core group - MSU'),600,500));
 3091:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3092:                                         &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3093:                                         &mt('LON-CAPA core group - MSU'),600,500));
 3094:         my @toggles = ('reporterrors','reportupdates');
 3095:         my %defaultchecked = ('reporterrors'  => 'on',
 3096:                               'reportupdates' => 'on');
 3097:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3098:                                                    \%choices,$rownum);
 3099:         $datatable .= $reports;
 3100:     } elsif ($position eq 'bottom') {
 3101:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3102:         $datatable .= '<tr'.$css_class.'>'.
 3103:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3104:                       &mt('(e-mail, subject, and description always shown)').
 3105:                       '</td><td class="LC_left_item">';
 3106:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3107:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3108:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3109:             foreach my $field (@{$fields}) {
 3110:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3111:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3112:                     $datatable .= ' '.&mt('(logged-in users)');
 3113:                 }
 3114:                 $datatable .='</td><td>';
 3115:                 my $clickaction;
 3116:                 if ($field eq 'screenshot') {
 3117:                     $clickaction = ' onclick="screenshotSize(this);"';
 3118:                 }
 3119:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3120:                     foreach my $option (@{$possoptions->{$field}}) {
 3121:                         my $checked;
 3122:                         if ($currfield{$field} eq $option) {
 3123:                             $checked = ' checked="checked"';
 3124:                         }
 3125:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3126:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3127:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3128:                                       '</label></span>'.('&nbsp;'x2);
 3129:                     }
 3130:                 }
 3131:                 if ($field eq 'screenshot') {
 3132:                     my $display;
 3133:                     if ($currfield{$field} eq 'no') {
 3134:                         $display = ' style="display:none"';
 3135:                     }
 3136:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.' />'.
 3137:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3138:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3139:                 }
 3140:                 $datatable .= '</td></tr>';
 3141:             }
 3142:             $datatable .= '</table>';
 3143:         }
 3144:         $datatable .= '</td></tr>'."\n";
 3145:         $rownum ++;
 3146:     }
 3147:     $$rowtotal += $rownum;
 3148:     return $datatable;
 3149: }
 3150: 
 3151: sub contacts_javascript {
 3152:     return <<"ENDSCRIPT";
 3153: 
 3154: <script type="text/javascript">
 3155: // <![CDATA[
 3156: 
 3157: function screenshotSize(field) {
 3158:     if (document.getElementById('help_screenshotsize')) {
 3159:         if (field.value == 'no') {
 3160:             document.getElementById('help_screenshotsize').style.display="none";
 3161:         } else {
 3162:             document.getElementById('help_screenshotsize').style.display="";
 3163:         }
 3164:     }
 3165:     return;
 3166: }
 3167: 
 3168: // ]]>
 3169: </script>
 3170: 
 3171: ENDSCRIPT
 3172: }
 3173: 
 3174: sub print_helpsettings {
 3175:     my ($position,$dom,$settings,$rowtotal) = @_;
 3176:     my $confname = $dom.'-domainconfig';
 3177:     my $formname = 'display';
 3178:     my ($datatable,$itemcount);
 3179:     if ($position eq 'top') {
 3180:         $itemcount = 1;
 3181:         my (%choices,%defaultchecked,@toggles);
 3182:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3183:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3184:                                      &mt('LON-CAPA bug tracker'),600,500));
 3185:         %defaultchecked = ('submitbugs' => 'on');
 3186:         @toggles = ('submitbugs');
 3187:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3188:                                                      \%choices,$itemcount);
 3189:         $$rowtotal ++;
 3190:     } else {
 3191:         my $css_class;
 3192:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3193:         my (%customroles,%ordered,%current);
 3194:         if (ref($settings) eq 'HASH') {
 3195:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 3196:                 %current = %{$settings->{'adhoc'}};
 3197:             }
 3198:         }
 3199:         my $count = 0;
 3200:         foreach my $key (sort(keys(%existing))) {
 3201:             if ($key=~/^rolesdef\_(\w+)$/) {
 3202:                 my $rolename = $1;
 3203:                 my (%privs,$order);
 3204:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3205:                 $customroles{$rolename} = \%privs;
 3206:                 if (ref($current{$rolename}) eq 'HASH') {
 3207:                     $order = $current{$rolename}{'order'};
 3208:                 }
 3209:                 if ($order eq '') {
 3210:                     $order = $count;
 3211:                 }
 3212:                 $ordered{$order} = $rolename;
 3213:                 $count++;
 3214:             }
 3215:         }
 3216:         my $maxnum = scalar(keys(%ordered));
 3217:         my @roles_by_num = ();
 3218:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3219:             push(@roles_by_num,$item);
 3220:         }
 3221:         my $context = 'domprefs';
 3222:         my $crstype = 'Course';
 3223:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3224:         my @accesstypes = ('all','dh','da','none');
 3225:         my ($numstatustypes,@jsarray);
 3226:         if (ref($types) eq 'ARRAY') {
 3227:             if (@{$types} > 0) {
 3228:                 $numstatustypes = scalar(@{$types});
 3229:                 push(@accesstypes,'status');
 3230:                 @jsarray = ('bystatus');
 3231:             }
 3232:         }
 3233:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3234:         if (keys(%domhelpdesk)) {
 3235:             push(@accesstypes,('inc','exc'));
 3236:             push(@jsarray,('notinc','notexc'));
 3237:         }
 3238:         my $hiddenstr = join("','",@jsarray);
 3239:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3240:         my $context = 'domprefs';
 3241:         my $crstype = 'Course';
 3242:         my $prefix = 'helproles_';
 3243:         my $add_class = 'LC_hidden';
 3244:         foreach my $num (@roles_by_num) {
 3245:             my $role = $ordered{$num};
 3246:             my ($desc,$access,@statuses);
 3247:             if (ref($current{$role}) eq 'HASH') {
 3248:                 $desc = $current{$role}{'desc'};
 3249:                 $access = $current{$role}{'access'};
 3250:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3251:                     @statuses = @{$current{$role}{'insttypes'}};
 3252:                 }
 3253:             }
 3254:             if ($desc eq '') {
 3255:                 $desc = $role;
 3256:             }
 3257:             my $identifier = 'custhelp'.$num;
 3258:             my %full=();
 3259:             my %levels= (
 3260:                          course => {},
 3261:                          domain => {},
 3262:                          system => {},
 3263:                         );
 3264:             my %levelscurrent=(
 3265:                                course => {},
 3266:                                domain => {},
 3267:                                system => {},
 3268:                               );
 3269:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3270:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3271:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3272:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3273:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 3274:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3275:             for (my $k=0; $k<=$maxnum; $k++) {
 3276:                 my $vpos = $k+1;
 3277:                 my $selstr;
 3278:                 if ($k == $num) {
 3279:                     $selstr = ' selected="selected" ';
 3280:                 }
 3281:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3282:             }
 3283:             $datatable .= '</select>'.('&nbsp;'x2).
 3284:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3285:                           '</td>'.
 3286:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3287:                           &mt('Name shown to users:').
 3288:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3289:                           '</fieldset>'.
 3290:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3291:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3292:                           '<fieldset>'.
 3293:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3294:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3295:                                                                    \%levelscurrent,$identifier,
 3296:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3297:                           '</fieldset></td>';
 3298:             $itemcount ++;
 3299:         }
 3300:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3301:         my $newcust = 'custhelp'.$count;
 3302:         my (%privs,%levelscurrent);
 3303:         my %full=();
 3304:         my %levels= (
 3305:                      course => {},
 3306:                      domain => {},
 3307:                      system => {},
 3308:                     );
 3309:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3310:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3311:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3312:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 3313:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3314:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3315:         for (my $k=0; $k<$maxnum+1; $k++) {
 3316:             my $vpos = $k+1;
 3317:             my $selstr;
 3318:             if ($k == $maxnum) {
 3319:                 $selstr = ' selected="selected" ';
 3320:             }
 3321:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3322:         }
 3323:         $datatable .= '</select>&nbsp;'."\n".
 3324:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3325:                       '</label></span></td>'.
 3326:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3327:                       '<span class="LC_nobreak">'.
 3328:                       &mt('Internal name:').
 3329:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3330:                       '</span>'.('&nbsp;'x4).
 3331:                       '<span class="LC_nobreak">'.
 3332:                       &mt('Name shown to users:').
 3333:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3334:                       '</span></fieldset>'.
 3335:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3336:                                              $usertypes,$types,\%domhelpdesk).
 3337:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3338:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3339:                                                                 \@templateroles,$newcust).
 3340:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3341:                                                                \%levelscurrent,$newcust).
 3342:                       '</fieldset></td></tr>';
 3343:         $count ++;
 3344:         $$rowtotal += $count;
 3345:     }
 3346:     return $datatable;
 3347: }
 3348: 
 3349: sub adhocbutton {
 3350:     my ($prefix,$num,$field,$visibility) = @_;
 3351:     my %lt = &Apache::lonlocal::texthash(
 3352:                                           show => 'Show details',
 3353:                                           hide => 'Hide details',
 3354:                                         );
 3355:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3356:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3357:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3358:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3359: }
 3360: 
 3361: sub helpsettings_javascript {
 3362:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3363:     return unless(ref($roles_by_num) eq 'ARRAY');
 3364:     my %html_js_lt = &Apache::lonlocal::texthash(
 3365:                                           show => 'Show details',
 3366:                                           hide => 'Hide details',
 3367:                                         );
 3368:     &html_escape(\%html_js_lt);
 3369:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3370:     return <<"ENDSCRIPT";
 3371: <script type="text/javascript">
 3372: // <![CDATA[
 3373: 
 3374: function reorderHelpRoles(form,item) {
 3375:     var changedVal;
 3376: $jstext
 3377:     var newpos = 'helproles_${total}_pos';
 3378:     var maxh = 1 + $total;
 3379:     var current = new Array();
 3380:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3381:     if (item == newpos) {
 3382:         changedVal = newitemVal;
 3383:     } else {
 3384:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3385:         current[newitemVal] = newpos;
 3386:     }
 3387:     for (var i=0; i<helproles.length; i++) {
 3388:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3389:         if (elementName != item) {
 3390:             if (form.elements[elementName]) {
 3391:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3392:                 current[currVal] = elementName;
 3393:             }
 3394:         }
 3395:     }
 3396:     var oldVal;
 3397:     for (var j=0; j<maxh; j++) {
 3398:         if (current[j] == undefined) {
 3399:             oldVal = j;
 3400:         }
 3401:     }
 3402:     if (oldVal < changedVal) {
 3403:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3404:            var elementName = current[k];
 3405:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3406:         }
 3407:     } else {
 3408:         for (var k=changedVal; k<oldVal; k++) {
 3409:             var elementName = current[k];
 3410:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3411:         }
 3412:     }
 3413:     return;
 3414: }
 3415: 
 3416: function helpdeskAccess(num) {
 3417:     var curraccess = null;
 3418:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3419:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3420:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3421:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3422:             }
 3423:         }
 3424:     }
 3425:     var shown = Array();
 3426:     var hidden = Array();
 3427:     if (curraccess == 'none') {
 3428:         hidden = Array('$hiddenstr');
 3429:     } else {
 3430:         if (curraccess == 'status') {
 3431:             shown = Array('bystatus');
 3432:             hidden = Array('notinc','notexc');
 3433:         } else {
 3434:             if (curraccess == 'exc') {
 3435:                 shown = Array('notexc');
 3436:                 hidden = Array('notinc','bystatus');
 3437:             }
 3438:             if (curraccess == 'inc') {
 3439:                 shown = Array('notinc');
 3440:                 hidden = Array('notexc','bystatus');
 3441:             }
 3442:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 3443:                 hidden = Array('notinc','notexc','bystatus');
 3444:             }
 3445:         }
 3446:     }
 3447:     if (hidden.length > 0) {
 3448:         for (var i=0; i<hidden.length; i++) {
 3449:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3450:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3451:             }
 3452:         }
 3453:     }
 3454:     if (shown.length > 0) {
 3455:         for (var i=0; i<shown.length; i++) {
 3456:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3457:                 if (shown[i] == 'privs') {
 3458:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3459:                 } else {
 3460:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3461:                 }
 3462:             }
 3463:         }
 3464:     }
 3465:     return;
 3466: }
 3467: 
 3468: function toggleHelpdeskItem(num,field) {
 3469:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3470:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3471:             document.getElementById('helproles_'+num+'_'+field).className =
 3472:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3473:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3474:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3475:             }
 3476:         } else {
 3477:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3478:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3479:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3480:             }
 3481:         }
 3482:     }
 3483:     return;
 3484: }
 3485: 
 3486: // ]]>
 3487: </script>
 3488: 
 3489: ENDSCRIPT
 3490: }
 3491: 
 3492: sub helpdeskroles_access {
 3493:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3494:         $usertypes,$types,$domhelpdesk) = @_;
 3495:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3496:     my %lt = &Apache::lonlocal::texthash(
 3497:                     'rou'    => 'Role usage',
 3498:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3499:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 3500:                     'dh'     => 'All with domain helpdesk role',
 3501:                     'da'     => 'All with domain helpdesk assistant role',
 3502:                     'none'   => 'None',
 3503:                     'status' => 'Determined based on institutional status',
 3504:                     'inc'    => 'Include all, but exclude specific personnel',
 3505:                     'exc'    => 'Exclude all, but include specific personnel',
 3506:                   );
 3507:     my %usecheck = (
 3508:                      all => ' checked="checked"',
 3509:                    );
 3510:     my %displaydiv = (
 3511:                       status => 'none',
 3512:                       inc    => 'none',
 3513:                       exc    => 'none',
 3514:                       priv   => 'block',
 3515:                      );
 3516:     my $output;
 3517:     if (ref($current) eq 'HASH') {
 3518:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3519:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3520:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3521:                 delete($usecheck{'all'});
 3522:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3523:                     my $access = $1;
 3524:                     $displaydiv{$access} = 'inline';
 3525:                 } elsif ($current->{access} eq 'none') {
 3526:                     $displaydiv{'priv'} = 'none';
 3527:                 }
 3528:             }
 3529:         }
 3530:     }
 3531:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3532:               '<p>'.$lt{'whi'}.'</p>';
 3533:     foreach my $access (@{$accesstypes}) {
 3534:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3535:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3536:                    $lt{$access}.'</label>';
 3537:         if ($access eq 'status') {
 3538:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3539:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3540:                                                                  $othertitle,$usertypes,$types).
 3541:                        '</div>';
 3542:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3543:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3544:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3545:                        '</div>';
 3546:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3547:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3548:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3549:                        '</div>';
 3550:         }
 3551:         $output .= '</p>';
 3552:     }
 3553:     $output .= '</fieldset>';
 3554:     return $output;
 3555: }
 3556: 
 3557: sub radiobutton_prefs {
 3558:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3559:         $additional,$align) = @_;
 3560:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3561:                    (ref($choices) eq 'HASH'));
 3562: 
 3563:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3564: 
 3565:     foreach my $item (@{$toggles}) {
 3566:         if ($defaultchecked->{$item} eq 'on') {
 3567:             $checkedon{$item} = ' checked="checked" ';
 3568:             $checkedoff{$item} = ' ';
 3569:         } elsif ($defaultchecked->{$item} eq 'off') {
 3570:             $checkedoff{$item} = ' checked="checked" ';
 3571:             $checkedon{$item} = ' ';
 3572:         }
 3573:     }
 3574:     if (ref($settings) eq 'HASH') {
 3575:         foreach my $item (@{$toggles}) {
 3576:             if ($settings->{$item} eq '1') {
 3577:                 $checkedon{$item} =  ' checked="checked" ';
 3578:                 $checkedoff{$item} = ' ';
 3579:             } elsif ($settings->{$item} eq '0') {
 3580:                 $checkedoff{$item} =  ' checked="checked" ';
 3581:                 $checkedon{$item} = ' ';
 3582:             }
 3583:         }
 3584:     }
 3585:     if ($onclick) {
 3586:         $onclick = ' onclick="'.$onclick.'"';
 3587:     }
 3588:     foreach my $item (@{$toggles}) {
 3589:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3590:         $datatable .=
 3591:             '<tr'.$css_class.'><td valign="top">'.
 3592:             '<span class="LC_nobreak">'.$choices->{$item}.
 3593:             '</span></td>';
 3594:         if ($align eq 'left') {
 3595:             $datatable .= '<td class="LC_left_item">';
 3596:         } else {
 3597:             $datatable .= '<td class="LC_right_item">';
 3598:         }
 3599:         $datatable .=
 3600:             '<span class="LC_nobreak">'.
 3601:             '<label><input type="radio" name="'.
 3602:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3603:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3604:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3605:             '</span>'.$additional.
 3606:             '</td>'.
 3607:             '</tr>';
 3608:         $itemcount ++;
 3609:     }
 3610:     return ($datatable,$itemcount);
 3611: }
 3612: 
 3613: sub print_ltitools {
 3614:     my ($dom,$settings,$rowtotal) = @_;
 3615:     my $rownum = 0;
 3616:     my $css_class;
 3617:     my $itemcount = 1;
 3618:     my $maxnum = 0;
 3619:     my %ordered;
 3620:     if (ref($settings) eq 'HASH') {
 3621:         foreach my $item (keys(%{$settings})) {
 3622:             if (ref($settings->{$item}) eq 'HASH') {
 3623:                 my $num = $settings->{$item}{'order'};
 3624:                 $ordered{$num} = $item;
 3625:             }
 3626:         }
 3627:     }
 3628:     my $confname = $dom.'-domainconfig';
 3629:     my $switchserver = &check_switchserver($dom,$confname);
 3630:     my $maxnum = scalar(keys(%ordered));
 3631:     my $datatable;
 3632:     my %lt = &ltitools_names();
 3633:     my @courseroles = ('cc','in','ta','ep','st');
 3634:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 3635:     my @fields = ('fullname','firstname','lastname','email','roles','user');
 3636:     if (keys(%ordered)) {
 3637:         my @items = sort { $a <=> $b } keys(%ordered);
 3638:         for (my $i=0; $i<@items; $i++) {
 3639:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3640:             my $item = $ordered{$items[$i]};
 3641:             my ($title,$key,$secret,$url,$lifetime,$imgsrc,%sigsel);
 3642:             if (ref($settings->{$item}) eq 'HASH') {
 3643:                 $title = $settings->{$item}->{'title'};
 3644:                 $url = $settings->{$item}->{'url'};
 3645:                 $key = $settings->{$item}->{'key'};
 3646:                 $secret = $settings->{$item}->{'secret'};
 3647:                 $lifetime = $settings->{$item}->{'lifetime'};
 3648:                 my $image = $settings->{$item}->{'image'};
 3649:                 if ($image ne '') {
 3650:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 3651:                 }
 3652:                 if ($settings->{$item}->{'sigmethod'} eq 'HMAC-256') {
 3653:                     $sigsel{'HMAC-256'} = ' selected="selected"';
 3654:                 } else {
 3655:                     $sigsel{'HMAC-SHA1'} = ' selected="selected"';
 3656:                 }
 3657:             }
 3658:             my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_".$item."'".');"';
 3659:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3660:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 3661:             for (my $k=0; $k<=$maxnum; $k++) {
 3662:                 my $vpos = $k+1;
 3663:                 my $selstr;
 3664:                 if ($k == $i) {
 3665:                     $selstr = ' selected="selected" ';
 3666:                 }
 3667:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3668:             }
 3669:             $datatable .= '</select>'.('&nbsp;'x2).
 3670:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 3671:                 &mt('Delete?').'</label></span></td>'.
 3672:                 '<td colspan="2">'.
 3673:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3674:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 3675:                 ('&nbsp;'x2).
 3676:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 3677:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 3678:                 ('&nbsp;'x2).
 3679:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 3680:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3681:                 ('&nbsp;'x2).
 3682:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_sigmethod_'.$i.'">'.
 3683:                 '<option value="HMAC-SHA1"'.$sigsel{'HMAC-SHA1'}.'>HMAC-SHA1</option>'.
 3684:                 '<option value="HMAC-SHA256"'.$sigsel{'HMAC-SHA256'}.'>HMAC-SHA256</option></select></span>'.
 3685:                 '<br /><br />'.
 3686:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_url_'.$i.'"'.
 3687:                 ' value="'.$url.'" /></span>'.
 3688:                 ('&nbsp;'x2).
 3689:                 '<span class="LC_nobreak">'.$lt{'key'}.':'.
 3690:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 3691:                 ('&nbsp;'x2).
 3692:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':'.
 3693:                 '<input type="text" size="5" name="ltitools_lifetime_'.$i.'" value="'.$lifetime.'" /></span> '.
 3694:                 ('&nbsp;'x2).
 3695:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 3696:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 3697:                 '<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>'.
 3698:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 3699:                 '</fieldset>'.
 3700:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3701:                 '<span class="LC_nobreak">'.&mt('Display target:');
 3702:             my %currdisp;
 3703:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 3704:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 3705:                     $currdisp{'window'} = ' checked="checked"';
 3706:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 3707:                     $currdisp{'tab'} = ' checked="checked"';
 3708:                 } else {
 3709:                     $currdisp{'iframe'} = ' checked="checked"';
 3710:                 }
 3711:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 3712:                     $currdisp{'width'} = $1;
 3713:                 }
 3714:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 3715:                      $currdisp{'height'} = $1;
 3716:                 }
 3717:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 3718:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 3719:             } else {
 3720:                 $currdisp{'iframe'} = ' checked="checked"';
 3721:             }
 3722:             foreach my $disp ('iframe','tab','window') {
 3723:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 3724:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 3725:             }
 3726:             $datatable .= ('&nbsp;'x4);
 3727:             foreach my $dimen ('width','height') {
 3728:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3729:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 3730:                               ('&nbsp;'x2);
 3731:             }
 3732:             $datatable .= '</span><br />'.
 3733:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 3734:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></div>'.
 3735:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 3736:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 3737:                           '</textarea></div><div style=""></div>'.
 3738:                           '<div style="padding:0;clear:both;margin:0;border:0"></div>';
 3739:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 3740:             if ($imgsrc) {
 3741:                 $datatable .= $imgsrc.
 3742:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 3743:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 3744:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 3745:             } else {
 3746:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3747:             }
 3748:             if ($switchserver) {
 3749:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3750:             } else {
 3751:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 3752:             }
 3753:             $datatable .= '</span></fieldset>';
 3754:             my (%checkedfields,%rolemaps,$userincdom);
 3755:             if (ref($settings->{$item}) eq 'HASH') {
 3756:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 3757:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 3758:                 }
 3759:                 $userincdom = $settings->{$item}->{'incdom'};
 3760:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 3761:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 3762:                     $checkedfields{'roles'} = 1;
 3763:                 }
 3764:             }
 3765:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3766:                           '<span class="LC_nobreak">';
 3767:             my $userfieldstyle = 'display:none;';
 3768:             my $seluserdom = '';
 3769:             my $unseluserdom = ' selected="selected"';
 3770:             foreach my $field (@fields) {
 3771:                 my ($checked,$onclick,$id,$spacer);
 3772:                 if ($checkedfields{$field}) {
 3773:                     $checked = ' checked="checked"';
 3774:                 }
 3775:                 if ($field eq 'user') {
 3776:                     $id = ' id="ltitools_user_field_'.$i.'"';
 3777:                     $onclick = ' onclick="toggleLTITools(this.form,'."'$field','$i'".')"';
 3778:                     if ($checked) {
 3779:                         $userfieldstyle = 'display:inline-block';
 3780:                         if ($userincdom) {
 3781:                             $seluserdom = $unseluserdom;
 3782:                             $unseluserdom = '';
 3783:                         }
 3784:                     }
 3785:                 } else {
 3786:                     $spacer = ('&nbsp;' x2);
 3787:                 }
 3788:                 $datatable .= '<label>'.
 3789:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$id.$checked.$onclick.' />'.
 3790:                               $lt{$field}.'</label>'.$spacer;
 3791:             }
 3792:             $datatable .= '</span>';
 3793:             $datatable .= '<div style="'.$userfieldstyle.'" id="ltitools_user_div_'.$i.'">'.
 3794:                           '<span class="LC_nobreak"> : '.
 3795:                           '<select name="ltitools_userincdom_'.$i.'">'.
 3796:                           '<option value="">'.&mt('Select').'</option>'.
 3797:                           '<option value="0"'.$unseluserdom.'>'.&mt('username').'</option>'.
 3798:                           '<option value="1"'.$seluserdom.'>'.&mt('username:domain').'</option>'.
 3799:                           '</select></span></div>';
 3800:             $datatable .= '</fieldset>'.
 3801:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 3802:             foreach my $role (@courseroles) {
 3803:                 my ($selected,$selectnone);
 3804:                 if (!$rolemaps{$role}) {
 3805:                     $selectnone = ' selected="selected"';
 3806:                 }
 3807:                 $datatable .= '<td align="center">'.
 3808:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 3809:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 3810:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 3811:                 foreach my $ltirole (@ltiroles) {
 3812:                     unless ($selectnone) {
 3813:                         if ($rolemaps{$role} eq $ltirole) {
 3814:                             $selected = ' selected="selected"';
 3815:                         } else {
 3816:                             $selected = '';
 3817:                         }
 3818:                     }
 3819:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 3820:                 }
 3821:                 $datatable .= '</select></td>';
 3822:             }
 3823:             $datatable .= '</tr></table></fieldset>';
 3824:             my %courseconfig;
 3825:             if (ref($settings->{$item}) eq 'HASH') {
 3826:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 3827:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 3828:                 }
 3829:             }
 3830:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 3831:             foreach my $item ('label','title','target','linktext','explanation','append') {
 3832:                 my $checked;
 3833:                 if ($courseconfig{$item}) {
 3834:                     $checked = ' checked="checked"';
 3835:                 }
 3836:                 $datatable .= '<label>'.
 3837:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 3838:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 3839:             }
 3840:             $datatable .= '</span></fieldset>'.
 3841:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 3842:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 3843:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 3844:                 my %custom = %{$settings->{$item}->{'custom'}};
 3845:                 if (keys(%custom) > 0) {
 3846:                     foreach my $key (sort(keys(%custom))) {
 3847:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 3848:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 3849:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 3850:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 3851:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 3852:                     }
 3853:                 }
 3854:             }
 3855:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 3856:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 3857:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 3858:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 3859:             $datatable .= '</table></fieldset></td></tr>'."\n";
 3860:             $itemcount ++;
 3861:         }
 3862:     }
 3863:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3864:     my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_add_pos'".');"';
 3865:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 3866:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 3867:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 3868:     for (my $k=0; $k<$maxnum+1; $k++) {
 3869:         my $vpos = $k+1;
 3870:         my $selstr;
 3871:         if ($k == $maxnum) {
 3872:             $selstr = ' selected="selected" ';
 3873:         }
 3874:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3875:     }
 3876:     $datatable .= '</select>&nbsp;'."\n".
 3877:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 3878:                   '<td colspan="2">'.
 3879:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3880:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_add_title" value="" /></span> '."\n".
 3881:                   ('&nbsp;'x2).
 3882:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 3883:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 3884:                   ('&nbsp;'x2).
 3885:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 3886:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3887:                   '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_add_sigmethod">'.
 3888:                   '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 3889:                   '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 3890:                   '<br />'.
 3891:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_add_url" value="" /></span> '."\n".
 3892:                   ('&nbsp;'x2).
 3893:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 3894:                   ('&nbsp;'x2).
 3895:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="ltitools_add_lifetime" value="300" /></span> '."\n".
 3896:                   ('&nbsp;'x2).
 3897:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 3898:                   '<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".
 3899:                   '</fieldset>'.
 3900:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3901:                   '<span class="LC_nobreak">'.&mt('Display target:');
 3902:     my %defaultdisp;
 3903:     $defaultdisp{'iframe'} = ' checked="checked"';
 3904:     foreach my $disp ('iframe','tab','window') {
 3905:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 3906:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 3907:     }
 3908:     $datatable .= ('&nbsp;'x4);
 3909:     foreach my $dimen ('width','height') {
 3910:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3911:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 3912:                       ('&nbsp;'x2);
 3913:     }
 3914:     $datatable .= '</span><br />'.
 3915:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 3916:                   '<input type="text" name="ltitools_add_linktext" size="5" /></div>'.
 3917:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 3918:                   '<textarea name="ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 3919:                   '</div><div style=""></div>'.
 3920:                   '<div style="padding:0;clear:both;margin:0;border:0"></div>';
 3921:     $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 3922:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3923:     if ($switchserver) {
 3924:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3925:     } else {
 3926:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 3927:     }
 3928:     $datatable .= '</span></fieldset>'.
 3929:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3930:                   '<span class="LC_nobreak">';
 3931:     foreach my $field (@fields) {
 3932:         my ($id,$onclick,$spacer);
 3933:         if ($field eq 'user') {
 3934:             $id = ' id="ltitools_user_field_add"';
 3935:             $onclick = ' onclick="toggleLTITools(this.form,'."'$field','add'".')"';
 3936:         } else {
 3937:             $spacer = ('&nbsp;' x2);
 3938:         }
 3939:         $datatable .= '<label>'.
 3940:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'"'.$id.$onclick.' />'.
 3941:                       $lt{$field}.'</label>'.$spacer;
 3942:     }
 3943:     $datatable .= '</span>'.
 3944:                   '<div style="display:none;" id="ltitools_user_div_add">'.
 3945:                   '<span class="LC_nobreak"> : '.
 3946:                   '<select name="ltitools_userincdom_add">'.
 3947:                   '<option value="" selected="selected">'.&mt('Select').'</option>'.
 3948:                   '<option value="0">'.&mt('username').'</option>'.
 3949:                   '<option value="1">'.&mt('username:domain').'</option>'.
 3950:                   '</select></span></div></fieldset>';
 3951:     $datatable .= '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 3952:     foreach my $role (@courseroles) {
 3953:         my ($checked,$checkednone);
 3954:         $datatable .= '<td align="center">'.
 3955:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 3956:                       '<select name="ltitools_add_roles_'.$role.'">'.
 3957:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 3958:         foreach my $ltirole (@ltiroles) {
 3959:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 3960:         }
 3961:         $datatable .= '</select></td>';
 3962:     }
 3963:     $datatable .= '</tr></table></fieldset>'.
 3964:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 3965:     foreach my $item ('label','title','target','linktext','explanation','append') {
 3966:         $datatable .= '<label>'.
 3967:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 3968:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 3969:     }
 3970:     $datatable .= '</span></fieldset>'.
 3971:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 3972:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 3973:                   '<tr><td><span class="LC_nobreak">'.
 3974:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 3975:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 3976:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 3977:                   '</table></fieldset>'."\n".
 3978:                   '</td>'."\n".
 3979:                   '</tr>'."\n";
 3980:     $itemcount ++;
 3981:     return $datatable;
 3982: }
 3983: 
 3984: sub ltitools_names {
 3985:     my %lt = &Apache::lonlocal::texthash(
 3986:                                           'title'          => 'Title',
 3987:                                           'version'        => 'Version',
 3988:                                           'msgtype'        => 'Message Type',
 3989:                                           'sigmethod'      => 'Signature Method',
 3990:                                           'url'            => 'URL',
 3991:                                           'key'            => 'Key',
 3992:                                           'lifetime'       => 'Nonce lifetime (s)',
 3993:                                           'secret'         => 'Secret',
 3994:                                           'icon'           => 'Icon',
 3995:                                           'user'           => 'User',
 3996:                                           'fullname'       => 'Full Name',
 3997:                                           'firstname'      => 'First Name',
 3998:                                           'lastname'       => 'Last Name',
 3999:                                           'email'          => 'E-mail',
 4000:                                           'roles'          => 'Role',
 4001:                                           'window'         => 'Window',
 4002:                                           'tab'            => 'Tab',
 4003:                                           'iframe'         => 'iFrame',
 4004:                                           'height'         => 'Height',
 4005:                                           'width'          => 'Width',
 4006:                                           'linktext'       => 'Default Link Text',
 4007:                                           'explanation'    => 'Default Explanation',
 4008:                                           'crstarget'      => 'Display target',
 4009:                                           'crslabel'       => 'Course label',
 4010:                                           'crstitle'       => 'Course title',
 4011:                                           'crslinktext'    => 'Link Text',
 4012:                                           'crsexplanation' => 'Explanation',
 4013:                                           'crsappend'      => 'Provider URL',
 4014:                                         );
 4015: 
 4016:     return %lt;
 4017: }
 4018: 
 4019: sub print_coursedefaults {
 4020:     my ($position,$dom,$settings,$rowtotal) = @_;
 4021:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 4022:     my $itemcount = 1;
 4023:     my %choices =  &Apache::lonlocal::texthash (
 4024:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 4025:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 4026:         coursecredits        => 'Credits can be specified for courses',
 4027:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 4028:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 4029:         postsubmit           => 'Disable submit button/keypress following student submission',
 4030:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 4031:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 4032:     );
 4033:     my %staticdefaults = (
 4034:                            anonsurvey_threshold => 10,
 4035:                            uploadquota          => 500,
 4036:                            postsubmit           => 60,
 4037:                            mysqltables          => 172800,
 4038:                          );
 4039:     if ($position eq 'top') {
 4040:         %defaultchecked = (
 4041:                             'uselcmath'       => 'on',
 4042:                             'usejsme'         => 'on',
 4043:                             'canclone'        => 'none',
 4044:                           );
 4045:         @toggles = ('uselcmath','usejsme');
 4046:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4047:                                                      \%choices,$itemcount);
 4048:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4049:         $datatable .=
 4050:             '<tr'.$css_class.'><td valign="top">'.
 4051:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 4052:             '</span></td><td class="LC_left_item">';
 4053:         my $currcanclone = 'none';
 4054:         my $onclick;
 4055:         my @cloneoptions = ('none','domain');
 4056:         my %clonetitles = (
 4057:                              none     => 'No additional course requesters',
 4058:                              domain   => "Any course requester in course's domain",
 4059:                              instcode => 'Course requests for official courses ...',
 4060:                           );
 4061:         my (%codedefaults,@code_order,@posscodes);
 4062:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 4063:                                                     \@code_order) eq 'ok') {
 4064:             if (@code_order > 0) {
 4065:                 push(@cloneoptions,'instcode');
 4066:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 4067:             }
 4068:         }
 4069:         if (ref($settings) eq 'HASH') {
 4070:             if ($settings->{'canclone'}) {
 4071:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 4072:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 4073:                         if (@code_order > 0) {
 4074:                             $currcanclone = 'instcode';
 4075:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 4076:                         }
 4077:                     }
 4078:                 } elsif ($settings->{'canclone'} eq 'domain') {
 4079:                     $currcanclone = $settings->{'canclone'};
 4080:                 }
 4081:             }
 4082:         }
 4083:         foreach my $option (@cloneoptions) {
 4084:             my ($checked,$additional);
 4085:             if ($currcanclone eq $option) {
 4086:                 $checked = ' checked="checked"';
 4087:             }
 4088:             if ($option eq 'instcode') {
 4089:                 if (@code_order) {
 4090:                     my $show = 'none';
 4091:                     if ($checked) {
 4092:                         $show = 'block';
 4093:                     }
 4094:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 4095:                                   &mt('Institutional codes for new and cloned course have identical:').
 4096:                                   '<br />';
 4097:                     foreach my $item (@code_order) {
 4098:                         my $codechk;
 4099:                         if ($checked) {
 4100:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 4101:                                 $codechk = ' checked="checked"';
 4102:                             }
 4103:                         }
 4104:                         $additional .= '<label>'.
 4105:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 4106:                                        $item.'</label>';
 4107:                     }
 4108:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 4109:                 }
 4110:             }
 4111:             $datatable .=
 4112:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 4113:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 4114:                 '</label>&nbsp;'.$additional.'</span><br />';
 4115:         }
 4116:         $datatable .= '</td>'.
 4117:                       '</tr>';
 4118:         $itemcount ++;
 4119:     } else {
 4120:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4121:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 4122:         my $currusecredits = 0;
 4123:         my $postsubmitclient = 1;
 4124:         my @types = ('official','unofficial','community','textbook');
 4125:         if (ref($settings) eq 'HASH') {
 4126:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 4127:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 4128:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 4129:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 4130:                 }
 4131:             }
 4132:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 4133:                 foreach my $type (@types) {
 4134:                     next if ($type eq 'community');
 4135:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 4136:                     if ($defcredits{$type} ne '') {
 4137:                         $currusecredits = 1;
 4138:                     }
 4139:                 }
 4140:             }
 4141:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 4142:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 4143:                     $postsubmitclient = 0;
 4144:                     foreach my $type (@types) {
 4145:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4146:                     }
 4147:                 } else {
 4148:                     foreach my $type (@types) {
 4149:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 4150:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 4151:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 4152:                             } else {
 4153:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4154:                             }
 4155:                         } else {
 4156:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4157:                         }
 4158:                     }
 4159:                 }
 4160:             } else {
 4161:                 foreach my $type (@types) {
 4162:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4163:                 }
 4164:             }
 4165:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 4166:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 4167:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 4168:                 }
 4169:             } else {
 4170:                 foreach my $type (@types) {
 4171:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 4172:                 }
 4173:             }
 4174:         } else {
 4175:             foreach my $type (@types) {
 4176:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4177:             }
 4178:         }
 4179:         if (!$currdefresponder) {
 4180:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 4181:         } elsif ($currdefresponder < 1) {
 4182:             $currdefresponder = 1;
 4183:         }
 4184:         foreach my $type (@types) {
 4185:             if ($curruploadquota{$type} eq '') {
 4186:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 4187:             }
 4188:         }
 4189:         $datatable .=
 4190:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4191:                 $choices{'anonsurvey_threshold'}.
 4192:                 '</span></td>'.
 4193:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 4194:                 '<input type="text" name="anonsurvey_threshold"'.
 4195:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 4196:                 '</td></tr>'."\n";
 4197:         $itemcount ++;
 4198:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4199:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4200:                       $choices{'uploadquota'}.
 4201:                       '</span></td>'.
 4202:                       '<td align="right" class="LC_right_item">'.
 4203:                       '<table><tr>';
 4204:         foreach my $type (@types) {
 4205:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4206:                            '<input type="text" name="uploadquota_'.$type.'"'.
 4207:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 4208:         }
 4209:         $datatable .= '</tr></table></td></tr>'."\n";
 4210:         $itemcount ++;
 4211:         my $onclick = "toggleDisplay(this.form,'credits');";
 4212:         my $display = 'none';
 4213:         if ($currusecredits) {
 4214:             $display = 'block';
 4215:         }
 4216:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 4217:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 4218:         foreach my $type (@types) {
 4219:             next if ($type eq 'community');
 4220:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4221:                            '<input type="text" name="'.$type.'_credits"'.
 4222:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 4223:         }
 4224:         $additional .= '</tr></table></div>'."\n";
 4225:         %defaultchecked = ('coursecredits' => 'off');
 4226:         @toggles = ('coursecredits');
 4227:         my $current = {
 4228:                         'coursecredits' => $currusecredits,
 4229:                       };
 4230:         (my $table,$itemcount) =
 4231:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4232:                                \%choices,$itemcount,$onclick,$additional,'left');
 4233:         $datatable .= $table;
 4234:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 4235:         my $display = 'none';
 4236:         if ($postsubmitclient) {
 4237:             $display = 'block';
 4238:         }
 4239:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 4240:                       &mt('Number of seconds submit is disabled').'<br />'.
 4241:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 4242:                       '<table><tr>';
 4243:         foreach my $type (@types) {
 4244:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4245:                            '<input type="text" name="'.$type.'_timeout" value="'.
 4246:                            $deftimeout{$type}.'" size="5" /></td>';
 4247:         }
 4248:         $additional .= '</tr></table></div>'."\n";
 4249:         %defaultchecked = ('postsubmit' => 'on');
 4250:         @toggles = ('postsubmit');
 4251:         $current = {
 4252:                        'postsubmit' => $postsubmitclient,
 4253:                    };
 4254:         ($table,$itemcount) =
 4255:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4256:                                \%choices,$itemcount,$onclick,$additional,'left');
 4257:         $datatable .= $table;
 4258:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4259:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4260:                       $choices{'mysqltables'}.
 4261:                       '</span></td>'.
 4262:                       '<td align="right" class="LC_right_item">'.
 4263:                       '<table><tr>';
 4264:         foreach my $type (@types) {
 4265:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4266:                            '<input type="text" name="mysqltables_'.$type.'"'.
 4267:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 4268:         }
 4269:         $datatable .= '</tr></table></td></tr>'."\n";
 4270:         $itemcount ++;
 4271: 
 4272:     }
 4273:     $$rowtotal += $itemcount;
 4274:     return $datatable;
 4275: }
 4276: 
 4277: sub print_selfenrollment {
 4278:     my ($position,$dom,$settings,$rowtotal) = @_;
 4279:     my ($css_class,$datatable);
 4280:     my $itemcount = 1;
 4281:     my @types = ('official','unofficial','community','textbook');
 4282:     if (($position eq 'top') || ($position eq 'middle')) {
 4283:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 4284:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 4285:         my @rows;
 4286:         my $key;
 4287:         if ($position eq 'top') {
 4288:             $key = 'admin'; 
 4289:             if (ref($rowsref) eq 'ARRAY') {
 4290:                 @rows = @{$rowsref};
 4291:             }
 4292:         } elsif ($position eq 'middle') {
 4293:             $key = 'default';
 4294:             @rows = ('types','registered','approval','limit');
 4295:         }
 4296:         foreach my $row (@rows) {
 4297:             if (defined($titlesref->{$row})) {
 4298:                 $itemcount ++;
 4299:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4300:                 $datatable .= '<tr'.$css_class.'>'.
 4301:                               '<td>'.$titlesref->{$row}.'</td>'.
 4302:                               '<td class="LC_left_item">'.
 4303:                               '<table><tr>';
 4304:                 my (%current,%currentcap);
 4305:                 if (ref($settings) eq 'HASH') {
 4306:                     if (ref($settings->{$key}) eq 'HASH') {
 4307:                         foreach my $type (@types) {
 4308:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4309:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 4310:                             }
 4311:                             if (($row eq 'limit') && ($key eq 'default')) {
 4312:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4313:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 4314:                                 }
 4315:                             }
 4316:                         }
 4317:                     }
 4318:                 }
 4319:                 my %roles = (
 4320:                              '0' => &Apache::lonnet::plaintext('dc'),
 4321:                             ); 
 4322:             
 4323:                 foreach my $type (@types) {
 4324:                     unless (($row eq 'registered') && ($key eq 'default')) {
 4325:                         $datatable .= '<th>'.&mt($type).'</th>';
 4326:                     }
 4327:                 }
 4328:                 unless (($row eq 'registered') && ($key eq 'default')) {
 4329:                     $datatable .= '</tr><tr>';
 4330:                 }
 4331:                 foreach my $type (@types) {
 4332:                     if ($type eq 'community') {
 4333:                         $roles{'1'} = &mt('Community personnel');
 4334:                     } else {
 4335:                         $roles{'1'} = &mt('Course personnel');
 4336:                     }
 4337:                     $datatable .= '<td style="vertical-align: top">';
 4338:                     if ($position eq 'top') {
 4339:                         my %checked;
 4340:                         if ($current{$type} eq '0') {
 4341:                             $checked{'0'} = ' checked="checked"';
 4342:                         } else {
 4343:                             $checked{'1'} = ' checked="checked"';
 4344:                         }
 4345:                         foreach my $role ('1','0') {
 4346:                             $datatable .= '<span class="LC_nobreak"><label>'.
 4347:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 4348:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 4349:                                           $roles{$role}.'</label></span> ';
 4350:                         }
 4351:                     } else {
 4352:                         if ($row eq 'types') {
 4353:                             my %checked;
 4354:                             if ($current{$type} =~ /^(all|dom)$/) {
 4355:                                 $checked{$1} = ' checked="checked"';
 4356:                             } else {
 4357:                                 $checked{''} = ' checked="checked"';
 4358:                             }
 4359:                             foreach my $val ('','dom','all') {
 4360:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4361:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4362:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4363:                             }
 4364:                         } elsif ($row eq 'registered') {
 4365:                             my %checked;
 4366:                             if ($current{$type} eq '1') {
 4367:                                 $checked{'1'} = ' checked="checked"';
 4368:                             } else {
 4369:                                 $checked{'0'} = ' checked="checked"';
 4370:                             }
 4371:                             foreach my $val ('0','1') {
 4372:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4373:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4374:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4375:                             }
 4376:                         } elsif ($row eq 'approval') {
 4377:                             my %checked;
 4378:                             if ($current{$type} =~ /^([12])$/) {
 4379:                                 $checked{$1} = ' checked="checked"';
 4380:                             } else {
 4381:                                 $checked{'0'} = ' checked="checked"';
 4382:                             }
 4383:                             for my $val (0..2) {
 4384:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4385:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4386:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4387:                             }
 4388:                         } elsif ($row eq 'limit') {
 4389:                             my %checked;
 4390:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 4391:                                 $checked{$1} = ' checked="checked"';
 4392:                             } else {
 4393:                                 $checked{'none'} = ' checked="checked"';
 4394:                             }
 4395:                             my $cap;
 4396:                             if ($currentcap{$type} =~ /^\d+$/) {
 4397:                                 $cap = $currentcap{$type};
 4398:                             }
 4399:                             foreach my $val ('none','allstudents','selfenrolled') {
 4400:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4401:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4402:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4403:                             }
 4404:                             $datatable .= '<br />'.
 4405:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 4406:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 4407:                                           '</span>'; 
 4408:                         }
 4409:                     }
 4410:                     $datatable .= '</td>';
 4411:                 }
 4412:                 $datatable .= '</tr>';
 4413:             }
 4414:             $datatable .= '</table></td></tr>';
 4415:         }
 4416:     } elsif ($position eq 'bottom') {
 4417:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 4418:     }
 4419:     $$rowtotal += $itemcount;
 4420:     return $datatable;
 4421: }
 4422: 
 4423: sub print_validation_rows {
 4424:     my ($caller,$dom,$settings,$rowtotal) = @_;
 4425:     my ($itemsref,$namesref,$fieldsref);
 4426:     if ($caller eq 'selfenroll') { 
 4427:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 4428:     } elsif ($caller eq 'requestcourses') {
 4429:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 4430:     }
 4431:     my %currvalidation;
 4432:     if (ref($settings) eq 'HASH') {
 4433:         if (ref($settings->{'validation'}) eq 'HASH') {
 4434:             %currvalidation = %{$settings->{'validation'}};
 4435:         }
 4436:     }
 4437:     my $datatable;
 4438:     my $itemcount = 0;
 4439:     foreach my $item (@{$itemsref}) {
 4440:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4441:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4442:                       $namesref->{$item}.
 4443:                       '</span></td>'.
 4444:                       '<td class="LC_left_item">';
 4445:         if (($item eq 'url') || ($item eq 'button')) {
 4446:             $datatable .= '<span class="LC_nobreak">'.
 4447:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 4448:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 4449:         } elsif ($item eq 'fields') {
 4450:             my @currfields;
 4451:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 4452:                 @currfields = @{$currvalidation{$item}};
 4453:             }
 4454:             foreach my $field (@{$fieldsref}) {
 4455:                 my $check = '';
 4456:                 if (grep(/^\Q$field\E$/,@currfields)) {
 4457:                     $check = ' checked="checked"';
 4458:                 }
 4459:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4460:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 4461:                               ' value="'.$field.'"'.$check.' />'.$field.
 4462:                               '</label></span> ';
 4463:             }
 4464:         } elsif ($item eq 'markup') {
 4465:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 4466:                            $currvalidation{$item}.
 4467:                               '</textarea>';
 4468:         }
 4469:         $datatable .= '</td></tr>'."\n";
 4470:         if (ref($rowtotal)) {
 4471:             $itemcount ++;
 4472:         }
 4473:     }
 4474:     if ($caller eq 'requestcourses') {
 4475:         my %currhash;
 4476:         if (ref($settings) eq 'HASH') {
 4477:             if (ref($settings->{'validation'}) eq 'HASH') {
 4478:                 if ($settings->{'validation'}{'dc'} ne '') {
 4479:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 4480:                 }
 4481:             }
 4482:         }
 4483:         my $numinrow = 2;
 4484:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4485:                                                        'validationdc',%currhash);
 4486:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4487:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 4488:         if ($numdc > 1) {
 4489:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 4490:         } else {
 4491:             $datatable .=  &mt('Course creation processed as: ');
 4492:         }
 4493:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 4494:         $itemcount ++;
 4495:     }
 4496:     if (ref($rowtotal)) {
 4497:         $$rowtotal += $itemcount;
 4498:     }
 4499:     return $datatable;
 4500: }
 4501: 
 4502: sub print_usersessions {
 4503:     my ($position,$dom,$settings,$rowtotal) = @_;
 4504:     my ($css_class,$datatable,%checked,%choices);
 4505:     my (%by_ip,%by_location,@intdoms);
 4506:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 4507: 
 4508:     my @alldoms = &Apache::lonnet::all_domains();
 4509:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4510:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4511:     my %altids = &id_for_thisdom(%servers);
 4512:     my $itemcount = 1;
 4513:     if ($position eq 'top') {
 4514:         if (keys(%serverhomes) > 1) {
 4515:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 4516:             my $curroffloadnow;
 4517:             if (ref($settings) eq 'HASH') {
 4518:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 4519:                     $curroffloadnow = $settings->{'offloadnow'};
 4520:                 }
 4521:             }
 4522:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 4523:         } else {
 4524:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4525:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 4526:         }
 4527:     } else {
 4528:         if (keys(%by_location) == 0) {
 4529:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4530:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 4531:         } else {
 4532:             my %lt = &usersession_titles();
 4533:             my $numinrow = 5;
 4534:             my $prefix;
 4535:             my @types;
 4536:             if ($position eq 'bottom') {
 4537:                 $prefix = 'remote';
 4538:                 @types = ('version','excludedomain','includedomain');
 4539:             } else {
 4540:                 $prefix = 'hosted';
 4541:                 @types = ('excludedomain','includedomain');
 4542:             }
 4543:             my (%current,%checkedon,%checkedoff);
 4544:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 4545:             my @locations = sort(keys(%by_location));
 4546:             foreach my $type (@types) {
 4547:                 $checkedon{$type} = '';
 4548:                 $checkedoff{$type} = ' checked="checked"';
 4549:             }
 4550:             if (ref($settings) eq 'HASH') {
 4551:                 if (ref($settings->{$prefix}) eq 'HASH') {
 4552:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 4553:                         $current{$key} = $settings->{$prefix}{$key};
 4554:                         if ($key eq 'version') {
 4555:                             if ($current{$key} ne '') {
 4556:                                 $checkedon{$key} = ' checked="checked"';
 4557:                                 $checkedoff{$key} = '';
 4558:                             }
 4559:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 4560:                             $checkedon{$key} = ' checked="checked"';
 4561:                             $checkedoff{$key} = '';
 4562:                         }
 4563:                     }
 4564:                 }
 4565:             }
 4566:             foreach my $type (@types) {
 4567:                 next if ($type ne 'version' && !@locations);
 4568:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4569:                 $datatable .= '<tr'.$css_class.'>
 4570:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 4571:                                <span class="LC_nobreak">&nbsp;
 4572:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 4573:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 4574:                 if ($type eq 'version') {
 4575:                     my $selector = '<select name="'.$prefix.'_version">';
 4576:                     foreach my $version (@lcversions) {
 4577:                         my $selected = '';
 4578:                         if ($current{'version'} eq $version) {
 4579:                             $selected = ' selected="selected"';
 4580:                         }
 4581:                         $selector .= ' <option value="'.$version.'"'.
 4582:                                      $selected.'>'.$version.'</option>';
 4583:                     }
 4584:                     $selector .= '</select> ';
 4585:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 4586:                 } else {
 4587:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 4588:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 4589:                                  ' />'.('&nbsp;'x2).
 4590:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 4591:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 4592:                                  "\n".
 4593:                                  '</div><div><table>';
 4594:                     my $rem;
 4595:                     for (my $i=0; $i<@locations; $i++) {
 4596:                         my ($showloc,$value,$checkedtype);
 4597:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 4598:                             my $ip = $by_location{$locations[$i]}->[0];
 4599:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 4600:                                  $value = join(':',@{$by_ip{$ip}});
 4601:                                 $showloc = join(', ',@{$by_ip{$ip}});
 4602:                                 if (ref($current{$type}) eq 'ARRAY') {
 4603:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 4604:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 4605:                                             $checkedtype = ' checked="checked"';
 4606:                                             last;
 4607:                                         }
 4608:                                     }
 4609:                                 }
 4610:                             }
 4611:                         }
 4612:                         $rem = $i%($numinrow);
 4613:                         if ($rem == 0) {
 4614:                             if ($i > 0) {
 4615:                                 $datatable .= '</tr>';
 4616:                             }
 4617:                             $datatable .= '<tr>';
 4618:                         }
 4619:                         $datatable .= '<td class="LC_left_item">'.
 4620:                                       '<span class="LC_nobreak"><label>'.
 4621:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 4622:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 4623:                                       '</label></span></td>';
 4624:                     }
 4625:                     $rem = @locations%($numinrow);
 4626:                     my $colsleft = $numinrow - $rem;
 4627:                     if ($colsleft > 1 ) {
 4628:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4629:                                       '&nbsp;</td>';
 4630:                     } elsif ($colsleft == 1) {
 4631:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4632:                     }
 4633:                     $datatable .= '</tr></table>';
 4634:                 }
 4635:                 $datatable .= '</td></tr>';
 4636:                 $itemcount ++;
 4637:             }
 4638:         }
 4639:     }
 4640:     $$rowtotal += $itemcount;
 4641:     return $datatable;
 4642: }
 4643: 
 4644: sub build_location_hashes {
 4645:     my ($intdoms,$by_ip,$by_location) = @_;
 4646:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 4647:                   (ref($by_location) eq 'HASH')); 
 4648:     my %iphost = &Apache::lonnet::get_iphost();
 4649:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 4650:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 4651:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 4652:         foreach my $id (@{$iphost{$primary_ip}}) {
 4653:             my $intdom = &Apache::lonnet::internet_dom($id);
 4654:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 4655:                 push(@{$intdoms},$intdom);
 4656:             }
 4657:         }
 4658:     }
 4659:     foreach my $ip (keys(%iphost)) {
 4660:         if (ref($iphost{$ip}) eq 'ARRAY') {
 4661:             foreach my $id (@{$iphost{$ip}}) {
 4662:                 my $location = &Apache::lonnet::internet_dom($id);
 4663:                 if ($location) {
 4664:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 4665:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4666:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 4667:                             push(@{$by_ip->{$ip}},$location);
 4668:                         }
 4669:                     } else {
 4670:                         $by_ip->{$ip} = [$location];
 4671:                     }
 4672:                 }
 4673:             }
 4674:         }
 4675:     }
 4676:     foreach my $ip (sort(keys(%{$by_ip}))) {
 4677:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4678:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 4679:             my $first = $by_ip->{$ip}->[0];
 4680:             if (ref($by_location->{$first}) eq 'ARRAY') {
 4681:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 4682:                     push(@{$by_location->{$first}},$ip);
 4683:                 }
 4684:             } else {
 4685:                 $by_location->{$first} = [$ip];
 4686:             }
 4687:         }
 4688:     }
 4689:     return;
 4690: }
 4691: 
 4692: sub current_offloads_to {
 4693:     my ($dom,$settings,$servers) = @_;
 4694:     my (%spareid,%otherdomconfigs);
 4695:     if (ref($servers) eq 'HASH') {
 4696:         foreach my $lonhost (sort(keys(%{$servers}))) {
 4697:             my $gotspares;
 4698:             if (ref($settings) eq 'HASH') {
 4699:                 if (ref($settings->{'spares'}) eq 'HASH') {
 4700:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 4701:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 4702:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 4703:                         $gotspares = 1;
 4704:                     }
 4705:                 }
 4706:             }
 4707:             unless ($gotspares) {
 4708:                 my $gotspares;
 4709:                 my $serverhomeID =
 4710:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 4711:                 my $serverhomedom =
 4712:                     &Apache::lonnet::host_domain($serverhomeID);
 4713:                 if ($serverhomedom ne $dom) {
 4714:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 4715:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4716:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4717:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4718:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4719:                                 $gotspares = 1;
 4720:                             }
 4721:                         }
 4722:                     } else {
 4723:                         $otherdomconfigs{$serverhomedom} =
 4724:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 4725:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 4726:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4727:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4728:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 4729:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4730:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4731:                                         $gotspares = 1;
 4732:                                     }
 4733:                                 }
 4734:                             }
 4735:                         }
 4736:                     }
 4737:                 }
 4738:             }
 4739:             unless ($gotspares) {
 4740:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4741:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4742:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4743:                } else {
 4744:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 4745:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 4746:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4747:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4748:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4749:                     } else {
 4750:                         my %what = (
 4751:                              spareid => 1,
 4752:                         );
 4753:                         my ($result,$returnhash) = 
 4754:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 4755:                         if ($result eq 'ok') { 
 4756:                             if (ref($returnhash) eq 'HASH') {
 4757:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 4758:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 4759:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 4760:                                 }
 4761:                             }
 4762:                         }
 4763:                     }
 4764:                 }
 4765:             }
 4766:         }
 4767:     }
 4768:     return %spareid;
 4769: }
 4770: 
 4771: sub spares_row {
 4772:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 4773:     my $css_class;
 4774:     my $numinrow = 4;
 4775:     my $itemcount = 1;
 4776:     my $datatable;
 4777:     my %typetitles = &sparestype_titles();
 4778:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4779:         foreach my $server (sort(keys(%{$servers}))) {
 4780:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 4781:             my ($othercontrol,$serverdom);
 4782:             if ($serverhome ne $server) {
 4783:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 4784:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4785:             } else {
 4786:                 $serverdom = &Apache::lonnet::host_domain($server);
 4787:                 if ($serverdom ne $dom) {
 4788:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 4789:                 }
 4790:             }
 4791:             next unless (ref($spareid->{$server}) eq 'HASH');
 4792:             my $checkednow;
 4793:             if (ref($curroffloadnow) eq 'HASH') {
 4794:                 if ($curroffloadnow->{$server}) {
 4795:                     $checkednow = ' checked="checked"';
 4796:                 }
 4797:             }
 4798:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4799:             $datatable .= '<tr'.$css_class.'>
 4800:                            <td rowspan="2">
 4801:                             <span class="LC_nobreak">'.
 4802:                           &mt('[_1] when busy, offloads to:'
 4803:                               ,'<b>'.$server.'</b>').'</span><br />'.
 4804:                           '<span class="LC_nobreak">'."\n".
 4805:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 4806:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 4807:                           "\n";
 4808:             my (%current,%canselect);
 4809:             my @choices = 
 4810:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 4811:             foreach my $type ('primary','default') {
 4812:                 if (ref($spareid->{$server}) eq 'HASH') {
 4813:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 4814:                         my @spares = @{$spareid->{$server}{$type}};
 4815:                         if (@spares > 0) {
 4816:                             if ($othercontrol) {
 4817:                                 $current{$type} = join(', ',@spares);
 4818:                             } else {
 4819:                                 $current{$type} .= '<table>';
 4820:                                 my $numspares = scalar(@spares);
 4821:                                 for (my $i=0;  $i<@spares; $i++) {
 4822:                                     my $rem = $i%($numinrow);
 4823:                                     if ($rem == 0) {
 4824:                                         if ($i > 0) {
 4825:                                             $current{$type} .= '</tr>';
 4826:                                         }
 4827:                                         $current{$type} .= '<tr>';
 4828:                                     }
 4829:                                     $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;'.
 4830:                                                        $spareid->{$server}{$type}[$i].
 4831:                                                        '</label></td>'."\n";
 4832:                                 }
 4833:                                 my $rem = @spares%($numinrow);
 4834:                                 my $colsleft = $numinrow - $rem;
 4835:                                 if ($colsleft > 1 ) {
 4836:                                     $current{$type} .= '<td colspan="'.$colsleft.
 4837:                                                        '" class="LC_left_item">'.
 4838:                                                        '&nbsp;</td>';
 4839:                                 } elsif ($colsleft == 1) {
 4840:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 4841:                                 }
 4842:                                 $current{$type} .= '</tr></table>';
 4843:                             }
 4844:                         }
 4845:                     }
 4846:                     if ($current{$type} eq '') {
 4847:                         $current{$type} = &mt('None specified');
 4848:                     }
 4849:                     if ($othercontrol) {
 4850:                         if ($type eq 'primary') {
 4851:                             $canselect{$type} = $othercontrol;
 4852:                         }
 4853:                     } else {
 4854:                         $canselect{$type} = 
 4855:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 4856:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 4857:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 4858:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 4859:                         if (@choices > 0) {
 4860:                             foreach my $lonhost (@choices) {
 4861:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 4862:                             }
 4863:                         }
 4864:                         $canselect{$type} .= '</select>'."\n";
 4865:                     }
 4866:                 } else {
 4867:                     $current{$type} = &mt('Could not be determined');
 4868:                     if ($type eq 'primary') {
 4869:                         $canselect{$type} =  $othercontrol;
 4870:                     }
 4871:                 }
 4872:                 if ($type eq 'default') {
 4873:                     $datatable .= '<tr'.$css_class.'>';
 4874:                 }
 4875:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 4876:                               '<td>'.$current{$type}.'</td>'."\n".
 4877:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 4878:             }
 4879:             $itemcount ++;
 4880:         }
 4881:     }
 4882:     $$rowtotal += $itemcount;
 4883:     return $datatable;
 4884: }
 4885: 
 4886: sub possible_newspares {
 4887:     my ($server,$currspares,$serverhomes,$altids) = @_;
 4888:     my $serverhostname = &Apache::lonnet::hostname($server);
 4889:     my %excluded;
 4890:     if ($serverhostname ne '') {
 4891:         %excluded = (
 4892:                        $serverhostname => 1,
 4893:                     );
 4894:     }
 4895:     if (ref($currspares) eq 'HASH') {
 4896:         foreach my $type (keys(%{$currspares})) {
 4897:             if (ref($currspares->{$type}) eq 'ARRAY') {
 4898:                 if (@{$currspares->{$type}} > 0) {
 4899:                     foreach my $curr (@{$currspares->{$type}}) {
 4900:                         my $hostname = &Apache::lonnet::hostname($curr);
 4901:                         $excluded{$hostname} = 1;
 4902:                     }
 4903:                 }
 4904:             }
 4905:         }
 4906:     }
 4907:     my @choices;
 4908:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4909:         if (keys(%{$serverhomes}) > 1) {
 4910:             foreach my $name (sort(keys(%{$serverhomes}))) {
 4911:                 unless ($excluded{$name}) {
 4912:                     if (exists($altids->{$serverhomes->{$name}})) {
 4913:                         push(@choices,$altids->{$serverhomes->{$name}});
 4914:                     } else {
 4915:                         push(@choices,$serverhomes->{$name});
 4916:                     }
 4917:                 }
 4918:             }
 4919:         }
 4920:     }
 4921:     return sort(@choices);
 4922: }
 4923: 
 4924: sub print_loadbalancing {
 4925:     my ($dom,$settings,$rowtotal) = @_;
 4926:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 4927:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 4928:     my $numinrow = 1;
 4929:     my $datatable;
 4930:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4931:     my (%currbalancer,%currtargets,%currrules,%existing);
 4932:     if (ref($settings) eq 'HASH') {
 4933:         %existing = %{$settings};
 4934:     }
 4935:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 4936:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 4937:                                   \%currtargets,\%currrules);
 4938:     } else {
 4939:         return;
 4940:     }
 4941:     my ($othertitle,$usertypes,$types) =
 4942:         &Apache::loncommon::sorted_inst_types($dom);
 4943:     my $rownum = 8;
 4944:     if (ref($types) eq 'ARRAY') {
 4945:         $rownum += scalar(@{$types});
 4946:     }
 4947:     my @css_class = ('LC_odd_row','LC_even_row');
 4948:     my $balnum = 0;
 4949:     my $islast;
 4950:     my (@toshow,$disabledtext);
 4951:     if (keys(%currbalancer) > 0) {
 4952:         @toshow = sort(keys(%currbalancer));
 4953:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 4954:             push(@toshow,'');
 4955:         }
 4956:     } else {
 4957:         @toshow = ('');
 4958:         $disabledtext = &mt('No existing load balancer');
 4959:     }
 4960:     foreach my $lonhost (@toshow) {
 4961:         if ($balnum == scalar(@toshow)-1) {
 4962:             $islast = 1;
 4963:         } else {
 4964:             $islast = 0;
 4965:         }
 4966:         my $cssidx = $balnum%2;
 4967:         my $targets_div_style = 'display: none';
 4968:         my $disabled_div_style = 'display: block';
 4969:         my $homedom_div_style = 'display: none';
 4970:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 4971:                       '<td rowspan="'.$rownum.'" valign="top">'.
 4972:                       '<p>';
 4973:         if ($lonhost eq '') {
 4974:             $datatable .= '<span class="LC_nobreak">';
 4975:             if (keys(%currbalancer) > 0) {
 4976:                 $datatable .= &mt('Add balancer:');
 4977:             } else {
 4978:                 $datatable .= &mt('Enable balancer:');
 4979:             }
 4980:             $datatable .= '&nbsp;'.
 4981:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 4982:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 4983:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 4984:                           '<option value="" selected="selected">'.&mt('None').
 4985:                           '</option>'."\n";
 4986:             foreach my $server (sort(keys(%servers))) {
 4987:                 next if ($currbalancer{$server});
 4988:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 4989:             }
 4990:             $datatable .=
 4991:                 '</select>'."\n".
 4992:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 4993:         } else {
 4994:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 4995:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 4996:                            &mt('Stop balancing').'</label>'.
 4997:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 4998:             $targets_div_style = 'display: block';
 4999:             $disabled_div_style = 'display: none';
 5000:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 5001:                 $homedom_div_style = 'display: block';
 5002:             }
 5003:         }
 5004:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 5005:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 5006:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 5007:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 5008:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 5009:         my @sparestypes = ('primary','default');
 5010:         my %typetitles = &sparestype_titles();
 5011:         my %hostherechecked = (
 5012:                                   no => ' checked="checked"',
 5013:                               );
 5014:         foreach my $sparetype (@sparestypes) {
 5015:             my $targettable;
 5016:             for (my $i=0; $i<$numspares; $i++) {
 5017:                 my $checked;
 5018:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 5019:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5020:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5021:                             $checked = ' checked="checked"';
 5022:                         }
 5023:                     }
 5024:                 }
 5025:                 my ($chkboxval,$disabled);
 5026:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 5027:                     $chkboxval = $spares[$i];
 5028:                 }
 5029:                 if (exists($currbalancer{$spares[$i]})) {
 5030:                     $disabled = ' disabled="disabled"';
 5031:                 }
 5032:                 $targettable .=
 5033:                     '<td><span class="LC_nobreak"><label>'.
 5034:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 5035:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 5036:                     '</span></label></span></td>';
 5037:                 my $rem = $i%($numinrow);
 5038:                 if ($rem == 0) {
 5039:                     if (($i > 0) && ($i < $numspares-1)) {
 5040:                         $targettable .= '</tr>';
 5041:                     }
 5042:                     if ($i < $numspares-1) {
 5043:                         $targettable .= '<tr>';
 5044:                     }
 5045:                 }
 5046:             }
 5047:             if ($targettable ne '') {
 5048:                 my $rem = $numspares%($numinrow);
 5049:                 my $colsleft = $numinrow - $rem;
 5050:                 if ($colsleft > 1 ) {
 5051:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5052:                                     '&nbsp;</td>';
 5053:                 } elsif ($colsleft == 1) {
 5054:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 5055:                 }
 5056:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 5057:                                '<table><tr>'.$targettable.'</tr></table><br />';
 5058:             }
 5059:             $hostherechecked{$sparetype} = '';
 5060:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 5061:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5062:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5063:                         $hostherechecked{$sparetype} = ' checked="checked"';
 5064:                         $hostherechecked{'no'} = '';
 5065:                     }
 5066:                 }
 5067:             }
 5068:         }
 5069:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 5070:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 5071:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 5072:         foreach my $sparetype (@sparestypes) {
 5073:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 5074:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 5075:                           '</i></label><br />';
 5076:         }
 5077:         $datatable .= '</div></td></tr>'.
 5078:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 5079:                                            $othertitle,$usertypes,$types,\%servers,
 5080:                                            \%currbalancer,$lonhost,
 5081:                                            $targets_div_style,$homedom_div_style,
 5082:                                            $css_class[$cssidx],$balnum,$islast);
 5083:         $$rowtotal += $rownum;
 5084:         $balnum ++;
 5085:     }
 5086:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 5087:     return $datatable;
 5088: }
 5089: 
 5090: sub get_loadbalancers_config {
 5091:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 5092:     return unless ((ref($servers) eq 'HASH') &&
 5093:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 5094:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 5095:     if (keys(%{$existing}) > 0) {
 5096:         my $oldlonhost;
 5097:         foreach my $key (sort(keys(%{$existing}))) {
 5098:             if ($key eq 'lonhost') {
 5099:                 $oldlonhost = $existing->{'lonhost'};
 5100:                 $currbalancer->{$oldlonhost} = 1;
 5101:             } elsif ($key eq 'targets') {
 5102:                 if ($oldlonhost) {
 5103:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 5104:                 }
 5105:             } elsif ($key eq 'rules') {
 5106:                 if ($oldlonhost) {
 5107:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 5108:                 }
 5109:             } elsif (ref($existing->{$key}) eq 'HASH') {
 5110:                 $currbalancer->{$key} = 1;
 5111:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 5112:                 $currrules->{$key} = $existing->{$key}{'rules'};
 5113:             }
 5114:         }
 5115:     } else {
 5116:         my ($balancerref,$targetsref) =
 5117:                 &Apache::lonnet::get_lonbalancer_config($servers);
 5118:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 5119:             foreach my $server (sort(keys(%{$balancerref}))) {
 5120:                 $currbalancer->{$server} = 1;
 5121:                 $currtargets->{$server} = $targetsref->{$server};
 5122:             }
 5123:         }
 5124:     }
 5125:     return;
 5126: }
 5127: 
 5128: sub loadbalancing_rules {
 5129:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 5130:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 5131:         $css_class,$balnum,$islast) = @_;
 5132:     my $output;
 5133:     my $num = 0;
 5134:     my ($alltypes,$othertypes,$titles) =
 5135:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 5136:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 5137:         foreach my $type (@{$alltypes}) {
 5138:             $num ++;
 5139:             my $current;
 5140:             if (ref($currrules) eq 'HASH') {
 5141:                 $current = $currrules->{$type};
 5142:             }
 5143:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5144:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 5145:                     $current = '';
 5146:                 }
 5147:             }
 5148:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 5149:                                              $servers,$currbalancer,$lonhost,$dom,
 5150:                                              $targets_div_style,$homedom_div_style,
 5151:                                              $css_class,$balnum,$num,$islast);
 5152:         }
 5153:     }
 5154:     return $output;
 5155: }
 5156: 
 5157: sub loadbalancing_titles {
 5158:     my ($dom,$intdom,$usertypes,$types) = @_;
 5159:     my %othertypes = (
 5160:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 5161:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 5162:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 5163:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 5164:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 5165:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 5166:                      );
 5167:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 5168:     if (ref($types) eq 'ARRAY') {
 5169:         unshift(@alltypes,@{$types},'default');
 5170:     }
 5171:     my %titles;
 5172:     foreach my $type (@alltypes) {
 5173:         if ($type =~ /^_LC_/) {
 5174:             $titles{$type} = $othertypes{$type};
 5175:         } elsif ($type eq 'default') {
 5176:             $titles{$type} = &mt('All users from [_1]',$dom);
 5177:             if (ref($types) eq 'ARRAY') {
 5178:                 if (@{$types} > 0) {
 5179:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 5180:                 }
 5181:             }
 5182:         } elsif (ref($usertypes) eq 'HASH') {
 5183:             $titles{$type} = $usertypes->{$type};
 5184:         }
 5185:     }
 5186:     return (\@alltypes,\%othertypes,\%titles);
 5187: }
 5188: 
 5189: sub loadbalance_rule_row {
 5190:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 5191:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 5192:     my @rulenames;
 5193:     my %ruletitles = &offloadtype_text();
 5194:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 5195:         @rulenames = ('balancer','offloadedto','specific');
 5196:     } else {
 5197:         @rulenames = ('default','homeserver');
 5198:         if ($type eq '_LC_external') {
 5199:             push(@rulenames,'externalbalancer');
 5200:         } else {
 5201:             push(@rulenames,'specific');
 5202:         }
 5203:         push(@rulenames,'none');
 5204:     }
 5205:     my $style = $targets_div_style;
 5206:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5207:         $style = $homedom_div_style;
 5208:     }
 5209:     my $space;
 5210:     if ($islast && $num == 1) {
 5211:         $space = '<div display="inline-block">&nbsp;</div>';
 5212:     }
 5213:     my $output =
 5214:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 5215:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 5216:         '<td valaign="top">'.$space.
 5217:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 5218:     for (my $i=0; $i<@rulenames; $i++) {
 5219:         my $rule = $rulenames[$i];
 5220:         my ($checked,$extra);
 5221:         if ($rulenames[$i] eq 'default') {
 5222:             $rule = '';
 5223:         }
 5224:         if ($rulenames[$i] eq 'specific') {
 5225:             if (ref($servers) eq 'HASH') {
 5226:                 my $default;
 5227:                 if (($current ne '') && (exists($servers->{$current}))) {
 5228:                     $checked = ' checked="checked"';
 5229:                 }
 5230:                 unless ($checked) {
 5231:                     $default = ' selected="selected"';
 5232:                 }
 5233:                 $extra =
 5234:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5235:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5236:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 5237:                     '<option value=""'.$default.'></option>'."\n";
 5238:                 foreach my $server (sort(keys(%{$servers}))) {
 5239:                     if (ref($currbalancer) eq 'HASH') {
 5240:                         next if (exists($currbalancer->{$server}));
 5241:                     }
 5242:                     my $selected;
 5243:                     if ($server eq $current) {
 5244:                         $selected = ' selected="selected"';
 5245:                     }
 5246:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 5247:                 }
 5248:                 $extra .= '</select>';
 5249:             }
 5250:         } elsif ($rule eq $current) {
 5251:             $checked = ' checked="checked"';
 5252:         }
 5253:         $output .= '<span class="LC_nobreak"><label>'.
 5254:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 5255:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 5256:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 5257:                    ')"'.$checked.' />&nbsp;';
 5258:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 5259:             $output .= $ruletitles{'particular'};
 5260:         } else {
 5261:             $output .= $ruletitles{$rulenames[$i]};
 5262:         }
 5263:         $output .= '</label>'.$extra.'</span><br />'."\n";
 5264:     }
 5265:     $output .= '</div></td></tr>'."\n";
 5266:     return $output;
 5267: }
 5268: 
 5269: sub offloadtype_text {
 5270:     my %ruletitles = &Apache::lonlocal::texthash (
 5271:            'default'          => 'Offloads to default destinations',
 5272:            'homeserver'       => "Offloads to user's home server",
 5273:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 5274:            'specific'         => 'Offloads to specific server',
 5275:            'none'             => 'No offload',
 5276:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 5277:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 5278:            'particular'       => 'Session hosted (after re-auth) on server:',
 5279:     );
 5280:     return %ruletitles;
 5281: }
 5282: 
 5283: sub sparestype_titles {
 5284:     my %typestitles = &Apache::lonlocal::texthash (
 5285:                           'primary' => 'primary',
 5286:                           'default' => 'default',
 5287:                       );
 5288:     return %typestitles;
 5289: }
 5290: 
 5291: sub contact_titles {
 5292:     my %titles = &Apache::lonlocal::texthash (
 5293:                    'supportemail'    => 'Support E-mail address',
 5294:                    'adminemail'      => 'Default Server Admin E-mail address',
 5295:                    'errormail'       => 'Error reports to be e-mailed to',
 5296:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 5297:                    'helpdeskmail'    => "Helpdesk requests for this domain's users",
 5298:                    'otherdomsmail'   => 'Helpdesk requests for other (unconfigured) domains',
 5299:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 5300:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 5301:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 5302:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 5303:                  );
 5304:     my %short_titles = &Apache::lonlocal::texthash (
 5305:                            adminemail   => 'Admin E-mail address',
 5306:                            supportemail => 'Support E-mail',
 5307:                        );   
 5308:     return (\%titles,\%short_titles);
 5309: }
 5310: 
 5311: sub helpform_fields {
 5312:     my %titles =  &Apache::lonlocal::texthash (
 5313:                        'username'   => 'Name',
 5314:                        'user'       => 'Username/domain',
 5315:                        'phone'      => 'Phone',
 5316:                        'cc'         => 'Cc e-mail',
 5317:                        'course'     => 'Course Details',
 5318:                        'section'    => 'Sections',
 5319:                        'screenshot' => 'File upload',
 5320:     );
 5321:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 5322:     my %possoptions = (
 5323:                         username     => ['yes','no','req'],
 5324:                         phone        => ['yes','no','req'],
 5325:                         user         => ['yes','no'],
 5326:                         cc           => ['yes','no'],
 5327:                         course       => ['yes','no'],
 5328:                         section      => ['yes','no'],
 5329:                         screenshot   => ['yes','no'],
 5330:                       );
 5331:     my %fieldoptions = &Apache::lonlocal::texthash (
 5332:                          'yes'  => 'Optional',
 5333:                          'req'  => 'Required',
 5334:                          'no'   => "Not shown",
 5335:     );
 5336:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 5337: }
 5338: 
 5339: sub tool_titles {
 5340:     my %titles = &Apache::lonlocal::texthash (
 5341:                      aboutme    => 'Personal web page',
 5342:                      blog       => 'Blog',
 5343:                      webdav     => 'WebDAV',
 5344:                      portfolio  => 'Portfolio',
 5345:                      official   => 'Official courses (with institutional codes)',
 5346:                      unofficial => 'Unofficial courses',
 5347:                      community  => 'Communities',
 5348:                      textbook   => 'Textbook courses',
 5349:                  );
 5350:     return %titles;
 5351: }
 5352: 
 5353: sub courserequest_titles {
 5354:     my %titles = &Apache::lonlocal::texthash (
 5355:                                    official   => 'Official',
 5356:                                    unofficial => 'Unofficial',
 5357:                                    community  => 'Communities',
 5358:                                    textbook   => 'Textbook',
 5359:                                    norequest  => 'Not allowed',
 5360:                                    approval   => 'Approval by Dom. Coord.',
 5361:                                    validate   => 'With validation',
 5362:                                    autolimit  => 'Numerical limit',
 5363:                                    unlimited  => '(blank for unlimited)',
 5364:                  );
 5365:     return %titles;
 5366: }
 5367: 
 5368: sub authorrequest_titles {
 5369:     my %titles = &Apache::lonlocal::texthash (
 5370:                                    norequest  => 'Not allowed',
 5371:                                    approval   => 'Approval by Dom. Coord.',
 5372:                                    automatic  => 'Automatic approval',
 5373:                  );
 5374:     return %titles;
 5375: }
 5376: 
 5377: sub courserequest_conditions {
 5378:     my %conditions = &Apache::lonlocal::texthash (
 5379:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 5380:        validate   => '(Processing of request subject to institutional validation).',
 5381:                  );
 5382:     return %conditions;
 5383: }
 5384: 
 5385: 
 5386: sub print_usercreation {
 5387:     my ($position,$dom,$settings,$rowtotal) = @_;
 5388:     my $numinrow = 4;
 5389:     my $datatable;
 5390:     if ($position eq 'top') {
 5391:         $$rowtotal ++;
 5392:         my $rowcount = 0;
 5393:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 5394:         if (ref($rules) eq 'HASH') {
 5395:             if (keys(%{$rules}) > 0) {
 5396:                 $datatable .= &user_formats_row('username',$settings,$rules,
 5397:                                                 $ruleorder,$numinrow,$rowcount);
 5398:                 $$rowtotal ++;
 5399:                 $rowcount ++;
 5400:             }
 5401:         }
 5402:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 5403:         if (ref($idrules) eq 'HASH') {
 5404:             if (keys(%{$idrules}) > 0) {
 5405:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 5406:                                                 $idruleorder,$numinrow,$rowcount);
 5407:                 $$rowtotal ++;
 5408:                 $rowcount ++;
 5409:             }
 5410:         }
 5411:         if ($rowcount == 0) {
 5412:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 5413:             $$rowtotal ++;
 5414:             $rowcount ++;
 5415:         }
 5416:     } elsif ($position eq 'middle') {
 5417:         my @creators = ('author','course','requestcrs');
 5418:         my ($rules,$ruleorder) =
 5419:             &Apache::lonnet::inst_userrules($dom,'username');
 5420:         my %lt = &usercreation_types();
 5421:         my %checked;
 5422:         if (ref($settings) eq 'HASH') {
 5423:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 5424:                 foreach my $item (@creators) {
 5425:                     $checked{$item} = $settings->{'cancreate'}{$item};
 5426:                 }
 5427:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 5428:                 foreach my $item (@creators) {
 5429:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 5430:                         $checked{$item} = 'none';
 5431:                     }
 5432:                 }
 5433:             }
 5434:         }
 5435:         my $rownum = 0;
 5436:         foreach my $item (@creators) {
 5437:             $rownum ++;
 5438:             if ($checked{$item} eq '') {
 5439:                 $checked{$item} = 'any';
 5440:             }
 5441:             my $css_class;
 5442:             if ($rownum%2) {
 5443:                 $css_class = '';
 5444:             } else {
 5445:                 $css_class = ' class="LC_odd_row" ';
 5446:             }
 5447:             $datatable .= '<tr'.$css_class.'>'.
 5448:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 5449:                          '</span></td><td align="right">';
 5450:             my @options = ('any');
 5451:             if (ref($rules) eq 'HASH') {
 5452:                 if (keys(%{$rules}) > 0) {
 5453:                     push(@options,('official','unofficial'));
 5454:                 }
 5455:             }
 5456:             push(@options,'none');
 5457:             foreach my $option (@options) {
 5458:                 my $type = 'radio';
 5459:                 my $check = ' ';
 5460:                 if ($checked{$item} eq $option) {
 5461:                     $check = ' checked="checked" ';
 5462:                 } 
 5463:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5464:                               '<input type="'.$type.'" name="can_createuser_'.
 5465:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 5466:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 5467:             }
 5468:             $datatable .= '</td></tr>';
 5469:         }
 5470:     } else {
 5471:         my @contexts = ('author','course','domain');
 5472:         my @authtypes = ('int','krb4','krb5','loc');
 5473:         my %checked;
 5474:         if (ref($settings) eq 'HASH') {
 5475:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 5476:                 foreach my $item (@contexts) {
 5477:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 5478:                         foreach my $auth (@authtypes) {
 5479:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 5480:                                 $checked{$item}{$auth} = ' checked="checked" ';
 5481:                             }
 5482:                         }
 5483:                     }
 5484:                 }
 5485:             }
 5486:         } else {
 5487:             foreach my $item (@contexts) {
 5488:                 foreach my $auth (@authtypes) {
 5489:                     $checked{$item}{$auth} = ' checked="checked" ';
 5490:                 }
 5491:             }
 5492:         }
 5493:         my %title = &context_names();
 5494:         my %authname = &authtype_names();
 5495:         my $rownum = 0;
 5496:         my $css_class; 
 5497:         foreach my $item (@contexts) {
 5498:             if ($rownum%2) {
 5499:                 $css_class = '';
 5500:             } else {
 5501:                 $css_class = ' class="LC_odd_row" ';
 5502:             }
 5503:             $datatable .=   '<tr'.$css_class.'>'.
 5504:                             '<td>'.$title{$item}.
 5505:                             '</td><td class="LC_left_item">'.
 5506:                             '<span class="LC_nobreak">';
 5507:             foreach my $auth (@authtypes) {
 5508:                 $datatable .= '<label>'. 
 5509:                               '<input type="checkbox" name="'.$item.'_auth" '.
 5510:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 5511:                               $authname{$auth}.'</label>&nbsp;';
 5512:             }
 5513:             $datatable .= '</span></td></tr>';
 5514:             $rownum ++;
 5515:         }
 5516:         $$rowtotal += $rownum;
 5517:     }
 5518:     return $datatable;
 5519: }
 5520: 
 5521: sub print_selfcreation {
 5522:     my ($position,$dom,$settings,$rowtotal) = @_;
 5523:     my (@selfcreate,$createsettings,$processing,$datatable);
 5524:     if (ref($settings) eq 'HASH') {
 5525:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 5526:             $createsettings = $settings->{'cancreate'};
 5527:             if (ref($createsettings) eq 'HASH') {
 5528:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 5529:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 5530:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 5531:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 5532:                         @selfcreate = ('email','login','sso');
 5533:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 5534:                         @selfcreate = ($createsettings->{'selfcreate'});
 5535:                     }
 5536:                 }
 5537:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 5538:                     $processing = $createsettings->{'selfcreateprocessing'};
 5539:                 }
 5540:             }
 5541:         }
 5542:     }
 5543:     my %radiohash;
 5544:     my $numinrow = 4;
 5545:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 5546:     if ($position eq 'top') {
 5547:         my %choices = &Apache::lonlocal::texthash (
 5548:                                                       cancreate_login      => 'Institutional Login',
 5549:                                                       cancreate_sso        => 'Institutional Single Sign On',
 5550:                                                   );
 5551:         my @toggles = sort(keys(%choices));
 5552:         my %defaultchecked = (
 5553:                                'cancreate_login' => 'off',
 5554:                                'cancreate_sso'   => 'off',
 5555:                              );
 5556:         my ($onclick,$itemcount);
 5557:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5558:                                                      \%choices,$itemcount,$onclick);
 5559:         $$rowtotal += $itemcount;
 5560:         
 5561:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5562: 
 5563:         if (ref($usertypes) eq 'HASH') {
 5564:             if (keys(%{$usertypes}) > 0) {
 5565:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 5566:                                              $dom,$numinrow,$othertitle,
 5567:                                              'statustocreate',$$rowtotal);
 5568:                 $$rowtotal ++;
 5569:             }
 5570:         }
 5571:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 5572:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5573:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 5574:         my $rem;
 5575:         my $numperrow = 2;
 5576:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 5577:         $datatable .= '<tr'.$css_class.'>'.
 5578:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 5579:                      '<td class="LC_left_item">'."\n".
 5580:                      '<table><tr><td>'."\n";
 5581:         for (my $i=0; $i<@fields; $i++) {
 5582:             $rem = $i%($numperrow);
 5583:             if ($rem == 0) {
 5584:                 if ($i > 0) {
 5585:                     $datatable .= '</tr>';
 5586:                 }
 5587:                 $datatable .= '<tr>';
 5588:             }
 5589:             my $currval;
 5590:             if (ref($createsettings) eq 'HASH') {
 5591:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 5592:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 5593:                 }
 5594:             }
 5595:             $datatable .= '<td class="LC_left_item">'.
 5596:                           '<span class="LC_nobreak">'.
 5597:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 5598:                           'value="'.$currval.'" size="10" />&nbsp;'.
 5599:                           $fieldtitles{$fields[$i]}.'</span></td>';
 5600:         }
 5601:         my $colsleft = $numperrow - $rem;
 5602:         if ($colsleft > 1 ) {
 5603:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5604:                          '&nbsp;</td>';
 5605:         } elsif ($colsleft == 1) {
 5606:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5607:         }
 5608:         $datatable .= '</tr></table></td></tr>';
 5609:         $$rowtotal ++;
 5610:     } elsif ($position eq 'middle') {
 5611:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 5612:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5613:         $usertypes->{'default'} = $othertitle;
 5614:         if (ref($types) eq 'ARRAY') {
 5615:             push(@{$types},'default');
 5616:             $usertypes->{'default'} = $othertitle;
 5617:             foreach my $status (@{$types}) {
 5618:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 5619:                                                        $numinrow,$$rowtotal,$usertypes);
 5620:                 $$rowtotal ++;
 5621:             }
 5622:         }
 5623:     } else {
 5624:         my %choices = &Apache::lonlocal::texthash (
 5625:                                                       cancreate_email => 'E-mail address as username',
 5626:                                                   );
 5627:         my @toggles = sort(keys(%choices));
 5628:         my %defaultchecked = (
 5629:                                'cancreate_email' => 'off',
 5630:                              );
 5631:         my $itemcount = 0;
 5632:         my $display = 'none';
 5633:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 5634:             $display = 'block';
 5635:         }
 5636:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 5637:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 5638:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5639:         my $usertypes = {};
 5640:         my $order = [];
 5641:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 5642:             $usertypes = $domdefaults{'inststatustypes'};
 5643:             $order = $domdefaults{'inststatusguest'};
 5644:         }
 5645:         if (ref($order) eq 'ARRAY') {
 5646:             push(@{$order},'default');
 5647:             if (@{$order} > 1) {
 5648:                 $usertypes->{'default'} = &mt('Other users');
 5649:                 $additional .= '<table><tr>';
 5650:                 foreach my $status (@{$order}) {
 5651:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 5652:                 }
 5653:                 $additional .= '</tr><tr>';
 5654:                 foreach my $status (@{$order}) {
 5655:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 5656:                 }
 5657:                 $additional .= '</tr></table>';
 5658:             } else {
 5659:                 $usertypes->{'default'} = &mt('All users');
 5660:                 $additional .= &email_as_username($rowtotal,$processing);
 5661:             }
 5662:         }
 5663:         $additional .= '</div>'."\n";
 5664: 
 5665:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5666:                                                      \%choices,$$rowtotal,$onclick,$additional);
 5667:         $$rowtotal ++;
 5668:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 5669:         $$rowtotal ++;
 5670:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 5671:         $numinrow = 1;
 5672:         if (ref($order) eq 'ARRAY') {
 5673:             foreach my $status (@{$order}) {
 5674:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 5675:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 5676:                 $$rowtotal ++;
 5677:             }
 5678:         }
 5679:         my ($emailrules,$emailruleorder) =
 5680:             &Apache::lonnet::inst_userrules($dom,'email');
 5681:         if (ref($emailrules) eq 'HASH') {
 5682:             if (keys(%{$emailrules}) > 0) {
 5683:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 5684:                                                 $emailruleorder,$numinrow,$$rowtotal);
 5685:                 $$rowtotal ++;
 5686:             }
 5687:         }
 5688:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 5689:     }
 5690:     return $datatable;
 5691: }
 5692: 
 5693: sub email_as_username {
 5694:     my ($rowtotal,$processing,$type) = @_;
 5695:     my %choices =
 5696:         &Apache::lonlocal::texthash (
 5697:                                       automatic => 'Automatic approval',
 5698:                                       approval  => 'Queued for approval',
 5699:                                     );
 5700:     my $output;
 5701:     foreach my $option ('automatic','approval') {
 5702:         my $checked;
 5703:         if (ref($processing) eq 'HASH') {
 5704:             if ($type eq '') {   
 5705:                 if (!exists($processing->{'default'})) {
 5706:                     if ($option eq 'automatic') {
 5707:                         $checked = ' checked="checked"';
 5708:                     }
 5709:                 } else {
 5710:                     if ($processing->{'default'} eq $option) {
 5711:                         $checked = ' checked="checked"';
 5712:                     }
 5713:                 }
 5714:             } else {
 5715:                 if (!exists($processing->{$type})) {
 5716:                     if ($option eq 'automatic') {
 5717:                         $checked = ' checked="checked"';
 5718:                     }
 5719:                 } else {
 5720:                     if ($processing->{$type} eq $option) {
 5721:                         $checked = ' checked="checked"';
 5722:                     }
 5723:                 }
 5724:             }
 5725:         } elsif ($option eq 'automatic') {
 5726:             $checked = ' checked="checked"'; 
 5727:         }
 5728:         my $name = 'cancreate_emailprocess';
 5729:         if (($type ne '') && ($type ne 'default')) {
 5730:             $name .= '_'.$type;
 5731:         }
 5732:         $output .= '<span class="LC_nobreak"><label>'.
 5733:                    '<input type="radio" name="'.$name.'"'.
 5734:                    $checked.' value="'.$option.'" />'.
 5735:                    $choices{$option}.'</label></span>';
 5736:         if ($type eq '') {
 5737:             $output .= '&nbsp;';
 5738:         } else {
 5739:             $output .= '<br />';
 5740:         }
 5741:     }
 5742:     $$rowtotal ++;
 5743:     return $output;
 5744: }
 5745: 
 5746: sub captcha_choice {
 5747:     my ($context,$settings,$itemcount) = @_;
 5748:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 5749:         $vertext,$currver); 
 5750:     my %lt = &captcha_phrases();
 5751:     $keyentry = 'hidden';
 5752:     if ($context eq 'cancreate') {
 5753:         $rowname = &mt('CAPTCHA validation');
 5754:     } elsif ($context eq 'login') {
 5755:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 5756:     }
 5757:     if (ref($settings) eq 'HASH') {
 5758:         if ($settings->{'captcha'}) {
 5759:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 5760:         } else {
 5761:             $checked{'original'} = ' checked="checked"';
 5762:         }
 5763:         if ($settings->{'captcha'} eq 'recaptcha') {
 5764:             $pubtext = $lt{'pub'};
 5765:             $privtext = $lt{'priv'};
 5766:             $keyentry = 'text';
 5767:             $vertext = $lt{'ver'};
 5768:             $currver = $settings->{'recaptchaversion'};
 5769:             if ($currver ne '2') {
 5770:                 $currver = 1;
 5771:             }
 5772:         }
 5773:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 5774:             $currpub = $settings->{'recaptchakeys'}{'public'};
 5775:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 5776:         }
 5777:     } else {
 5778:         $checked{'original'} = ' checked="checked"';
 5779:     }
 5780:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5781:     my $output = '<tr'.$css_class.'>'.
 5782:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 5783:                  '<table><tr><td>'."\n";
 5784:     foreach my $option ('original','recaptcha','notused') {
 5785:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 5786:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 5787:                    $lt{$option}.'</label></span>';
 5788:         unless ($option eq 'notused') {
 5789:             $output .= ('&nbsp;'x2)."\n";
 5790:         }
 5791:     }
 5792: #
 5793: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 5794: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 5795: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 5796: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 5797: #
 5798:     $output .= '</td></tr>'."\n".
 5799:                '<tr><td>'."\n".
 5800:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 5801:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 5802:                $currpub.'" size="40" /></span><br />'."\n".
 5803:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 5804:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 5805:                $currpriv.'" size="40" /></span><br />'.
 5806:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 5807:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 5808:                $currver.'" size="3" /></span><br />'.
 5809:                '</td></tr></table>'."\n".
 5810:                '</td></tr>';
 5811:     return $output;
 5812: }
 5813: 
 5814: sub user_formats_row {
 5815:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 5816:     my $output;
 5817:     my %text = (
 5818:                    'username' => 'new usernames',
 5819:                    'id'       => 'IDs',
 5820:                    'email'    => 'self-created accounts (e-mail)',
 5821:                );
 5822:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5823:     $output = '<tr '.$css_class.'>'.
 5824:               '<td><span class="LC_nobreak">';
 5825:     if ($type eq 'email') {
 5826:         $output .= &mt("Formats disallowed for $text{$type}: ");
 5827:     } else {
 5828:         $output .= &mt("Format rules to check for $text{$type}: ");
 5829:     }
 5830:     $output .= '</span></td>'.
 5831:                '<td class="LC_left_item" colspan="2"><table>';
 5832:     my $rem;
 5833:     if (ref($ruleorder) eq 'ARRAY') {
 5834:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 5835:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 5836:                 my $rem = $i%($numinrow);
 5837:                 if ($rem == 0) {
 5838:                     if ($i > 0) {
 5839:                         $output .= '</tr>';
 5840:                     }
 5841:                     $output .= '<tr>';
 5842:                 }
 5843:                 my $check = ' ';
 5844:                 if (ref($settings) eq 'HASH') {
 5845:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 5846:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 5847:                             $check = ' checked="checked" ';
 5848:                         }
 5849:                     }
 5850:                 }
 5851:                 $output .= '<td class="LC_left_item">'.
 5852:                            '<span class="LC_nobreak"><label>'.
 5853:                            '<input type="checkbox" name="'.$type.'_rule" '.
 5854:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 5855:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 5856:             }
 5857:         }
 5858:         $rem = @{$ruleorder}%($numinrow);
 5859:     }
 5860:     my $colsleft = $numinrow - $rem;
 5861:     if ($colsleft > 1 ) {
 5862:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5863:                    '&nbsp;</td>';
 5864:     } elsif ($colsleft == 1) {
 5865:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5866:     }
 5867:     $output .= '</tr></table></td></tr>';
 5868:     return $output;
 5869: }
 5870: 
 5871: sub usercreation_types {
 5872:     my %lt = &Apache::lonlocal::texthash (
 5873:                     author     => 'When adding a co-author',
 5874:                     course     => 'When adding a user to a course',
 5875:                     requestcrs => 'When requesting a course',
 5876:                     any        => 'Any',
 5877:                     official   => 'Institutional only ',
 5878:                     unofficial => 'Non-institutional only',
 5879:                     none       => 'None',
 5880:     );
 5881:     return %lt;
 5882: }
 5883: 
 5884: sub selfcreation_types {
 5885:     my %lt = &Apache::lonlocal::texthash (
 5886:                     selfcreate => 'User creates own account',
 5887:                     any        => 'Any',
 5888:                     official   => 'Institutional only ',
 5889:                     unofficial => 'Non-institutional only',
 5890:                     email      => 'E-mail address',
 5891:                     login      => 'Institutional Login',
 5892:                     sso        => 'SSO',
 5893:              );
 5894: }
 5895: 
 5896: sub authtype_names {
 5897:     my %lt = &Apache::lonlocal::texthash(
 5898:                       int    => 'Internal',
 5899:                       krb4   => 'Kerberos 4',
 5900:                       krb5   => 'Kerberos 5',
 5901:                       loc    => 'Local',
 5902:                   );
 5903:     return %lt;
 5904: }
 5905: 
 5906: sub context_names {
 5907:     my %context_title = &Apache::lonlocal::texthash(
 5908:        author => 'Creating users when an Author',
 5909:        course => 'Creating users when in a course',
 5910:        domain => 'Creating users when a Domain Coordinator',
 5911:     );
 5912:     return %context_title;
 5913: }
 5914: 
 5915: sub print_usermodification {
 5916:     my ($position,$dom,$settings,$rowtotal) = @_;
 5917:     my $numinrow = 4;
 5918:     my ($context,$datatable,$rowcount);
 5919:     if ($position eq 'top') {
 5920:         $rowcount = 0;
 5921:         $context = 'author'; 
 5922:         foreach my $role ('ca','aa') {
 5923:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 5924:                                                    $numinrow,$rowcount);
 5925:             $$rowtotal ++;
 5926:             $rowcount ++;
 5927:         }
 5928:     } elsif ($position eq 'bottom') {
 5929:         $context = 'course';
 5930:         $rowcount = 0;
 5931:         foreach my $role ('st','ep','ta','in','cr') {
 5932:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 5933:                                                    $numinrow,$rowcount);
 5934:             $$rowtotal ++;
 5935:             $rowcount ++;
 5936:         }
 5937:     }
 5938:     return $datatable;
 5939: }
 5940: 
 5941: sub print_defaults {
 5942:     my ($position,$dom,$settings,$rowtotal) = @_;
 5943:     my $rownum = 0;
 5944:     my ($datatable,$css_class,$titles);
 5945:     unless ($position eq 'bottom') {
 5946:         $titles = &defaults_titles($dom);
 5947:     }
 5948:     if ($position eq 'top') {
 5949:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 5950:                      'datelocale_def','portal_def');
 5951:         my %defaults;
 5952:         if (ref($settings) eq 'HASH') {
 5953:             %defaults = %{$settings};
 5954:         } else {
 5955:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 5956:             foreach my $item (@items) {
 5957:                 $defaults{$item} = $domdefaults{$item};
 5958:             }
 5959:         }
 5960:         foreach my $item (@items) {
 5961:             if ($rownum%2) {
 5962:                 $css_class = '';
 5963:             } else {
 5964:                 $css_class = ' class="LC_odd_row" ';
 5965:             }
 5966:             $datatable .= '<tr'.$css_class.'>'.
 5967:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 5968:                           '</span></td><td class="LC_right_item" colspan="3">';
 5969:             if ($item eq 'auth_def') {
 5970:                 my @authtypes = ('internal','krb4','krb5','localauth');
 5971:                 my %shortauth = (
 5972:                                  internal => 'int',
 5973:                                  krb4 => 'krb4',
 5974:                                  krb5 => 'krb5',
 5975:                                  localauth  => 'loc'
 5976:                                 );
 5977:                 my %authnames = &authtype_names();
 5978:                 foreach my $auth (@authtypes) {
 5979:                     my $checked = ' ';
 5980:                     if ($defaults{$item} eq $auth) {
 5981:                         $checked = ' checked="checked" ';
 5982:                     }
 5983:                     $datatable .= '<label><input type="radio" name="'.$item.
 5984:                                   '" value="'.$auth.'"'.$checked.'/>'.
 5985:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 5986:                 }
 5987:             } elsif ($item eq 'timezone_def') {
 5988:                 my $includeempty = 1;
 5989:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 5990:             } elsif ($item eq 'datelocale_def') {
 5991:                 my $includeempty = 1;
 5992:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 5993:             } elsif ($item eq 'lang_def') {
 5994:                 my $includeempty = 1;
 5995:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 5996:             } else {
 5997:                 my $size;
 5998:                 if ($item eq 'portal_def') {
 5999:                     $size = ' size="25"';
 6000:                 }
 6001:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6002:                               $defaults{$item}.'"'.$size.' />';
 6003:             }
 6004:             $datatable .= '</td></tr>';
 6005:             $rownum ++;
 6006:         }
 6007:     } elsif ($position eq 'middle') {
 6008:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 6009:         my %defaults;
 6010:         if (ref($settings) eq 'HASH') {
 6011:             %defaults = %{$settings};
 6012:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 6013:                 $defaults{'intauth_cost'} = 10;
 6014:             }
 6015:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 6016:                 $defaults{'intauth_check'} = 0;
 6017:             }
 6018:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 6019:                 $defaults{'intauth_switch'} = 0;
 6020:             }
 6021:         } else {
 6022:             %defaults = (
 6023:                           'intauth_cost'   => 10,
 6024:                           'intauth_check'  => 0,
 6025:                           'intauth_switch' => 0,
 6026:                         );
 6027:         }
 6028:         foreach my $item (@items) {
 6029:             if ($rownum%2) {
 6030:                 $css_class = '';
 6031:             } else {
 6032:                 $css_class = ' class="LC_odd_row" ';
 6033:             }
 6034:             $datatable .= '<tr'.$css_class.'>'.
 6035:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6036:                           '</span></td><td class="LC_left_item" colspan="3">';
 6037:             if ($item eq 'intauth_switch') {
 6038:                 my @options = (0,1,2);
 6039:                 my %optiondesc = &Apache::lonlocal::texthash (
 6040:                                    0 => 'No',
 6041:                                    1 => 'Yes',
 6042:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 6043:                                  );
 6044:                 $datatable .= '<table width="100%">';
 6045:                 foreach my $option (@options) {
 6046:                     my $checked = ' ';
 6047:                     if ($defaults{$item} eq $option) {
 6048:                         $checked = ' checked="checked"';
 6049:                     }
 6050:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6051:                                   '<label><input type="radio" name="'.$item.
 6052:                                   '" value="'.$option.'"'.$checked.' />'.
 6053:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6054:                 }
 6055:                 $datatable .= '</table>';
 6056:             } elsif ($item eq 'intauth_check') {
 6057:                 my @options = (0,1,2);
 6058:                 my %optiondesc = &Apache::lonlocal::texthash (
 6059:                                    0 => 'No',
 6060:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 6061:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 6062:                                  );
 6063:                 $datatable .= '<table wisth="100%">';
 6064:                 foreach my $option (@options) {
 6065:                     my $checked = ' ';
 6066:                     my $onclick;
 6067:                     if ($defaults{$item} eq $option) {
 6068:                         $checked = ' checked="checked"';
 6069:                     }
 6070:                     if ($option == 2) {
 6071:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 6072:                     }
 6073:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6074:                                   '<label><input type="radio" name="'.$item.
 6075:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 6076:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6077:                 }
 6078:                 $datatable .= '</table>';
 6079:             } else {
 6080:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6081:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 6082:             }
 6083:             $datatable .= '</td></tr>';
 6084:             $rownum ++;
 6085:         }
 6086:     } else {
 6087:         my %defaults;
 6088:         if (ref($settings) eq 'HASH') {
 6089:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 6090:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 6091:                 my $maxnum = @{$settings->{'inststatusorder'}};
 6092:                 for (my $i=0; $i<$maxnum; $i++) {
 6093:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 6094:                     my $item = $settings->{'inststatusorder'}->[$i];
 6095:                     my $title = $settings->{'inststatustypes'}->{$item};
 6096:                     my $guestok;
 6097:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 6098:                         $guestok = 1;
 6099:                     }
 6100:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 6101:                     $datatable .= '<tr'.$css_class.'>'.
 6102:                                   '<td><span class="LC_nobreak">'.
 6103:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 6104:                     for (my $k=0; $k<=$maxnum; $k++) {
 6105:                         my $vpos = $k+1;
 6106:                         my $selstr;
 6107:                         if ($k == $i) {
 6108:                             $selstr = ' selected="selected" ';
 6109:                         }
 6110:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6111:                     }
 6112:                     my ($checkedon,$checkedoff);
 6113:                     $checkedoff = ' checked="checked"';
 6114:                     if ($guestok) {
 6115:                         $checkedon = $checkedoff;
 6116:                         $checkedoff = ''; 
 6117:                     }
 6118:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 6119:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 6120:                                   &mt('delete').'</span></td>'.
 6121:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 6122:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 6123:                                   '</span></td>'.
 6124:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6125:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 6126:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 6127:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 6128:                                   &mt('No').'</label></span></td></tr>';
 6129:                 }
 6130:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 6131:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 6132:                 $datatable .= '<tr '.$css_class.'>'.
 6133:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 6134:                 for (my $k=0; $k<=$maxnum; $k++) {
 6135:                     my $vpos = $k+1;
 6136:                     my $selstr;
 6137:                     if ($k == $maxnum) {
 6138:                         $selstr = ' selected="selected" ';
 6139:                     }
 6140:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6141:                 }
 6142:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 6143:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 6144:                               '&nbsp;'.&mt('(new)').
 6145:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 6146:                               &mt('Name displayed:').
 6147:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 6148:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 6149:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 6150:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 6151:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 6152:                               &mt('No').'</label></span></td></tr>';
 6153:                               '</tr>'."\n";
 6154:                 $rownum ++;
 6155:             }
 6156:         }
 6157:     }
 6158:     $$rowtotal += $rownum;
 6159:     return $datatable;
 6160: }
 6161: 
 6162: sub get_languages_hash {
 6163:     my %langchoices;
 6164:     foreach my $id (&Apache::loncommon::languageids()) {
 6165:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 6166:         if ($code ne '') {
 6167:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 6168:         }
 6169:     }
 6170:     return %langchoices;
 6171: }
 6172: 
 6173: sub defaults_titles {
 6174:     my ($dom) = @_;
 6175:     my %titles = &Apache::lonlocal::texthash (
 6176:                    'auth_def'      => 'Default authentication type',
 6177:                    'auth_arg_def'  => 'Default authentication argument',
 6178:                    'lang_def'      => 'Default language',
 6179:                    'timezone_def'  => 'Default timezone',
 6180:                    'datelocale_def' => 'Default locale for dates',
 6181:                    'portal_def'     => 'Portal/Default URL',
 6182:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 6183:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 6184:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 6185:                  );
 6186:     if ($dom) {
 6187:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 6188:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 6189:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 6190:         $protocol = 'http' if ($protocol ne 'https');
 6191:         if ($uint_dom) {
 6192:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 6193:                                          $uint_dom);
 6194:         }
 6195:     }
 6196:     return (\%titles);
 6197: }
 6198: 
 6199: sub print_scantronformat {
 6200:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 6201:     my $itemcount = 1;
 6202:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 6203:         %confhash);
 6204:     my $switchserver = &check_switchserver($dom,$confname);
 6205:     my %lt = &Apache::lonlocal::texthash (
 6206:                 default => 'Default bubblesheet format file error',
 6207:                 custom  => 'Custom bubblesheet format file error',
 6208:              );
 6209:     my %scantronfiles = (
 6210:         default => 'default.tab',
 6211:         custom => 'custom.tab',
 6212:     );
 6213:     foreach my $key (keys(%scantronfiles)) {
 6214:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 6215:                               .$scantronfiles{$key};
 6216:     }
 6217:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 6218:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 6219:         if (!$switchserver) {
 6220:             my $servadm = $r->dir_config('lonAdmEMail');
 6221:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 6222:             if ($configuserok eq 'ok') {
 6223:                 if ($author_ok eq 'ok') {
 6224:                     my %legacyfile = (
 6225:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 6226:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 6227:                     );
 6228:                     my %md5chk;
 6229:                     foreach my $type (keys(%legacyfile)) {
 6230:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 6231:                         chomp($md5chk{$type});
 6232:                     }
 6233:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 6234:                         foreach my $type (keys(%legacyfile)) {
 6235:                             ($scantronurls{$type},my $error) = 
 6236:                                 &legacy_scantronformat($r,$dom,$confname,
 6237:                                                  $type,$legacyfile{$type},
 6238:                                                  $scantronurls{$type},
 6239:                                                  $scantronfiles{$type});
 6240:                             if ($error ne '') {
 6241:                                 $error{$type} = $error;
 6242:                             }
 6243:                         }
 6244:                         if (keys(%error) == 0) {
 6245:                             $is_custom = 1;
 6246:                             $confhash{'scantron'}{'scantronformat'} = 
 6247:                                 $scantronurls{'custom'};
 6248:                             my $putresult = 
 6249:                                 &Apache::lonnet::put_dom('configuration',
 6250:                                                          \%confhash,$dom);
 6251:                             if ($putresult ne 'ok') {
 6252:                                 $error{'custom'} = 
 6253:                                     '<span class="LC_error">'.
 6254:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6255:                             }
 6256:                         }
 6257:                     } else {
 6258:                         ($scantronurls{'default'},my $error) =
 6259:                             &legacy_scantronformat($r,$dom,$confname,
 6260:                                           'default',$legacyfile{'default'},
 6261:                                           $scantronurls{'default'},
 6262:                                           $scantronfiles{'default'});
 6263:                         if ($error eq '') {
 6264:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 6265:                             my $putresult =
 6266:                                 &Apache::lonnet::put_dom('configuration',
 6267:                                                          \%confhash,$dom);
 6268:                             if ($putresult ne 'ok') {
 6269:                                 $error{'default'} =
 6270:                                     '<span class="LC_error">'.
 6271:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6272:                             }
 6273:                         } else {
 6274:                             $error{'default'} = $error;
 6275:                         }
 6276:                     }
 6277:                 }
 6278:             }
 6279:         } else {
 6280:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 6281:         }
 6282:     }
 6283:     if (ref($settings) eq 'HASH') {
 6284:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 6285:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 6286:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 6287:                 $scantronurl = '';
 6288:             } else {
 6289:                 $scantronurl = $settings->{'scantronformat'};
 6290:             }
 6291:             $is_custom = 1;
 6292:         } else {
 6293:             $scantronurl = $scantronurls{'default'};
 6294:         }
 6295:     } else {
 6296:         if ($is_custom) {
 6297:             $scantronurl = $scantronurls{'custom'};
 6298:         } else {
 6299:             $scantronurl = $scantronurls{'default'};
 6300:         }
 6301:     }
 6302:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6303:     $datatable .= '<tr'.$css_class.'>';
 6304:     if (!$is_custom) {
 6305:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 6306:                       '<span class="LC_nobreak">';
 6307:         if ($scantronurl) {
 6308:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 6309:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 6310:         } else {
 6311:             $datatable = &mt('File unavailable for display');
 6312:         }
 6313:         $datatable .= '</span></td>';
 6314:         if (keys(%error) == 0) { 
 6315:             $datatable .= '<td valign="bottom">';
 6316:             if (!$switchserver) {
 6317:                 $datatable .= &mt('Upload:').'<br />';
 6318:             }
 6319:         } else {
 6320:             my $errorstr;
 6321:             foreach my $key (sort(keys(%error))) {
 6322:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6323:             }
 6324:             $datatable .= '<td>'.$errorstr;
 6325:         }
 6326:     } else {
 6327:         if (keys(%error) > 0) {
 6328:             my $errorstr;
 6329:             foreach my $key (sort(keys(%error))) {
 6330:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6331:             } 
 6332:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 6333:         } elsif ($scantronurl) {
 6334:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 6335:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 6336:             $datatable .= '<td><span class="LC_nobreak">'.
 6337:                           $link.
 6338:                           '<label><input type="checkbox" name="scantronformat_del"'.
 6339:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 6340:                           '<td><span class="LC_nobreak">&nbsp;'.
 6341:                           &mt('Replace:').'</span><br />';
 6342:         }
 6343:     }
 6344:     if (keys(%error) == 0) {
 6345:         if ($switchserver) {
 6346:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 6347:         } else {
 6348:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 6349:                          '<input type="file" name="scantronformat" /></span>';
 6350:         }
 6351:     }
 6352:     $datatable .= '</td></tr>';
 6353:     $$rowtotal ++;
 6354:     return $datatable;
 6355: }
 6356: 
 6357: sub legacy_scantronformat {
 6358:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 6359:     my ($url,$error);
 6360:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 6361:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 6362:         (my $result,$url) =
 6363:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 6364:                          '','',$newfile);
 6365:         if ($result ne 'ok') {
 6366:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 6367:         }
 6368:     }
 6369:     return ($url,$error);
 6370: }
 6371: 
 6372: sub print_coursecategories {
 6373:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 6374:     my $datatable;
 6375:     if ($position eq 'top') {
 6376:         my (%checked);
 6377:         my @catitems = ('unauth','auth');
 6378:         my @cattypes = ('std','domonly','codesrch','none');
 6379:         $checked{'unauth'} = 'std';
 6380:         $checked{'auth'} = 'std';
 6381:         if (ref($settings) eq 'HASH') {
 6382:             foreach my $type (@cattypes) {
 6383:                 if ($type eq $settings->{'unauth'}) {
 6384:                     $checked{'unauth'} = $type;
 6385:                 }
 6386:                 if ($type eq $settings->{'auth'}) {
 6387:                     $checked{'auth'} = $type;
 6388:                 }
 6389:             }
 6390:         }
 6391:         my %lt = &Apache::lonlocal::texthash (
 6392:                                                unauth   => 'Catalog type for unauthenticated users',
 6393:                                                auth     => 'Catalog type for authenticated users',
 6394:                                                none     => 'No catalog',
 6395:                                                std      => 'Standard catalog',
 6396:                                                domonly  => 'Domain-only catalog',
 6397:                                                codesrch => "Code search form",
 6398:                                              );
 6399:        my $itemcount = 0;
 6400:        foreach my $item (@catitems) {
 6401:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 6402:            $datatable .= '<tr '.$css_class.'>'.
 6403:                          '<td>'.$lt{$item}.'</td>'.
 6404:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 6405:            foreach my $type (@cattypes) {
 6406:                my $ischecked;
 6407:                if ($checked{$item} eq $type) {
 6408:                    $ischecked=' checked="checked"';
 6409:                }
 6410:                $datatable .= '<label>'.
 6411:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 6412:                              ' />'.$lt{$type}.'</label>&nbsp;';
 6413:            }
 6414:            $datatable .= '</td></tr>';
 6415:            $itemcount ++;
 6416:         }
 6417:         $$rowtotal += $itemcount;
 6418:     } elsif ($position eq 'middle') {
 6419:         my $toggle_cats_crs = ' ';
 6420:         my $toggle_cats_dom = ' checked="checked" ';
 6421:         my $can_cat_crs = ' ';
 6422:         my $can_cat_dom = ' checked="checked" ';
 6423:         my $toggle_catscomm_comm = ' ';
 6424:         my $toggle_catscomm_dom = ' checked="checked" ';
 6425:         my $can_catcomm_comm = ' ';
 6426:         my $can_catcomm_dom = ' checked="checked" ';
 6427: 
 6428:         if (ref($settings) eq 'HASH') {
 6429:             if ($settings->{'togglecats'} eq 'crs') {
 6430:                 $toggle_cats_crs = $toggle_cats_dom;
 6431:                 $toggle_cats_dom = ' ';
 6432:             }
 6433:             if ($settings->{'categorize'} eq 'crs') {
 6434:                 $can_cat_crs = $can_cat_dom;
 6435:                 $can_cat_dom = ' ';
 6436:             }
 6437:             if ($settings->{'togglecatscomm'} eq 'comm') {
 6438:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 6439:                 $toggle_catscomm_dom = ' ';
 6440:             }
 6441:             if ($settings->{'categorizecomm'} eq 'comm') {
 6442:                 $can_catcomm_comm = $can_catcomm_dom;
 6443:                 $can_catcomm_dom = ' ';
 6444:             }
 6445:         }
 6446:         my %title = &Apache::lonlocal::texthash (
 6447:                      togglecats     => 'Show/Hide a course in catalog',
 6448:                      togglecatscomm => 'Show/Hide a community in catalog',
 6449:                      categorize     => 'Assign a category to a course',
 6450:                      categorizecomm => 'Assign a category to a community',
 6451:                     );
 6452:         my %level = &Apache::lonlocal::texthash (
 6453:                      dom  => 'Set in Domain',
 6454:                      crs  => 'Set in Course',
 6455:                      comm => 'Set in Community',
 6456:                     );
 6457:         $datatable = '<tr class="LC_odd_row">'.
 6458:                   '<td>'.$title{'togglecats'}.'</td>'.
 6459:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6460:                   '<input type="radio" name="togglecats"'.
 6461:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6462:                   '<label><input type="radio" name="togglecats"'.
 6463:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6464:                   '</tr><tr>'.
 6465:                   '<td>'.$title{'categorize'}.'</td>'.
 6466:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6467:                   '<label><input type="radio" name="categorize"'.
 6468:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6469:                   '<label><input type="radio" name="categorize"'.
 6470:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6471:                   '</tr><tr class="LC_odd_row">'.
 6472:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 6473:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6474:                   '<input type="radio" name="togglecatscomm"'.
 6475:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6476:                   '<label><input type="radio" name="togglecatscomm"'.
 6477:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6478:                   '</tr><tr>'.
 6479:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 6480:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6481:                   '<label><input type="radio" name="categorizecomm"'.
 6482:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6483:                   '<label><input type="radio" name="categorizecomm"'.
 6484:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6485:                   '</tr>';
 6486:         $$rowtotal += 4;
 6487:     } else {
 6488:         my $css_class;
 6489:         my $itemcount = 1;
 6490:         my $cathash; 
 6491:         if (ref($settings) eq 'HASH') {
 6492:             $cathash = $settings->{'cats'};
 6493:         }
 6494:         if (ref($cathash) eq 'HASH') {
 6495:             my (@cats,@trails,%allitems,%idx,@jsarray);
 6496:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 6497:                                                    \%allitems,\%idx,\@jsarray);
 6498:             my $maxdepth = scalar(@cats);
 6499:             my $colattrib = '';
 6500:             if ($maxdepth > 2) {
 6501:                 $colattrib = ' colspan="2" ';
 6502:             }
 6503:             my @path;
 6504:             if (@cats > 0) {
 6505:                 if (ref($cats[0]) eq 'ARRAY') {
 6506:                     my $numtop = @{$cats[0]};
 6507:                     my $maxnum = $numtop;
 6508:                     my %default_names = (
 6509:                           instcode    => &mt('Official courses'),
 6510:                           communities => &mt('Communities'),
 6511:                     );
 6512: 
 6513:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 6514:                         ($cathash->{'instcode::0'} eq '') ||
 6515:                         (!grep(/^communities$/,@{$cats[0]})) || 
 6516:                         ($cathash->{'communities::0'} eq '')) {
 6517:                         $maxnum ++;
 6518:                     }
 6519:                     my $lastidx;
 6520:                     for (my $i=0; $i<$numtop; $i++) {
 6521:                         my $parent = $cats[0][$i];
 6522:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6523:                         my $item = &escape($parent).'::0';
 6524:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 6525:                         $lastidx = $idx{$item};
 6526:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6527:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 6528:                         for (my $k=0; $k<=$maxnum; $k++) {
 6529:                             my $vpos = $k+1;
 6530:                             my $selstr;
 6531:                             if ($k == $i) {
 6532:                                 $selstr = ' selected="selected" ';
 6533:                             }
 6534:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6535:                         }
 6536:                         $datatable .= '</select></span></td><td>';
 6537:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 6538:                             $datatable .=  '<span class="LC_nobreak">'
 6539:                                            .$default_names{$parent}.'</span>';
 6540:                             if ($parent eq 'instcode') {
 6541:                                 $datatable .= '<br /><span class="LC_nobreak">('
 6542:                                               .&mt('with institutional codes')
 6543:                                               .')</span></td><td'.$colattrib.'>';
 6544:                             } else {
 6545:                                 $datatable .= '<table><tr><td>';
 6546:                             }
 6547:                             $datatable .= '<span class="LC_nobreak">'
 6548:                                           .'<label><input type="radio" name="'
 6549:                                           .$parent.'" value="1" checked="checked" />'
 6550:                                           .&mt('Display').'</label>';
 6551:                             if ($parent eq 'instcode') {
 6552:                                 $datatable .= '&nbsp;';
 6553:                             } else {
 6554:                                 $datatable .= '</span></td></tr><tr><td>'
 6555:                                               .'<span class="LC_nobreak">';
 6556:                             }
 6557:                             $datatable .= '<label><input type="radio" name="'
 6558:                                           .$parent.'" value="0" />'
 6559:                                           .&mt('Do not display').'</label></span>';
 6560:                             if ($parent eq 'communities') {
 6561:                                 $datatable .= '</td></tr></table>';
 6562:                             }
 6563:                             $datatable .= '</td>';
 6564:                         } else {
 6565:                             $datatable .= $parent
 6566:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 6567:                                           .'<input type="checkbox" name="deletecategory" '
 6568:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 6569:                         }
 6570:                         my $depth = 1;
 6571:                         push(@path,$parent);
 6572:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 6573:                         pop(@path);
 6574:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 6575:                         $itemcount ++;
 6576:                     }
 6577:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6578:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 6579:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 6580:                     for (my $k=0; $k<=$maxnum; $k++) {
 6581:                         my $vpos = $k+1;
 6582:                         my $selstr;
 6583:                         if ($k == $numtop) {
 6584:                             $selstr = ' selected="selected" ';
 6585:                         }
 6586:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6587:                     }
 6588:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 6589:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 6590:                                   .'</tr>'."\n";
 6591:                     $itemcount ++;
 6592:                     foreach my $default ('instcode','communities') {
 6593:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 6594:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6595:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 6596:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 6597:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 6598:                             for (my $k=0; $k<=$maxnum; $k++) {
 6599:                                 my $vpos = $k+1;
 6600:                                 my $selstr;
 6601:                                 if ($k == $maxnum) {
 6602:                                     $selstr = ' selected="selected" ';
 6603:                                 }
 6604:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6605:                             }
 6606:                             $datatable .= '</select></span></td>'.
 6607:                                           '<td><span class="LC_nobreak">'.
 6608:                                           $default_names{$default}.'</span>';
 6609:                             if ($default eq 'instcode') {
 6610:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 6611:                                               .&mt('with institutional codes').')</span>';
 6612:                             }
 6613:                             $datatable .= '</td>'
 6614:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 6615:                                           .&mt('Display').'</label>&nbsp;'
 6616:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 6617:                                           .&mt('Do not display').'</label></span></td></tr>';
 6618:                         }
 6619:                     }
 6620:                 }
 6621:             } else {
 6622:                 $datatable .= &initialize_categories($itemcount);
 6623:             }
 6624:         } else {
 6625:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 6626:                           .&initialize_categories($itemcount);
 6627:         }
 6628:         $$rowtotal += $itemcount;
 6629:     }
 6630:     return $datatable;
 6631: }
 6632: 
 6633: sub print_serverstatuses {
 6634:     my ($dom,$settings,$rowtotal) = @_;
 6635:     my $datatable;
 6636:     my @pages = &serverstatus_pages();
 6637:     my (%namedaccess,%machineaccess);
 6638:     foreach my $type (@pages) {
 6639:         $namedaccess{$type} = '';
 6640:         $machineaccess{$type}= '';
 6641:     }
 6642:     if (ref($settings) eq 'HASH') {
 6643:         foreach my $type (@pages) {
 6644:             if (exists($settings->{$type})) {
 6645:                 if (ref($settings->{$type}) eq 'HASH') {
 6646:                     foreach my $key (keys(%{$settings->{$type}})) {
 6647:                         if ($key eq 'namedusers') {
 6648:                             $namedaccess{$type} = $settings->{$type}->{$key};
 6649:                         } elsif ($key eq 'machines') {
 6650:                             $machineaccess{$type} = $settings->{$type}->{$key};
 6651:                         }
 6652:                     }
 6653:                 }
 6654:             }
 6655:         }
 6656:     }
 6657:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 6658:     my $rownum = 0;
 6659:     my $css_class;
 6660:     foreach my $type (@pages) {
 6661:         $rownum ++;
 6662:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 6663:         $datatable .= '<tr'.$css_class.'>'.
 6664:                       '<td><span class="LC_nobreak">'.
 6665:                       $titles->{$type}.'</span></td>'.
 6666:                       '<td class="LC_left_item">'.
 6667:                       '<input type="text" name="'.$type.'_namedusers" '.
 6668:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 6669:                       '<td class="LC_right_item">'.
 6670:                       '<span class="LC_nobreak">'.
 6671:                       '<input type="text" name="'.$type.'_machines" '.
 6672:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 6673:                       '</td></tr>'."\n";
 6674:     }
 6675:     $$rowtotal += $rownum;
 6676:     return $datatable;
 6677: }
 6678: 
 6679: sub serverstatus_pages {
 6680:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 6681:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 6682:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 6683:             'uniquecodes','diskusage','coursecatalog');
 6684: }
 6685: 
 6686: sub defaults_javascript {
 6687:     my ($settings) = @_;
 6688:     my $intauthcheck = &mt('Warning: disallowing login for an authenticated user if the stored cost is less than the default will require a password reset by/for the user.');
 6689:     my $intauthcost = &mt('Warning: bcrypt encryption cost for internal authentication must be an integer.');
 6690:     &js_escape(\$intauthcheck);
 6691:     &js_escape(\$intauthcost);
 6692:     my $intauthjs = <<"ENDSCRIPT";
 6693: 
 6694: function warnIntAuth(field) {
 6695:     if (field.name == 'intauth_check') {
 6696:         if (field.value == '2') {
 6697:             alert('$intauthcheck');
 6698:         }
 6699:     }
 6700:     if (field.name == 'intauth_cost') {
 6701:         field.value.replace(/\s/g,'');
 6702:         if (field.value != '') {
 6703:             var regexdigit=/^\\d+\$/;
 6704:             if (!regexdigit.test(field.value)) {
 6705:                 alert('$intauthcost');
 6706:             }
 6707:         }
 6708:     }
 6709:     return;
 6710: }
 6711: 
 6712: ENDSCRIPT
 6713: 
 6714:     if (ref($settings) ne 'HASH') {
 6715:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 6716:     }
 6717:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 6718:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 6719:         if ($maxnum eq '') {
 6720:             $maxnum = 0;
 6721:         }
 6722:         $maxnum ++;
 6723:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 6724:         return <<"ENDSCRIPT";
 6725: <script type="text/javascript">
 6726: // <![CDATA[
 6727: function reorderTypes(form,caller) {
 6728:     var changedVal;
 6729: $jstext 
 6730:     var newpos = 'addinststatus_pos';
 6731:     var current = new Array;
 6732:     var maxh = $maxnum;
 6733:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6734:     var oldVal;
 6735:     if (caller == newpos) {
 6736:         changedVal = newitemVal;
 6737:     } else {
 6738:         var curritem = 'inststatus_pos_'+caller;
 6739:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 6740:         current[newitemVal] = newpos;
 6741:     }
 6742:     for (var i=0; i<inststatuses.length; i++) {
 6743:         if (inststatuses[i] != caller) {
 6744:             var elementName = 'inststatus_pos_'+inststatuses[i];
 6745:             if (form.elements[elementName]) {
 6746:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 6747:                 current[currVal] = elementName;
 6748:             }
 6749:         }
 6750:     }
 6751:     for (var j=0; j<maxh; j++) {
 6752:         if (current[j] == undefined) {
 6753:             oldVal = j;
 6754:         }
 6755:     }
 6756:     if (oldVal < changedVal) {
 6757:         for (var k=oldVal+1; k<=changedVal ; k++) {
 6758:            var elementName = current[k];
 6759:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 6760:         }
 6761:     } else {
 6762:         for (var k=changedVal; k<oldVal; k++) {
 6763:             var elementName = current[k];
 6764:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 6765:         }
 6766:     }
 6767:     return;
 6768: }
 6769: 
 6770: $intauthjs
 6771: 
 6772: // ]]>
 6773: </script>
 6774: 
 6775: ENDSCRIPT
 6776:     } else {
 6777:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 6778:     }
 6779: }
 6780: 
 6781: sub coursecategories_javascript {
 6782:     my ($settings) = @_;
 6783:     my ($output,$jstext,$cathash);
 6784:     if (ref($settings) eq 'HASH') {
 6785:         $cathash = $settings->{'cats'};
 6786:     }
 6787:     if (ref($cathash) eq 'HASH') {
 6788:         my (@cats,@jsarray,%idx);
 6789:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 6790:         if (@jsarray > 0) {
 6791:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 6792:             for (my $i=0; $i<@jsarray; $i++) {
 6793:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 6794:                     my $catstr = join('","',@{$jsarray[$i]});
 6795:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 6796:                 }
 6797:             }
 6798:         }
 6799:     } else {
 6800:         $jstext  = '    var categories = Array(1);'."\n".
 6801:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 6802:     }
 6803:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 6804:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 6805:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 6806:     &js_escape(\$instcode_reserved);
 6807:     &js_escape(\$communities_reserved);
 6808:     &js_escape(\$choose_again);
 6809:     $output = <<"ENDSCRIPT";
 6810: <script type="text/javascript">
 6811: // <![CDATA[
 6812: function reorderCats(form,parent,item,idx) {
 6813:     var changedVal;
 6814: $jstext
 6815:     var newpos = 'addcategory_pos';
 6816:     if (parent == '') {
 6817:         var has_instcode = 0;
 6818:         var maxtop = categories[idx].length;
 6819:         for (var j=0; j<maxtop; j++) {
 6820:             if (categories[idx][j] == 'instcode::0') {
 6821:                 has_instcode == 1;
 6822:             }
 6823:         }
 6824:         if (has_instcode == 0) {
 6825:             categories[idx][maxtop] = 'instcode_pos';
 6826:         }
 6827:     } else {
 6828:         newpos += '_'+parent;
 6829:     }
 6830:     var maxh = 1 + categories[idx].length;
 6831:     var current = new Array;
 6832:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6833:     if (item == newpos) {
 6834:         changedVal = newitemVal;
 6835:     } else {
 6836:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 6837:         current[newitemVal] = newpos;
 6838:     }
 6839:     for (var i=0; i<categories[idx].length; i++) {
 6840:         var elementName = categories[idx][i];
 6841:         if (elementName != item) {
 6842:             if (form.elements[elementName]) {
 6843:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 6844:                 current[currVal] = elementName;
 6845:             }
 6846:         }
 6847:     }
 6848:     var oldVal;
 6849:     for (var j=0; j<maxh; j++) {
 6850:         if (current[j] == undefined) {
 6851:             oldVal = j;
 6852:         }
 6853:     }
 6854:     if (oldVal < changedVal) {
 6855:         for (var k=oldVal+1; k<=changedVal ; k++) {
 6856:            var elementName = current[k];
 6857:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 6858:         }
 6859:     } else {
 6860:         for (var k=changedVal; k<oldVal; k++) {
 6861:             var elementName = current[k];
 6862:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 6863:         }
 6864:     }
 6865:     return;
 6866: }
 6867: 
 6868: function categoryCheck(form) {
 6869:     if (form.elements['addcategory_name'].value == 'instcode') {
 6870:         alert('$instcode_reserved\\n$choose_again');
 6871:         return false;
 6872:     }
 6873:     if (form.elements['addcategory_name'].value == 'communities') {
 6874:         alert('$communities_reserved\\n$choose_again');
 6875:         return false;
 6876:     }
 6877:     return true;
 6878: }
 6879: 
 6880: // ]]>
 6881: </script>
 6882: 
 6883: ENDSCRIPT
 6884:     return $output;
 6885: }
 6886: 
 6887: sub initialize_categories {
 6888:     my ($itemcount) = @_;
 6889:     my ($datatable,$css_class,$chgstr);
 6890:     my %default_names = (
 6891:                       instcode    => 'Official courses (with institutional codes)',
 6892:                       communities => 'Communities',
 6893:                         );
 6894:     my $select0 = ' selected="selected"';
 6895:     my $select1 = '';
 6896:     foreach my $default ('instcode','communities') {
 6897:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6898:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 6899:         if ($default eq 'communities') {
 6900:             $select1 = $select0;
 6901:             $select0 = '';
 6902:         }
 6903:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6904:                      .'<select name="'.$default.'_pos">'
 6905:                      .'<option value="0"'.$select0.'>1</option>'
 6906:                      .'<option value="1"'.$select1.'>2</option>'
 6907:                      .'<option value="2">3</option></select>&nbsp;'
 6908:                      .$default_names{$default}
 6909:                      .'</span></td><td><span class="LC_nobreak">'
 6910:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 6911:                      .&mt('Display').'</label>&nbsp;<label>'
 6912:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 6913:                  .'</label></span></td></tr>';
 6914:         $itemcount ++;
 6915:     }
 6916:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6917:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 6918:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6919:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 6920:                   .'<option value="0">1</option>'
 6921:                   .'<option value="1">2</option>'
 6922:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 6923:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 6924:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 6925:     return $datatable;
 6926: }
 6927: 
 6928: sub build_category_rows {
 6929:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 6930:     my ($text,$name,$item,$chgstr);
 6931:     if (ref($cats) eq 'ARRAY') {
 6932:         my $maxdepth = scalar(@{$cats});
 6933:         if (ref($cats->[$depth]) eq 'HASH') {
 6934:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 6935:                 my $numchildren = @{$cats->[$depth]{$parent}};
 6936:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6937:                 $text .= '<td><table class="LC_data_table">';
 6938:                 my ($idxnum,$parent_name,$parent_item);
 6939:                 my $higher = $depth - 1;
 6940:                 if ($higher == 0) {
 6941:                     $parent_name = &escape($parent).'::'.$higher;
 6942:                 } else {
 6943:                     if (ref($path) eq 'ARRAY') {
 6944:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 6945:                     }
 6946:                 }
 6947:                 $parent_item = 'addcategory_pos_'.$parent_name;
 6948:                 for (my $j=0; $j<=$numchildren; $j++) {
 6949:                     if ($j < $numchildren) {
 6950:                         $name = $cats->[$depth]{$parent}[$j];
 6951:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 6952:                         $idxnum = $idx->{$item};
 6953:                     } else {
 6954:                         $name = $parent_name;
 6955:                         $item = $parent_item;
 6956:                     }
 6957:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 6958:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 6959:                     for (my $i=0; $i<=$numchildren; $i++) {
 6960:                         my $vpos = $i+1;
 6961:                         my $selstr;
 6962:                         if ($j == $i) {
 6963:                             $selstr = ' selected="selected" ';
 6964:                         }
 6965:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 6966:                     }
 6967:                     $text .= '</select>&nbsp;';
 6968:                     if ($j < $numchildren) {
 6969:                         my $deeper = $depth+1;
 6970:                         $text .= $name.'&nbsp;'
 6971:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 6972:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 6973:                         if(ref($path) eq 'ARRAY') {
 6974:                             push(@{$path},$name);
 6975:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 6976:                             pop(@{$path});
 6977:                         }
 6978:                     } else {
 6979:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 6980:                         if ($j == $numchildren) {
 6981:                             $text .= $name;
 6982:                         } else {
 6983:                             $text .= $item;
 6984:                         }
 6985:                         $text .= '" value="" />';
 6986:                     }
 6987:                     $text .= '</td></tr>';
 6988:                 }
 6989:                 $text .= '</table></td>';
 6990:             } else {
 6991:                 my $higher = $depth-1;
 6992:                 if ($higher == 0) {
 6993:                     $name = &escape($parent).'::'.$higher;
 6994:                 } else {
 6995:                     if (ref($path) eq 'ARRAY') {
 6996:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 6997:                     }
 6998:                 }
 6999:                 my $colspan;
 7000:                 if ($parent ne 'instcode') {
 7001:                     $colspan = $maxdepth - $depth - 1;
 7002:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 7003:                 }
 7004:             }
 7005:         }
 7006:     }
 7007:     return $text;
 7008: }
 7009: 
 7010: sub modifiable_userdata_row {
 7011:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 7012:     my ($role,$rolename,$statustype);
 7013:     $role = $item;
 7014:     if ($context eq 'cancreate') {
 7015:         if ($item =~ /^emailusername_(.+)$/) {
 7016:             $statustype = $1;
 7017:             $role = 'emailusername';
 7018:             if (ref($usertypes) eq 'HASH') {
 7019:                 if ($usertypes->{$statustype}) {
 7020:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 7021:                 } else {
 7022:                     $rolename = &mt('Data provided by user');
 7023:                 }
 7024:             }
 7025:         }
 7026:     } elsif ($context eq 'selfcreate') {
 7027:         if (ref($usertypes) eq 'HASH') {
 7028:             $rolename = $usertypes->{$role};
 7029:         } else {
 7030:             $rolename = $role;
 7031:         }
 7032:     } else {
 7033:         if ($role eq 'cr') {
 7034:             $rolename = &mt('Custom role');
 7035:         } else {
 7036:             $rolename = &Apache::lonnet::plaintext($role);
 7037:         }
 7038:     }
 7039:     my (@fields,%fieldtitles);
 7040:     if (ref($fieldsref) eq 'ARRAY') {
 7041:         @fields = @{$fieldsref};
 7042:     } else {
 7043:         @fields = ('lastname','firstname','middlename','generation',
 7044:                    'permanentemail','id');
 7045:     }
 7046:     if ((ref($titlesref) eq 'HASH')) {
 7047:         %fieldtitles = %{$titlesref};
 7048:     } else {
 7049:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7050:     }
 7051:     my $output;
 7052:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 7053:     $output = '<tr '.$css_class.'>'.
 7054:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 7055:               '<td class="LC_left_item" colspan="2"><table>';
 7056:     my $rem;
 7057:     my %checks;
 7058:     if (ref($settings) eq 'HASH') {
 7059:         if (ref($settings->{$context}) eq 'HASH') {
 7060:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 7061:                 my $hashref = $settings->{$context}->{$role};
 7062:                 if ($role eq 'emailusername') {
 7063:                     if ($statustype) {
 7064:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 7065:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 7066:                             if (ref($hashref) eq 'HASH') { 
 7067:                                 foreach my $field (@fields) {
 7068:                                     if ($hashref->{$field}) {
 7069:                                         $checks{$field} = $hashref->{$field};
 7070:                                     }
 7071:                                 }
 7072:                             }
 7073:                         }
 7074:                     }
 7075:                 } else {
 7076:                     if (ref($hashref) eq 'HASH') {
 7077:                         foreach my $field (@fields) {
 7078:                             if ($hashref->{$field}) {
 7079:                                 $checks{$field} = ' checked="checked" ';
 7080:                             }
 7081:                         }
 7082:                     }
 7083:                 }
 7084:             }
 7085:         }
 7086:     }
 7087:      
 7088:     for (my $i=0; $i<@fields; $i++) {
 7089:         my $rem = $i%($numinrow);
 7090:         if ($rem == 0) {
 7091:             if ($i > 0) {
 7092:                 $output .= '</tr>';
 7093:             }
 7094:             $output .= '<tr>';
 7095:         }
 7096:         my $check = ' ';
 7097:         unless ($role eq 'emailusername') {
 7098:             if (exists($checks{$fields[$i]})) {
 7099:                 $check = $checks{$fields[$i]}
 7100:             } else {
 7101:                 if ($role eq 'st') {
 7102:                     if (ref($settings) ne 'HASH') {
 7103:                         $check = ' checked="checked" '; 
 7104:                     }
 7105:                 }
 7106:             }
 7107:         }
 7108:         $output .= '<td class="LC_left_item">'.
 7109:                    '<span class="LC_nobreak">';
 7110:         if ($role eq 'emailusername') {
 7111:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 7112:                 $checks{$fields[$i]} = 'omit';
 7113:             }
 7114:             foreach my $option ('required','optional','omit') {
 7115:                 my $checked='';
 7116:                 if ($checks{$fields[$i]} eq $option) {
 7117:                     $checked='checked="checked" ';
 7118:                 }
 7119:                 $output .= '<label>'.
 7120:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 7121:                            &mt($option).'</label>'.('&nbsp;' x2);
 7122:             }
 7123:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 7124:         } else {
 7125:             $output .= '<label>'.
 7126:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 7127:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 7128:                        '</label>';
 7129:         }
 7130:         $output .= '</span></td>';
 7131:         $rem = @fields%($numinrow);
 7132:     }
 7133:     my $colsleft = $numinrow - $rem;
 7134:     if ($colsleft > 1 ) {
 7135:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7136:                    '&nbsp;</td>';
 7137:     } elsif ($colsleft == 1) {
 7138:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7139:     }
 7140:     $output .= '</tr></table></td></tr>';
 7141:     return $output;
 7142: }
 7143: 
 7144: sub insttypes_row {
 7145:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 7146:     my %lt = &Apache::lonlocal::texthash (
 7147:                       cansearch => 'Users allowed to search',
 7148:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 7149:                       lockablenames => 'User preference to lock name',
 7150:              );
 7151:     my $showdom;
 7152:     if ($context eq 'cansearch') {
 7153:         $showdom = ' ('.$dom.')';
 7154:     }
 7155:     my $class = 'LC_left_item';
 7156:     if ($context eq 'statustocreate') {
 7157:         $class = 'LC_right_item';
 7158:     }
 7159:     my $css_class = ' class="LC_odd_row"';
 7160:     if ($rownum ne '') { 
 7161:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 7162:     }
 7163:     my $output = '<tr'.$css_class.'>'.
 7164:                  '<td>'.$lt{$context}.$showdom.
 7165:                  '</td><td class="'.$class.'" colspan="2"><table>';
 7166:     my $rem;
 7167:     if (ref($types) eq 'ARRAY') {
 7168:         for (my $i=0; $i<@{$types}; $i++) {
 7169:             if (defined($usertypes->{$types->[$i]})) {
 7170:                 my $rem = $i%($numinrow);
 7171:                 if ($rem == 0) {
 7172:                     if ($i > 0) {
 7173:                         $output .= '</tr>';
 7174:                     }
 7175:                     $output .= '<tr>';
 7176:                 }
 7177:                 my $check = ' ';
 7178:                 if (ref($settings) eq 'HASH') {
 7179:                     if (ref($settings->{$context}) eq 'ARRAY') {
 7180:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 7181:                             $check = ' checked="checked" ';
 7182:                         }
 7183:                     } elsif ($context eq 'statustocreate') {
 7184:                         $check = ' checked="checked" ';
 7185:                     }
 7186:                 }
 7187:                 $output .= '<td class="LC_left_item">'.
 7188:                            '<span class="LC_nobreak"><label>'.
 7189:                            '<input type="checkbox" name="'.$context.'" '.
 7190:                            'value="'.$types->[$i].'"'.$check.'/>'.
 7191:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 7192:             }
 7193:         }
 7194:         $rem = @{$types}%($numinrow);
 7195:     }
 7196:     my $colsleft = $numinrow - $rem;
 7197:     if (($rem == 0) && (@{$types} > 0)) {
 7198:         $output .= '<tr>';
 7199:     }
 7200:     if ($colsleft > 1) {
 7201:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 7202:     } else {
 7203:         $output .= '<td class="LC_left_item">';
 7204:     }
 7205:     my $defcheck = ' ';
 7206:     if (ref($settings) eq 'HASH') {  
 7207:         if (ref($settings->{$context}) eq 'ARRAY') {
 7208:             if (grep(/^default$/,@{$settings->{$context}})) {
 7209:                 $defcheck = ' checked="checked" ';
 7210:             }
 7211:         } elsif ($context eq 'statustocreate') {
 7212:             $defcheck = ' checked="checked" ';
 7213:         }
 7214:     }
 7215:     $output .= '<span class="LC_nobreak"><label>'.
 7216:                '<input type="checkbox" name="'.$context.'" '.
 7217:                'value="default"'.$defcheck.'/>'.
 7218:                $othertitle.'</label></span></td>'.
 7219:                '</tr></table></td></tr>';
 7220:     return $output;
 7221: }
 7222: 
 7223: sub sorted_searchtitles {
 7224:     my %searchtitles = &Apache::lonlocal::texthash(
 7225:                          'uname' => 'username',
 7226:                          'lastname' => 'last name',
 7227:                          'lastfirst' => 'last name, first name',
 7228:                      );
 7229:     my @titleorder = ('uname','lastname','lastfirst');
 7230:     return (\%searchtitles,\@titleorder);
 7231: }
 7232: 
 7233: sub sorted_searchtypes {
 7234:     my %srchtypes_desc = (
 7235:                            exact    => 'is exact match',
 7236:                            contains => 'contains ..',
 7237:                            begins   => 'begins with ..',
 7238:                          );
 7239:     my @srchtypeorder = ('exact','begins','contains');
 7240:     return (\%srchtypes_desc,\@srchtypeorder);
 7241: }
 7242: 
 7243: sub usertype_update_row {
 7244:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 7245:     my $datatable;
 7246:     my $numinrow = 4;
 7247:     foreach my $type (@{$types}) {
 7248:         if (defined($usertypes->{$type})) {
 7249:             $$rownums ++;
 7250:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 7251:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 7252:                           '</td><td class="LC_left_item"><table>';
 7253:             for (my $i=0; $i<@{$fields}; $i++) {
 7254:                 my $rem = $i%($numinrow);
 7255:                 if ($rem == 0) {
 7256:                     if ($i > 0) {
 7257:                         $datatable .= '</tr>';
 7258:                     }
 7259:                     $datatable .= '<tr>';
 7260:                 }
 7261:                 my $check = ' ';
 7262:                 if (ref($settings) eq 'HASH') {
 7263:                     if (ref($settings->{'fields'}) eq 'HASH') {
 7264:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 7265:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 7266:                                 $check = ' checked="checked" ';
 7267:                             }
 7268:                         }
 7269:                     }
 7270:                 }
 7271: 
 7272:                 if ($i == @{$fields}-1) {
 7273:                     my $colsleft = $numinrow - $rem;
 7274:                     if ($colsleft > 1) {
 7275:                         $datatable .= '<td colspan="'.$colsleft.'">';
 7276:                     } else {
 7277:                         $datatable .= '<td>';
 7278:                     }
 7279:                 } else {
 7280:                     $datatable .= '<td>';
 7281:                 }
 7282:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7283:                               '<input type="checkbox" name="updateable_'.$type.
 7284:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 7285:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 7286:             }
 7287:             $datatable .= '</tr></table></td></tr>';
 7288:         }
 7289:     }
 7290:     return $datatable;
 7291: }
 7292: 
 7293: sub modify_login {
 7294:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 7295:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 7296:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 7297:     %title = ( coursecatalog => 'Display course catalog',
 7298:                adminmail => 'Display administrator E-mail address',
 7299:                helpdesk  => 'Display "Contact Helpdesk" link',
 7300:                newuser => 'Link for visitors to create a user account',
 7301:                loginheader => 'Log-in box header');
 7302:     @offon = ('off','on');
 7303:     if (ref($domconfig{login}) eq 'HASH') {
 7304:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 7305:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 7306:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 7307:             }
 7308:         }
 7309:     }
 7310:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 7311:                                            \%domconfig,\%loginhash);
 7312:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7313:     foreach my $item (@toggles) {
 7314:         $loginhash{login}{$item} = $env{'form.'.$item};
 7315:     }
 7316:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 7317:     if (ref($colchanges{'login'}) eq 'HASH') {  
 7318:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 7319:                                          \%loginhash);
 7320:     }
 7321: 
 7322:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7323:     my %domservers = &Apache::lonnet::get_servers($dom);
 7324:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 7325:     if (keys(%servers) > 1) {
 7326:         foreach my $lonhost (keys(%servers)) {
 7327:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 7328:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 7329:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 7330:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 7331:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 7332:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7333:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7334:                         $changes{'loginvia'}{$lonhost} = 1;
 7335:                     } else {
 7336:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 7337:                         $changes{'loginvia'}{$lonhost} = 1;
 7338:                     }
 7339:                 } else {
 7340:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7341:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7342:                         $changes{'loginvia'}{$lonhost} = 1;
 7343:                     }
 7344:                 }
 7345:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 7346:                     foreach my $item (@loginvia_attribs) {
 7347:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 7348:                     }
 7349:                 } else {
 7350:                     foreach my $item (@loginvia_attribs) {
 7351:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7352:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7353:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 7354:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7355:                                 $new = '/';
 7356:                             }
 7357:                         }
 7358:                         if (($item eq 'custompath') && 
 7359:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7360:                             $new = '';
 7361:                         }
 7362:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 7363:                             $changes{'loginvia'}{$lonhost} = 1;
 7364:                         }
 7365:                         if ($item eq 'exempt') {
 7366:                             $new = &check_exempt_addresses($new);
 7367:                         }
 7368:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7369:                     }
 7370:                 }
 7371:             } else {
 7372:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7373:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7374:                     $changes{'loginvia'}{$lonhost} = 1;
 7375:                     foreach my $item (@loginvia_attribs) {
 7376:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7377:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7378:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7379:                                 $new = '/';
 7380:                             }
 7381:                         }
 7382:                         if (($item eq 'custompath') && 
 7383:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7384:                             $new = '';
 7385:                         }
 7386:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7387:                     }
 7388:                 }
 7389:             }
 7390:         }
 7391:     }
 7392: 
 7393:     my $servadm = $r->dir_config('lonAdmEMail');
 7394:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 7395:     if (ref($domconfig{'login'}) eq 'HASH') {
 7396:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 7397:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 7398:                 if ($lang eq 'nolang') {
 7399:                     push(@currlangs,$lang);
 7400:                 } elsif (defined($langchoices{$lang})) {
 7401:                     push(@currlangs,$lang);
 7402:                 } else {
 7403:                     next;
 7404:                 }
 7405:             }
 7406:         }
 7407:     }
 7408:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 7409:     if (@currlangs > 0) {
 7410:         foreach my $lang (@currlangs) {
 7411:             if (grep(/^\Q$lang\E$/,@delurls)) {
 7412:                 $changes{'helpurl'}{$lang} = 1;
 7413:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 7414:                 $changes{'helpurl'}{$lang} = 1;
 7415:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 7416:                 push(@newlangs,$lang);
 7417:             } else {
 7418:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7419:             }
 7420:         }
 7421:     }
 7422:     unless (grep(/^nolang$/,@currlangs)) {
 7423:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 7424:             $changes{'helpurl'}{'nolang'} = 1;
 7425:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 7426:             push(@newlangs,'nolang');
 7427:         }
 7428:     }
 7429:     if ($env{'form.loginhelpurl_add_lang'}) {
 7430:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 7431:             ($env{'form.loginhelpurl_add_file.filename'})) {
 7432:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 7433:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 7434:         }
 7435:     }
 7436:     if ((@newlangs > 0) || ($addedfile)) {
 7437:         my $error;
 7438:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7439:         if ($configuserok eq 'ok') {
 7440:             if ($switchserver) {
 7441:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 7442:             } elsif ($author_ok eq 'ok') {
 7443:                 my @allnew = @newlangs;
 7444:                 if ($addedfile ne '') {
 7445:                     push(@allnew,$addedfile);
 7446:                 }
 7447:                 foreach my $lang (@allnew) {
 7448:                     my $formelem = 'loginhelpurl_'.$lang;
 7449:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 7450:                         $formelem = 'loginhelpurl_add_file';
 7451:                     }
 7452:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7453:                                                                "help/$lang",'','',$newfile{$lang});
 7454:                     if ($result eq 'ok') {
 7455:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 7456:                         $changes{'helpurl'}{$lang} = 1;
 7457:                     } else {
 7458:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 7459:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7460:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 7461:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 7462:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7463:                         }
 7464:                     }
 7465:                 }
 7466:             } else {
 7467:                 $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);
 7468:             }
 7469:         } else {
 7470:             $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);
 7471:         }
 7472:         if ($error) {
 7473:             &Apache::lonnet::logthis($error);
 7474:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7475:         }
 7476:     }
 7477: 
 7478:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 7479:     if (ref($domconfig{'login'}) eq 'HASH') {
 7480:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 7481:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 7482:                 if ($domservers{$lonhost}) {
 7483:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7484:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 7485:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 7486:                     }
 7487:                 }
 7488:             }
 7489:         }
 7490:     }
 7491:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 7492:     foreach my $lonhost (sort(keys(%domservers))) {
 7493:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7494:             $changes{'headtag'}{$lonhost} = 1;
 7495:         } else {
 7496:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 7497:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 7498:             }
 7499:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 7500:                 push(@newhosts,$lonhost);
 7501:             } elsif ($currheadtagurls{$lonhost}) {
 7502:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 7503:                 if ($currexempt{$lonhost}) {
 7504:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 7505:                         $changes{'headtag'}{$lonhost} = 1;
 7506:                     }
 7507:                 } elsif ($possexempt{$lonhost}) {
 7508:                     $changes{'headtag'}{$lonhost} = 1;
 7509:                 }
 7510:                 if ($possexempt{$lonhost}) {
 7511:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7512:                 }
 7513:             }
 7514:         }
 7515:     }
 7516:     if (@newhosts) {
 7517:         my $error;
 7518:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7519:         if ($configuserok eq 'ok') {
 7520:             if ($switchserver) {
 7521:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 7522:             } elsif ($author_ok eq 'ok') {
 7523:                 foreach my $lonhost (@newhosts) {
 7524:                     my $formelem = 'loginheadtag_'.$lonhost;
 7525:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7526:                                                                           "login/headtag/$lonhost",'','',
 7527:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 7528:                     if ($result eq 'ok') {
 7529:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 7530:                         $changes{'headtag'}{$lonhost} = 1;
 7531:                         if ($possexempt{$lonhost}) {
 7532:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7533:                         }
 7534:                     } else {
 7535:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 7536:                                            $newheadtagurls{$lonhost},$result);
 7537:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7538:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 7539:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 7540:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 7541:                         }
 7542:                     }
 7543:                 }
 7544:             } else {
 7545:                 $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);
 7546:             }
 7547:         } else {
 7548:             $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);
 7549:         }
 7550:         if ($error) {
 7551:             &Apache::lonnet::logthis($error);
 7552:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7553:         }
 7554:     }
 7555:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 7556: 
 7557:     my $defaulthelpfile = '/adm/loginproblems.html';
 7558:     my $defaulttext = &mt('Default in use');
 7559: 
 7560:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 7561:                                              $dom);
 7562:     if ($putresult eq 'ok') {
 7563:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7564:         my %defaultchecked = (
 7565:                     'coursecatalog' => 'on',
 7566:                     'helpdesk'      => 'on',
 7567:                     'adminmail'     => 'off',
 7568:                     'newuser'       => 'off',
 7569:         );
 7570:         if (ref($domconfig{'login'}) eq 'HASH') {
 7571:             foreach my $item (@toggles) {
 7572:                 if ($defaultchecked{$item} eq 'on') { 
 7573:                     if (($domconfig{'login'}{$item} eq '0') &&
 7574:                         ($env{'form.'.$item} eq '1')) {
 7575:                         $changes{$item} = 1;
 7576:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7577:                               $domconfig{'login'}{$item} eq '1') &&
 7578:                              ($env{'form.'.$item} eq '0')) {
 7579:                         $changes{$item} = 1;
 7580:                     }
 7581:                 } elsif ($defaultchecked{$item} eq 'off') {
 7582:                     if (($domconfig{'login'}{$item} eq '1') &&
 7583:                         ($env{'form.'.$item} eq '0')) {
 7584:                         $changes{$item} = 1;
 7585:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7586:                               $domconfig{'login'}{$item} eq '0') &&
 7587:                              ($env{'form.'.$item} eq '1')) {
 7588:                         $changes{$item} = 1;
 7589:                     }
 7590:                 }
 7591:             }
 7592:         }
 7593:         if (keys(%changes) > 0 || $colchgtext) {
 7594:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7595:             if (ref($lastactref) eq 'HASH') {
 7596:                 $lastactref->{'domainconfig'} = 1;
 7597:             }
 7598:             $resulttext = &mt('Changes made:').'<ul>';
 7599:             foreach my $item (sort(keys(%changes))) {
 7600:                 if ($item eq 'loginvia') {
 7601:                     if (ref($changes{$item}) eq 'HASH') {
 7602:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 7603:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7604:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 7605:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 7606:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 7607:                                     $protocol = 'http' if ($protocol ne 'https');
 7608:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 7609: 
 7610:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 7611:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 7612:                                     } else {
 7613:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 7614:                                     }
 7615:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 7616:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 7617:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 7618:                                     }
 7619:                                     $resulttext .= '</li>';
 7620:                                 } else {
 7621:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 7622:                                 }
 7623:                             } else {
 7624:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 7625:                             }
 7626:                         }
 7627:                         $resulttext .= '</ul></li>';
 7628:                     }
 7629:                 } elsif ($item eq 'helpurl') {
 7630:                     if (ref($changes{$item}) eq 'HASH') {
 7631:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 7632:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 7633:                                 my ($chg,$link);
 7634:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 7635:                                 if ($lang eq 'nolang') {
 7636:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 7637:                                 } else {
 7638:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 7639:                                 }
 7640:                                 $resulttext .= '<li>'.$chg.'</li>';
 7641:                             } else {
 7642:                                 my $chg;
 7643:                                 if ($lang eq 'nolang') {
 7644:                                     $chg = &mt('custom log-in help file for no preferred language');
 7645:                                 } else {
 7646:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 7647:                                 }
 7648:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 7649:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 7650:                                                       '?inhibitmenu=yes',$chg,600,500).
 7651:                                                '</li>';
 7652:                             }
 7653:                         }
 7654:                     }
 7655:                 } elsif ($item eq 'headtag') {
 7656:                     if (ref($changes{$item}) eq 'HASH') {
 7657:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7658:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7659:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 7660:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7661:                                 $resulttext .= '<li><a href="'.
 7662:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 7663:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 7664:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 7665:                                 if ($possexempt{$lonhost}) {
 7666:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 7667:                                 } else {
 7668:                                     $resulttext .= &mt('included for any client IP');
 7669:                                 }
 7670:                                 $resulttext .= '</li>';
 7671:                             }
 7672:                         }
 7673:                     }
 7674:                 } elsif ($item eq 'captcha') {
 7675:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7676:                         my $chgtxt;
 7677:                         if ($loginhash{'login'}{$item} eq 'notused') {
 7678:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 7679:                         } else {
 7680:                             my %captchas = &captcha_phrases();
 7681:                             if ($captchas{$loginhash{'login'}{$item}}) {
 7682:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 7683:                             } else {
 7684:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 7685:                             }
 7686:                         }
 7687:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7688:                     }
 7689:                 } elsif ($item eq 'recaptchakeys') {
 7690:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7691:                         my ($privkey,$pubkey);
 7692:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 7693:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 7694:                             $privkey = $loginhash{'login'}{$item}{'private'};
 7695:                         }
 7696:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 7697:                         if (!$pubkey) {
 7698:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 7699:                         } else {
 7700:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 7701:                         }
 7702:                         if (!$privkey) {
 7703:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 7704:                         } else {
 7705:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 7706:                         }
 7707:                         $chgtxt .= '</ul>';
 7708:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7709:                     }
 7710:                 } elsif ($item eq 'recaptchaversion') {
 7711:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7712:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 7713:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 7714:                                            '</li>';
 7715:                         }
 7716:                     }
 7717:                 } else {
 7718:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 7719:                 }
 7720:             }
 7721:             $resulttext .= $colchgtext.'</ul>';
 7722:         } else {
 7723:             $resulttext = &mt('No changes made to log-in page settings');
 7724:         }
 7725:     } else {
 7726:         $resulttext = '<span class="LC_error">'.
 7727: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7728:     }
 7729:     if ($errors) {
 7730:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7731:                        $errors.'</ul>';
 7732:     }
 7733:     return $resulttext;
 7734: }
 7735: 
 7736: sub check_exempt_addresses {
 7737:     my ($iplist) = @_;
 7738:     $iplist =~ s/^\s+//;
 7739:     $iplist =~ s/\s+$//;
 7740:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 7741:     my (@okips,$new);
 7742:     foreach my $ip (@poss_ips) {
 7743:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 7744:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 7745:                 push(@okips,$ip);
 7746:             }
 7747:         }
 7748:     }
 7749:     if (@okips > 0) {
 7750:         $new = join(',',@okips);
 7751:     } else {
 7752:         $new = '';
 7753:     }
 7754:     return $new;
 7755: }
 7756: 
 7757: sub color_font_choices {
 7758:     my %choices =
 7759:         &Apache::lonlocal::texthash (
 7760:             img => "Header",
 7761:             bgs => "Background colors",
 7762:             links => "Link colors",
 7763:             images => "Images",
 7764:             font => "Font color",
 7765:             fontmenu => "Font menu",
 7766:             pgbg => "Page",
 7767:             tabbg => "Header",
 7768:             sidebg => "Border",
 7769:             link => "Link",
 7770:             alink => "Active link",
 7771:             vlink => "Visited link",
 7772:         );
 7773:     return %choices;
 7774: }
 7775: 
 7776: sub modify_rolecolors {
 7777:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 7778:     my ($resulttext,%rolehash);
 7779:     $rolehash{'rolecolors'} = {};
 7780:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 7781:         if ($domconfig{'rolecolors'} eq '') {
 7782:             $domconfig{'rolecolors'} = {};
 7783:         }
 7784:     }
 7785:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 7786:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 7787:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 7788:                                              $dom);
 7789:     if ($putresult eq 'ok') {
 7790:         if (keys(%changes) > 0) {
 7791:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7792:             if (ref($lastactref) eq 'HASH') {
 7793:                 $lastactref->{'domainconfig'} = 1;
 7794:             }
 7795:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 7796:                                              $rolehash{'rolecolors'});
 7797:         } else {
 7798:             $resulttext = &mt('No changes made to default color schemes');
 7799:         }
 7800:     } else {
 7801:         $resulttext = '<span class="LC_error">'.
 7802: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7803:     }
 7804:     if ($errors) {
 7805:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7806:                        $errors.'</ul>';
 7807:     }
 7808:     return $resulttext;
 7809: }
 7810: 
 7811: sub modify_colors {
 7812:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 7813:     my (%changes,%choices);
 7814:     my @bgs;
 7815:     my @links = ('link','alink','vlink');
 7816:     my @logintext;
 7817:     my @images;
 7818:     my $servadm = $r->dir_config('lonAdmEMail');
 7819:     my $errors;
 7820:     my %defaults;
 7821:     foreach my $role (@{$roles}) {
 7822:         if ($role eq 'login') {
 7823:             %choices = &login_choices();
 7824:             @logintext = ('textcol','bgcol');
 7825:         } else {
 7826:             %choices = &color_font_choices();
 7827:         }
 7828:         if ($role eq 'login') {
 7829:             @images = ('img','logo','domlogo','login');
 7830:             @bgs = ('pgbg','mainbg','sidebg');
 7831:         } else {
 7832:             @images = ('img');
 7833:             @bgs = ('pgbg','tabbg','sidebg');
 7834:         }
 7835:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 7836:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 7837:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 7838:         }
 7839:         if ($role eq 'login') {
 7840:             foreach my $item (@logintext) {
 7841:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7842:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7843:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7844:                 }
 7845:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 7846:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7847:                 }
 7848:             }
 7849:         } else {
 7850:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 7851:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 7852:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 7853:             }
 7854:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 7855:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 7856:             }
 7857:         }
 7858:         foreach my $item (@bgs) {
 7859:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7860:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7861:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7862:             }
 7863:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 7864:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7865:             }
 7866:         }
 7867:         foreach my $item (@links) {
 7868:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 7869:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 7870:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 7871:             }
 7872:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 7873:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 7874:             }
 7875:         }
 7876:         my ($configuserok,$author_ok,$switchserver) = 
 7877:             &config_check($dom,$confname,$servadm);
 7878:         my ($width,$height) = &thumb_dimensions();
 7879:         if (ref($domconfig->{$role}) ne 'HASH') {
 7880:             $domconfig->{$role} = {};
 7881:         }
 7882:         foreach my $img (@images) {
 7883:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 7884:                 if (defined($env{'form.login_showlogo_'.$img})) {
 7885:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 7886:                 } else { 
 7887:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 7888:                 }
 7889:             } 
 7890: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 7891: 		 && !defined($domconfig->{$role}{$img})
 7892: 		 && !$env{'form.'.$role.'_del_'.$img}
 7893: 		 && $env{'form.'.$role.'_import_'.$img}) {
 7894: 		# import the old configured image from the .tab setting
 7895: 		# if they haven't provided a new one 
 7896: 		$domconfig->{$role}{$img} = 
 7897: 		    $env{'form.'.$role.'_import_'.$img};
 7898: 	    }
 7899:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 7900:                 my $error;
 7901:                 if ($configuserok eq 'ok') {
 7902:                     if ($switchserver) {
 7903:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 7904:                     } else {
 7905:                         if ($author_ok eq 'ok') {
 7906:                             my ($result,$logourl) = 
 7907:                                 &publishlogo($r,'upload',$role.'_'.$img,
 7908:                                            $dom,$confname,$img,$width,$height);
 7909:                             if ($result eq 'ok') {
 7910:                                 $confhash->{$role}{$img} = $logourl;
 7911:                                 $changes{$role}{'images'}{$img} = 1;
 7912:                             } else {
 7913:                                 $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);
 7914:                             }
 7915:                         } else {
 7916:                             $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);
 7917:                         }
 7918:                     }
 7919:                 } else {
 7920:                     $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);
 7921:                 }
 7922:                 if ($error) {
 7923:                     &Apache::lonnet::logthis($error);
 7924:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7925:                 }
 7926:             } elsif ($domconfig->{$role}{$img} ne '') {
 7927:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 7928:                     my $error;
 7929:                     if ($configuserok eq 'ok') {
 7930: # is confname an author?
 7931:                         if ($switchserver eq '') {
 7932:                             if ($author_ok eq 'ok') {
 7933:                                 my ($result,$logourl) = 
 7934:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 7935:                                             $dom,$confname,$img,$width,$height);
 7936:                                 if ($result eq 'ok') {
 7937:                                     $confhash->{$role}{$img} = $logourl;
 7938: 				    $changes{$role}{'images'}{$img} = 1;
 7939:                                 }
 7940:                             }
 7941:                         }
 7942:                     }
 7943:                 }
 7944:             }
 7945:         }
 7946:         if (ref($domconfig) eq 'HASH') {
 7947:             if (ref($domconfig->{$role}) eq 'HASH') {
 7948:                 foreach my $img (@images) {
 7949:                     if ($domconfig->{$role}{$img} ne '') {
 7950:                         if ($env{'form.'.$role.'_del_'.$img}) {
 7951:                             $confhash->{$role}{$img} = '';
 7952:                             $changes{$role}{'images'}{$img} = 1;
 7953:                         } else {
 7954:                             if ($confhash->{$role}{$img} eq '') {
 7955:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 7956:                             }
 7957:                         }
 7958:                     } else {
 7959:                         if ($env{'form.'.$role.'_del_'.$img}) {
 7960:                             $confhash->{$role}{$img} = '';
 7961:                             $changes{$role}{'images'}{$img} = 1;
 7962:                         } 
 7963:                     }
 7964:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 7965:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 7966:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 7967:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 7968:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 7969:                             }
 7970:                         } else {
 7971:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 7972:                                 $changes{$role}{'showlogo'}{$img} = 1;
 7973:                             }
 7974:                         }
 7975:                     }
 7976:                 }
 7977:                 if ($domconfig->{$role}{'font'} ne '') {
 7978:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 7979:                         $changes{$role}{'font'} = 1;
 7980:                     }
 7981:                 } else {
 7982:                     if ($confhash->{$role}{'font'}) {
 7983:                         $changes{$role}{'font'} = 1;
 7984:                     }
 7985:                 }
 7986:                 if ($role ne 'login') {
 7987:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 7988:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 7989:                             $changes{$role}{'fontmenu'} = 1;
 7990:                         }
 7991:                     } else {
 7992:                         if ($confhash->{$role}{'fontmenu'}) {
 7993:                             $changes{$role}{'fontmenu'} = 1;
 7994:                         }
 7995:                     }
 7996:                 }
 7997:                 foreach my $item (@bgs) {
 7998:                     if ($domconfig->{$role}{$item} ne '') {
 7999:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8000:                             $changes{$role}{'bgs'}{$item} = 1;
 8001:                         } 
 8002:                     } else {
 8003:                         if ($confhash->{$role}{$item}) {
 8004:                             $changes{$role}{'bgs'}{$item} = 1;
 8005:                         }
 8006:                     }
 8007:                 }
 8008:                 foreach my $item (@links) {
 8009:                     if ($domconfig->{$role}{$item} ne '') {
 8010:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8011:                             $changes{$role}{'links'}{$item} = 1;
 8012:                         }
 8013:                     } else {
 8014:                         if ($confhash->{$role}{$item}) {
 8015:                             $changes{$role}{'links'}{$item} = 1;
 8016:                         }
 8017:                     }
 8018:                 }
 8019:                 foreach my $item (@logintext) {
 8020:                     if ($domconfig->{$role}{$item} ne '') {
 8021:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8022:                             $changes{$role}{'logintext'}{$item} = 1;
 8023:                         }
 8024:                     } else {
 8025:                         if ($confhash->{$role}{$item}) {
 8026:                             $changes{$role}{'logintext'}{$item} = 1;
 8027:                         }
 8028:                     }
 8029:                 }
 8030:             } else {
 8031:                 &default_change_checker($role,\@images,\@links,\@bgs,
 8032:                                         \@logintext,$confhash,\%changes); 
 8033:             }
 8034:         } else {
 8035:             &default_change_checker($role,\@images,\@links,\@bgs,
 8036:                                     \@logintext,$confhash,\%changes); 
 8037:         }
 8038:     }
 8039:     return ($errors,%changes);
 8040: }
 8041: 
 8042: sub config_check {
 8043:     my ($dom,$confname,$servadm) = @_;
 8044:     my ($configuserok,$author_ok,$switchserver,%currroles);
 8045:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 8046:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 8047:                                                    $confname,$servadm);
 8048:     if ($configuserok eq 'ok') {
 8049:         $switchserver = &check_switchserver($dom,$confname);
 8050:         if ($switchserver eq '') {
 8051:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 8052:         }
 8053:     }
 8054:     return ($configuserok,$author_ok,$switchserver);
 8055: }
 8056: 
 8057: sub default_change_checker {
 8058:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 8059:     foreach my $item (@{$links}) {
 8060:         if ($confhash->{$role}{$item}) {
 8061:             $changes->{$role}{'links'}{$item} = 1;
 8062:         }
 8063:     }
 8064:     foreach my $item (@{$bgs}) {
 8065:         if ($confhash->{$role}{$item}) {
 8066:             $changes->{$role}{'bgs'}{$item} = 1;
 8067:         }
 8068:     }
 8069:     foreach my $item (@{$logintext}) {
 8070:         if ($confhash->{$role}{$item}) {
 8071:             $changes->{$role}{'logintext'}{$item} = 1;
 8072:         }
 8073:     }
 8074:     foreach my $img (@{$images}) {
 8075:         if ($env{'form.'.$role.'_del_'.$img}) {
 8076:             $confhash->{$role}{$img} = '';
 8077:             $changes->{$role}{'images'}{$img} = 1;
 8078:         }
 8079:         if ($role eq 'login') {
 8080:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8081:                 $changes->{$role}{'showlogo'}{$img} = 1;
 8082:             }
 8083:         }
 8084:     }
 8085:     if ($confhash->{$role}{'font'}) {
 8086:         $changes->{$role}{'font'} = 1;
 8087:     }
 8088: }
 8089: 
 8090: sub display_colorchgs {
 8091:     my ($dom,$changes,$roles,$confhash) = @_;
 8092:     my (%choices,$resulttext);
 8093:     if (!grep(/^login$/,@{$roles})) {
 8094:         $resulttext = &mt('Changes made:').'<br />';
 8095:     }
 8096:     foreach my $role (@{$roles}) {
 8097:         if ($role eq 'login') {
 8098:             %choices = &login_choices();
 8099:         } else {
 8100:             %choices = &color_font_choices();
 8101:         }
 8102:         if (ref($changes->{$role}) eq 'HASH') {
 8103:             if ($role ne 'login') {
 8104:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 8105:             }
 8106:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 8107:                 if ($role ne 'login') {
 8108:                     $resulttext .= '<ul>';
 8109:                 }
 8110:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 8111:                     if ($role ne 'login') {
 8112:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 8113:                     }
 8114:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 8115:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 8116:                             if ($confhash->{$role}{$key}{$item}) {
 8117:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 8118:                             } else {
 8119:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 8120:                             }
 8121:                         } elsif ($confhash->{$role}{$item} eq '') {
 8122:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 8123:                         } else {
 8124:                             my $newitem = $confhash->{$role}{$item};
 8125:                             if ($key eq 'images') {
 8126:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 8127:                             }
 8128:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 8129:                         }
 8130:                     }
 8131:                     if ($role ne 'login') {
 8132:                         $resulttext .= '</ul></li>';
 8133:                     }
 8134:                 } else {
 8135:                     if ($confhash->{$role}{$key} eq '') {
 8136:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 8137:                     } else {
 8138:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 8139:                     }
 8140:                 }
 8141:                 if ($role ne 'login') {
 8142:                     $resulttext .= '</ul>';
 8143:                 }
 8144:             }
 8145:         }
 8146:     }
 8147:     return $resulttext;
 8148: }
 8149: 
 8150: sub thumb_dimensions {
 8151:     return ('200','50');
 8152: }
 8153: 
 8154: sub check_dimensions {
 8155:     my ($inputfile) = @_;
 8156:     my ($fullwidth,$fullheight);
 8157:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 8158:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 8159:             my $imageinfo = <PIPE>;
 8160:             if (!close(PIPE)) {
 8161:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 8162:             }
 8163:             chomp($imageinfo);
 8164:             my ($fullsize) = 
 8165:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 8166:             if ($fullsize) {
 8167:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 8168:             }
 8169:         }
 8170:     }
 8171:     return ($fullwidth,$fullheight);
 8172: }
 8173: 
 8174: sub check_configuser {
 8175:     my ($uhome,$dom,$confname,$servadm) = @_;
 8176:     my ($configuserok,%currroles);
 8177:     if ($uhome eq 'no_host') {
 8178:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 8179:         my $configpass = &LONCAPA::Enrollment::create_password();
 8180:         $configuserok = 
 8181:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 8182:                              $configpass,'','','','','',undef,$servadm);
 8183:     } else {
 8184:         $configuserok = 'ok';
 8185:         %currroles = 
 8186:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 8187:     }
 8188:     return ($configuserok,%currroles);
 8189: }
 8190: 
 8191: sub check_authorstatus {
 8192:     my ($dom,$confname,%currroles) = @_;
 8193:     my $author_ok;
 8194:     if (!$currroles{':'.$dom.':au'}) {
 8195:         my $start = time;
 8196:         my $end = 0;
 8197:         $author_ok = 
 8198:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 8199:                                         'au',$end,$start,'','','domconfig');
 8200:     } else {
 8201:         $author_ok = 'ok';
 8202:     }
 8203:     return $author_ok;
 8204: }
 8205: 
 8206: sub publishlogo {
 8207:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 8208:     my ($output,$fname,$logourl);
 8209:     if ($action eq 'upload') {
 8210:         $fname=$env{'form.'.$formname.'.filename'};
 8211:         chop($env{'form.'.$formname});
 8212:     } else {
 8213:         ($fname) = ($formname =~ /([^\/]+)$/);
 8214:     }
 8215:     if ($savefileas ne '') {
 8216:         $fname = $savefileas;
 8217:     }
 8218:     $fname=&Apache::lonnet::clean_filename($fname);
 8219: # See if there is anything left
 8220:     unless ($fname) { return ('error: no uploaded file'); }
 8221:     $fname="$subdir/$fname";
 8222:     my $docroot=$r->dir_config('lonDocRoot');
 8223:     my $filepath="$docroot/priv";
 8224:     my $relpath = "$dom/$confname";
 8225:     my ($fnamepath,$file,$fetchthumb);
 8226:     $file=$fname;
 8227:     if ($fname=~m|/|) {
 8228:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 8229:     }
 8230:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 8231:     my $count;
 8232:     for ($count=5;$count<=$#parts;$count++) {
 8233:         $filepath.="/$parts[$count]";
 8234:         if ((-e $filepath)!=1) {
 8235:             mkdir($filepath,02770);
 8236:         }
 8237:     }
 8238:     # Check for bad extension and disallow upload
 8239:     if ($file=~/\.(\w+)$/ &&
 8240:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 8241:         $output = 
 8242:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 8243:     } elsif ($file=~/\.(\w+)$/ &&
 8244:         !defined(&Apache::loncommon::fileembstyle($1))) {
 8245:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 8246:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 8247:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 8248:     } elsif (-d "$filepath/$file") {
 8249:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 8250:     } else {
 8251:         my $source = $filepath.'/'.$file;
 8252:         my $logfile;
 8253:         if (!open($logfile,">>$source".'.log')) {
 8254:             return (&mt('No write permission to Authoring Space'));
 8255:         }
 8256:         print $logfile
 8257: "\n================= Publish ".localtime()." ================\n".
 8258: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 8259: # Save the file
 8260:         if (!open(FH,'>'.$source)) {
 8261:             &Apache::lonnet::logthis('Failed to create '.$source);
 8262:             return (&mt('Failed to create file'));
 8263:         }
 8264:         if ($action eq 'upload') {
 8265:             if (!print FH ($env{'form.'.$formname})) {
 8266:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 8267:                 return (&mt('Failed to write file'));
 8268:             }
 8269:         } else {
 8270:             my $original = &Apache::lonnet::filelocation('',$formname);
 8271:             if(!copy($original,$source)) {
 8272:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 8273:                 return (&mt('Failed to write file'));
 8274:             }
 8275:         }
 8276:         close(FH);
 8277:         chmod(0660, $source); # Permissions to rw-rw---.
 8278: 
 8279:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 8280:         my $copyfile=$targetdir.'/'.$file;
 8281: 
 8282:         my @parts=split(/\//,$targetdir);
 8283:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 8284:         for (my $count=5;$count<=$#parts;$count++) {
 8285:             $path.="/$parts[$count]";
 8286:             if (!-e $path) {
 8287:                 print $logfile "\nCreating directory ".$path;
 8288:                 mkdir($path,02770);
 8289:             }
 8290:         }
 8291:         my $versionresult;
 8292:         if (-e $copyfile) {
 8293:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 8294:         } else {
 8295:             $versionresult = 'ok';
 8296:         }
 8297:         if ($versionresult eq 'ok') {
 8298:             if (copy($source,$copyfile)) {
 8299:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 8300:                 $output = 'ok';
 8301:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 8302:                 push(@{$modified_urls},[$copyfile,$source]);
 8303:                 my $metaoutput = 
 8304:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 8305:                 unless ($registered_cleanup) {
 8306:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8307:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8308:                     $registered_cleanup=1;
 8309:                 }
 8310:             } else {
 8311:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 8312:                 $output = &mt('Failed to copy file to RES space').", $!";
 8313:             }
 8314:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 8315:                 my $inputfile = $filepath.'/'.$file;
 8316:                 my $outfile = $filepath.'/'.'tn-'.$file;
 8317:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 8318:                 if ($fullwidth ne '' && $fullheight ne '') { 
 8319:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 8320:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 8321:                         system("convert -sample $thumbsize $inputfile $outfile");
 8322:                         chmod(0660, $filepath.'/tn-'.$file);
 8323:                         if (-e $outfile) {
 8324:                             my $copyfile=$targetdir.'/tn-'.$file;
 8325:                             if (copy($outfile,$copyfile)) {
 8326:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 8327:                                 my $thumb_metaoutput = 
 8328:                                     &write_metadata($dom,$confname,$formname,
 8329:                                                     $targetdir,'tn-'.$file,$logfile);
 8330:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 8331:                                 unless ($registered_cleanup) {
 8332:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8333:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8334:                                     $registered_cleanup=1;
 8335:                                 }
 8336:                             } else {
 8337:                                 print $logfile "\nUnable to write ".$copyfile.
 8338:                                                ':'.$!."\n";
 8339:                             }
 8340:                         }
 8341:                     }
 8342:                 }
 8343:             }
 8344:         } else {
 8345:             $output = $versionresult;
 8346:         }
 8347:     }
 8348:     return ($output,$logourl);
 8349: }
 8350: 
 8351: sub logo_versioning {
 8352:     my ($targetdir,$file,$logfile) = @_;
 8353:     my $target = $targetdir.'/'.$file;
 8354:     my ($maxversion,$fn,$extn,$output);
 8355:     $maxversion = 0;
 8356:     if ($file =~ /^(.+)\.(\w+)$/) {
 8357:         $fn=$1;
 8358:         $extn=$2;
 8359:     }
 8360:     opendir(DIR,$targetdir);
 8361:     while (my $filename=readdir(DIR)) {
 8362:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 8363:             $maxversion=($1>$maxversion)?$1:$maxversion;
 8364:         }
 8365:     }
 8366:     $maxversion++;
 8367:     print $logfile "\nCreating old version ".$maxversion."\n";
 8368:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 8369:     if (copy($target,$copyfile)) {
 8370:         print $logfile "Copied old target to ".$copyfile."\n";
 8371:         $copyfile=$copyfile.'.meta';
 8372:         if (copy($target.'.meta',$copyfile)) {
 8373:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 8374:             $output = 'ok';
 8375:         } else {
 8376:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 8377:             $output = &mt('Failed to copy old meta').", $!, ";
 8378:         }
 8379:     } else {
 8380:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 8381:         $output = &mt('Failed to copy old target').", $!, ";
 8382:     }
 8383:     return $output;
 8384: }
 8385: 
 8386: sub write_metadata {
 8387:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 8388:     my (%metadatafields,%metadatakeys,$output);
 8389:     $metadatafields{'title'}=$formname;
 8390:     $metadatafields{'creationdate'}=time;
 8391:     $metadatafields{'lastrevisiondate'}=time;
 8392:     $metadatafields{'copyright'}='public';
 8393:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 8394:                                          $env{'user.domain'};
 8395:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 8396:     $metadatafields{'domain'}=$dom;
 8397:     {
 8398:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 8399:         my $mfh;
 8400:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 8401:             foreach (sort(keys(%metadatafields))) {
 8402:                 unless ($_=~/\./) {
 8403:                     my $unikey=$_;
 8404:                     $unikey=~/^([A-Za-z]+)/;
 8405:                     my $tag=$1;
 8406:                     $tag=~tr/A-Z/a-z/;
 8407:                     print $mfh "\n\<$tag";
 8408:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 8409:                         my $value=$metadatafields{$unikey.'.'.$_};
 8410:                         $value=~s/\"/\'\'/g;
 8411:                         print $mfh ' '.$_.'="'.$value.'"';
 8412:                     }
 8413:                     print $mfh '>'.
 8414:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 8415:                             .'</'.$tag.'>';
 8416:                 }
 8417:             }
 8418:             $output = 'ok';
 8419:             print $logfile "\nWrote metadata";
 8420:             close($mfh);
 8421:         } else {
 8422:             print $logfile "\nFailed to open metadata file";
 8423:             $output = &mt('Could not write metadata');
 8424:         }
 8425:     }
 8426:     return $output;
 8427: }
 8428: 
 8429: sub notifysubscribed {
 8430:     foreach my $targetsource (@{$modified_urls}){
 8431:         next unless (ref($targetsource) eq 'ARRAY');
 8432:         my ($target,$source)=@{$targetsource};
 8433:         if ($source ne '') {
 8434:             if (open(my $logfh,'>>'.$source.'.log')) {
 8435:                 print $logfh "\nCleanup phase: Notifications\n";
 8436:                 my @subscribed=&subscribed_hosts($target);
 8437:                 foreach my $subhost (@subscribed) {
 8438:                     print $logfh "\nNotifying host ".$subhost.':';
 8439:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 8440:                     print $logfh $reply;
 8441:                 }
 8442:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 8443:                 foreach my $subhost (@subscribedmeta) {
 8444:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 8445:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 8446:                                                         $subhost);
 8447:                     print $logfh $reply;
 8448:                 }
 8449:                 print $logfh "\n============ Done ============\n";
 8450:                 close($logfh);
 8451:             }
 8452:         }
 8453:     }
 8454:     return OK;
 8455: }
 8456: 
 8457: sub subscribed_hosts {
 8458:     my ($target) = @_;
 8459:     my @subscribed;
 8460:     if (open(my $fh,"<$target.subscription")) {
 8461:         while (my $subline=<$fh>) {
 8462:             if ($subline =~ /^($match_lonid):/) {
 8463:                 my $host = $1;
 8464:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 8465:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 8466:                         push(@subscribed,$host);
 8467:                     }
 8468:                 }
 8469:             }
 8470:         }
 8471:     }
 8472:     return @subscribed;
 8473: }
 8474: 
 8475: sub check_switchserver {
 8476:     my ($dom,$confname) = @_;
 8477:     my ($allowed,$switchserver);
 8478:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 8479:     if ($home eq 'no_host') {
 8480:         $home = &Apache::lonnet::domain($dom,'primary');
 8481:     }
 8482:     my @ids=&Apache::lonnet::current_machine_ids();
 8483:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 8484:     if (!$allowed) {
 8485: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 8486:     }
 8487:     return $switchserver;
 8488: }
 8489: 
 8490: sub modify_quotas {
 8491:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 8492:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 8493:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 8494:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 8495:         $validationfieldsref);
 8496:     if ($action eq 'quotas') {
 8497:         $context = 'tools'; 
 8498:     } else {
 8499:         $context = $action;
 8500:     }
 8501:     if ($context eq 'requestcourses') {
 8502:         @usertools = ('official','unofficial','community','textbook');
 8503:         @options =('norequest','approval','validate','autolimit');
 8504:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 8505:         %titles = &courserequest_titles();
 8506:         $toolregexp = join('|',@usertools);
 8507:         %conditions = &courserequest_conditions();
 8508:         $confname = $dom.'-domainconfig';
 8509:         my $servadm = $r->dir_config('lonAdmEMail');
 8510:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8511:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 8512:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 8513:     } elsif ($context eq 'requestauthor') {
 8514:         @usertools = ('author');
 8515:         %titles = &authorrequest_titles();
 8516:     } else {
 8517:         @usertools = ('aboutme','blog','webdav','portfolio');
 8518:         %titles = &tool_titles();
 8519:     }
 8520:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8521:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8522:     foreach my $key (keys(%env)) {
 8523:         if ($context eq 'requestcourses') {
 8524:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 8525:                 my $item = $1;
 8526:                 my $type = $2;
 8527:                 if ($type =~ /^limit_(.+)/) {
 8528:                     $limithash{$item}{$1} = $env{$key};
 8529:                 } else {
 8530:                     $confhash{$item}{$type} = $env{$key};
 8531:                 }
 8532:             }
 8533:         } elsif ($context eq 'requestauthor') {
 8534:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 8535:                 $confhash{$1} = $env{$key};
 8536:             }
 8537:         } else {
 8538:             if ($key =~ /^form\.quota_(.+)$/) {
 8539:                 $confhash{'defaultquota'}{$1} = $env{$key};
 8540:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 8541:                 $confhash{'authorquota'}{$1} = $env{$key};
 8542:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 8543:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 8544:             }
 8545:         }
 8546:     }
 8547:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 8548:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 8549:         @approvalnotify = sort(@approvalnotify);
 8550:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 8551:         my @crstypes = ('official','unofficial','community','textbook');
 8552:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 8553:         foreach my $type (@hasuniquecode) {
 8554:             if (grep(/^\Q$type\E$/,@crstypes)) {
 8555:                 $confhash{'uniquecode'}{$type} = 1;
 8556:             }
 8557:         }
 8558:         my (%newbook,%allpos);
 8559:         if ($context eq 'requestcourses') {
 8560:             foreach my $type ('textbooks','templates') {
 8561:                 @{$allpos{$type}} = (); 
 8562:                 my $invalid;
 8563:                 if ($type eq 'textbooks') {
 8564:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 8565:                 } else {
 8566:                     $invalid = &mt('Invalid LON-CAPA course for template');
 8567:                 }
 8568:                 if ($env{'form.'.$type.'_addbook'}) {
 8569:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 8570:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 8571:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 8572:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 8573:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8574:                         } else {
 8575:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 8576:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 8577:                             $position =~ s/\D+//g;
 8578:                             if ($position ne '') {
 8579:                                 $allpos{$type}[$position] = $newbook{$type};
 8580:                             }
 8581:                         }
 8582:                     } else {
 8583:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8584:                     }
 8585:                 }
 8586:             } 
 8587:         }
 8588:         if (ref($domconfig{$action}) eq 'HASH') {
 8589:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 8590:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 8591:                     $changes{'notify'}{'approval'} = 1;
 8592:                 }
 8593:             } else {
 8594:                 if ($confhash{'notify'}{'approval'}) {
 8595:                     $changes{'notify'}{'approval'} = 1;
 8596:                 }
 8597:             }
 8598:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 8599:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 8600:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 8601:                         unless ($confhash{'uniquecode'}{$crstype}) {
 8602:                             $changes{'uniquecode'} = 1;
 8603:                         }
 8604:                     }
 8605:                     unless ($changes{'uniquecode'}) {
 8606:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 8607:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 8608:                                 $changes{'uniquecode'} = 1;
 8609:                             }
 8610:                         }
 8611:                     }
 8612:                } else {
 8613:                    $changes{'uniquecode'} = 1;
 8614:                }
 8615:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 8616:                 $changes{'uniquecode'} = 1;
 8617:             }
 8618:             if ($context eq 'requestcourses') {
 8619:                 foreach my $type ('textbooks','templates') {
 8620:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8621:                         my %deletions;
 8622:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 8623:                         if (@todelete) {
 8624:                             map { $deletions{$_} = 1; } @todelete;
 8625:                         }
 8626:                         my %imgdeletions;
 8627:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 8628:                         if (@todeleteimages) {
 8629:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 8630:                         }
 8631:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 8632:                         for (my $i=0; $i<=$maxnum; $i++) {
 8633:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 8634:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 8635:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 8636:                                 if ($deletions{$key}) {
 8637:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 8638:                                         #FIXME need to obsolete item in RES space
 8639:                                     }
 8640:                                     next;
 8641:                                 } else {
 8642:                                     my $newpos = $env{'form.'.$itemid};
 8643:                                     $newpos =~ s/\D+//g;
 8644:                                     foreach my $item ('subject','title','publisher','author') {
 8645:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8646:                                                  ($type eq 'templates'));
 8647:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 8648:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 8649:                                             $changes{$type}{$key} = 1;
 8650:                                         }
 8651:                                     }
 8652:                                     $allpos{$type}[$newpos] = $key;
 8653:                                 }
 8654:                                 if ($imgdeletions{$key}) {
 8655:                                     $changes{$type}{$key} = 1;
 8656:                                     #FIXME need to obsolete item in RES space
 8657:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 8658:                                     my ($cdom,$cnum) = split(/_/,$key);
 8659:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 8660:                                                                                   $cdom,$cnum,$type,$configuserok,
 8661:                                                                                   $switchserver,$author_ok);
 8662:                                     if ($imgurl) {
 8663:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 8664:                                         $changes{$type}{$key} = 1; 
 8665:                                     }
 8666:                                     if ($error) {
 8667:                                         &Apache::lonnet::logthis($error);
 8668:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8669:                                     } 
 8670:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 8671:                                     $confhash{$type}{$key}{'image'} = 
 8672:                                         $domconfig{$action}{$type}{$key}{'image'};
 8673:                                 }
 8674:                             }
 8675:                         }
 8676:                     }
 8677:                 }
 8678:             }
 8679:         } else {
 8680:             if ($confhash{'notify'}{'approval'}) {
 8681:                 $changes{'notify'}{'approval'} = 1;
 8682:             }
 8683:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 8684:                 $changes{'uniquecode'} = 1;
 8685:             }
 8686:         }
 8687:         if ($context eq 'requestcourses') {
 8688:             foreach my $type ('textbooks','templates') {
 8689:                 if ($newbook{$type}) {
 8690:                     $changes{$type}{$newbook{$type}} = 1;
 8691:                     foreach my $item ('subject','title','publisher','author') {
 8692:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8693:                                  ($type eq 'template'));
 8694:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 8695:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 8696:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 8697:                         }
 8698:                     }
 8699:                     if ($type eq 'textbooks') {
 8700:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 8701:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 8702:                             my ($imageurl,$error) =
 8703:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 8704:                                                         $configuserok,$switchserver,$author_ok);
 8705:                             if ($imageurl) {
 8706:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 8707:                             }
 8708:                             if ($error) {
 8709:                                 &Apache::lonnet::logthis($error);
 8710:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8711:                             }
 8712:                         }
 8713:                     }
 8714:                 }
 8715:                 if (@{$allpos{$type}} > 0) {
 8716:                     my $idx = 0;
 8717:                     foreach my $item (@{$allpos{$type}}) {
 8718:                         if ($item ne '') {
 8719:                             $confhash{$type}{$item}{'order'} = $idx;
 8720:                             if (ref($domconfig{$action}) eq 'HASH') {
 8721:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8722:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 8723:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 8724:                                             $changes{$type}{$item} = 1;
 8725:                                         }
 8726:                                     }
 8727:                                 }
 8728:                             }
 8729:                             $idx ++;
 8730:                         }
 8731:                     }
 8732:                 }
 8733:             }
 8734:             if (ref($validationitemsref) eq 'ARRAY') {
 8735:                 foreach my $item (@{$validationitemsref}) {
 8736:                     if ($item eq 'fields') {
 8737:                         my @changed;
 8738:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 8739:                         if (@{$confhash{'validation'}{$item}} > 0) {
 8740:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 8741:                         }
 8742:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8743:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8744:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 8745:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 8746:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 8747:                                 } else {
 8748:                                     @changed = @{$confhash{'validation'}{$item}};
 8749:                                 }
 8750:                             } else {
 8751:                                 @changed = @{$confhash{'validation'}{$item}};
 8752:                             }
 8753:                         } else {
 8754:                             @changed = @{$confhash{'validation'}{$item}};
 8755:                         }
 8756:                         if (@changed) {
 8757:                             if ($confhash{'validation'}{$item}) {
 8758:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 8759:                             } else {
 8760:                                 $changes{'validation'}{$item} = &mt('None');
 8761:                             }
 8762:                         }
 8763:                     } else {
 8764:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 8765:                         if ($item eq 'markup') {
 8766:                             if ($env{'form.requestcourses_validation_'.$item}) {
 8767:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 8768:                             }
 8769:                         }
 8770:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8771:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8772:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 8773:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8774:                                 }
 8775:                             } else {
 8776:                                 if ($confhash{'validation'}{$item} ne '') {
 8777:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8778:                                 }
 8779:                             }
 8780:                         } else {
 8781:                             if ($confhash{'validation'}{$item} ne '') {
 8782:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 8783:                             }
 8784:                         }
 8785:                     }
 8786:                 }
 8787:             }
 8788:             if ($env{'form.validationdc'}) {
 8789:                 my $newval = $env{'form.validationdc'};
 8790:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 8791:                 if (exists($domcoords{$newval})) {
 8792:                     $confhash{'validation'}{'dc'} = $newval;
 8793:                 }
 8794:             }
 8795:             if (ref($confhash{'validation'}) eq 'HASH') {
 8796:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8797:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8798:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8799:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8800:                                 if ($confhash{'validation'}{'dc'} eq '') {
 8801:                                     $changes{'validation'}{'dc'} = &mt('None');
 8802:                                 } else {
 8803:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8804:                                 }
 8805:                             }
 8806:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 8807:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8808:                         }
 8809:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 8810:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8811:                     }
 8812:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 8813:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 8814:                 }
 8815:             } else {
 8816:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 8817:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 8818:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 8819:                             $changes{'validation'}{'dc'} = &mt('None');
 8820:                         }
 8821:                     }
 8822:                 }
 8823:             }
 8824:         }
 8825:     } else {
 8826:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 8827:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 8828:     }
 8829:     foreach my $item (@usertools) {
 8830:         foreach my $type (@{$types},'default','_LC_adv') {
 8831:             my $unset; 
 8832:             if ($context eq 'requestcourses') {
 8833:                 $unset = '0';
 8834:                 if ($type eq '_LC_adv') {
 8835:                     $unset = '';
 8836:                 }
 8837:                 if ($confhash{$item}{$type} eq 'autolimit') {
 8838:                     $confhash{$item}{$type} .= '=';
 8839:                     unless ($limithash{$item}{$type} =~ /\D/) {
 8840:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 8841:                     }
 8842:                 }
 8843:             } elsif ($context eq 'requestauthor') {
 8844:                 $unset = '0';
 8845:                 if ($type eq '_LC_adv') {
 8846:                     $unset = '';
 8847:                 }
 8848:             } else {
 8849:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 8850:                     $confhash{$item}{$type} = 1;
 8851:                 } else {
 8852:                     $confhash{$item}{$type} = 0;
 8853:                 }
 8854:             }
 8855:             if (ref($domconfig{$action}) eq 'HASH') {
 8856:                 if ($action eq 'requestauthor') {
 8857:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 8858:                         $changes{$type} = 1;
 8859:                     }
 8860:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 8861:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 8862:                         $changes{$item}{$type} = 1;
 8863:                     }
 8864:                 } else {
 8865:                     if ($context eq 'requestcourses') {
 8866:                         if ($confhash{$item}{$type} ne $unset) {
 8867:                             $changes{$item}{$type} = 1;
 8868:                         }
 8869:                     } else {
 8870:                         if (!$confhash{$item}{$type}) {
 8871:                             $changes{$item}{$type} = 1;
 8872:                         }
 8873:                     }
 8874:                 }
 8875:             } else {
 8876:                 if ($context eq 'requestcourses') {
 8877:                     if ($confhash{$item}{$type} ne $unset) {
 8878:                         $changes{$item}{$type} = 1;
 8879:                     }
 8880:                 } elsif ($context eq 'requestauthor') {
 8881:                     if ($confhash{$type} ne $unset) {
 8882:                         $changes{$type} = 1;
 8883:                     }
 8884:                 } else {
 8885:                     if (!$confhash{$item}{$type}) {
 8886:                         $changes{$item}{$type} = 1;
 8887:                     }
 8888:                 }
 8889:             }
 8890:         }
 8891:     }
 8892:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 8893:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 8894:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 8895:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 8896:                     if (exists($confhash{'defaultquota'}{$key})) {
 8897:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 8898:                             $changes{'defaultquota'}{$key} = 1;
 8899:                         }
 8900:                     } else {
 8901:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 8902:                     }
 8903:                 }
 8904:             } else {
 8905:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 8906:                     if (exists($confhash{'defaultquota'}{$key})) {
 8907:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 8908:                             $changes{'defaultquota'}{$key} = 1;
 8909:                         }
 8910:                     } else {
 8911:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 8912:                     }
 8913:                 }
 8914:             }
 8915:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 8916:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 8917:                     if (exists($confhash{'authorquota'}{$key})) {
 8918:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 8919:                             $changes{'authorquota'}{$key} = 1;
 8920:                         }
 8921:                     } else {
 8922:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 8923:                     }
 8924:                 }
 8925:             }
 8926:         }
 8927:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 8928:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 8929:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 8930:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 8931:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 8932:                             $changes{'defaultquota'}{$key} = 1;
 8933:                         }
 8934:                     } else {
 8935:                         if (!exists($domconfig{'quotas'}{$key})) {
 8936:                             $changes{'defaultquota'}{$key} = 1;
 8937:                         }
 8938:                     }
 8939:                 } else {
 8940:                     $changes{'defaultquota'}{$key} = 1;
 8941:                 }
 8942:             }
 8943:         }
 8944:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 8945:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 8946:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 8947:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 8948:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 8949:                             $changes{'authorquota'}{$key} = 1;
 8950:                         }
 8951:                     } else {
 8952:                         $changes{'authorquota'}{$key} = 1;
 8953:                     }
 8954:                 } else {
 8955:                     $changes{'authorquota'}{$key} = 1;
 8956:                 }
 8957:             }
 8958:         }
 8959:     }
 8960: 
 8961:     if ($context eq 'requestauthor') {
 8962:         $domdefaults{'requestauthor'} = \%confhash;
 8963:     } else {
 8964:         foreach my $key (keys(%confhash)) {
 8965:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 8966:                 $domdefaults{$key} = $confhash{$key};
 8967:             }
 8968:         }
 8969:     }
 8970: 
 8971:     my %quotahash = (
 8972:                       $action => { %confhash }
 8973:                     );
 8974:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 8975:                                              $dom);
 8976:     if ($putresult eq 'ok') {
 8977:         if (keys(%changes) > 0) {
 8978:             my $cachetime = 24*60*60;
 8979:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8980:             if (ref($lastactref) eq 'HASH') {
 8981:                 $lastactref->{'domdefaults'} = 1;
 8982:             }
 8983:             $resulttext = &mt('Changes made:').'<ul>';
 8984:             unless (($context eq 'requestcourses') ||
 8985:                     ($context eq 'requestauthor')) {
 8986:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 8987:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 8988:                     foreach my $type (@{$types},'default') {
 8989:                         if (defined($changes{'defaultquota'}{$type})) {
 8990:                             my $typetitle = $usertypes->{$type};
 8991:                             if ($type eq 'default') {
 8992:                                 $typetitle = $othertitle;
 8993:                             }
 8994:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 8995:                         }
 8996:                     }
 8997:                     $resulttext .= '</ul></li>';
 8998:                 }
 8999:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 9000:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 9001:                     foreach my $type (@{$types},'default') {
 9002:                         if (defined($changes{'authorquota'}{$type})) {
 9003:                             my $typetitle = $usertypes->{$type};
 9004:                             if ($type eq 'default') {
 9005:                                 $typetitle = $othertitle;
 9006:                             }
 9007:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 9008:                         }
 9009:                     }
 9010:                     $resulttext .= '</ul></li>';
 9011:                 }
 9012:             }
 9013:             my %newenv;
 9014:             foreach my $item (@usertools) {
 9015:                 my (%haschgs,%inconf);
 9016:                 if ($context eq 'requestauthor') {
 9017:                     %haschgs = %changes;
 9018:                     %inconf = %confhash;
 9019:                 } else {
 9020:                     if (ref($changes{$item}) eq 'HASH') {
 9021:                         %haschgs = %{$changes{$item}};
 9022:                     }
 9023:                     if (ref($confhash{$item}) eq 'HASH') {
 9024:                         %inconf = %{$confhash{$item}};
 9025:                     }
 9026:                 }
 9027:                 if (keys(%haschgs) > 0) {
 9028:                     my $newacc = 
 9029:                         &Apache::lonnet::usertools_access($env{'user.name'},
 9030:                                                           $env{'user.domain'},
 9031:                                                           $item,'reload',$context);
 9032:                     if (($context eq 'requestcourses') ||
 9033:                         ($context eq 'requestauthor')) {
 9034:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 9035:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 9036:                         }
 9037:                     } else {
 9038:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 9039:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 9040:                         }
 9041:                     }
 9042:                     unless ($context eq 'requestauthor') {
 9043:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 9044:                     }
 9045:                     foreach my $type (@{$types},'default','_LC_adv') {
 9046:                         if ($haschgs{$type}) {
 9047:                             my $typetitle = $usertypes->{$type};
 9048:                             if ($type eq 'default') {
 9049:                                 $typetitle = $othertitle;
 9050:                             } elsif ($type eq '_LC_adv') {
 9051:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 9052:                             }
 9053:                             if ($inconf{$type}) {
 9054:                                 if ($context eq 'requestcourses') {
 9055:                                     my $cond;
 9056:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 9057:                                         if ($1 eq '') {
 9058:                                             $cond = &mt('(Automatic processing of any request).');
 9059:                                         } else {
 9060:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 9061:                                         }
 9062:                                     } else { 
 9063:                                         $cond = $conditions{$inconf{$type}};
 9064:                                     }
 9065:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 9066:                                 } elsif ($context eq 'requestauthor') {
 9067:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 9068:                                                              $titles{$inconf{$type}},$typetitle);
 9069: 
 9070:                                 } else {
 9071:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 9072:                                 }
 9073:                             } else {
 9074:                                 if ($type eq '_LC_adv') {
 9075:                                     if ($inconf{$type} eq '0') {
 9076:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9077:                                     } else { 
 9078:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 9079:                                     }
 9080:                                 } else {
 9081:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9082:                                 }
 9083:                             }
 9084:                         }
 9085:                     }
 9086:                     unless ($context eq 'requestauthor') {
 9087:                         $resulttext .= '</ul></li>';
 9088:                     }
 9089:                 }
 9090:             }
 9091:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 9092:                 if (ref($changes{'notify'}) eq 'HASH') {
 9093:                     if ($changes{'notify'}{'approval'}) {
 9094:                         if (ref($confhash{'notify'}) eq 'HASH') {
 9095:                             if ($confhash{'notify'}{'approval'}) {
 9096:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 9097:                             } else {
 9098:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 9099:                             }
 9100:                         }
 9101:                     }
 9102:                 }
 9103:             }
 9104:             if ($action eq 'requestcourses') {
 9105:                 my @offon = ('off','on');
 9106:                 if ($changes{'uniquecode'}) {
 9107:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9108:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 9109:                         $resulttext .= '<li>'.
 9110:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 9111:                                        '</li>';
 9112:                     } else {
 9113:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 9114:                                        '</li>';
 9115:                     }
 9116:                 }
 9117:                 foreach my $type ('textbooks','templates') {
 9118:                     if (ref($changes{$type}) eq 'HASH') {
 9119:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 9120:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 9121:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 9122:                             my $coursetitle = $coursehash{'description'};
 9123:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 9124:                             $resulttext .= '<li>';
 9125:                             foreach my $item ('subject','title','publisher','author') {
 9126:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9127:                                          ($type eq 'templates'));
 9128:                                 my $name = $item.':';
 9129:                                 $name =~ s/^(\w)/\U$1/;
 9130:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 9131:                             }
 9132:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 9133:                             if ($type eq 'textbooks') {
 9134:                                 if ($confhash{$type}{$key}{'image'}) {
 9135:                                     $resulttext .= ' '.&mt('Image: [_1]',
 9136:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 9137:                                                    ' alt="Textbook cover" />').'<br />';
 9138:                                 }
 9139:                             }
 9140:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 9141:                         }
 9142:                         $resulttext .= '</ul></li>';
 9143:                     }
 9144:                 }
 9145:                 if (ref($changes{'validation'}) eq 'HASH') {
 9146:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 9147:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 9148:                         foreach my $item (@{$validationitemsref}) {
 9149:                             if (exists($changes{'validation'}{$item})) {
 9150:                                 if ($item eq 'markup') {
 9151:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9152:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 9153:                                 } else {
 9154:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9155:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 9156:                                 }
 9157:                             }
 9158:                         }
 9159:                         if (exists($changes{'validation'}{'dc'})) {
 9160:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 9161:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 9162:                         }
 9163:                     }
 9164:                 }
 9165:             }
 9166:             $resulttext .= '</ul>';
 9167:             if (keys(%newenv)) {
 9168:                 &Apache::lonnet::appenv(\%newenv);
 9169:             }
 9170:         } else {
 9171:             if ($context eq 'requestcourses') {
 9172:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 9173:             } elsif ($context eq 'requestauthor') {
 9174:                 $resulttext = &mt('No changes made to rights to request author space.');
 9175:             } else {
 9176:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 9177:             }
 9178:         }
 9179:     } else {
 9180:         $resulttext = '<span class="LC_error">'.
 9181: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9182:     }
 9183:     if ($errors) {
 9184:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 9185:                        '<ul>'.$errors.'</ul></p>';
 9186:     }
 9187:     return $resulttext;
 9188: }
 9189: 
 9190: sub process_textbook_image {
 9191:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 9192:     my $filename = $env{'form.'.$caller.'.filename'};
 9193:     my ($error,$url);
 9194:     my ($width,$height) = (50,50);
 9195:     if ($configuserok eq 'ok') {
 9196:         if ($switchserver) {
 9197:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 9198:                          $switchserver);
 9199:         } elsif ($author_ok eq 'ok') {
 9200:             my ($result,$imageurl) =
 9201:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9202:                              "$type/$dom/$cnum/cover",$width,$height);
 9203:             if ($result eq 'ok') {
 9204:                 $url = $imageurl;
 9205:             } else {
 9206:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9207:             }
 9208:         } else {
 9209:             $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);
 9210:         }
 9211:     } else {
 9212:         $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);
 9213:     }
 9214:     return ($url,$error);
 9215: }
 9216: 
 9217: sub modify_ltitools {
 9218:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 9219:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9220:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
 9221:     my $confname = $dom.'-domainconfig';
 9222:     my $servadm = $r->dir_config('lonAdmEMail');
 9223:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9224:     my (%posslti,%possfield);
 9225:     my @courseroles = ('cc','in','ta','ep','st');
 9226:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 9227:     map { $posslti{$_} = 1; } @ltiroles;
 9228:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
 9229:     map { $possfield{$_} = 1; } @allfields;
 9230:     my %lt = &ltitools_names();
 9231:     if ($env{'form.ltitools_add'}) {
 9232:         my $title = $env{'form.ltitools_add_title'};
 9233:         $title =~ s/(`)/'/g;
 9234:         ($newid,my $error) = &get_ltitools_id($dom,$title);
 9235:         if ($newid) {
 9236:             my $position = $env{'form.ltitools_add_pos'};
 9237:             $position =~ s/\D+//g;
 9238:             if ($position ne '') {
 9239:                 $allpos[$position] = $newid;
 9240:             }
 9241:             $changes{$newid} = 1;
 9242:             foreach my $item ('title','url','key','secret','lifetime') {
 9243:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
 9244:                 if ($item eq 'lifetime') {
 9245:                     $env{'form.ltitools_add_'.$item} =~ s/[^\d.]//g;
 9246:                 }
 9247:                 if ($env{'form.ltitools_add_'.$item}) {
 9248:                     if (($item eq 'key') || ($item eq 'secret')) {
 9249:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
 9250:                     } else {
 9251:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
 9252:                     }
 9253:                 }
 9254:             }
 9255:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
 9256:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
 9257:             }
 9258:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
 9259:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
 9260:             }
 9261:             if ($env{'form.ltitools_add_sigmethod'} eq 'HMAC-SHA256') {
 9262:                 $confhash{$newid}{'sigmethod'} = $env{'form.ltitools_add_sigmethod'};
 9263:             } else {
 9264:                 $confhash{$newid}{'sigmethod'} = 'HMAC-SHA1';
 9265:             }
 9266:             foreach my $item ('width','height','linktext','explanation') {
 9267:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
 9268:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
 9269:                 if (($item eq 'width') || ($item eq 'height')) {
 9270:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
 9271:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
 9272:                     }
 9273:                 } else {
 9274:                     if ($env{'form.ltitools_add_'.$item} ne '') {
 9275:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
 9276:                     }
 9277:                 }
 9278:             }
 9279:             if ($env{'form.ltitools_add_target'} eq 'window') {
 9280:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
 9281:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
 9282:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
 9283:             } else {
 9284:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
 9285:             }
 9286:             if ($env{'form.ltitools_add_image.filename'} ne '') {
 9287:                 my ($imageurl,$error) =
 9288:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
 9289:                                             $configuserok,$switchserver,$author_ok);
 9290:                 if ($imageurl) {
 9291:                     $confhash{$newid}{'image'} = $imageurl;
 9292:                 }
 9293:                 if ($error) {
 9294:                     &Apache::lonnet::logthis($error);
 9295:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9296:                 }
 9297:             }
 9298:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
 9299:             foreach my $field (@fields) {
 9300:                 if ($possfield{$field}) {
 9301:                     if ($field eq 'roles') {
 9302:                         foreach my $role (@courseroles) {
 9303:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
 9304:                             if (($choice ne '') && ($posslti{$choice})) {
 9305:                                 $confhash{$newid}{'roles'}{$role} = $choice;
 9306:                                 if ($role eq 'cc') {
 9307:                                     $confhash{$newid}{'roles'}{'co'} = $choice;
 9308:                                 }
 9309:                             }
 9310:                         }
 9311:                     } else {
 9312:                         $confhash{$newid}{'fields'}{$field} = 1;
 9313:                     }
 9314:                 }
 9315:             }
 9316:             if (ref($confhash{$newid}{'fields'}) eq 'HASH') {
 9317:                 if ($confhash{$newid}{'fields'}{'user'}) {
 9318:                     if ($env{'form.ltitools_userincdom_add'}) {
 9319:                         $confhash{$newid}{'incdom'} = 1;
 9320:                     }
 9321:                 }
 9322:             }
 9323:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
 9324:             foreach my $item (@courseconfig) {
 9325:                 $confhash{$newid}{'crsconf'}{$item} = 1;
 9326:             }
 9327:             if ($env{'form.ltitools_add_custom'}) {
 9328:                 my $name = $env{'form.ltitools_add_custom_name'};
 9329:                 my $value = $env{'form.ltitools_add_custom_value'};
 9330:                 $value =~ s/(`)/'/g;
 9331:                 $name =~ s/(`)/'/g;
 9332:                 $confhash{$newid}{'custom'}{$name} = $value;
 9333:             }
 9334:         } else {
 9335:             my $error = &mt('Failed to acquire unique ID for new external tool');
 9336:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9337:         }
 9338:     }
 9339:     if (ref($domconfig{$action}) eq 'HASH') {
 9340:         my %deletions;
 9341:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
 9342:         if (@todelete) {
 9343:             map { $deletions{$_} = 1; } @todelete;
 9344:         }
 9345:         my %customadds;
 9346:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
 9347:         if (@newcustom) {
 9348:             map { $customadds{$_} = 1; } @newcustom;
 9349:         }
 9350:         my %imgdeletions;
 9351:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
 9352:         if (@todeleteimages) {
 9353:             map { $imgdeletions{$_} = 1; } @todeleteimages;
 9354:         }
 9355:         my $maxnum = $env{'form.ltitools_maxnum'};
 9356:         for (my $i=0; $i<=$maxnum; $i++) {
 9357:             my $itemid = $env{'form.ltitools_id_'.$i};
 9358:             $itemid =~ s/\D+//g;
 9359:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
 9360:                 if ($deletions{$itemid}) {
 9361:                     if ($domconfig{$action}{$itemid}{'image'}) {
 9362:                         #FIXME need to obsolete item in RES space
 9363:                     }
 9364:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
 9365:                     next;
 9366:                 } else {
 9367:                     my $newpos = $env{'form.ltitools_'.$itemid};
 9368:                     $newpos =~ s/\D+//g;
 9369:                     foreach my $item ('title','url','lifetime') {
 9370:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9371:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
 9372:                             $changes{$itemid} = 1;
 9373:                         }
 9374:                     }
 9375:                     foreach my $item ('key','secret') {
 9376:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9377:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
 9378:                             $changes{$itemid} = 1;
 9379:                         }
 9380:                     }
 9381:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
 9382:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
 9383:                     }
 9384:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
 9385:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
 9386:                     }
 9387:                     if ($env{'form.ltitools_sigmethod_'.$i} eq 'HMAC-SHA256') {
 9388:                         $confhash{$itemid}{'sigmethod'} = $env{'form.ltitools_sigmethod_'.$i};
 9389:                     } else {
 9390:                         $confhash{$itemid}{'sigmethod'} = 'HMAC-SHA1';
 9391:                     }
 9392:                     if ($domconfig{$action}{$itemid}{'sigmethod'} eq '') {
 9393:                         if ($confhash{$itemid}{'sigmethod'} ne 'HMAC-SHA1') {
 9394:                             $changes{$itemid} = 1;
 9395:                         }
 9396:                     } elsif ($domconfig{$action}{$itemid}{'sigmethod'} ne $confhash{$itemid}{'sigmethod'}) {
 9397:                         $changes{$itemid} = 1;
 9398:                     }
 9399:                     foreach my $size ('width','height') {
 9400:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
 9401:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
 9402:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
 9403:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
 9404:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9405:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
 9406:                                     $changes{$itemid} = 1;
 9407:                                 }
 9408:                             } else {
 9409:                                 $changes{$itemid} = 1;
 9410:                             }
 9411:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9412:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
 9413:                                 $changes{$itemid} = 1;
 9414:                             }
 9415:                         }
 9416:                     }
 9417:                     foreach my $item ('linktext','explanation') {
 9418:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
 9419:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
 9420:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
 9421:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9422:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9423:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
 9424:                                     $changes{$itemid} = 1;
 9425:                                 }
 9426:                             } else {
 9427:                                 $changes{$itemid} = 1;
 9428:                             }
 9429:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9430:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
 9431:                                 $changes{$itemid} = 1;
 9432:                             }
 9433:                         }
 9434:                     }
 9435:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
 9436:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
 9437:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
 9438:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
 9439:                     } else {
 9440:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
 9441:                     }
 9442:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9443:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
 9444:                             $changes{$itemid} = 1;
 9445:                         }
 9446:                     } else {
 9447:                         $changes{$itemid} = 1;
 9448:                     }
 9449:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
 9450:                     foreach my $item ('label','title','target','linktext','explanation','append') {
 9451:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
 9452:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
 9453:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
 9454:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
 9455:                                     $changes{$itemid} = 1;
 9456:                                 }
 9457:                             } else {
 9458:                                 $changes{$itemid} = 1;
 9459:                             }
 9460:                         }
 9461:                     }
 9462:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
 9463:                     foreach my $field (@fields) {
 9464:                         if ($possfield{$field}) {
 9465:                             if ($field eq 'roles') {
 9466:                                 foreach my $role (@courseroles) {
 9467:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
 9468:                                     if (($choice ne '') && ($posslti{$choice})) {
 9469:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
 9470:                                         if ($role eq 'cc') {
 9471:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
 9472:                                         }
 9473:                                     }
 9474:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
 9475:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
 9476:                                             $changes{$itemid} = 1;
 9477:                                         }
 9478:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
 9479:                                         $changes{$itemid} = 1;
 9480:                                     }
 9481:                                 }
 9482:                             } else {
 9483:                                 $confhash{$itemid}{'fields'}{$field} = 1;
 9484:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
 9485:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
 9486:                                         $changes{$itemid} = 1;
 9487:                                     }
 9488:                                 } else {
 9489:                                     $changes{$itemid} = 1;
 9490:                                 }
 9491:                             }
 9492:                         }
 9493:                     }
 9494:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
 9495:                         if ($confhash{$itemid}{'fields'}{'user'}) {
 9496:                             if ($env{'form.ltitools_userincdom_'.$i}) {
 9497:                                 $confhash{$itemid}{'incdom'} = 1;
 9498:                             }
 9499:                             if ($domconfig{$action}{$itemid}{'incdom'} ne $confhash{$itemid}{'incdom'}) {
 9500:                                 $changes{$itemid} = 1;
 9501:                             }
 9502:                         }
 9503:                     }
 9504:                     $allpos[$newpos] = $itemid;
 9505:                 }
 9506:                 if ($imgdeletions{$itemid}) {
 9507:                     $changes{$itemid} = 1;
 9508:                     #FIXME need to obsolete item in RES space
 9509:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
 9510:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
 9511:                                                                  $itemid,$configuserok,$switchserver,
 9512:                                                                  $author_ok);
 9513:                     if ($imgurl) {
 9514:                         $confhash{$itemid}{'image'} = $imgurl;
 9515:                         $changes{$itemid} = 1;
 9516:                     }
 9517:                     if ($error) {
 9518:                         &Apache::lonnet::logthis($error);
 9519:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9520:                     }
 9521:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
 9522:                     $confhash{$itemid}{'image'} =
 9523:                        $domconfig{$action}{$itemid}{'image'};
 9524:                 }
 9525:                 if ($customadds{$i}) {
 9526:                     my $name = $env{'form.ltitools_custom_name_'.$i};
 9527:                     $name =~ s/(`)/'/g;
 9528:                     $name =~ s/^\s+//;
 9529:                     $name =~ s/\s+$//;
 9530:                     my $value = $env{'form.ltitools_custom_value_'.$i};
 9531:                     $value =~ s/(`)/'/g;
 9532:                     $value =~ s/^\s+//;
 9533:                     $value =~ s/\s+$//;
 9534:                     if ($name ne '') {
 9535:                         $confhash{$itemid}{'custom'}{$name} = $value;
 9536:                         $changes{$itemid} = 1;
 9537:                     }
 9538:                 }
 9539:                 my %customdels;
 9540:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i);
 9541:                 if (@customdeletions) {
 9542:                     $changes{$itemid} = 1;
 9543:                 }
 9544:                 map { $customdels{$_} = 1; } @customdeletions;
 9545:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
 9546:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
 9547:                         unless ($customdels{$key}) {
 9548:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
 9549:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i};
 9550:                             }
 9551:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
 9552:                                 $changes{$itemid} = 1;
 9553:                             }
 9554:                         }
 9555:                     }
 9556:                 }
 9557:                 unless ($changes{$itemid}) {
 9558:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
 9559:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
 9560:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
 9561:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
 9562:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
 9563:                                         $changes{$itemid} = 1;
 9564:                                         last;
 9565:                                     }
 9566:                                 }
 9567:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
 9568:                                 $changes{$itemid} = 1;
 9569:                             }
 9570:                         }
 9571:                         last if ($changes{$itemid});
 9572:                     }
 9573:                 }
 9574:             }
 9575:         }
 9576:     }
 9577:     if (@allpos > 0) {
 9578:         my $idx = 0;
 9579:         foreach my $itemid (@allpos) {
 9580:             if ($itemid ne '') {
 9581:                 $confhash{$itemid}{'order'} = $idx;
 9582:                 if (ref($domconfig{$action}) eq 'HASH') {
 9583:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
 9584:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
 9585:                             $changes{$itemid} = 1;
 9586:                         }
 9587:                     }
 9588:                 }
 9589:                 $idx ++;
 9590:             }
 9591:         }
 9592:     }
 9593:     my %ltitoolshash = (
 9594:                           $action => { %confhash }
 9595:                        );
 9596:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
 9597:                                              $dom);
 9598:     if ($putresult eq 'ok') {
 9599:         my %ltienchash = (
 9600:                              $action => { %encconfig }
 9601:                          );
 9602:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
 9603:         if (keys(%changes) > 0) {
 9604:             my $cachetime = 24*60*60;
 9605:             my %ltiall = %confhash;
 9606:             foreach my $id (keys(%ltiall)) {
 9607:                 if (ref($encconfig{$id}) eq 'HASH') {
 9608:                     foreach my $item ('key','secret') {
 9609:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
 9610:                     }
 9611:                 }
 9612:             }
 9613:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
 9614:             if (ref($lastactref) eq 'HASH') {
 9615:                 $lastactref->{'ltitools'} = 1;
 9616:             }
 9617:             $resulttext = &mt('Changes made:').'<ul>';
 9618:             my %bynum;
 9619:             foreach my $itemid (sort(keys(%changes))) {
 9620:                 my $position = $confhash{$itemid}{'order'};
 9621:                 $bynum{$position} = $itemid;
 9622:             }
 9623:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
 9624:                 my $itemid = $bynum{$pos};
 9625:                 if (ref($confhash{$itemid}) ne 'HASH') {
 9626:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
 9627:                 } else {
 9628:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
 9629:                     if ($confhash{$itemid}{'image'}) {
 9630:                         $resulttext .= '&nbsp;'.
 9631:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
 9632:                                        ' alt="'.&mt('Tool Provider icon').'" />';
 9633:                     }
 9634:                     $resulttext .= '</li><ul>';
 9635:                     my $position = $pos + 1;
 9636:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
 9637:                     foreach my $item ('version','msgtype','sigmethod','url','lifetime') {
 9638:                         if ($confhash{$itemid}{$item} ne '') {
 9639:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
 9640:                         }
 9641:                     }
 9642:                     if ($encconfig{$itemid}{'key'} ne '') {
 9643:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
 9644:                     }
 9645:                     if ($encconfig{$itemid}{'secret'} ne '') {
 9646:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
 9647:                         my $num = length($encconfig{$itemid}{'secret'});
 9648:                         $resulttext .= ('*'x$num).'</li>';
 9649:                     }
 9650:                     $resulttext .= '<li>'.&mt('Configurable in course:');
 9651:                     my @possconfig = ('label','title','target','linktext','explanation','append');
 9652:                     my $numconfig = 0;
 9653:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') {
 9654:                         foreach my $item (@possconfig) {
 9655:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
 9656:                                 $numconfig ++;
 9657:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
 9658:                             }
 9659:                         }
 9660:                     }
 9661:                     if (!$numconfig) {
 9662:                         $resulttext .= &mt('None');
 9663:                     }
 9664:                     $resulttext .= '</li>';
 9665:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
 9666:                         my $displaylist;
 9667:                         if ($confhash{$itemid}{'display'}{'target'}) {
 9668:                             $displaylist = &mt('Display target').':&nbsp;'.
 9669:                                            $confhash{$itemid}{'display'}{'target'}.',';
 9670:                         }
 9671:                         foreach my $size ('width','height') {
 9672:                             if ($confhash{$itemid}{'display'}{$size}) {
 9673:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
 9674:                                                 $confhash{$itemid}{'display'}{$size}.',';
 9675:                             }
 9676:                         }
 9677:                         if ($displaylist) {
 9678:                             $displaylist =~ s/,$//;
 9679:                             $resulttext .= '<li>'.$displaylist.'</li>';
 9680:                         }
 9681:                         foreach my $item ('linktext','explanation') {
 9682:                             if ($confhash{$itemid}{'display'}{$item}) {
 9683:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
 9684:                             }
 9685:                         }
 9686:                     }
 9687:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
 9688:                         my $fieldlist;
 9689:                         foreach my $field (@allfields) {
 9690:                             if ($confhash{$itemid}{'fields'}{$field}) {
 9691:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
 9692:                             }
 9693:                         }
 9694:                         if ($fieldlist) {
 9695:                             $fieldlist =~ s/,$//;
 9696:                             if ($confhash{$itemid}{'fields'}{'user'}) {
 9697:                                 if ($confhash{$itemid}{'incdom'}) {
 9698:                                     $fieldlist .= ' ('.&mt('username:domain').')';
 9699:                                 } else {
 9700:                                     $fieldlist .= ' ('.&mt('username').')';
 9701:                                 }
 9702:                             }
 9703:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
 9704:                         }
 9705:                     }
 9706:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
 9707:                         my $rolemaps;
 9708:                         foreach my $role (@courseroles) {
 9709:                             if ($confhash{$itemid}{'roles'}{$role}) {
 9710:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
 9711:                                              $confhash{$itemid}{'roles'}{$role}.',';
 9712:                             }
 9713:                         }
 9714:                         if ($rolemaps) {
 9715:                             $rolemaps =~ s/,$//;
 9716:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
 9717:                         }
 9718:                     }
 9719:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
 9720:                         my $customlist;
 9721:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
 9722:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
 9723:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
 9724:                             }
 9725:                         }
 9726:                         if ($customlist) {
 9727:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
 9728:                         }
 9729:                     }
 9730:                     $resulttext .= '</ul></li>';
 9731:                 }
 9732:             }
 9733:             $resulttext .= '</ul>';
 9734:         } else {
 9735:             $resulttext = &mt('No changes made.');
 9736:         }
 9737:     } else {
 9738:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
 9739:     }
 9740:     if ($errors) {
 9741:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9742:                        $errors.'</ul>';
 9743:     }
 9744:     return $resulttext;
 9745: }
 9746: 
 9747: sub process_ltitools_image {
 9748:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
 9749:     my $filename = $env{'form.'.$caller.'.filename'};
 9750:     my ($error,$url);
 9751:     my ($width,$height) = (21,21);
 9752:     if ($configuserok eq 'ok') {
 9753:         if ($switchserver) {
 9754:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
 9755:                          $switchserver);
 9756:         } elsif ($author_ok eq 'ok') {
 9757:             my ($result,$imageurl,$madethumb) =
 9758:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9759:                              "ltitools/$itemid/icon",$width,$height);
 9760:             if ($result eq 'ok') {
 9761:                 if ($madethumb) {
 9762:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
 9763:                     my $imagethumb = "$path/tn-".$imagefile;
 9764:                     $url = $imagethumb;
 9765:                 } else {
 9766:                     $url = $imageurl;
 9767:                 }
 9768:             } else {
 9769:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9770:             }
 9771:         } else {
 9772:             $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);
 9773:         }
 9774:     } else {
 9775:         $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);
 9776:     }
 9777:     return ($url,$error);
 9778: }
 9779: 
 9780: sub get_ltitools_id {
 9781:     my ($cdom,$title) = @_;
 9782:     # get lock on ltitools db
 9783:     my $lockhash = {
 9784:                       lock => $env{'user.name'}.
 9785:                               ':'.$env{'user.domain'},
 9786:                    };
 9787:     my $tries = 0;
 9788:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
 9789:     my ($id,$error);
 9790: 
 9791:     while (($gotlock ne 'ok') && ($tries<10)) {
 9792:         $tries ++;
 9793:         sleep (0.1);
 9794:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
 9795:     }
 9796:     if ($gotlock eq 'ok') {
 9797:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
 9798:         if ($currids{'lock'}) {
 9799:             delete($currids{'lock'});
 9800:             if (keys(%currids)) {
 9801:                 my @curr = sort { $a <=> $b } keys(%currids);
 9802:                 if ($curr[-1] =~ /^\d+$/) {
 9803:                     $id = 1 + $curr[-1];
 9804:                 }
 9805:             } else {
 9806:                 $id = 1;
 9807:             }
 9808:             if ($id) {
 9809:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
 9810:                     $error = 'nostore';
 9811:                 }
 9812:             } else {
 9813:                 $error = 'nonumber';
 9814:             }
 9815:         }
 9816:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
 9817:     } else {
 9818:         $error = 'nolock';
 9819:     }
 9820:     return ($id,$error);
 9821: }
 9822: 
 9823: sub modify_autoenroll {
 9824:     my ($dom,$lastactref,%domconfig) = @_;
 9825:     my ($resulttext,%changes);
 9826:     my %currautoenroll;
 9827:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 9828:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 9829:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 9830:         }
 9831:     }
 9832:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 9833:     my %title = ( run => 'Auto-enrollment active',
 9834:                   sender => 'Sender for notification messages',
 9835:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
 9836:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
 9837:     my @offon = ('off','on');
 9838:     my $sender_uname = $env{'form.sender_uname'};
 9839:     my $sender_domain = $env{'form.sender_domain'};
 9840:     if ($sender_domain eq '') {
 9841:         $sender_uname = '';
 9842:     } elsif ($sender_uname eq '') {
 9843:         $sender_domain = '';
 9844:     }
 9845:     my $coowners = $env{'form.autoassign_coowners'};
 9846:     my $failsafe = $env{'form.autoenroll_failsafe'};
 9847:     $failsafe =~ s{^\s+|\s+$}{}g;
 9848:     if ($failsafe =~ /\D/) {
 9849:         undef($failsafe);
 9850:     }
 9851:     my %autoenrollhash =  (
 9852:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 9853:                                        'sender_uname' => $sender_uname,
 9854:                                        'sender_domain' => $sender_domain,
 9855:                                        'co-owners' => $coowners,
 9856:                                        'autofailsafe' => $failsafe,
 9857:                                 }
 9858:                      );
 9859:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 9860:                                              $dom);
 9861:     if ($putresult eq 'ok') {
 9862:         if (exists($currautoenroll{'run'})) {
 9863:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 9864:                  $changes{'run'} = 1;
 9865:              }
 9866:         } elsif ($autorun) {
 9867:             if ($env{'form.autoenroll_run'} ne '1') {
 9868:                  $changes{'run'} = 1;
 9869:             }
 9870:         }
 9871:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 9872:             $changes{'sender'} = 1;
 9873:         }
 9874:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 9875:             $changes{'sender'} = 1;
 9876:         }
 9877:         if ($currautoenroll{'co-owners'} ne '') {
 9878:             if ($currautoenroll{'co-owners'} ne $coowners) {
 9879:                 $changes{'coowners'} = 1;
 9880:             }
 9881:         } elsif ($coowners) {
 9882:             $changes{'coowners'} = 1;
 9883:         }
 9884:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
 9885:             $changes{'autofailsafe'} = 1;
 9886:         }
 9887:         if (keys(%changes) > 0) {
 9888:             $resulttext = &mt('Changes made:').'<ul>';
 9889:             if ($changes{'run'}) {
 9890:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 9891:             }
 9892:             if ($changes{'sender'}) {
 9893:                 if ($sender_uname eq '' || $sender_domain eq '') {
 9894:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 9895:                 } else {
 9896:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 9897:                 }
 9898:             }
 9899:             if ($changes{'coowners'}) {
 9900:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 9901:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9902:                 if (ref($lastactref) eq 'HASH') {
 9903:                     $lastactref->{'domainconfig'} = 1;
 9904:                 }
 9905:             }
 9906:             if ($changes{'autofailsafe'}) {
 9907:                 if ($failsafe ne '') {
 9908:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
 9909:                 } else {
 9910:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
 9911:                 }
 9912:                 &Apache::lonnet::get_domain_defaults($dom,1);
 9913:                 if (ref($lastactref) eq 'HASH') {
 9914:                     $lastactref->{'domdefaults'} = 1;
 9915:                 }
 9916:             }
 9917:             $resulttext .= '</ul>';
 9918:         } else {
 9919:             $resulttext = &mt('No changes made to auto-enrollment settings');
 9920:         }
 9921:     } else {
 9922:         $resulttext = '<span class="LC_error">'.
 9923: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9924:     }
 9925:     return $resulttext;
 9926: }
 9927: 
 9928: sub modify_autoupdate {
 9929:     my ($dom,%domconfig) = @_;
 9930:     my ($resulttext,%currautoupdate,%fields,%changes);
 9931:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 9932:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 9933:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 9934:         }
 9935:     }
 9936:     my @offon = ('off','on');
 9937:     my %title = &Apache::lonlocal::texthash (
 9938:                    run => 'Auto-update:',
 9939:                    classlists => 'Updates to user information in classlists?'
 9940:                 );
 9941:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9942:     my %fieldtitles = &Apache::lonlocal::texthash (
 9943:                         id => 'Student/Employee ID',
 9944:                         permanentemail => 'E-mail address',
 9945:                         lastname => 'Last Name',
 9946:                         firstname => 'First Name',
 9947:                         middlename => 'Middle Name',
 9948:                         generation => 'Generation',
 9949:                       );
 9950:     $othertitle = &mt('All users');
 9951:     if (keys(%{$usertypes}) >  0) {
 9952:         $othertitle = &mt('Other users');
 9953:     }
 9954:     foreach my $key (keys(%env)) {
 9955:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 9956:             my ($usertype,$item) = ($1,$2);
 9957:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 9958:                 if ($usertype eq 'default') {   
 9959:                     push(@{$fields{$1}},$2);
 9960:                 } elsif (ref($types) eq 'ARRAY') {
 9961:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 9962:                         push(@{$fields{$1}},$2);
 9963:                     }
 9964:                 }
 9965:             }
 9966:         }
 9967:     }
 9968:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 9969:     @lockablenames = sort(@lockablenames);
 9970:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 9971:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 9972:         if (@changed) {
 9973:             $changes{'lockablenames'} = 1;
 9974:         }
 9975:     } else {
 9976:         if (@lockablenames) {
 9977:             $changes{'lockablenames'} = 1;
 9978:         }
 9979:     }
 9980:     my %updatehash = (
 9981:                       autoupdate => { run => $env{'form.autoupdate_run'},
 9982:                                       classlists => $env{'form.classlists'},
 9983:                                       fields => {%fields},
 9984:                                       lockablenames => \@lockablenames,
 9985:                                     }
 9986:                      );
 9987:     foreach my $key (keys(%currautoupdate)) {
 9988:         if (($key eq 'run') || ($key eq 'classlists')) {
 9989:             if (exists($updatehash{autoupdate}{$key})) {
 9990:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 9991:                     $changes{$key} = 1;
 9992:                 }
 9993:             }
 9994:         } elsif ($key eq 'fields') {
 9995:             if (ref($currautoupdate{$key}) eq 'HASH') {
 9996:                 foreach my $item (@{$types},'default') {
 9997:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 9998:                         my $change = 0;
 9999:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
10000:                             if (!exists($fields{$item})) {
10001:                                 $change = 1;
10002:                                 last;
10003:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
10004:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
10005:                                     $change = 1;
10006:                                     last;
10007:                                 }
10008:                             }
10009:                         }
10010:                         if ($change) {
10011:                             push(@{$changes{$key}},$item);
10012:                         }
10013:                     } 
10014:                 }
10015:             }
10016:         } elsif ($key eq 'lockablenames') {
10017:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
10018:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10019:                 if (@changed) {
10020:                     $changes{'lockablenames'} = 1;
10021:                 }
10022:             } else {
10023:                 if (@lockablenames) {
10024:                     $changes{'lockablenames'} = 1;
10025:                 }
10026:             }
10027:         }
10028:     }
10029:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
10030:         if (@lockablenames) {
10031:             $changes{'lockablenames'} = 1;
10032:         }
10033:     }
10034:     foreach my $item (@{$types},'default') {
10035:         if (defined($fields{$item})) {
10036:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
10037:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
10038:                     my $change = 0;
10039:                     if (ref($fields{$item}) eq 'ARRAY') {
10040:                         foreach my $type (@{$fields{$item}}) {
10041:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
10042:                                 $change = 1;
10043:                                 last;
10044:                             }
10045:                         }
10046:                     }
10047:                     if ($change) {
10048:                         push(@{$changes{'fields'}},$item);
10049:                     }
10050:                 } else {
10051:                     push(@{$changes{'fields'}},$item);
10052:                 }
10053:             } else {
10054:                 push(@{$changes{'fields'}},$item);
10055:             }
10056:         }
10057:     }
10058:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
10059:                                              $dom);
10060:     if ($putresult eq 'ok') {
10061:         if (keys(%changes) > 0) {
10062:             $resulttext = &mt('Changes made:').'<ul>';
10063:             foreach my $key (sort(keys(%changes))) {
10064:                 if ($key eq 'lockablenames') {
10065:                     $resulttext .= '<li>';
10066:                     if (@lockablenames) {
10067:                         $usertypes->{'default'} = $othertitle;
10068:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
10069:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
10070:                     } else {
10071:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
10072:                     }
10073:                     $resulttext .= '</li>';
10074:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
10075:                     foreach my $item (@{$changes{$key}}) {
10076:                         my @newvalues;
10077:                         foreach my $type (@{$fields{$item}}) {
10078:                             push(@newvalues,$fieldtitles{$type});
10079:                         }
10080:                         my $newvaluestr;
10081:                         if (@newvalues > 0) {
10082:                             $newvaluestr = join(', ',@newvalues);
10083:                         } else {
10084:                             $newvaluestr = &mt('none');
10085:                         }
10086:                         if ($item eq 'default') {
10087:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
10088:                         } else {
10089:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
10090:                         }
10091:                     }
10092:                 } else {
10093:                     my $newvalue;
10094:                     if ($key eq 'run') {
10095:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
10096:                     } else {
10097:                         $newvalue = $offon[$env{'form.'.$key}];
10098:                     }
10099:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
10100:                 }
10101:             }
10102:             $resulttext .= '</ul>';
10103:         } else {
10104:             $resulttext = &mt('No changes made to autoupdates');
10105:         }
10106:     } else {
10107:         $resulttext = '<span class="LC_error">'.
10108: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10109:     }
10110:     return $resulttext;
10111: }
10112: 
10113: sub modify_autocreate {
10114:     my ($dom,%domconfig) = @_;
10115:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
10116:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
10117:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
10118:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
10119:         }
10120:     }
10121:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
10122:                  req => 'Auto-creation of validated requests for official courses',
10123:                  xmldc => 'Identity of course creator of courses from XML files',
10124:                );
10125:     my @types = ('xml','req');
10126:     foreach my $item (@types) {
10127:         $newvals{$item} = $env{'form.autocreate_'.$item};
10128:         $newvals{$item} =~ s/\D//g;
10129:         $newvals{$item} = 0 if ($newvals{$item} eq '');
10130:     }
10131:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
10132:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
10133:     unless (exists($domcoords{$newvals{'xmldc'}})) {
10134:         $newvals{'xmldc'} = '';
10135:     } 
10136:     %autocreatehash =  (
10137:                         autocreate => { xml => $newvals{'xml'},
10138:                                         req => $newvals{'req'},
10139:                                       }
10140:                        );
10141:     if ($newvals{'xmldc'} ne '') {
10142:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
10143:     }
10144:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
10145:                                              $dom);
10146:     if ($putresult eq 'ok') {
10147:         my @items = @types;
10148:         if ($newvals{'xml'}) {
10149:             push(@items,'xmldc');
10150:         }
10151:         foreach my $item (@items) {
10152:             if (exists($currautocreate{$item})) {
10153:                 if ($currautocreate{$item} ne $newvals{$item}) {
10154:                     $changes{$item} = 1;
10155:                 }
10156:             } elsif ($newvals{$item}) {
10157:                 $changes{$item} = 1;
10158:             }
10159:         }
10160:         if (keys(%changes) > 0) {
10161:             my @offon = ('off','on'); 
10162:             $resulttext = &mt('Changes made:').'<ul>';
10163:             foreach my $item (@types) {
10164:                 if ($changes{$item}) {
10165:                     my $newtxt = $offon[$newvals{$item}];
10166:                     $resulttext .= '<li>'.
10167:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
10168:                                        '<b>','</b>').
10169:                                    '</li>';
10170:                 }
10171:             }
10172:             if ($changes{'xmldc'}) {
10173:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
10174:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
10175:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
10176:             }
10177:             $resulttext .= '</ul>';
10178:         } else {
10179:             $resulttext = &mt('No changes made to auto-creation settings');
10180:         }
10181:     } else {
10182:         $resulttext = '<span class="LC_error">'.
10183:             &mt('An error occurred: [_1]',$putresult).'</span>';
10184:     }
10185:     return $resulttext;
10186: }
10187: 
10188: sub modify_directorysrch {
10189:     my ($dom,$lastactref,%domconfig) = @_;
10190:     my ($resulttext,%changes);
10191:     my %currdirsrch;
10192:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
10193:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
10194:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
10195:         }
10196:     }
10197:     my %title = ( available => 'Institutional directory search available',
10198:                   localonly => 'Other domains can search institution',
10199:                   lcavailable => 'LON-CAPA directory search available',
10200:                   lclocalonly => 'Other domains can search LON-CAPA domain',
10201:                   searchby => 'Search types',
10202:                   searchtypes => 'Search latitude');
10203:     my @offon = ('off','on');
10204:     my @otherdoms = ('Yes','No');
10205: 
10206:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
10207:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
10208:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
10209: 
10210:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10211:     if (keys(%{$usertypes}) == 0) {
10212:         @cansearch = ('default');
10213:     } else {
10214:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
10215:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
10216:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
10217:                     push(@{$changes{'cansearch'}},$type);
10218:                 }
10219:             }
10220:             foreach my $type (@cansearch) {
10221:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
10222:                     push(@{$changes{'cansearch'}},$type);
10223:                 }
10224:             }
10225:         } else {
10226:             push(@{$changes{'cansearch'}},@cansearch);
10227:         }
10228:     }
10229: 
10230:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
10231:         foreach my $by (@{$currdirsrch{'searchby'}}) {
10232:             if (!grep(/^\Q$by\E$/,@searchby)) {
10233:                 push(@{$changes{'searchby'}},$by);
10234:             }
10235:         }
10236:         foreach my $by (@searchby) {
10237:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
10238:                 push(@{$changes{'searchby'}},$by);
10239:             }
10240:         }
10241:     } else {
10242:         push(@{$changes{'searchby'}},@searchby);
10243:     }
10244: 
10245:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
10246:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
10247:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
10248:                 push(@{$changes{'searchtypes'}},$type);
10249:             }
10250:         }
10251:         foreach my $type (@searchtypes) {
10252:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
10253:                 push(@{$changes{'searchtypes'}},$type);
10254:             }
10255:         }
10256:     } else {
10257:         if (exists($currdirsrch{'searchtypes'})) {
10258:             foreach my $type (@searchtypes) {  
10259:                 if ($type ne $currdirsrch{'searchtypes'}) { 
10260:                     push(@{$changes{'searchtypes'}},$type);
10261:                 }
10262:             }
10263:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
10264:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
10265:             }   
10266:         } else {
10267:             push(@{$changes{'searchtypes'}},@searchtypes); 
10268:         }
10269:     }
10270: 
10271:     my %dirsrch_hash =  (
10272:             directorysrch => { available => $env{'form.dirsrch_available'},
10273:                                cansearch => \@cansearch,
10274:                                localonly => $env{'form.dirsrch_instlocalonly'},
10275:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
10276:                                lcavailable => $env{'form.dirsrch_domavailable'},
10277:                                searchby => \@searchby,
10278:                                searchtypes => \@searchtypes,
10279:                              }
10280:             );
10281:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
10282:                                              $dom);
10283:     if ($putresult eq 'ok') {
10284:         if (exists($currdirsrch{'available'})) {
10285:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
10286:                  $changes{'available'} = 1;
10287:              }
10288:         } else {
10289:             if ($env{'form.dirsrch_available'} eq '1') {
10290:                 $changes{'available'} = 1;
10291:             }
10292:         }
10293:         if (exists($currdirsrch{'lcavailable'})) {
10294:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
10295:                 $changes{'lcavailable'} = 1;
10296:             }
10297:         } else {
10298:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
10299:                 $changes{'lcavailable'} = 1;
10300:             }
10301:         }
10302:         if (exists($currdirsrch{'localonly'})) {
10303:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
10304:                 $changes{'localonly'} = 1;
10305:             }
10306:         } else {
10307:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
10308:                 $changes{'localonly'} = 1;
10309:             }
10310:         }
10311:         if (exists($currdirsrch{'lclocalonly'})) {
10312:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
10313:                 $changes{'lclocalonly'} = 1;
10314:             }
10315:         } else {
10316:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
10317:                 $changes{'lclocalonly'} = 1;
10318:             }
10319:         }
10320:         if (keys(%changes) > 0) {
10321:             $resulttext = &mt('Changes made:').'<ul>';
10322:             if ($changes{'available'}) {
10323:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
10324:             }
10325:             if ($changes{'lcavailable'}) {
10326:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
10327:             }
10328:             if ($changes{'localonly'}) {
10329:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
10330:             }
10331:             if ($changes{'lclocalonly'}) {
10332:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
10333:             }
10334:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
10335:                 my $chgtext;
10336:                 if (ref($usertypes) eq 'HASH') {
10337:                     if (keys(%{$usertypes}) > 0) {
10338:                         foreach my $type (@{$types}) {
10339:                             if (grep(/^\Q$type\E$/,@cansearch)) {
10340:                                 $chgtext .= $usertypes->{$type}.'; ';
10341:                             }
10342:                         }
10343:                         if (grep(/^default$/,@cansearch)) {
10344:                             $chgtext .= $othertitle;
10345:                         } else {
10346:                             $chgtext =~ s/\; $//;
10347:                         }
10348:                         $resulttext .=
10349:                             '<li>'.
10350:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
10351:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
10352:                             '</li>';
10353:                     }
10354:                 }
10355:             }
10356:             if (ref($changes{'searchby'}) eq 'ARRAY') {
10357:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
10358:                 my $chgtext;
10359:                 foreach my $type (@{$titleorder}) {
10360:                     if (grep(/^\Q$type\E$/,@searchby)) {
10361:                         if (defined($searchtitles->{$type})) {
10362:                             $chgtext .= $searchtitles->{$type}.'; ';
10363:                         }
10364:                     }
10365:                 }
10366:                 $chgtext =~ s/\; $//;
10367:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
10368:             }
10369:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
10370:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
10371:                 my $chgtext;
10372:                 foreach my $type (@{$srchtypeorder}) {
10373:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
10374:                         if (defined($srchtypes_desc->{$type})) {
10375:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
10376:                         }
10377:                     }
10378:                 }
10379:                 $chgtext =~ s/\; $//;
10380:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
10381:             }
10382:             $resulttext .= '</ul>';
10383:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
10384:             if (ref($lastactref) eq 'HASH') {
10385:                 $lastactref->{'directorysrch'} = 1;
10386:             }
10387:         } else {
10388:             $resulttext = &mt('No changes made to directory search settings');
10389:         }
10390:     } else {
10391:         $resulttext = '<span class="LC_error">'.
10392:                       &mt('An error occurred: [_1]',$putresult).'</span>';
10393:     }
10394:     return $resulttext;
10395: }
10396: 
10397: sub modify_contacts {
10398:     my ($dom,$lastactref,%domconfig) = @_;
10399:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
10400:     if (ref($domconfig{'contacts'}) eq 'HASH') {
10401:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
10402:             $currsetting{$key} = $domconfig{'contacts'}{$key};
10403:         }
10404:     }
10405:     my (%others,%to,%bcc,%includestr,%includeloc);
10406:     my @contacts = ('supportemail','adminemail');
10407:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
10408:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail');
10409:     my @toggles = ('reporterrors','reportupdates');
10410:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
10411:     foreach my $type (@mailings) {
10412:         @{$newsetting{$type}} = 
10413:             &Apache::loncommon::get_env_multiple('form.'.$type);
10414:         foreach my $item (@contacts) {
10415:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
10416:                 $contacts_hash{contacts}{$type}{$item} = 1;
10417:             } else {
10418:                 $contacts_hash{contacts}{$type}{$item} = 0;
10419:             }
10420:         }
10421:         $others{$type} = $env{'form.'.$type.'_others'};
10422:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
10423:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10424:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
10425:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
10426:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
10427:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
10428:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
10429:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
10430:             }
10431:         }
10432:     }
10433:     foreach my $item (@contacts) {
10434:         $to{$item} = $env{'form.'.$item};
10435:         $contacts_hash{'contacts'}{$item} = $to{$item};
10436:     }
10437:     foreach my $item (@toggles) {
10438:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
10439:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
10440:         }
10441:     }
10442:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
10443:         foreach my $field (@{$fields}) {
10444:             if (ref($possoptions->{$field}) eq 'ARRAY') {
10445:                 my $value = $env{'form.helpform_'.$field};
10446:                 $value =~ s/^\s+|\s+$//g;
10447:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
10448:                     $contacts_hash{contacts}{'helpform'}{$field} = $value;
10449:                     if ($field eq 'screenshot') {
10450:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
10451:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
10452:                             $contacts_hash{contacts}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
10453:                         }
10454:                     }
10455:                 }
10456:             }
10457:         }
10458:     }
10459:     if (keys(%currsetting) > 0) {
10460:         foreach my $item (@contacts) {
10461:             if ($to{$item} ne $currsetting{$item}) {
10462:                 $changes{$item} = 1;
10463:             }
10464:         }
10465:         foreach my $type (@mailings) {
10466:             foreach my $item (@contacts) {
10467:                 if (ref($currsetting{$type}) eq 'HASH') {
10468:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
10469:                         push(@{$changes{$type}},$item);
10470:                     }
10471:                 } else {
10472:                     push(@{$changes{$type}},@{$newsetting{$type}});
10473:                 }
10474:             }
10475:             if ($others{$type} ne $currsetting{$type}{'others'}) {
10476:                 push(@{$changes{$type}},'others');
10477:             }
10478:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10479:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
10480:                     push(@{$changes{$type}},'bcc'); 
10481:                 }
10482:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
10483:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
10484:                     push(@{$changes{$type}},'include');
10485:                 }
10486:             }
10487:         }
10488:         if (ref($fields) eq 'ARRAY') {
10489:             if (ref($currsetting{'helpform'}) eq 'HASH') {
10490:                 foreach my $field (@{$fields}) {
10491:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
10492:                         push(@{$changes{'helpform'}},$field);
10493:                     }
10494:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10495:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
10496:                             push(@{$changes{'helpform'}},'maxsize');
10497:                         }
10498:                     }
10499:                 }
10500:             } else {
10501:                 foreach my $field (@{$fields}) {
10502:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10503:                         push(@{$changes{'helpform'}},$field);
10504:                     }
10505:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10506:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10507:                             push(@{$changes{'helpform'}},'maxsize');
10508:                         }
10509:                     }
10510:                 }
10511:             }
10512:         }
10513:     } else {
10514:         my %default;
10515:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
10516:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
10517:         $default{'errormail'} = 'adminemail';
10518:         $default{'packagesmail'} = 'adminemail';
10519:         $default{'helpdeskmail'} = 'supportemail';
10520:         $default{'otherdomsmail'} = 'supportemail';
10521:         $default{'lonstatusmail'} = 'adminemail';
10522:         $default{'requestsmail'} = 'adminemail';
10523:         $default{'updatesmail'} = 'adminemail';
10524:         foreach my $item (@contacts) {
10525:            if ($to{$item} ne $default{$item}) {
10526:                $changes{$item} = 1;
10527:            }
10528:         }
10529:         foreach my $type (@mailings) {
10530:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
10531:                 push(@{$changes{$type}},@{$newsetting{$type}});
10532:             }
10533:             if ($others{$type} ne '') {
10534:                 push(@{$changes{$type}},'others');
10535:             }
10536:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10537:                 if ($bcc{$type} ne '') {
10538:                     push(@{$changes{$type}},'bcc');
10539:                 }
10540:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
10541:                     push(@{$changes{$type}},'include');
10542:                 }
10543:             }
10544:         }
10545:         if (ref($fields) eq 'ARRAY') {
10546:             foreach my $field (@{$fields}) {
10547:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10548:                     push(@{$changes{'helpform'}},$field);
10549:                 }
10550:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10551:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10552:                         push(@{$changes{'helpform'}},'maxsize');
10553:                     }
10554:                 }
10555:             }
10556:         }
10557:     }
10558:     foreach my $item (@toggles) {
10559:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
10560:             $changes{$item} = 1;
10561:         } elsif ((!$env{'form.'.$item}) &&
10562:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
10563:             $changes{$item} = 1;
10564:         }
10565:     }
10566:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
10567:                                              $dom);
10568:     if ($putresult eq 'ok') {
10569:         if (keys(%changes) > 0) {
10570:             &Apache::loncommon::devalidate_domconfig_cache($dom);
10571:             if (ref($lastactref) eq 'HASH') {
10572:                 $lastactref->{'domainconfig'} = 1;
10573:             }
10574:             my ($titles,$short_titles)  = &contact_titles();
10575:             $resulttext = &mt('Changes made:').'<ul>';
10576:             foreach my $item (@contacts) {
10577:                 if ($changes{$item}) {
10578:                     $resulttext .= '<li>'.$titles->{$item}.
10579:                                     &mt(' set to: ').
10580:                                     '<span class="LC_cusr_emph">'.
10581:                                     $to{$item}.'</span></li>';
10582:                 }
10583:             }
10584:             foreach my $type (@mailings) {
10585:                 if (ref($changes{$type}) eq 'ARRAY') {
10586:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10587:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
10588:                     } else {
10589:                         $resulttext .= '<li>'.$titles->{$type}.': ';
10590:                     }
10591:                     my @text;
10592:                     foreach my $item (@{$newsetting{$type}}) {
10593:                         push(@text,$short_titles->{$item});
10594:                     }
10595:                     if ($others{$type} ne '') {
10596:                         push(@text,$others{$type});
10597:                     }
10598:                     if (@text) {
10599:                         $resulttext .= '<span class="LC_cusr_emph">'.
10600:                                        join(', ',@text).'</span>';
10601:                     }
10602:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10603:                         if ($bcc{$type} ne '') {
10604:                             my $bcctext;
10605:                             if (@text) {
10606:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
10607:                             } else {
10608:                                 $bcctext = '(Bcc)';
10609:                             }
10610:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
10611:                         } elsif (!@text) {
10612:                             $resulttext .= &mt('No one');
10613:                         }
10614:                         if ($includestr{$type} ne '') {
10615:                             if ($includeloc{$type} eq 'b') {
10616:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
10617:                             } elsif ($includeloc{$type} eq 's') {
10618:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
10619:                             }
10620:                         }
10621:                     } elsif (!@text) {
10622:                         $resulttext .= &mt('No recipients');
10623:                     }
10624:                     $resulttext .= '</li>';
10625:                 }
10626:             }
10627:             my @offon = ('off','on');
10628:             if ($changes{'reporterrors'}) {
10629:                 $resulttext .= '<li>'.
10630:                                &mt('E-mail error reports to [_1] set to "'.
10631:                                    $offon[$env{'form.reporterrors'}].'".',
10632:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10633:                                        &mt('LON-CAPA core group - MSU'),600,500)).
10634:                                '</li>';
10635:             }
10636:             if ($changes{'reportupdates'}) {
10637:                 $resulttext .= '<li>'.
10638:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
10639:                                     $offon[$env{'form.reportupdates'}].'".',
10640:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10641:                                         &mt('LON-CAPA core group - MSU'),600,500)).
10642:                                 '</li>';
10643:             }
10644:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
10645:                 my (@optional,@required,@unused,$maxsizechg);
10646:                 foreach my $field (@{$changes{'helpform'}}) {
10647:                     if ($field eq 'maxsize') {
10648:                         $maxsizechg = 1;
10649:                         next;
10650:                     }
10651:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
10652:                         push(@optional,$field);
10653:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
10654:                         push(@unused,$field);
10655:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
10656:                         push(@required,$field);
10657:                     }
10658:                 }
10659:                 if (@optional) {
10660:                     $resulttext .= '<li>'.
10661:                                    &mt('Help form fields changed to "Optional": [_1].',
10662:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
10663:                                    '</li>';
10664:                 }
10665:                 if (@required) {
10666:                     $resulttext .= '<li>'.
10667:                                    &mt('Help form fields changed to "Required": [_1].',
10668:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
10669:                                    '</li>';
10670:                 }
10671:                 if (@unused) {
10672:                     $resulttext .= '<li>'.
10673:                                    &mt('Help form fields changed to "Not shown": [_1].',
10674:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
10675:                                    '</li>';
10676:                 }
10677:                 if ($maxsizechg) {
10678:                     $resulttext .= '<li>'.
10679:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
10680:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
10681:                                    '</li>';
10682: 
10683:                 }
10684:             }
10685:             $resulttext .= '</ul>';
10686:         } else {
10687:             $resulttext = &mt('No changes made to contacts and form settings');
10688:         }
10689:     } else {
10690:         $resulttext = '<span class="LC_error">'.
10691:             &mt('An error occurred: [_1].',$putresult).'</span>';
10692:     }
10693:     return $resulttext;
10694: }
10695: 
10696: sub modify_usercreation {
10697:     my ($dom,%domconfig) = @_;
10698:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
10699:     my $warningmsg;
10700:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
10701:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
10702:             if ($key eq 'cancreate') {
10703:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
10704:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
10705:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
10706:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
10707:                             ($item eq 'recaptchaversion')) {
10708:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10709:                         } else {
10710:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10711:                         }
10712:                     }
10713:                 }
10714:             } elsif ($key eq 'email_rule') {
10715:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
10716:             } else {
10717:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
10718:             }
10719:         }
10720:     }
10721:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
10722:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
10723:     my @contexts = ('author','course','requestcrs');
10724:     foreach my $item(@contexts) {
10725:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
10726:     }
10727:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
10728:         foreach my $item (@contexts) {
10729:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
10730:                 push(@{$changes{'cancreate'}},$item);
10731:             }
10732:         }
10733:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
10734:         foreach my $item (@contexts) {
10735:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
10736:                 if ($cancreate{$item} ne 'any') {
10737:                     push(@{$changes{'cancreate'}},$item);
10738:                 }
10739:             } else {
10740:                 if ($cancreate{$item} ne 'none') {
10741:                     push(@{$changes{'cancreate'}},$item);
10742:                 }
10743:             }
10744:         }
10745:     } else {
10746:         foreach my $item (@contexts)  {
10747:             push(@{$changes{'cancreate'}},$item);
10748:         }
10749:     }
10750: 
10751:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
10752:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
10753:             if (!grep(/^\Q$type\E$/,@username_rule)) {
10754:                 push(@{$changes{'username_rule'}},$type);
10755:             }
10756:         }
10757:         foreach my $type (@username_rule) {
10758:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
10759:                 push(@{$changes{'username_rule'}},$type);
10760:             }
10761:         }
10762:     } else {
10763:         push(@{$changes{'username_rule'}},@username_rule);
10764:     }
10765: 
10766:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
10767:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
10768:             if (!grep(/^\Q$type\E$/,@id_rule)) {
10769:                 push(@{$changes{'id_rule'}},$type);
10770:             }
10771:         }
10772:         foreach my $type (@id_rule) {
10773:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
10774:                 push(@{$changes{'id_rule'}},$type);
10775:             }
10776:         }
10777:     } else {
10778:         push(@{$changes{'id_rule'}},@id_rule);
10779:     }
10780: 
10781:     my @authen_contexts = ('author','course','domain');
10782:     my @authtypes = ('int','krb4','krb5','loc');
10783:     my %authhash;
10784:     foreach my $item (@authen_contexts) {
10785:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
10786:         foreach my $auth (@authtypes) {
10787:             if (grep(/^\Q$auth\E$/,@authallowed)) {
10788:                 $authhash{$item}{$auth} = 1;
10789:             } else {
10790:                 $authhash{$item}{$auth} = 0;
10791:             }
10792:         }
10793:     }
10794:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
10795:         foreach my $item (@authen_contexts) {
10796:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
10797:                 foreach my $auth (@authtypes) {
10798:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
10799:                         push(@{$changes{'authtypes'}},$item);
10800:                         last;
10801:                     }
10802:                 }
10803:             }
10804:         }
10805:     } else {
10806:         foreach my $item (@authen_contexts) {
10807:             push(@{$changes{'authtypes'}},$item);
10808:         }
10809:     }
10810: 
10811:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
10812:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
10813:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
10814:     $save_usercreate{'id_rule'} = \@id_rule;
10815:     $save_usercreate{'username_rule'} = \@username_rule,
10816:     $save_usercreate{'authtypes'} = \%authhash;
10817: 
10818:     my %usercreation_hash =  (
10819:         usercreation     => \%save_usercreate,
10820:     );
10821: 
10822:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
10823:                                              $dom);
10824: 
10825:     if ($putresult eq 'ok') {
10826:         if (keys(%changes) > 0) {
10827:             $resulttext = &mt('Changes made:').'<ul>';
10828:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
10829:                 my %lt = &usercreation_types();
10830:                 foreach my $type (@{$changes{'cancreate'}}) {
10831:                     my $chgtext = $lt{$type}.', ';
10832:                     if ($cancreate{$type} eq 'none') {
10833:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
10834:                     } elsif ($cancreate{$type} eq 'any') {
10835:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
10836:                     } elsif ($cancreate{$type} eq 'official') {
10837:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
10838:                     } elsif ($cancreate{$type} eq 'unofficial') {
10839:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
10840:                     }
10841:                     $resulttext .= '<li>'.$chgtext.'</li>';
10842:                 }
10843:             }
10844:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
10845:                 my ($rules,$ruleorder) = 
10846:                     &Apache::lonnet::inst_userrules($dom,'username');
10847:                 my $chgtext = '<ul>';
10848:                 foreach my $type (@username_rule) {
10849:                     if (ref($rules->{$type}) eq 'HASH') {
10850:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
10851:                     }
10852:                 }
10853:                 $chgtext .= '</ul>';
10854:                 if (@username_rule > 0) {
10855:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
10856:                 } else {
10857:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
10858:                 }
10859:             }
10860:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
10861:                 my ($idrules,$idruleorder) = 
10862:                     &Apache::lonnet::inst_userrules($dom,'id');
10863:                 my $chgtext = '<ul>';
10864:                 foreach my $type (@id_rule) {
10865:                     if (ref($idrules->{$type}) eq 'HASH') {
10866:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
10867:                     }
10868:                 }
10869:                 $chgtext .= '</ul>';
10870:                 if (@id_rule > 0) {
10871:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
10872:                 } else {
10873:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
10874:                 }
10875:             }
10876:             my %authname = &authtype_names();
10877:             my %context_title = &context_names();
10878:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
10879:                 my $chgtext = '<ul>';
10880:                 foreach my $type (@{$changes{'authtypes'}}) {
10881:                     my @allowed;
10882:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
10883:                     foreach my $auth (@authtypes) {
10884:                         if ($authhash{$type}{$auth}) {
10885:                             push(@allowed,$authname{$auth});
10886:                         }
10887:                     }
10888:                     if (@allowed > 0) {
10889:                         $chgtext .= join(', ',@allowed).'</li>';
10890:                     } else {
10891:                         $chgtext .= &mt('none').'</li>';
10892:                     }
10893:                 }
10894:                 $chgtext .= '</ul>';
10895:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
10896:                 $resulttext .= '</li>';
10897:             }
10898:             $resulttext .= '</ul>';
10899:         } else {
10900:             $resulttext = &mt('No changes made to user creation settings');
10901:         }
10902:     } else {
10903:         $resulttext = '<span class="LC_error">'.
10904:             &mt('An error occurred: [_1]',$putresult).'</span>';
10905:     }
10906:     if ($warningmsg ne '') {
10907:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
10908:     }
10909:     return $resulttext;
10910: }
10911: 
10912: sub modify_selfcreation {
10913:     my ($dom,%domconfig) = @_;
10914:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
10915:     my (%save_usercreate,%save_usermodify);
10916:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10917:     if (ref($types) eq 'ARRAY') {
10918:         $usertypes->{'default'} = $othertitle;
10919:         push(@{$types},'default');
10920:     }
10921: #
10922: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
10923: #
10924:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
10925:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
10926:             if ($key eq 'cancreate') {
10927:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
10928:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
10929:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
10930:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
10931:                             ($item eq 'recaptchaversion') ||
10932:                             ($item eq 'emailusername') || ($item eq 'notify') ||
10933:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
10934:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10935:                         } else {
10936:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10937:                         }
10938:                     }
10939:                 }
10940:             } elsif ($key eq 'email_rule') {
10941:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
10942:             } else {
10943:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
10944:             }
10945:         }
10946:     }
10947: #
10948: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
10949: #
10950:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
10951:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
10952:             if ($key eq 'selfcreate') {
10953:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
10954:             } else {
10955:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
10956:             }
10957:         }
10958:     }
10959: 
10960:     my @contexts = ('selfcreate');
10961:     @{$cancreate{'selfcreate'}} = ();
10962:     %{$cancreate{'emailusername'}} = ();
10963:     @{$cancreate{'statustocreate'}} = ();
10964:     %{$cancreate{'selfcreateprocessing'}} = ();
10965:     %{$cancreate{'shibenv'}} = ();
10966:     my %selfcreatetypes = (
10967:                              sso   => 'users authenticated by institutional single sign on',
10968:                              login => 'users authenticated by institutional log-in',
10969:                              email => 'users who provide a valid e-mail address for use as username',
10970:                           );
10971: #
10972: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
10973: # is permitted.
10974: #
10975: 
10976:     my @statuses;
10977:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
10978:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
10979:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
10980:         }
10981:     }
10982:     push(@statuses,'default');
10983: 
10984:     foreach my $item ('login','sso','email') {
10985:         if ($item eq 'email') {
10986:             if ($env{'form.cancreate_email'}) {
10987:                 push(@{$cancreate{'selfcreate'}},'email');
10988:                 push(@contexts,'selfcreateprocessing');
10989:                 foreach my $type (@statuses) {
10990:                     if ($type eq 'default') {
10991:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
10992:                     } else { 
10993:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
10994:                     }
10995:                 }
10996:             }
10997:         } else {
10998:             if ($env{'form.cancreate_'.$item}) {
10999:                 push(@{$cancreate{'selfcreate'}},$item);
11000:             }
11001:         }
11002:     }
11003:     my (@email_rule,%userinfo,%savecaptcha);
11004:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
11005: #
11006: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
11007: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
11008: #
11009: 
11010:     if ($env{'form.cancreate_email'}) {
11011:         push(@contexts,'emailusername');
11012:         if (ref($types) eq 'ARRAY') {
11013:             foreach my $type (@{$types}) {
11014:                 if (ref($infofields) eq 'ARRAY') {
11015:                     foreach my $field (@{$infofields}) {
11016:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
11017:                             $cancreate{'emailusername'}{$type}{$field} = $1;
11018:                         }
11019:                     }
11020:                 }
11021:             }
11022:         }
11023: #
11024: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
11025: # queued requests for self-creation of account using e-mail address as username
11026: #
11027: 
11028:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
11029:         @approvalnotify = sort(@approvalnotify);
11030:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
11031:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11032:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
11033:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
11034:                     push(@{$changes{'cancreate'}},'notify');
11035:                 }
11036:             } else {
11037:                 if ($cancreate{'notify'}{'approval'}) {
11038:                     push(@{$changes{'cancreate'}},'notify');
11039:                 }
11040:             }
11041:         } elsif ($cancreate{'notify'}{'approval'}) {
11042:             push(@{$changes{'cancreate'}},'notify');
11043:         }
11044: 
11045: #
11046: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
11047: #
11048:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
11049:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
11050:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
11051:             if (@{$curr_usercreation{'email_rule'}} > 0) {
11052:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
11053:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
11054:                         push(@{$changes{'email_rule'}},$type);
11055:                     }
11056:                 }
11057:             }
11058:             if (@email_rule > 0) {
11059:                 foreach my $type (@email_rule) {
11060:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
11061:                         push(@{$changes{'email_rule'}},$type);
11062:                     }
11063:                 }
11064:             }
11065:         } elsif (@email_rule > 0) {
11066:             push(@{$changes{'email_rule'}},@email_rule);
11067:         }
11068:     }
11069: #  
11070: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
11071: # institutional log-in.
11072: #
11073:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
11074:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11075:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
11076:                ($domdefaults{'auth_def'} eq 'localauth'))) {
11077:             $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.').' '.
11078:                           &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.');
11079:         }
11080:     }
11081:     my @fields = ('lastname','firstname','middlename','generation',
11082:                   'permanentemail','id');
11083:     my @shibfields = (@fields,'inststatus');
11084:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11085: #
11086: # Where usernames may created for institutional log-in and/or institutional single sign on:
11087: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
11088: # may self-create accounts 
11089: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
11090: # which the user may supply, if institutional data is unavailable.
11091: #
11092:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
11093:         if (ref($types) eq 'ARRAY') {
11094:             if (@{$types} > 1) {
11095:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
11096:                 push(@contexts,'statustocreate');
11097:             } else {
11098:                 undef($cancreate{'statustocreate'});
11099:             } 
11100:             foreach my $type (@{$types}) {
11101:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
11102:                 foreach my $field (@fields) {
11103:                     if (grep(/^\Q$field\E$/,@modifiable)) {
11104:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
11105:                     } else {
11106:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
11107:                     }
11108:                 }
11109:             }
11110:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
11111:                 foreach my $type (@{$types}) {
11112:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
11113:                         foreach my $field (@fields) {
11114:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
11115:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
11116:                                 push(@{$changes{'selfcreate'}},$type);
11117:                                 last;
11118:                             }
11119:                         }
11120:                     }
11121:                 }
11122:             } else {
11123:                 foreach my $type (@{$types}) {
11124:                     push(@{$changes{'selfcreate'}},$type);
11125:                 }
11126:             }
11127:         }
11128:         foreach my $field (@shibfields) {
11129:             if ($env{'form.shibenv_'.$field} ne '') {
11130:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
11131:             }
11132:         }
11133:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11134:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
11135:                 foreach my $field (@shibfields) {
11136:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
11137:                         push(@{$changes{'cancreate'}},'shibenv');
11138:                     }
11139:                 }
11140:             } else {
11141:                 foreach my $field (@shibfields) {
11142:                     if ($env{'form.shibenv_'.$field}) {
11143:                         push(@{$changes{'cancreate'}},'shibenv');
11144:                         last;
11145:                     }
11146:                 }
11147:             }
11148:         }
11149:     }
11150:     foreach my $item (@contexts) {
11151:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
11152:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
11153:                 if (ref($cancreate{$item}) eq 'ARRAY') {
11154:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
11155:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11156:                             push(@{$changes{'cancreate'}},$item);
11157:                         }
11158:                     }
11159:                 }
11160:             }
11161:             if (ref($cancreate{$item}) eq 'ARRAY') {
11162:                 foreach my $type (@{$cancreate{$item}}) {
11163:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
11164:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11165:                             push(@{$changes{'cancreate'}},$item);
11166:                         }
11167:                     }
11168:                 }
11169:             }
11170:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
11171:             if (ref($cancreate{$item}) eq 'HASH') {
11172:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
11173:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
11174:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
11175:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
11176:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11177:                                     push(@{$changes{'cancreate'}},$item);
11178:                                 }
11179:                             }
11180:                         }
11181:                     } elsif ($item eq 'selfcreateprocessing') {
11182:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
11183:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11184:                                 push(@{$changes{'cancreate'}},$item);
11185:                             }
11186:                         }
11187:                     } else {
11188:                         if (!$cancreate{$item}{$curr}) {
11189:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11190:                                 push(@{$changes{'cancreate'}},$item);
11191:                             }
11192:                         }
11193:                     }
11194:                 }
11195:                 foreach my $field (keys(%{$cancreate{$item}})) {
11196:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
11197:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
11198:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
11199:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
11200:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11201:                                         push(@{$changes{'cancreate'}},$item);
11202:                                     }
11203:                                 }
11204:                             } else {
11205:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11206:                                     push(@{$changes{'cancreate'}},$item);
11207:                                 }
11208:                             }
11209:                         }
11210:                     } elsif ($item eq 'selfcreateprocessing') {
11211:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
11212:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11213:                                 push(@{$changes{'cancreate'}},$item);
11214:                             }
11215:                         }
11216:                     } else {
11217:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
11218:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11219:                                 push(@{$changes{'cancreate'}},$item);
11220:                             }
11221:                         }
11222:                     }
11223:                 }
11224:             }
11225:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
11226:             if (ref($cancreate{$item}) eq 'ARRAY') {
11227:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
11228:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11229:                         push(@{$changes{'cancreate'}},$item);
11230:                     }
11231:                 }
11232:             } elsif (ref($cancreate{$item}) eq 'HASH') {
11233:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
11234:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11235:                         push(@{$changes{'cancreate'}},$item);
11236:                     }
11237:                 }
11238:             }
11239:         } elsif ($item eq 'emailusername') {
11240:             if (ref($cancreate{$item}) eq 'HASH') {
11241:                 foreach my $type (keys(%{$cancreate{$item}})) {
11242:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
11243:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
11244:                             if ($cancreate{$item}{$type}{$field}) {
11245:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11246:                                     push(@{$changes{'cancreate'}},$item);
11247:                                 }
11248:                                 last;
11249:                             }
11250:                         }
11251:                     }
11252:                 }
11253:             }
11254:         }
11255:     }
11256: #
11257: # Populate %save_usercreate hash with updates to self-creation configuration.
11258: #
11259:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
11260:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
11261:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
11262:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
11263:     if (ref($cancreate{'notify'}) eq 'HASH') {
11264:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
11265:     }
11266:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
11267:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
11268:     }
11269:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11270:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
11271:     }
11272:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
11273:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
11274:     }
11275:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
11276:     $save_usercreate{'emailrule'} = \@email_rule;
11277: 
11278:     my %userconfig_hash = (
11279:             usercreation     => \%save_usercreate,
11280:             usermodification => \%save_usermodify,
11281:     );
11282:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
11283:                                              $dom);
11284: #
11285: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
11286: #
11287:     if ($putresult eq 'ok') {
11288:         if (keys(%changes) > 0) {
11289:             $resulttext = &mt('Changes made:').'<ul>';
11290:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11291:                 my %lt = &selfcreation_types();
11292:                 foreach my $type (@{$changes{'cancreate'}}) {
11293:                     my $chgtext;
11294:                     if ($type eq 'selfcreate') {
11295:                         if (@{$cancreate{$type}} == 0) {
11296:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
11297:                         } else {
11298:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
11299:                                         '<ul>';
11300:                             foreach my $case (@{$cancreate{$type}}) {
11301:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
11302:                             }
11303:                             $chgtext .= '</ul>';
11304:                             if (ref($cancreate{$type}) eq 'ARRAY') {
11305:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
11306:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11307:                                         if (@{$cancreate{'statustocreate'}} == 0) {
11308:                                             $chgtext .= '<br />'.
11309:                                                         '<span class="LC_warning">'.
11310:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
11311:                                                         '</span>';
11312:                                         }
11313:                                     }
11314:                                 }
11315:                             }
11316:                         }
11317:                     } elsif ($type eq 'shibenv') {
11318:                         if (keys(%{$cancreate{$type}}) == 0) {
11319:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
11320:                         } else {
11321:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
11322:                                         '<ul>';
11323:                             foreach my $field (@shibfields) {
11324:                                 next if ($cancreate{$type}{$field} eq '');
11325:                                 if ($field eq 'inststatus') {
11326:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
11327:                                 } else {
11328:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
11329:                                 }
11330:                             }
11331:                             $chgtext .= '</ul>';
11332:                         }  
11333:                     } elsif ($type eq 'statustocreate') {
11334:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
11335:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
11336:                             if (@{$cancreate{'selfcreate'}} > 0) {
11337:                                 if (@{$cancreate{'statustocreate'}} == 0) {
11338:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
11339:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
11340:                                         $chgtext .= '<br />'.
11341:                                                     '<span class="LC_warning">'.
11342:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
11343:                                                     '</span>';
11344:                                     }
11345:                                 } elsif (ref($usertypes) eq 'HASH') {
11346:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
11347:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
11348:                                     } else {
11349:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
11350:                                     }
11351:                                     $chgtext .= '<ul>';
11352:                                     foreach my $case (@{$cancreate{$type}}) {
11353:                                         if ($case eq 'default') {
11354:                                             $chgtext .= '<li>'.$othertitle.'</li>';
11355:                                         } else {
11356:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
11357:                                         }
11358:                                     }
11359:                                     $chgtext .= '</ul>';
11360:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
11361:                                         $chgtext .= '<br /><span class="LC_warning">'.
11362:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
11363:                                                     '</span>';
11364:                                     }
11365:                                 }
11366:                             } else {
11367:                                 if (@{$cancreate{$type}} == 0) {
11368:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
11369:                                 } else {
11370:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
11371:                                 }
11372:                             }
11373:                         }
11374:                     } elsif ($type eq 'selfcreateprocessing') {
11375:                         my %choices = &Apache::lonlocal::texthash (
11376:                                                                     automatic => 'Automatic approval',
11377:                                                                     approval  => 'Queued for approval',
11378:                                                                   );
11379:                         if (@statuses > 1) {
11380:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
11381:                                         '<ul>';
11382:                            foreach my $type (@statuses) {
11383:                                if ($type eq 'default') {
11384:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
11385:                                } else {
11386:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
11387:                                }
11388:                            }
11389:                            $chgtext .= '</ul>';
11390:                         } else {
11391:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
11392:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
11393:                         }
11394:                     } elsif ($type eq 'captcha') {
11395:                         if ($savecaptcha{$type} eq 'notused') {
11396:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
11397:                         } else {
11398:                             my %captchas = &captcha_phrases();
11399:                             if ($captchas{$savecaptcha{$type}}) {
11400:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
11401:                             } else {
11402:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
11403:                             }
11404:                         }
11405:                     } elsif ($type eq 'recaptchakeys') {
11406:                         my ($privkey,$pubkey);
11407:                         if (ref($savecaptcha{$type}) eq 'HASH') {
11408:                             $pubkey = $savecaptcha{$type}{'public'};
11409:                             $privkey = $savecaptcha{$type}{'private'};
11410:                         }
11411:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
11412:                         if (!$pubkey) {
11413:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
11414:                         } else {
11415:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
11416:                         }
11417:                         if (!$privkey) {
11418:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
11419:                         } else {
11420:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
11421:                         }
11422:                         $chgtext .= '</ul>';
11423:                     } elsif ($type eq 'recaptchaversion') {
11424:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
11425:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
11426:                         }
11427:                     } elsif ($type eq 'emailusername') {
11428:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
11429:                             if (ref($types) eq 'ARRAY') {
11430:                                 foreach my $type (@{$types}) {
11431:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
11432:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
11433:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
11434:                                                     '<ul>';
11435:                                             foreach my $field (@{$infofields}) {
11436:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
11437:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
11438:                                                 }
11439:                                             }
11440:                                             $chgtext .= '</ul>';
11441:                                         } else {
11442:                                             $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 />';
11443:                                         }
11444:                                     } else {
11445:                                         $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 />';
11446:                                     }
11447:                                 }
11448:                             }
11449:                         }
11450:                     } elsif ($type eq 'notify') {
11451:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
11452:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
11453:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
11454:                                 if ($cancreate{'notify'}{'approval'}) {
11455:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
11456:                                 }
11457:                             }
11458:                         }
11459:                     }
11460:                     if ($chgtext) {
11461:                         $resulttext .= '<li>'.$chgtext.'</li>';
11462:                     }
11463:                 }
11464:             }
11465:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
11466:                 my ($emailrules,$emailruleorder) =
11467:                     &Apache::lonnet::inst_userrules($dom,'email');
11468:                 my $chgtext = '<ul>';
11469:                 foreach my $type (@email_rule) {
11470:                     if (ref($emailrules->{$type}) eq 'HASH') {
11471:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
11472:                     }
11473:                 }
11474:                 $chgtext .= '</ul>';
11475:                 if (@email_rule > 0) {
11476:                     $resulttext .= '<li>'.
11477:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
11478:                                        $chgtext.
11479:                                    '</li>';
11480:                 } else {
11481:                     $resulttext .= '<li>'.
11482:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
11483:                                    '</li>';
11484:                 }
11485:             }
11486:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
11487:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
11488:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11489:                 foreach my $type (@{$changes{'selfcreate'}}) {
11490:                     my $typename = $type;
11491:                     if (ref($usertypes) eq 'HASH') {
11492:                         if ($usertypes->{$type} ne '') {
11493:                             $typename = $usertypes->{$type};
11494:                         }
11495:                     }
11496:                     my @modifiable;
11497:                     $resulttext .= '<li>'.
11498:                                     &mt('Self-creation of account by users with status: [_1]',
11499:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
11500:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
11501:                     foreach my $field (@fields) {
11502:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
11503:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
11504:                         }
11505:                     }
11506:                     if (@modifiable > 0) {
11507:                         $resulttext .= join(', ',@modifiable);
11508:                     } else {
11509:                         $resulttext .= &mt('none');
11510:                     }
11511:                     $resulttext .= '</li>';
11512:                 }
11513:                 $resulttext .= '</ul></li>';
11514:             }
11515:             $resulttext .= '</ul>';
11516:         } else {
11517:             $resulttext = &mt('No changes made to self-creation settings');
11518:         }
11519:     } else {
11520:         $resulttext = '<span class="LC_error">'.
11521:             &mt('An error occurred: [_1]',$putresult).'</span>';
11522:     }
11523:     if ($warningmsg ne '') {
11524:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11525:     }
11526:     return $resulttext;
11527: }
11528: 
11529: sub process_captcha {
11530:     my ($container,$changes,$newsettings,$current) = @_;
11531:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
11532:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
11533:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
11534:         $newsettings->{'captcha'} = 'original';
11535:     }
11536:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
11537:         if ($container eq 'cancreate') {
11538:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11539:                 push(@{$changes->{'cancreate'}},'captcha');
11540:             } elsif (!defined($changes->{'cancreate'})) {
11541:                 $changes->{'cancreate'} = ['captcha'];
11542:             }
11543:         } else {
11544:             $changes->{'captcha'} = 1;
11545:         }
11546:     }
11547:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
11548:     if ($newsettings->{'captcha'} eq 'recaptcha') {
11549:         $newpub = $env{'form.'.$container.'_recaptchapub'};
11550:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
11551:         $newpub =~ s/[^\w\-]//g;
11552:         $newpriv =~ s/[^\w\-]//g;
11553:         $newsettings->{'recaptchakeys'} = {
11554:                                              public  => $newpub,
11555:                                              private => $newpriv,
11556:                                           };
11557:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
11558:         $newversion =~ s/\D//g;
11559:         if ($newversion ne '2') {
11560:             $newversion = 1;
11561:         }
11562:         $newsettings->{'recaptchaversion'} = $newversion;
11563:     }
11564:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
11565:         $currpub = $current->{'recaptchakeys'}{'public'};
11566:         $currpriv = $current->{'recaptchakeys'}{'private'};
11567:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
11568:             $newsettings->{'recaptchakeys'} = {
11569:                                                  public  => '',
11570:                                                  private => '',
11571:                                               }
11572:         }
11573:     }
11574:     if ($current->{'captcha'} eq 'recaptcha') {
11575:         $currversion = $current->{'recaptchaversion'};
11576:         if ($currversion ne '2') {
11577:             $currversion = 1;
11578:         }
11579:     }
11580:     if ($currversion ne $newversion) {
11581:         if ($container eq 'cancreate') {
11582:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11583:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
11584:             } elsif (!defined($changes->{'cancreate'})) {
11585:                 $changes->{'cancreate'} = ['recaptchaversion'];
11586:             }
11587:         } else {
11588:             $changes->{'recaptchaversion'} = 1;
11589:         }
11590:     }
11591:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
11592:         if ($container eq 'cancreate') {
11593:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11594:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
11595:             } elsif (!defined($changes->{'cancreate'})) {
11596:                 $changes->{'cancreate'} = ['recaptchakeys'];
11597:             }
11598:         } else {
11599:             $changes->{'recaptchakeys'} = 1;
11600:         }
11601:     }
11602:     return;
11603: }
11604: 
11605: sub modify_usermodification {
11606:     my ($dom,%domconfig) = @_;
11607:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
11608:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11609:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11610:             if ($key eq 'selfcreate') {
11611:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
11612:             } else {  
11613:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
11614:             }
11615:         }
11616:     }
11617:     my @contexts = ('author','course');
11618:     my %context_title = (
11619:                            author => 'In author context',
11620:                            course => 'In course context',
11621:                         );
11622:     my @fields = ('lastname','firstname','middlename','generation',
11623:                   'permanentemail','id');
11624:     my %roles = (
11625:                   author => ['ca','aa'],
11626:                   course => ['st','ep','ta','in','cr'],
11627:                 );
11628:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11629:     foreach my $context (@contexts) {
11630:         foreach my $role (@{$roles{$context}}) {
11631:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
11632:             foreach my $item (@fields) {
11633:                 if (grep(/^\Q$item\E$/,@modifiable)) {
11634:                     $modifyhash{$context}{$role}{$item} = 1;
11635:                 } else {
11636:                     $modifyhash{$context}{$role}{$item} = 0;
11637:                 }
11638:             }
11639:         }
11640:         if (ref($curr_usermodification{$context}) eq 'HASH') {
11641:             foreach my $role (@{$roles{$context}}) {
11642:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
11643:                     foreach my $field (@fields) {
11644:                         if ($modifyhash{$context}{$role}{$field} ne 
11645:                                 $curr_usermodification{$context}{$role}{$field}) {
11646:                             push(@{$changes{$context}},$role);
11647:                             last;
11648:                         }
11649:                     }
11650:                 }
11651:             }
11652:         } else {
11653:             foreach my $context (@contexts) {
11654:                 foreach my $role (@{$roles{$context}}) {
11655:                     push(@{$changes{$context}},$role);
11656:                 }
11657:             }
11658:         }
11659:     }
11660:     my %usermodification_hash =  (
11661:                                    usermodification => \%modifyhash,
11662:                                  );
11663:     my $putresult = &Apache::lonnet::put_dom('configuration',
11664:                                              \%usermodification_hash,$dom);
11665:     if ($putresult eq 'ok') {
11666:         if (keys(%changes) > 0) {
11667:             $resulttext = &mt('Changes made: ').'<ul>';
11668:             foreach my $context (@contexts) {
11669:                 if (ref($changes{$context}) eq 'ARRAY') {
11670:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
11671:                     if (ref($changes{$context}) eq 'ARRAY') {
11672:                         foreach my $role (@{$changes{$context}}) {
11673:                             my $rolename;
11674:                             if ($role eq 'cr') {
11675:                                 $rolename = &mt('Custom');
11676:                             } else {
11677:                                 $rolename = &Apache::lonnet::plaintext($role);
11678:                             }
11679:                             my @modifiable;
11680:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
11681:                             foreach my $field (@fields) {
11682:                                 if ($modifyhash{$context}{$role}{$field}) {
11683:                                     push(@modifiable,$fieldtitles{$field});
11684:                                 }
11685:                             }
11686:                             if (@modifiable > 0) {
11687:                                 $resulttext .= join(', ',@modifiable);
11688:                             } else {
11689:                                 $resulttext .= &mt('none'); 
11690:                             }
11691:                             $resulttext .= '</li>';
11692:                         }
11693:                         $resulttext .= '</ul></li>';
11694:                     }
11695:                 }
11696:             }
11697:             $resulttext .= '</ul>';
11698:         } else {
11699:             $resulttext = &mt('No changes made to user modification settings');
11700:         }
11701:     } else {
11702:         $resulttext = '<span class="LC_error">'.
11703:             &mt('An error occurred: [_1]',$putresult).'</span>';
11704:     }
11705:     return $resulttext;
11706: }
11707: 
11708: sub modify_defaults {
11709:     my ($dom,$lastactref,%domconfig) = @_;
11710:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
11711:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11712:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
11713:                  'portal_def','intauth_cost','intauth_check','intauth_switch');
11714:     my @authtypes = ('internal','krb4','krb5','localauth');
11715:     foreach my $item (@items) {
11716:         $newvalues{$item} = $env{'form.'.$item};
11717:         if ($item eq 'auth_def') {
11718:             if ($newvalues{$item} ne '') {
11719:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
11720:                     push(@errors,$item);
11721:                 }
11722:             }
11723:         } elsif ($item eq 'lang_def') {
11724:             if ($newvalues{$item} ne '') {
11725:                 if ($newvalues{$item} =~ /^(\w+)/) {
11726:                     my $langcode = $1;
11727:                     if ($langcode ne 'x_chef') {
11728:                         if (code2language($langcode) eq '') {
11729:                             push(@errors,$item);
11730:                         }
11731:                     }
11732:                 } else {
11733:                     push(@errors,$item);
11734:                 }
11735:             }
11736:         } elsif ($item eq 'timezone_def') {
11737:             if ($newvalues{$item} ne '') {
11738:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
11739:                     push(@errors,$item);   
11740:                 }
11741:             }
11742:         } elsif ($item eq 'datelocale_def') {
11743:             if ($newvalues{$item} ne '') {
11744:                 my @datelocale_ids = DateTime::Locale->ids();
11745:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
11746:                     push(@errors,$item);
11747:                 }
11748:             }
11749:         } elsif ($item eq 'portal_def') {
11750:             if ($newvalues{$item} ne '') {
11751:                 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])\/?$/) {
11752:                     push(@errors,$item);
11753:                 }
11754:             }
11755:         } elsif ($item eq 'intauth_cost') {
11756:             if ($newvalues{$item} ne '') {
11757:                 if ($newvalues{$item} =~ /\D/) {
11758:                     push(@errors,$item);
11759:                 }
11760:             }
11761:         } elsif ($item eq 'intauth_check') {
11762:             if ($newvalues{$item} ne '') {
11763:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
11764:                     push(@errors,$item);
11765:                 }
11766:             }
11767:         } elsif ($item eq 'intauth_switch') {
11768:             if ($newvalues{$item} ne '') {
11769:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
11770:                     push(@errors,$item);
11771:                 }
11772:             }
11773:         }
11774:         if (grep(/^\Q$item\E$/,@errors)) {
11775:             $newvalues{$item} = $domdefaults{$item};
11776:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
11777:             $changes{$item} = 1;
11778:         }
11779:         $domdefaults{$item} = $newvalues{$item};
11780:     }
11781:     my %defaults_hash = (
11782:                          defaults => \%newvalues,
11783:                         );
11784:     my $title = &defaults_titles();
11785: 
11786:     my $currinststatus;
11787:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
11788:         $currinststatus = $domconfig{'inststatus'};
11789:     } else {
11790:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11791:         $currinststatus = {
11792:                              inststatustypes => $usertypes,
11793:                              inststatusorder => $types,
11794:                              inststatusguest => [],
11795:                           };
11796:     }
11797:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
11798:     my @allpos;
11799:     my %guests;
11800:     my %alltypes;
11801:     my ($currtitles,$currguests,$currorder);
11802:     if (ref($currinststatus) eq 'HASH') {
11803:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
11804:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
11805:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
11806:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
11807:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
11808:                     }
11809:                 }
11810:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
11811:                     my $position = $env{'form.inststatus_pos_'.$type};
11812:                     $position =~ s/\D+//g;
11813:                     $allpos[$position] = $type;
11814:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
11815:                     $alltypes{$type} =~ s/`//g;
11816:                     if ($env{'form.inststatus_guest_'.$type}) {
11817:                         $guests{$type} = 1;
11818:                     }
11819:                 }
11820:             }
11821:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
11822:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
11823:             }
11824:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
11825:             $currtitles =~ s/,$//;
11826:         }
11827:     }
11828:     if ($env{'form.addinststatus'}) {
11829:         my $newtype = $env{'form.addinststatus'};
11830:         $newtype =~ s/\W//g;
11831:         unless (exists($alltypes{$newtype})) {
11832:             if ($env{'form.addinststatus_guest'}) {
11833:                 $guests{$newtype} = 1;
11834:             }
11835:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
11836:             $alltypes{$newtype} =~ s/`//g; 
11837:             my $position = $env{'form.addinststatus_pos'};
11838:             $position =~ s/\D+//g;
11839:             if ($position ne '') {
11840:                 $allpos[$position] = $newtype;
11841:             }
11842:         }
11843:     }
11844:     my (@orderedstatus,@orderedguests);
11845:     foreach my $type (@allpos) {
11846:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
11847:             push(@orderedstatus,$type);
11848:             if ($guests{$type}) {
11849:                 push(@orderedguests,$type);
11850:             }
11851:         }
11852:     }
11853:     foreach my $type (keys(%alltypes)) {
11854:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
11855:             delete($alltypes{$type});
11856:         }
11857:     }
11858:     $defaults_hash{'inststatus'} = {
11859:                                      inststatustypes => \%alltypes,
11860:                                      inststatusorder => \@orderedstatus,
11861:                                      inststatusguest => \@orderedguests,
11862:                                    };
11863:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
11864:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
11865:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
11866:         }
11867:     }
11868:     if ($currorder ne join(',',@orderedstatus)) {
11869:         $changes{'inststatus'}{'inststatusorder'} = 1;
11870:     }
11871:     if ($currguests ne join(',',@orderedguests)) {
11872:         $changes{'inststatus'}{'inststatusguest'} = 1;
11873:     }
11874:     my $newtitles;
11875:     foreach my $item (@orderedstatus) {
11876:         $newtitles .= $alltypes{$item}.',';
11877:     }
11878:     $newtitles =~ s/,$//;
11879:     if ($currtitles ne $newtitles) {
11880:         $changes{'inststatus'}{'inststatustypes'} = 1;
11881:     }
11882:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
11883:                                              $dom);
11884:     if ($putresult eq 'ok') {
11885:         if (keys(%changes) > 0) {
11886:             $resulttext = &mt('Changes made:').'<ul>';
11887:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
11888:             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";
11889:             foreach my $item (sort(keys(%changes))) {
11890:                 if ($item eq 'inststatus') {
11891:                     if (ref($changes{'inststatus'}) eq 'HASH') {
11892:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
11893:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
11894:                             foreach my $type (@orderedstatus) { 
11895:                                 $resulttext .= $alltypes{$type}.', ';
11896:                             }
11897:                             $resulttext =~ s/, $//;
11898:                             $resulttext .= '</li>';
11899:                         }
11900:                         if ($changes{'inststatus'}{'inststatusguest'}) {
11901:                             $resulttext .= '<li>'; 
11902:                             if (@orderedguests) {
11903:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
11904:                                 foreach my $type (@orderedguests) {
11905:                                     $resulttext .= $alltypes{$type}.', ';
11906:                                 }
11907:                                 $resulttext =~ s/, $//;
11908:                             } else {
11909:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
11910:                             }
11911:                             $resulttext .= '</li>';
11912:                         }
11913:                     }
11914:                 } else {
11915:                     my $value = $env{'form.'.$item};
11916:                     if ($value eq '') {
11917:                         $value = &mt('none');
11918:                     } elsif ($item eq 'auth_def') {
11919:                         my %authnames = &authtype_names();
11920:                         my %shortauth = (
11921:                                           internal   => 'int',
11922:                                           krb4       => 'krb4',
11923:                                           krb5       => 'krb5',
11924:                                           localauth  => 'loc',
11925:                         );
11926:                         $value = $authnames{$shortauth{$value}};
11927:                     } elsif ($item eq 'intauth_switch') {
11928:                         my %optiondesc = &Apache::lonlocal::texthash (
11929:                                             0 => 'No',
11930:                                             1 => 'Yes',
11931:                                             2 => 'Yes, and copy existing passwd file to passwd.bak file',
11932:                                          );
11933:                         if ($value =~ /^(0|1|2)$/) {
11934:                             $value = $optiondesc{$value};
11935:                         } else {
11936:                             $value = &mt('none -- defaults to No');
11937:                         }
11938:                     } elsif ($item eq 'intauth_check') {
11939:                         my %optiondesc = &Apache::lonlocal::texthash (
11940:                                              0 => 'No',
11941:                                              1 => 'Yes, allow login then update passwd file using default cost (if higher)',
11942:                                              2 => 'Yes, disallow login if stored cost is less than domain default',
11943:                                          );
11944:                         if ($value =~ /^(0|1|2)$/) {
11945:                             $value = $optiondesc{$value};
11946:                         } else {
11947:                             $value = &mt('none -- defaults to No');
11948:                         }
11949:                     }
11950:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
11951:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
11952:                 }
11953:             }
11954:             $resulttext .= '</ul>';
11955:             $mailmsgtext .= "\n";
11956:             my $cachetime = 24*60*60;
11957:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11958:             if (ref($lastactref) eq 'HASH') {
11959:                 $lastactref->{'domdefaults'} = 1;
11960:             }
11961:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
11962:                 my $notify = 1;
11963:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
11964:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
11965:                         $notify = 0;
11966:                     }
11967:                 }
11968:                 if ($notify) {
11969:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
11970:                                                "LON-CAPA Domain Settings Change - $dom",
11971:                                                $mailmsgtext);
11972:                 }
11973:             }
11974:         } else {
11975:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
11976:         }
11977:     } else {
11978:         $resulttext = '<span class="LC_error">'.
11979:             &mt('An error occurred: [_1]',$putresult).'</span>';
11980:     }
11981:     if (@errors > 0) {
11982:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
11983:         foreach my $item (@errors) {
11984:             $resulttext .= ' "'.$title->{$item}.'",';
11985:         }
11986:         $resulttext =~ s/,$//;
11987:     }
11988:     return $resulttext;
11989: }
11990: 
11991: sub modify_scantron {
11992:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
11993:     my ($resulttext,%confhash,%changes,$errors);
11994:     my $custom = 'custom.tab';
11995:     my $default = 'default.tab';
11996:     my $servadm = $r->dir_config('lonAdmEMail');
11997:     my ($configuserok,$author_ok,$switchserver) = 
11998:         &config_check($dom,$confname,$servadm);
11999:     if ($env{'form.scantronformat.filename'} ne '') {
12000:         my $error;
12001:         if ($configuserok eq 'ok') {
12002:             if ($switchserver) {
12003:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
12004:             } else {
12005:                 if ($author_ok eq 'ok') {
12006:                     my ($result,$scantronurl) =
12007:                         &publishlogo($r,'upload','scantronformat',$dom,
12008:                                      $confname,'scantron','','',$custom);
12009:                     if ($result eq 'ok') {
12010:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
12011:                         $changes{'scantronformat'} = 1;
12012:                     } else {
12013:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
12014:                     }
12015:                 } else {
12016:                     $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);
12017:                 }
12018:             }
12019:         } else {
12020:             $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);
12021:         }
12022:         if ($error) {
12023:             &Apache::lonnet::logthis($error);
12024:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12025:         }
12026:     }
12027:     if (ref($domconfig{'scantron'}) eq 'HASH') {
12028:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
12029:             if ($env{'form.scantronformat_del'}) {
12030:                 $confhash{'scantron'}{'scantronformat'} = '';
12031:                 $changes{'scantronformat'} = 1;
12032:             }
12033:         }
12034:     }
12035:     if (keys(%confhash) > 0) {
12036:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
12037:                                                  $dom);
12038:         if ($putresult eq 'ok') {
12039:             if (keys(%changes) > 0) {
12040:                 if (ref($confhash{'scantron'}) eq 'HASH') {
12041:                     $resulttext = &mt('Changes made:').'<ul>';
12042:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
12043:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
12044:                     } else {
12045:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
12046:                     }
12047:                     $resulttext .= '</ul>';
12048:                 } else {
12049:                     $resulttext = &mt('Changes made to bubblesheet format file.');
12050:                 }
12051:                 $resulttext .= '</ul>';
12052:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
12053:                 if (ref($lastactref) eq 'HASH') {
12054:                     $lastactref->{'domainconfig'} = 1;
12055:                 }
12056:             } else {
12057:                 $resulttext = &mt('No changes made to bubblesheet format file');
12058:             }
12059:         } else {
12060:             $resulttext = '<span class="LC_error">'.
12061:                 &mt('An error occurred: [_1]',$putresult).'</span>';
12062:         }
12063:     } else {
12064:         $resulttext = &mt('No changes made to bubblesheet format file'); 
12065:     }
12066:     if ($errors) {
12067:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12068:                        $errors.'</ul>';
12069:     }
12070:     return $resulttext;
12071: }
12072: 
12073: sub modify_coursecategories {
12074:     my ($dom,$lastactref,%domconfig) = @_;
12075:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
12076:         $cathash);
12077:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
12078:     my @catitems = ('unauth','auth');
12079:     my @cattypes = ('std','domonly','codesrch','none');
12080:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12081:         $cathash = $domconfig{'coursecategories'}{'cats'};
12082:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
12083:             $changes{'togglecats'} = 1;
12084:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
12085:         }
12086:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
12087:             $changes{'categorize'} = 1;
12088:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
12089:         }
12090:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
12091:             $changes{'togglecatscomm'} = 1;
12092:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
12093:         }
12094:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
12095:             $changes{'categorizecomm'} = 1;
12096:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
12097:         }
12098:         foreach my $item (@catitems) {
12099:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12100:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
12101:                     $changes{$item} = 1;
12102:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12103:                 }
12104:             }
12105:         }
12106:     } else {
12107:         $changes{'togglecats'} = 1;
12108:         $changes{'categorize'} = 1;
12109:         $changes{'togglecatscomm'} = 1;
12110:         $changes{'categorizecomm'} = 1;
12111:         $domconfig{'coursecategories'} = {
12112:                                              togglecats => $env{'form.togglecats'},
12113:                                              categorize => $env{'form.categorize'},
12114:                                              togglecatscomm => $env{'form.togglecatscomm'},
12115:                                              categorizecomm => $env{'form.categorizecomm'},
12116:                                          };
12117:         foreach my $item (@catitems) {
12118:             if ($env{'form.coursecat_'.$item} ne 'std') {
12119:                 $changes{$item} = 1;
12120:             }
12121:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12122:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12123:             }
12124:         }
12125:     }
12126:     if (ref($cathash) eq 'HASH') {
12127:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
12128:             push (@deletecategory,'instcode::0');
12129:         }
12130:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
12131:             push(@deletecategory,'communities::0');
12132:         }
12133:     }
12134:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
12135:     if (ref($cathash) eq 'HASH') {
12136:         if (@deletecategory > 0) {
12137:             #FIXME Need to remove category from all courses using a deleted category 
12138:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
12139:             foreach my $item (@deletecategory) {
12140:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
12141:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
12142:                     $deletions{$item} = 1;
12143:                     &recurse_cat_deletes($item,$cathash,\%deletions);
12144:                 }
12145:             }
12146:         }
12147:         foreach my $item (keys(%{$cathash})) {
12148:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
12149:             if ($cathash->{$item} ne $env{'form.'.$item}) {
12150:                 $reorderings{$item} = 1;
12151:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
12152:             }
12153:             if ($env{'form.addcategory_name_'.$item} ne '') {
12154:                 my $newcat = $env{'form.addcategory_name_'.$item};
12155:                 my $newdepth = $depth+1;
12156:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
12157:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
12158:                 $adds{$newitem} = 1; 
12159:             }
12160:             if ($env{'form.subcat_'.$item} ne '') {
12161:                 my $newcat = $env{'form.subcat_'.$item};
12162:                 my $newdepth = $depth+1;
12163:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
12164:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
12165:                 $adds{$newitem} = 1;
12166:             }
12167:         }
12168:     }
12169:     if ($env{'form.instcode'} eq '1') {
12170:         if (ref($cathash) eq 'HASH') {
12171:             my $newitem = 'instcode::0';
12172:             if ($cathash->{$newitem} eq '') {  
12173:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
12174:                 $adds{$newitem} = 1;
12175:             }
12176:         } else {
12177:             my $newitem = 'instcode::0';
12178:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
12179:             $adds{$newitem} = 1;
12180:         }
12181:     }
12182:     if ($env{'form.communities'} eq '1') {
12183:         if (ref($cathash) eq 'HASH') {
12184:             my $newitem = 'communities::0';
12185:             if ($cathash->{$newitem} eq '') {
12186:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
12187:                 $adds{$newitem} = 1;
12188:             }
12189:         } else {
12190:             my $newitem = 'communities::0';
12191:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
12192:             $adds{$newitem} = 1;
12193:         }
12194:     }
12195:     if ($env{'form.addcategory_name'} ne '') {
12196:         if (($env{'form.addcategory_name'} ne 'instcode') &&
12197:             ($env{'form.addcategory_name'} ne 'communities')) {
12198:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
12199:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
12200:             $adds{$newitem} = 1;
12201:         }
12202:     }
12203:     my $putresult;
12204:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12205:         if (keys(%deletions) > 0) {
12206:             foreach my $key (keys(%deletions)) {
12207:                 if ($predelallitems{$key} ne '') {
12208:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
12209:                 }
12210:             }
12211:         }
12212:         my (@chkcats,@chktrails,%chkallitems);
12213:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
12214:         if (ref($chkcats[0]) eq 'ARRAY') {
12215:             my $depth = 0;
12216:             my $chg = 0;
12217:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
12218:                 my $name = $chkcats[0][$i];
12219:                 my $item;
12220:                 if ($name eq '') {
12221:                     $chg ++;
12222:                 } else {
12223:                     $item = &escape($name).'::0';
12224:                     if ($chg) {
12225:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
12226:                     }
12227:                     $depth ++; 
12228:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
12229:                     $depth --;
12230:                 }
12231:             }
12232:         }
12233:     }
12234:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12235:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
12236:         if ($putresult eq 'ok') {
12237:             my %title = (
12238:                          togglecats     => 'Show/Hide a course in catalog',
12239:                          categorize     => 'Assign a category to a course',
12240:                          togglecatscomm => 'Show/Hide a community in catalog',
12241:                          categorizecomm => 'Assign a category to a community',
12242:                         );
12243:             my %level = (
12244:                          dom  => 'set in Domain ("Modify Course/Community")',
12245:                          crs  => 'set in Course ("Course Configuration")',
12246:                          comm => 'set in Community ("Community Configuration")',
12247:                          none     => 'No catalog',
12248:                          std      => 'Standard catalog',
12249:                          domonly  => 'Domain-only catalog',
12250:                          codesrch => 'Code search form',
12251:                         );
12252:             $resulttext = &mt('Changes made:').'<ul>';
12253:             if ($changes{'togglecats'}) {
12254:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
12255:             }
12256:             if ($changes{'categorize'}) {
12257:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
12258:             }
12259:             if ($changes{'togglecatscomm'}) {
12260:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
12261:             }
12262:             if ($changes{'categorizecomm'}) {
12263:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
12264:             }
12265:             if ($changes{'unauth'}) {
12266:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
12267:             }
12268:             if ($changes{'auth'}) {
12269:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
12270:             }
12271:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12272:                 my $cathash;
12273:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12274:                     $cathash = $domconfig{'coursecategories'}{'cats'};
12275:                 } else {
12276:                     $cathash = {};
12277:                 } 
12278:                 my (@cats,@trails,%allitems);
12279:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
12280:                 if (keys(%deletions) > 0) {
12281:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
12282:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
12283:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
12284:                     }
12285:                     $resulttext .= '</ul></li>';
12286:                 }
12287:                 if (keys(%reorderings) > 0) {
12288:                     my %sort_by_trail;
12289:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
12290:                     foreach my $key (keys(%reorderings)) {
12291:                         if ($allitems{$key} ne '') {
12292:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
12293:                         }
12294:                     }
12295:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
12296:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
12297:                     }
12298:                     $resulttext .= '</ul></li>';
12299:                 }
12300:                 if (keys(%adds) > 0) {
12301:                     my %sort_by_trail;
12302:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
12303:                     foreach my $key (keys(%adds)) {
12304:                         if ($allitems{$key} ne '') {
12305:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
12306:                         }
12307:                     }
12308:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
12309:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
12310:                     }
12311:                     $resulttext .= '</ul></li>';
12312:                 }
12313:             }
12314:             $resulttext .= '</ul>';
12315:             if ($changes{'unauth'} || $changes{'auth'}) {
12316:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
12317:                 if ($changes{'auth'}) {
12318:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
12319:                 }
12320:                 if ($changes{'unauth'}) {
12321:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
12322:                 }
12323:                 my $cachetime = 24*60*60;
12324:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12325:                 if (ref($lastactref) eq 'HASH') {
12326:                     $lastactref->{'domdefaults'} = 1;
12327:                 }
12328:             }
12329:         } else {
12330:             $resulttext = '<span class="LC_error">'.
12331:                           &mt('An error occurred: [_1]',$putresult).'</span>';
12332:         }
12333:     } else {
12334:         $resulttext = &mt('No changes made to course and community categories');
12335:     }
12336:     return $resulttext;
12337: }
12338: 
12339: sub modify_serverstatuses {
12340:     my ($dom,%domconfig) = @_;
12341:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
12342:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
12343:         %currserverstatus = %{$domconfig{'serverstatuses'}};
12344:     }
12345:     my @pages = &serverstatus_pages();
12346:     foreach my $type (@pages) {
12347:         $newserverstatus{$type}{'namedusers'} = '';
12348:         $newserverstatus{$type}{'machines'} = '';
12349:         if (defined($env{'form.'.$type.'_namedusers'})) {
12350:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
12351:             my @okusers;
12352:             foreach my $user (@users) {
12353:                 my ($uname,$udom) = split(/:/,$user);
12354:                 if (($udom =~ /^$match_domain$/) &&   
12355:                     (&Apache::lonnet::domain($udom)) &&
12356:                     ($uname =~ /^$match_username$/)) {
12357:                     if (!grep(/^\Q$user\E/,@okusers)) {
12358:                         push(@okusers,$user);
12359:                     }
12360:                 }
12361:             }
12362:             if (@okusers > 0) {
12363:                  @okusers = sort(@okusers);
12364:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
12365:             }
12366:         }
12367:         if (defined($env{'form.'.$type.'_machines'})) {
12368:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
12369:             my @okmachines;
12370:             foreach my $ip (@machines) {
12371:                 my @parts = split(/\./,$ip);
12372:                 next if (@parts < 4);
12373:                 my $badip = 0;
12374:                 for (my $i=0; $i<4; $i++) {
12375:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
12376:                         $badip = 1;
12377:                         last;
12378:                     }
12379:                 }
12380:                 if (!$badip) {
12381:                     push(@okmachines,$ip);     
12382:                 }
12383:             }
12384:             @okmachines = sort(@okmachines);
12385:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
12386:         }
12387:     }
12388:     my %serverstatushash =  (
12389:                                 serverstatuses => \%newserverstatus,
12390:                             );
12391:     foreach my $type (@pages) {
12392:         foreach my $setting ('namedusers','machines') {
12393:             my (@current,@new);
12394:             if (ref($currserverstatus{$type}) eq 'HASH') {
12395:                 if ($currserverstatus{$type}{$setting} ne '') { 
12396:                     @current = split(/,/,$currserverstatus{$type}{$setting});
12397:                 }
12398:             }
12399:             if ($newserverstatus{$type}{$setting} ne '') {
12400:                 @new = split(/,/,$newserverstatus{$type}{$setting});
12401:             }
12402:             if (@current > 0) {
12403:                 if (@new > 0) {
12404:                     foreach my $item (@current) {
12405:                         if (!grep(/^\Q$item\E$/,@new)) {
12406:                             $changes{$type}{$setting} = 1;
12407:                             last;
12408:                         }
12409:                     }
12410:                     foreach my $item (@new) {
12411:                         if (!grep(/^\Q$item\E$/,@current)) {
12412:                             $changes{$type}{$setting} = 1;
12413:                             last;
12414:                         }
12415:                     }
12416:                 } else {
12417:                     $changes{$type}{$setting} = 1;
12418:                 }
12419:             } elsif (@new > 0) {
12420:                 $changes{$type}{$setting} = 1;
12421:             }
12422:         }
12423:     }
12424:     if (keys(%changes) > 0) {
12425:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
12426:         my $putresult = &Apache::lonnet::put_dom('configuration',
12427:                                                  \%serverstatushash,$dom);
12428:         if ($putresult eq 'ok') {
12429:             $resulttext .= &mt('Changes made:').'<ul>';
12430:             foreach my $type (@pages) {
12431:                 if (ref($changes{$type}) eq 'HASH') {
12432:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
12433:                     if ($changes{$type}{'namedusers'}) {
12434:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
12435:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
12436:                         } else {
12437:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
12438:                         }
12439:                     }
12440:                     if ($changes{$type}{'machines'}) {
12441:                         if ($newserverstatus{$type}{'machines'} eq '') {
12442:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
12443:                         } else {
12444:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
12445:                         }
12446: 
12447:                     }
12448:                     $resulttext .= '</ul></li>';
12449:                 }
12450:             }
12451:             $resulttext .= '</ul>';
12452:         } else {
12453:             $resulttext = '<span class="LC_error">'.
12454:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
12455: 
12456:         }
12457:     } else {
12458:         $resulttext = &mt('No changes made to access to server status pages');
12459:     }
12460:     return $resulttext;
12461: }
12462: 
12463: sub modify_helpsettings {
12464:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12465:     my ($resulttext,$errors,%changes,%helphash);
12466:     my %defaultchecked = ('submitbugs' => 'on');
12467:     my @offon = ('off','on');
12468:     my @toggles = ('submitbugs');
12469:     my %current = ('submitbugs' => '',
12470:                    'adhoc'      => {},
12471:                   );
12472:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
12473:         %current = %{$domconfig{'helpsettings'}};
12474:     }
12475:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12476:     foreach my $item (@toggles) {
12477:         if ($defaultchecked{$item} eq 'on') { 
12478:             if ($current{$item} eq '') {
12479:                 if ($env{'form.'.$item} eq '0') {
12480:                     $changes{$item} = 1;
12481:                 }
12482:             } elsif ($current{$item} ne $env{'form.'.$item}) {
12483:                 $changes{$item} = 1;
12484:             }
12485:         } elsif ($defaultchecked{$item} eq 'off') {
12486:             if ($current{$item} eq '') {
12487:                 if ($env{'form.'.$item} eq '1') {
12488:                     $changes{$item} = 1;
12489:                 }
12490:             } elsif ($current{$item} ne $env{'form.'.$item}) {
12491:                 $changes{$item} = 1;
12492:             }
12493:         }
12494:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
12495:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
12496:         }
12497:     }
12498:     my $maxnum = $env{'form.helproles_maxnum'};
12499:     my $confname = $dom.'-domainconfig';
12500:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
12501:     my (@allpos,%newsettings,%changedprivs,$newrole);
12502:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12503:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
12504:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
12505:     my %lt = &Apache::lonlocal::texthash(
12506:                     s      => 'system',
12507:                     d      => 'domain',
12508:                     order  => 'Display order',
12509:                     access => 'Role usage',
12510:                     all    => 'All with domain helpdesk or helpdesk assistant role',
12511:                     dh     => 'All with domain helpdesk role',
12512:                     da     => 'All with domain helpdesk assistant role',
12513:                     none   => 'None',
12514:                     status => 'Determined based on institutional status',
12515:                     inc    => 'Include all, but exclude specific personnel',
12516:                     exc    => 'Exclude all, but include specific personnel',
12517:     );
12518:     for (my $num=0; $num<=$maxnum; $num++) {
12519:         my ($prefix,$identifier,$rolename,%curr);
12520:         if ($num == $maxnum) {
12521:             next unless ($env{'form.newcusthelp'} == $maxnum);
12522:             $identifier = 'custhelp'.$num;
12523:             $prefix = 'helproles_'.$num;
12524:             $rolename = $env{'form.custhelpname'.$num};
12525:             $rolename=~s/[^A-Za-z0-9]//gs;
12526:             next if ($rolename eq '');
12527:             next if (exists($existing{'rolesdef_'.$rolename}));
12528:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12529:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12530:                                                      $newprivs{'c'},$confname,$dom);
12531:             if ($result ne 'ok') {
12532:                 $errors .= '<li><span class="LC_error">'.
12533:                            &mt('An error occurred storing the new custom role: [_1]',
12534:                            $result).'</span></li>';
12535:                 next;
12536:             } else {
12537:                 $changedprivs{$rolename} = \%newprivs;
12538:                 $newrole = $rolename;
12539:             }
12540:         } else {
12541:             $prefix = 'helproles_'.$num;
12542:             $rolename = $env{'form.'.$prefix};
12543:             next if ($rolename eq '');
12544:             next unless (exists($existing{'rolesdef_'.$rolename}));
12545:             $identifier = 'custhelp'.$num;
12546:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12547:             my %currprivs;
12548:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
12549:                 split(/\_/,$existing{'rolesdef_'.$rolename});
12550:             foreach my $level ('c','d','s') {
12551:                 if ($newprivs{$level} ne $currprivs{$level}) {
12552:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12553:                                                              $newprivs{'c'},$confname,$dom);
12554:                     if ($result ne 'ok') {
12555:                         $errors .= '<li><span class="LC_error">'.
12556:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
12557:                                        $rolename,$result).'</span></li>';
12558:                     } else {
12559:                         $changedprivs{$rolename} = \%newprivs;
12560:                     }
12561:                     last;
12562:                 }
12563:             }
12564:             if (ref($current{'adhoc'}) eq 'HASH') {
12565:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12566:                     %curr = %{$current{'adhoc'}{$rolename}};
12567:                 }
12568:             }
12569:         }
12570:         my $newpos = $env{'form.'.$prefix.'_pos'};
12571:         $newpos =~ s/\D+//g;
12572:         $allpos[$newpos] = $rolename;
12573:         my $newdesc = $env{'form.'.$prefix.'_desc'};
12574:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
12575:         if ($curr{'desc'}) {
12576:             if ($curr{'desc'} ne $newdesc) {
12577:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
12578:                 $newsettings{$rolename}{'desc'} = $newdesc;
12579:             }
12580:         } elsif ($newdesc ne '') {
12581:             $changes{'customrole'}{$rolename}{'desc'} = 1;
12582:             $newsettings{$rolename}{'desc'} = $newdesc;
12583:         }
12584:         my $access = $env{'form.'.$prefix.'_access'};
12585:         if (grep(/^\Q$access\E$/,@accesstypes)) {
12586:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
12587:             if ($access eq 'status') {
12588:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
12589:                 if (scalar(@statuses) == 0) {
12590:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
12591:                 } else {
12592:                     my (@shownstatus,$numtypes);
12593:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12594:                     if (ref($types) eq 'ARRAY') {
12595:                         $numtypes = scalar(@{$types});
12596:                         foreach my $type (sort(@statuses)) {
12597:                             if ($type eq 'default') {
12598:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12599:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
12600:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12601:                                 push(@shownstatus,$usertypes->{$type});
12602:                             }
12603:                         }
12604:                     }
12605:                     if (grep(/^default$/,@statuses)) {
12606:                         push(@shownstatus,$othertitle);
12607:                     }
12608:                     if (scalar(@shownstatus) == 1+$numtypes) {
12609:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
12610:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
12611:                     } else {
12612:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
12613:                         if (ref($curr{'status'}) eq 'ARRAY') {
12614:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12615:                             if (@diffs) {
12616:                                 $changes{'customrole'}{$rolename}{$access} = 1;
12617:                             }
12618:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12619:                             $changes{'customrole'}{$rolename}{$access} = 1;
12620:                         }
12621:                     }
12622:                 }
12623:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
12624:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
12625:                 my @newspecstaff;
12626:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12627:                 foreach my $person (sort(@personnel)) {
12628:                     if ($domhelpdesk{$person}) {
12629:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
12630:                     }
12631:                 }
12632:                 if (ref($curr{$access}) eq 'ARRAY') {
12633:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12634:                     if (@diffs) {
12635:                         $changes{'customrole'}{$rolename}{$access} = 1;
12636:                     }
12637:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12638:                     $changes{'customrole'}{$rolename}{$access} = 1;
12639:                 }
12640:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12641:                     my ($uname,$udom) = split(/:/,$person);
12642:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
12643:                 }
12644:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
12645:             }
12646:         } else {
12647:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
12648:         }
12649:         unless ($curr{'access'} eq $access) {
12650:             $changes{'customrole'}{$rolename}{'access'} = 1;
12651:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
12652:         }
12653:     }
12654:     if (@allpos > 0) {
12655:         my $idx = 0;
12656:         foreach my $rolename (@allpos) {
12657:             if ($rolename ne '') {
12658:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
12659:                 if (ref($current{'adhoc'}) eq 'HASH') {
12660:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12661:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
12662:                             $changes{'customrole'}{$rolename}{'order'} = 1;
12663:                             $newsettings{$rolename}{'order'} = $idx+1;
12664:                         }
12665:                     }
12666:                 }
12667:                 $idx ++;
12668:             }
12669:         }
12670:     }
12671:     my $putresult;
12672:     if (keys(%changes) > 0) {
12673:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
12674:         if ($putresult eq 'ok') {
12675:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
12676:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
12677:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
12678:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
12679:                 }
12680:             }
12681:             my $cachetime = 24*60*60;
12682:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12683:             if (ref($lastactref) eq 'HASH') {
12684:                 $lastactref->{'domdefaults'} = 1;
12685:             }
12686:         } else {
12687:             $errors .= '<li><span class="LC_error">'.
12688:                        &mt('An error occurred storing the settings: [_1]',
12689:                            $putresult).'</span></li>';
12690:         }
12691:     }
12692:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
12693:         $resulttext = &mt('Changes made:').'<ul>';
12694:         my (%shownprivs,@levelorder);
12695:         @levelorder = ('c','d','s');
12696:         if ((keys(%changes)) && ($putresult eq 'ok')) {
12697:             foreach my $item (sort(keys(%changes))) {
12698:                 if ($item eq 'submitbugs') {
12699:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
12700:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
12701:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
12702:                 } elsif ($item eq 'customrole') {
12703:                     if (ref($changes{'customrole'}) eq 'HASH') {
12704:                         my @keyorder = ('order','desc','access','status','exc','inc');
12705:                         my %keytext = &Apache::lonlocal::texthash(
12706:                                                                    order  => 'Order',
12707:                                                                    desc   => 'Role description',
12708:                                                                    access => 'Role usage',
12709:                                                                    status => 'Allowed institutional types',
12710:                                                                    exc    => 'Allowed personnel',
12711:                                                                    inc    => 'Disallowed personnel',
12712:                         );
12713:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
12714:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
12715:                                 if ($role eq $newrole) {
12716:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
12717:                                                               $role).'<ul>';
12718:                                 } else {
12719:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
12720:                                                               $role).'<ul>';
12721:                                 }
12722:                                 foreach my $key (@keyorder) {
12723:                                     if ($changes{'customrole'}{$role}{$key}) {
12724:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
12725:                                                                   $keytext{$key},$newsettings{$role}{$key}).
12726:                                                        '</li>';
12727:                                     }
12728:                                 }
12729:                                 if (ref($changedprivs{$role}) eq 'HASH') {
12730:                                     $shownprivs{$role} = 1;
12731:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
12732:                                     foreach my $level (@levelorder) {
12733:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
12734:                                             next if ($item eq '');
12735:                                             my ($priv) = split(/\&/,$item,2);
12736:                                             if (&Apache::lonnet::plaintext($priv)) {
12737:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
12738:                                                 unless ($level eq 'c') {
12739:                                                     $resulttext .= ' ('.$lt{$level}.')';
12740:                                                 }
12741:                                                 $resulttext .= '</li>';
12742:                                             }
12743:                                         }
12744:                                     }
12745:                                     $resulttext .= '</ul>';
12746:                                 }
12747:                                 $resulttext .= '</ul></li>';
12748:                             }
12749:                         }
12750:                     }
12751:                 }
12752:             }
12753:         }
12754:         if (keys(%changedprivs)) {
12755:             foreach my $role (sort(keys(%changedprivs))) {
12756:                 unless ($shownprivs{$role}) {
12757:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
12758:                                               $role).'<ul>'.
12759:                                    '<li>'.&mt('Privileges set to :').'<ul>';
12760:                     foreach my $level (@levelorder) {
12761:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
12762:                             next if ($item eq '');
12763:                             my ($priv) = split(/\&/,$item,2);
12764:                             if (&Apache::lonnet::plaintext($priv)) {
12765:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
12766:                                 unless ($level eq 'c') {
12767:                                     $resulttext .= ' ('.$lt{$level}.')';
12768:                                 }
12769:                                 $resulttext .= '</li>';
12770:                             }
12771:                         }
12772:                     }
12773:                     $resulttext .= '</ul></li></ul></li>';
12774:                 }
12775:             }
12776:         }
12777:         $resulttext .= '</ul>';
12778:     } else {
12779:         $resulttext = &mt('No changes made to help settings');
12780:     }
12781:     if ($errors) {
12782:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
12783:                                     $errors.'</ul>';
12784:     }
12785:     return $resulttext;
12786: }
12787: 
12788: sub modify_coursedefaults {
12789:     my ($dom,$lastactref,%domconfig) = @_;
12790:     my ($resulttext,$errors,%changes,%defaultshash);
12791:     my %defaultchecked = (
12792:                            'uselcmath'       => 'on',
12793:                            'usejsme'         => 'on'
12794:                          );
12795:     my @toggles = ('uselcmath','usejsme');
12796:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
12797:                    'uploadquota_community','uploadquota_textbook','mysqltables_official',
12798:                    'mysqltables_unofficial','mysqltables_community','mysqltables_textbook');
12799:     my @types = ('official','unofficial','community','textbook');
12800:     my %staticdefaults = (
12801:                            anonsurvey_threshold => 10,
12802:                            uploadquota          => 500,
12803:                            postsubmit           => 60,
12804:                            mysqltables          => 172800,
12805:                          );
12806: 
12807:     $defaultshash{'coursedefaults'} = {};
12808: 
12809:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
12810:         if ($domconfig{'coursedefaults'} eq '') {
12811:             $domconfig{'coursedefaults'} = {};
12812:         }
12813:     }
12814: 
12815:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
12816:         foreach my $item (@toggles) {
12817:             if ($defaultchecked{$item} eq 'on') {
12818:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
12819:                     ($env{'form.'.$item} eq '0')) {
12820:                     $changes{$item} = 1;
12821:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
12822:                     $changes{$item} = 1;
12823:                 }
12824:             } elsif ($defaultchecked{$item} eq 'off') {
12825:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
12826:                     ($env{'form.'.$item} eq '1')) {
12827:                     $changes{$item} = 1;
12828:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
12829:                     $changes{$item} = 1;
12830:                 }
12831:             }
12832:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
12833:         }
12834:         foreach my $item (@numbers) {
12835:             my ($currdef,$newdef);
12836:             $newdef = $env{'form.'.$item};
12837:             if ($item eq 'anonsurvey_threshold') {
12838:                 $currdef = $domconfig{'coursedefaults'}{$item};
12839:                 $newdef =~ s/\D//g;
12840:                 if ($newdef eq '' || $newdef < 1) {
12841:                     $newdef = 1;
12842:                 }
12843:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
12844:             } else {
12845:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
12846:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
12847:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
12848:                 }
12849:                 $newdef =~ s/[^\w.\-]//g;
12850:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
12851:             }
12852:             if ($currdef ne $newdef) {
12853:                 my $staticdef;
12854:                 if ($item eq 'anonsurvey_threshold') {
12855:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
12856:                         $changes{$item} = 1;
12857:                     }
12858:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
12859:                     my $setting = $1;
12860:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
12861:                         $changes{$setting} = 1;
12862:                     }
12863:                 }
12864:             }
12865:         }
12866:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
12867:         my @currclonecode;
12868:         if (ref($currclone) eq 'HASH') {
12869:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
12870:                 @currclonecode = @{$currclone->{'instcode'}};
12871:             }
12872:         }
12873:         my $newclone;
12874:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
12875:             $newclone = $env{'form.canclone'};
12876:         }
12877:         if ($newclone eq 'instcode') {
12878:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
12879:             my (%codedefaults,@code_order,@clonecode);
12880:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
12881:                                                     \@code_order);
12882:             foreach my $item (@code_order) {
12883:                 if (grep(/^\Q$item\E$/,@newcodes)) {
12884:                     push(@clonecode,$item);
12885:                 }
12886:             }
12887:             if (@clonecode) {
12888:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
12889:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
12890:                 if (@diffs) {
12891:                     $changes{'canclone'} = 1;
12892:                 }
12893:             } else {
12894:                 $newclone eq '';
12895:             }
12896:         } elsif ($newclone ne '') {
12897:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
12898:         }
12899:         if ($newclone ne $currclone) {
12900:             $changes{'canclone'} = 1;
12901:         }
12902:         my %credits;
12903:         foreach my $type (@types) {
12904:             unless ($type eq 'community') {
12905:                 $credits{$type} = $env{'form.'.$type.'_credits'};
12906:                 $credits{$type} =~ s/[^\d.]+//g;
12907:             }
12908:         }
12909:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
12910:             ($env{'form.coursecredits'} eq '1')) {
12911:             $changes{'coursecredits'} = 1;
12912:             foreach my $type (keys(%credits)) {
12913:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
12914:             }
12915:         } else {
12916:             if ($env{'form.coursecredits'} eq '1') {
12917:                 foreach my $type (@types) {
12918:                     unless ($type eq 'community') {
12919:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
12920:                             $changes{'coursecredits'} = 1;
12921:                         }
12922:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
12923:                     }
12924:                 }
12925:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
12926:                 foreach my $type (@types) {
12927:                     unless ($type eq 'community') {
12928:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
12929:                             $changes{'coursecredits'} = 1;
12930:                             last;
12931:                         }
12932:                     }
12933:                 }
12934:             }
12935:         }
12936:         if ($env{'form.postsubmit'} eq '1') {
12937:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
12938:             my %currtimeout;
12939:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
12940:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
12941:                     $changes{'postsubmit'} = 1;
12942:                 }
12943:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
12944:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
12945:                 }
12946:             } else {
12947:                 $changes{'postsubmit'} = 1;
12948:             }
12949:             foreach my $type (@types) {
12950:                 my $timeout = $env{'form.'.$type.'_timeout'};
12951:                 $timeout =~ s/\D//g;
12952:                 if ($timeout == $staticdefaults{'postsubmit'}) {
12953:                     $timeout = '';
12954:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
12955:                     $timeout = '0';
12956:                 }
12957:                 unless ($timeout eq '') {
12958:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
12959:                 }
12960:                 if (exists($currtimeout{$type})) {
12961:                     if ($timeout ne $currtimeout{$type}) {
12962:                         $changes{'postsubmit'} = 1;
12963:                     }
12964:                 } elsif ($timeout ne '') {
12965:                     $changes{'postsubmit'} = 1;
12966:                 }
12967:             }
12968:         } else {
12969:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
12970:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
12971:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
12972:                     $changes{'postsubmit'} = 1;
12973:                 }
12974:             } else {
12975:                 $changes{'postsubmit'} = 1;
12976:             }
12977:         }
12978:     }
12979:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
12980:                                              $dom);
12981:     if ($putresult eq 'ok') {
12982:         if (keys(%changes) > 0) {
12983:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12984:             if (($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
12985:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
12986:                 ($changes{'canclone'}) || ($changes{'mysqltables'})) {
12987:                 foreach my $item ('uselcmath','usejsme') {
12988:                     if ($changes{$item}) {
12989:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
12990:                     }
12991:                 }
12992:                 if ($changes{'coursecredits'}) {
12993:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
12994:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
12995:                             $domdefaults{$type.'credits'} =
12996:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
12997:                         }
12998:                     }
12999:                 }
13000:                 if ($changes{'postsubmit'}) {
13001:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13002:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
13003:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13004:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
13005:                                 $domdefaults{$type.'postsubtimeout'} =
13006:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13007:                             }
13008:                         }
13009:                     }
13010:                 }
13011:                 if ($changes{'uploadquota'}) {
13012:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13013:                         foreach my $type (@types) {
13014:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
13015:                         }
13016:                     }
13017:                 }
13018:                 if ($changes{'canclone'}) {
13019:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13020:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13021:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
13022:                             if (@clonecodes) {
13023:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
13024:                             }
13025:                         }
13026:                     } else {
13027:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
13028:                     }
13029:                 }
13030:                 my $cachetime = 24*60*60;
13031:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13032:                 if (ref($lastactref) eq 'HASH') {
13033:                     $lastactref->{'domdefaults'} = 1;
13034:                 }
13035:             }
13036:             $resulttext = &mt('Changes made:').'<ul>';
13037:             foreach my $item (sort(keys(%changes))) {
13038:                 if ($item eq 'uselcmath') {
13039:                     if ($env{'form.'.$item} eq '1') {
13040:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
13041:                     } else {
13042:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
13043:                     }
13044:                 } elsif ($item eq 'usejsme') {
13045:                     if ($env{'form.'.$item} eq '1') {
13046:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
13047:                     } else {
13048:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
13049:                     }
13050:                 } elsif ($item eq 'anonsurvey_threshold') {
13051:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
13052:                 } elsif ($item eq 'uploadquota') {
13053:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13054:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
13055:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
13056:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
13057:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
13058: 
13059:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
13060:                                        '</ul>'.
13061:                                        '</li>';
13062:                     } else {
13063:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
13064:                     }
13065:                 } elsif ($item eq 'mysqltables') {
13066:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
13067:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
13068:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
13069:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
13070:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
13071:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
13072:                                        '</ul>'.
13073:                                        '</li>';
13074:                     } else {
13075:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
13076:                     }
13077:                 } elsif ($item eq 'postsubmit') {
13078:                     if ($domdefaults{'postsubmit'} eq 'off') {
13079:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
13080:                     } else {
13081:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
13082:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13083:                             $resulttext .= &mt('durations:').'<ul>';
13084:                             foreach my $type (@types) {
13085:                                 $resulttext .= '<li>';
13086:                                 my $timeout;
13087:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13088:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13089:                                 }
13090:                                 my $display;
13091:                                 if ($timeout eq '0') {
13092:                                     $display = &mt('unlimited');
13093:                                 } elsif ($timeout eq '') {
13094:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
13095:                                 } else {
13096:                                     $display = &mt('[quant,_1,second]',$timeout);
13097:                                 }
13098:                                 if ($type eq 'community') {
13099:                                     $resulttext .= &mt('Communities');
13100:                                 } elsif ($type eq 'official') {
13101:                                     $resulttext .= &mt('Official courses');
13102:                                 } elsif ($type eq 'unofficial') {
13103:                                     $resulttext .= &mt('Unofficial courses');
13104:                                 } elsif ($type eq 'textbook') {
13105:                                     $resulttext .= &mt('Textbook courses');
13106:                                 }
13107:                                 $resulttext .= ' -- '.$display.'</li>';
13108:                             }
13109:                             $resulttext .= '</ul>';
13110:                         }
13111:                         $resulttext .= '</li>';
13112:                     }
13113:                 } elsif ($item eq 'coursecredits') {
13114:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13115:                         if (($domdefaults{'officialcredits'} eq '') &&
13116:                             ($domdefaults{'unofficialcredits'} eq '') &&
13117:                             ($domdefaults{'textbookcredits'} eq '')) {
13118:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13119:                         } else {
13120:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
13121:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
13122:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
13123:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
13124:                                            '</ul>'.
13125:                                            '</li>';
13126:                         }
13127:                     } else {
13128:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13129:                     }
13130:                 } elsif ($item eq 'canclone') {
13131:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13132:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13133:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
13134:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
13135:                         }
13136:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
13137:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
13138:                     } else {
13139:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
13140:                     }
13141:                 }
13142:             }
13143:             $resulttext .= '</ul>';
13144:         } else {
13145:             $resulttext = &mt('No changes made to course defaults');
13146:         }
13147:     } else {
13148:         $resulttext = '<span class="LC_error">'.
13149:             &mt('An error occurred: [_1]',$putresult).'</span>';
13150:     }
13151:     return $resulttext;
13152: }
13153: 
13154: sub modify_selfenrollment {
13155:     my ($dom,$lastactref,%domconfig) = @_;
13156:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
13157:     my @types = ('official','unofficial','community','textbook');
13158:     my %titles = &tool_titles();
13159:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
13160:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
13161:     $ordered{'default'} = ['types','registered','approval','limit'];
13162: 
13163:     my (%roles,%shown,%toplevel);
13164:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
13165: 
13166:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
13167:         if ($domconfig{'selfenrollment'} eq '') {
13168:             $domconfig{'selfenrollment'} = {};
13169:         }
13170:     }
13171:     %toplevel = (
13172:                   admin      => 'Configuration Rights',
13173:                   default    => 'Default settings',
13174:                   validation => 'Validation of self-enrollment requests',
13175:                 );
13176:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
13177: 
13178:     if (ref($ordered{'admin'}) eq 'ARRAY') {
13179:         foreach my $item (@{$ordered{'admin'}}) {
13180:             foreach my $type (@types) {
13181:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
13182:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
13183:                 } else {
13184:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
13185:                 }
13186:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
13187:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
13188:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
13189:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
13190:                             push(@{$changes{'admin'}{$type}},$item);
13191:                         }
13192:                     } else {
13193:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
13194:                             push(@{$changes{'admin'}{$type}},$item);
13195:                         }
13196:                     }
13197:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
13198:                     push(@{$changes{'admin'}{$type}},$item);
13199:                 }
13200:             }
13201:         }
13202:     }
13203: 
13204:     foreach my $item (@{$ordered{'default'}}) {
13205:         foreach my $type (@types) {
13206:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
13207:             if ($item eq 'types') {
13208:                 unless (($value eq 'all') || ($value eq 'dom')) {
13209:                     $value = '';
13210:                 }
13211:             } elsif ($item eq 'registered') {
13212:                 unless ($value eq '1') {
13213:                     $value = 0;
13214:                 }
13215:             } elsif ($item eq 'approval') {
13216:                 unless ($value =~ /^[012]$/) {
13217:                     $value = 0;
13218:                 }
13219:             } else {
13220:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
13221:                     $value = 'none';
13222:                 }
13223:             }
13224:             $selfenrollhash{'default'}{$type}{$item} = $value;
13225:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
13226:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
13227:                     if ($selfenrollhash{'default'}{$type}{$item} ne
13228:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
13229:                          push(@{$changes{'default'}{$type}},$item);
13230:                     }
13231:                 } else {
13232:                     push(@{$changes{'default'}{$type}},$item);
13233:                 }
13234:             } else {
13235:                 push(@{$changes{'default'}{$type}},$item);
13236:             }
13237:             if ($item eq 'limit') {
13238:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
13239:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
13240:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
13241:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
13242:                     }
13243:                 } else {
13244:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
13245:                 }
13246:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
13247:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
13248:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
13249:                          push(@{$changes{'default'}{$type}},'cap');
13250:                     }
13251:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
13252:                     push(@{$changes{'default'}{$type}},'cap');
13253:                 }
13254:             }
13255:         }
13256:     }
13257: 
13258:     foreach my $item (@{$itemsref}) {
13259:         if ($item eq 'fields') {
13260:             my @changed;
13261:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
13262:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
13263:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
13264:             }
13265:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
13266:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
13267:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
13268:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
13269:                 } else {
13270:                     @changed = @{$selfenrollhash{'validation'}{$item}};
13271:                 }
13272:             } else {
13273:                 @changed = @{$selfenrollhash{'validation'}{$item}};
13274:             }
13275:             if (@changed) {
13276:                 if ($selfenrollhash{'validation'}{$item}) { 
13277:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
13278:                 } else {
13279:                     $changes{'validation'}{$item} = &mt('None');
13280:                 }
13281:             }
13282:         } else {
13283:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
13284:             if ($item eq 'markup') {
13285:                if ($env{'form.selfenroll_validation_'.$item}) {
13286:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
13287:                }
13288:             }
13289:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
13290:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
13291:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
13292:                 }
13293:             }
13294:         }
13295:     }
13296: 
13297:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
13298:                                              $dom);
13299:     if ($putresult eq 'ok') {
13300:         if (keys(%changes) > 0) {
13301:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13302:             $resulttext = &mt('Changes made:').'<ul>';
13303:             foreach my $key ('admin','default','validation') {
13304:                 if (ref($changes{$key}) eq 'HASH') {
13305:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
13306:                     if ($key eq 'validation') {
13307:                         foreach my $item (@{$itemsref}) {
13308:                             if (exists($changes{$key}{$item})) {
13309:                                 if ($item eq 'markup') {
13310:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
13311:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
13312:                                 } else {  
13313:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
13314:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
13315:                                 }
13316:                             }
13317:                         }
13318:                     } else {
13319:                         foreach my $type (@types) {
13320:                             if ($type eq 'community') {
13321:                                 $roles{'1'} = &mt('Community personnel');
13322:                             } else {
13323:                                 $roles{'1'} = &mt('Course personnel');
13324:                             }
13325:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
13326:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
13327:                                     if ($key eq 'admin') {
13328:                                         my @mgrdc = ();
13329:                                         if (ref($ordered{$key}) eq 'ARRAY') {
13330:                                             foreach my $item (@{$ordered{'admin'}}) {
13331:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
13332:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
13333:                                                         push(@mgrdc,$item);
13334:                                                     }
13335:                                                 }
13336:                                             }
13337:                                             if (@mgrdc) {
13338:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
13339:                                             } else {
13340:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
13341:                                             }
13342:                                         }
13343:                                     } else {
13344:                                         if (ref($ordered{$key}) eq 'ARRAY') {
13345:                                             foreach my $item (@{$ordered{$key}}) {
13346:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
13347:                                                     $domdefaults{$type.'selfenroll'.$item} =
13348:                                                         $selfenrollhash{$key}{$type}{$item};
13349:                                                 }
13350:                                             }
13351:                                         }
13352:                                     }
13353:                                 }
13354:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
13355:                                 foreach my $item (@{$ordered{$key}}) {
13356:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
13357:                                         $resulttext .= '<li>';
13358:                                         if ($key eq 'admin') {
13359:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
13360:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
13361:                                         } else {
13362:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
13363:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
13364:                                         }
13365:                                         $resulttext .= '</li>';
13366:                                     }
13367:                                 }
13368:                                 $resulttext .= '</ul></li>';
13369:                             }
13370:                         }
13371:                         $resulttext .= '</ul></li>'; 
13372:                     }
13373:                 }
13374:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
13375:                     my $cachetime = 24*60*60;
13376:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13377:                     if (ref($lastactref) eq 'HASH') {
13378:                         $lastactref->{'domdefaults'} = 1;
13379:                     }
13380:                 }
13381:             }
13382:             $resulttext .= '</ul>';
13383:         } else {
13384:             $resulttext = &mt('No changes made to self-enrollment settings');
13385:         }
13386:     } else {
13387:         $resulttext = '<span class="LC_error">'.
13388:             &mt('An error occurred: [_1]',$putresult).'</span>';
13389:     }
13390:     return $resulttext;
13391: }
13392: 
13393: sub modify_usersessions {
13394:     my ($dom,$lastactref,%domconfig) = @_;
13395:     my @hostingtypes = ('version','excludedomain','includedomain');
13396:     my @offloadtypes = ('primary','default');
13397:     my %types = (
13398:                   remote => \@hostingtypes,
13399:                   hosted => \@hostingtypes,
13400:                   spares => \@offloadtypes,
13401:                 );
13402:     my @prefixes = ('remote','hosted','spares');
13403:     my @lcversions = &Apache::lonnet::all_loncaparevs();
13404:     my (%by_ip,%by_location,@intdoms);
13405:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
13406:     my @locations = sort(keys(%by_location));
13407:     my (%defaultshash,%changes);
13408:     foreach my $prefix (@prefixes) {
13409:         $defaultshash{'usersessions'}{$prefix} = {};
13410:     }
13411:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13412:     my $resulttext;
13413:     my %iphost = &Apache::lonnet::get_iphost();
13414:     foreach my $prefix (@prefixes) {
13415:         next if ($prefix eq 'spares');
13416:         foreach my $type (@{$types{$prefix}}) {
13417:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
13418:             if ($type eq 'version') {
13419:                 my $value = $env{'form.'.$prefix.'_'.$type};
13420:                 my $okvalue;
13421:                 if ($value ne '') {
13422:                     if (grep(/^\Q$value\E$/,@lcversions)) {
13423:                         $okvalue = $value;
13424:                     }
13425:                 }
13426:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
13427:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
13428:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
13429:                             if ($inuse == 0) {
13430:                                 $changes{$prefix}{$type} = 1;
13431:                             } else {
13432:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
13433:                                     $changes{$prefix}{$type} = 1;
13434:                                 }
13435:                                 if ($okvalue ne '') {
13436:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13437:                                 } 
13438:                             }
13439:                         } else {
13440:                             if (($inuse == 1) && ($okvalue ne '')) {
13441:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13442:                                 $changes{$prefix}{$type} = 1;
13443:                             }
13444:                         }
13445:                     } else {
13446:                         if (($inuse == 1) && ($okvalue ne '')) {
13447:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13448:                             $changes{$prefix}{$type} = 1;
13449:                         }
13450:                     }
13451:                 } else {
13452:                     if (($inuse == 1) && ($okvalue ne '')) {
13453:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13454:                         $changes{$prefix}{$type} = 1;
13455:                     }
13456:                 }
13457:             } else {
13458:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
13459:                 my @okvals;
13460:                 foreach my $val (@vals) {
13461:                     if ($val =~ /:/) {
13462:                         my @items = split(/:/,$val);
13463:                         foreach my $item (@items) {
13464:                             if (ref($by_location{$item}) eq 'ARRAY') {
13465:                                 push(@okvals,$item);
13466:                             }
13467:                         }
13468:                     } else {
13469:                         if (ref($by_location{$val}) eq 'ARRAY') {
13470:                             push(@okvals,$val);
13471:                         }
13472:                     }
13473:                 }
13474:                 @okvals = sort(@okvals);
13475:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
13476:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
13477:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13478:                             if ($inuse == 0) {
13479:                                 $changes{$prefix}{$type} = 1; 
13480:                             } else {
13481:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13482:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
13483:                                 if (@changed > 0) {
13484:                                     $changes{$prefix}{$type} = 1;
13485:                                 }
13486:                             }
13487:                         } else {
13488:                             if ($inuse == 1) {
13489:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13490:                                 $changes{$prefix}{$type} = 1;
13491:                             }
13492:                         } 
13493:                     } else {
13494:                         if ($inuse == 1) {
13495:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13496:                             $changes{$prefix}{$type} = 1;
13497:                         }
13498:                     }
13499:                 } else {
13500:                     if ($inuse == 1) {
13501:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13502:                         $changes{$prefix}{$type} = 1;
13503:                     }
13504:                 }
13505:             }
13506:         }
13507:     }
13508: 
13509:     my @alldoms = &Apache::lonnet::all_domains();
13510:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13511:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
13512:     my $savespares;
13513: 
13514:     foreach my $lonhost (sort(keys(%servers))) {
13515:         my $serverhomeID =
13516:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
13517:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
13518:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
13519:         my %spareschg;
13520:         foreach my $type (@{$types{'spares'}}) {
13521:             my @okspares;
13522:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
13523:             foreach my $server (@checked) {
13524:                 if (&Apache::lonnet::hostname($server) ne '') {
13525:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
13526:                         unless (grep(/^\Q$server\E$/,@okspares)) {
13527:                             push(@okspares,$server);
13528:                         }
13529:                     }
13530:                 }
13531:             }
13532:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
13533:             my $newspare;
13534:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
13535:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
13536:                     $newspare = $new;
13537:                 }
13538:             }
13539:             my @spares;
13540:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
13541:                 @spares = sort(@okspares,$newspare);
13542:             } else {
13543:                 @spares = sort(@okspares);
13544:             }
13545:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
13546:             if (ref($spareid{$lonhost}) eq 'HASH') {
13547:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
13548:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
13549:                     if (@diffs > 0) {
13550:                         $spareschg{$type} = 1;
13551:                     }
13552:                 }
13553:             }
13554:         }
13555:         if (keys(%spareschg) > 0) {
13556:             $changes{'spares'}{$lonhost} = \%spareschg;
13557:         }
13558:     }
13559:     $defaultshash{'usersessions'}{'offloadnow'} = {};
13560:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
13561:     my @okoffload;
13562:     if (@offloadnow) {
13563:         foreach my $server (@offloadnow) {
13564:             if (&Apache::lonnet::hostname($server) ne '') {
13565:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
13566:                     push(@okoffload,$server);
13567:                 }
13568:             }
13569:         }
13570:         if (@okoffload) {
13571:             foreach my $lonhost (@okoffload) {
13572:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
13573:             }
13574:         }
13575:     }
13576:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
13577:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
13578:             if (ref($changes{'spares'}) eq 'HASH') {
13579:                 if (keys(%{$changes{'spares'}}) > 0) {
13580:                     $savespares = 1;
13581:                 }
13582:             }
13583:         } else {
13584:             $savespares = 1;
13585:         }
13586:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
13587:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
13588:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
13589:                     $changes{'offloadnow'} = 1;
13590:                     last;
13591:                 }
13592:             }
13593:             unless ($changes{'offloadnow'}) {
13594:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
13595:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
13596:                         $changes{'offloadnow'} = 1;
13597:                         last;
13598:                     }
13599:                 }
13600:             }
13601:         } elsif (@okoffload) {
13602:             $changes{'offloadnow'} = 1;
13603:         }
13604:     } elsif (@okoffload) {
13605:         $changes{'offloadnow'} = 1;
13606:     }
13607:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
13608:     if ((keys(%changes) > 0) || ($savespares)) {
13609:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13610:                                                  $dom);
13611:         if ($putresult eq 'ok') {
13612:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13613:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
13614:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
13615:                 }
13616:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
13617:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
13618:                 }
13619:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13620:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
13621:                 }
13622:             }
13623:             my $cachetime = 24*60*60;
13624:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13625:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
13626:             if (ref($lastactref) eq 'HASH') {
13627:                 $lastactref->{'domdefaults'} = 1;
13628:                 $lastactref->{'usersessions'} = 1;
13629:             }
13630:             if (keys(%changes) > 0) {
13631:                 my %lt = &usersession_titles();
13632:                 $resulttext = &mt('Changes made:').'<ul>';
13633:                 foreach my $prefix (@prefixes) {
13634:                     if (ref($changes{$prefix}) eq 'HASH') {
13635:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
13636:                         if ($prefix eq 'spares') {
13637:                             if (ref($changes{$prefix}) eq 'HASH') {
13638:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
13639:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
13640:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
13641:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
13642:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
13643:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
13644:                                         foreach my $type (@{$types{$prefix}}) {
13645:                                             if ($changes{$prefix}{$lonhost}{$type}) {
13646:                                                 my $offloadto = &mt('None');
13647:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
13648:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
13649:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
13650:                                                     }
13651:                                                 }
13652:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
13653:                                             }
13654:                                         }
13655:                                     }
13656:                                     $resulttext .= '</li>';
13657:                                 }
13658:                             }
13659:                         } else {
13660:                             foreach my $type (@{$types{$prefix}}) {
13661:                                 if (defined($changes{$prefix}{$type})) {
13662:                                     my $newvalue;
13663:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13664:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
13665:                                             if ($type eq 'version') {
13666:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
13667:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13668:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
13669:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
13670:                                                 }
13671:                                             }
13672:                                         }
13673:                                     }
13674:                                     if ($newvalue eq '') {
13675:                                         if ($type eq 'version') {
13676:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
13677:                                         } else {
13678:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
13679:                                         }
13680:                                     } else {
13681:                                         if ($type eq 'version') {
13682:                                             $newvalue .= ' '.&mt('(or later)'); 
13683:                                         }
13684:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
13685:                                     }
13686:                                 }
13687:                             }
13688:                         }
13689:                         $resulttext .= '</ul>';
13690:                     }
13691:                 }
13692:                 if ($changes{'offloadnow'}) {
13693:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13694:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
13695:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
13696:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
13697:                                 $resulttext .= '<li>'.$lonhost.'</li>';
13698:                             }
13699:                             $resulttext .= '</ul>';
13700:                         } else {
13701:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
13702:                         }
13703:                     } else {
13704:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
13705:                     }
13706:                 }
13707:                 $resulttext .= '</ul>';
13708:             } else {
13709:                 $resulttext = $nochgmsg;
13710:             }
13711:         } else {
13712:             $resulttext = '<span class="LC_error">'.
13713:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13714:         }
13715:     } else {
13716:         $resulttext = $nochgmsg;
13717:     }
13718:     return $resulttext;
13719: }
13720: 
13721: sub modify_loadbalancing {
13722:     my ($dom,%domconfig) = @_;
13723:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
13724:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
13725:     my ($othertitle,$usertypes,$types) =
13726:         &Apache::loncommon::sorted_inst_types($dom);
13727:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13728:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
13729:     my @sparestypes = ('primary','default');
13730:     my %typetitles = &sparestype_titles();
13731:     my $resulttext;
13732:     my (%currbalancer,%currtargets,%currrules,%existing);
13733:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
13734:         %existing = %{$domconfig{'loadbalancing'}};
13735:     }
13736:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
13737:                               \%currtargets,\%currrules);
13738:     my ($saveloadbalancing,%defaultshash,%changes);
13739:     my ($alltypes,$othertypes,$titles) =
13740:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
13741:     my %ruletitles = &offloadtype_text();
13742:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
13743:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
13744:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
13745:         if ($balancer eq '') {
13746:             next;
13747:         }
13748:         if (!exists($servers{$balancer})) {
13749:             if (exists($currbalancer{$balancer})) {
13750:                 push(@{$changes{'delete'}},$balancer);
13751:             }
13752:             next;
13753:         }
13754:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
13755:             push(@{$changes{'delete'}},$balancer);
13756:             next;
13757:         }
13758:         if (!exists($currbalancer{$balancer})) {
13759:             push(@{$changes{'add'}},$balancer);
13760:         }
13761:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
13762:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
13763:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
13764:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
13765:             $saveloadbalancing = 1;
13766:         }
13767:         foreach my $sparetype (@sparestypes) {
13768:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
13769:             my @offloadto;
13770:             foreach my $target (@targets) {
13771:                 if (($servers{$target}) && ($target ne $balancer)) {
13772:                     if ($sparetype eq 'default') {
13773:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
13774:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
13775:                         }
13776:                     }
13777:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
13778:                         push(@offloadto,$target);
13779:                     }
13780:                 }
13781:             }
13782:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
13783:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
13784:                     push(@offloadto,$balancer);
13785:                 }
13786:             }
13787:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
13788:         }
13789:         if (ref($currtargets{$balancer}) eq 'HASH') {
13790:             foreach my $sparetype (@sparestypes) {
13791:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
13792:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
13793:                     if (@targetdiffs > 0) {
13794:                         $changes{'curr'}{$balancer}{'targets'} = 1;
13795:                     }
13796:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
13797:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
13798:                         $changes{'curr'}{$balancer}{'targets'} = 1;
13799:                     }
13800:                 }
13801:             }
13802:         } else {
13803:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
13804:                 foreach my $sparetype (@sparestypes) {
13805:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
13806:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
13807:                             $changes{'curr'}{$balancer}{'targets'} = 1;
13808:                         }
13809:                     }
13810:                 }
13811:             }
13812:         }
13813:         my $ishomedom;
13814:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
13815:             $ishomedom = 1;
13816:         }
13817:         if (ref($alltypes) eq 'ARRAY') {
13818:             foreach my $type (@{$alltypes}) {
13819:                 my $rule;
13820:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
13821:                          (!$ishomedom)) {
13822:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
13823:                 }
13824:                 if ($rule eq 'specific') {
13825:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
13826:                     if (exists($servers{$specifiedhost})) {
13827:                         $rule = $specifiedhost;
13828:                     }
13829:                 }
13830:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
13831:                 if (ref($currrules{$balancer}) eq 'HASH') {
13832:                     if ($rule ne $currrules{$balancer}{$type}) {
13833:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
13834:                     }
13835:                 } elsif ($rule ne '') {
13836:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
13837:                 }
13838:             }
13839:         }
13840:     }
13841:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
13842:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
13843:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
13844:             $defaultshash{'loadbalancing'} = {};
13845:         }
13846:         my $putresult = &Apache::lonnet::put_dom('configuration',
13847:                                                  \%defaultshash,$dom);
13848:         if ($putresult eq 'ok') {
13849:             if (keys(%changes) > 0) {
13850:                 my %toupdate;
13851:                 if (ref($changes{'delete'}) eq 'ARRAY') {
13852:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
13853:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
13854:                         $toupdate{$balancer} = 1;
13855:                     }
13856:                 }
13857:                 if (ref($changes{'add'}) eq 'ARRAY') {
13858:                     foreach my $balancer (sort(@{$changes{'add'}})) {
13859:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
13860:                         $toupdate{$balancer} = 1;
13861:                     }
13862:                 }
13863:                 if (ref($changes{'curr'}) eq 'HASH') {
13864:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
13865:                         $toupdate{$balancer} = 1;
13866:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
13867:                             if ($changes{'curr'}{$balancer}{'targets'}) {
13868:                                 my %offloadstr;
13869:                                 foreach my $sparetype (@sparestypes) {
13870:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
13871:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
13872:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
13873:                                         }
13874:                                     }
13875:                                 }
13876:                                 if (keys(%offloadstr) == 0) {
13877:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
13878:                                 } else {
13879:                                     my $showoffload;
13880:                                     foreach my $sparetype (@sparestypes) {
13881:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
13882:                                         if (defined($offloadstr{$sparetype})) {
13883:                                             $showoffload .= $offloadstr{$sparetype};
13884:                                         } else {
13885:                                             $showoffload .= &mt('None');
13886:                                         }
13887:                                         $showoffload .= ('&nbsp;'x3);
13888:                                     }
13889:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
13890:                                 }
13891:                             }
13892:                         }
13893:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
13894:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
13895:                                 foreach my $type (@{$alltypes}) {
13896:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
13897:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
13898:                                         my $balancetext;
13899:                                         if ($rule eq '') {
13900:                                             $balancetext =  $ruletitles{'default'};
13901:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
13902:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
13903:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
13904:                                                 foreach my $sparetype (@sparestypes) {
13905:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
13906:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
13907:                                                     }
13908:                                                 }
13909:                                                 foreach my $item (@{$alltypes}) {
13910:                                                     next if ($item =~  /^_LC_ipchange/);
13911:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
13912:                                                     if ($hasrule eq 'homeserver') {
13913:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
13914:                                                     } else {
13915:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
13916:                                                             if ($servers{$hasrule}) {
13917:                                                                 $toupdate{$hasrule} = 1;
13918:                                                             }
13919:                                                         }
13920:                                                     }
13921:                                                 }
13922:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
13923:                                                     $balancetext =  $ruletitles{$rule};
13924:                                                 } else {
13925:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
13926:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
13927:                                                     if ($receiver) {
13928:                                                         $toupdate{$receiver};
13929:                                                     }
13930:                                                 }
13931:                                             } else {
13932:                                                 $balancetext =  $ruletitles{$rule};
13933:                                             }
13934:                                         } else {
13935:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
13936:                                         }
13937:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
13938:                                     }
13939:                                 }
13940:                             }
13941:                         }
13942:                         if (keys(%toupdate)) {
13943:                             my %thismachine;
13944:                             my $updatedhere;
13945:                             my $cachetime = 60*60*24;
13946:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
13947:                             foreach my $lonhost (keys(%toupdate)) {
13948:                                 if ($thismachine{$lonhost}) {
13949:                                     unless ($updatedhere) {
13950:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
13951:                                                                       $defaultshash{'loadbalancing'},
13952:                                                                       $cachetime);
13953:                                         $updatedhere = 1;
13954:                                     }
13955:                                 } else {
13956:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
13957:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
13958:                                 }
13959:                             }
13960:                         }
13961:                     }
13962:                 }
13963:                 if ($resulttext ne '') {
13964:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
13965:                 } else {
13966:                     $resulttext = $nochgmsg;
13967:                 }
13968:             } else {
13969:                 $resulttext = $nochgmsg;
13970:             }
13971:         } else {
13972:             $resulttext = '<span class="LC_error">'.
13973:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13974:         }
13975:     } else {
13976:         $resulttext = $nochgmsg;
13977:     }
13978:     return $resulttext;
13979: }
13980: 
13981: sub recurse_check {
13982:     my ($chkcats,$categories,$depth,$name) = @_;
13983:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
13984:         my $chg = 0;
13985:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
13986:             my $category = $chkcats->[$depth]{$name}[$j];
13987:             my $item;
13988:             if ($category eq '') {
13989:                 $chg ++;
13990:             } else {
13991:                 my $deeper = $depth + 1;
13992:                 $item = &escape($category).':'.&escape($name).':'.$depth;
13993:                 if ($chg) {
13994:                     $categories->{$item} -= $chg;
13995:                 }
13996:                 &recurse_check($chkcats,$categories,$deeper,$category);
13997:                 $deeper --;
13998:             }
13999:         }
14000:     }
14001:     return;
14002: }
14003: 
14004: sub recurse_cat_deletes {
14005:     my ($item,$coursecategories,$deletions) = @_;
14006:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
14007:     my $subdepth = $depth + 1;
14008:     if (ref($coursecategories) eq 'HASH') {
14009:         foreach my $subitem (keys(%{$coursecategories})) {
14010:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
14011:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
14012:                 delete($coursecategories->{$subitem});
14013:                 $deletions->{$subitem} = 1;
14014:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
14015:             }
14016:         }
14017:     }
14018:     return;
14019: }
14020: 
14021: sub active_dc_picker {
14022:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
14023:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
14024:     my @domcoord = keys(%domcoords);
14025:     if (keys(%currhash)) {
14026:         foreach my $dc (keys(%currhash)) {
14027:             unless (exists($domcoords{$dc})) {
14028:                 push(@domcoord,$dc);
14029:             }
14030:         }
14031:     }
14032:     @domcoord = sort(@domcoord);
14033:     my $numdcs = scalar(@domcoord);
14034:     my $rows = 0;
14035:     my $table;
14036:     if ($numdcs > 1) {
14037:         $table = '<table>';
14038:         for (my $i=0; $i<@domcoord; $i++) {
14039:             my $rem = $i%($numinrow);
14040:             if ($rem == 0) {
14041:                 if ($i > 0) {
14042:                     $table .= '</tr>';
14043:                 }
14044:                 $table .= '<tr>';
14045:                 $rows ++;
14046:             }
14047:             my $check = '';
14048:             if ($inputtype eq 'radio') {
14049:                 if (keys(%currhash) == 0) {
14050:                     if (!$i) {
14051:                         $check = ' checked="checked"';
14052:                     }
14053:                 } elsif (exists($currhash{$domcoord[$i]})) {
14054:                     $check = ' checked="checked"';
14055:                 }
14056:             } else {
14057:                 if (exists($currhash{$domcoord[$i]})) {
14058:                     $check = ' checked="checked"';
14059:                 }
14060:             }
14061:             if ($i == @domcoord - 1) {
14062:                 my $colsleft = $numinrow - $rem;
14063:                 if ($colsleft > 1) {
14064:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
14065:                 } else {
14066:                     $table .= '<td class="LC_left_item">';
14067:                 }
14068:             } else {
14069:                 $table .= '<td class="LC_left_item">';
14070:             }
14071:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
14072:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14073:             $table .= '<span class="LC_nobreak"><label>'.
14074:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
14075:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
14076:             if ($user ne $dcname.':'.$dcdom) {
14077:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14078:             }
14079:             $table .= '</label></span></td>';
14080:         }
14081:         $table .= '</tr></table>';
14082:     } elsif ($numdcs == 1) {
14083:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
14084:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14085:         if ($inputtype eq 'radio') {
14086:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
14087:             if ($user ne $dcname.':'.$dcdom) {
14088:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14089:             }
14090:         } else {
14091:             my $check;
14092:             if (exists($currhash{$domcoord[0]})) {
14093:                 $check = ' checked="checked"';
14094:             }
14095:             $table = '<span class="LC_nobreak"><label>'.
14096:                      '<input type="checkbox" name="'.$name.'" '.
14097:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
14098:             if ($user ne $dcname.':'.$dcdom) {
14099:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14100:             }
14101:             $table .= '</label></span>';
14102:             $rows ++;
14103:         }
14104:     }
14105:     return ($numdcs,$table,$rows);
14106: }
14107: 
14108: sub usersession_titles {
14109:     return &Apache::lonlocal::texthash(
14110:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
14111:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
14112:                spares => 'Servers offloaded to, when busy',
14113:                version => 'LON-CAPA version requirement',
14114:                excludedomain => 'Allow all, but exclude specific domains',
14115:                includedomain => 'Deny all, but include specific domains',
14116:                primary => 'Primary (checked first)',
14117:                default => 'Default',
14118:            );
14119: }
14120: 
14121: sub id_for_thisdom {
14122:     my (%servers) = @_;
14123:     my %altids;
14124:     foreach my $server (keys(%servers)) {
14125:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
14126:         if ($serverhome ne $server) {
14127:             $altids{$serverhome} = $server;
14128:         }
14129:     }
14130:     return %altids;
14131: }
14132: 
14133: sub count_servers {
14134:     my ($currbalancer,%servers) = @_;
14135:     my (@spares,$numspares);
14136:     foreach my $lonhost (sort(keys(%servers))) {
14137:         next if ($currbalancer eq $lonhost);
14138:         push(@spares,$lonhost);
14139:     }
14140:     if ($currbalancer) {
14141:         $numspares = scalar(@spares);
14142:     } else {
14143:         $numspares = scalar(@spares) - 1;
14144:     }
14145:     return ($numspares,@spares);
14146: }
14147: 
14148: sub lonbalance_targets_js {
14149:     my ($dom,$types,$servers,$settings) = @_;
14150:     my $select = &mt('Select');
14151:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
14152:     if (ref($servers) eq 'HASH') {
14153:         $alltargets = join("','",sort(keys(%{$servers})));
14154:         my @homedoms;
14155:         foreach my $server (sort(keys(%{$servers}))) {
14156:             if (&Apache::lonnet::host_domain($server) eq $dom) {
14157:                 push(@homedoms,'1');
14158:             } else {
14159:                 push(@homedoms,'0');
14160:             }
14161:         }
14162:         $allishome = join("','",@homedoms);
14163:     }
14164:     if (ref($types) eq 'ARRAY') {
14165:         if (@{$types} > 0) {
14166:             @alltypes = @{$types};
14167:         }
14168:     }
14169:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
14170:     $allinsttypes = join("','",@alltypes);
14171:     my (%currbalancer,%currtargets,%currrules,%existing);
14172:     if (ref($settings) eq 'HASH') {
14173:         %existing = %{$settings};
14174:     }
14175:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
14176:                               \%currtargets,\%currrules);
14177:     my $balancers = join("','",sort(keys(%currbalancer)));
14178:     return <<"END";
14179: 
14180: <script type="text/javascript">
14181: // <![CDATA[
14182: 
14183: currBalancers = new Array('$balancers');
14184: 
14185: function toggleTargets(balnum) {
14186:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
14187:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
14188:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
14189:     var prevbalancer = prevhostitem.value;
14190:     var baltotal = document.getElementById('loadbalancing_total').value;
14191:     prevhostitem.value = balancer;
14192:     if (prevbalancer != '') {
14193:         var prevIdx = currBalancers.indexOf(prevbalancer);
14194:         if (prevIdx != -1) {
14195:             currBalancers.splice(prevIdx,1);
14196:         }
14197:     }
14198:     if (balancer == '') {
14199:         hideSpares(balnum);
14200:     } else {
14201:         var currIdx = currBalancers.indexOf(balancer);
14202:         if (currIdx == -1) {
14203:             currBalancers.push(balancer);
14204:         }
14205:         var homedoms = new Array('$allishome');
14206:         var ishomedom = homedoms[lonhostitem.selectedIndex];
14207:         showSpares(balancer,ishomedom,balnum);
14208:     }
14209:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
14210:     return;
14211: }
14212: 
14213: function showSpares(balancer,ishomedom,balnum) {
14214:     var alltargets = new Array('$alltargets');
14215:     var insttypes = new Array('$allinsttypes');
14216:     var offloadtypes = new Array('primary','default');
14217: 
14218:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
14219:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
14220:  
14221:     for (var i=0; i<offloadtypes.length; i++) {
14222:         var count = 0;
14223:         for (var j=0; j<alltargets.length; j++) {
14224:             if (alltargets[j] != balancer) {
14225:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
14226:                 item.value = alltargets[j];
14227:                 item.style.textAlign='left';
14228:                 item.style.textFace='normal';
14229:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
14230:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
14231:                     item.disabled = '';
14232:                 } else {
14233:                     item.disabled = 'disabled';
14234:                     item.checked = false;
14235:                 }
14236:                 count ++;
14237:             }
14238:         }
14239:     }
14240:     for (var k=0; k<insttypes.length; k++) {
14241:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
14242:             if (ishomedom == 1) {
14243:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
14244:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
14245:             } else {
14246:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
14247:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
14248:             }
14249:         } else {
14250:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
14251:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
14252:         }
14253:         if ((insttypes[k] != '_LC_external') && 
14254:             ((insttypes[k] != '_LC_internetdom') ||
14255:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
14256:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
14257:             item.options.length = 0;
14258:             item.options[0] = new Option("","",true,true);
14259:             var idx = 0;
14260:             for (var m=0; m<alltargets.length; m++) {
14261:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
14262:                     idx ++;
14263:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
14264:                 }
14265:             }
14266:         }
14267:     }
14268:     return;
14269: }
14270: 
14271: function hideSpares(balnum) {
14272:     var alltargets = new Array('$alltargets');
14273:     var insttypes = new Array('$allinsttypes');
14274:     var offloadtypes = new Array('primary','default');
14275: 
14276:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
14277:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
14278: 
14279:     var total = alltargets.length - 1;
14280:     for (var i=0; i<offloadtypes; i++) {
14281:         for (var j=0; j<total; j++) {
14282:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
14283:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
14284:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
14285:         }
14286:     }
14287:     for (var k=0; k<insttypes.length; k++) {
14288:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
14289:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
14290:         if (insttypes[k] != '_LC_external') {
14291:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
14292:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
14293:         }
14294:     }
14295:     return;
14296: }
14297: 
14298: function checkOffloads(item,balnum,type) {
14299:     var alltargets = new Array('$alltargets');
14300:     var offloadtypes = new Array('primary','default');
14301:     if (item.checked) {
14302:         var total = alltargets.length - 1;
14303:         var other;
14304:         if (type == offloadtypes[0]) {
14305:             other = offloadtypes[1];
14306:         } else {
14307:             other = offloadtypes[0];
14308:         }
14309:         for (var i=0; i<total; i++) {
14310:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
14311:             if (server == item.value) {
14312:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
14313:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
14314:                 }
14315:             }
14316:         }
14317:     }
14318:     return;
14319: }
14320: 
14321: function singleServerToggle(balnum,type) {
14322:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
14323:     if (offloadtoSelIdx == 0) {
14324:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
14325:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
14326: 
14327:     } else {
14328:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
14329:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
14330:     }
14331:     return;
14332: }
14333: 
14334: function balanceruleChange(formname,balnum,type) {
14335:     if (type == '_LC_external') {
14336:         return;
14337:     }
14338:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
14339:     for (var i=0; i<typesRules.length; i++) {
14340:         if (formname.elements[typesRules[i]].checked) {
14341:             if (formname.elements[typesRules[i]].value != 'specific') {
14342:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
14343:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
14344:             } else {
14345:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
14346:             }
14347:         }
14348:     }
14349:     return;
14350: }
14351: 
14352: function balancerDeleteChange(balnum) {
14353:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
14354:     var baltotal = document.getElementById('loadbalancing_total').value;
14355:     var addtarget;
14356:     var removetarget;
14357:     var action = 'delete';
14358:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
14359:         var lonhost = hostitem.value;
14360:         var currIdx = currBalancers.indexOf(lonhost);
14361:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
14362:             if (currIdx != -1) {
14363:                 currBalancers.splice(currIdx,1);
14364:             }
14365:             addtarget = lonhost;
14366:         } else {
14367:             if (currIdx == -1) {
14368:                 currBalancers.push(lonhost);
14369:             }
14370:             removetarget = lonhost;
14371:             action = 'undelete';
14372:         }
14373:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
14374:     }
14375:     return;
14376: }
14377: 
14378: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
14379:     if (baltotal > 1) {
14380:         var offloadtypes = new Array('primary','default');
14381:         var alltargets = new Array('$alltargets');
14382:         var insttypes = new Array('$allinsttypes');
14383:         for (var i=0; i<baltotal; i++) {
14384:             if (i != balnum) {
14385:                 for (var j=0; j<offloadtypes.length; j++) {
14386:                     var total = alltargets.length - 1;
14387:                     for (var k=0; k<total; k++) {
14388:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
14389:                         var server = serveritem.value;
14390:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
14391:                             if (server == addtarget) {
14392:                                 serveritem.disabled = '';
14393:                             }
14394:                         }
14395:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
14396:                             if (server == removetarget) {
14397:                                 serveritem.disabled = 'disabled';
14398:                                 serveritem.checked = false;
14399:                             }
14400:                         }
14401:                     }
14402:                 }
14403:                 for (var j=0; j<insttypes.length; j++) {
14404:                     if (insttypes[j] != '_LC_external') {
14405:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
14406:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
14407:                             var currSel = singleserver.selectedIndex;
14408:                             var currVal = singleserver.options[currSel].value;
14409:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
14410:                                 var numoptions = singleserver.options.length;
14411:                                 var needsnew = 1;
14412:                                 for (var k=0; k<numoptions; k++) {
14413:                                     if (singleserver.options[k] == addtarget) {
14414:                                         needsnew = 0;
14415:                                         break;
14416:                                     }
14417:                                 }
14418:                                 if (needsnew == 1) {
14419:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
14420:                                 }
14421:                             }
14422:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
14423:                                 singleserver.options.length = 0;
14424:                                 if ((currVal) && (currVal != removetarget)) {
14425:                                     singleserver.options[0] = new Option("","",false,false);
14426:                                 } else {
14427:                                     singleserver.options[0] = new Option("","",true,true);
14428:                                 }
14429:                                 var idx = 0;
14430:                                 for (var m=0; m<alltargets.length; m++) {
14431:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
14432:                                         idx ++;
14433:                                         if (currVal == alltargets[m]) {
14434:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
14435:                                         } else {
14436:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
14437:                                         }
14438:                                     }
14439:                                 }
14440:                             }
14441:                         }
14442:                     }
14443:                 }
14444:             }
14445:         }
14446:     }
14447:     return;
14448: }
14449: 
14450: // ]]>
14451: </script>
14452: 
14453: END
14454: }
14455: 
14456: sub new_spares_js {
14457:     my @sparestypes = ('primary','default');
14458:     my $types = join("','",@sparestypes);
14459:     my $select = &mt('Select');
14460:     return <<"END";
14461: 
14462: <script type="text/javascript">
14463: // <![CDATA[
14464: 
14465: function updateNewSpares(formname,lonhost) {
14466:     var types = new Array('$types');
14467:     var include = new Array();
14468:     var exclude = new Array();
14469:     for (var i=0; i<types.length; i++) {
14470:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
14471:         for (var j=0; j<spareboxes.length; j++) {
14472:             if (formname.elements[spareboxes[j]].checked) {
14473:                 exclude.push(formname.elements[spareboxes[j]].value);
14474:             } else {
14475:                 include.push(formname.elements[spareboxes[j]].value);
14476:             }
14477:         }
14478:     }
14479:     for (var i=0; i<types.length; i++) {
14480:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
14481:         var selIdx = newSpare.selectedIndex;
14482:         var currnew = newSpare.options[selIdx].value;
14483:         var okSpares = new Array();
14484:         for (var j=0; j<newSpare.options.length; j++) {
14485:             var possible = newSpare.options[j].value;
14486:             if (possible != '') {
14487:                 if (exclude.indexOf(possible) == -1) {
14488:                     okSpares.push(possible);
14489:                 } else {
14490:                     if (currnew == possible) {
14491:                         selIdx = 0;
14492:                     }
14493:                 }
14494:             }
14495:         }
14496:         for (var k=0; k<include.length; k++) {
14497:             if (okSpares.indexOf(include[k]) == -1) {
14498:                 okSpares.push(include[k]);
14499:             }
14500:         }
14501:         okSpares.sort();
14502:         newSpare.options.length = 0;
14503:         if (selIdx == 0) {
14504:             newSpare.options[0] = new Option("$select","",true,true);
14505:         } else {
14506:             newSpare.options[0] = new Option("$select","",false,false);
14507:         }
14508:         for (var m=0; m<okSpares.length; m++) {
14509:             var idx = m+1;
14510:             var selThis = 0;
14511:             if (selIdx != 0) {
14512:                 if (okSpares[m] == currnew) {
14513:                     selThis = 1;
14514:                 }
14515:             }
14516:             if (selThis == 1) {
14517:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
14518:             } else {
14519:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
14520:             }
14521:         }
14522:     }
14523:     return;
14524: }
14525: 
14526: function checkNewSpares(lonhost,type) {
14527:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
14528:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
14529:     if (chosen != '') { 
14530:         var othertype;
14531:         var othernewSpare;
14532:         if (type == 'primary') {
14533:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
14534:         }
14535:         if (type == 'default') {
14536:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
14537:         }
14538:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
14539:             othernewSpare.selectedIndex = 0;
14540:         }
14541:     }
14542:     return;
14543: }
14544: 
14545: // ]]>
14546: </script>
14547: 
14548: END
14549: 
14550: }
14551: 
14552: sub common_domprefs_js {
14553:     return <<"END";
14554: 
14555: <script type="text/javascript">
14556: // <![CDATA[
14557: 
14558: function getIndicesByName(formname,item) {
14559:     var group = new Array();
14560:     for (var i=0;i<formname.elements.length;i++) {
14561:         if (formname.elements[i].name == item) {
14562:             group.push(formname.elements[i].id);
14563:         }
14564:     }
14565:     return group;
14566: }
14567: 
14568: // ]]>
14569: </script>
14570: 
14571: END
14572: 
14573: }
14574: 
14575: sub recaptcha_js {
14576:     my %lt = &captcha_phrases();
14577:     return <<"END";
14578: 
14579: <script type="text/javascript">
14580: // <![CDATA[
14581: 
14582: function updateCaptcha(caller,context) {
14583:     var privitem;
14584:     var pubitem;
14585:     var privtext;
14586:     var pubtext;
14587:     var versionitem;
14588:     var versiontext;
14589:     if (document.getElementById(context+'_recaptchapub')) {
14590:         pubitem = document.getElementById(context+'_recaptchapub');
14591:     } else {
14592:         return;
14593:     }
14594:     if (document.getElementById(context+'_recaptchapriv')) {
14595:         privitem = document.getElementById(context+'_recaptchapriv');
14596:     } else {
14597:         return;
14598:     }
14599:     if (document.getElementById(context+'_recaptchapubtxt')) {
14600:         pubtext = document.getElementById(context+'_recaptchapubtxt');
14601:     } else {
14602:         return;
14603:     }
14604:     if (document.getElementById(context+'_recaptchaprivtxt')) {
14605:         privtext = document.getElementById(context+'_recaptchaprivtxt');
14606:     } else {
14607:         return;
14608:     }
14609:     if (document.getElementById(context+'_recaptchaversion')) {
14610:         versionitem = document.getElementById(context+'_recaptchaversion');
14611:     } else {
14612:         return;
14613:     }
14614:     if (document.getElementById(context+'_recaptchavertxt')) {
14615:         versiontext = document.getElementById(context+'_recaptchavertxt');
14616:     } else {
14617:         return;
14618:     }
14619:     if (caller.checked) {
14620:         if (caller.value == 'recaptcha') {
14621:             pubitem.type = 'text';
14622:             privitem.type = 'text';
14623:             pubitem.size = '40';
14624:             privitem.size = '40';
14625:             pubtext.innerHTML = "$lt{'pub'}";
14626:             privtext.innerHTML = "$lt{'priv'}";
14627:             versionitem.type = 'text';
14628:             versionitem.size = '3';
14629:             versiontext.innerHTML = "$lt{'ver'}";
14630:         } else {
14631:             pubitem.type = 'hidden';
14632:             privitem.type = 'hidden';
14633:             versionitem.type = 'hidden';
14634:             pubtext.innerHTML = '';
14635:             privtext.innerHTML = '';
14636:             versiontext.innerHTML = '';
14637:         }
14638:     }
14639:     return;
14640: }
14641: 
14642: // ]]>
14643: </script>
14644: 
14645: END
14646: 
14647: }
14648: 
14649: sub toggle_display_js {
14650:     return <<"END";
14651: 
14652: <script type="text/javascript">
14653: // <![CDATA[
14654: 
14655: function toggleDisplay(domForm,caller) {
14656:     if (document.getElementById(caller)) {
14657:         var divitem = document.getElementById(caller);
14658:         var optionsElement = domForm.coursecredits;
14659:         var checkval = 1;
14660:         var dispval = 'block';
14661:         if (caller == 'emailoptions') {
14662:             optionsElement = domForm.cancreate_email; 
14663:         }
14664:         if (caller == 'studentsubmission') {
14665:             optionsElement = domForm.postsubmit;
14666:         }
14667:         if (caller == 'cloneinstcode') {
14668:             optionsElement = domForm.canclone;
14669:             checkval = 'instcode';
14670:         }
14671:         if (optionsElement.length) {
14672:             var currval;
14673:             for (var i=0; i<optionsElement.length; i++) {
14674:                 if (optionsElement[i].checked) {
14675:                    currval = optionsElement[i].value;
14676:                 }
14677:             }
14678:             if (currval == checkval) {
14679:                 divitem.style.display = dispval;
14680:             } else {
14681:                 divitem.style.display = 'none';
14682:             }
14683:         }
14684:     }
14685:     return;
14686: }
14687: 
14688: // ]]>
14689: </script>
14690: 
14691: END
14692: 
14693: }
14694: 
14695: sub captcha_phrases {
14696:     return &Apache::lonlocal::texthash (
14697:                  priv => 'Private key',
14698:                  pub  => 'Public key',
14699:                  original  => 'original (CAPTCHA)',
14700:                  recaptcha => 'successor (ReCAPTCHA)',
14701:                  notused   => 'unused',
14702:                  ver => 'ReCAPTCHA version (1 or 2)',
14703:     );
14704: }
14705: 
14706: sub devalidate_remote_domconfs {
14707:     my ($dom,$cachekeys) = @_;
14708:     return unless (ref($cachekeys) eq 'HASH');
14709:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14710:     my %thismachine;
14711:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
14712:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions','directorysrch');
14713:     if (keys(%servers)) {
14714:         foreach my $server (keys(%servers)) {
14715:             next if ($thismachine{$server});
14716:             my @cached;
14717:             foreach my $name (@posscached) {
14718:                 if ($cachekeys->{$name}) {
14719:                     push(@cached,&escape($name).':'.&escape($dom));
14720:                 }
14721:             }
14722:             if (@cached) {
14723:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
14724:             }
14725:         }
14726:     }
14727:     return;
14728: }
14729: 
14730: 1;

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