File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.71: download - view: text, annotated - select for diffs
Sun Sep 18 19:22:48 2016 UTC (7 years, 8 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11
  - Typo in 1.160.6.70 backport of 1.276

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.71 2016/09/18 19:22:48 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: #
   28: ###############################################################
   29: ##############################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::domainprefs.pm
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Handles configuration of a LON-CAPA domain.  
   40: 
   41: This is part of the LearningOnline Network with CAPA project
   42: described at http://www.lon-capa.org.
   43: 
   44: 
   45: =head1 OVERVIEW
   46: 
   47: Each institution using LON-CAPA will typically have a single domain designated 
   48: for use by individuals affiliated with the institution.  Accordingly, each domain
   49: may define a default set of logos and a color scheme which can be used to "brand"
   50: the LON-CAPA instance. In addition, an institution will typically have a language
   51: and timezone which are used for the majority of courses.
   52: 
   53: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   54: host of other domain-wide settings which determine the types of functionality
   55: available to users and courses in the domain.
   56: 
   57: There is also a mechanism to configure cataloging of courses in the domain, and
   58: controls on the operation of automated processes which govern such things as
   59: roster updates, user directory updates and processing of course requests.
   60: 
   61: The domain coordination manual which is built dynamically on install/update of 
   62: LON-CAPA from the relevant help items provides more information about domain 
   63: configuration.
   64: 
   65: Most of the domain settings are stored in the configuration.db GDBM file which is
   66: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   67: where $dom is the domain.  The configuration.db stores settings in a number of 
   68: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   69: the domain as files (e.g., image files for logos etc., or plain text files for
   70: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   71: session hosted on the primary library server in the domain, as these files are 
   72: stored in author space belonging to a special $dom-domainconfig user.   
   73: 
   74: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   75: the current settings, and provides an interface to make modifications.
   76: 
   77: =head1 SUBROUTINES
   78: 
   79: =over
   80: 
   81: =item print_quotas()
   82: 
   83: Inputs: 4 
   84: 
   85: $dom,$settings,$rowtotal,$action.
   86: 
   87: $dom is the domain, $settings is a reference to a hash of current settings for
   88: the current context, $rowtotal is a reference to the scalar used to record the 
   89: number of rows displayed on the page, and $action is the context (quotas, 
   90: requestcourses or requestauthor).
   91: 
   92: The print_quotas routine was orginally created to display/store information
   93: about default quota sizes for portfolio spaces for the different types of 
   94: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   95: but is now also used to manage availability of user tools: 
   96: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   97: used by course owners to request creation of a course, and to display/store
   98: default quota sizes for Authoring Spaces.
   99: 
  100: Outputs: 1
  101: 
  102: $datatable  - HTML containing form elements which allow settings to be changed. 
  103: 
  104: In the case of course requests, radio buttons are displayed for each institutional
  105: affiliate type (and also default, and _LC_adv) for each of the course types 
  106: (official, unofficial, community, and textbook).  In each case the radio buttons 
  107: allow the selection of one of four values:
  108: 
  109: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  110: which have the following effects:
  111: 
  112: 0
  113: 
  114: =over
  115: 
  116: - course requests are not allowed for this course types/affiliation
  117: 
  118: =back
  119: 
  120: approval 
  121: 
  122: =over 
  123: 
  124: - course requests must be approved by a Doman Coordinator in the 
  125: course's domain
  126: 
  127: =back
  128: 
  129: validate 
  130: 
  131: =over
  132: 
  133: - an institutional validation (e.g., check requestor is instructor
  134: of record) needs to be passed before the course will be created.  The required
  135: validation is in localenroll.pm on the primary library server for the course 
  136: domain.
  137: 
  138: =back
  139: 
  140: autolimit 
  141: 
  142: =over
  143:  
  144: - course requests will be processed automatically up to a limit of
  145: N requests for the course type for the particular requestor.
  146: If N is undefined, there is no limit to the number of course requests
  147: which a course owner may submit and have processed automatically. 
  148: 
  149: =back
  150: 
  151: =item modify_quotas() 
  152: 
  153: =back
  154: 
  155: =cut
  156: 
  157: package Apache::domainprefs;
  158: 
  159: use strict;
  160: use Apache::Constants qw(:common :http);
  161: use Apache::lonnet;
  162: use Apache::loncommon();
  163: use Apache::lonhtmlcommon();
  164: use Apache::lonlocal;
  165: use Apache::lonmsg();
  166: use Apache::lonconfigsettings;
  167: use Apache::lonuserutils();
  168: use Apache::loncoursequeueadmin();
  169: use LONCAPA qw(:DEFAULT :match);
  170: use LONCAPA::Enrollment;
  171: use LONCAPA::lonauthcgi();
  172: use File::Copy;
  173: use Locale::Language;
  174: use DateTime::TimeZone;
  175: use DateTime::Locale;
  176: 
  177: my $registered_cleanup;
  178: my $modified_urls;
  179: 
  180: sub handler {
  181:     my $r=shift;
  182:     if ($r->header_only) {
  183:         &Apache::loncommon::content_type($r,'text/html');
  184:         $r->send_http_header;
  185:         return OK;
  186:     }
  187: 
  188:     my $context = 'domain';
  189:     my $dom = $env{'request.role.domain'};
  190:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  191:     if (&Apache::lonnet::allowed('mau',$dom)) {
  192:         &Apache::loncommon::content_type($r,'text/html');
  193:         $r->send_http_header;
  194:     } else {
  195:         $env{'user.error.msg'}=
  196:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  197:         return HTTP_NOT_ACCEPTABLE;
  198:     }
  199: 
  200:     $registered_cleanup=0;
  201:     @{$modified_urls}=();
  202: 
  203:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  204:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  205:                                             ['phase','actions']);
  206:     my $phase = 'pickactions';
  207:     if ( exists($env{'form.phase'}) ) {
  208:         $phase = $env{'form.phase'};
  209:     }
  210:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  211:     my %domconfig =
  212:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  213:                 'quotas','autoenroll','autoupdate','autocreate',
  214:                 'directorysrch','usercreation','usermodification',
  215:                 'contacts','defaults','scantron','coursecategories',
  216:                 'serverstatuses','requestcourses','coursedefaults',
  217:                 'usersessions','loadbalancing','requestauthor',
  218:                 'selfenrollment','inststatus'],$dom);
  219:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  220:                        'autoupdate','autocreate','directorysrch','contacts',
  221:                        'usercreation','selfcreation','usermodification','scantron',
  222:                        'requestcourses','requestauthor','coursecategories',
  223:                        'serverstatuses','coursedefaults','selfenrollment',
  224:                        'usersessions');
  225:     my %existing;
  226:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  227:         %existing = %{$domconfig{'loadbalancing'}};
  228:     }
  229:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  230:         push(@prefs_order,'loadbalancing');
  231:     }
  232:     my %prefs = (
  233:         'rolecolors' =>
  234:                    { text => 'Default color schemes',
  235:                      help => 'Domain_Configuration_Color_Schemes',
  236:                      header => [{col1 => 'Student Settings',
  237:                                  col2 => '',},
  238:                                 {col1 => 'Coordinator Settings',
  239:                                  col2 => '',},
  240:                                 {col1 => 'Author Settings',
  241:                                  col2 => '',},
  242:                                 {col1 => 'Administrator Settings',
  243:                                  col2 => '',}],
  244:                       print => \&print_rolecolors,
  245:                       modify => \&modify_rolecolors,
  246:                     },
  247:         'login' =>
  248:                     { text => 'Log-in page options',
  249:                       help => 'Domain_Configuration_Login_Page',
  250:                       header => [{col1 => 'Log-in Page Items',
  251:                                   col2 => '',},
  252:                                  {col1 => 'Log-in Help',
  253:                                   col2 => 'Value'},
  254:                                  {col1 => 'Custom HTML in document head',
  255:                                   col2 => 'Value'}],
  256:                       print => \&print_login,
  257:                       modify => \&modify_login,
  258:                     },
  259:         'defaults' => 
  260:                     { text => 'Default authentication/language/timezone/portal/types',
  261:                       help => 'Domain_Configuration_LangTZAuth',
  262:                       header => [{col1 => 'Setting',
  263:                                   col2 => 'Value'},
  264:                                  {col1 => 'Institutional user types',
  265:                                   col2 => 'Assignable to e-mail usernames'}],
  266:                       print => \&print_defaults,
  267:                       modify => \&modify_defaults,
  268:                     },
  269:         'quotas' => 
  270:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  271:                       help => 'Domain_Configuration_Quotas',
  272:                       header => [{col1 => 'User affiliation',
  273:                                   col2 => 'Available tools',
  274:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  275:                       print => \&print_quotas,
  276:                       modify => \&modify_quotas,
  277:                     },
  278:         'autoenroll' =>
  279:                    { text => 'Auto-enrollment settings',
  280:                      help => 'Domain_Configuration_Auto_Enrollment',
  281:                      header => [{col1 => 'Configuration setting',
  282:                                  col2 => 'Value(s)'}],
  283:                      print => \&print_autoenroll,
  284:                      modify => \&modify_autoenroll,
  285:                    },
  286:         'autoupdate' => 
  287:                    { text => 'Auto-update settings',
  288:                      help => 'Domain_Configuration_Auto_Updates',
  289:                      header => [{col1 => 'Setting',
  290:                                  col2 => 'Value',},
  291:                                 {col1 => 'Setting',
  292:                                  col2 => 'Affiliation'},
  293:                                 {col1 => 'User population',
  294:                                  col2 => 'Updatable user data'}],
  295:                      print => \&print_autoupdate,
  296:                      modify => \&modify_autoupdate,
  297:                   },
  298:         'autocreate' => 
  299:                   { text => 'Auto-course creation settings',
  300:                      help => 'Domain_Configuration_Auto_Creation',
  301:                      header => [{col1 => 'Configuration Setting',
  302:                                  col2 => 'Value',}],
  303:                      print => \&print_autocreate,
  304:                      modify => \&modify_autocreate,
  305:                   },
  306:         'directorysrch' => 
  307:                   { text => 'Institutional directory searches',
  308:                     help => 'Domain_Configuration_InstDirectory_Search',
  309:                     header => [{col1 => 'Setting',
  310:                                 col2 => 'Value',}],
  311:                     print => \&print_directorysrch,
  312:                     modify => \&modify_directorysrch,
  313:                   },
  314:         'contacts' =>
  315:                   { text => 'Contact Information',
  316:                     help => 'Domain_Configuration_Contact_Info',
  317:                     header => [{col1 => 'Setting',
  318:                                 col2 => 'Value',}],
  319:                     print => \&print_contacts,
  320:                     modify => \&modify_contacts,
  321:                   },
  322:         'usercreation' => 
  323:                   { text => 'User creation',
  324:                     help => 'Domain_Configuration_User_Creation',
  325:                     header => [{col1 => 'Format rule type',
  326:                                 col2 => 'Format rules in force'},
  327:                                {col1 => 'User account creation',
  328:                                 col2 => 'Usernames which may be created',},
  329:                                {col1 => 'Context',
  330:                                 col2 => 'Assignable authentication types'}],
  331:                     print => \&print_usercreation,
  332:                     modify => \&modify_usercreation,
  333:                   },
  334:         'selfcreation' => 
  335:                   { text => 'Users self-creating accounts',
  336:                     help => 'Domain_Configuration_Self_Creation', 
  337:                     header => [{col1 => 'Self-creation with institutional username',
  338:                                 col2 => 'Enabled?'},
  339:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  340:                                 col2 => 'Information user can enter'},
  341:                                {col1 => 'Self-creation with e-mail as username',
  342:                                 col2 => 'Settings'}],
  343:                     print => \&print_selfcreation,
  344:                     modify => \&modify_selfcreation,
  345:                   },
  346:         'usermodification' =>
  347:                   { text => 'User modification',
  348:                     help => 'Domain_Configuration_User_Modification',
  349:                     header => [{col1 => 'Target user has role',
  350:                                 col2 => 'User information updatable in author context'},
  351:                                {col1 => 'Target user has role',
  352:                                 col2 => 'User information updatable in course context'}],
  353:                     print => \&print_usermodification,
  354:                     modify => \&modify_usermodification,
  355:                   },
  356:         'scantron' =>
  357:                   { text => 'Bubblesheet format file',
  358:                     help => 'Domain_Configuration_Scantron_Format',
  359:                     header => [ {col1 => 'Item',
  360:                                  col2 => '',
  361:                               }],
  362:                     print => \&print_scantron,
  363:                     modify => \&modify_scantron,
  364:                   },
  365:         'requestcourses' => 
  366:                  {text => 'Request creation of courses',
  367:                   help => 'Domain_Configuration_Request_Courses',
  368:                   header => [{col1 => 'User affiliation',
  369:                               col2 => 'Availability/Processing of requests',},
  370:                              {col1 => 'Setting',
  371:                               col2 => 'Value'},
  372:                              {col1 => 'Available textbooks',
  373:                               col2 => ''},
  374:                              {col1 => 'Available templates',
  375:                               col2 => ''},
  376:                              {col1 => 'Validation (not official courses)',
  377:                               col2 => 'Value'},],
  378:                   print => \&print_quotas,
  379:                   modify => \&modify_quotas,
  380:                  },
  381:         'requestauthor' =>
  382:                  {text => 'Request Authoring Space',
  383:                   help => 'Domain_Configuration_Request_Author',
  384:                   header => [{col1 => 'User affiliation',
  385:                               col2 => 'Availability/Processing of requests',},
  386:                              {col1 => 'Setting',
  387:                               col2 => 'Value'}],
  388:                   print => \&print_quotas,
  389:                   modify => \&modify_quotas,
  390:                  },
  391:         'coursecategories' =>
  392:                   { text => 'Cataloging of courses/communities',
  393:                     help => 'Domain_Configuration_Cataloging_Courses',
  394:                     header => [{col1 => 'Catalog type/availability',
  395:                                 col2 => '',},
  396:                                {col1 => 'Category settings for standard catalog',
  397:                                 col2 => '',},
  398:                                {col1 => 'Categories',
  399:                                 col2 => '',
  400:                                }],
  401:                     print => \&print_coursecategories,
  402:                     modify => \&modify_coursecategories,
  403:                   },
  404:         'serverstatuses' =>
  405:                  {text   => 'Access to server status pages',
  406:                   help   => 'Domain_Configuration_Server_Status',
  407:                   header => [{col1 => 'Status Page',
  408:                               col2 => 'Other named users',
  409:                               col3 => 'Specific IPs',
  410:                             }],
  411:                   print => \&print_serverstatuses,
  412:                   modify => \&modify_serverstatuses,
  413:                  },
  414:         'coursedefaults' => 
  415:                  {text => 'Course/Community defaults',
  416:                   help => 'Domain_Configuration_Course_Defaults',
  417:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  418:                               col2 => 'Value',},
  419:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  420:                               col2 => 'Value',},],
  421:                   print => \&print_coursedefaults,
  422:                   modify => \&modify_coursedefaults,
  423:                  },
  424:         'selfenrollment' => 
  425:                  {text   => 'Self-enrollment in Course/Community',
  426:                   help   => 'Domain_Configuration_Selfenrollment',
  427:                   header => [{col1 => 'Configuration Rights',
  428:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  429:                              {col1 => 'Defaults',
  430:                               col2 => 'Value'},
  431:                              {col1 => 'Self-enrollment validation (optional)',
  432:                               col2 => 'Value'},],
  433:                   print => \&print_selfenrollment,
  434:                   modify => \&modify_selfenrollment,
  435:                  },
  436:         'usersessions' =>
  437:                  {text  => 'User session hosting/offloading',
  438:                   help  => 'Domain_Configuration_User_Sessions',
  439:                   header => [{col1 => 'Domain server',
  440:                               col2 => 'Servers to offload sessions to when busy'},
  441:                              {col1 => 'Hosting of users from other domains',
  442:                               col2 => 'Rules'},
  443:                              {col1 => "Hosting domain's own users elsewhere",
  444:                               col2 => 'Rules'}],
  445:                   print => \&print_usersessions,
  446:                   modify => \&modify_usersessions,
  447:                  },
  448:          'loadbalancing' =>
  449:                  {text  => 'Dedicated Load Balancer(s)',
  450:                   help  => 'Domain_Configuration_Load_Balancing',
  451:                   header => [{col1 => 'Balancers',
  452:                               col2 => 'Default destinations',
  453:                               col3 => 'User affiliation',
  454:                               col4 => 'Overrides'},
  455:                             ],
  456:                   print => \&print_loadbalancing,
  457:                   modify => \&modify_loadbalancing,
  458:                  },
  459:     );
  460:     if (keys(%servers) > 1) {
  461:         $prefs{'login'}  = { text   => 'Log-in page options',
  462:                              help   => 'Domain_Configuration_Login_Page',
  463:                             header => [{col1 => 'Log-in Service',
  464:                                         col2 => 'Server Setting',},
  465:                                        {col1 => 'Log-in Page Items',
  466:                                         col2 => ''},
  467:                                        {col1 => 'Log-in Help',
  468:                                         col2 => 'Value'},
  469:                                        {col1 => 'Custom HTML in document head',
  470:                                         col2 => 'Value'}],
  471:                             print => \&print_login,
  472:                             modify => \&modify_login,
  473:                            };
  474:     }
  475: 
  476:     my @roles = ('student','coordinator','author','admin');
  477:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  478:     &Apache::lonhtmlcommon::add_breadcrumb
  479:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  480:       text=>"Settings to display/modify"});
  481:     my $confname = $dom.'-domainconfig';
  482: 
  483:     if ($phase eq 'process') {
  484:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  485:                                                               \%prefs,\%domconfig,$confname,\@roles);
  486:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  487:             $r->rflush();
  488:             &devalidate_remote_domconfs($dom,$result);
  489:         }
  490:     } elsif ($phase eq 'display') {
  491:         my $js = &recaptcha_js().
  492:                  &toggle_display_js();
  493:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  494:             my ($othertitle,$usertypes,$types) =
  495:                 &Apache::loncommon::sorted_inst_types($dom);
  496:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  497:                                           $domconfig{'loadbalancing'}).
  498:                    &new_spares_js().
  499:                    &common_domprefs_js().
  500:                    &Apache::loncommon::javascript_array_indexof();
  501:         }
  502:         if (grep(/^requestcourses$/,@actions)) {
  503:             my $javascript_validations;
  504:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  505:             $js .= <<END;
  506: <script type="text/javascript">
  507: $javascript_validations
  508: </script>
  509: $coursebrowserjs
  510: END
  511:         }
  512:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  513:     } else {
  514: # check if domconfig user exists for the domain.
  515:         my $servadm = $r->dir_config('lonAdmEMail');
  516:         my ($configuserok,$author_ok,$switchserver) =
  517:             &config_check($dom,$confname,$servadm);
  518:         unless ($configuserok eq 'ok') {
  519:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  520:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  521:                           $confname).
  522:                       '<br />'
  523:             );
  524:             if ($switchserver) {
  525:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  526:                           '<br />'.
  527:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  528:                           '<br />'.
  529:                           &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).
  530:                           '<br />'.
  531:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  532:                 );
  533:             } else {
  534:                 $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.').
  535:                           '<br />'.
  536:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  537:                 );
  538:             }
  539:             $r->print(&Apache::loncommon::end_page());
  540:             return OK;
  541:         }
  542:         if (keys(%domconfig) == 0) {
  543:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  544:             my @ids=&Apache::lonnet::current_machine_ids();
  545:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  546:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  547:                 my @loginimages = ('img','logo','domlogo','login');
  548:                 my $custom_img_count = 0;
  549:                 foreach my $img (@loginimages) {
  550:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  551:                         $custom_img_count ++;
  552:                     }
  553:                 }
  554:                 foreach my $role (@roles) {
  555:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  556:                         $custom_img_count ++;
  557:                     }
  558:                 }
  559:                 if ($custom_img_count > 0) {
  560:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  561:                     my $switch_server = &check_switchserver($dom,$confname);
  562:                     $r->print(
  563:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  564:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  565:     &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 />'.
  566:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  567:                     if ($switch_server) {
  568:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  569:                     }
  570:                     $r->print(&Apache::loncommon::end_page());
  571:                     return OK;
  572:                 }
  573:             }
  574:         }
  575:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  576:     }
  577:     return OK;
  578: }
  579: 
  580: sub process_changes {
  581:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  582:     my %domconfig;
  583:     if (ref($values) eq 'HASH') {
  584:         %domconfig = %{$values};
  585:     }
  586:     my $output;
  587:     if ($action eq 'login') {
  588:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  589:     } elsif ($action eq 'rolecolors') {
  590:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  591:                                      $lastactref,%domconfig);
  592:     } elsif ($action eq 'quotas') {
  593:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  594:     } elsif ($action eq 'autoenroll') {
  595:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  596:     } elsif ($action eq 'autoupdate') {
  597:         $output = &modify_autoupdate($dom,%domconfig);
  598:     } elsif ($action eq 'autocreate') {
  599:         $output = &modify_autocreate($dom,%domconfig);
  600:     } elsif ($action eq 'directorysrch') {
  601:         $output = &modify_directorysrch($dom,%domconfig);
  602:     } elsif ($action eq 'usercreation') {
  603:         $output = &modify_usercreation($dom,%domconfig);
  604:     } elsif ($action eq 'selfcreation') {
  605:         $output = &modify_selfcreation($dom,%domconfig);
  606:     } elsif ($action eq 'usermodification') {
  607:         $output = &modify_usermodification($dom,%domconfig);
  608:     } elsif ($action eq 'contacts') {
  609:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  610:     } elsif ($action eq 'defaults') {
  611:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  612:     } elsif ($action eq 'scantron') {
  613:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  614:     } elsif ($action eq 'coursecategories') {
  615:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  616:     } elsif ($action eq 'serverstatuses') {
  617:         $output = &modify_serverstatuses($dom,%domconfig);
  618:     } elsif ($action eq 'requestcourses') {
  619:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  620:     } elsif ($action eq 'requestauthor') {
  621:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  622:     } elsif ($action eq 'coursedefaults') {
  623:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  624:     } elsif ($action eq 'selfenrollment') {
  625:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  626:     } elsif ($action eq 'usersessions') {
  627:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  628:     } elsif ($action eq 'loadbalancing') {
  629:         $output = &modify_loadbalancing($dom,%domconfig);
  630:     }
  631:     return $output;
  632: }
  633: 
  634: sub print_config_box {
  635:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  636:     my $rowtotal = 0;
  637:     my $output;
  638:     if ($action eq 'coursecategories') {
  639:         $output = &coursecategories_javascript($settings);
  640:     } elsif ($action eq 'defaults') {
  641:         $output = &defaults_javascript($settings); 
  642:     }
  643:     $output .=
  644:          '<table class="LC_nested_outer">
  645:           <tr>
  646:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  647:            &mt($item->{text}).'&nbsp;'.
  648:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  649:           '</tr>';
  650:     $rowtotal ++;
  651:     my $numheaders = 1;
  652:     if (ref($item->{'header'}) eq 'ARRAY') {
  653:         $numheaders = scalar(@{$item->{'header'}});
  654:     }
  655:     if ($numheaders > 1) {
  656:         my $colspan = '';
  657:         my $rightcolspan = '';
  658:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  659:             (($action eq 'login') && ($numheaders < 4))) {
  660:             $colspan = ' colspan="2"';
  661:         }
  662:         if ($action eq 'usersessions') {
  663:             $rightcolspan = ' colspan="3"'; 
  664:         }
  665:         $output .= '
  666:           <tr>
  667:            <td>
  668:             <table class="LC_nested">
  669:              <tr class="LC_info_row">
  670:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  671:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  672:              </tr>';
  673:         $rowtotal ++;
  674:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  675:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  676:             ($action eq 'selfenrollment') || ($action eq 'usersessions')) {
  677:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  678:         } elsif ($action eq 'coursecategories') {
  679:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  680:         } elsif ($action eq 'login') {
  681:             if ($numheaders == 4) {
  682:                 $colspan = ' colspan="2"';
  683:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  684:             } else {
  685:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  686:             }
  687:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  688:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  689:         } elsif ($action eq 'rolecolors') {
  690:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  691:         }
  692:         $output .= '
  693:            </table>
  694:           </td>
  695:          </tr>
  696:          <tr>
  697:            <td>
  698:             <table class="LC_nested">
  699:              <tr class="LC_info_row">
  700:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  701:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  702:              </tr>';
  703:             $rowtotal ++;
  704:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  705:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  706:             ($action eq 'usersessions') || ($action eq 'coursecategories')) {
  707:             if ($action eq 'coursecategories') {
  708:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  709:                 $colspan = ' colspan="2"';
  710:             } else {
  711:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  712:             }
  713:             $output .= '
  714:            </table>
  715:           </td>
  716:          </tr>
  717:          <tr>
  718:            <td>
  719:             <table class="LC_nested">
  720:              <tr class="LC_info_row">
  721:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  722:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  723:              </tr>'."\n";
  724:             if ($action eq 'coursecategories') {
  725:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  726:             } else {
  727:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  728:             }
  729:             $rowtotal ++;
  730:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  731:                   ($action eq 'defaults')) {
  732:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  733:         } elsif ($action eq 'login') {
  734:             if ($numheaders == 4) {
  735:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  736:            </table>
  737:           </td>
  738:          </tr>
  739:          <tr>
  740:            <td>
  741:             <table class="LC_nested">
  742:              <tr class="LC_info_row">
  743:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  744:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  745:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  746:                 $rowtotal ++;
  747:             } else {
  748:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  749:             }
  750:             $output .= '
  751:            </table>
  752:           </td>
  753:          </tr>
  754:          <tr>
  755:            <td>
  756:             <table class="LC_nested">
  757:              <tr class="LC_info_row">';
  758:             if ($numheaders == 4) {
  759:                 $output .= '
  760:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  761:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  762:              </tr>';
  763:             } else {
  764:                 $output .= '
  765:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  766:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  767:              </tr>';
  768:             }
  769:             $rowtotal ++;
  770:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  771:         } elsif ($action eq 'requestcourses') {
  772:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  773:             $rowtotal ++;
  774:             $output .= &print_studentcode($settings,\$rowtotal).'
  775:            </table>
  776:           </td>
  777:          </tr>
  778:          <tr>
  779:            <td>
  780:             <table class="LC_nested">
  781:              <tr class="LC_info_row">
  782:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  783:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  784:                        &textbookcourses_javascript($settings).
  785:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  786:             </table>
  787:            </td>
  788:           </tr>
  789:          <tr>
  790:            <td>
  791:             <table class="LC_nested">
  792:              <tr class="LC_info_row">
  793:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  794:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  795:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  796:             </table>
  797:            </td>
  798:           </tr>
  799:           <tr>
  800:            <td>
  801:             <table class="LC_nested">
  802:              <tr class="LC_info_row">
  803:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  804:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  805:              </tr>'.
  806:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  807:         } elsif ($action eq 'requestauthor') {
  808:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  809:             $rowtotal ++;
  810:         } elsif ($action eq 'rolecolors') {
  811:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  812:            </table>
  813:           </td>
  814:          </tr>
  815:          <tr>
  816:            <td>
  817:             <table class="LC_nested">
  818:              <tr class="LC_info_row">
  819:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  820:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  821:               <td class="LC_right_item" valign="top">'.
  822:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  823:              </tr>'.
  824:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  825:            </table>
  826:           </td>
  827:          </tr>
  828:          <tr>
  829:            <td>
  830:             <table class="LC_nested">
  831:              <tr class="LC_info_row">
  832:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  833:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  834:              </tr>'.
  835:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  836:             $rowtotal += 2;
  837:         }
  838:     } else {
  839:         $output .= '
  840:           <tr>
  841:            <td>
  842:             <table class="LC_nested">
  843:              <tr class="LC_info_row">';
  844:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  845:             $output .= '  
  846:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  847:         } elsif ($action eq 'serverstatuses') {
  848:             $output .= '
  849:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  850:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  851: 
  852:         } else {
  853:             $output .= '
  854:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  855:         }
  856:         if (defined($item->{'header'}->[0]->{'col3'})) {
  857:             $output .= '<td class="LC_left_item" valign="top">'.
  858:                        &mt($item->{'header'}->[0]->{'col2'});
  859:             if ($action eq 'serverstatuses') {
  860:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  861:             } 
  862:         } else {
  863:             $output .= '<td class="LC_right_item" valign="top">'.
  864:                        &mt($item->{'header'}->[0]->{'col2'});
  865:         }
  866:         $output .= '</td>';
  867:         if ($item->{'header'}->[0]->{'col3'}) {
  868:             if (defined($item->{'header'}->[0]->{'col4'})) {
  869:                 $output .= '<td class="LC_left_item" valign="top">'.
  870:                             &mt($item->{'header'}->[0]->{'col3'});
  871:             } else {
  872:                 $output .= '<td class="LC_right_item" valign="top">'.
  873:                            &mt($item->{'header'}->[0]->{'col3'});
  874:             }
  875:             if ($action eq 'serverstatuses') {
  876:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  877:             }
  878:             $output .= '</td>';
  879:         }
  880:         if ($item->{'header'}->[0]->{'col4'}) {
  881:             $output .= '<td class="LC_right_item" valign="top">'.
  882:                        &mt($item->{'header'}->[0]->{'col4'});
  883:         }
  884:         $output .= '</tr>';
  885:         $rowtotal ++;
  886:         if ($action eq 'quotas') {
  887:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  888:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || ($action eq 'directorysrch') ||
  889:                  ($action eq 'contacts') || ($action eq 'serverstatuses') || ($action eq 'loadbalancing')) {
  890:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
  891:         } elsif ($action eq 'scantron') {
  892:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  893:         } elsif ($action eq 'helpsettings') {
  894:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  895:         }
  896:     }
  897:     $output .= '
  898:    </table>
  899:   </td>
  900:  </tr>
  901: </table><br />';
  902:     return ($output,$rowtotal);
  903: }
  904: 
  905: sub print_login {
  906:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  907:     my ($css_class,$datatable);
  908:     my %choices = &login_choices();
  909: 
  910:     if ($caller eq 'service') {
  911:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  912:         my $choice = $choices{'disallowlogin'};
  913:         $css_class = ' class="LC_odd_row"';
  914:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  915:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  916:                       '<th>'.$choices{'server'}.'</th>'.
  917:                       '<th>'.$choices{'serverpath'}.'</th>'.
  918:                       '<th>'.$choices{'custompath'}.'</th>'.
  919:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  920:         my %disallowed;
  921:         if (ref($settings) eq 'HASH') {
  922:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  923:                %disallowed = %{$settings->{'loginvia'}};
  924:             }
  925:         }
  926:         foreach my $lonhost (sort(keys(%servers))) {
  927:             my $direct = 'selected="selected"';
  928:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  929:                 if ($disallowed{$lonhost}{'server'} ne '') {
  930:                     $direct = '';
  931:                 }
  932:             }
  933:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  934:                           '<td><select name="'.$lonhost.'_server">'.
  935:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  936:                           '</option>';
  937:             foreach my $hostid (sort(keys(%servers))) {
  938:                 next if ($servers{$hostid} eq $servers{$lonhost});
  939:                 my $selected = '';
  940:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  941:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  942:                         $selected = 'selected="selected"';
  943:                     }
  944:                 }
  945:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  946:                               $servers{$hostid}.'</option>';
  947:             }
  948:             $datatable .= '</select></td>'.
  949:                           '<td><select name="'.$lonhost.'_serverpath">';
  950:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  951:                 my $pathname = $path;
  952:                 if ($path eq 'custom') {
  953:                     $pathname = &mt('Custom Path').' ->';
  954:                 }
  955:                 my $selected = '';
  956:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  957:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  958:                         $selected = 'selected="selected"';
  959:                     }
  960:                 } elsif ($path eq '') {
  961:                     $selected = 'selected="selected"';
  962:                 }
  963:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  964:             }
  965:             $datatable .= '</select></td>';
  966:             my ($custom,$exempt);
  967:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  968:                 $custom = $disallowed{$lonhost}{'custompath'};
  969:                 $exempt = $disallowed{$lonhost}{'exempt'};
  970:             }
  971:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  972:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  973:                           '</tr>';
  974:         }
  975:         $datatable .= '</table></td></tr>';
  976:         return $datatable;
  977:     } elsif ($caller eq 'page') {
  978:         my %defaultchecked = ( 
  979:                                'coursecatalog' => 'on',
  980:                                'helpdesk'      => 'on',
  981:                                'adminmail'     => 'off',
  982:                                'newuser'       => 'off',
  983:                              );
  984:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  985:         my (%checkedon,%checkedoff);
  986:         foreach my $item (@toggles) {
  987:             if ($defaultchecked{$item} eq 'on') { 
  988:                 $checkedon{$item} = ' checked="checked" ';
  989:                 $checkedoff{$item} = ' ';
  990:             } elsif ($defaultchecked{$item} eq 'off') {
  991:                 $checkedoff{$item} = ' checked="checked" ';
  992:                 $checkedon{$item} = ' ';
  993:             }
  994:         }
  995:         my @images = ('img','logo','domlogo','login');
  996:         my @logintext = ('textcol','bgcol');
  997:         my @bgs = ('pgbg','mainbg','sidebg');
  998:         my @links = ('link','alink','vlink');
  999:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1000:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1001:         my (%is_custom,%designs);
 1002:         my %defaults = (
 1003:                        font => $defaultdesign{'login.font'},
 1004:                        );
 1005:         foreach my $item (@images) {
 1006:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1007:             $defaults{'showlogo'}{$item} = 1;
 1008:         }
 1009:         foreach my $item (@bgs) {
 1010:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1011:         }
 1012:         foreach my $item (@logintext) {
 1013:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1014:         }
 1015:         foreach my $item (@links) {
 1016:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1017:         }
 1018:         if (ref($settings) eq 'HASH') {
 1019:             foreach my $item (@toggles) {
 1020:                 if ($settings->{$item} eq '1') {
 1021:                     $checkedon{$item} =  ' checked="checked" ';
 1022:                     $checkedoff{$item} = ' ';
 1023:                 } elsif ($settings->{$item} eq '0') {
 1024:                     $checkedoff{$item} =  ' checked="checked" ';
 1025:                     $checkedon{$item} = ' ';
 1026:                 }
 1027:             }
 1028:             foreach my $item (@images) {
 1029:                 if (defined($settings->{$item})) {
 1030:                     $designs{$item} = $settings->{$item};
 1031:                     $is_custom{$item} = 1;
 1032:                 }
 1033:                 if (defined($settings->{'showlogo'}{$item})) {
 1034:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1035:                 }
 1036:             }
 1037:             foreach my $item (@logintext) {
 1038:                 if ($settings->{$item} ne '') {
 1039:                     $designs{'logintext'}{$item} = $settings->{$item};
 1040:                     $is_custom{$item} = 1;
 1041:                 }
 1042:             }
 1043:             if ($settings->{'font'} ne '') {
 1044:                 $designs{'font'} = $settings->{'font'};
 1045:                 $is_custom{'font'} = 1;
 1046:             }
 1047:             foreach my $item (@bgs) {
 1048:                 if ($settings->{$item} ne '') {
 1049:                     $designs{'bgs'}{$item} = $settings->{$item};
 1050:                     $is_custom{$item} = 1;
 1051:                 }
 1052:             }
 1053:             foreach my $item (@links) {
 1054:                 if ($settings->{$item} ne '') {
 1055:                     $designs{'links'}{$item} = $settings->{$item};
 1056:                     $is_custom{$item} = 1;
 1057:                 }
 1058:             }
 1059:         } else {
 1060:             if ($designhash{$dom.'.login.font'} ne '') {
 1061:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1062:                 $is_custom{'font'} = 1;
 1063:             }
 1064:             foreach my $item (@images) {
 1065:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1066:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1067:                     $is_custom{$item} = 1;
 1068:                 }
 1069:             }
 1070:             foreach my $item (@bgs) {
 1071:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1072:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1073:                     $is_custom{$item} = 1;
 1074:                 }
 1075:             }
 1076:             foreach my $item (@links) {
 1077:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1078:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1079:                     $is_custom{$item} = 1;
 1080:                 }
 1081:             }
 1082:         }
 1083:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1084:                                                       logo => 'Institution Logo',
 1085:                                                       domlogo => 'Domain Logo',
 1086:                                                       login => 'Login box');
 1087:         my $itemcount = 1;
 1088:         foreach my $item (@toggles) {
 1089:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1090:             $datatable .=  
 1091:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1092:                 '</td><td>'.
 1093:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1094:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1095:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1096:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1097:                 '</tr>';
 1098:             $itemcount ++;
 1099:         }
 1100:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1101:         $datatable .= '</tr></table></td></tr>';
 1102:     } elsif ($caller eq 'help') {
 1103:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1104:         my $switchserver = &check_switchserver($dom,$confname);
 1105:         my $itemcount = 1;
 1106:         $defaulturl = '/adm/loginproblems.html';
 1107:         $defaulttype = 'default';
 1108:         %lt = &Apache::lonlocal::texthash (
 1109:                      del     => 'Delete?',
 1110:                      rep     => 'Replace:',
 1111:                      upl     => 'Upload:',
 1112:                      default => 'Default',
 1113:                      custom  => 'Custom',
 1114:                                              );
 1115:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1116:         my @currlangs;
 1117:         if (ref($settings) eq 'HASH') {
 1118:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1119:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1120:                     next if ($settings->{'helpurl'}{$key} eq '');
 1121:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1122:                     $type{$key} = 'custom';
 1123:                     unless ($key eq 'nolang') {
 1124:                         push(@currlangs,$key);
 1125:                     }
 1126:                 }
 1127:             } elsif ($settings->{'helpurl'} ne '') {
 1128:                 $type{'nolang'} = 'custom';
 1129:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1130:             }
 1131:         }
 1132:         foreach my $lang ('nolang',sort(@currlangs)) {
 1133:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1134:             $datatable .= '<tr'.$css_class.'>';
 1135:             if ($url{$lang} eq '') {
 1136:                 $url{$lang} = $defaulturl;
 1137:             }
 1138:             if ($type{$lang} eq '') {
 1139:                 $type{$lang} = $defaulttype;
 1140:             }
 1141:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1142:             if ($lang eq 'nolang') {
 1143:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1144:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1145:             } else {
 1146:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1147:                                   $langchoices{$lang},
 1148:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1149:             }
 1150:             $datatable .= '</span></td>'."\n".
 1151:                           '<td class="LC_left_item">';
 1152:             if ($type{$lang} eq 'custom') {
 1153:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1154:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1155:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1156:             } else {
 1157:                 $datatable .= $lt{'upl'};
 1158:             }
 1159:             $datatable .='<br />';
 1160:             if ($switchserver) {
 1161:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1162:             } else {
 1163:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1164:             }
 1165:             $datatable .= '</td></tr>';
 1166:             $itemcount ++;
 1167:         }
 1168:         my @addlangs;
 1169:         foreach my $lang (sort(keys(%langchoices))) {
 1170:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1171:             push(@addlangs,$lang);
 1172:         }
 1173:         if (@addlangs > 0) {
 1174:             my %toadd;
 1175:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1176:             $toadd{''} = &mt('Select');
 1177:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1178:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1179:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1180:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1181:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1182:             if ($switchserver) {
 1183:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1184:             } else {
 1185:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1186:             }
 1187:             $datatable .= '</td></tr>';
 1188:             $itemcount ++;
 1189:         }
 1190:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1191:     } elsif ($caller eq 'headtag') {
 1192:         my %domservers = &Apache::lonnet::get_servers($dom);
 1193:         my $choice = $choices{'headtag'};
 1194:         $css_class = ' class="LC_odd_row"';
 1195:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1196:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1197:                       '<th>'.$choices{'current'}.'</th>'.
 1198:                       '<th>'.$choices{'action'}.'</th>'.
 1199:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1200:         my (%currurls,%currexempt);
 1201:         if (ref($settings) eq 'HASH') {
 1202:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1203:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1204:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1205:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1206:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1207:                     }
 1208:                 }
 1209:             }
 1210:         }
 1211:         my %lt = &Apache::lonlocal::texthash(
 1212:                                                del  => 'Delete?',
 1213:                                                rep  => 'Replace:',
 1214:                                                upl  => 'Upload:',
 1215:                                                curr => 'View contents',
 1216:                                                none => 'None',
 1217:         );
 1218:         my $switchserver = &check_switchserver($dom,$confname);
 1219:         foreach my $lonhost (sort(keys(%domservers))) {
 1220:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1221:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1222:             if ($currurls{$lonhost}) {
 1223:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1224:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1225:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1226:                               '">'.$lt{'curr'}.'</a></td>'.
 1227:                               '<td><span class="LC_nobreak"><label>'.
 1228:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1229:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1230:             } else {
 1231:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1232:             }
 1233:             $datatable .='<br />';
 1234:             if ($switchserver) {
 1235:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1236:             } else {
 1237:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1238:             }
 1239:             $datatable .= '</td><td><input type="textbox" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1240:         }
 1241:         $datatable .= '</table></td></tr>';
 1242:     }
 1243:     return $datatable;
 1244: }
 1245: 
 1246: sub login_choices {
 1247:     my %choices =
 1248:         &Apache::lonlocal::texthash (
 1249:             coursecatalog => 'Display Course/Community Catalog link?',
 1250:             adminmail     => "Display Administrator's E-mail Address?",
 1251:             helpdesk      => 'Display "Contact Helpdesk" link',
 1252:             disallowlogin => "Login page requests redirected",
 1253:             hostid        => "Server",
 1254:             server        => "Redirect to:",
 1255:             serverpath    => "Path",
 1256:             custompath    => "Custom", 
 1257:             exempt        => "Exempt IP(s)",
 1258:             directlogin   => "No redirect",
 1259:             newuser       => "Link to create a user account",
 1260:             img           => "Header",
 1261:             logo          => "Main Logo",
 1262:             domlogo       => "Domain Logo",
 1263:             login         => "Log-in Header", 
 1264:             textcol       => "Text color",
 1265:             bgcol         => "Box color",
 1266:             bgs           => "Background colors",
 1267:             links         => "Link colors",
 1268:             font          => "Font color",
 1269:             pgbg          => "Header",
 1270:             mainbg        => "Page",
 1271:             sidebg        => "Login box",
 1272:             link          => "Link",
 1273:             alink         => "Active link",
 1274:             vlink         => "Visited link",
 1275:             headtag       => "Custom markup",
 1276:             action        => "Action",
 1277:             current       => "Current",
 1278:         );
 1279:     return %choices;
 1280: }
 1281: 
 1282: sub print_rolecolors {
 1283:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1284:     my %choices = &color_font_choices();
 1285:     my @bgs = ('pgbg','tabbg','sidebg');
 1286:     my @links = ('link','alink','vlink');
 1287:     my @images = ('img');
 1288:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1289:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1290:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1291:     my (%is_custom,%designs);
 1292:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1293:     if (ref($settings) eq 'HASH') {
 1294:         if (ref($settings->{$role}) eq 'HASH') {
 1295:             if ($settings->{$role}->{'img'} ne '') {
 1296:                 $designs{'img'} = $settings->{$role}->{'img'};
 1297:                 $is_custom{'img'} = 1;
 1298:             }
 1299:             if ($settings->{$role}->{'font'} ne '') {
 1300:                 $designs{'font'} = $settings->{$role}->{'font'};
 1301:                 $is_custom{'font'} = 1;
 1302:             }
 1303:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1304:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1305:                 $is_custom{'fontmenu'} = 1;
 1306:             }
 1307:             foreach my $item (@bgs) {
 1308:                 if ($settings->{$role}->{$item} ne '') {
 1309:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1310:                     $is_custom{$item} = 1;
 1311:                 }
 1312:             }
 1313:             foreach my $item (@links) {
 1314:                 if ($settings->{$role}->{$item} ne '') {
 1315:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1316:                     $is_custom{$item} = 1;
 1317:                 }
 1318:             }
 1319:         }
 1320:     } else {
 1321:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1322:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1323:             $is_custom{'img'} = 1;
 1324:         }
 1325:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1326:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1327:             $is_custom{'fontmenu'} = 1; 
 1328:         }
 1329:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1330:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1331:             $is_custom{'font'} = 1;
 1332:         }
 1333:         foreach my $item (@bgs) {
 1334:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1335:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1336:                 $is_custom{$item} = 1;
 1337:             
 1338:             }
 1339:         }
 1340:         foreach my $item (@links) {
 1341:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1342:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1343:                 $is_custom{$item} = 1;
 1344:             }
 1345:         }
 1346:     }
 1347:     my $itemcount = 1;
 1348:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1349:     $datatable .= '</tr></table></td></tr>';
 1350:     return $datatable;
 1351: }
 1352: 
 1353: sub role_defaults {
 1354:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1355:     my %defaults;
 1356:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1357:         return %defaults;
 1358:     }
 1359:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1360:     if ($role eq 'login') {
 1361:         %defaults = (
 1362:                        font => $defaultdesign{$role.'.font'},
 1363:                     );
 1364:         if (ref($logintext) eq 'ARRAY') {
 1365:             foreach my $item (@{$logintext}) {
 1366:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1367:             }
 1368:         }
 1369:         foreach my $item (@{$images}) {
 1370:             $defaults{'showlogo'}{$item} = 1;
 1371:         }
 1372:     } else {
 1373:         %defaults = (
 1374:                        img => $defaultdesign{$role.'.img'},
 1375:                        font => $defaultdesign{$role.'.font'},
 1376:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1377:                     );
 1378:     }
 1379:     foreach my $item (@{$bgs}) {
 1380:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1381:     }
 1382:     foreach my $item (@{$links}) {
 1383:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1384:     }
 1385:     foreach my $item (@{$images}) {
 1386:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1387:     }
 1388:     return %defaults;
 1389: }
 1390: 
 1391: sub display_color_options {
 1392:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1393:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1394:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1395:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1396:     my $datatable = '<tr'.$css_class.'>'.
 1397:         '<td>'.$choices->{'font'}.'</td>';
 1398:     if (!$is_custom->{'font'}) {
 1399:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1400:     } else {
 1401:         $datatable .= '<td>&nbsp;</td>';
 1402:     }
 1403:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1404: 
 1405:     $datatable .= '<td><span class="LC_nobreak">'.
 1406:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1407:                   ' value="'.$current_color.'" />&nbsp;'.
 1408:                   '&nbsp;</td></tr>';
 1409:     unless ($role eq 'login') { 
 1410:         $datatable .= '<tr'.$css_class.'>'.
 1411:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1412:         if (!$is_custom->{'fontmenu'}) {
 1413:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1414:         } else {
 1415:             $datatable .= '<td>&nbsp;</td>';
 1416:         }
 1417: 	$current_color = $designs->{'fontmenu'} ?
 1418: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1419:         $datatable .= '<td><span class="LC_nobreak">'.
 1420:                       '<input class="colorchooser" type="text" size="10" name="'
 1421: 		      .$role.'_fontmenu"'.
 1422:                       ' value="'.$current_color.'" />&nbsp;'.
 1423:                       '&nbsp;</td></tr>';
 1424:     }
 1425:     my $switchserver = &check_switchserver($dom,$confname);
 1426:     foreach my $img (@{$images}) {
 1427: 	$itemcount ++;
 1428:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1429:         $datatable .= '<tr'.$css_class.'>'.
 1430:                       '<td>'.$choices->{$img};
 1431:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1432:         if ($role eq 'login') {
 1433:             if ($img eq 'login') {
 1434:                 $login_hdr_pick =
 1435:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1436:                 $logincolors =
 1437:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1438:                                        $designs,$defaults);
 1439:             } elsif ($img ne 'domlogo') {
 1440:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1441:             }
 1442:         }
 1443:         $datatable .= '</td>';
 1444:         if ($designs->{$img} ne '') {
 1445:             $imgfile = $designs->{$img};
 1446: 	    $img_import = ($imgfile =~ m{^/adm/});
 1447:         } else {
 1448:             $imgfile = $defaults->{$img};
 1449:         }
 1450:         if ($imgfile) {
 1451:             my ($showfile,$fullsize);
 1452:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1453:                 my $urldir = $1;
 1454:                 my $filename = $2;
 1455:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1456:                 if (@info) {
 1457:                     my $thumbfile = 'tn-'.$filename;
 1458:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1459:                     if (@thumb) {
 1460:                         $showfile = $urldir.'/'.$thumbfile;
 1461:                     } else {
 1462:                         $showfile = $imgfile;
 1463:                     }
 1464:                 } else {
 1465:                     $showfile = '';
 1466:                 }
 1467:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1468:                 $showfile = $imgfile;
 1469:                 my $imgdir = $1;
 1470:                 my $filename = $2;
 1471:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1472:                     $showfile = "/$imgdir/tn-".$filename;
 1473:                 } else {
 1474:                     my $input = $londocroot.$imgfile;
 1475:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1476:                     if (!-e $output) {
 1477:                         my ($width,$height) = &thumb_dimensions();
 1478:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1479:                         if ($fullwidth ne '' && $fullheight ne '') {
 1480:                             if ($fullwidth > $width && $fullheight > $height) { 
 1481:                                 my $size = $width.'x'.$height;
 1482:                                 system("convert -sample $size $input $output");
 1483:                                 $showfile = "/$imgdir/tn-".$filename;
 1484:                             }
 1485:                         }
 1486:                     }
 1487:                 }
 1488:             }
 1489:             if ($showfile) {
 1490:                 if ($showfile =~ m{^/(adm|res)/}) {
 1491:                     if ($showfile =~ m{^/res/}) {
 1492:                         my $local_showfile =
 1493:                             &Apache::lonnet::filelocation('',$showfile);
 1494:                         &Apache::lonnet::repcopy($local_showfile);
 1495:                     }
 1496:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1497:                 }
 1498:                 if ($imgfile) {
 1499:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1500:                         if ($imgfile =~ m{^/res/}) {
 1501:                             my $local_imgfile =
 1502:                                 &Apache::lonnet::filelocation('',$imgfile);
 1503:                             &Apache::lonnet::repcopy($local_imgfile);
 1504:                         }
 1505:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1506:                     } else {
 1507:                         $fullsize = $imgfile;
 1508:                     }
 1509:                 }
 1510:                 $datatable .= '<td>';
 1511:                 if ($img eq 'login') {
 1512:                     $datatable .= $login_hdr_pick;
 1513:                 } 
 1514:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1515:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1516:             } else {
 1517:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1518:                               &mt('Upload:').'<br />';
 1519:             }
 1520:         } else {
 1521:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1522:                           &mt('Upload:').'<br />';
 1523:         }
 1524:         if ($switchserver) {
 1525:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1526:         } else {
 1527:             if ($img ne 'login') { # suppress file selection for Log-in header
 1528:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1529:             }
 1530:         }
 1531:         $datatable .= '</td></tr>';
 1532:     }
 1533:     $itemcount ++;
 1534:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1535:     $datatable .= '<tr'.$css_class.'>'.
 1536:                   '<td>'.$choices->{'bgs'}.'</td>';
 1537:     my $bgs_def;
 1538:     foreach my $item (@{$bgs}) {
 1539:         if (!$is_custom->{$item}) {
 1540:             $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>';
 1541:         }
 1542:     }
 1543:     if ($bgs_def) {
 1544:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1545:     } else {
 1546:         $datatable .= '<td>&nbsp;</td>';
 1547:     }
 1548:     $datatable .= '<td class="LC_right_item">'.
 1549:                   '<table border="0"><tr>';
 1550: 
 1551:     foreach my $item (@{$bgs}) {
 1552:         $datatable .= '<td align="center">'.$choices->{$item};
 1553: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1554:         if ($designs->{'bgs'}{$item}) {
 1555:             $datatable .= '&nbsp;';
 1556:         }
 1557:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1558:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1559:     }
 1560:     $datatable .= '</tr></table></td></tr>';
 1561:     $itemcount ++;
 1562:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1563:     $datatable .= '<tr'.$css_class.'>'.
 1564:                   '<td>'.$choices->{'links'}.'</td>';
 1565:     my $links_def;
 1566:     foreach my $item (@{$links}) {
 1567:         if (!$is_custom->{$item}) {
 1568:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1569:         }
 1570:     }
 1571:     if ($links_def) {
 1572:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1573:     } else {
 1574:         $datatable .= '<td>&nbsp;</td>';
 1575:     }
 1576:     $datatable .= '<td class="LC_right_item">'.
 1577:                   '<table border="0"><tr>';
 1578:     foreach my $item (@{$links}) {
 1579: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1580:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1581:         if ($designs->{'links'}{$item}) {
 1582:             $datatable.='&nbsp;';
 1583:         }
 1584:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1585:                       '" /></td>';
 1586:     }
 1587:     $$rowtotal += $itemcount;
 1588:     return $datatable;
 1589: }
 1590: 
 1591: sub logo_display_options {
 1592:     my ($img,$defaults,$designs) = @_;
 1593:     my $checkedon;
 1594:     if (ref($defaults) eq 'HASH') {
 1595:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1596:             if ($defaults->{'showlogo'}{$img}) {
 1597:                 $checkedon = 'checked="checked" ';     
 1598:             }
 1599:         } 
 1600:     }
 1601:     if (ref($designs) eq 'HASH') {
 1602:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1603:             if (defined($designs->{'showlogo'}{$img})) {
 1604:                 if ($designs->{'showlogo'}{$img} == 0) {
 1605:                     $checkedon = '';
 1606:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1607:                     $checkedon = 'checked="checked" ';
 1608:                 }
 1609:             }
 1610:         }
 1611:     }
 1612:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1613:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1614:            &mt('show').'</label>'."\n";
 1615: }
 1616: 
 1617: sub login_header_options  {
 1618:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1619:     my $output = '';
 1620:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1621:         $output .= &mt('Text default(s):').'<br />';
 1622:         if (!$is_custom->{'textcol'}) {
 1623:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1624:                        '&nbsp;&nbsp;&nbsp;';
 1625:         }
 1626:         if (!$is_custom->{'bgcol'}) {
 1627:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1628:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1629:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1630:         }
 1631:         $output .= '<br />';
 1632:     }
 1633:     $output .='<br />';
 1634:     return $output;
 1635: }
 1636: 
 1637: sub login_text_colors {
 1638:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1639:     my $color_menu = '<table border="0"><tr>';
 1640:     foreach my $item (@{$logintext}) {
 1641:         $color_menu .= '<td align="center">'.$choices->{$item};
 1642:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1643:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1644:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1645:     }
 1646:     $color_menu .= '</tr></table><br />';
 1647:     return $color_menu;
 1648: }
 1649: 
 1650: sub image_changes {
 1651:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1652:     my $output;
 1653:     if ($img eq 'login') {
 1654:             # suppress image for Log-in header
 1655:     } elsif (!$is_custom) {
 1656:         if ($img ne 'domlogo') {
 1657:             $output .= &mt('Default image:').'<br />';
 1658:         } else {
 1659:             $output .= &mt('Default in use:').'<br />';
 1660:         }
 1661:     }
 1662:     if ($img eq 'login') { # suppress image for Log-in header
 1663:         $output .= '<td>'.$logincolors;
 1664:     } else {
 1665:         if ($img_import) {
 1666:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1667:         }
 1668:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1669:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1670:         if ($is_custom) {
 1671:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1672:                        '<input type="checkbox" name="'.
 1673:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1674:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1675:         } else {
 1676:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1677:         }
 1678:     }
 1679:     return $output;
 1680: }
 1681: 
 1682: sub print_quotas {
 1683:     my ($dom,$settings,$rowtotal,$action) = @_;
 1684:     my $context;
 1685:     if ($action eq 'quotas') {
 1686:         $context = 'tools';
 1687:     } else {
 1688:         $context = $action;
 1689:     }
 1690:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1691:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1692:     my $typecount = 0;
 1693:     my ($css_class,%titles);
 1694:     if ($context eq 'requestcourses') {
 1695:         @usertools = ('official','unofficial','community','textbook');
 1696:         @options =('norequest','approval','validate','autolimit');
 1697:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1698:         %titles = &courserequest_titles();
 1699:     } elsif ($context eq 'requestauthor') {
 1700:         @usertools = ('author');
 1701:         @options = ('norequest','approval','automatic');
 1702:         %titles = &authorrequest_titles();
 1703:     } else {
 1704:         @usertools = ('aboutme','blog','webdav','portfolio');
 1705:         %titles = &tool_titles();
 1706:     }
 1707:     if (ref($types) eq 'ARRAY') {
 1708:         foreach my $type (@{$types}) {
 1709:             my ($currdefquota,$currauthorquota);
 1710:             unless (($context eq 'requestcourses') ||
 1711:                     ($context eq 'requestauthor')) {
 1712:                 if (ref($settings) eq 'HASH') {
 1713:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1714:                         $currdefquota = $settings->{defaultquota}->{$type};
 1715:                     } else {
 1716:                         $currdefquota = $settings->{$type};
 1717:                     }
 1718:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1719:                         $currauthorquota = $settings->{authorquota}->{$type};
 1720:                     }
 1721:                 }
 1722:             }
 1723:             if (defined($usertypes->{$type})) {
 1724:                 $typecount ++;
 1725:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1726:                 $datatable .= '<tr'.$css_class.'>'.
 1727:                               '<td>'.$usertypes->{$type}.'</td>'.
 1728:                               '<td class="LC_left_item">';
 1729:                 if ($context eq 'requestcourses') {
 1730:                     $datatable .= '<table><tr>';
 1731:                 }
 1732:                 my %cell;  
 1733:                 foreach my $item (@usertools) {
 1734:                     if ($context eq 'requestcourses') {
 1735:                         my ($curroption,$currlimit);
 1736:                         if (ref($settings) eq 'HASH') {
 1737:                             if (ref($settings->{$item}) eq 'HASH') {
 1738:                                 $curroption = $settings->{$item}->{$type};
 1739:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1740:                                     $currlimit = $1; 
 1741:                                 }
 1742:                             }
 1743:                         }
 1744:                         if (!$curroption) {
 1745:                             $curroption = 'norequest';
 1746:                         }
 1747:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1748:                         foreach my $option (@options) {
 1749:                             my $val = $option;
 1750:                             if ($option eq 'norequest') {
 1751:                                 $val = 0;  
 1752:                             }
 1753:                             if ($option eq 'validate') {
 1754:                                 my $canvalidate = 0;
 1755:                                 if (ref($validations{$item}) eq 'HASH') { 
 1756:                                     if ($validations{$item}{$type}) {
 1757:                                         $canvalidate = 1;
 1758:                                     }
 1759:                                 }
 1760:                                 next if (!$canvalidate);
 1761:                             }
 1762:                             my $checked = '';
 1763:                             if ($option eq $curroption) {
 1764:                                 $checked = ' checked="checked"';
 1765:                             } elsif ($option eq 'autolimit') {
 1766:                                 if ($curroption =~ /^autolimit/) {
 1767:                                     $checked = ' checked="checked"';
 1768:                                 }                       
 1769:                             } 
 1770:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1771:                                   '<input type="radio" name="crsreq_'.$item.
 1772:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1773:                                   $titles{$option}.'</label>';
 1774:                             if ($option eq 'autolimit') {
 1775:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1776:                                                 $item.'_limit_'.$type.'" size="1" '.
 1777:                                                 'value="'.$currlimit.'" />';
 1778:                             }
 1779:                             $cell{$item} .= '</span> ';
 1780:                             if ($option eq 'autolimit') {
 1781:                                 $cell{$item} .= $titles{'unlimited'};
 1782:                             }
 1783:                         }
 1784:                     } elsif ($context eq 'requestauthor') {
 1785:                         my $curroption;
 1786:                         if (ref($settings) eq 'HASH') {
 1787:                             $curroption = $settings->{$type};
 1788:                         }
 1789:                         if (!$curroption) {
 1790:                             $curroption = 'norequest';
 1791:                         }
 1792:                         foreach my $option (@options) {
 1793:                             my $val = $option;
 1794:                             if ($option eq 'norequest') {
 1795:                                 $val = 0;
 1796:                             }
 1797:                             my $checked = '';
 1798:                             if ($option eq $curroption) {
 1799:                                 $checked = ' checked="checked"';
 1800:                             }
 1801:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1802:                                   '<input type="radio" name="authorreq_'.$type.
 1803:                                   '" value="'.$val.'"'.$checked.' />'.
 1804:                                   $titles{$option}.'</label></span>&nbsp; ';
 1805:                         }
 1806:                     } else {
 1807:                         my $checked = 'checked="checked" ';
 1808:                         if (ref($settings) eq 'HASH') {
 1809:                             if (ref($settings->{$item}) eq 'HASH') {
 1810:                                 if ($settings->{$item}->{$type} == 0) {
 1811:                                     $checked = '';
 1812:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1813:                                     $checked =  'checked="checked" ';
 1814:                                 }
 1815:                             }
 1816:                         }
 1817:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1818:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1819:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1820:                                       '</label></span>&nbsp; ';
 1821:                     }
 1822:                 }
 1823:                 if ($context eq 'requestcourses') {
 1824:                     $datatable .= '</tr><tr>';
 1825:                     foreach my $item (@usertools) {
 1826:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1827:                     }
 1828:                     $datatable .= '</tr></table>';
 1829:                 }
 1830:                 $datatable .= '</td>';
 1831:                 unless (($context eq 'requestcourses') ||
 1832:                         ($context eq 'requestauthor')) {
 1833:                     $datatable .= 
 1834:                               '<td class="LC_right_item">'.
 1835:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1836:                               '<input type="text" name="quota_'.$type.
 1837:                               '" value="'.$currdefquota.
 1838:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1839:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1840:                               '<input type="text" name="authorquota_'.$type.
 1841:                               '" value="'.$currauthorquota.
 1842:                               '" size="5" /></span></td>';
 1843:                 }
 1844:                 $datatable .= '</tr>';
 1845:             }
 1846:         }
 1847:     }
 1848:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1849:         $defaultquota = '20';
 1850:         $authorquota = '500';
 1851:         if (ref($settings) eq 'HASH') {
 1852:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1853:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1854:             } elsif (defined($settings->{'default'})) {
 1855:                 $defaultquota = $settings->{'default'};
 1856:             }
 1857:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1858:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1859:             }
 1860:         }
 1861:     }
 1862:     $typecount ++;
 1863:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1864:     $datatable .= '<tr'.$css_class.'>'.
 1865:                   '<td>'.$othertitle.'</td>'.
 1866:                   '<td class="LC_left_item">';
 1867:     if ($context eq 'requestcourses') {
 1868:         $datatable .= '<table><tr>';
 1869:     }
 1870:     my %defcell;
 1871:     foreach my $item (@usertools) {
 1872:         if ($context eq 'requestcourses') {
 1873:             my ($curroption,$currlimit);
 1874:             if (ref($settings) eq 'HASH') {
 1875:                 if (ref($settings->{$item}) eq 'HASH') {
 1876:                     $curroption = $settings->{$item}->{'default'};
 1877:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1878:                         $currlimit = $1;
 1879:                     }
 1880:                 }
 1881:             }
 1882:             if (!$curroption) {
 1883:                 $curroption = 'norequest';
 1884:             }
 1885:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1886:             foreach my $option (@options) {
 1887:                 my $val = $option;
 1888:                 if ($option eq 'norequest') {
 1889:                     $val = 0;
 1890:                 }
 1891:                 if ($option eq 'validate') {
 1892:                     my $canvalidate = 0;
 1893:                     if (ref($validations{$item}) eq 'HASH') {
 1894:                         if ($validations{$item}{'default'}) {
 1895:                             $canvalidate = 1;
 1896:                         }
 1897:                     }
 1898:                     next if (!$canvalidate);
 1899:                 }
 1900:                 my $checked = '';
 1901:                 if ($option eq $curroption) {
 1902:                     $checked = ' checked="checked"';
 1903:                 } elsif ($option eq 'autolimit') {
 1904:                     if ($curroption =~ /^autolimit/) {
 1905:                         $checked = ' checked="checked"';
 1906:                     }
 1907:                 }
 1908:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1909:                                   '<input type="radio" name="crsreq_'.$item.
 1910:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1911:                                   $titles{$option}.'</label>';
 1912:                 if ($option eq 'autolimit') {
 1913:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1914:                                        $item.'_limit_default" size="1" '.
 1915:                                        'value="'.$currlimit.'" />';
 1916:                 }
 1917:                 $defcell{$item} .= '</span> ';
 1918:                 if ($option eq 'autolimit') {
 1919:                     $defcell{$item} .= $titles{'unlimited'};
 1920:                 }
 1921:             }
 1922:         } elsif ($context eq 'requestauthor') {
 1923:             my $curroption;
 1924:             if (ref($settings) eq 'HASH') {
 1925:                 $curroption = $settings->{'default'};
 1926:             }
 1927:             if (!$curroption) {
 1928:                 $curroption = 'norequest';
 1929:             }
 1930:             foreach my $option (@options) {
 1931:                 my $val = $option;
 1932:                 if ($option eq 'norequest') {
 1933:                     $val = 0;
 1934:                 }
 1935:                 my $checked = '';
 1936:                 if ($option eq $curroption) {
 1937:                     $checked = ' checked="checked"';
 1938:                 }
 1939:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1940:                               '<input type="radio" name="authorreq_default"'.
 1941:                               ' value="'.$val.'"'.$checked.' />'.
 1942:                               $titles{$option}.'</label></span>&nbsp; ';
 1943:             }
 1944:         } else {
 1945:             my $checked = 'checked="checked" ';
 1946:             if (ref($settings) eq 'HASH') {
 1947:                 if (ref($settings->{$item}) eq 'HASH') {
 1948:                     if ($settings->{$item}->{'default'} == 0) {
 1949:                         $checked = '';
 1950:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1951:                         $checked = 'checked="checked" ';
 1952:                     }
 1953:                 }
 1954:             }
 1955:             $datatable .= '<span class="LC_nobreak"><label>'.
 1956:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1957:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1958:                           '</label></span>&nbsp; ';
 1959:         }
 1960:     }
 1961:     if ($context eq 'requestcourses') {
 1962:         $datatable .= '</tr><tr>';
 1963:         foreach my $item (@usertools) {
 1964:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1965:         }
 1966:         $datatable .= '</tr></table>';
 1967:     }
 1968:     $datatable .= '</td>';
 1969:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1970:         $datatable .= '<td class="LC_right_item">'.
 1971:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1972:                       '<input type="text" name="defaultquota" value="'.
 1973:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1974:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1975:                       '<input type="text" name="authorquota" value="'.
 1976:                       $authorquota.'" size="5" /></span></td>';
 1977:     }
 1978:     $datatable .= '</tr>';
 1979:     $typecount ++;
 1980:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1981:     $datatable .= '<tr'.$css_class.'>'.
 1982:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 1983:     if ($context eq 'requestcourses') {
 1984:         $datatable .= &mt('(overrides affiliation, if set)').
 1985:                       '</td>'.
 1986:                       '<td class="LC_left_item">'.
 1987:                       '<table><tr>';
 1988:     } else {
 1989:         $datatable .= &mt('(overrides affiliation, if checked)').
 1990:                       '</td>'.
 1991:                       '<td class="LC_left_item" colspan="2">'.
 1992:                       '<br />';
 1993:     }
 1994:     my %advcell;
 1995:     foreach my $item (@usertools) {
 1996:         if ($context eq 'requestcourses') {
 1997:             my ($curroption,$currlimit);
 1998:             if (ref($settings) eq 'HASH') {
 1999:                 if (ref($settings->{$item}) eq 'HASH') {
 2000:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2001:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2002:                         $currlimit = $1;
 2003:                     }
 2004:                 }
 2005:             }
 2006:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2007:             my $checked = '';
 2008:             if ($curroption eq '') {
 2009:                 $checked = ' checked="checked"';
 2010:             }
 2011:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2012:                                '<input type="radio" name="crsreq_'.$item.
 2013:                                '__LC_adv" value=""'.$checked.' />'.
 2014:                                &mt('No override set').'</label></span>&nbsp; ';
 2015:             foreach my $option (@options) {
 2016:                 my $val = $option;
 2017:                 if ($option eq 'norequest') {
 2018:                     $val = 0;
 2019:                 }
 2020:                 if ($option eq 'validate') {
 2021:                     my $canvalidate = 0;
 2022:                     if (ref($validations{$item}) eq 'HASH') {
 2023:                         if ($validations{$item}{'_LC_adv'}) {
 2024:                             $canvalidate = 1;
 2025:                         }
 2026:                     }
 2027:                     next if (!$canvalidate);
 2028:                 }
 2029:                 my $checked = '';
 2030:                 if ($val eq $curroption) {
 2031:                     $checked = ' checked="checked"';
 2032:                 } elsif ($option eq 'autolimit') {
 2033:                     if ($curroption =~ /^autolimit/) {
 2034:                         $checked = ' checked="checked"';
 2035:                     }
 2036:                 }
 2037:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2038:                                   '<input type="radio" name="crsreq_'.$item.
 2039:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2040:                                   $titles{$option}.'</label>';
 2041:                 if ($option eq 'autolimit') {
 2042:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2043:                                        $item.'_limit__LC_adv" size="1" '.
 2044:                                        'value="'.$currlimit.'" />';
 2045:                 }
 2046:                 $advcell{$item} .= '</span> ';
 2047:                 if ($option eq 'autolimit') {
 2048:                     $advcell{$item} .= $titles{'unlimited'};
 2049:                 }
 2050:             }
 2051:         } elsif ($context eq 'requestauthor') {
 2052:             my $curroption;
 2053:             if (ref($settings) eq 'HASH') {
 2054:                 $curroption = $settings->{'_LC_adv'};
 2055:             }
 2056:             my $checked = '';
 2057:             if ($curroption eq '') {
 2058:                 $checked = ' checked="checked"';
 2059:             }
 2060:             $datatable .= '<span class="LC_nobreak"><label>'.
 2061:                           '<input type="radio" name="authorreq__LC_adv"'.
 2062:                           ' value=""'.$checked.' />'.
 2063:                           &mt('No override set').'</label></span>&nbsp; ';
 2064:             foreach my $option (@options) {
 2065:                 my $val = $option;
 2066:                 if ($option eq 'norequest') {
 2067:                     $val = 0;
 2068:                 }
 2069:                 my $checked = '';
 2070:                 if ($val eq $curroption) {
 2071:                     $checked = ' checked="checked"';
 2072:                 }
 2073:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2074:                               '<input type="radio" name="authorreq__LC_adv"'.
 2075:                               ' value="'.$val.'"'.$checked.' />'.
 2076:                               $titles{$option}.'</label></span>&nbsp; ';
 2077:             }
 2078:         } else {
 2079:             my $checked = 'checked="checked" ';
 2080:             if (ref($settings) eq 'HASH') {
 2081:                 if (ref($settings->{$item}) eq 'HASH') {
 2082:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2083:                         $checked = '';
 2084:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2085:                         $checked = 'checked="checked" ';
 2086:                     }
 2087:                 }
 2088:             }
 2089:             $datatable .= '<span class="LC_nobreak"><label>'.
 2090:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2091:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2092:                           '</label></span>&nbsp; ';
 2093:         }
 2094:     }
 2095:     if ($context eq 'requestcourses') {
 2096:         $datatable .= '</tr><tr>';
 2097:         foreach my $item (@usertools) {
 2098:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2099:         }
 2100:         $datatable .= '</tr></table>';
 2101:     }
 2102:     $datatable .= '</td></tr>';
 2103:     $$rowtotal += $typecount;
 2104:     return $datatable;
 2105: }
 2106: 
 2107: sub print_requestmail {
 2108:     my ($dom,$action,$settings,$rowtotal) = @_;
 2109:     my ($now,$datatable,%currapp);
 2110:     $now = time;
 2111:     if (ref($settings) eq 'HASH') {
 2112:         if (ref($settings->{'notify'}) eq 'HASH') {
 2113:             if ($settings->{'notify'}{'approval'} ne '') {
 2114:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2115:             }
 2116:         }
 2117:     }
 2118:     my $numinrow = 2;
 2119:     my $css_class;
 2120:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2121:     my $text;
 2122:     if ($action eq 'requestcourses') {
 2123:         $text = &mt('Receive notification of course requests requiring approval');
 2124:     } elsif ($action eq 'requestauthor') {
 2125:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2126:     } else {
 2127:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2128:     }
 2129:     $datatable = '<tr'.$css_class.'>'.
 2130:                  ' <td>'.$text.'</td>'.
 2131:                  ' <td class="LC_left_item">';
 2132:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2133:                                                  $action.'notifyapproval',%currapp);
 2134:     if ($numdc > 0) {
 2135:         $datatable .= $table;
 2136:     } else {
 2137:         $datatable .= &mt('There are no active Domain Coordinators');
 2138:     }
 2139:     $datatable .='</td></tr>';
 2140:     return $datatable;
 2141: }
 2142: 
 2143: sub print_studentcode {
 2144:     my ($settings,$rowtotal) = @_;
 2145:     my $rownum = 0; 
 2146:     my ($output,%current);
 2147:     my @crstypes = ('official','unofficial','community','textbook');
 2148:     if (ref($settings) eq 'HASH') {
 2149:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2150:             foreach my $type (@crstypes) {
 2151:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2152:             }
 2153:         }
 2154:     }
 2155:     $output .= '<tr>'.
 2156:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2157:                '<td class="LC_left_item">';
 2158:     foreach my $type (@crstypes) {
 2159:         my $check = ' ';
 2160:         if ($current{$type}) {
 2161:             $check = ' checked="checked" ';
 2162:         }
 2163:         $output .= '<span class="LC_nobreak"><label>'.
 2164:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2165:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2166:     }
 2167:     $output .= '</td></tr>';
 2168:     $$rowtotal ++;
 2169:     return $output;
 2170: }
 2171: 
 2172: sub print_textbookcourses {
 2173:     my ($dom,$type,$settings,$rowtotal) = @_;
 2174:     my $rownum = 0;
 2175:     my $css_class;
 2176:     my $itemcount = 1;
 2177:     my $maxnum = 0;
 2178:     my $bookshash;
 2179:     if (ref($settings) eq 'HASH') {
 2180:         $bookshash = $settings->{$type};
 2181:     }
 2182:     my %ordered;
 2183:     if (ref($bookshash) eq 'HASH') {
 2184:         foreach my $item (keys(%{$bookshash})) {
 2185:             if (ref($bookshash->{$item}) eq 'HASH') {
 2186:                 my $num = $bookshash->{$item}{'order'};
 2187:                 $ordered{$num} = $item;
 2188:             }
 2189:         }
 2190:     }
 2191:     my $confname = $dom.'-domainconfig';
 2192:     my $switchserver = &check_switchserver($dom,$confname);
 2193:     my $maxnum = scalar(keys(%ordered));
 2194:     my $datatable;
 2195:     if (keys(%ordered)) {
 2196:         my @items = sort { $a <=> $b } keys(%ordered);
 2197:         for (my $i=0; $i<@items; $i++) {
 2198:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2199:             my $key = $ordered{$items[$i]};
 2200:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2201:             my $coursetitle = $coursehash{'description'};
 2202:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2203:             if (ref($bookshash->{$key}) eq 'HASH') {
 2204:                 $subject = $bookshash->{$key}->{'subject'};
 2205:                 $title = $bookshash->{$key}->{'title'};
 2206:                 if ($type eq 'textbooks') {
 2207:                     $publisher = $bookshash->{$key}->{'publisher'};
 2208:                     $author = $bookshash->{$key}->{'author'};
 2209:                     $image = $bookshash->{$key}->{'image'};
 2210:                     if ($image ne '') {
 2211:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2212:                         my $imagethumb = "$path/tn-".$imagefile;
 2213:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2214:                     }
 2215:                 }
 2216:             }
 2217:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2218:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2219:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2220:             for (my $k=0; $k<=$maxnum; $k++) {
 2221:                 my $vpos = $k+1;
 2222:                 my $selstr;
 2223:                 if ($k == $i) {
 2224:                     $selstr = ' selected="selected" ';
 2225:                 }
 2226:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2227:             }
 2228:             $datatable .= '</select>'.('&nbsp;'x2).
 2229:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2230:                 &mt('Delete?').'</label></span></td>'.
 2231:                 '<td colspan="2">'.
 2232:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2233:                 ('&nbsp;'x2).
 2234:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2235:             if ($type eq 'textbooks') {
 2236:                 $datatable .= ('&nbsp;'x2).
 2237:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2238:                               ('&nbsp;'x2).
 2239:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2240:                               ('&nbsp;'x2).
 2241:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2242:                 if ($image) {
 2243:                     $datatable .= '<span class="LC_nobreak">'.
 2244:                                   $imgsrc.
 2245:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2246:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2247:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2248:                 }
 2249:                 if ($switchserver) {
 2250:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2251:                 } else {
 2252:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2253:                 }
 2254:             }
 2255:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2256:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2257:                           $coursetitle.'</span></td></tr>'."\n";
 2258:             $itemcount ++;
 2259:         }
 2260:     }
 2261:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2262:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2263:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2264:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2265:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2266:     for (my $k=0; $k<$maxnum+1; $k++) {
 2267:         my $vpos = $k+1;
 2268:         my $selstr;
 2269:         if ($k == $maxnum) {
 2270:             $selstr = ' selected="selected" ';
 2271:         }
 2272:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2273:     }
 2274:     $datatable .= '</select>&nbsp;'."\n".
 2275:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2276:                   '<td colspan="2">'.
 2277:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2278:                   ('&nbsp;'x2).
 2279:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2280:                   ('&nbsp;'x2);
 2281:     if ($type eq 'textbooks') {
 2282:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2283:                       ('&nbsp;'x2).
 2284:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2285:                       ('&nbsp;'x2).
 2286:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2287:         if ($switchserver) {
 2288:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2289:         } else {
 2290:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2291:         }
 2292:     }
 2293:     $datatable .= '</span>'."\n".
 2294:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2295:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2296:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2297:                   &Apache::loncommon::selectcourse_link
 2298:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2299:                   '</span></td>'."\n".
 2300:                   '</tr>'."\n";
 2301:     $itemcount ++;
 2302:     return $datatable;
 2303: }
 2304: 
 2305: sub textbookcourses_javascript {
 2306:     my ($settings) = @_;
 2307:     return unless(ref($settings) eq 'HASH');
 2308:     my (%ordered,%total,%jstext);
 2309:     foreach my $type ('textbooks','templates') {
 2310:         $total{$type} = 0;
 2311:         if (ref($settings->{$type}) eq 'HASH') {
 2312:             foreach my $item (keys(%{$settings->{$type}})) {
 2313:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2314:                     my $num = $settings->{$type}->{$item}{'order'};
 2315:                     $ordered{$type}{$num} = $item;
 2316:                 }
 2317:             }
 2318:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2319:         }
 2320:         my @jsarray = ();
 2321:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2322:             push(@jsarray,$ordered{$type}{$item});
 2323:         }
 2324:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2325:     }
 2326:     return <<"ENDSCRIPT";
 2327: <script type="text/javascript">
 2328: // <![CDATA[
 2329: function reorderBooks(form,item,caller) {
 2330:     var changedVal;
 2331: $jstext{'textbooks'};
 2332: $jstext{'templates'};
 2333:     var newpos;
 2334:     var maxh;
 2335:     if (caller == 'textbooks') {  
 2336:         newpos = 'textbooks_addbook_pos';
 2337:         maxh = 1 + $total{'textbooks'};
 2338:     } else {
 2339:         newpos = 'templates_addbook_pos';
 2340:         maxh = 1 + $total{'templates'};
 2341:     }
 2342:     var current = new Array;
 2343:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2344:     if (item == newpos) {
 2345:         changedVal = newitemVal;
 2346:     } else {
 2347:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2348:         current[newitemVal] = newpos;
 2349:     }
 2350:     if (caller == 'textbooks') {
 2351:         for (var i=0; i<textbooks.length; i++) {
 2352:             var elementName = 'textbooks_'+textbooks[i];
 2353:             if (elementName != item) {
 2354:                 if (form.elements[elementName]) {
 2355:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2356:                     current[currVal] = elementName;
 2357:                 }
 2358:             }
 2359:         }
 2360:     }
 2361:     if (caller == 'templates') {
 2362:         for (var i=0; i<templates.length; i++) {
 2363:             var elementName = 'templates_'+templates[i];
 2364:             if (elementName != item) {
 2365:                 if (form.elements[elementName]) {
 2366:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2367:                     current[currVal] = elementName;
 2368:                 }
 2369:             }
 2370:         }
 2371:     }
 2372:     var oldVal;
 2373:     for (var j=0; j<maxh; j++) {
 2374:         if (current[j] == undefined) {
 2375:             oldVal = j;
 2376:         }
 2377:     }
 2378:     if (oldVal < changedVal) {
 2379:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2380:            var elementName = current[k];
 2381:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2382:         }
 2383:     } else {
 2384:         for (var k=changedVal; k<oldVal; k++) {
 2385:             var elementName = current[k];
 2386:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2387:         }
 2388:     }
 2389:     return;
 2390: }
 2391: 
 2392: // ]]>
 2393: </script>
 2394: 
 2395: ENDSCRIPT
 2396: }
 2397: 
 2398: sub print_autoenroll {
 2399:     my ($dom,$settings,$rowtotal) = @_;
 2400:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2401:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2402:     if (ref($settings) eq 'HASH') {
 2403:         if (exists($settings->{'run'})) {
 2404:             if ($settings->{'run'} eq '0') {
 2405:                 $runoff = ' checked="checked" ';
 2406:                 $runon = ' ';
 2407:             } else {
 2408:                 $runon = ' checked="checked" ';
 2409:                 $runoff = ' ';
 2410:             }
 2411:         } else {
 2412:             if ($autorun) {
 2413:                 $runon = ' checked="checked" ';
 2414:                 $runoff = ' ';
 2415:             } else {
 2416:                 $runoff = ' checked="checked" ';
 2417:                 $runon = ' ';
 2418:             }
 2419:         }
 2420:         if (exists($settings->{'co-owners'})) {
 2421:             if ($settings->{'co-owners'} eq '0') {
 2422:                 $coownersoff = ' checked="checked" ';
 2423:                 $coownerson = ' ';
 2424:             } else {
 2425:                 $coownerson = ' checked="checked" ';
 2426:                 $coownersoff = ' ';
 2427:             }
 2428:         } else {
 2429:             $coownersoff = ' checked="checked" ';
 2430:             $coownerson = ' ';
 2431:         }
 2432:         if (exists($settings->{'sender_domain'})) {
 2433:             $defdom = $settings->{'sender_domain'};
 2434:         }
 2435:         if (exists($settings->{'autofailsafe'})) {
 2436:             $failsafe = $settings->{'autofailsafe'};
 2437:         }
 2438:     } else {
 2439:         if ($autorun) {
 2440:             $runon = ' checked="checked" ';
 2441:             $runoff = ' ';
 2442:         } else {
 2443:             $runoff = ' checked="checked" ';
 2444:             $runon = ' ';
 2445:         }
 2446:     }
 2447:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2448:     my $notif_sender;
 2449:     if (ref($settings) eq 'HASH') {
 2450:         $notif_sender = $settings->{'sender_uname'};
 2451:     }
 2452:     my $datatable='<tr class="LC_odd_row">'.
 2453:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2454:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2455:                   '<input type="radio" name="autoenroll_run"'.
 2456:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2457:                   '<label><input type="radio" name="autoenroll_run"'.
 2458:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2459:                   '</tr><tr>'.
 2460:                   '<td>'.&mt('Notification messages - sender').
 2461:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2462:                   &mt('username').':&nbsp;'.
 2463:                   '<input type="text" name="sender_uname" value="'.
 2464:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2465:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2466:                   '<tr class="LC_odd_row">'.
 2467:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2468:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2469:                   '<input type="radio" name="autoassign_coowners"'.
 2470:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2471:                   '<label><input type="radio" name="autoassign_coowners"'.
 2472:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2473:                   '</tr><tr>'.
 2474:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2475:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2476:                   '<input type="text" name="autoenroll_failsafe"'.
 2477:                   ' value="'.$failsafe.'" size="4" /></td></tr>';
 2478:     $$rowtotal += 4;
 2479:     return $datatable;
 2480: }
 2481: 
 2482: sub print_autoupdate {
 2483:     my ($position,$dom,$settings,$rowtotal) = @_;
 2484:     my $datatable;
 2485:     if ($position eq 'top') {
 2486:         my $updateon = ' ';
 2487:         my $updateoff = ' checked="checked" ';
 2488:         my $classlistson = ' ';
 2489:         my $classlistsoff = ' checked="checked" ';
 2490:         if (ref($settings) eq 'HASH') {
 2491:             if ($settings->{'run'} eq '1') {
 2492:                 $updateon = $updateoff;
 2493:                 $updateoff = ' ';
 2494:             }
 2495:             if ($settings->{'classlists'} eq '1') {
 2496:                 $classlistson = $classlistsoff;
 2497:                 $classlistsoff = ' ';
 2498:             }
 2499:         }
 2500:         my %title = (
 2501:                    run => 'Auto-update active?',
 2502:                    classlists => 'Update information in classlists?',
 2503:                     );
 2504:         $datatable = '<tr class="LC_odd_row">'. 
 2505:                   '<td>'.&mt($title{'run'}).'</td>'.
 2506:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2507:                   '<input type="radio" name="autoupdate_run"'.
 2508:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2509:                   '<label><input type="radio" name="autoupdate_run"'.
 2510:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2511:                   '</tr><tr>'.
 2512:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2513:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2514:                   '<label><input type="radio" name="classlists"'.
 2515:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2516:                   '<label><input type="radio" name="classlists"'.
 2517:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2518:                   '</tr>';
 2519:         $$rowtotal += 2;
 2520:     } elsif ($position eq 'middle') {
 2521:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2522:         my $numinrow = 3;
 2523:         my $locknamesettings;
 2524:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2525:                                      $dom,$numinrow,$othertitle,
 2526:                                     'lockablenames');
 2527:         $$rowtotal ++;
 2528:     } else {
 2529:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2530:         my @fields = ('lastname','firstname','middlename','generation',
 2531:                       'permanentemail','id');
 2532:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2533:         my $numrows = 0;
 2534:         if (ref($types) eq 'ARRAY') {
 2535:             if (@{$types} > 0) {
 2536:                 $datatable = 
 2537:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2538:                                          \@fields,$types,\$numrows);
 2539:                     $$rowtotal += @{$types}; 
 2540:             }
 2541:         }
 2542:         $datatable .= 
 2543:             &usertype_update_row($settings,{'default' => $othertitle},
 2544:                                  \%fieldtitles,\@fields,['default'],
 2545:                                  \$numrows);
 2546:         $$rowtotal ++;     
 2547:     }
 2548:     return $datatable;
 2549: }
 2550: 
 2551: sub print_autocreate {
 2552:     my ($dom,$settings,$rowtotal) = @_;
 2553:     my (%createon,%createoff,%currhash);
 2554:     my @types = ('xml','req');
 2555:     if (ref($settings) eq 'HASH') {
 2556:         foreach my $item (@types) {
 2557:             $createoff{$item} = ' checked="checked" ';
 2558:             $createon{$item} = ' ';
 2559:             if (exists($settings->{$item})) {
 2560:                 if ($settings->{$item}) {
 2561:                     $createon{$item} = ' checked="checked" ';
 2562:                     $createoff{$item} = ' ';
 2563:                 }
 2564:             }
 2565:         }
 2566:         if ($settings->{'xmldc'} ne '') {
 2567:             $currhash{$settings->{'xmldc'}} = 1;
 2568:         }
 2569:     } else {
 2570:         foreach my $item (@types) {
 2571:             $createoff{$item} = ' checked="checked" ';
 2572:             $createon{$item} = ' ';
 2573:         }
 2574:     }
 2575:     $$rowtotal += 2;
 2576:     my $numinrow = 2;
 2577:     my $datatable='<tr class="LC_odd_row">'.
 2578:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2579:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2580:                   '<input type="radio" name="autocreate_xml"'.
 2581:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2582:                   '<label><input type="radio" name="autocreate_xml"'.
 2583:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2584:                   '</td></tr><tr>'.
 2585:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2586:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2587:                   '<input type="radio" name="autocreate_req"'.
 2588:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2589:                   '<label><input type="radio" name="autocreate_req"'.
 2590:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2591:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2592:                                                    'autocreate_xmldc',%currhash);
 2593:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2594:     if ($numdc > 1) {
 2595:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2596:                       '</td><td class="LC_left_item">';
 2597:     } else {
 2598:         $datatable .= &mt('Course creation processed as:').
 2599:                       '</td><td class="LC_right_item">';
 2600:     }
 2601:     $datatable .= $dctable.'</td></tr>';
 2602:     $$rowtotal += $rows;
 2603:     return $datatable;
 2604: }
 2605: 
 2606: sub print_directorysrch {
 2607:     my ($dom,$settings,$rowtotal) = @_;
 2608:     my $srchon = ' ';
 2609:     my $srchoff = ' checked="checked" ';
 2610:     my ($exacton,$containson,$beginson);
 2611:     my $localon = ' ';
 2612:     my $localoff = ' checked="checked" ';
 2613:     if (ref($settings) eq 'HASH') {
 2614:         if ($settings->{'available'} eq '1') {
 2615:             $srchon = $srchoff;
 2616:             $srchoff = ' ';
 2617:         }
 2618:         if ($settings->{'localonly'} eq '1') {
 2619:             $localon = $localoff;
 2620:             $localoff = ' ';
 2621:         }
 2622:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2623:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2624:                 if ($type eq 'exact') {
 2625:                     $exacton = ' checked="checked" ';
 2626:                 } elsif ($type eq 'contains') {
 2627:                     $containson = ' checked="checked" ';
 2628:                 } elsif ($type eq 'begins') {
 2629:                     $beginson = ' checked="checked" ';
 2630:                 }
 2631:             }
 2632:         } else {
 2633:             if ($settings->{'searchtypes'} eq 'exact') {
 2634:                 $exacton = ' checked="checked" ';
 2635:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2636:                 $containson = ' checked="checked" ';
 2637:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2638:                 $exacton = ' checked="checked" ';
 2639:                 $containson = ' checked="checked" ';
 2640:             }
 2641:         }
 2642:     }
 2643:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2644:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2645: 
 2646:     my $numinrow = 4;
 2647:     my $cansrchrow = 0;
 2648:     my $datatable='<tr class="LC_odd_row">'.
 2649:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2650:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2651:                   '<input type="radio" name="dirsrch_available"'.
 2652:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2653:                   '<label><input type="radio" name="dirsrch_available"'.
 2654:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2655:                   '</tr><tr>'.
 2656:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2657:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2658:                   '<input type="radio" name="dirsrch_localonly"'.
 2659:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2660:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2661:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2662:                   '</tr>';
 2663:     $$rowtotal += 2;
 2664:     if (ref($usertypes) eq 'HASH') {
 2665:         if (keys(%{$usertypes}) > 0) {
 2666:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2667:                                          $numinrow,$othertitle,'cansearch');
 2668:             $cansrchrow = 1;
 2669:         }
 2670:     }
 2671:     if ($cansrchrow) {
 2672:         $$rowtotal ++;
 2673:         $datatable .= '<tr>';
 2674:     } else {
 2675:         $datatable .= '<tr class="LC_odd_row">';
 2676:     }
 2677:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2678:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2679:     foreach my $title (@{$titleorder}) {
 2680:         if (defined($searchtitles->{$title})) {
 2681:             my $check = ' ';
 2682:             if (ref($settings) eq 'HASH') {
 2683:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2684:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2685:                         $check = ' checked="checked" ';
 2686:                     }
 2687:                 }
 2688:             }
 2689:             $datatable .= '<td class="LC_left_item">'.
 2690:                           '<span class="LC_nobreak"><label>'.
 2691:                           '<input type="checkbox" name="searchby" '.
 2692:                           'value="'.$title.'"'.$check.'/>'.
 2693:                           $searchtitles->{$title}.'</label></span></td>';
 2694:         }
 2695:     }
 2696:     $datatable .= '</tr></table></td></tr>';
 2697:     $$rowtotal ++;
 2698:     if ($cansrchrow) {
 2699:         $datatable .= '<tr class="LC_odd_row">';
 2700:     } else {
 2701:         $datatable .= '<tr>';
 2702:     }
 2703:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2704:                   '<td class="LC_left_item" colspan="2">'.
 2705:                   '<span class="LC_nobreak"><label>'.
 2706:                   '<input type="checkbox" name="searchtypes" '.
 2707:                   $exacton.' value="exact" />'.&mt('Exact match').
 2708:                   '</label>&nbsp;'.
 2709:                   '<label><input type="checkbox" name="searchtypes" '.
 2710:                   $beginson.' value="begins" />'.&mt('Begins with').
 2711:                   '</label>&nbsp;'.
 2712:                   '<label><input type="checkbox" name="searchtypes" '.
 2713:                   $containson.' value="contains" />'.&mt('Contains').
 2714:                   '</label></span></td></tr>';
 2715:     $$rowtotal ++;
 2716:     return $datatable;
 2717: }
 2718: 
 2719: sub print_contacts {
 2720:     my ($dom,$settings,$rowtotal) = @_;
 2721:     my $datatable;
 2722:     my @contacts = ('adminemail','supportemail');
 2723:     my (%checked,%to,%otheremails,%bccemails);
 2724:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2725:                     'requestsmail','updatesmail','idconflictsmail');
 2726:     foreach my $type (@mailings) {
 2727:         $otheremails{$type} = '';
 2728:     }
 2729:     $bccemails{'helpdeskmail'} = '';
 2730:     if (ref($settings) eq 'HASH') {
 2731:         foreach my $item (@contacts) {
 2732:             if (exists($settings->{$item})) {
 2733:                 $to{$item} = $settings->{$item};
 2734:             }
 2735:         }
 2736:         foreach my $type (@mailings) {
 2737:             if (exists($settings->{$type})) {
 2738:                 if (ref($settings->{$type}) eq 'HASH') {
 2739:                     foreach my $item (@contacts) {
 2740:                         if ($settings->{$type}{$item}) {
 2741:                             $checked{$type}{$item} = ' checked="checked" ';
 2742:                         }
 2743:                     }
 2744:                     $otheremails{$type} = $settings->{$type}{'others'};
 2745:                     if ($type eq 'helpdeskmail') {
 2746:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2747:                     }
 2748:                 }
 2749:             } elsif ($type eq 'lonstatusmail') {
 2750:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2751:             }
 2752:         }
 2753:     } else {
 2754:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2755:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2756:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2757:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2758:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2759:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2760:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2761:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2762:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2763:     }
 2764:     my ($titles,$short_titles) = &contact_titles();
 2765:     my $rownum = 0;
 2766:     my $css_class;
 2767:     foreach my $item (@contacts) {
 2768:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2769:         $datatable .= '<tr'.$css_class.'>'. 
 2770:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2771:                   '</span></td><td class="LC_right_item">'.
 2772:                   '<input type="text" name="'.$item.'" value="'.
 2773:                   $to{$item}.'" /></td></tr>';
 2774:         $rownum ++;
 2775:     }
 2776:     foreach my $type (@mailings) {
 2777:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2778:         $datatable .= '<tr'.$css_class.'>'.
 2779:                       '<td><span class="LC_nobreak">'.
 2780:                       $titles->{$type}.': </span></td>'.
 2781:                       '<td class="LC_left_item">'.
 2782:                       '<span class="LC_nobreak">';
 2783:         foreach my $item (@contacts) {
 2784:             $datatable .= '<label>'.
 2785:                           '<input type="checkbox" name="'.$type.'"'.
 2786:                           $checked{$type}{$item}.
 2787:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2788:                           '</label>&nbsp;';
 2789:         }
 2790:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2791:                       '<input type="text" name="'.$type.'_others" '.
 2792:                       'value="'.$otheremails{$type}.'"  />';
 2793:         if ($type eq 'helpdeskmail') {
 2794:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2795:                           '<input type="text" name="'.$type.'_bcc" '.
 2796:                           'value="'.$bccemails{$type}.'"  />';
 2797:         }
 2798:         $datatable .= '</td></tr>'."\n";
 2799:         $rownum ++;
 2800:     }
 2801:     my %choices;
 2802:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2803:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2804:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2805:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2806:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2807:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2808:     my @toggles = ('reporterrors','reportupdates');
 2809:     my %defaultchecked = ('reporterrors'  => 'on',
 2810:                           'reportupdates' => 'on');
 2811:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2812:                                                \%choices,$rownum);
 2813:     $datatable .= $reports;
 2814:     $$rowtotal += $rownum;
 2815:     return $datatable;
 2816: }
 2817: 
 2818: sub print_helpsettings {
 2819:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2820:     my ($datatable,$itemcount);
 2821:     $itemcount = 1;
 2822:     my (%choices,%defaultchecked,@toggles);
 2823:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2824:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2825:                                  &mt('LON-CAPA bug tracker'),600,500));
 2826:     %defaultchecked = ('submitbugs' => 'on');
 2827:     @toggles = ('submitbugs',);
 2828: 
 2829:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2830:                                                  \%choices,$itemcount);
 2831:     return $datatable;
 2832: }
 2833: 
 2834: sub radiobutton_prefs {
 2835:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2836:         $additional,$align) = @_;
 2837:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2838:                    (ref($choices) eq 'HASH'));
 2839: 
 2840:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2841: 
 2842:     foreach my $item (@{$toggles}) {
 2843:         if ($defaultchecked->{$item} eq 'on') {
 2844:             $checkedon{$item} = ' checked="checked" ';
 2845:             $checkedoff{$item} = ' ';
 2846:         } elsif ($defaultchecked->{$item} eq 'off') {
 2847:             $checkedoff{$item} = ' checked="checked" ';
 2848:             $checkedon{$item} = ' ';
 2849:         }
 2850:     }
 2851:     if (ref($settings) eq 'HASH') {
 2852:         foreach my $item (@{$toggles}) {
 2853:             if ($settings->{$item} eq '1') {
 2854:                 $checkedon{$item} =  ' checked="checked" ';
 2855:                 $checkedoff{$item} = ' ';
 2856:             } elsif ($settings->{$item} eq '0') {
 2857:                 $checkedoff{$item} =  ' checked="checked" ';
 2858:                 $checkedon{$item} = ' ';
 2859:             }
 2860:         }
 2861:     }
 2862:     if ($onclick) {
 2863:         $onclick = ' onclick="'.$onclick.'"';
 2864:     }
 2865:     foreach my $item (@{$toggles}) {
 2866:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2867:         $datatable .=
 2868:             '<tr'.$css_class.'><td valign="top">'.
 2869:             '<span class="LC_nobreak">'.$choices->{$item}.
 2870:             '</span></td>';
 2871:         if ($align eq 'left') {
 2872:             $datatable .= '<td class="LC_left_item">';
 2873:         } else {
 2874:             $datatable .= '<td class="LC_right_item">';
 2875:         }
 2876:         $datatable .=
 2877:             '<span class="LC_nobreak">'.
 2878:             '<label><input type="radio" name="'.
 2879:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2880:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2881:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2882:             '</span>'.$additional.
 2883:             '</td>'.
 2884:             '</tr>';
 2885:         $itemcount ++;
 2886:     }
 2887:     return ($datatable,$itemcount);
 2888: }
 2889: 
 2890: sub print_coursedefaults {
 2891:     my ($position,$dom,$settings,$rowtotal) = @_;
 2892:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2893:     my $itemcount = 1;
 2894:     my %choices =  &Apache::lonlocal::texthash (
 2895:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2896:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2897:         coursecredits        => 'Credits can be specified for courses',
 2898:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 2899:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 2900:         postsubmit           => 'Disable submit button/keypress following student submission',
 2901:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 2902:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 2903:     );
 2904:     my %staticdefaults = (
 2905:                            anonsurvey_threshold => 10,
 2906:                            uploadquota          => 500,
 2907:                            postsubmit           => 60,
 2908:                            mysqltables          => 172800,
 2909:                          );
 2910:     if ($position eq 'top') {
 2911:         %defaultchecked = (
 2912:                             'uselcmath'       => 'on',
 2913:                             'usejsme'         => 'on',
 2914:                             'canclone'        => 'none',
 2915:                           );
 2916:         @toggles = ('uselcmath','usejsme');
 2917:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2918:                                                      \%choices,$itemcount);
 2919:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2920:         $datatable .=
 2921:             '<tr'.$css_class.'><td valign="top">'.
 2922:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 2923:             '</span></td><td class="LC_left_item">';
 2924:         my $currcanclone = 'none';
 2925:         my $onclick;
 2926:         my @cloneoptions = ('none','domain');
 2927:         my %clonetitles = (
 2928:                              none     => 'No additional course requesters',
 2929:                              domain   => "Any course requester in course's domain",
 2930:                              instcode => 'Course requests for official courses ...',
 2931:                           );
 2932:         my (%codedefaults,@code_order,@posscodes);
 2933:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 2934:                                                     \@code_order) eq 'ok') {
 2935:             if (@code_order > 0) {
 2936:                 push(@cloneoptions,'instcode');
 2937:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 2938:             }
 2939:         }
 2940:         if (ref($settings) eq 'HASH') {
 2941:             if ($settings->{'canclone'}) {
 2942:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 2943:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 2944:                         if (@code_order > 0) {
 2945:                             $currcanclone = 'instcode';
 2946:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 2947:                         }
 2948:                     }
 2949:                 } elsif ($settings->{'canclone'} eq 'domain') {
 2950:                     $currcanclone = $settings->{'canclone'};
 2951:                 }
 2952:             }
 2953:         }
 2954:         foreach my $option (@cloneoptions) {
 2955:             my ($checked,$additional);
 2956:             if ($currcanclone eq $option) {
 2957:                 $checked = ' checked="checked"';
 2958:             }
 2959:             if ($option eq 'instcode') {
 2960:                 if (@code_order) {
 2961:                     my $show = 'none';
 2962:                     if ($checked) {
 2963:                         $show = 'block';
 2964:                     }
 2965:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 2966:                                   &mt('Institutional codes for new and cloned course have identical:').
 2967:                                   '<br />';
 2968:                     foreach my $item (@code_order) {
 2969:                         my $codechk;
 2970:                         if ($checked) {
 2971:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 2972:                                 $codechk = ' checked="checked"';
 2973:                             }
 2974:                         }
 2975:                         $additional .= '<label>'.
 2976:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 2977:                                        $item.'</label>';
 2978:                     }
 2979:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 2980:                 }
 2981:             }
 2982:             $datatable .=
 2983:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 2984:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 2985:                 '</label>&nbsp;'.$additional.'</span><br />';
 2986:         }
 2987:         $datatable .= '</td>'.
 2988:                       '</tr>';
 2989:         $itemcount ++;
 2990:     } else {
 2991:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2992:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 2993:         my $currusecredits = 0;
 2994:         my $postsubmitclient = 1;
 2995:         my @types = ('official','unofficial','community','textbook');
 2996:         if (ref($settings) eq 'HASH') {
 2997:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2998:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2999:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 3000:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 3001:                 }
 3002:             }
 3003:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 3004:                 foreach my $type (@types) {
 3005:                     next if ($type eq 'community');
 3006:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 3007:                     if ($defcredits{$type} ne '') {
 3008:                         $currusecredits = 1;
 3009:                     }
 3010:                 }
 3011:             }
 3012:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 3013:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 3014:                     $postsubmitclient = 0;
 3015:                     foreach my $type (@types) {
 3016:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3017:                     }
 3018:                 } else {
 3019:                     foreach my $type (@types) {
 3020:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 3021:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 3022:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 3023:                             } else {
 3024:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3025:                             }
 3026:                         } else {
 3027:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3028:                         }
 3029:                     }
 3030:                 }
 3031:             } else {
 3032:                 foreach my $type (@types) {
 3033:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3034:                 }
 3035:             }
 3036:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 3037:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 3038:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 3039:                 }
 3040:             } else {
 3041:                 foreach my $type (@types) {
 3042:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 3043:                 }
 3044:             }
 3045:         } else {
 3046:             foreach my $type (@types) {
 3047:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3048:             }
 3049:         }
 3050:         if (!$currdefresponder) {
 3051:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 3052:         } elsif ($currdefresponder < 1) {
 3053:             $currdefresponder = 1;
 3054:         }
 3055:         foreach my $type (@types) {
 3056:             if ($curruploadquota{$type} eq '') {
 3057:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 3058:             }
 3059:         }
 3060:         $datatable .=
 3061:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3062:                 $choices{'anonsurvey_threshold'}.
 3063:                 '</span></td>'.
 3064:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 3065:                 '<input type="text" name="anonsurvey_threshold"'.
 3066:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 3067:                 '</td></tr>'."\n";
 3068:         $itemcount ++;
 3069:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3070:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3071:                       $choices{'uploadquota'}.
 3072:                       '</span></td>'.
 3073:                       '<td align="right" class="LC_right_item">'.
 3074:                       '<table><tr>';
 3075:         foreach my $type (@types) {
 3076:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 3077:                            '<input type="text" name="uploadquota_'.$type.'"'.
 3078:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 3079:         }
 3080:         $datatable .= '</tr></table></td></tr>'."\n";
 3081:         $itemcount ++;
 3082:         my $onclick = "toggleDisplay(this.form,'credits');";
 3083:         my $display = 'none';
 3084:         if ($currusecredits) {
 3085:             $display = 'block';
 3086:         }
 3087:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 3088:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 3089:         foreach my $type (@types) {
 3090:             next if ($type eq 'community');
 3091:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 3092:                            '<input type="text" name="'.$type.'_credits"'.
 3093:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 3094:         }
 3095:         $additional .= '</tr></table></div>'."\n";
 3096:         %defaultchecked = ('coursecredits' => 'off');
 3097:         @toggles = ('coursecredits');
 3098:         my $current = {
 3099:                         'coursecredits' => $currusecredits,
 3100:                       };
 3101:         (my $table,$itemcount) =
 3102:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 3103:                                \%choices,$itemcount,$onclick,$additional,'left');
 3104:         $datatable .= $table;
 3105:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 3106:         my $display = 'none';
 3107:         if ($postsubmitclient) {
 3108:             $display = 'block';
 3109:         }
 3110:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 3111:                       &mt('Number of seconds submit is disabled').'<br />'.
 3112:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 3113:                       '<table><tr>';
 3114:         foreach my $type (@types) {
 3115:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 3116:                            '<input type="text" name="'.$type.'_timeout" value="'.
 3117:                            $deftimeout{$type}.'" size="5" /></td>';
 3118:         }
 3119:         $additional .= '</tr></table></div>'."\n";
 3120:         %defaultchecked = ('postsubmit' => 'on');
 3121:         @toggles = ('postsubmit');
 3122:         $current = {
 3123:                        'postsubmit' => $postsubmitclient,
 3124:                    };
 3125:         ($table,$itemcount) =
 3126:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 3127:                                \%choices,$itemcount,$onclick,$additional,'left');
 3128:         $datatable .= $table;
 3129:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3130:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3131:                       $choices{'mysqltables'}.
 3132:                       '</span></td>'.
 3133:                       '<td align="right" class="LC_right_item">'.
 3134:                       '<table><tr>';
 3135:         foreach my $type (@types) {
 3136:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 3137:                            '<input type="text" name="mysqltables_'.$type.'"'.
 3138:                            ' value="'.$currmysql{$type}.'" size="5" /></td>';
 3139:         }
 3140:         $datatable .= '</tr></table></td></tr>'."\n";
 3141:         $itemcount ++;
 3142: 
 3143:     }
 3144:     $$rowtotal += $itemcount;
 3145:     return $datatable;
 3146: }
 3147: 
 3148: sub print_selfenrollment {
 3149:     my ($position,$dom,$settings,$rowtotal) = @_;
 3150:     my ($css_class,$datatable);
 3151:     my $itemcount = 1;
 3152:     my @types = ('official','unofficial','community','textbook');
 3153:     if (($position eq 'top') || ($position eq 'middle')) {
 3154:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 3155:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 3156:         my @rows;
 3157:         my $key;
 3158:         if ($position eq 'top') {
 3159:             $key = 'admin'; 
 3160:             if (ref($rowsref) eq 'ARRAY') {
 3161:                 @rows = @{$rowsref};
 3162:             }
 3163:         } elsif ($position eq 'middle') {
 3164:             $key = 'default';
 3165:             @rows = ('types','registered','approval','limit');
 3166:         }
 3167:         foreach my $row (@rows) {
 3168:             if (defined($titlesref->{$row})) {
 3169:                 $itemcount ++;
 3170:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3171:                 $datatable .= '<tr'.$css_class.'>'.
 3172:                               '<td>'.$titlesref->{$row}.'</td>'.
 3173:                               '<td class="LC_left_item">'.
 3174:                               '<table><tr>';
 3175:                 my (%current,%currentcap);
 3176:                 if (ref($settings) eq 'HASH') {
 3177:                     if (ref($settings->{$key}) eq 'HASH') {
 3178:                         foreach my $type (@types) {
 3179:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 3180:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 3181:                             }
 3182:                             if (($row eq 'limit') && ($key eq 'default')) {
 3183:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 3184:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 3185:                                 }
 3186:                             }
 3187:                         }
 3188:                     }
 3189:                 }
 3190:                 my %roles = (
 3191:                              '0' => &Apache::lonnet::plaintext('dc'),
 3192:                             ); 
 3193:             
 3194:                 foreach my $type (@types) {
 3195:                     unless (($row eq 'registered') && ($key eq 'default')) {
 3196:                         $datatable .= '<th>'.&mt($type).'</th>';
 3197:                     }
 3198:                 }
 3199:                 unless (($row eq 'registered') && ($key eq 'default')) {
 3200:                     $datatable .= '</tr><tr>';
 3201:                 }
 3202:                 foreach my $type (@types) {
 3203:                     if ($type eq 'community') {
 3204:                         $roles{'1'} = &mt('Community personnel');
 3205:                     } else {
 3206:                         $roles{'1'} = &mt('Course personnel');
 3207:                     }
 3208:                     $datatable .= '<td style="vertical-align: top">';
 3209:                     if ($position eq 'top') {
 3210:                         my %checked;
 3211:                         if ($current{$type} eq '0') {
 3212:                             $checked{'0'} = ' checked="checked"';
 3213:                         } else {
 3214:                             $checked{'1'} = ' checked="checked"';
 3215:                         }
 3216:                         foreach my $role ('1','0') {
 3217:                             $datatable .= '<span class="LC_nobreak"><label>'.
 3218:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 3219:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 3220:                                           $roles{$role}.'</label></span> ';
 3221:                         }
 3222:                     } else {
 3223:                         if ($row eq 'types') {
 3224:                             my %checked;
 3225:                             if ($current{$type} =~ /^(all|dom)$/) {
 3226:                                 $checked{$1} = ' checked="checked"';
 3227:                             } else {
 3228:                                 $checked{''} = ' checked="checked"';
 3229:                             }
 3230:                             foreach my $val ('','dom','all') {
 3231:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3232:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3233:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3234:                             }
 3235:                         } elsif ($row eq 'registered') {
 3236:                             my %checked;
 3237:                             if ($current{$type} eq '1') {
 3238:                                 $checked{'1'} = ' checked="checked"';
 3239:                             } else {
 3240:                                 $checked{'0'} = ' checked="checked"';
 3241:                             }
 3242:                             foreach my $val ('0','1') {
 3243:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3244:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3245:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3246:                             }
 3247:                         } elsif ($row eq 'approval') {
 3248:                             my %checked;
 3249:                             if ($current{$type} =~ /^([12])$/) {
 3250:                                 $checked{$1} = ' checked="checked"';
 3251:                             } else {
 3252:                                 $checked{'0'} = ' checked="checked"';
 3253:                             }
 3254:                             for my $val (0..2) {
 3255:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3256:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3257:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3258:                             }
 3259:                         } elsif ($row eq 'limit') {
 3260:                             my %checked;
 3261:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 3262:                                 $checked{$1} = ' checked="checked"';
 3263:                             } else {
 3264:                                 $checked{'none'} = ' checked="checked"';
 3265:                             }
 3266:                             my $cap;
 3267:                             if ($currentcap{$type} =~ /^\d+$/) {
 3268:                                 $cap = $currentcap{$type};
 3269:                             }
 3270:                             foreach my $val ('none','allstudents','selfenrolled') {
 3271:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3272:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3273:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3274:                             }
 3275:                             $datatable .= '<br />'.
 3276:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 3277:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 3278:                                           '</span>'; 
 3279:                         }
 3280:                     }
 3281:                     $datatable .= '</td>';
 3282:                 }
 3283:                 $datatable .= '</tr>';
 3284:             }
 3285:             $datatable .= '</table></td></tr>';
 3286:         }
 3287:     } elsif ($position eq 'bottom') {
 3288:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 3289:     }
 3290:     $$rowtotal += $itemcount;
 3291:     return $datatable;
 3292: }
 3293: 
 3294: sub print_validation_rows {
 3295:     my ($caller,$dom,$settings,$rowtotal) = @_;
 3296:     my ($itemsref,$namesref,$fieldsref);
 3297:     if ($caller eq 'selfenroll') { 
 3298:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 3299:     } elsif ($caller eq 'requestcourses') {
 3300:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 3301:     }
 3302:     my %currvalidation;
 3303:     if (ref($settings) eq 'HASH') {
 3304:         if (ref($settings->{'validation'}) eq 'HASH') {
 3305:             %currvalidation = %{$settings->{'validation'}};
 3306:         }
 3307:     }
 3308:     my $datatable;
 3309:     my $itemcount = 0;
 3310:     foreach my $item (@{$itemsref}) {
 3311:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3312:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3313:                       $namesref->{$item}.
 3314:                       '</span></td>'.
 3315:                       '<td class="LC_left_item">';
 3316:         if (($item eq 'url') || ($item eq 'button')) {
 3317:             $datatable .= '<span class="LC_nobreak">'.
 3318:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 3319:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 3320:         } elsif ($item eq 'fields') {
 3321:             my @currfields;
 3322:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 3323:                 @currfields = @{$currvalidation{$item}};
 3324:             }
 3325:             foreach my $field (@{$fieldsref}) {
 3326:                 my $check = '';
 3327:                 if (grep(/^\Q$field\E$/,@currfields)) {
 3328:                     $check = ' checked="checked"';
 3329:                 }
 3330:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3331:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 3332:                               ' value="'.$field.'"'.$check.' />'.$field.
 3333:                               '</label></span> ';
 3334:             }
 3335:         } elsif ($item eq 'markup') {
 3336:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 3337:                            $currvalidation{$item}.
 3338:                               '</textarea>';
 3339:         }
 3340:         $datatable .= '</td></tr>'."\n";
 3341:         if (ref($rowtotal)) {
 3342:             $itemcount ++;
 3343:         }
 3344:     }
 3345:     if ($caller eq 'requestcourses') {
 3346:         my %currhash;
 3347:         if (ref($settings) eq 'HASH') {
 3348:             if (ref($settings->{'validation'}) eq 'HASH') {
 3349:                 if ($settings->{'validation'}{'dc'} ne '') {
 3350:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 3351:                 }
 3352:             }
 3353:         }
 3354:         my $numinrow = 2;
 3355:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3356:                                                        'validationdc',%currhash);
 3357:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3358:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 3359:         if ($numdc > 1) {
 3360:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 3361:         } else {
 3362:             $datatable .=  &mt('Course creation processed as: ');
 3363:         }
 3364:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 3365:         $itemcount ++;
 3366:     }
 3367:     if (ref($rowtotal)) {
 3368:         $$rowtotal += $itemcount;
 3369:     }
 3370:     return $datatable;
 3371: }
 3372: 
 3373: sub print_usersessions {
 3374:     my ($position,$dom,$settings,$rowtotal) = @_;
 3375:     my ($css_class,$datatable,%checked,%choices);
 3376:     my (%by_ip,%by_location,@intdoms);
 3377:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 3378: 
 3379:     my @alldoms = &Apache::lonnet::all_domains();
 3380:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 3381:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3382:     my %altids = &id_for_thisdom(%servers);
 3383:     my $itemcount = 1;
 3384:     if ($position eq 'top') {
 3385:         if (keys(%serverhomes) > 1) {
 3386:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 3387:             my $curroffloadnow;
 3388:             if (ref($settings) eq 'HASH') {
 3389:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 3390:                     $curroffloadnow = $settings->{'offloadnow'};
 3391:                 }
 3392:             }
 3393:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 3394:         } else {
 3395:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3396:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 3397:         }
 3398:     } else {
 3399:         if (keys(%by_location) == 0) {
 3400:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3401:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 3402:         } else {
 3403:             my %lt = &usersession_titles();
 3404:             my $numinrow = 5;
 3405:             my $prefix;
 3406:             my @types;
 3407:             if ($position eq 'bottom') {
 3408:                 $prefix = 'remote';
 3409:                 @types = ('version','excludedomain','includedomain');
 3410:             } else {
 3411:                 $prefix = 'hosted';
 3412:                 @types = ('excludedomain','includedomain');
 3413:             }
 3414:             my (%current,%checkedon,%checkedoff);
 3415:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 3416:             my @locations = sort(keys(%by_location));
 3417:             foreach my $type (@types) {
 3418:                 $checkedon{$type} = '';
 3419:                 $checkedoff{$type} = ' checked="checked"';
 3420:             }
 3421:             if (ref($settings) eq 'HASH') {
 3422:                 if (ref($settings->{$prefix}) eq 'HASH') {
 3423:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 3424:                         $current{$key} = $settings->{$prefix}{$key};
 3425:                         if ($key eq 'version') {
 3426:                             if ($current{$key} ne '') {
 3427:                                 $checkedon{$key} = ' checked="checked"';
 3428:                                 $checkedoff{$key} = '';
 3429:                             }
 3430:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 3431:                             $checkedon{$key} = ' checked="checked"';
 3432:                             $checkedoff{$key} = '';
 3433:                         }
 3434:                     }
 3435:                 }
 3436:             }
 3437:             foreach my $type (@types) {
 3438:                 next if ($type ne 'version' && !@locations);
 3439:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3440:                 $datatable .= '<tr'.$css_class.'>
 3441:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 3442:                                <span class="LC_nobreak">&nbsp;
 3443:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 3444:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 3445:                 if ($type eq 'version') {
 3446:                     my $selector = '<select name="'.$prefix.'_version">';
 3447:                     foreach my $version (@lcversions) {
 3448:                         my $selected = '';
 3449:                         if ($current{'version'} eq $version) {
 3450:                             $selected = ' selected="selected"';
 3451:                         }
 3452:                         $selector .= ' <option value="'.$version.'"'.
 3453:                                      $selected.'>'.$version.'</option>';
 3454:                     }
 3455:                     $selector .= '</select> ';
 3456:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 3457:                 } else {
 3458:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 3459:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 3460:                                  ' />'.('&nbsp;'x2).
 3461:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 3462:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 3463:                                  "\n".
 3464:                                  '</div><div><table>';
 3465:                     my $rem;
 3466:                     for (my $i=0; $i<@locations; $i++) {
 3467:                         my ($showloc,$value,$checkedtype);
 3468:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 3469:                             my $ip = $by_location{$locations[$i]}->[0];
 3470:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 3471:                                  $value = join(':',@{$by_ip{$ip}});
 3472:                                 $showloc = join(', ',@{$by_ip{$ip}});
 3473:                                 if (ref($current{$type}) eq 'ARRAY') {
 3474:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 3475:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 3476:                                             $checkedtype = ' checked="checked"';
 3477:                                             last;
 3478:                                         }
 3479:                                     }
 3480:                                 }
 3481:                             }
 3482:                         }
 3483:                         $rem = $i%($numinrow);
 3484:                         if ($rem == 0) {
 3485:                             if ($i > 0) {
 3486:                                 $datatable .= '</tr>';
 3487:                             }
 3488:                             $datatable .= '<tr>';
 3489:                         }
 3490:                         $datatable .= '<td class="LC_left_item">'.
 3491:                                       '<span class="LC_nobreak"><label>'.
 3492:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 3493:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 3494:                                       '</label></span></td>';
 3495:                     }
 3496:                     $rem = @locations%($numinrow);
 3497:                     my $colsleft = $numinrow - $rem;
 3498:                     if ($colsleft > 1 ) {
 3499:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3500:                                       '&nbsp;</td>';
 3501:                     } elsif ($colsleft == 1) {
 3502:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3503:                     }
 3504:                     $datatable .= '</tr></table>';
 3505:                 }
 3506:                 $datatable .= '</td></tr>';
 3507:                 $itemcount ++;
 3508:             }
 3509:         }
 3510:     }
 3511:     $$rowtotal += $itemcount;
 3512:     return $datatable;
 3513: }
 3514: 
 3515: sub build_location_hashes {
 3516:     my ($intdoms,$by_ip,$by_location) = @_;
 3517:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 3518:                   (ref($by_location) eq 'HASH')); 
 3519:     my %iphost = &Apache::lonnet::get_iphost();
 3520:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 3521:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 3522:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 3523:         foreach my $id (@{$iphost{$primary_ip}}) {
 3524:             my $intdom = &Apache::lonnet::internet_dom($id);
 3525:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 3526:                 push(@{$intdoms},$intdom);
 3527:             }
 3528:         }
 3529:     }
 3530:     foreach my $ip (keys(%iphost)) {
 3531:         if (ref($iphost{$ip}) eq 'ARRAY') {
 3532:             foreach my $id (@{$iphost{$ip}}) {
 3533:                 my $location = &Apache::lonnet::internet_dom($id);
 3534:                 if ($location) {
 3535:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 3536:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3537:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 3538:                             push(@{$by_ip->{$ip}},$location);
 3539:                         }
 3540:                     } else {
 3541:                         $by_ip->{$ip} = [$location];
 3542:                     }
 3543:                 }
 3544:             }
 3545:         }
 3546:     }
 3547:     foreach my $ip (sort(keys(%{$by_ip}))) {
 3548:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3549:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 3550:             my $first = $by_ip->{$ip}->[0];
 3551:             if (ref($by_location->{$first}) eq 'ARRAY') {
 3552:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 3553:                     push(@{$by_location->{$first}},$ip);
 3554:                 }
 3555:             } else {
 3556:                 $by_location->{$first} = [$ip];
 3557:             }
 3558:         }
 3559:     }
 3560:     return;
 3561: }
 3562: 
 3563: sub current_offloads_to {
 3564:     my ($dom,$settings,$servers) = @_;
 3565:     my (%spareid,%otherdomconfigs);
 3566:     if (ref($servers) eq 'HASH') {
 3567:         foreach my $lonhost (sort(keys(%{$servers}))) {
 3568:             my $gotspares;
 3569:             if (ref($settings) eq 'HASH') {
 3570:                 if (ref($settings->{'spares'}) eq 'HASH') {
 3571:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 3572:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 3573:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 3574:                         $gotspares = 1;
 3575:                     }
 3576:                 }
 3577:             }
 3578:             unless ($gotspares) {
 3579:                 my $gotspares;
 3580:                 my $serverhomeID =
 3581:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 3582:                 my $serverhomedom =
 3583:                     &Apache::lonnet::host_domain($serverhomeID);
 3584:                 if ($serverhomedom ne $dom) {
 3585:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 3586:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3587:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3588:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3589:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3590:                                 $gotspares = 1;
 3591:                             }
 3592:                         }
 3593:                     } else {
 3594:                         $otherdomconfigs{$serverhomedom} =
 3595:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 3596:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 3597:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3598:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3599:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 3600:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3601:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3602:                                         $gotspares = 1;
 3603:                                     }
 3604:                                 }
 3605:                             }
 3606:                         }
 3607:                     }
 3608:                 }
 3609:             }
 3610:             unless ($gotspares) {
 3611:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3612:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3613:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3614:                } else {
 3615:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 3616:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 3617:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3618:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3619:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3620:                     } else {
 3621:                         my %what = (
 3622:                              spareid => 1,
 3623:                         );
 3624:                         my ($result,$returnhash) = 
 3625:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 3626:                         if ($result eq 'ok') { 
 3627:                             if (ref($returnhash) eq 'HASH') {
 3628:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 3629:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 3630:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 3631:                                 }
 3632:                             }
 3633:                         }
 3634:                     }
 3635:                 }
 3636:             }
 3637:         }
 3638:     }
 3639:     return %spareid;
 3640: }
 3641: 
 3642: sub spares_row {
 3643:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 3644:     my $css_class;
 3645:     my $numinrow = 4;
 3646:     my $itemcount = 1;
 3647:     my $datatable;
 3648:     my %typetitles = &sparestype_titles();
 3649:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3650:         foreach my $server (sort(keys(%{$servers}))) {
 3651:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 3652:             my ($othercontrol,$serverdom);
 3653:             if ($serverhome ne $server) {
 3654:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 3655:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3656:             } else {
 3657:                 $serverdom = &Apache::lonnet::host_domain($server);
 3658:                 if ($serverdom ne $dom) {
 3659:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3660:                 }
 3661:             }
 3662:             next unless (ref($spareid->{$server}) eq 'HASH');
 3663:             my $checkednow;
 3664:             if (ref($curroffloadnow) eq 'HASH') {
 3665:                 if ($curroffloadnow->{$server}) {
 3666:                     $checkednow = ' checked="checked"';
 3667:                 }
 3668:             }
 3669:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3670:             $datatable .= '<tr'.$css_class.'>
 3671:                            <td rowspan="2">
 3672:                             <span class="LC_nobreak">'.
 3673:                           &mt('[_1] when busy, offloads to:'
 3674:                               ,'<b>'.$server.'</b>').'</span><br />'.
 3675:                           '<span class="LC_nobreak">'."\n".
 3676:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 3677:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 3678:                           "\n";
 3679:             my (%current,%canselect);
 3680:             my @choices = 
 3681:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 3682:             foreach my $type ('primary','default') {
 3683:                 if (ref($spareid->{$server}) eq 'HASH') {
 3684:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 3685:                         my @spares = @{$spareid->{$server}{$type}};
 3686:                         if (@spares > 0) {
 3687:                             if ($othercontrol) {
 3688:                                 $current{$type} = join(', ',@spares);
 3689:                             } else {
 3690:                                 $current{$type} .= '<table>';
 3691:                                 my $numspares = scalar(@spares);
 3692:                                 for (my $i=0;  $i<@spares; $i++) {
 3693:                                     my $rem = $i%($numinrow);
 3694:                                     if ($rem == 0) {
 3695:                                         if ($i > 0) {
 3696:                                             $current{$type} .= '</tr>';
 3697:                                         }
 3698:                                         $current{$type} .= '<tr>';
 3699:                                     }
 3700:                                     $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;'.
 3701:                                                        $spareid->{$server}{$type}[$i].
 3702:                                                        '</label></td>'."\n";
 3703:                                 }
 3704:                                 my $rem = @spares%($numinrow);
 3705:                                 my $colsleft = $numinrow - $rem;
 3706:                                 if ($colsleft > 1 ) {
 3707:                                     $current{$type} .= '<td colspan="'.$colsleft.
 3708:                                                        '" class="LC_left_item">'.
 3709:                                                        '&nbsp;</td>';
 3710:                                 } elsif ($colsleft == 1) {
 3711:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 3712:                                 }
 3713:                                 $current{$type} .= '</tr></table>';
 3714:                             }
 3715:                         }
 3716:                     }
 3717:                     if ($current{$type} eq '') {
 3718:                         $current{$type} = &mt('None specified');
 3719:                     }
 3720:                     if ($othercontrol) {
 3721:                         if ($type eq 'primary') {
 3722:                             $canselect{$type} = $othercontrol;
 3723:                         }
 3724:                     } else {
 3725:                         $canselect{$type} = 
 3726:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 3727:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 3728:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 3729:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 3730:                         if (@choices > 0) {
 3731:                             foreach my $lonhost (@choices) {
 3732:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 3733:                             }
 3734:                         }
 3735:                         $canselect{$type} .= '</select>'."\n";
 3736:                     }
 3737:                 } else {
 3738:                     $current{$type} = &mt('Could not be determined');
 3739:                     if ($type eq 'primary') {
 3740:                         $canselect{$type} =  $othercontrol;
 3741:                     }
 3742:                 }
 3743:                 if ($type eq 'default') {
 3744:                     $datatable .= '<tr'.$css_class.'>';
 3745:                 }
 3746:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 3747:                               '<td>'.$current{$type}.'</td>'."\n".
 3748:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 3749:             }
 3750:             $itemcount ++;
 3751:         }
 3752:     }
 3753:     $$rowtotal += $itemcount;
 3754:     return $datatable;
 3755: }
 3756: 
 3757: sub possible_newspares {
 3758:     my ($server,$currspares,$serverhomes,$altids) = @_;
 3759:     my $serverhostname = &Apache::lonnet::hostname($server);
 3760:     my %excluded;
 3761:     if ($serverhostname ne '') {
 3762:         %excluded = (
 3763:                        $serverhostname => 1,
 3764:                     );
 3765:     }
 3766:     if (ref($currspares) eq 'HASH') {
 3767:         foreach my $type (keys(%{$currspares})) {
 3768:             if (ref($currspares->{$type}) eq 'ARRAY') {
 3769:                 if (@{$currspares->{$type}} > 0) {
 3770:                     foreach my $curr (@{$currspares->{$type}}) {
 3771:                         my $hostname = &Apache::lonnet::hostname($curr);
 3772:                         $excluded{$hostname} = 1;
 3773:                     }
 3774:                 }
 3775:             }
 3776:         }
 3777:     }
 3778:     my @choices;
 3779:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3780:         if (keys(%{$serverhomes}) > 1) {
 3781:             foreach my $name (sort(keys(%{$serverhomes}))) {
 3782:                 unless ($excluded{$name}) {
 3783:                     if (exists($altids->{$serverhomes->{$name}})) {
 3784:                         push(@choices,$altids->{$serverhomes->{$name}});
 3785:                     } else {
 3786:                         push(@choices,$serverhomes->{$name});
 3787:                     }
 3788:                 }
 3789:             }
 3790:         }
 3791:     }
 3792:     return sort(@choices);
 3793: }
 3794: 
 3795: sub print_loadbalancing {
 3796:     my ($dom,$settings,$rowtotal) = @_;
 3797:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 3798:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 3799:     my $numinrow = 1;
 3800:     my $datatable;
 3801:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3802:     my (%currbalancer,%currtargets,%currrules,%existing);
 3803:     if (ref($settings) eq 'HASH') {
 3804:         %existing = %{$settings};
 3805:     }
 3806:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 3807:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 3808:                                   \%currtargets,\%currrules);
 3809:     } else {
 3810:         return;
 3811:     }
 3812:     my ($othertitle,$usertypes,$types) =
 3813:         &Apache::loncommon::sorted_inst_types($dom);
 3814:     my $rownum = 8;
 3815:     if (ref($types) eq 'ARRAY') {
 3816:         $rownum += scalar(@{$types});
 3817:     }
 3818:     my @css_class = ('LC_odd_row','LC_even_row');
 3819:     my $balnum = 0;
 3820:     my $islast;
 3821:     my (@toshow,$disabledtext);
 3822:     if (keys(%currbalancer) > 0) {
 3823:         @toshow = sort(keys(%currbalancer));
 3824:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3825:             push(@toshow,'');
 3826:         }
 3827:     } else {
 3828:         @toshow = ('');
 3829:         $disabledtext = &mt('No existing load balancer');
 3830:     }
 3831:     foreach my $lonhost (@toshow) {
 3832:         if ($balnum == scalar(@toshow)-1) {
 3833:             $islast = 1;
 3834:         } else {
 3835:             $islast = 0;
 3836:         }
 3837:         my $cssidx = $balnum%2;
 3838:         my $targets_div_style = 'display: none';
 3839:         my $disabled_div_style = 'display: block';
 3840:         my $homedom_div_style = 'display: none';
 3841:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3842:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3843:                       '<p>';
 3844:         if ($lonhost eq '') {
 3845:             $datatable .= '<span class="LC_nobreak">';
 3846:             if (keys(%currbalancer) > 0) {
 3847:                 $datatable .= &mt('Add balancer:');
 3848:             } else {
 3849:                 $datatable .= &mt('Enable balancer:');
 3850:             }
 3851:             $datatable .= '&nbsp;'.
 3852:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3853:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3854:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3855:                           '<option value="" selected="selected">'.&mt('None').
 3856:                           '</option>'."\n";
 3857:             foreach my $server (sort(keys(%servers))) {
 3858:                 next if ($currbalancer{$server});
 3859:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3860:             }
 3861:             $datatable .=
 3862:                 '</select>'."\n".
 3863:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3864:         } else {
 3865:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3866:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3867:                            &mt('Stop balancing').'</label>'.
 3868:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3869:             $targets_div_style = 'display: block';
 3870:             $disabled_div_style = 'display: none';
 3871:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3872:                 $homedom_div_style = 'display: block';
 3873:             }
 3874:         }
 3875:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3876:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3877:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3878:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3879:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3880:         my @sparestypes = ('primary','default');
 3881:         my %typetitles = &sparestype_titles();
 3882:         foreach my $sparetype (@sparestypes) {
 3883:             my $targettable;
 3884:             for (my $i=0; $i<$numspares; $i++) {
 3885:                 my $checked;
 3886:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3887:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3888:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3889:                             $checked = ' checked="checked"';
 3890:                         }
 3891:                     }
 3892:                 }
 3893:                 my ($chkboxval,$disabled);
 3894:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3895:                     $chkboxval = $spares[$i];
 3896:                 }
 3897:                 if (exists($currbalancer{$spares[$i]})) {
 3898:                     $disabled = ' disabled="disabled"';
 3899:                 }
 3900:                 $targettable .=
 3901:                     '<td><span class="LC_nobreak"><label>'.
 3902:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3903:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3904:                     '</span></label></span></td>';
 3905:                 my $rem = $i%($numinrow);
 3906:                 if ($rem == 0) {
 3907:                     if (($i > 0) && ($i < $numspares-1)) {
 3908:                         $targettable .= '</tr>';
 3909:                     }
 3910:                     if ($i < $numspares-1) {
 3911:                         $targettable .= '<tr>';
 3912:                     }
 3913:                 }
 3914:             }
 3915:             if ($targettable ne '') {
 3916:                 my $rem = $numspares%($numinrow);
 3917:                 my $colsleft = $numinrow - $rem;
 3918:                 if ($colsleft > 1 ) {
 3919:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3920:                                     '&nbsp;</td>';
 3921:                 } elsif ($colsleft == 1) {
 3922:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3923:                 }
 3924:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3925:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3926:             }
 3927:         }
 3928:         $datatable .= '</div></td></tr>'.
 3929:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3930:                                            $othertitle,$usertypes,$types,\%servers,
 3931:                                            \%currbalancer,$lonhost,
 3932:                                            $targets_div_style,$homedom_div_style,
 3933:                                            $css_class[$cssidx],$balnum,$islast);
 3934:         $$rowtotal += $rownum;
 3935:         $balnum ++;
 3936:     }
 3937:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3938:     return $datatable;
 3939: }
 3940: 
 3941: sub get_loadbalancers_config {
 3942:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3943:     return unless ((ref($servers) eq 'HASH') &&
 3944:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3945:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3946:     if (keys(%{$existing}) > 0) {
 3947:         my $oldlonhost;
 3948:         foreach my $key (sort(keys(%{$existing}))) {
 3949:             if ($key eq 'lonhost') {
 3950:                 $oldlonhost = $existing->{'lonhost'};
 3951:                 $currbalancer->{$oldlonhost} = 1;
 3952:             } elsif ($key eq 'targets') {
 3953:                 if ($oldlonhost) {
 3954:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3955:                 }
 3956:             } elsif ($key eq 'rules') {
 3957:                 if ($oldlonhost) {
 3958:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3959:                 }
 3960:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3961:                 $currbalancer->{$key} = 1;
 3962:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3963:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3964:             }
 3965:         }
 3966:     } else {
 3967:         my ($balancerref,$targetsref) =
 3968:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3969:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3970:             foreach my $server (sort(keys(%{$balancerref}))) {
 3971:                 $currbalancer->{$server} = 1;
 3972:                 $currtargets->{$server} = $targetsref->{$server};
 3973:             }
 3974:         }
 3975:     }
 3976:     return;
 3977: }
 3978: 
 3979: sub loadbalancing_rules {
 3980:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3981:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3982:         $css_class,$balnum,$islast) = @_;
 3983:     my $output;
 3984:     my $num = 0;
 3985:     my ($alltypes,$othertypes,$titles) =
 3986:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3987:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3988:         foreach my $type (@{$alltypes}) {
 3989:             $num ++;
 3990:             my $current;
 3991:             if (ref($currrules) eq 'HASH') {
 3992:                 $current = $currrules->{$type};
 3993:             }
 3994:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3995:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3996:                     $current = '';
 3997:                 }
 3998:             }
 3999:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 4000:                                              $servers,$currbalancer,$lonhost,$dom,
 4001:                                              $targets_div_style,$homedom_div_style,
 4002:                                              $css_class,$balnum,$num,$islast);
 4003:         }
 4004:     }
 4005:     return $output;
 4006: }
 4007: 
 4008: sub loadbalancing_titles {
 4009:     my ($dom,$intdom,$usertypes,$types) = @_;
 4010:     my %othertypes = (
 4011:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 4012:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 4013:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 4014:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 4015:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 4016:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 4017:                      );
 4018:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 4019:     if (ref($types) eq 'ARRAY') {
 4020:         unshift(@alltypes,@{$types},'default');
 4021:     }
 4022:     my %titles;
 4023:     foreach my $type (@alltypes) {
 4024:         if ($type =~ /^_LC_/) {
 4025:             $titles{$type} = $othertypes{$type};
 4026:         } elsif ($type eq 'default') {
 4027:             $titles{$type} = &mt('All users from [_1]',$dom);
 4028:             if (ref($types) eq 'ARRAY') {
 4029:                 if (@{$types} > 0) {
 4030:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 4031:                 }
 4032:             }
 4033:         } elsif (ref($usertypes) eq 'HASH') {
 4034:             $titles{$type} = $usertypes->{$type};
 4035:         }
 4036:     }
 4037:     return (\@alltypes,\%othertypes,\%titles);
 4038: }
 4039: 
 4040: sub loadbalance_rule_row {
 4041:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 4042:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 4043:     my @rulenames;
 4044:     my %ruletitles = &offloadtype_text();
 4045:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 4046:         @rulenames = ('balancer','offloadedto','specific');
 4047:     } else {
 4048:         @rulenames = ('default','homeserver');
 4049:         if ($type eq '_LC_external') {
 4050:             push(@rulenames,'externalbalancer');
 4051:         } else {
 4052:             push(@rulenames,'specific');
 4053:         }
 4054:         push(@rulenames,'none');
 4055:     }
 4056:     my $style = $targets_div_style;
 4057:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 4058:         $style = $homedom_div_style;
 4059:     }
 4060:     my $space;
 4061:     if ($islast && $num == 1) {
 4062:         $space = '<div display="inline-block">&nbsp;</div>';
 4063:     }
 4064:     my $output =
 4065:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 4066:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 4067:         '<td valaign="top">'.$space.
 4068:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 4069:     for (my $i=0; $i<@rulenames; $i++) {
 4070:         my $rule = $rulenames[$i];
 4071:         my ($checked,$extra);
 4072:         if ($rulenames[$i] eq 'default') {
 4073:             $rule = '';
 4074:         }
 4075:         if ($rulenames[$i] eq 'specific') {
 4076:             if (ref($servers) eq 'HASH') {
 4077:                 my $default;
 4078:                 if (($current ne '') && (exists($servers->{$current}))) {
 4079:                     $checked = ' checked="checked"';
 4080:                 }
 4081:                 unless ($checked) {
 4082:                     $default = ' selected="selected"';
 4083:                 }
 4084:                 $extra =
 4085:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 4086:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 4087:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 4088:                     '<option value=""'.$default.'></option>'."\n";
 4089:                 foreach my $server (sort(keys(%{$servers}))) {
 4090:                     if (ref($currbalancer) eq 'HASH') {
 4091:                         next if (exists($currbalancer->{$server}));
 4092:                     }
 4093:                     my $selected;
 4094:                     if ($server eq $current) {
 4095:                         $selected = ' selected="selected"';
 4096:                     }
 4097:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 4098:                 }
 4099:                 $extra .= '</select>';
 4100:             }
 4101:         } elsif ($rule eq $current) {
 4102:             $checked = ' checked="checked"';
 4103:         }
 4104:         $output .= '<span class="LC_nobreak"><label>'.
 4105:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 4106:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 4107:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 4108:                    ')"'.$checked.' />&nbsp;';
 4109:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 4110:             $output .= $ruletitles{'particular'};
 4111:         } else {
 4112:             $output .= $ruletitles{$rulenames[$i]};
 4113:         }
 4114:         $output .= '</label>'.$extra.'</span><br />'."\n";
 4115:     }
 4116:     $output .= '</div></td></tr>'."\n";
 4117:     return $output;
 4118: }
 4119: 
 4120: sub offloadtype_text {
 4121:     my %ruletitles = &Apache::lonlocal::texthash (
 4122:            'default'          => 'Offloads to default destinations',
 4123:            'homeserver'       => "Offloads to user's home server",
 4124:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 4125:            'specific'         => 'Offloads to specific server',
 4126:            'none'             => 'No offload',
 4127:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 4128:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 4129:            'particular'       => 'Session hosted (after re-auth) on server:',
 4130:     );
 4131:     return %ruletitles;
 4132: }
 4133: 
 4134: sub sparestype_titles {
 4135:     my %typestitles = &Apache::lonlocal::texthash (
 4136:                           'primary' => 'primary',
 4137:                           'default' => 'default',
 4138:                       );
 4139:     return %typestitles;
 4140: }
 4141: 
 4142: sub contact_titles {
 4143:     my %titles = &Apache::lonlocal::texthash (
 4144:                    'supportemail' => 'Support E-mail address',
 4145:                    'adminemail'   => 'Default Server Admin E-mail address',
 4146:                    'errormail'    => 'Error reports to be e-mailed to',
 4147:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 4148:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 4149:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 4150:                    'requestsmail' => 'E-mail from course requests requiring approval',
 4151:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 4152:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 4153:                  );
 4154:     my %short_titles = &Apache::lonlocal::texthash (
 4155:                            adminemail   => 'Admin E-mail address',
 4156:                            supportemail => 'Support E-mail',
 4157:                        );   
 4158:     return (\%titles,\%short_titles);
 4159: }
 4160: 
 4161: sub tool_titles {
 4162:     my %titles = &Apache::lonlocal::texthash (
 4163:                      aboutme    => 'Personal web page',
 4164:                      blog       => 'Blog',
 4165:                      webdav     => 'WebDAV',
 4166:                      portfolio  => 'Portfolio',
 4167:                      official   => 'Official courses (with institutional codes)',
 4168:                      unofficial => 'Unofficial courses',
 4169:                      community  => 'Communities',
 4170:                      textbook   => 'Textbook courses',
 4171:                  );
 4172:     return %titles;
 4173: }
 4174: 
 4175: sub courserequest_titles {
 4176:     my %titles = &Apache::lonlocal::texthash (
 4177:                                    official   => 'Official',
 4178:                                    unofficial => 'Unofficial',
 4179:                                    community  => 'Communities',
 4180:                                    textbook   => 'Textbook',
 4181:                                    norequest  => 'Not allowed',
 4182:                                    approval   => 'Approval by Dom. Coord.',
 4183:                                    validate   => 'With validation',
 4184:                                    autolimit  => 'Numerical limit',
 4185:                                    unlimited  => '(blank for unlimited)',
 4186:                  );
 4187:     return %titles;
 4188: }
 4189: 
 4190: sub authorrequest_titles {
 4191:     my %titles = &Apache::lonlocal::texthash (
 4192:                                    norequest  => 'Not allowed',
 4193:                                    approval   => 'Approval by Dom. Coord.',
 4194:                                    automatic  => 'Automatic approval',
 4195:                  );
 4196:     return %titles;
 4197: }
 4198: 
 4199: sub courserequest_conditions {
 4200:     my %conditions = &Apache::lonlocal::texthash (
 4201:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 4202:        validate   => '(Processing of request subject to institutional validation).',
 4203:                  );
 4204:     return %conditions;
 4205: }
 4206: 
 4207: 
 4208: sub print_usercreation {
 4209:     my ($position,$dom,$settings,$rowtotal) = @_;
 4210:     my $numinrow = 4;
 4211:     my $datatable;
 4212:     if ($position eq 'top') {
 4213:         $$rowtotal ++;
 4214:         my $rowcount = 0;
 4215:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 4216:         if (ref($rules) eq 'HASH') {
 4217:             if (keys(%{$rules}) > 0) {
 4218:                 $datatable .= &user_formats_row('username',$settings,$rules,
 4219:                                                 $ruleorder,$numinrow,$rowcount);
 4220:                 $$rowtotal ++;
 4221:                 $rowcount ++;
 4222:             }
 4223:         }
 4224:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 4225:         if (ref($idrules) eq 'HASH') {
 4226:             if (keys(%{$idrules}) > 0) {
 4227:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 4228:                                                 $idruleorder,$numinrow,$rowcount);
 4229:                 $$rowtotal ++;
 4230:                 $rowcount ++;
 4231:             }
 4232:         }
 4233:         if ($rowcount == 0) {
 4234:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 4235:             $$rowtotal ++;
 4236:             $rowcount ++;
 4237:         }
 4238:     } elsif ($position eq 'middle') {
 4239:         my @creators = ('author','course','requestcrs');
 4240:         my ($rules,$ruleorder) =
 4241:             &Apache::lonnet::inst_userrules($dom,'username');
 4242:         my %lt = &usercreation_types();
 4243:         my %checked;
 4244:         if (ref($settings) eq 'HASH') {
 4245:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 4246:                 foreach my $item (@creators) {
 4247:                     $checked{$item} = $settings->{'cancreate'}{$item};
 4248:                 }
 4249:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 4250:                 foreach my $item (@creators) {
 4251:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 4252:                         $checked{$item} = 'none';
 4253:                     }
 4254:                 }
 4255:             }
 4256:         }
 4257:         my $rownum = 0;
 4258:         foreach my $item (@creators) {
 4259:             $rownum ++;
 4260:             if ($checked{$item} eq '') {
 4261:                 $checked{$item} = 'any';
 4262:             }
 4263:             my $css_class;
 4264:             if ($rownum%2) {
 4265:                 $css_class = '';
 4266:             } else {
 4267:                 $css_class = ' class="LC_odd_row" ';
 4268:             }
 4269:             $datatable .= '<tr'.$css_class.'>'.
 4270:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 4271:                          '</span></td><td align="right">';
 4272:             my @options = ('any');
 4273:             if (ref($rules) eq 'HASH') {
 4274:                 if (keys(%{$rules}) > 0) {
 4275:                     push(@options,('official','unofficial'));
 4276:                 }
 4277:             }
 4278:             push(@options,'none');
 4279:             foreach my $option (@options) {
 4280:                 my $type = 'radio';
 4281:                 my $check = ' ';
 4282:                 if ($checked{$item} eq $option) {
 4283:                     $check = ' checked="checked" ';
 4284:                 } 
 4285:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4286:                               '<input type="'.$type.'" name="can_createuser_'.
 4287:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 4288:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 4289:             }
 4290:             $datatable .= '</td></tr>';
 4291:         }
 4292:     } else {
 4293:         my @contexts = ('author','course','domain');
 4294:         my @authtypes = ('int','krb4','krb5','loc');
 4295:         my %checked;
 4296:         if (ref($settings) eq 'HASH') {
 4297:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 4298:                 foreach my $item (@contexts) {
 4299:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 4300:                         foreach my $auth (@authtypes) {
 4301:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 4302:                                 $checked{$item}{$auth} = ' checked="checked" ';
 4303:                             }
 4304:                         }
 4305:                     }
 4306:                 }
 4307:             }
 4308:         } else {
 4309:             foreach my $item (@contexts) {
 4310:                 foreach my $auth (@authtypes) {
 4311:                     $checked{$item}{$auth} = ' checked="checked" ';
 4312:                 }
 4313:             }
 4314:         }
 4315:         my %title = &context_names();
 4316:         my %authname = &authtype_names();
 4317:         my $rownum = 0;
 4318:         my $css_class; 
 4319:         foreach my $item (@contexts) {
 4320:             if ($rownum%2) {
 4321:                 $css_class = '';
 4322:             } else {
 4323:                 $css_class = ' class="LC_odd_row" ';
 4324:             }
 4325:             $datatable .=   '<tr'.$css_class.'>'.
 4326:                             '<td>'.$title{$item}.
 4327:                             '</td><td class="LC_left_item">'.
 4328:                             '<span class="LC_nobreak">';
 4329:             foreach my $auth (@authtypes) {
 4330:                 $datatable .= '<label>'. 
 4331:                               '<input type="checkbox" name="'.$item.'_auth" '.
 4332:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 4333:                               $authname{$auth}.'</label>&nbsp;';
 4334:             }
 4335:             $datatable .= '</span></td></tr>';
 4336:             $rownum ++;
 4337:         }
 4338:         $$rowtotal += $rownum;
 4339:     }
 4340:     return $datatable;
 4341: }
 4342: 
 4343: sub print_selfcreation {
 4344:     my ($position,$dom,$settings,$rowtotal) = @_;
 4345:     my (@selfcreate,$createsettings,$processing,$datatable);
 4346:     if (ref($settings) eq 'HASH') {
 4347:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 4348:             $createsettings = $settings->{'cancreate'};
 4349:             if (ref($createsettings) eq 'HASH') {
 4350:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 4351:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 4352:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 4353:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 4354:                         @selfcreate = ('email','login','sso');
 4355:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 4356:                         @selfcreate = ($createsettings->{'selfcreate'});
 4357:                     }
 4358:                 }
 4359:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 4360:                     $processing = $createsettings->{'selfcreateprocessing'};
 4361:                 }
 4362:             }
 4363:         }
 4364:     }
 4365:     my %radiohash;
 4366:     my $numinrow = 4;
 4367:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 4368:     if ($position eq 'top') {
 4369:         my %choices = &Apache::lonlocal::texthash (
 4370:                                                       cancreate_login      => 'Institutional Login',
 4371:                                                       cancreate_sso        => 'Institutional Single Sign On',
 4372:                                                   );
 4373:         my @toggles = sort(keys(%choices));
 4374:         my %defaultchecked = (
 4375:                                'cancreate_login' => 'off',
 4376:                                'cancreate_sso'   => 'off',
 4377:                              );
 4378:         my ($onclick,$itemcount);
 4379:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4380:                                                      \%choices,$itemcount,$onclick);
 4381:         $$rowtotal += $itemcount;
 4382:         
 4383:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4384: 
 4385:         if (ref($usertypes) eq 'HASH') {
 4386:             if (keys(%{$usertypes}) > 0) {
 4387:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 4388:                                              $dom,$numinrow,$othertitle,
 4389:                                              'statustocreate',$$rowtotal);
 4390:                 $$rowtotal ++;
 4391:             }
 4392:         }
 4393:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 4394:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4395:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 4396:         my $rem;
 4397:         my $numperrow = 2;
 4398:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 4399:         $datatable .= '<tr'.$css_class.'>'.
 4400:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 4401:                      '<td class="LC_left_item">'."\n".
 4402:                      '<table><tr><td>'."\n";
 4403:         for (my $i=0; $i<@fields; $i++) {
 4404:             $rem = $i%($numperrow);
 4405:             if ($rem == 0) {
 4406:                 if ($i > 0) {
 4407:                     $datatable .= '</tr>';
 4408:                 }
 4409:                 $datatable .= '<tr>';
 4410:             }
 4411:             my $currval;
 4412:             if (ref($createsettings) eq 'HASH') {
 4413:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 4414:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 4415:                 }
 4416:             }
 4417:             $datatable .= '<td class="LC_left_item">'.
 4418:                           '<span class="LC_nobreak">'.
 4419:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 4420:                           'value="'.$currval.'" size="10" />&nbsp;'.
 4421:                           $fieldtitles{$fields[$i]}.'</span></td>';
 4422:         }
 4423:         my $colsleft = $numperrow - $rem;
 4424:         if ($colsleft > 1 ) {
 4425:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4426:                          '&nbsp;</td>';
 4427:         } elsif ($colsleft == 1) {
 4428:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4429:         }
 4430:         $datatable .= '</tr></table></td></tr>';
 4431:         $$rowtotal ++;
 4432:     } elsif ($position eq 'middle') {
 4433:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 4434:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4435:         $usertypes->{'default'} = $othertitle;
 4436:         if (ref($types) eq 'ARRAY') {
 4437:             push(@{$types},'default');
 4438:             $usertypes->{'default'} = $othertitle;
 4439:             foreach my $status (@{$types}) {
 4440:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 4441:                                                        $numinrow,$$rowtotal,$usertypes);
 4442:                 $$rowtotal ++;
 4443:             }
 4444:         }
 4445:     } else {
 4446:         my %choices = &Apache::lonlocal::texthash (
 4447:                                                       cancreate_email => 'E-mail address as username',
 4448:                                                   );
 4449:         my @toggles = sort(keys(%choices));
 4450:         my %defaultchecked = (
 4451:                                'cancreate_email' => 'off',
 4452:                              );
 4453:         my $itemcount = 0;
 4454:         my $display = 'none';
 4455:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 4456:             $display = 'block';
 4457:         }
 4458:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 4459:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 4460:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 4461:         my $usertypes = {};
 4462:         my $order = [];
 4463:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 4464:             $usertypes = $domdefaults{'inststatustypes'};
 4465:             $order = $domdefaults{'inststatusguest'};
 4466:         }
 4467:         if (ref($order) eq 'ARRAY') {
 4468:             push(@{$order},'default');
 4469:             if (@{$order} > 1) {
 4470:                 $usertypes->{'default'} = &mt('Other users');
 4471:                 $additional .= '<table><tr>';
 4472:                 foreach my $status (@{$order}) {
 4473:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 4474:                 }
 4475:                 $additional .= '</tr><tr>';
 4476:                 foreach my $status (@{$order}) {
 4477:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 4478:                 }
 4479:                 $additional .= '</tr></table>';
 4480:             } else {
 4481:                 $usertypes->{'default'} = &mt('All users');
 4482:                 $additional .= &email_as_username($rowtotal,$processing);
 4483:             }
 4484:         }
 4485:         $additional .= '</div>'."\n";
 4486: 
 4487:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4488:                                                      \%choices,$$rowtotal,$onclick,$additional);
 4489:         $$rowtotal ++;
 4490:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 4491:         $$rowtotal ++;
 4492:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 4493:         $numinrow = 1;
 4494:         if (ref($order) eq 'ARRAY') {
 4495:             foreach my $status (@{$order}) {
 4496:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 4497:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 4498:                 $$rowtotal ++;
 4499:             }
 4500:         }
 4501:         my ($emailrules,$emailruleorder) =
 4502:             &Apache::lonnet::inst_userrules($dom,'email');
 4503:         if (ref($emailrules) eq 'HASH') {
 4504:             if (keys(%{$emailrules}) > 0) {
 4505:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 4506:                                                 $emailruleorder,$numinrow,$$rowtotal);
 4507:                 $$rowtotal ++;
 4508:             }
 4509:         }
 4510:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 4511:     }
 4512:     return $datatable;
 4513: }
 4514: 
 4515: sub email_as_username {
 4516:     my ($rowtotal,$processing,$type) = @_;
 4517:     my %choices =
 4518:         &Apache::lonlocal::texthash (
 4519:                                       automatic => 'Automatic approval',
 4520:                                       approval  => 'Queued for approval',
 4521:                                     );
 4522:     my $output;
 4523:     foreach my $option ('automatic','approval') {
 4524:         my $checked;
 4525:         if (ref($processing) eq 'HASH') {
 4526:             if ($type eq '') {   
 4527:                 if (!exists($processing->{'default'})) {
 4528:                     if ($option eq 'automatic') {
 4529:                         $checked = ' checked="checked"';
 4530:                     }
 4531:                 } else {
 4532:                     if ($processing->{'default'} eq $option) {
 4533:                         $checked = ' checked="checked"';
 4534:                     }
 4535:                 }
 4536:             } else {
 4537:                 if (!exists($processing->{$type})) {
 4538:                     if ($option eq 'automatic') {
 4539:                         $checked = ' checked="checked"';
 4540:                     }
 4541:                 } else {
 4542:                     if ($processing->{$type} eq $option) {
 4543:                         $checked = ' checked="checked"';
 4544:                     }
 4545:                 }
 4546:             }
 4547:         } elsif ($option eq 'automatic') {
 4548:             $checked = ' checked="checked"'; 
 4549:         }
 4550:         my $name = 'cancreate_emailprocess';
 4551:         if (($type ne '') && ($type ne 'default')) {
 4552:             $name .= '_'.$type;
 4553:         }
 4554:         $output .= '<span class="LC_nobreak"><label>'.
 4555:                    '<input type="radio" name="'.$name.'"'.
 4556:                    $checked.' value="'.$option.'" />'.
 4557:                    $choices{$option}.'</label></span>';
 4558:         if ($type eq '') {
 4559:             $output .= '&nbsp;';
 4560:         } else {
 4561:             $output .= '<br />';
 4562:         }
 4563:     }
 4564:     $$rowtotal ++;
 4565:     return $output;
 4566: }
 4567: 
 4568: sub captcha_choice {
 4569:     my ($context,$settings,$itemcount) = @_;
 4570:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 4571:         $vertext,$currver); 
 4572:     my %lt = &captcha_phrases();
 4573:     $keyentry = 'hidden';
 4574:     if ($context eq 'cancreate') {
 4575:         $rowname = &mt('CAPTCHA validation');
 4576:     } elsif ($context eq 'login') {
 4577:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 4578:     }
 4579:     if (ref($settings) eq 'HASH') {
 4580:         if ($settings->{'captcha'}) {
 4581:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 4582:         } else {
 4583:             $checked{'original'} = ' checked="checked"';
 4584:         }
 4585:         if ($settings->{'captcha'} eq 'recaptcha') {
 4586:             $pubtext = $lt{'pub'};
 4587:             $privtext = $lt{'priv'};
 4588:             $keyentry = 'text';
 4589:             $vertext = $lt{'ver'};
 4590:             $currver = $settings->{'recaptchaversion'};
 4591:             if ($currver ne '2') {
 4592:                 $currver = 1;
 4593:             }
 4594:         }
 4595:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 4596:             $currpub = $settings->{'recaptchakeys'}{'public'};
 4597:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 4598:         }
 4599:     } else {
 4600:         $checked{'original'} = ' checked="checked"';
 4601:     }
 4602:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4603:     my $output = '<tr'.$css_class.'>'.
 4604:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 4605:                  '<table><tr><td>'."\n";
 4606:     foreach my $option ('original','recaptcha','notused') {
 4607:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 4608:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 4609:                    $lt{$option}.'</label></span>';
 4610:         unless ($option eq 'notused') {
 4611:             $output .= ('&nbsp;'x2)."\n";
 4612:         }
 4613:     }
 4614: #
 4615: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 4616: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 4617: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 4618: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 4619: #
 4620:     $output .= '</td></tr>'."\n".
 4621:                '<tr><td>'."\n".
 4622:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 4623:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 4624:                $currpub.'" size="40" /></span><br />'."\n".
 4625:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 4626:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 4627:                $currpriv.'" size="40" /></span><br />'.
 4628:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 4629:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 4630:                $currver.'" size="3" /></span><br />'.
 4631:                '</td></tr></table>'."\n".
 4632:                '</td></tr>';
 4633:     return $output;
 4634: }
 4635: 
 4636: sub user_formats_row {
 4637:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 4638:     my $output;
 4639:     my %text = (
 4640:                    'username' => 'new usernames',
 4641:                    'id'       => 'IDs',
 4642:                    'email'    => 'self-created accounts (e-mail)',
 4643:                );
 4644:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4645:     $output = '<tr '.$css_class.'>'.
 4646:               '<td><span class="LC_nobreak">';
 4647:     if ($type eq 'email') {
 4648:         $output .= &mt("Formats disallowed for $text{$type}: ");
 4649:     } else {
 4650:         $output .= &mt("Format rules to check for $text{$type}: ");
 4651:     }
 4652:     $output .= '</span></td>'.
 4653:                '<td class="LC_left_item" colspan="2"><table>';
 4654:     my $rem;
 4655:     if (ref($ruleorder) eq 'ARRAY') {
 4656:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 4657:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 4658:                 my $rem = $i%($numinrow);
 4659:                 if ($rem == 0) {
 4660:                     if ($i > 0) {
 4661:                         $output .= '</tr>';
 4662:                     }
 4663:                     $output .= '<tr>';
 4664:                 }
 4665:                 my $check = ' ';
 4666:                 if (ref($settings) eq 'HASH') {
 4667:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 4668:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 4669:                             $check = ' checked="checked" ';
 4670:                         }
 4671:                     }
 4672:                 }
 4673:                 $output .= '<td class="LC_left_item">'.
 4674:                            '<span class="LC_nobreak"><label>'.
 4675:                            '<input type="checkbox" name="'.$type.'_rule" '.
 4676:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 4677:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 4678:             }
 4679:         }
 4680:         $rem = @{$ruleorder}%($numinrow);
 4681:     }
 4682:     my $colsleft = $numinrow - $rem;
 4683:     if ($colsleft > 1 ) {
 4684:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4685:                    '&nbsp;</td>';
 4686:     } elsif ($colsleft == 1) {
 4687:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4688:     }
 4689:     $output .= '</tr></table></td></tr>';
 4690:     return $output;
 4691: }
 4692: 
 4693: sub usercreation_types {
 4694:     my %lt = &Apache::lonlocal::texthash (
 4695:                     author     => 'When adding a co-author',
 4696:                     course     => 'When adding a user to a course',
 4697:                     requestcrs => 'When requesting a course',
 4698:                     any        => 'Any',
 4699:                     official   => 'Institutional only ',
 4700:                     unofficial => 'Non-institutional only',
 4701:                     none       => 'None',
 4702:     );
 4703:     return %lt;
 4704: }
 4705: 
 4706: sub selfcreation_types {
 4707:     my %lt = &Apache::lonlocal::texthash (
 4708:                     selfcreate => 'User creates own account',
 4709:                     any        => 'Any',
 4710:                     official   => 'Institutional only ',
 4711:                     unofficial => 'Non-institutional only',
 4712:                     email      => 'E-mail address',
 4713:                     login      => 'Institutional Login',
 4714:                     sso        => 'SSO',
 4715:              );
 4716: }
 4717: 
 4718: sub authtype_names {
 4719:     my %lt = &Apache::lonlocal::texthash(
 4720:                       int    => 'Internal',
 4721:                       krb4   => 'Kerberos 4',
 4722:                       krb5   => 'Kerberos 5',
 4723:                       loc    => 'Local',
 4724:                   );
 4725:     return %lt;
 4726: }
 4727: 
 4728: sub context_names {
 4729:     my %context_title = &Apache::lonlocal::texthash(
 4730:        author => 'Creating users when an Author',
 4731:        course => 'Creating users when in a course',
 4732:        domain => 'Creating users when a Domain Coordinator',
 4733:     );
 4734:     return %context_title;
 4735: }
 4736: 
 4737: sub print_usermodification {
 4738:     my ($position,$dom,$settings,$rowtotal) = @_;
 4739:     my $numinrow = 4;
 4740:     my ($context,$datatable,$rowcount);
 4741:     if ($position eq 'top') {
 4742:         $rowcount = 0;
 4743:         $context = 'author'; 
 4744:         foreach my $role ('ca','aa') {
 4745:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4746:                                                    $numinrow,$rowcount);
 4747:             $$rowtotal ++;
 4748:             $rowcount ++;
 4749:         }
 4750:     } elsif ($position eq 'bottom') {
 4751:         $context = 'course';
 4752:         $rowcount = 0;
 4753:         foreach my $role ('st','ep','ta','in','cr') {
 4754:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4755:                                                    $numinrow,$rowcount);
 4756:             $$rowtotal ++;
 4757:             $rowcount ++;
 4758:         }
 4759:     }
 4760:     return $datatable;
 4761: }
 4762: 
 4763: sub print_defaults {
 4764:     my ($position,$dom,$settings,$rowtotal) = @_;
 4765:     my $rownum = 0;
 4766:     my ($datatable,$css_class);
 4767:     if ($position eq 'top') {
 4768:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 4769:                      'datelocale_def','portal_def');
 4770:         my %defaults;
 4771:         if (ref($settings) eq 'HASH') {
 4772:             %defaults = %{$settings};
 4773:         } else {
 4774:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 4775:             foreach my $item (@items) {
 4776:                 $defaults{$item} = $domdefaults{$item};
 4777:             }
 4778:         }
 4779:         my $titles = &defaults_titles($dom);
 4780:         foreach my $item (@items) {
 4781:             if ($rownum%2) {
 4782:                 $css_class = '';
 4783:             } else {
 4784:                 $css_class = ' class="LC_odd_row" ';
 4785:             }
 4786:             $datatable .= '<tr'.$css_class.'>'.
 4787:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4788:                           '</span></td><td class="LC_right_item" colspan="3">';
 4789:             if ($item eq 'auth_def') {
 4790:                 my @authtypes = ('internal','krb4','krb5','localauth');
 4791:                 my %shortauth = (
 4792:                                  internal => 'int',
 4793:                                  krb4 => 'krb4',
 4794:                                  krb5 => 'krb5',
 4795:                                  localauth  => 'loc'
 4796:                                 );
 4797:                 my %authnames = &authtype_names();
 4798:                 foreach my $auth (@authtypes) {
 4799:                     my $checked = ' ';
 4800:                     if ($defaults{$item} eq $auth) {
 4801:                         $checked = ' checked="checked" ';
 4802:                     }
 4803:                     $datatable .= '<label><input type="radio" name="'.$item.
 4804:                                   '" value="'.$auth.'"'.$checked.'/>'.
 4805:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 4806:                 }
 4807:             } elsif ($item eq 'timezone_def') {
 4808:                 my $includeempty = 1;
 4809:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 4810:             } elsif ($item eq 'datelocale_def') {
 4811:                 my $includeempty = 1;
 4812:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 4813:             } elsif ($item eq 'lang_def') {
 4814:                 my $includeempty = 1;
 4815:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 4816:             } else {
 4817:                 my $size;
 4818:                 if ($item eq 'portal_def') {
 4819:                     $size = ' size="25"';
 4820:                 }
 4821:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 4822:                               $defaults{$item}.'"'.$size.' />';
 4823:             }
 4824:             $datatable .= '</td></tr>';
 4825:             $rownum ++;
 4826:         }
 4827:     } else {
 4828:         my (%defaults);
 4829:         if (ref($settings) eq 'HASH') {
 4830:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 4831:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 4832:                 my $maxnum = @{$settings->{'inststatusorder'}};
 4833:                 for (my $i=0; $i<$maxnum; $i++) {
 4834:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 4835:                     my $item = $settings->{'inststatusorder'}->[$i];
 4836:                     my $title = $settings->{'inststatustypes'}->{$item};
 4837:                     my $guestok;
 4838:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 4839:                         $guestok = 1;
 4840:                     }
 4841:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 4842:                     $datatable .= '<tr'.$css_class.'>'.
 4843:                                   '<td><span class="LC_nobreak">'.
 4844:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 4845:                     for (my $k=0; $k<=$maxnum; $k++) {
 4846:                         my $vpos = $k+1;
 4847:                         my $selstr;
 4848:                         if ($k == $i) {
 4849:                             $selstr = ' selected="selected" ';
 4850:                         }
 4851:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4852:                     }
 4853:                     my ($checkedon,$checkedoff);
 4854:                     $checkedoff = ' checked="checked"';
 4855:                     if ($guestok) {
 4856:                         $checkedon = $checkedoff;
 4857:                         $checkedoff = ''; 
 4858:                     }
 4859:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 4860:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 4861:                                   &mt('delete').'</span></td>'.
 4862:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 4863:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 4864:                                   '</span></td>'.
 4865:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4866:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 4867:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 4868:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 4869:                                   &mt('No').'</label></span></td></tr>';
 4870:                 }
 4871:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 4872:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 4873:                 $datatable .= '<tr '.$css_class.'>'.
 4874:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 4875:                 for (my $k=0; $k<=$maxnum; $k++) {
 4876:                     my $vpos = $k+1;
 4877:                     my $selstr;
 4878:                     if ($k == $maxnum) {
 4879:                         $selstr = ' selected="selected" ';
 4880:                     }
 4881:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4882:                 }
 4883:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 4884:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 4885:                               '&nbsp;'.&mt('(new)').
 4886:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 4887:                               &mt('Name displayed:').
 4888:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 4889:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 4890:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 4891:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 4892:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 4893:                               &mt('No').'</label></span></td></tr>';
 4894:                               '</tr>'."\n";
 4895:                 $rownum ++;
 4896:             }
 4897:         }
 4898:     }
 4899:     $$rowtotal += $rownum;
 4900:     return $datatable;
 4901: }
 4902: 
 4903: sub get_languages_hash {
 4904:     my %langchoices;
 4905:     foreach my $id (&Apache::loncommon::languageids()) {
 4906:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 4907:         if ($code ne '') {
 4908:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 4909:         }
 4910:     }
 4911:     return %langchoices;
 4912: }
 4913: 
 4914: sub defaults_titles {
 4915:     my ($dom) = @_;
 4916:     my %titles = &Apache::lonlocal::texthash (
 4917:                    'auth_def'      => 'Default authentication type',
 4918:                    'auth_arg_def'  => 'Default authentication argument',
 4919:                    'lang_def'      => 'Default language',
 4920:                    'timezone_def'  => 'Default timezone',
 4921:                    'datelocale_def' => 'Default locale for dates',
 4922:                    'portal_def'     => 'Portal/Default URL',
 4923:                  );
 4924:     if ($dom) {
 4925:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 4926:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 4927:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 4928:         $protocol = 'http' if ($protocol ne 'https');
 4929:         if ($uint_dom) {
 4930:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 4931:                                          $uint_dom);
 4932:         }
 4933:     }
 4934:     return (\%titles);
 4935: }
 4936: 
 4937: sub print_scantronformat {
 4938:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 4939:     my $itemcount = 1;
 4940:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 4941:         %confhash);
 4942:     my $switchserver = &check_switchserver($dom,$confname);
 4943:     my %lt = &Apache::lonlocal::texthash (
 4944:                 default => 'Default bubblesheet format file error',
 4945:                 custom  => 'Custom bubblesheet format file error',
 4946:              );
 4947:     my %scantronfiles = (
 4948:         default => 'default.tab',
 4949:         custom => 'custom.tab',
 4950:     );
 4951:     foreach my $key (keys(%scantronfiles)) {
 4952:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 4953:                               .$scantronfiles{$key};
 4954:     }
 4955:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 4956:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 4957:         if (!$switchserver) {
 4958:             my $servadm = $r->dir_config('lonAdmEMail');
 4959:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 4960:             if ($configuserok eq 'ok') {
 4961:                 if ($author_ok eq 'ok') {
 4962:                     my %legacyfile = (
 4963:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 4964:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 4965:                     );
 4966:                     my %md5chk;
 4967:                     foreach my $type (keys(%legacyfile)) {
 4968:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 4969:                         chomp($md5chk{$type});
 4970:                     }
 4971:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 4972:                         foreach my $type (keys(%legacyfile)) {
 4973:                             ($scantronurls{$type},my $error) = 
 4974:                                 &legacy_scantronformat($r,$dom,$confname,
 4975:                                                  $type,$legacyfile{$type},
 4976:                                                  $scantronurls{$type},
 4977:                                                  $scantronfiles{$type});
 4978:                             if ($error ne '') {
 4979:                                 $error{$type} = $error;
 4980:                             }
 4981:                         }
 4982:                         if (keys(%error) == 0) {
 4983:                             $is_custom = 1;
 4984:                             $confhash{'scantron'}{'scantronformat'} = 
 4985:                                 $scantronurls{'custom'};
 4986:                             my $putresult = 
 4987:                                 &Apache::lonnet::put_dom('configuration',
 4988:                                                          \%confhash,$dom);
 4989:                             if ($putresult ne 'ok') {
 4990:                                 $error{'custom'} = 
 4991:                                     '<span class="LC_error">'.
 4992:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4993:                             }
 4994:                         }
 4995:                     } else {
 4996:                         ($scantronurls{'default'},my $error) =
 4997:                             &legacy_scantronformat($r,$dom,$confname,
 4998:                                           'default',$legacyfile{'default'},
 4999:                                           $scantronurls{'default'},
 5000:                                           $scantronfiles{'default'});
 5001:                         if ($error eq '') {
 5002:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 5003:                             my $putresult =
 5004:                                 &Apache::lonnet::put_dom('configuration',
 5005:                                                          \%confhash,$dom);
 5006:                             if ($putresult ne 'ok') {
 5007:                                 $error{'default'} =
 5008:                                     '<span class="LC_error">'.
 5009:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 5010:                             }
 5011:                         } else {
 5012:                             $error{'default'} = $error;
 5013:                         }
 5014:                     }
 5015:                 }
 5016:             }
 5017:         } else {
 5018:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 5019:         }
 5020:     }
 5021:     if (ref($settings) eq 'HASH') {
 5022:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 5023:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 5024:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 5025:                 $scantronurl = '';
 5026:             } else {
 5027:                 $scantronurl = $settings->{'scantronformat'};
 5028:             }
 5029:             $is_custom = 1;
 5030:         } else {
 5031:             $scantronurl = $scantronurls{'default'};
 5032:         }
 5033:     } else {
 5034:         if ($is_custom) {
 5035:             $scantronurl = $scantronurls{'custom'};
 5036:         } else {
 5037:             $scantronurl = $scantronurls{'default'};
 5038:         }
 5039:     }
 5040:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5041:     $datatable .= '<tr'.$css_class.'>';
 5042:     if (!$is_custom) {
 5043:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 5044:                       '<span class="LC_nobreak">';
 5045:         if ($scantronurl) {
 5046:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 5047:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 5048:         } else {
 5049:             $datatable = &mt('File unavailable for display');
 5050:         }
 5051:         $datatable .= '</span></td>';
 5052:         if (keys(%error) == 0) { 
 5053:             $datatable .= '<td valign="bottom">';
 5054:             if (!$switchserver) {
 5055:                 $datatable .= &mt('Upload:').'<br />';
 5056:             }
 5057:         } else {
 5058:             my $errorstr;
 5059:             foreach my $key (sort(keys(%error))) {
 5060:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 5061:             }
 5062:             $datatable .= '<td>'.$errorstr;
 5063:         }
 5064:     } else {
 5065:         if (keys(%error) > 0) {
 5066:             my $errorstr;
 5067:             foreach my $key (sort(keys(%error))) {
 5068:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 5069:             } 
 5070:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 5071:         } elsif ($scantronurl) {
 5072:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 5073:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 5074:             $datatable .= '<td><span class="LC_nobreak">'.
 5075:                           $link.
 5076:                           '<label><input type="checkbox" name="scantronformat_del"'.
 5077:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 5078:                           '<td><span class="LC_nobreak">&nbsp;'.
 5079:                           &mt('Replace:').'</span><br />';
 5080:         }
 5081:     }
 5082:     if (keys(%error) == 0) {
 5083:         if ($switchserver) {
 5084:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5085:         } else {
 5086:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 5087:                          '<input type="file" name="scantronformat" /></span>';
 5088:         }
 5089:     }
 5090:     $datatable .= '</td></tr>';
 5091:     $$rowtotal ++;
 5092:     return $datatable;
 5093: }
 5094: 
 5095: sub legacy_scantronformat {
 5096:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 5097:     my ($url,$error);
 5098:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 5099:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 5100:         (my $result,$url) =
 5101:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 5102:                          '','',$newfile);
 5103:         if ($result ne 'ok') {
 5104:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 5105:         }
 5106:     }
 5107:     return ($url,$error);
 5108: }
 5109: 
 5110: sub print_coursecategories {
 5111:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 5112:     my $datatable;
 5113:     if ($position eq 'top') {
 5114:         my (%checked);
 5115:         my @catitems = ('unauth','auth');
 5116:         my @cattypes = ('std','domonly','codesrch','none');
 5117:         $checked{'unauth'} = 'std';
 5118:         $checked{'auth'} = 'std';
 5119:         if (ref($settings) eq 'HASH') {
 5120:             foreach my $type (@cattypes) {
 5121:                 if ($type eq $settings->{'unauth'}) {
 5122:                     $checked{'unauth'} = $type;
 5123:                 }
 5124:                 if ($type eq $settings->{'auth'}) {
 5125:                     $checked{'auth'} = $type;
 5126:                 }
 5127:             }
 5128:         }
 5129:         my %lt = &Apache::lonlocal::texthash (
 5130:                                                unauth   => 'Catalog type for unauthenticated users',
 5131:                                                auth     => 'Catalog type for authenticated users',
 5132:                                                none     => 'No catalog',
 5133:                                                std      => 'Standard catalog',
 5134:                                                domonly  => 'Domain-only catalog',
 5135:                                                codesrch => "Code search form",
 5136:                                              );
 5137:        my $itemcount = 0;
 5138:        foreach my $item (@catitems) {
 5139:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 5140:            $datatable .= '<tr '.$css_class.'>'.
 5141:                          '<td>'.$lt{$item}.'</td>'.
 5142:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 5143:            foreach my $type (@cattypes) {
 5144:                my $ischecked;
 5145:                if ($checked{$item} eq $type) {
 5146:                    $ischecked=' checked="checked"';
 5147:                }
 5148:                $datatable .= '<label>'.
 5149:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 5150:                              ' />'.$lt{$type}.'</label>&nbsp;';
 5151:            }
 5152:            $datatable .= '</td></tr>';
 5153:            $itemcount ++;
 5154:         }
 5155:         $$rowtotal += $itemcount;
 5156:     } elsif ($position eq 'middle') {
 5157:         my $toggle_cats_crs = ' ';
 5158:         my $toggle_cats_dom = ' checked="checked" ';
 5159:         my $can_cat_crs = ' ';
 5160:         my $can_cat_dom = ' checked="checked" ';
 5161:         my $toggle_catscomm_comm = ' ';
 5162:         my $toggle_catscomm_dom = ' checked="checked" ';
 5163:         my $can_catcomm_comm = ' ';
 5164:         my $can_catcomm_dom = ' checked="checked" ';
 5165: 
 5166:         if (ref($settings) eq 'HASH') {
 5167:             if ($settings->{'togglecats'} eq 'crs') {
 5168:                 $toggle_cats_crs = $toggle_cats_dom;
 5169:                 $toggle_cats_dom = ' ';
 5170:             }
 5171:             if ($settings->{'categorize'} eq 'crs') {
 5172:                 $can_cat_crs = $can_cat_dom;
 5173:                 $can_cat_dom = ' ';
 5174:             }
 5175:             if ($settings->{'togglecatscomm'} eq 'comm') {
 5176:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 5177:                 $toggle_catscomm_dom = ' ';
 5178:             }
 5179:             if ($settings->{'categorizecomm'} eq 'comm') {
 5180:                 $can_catcomm_comm = $can_catcomm_dom;
 5181:                 $can_catcomm_dom = ' ';
 5182:             }
 5183:         }
 5184:         my %title = &Apache::lonlocal::texthash (
 5185:                      togglecats     => 'Show/Hide a course in catalog',
 5186:                      togglecatscomm => 'Show/Hide a community in catalog',
 5187:                      categorize     => 'Assign a category to a course',
 5188:                      categorizecomm => 'Assign a category to a community',
 5189:                     );
 5190:         my %level = &Apache::lonlocal::texthash (
 5191:                      dom  => 'Set in Domain',
 5192:                      crs  => 'Set in Course',
 5193:                      comm => 'Set in Community',
 5194:                     );
 5195:         $datatable = '<tr class="LC_odd_row">'.
 5196:                   '<td>'.$title{'togglecats'}.'</td>'.
 5197:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 5198:                   '<input type="radio" name="togglecats"'.
 5199:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5200:                   '<label><input type="radio" name="togglecats"'.
 5201:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 5202:                   '</tr><tr>'.
 5203:                   '<td>'.$title{'categorize'}.'</td>'.
 5204:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 5205:                   '<label><input type="radio" name="categorize"'.
 5206:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5207:                   '<label><input type="radio" name="categorize"'.
 5208:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 5209:                   '</tr><tr class="LC_odd_row">'.
 5210:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 5211:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 5212:                   '<input type="radio" name="togglecatscomm"'.
 5213:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5214:                   '<label><input type="radio" name="togglecatscomm"'.
 5215:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 5216:                   '</tr><tr>'.
 5217:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 5218:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 5219:                   '<label><input type="radio" name="categorizecomm"'.
 5220:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5221:                   '<label><input type="radio" name="categorizecomm"'.
 5222:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 5223:                   '</tr>';
 5224:         $$rowtotal += 4;
 5225:     } else {
 5226:         my $css_class;
 5227:         my $itemcount = 1;
 5228:         my $cathash; 
 5229:         if (ref($settings) eq 'HASH') {
 5230:             $cathash = $settings->{'cats'};
 5231:         }
 5232:         if (ref($cathash) eq 'HASH') {
 5233:             my (@cats,@trails,%allitems,%idx,@jsarray);
 5234:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 5235:                                                    \%allitems,\%idx,\@jsarray);
 5236:             my $maxdepth = scalar(@cats);
 5237:             my $colattrib = '';
 5238:             if ($maxdepth > 2) {
 5239:                 $colattrib = ' colspan="2" ';
 5240:             }
 5241:             my @path;
 5242:             if (@cats > 0) {
 5243:                 if (ref($cats[0]) eq 'ARRAY') {
 5244:                     my $numtop = @{$cats[0]};
 5245:                     my $maxnum = $numtop;
 5246:                     my %default_names = (
 5247:                           instcode    => &mt('Official courses'),
 5248:                           communities => &mt('Communities'),
 5249:                     );
 5250: 
 5251:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 5252:                         ($cathash->{'instcode::0'} eq '') ||
 5253:                         (!grep(/^communities$/,@{$cats[0]})) || 
 5254:                         ($cathash->{'communities::0'} eq '')) {
 5255:                         $maxnum ++;
 5256:                     }
 5257:                     my $lastidx;
 5258:                     for (my $i=0; $i<$numtop; $i++) {
 5259:                         my $parent = $cats[0][$i];
 5260:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5261:                         my $item = &escape($parent).'::0';
 5262:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 5263:                         $lastidx = $idx{$item};
 5264:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5265:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 5266:                         for (my $k=0; $k<=$maxnum; $k++) {
 5267:                             my $vpos = $k+1;
 5268:                             my $selstr;
 5269:                             if ($k == $i) {
 5270:                                 $selstr = ' selected="selected" ';
 5271:                             }
 5272:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5273:                         }
 5274:                         $datatable .= '</select></span></td><td>';
 5275:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 5276:                             $datatable .=  '<span class="LC_nobreak">'
 5277:                                            .$default_names{$parent}.'</span>';
 5278:                             if ($parent eq 'instcode') {
 5279:                                 $datatable .= '<br /><span class="LC_nobreak">('
 5280:                                               .&mt('with institutional codes')
 5281:                                               .')</span></td><td'.$colattrib.'>';
 5282:                             } else {
 5283:                                 $datatable .= '<table><tr><td>';
 5284:                             }
 5285:                             $datatable .= '<span class="LC_nobreak">'
 5286:                                           .'<label><input type="radio" name="'
 5287:                                           .$parent.'" value="1" checked="checked" />'
 5288:                                           .&mt('Display').'</label>';
 5289:                             if ($parent eq 'instcode') {
 5290:                                 $datatable .= '&nbsp;';
 5291:                             } else {
 5292:                                 $datatable .= '</span></td></tr><tr><td>'
 5293:                                               .'<span class="LC_nobreak">';
 5294:                             }
 5295:                             $datatable .= '<label><input type="radio" name="'
 5296:                                           .$parent.'" value="0" />'
 5297:                                           .&mt('Do not display').'</label></span>';
 5298:                             if ($parent eq 'communities') {
 5299:                                 $datatable .= '</td></tr></table>';
 5300:                             }
 5301:                             $datatable .= '</td>';
 5302:                         } else {
 5303:                             $datatable .= $parent
 5304:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 5305:                                           .'<input type="checkbox" name="deletecategory" '
 5306:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 5307:                         }
 5308:                         my $depth = 1;
 5309:                         push(@path,$parent);
 5310:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 5311:                         pop(@path);
 5312:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 5313:                         $itemcount ++;
 5314:                     }
 5315:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5316:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 5317:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 5318:                     for (my $k=0; $k<=$maxnum; $k++) {
 5319:                         my $vpos = $k+1;
 5320:                         my $selstr;
 5321:                         if ($k == $numtop) {
 5322:                             $selstr = ' selected="selected" ';
 5323:                         }
 5324:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5325:                     }
 5326:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 5327:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 5328:                                   .'</tr>'."\n";
 5329:                     $itemcount ++;
 5330:                     foreach my $default ('instcode','communities') {
 5331:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 5332:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5333:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 5334:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 5335:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 5336:                             for (my $k=0; $k<=$maxnum; $k++) {
 5337:                                 my $vpos = $k+1;
 5338:                                 my $selstr;
 5339:                                 if ($k == $maxnum) {
 5340:                                     $selstr = ' selected="selected" ';
 5341:                                 }
 5342:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5343:                             }
 5344:                             $datatable .= '</select></span></td>'.
 5345:                                           '<td><span class="LC_nobreak">'.
 5346:                                           $default_names{$default}.'</span>';
 5347:                             if ($default eq 'instcode') {
 5348:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 5349:                                               .&mt('with institutional codes').')</span>';
 5350:                             }
 5351:                             $datatable .= '</td>'
 5352:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 5353:                                           .&mt('Display').'</label>&nbsp;'
 5354:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 5355:                                           .&mt('Do not display').'</label></span></td></tr>';
 5356:                         }
 5357:                     }
 5358:                 }
 5359:             } else {
 5360:                 $datatable .= &initialize_categories($itemcount);
 5361:             }
 5362:         } else {
 5363:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 5364:                           .&initialize_categories($itemcount);
 5365:         }
 5366:         $$rowtotal += $itemcount;
 5367:     }
 5368:     return $datatable;
 5369: }
 5370: 
 5371: sub print_serverstatuses {
 5372:     my ($dom,$settings,$rowtotal) = @_;
 5373:     my $datatable;
 5374:     my @pages = &serverstatus_pages();
 5375:     my (%namedaccess,%machineaccess);
 5376:     foreach my $type (@pages) {
 5377:         $namedaccess{$type} = '';
 5378:         $machineaccess{$type}= '';
 5379:     }
 5380:     if (ref($settings) eq 'HASH') {
 5381:         foreach my $type (@pages) {
 5382:             if (exists($settings->{$type})) {
 5383:                 if (ref($settings->{$type}) eq 'HASH') {
 5384:                     foreach my $key (keys(%{$settings->{$type}})) {
 5385:                         if ($key eq 'namedusers') {
 5386:                             $namedaccess{$type} = $settings->{$type}->{$key};
 5387:                         } elsif ($key eq 'machines') {
 5388:                             $machineaccess{$type} = $settings->{$type}->{$key};
 5389:                         }
 5390:                     }
 5391:                 }
 5392:             }
 5393:         }
 5394:     }
 5395:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 5396:     my $rownum = 0;
 5397:     my $css_class;
 5398:     foreach my $type (@pages) {
 5399:         $rownum ++;
 5400:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 5401:         $datatable .= '<tr'.$css_class.'>'.
 5402:                       '<td><span class="LC_nobreak">'.
 5403:                       $titles->{$type}.'</span></td>'.
 5404:                       '<td class="LC_left_item">'.
 5405:                       '<input type="text" name="'.$type.'_namedusers" '.
 5406:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 5407:                       '<td class="LC_right_item">'.
 5408:                       '<span class="LC_nobreak">'.
 5409:                       '<input type="text" name="'.$type.'_machines" '.
 5410:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 5411:                       '</td></tr>'."\n";
 5412:     }
 5413:     $$rowtotal += $rownum;
 5414:     return $datatable;
 5415: }
 5416: 
 5417: sub serverstatus_pages {
 5418:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 5419:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 5420:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 5421:             'uniquecodes','diskusage','coursecatalog');
 5422: }
 5423: 
 5424: sub defaults_javascript {
 5425:     my ($settings) = @_;
 5426:     return unless (ref($settings) eq 'HASH');
 5427:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 5428:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 5429:         if ($maxnum eq '') {
 5430:             $maxnum = 0;
 5431:         }
 5432:         $maxnum ++;
 5433:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 5434:         return <<"ENDSCRIPT";
 5435: <script type="text/javascript">
 5436: // <![CDATA[
 5437: function reorderTypes(form,caller) {
 5438:     var changedVal;
 5439: $jstext 
 5440:     var newpos = 'addinststatus_pos';
 5441:     var current = new Array;
 5442:     var maxh = $maxnum;
 5443:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5444:     var oldVal;
 5445:     if (caller == newpos) {
 5446:         changedVal = newitemVal;
 5447:     } else {
 5448:         var curritem = 'inststatus_pos_'+caller;
 5449:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 5450:         current[newitemVal] = newpos;
 5451:     }
 5452:     for (var i=0; i<inststatuses.length; i++) {
 5453:         if (inststatuses[i] != caller) {
 5454:             var elementName = 'inststatus_pos_'+inststatuses[i];
 5455:             if (form.elements[elementName]) {
 5456:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5457:                 current[currVal] = elementName;
 5458:             }
 5459:         }
 5460:     }
 5461:     for (var j=0; j<maxh; j++) {
 5462:         if (current[j] == undefined) {
 5463:             oldVal = j;
 5464:         }
 5465:     }
 5466:     if (oldVal < changedVal) {
 5467:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5468:            var elementName = current[k];
 5469:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5470:         }
 5471:     } else {
 5472:         for (var k=changedVal; k<oldVal; k++) {
 5473:             var elementName = current[k];
 5474:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5475:         }
 5476:     }
 5477:     return;
 5478: }
 5479: 
 5480: // ]]>
 5481: </script>
 5482: 
 5483: ENDSCRIPT
 5484:     }
 5485: }
 5486: 
 5487: sub coursecategories_javascript {
 5488:     my ($settings) = @_;
 5489:     my ($output,$jstext,$cathash);
 5490:     if (ref($settings) eq 'HASH') {
 5491:         $cathash = $settings->{'cats'};
 5492:     }
 5493:     if (ref($cathash) eq 'HASH') {
 5494:         my (@cats,@jsarray,%idx);
 5495:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 5496:         if (@jsarray > 0) {
 5497:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 5498:             for (my $i=0; $i<@jsarray; $i++) {
 5499:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 5500:                     my $catstr = join('","',@{$jsarray[$i]});
 5501:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 5502:                 }
 5503:             }
 5504:         }
 5505:     } else {
 5506:         $jstext  = '    var categories = Array(1);'."\n".
 5507:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 5508:     }
 5509:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 5510:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 5511:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 5512:     &js_escape(\$instcode_reserved);
 5513:     &js_escape(\$communities_reserved);
 5514:     &js_escape(\$choose_again);
 5515:     $output = <<"ENDSCRIPT";
 5516: <script type="text/javascript">
 5517: // <![CDATA[
 5518: function reorderCats(form,parent,item,idx) {
 5519:     var changedVal;
 5520: $jstext
 5521:     var newpos = 'addcategory_pos';
 5522:     if (parent == '') {
 5523:         var has_instcode = 0;
 5524:         var maxtop = categories[idx].length;
 5525:         for (var j=0; j<maxtop; j++) {
 5526:             if (categories[idx][j] == 'instcode::0') {
 5527:                 has_instcode == 1;
 5528:             }
 5529:         }
 5530:         if (has_instcode == 0) {
 5531:             categories[idx][maxtop] = 'instcode_pos';
 5532:         }
 5533:     } else {
 5534:         newpos += '_'+parent;
 5535:     }
 5536:     var maxh = 1 + categories[idx].length;
 5537:     var current = new Array;
 5538:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5539:     if (item == newpos) {
 5540:         changedVal = newitemVal;
 5541:     } else {
 5542:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 5543:         current[newitemVal] = newpos;
 5544:     }
 5545:     for (var i=0; i<categories[idx].length; i++) {
 5546:         var elementName = categories[idx][i];
 5547:         if (elementName != item) {
 5548:             if (form.elements[elementName]) {
 5549:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5550:                 current[currVal] = elementName;
 5551:             }
 5552:         }
 5553:     }
 5554:     var oldVal;
 5555:     for (var j=0; j<maxh; j++) {
 5556:         if (current[j] == undefined) {
 5557:             oldVal = j;
 5558:         }
 5559:     }
 5560:     if (oldVal < changedVal) {
 5561:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5562:            var elementName = current[k];
 5563:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5564:         }
 5565:     } else {
 5566:         for (var k=changedVal; k<oldVal; k++) {
 5567:             var elementName = current[k];
 5568:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5569:         }
 5570:     }
 5571:     return;
 5572: }
 5573: 
 5574: function categoryCheck(form) {
 5575:     if (form.elements['addcategory_name'].value == 'instcode') {
 5576:         alert('$instcode_reserved\\n$choose_again');
 5577:         return false;
 5578:     }
 5579:     if (form.elements['addcategory_name'].value == 'communities') {
 5580:         alert('$communities_reserved\\n$choose_again');
 5581:         return false;
 5582:     }
 5583:     return true;
 5584: }
 5585: 
 5586: // ]]>
 5587: </script>
 5588: 
 5589: ENDSCRIPT
 5590:     return $output;
 5591: }
 5592: 
 5593: sub initialize_categories {
 5594:     my ($itemcount) = @_;
 5595:     my ($datatable,$css_class,$chgstr);
 5596:     my %default_names = (
 5597:                       instcode    => 'Official courses (with institutional codes)',
 5598:                       communities => 'Communities',
 5599:                         );
 5600:     my $select0 = ' selected="selected"';
 5601:     my $select1 = '';
 5602:     foreach my $default ('instcode','communities') {
 5603:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5604:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 5605:         if ($default eq 'communities') {
 5606:             $select1 = $select0;
 5607:             $select0 = '';
 5608:         }
 5609:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5610:                      .'<select name="'.$default.'_pos">'
 5611:                      .'<option value="0"'.$select0.'>1</option>'
 5612:                      .'<option value="1"'.$select1.'>2</option>'
 5613:                      .'<option value="2">3</option></select>&nbsp;'
 5614:                      .$default_names{$default}
 5615:                      .'</span></td><td><span class="LC_nobreak">'
 5616:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 5617:                      .&mt('Display').'</label>&nbsp;<label>'
 5618:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 5619:                  .'</label></span></td></tr>';
 5620:         $itemcount ++;
 5621:     }
 5622:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5623:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 5624:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5625:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 5626:                   .'<option value="0">1</option>'
 5627:                   .'<option value="1">2</option>'
 5628:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 5629:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 5630:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 5631:     return $datatable;
 5632: }
 5633: 
 5634: sub build_category_rows {
 5635:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 5636:     my ($text,$name,$item,$chgstr);
 5637:     if (ref($cats) eq 'ARRAY') {
 5638:         my $maxdepth = scalar(@{$cats});
 5639:         if (ref($cats->[$depth]) eq 'HASH') {
 5640:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 5641:                 my $numchildren = @{$cats->[$depth]{$parent}};
 5642:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5643:                 $text .= '<td><table class="LC_data_table">';
 5644:                 my ($idxnum,$parent_name,$parent_item);
 5645:                 my $higher = $depth - 1;
 5646:                 if ($higher == 0) {
 5647:                     $parent_name = &escape($parent).'::'.$higher;
 5648:                 } else {
 5649:                     if (ref($path) eq 'ARRAY') {
 5650:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5651:                     }
 5652:                 }
 5653:                 $parent_item = 'addcategory_pos_'.$parent_name;
 5654:                 for (my $j=0; $j<=$numchildren; $j++) {
 5655:                     if ($j < $numchildren) {
 5656:                         $name = $cats->[$depth]{$parent}[$j];
 5657:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 5658:                         $idxnum = $idx->{$item};
 5659:                     } else {
 5660:                         $name = $parent_name;
 5661:                         $item = $parent_item;
 5662:                     }
 5663:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 5664:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 5665:                     for (my $i=0; $i<=$numchildren; $i++) {
 5666:                         my $vpos = $i+1;
 5667:                         my $selstr;
 5668:                         if ($j == $i) {
 5669:                             $selstr = ' selected="selected" ';
 5670:                         }
 5671:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 5672:                     }
 5673:                     $text .= '</select>&nbsp;';
 5674:                     if ($j < $numchildren) {
 5675:                         my $deeper = $depth+1;
 5676:                         $text .= $name.'&nbsp;'
 5677:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 5678:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 5679:                         if(ref($path) eq 'ARRAY') {
 5680:                             push(@{$path},$name);
 5681:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 5682:                             pop(@{$path});
 5683:                         }
 5684:                     } else {
 5685:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 5686:                         if ($j == $numchildren) {
 5687:                             $text .= $name;
 5688:                         } else {
 5689:                             $text .= $item;
 5690:                         }
 5691:                         $text .= '" value="" />';
 5692:                     }
 5693:                     $text .= '</td></tr>';
 5694:                 }
 5695:                 $text .= '</table></td>';
 5696:             } else {
 5697:                 my $higher = $depth-1;
 5698:                 if ($higher == 0) {
 5699:                     $name = &escape($parent).'::'.$higher;
 5700:                 } else {
 5701:                     if (ref($path) eq 'ARRAY') {
 5702:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5703:                     }
 5704:                 }
 5705:                 my $colspan;
 5706:                 if ($parent ne 'instcode') {
 5707:                     $colspan = $maxdepth - $depth - 1;
 5708:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 5709:                 }
 5710:             }
 5711:         }
 5712:     }
 5713:     return $text;
 5714: }
 5715: 
 5716: sub modifiable_userdata_row {
 5717:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 5718:     my ($role,$rolename,$statustype);
 5719:     $role = $item;
 5720:     if ($context eq 'cancreate') {
 5721:         if ($item =~ /^emailusername_(.+)$/) {
 5722:             $statustype = $1;
 5723:             $role = 'emailusername';
 5724:             if (ref($usertypes) eq 'HASH') {
 5725:                 if ($usertypes->{$statustype}) {
 5726:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 5727:                 } else {
 5728:                     $rolename = &mt('Data provided by user');
 5729:                 }
 5730:             }
 5731:         }
 5732:     } elsif ($context eq 'selfcreate') {
 5733:         if (ref($usertypes) eq 'HASH') {
 5734:             $rolename = $usertypes->{$role};
 5735:         } else {
 5736:             $rolename = $role;
 5737:         }
 5738:     } else {
 5739:         if ($role eq 'cr') {
 5740:             $rolename = &mt('Custom role');
 5741:         } else {
 5742:             $rolename = &Apache::lonnet::plaintext($role);
 5743:         }
 5744:     }
 5745:     my (@fields,%fieldtitles);
 5746:     if (ref($fieldsref) eq 'ARRAY') {
 5747:         @fields = @{$fieldsref};
 5748:     } else {
 5749:         @fields = ('lastname','firstname','middlename','generation',
 5750:                    'permanentemail','id');
 5751:     }
 5752:     if ((ref($titlesref) eq 'HASH')) {
 5753:         %fieldtitles = %{$titlesref};
 5754:     } else {
 5755:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5756:     }
 5757:     my $output;
 5758:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5759:     $output = '<tr '.$css_class.'>'.
 5760:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 5761:               '<td class="LC_left_item" colspan="2"><table>';
 5762:     my $rem;
 5763:     my %checks;
 5764:     if (ref($settings) eq 'HASH') {
 5765:         if (ref($settings->{$context}) eq 'HASH') {
 5766:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 5767:                 my $hashref = $settings->{$context}->{$role};
 5768:                 if ($role eq 'emailusername') {
 5769:                     if ($statustype) {
 5770:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 5771:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 5772:                             if (ref($hashref) eq 'HASH') { 
 5773:                                 foreach my $field (@fields) {
 5774:                                     if ($hashref->{$field}) {
 5775:                                         $checks{$field} = $hashref->{$field};
 5776:                                     }
 5777:                                 }
 5778:                             }
 5779:                         }
 5780:                     }
 5781:                 } else {
 5782:                     if (ref($hashref) eq 'HASH') {
 5783:                         foreach my $field (@fields) {
 5784:                             if ($hashref->{$field}) {
 5785:                                 $checks{$field} = ' checked="checked" ';
 5786:                             }
 5787:                         }
 5788:                     }
 5789:                 }
 5790:             }
 5791:         }
 5792:     }
 5793:      
 5794:     for (my $i=0; $i<@fields; $i++) {
 5795:         my $rem = $i%($numinrow);
 5796:         if ($rem == 0) {
 5797:             if ($i > 0) {
 5798:                 $output .= '</tr>';
 5799:             }
 5800:             $output .= '<tr>';
 5801:         }
 5802:         my $check = ' ';
 5803:         unless ($role eq 'emailusername') {
 5804:             if (exists($checks{$fields[$i]})) {
 5805:                 $check = $checks{$fields[$i]}
 5806:             } else {
 5807:                 if ($role eq 'st') {
 5808:                     if (ref($settings) ne 'HASH') {
 5809:                         $check = ' checked="checked" '; 
 5810:                     }
 5811:                 }
 5812:             }
 5813:         }
 5814:         $output .= '<td class="LC_left_item">'.
 5815:                    '<span class="LC_nobreak">';
 5816:         if ($role eq 'emailusername') {
 5817:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 5818:                 $checks{$fields[$i]} = 'omit';
 5819:             }
 5820:             foreach my $option ('required','optional','omit') {
 5821:                 my $checked='';
 5822:                 if ($checks{$fields[$i]} eq $option) {
 5823:                     $checked='checked="checked" ';
 5824:                 }
 5825:                 $output .= '<label>'.
 5826:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 5827:                            &mt($option).'</label>'.('&nbsp;' x2);
 5828:             }
 5829:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 5830:         } else {
 5831:             $output .= '<label>'.
 5832:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 5833:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 5834:                        '</label>';
 5835:         }
 5836:         $output .= '</span></td>';
 5837:         $rem = @fields%($numinrow);
 5838:     }
 5839:     my $colsleft = $numinrow - $rem;
 5840:     if ($colsleft > 1 ) {
 5841:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5842:                    '&nbsp;</td>';
 5843:     } elsif ($colsleft == 1) {
 5844:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5845:     }
 5846:     $output .= '</tr></table></td></tr>';
 5847:     return $output;
 5848: }
 5849: 
 5850: sub insttypes_row {
 5851:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 5852:     my %lt = &Apache::lonlocal::texthash (
 5853:                       cansearch => 'Users allowed to search',
 5854:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 5855:                       lockablenames => 'User preference to lock name',
 5856:              );
 5857:     my $showdom;
 5858:     if ($context eq 'cansearch') {
 5859:         $showdom = ' ('.$dom.')';
 5860:     }
 5861:     my $class = 'LC_left_item';
 5862:     if ($context eq 'statustocreate') {
 5863:         $class = 'LC_right_item';
 5864:     }
 5865:     my $css_class = ' class="LC_odd_row"';
 5866:     if ($rownum ne '') { 
 5867:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 5868:     }
 5869:     my $output = '<tr'.$css_class.'>'.
 5870:                  '<td>'.$lt{$context}.$showdom.
 5871:                  '</td><td class="'.$class.'" colspan="2"><table>';
 5872:     my $rem;
 5873:     if (ref($types) eq 'ARRAY') {
 5874:         for (my $i=0; $i<@{$types}; $i++) {
 5875:             if (defined($usertypes->{$types->[$i]})) {
 5876:                 my $rem = $i%($numinrow);
 5877:                 if ($rem == 0) {
 5878:                     if ($i > 0) {
 5879:                         $output .= '</tr>';
 5880:                     }
 5881:                     $output .= '<tr>';
 5882:                 }
 5883:                 my $check = ' ';
 5884:                 if (ref($settings) eq 'HASH') {
 5885:                     if (ref($settings->{$context}) eq 'ARRAY') {
 5886:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 5887:                             $check = ' checked="checked" ';
 5888:                         }
 5889:                     } elsif ($context eq 'statustocreate') {
 5890:                         $check = ' checked="checked" ';
 5891:                     }
 5892:                 }
 5893:                 $output .= '<td class="LC_left_item">'.
 5894:                            '<span class="LC_nobreak"><label>'.
 5895:                            '<input type="checkbox" name="'.$context.'" '.
 5896:                            'value="'.$types->[$i].'"'.$check.'/>'.
 5897:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 5898:             }
 5899:         }
 5900:         $rem = @{$types}%($numinrow);
 5901:     }
 5902:     my $colsleft = $numinrow - $rem;
 5903:     if (($rem == 0) && (@{$types} > 0)) {
 5904:         $output .= '<tr>';
 5905:     }
 5906:     if ($colsleft > 1) {
 5907:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5908:     } else {
 5909:         $output .= '<td class="LC_left_item">';
 5910:     }
 5911:     my $defcheck = ' ';
 5912:     if (ref($settings) eq 'HASH') {  
 5913:         if (ref($settings->{$context}) eq 'ARRAY') {
 5914:             if (grep(/^default$/,@{$settings->{$context}})) {
 5915:                 $defcheck = ' checked="checked" ';
 5916:             }
 5917:         } elsif ($context eq 'statustocreate') {
 5918:             $defcheck = ' checked="checked" ';
 5919:         }
 5920:     }
 5921:     $output .= '<span class="LC_nobreak"><label>'.
 5922:                '<input type="checkbox" name="'.$context.'" '.
 5923:                'value="default"'.$defcheck.'/>'.
 5924:                $othertitle.'</label></span></td>'.
 5925:                '</tr></table></td></tr>';
 5926:     return $output;
 5927: }
 5928: 
 5929: sub sorted_searchtitles {
 5930:     my %searchtitles = &Apache::lonlocal::texthash(
 5931:                          'uname' => 'username',
 5932:                          'lastname' => 'last name',
 5933:                          'lastfirst' => 'last name, first name',
 5934:                      );
 5935:     my @titleorder = ('uname','lastname','lastfirst');
 5936:     return (\%searchtitles,\@titleorder);
 5937: }
 5938: 
 5939: sub sorted_searchtypes {
 5940:     my %srchtypes_desc = (
 5941:                            exact    => 'is exact match',
 5942:                            contains => 'contains ..',
 5943:                            begins   => 'begins with ..',
 5944:                          );
 5945:     my @srchtypeorder = ('exact','begins','contains');
 5946:     return (\%srchtypes_desc,\@srchtypeorder);
 5947: }
 5948: 
 5949: sub usertype_update_row {
 5950:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 5951:     my $datatable;
 5952:     my $numinrow = 4;
 5953:     foreach my $type (@{$types}) {
 5954:         if (defined($usertypes->{$type})) {
 5955:             $$rownums ++;
 5956:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 5957:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 5958:                           '</td><td class="LC_left_item"><table>';
 5959:             for (my $i=0; $i<@{$fields}; $i++) {
 5960:                 my $rem = $i%($numinrow);
 5961:                 if ($rem == 0) {
 5962:                     if ($i > 0) {
 5963:                         $datatable .= '</tr>';
 5964:                     }
 5965:                     $datatable .= '<tr>';
 5966:                 }
 5967:                 my $check = ' ';
 5968:                 if (ref($settings) eq 'HASH') {
 5969:                     if (ref($settings->{'fields'}) eq 'HASH') {
 5970:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 5971:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 5972:                                 $check = ' checked="checked" ';
 5973:                             }
 5974:                         }
 5975:                     }
 5976:                 }
 5977: 
 5978:                 if ($i == @{$fields}-1) {
 5979:                     my $colsleft = $numinrow - $rem;
 5980:                     if ($colsleft > 1) {
 5981:                         $datatable .= '<td colspan="'.$colsleft.'">';
 5982:                     } else {
 5983:                         $datatable .= '<td>';
 5984:                     }
 5985:                 } else {
 5986:                     $datatable .= '<td>';
 5987:                 }
 5988:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5989:                               '<input type="checkbox" name="updateable_'.$type.
 5990:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 5991:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 5992:             }
 5993:             $datatable .= '</tr></table></td></tr>';
 5994:         }
 5995:     }
 5996:     return $datatable;
 5997: }
 5998: 
 5999: sub modify_login {
 6000:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 6001:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 6002:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 6003:     %title = ( coursecatalog => 'Display course catalog',
 6004:                adminmail => 'Display administrator E-mail address',
 6005:                helpdesk  => 'Display "Contact Helpdesk" link',
 6006:                newuser => 'Link for visitors to create a user account',
 6007:                loginheader => 'Log-in box header');
 6008:     @offon = ('off','on');
 6009:     if (ref($domconfig{login}) eq 'HASH') {
 6010:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 6011:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 6012:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 6013:             }
 6014:         }
 6015:     }
 6016:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 6017:                                            \%domconfig,\%loginhash);
 6018:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 6019:     foreach my $item (@toggles) {
 6020:         $loginhash{login}{$item} = $env{'form.'.$item};
 6021:     }
 6022:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 6023:     if (ref($colchanges{'login'}) eq 'HASH') {  
 6024:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 6025:                                          \%loginhash);
 6026:     }
 6027: 
 6028:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 6029:     my %domservers = &Apache::lonnet::get_servers($dom);
 6030:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 6031:     if (keys(%servers) > 1) {
 6032:         foreach my $lonhost (keys(%servers)) {
 6033:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 6034:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 6035:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 6036:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 6037:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 6038:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 6039:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 6040:                         $changes{'loginvia'}{$lonhost} = 1;
 6041:                     } else {
 6042:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 6043:                         $changes{'loginvia'}{$lonhost} = 1;
 6044:                     }
 6045:                 } else {
 6046:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 6047:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 6048:                         $changes{'loginvia'}{$lonhost} = 1;
 6049:                     }
 6050:                 }
 6051:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 6052:                     foreach my $item (@loginvia_attribs) {
 6053:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 6054:                     }
 6055:                 } else {
 6056:                     foreach my $item (@loginvia_attribs) {
 6057:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 6058:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 6059:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 6060:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 6061:                                 $new = '/';
 6062:                             }
 6063:                         }
 6064:                         if (($item eq 'custompath') && 
 6065:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 6066:                             $new = '';
 6067:                         }
 6068:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 6069:                             $changes{'loginvia'}{$lonhost} = 1;
 6070:                         }
 6071:                         if ($item eq 'exempt') {
 6072:                             $new = &check_exempt_addresses($new);
 6073:                         }
 6074:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 6075:                     }
 6076:                 }
 6077:             } else {
 6078:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 6079:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 6080:                     $changes{'loginvia'}{$lonhost} = 1;
 6081:                     foreach my $item (@loginvia_attribs) {
 6082:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 6083:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 6084:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 6085:                                 $new = '/';
 6086:                             }
 6087:                         }
 6088:                         if (($item eq 'custompath') && 
 6089:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 6090:                             $new = '';
 6091:                         }
 6092:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 6093:                     }
 6094:                 }
 6095:             }
 6096:         }
 6097:     }
 6098: 
 6099:     my $servadm = $r->dir_config('lonAdmEMail');
 6100:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 6101:     if (ref($domconfig{'login'}) eq 'HASH') {
 6102:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 6103:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 6104:                 if ($lang eq 'nolang') {
 6105:                     push(@currlangs,$lang);
 6106:                 } elsif (defined($langchoices{$lang})) {
 6107:                     push(@currlangs,$lang);
 6108:                 } else {
 6109:                     next;
 6110:                 }
 6111:             }
 6112:         }
 6113:     }
 6114:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 6115:     if (@currlangs > 0) {
 6116:         foreach my $lang (@currlangs) {
 6117:             if (grep(/^\Q$lang\E$/,@delurls)) {
 6118:                 $changes{'helpurl'}{$lang} = 1;
 6119:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 6120:                 $changes{'helpurl'}{$lang} = 1;
 6121:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 6122:                 push(@newlangs,$lang);
 6123:             } else {
 6124:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 6125:             }
 6126:         }
 6127:     }
 6128:     unless (grep(/^nolang$/,@currlangs)) {
 6129:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 6130:             $changes{'helpurl'}{'nolang'} = 1;
 6131:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 6132:             push(@newlangs,'nolang');
 6133:         }
 6134:     }
 6135:     if ($env{'form.loginhelpurl_add_lang'}) {
 6136:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 6137:             ($env{'form.loginhelpurl_add_file.filename'})) {
 6138:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 6139:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 6140:         }
 6141:     }
 6142:     if ((@newlangs > 0) || ($addedfile)) {
 6143:         my $error;
 6144:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6145:         if ($configuserok eq 'ok') {
 6146:             if ($switchserver) {
 6147:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 6148:             } elsif ($author_ok eq 'ok') {
 6149:                 my @allnew = @newlangs;
 6150:                 if ($addedfile ne '') {
 6151:                     push(@allnew,$addedfile);
 6152:                 }
 6153:                 foreach my $lang (@allnew) {
 6154:                     my $formelem = 'loginhelpurl_'.$lang;
 6155:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 6156:                         $formelem = 'loginhelpurl_add_file';
 6157:                     }
 6158:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 6159:                                                                "help/$lang",'','',$newfile{$lang});
 6160:                     if ($result eq 'ok') {
 6161:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 6162:                         $changes{'helpurl'}{$lang} = 1;
 6163:                     } else {
 6164:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 6165:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 6166:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 6167:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 6168: 
 6169:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 6170:                         }
 6171:                     }
 6172:                 }
 6173:             } else {
 6174:                 $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);
 6175:             }
 6176:         } else {
 6177:             $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);
 6178:         }
 6179:         if ($error) {
 6180:             &Apache::lonnet::logthis($error);
 6181:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6182:         }
 6183:     }
 6184: 
 6185:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 6186:     if (ref($domconfig{'login'}) eq 'HASH') {
 6187:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 6188:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 6189:                 if ($domservers{$lonhost}) {
 6190:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 6191:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 6192:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'}
 6193:                     }
 6194:                 }
 6195:             }
 6196:         }
 6197:     }
 6198:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 6199:     foreach my $lonhost (sort(keys(%domservers))) {
 6200:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 6201:             $changes{'headtag'}{$lonhost} = 1;
 6202:         } else {
 6203:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 6204:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 6205:             }
 6206:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 6207:                 push(@newhosts,$lonhost);
 6208:             } elsif ($currheadtagurls{$lonhost}) {
 6209:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 6210:                 if ($currexempt{$lonhost}) {
 6211:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 6212:                         $changes{'headtag'}{$lonhost} = 1;
 6213:                     }
 6214:                 } elsif ($possexempt{$lonhost}) {
 6215:                     $changes{'headtag'}{$lonhost} = 1;
 6216:                 }
 6217:                 if ($possexempt{$lonhost}) {
 6218:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 6219:                 }
 6220:             }
 6221:         }
 6222:     }
 6223:     if (@newhosts) {
 6224:         my $error;
 6225:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6226:         if ($configuserok eq 'ok') {
 6227:             if ($switchserver) {
 6228:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 6229:             } elsif ($author_ok eq 'ok') {
 6230:                 foreach my $lonhost (@newhosts) {
 6231:                     my $formelem = 'loginheadtag_'.$lonhost;
 6232:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 6233:                                                                           "login/headtag/$lonhost",'','',
 6234:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 6235:                     if ($result eq 'ok') {
 6236:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 6237:                         $changes{'headtag'}{$lonhost} = 1;
 6238:                         if ($possexempt{$lonhost}) {
 6239:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 6240:                         }
 6241:                     } else {
 6242:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 6243:                                            $newheadtagurls{$lonhost},$result);
 6244:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 6245:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 6246:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 6247:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 6248:                         }
 6249:                     }
 6250:                 }
 6251:             } else {
 6252:                 $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);
 6253:             }
 6254:         } else {
 6255:             $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);
 6256:         }
 6257:         if ($error) {
 6258:             &Apache::lonnet::logthis($error);
 6259:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6260:         }
 6261:     }
 6262: 
 6263:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 6264: 
 6265:     my $defaulthelpfile = '/adm/loginproblems.html';
 6266:     my $defaulttext = &mt('Default in use');
 6267: 
 6268:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 6269:                                              $dom);
 6270:     if ($putresult eq 'ok') {
 6271:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 6272:         my %defaultchecked = (
 6273:                     'coursecatalog' => 'on',
 6274:                     'helpdesk'      => 'on',
 6275:                     'adminmail'     => 'off',
 6276:                     'newuser'       => 'off',
 6277:         );
 6278:         if (ref($domconfig{'login'}) eq 'HASH') {
 6279:             foreach my $item (@toggles) {
 6280:                 if ($defaultchecked{$item} eq 'on') { 
 6281:                     if (($domconfig{'login'}{$item} eq '0') &&
 6282:                         ($env{'form.'.$item} eq '1')) {
 6283:                         $changes{$item} = 1;
 6284:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 6285:                               $domconfig{'login'}{$item} eq '1') &&
 6286:                              ($env{'form.'.$item} eq '0')) {
 6287:                         $changes{$item} = 1;
 6288:                     }
 6289:                 } elsif ($defaultchecked{$item} eq 'off') {
 6290:                     if (($domconfig{'login'}{$item} eq '1') &&
 6291:                         ($env{'form.'.$item} eq '0')) {
 6292:                         $changes{$item} = 1;
 6293:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 6294:                               $domconfig{'login'}{$item} eq '0') &&
 6295:                              ($env{'form.'.$item} eq '1')) {
 6296:                         $changes{$item} = 1;
 6297:                     }
 6298:                 }
 6299:             }
 6300:         }
 6301:         if (keys(%changes) > 0 || $colchgtext) {
 6302:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6303:             if (ref($lastactref) eq 'HASH') {
 6304:                 $lastactref->{'domainconfig'} = 1;
 6305:             }
 6306:             $resulttext = &mt('Changes made:').'<ul>';
 6307:             foreach my $item (sort(keys(%changes))) {
 6308:                 if ($item eq 'loginvia') {
 6309:                     if (ref($changes{$item}) eq 'HASH') {
 6310:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 6311:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 6312:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 6313:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 6314:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 6315:                                     $protocol = 'http' if ($protocol ne 'https');
 6316:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 6317: 
 6318:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 6319:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 6320:                                     } else {
 6321:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 6322:                                     }
 6323:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 6324:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 6325:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 6326:                                     }
 6327:                                     $resulttext .= '</li>';
 6328:                                 } else {
 6329:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 6330:                                 }
 6331:                             } else {
 6332:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 6333:                             }
 6334:                         }
 6335:                         $resulttext .= '</ul></li>';
 6336:                     }
 6337:                 } elsif ($item eq 'helpurl') {
 6338:                     if (ref($changes{$item}) eq 'HASH') {
 6339:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 6340:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 6341:                                 my ($chg,$link);
 6342:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 6343:                                 if ($lang eq 'nolang') {
 6344:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 6345:                                 } else {
 6346:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 6347:                                 }
 6348:                                 $resulttext .= '<li>'.$chg.'</li>';
 6349:                             } else {
 6350:                                 my $chg;
 6351:                                 if ($lang eq 'nolang') {
 6352:                                     $chg = &mt('custom log-in help file for no preferred language');
 6353:                                 } else {
 6354:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 6355:                                 }
 6356:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 6357:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 6358:                                                       '?inhibitmenu=yes',$chg,600,500).
 6359:                                                '</li>';
 6360:                             }
 6361:                         }
 6362:                     }
 6363:                 } elsif ($item eq 'headtag') {
 6364:                     if (ref($changes{$item}) eq 'HASH') {
 6365:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 6366:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 6367:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 6368:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 6369:                                 $resulttext .= '<li><a href="'.
 6370:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 6371:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 6372:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 6373:                                 if ($possexempt{$lonhost}) {
 6374:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 6375:                                 } else {
 6376:                                     $resulttext .= &mt('included for any client IP');
 6377:                                 }
 6378:                                 $resulttext .= '</li>';
 6379:                             }
 6380:                         }
 6381:                     }
 6382:                 } elsif ($item eq 'captcha') {
 6383:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6384:                         my $chgtxt;
 6385:                         if ($loginhash{'login'}{$item} eq 'notused') {
 6386:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 6387:                         } else {
 6388:                             my %captchas = &captcha_phrases();
 6389:                             if ($captchas{$loginhash{'login'}{$item}}) {
 6390:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 6391:                             } else {
 6392:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 6393:                             }
 6394:                         }
 6395:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6396:                     }
 6397:                 } elsif ($item eq 'recaptchakeys') {
 6398:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6399:                         my ($privkey,$pubkey);
 6400:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 6401:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 6402:                             $privkey = $loginhash{'login'}{$item}{'private'};
 6403:                         }
 6404:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 6405:                         if (!$pubkey) {
 6406:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 6407:                         } else {
 6408:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6409:                         }
 6410:                         if (!$privkey) {
 6411:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 6412:                         } else {
 6413:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 6414:                         }
 6415:                         $chgtxt .= '</ul>';
 6416:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6417:                     }
 6418:                 } elsif ($item eq 'recaptchaversion') {
 6419:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6420:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 6421:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 6422:                                            '</li>';
 6423:                         }
 6424:                     }
 6425:                 } else {
 6426:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 6427:                 }
 6428:             }
 6429:             $resulttext .= $colchgtext.'</ul>';
 6430:         } else {
 6431:             $resulttext = &mt('No changes made to log-in page settings');
 6432:         }
 6433:     } else {
 6434:         $resulttext = '<span class="LC_error">'.
 6435: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6436:     }
 6437:     if ($errors) {
 6438:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 6439:                        $errors.'</ul>';
 6440:     }
 6441:     return $resulttext;
 6442: }
 6443: 
 6444: sub check_exempt_addresses {
 6445:     my ($iplist) = @_;
 6446:     $iplist =~ s/^\s+//;
 6447:     $iplist =~ s/\s+$//;
 6448:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 6449:     my (@okips,$new);
 6450:     foreach my $ip (@poss_ips) {
 6451:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 6452:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 6453:                 push(@okips,$ip);
 6454:             }
 6455:         }
 6456:     }
 6457:     if (@okips > 0) {
 6458:         $new = join(',',@okips);
 6459:     } else {
 6460:         $new = '';
 6461:     }
 6462:     return $new;
 6463: }
 6464: 
 6465: sub color_font_choices {
 6466:     my %choices =
 6467:         &Apache::lonlocal::texthash (
 6468:             img => "Header",
 6469:             bgs => "Background colors",
 6470:             links => "Link colors",
 6471:             images => "Images",
 6472:             font => "Font color",
 6473:             fontmenu => "Font menu",
 6474:             pgbg => "Page",
 6475:             tabbg => "Header",
 6476:             sidebg => "Border",
 6477:             link => "Link",
 6478:             alink => "Active link",
 6479:             vlink => "Visited link",
 6480:         );
 6481:     return %choices;
 6482: }
 6483: 
 6484: sub modify_rolecolors {
 6485:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 6486:     my ($resulttext,%rolehash);
 6487:     $rolehash{'rolecolors'} = {};
 6488:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 6489:         if ($domconfig{'rolecolors'} eq '') {
 6490:             $domconfig{'rolecolors'} = {};
 6491:         }
 6492:     }
 6493:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 6494:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 6495:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 6496:                                              $dom);
 6497:     if ($putresult eq 'ok') {
 6498:         if (keys(%changes) > 0) {
 6499:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6500:             if (ref($lastactref) eq 'HASH') {
 6501:                 $lastactref->{'domainconfig'} = 1;
 6502:             }
 6503:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 6504:                                              $rolehash{'rolecolors'});
 6505:         } else {
 6506:             $resulttext = &mt('No changes made to default color schemes');
 6507:         }
 6508:     } else {
 6509:         $resulttext = '<span class="LC_error">'.
 6510: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6511:     }
 6512:     if ($errors) {
 6513:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 6514:                        $errors.'</ul>';
 6515:     }
 6516:     return $resulttext;
 6517: }
 6518: 
 6519: sub modify_colors {
 6520:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 6521:     my (%changes,%choices);
 6522:     my @bgs;
 6523:     my @links = ('link','alink','vlink');
 6524:     my @logintext;
 6525:     my @images;
 6526:     my $servadm = $r->dir_config('lonAdmEMail');
 6527:     my $errors;
 6528:     my %defaults;
 6529:     foreach my $role (@{$roles}) {
 6530:         if ($role eq 'login') {
 6531:             %choices = &login_choices();
 6532:             @logintext = ('textcol','bgcol');
 6533:         } else {
 6534:             %choices = &color_font_choices();
 6535:         }
 6536:         if ($role eq 'login') {
 6537:             @images = ('img','logo','domlogo','login');
 6538:             @bgs = ('pgbg','mainbg','sidebg');
 6539:         } else {
 6540:             @images = ('img');
 6541:             @bgs = ('pgbg','tabbg','sidebg');
 6542:         }
 6543:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 6544:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 6545:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 6546:         }
 6547:         if ($role eq 'login') {
 6548:             foreach my $item (@logintext) {
 6549:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6550:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6551:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6552:                 }
 6553:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 6554:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6555:                 }
 6556:             }
 6557:         } else {
 6558:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 6559:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 6560:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 6561:             }
 6562:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 6563:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 6564:             }
 6565:         }
 6566:         foreach my $item (@bgs) {
 6567:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6568:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6569:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6570:             }
 6571:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 6572:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6573:             }
 6574:         }
 6575:         foreach my $item (@links) {
 6576:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6577:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6578:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6579:             }
 6580:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 6581:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6582:             }
 6583:         }
 6584:         my ($configuserok,$author_ok,$switchserver) = 
 6585:             &config_check($dom,$confname,$servadm);
 6586:         my ($width,$height) = &thumb_dimensions();
 6587:         if (ref($domconfig->{$role}) ne 'HASH') {
 6588:             $domconfig->{$role} = {};
 6589:         }
 6590:         foreach my $img (@images) {
 6591:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 6592:                 if (defined($env{'form.login_showlogo_'.$img})) {
 6593:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 6594:                 } else { 
 6595:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 6596:                 }
 6597:             } 
 6598: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 6599: 		 && !defined($domconfig->{$role}{$img})
 6600: 		 && !$env{'form.'.$role.'_del_'.$img}
 6601: 		 && $env{'form.'.$role.'_import_'.$img}) {
 6602: 		# import the old configured image from the .tab setting
 6603: 		# if they haven't provided a new one 
 6604: 		$domconfig->{$role}{$img} = 
 6605: 		    $env{'form.'.$role.'_import_'.$img};
 6606: 	    }
 6607:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 6608:                 my $error;
 6609:                 if ($configuserok eq 'ok') {
 6610:                     if ($switchserver) {
 6611:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 6612:                     } else {
 6613:                         if ($author_ok eq 'ok') {
 6614:                             my ($result,$logourl) = 
 6615:                                 &publishlogo($r,'upload',$role.'_'.$img,
 6616:                                            $dom,$confname,$img,$width,$height);
 6617:                             if ($result eq 'ok') {
 6618:                                 $confhash->{$role}{$img} = $logourl;
 6619:                                 $changes{$role}{'images'}{$img} = 1;
 6620:                             } else {
 6621:                                 $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);
 6622:                             }
 6623:                         } else {
 6624:                             $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);
 6625:                         }
 6626:                     }
 6627:                 } else {
 6628:                     $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);
 6629:                 }
 6630:                 if ($error) {
 6631:                     &Apache::lonnet::logthis($error);
 6632:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6633:                 }
 6634:             } elsif ($domconfig->{$role}{$img} ne '') {
 6635:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 6636:                     my $error;
 6637:                     if ($configuserok eq 'ok') {
 6638: # is confname an author?
 6639:                         if ($switchserver eq '') {
 6640:                             if ($author_ok eq 'ok') {
 6641:                                 my ($result,$logourl) = 
 6642:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 6643:                                             $dom,$confname,$img,$width,$height);
 6644:                                 if ($result eq 'ok') {
 6645:                                     $confhash->{$role}{$img} = $logourl;
 6646: 				    $changes{$role}{'images'}{$img} = 1;
 6647:                                 }
 6648:                             }
 6649:                         }
 6650:                     }
 6651:                 }
 6652:             }
 6653:         }
 6654:         if (ref($domconfig) eq 'HASH') {
 6655:             if (ref($domconfig->{$role}) eq 'HASH') {
 6656:                 foreach my $img (@images) {
 6657:                     if ($domconfig->{$role}{$img} ne '') {
 6658:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6659:                             $confhash->{$role}{$img} = '';
 6660:                             $changes{$role}{'images'}{$img} = 1;
 6661:                         } else {
 6662:                             if ($confhash->{$role}{$img} eq '') {
 6663:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 6664:                             }
 6665:                         }
 6666:                     } else {
 6667:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6668:                             $confhash->{$role}{$img} = '';
 6669:                             $changes{$role}{'images'}{$img} = 1;
 6670:                         } 
 6671:                     }
 6672:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 6673:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 6674:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 6675:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 6676:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 6677:                             }
 6678:                         } else {
 6679:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6680:                                 $changes{$role}{'showlogo'}{$img} = 1;
 6681:                             }
 6682:                         }
 6683:                     }
 6684:                 }
 6685:                 if ($domconfig->{$role}{'font'} ne '') {
 6686:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 6687:                         $changes{$role}{'font'} = 1;
 6688:                     }
 6689:                 } else {
 6690:                     if ($confhash->{$role}{'font'}) {
 6691:                         $changes{$role}{'font'} = 1;
 6692:                     }
 6693:                 }
 6694:                 if ($role ne 'login') {
 6695:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 6696:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 6697:                             $changes{$role}{'fontmenu'} = 1;
 6698:                         }
 6699:                     } else {
 6700:                         if ($confhash->{$role}{'fontmenu'}) {
 6701:                             $changes{$role}{'fontmenu'} = 1;
 6702:                         }
 6703:                     }
 6704:                 }
 6705:                 foreach my $item (@bgs) {
 6706:                     if ($domconfig->{$role}{$item} ne '') {
 6707:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6708:                             $changes{$role}{'bgs'}{$item} = 1;
 6709:                         } 
 6710:                     } else {
 6711:                         if ($confhash->{$role}{$item}) {
 6712:                             $changes{$role}{'bgs'}{$item} = 1;
 6713:                         }
 6714:                     }
 6715:                 }
 6716:                 foreach my $item (@links) {
 6717:                     if ($domconfig->{$role}{$item} ne '') {
 6718:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6719:                             $changes{$role}{'links'}{$item} = 1;
 6720:                         }
 6721:                     } else {
 6722:                         if ($confhash->{$role}{$item}) {
 6723:                             $changes{$role}{'links'}{$item} = 1;
 6724:                         }
 6725:                     }
 6726:                 }
 6727:                 foreach my $item (@logintext) {
 6728:                     if ($domconfig->{$role}{$item} ne '') {
 6729:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6730:                             $changes{$role}{'logintext'}{$item} = 1;
 6731:                         }
 6732:                     } else {
 6733:                         if ($confhash->{$role}{$item}) {
 6734:                             $changes{$role}{'logintext'}{$item} = 1;
 6735:                         }
 6736:                     }
 6737:                 }
 6738:             } else {
 6739:                 &default_change_checker($role,\@images,\@links,\@bgs,
 6740:                                         \@logintext,$confhash,\%changes); 
 6741:             }
 6742:         } else {
 6743:             &default_change_checker($role,\@images,\@links,\@bgs,
 6744:                                     \@logintext,$confhash,\%changes); 
 6745:         }
 6746:     }
 6747:     return ($errors,%changes);
 6748: }
 6749: 
 6750: sub config_check {
 6751:     my ($dom,$confname,$servadm) = @_;
 6752:     my ($configuserok,$author_ok,$switchserver,%currroles);
 6753:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 6754:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 6755:                                                    $confname,$servadm);
 6756:     if ($configuserok eq 'ok') {
 6757:         $switchserver = &check_switchserver($dom,$confname);
 6758:         if ($switchserver eq '') {
 6759:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 6760:         }
 6761:     }
 6762:     return ($configuserok,$author_ok,$switchserver);
 6763: }
 6764: 
 6765: sub default_change_checker {
 6766:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 6767:     foreach my $item (@{$links}) {
 6768:         if ($confhash->{$role}{$item}) {
 6769:             $changes->{$role}{'links'}{$item} = 1;
 6770:         }
 6771:     }
 6772:     foreach my $item (@{$bgs}) {
 6773:         if ($confhash->{$role}{$item}) {
 6774:             $changes->{$role}{'bgs'}{$item} = 1;
 6775:         }
 6776:     }
 6777:     foreach my $item (@{$logintext}) {
 6778:         if ($confhash->{$role}{$item}) {
 6779:             $changes->{$role}{'logintext'}{$item} = 1;
 6780:         }
 6781:     }
 6782:     foreach my $img (@{$images}) {
 6783:         if ($env{'form.'.$role.'_del_'.$img}) {
 6784:             $confhash->{$role}{$img} = '';
 6785:             $changes->{$role}{'images'}{$img} = 1;
 6786:         }
 6787:         if ($role eq 'login') {
 6788:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6789:                 $changes->{$role}{'showlogo'}{$img} = 1;
 6790:             }
 6791:         }
 6792:     }
 6793:     if ($confhash->{$role}{'font'}) {
 6794:         $changes->{$role}{'font'} = 1;
 6795:     }
 6796: }
 6797: 
 6798: sub display_colorchgs {
 6799:     my ($dom,$changes,$roles,$confhash) = @_;
 6800:     my (%choices,$resulttext);
 6801:     if (!grep(/^login$/,@{$roles})) {
 6802:         $resulttext = &mt('Changes made:').'<br />';
 6803:     }
 6804:     foreach my $role (@{$roles}) {
 6805:         if ($role eq 'login') {
 6806:             %choices = &login_choices();
 6807:         } else {
 6808:             %choices = &color_font_choices();
 6809:         }
 6810:         if (ref($changes->{$role}) eq 'HASH') {
 6811:             if ($role ne 'login') {
 6812:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 6813:             }
 6814:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 6815:                 if ($role ne 'login') {
 6816:                     $resulttext .= '<ul>';
 6817:                 }
 6818:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 6819:                     if ($role ne 'login') {
 6820:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 6821:                     }
 6822:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 6823:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 6824:                             if ($confhash->{$role}{$key}{$item}) {
 6825:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 6826:                             } else {
 6827:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 6828:                             }
 6829:                         } elsif ($confhash->{$role}{$item} eq '') {
 6830:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 6831:                         } else {
 6832:                             my $newitem = $confhash->{$role}{$item};
 6833:                             if ($key eq 'images') {
 6834:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 6835:                             }
 6836:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 6837:                         }
 6838:                     }
 6839:                     if ($role ne 'login') {
 6840:                         $resulttext .= '</ul></li>';
 6841:                     }
 6842:                 } else {
 6843:                     if ($confhash->{$role}{$key} eq '') {
 6844:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 6845:                     } else {
 6846:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 6847:                     }
 6848:                 }
 6849:                 if ($role ne 'login') {
 6850:                     $resulttext .= '</ul>';
 6851:                 }
 6852:             }
 6853:         }
 6854:     }
 6855:     return $resulttext;
 6856: }
 6857: 
 6858: sub thumb_dimensions {
 6859:     return ('200','50');
 6860: }
 6861: 
 6862: sub check_dimensions {
 6863:     my ($inputfile) = @_;
 6864:     my ($fullwidth,$fullheight);
 6865:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 6866:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 6867:             my $imageinfo = <PIPE>;
 6868:             if (!close(PIPE)) {
 6869:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 6870:             }
 6871:             chomp($imageinfo);
 6872:             my ($fullsize) = 
 6873:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 6874:             if ($fullsize) {
 6875:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 6876:             }
 6877:         }
 6878:     }
 6879:     return ($fullwidth,$fullheight);
 6880: }
 6881: 
 6882: sub check_configuser {
 6883:     my ($uhome,$dom,$confname,$servadm) = @_;
 6884:     my ($configuserok,%currroles);
 6885:     if ($uhome eq 'no_host') {
 6886:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 6887:         my $configpass = &LONCAPA::Enrollment::create_password();
 6888:         $configuserok = 
 6889:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 6890:                              $configpass,'','','','','',undef,$servadm);
 6891:     } else {
 6892:         $configuserok = 'ok';
 6893:         %currroles = 
 6894:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 6895:     }
 6896:     return ($configuserok,%currroles);
 6897: }
 6898: 
 6899: sub check_authorstatus {
 6900:     my ($dom,$confname,%currroles) = @_;
 6901:     my $author_ok;
 6902:     if (!$currroles{':'.$dom.':au'}) {
 6903:         my $start = time;
 6904:         my $end = 0;
 6905:         $author_ok = 
 6906:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 6907:                                         'au',$end,$start,'','','domconfig');
 6908:     } else {
 6909:         $author_ok = 'ok';
 6910:     }
 6911:     return $author_ok;
 6912: }
 6913: 
 6914: sub publishlogo {
 6915:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 6916:     my ($output,$fname,$logourl);
 6917:     if ($action eq 'upload') {
 6918:         $fname=$env{'form.'.$formname.'.filename'};
 6919:         chop($env{'form.'.$formname});
 6920:     } else {
 6921:         ($fname) = ($formname =~ /([^\/]+)$/);
 6922:     }
 6923:     if ($savefileas ne '') {
 6924:         $fname = $savefileas;
 6925:     }
 6926:     $fname=&Apache::lonnet::clean_filename($fname);
 6927: # See if there is anything left
 6928:     unless ($fname) { return ('error: no uploaded file'); }
 6929:     $fname="$subdir/$fname";
 6930:     my $docroot=$r->dir_config('lonDocRoot');
 6931:     my $filepath="$docroot/priv";
 6932:     my $relpath = "$dom/$confname";
 6933:     my ($fnamepath,$file,$fetchthumb);
 6934:     $file=$fname;
 6935:     if ($fname=~m|/|) {
 6936:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 6937:     }
 6938:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 6939:     my $count;
 6940:     for ($count=5;$count<=$#parts;$count++) {
 6941:         $filepath.="/$parts[$count]";
 6942:         if ((-e $filepath)!=1) {
 6943:             mkdir($filepath,02770);
 6944:         }
 6945:     }
 6946:     # Check for bad extension and disallow upload
 6947:     if ($file=~/\.(\w+)$/ &&
 6948:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 6949:         $output = 
 6950:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 6951:     } elsif ($file=~/\.(\w+)$/ &&
 6952:         !defined(&Apache::loncommon::fileembstyle($1))) {
 6953:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 6954:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 6955:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 6956:     } elsif (-d "$filepath/$file") {
 6957:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 6958:     } else {
 6959:         my $source = $filepath.'/'.$file;
 6960:         my $logfile;
 6961:         if (!open($logfile,">>$source".'.log')) {
 6962:             return (&mt('No write permission to Authoring Space'));
 6963:         }
 6964:         print $logfile
 6965: "\n================= Publish ".localtime()." ================\n".
 6966: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 6967: # Save the file
 6968:         if (!open(FH,'>'.$source)) {
 6969:             &Apache::lonnet::logthis('Failed to create '.$source);
 6970:             return (&mt('Failed to create file'));
 6971:         }
 6972:         if ($action eq 'upload') {
 6973:             if (!print FH ($env{'form.'.$formname})) {
 6974:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 6975:                 return (&mt('Failed to write file'));
 6976:             }
 6977:         } else {
 6978:             my $original = &Apache::lonnet::filelocation('',$formname);
 6979:             if(!copy($original,$source)) {
 6980:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 6981:                 return (&mt('Failed to write file'));
 6982:             }
 6983:         }
 6984:         close(FH);
 6985:         chmod(0660, $source); # Permissions to rw-rw---.
 6986: 
 6987:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 6988:         my $copyfile=$targetdir.'/'.$file;
 6989: 
 6990:         my @parts=split(/\//,$targetdir);
 6991:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 6992:         for (my $count=5;$count<=$#parts;$count++) {
 6993:             $path.="/$parts[$count]";
 6994:             if (!-e $path) {
 6995:                 print $logfile "\nCreating directory ".$path;
 6996:                 mkdir($path,02770);
 6997:             }
 6998:         }
 6999:         my $versionresult;
 7000:         if (-e $copyfile) {
 7001:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 7002:         } else {
 7003:             $versionresult = 'ok';
 7004:         }
 7005:         if ($versionresult eq 'ok') {
 7006:             if (copy($source,$copyfile)) {
 7007:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 7008:                 $output = 'ok';
 7009:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 7010:                 push(@{$modified_urls},[$copyfile,$source]);
 7011:                 my $metaoutput = 
 7012:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 7013:                 unless ($registered_cleanup) {
 7014:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 7015:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 7016:                     $registered_cleanup=1;
 7017:                 }
 7018:             } else {
 7019:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 7020:                 $output = &mt('Failed to copy file to RES space').", $!";
 7021:             }
 7022:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 7023:                 my $inputfile = $filepath.'/'.$file;
 7024:                 my $outfile = $filepath.'/'.'tn-'.$file;
 7025:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 7026:                 if ($fullwidth ne '' && $fullheight ne '') { 
 7027:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 7028:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 7029:                         system("convert -sample $thumbsize $inputfile $outfile");
 7030:                         chmod(0660, $filepath.'/tn-'.$file);
 7031:                         if (-e $outfile) {
 7032:                             my $copyfile=$targetdir.'/tn-'.$file;
 7033:                             if (copy($outfile,$copyfile)) {
 7034:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 7035:                                 my $thumb_metaoutput = 
 7036:                                     &write_metadata($dom,$confname,$formname,
 7037:                                                     $targetdir,'tn-'.$file,$logfile);
 7038:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 7039:                                 unless ($registered_cleanup) {
 7040:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 7041:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 7042:                                     $registered_cleanup=1;
 7043:                                 }
 7044:                             } else {
 7045:                                 print $logfile "\nUnable to write ".$copyfile.
 7046:                                                ':'.$!."\n";
 7047:                             }
 7048:                         }
 7049:                     }
 7050:                 }
 7051:             }
 7052:         } else {
 7053:             $output = $versionresult;
 7054:         }
 7055:     }
 7056:     return ($output,$logourl);
 7057: }
 7058: 
 7059: sub logo_versioning {
 7060:     my ($targetdir,$file,$logfile) = @_;
 7061:     my $target = $targetdir.'/'.$file;
 7062:     my ($maxversion,$fn,$extn,$output);
 7063:     $maxversion = 0;
 7064:     if ($file =~ /^(.+)\.(\w+)$/) {
 7065:         $fn=$1;
 7066:         $extn=$2;
 7067:     }
 7068:     opendir(DIR,$targetdir);
 7069:     while (my $filename=readdir(DIR)) {
 7070:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 7071:             $maxversion=($1>$maxversion)?$1:$maxversion;
 7072:         }
 7073:     }
 7074:     $maxversion++;
 7075:     print $logfile "\nCreating old version ".$maxversion."\n";
 7076:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 7077:     if (copy($target,$copyfile)) {
 7078:         print $logfile "Copied old target to ".$copyfile."\n";
 7079:         $copyfile=$copyfile.'.meta';
 7080:         if (copy($target.'.meta',$copyfile)) {
 7081:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 7082:             $output = 'ok';
 7083:         } else {
 7084:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 7085:             $output = &mt('Failed to copy old meta').", $!, ";
 7086:         }
 7087:     } else {
 7088:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 7089:         $output = &mt('Failed to copy old target').", $!, ";
 7090:     }
 7091:     return $output;
 7092: }
 7093: 
 7094: sub write_metadata {
 7095:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 7096:     my (%metadatafields,%metadatakeys,$output);
 7097:     $metadatafields{'title'}=$formname;
 7098:     $metadatafields{'creationdate'}=time;
 7099:     $metadatafields{'lastrevisiondate'}=time;
 7100:     $metadatafields{'copyright'}='public';
 7101:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 7102:                                          $env{'user.domain'};
 7103:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 7104:     $metadatafields{'domain'}=$dom;
 7105:     {
 7106:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 7107:         my $mfh;
 7108:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 7109:             foreach (sort(keys(%metadatafields))) {
 7110:                 unless ($_=~/\./) {
 7111:                     my $unikey=$_;
 7112:                     $unikey=~/^([A-Za-z]+)/;
 7113:                     my $tag=$1;
 7114:                     $tag=~tr/A-Z/a-z/;
 7115:                     print $mfh "\n\<$tag";
 7116:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 7117:                         my $value=$metadatafields{$unikey.'.'.$_};
 7118:                         $value=~s/\"/\'\'/g;
 7119:                         print $mfh ' '.$_.'="'.$value.'"';
 7120:                     }
 7121:                     print $mfh '>'.
 7122:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 7123:                             .'</'.$tag.'>';
 7124:                 }
 7125:             }
 7126:             $output = 'ok';
 7127:             print $logfile "\nWrote metadata";
 7128:             close($mfh);
 7129:         } else {
 7130:             print $logfile "\nFailed to open metadata file";
 7131:             $output = &mt('Could not write metadata');
 7132:         }
 7133:     }
 7134:     return $output;
 7135: }
 7136: 
 7137: sub notifysubscribed {
 7138:     foreach my $targetsource (@{$modified_urls}){
 7139:         next unless (ref($targetsource) eq 'ARRAY');
 7140:         my ($target,$source)=@{$targetsource};
 7141:         if ($source ne '') {
 7142:             if (open(my $logfh,'>>'.$source.'.log')) {
 7143:                 print $logfh "\nCleanup phase: Notifications\n";
 7144:                 my @subscribed=&subscribed_hosts($target);
 7145:                 foreach my $subhost (@subscribed) {
 7146:                     print $logfh "\nNotifying host ".$subhost.':';
 7147:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 7148:                     print $logfh $reply;
 7149:                 }
 7150:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 7151:                 foreach my $subhost (@subscribedmeta) {
 7152:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 7153:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 7154:                                                         $subhost);
 7155:                     print $logfh $reply;
 7156:                 }
 7157:                 print $logfh "\n============ Done ============\n";
 7158:                 close($logfh);
 7159:             }
 7160:         }
 7161:     }
 7162:     return OK;
 7163: }
 7164: 
 7165: sub subscribed_hosts {
 7166:     my ($target) = @_;
 7167:     my @subscribed;
 7168:     if (open(my $fh,"<$target.subscription")) {
 7169:         while (my $subline=<$fh>) {
 7170:             if ($subline =~ /^($match_lonid):/) {
 7171:                 my $host = $1;
 7172:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 7173:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 7174:                         push(@subscribed,$host);
 7175:                     }
 7176:                 }
 7177:             }
 7178:         }
 7179:     }
 7180:     return @subscribed;
 7181: }
 7182: 
 7183: sub check_switchserver {
 7184:     my ($dom,$confname) = @_;
 7185:     my ($allowed,$switchserver);
 7186:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 7187:     if ($home eq 'no_host') {
 7188:         $home = &Apache::lonnet::domain($dom,'primary');
 7189:     }
 7190:     my @ids=&Apache::lonnet::current_machine_ids();
 7191:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 7192:     if (!$allowed) {
 7193: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 7194:     }
 7195:     return $switchserver;
 7196: }
 7197: 
 7198: sub modify_quotas {
 7199:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 7200:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 7201:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 7202:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 7203:         $validationfieldsref);
 7204:     if ($action eq 'quotas') {
 7205:         $context = 'tools'; 
 7206:     } else {
 7207:         $context = $action;
 7208:     }
 7209:     if ($context eq 'requestcourses') {
 7210:         @usertools = ('official','unofficial','community','textbook');
 7211:         @options =('norequest','approval','validate','autolimit');
 7212:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 7213:         %titles = &courserequest_titles();
 7214:         $toolregexp = join('|',@usertools);
 7215:         %conditions = &courserequest_conditions();
 7216:         $confname = $dom.'-domainconfig';
 7217:         my $servadm = $r->dir_config('lonAdmEMail');
 7218:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7219:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 7220:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 7221:     } elsif ($context eq 'requestauthor') {
 7222:         @usertools = ('author');
 7223:         %titles = &authorrequest_titles();
 7224:     } else {
 7225:         @usertools = ('aboutme','blog','webdav','portfolio');
 7226:         %titles = &tool_titles();
 7227:     }
 7228:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 7229:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7230:     foreach my $key (keys(%env)) {
 7231:         if ($context eq 'requestcourses') {
 7232:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 7233:                 my $item = $1;
 7234:                 my $type = $2;
 7235:                 if ($type =~ /^limit_(.+)/) {
 7236:                     $limithash{$item}{$1} = $env{$key};
 7237:                 } else {
 7238:                     $confhash{$item}{$type} = $env{$key};
 7239:                 }
 7240:             }
 7241:         } elsif ($context eq 'requestauthor') {
 7242:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 7243:                 $confhash{$1} = $env{$key};
 7244:             }
 7245:         } else {
 7246:             if ($key =~ /^form\.quota_(.+)$/) {
 7247:                 $confhash{'defaultquota'}{$1} = $env{$key};
 7248:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 7249:                 $confhash{'authorquota'}{$1} = $env{$key};
 7250:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 7251:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 7252:             }
 7253:         }
 7254:     }
 7255:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 7256:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 7257:         @approvalnotify = sort(@approvalnotify);
 7258:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 7259:         my @crstypes = ('official','unofficial','community','textbook');
 7260:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 7261:         foreach my $type (@hasuniquecode) {
 7262:             if (grep(/^\Q$type\E$/,@crstypes)) {
 7263:                 $confhash{'uniquecode'}{$type} = 1;
 7264:             }
 7265:         }
 7266:         my (%newbook,%allpos);
 7267:         if ($context eq 'requestcourses') {
 7268:             foreach my $type ('textbooks','templates') {
 7269:                 @{$allpos{$type}} = (); 
 7270:                 my $invalid;
 7271:                 if ($type eq 'textbooks') {
 7272:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 7273:                 } else {
 7274:                     $invalid = &mt('Invalid LON-CAPA course for template');
 7275:                 }
 7276:                 if ($env{'form.'.$type.'_addbook'}) {
 7277:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 7278:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 7279:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 7280:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 7281:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 7282:                         } else {
 7283:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 7284:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 7285:                             $position =~ s/\D+//g;
 7286:                             if ($position ne '') {
 7287:                                 $allpos{$type}[$position] = $newbook{$type};
 7288:                             }
 7289:                         }
 7290:                     } else {
 7291:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 7292:                     }
 7293:                 }
 7294:             } 
 7295:         }
 7296:         if (ref($domconfig{$action}) eq 'HASH') {
 7297:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 7298:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 7299:                     $changes{'notify'}{'approval'} = 1;
 7300:                 }
 7301:             } else {
 7302:                 if ($confhash{'notify'}{'approval'}) {
 7303:                     $changes{'notify'}{'approval'} = 1;
 7304:                 }
 7305:             }
 7306:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 7307:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 7308:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 7309:                         unless ($confhash{'uniquecode'}{$crstype}) {
 7310:                             $changes{'uniquecode'} = 1;
 7311:                         }
 7312:                     }
 7313:                     unless ($changes{'uniquecode'}) {
 7314:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 7315:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 7316:                                 $changes{'uniquecode'} = 1;
 7317:                             }
 7318:                         }
 7319:                     }
 7320:                } else {
 7321:                    $changes{'uniquecode'} = 1;
 7322:                }
 7323:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 7324:                 $changes{'uniquecode'} = 1;
 7325:             }
 7326:             if ($context eq 'requestcourses') {
 7327:                 foreach my $type ('textbooks','templates') {
 7328:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 7329:                         my %deletions;
 7330:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 7331:                         if (@todelete) {
 7332:                             map { $deletions{$_} = 1; } @todelete;
 7333:                         }
 7334:                         my %imgdeletions;
 7335:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 7336:                         if (@todeleteimages) {
 7337:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 7338:                         }
 7339:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 7340:                         for (my $i=0; $i<=$maxnum; $i++) {
 7341:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 7342:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 7343:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 7344:                                 if ($deletions{$key}) {
 7345:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 7346:                                         #FIXME need to obsolete item in RES space
 7347:                                     }
 7348:                                     next;
 7349:                                 } else {
 7350:                                     my $newpos = $env{'form.'.$itemid};
 7351:                                     $newpos =~ s/\D+//g;
 7352:                                     foreach my $item ('subject','title','publisher','author') {
 7353:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7354:                                                  ($type eq 'templates'));
 7355:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 7356:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 7357:                                             $changes{$type}{$key} = 1;
 7358:                                         }
 7359:                                     }
 7360:                                     $allpos{$type}[$newpos] = $key;
 7361:                                 }
 7362:                                 if ($imgdeletions{$key}) {
 7363:                                     $changes{$type}{$key} = 1;
 7364:                                     #FIXME need to obsolete item in RES space
 7365:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 7366:                                     my ($cdom,$cnum) = split(/_/,$key);
 7367:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 7368:                                                                                   $cdom,$cnum,$type,$configuserok,
 7369:                                                                                   $switchserver,$author_ok);
 7370:                                     if ($imgurl) {
 7371:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 7372:                                         $changes{$type}{$key} = 1; 
 7373:                                     }
 7374:                                     if ($error) {
 7375:                                         &Apache::lonnet::logthis($error);
 7376:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7377:                                     } 
 7378:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 7379:                                     $confhash{$type}{$key}{'image'} = 
 7380:                                         $domconfig{$action}{$type}{$key}{'image'};
 7381:                                 }
 7382:                             }
 7383:                         }
 7384:                     }
 7385:                 }
 7386:             }
 7387:         } else {
 7388:             if ($confhash{'notify'}{'approval'}) {
 7389:                 $changes{'notify'}{'approval'} = 1;
 7390:             }
 7391:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 7392:                 $changes{'uniquecode'} = 1;
 7393:             }
 7394:         }
 7395:         if ($context eq 'requestcourses') {
 7396:             foreach my $type ('textbooks','templates') {
 7397:                 if ($newbook{$type}) {
 7398:                     $changes{$type}{$newbook{$type}} = 1;
 7399:                     foreach my $item ('subject','title','publisher','author') {
 7400:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7401:                                  ($type eq 'template'));
 7402:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 7403:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 7404:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 7405:                         }
 7406:                     }
 7407:                     if ($type eq 'textbooks') {
 7408:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 7409:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 7410:                             my ($imageurl,$error) =
 7411:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 7412:                                                         $configuserok,$switchserver,$author_ok);
 7413:                             if ($imageurl) {
 7414:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 7415:                             }
 7416:                             if ($error) {
 7417:                                 &Apache::lonnet::logthis($error);
 7418:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7419:                             }
 7420:                         }
 7421:                     }
 7422:                 }
 7423:                 if (@{$allpos{$type}} > 0) {
 7424:                     my $idx = 0;
 7425:                     foreach my $item (@{$allpos{$type}}) {
 7426:                         if ($item ne '') {
 7427:                             $confhash{$type}{$item}{'order'} = $idx;
 7428:                             if (ref($domconfig{$action}) eq 'HASH') {
 7429:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 7430:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 7431:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 7432:                                             $changes{$type}{$item} = 1;
 7433:                                         }
 7434:                                     }
 7435:                                 }
 7436:                             }
 7437:                             $idx ++;
 7438:                         }
 7439:                     }
 7440:                 }
 7441:             }
 7442:             if (ref($validationitemsref) eq 'ARRAY') {
 7443:                 foreach my $item (@{$validationitemsref}) {
 7444:                     if ($item eq 'fields') {
 7445:                         my @changed;
 7446:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 7447:                         if (@{$confhash{'validation'}{$item}} > 0) {
 7448:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 7449:                         }
 7450:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 7451:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7452:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 7453:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 7454:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 7455:                                 } else {
 7456:                                     @changed = @{$confhash{'validation'}{$item}};
 7457:                                 }
 7458:                             } else {
 7459:                                 @changed = @{$confhash{'validation'}{$item}};
 7460:                             }
 7461:                         } else {
 7462:                             @changed = @{$confhash{'validation'}{$item}};
 7463:                         }
 7464:                         if (@changed) {
 7465:                             if ($confhash{'validation'}{$item}) {
 7466:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 7467:                             } else {
 7468:                                 $changes{'validation'}{$item} = &mt('None');
 7469:                             }
 7470:                         }
 7471:                     } else {
 7472:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 7473:                         if ($item eq 'markup') {
 7474:                             if ($env{'form.requestcourses_validation_'.$item}) {
 7475:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 7476:                             }
 7477:                         }
 7478:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 7479:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7480:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 7481:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7482:                                 }
 7483:                             } else {
 7484:                                 if ($confhash{'validation'}{$item} ne '') {
 7485:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7486:                                 }
 7487:                             }
 7488:                         } else {
 7489:                             if ($confhash{'validation'}{$item} ne '') {
 7490:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7491:                             }
 7492:                         }
 7493:                     }
 7494:                 }
 7495:             }
 7496:             if ($env{'form.validationdc'}) {
 7497:                 my $newval = $env{'form.validationdc'};
 7498:                 my %domcoords = &get_active_dcs($dom);
 7499:                 if (exists($domcoords{$newval})) {
 7500:                     $confhash{'validation'}{'dc'} = $newval;
 7501:                 }
 7502:             }
 7503:             if (ref($confhash{'validation'}) eq 'HASH') {
 7504:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 7505:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7506:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7507:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7508:                                 if ($confhash{'validation'}{'dc'} eq '') {
 7509:                                     $changes{'validation'}{'dc'} = &mt('None');
 7510:                                 } else {
 7511:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7512:                                 }
 7513:                             }
 7514:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 7515:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7516:                         }
 7517:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 7518:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7519:                     }
 7520:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 7521:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7522:                 }
 7523:             } else {
 7524:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 7525:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7526:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7527:                             $changes{'validation'}{'dc'} = &mt('None');
 7528:                         }
 7529:                     }
 7530:                 }
 7531:             }
 7532:         }
 7533:     } else {
 7534:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 7535:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 7536:     }
 7537:     foreach my $item (@usertools) {
 7538:         foreach my $type (@{$types},'default','_LC_adv') {
 7539:             my $unset; 
 7540:             if ($context eq 'requestcourses') {
 7541:                 $unset = '0';
 7542:                 if ($type eq '_LC_adv') {
 7543:                     $unset = '';
 7544:                 }
 7545:                 if ($confhash{$item}{$type} eq 'autolimit') {
 7546:                     $confhash{$item}{$type} .= '=';
 7547:                     unless ($limithash{$item}{$type} =~ /\D/) {
 7548:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 7549:                     }
 7550:                 }
 7551:             } elsif ($context eq 'requestauthor') {
 7552:                 $unset = '0';
 7553:                 if ($type eq '_LC_adv') {
 7554:                     $unset = '';
 7555:                 }
 7556:             } else {
 7557:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 7558:                     $confhash{$item}{$type} = 1;
 7559:                 } else {
 7560:                     $confhash{$item}{$type} = 0;
 7561:                 }
 7562:             }
 7563:             if (ref($domconfig{$action}) eq 'HASH') {
 7564:                 if ($action eq 'requestauthor') {
 7565:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 7566:                         $changes{$type} = 1;
 7567:                     }
 7568:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 7569:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 7570:                         $changes{$item}{$type} = 1;
 7571:                     }
 7572:                 } else {
 7573:                     if ($context eq 'requestcourses') {
 7574:                         if ($confhash{$item}{$type} ne $unset) {
 7575:                             $changes{$item}{$type} = 1;
 7576:                         }
 7577:                     } else {
 7578:                         if (!$confhash{$item}{$type}) {
 7579:                             $changes{$item}{$type} = 1;
 7580:                         }
 7581:                     }
 7582:                 }
 7583:             } else {
 7584:                 if ($context eq 'requestcourses') {
 7585:                     if ($confhash{$item}{$type} ne $unset) {
 7586:                         $changes{$item}{$type} = 1;
 7587:                     }
 7588:                 } elsif ($context eq 'requestauthor') {
 7589:                     if ($confhash{$type} ne $unset) {
 7590:                         $changes{$type} = 1;
 7591:                     }
 7592:                 } else {
 7593:                     if (!$confhash{$item}{$type}) {
 7594:                         $changes{$item}{$type} = 1;
 7595:                     }
 7596:                 }
 7597:             }
 7598:         }
 7599:     }
 7600:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 7601:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 7602:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7603:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 7604:                     if (exists($confhash{'defaultquota'}{$key})) {
 7605:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 7606:                             $changes{'defaultquota'}{$key} = 1;
 7607:                         }
 7608:                     } else {
 7609:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 7610:                     }
 7611:                 }
 7612:             } else {
 7613:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 7614:                     if (exists($confhash{'defaultquota'}{$key})) {
 7615:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 7616:                             $changes{'defaultquota'}{$key} = 1;
 7617:                         }
 7618:                     } else {
 7619:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 7620:                     }
 7621:                 }
 7622:             }
 7623:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7624:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 7625:                     if (exists($confhash{'authorquota'}{$key})) {
 7626:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 7627:                             $changes{'authorquota'}{$key} = 1;
 7628:                         }
 7629:                     } else {
 7630:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 7631:                     }
 7632:                 }
 7633:             }
 7634:         }
 7635:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 7636:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 7637:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7638:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7639:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 7640:                             $changes{'defaultquota'}{$key} = 1;
 7641:                         }
 7642:                     } else {
 7643:                         if (!exists($domconfig{'quotas'}{$key})) {
 7644:                             $changes{'defaultquota'}{$key} = 1;
 7645:                         }
 7646:                     }
 7647:                 } else {
 7648:                     $changes{'defaultquota'}{$key} = 1;
 7649:                 }
 7650:             }
 7651:         }
 7652:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 7653:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 7654:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7655:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7656:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 7657:                             $changes{'authorquota'}{$key} = 1;
 7658:                         }
 7659:                     } else {
 7660:                         $changes{'authorquota'}{$key} = 1;
 7661:                     }
 7662:                 } else {
 7663:                     $changes{'authorquota'}{$key} = 1;
 7664:                 }
 7665:             }
 7666:         }
 7667:     }
 7668: 
 7669:     if ($context eq 'requestauthor') {
 7670:         $domdefaults{'requestauthor'} = \%confhash;
 7671:     } else {
 7672:         foreach my $key (keys(%confhash)) {
 7673:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 7674:                 $domdefaults{$key} = $confhash{$key};
 7675:             }
 7676:         }
 7677:     }
 7678: 
 7679:     my %quotahash = (
 7680:                       $action => { %confhash }
 7681:                     );
 7682:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 7683:                                              $dom);
 7684:     if ($putresult eq 'ok') {
 7685:         if (keys(%changes) > 0) {
 7686:             my $cachetime = 24*60*60;
 7687:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7688:             if (ref($lastactref) eq 'HASH') {
 7689:                 $lastactref->{'domdefaults'} = 1;
 7690:             }
 7691:             $resulttext = &mt('Changes made:').'<ul>';
 7692:             unless (($context eq 'requestcourses') ||
 7693:                     ($context eq 'requestauthor')) {
 7694:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 7695:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 7696:                     foreach my $type (@{$types},'default') {
 7697:                         if (defined($changes{'defaultquota'}{$type})) {
 7698:                             my $typetitle = $usertypes->{$type};
 7699:                             if ($type eq 'default') {
 7700:                                 $typetitle = $othertitle;
 7701:                             }
 7702:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 7703:                         }
 7704:                     }
 7705:                     $resulttext .= '</ul></li>';
 7706:                 }
 7707:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 7708:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 7709:                     foreach my $type (@{$types},'default') {
 7710:                         if (defined($changes{'authorquota'}{$type})) {
 7711:                             my $typetitle = $usertypes->{$type};
 7712:                             if ($type eq 'default') {
 7713:                                 $typetitle = $othertitle;
 7714:                             }
 7715:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 7716:                         }
 7717:                     }
 7718:                     $resulttext .= '</ul></li>';
 7719:                 }
 7720:             }
 7721:             my %newenv;
 7722:             foreach my $item (@usertools) {
 7723:                 my (%haschgs,%inconf);
 7724:                 if ($context eq 'requestauthor') {
 7725:                     %haschgs = %changes;
 7726:                     %inconf = %confhash;
 7727:                 } else {
 7728:                     if (ref($changes{$item}) eq 'HASH') {
 7729:                         %haschgs = %{$changes{$item}};
 7730:                     }
 7731:                     if (ref($confhash{$item}) eq 'HASH') {
 7732:                         %inconf = %{$confhash{$item}};
 7733:                     }
 7734:                 }
 7735:                 if (keys(%haschgs) > 0) {
 7736:                     my $newacc = 
 7737:                         &Apache::lonnet::usertools_access($env{'user.name'},
 7738:                                                           $env{'user.domain'},
 7739:                                                           $item,'reload',$context);
 7740:                     if (($context eq 'requestcourses') ||
 7741:                         ($context eq 'requestauthor')) {
 7742:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 7743:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 7744:                         }
 7745:                     } else {
 7746:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 7747:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 7748:                         }
 7749:                     }
 7750:                     unless ($context eq 'requestauthor') {
 7751:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 7752:                     }
 7753:                     foreach my $type (@{$types},'default','_LC_adv') {
 7754:                         if ($haschgs{$type}) {
 7755:                             my $typetitle = $usertypes->{$type};
 7756:                             if ($type eq 'default') {
 7757:                                 $typetitle = $othertitle;
 7758:                             } elsif ($type eq '_LC_adv') {
 7759:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 7760:                             }
 7761:                             if ($inconf{$type}) {
 7762:                                 if ($context eq 'requestcourses') {
 7763:                                     my $cond;
 7764:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 7765:                                         if ($1 eq '') {
 7766:                                             $cond = &mt('(Automatic processing of any request).');
 7767:                                         } else {
 7768:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 7769:                                         }
 7770:                                     } else { 
 7771:                                         $cond = $conditions{$inconf{$type}};
 7772:                                     }
 7773:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 7774:                                 } elsif ($context eq 'requestauthor') {
 7775:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 7776:                                                              $titles{$inconf{$type}},$typetitle);
 7777: 
 7778:                                 } else {
 7779:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 7780:                                 }
 7781:                             } else {
 7782:                                 if ($type eq '_LC_adv') {
 7783:                                     if ($inconf{$type} eq '0') {
 7784:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7785:                                     } else { 
 7786:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 7787:                                     }
 7788:                                 } else {
 7789:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7790:                                 }
 7791:                             }
 7792:                         }
 7793:                     }
 7794:                     unless ($context eq 'requestauthor') {
 7795:                         $resulttext .= '</ul></li>';
 7796:                     }
 7797:                 }
 7798:             }
 7799:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 7800:                 if (ref($changes{'notify'}) eq 'HASH') {
 7801:                     if ($changes{'notify'}{'approval'}) {
 7802:                         if (ref($confhash{'notify'}) eq 'HASH') {
 7803:                             if ($confhash{'notify'}{'approval'}) {
 7804:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 7805:                             } else {
 7806:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 7807:                             }
 7808:                         }
 7809:                     }
 7810:                 }
 7811:             }
 7812:             if ($action eq 'requestcourses') {
 7813:                 my @offon = ('off','on');
 7814:                 if ($changes{'uniquecode'}) {
 7815:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 7816:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 7817:                         $resulttext .= '<li>'.
 7818:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 7819:                                        '</li>';
 7820:                     } else {
 7821:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 7822:                                        '</li>';
 7823:                     }
 7824:                 }
 7825:                 foreach my $type ('textbooks','templates') {
 7826:                     if (ref($changes{$type}) eq 'HASH') {
 7827:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 7828:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 7829:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 7830:                             my $coursetitle = $coursehash{'description'};
 7831:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 7832:                             $resulttext .= '<li>';
 7833:                             foreach my $item ('subject','title','publisher','author') {
 7834:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7835:                                          ($type eq 'templates'));
 7836:                                 my $name = $item.':';
 7837:                                 $name =~ s/^(\w)/\U$1/;
 7838:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 7839:                             }
 7840:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 7841:                             if ($type eq 'textbooks') {
 7842:                                 if ($confhash{$type}{$key}{'image'}) {
 7843:                                     $resulttext .= ' '.&mt('Image: [_1]',
 7844:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 7845:                                                    ' alt="Textbook cover" />').'<br />';
 7846:                                 }
 7847:                             }
 7848:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 7849:                         }
 7850:                         $resulttext .= '</ul></li>';
 7851:                     }
 7852:                 }
 7853:                 if (ref($changes{'validation'}) eq 'HASH') {
 7854:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 7855:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 7856:                         foreach my $item (@{$validationitemsref}) {
 7857:                             if (exists($changes{'validation'}{$item})) {
 7858:                                 if ($item eq 'markup') {
 7859:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7860:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 7861:                                 } else {
 7862:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7863:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 7864:                                 }
 7865:                             }
 7866:                         }
 7867:                         if (exists($changes{'validation'}{'dc'})) {
 7868:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 7869:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 7870:                         }
 7871:                     }
 7872:                 }
 7873:             }
 7874:             $resulttext .= '</ul>';
 7875:             if (keys(%newenv)) {
 7876:                 &Apache::lonnet::appenv(\%newenv);
 7877:             }
 7878:         } else {
 7879:             if ($context eq 'requestcourses') {
 7880:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 7881:             } elsif ($context eq 'requestauthor') {
 7882:                 $resulttext = &mt('No changes made to rights to request author space.');
 7883:             } else {
 7884:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 7885:             }
 7886:         }
 7887:     } else {
 7888:         $resulttext = '<span class="LC_error">'.
 7889: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7890:     }
 7891:     if ($errors) {
 7892:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 7893:                        '<ul>'.$errors.'</ul></p>';
 7894:     }
 7895:     return $resulttext;
 7896: }
 7897: 
 7898: sub process_textbook_image {
 7899:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 7900:     my $filename = $env{'form.'.$caller.'.filename'};
 7901:     my ($error,$url);
 7902:     my ($width,$height) = (50,50);
 7903:     if ($configuserok eq 'ok') {
 7904:         if ($switchserver) {
 7905:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 7906:                          $switchserver);
 7907:         } elsif ($author_ok eq 'ok') {
 7908:             my ($result,$imageurl) =
 7909:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 7910:                              "$type/$dom/$cnum/cover",$width,$height);
 7911:             if ($result eq 'ok') {
 7912:                 $url = $imageurl;
 7913:             } else {
 7914:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 7915:             }
 7916:         } else {
 7917:             $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);
 7918:         }
 7919:     } else {
 7920:         $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);
 7921:     }
 7922:     return ($url,$error);
 7923: }
 7924: 
 7925: sub modify_autoenroll {
 7926:     my ($dom,$lastactref,%domconfig) = @_;
 7927:     my ($resulttext,%changes);
 7928:     my %currautoenroll;
 7929:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 7930:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 7931:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 7932:         }
 7933:     }
 7934:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 7935:     my %title = ( run => 'Auto-enrollment active',
 7936:                   sender => 'Sender for notification messages',
 7937:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
 7938:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
 7939:     my @offon = ('off','on');
 7940:     my $sender_uname = $env{'form.sender_uname'};
 7941:     my $sender_domain = $env{'form.sender_domain'};
 7942:     if ($sender_domain eq '') {
 7943:         $sender_uname = '';
 7944:     } elsif ($sender_uname eq '') {
 7945:         $sender_domain = '';
 7946:     }
 7947:     my $coowners = $env{'form.autoassign_coowners'};
 7948:     my $failsafe = $env{'form.autoenroll_failsafe'};
 7949:     $failsafe =~ s{^\s+|\s+$}{}g;
 7950:     if ($failsafe =~ /\D/) {
 7951:         undef($failsafe);
 7952:     }
 7953:     my %autoenrollhash =  (
 7954:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 7955:                                        'sender_uname' => $sender_uname,
 7956:                                        'sender_domain' => $sender_domain,
 7957:                                        'co-owners' => $coowners,
 7958:                                        'autofailsafe' => $failsafe,
 7959:                                 }
 7960:                      );
 7961:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 7962:                                              $dom);
 7963:     if ($putresult eq 'ok') {
 7964:         if (exists($currautoenroll{'run'})) {
 7965:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 7966:                  $changes{'run'} = 1;
 7967:              }
 7968:         } elsif ($autorun) {
 7969:             if ($env{'form.autoenroll_run'} ne '1') {
 7970:                  $changes{'run'} = 1;
 7971:             }
 7972:         }
 7973:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 7974:             $changes{'sender'} = 1;
 7975:         }
 7976:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 7977:             $changes{'sender'} = 1;
 7978:         }
 7979:         if ($currautoenroll{'co-owners'} ne '') {
 7980:             if ($currautoenroll{'co-owners'} ne $coowners) {
 7981:                 $changes{'coowners'} = 1;
 7982:             }
 7983:         } elsif ($coowners) {
 7984:             $changes{'coowners'} = 1;
 7985:         }
 7986:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
 7987:             $changes{'autofailsafe'} = 1;
 7988:         }
 7989:         if (keys(%changes) > 0) {
 7990:             $resulttext = &mt('Changes made:').'<ul>';
 7991:             if ($changes{'run'}) {
 7992:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 7993:             }
 7994:             if ($changes{'sender'}) {
 7995:                 if ($sender_uname eq '' || $sender_domain eq '') {
 7996:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 7997:                 } else {
 7998:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 7999:                 }
 8000:             }
 8001:             if ($changes{'coowners'}) {
 8002:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 8003:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 8004:                 if (ref($lastactref) eq 'HASH') {
 8005:                     $lastactref->{'domainconfig'} = 1;
 8006:                 }
 8007:             }
 8008:             if ($changes{'autofailsafe'}) {
 8009:                 if ($failsafe ne '') {
 8010:                     $resulttext .= '<li>'.&mt("$title{'failsafe'} set to [_1]",$failsafe).'</li>';
 8011:                 } else {
 8012:                     $resulttext .= '<li>'.&mt("$title{'failsafe'} deleted");
 8013:                 }
 8014:                 &Apache::lonnet::get_domain_defaults($dom,1);
 8015:                 if (ref($lastactref) eq 'HASH') {
 8016:                     $lastactref->{'domdefaults'} = 1;
 8017:                 }
 8018:             }
 8019:             $resulttext .= '</ul>';
 8020:         } else {
 8021:             $resulttext = &mt('No changes made to auto-enrollment settings');
 8022:         }
 8023:     } else {
 8024:         $resulttext = '<span class="LC_error">'.
 8025: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8026:     }
 8027:     return $resulttext;
 8028: }
 8029: 
 8030: sub modify_autoupdate {
 8031:     my ($dom,%domconfig) = @_;
 8032:     my ($resulttext,%currautoupdate,%fields,%changes);
 8033:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 8034:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 8035:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 8036:         }
 8037:     }
 8038:     my @offon = ('off','on');
 8039:     my %title = &Apache::lonlocal::texthash (
 8040:                    run => 'Auto-update:',
 8041:                    classlists => 'Updates to user information in classlists?'
 8042:                 );
 8043:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8044:     my %fieldtitles = &Apache::lonlocal::texthash (
 8045:                         id => 'Student/Employee ID',
 8046:                         permanentemail => 'E-mail address',
 8047:                         lastname => 'Last Name',
 8048:                         firstname => 'First Name',
 8049:                         middlename => 'Middle Name',
 8050:                         generation => 'Generation',
 8051:                       );
 8052:     $othertitle = &mt('All users');
 8053:     if (keys(%{$usertypes}) >  0) {
 8054:         $othertitle = &mt('Other users');
 8055:     }
 8056:     foreach my $key (keys(%env)) {
 8057:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 8058:             my ($usertype,$item) = ($1,$2);
 8059:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 8060:                 if ($usertype eq 'default') {   
 8061:                     push(@{$fields{$1}},$2);
 8062:                 } elsif (ref($types) eq 'ARRAY') {
 8063:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 8064:                         push(@{$fields{$1}},$2);
 8065:                     }
 8066:                 }
 8067:             }
 8068:         }
 8069:     }
 8070:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 8071:     @lockablenames = sort(@lockablenames);
 8072:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 8073:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 8074:         if (@changed) {
 8075:             $changes{'lockablenames'} = 1;
 8076:         }
 8077:     } else {
 8078:         if (@lockablenames) {
 8079:             $changes{'lockablenames'} = 1;
 8080:         }
 8081:     }
 8082:     my %updatehash = (
 8083:                       autoupdate => { run => $env{'form.autoupdate_run'},
 8084:                                       classlists => $env{'form.classlists'},
 8085:                                       fields => {%fields},
 8086:                                       lockablenames => \@lockablenames,
 8087:                                     }
 8088:                      );
 8089:     foreach my $key (keys(%currautoupdate)) {
 8090:         if (($key eq 'run') || ($key eq 'classlists')) {
 8091:             if (exists($updatehash{autoupdate}{$key})) {
 8092:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 8093:                     $changes{$key} = 1;
 8094:                 }
 8095:             }
 8096:         } elsif ($key eq 'fields') {
 8097:             if (ref($currautoupdate{$key}) eq 'HASH') {
 8098:                 foreach my $item (@{$types},'default') {
 8099:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 8100:                         my $change = 0;
 8101:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 8102:                             if (!exists($fields{$item})) {
 8103:                                 $change = 1;
 8104:                                 last;
 8105:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 8106:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 8107:                                     $change = 1;
 8108:                                     last;
 8109:                                 }
 8110:                             }
 8111:                         }
 8112:                         if ($change) {
 8113:                             push(@{$changes{$key}},$item);
 8114:                         }
 8115:                     } 
 8116:                 }
 8117:             }
 8118:         } elsif ($key eq 'lockablenames') {
 8119:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 8120:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 8121:                 if (@changed) {
 8122:                     $changes{'lockablenames'} = 1;
 8123:                 }
 8124:             } else {
 8125:                 if (@lockablenames) {
 8126:                     $changes{'lockablenames'} = 1;
 8127:                 }
 8128:             }
 8129:         }
 8130:     }
 8131:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 8132:         if (@lockablenames) {
 8133:             $changes{'lockablenames'} = 1;
 8134:         }
 8135:     }
 8136:     foreach my $item (@{$types},'default') {
 8137:         if (defined($fields{$item})) {
 8138:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 8139:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 8140:                     my $change = 0;
 8141:                     if (ref($fields{$item}) eq 'ARRAY') {
 8142:                         foreach my $type (@{$fields{$item}}) {
 8143:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 8144:                                 $change = 1;
 8145:                                 last;
 8146:                             }
 8147:                         }
 8148:                     }
 8149:                     if ($change) {
 8150:                         push(@{$changes{'fields'}},$item);
 8151:                     }
 8152:                 } else {
 8153:                     push(@{$changes{'fields'}},$item);
 8154:                 }
 8155:             } else {
 8156:                 push(@{$changes{'fields'}},$item);
 8157:             }
 8158:         }
 8159:     }
 8160:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 8161:                                              $dom);
 8162:     if ($putresult eq 'ok') {
 8163:         if (keys(%changes) > 0) {
 8164:             $resulttext = &mt('Changes made:').'<ul>';
 8165:             foreach my $key (sort(keys(%changes))) {
 8166:                 if ($key eq 'lockablenames') {
 8167:                     $resulttext .= '<li>';
 8168:                     if (@lockablenames) {
 8169:                         $usertypes->{'default'} = $othertitle;
 8170:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 8171:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 8172:                     } else {
 8173:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 8174:                     }
 8175:                     $resulttext .= '</li>';
 8176:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 8177:                     foreach my $item (@{$changes{$key}}) {
 8178:                         my @newvalues;
 8179:                         foreach my $type (@{$fields{$item}}) {
 8180:                             push(@newvalues,$fieldtitles{$type});
 8181:                         }
 8182:                         my $newvaluestr;
 8183:                         if (@newvalues > 0) {
 8184:                             $newvaluestr = join(', ',@newvalues);
 8185:                         } else {
 8186:                             $newvaluestr = &mt('none');
 8187:                         }
 8188:                         if ($item eq 'default') {
 8189:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 8190:                         } else {
 8191:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 8192:                         }
 8193:                     }
 8194:                 } else {
 8195:                     my $newvalue;
 8196:                     if ($key eq 'run') {
 8197:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 8198:                     } else {
 8199:                         $newvalue = $offon[$env{'form.'.$key}];
 8200:                     }
 8201:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 8202:                 }
 8203:             }
 8204:             $resulttext .= '</ul>';
 8205:         } else {
 8206:             $resulttext = &mt('No changes made to autoupdates');
 8207:         }
 8208:     } else {
 8209:         $resulttext = '<span class="LC_error">'.
 8210: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8211:     }
 8212:     return $resulttext;
 8213: }
 8214: 
 8215: sub modify_autocreate {
 8216:     my ($dom,%domconfig) = @_;
 8217:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 8218:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 8219:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 8220:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 8221:         }
 8222:     }
 8223:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 8224:                  req => 'Auto-creation of validated requests for official courses',
 8225:                  xmldc => 'Identity of course creator of courses from XML files',
 8226:                );
 8227:     my @types = ('xml','req');
 8228:     foreach my $item (@types) {
 8229:         $newvals{$item} = $env{'form.autocreate_'.$item};
 8230:         $newvals{$item} =~ s/\D//g;
 8231:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 8232:     }
 8233:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 8234:     my %domcoords = &get_active_dcs($dom);
 8235:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 8236:         $newvals{'xmldc'} = '';
 8237:     } 
 8238:     %autocreatehash =  (
 8239:                         autocreate => { xml => $newvals{'xml'},
 8240:                                         req => $newvals{'req'},
 8241:                                       }
 8242:                        );
 8243:     if ($newvals{'xmldc'} ne '') {
 8244:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 8245:     }
 8246:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 8247:                                              $dom);
 8248:     if ($putresult eq 'ok') {
 8249:         my @items = @types;
 8250:         if ($newvals{'xml'}) {
 8251:             push(@items,'xmldc');
 8252:         }
 8253:         foreach my $item (@items) {
 8254:             if (exists($currautocreate{$item})) {
 8255:                 if ($currautocreate{$item} ne $newvals{$item}) {
 8256:                     $changes{$item} = 1;
 8257:                 }
 8258:             } elsif ($newvals{$item}) {
 8259:                 $changes{$item} = 1;
 8260:             }
 8261:         }
 8262:         if (keys(%changes) > 0) {
 8263:             my @offon = ('off','on'); 
 8264:             $resulttext = &mt('Changes made:').'<ul>';
 8265:             foreach my $item (@types) {
 8266:                 if ($changes{$item}) {
 8267:                     my $newtxt = $offon[$newvals{$item}];
 8268:                     $resulttext .= '<li>'.
 8269:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 8270:                                        '<b>','</b>').
 8271:                                    '</li>';
 8272:                 }
 8273:             }
 8274:             if ($changes{'xmldc'}) {
 8275:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 8276:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 8277:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 8278:             }
 8279:             $resulttext .= '</ul>';
 8280:         } else {
 8281:             $resulttext = &mt('No changes made to auto-creation settings');
 8282:         }
 8283:     } else {
 8284:         $resulttext = '<span class="LC_error">'.
 8285:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8286:     }
 8287:     return $resulttext;
 8288: }
 8289: 
 8290: sub modify_directorysrch {
 8291:     my ($dom,%domconfig) = @_;
 8292:     my ($resulttext,%changes);
 8293:     my %currdirsrch;
 8294:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 8295:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 8296:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 8297:         }
 8298:     }
 8299:     my %title = ( available => 'Directory search available',
 8300:                   localonly => 'Other domains can search',
 8301:                   searchby => 'Search types',
 8302:                   searchtypes => 'Search latitude');
 8303:     my @offon = ('off','on');
 8304:     my @otherdoms = ('Yes','No');
 8305: 
 8306:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 8307:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 8308:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 8309: 
 8310:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8311:     if (keys(%{$usertypes}) == 0) {
 8312:         @cansearch = ('default');
 8313:     } else {
 8314:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 8315:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 8316:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 8317:                     push(@{$changes{'cansearch'}},$type);
 8318:                 }
 8319:             }
 8320:             foreach my $type (@cansearch) {
 8321:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 8322:                     push(@{$changes{'cansearch'}},$type);
 8323:                 }
 8324:             }
 8325:         } else {
 8326:             push(@{$changes{'cansearch'}},@cansearch);
 8327:         }
 8328:     }
 8329: 
 8330:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 8331:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 8332:             if (!grep(/^\Q$by\E$/,@searchby)) {
 8333:                 push(@{$changes{'searchby'}},$by);
 8334:             }
 8335:         }
 8336:         foreach my $by (@searchby) {
 8337:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 8338:                 push(@{$changes{'searchby'}},$by);
 8339:             }
 8340:         }
 8341:     } else {
 8342:         push(@{$changes{'searchby'}},@searchby);
 8343:     }
 8344: 
 8345:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 8346:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 8347:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 8348:                 push(@{$changes{'searchtypes'}},$type);
 8349:             }
 8350:         }
 8351:         foreach my $type (@searchtypes) {
 8352:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 8353:                 push(@{$changes{'searchtypes'}},$type);
 8354:             }
 8355:         }
 8356:     } else {
 8357:         if (exists($currdirsrch{'searchtypes'})) {
 8358:             foreach my $type (@searchtypes) {  
 8359:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 8360:                     push(@{$changes{'searchtypes'}},$type);
 8361:                 }
 8362:             }
 8363:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 8364:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 8365:             }   
 8366:         } else {
 8367:             push(@{$changes{'searchtypes'}},@searchtypes); 
 8368:         }
 8369:     }
 8370: 
 8371:     my %dirsrch_hash =  (
 8372:             directorysrch => { available => $env{'form.dirsrch_available'},
 8373:                                cansearch => \@cansearch,
 8374:                                localonly => $env{'form.dirsrch_localonly'},
 8375:                                searchby => \@searchby,
 8376:                                searchtypes => \@searchtypes,
 8377:                              }
 8378:             );
 8379:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 8380:                                              $dom);
 8381:     if ($putresult eq 'ok') {
 8382:         if (exists($currdirsrch{'available'})) {
 8383:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 8384:                  $changes{'available'} = 1;
 8385:              }
 8386:         } else {
 8387:             if ($env{'form.dirsrch_available'} eq '1') {
 8388:                 $changes{'available'} = 1;
 8389:             }
 8390:         }
 8391:         if (exists($currdirsrch{'localonly'})) {
 8392:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 8393:                  $changes{'localonly'} = 1;
 8394:              }
 8395:         } else {
 8396:             if ($env{'form.dirsrch_localonly'} eq '1') {
 8397:                 $changes{'localonly'} = 1;
 8398:             }
 8399:         }
 8400:         if (keys(%changes) > 0) {
 8401:             $resulttext = &mt('Changes made:').'<ul>';
 8402:             if ($changes{'available'}) {
 8403:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 8404:             }
 8405:             if ($changes{'localonly'}) {
 8406:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 8407:             }
 8408: 
 8409:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 8410:                 my $chgtext;
 8411:                 if (ref($usertypes) eq 'HASH') {
 8412:                     if (keys(%{$usertypes}) > 0) {
 8413:                         foreach my $type (@{$types}) {
 8414:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 8415:                                 $chgtext .= $usertypes->{$type}.'; ';
 8416:                             }
 8417:                         }
 8418:                         if (grep(/^default$/,@cansearch)) {
 8419:                             $chgtext .= $othertitle;
 8420:                         } else {
 8421:                             $chgtext =~ s/\; $//;
 8422:                         }
 8423:                         $resulttext .=
 8424:                             '<li>'.
 8425:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 8426:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 8427:                             '</li>';
 8428:                     }
 8429:                 }
 8430:             }
 8431:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 8432:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 8433:                 my $chgtext;
 8434:                 foreach my $type (@{$titleorder}) {
 8435:                     if (grep(/^\Q$type\E$/,@searchby)) {
 8436:                         if (defined($searchtitles->{$type})) {
 8437:                             $chgtext .= $searchtitles->{$type}.'; ';
 8438:                         }
 8439:                     }
 8440:                 }
 8441:                 $chgtext =~ s/\; $//;
 8442:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 8443:             }
 8444:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 8445:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 8446:                 my $chgtext;
 8447:                 foreach my $type (@{$srchtypeorder}) {
 8448:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 8449:                         if (defined($srchtypes_desc->{$type})) {
 8450:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 8451:                         }
 8452:                     }
 8453:                 }
 8454:                 $chgtext =~ s/\; $//;
 8455:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 8456:             }
 8457:             $resulttext .= '</ul>';
 8458:         } else {
 8459:             $resulttext = &mt('No changes made to institution directory search settings');
 8460:         }
 8461:     } else {
 8462:         $resulttext = '<span class="LC_error">'.
 8463:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 8464:     }
 8465:     return $resulttext;
 8466: }
 8467: 
 8468: sub modify_contacts {
 8469:     my ($dom,$lastactref,%domconfig) = @_;
 8470:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 8471:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 8472:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 8473:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 8474:         }
 8475:     }
 8476:     my (%others,%to,%bcc);
 8477:     my @contacts = ('supportemail','adminemail');
 8478:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 8479:                     'requestsmail','updatesmail','idconflictsmail');
 8480:     my @toggles = ('reporterrors','reportupdates');
 8481:     foreach my $type (@mailings) {
 8482:         @{$newsetting{$type}} = 
 8483:             &Apache::loncommon::get_env_multiple('form.'.$type);
 8484:         foreach my $item (@contacts) {
 8485:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 8486:                 $contacts_hash{contacts}{$type}{$item} = 1;
 8487:             } else {
 8488:                 $contacts_hash{contacts}{$type}{$item} = 0;
 8489:             }
 8490:         }  
 8491:         $others{$type} = $env{'form.'.$type.'_others'};
 8492:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 8493:         if ($type eq 'helpdeskmail') {
 8494:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 8495:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 8496:         }
 8497:     }
 8498:     foreach my $item (@contacts) {
 8499:         $to{$item} = $env{'form.'.$item};
 8500:         $contacts_hash{'contacts'}{$item} = $to{$item};
 8501:     }
 8502:     foreach my $item (@toggles) {
 8503:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 8504:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 8505:         }
 8506:     }
 8507:     if (keys(%currsetting) > 0) {
 8508:         foreach my $item (@contacts) {
 8509:             if ($to{$item} ne $currsetting{$item}) {
 8510:                 $changes{$item} = 1;
 8511:             }
 8512:         }
 8513:         foreach my $type (@mailings) {
 8514:             foreach my $item (@contacts) {
 8515:                 if (ref($currsetting{$type}) eq 'HASH') {
 8516:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 8517:                         push(@{$changes{$type}},$item);
 8518:                     }
 8519:                 } else {
 8520:                     push(@{$changes{$type}},@{$newsetting{$type}});
 8521:                 }
 8522:             }
 8523:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 8524:                 push(@{$changes{$type}},'others');
 8525:             }
 8526:             if ($type eq 'helpdeskmail') {   
 8527:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 8528:                     push(@{$changes{$type}},'bcc'); 
 8529:                 }
 8530:             }
 8531:         }
 8532:     } else {
 8533:         my %default;
 8534:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 8535:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 8536:         $default{'errormail'} = 'adminemail';
 8537:         $default{'packagesmail'} = 'adminemail';
 8538:         $default{'helpdeskmail'} = 'supportemail';
 8539:         $default{'lonstatusmail'} = 'adminemail';
 8540:         $default{'requestsmail'} = 'adminemail';
 8541:         $default{'updatesmail'} = 'adminemail';
 8542:         foreach my $item (@contacts) {
 8543:            if ($to{$item} ne $default{$item}) {
 8544:               $changes{$item} = 1;
 8545:            }
 8546:         }
 8547:         foreach my $type (@mailings) {
 8548:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 8549:                
 8550:                 push(@{$changes{$type}},@{$newsetting{$type}});
 8551:             }
 8552:             if ($others{$type} ne '') {
 8553:                 push(@{$changes{$type}},'others');
 8554:             }
 8555:             if ($type eq 'helpdeskmail') {
 8556:                 if ($bcc{$type} ne '') {
 8557:                     push(@{$changes{$type}},'bcc');
 8558:                 }
 8559:             }
 8560:         }
 8561:     }
 8562:     foreach my $item (@toggles) {
 8563:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 8564:             $changes{$item} = 1;
 8565:         } elsif ((!$env{'form.'.$item}) &&
 8566:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 8567:             $changes{$item} = 1;
 8568:         }
 8569:     }
 8570:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 8571:                                              $dom);
 8572:     if ($putresult eq 'ok') {
 8573:         if (keys(%changes) > 0) {
 8574:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8575:             if (ref($lastactref) eq 'HASH') {
 8576:                 $lastactref->{'domainconfig'} = 1;
 8577:             }
 8578:             my ($titles,$short_titles)  = &contact_titles();
 8579:             $resulttext = &mt('Changes made:').'<ul>';
 8580:             foreach my $item (@contacts) {
 8581:                 if ($changes{$item}) {
 8582:                     $resulttext .= '<li>'.$titles->{$item}.
 8583:                                     &mt(' set to: ').
 8584:                                     '<span class="LC_cusr_emph">'.
 8585:                                     $to{$item}.'</span></li>';
 8586:                 }
 8587:             }
 8588:             foreach my $type (@mailings) {
 8589:                 if (ref($changes{$type}) eq 'ARRAY') {
 8590:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 8591:                     my @text;
 8592:                     foreach my $item (@{$newsetting{$type}}) {
 8593:                         push(@text,$short_titles->{$item});
 8594:                     }
 8595:                     if ($others{$type} ne '') {
 8596:                         push(@text,$others{$type});
 8597:                     }
 8598:                     $resulttext .= '<span class="LC_cusr_emph">'.
 8599:                                    join(', ',@text).'</span>';
 8600:                     if ($type eq 'helpdeskmail') {
 8601:                         if ($bcc{$type} ne '') {
 8602:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 8603:                         }
 8604:                     }
 8605:                     $resulttext .= '</li>';
 8606:                 }
 8607:             }
 8608:             my @offon = ('off','on');
 8609:             if ($changes{'reporterrors'}) {
 8610:                 $resulttext .= '<li>'.
 8611:                                &mt('E-mail error reports to [_1] set to "'.
 8612:                                    $offon[$env{'form.reporterrors'}].'".',
 8613:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8614:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 8615:                                '</li>';
 8616:             }
 8617:             if ($changes{'reportupdates'}) {
 8618:                 $resulttext .= '<li>'.
 8619:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 8620:                                     $offon[$env{'form.reportupdates'}].'".',
 8621:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8622:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 8623:                                 '</li>';
 8624:             }
 8625:             $resulttext .= '</ul>';
 8626:         } else {
 8627:             $resulttext = &mt('No changes made to contact information');
 8628:         }
 8629:     } else {
 8630:         $resulttext = '<span class="LC_error">'.
 8631:             &mt('An error occurred: [_1].',$putresult).'</span>';
 8632:     }
 8633:     return $resulttext;
 8634: }
 8635: 
 8636: sub modify_usercreation {
 8637:     my ($dom,%domconfig) = @_;
 8638:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 8639:     my $warningmsg;
 8640:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8641:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8642:             if ($key eq 'cancreate') {
 8643:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8644:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8645:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8646:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
 8647:                             ($item eq 'recaptchaversion')) {
 8648:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8649:                         } else {
 8650:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8651:                         }
 8652:                     }
 8653:                 }
 8654:             } elsif ($key eq 'email_rule') {
 8655:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8656:             } else {
 8657:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8658:             }
 8659:         }
 8660:     }
 8661:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 8662:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 8663:     my @contexts = ('author','course','requestcrs');
 8664:     foreach my $item(@contexts) {
 8665:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 8666:     }
 8667:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8668:         foreach my $item (@contexts) {
 8669:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 8670:                 push(@{$changes{'cancreate'}},$item);
 8671:             }
 8672:         }
 8673:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 8674:         foreach my $item (@contexts) {
 8675:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 8676:                 if ($cancreate{$item} ne 'any') {
 8677:                     push(@{$changes{'cancreate'}},$item);
 8678:                 }
 8679:             } else {
 8680:                 if ($cancreate{$item} ne 'none') {
 8681:                     push(@{$changes{'cancreate'}},$item);
 8682:                 }
 8683:             }
 8684:         }
 8685:     } else {
 8686:         foreach my $item (@contexts)  {
 8687:             push(@{$changes{'cancreate'}},$item);
 8688:         }
 8689:     }
 8690: 
 8691:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 8692:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 8693:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 8694:                 push(@{$changes{'username_rule'}},$type);
 8695:             }
 8696:         }
 8697:         foreach my $type (@username_rule) {
 8698:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 8699:                 push(@{$changes{'username_rule'}},$type);
 8700:             }
 8701:         }
 8702:     } else {
 8703:         push(@{$changes{'username_rule'}},@username_rule);
 8704:     }
 8705: 
 8706:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 8707:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 8708:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 8709:                 push(@{$changes{'id_rule'}},$type);
 8710:             }
 8711:         }
 8712:         foreach my $type (@id_rule) {
 8713:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 8714:                 push(@{$changes{'id_rule'}},$type);
 8715:             }
 8716:         }
 8717:     } else {
 8718:         push(@{$changes{'id_rule'}},@id_rule);
 8719:     }
 8720: 
 8721:     my @authen_contexts = ('author','course','domain');
 8722:     my @authtypes = ('int','krb4','krb5','loc');
 8723:     my %authhash;
 8724:     foreach my $item (@authen_contexts) {
 8725:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 8726:         foreach my $auth (@authtypes) {
 8727:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 8728:                 $authhash{$item}{$auth} = 1;
 8729:             } else {
 8730:                 $authhash{$item}{$auth} = 0;
 8731:             }
 8732:         }
 8733:     }
 8734:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 8735:         foreach my $item (@authen_contexts) {
 8736:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 8737:                 foreach my $auth (@authtypes) {
 8738:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 8739:                         push(@{$changes{'authtypes'}},$item);
 8740:                         last;
 8741:                     }
 8742:                 }
 8743:             }
 8744:         }
 8745:     } else {
 8746:         foreach my $item (@authen_contexts) {
 8747:             push(@{$changes{'authtypes'}},$item);
 8748:         }
 8749:     }
 8750: 
 8751:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
 8752:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
 8753:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
 8754:     $save_usercreate{'id_rule'} = \@id_rule;
 8755:     $save_usercreate{'username_rule'} = \@username_rule,
 8756:     $save_usercreate{'authtypes'} = \%authhash;
 8757: 
 8758:     my %usercreation_hash =  (
 8759:         usercreation     => \%save_usercreate,
 8760:     );
 8761: 
 8762:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 8763:                                              $dom);
 8764: 
 8765:     if ($putresult eq 'ok') {
 8766:         if (keys(%changes) > 0) {
 8767:             $resulttext = &mt('Changes made:').'<ul>';
 8768:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8769:                 my %lt = &usercreation_types();
 8770:                 foreach my $type (@{$changes{'cancreate'}}) {
 8771:                     my $chgtext = $lt{$type}.', ';
 8772:                     if ($cancreate{$type} eq 'none') {
 8773:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 8774:                     } elsif ($cancreate{$type} eq 'any') {
 8775:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 8776:                     } elsif ($cancreate{$type} eq 'official') {
 8777:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 8778:                     } elsif ($cancreate{$type} eq 'unofficial') {
 8779:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 8780:                     }
 8781:                     $resulttext .= '<li>'.$chgtext.'</li>';
 8782:                 }
 8783:             }
 8784:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 8785:                 my ($rules,$ruleorder) = 
 8786:                     &Apache::lonnet::inst_userrules($dom,'username');
 8787:                 my $chgtext = '<ul>';
 8788:                 foreach my $type (@username_rule) {
 8789:                     if (ref($rules->{$type}) eq 'HASH') {
 8790:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 8791:                     }
 8792:                 }
 8793:                 $chgtext .= '</ul>';
 8794:                 if (@username_rule > 0) {
 8795:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 8796:                 } else {
 8797:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 8798:                 }
 8799:             }
 8800:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 8801:                 my ($idrules,$idruleorder) = 
 8802:                     &Apache::lonnet::inst_userrules($dom,'id');
 8803:                 my $chgtext = '<ul>';
 8804:                 foreach my $type (@id_rule) {
 8805:                     if (ref($idrules->{$type}) eq 'HASH') {
 8806:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 8807:                     }
 8808:                 }
 8809:                 $chgtext .= '</ul>';
 8810:                 if (@id_rule > 0) {
 8811:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 8812:                 } else {
 8813:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 8814:                 }
 8815:             }
 8816:             my %authname = &authtype_names();
 8817:             my %context_title = &context_names();
 8818:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 8819:                 my $chgtext = '<ul>';
 8820:                 foreach my $type (@{$changes{'authtypes'}}) {
 8821:                     my @allowed;
 8822:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 8823:                     foreach my $auth (@authtypes) {
 8824:                         if ($authhash{$type}{$auth}) {
 8825:                             push(@allowed,$authname{$auth});
 8826:                         }
 8827:                     }
 8828:                     if (@allowed > 0) {
 8829:                         $chgtext .= join(', ',@allowed).'</li>';
 8830:                     } else {
 8831:                         $chgtext .= &mt('none').'</li>';
 8832:                     }
 8833:                 }
 8834:                 $chgtext .= '</ul>';
 8835:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 8836:                 $resulttext .= '</li>';
 8837:             }
 8838:             $resulttext .= '</ul>';
 8839:         } else {
 8840:             $resulttext = &mt('No changes made to user creation settings');
 8841:         }
 8842:     } else {
 8843:         $resulttext = '<span class="LC_error">'.
 8844:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8845:     }
 8846:     if ($warningmsg ne '') {
 8847:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8848:     }
 8849:     return $resulttext;
 8850: }
 8851: 
 8852: sub modify_selfcreation {
 8853:     my ($dom,%domconfig) = @_;
 8854:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
 8855:     my (%save_usercreate,%save_usermodify);
 8856:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8857:     if (ref($types) eq 'ARRAY') {
 8858:         $usertypes->{'default'} = $othertitle;
 8859:         push(@{$types},'default');
 8860:     }
 8861: #
 8862: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
 8863: #
 8864:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8865:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8866:             if ($key eq 'cancreate') {
 8867:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8868:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8869:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8870:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
 8871:                             ($item eq 'recaptchaversion') ||
 8872:                             ($item eq 'emailusername') || ($item eq 'notify') ||
 8873:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
 8874:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8875:                         } else {
 8876:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8877:                         }
 8878:                     }
 8879:                 }
 8880:             } elsif ($key eq 'email_rule') {
 8881:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8882:             } else {
 8883:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8884:             }
 8885:         }
 8886:     }
 8887: #
 8888: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
 8889: #
 8890:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 8891:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 8892:             if ($key eq 'selfcreate') {
 8893:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8894:             } else {
 8895:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8896:             }
 8897:         }
 8898:     }
 8899: 
 8900:     my @contexts = ('selfcreate');
 8901:     @{$cancreate{'selfcreate'}} = ();
 8902:     %{$cancreate{'emailusername'}} = ();
 8903:     @{$cancreate{'statustocreate'}} = ();
 8904:     %{$cancreate{'selfcreateprocessing'}} = ();
 8905:     %{$cancreate{'shibenv'}} = ();
 8906:     my %selfcreatetypes = (
 8907:                              sso   => 'users authenticated by institutional single sign on',
 8908:                              login => 'users authenticated by institutional log-in',
 8909:                              email => 'users who provide a valid e-mail address for use as username',
 8910:                           );
 8911: #
 8912: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
 8913: # is permitted.
 8914: #
 8915: 
 8916:     my @statuses;
 8917:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 8918:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
 8919:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
 8920:         }
 8921:     }
 8922:     push(@statuses,'default');
 8923: 
 8924:     foreach my $item ('login','sso','email') {
 8925:         if ($item eq 'email') {
 8926:             if ($env{'form.cancreate_email'}) {
 8927:                 push(@{$cancreate{'selfcreate'}},'email');
 8928:                 push(@contexts,'selfcreateprocessing');
 8929:                 foreach my $type (@statuses) {
 8930:                     if ($type eq 'default') {
 8931:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
 8932:                     } else { 
 8933:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
 8934:                     }
 8935:                 }
 8936:             }
 8937:         } else {
 8938:             if ($env{'form.cancreate_'.$item}) {
 8939:                 push(@{$cancreate{'selfcreate'}},$item);
 8940:             }
 8941:         }
 8942:     }
 8943:     my (@email_rule,%userinfo,%savecaptcha);
 8944:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 8945: #
 8946: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
 8947: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
 8948: #
 8949: 
 8950:     if ($env{'form.cancreate_email'}) {
 8951:         push(@contexts,'emailusername');
 8952:         if (ref($types) eq 'ARRAY') {
 8953:             foreach my $type (@{$types}) {
 8954:                 if (ref($infofields) eq 'ARRAY') {
 8955:                     foreach my $field (@{$infofields}) {
 8956:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
 8957:                             $cancreate{'emailusername'}{$type}{$field} = $1;
 8958:                         }
 8959:                     }
 8960:                 }
 8961:             }
 8962:         }
 8963: #
 8964: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
 8965: # queued requests for self-creation of account using e-mail address as username
 8966: #
 8967: 
 8968:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
 8969:         @approvalnotify = sort(@approvalnotify);
 8970:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
 8971:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8972:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
 8973:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
 8974:                     push(@{$changes{'cancreate'}},'notify');
 8975:                 }
 8976:             } else {
 8977:                 if ($cancreate{'notify'}{'approval'}) {
 8978:                     push(@{$changes{'cancreate'}},'notify');
 8979:                 }
 8980:             }
 8981:         } elsif ($cancreate{'notify'}{'approval'}) {
 8982:             push(@{$changes{'cancreate'}},'notify');
 8983:         }
 8984: 
 8985: #
 8986: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
 8987: #
 8988:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 8989:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
 8990:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 8991:             if (@{$curr_usercreation{'email_rule'}} > 0) {
 8992:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 8993:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
 8994:                         push(@{$changes{'email_rule'}},$type);
 8995:                     }
 8996:                 }
 8997:             }
 8998:             if (@email_rule > 0) {
 8999:                 foreach my $type (@email_rule) {
 9000:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 9001:                         push(@{$changes{'email_rule'}},$type);
 9002:                     }
 9003:                 }
 9004:             }
 9005:         } elsif (@email_rule > 0) {
 9006:             push(@{$changes{'email_rule'}},@email_rule);
 9007:         }
 9008:     }
 9009: #  
 9010: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
 9011: # institutional log-in.
 9012: #
 9013:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
 9014:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9015:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
 9016:                ($domdefaults{'auth_def'} eq 'localauth'))) {
 9017:             $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.').' '.
 9018:                           &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.');
 9019:         }
 9020:     }
 9021:     my @fields = ('lastname','firstname','middlename','generation',
 9022:                   'permanentemail','id');
 9023:     my @shibfields = (@fields,'inststatus');
 9024:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9025: #
 9026: # Where usernames may created for institutional log-in and/or institutional single sign on:
 9027: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
 9028: # may self-create accounts 
 9029: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
 9030: # which the user may supply, if institutional data is unavailable.
 9031: #
 9032:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
 9033:         if (ref($types) eq 'ARRAY') {
 9034:             if (@{$types} > 1) {
 9035:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
 9036:                 push(@contexts,'statustocreate');
 9037:             } else {
 9038:                 undef($cancreate{'statustocreate'});
 9039:             } 
 9040:             foreach my $type (@{$types}) {
 9041:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
 9042:                 foreach my $field (@fields) {
 9043:                     if (grep(/^\Q$field\E$/,@modifiable)) {
 9044:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
 9045:                     } else {
 9046:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
 9047:                     }
 9048:                 }
 9049:             }
 9050:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
 9051:                 foreach my $type (@{$types}) {
 9052:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
 9053:                         foreach my $field (@fields) {
 9054:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
 9055:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
 9056:                                 push(@{$changes{'selfcreate'}},$type);
 9057:                                 last;
 9058:                             }
 9059:                         }
 9060:                     }
 9061:                 }
 9062:             } else {
 9063:                 foreach my $type (@{$types}) {
 9064:                     push(@{$changes{'selfcreate'}},$type);
 9065:                 }
 9066:             }
 9067:         }
 9068:         foreach my $field (@shibfields) {
 9069:             if ($env{'form.shibenv_'.$field} ne '') {
 9070:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
 9071:             }
 9072:         }
 9073:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 9074:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
 9075:                 foreach my $field (@shibfields) {
 9076:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
 9077:                         push(@{$changes{'cancreate'}},'shibenv');
 9078:                     }
 9079:                 }
 9080:             } else {
 9081:                 foreach my $field (@shibfields) {
 9082:                     if ($env{'form.shibenv_'.$field}) {
 9083:                         push(@{$changes{'cancreate'}},'shibenv');
 9084:                         last;
 9085:                     }
 9086:                 }
 9087:             }
 9088:         }
 9089:     }
 9090:     foreach my $item (@contexts) {
 9091:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 9092:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 9093:                 if (ref($cancreate{$item}) eq 'ARRAY') {
 9094:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 9095:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9096:                             push(@{$changes{'cancreate'}},$item);
 9097:                         }
 9098:                     }
 9099:                 }
 9100:             }
 9101:             if (ref($cancreate{$item}) eq 'ARRAY') {
 9102:                 foreach my $type (@{$cancreate{$item}}) {
 9103:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 9104:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9105:                             push(@{$changes{'cancreate'}},$item);
 9106:                         }
 9107:                     }
 9108:                 }
 9109:             }
 9110:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
 9111:             if (ref($cancreate{$item}) eq 'HASH') {
 9112:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
 9113:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
 9114:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
 9115:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
 9116:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9117:                                     push(@{$changes{'cancreate'}},$item);
 9118:                                 }
 9119:                             }
 9120:                         }
 9121:                     } elsif ($item eq 'selfcreateprocessing') {
 9122:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
 9123:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9124:                                 push(@{$changes{'cancreate'}},$item);
 9125:                             }
 9126:                         }
 9127:                     } else {
 9128:                         if (!$cancreate{$item}{$curr}) {
 9129:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9130:                                 push(@{$changes{'cancreate'}},$item);
 9131:                             }
 9132:                         }
 9133:                     }
 9134:                 }
 9135:                 foreach my $field (keys(%{$cancreate{$item}})) {
 9136:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
 9137:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
 9138:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
 9139:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
 9140:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9141:                                         push(@{$changes{'cancreate'}},$item);
 9142:                                     }
 9143:                                 }
 9144:                             } else {
 9145:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9146:                                     push(@{$changes{'cancreate'}},$item);
 9147:                                 }
 9148:                             }
 9149:                         }
 9150:                     } elsif ($item eq 'selfcreateprocessing') {
 9151:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
 9152:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9153:                                 push(@{$changes{'cancreate'}},$item);
 9154:                             }
 9155:                         }
 9156:                     } else {
 9157:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
 9158:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9159:                                 push(@{$changes{'cancreate'}},$item);
 9160:                             }
 9161:                         }
 9162:                     }
 9163:                 }
 9164:             }
 9165:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
 9166:             if (ref($cancreate{$item}) eq 'ARRAY') {
 9167:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
 9168:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9169:                         push(@{$changes{'cancreate'}},$item);
 9170:                     }
 9171:                 }
 9172:             } elsif (ref($cancreate{$item}) eq 'HASH') {
 9173:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
 9174:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9175:                         push(@{$changes{'cancreate'}},$item);
 9176:                     }
 9177:                 }
 9178:             }
 9179:         } elsif ($item eq 'emailusername') {
 9180:             if (ref($cancreate{$item}) eq 'HASH') {
 9181:                 foreach my $type (keys(%{$cancreate{$item}})) {
 9182:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
 9183:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
 9184:                             if ($cancreate{$item}{$type}{$field}) {
 9185:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9186:                                     push(@{$changes{'cancreate'}},$item);
 9187:                                 }
 9188:                                 last;
 9189:                             }
 9190:                         }
 9191:                     }
 9192:                 }
 9193:             }
 9194:         }
 9195:     }
 9196: #
 9197: # Populate %save_usercreate hash with updates to self-creation configuration.
 9198: #
 9199:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
 9200:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
 9201:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
 9202:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
 9203:     if (ref($cancreate{'notify'}) eq 'HASH') {
 9204:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
 9205:     }
 9206:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
 9207:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
 9208:     }
 9209:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 9210:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
 9211:     }
 9212:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
 9213:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
 9214:     }
 9215:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
 9216:     $save_usercreate{'emailrule'} = \@email_rule;
 9217: 
 9218:     my %userconfig_hash = (
 9219:             usercreation     => \%save_usercreate,
 9220:             usermodification => \%save_usermodify,
 9221:     );
 9222:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
 9223:                                              $dom);
 9224: #
 9225: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
 9226: #
 9227:     if ($putresult eq 'ok') {
 9228:         if (keys(%changes) > 0) {
 9229:             $resulttext = &mt('Changes made:').'<ul>';
 9230:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 9231:                 my %lt = &selfcreation_types();
 9232:                 foreach my $type (@{$changes{'cancreate'}}) {
 9233:                     my $chgtext;
 9234:                     if ($type eq 'selfcreate') {
 9235:                         if (@{$cancreate{$type}} == 0) {
 9236:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
 9237:                         } else {
 9238:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
 9239:                                         '<ul>';
 9240:                             foreach my $case (@{$cancreate{$type}}) {
 9241:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 9242:                             }
 9243:                             $chgtext .= '</ul>';
 9244:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 9245:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 9246:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 9247:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 9248:                                             $chgtext .= '<br />'.
 9249:                                                         '<span class="LC_warning">'.
 9250:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 9251:                                                         '</span>';
 9252:                                         }
 9253:                                     }
 9254:                                 }
 9255:                             }
 9256:                         }
 9257:                     } elsif ($type eq 'shibenv') {
 9258:                         if (keys(%{$cancreate{$type}}) == 0) {
 9259:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
 9260:                         } else {
 9261:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
 9262:                                         '<ul>';
 9263:                             foreach my $field (@shibfields) {
 9264:                                 next if ($cancreate{$type}{$field} eq '');
 9265:                                 if ($field eq 'inststatus') {
 9266:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
 9267:                                 } else {
 9268:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
 9269:                                 }
 9270:                             }
 9271:                             $chgtext .= '</ul>';
 9272:                         }  
 9273:                     } elsif ($type eq 'statustocreate') {
 9274:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 9275:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 9276:                             if (@{$cancreate{'selfcreate'}} > 0) {
 9277:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 9278:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 9279:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 9280:                                         $chgtext .= '<br />'.
 9281:                                                     '<span class="LC_warning">'.
 9282:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 9283:                                                     '</span>';
 9284:                                     }
 9285:                                 } elsif (ref($usertypes) eq 'HASH') {
 9286:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 9287:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 9288:                                     } else {
 9289:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 9290:                                     }
 9291:                                     $chgtext .= '<ul>';
 9292:                                     foreach my $case (@{$cancreate{$type}}) {
 9293:                                         if ($case eq 'default') {
 9294:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 9295:                                         } else {
 9296:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 9297:                                         }
 9298:                                     }
 9299:                                     $chgtext .= '</ul>';
 9300:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 9301:                                         $chgtext .= '<br /><span class="LC_warning">'.
 9302:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
 9303:                                                     '</span>';
 9304:                                     }
 9305:                                 }
 9306:                             } else {
 9307:                                 if (@{$cancreate{$type}} == 0) {
 9308:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 9309:                                 } else {
 9310:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 9311:                                 }
 9312:                             }
 9313:                         }
 9314:                     } elsif ($type eq 'selfcreateprocessing') {
 9315:                         my %choices = &Apache::lonlocal::texthash (
 9316:                                                                     automatic => 'Automatic approval',
 9317:                                                                     approval  => 'Queued for approval',
 9318:                                                                   );
 9319:                         if (@statuses > 1) {
 9320:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
 9321:                                         '<ul>';
 9322:                            foreach my $type (@statuses) {
 9323:                                if ($type eq 'default') {
 9324:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 9325:                                } else {
 9326:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 9327:                                }
 9328:                            }
 9329:                            $chgtext .= '</ul>';
 9330:                         } else {
 9331:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
 9332:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
 9333:                         }
 9334:                     } elsif ($type eq 'captcha') {
 9335:                         if ($savecaptcha{$type} eq 'notused') {
 9336:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 9337:                         } else {
 9338:                             my %captchas = &captcha_phrases();
 9339:                             if ($captchas{$savecaptcha{$type}}) {
 9340:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
 9341:                             } else {
 9342:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 9343:                             }
 9344:                         }
 9345:                     } elsif ($type eq 'recaptchakeys') {
 9346:                         my ($privkey,$pubkey);
 9347:                         if (ref($savecaptcha{$type}) eq 'HASH') {
 9348:                             $pubkey = $savecaptcha{$type}{'public'};
 9349:                             $privkey = $savecaptcha{$type}{'private'};
 9350:                         }
 9351:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 9352:                         if (!$pubkey) {
 9353:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 9354:                         } else {
 9355:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 9356:                         }
 9357:                         if (!$privkey) {
 9358:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 9359:                         } else {
 9360:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 9361:                         }
 9362:                         $chgtext .= '</ul>';
 9363:                     } elsif ($type eq 'recaptchaversion') {
 9364:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
 9365:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
 9366:                         }
 9367:                     } elsif ($type eq 'emailusername') {
 9368:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
 9369:                             if (ref($types) eq 'ARRAY') {
 9370:                                 foreach my $type (@{$types}) {
 9371:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
 9372:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
 9373:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
 9374:                                                     '<ul>';
 9375:                                             foreach my $field (@{$infofields}) {
 9376:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
 9377:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
 9378:                                                 }
 9379:                                             }
 9380:                                             $chgtext .= '</ul>';
 9381:                                         } else {
 9382:                                             $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 />';
 9383:                                         }
 9384:                                     } else {
 9385:                                         $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 />';
 9386:                                     }
 9387:                                 }
 9388:                             }
 9389:                         }
 9390:                     } elsif ($type eq 'notify') {
 9391:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
 9392:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
 9393:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
 9394:                                 if ($cancreate{'notify'}{'approval'}) {
 9395:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
 9396:                                 }
 9397:                             }
 9398:                         }
 9399:                     }
 9400:                     if ($chgtext) {
 9401:                         $resulttext .= '<li>'.$chgtext.'</li>';
 9402:                     }
 9403:                 }
 9404:             }
 9405:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 9406:                 my ($emailrules,$emailruleorder) =
 9407:                     &Apache::lonnet::inst_userrules($dom,'email');
 9408:                 my $chgtext = '<ul>';
 9409:                 foreach my $type (@email_rule) {
 9410:                     if (ref($emailrules->{$type}) eq 'HASH') {
 9411:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 9412:                     }
 9413:                 }
 9414:                 $chgtext .= '</ul>';
 9415:                 if (@email_rule > 0) {
 9416:                     $resulttext .= '<li>'.
 9417:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
 9418:                                        $chgtext.
 9419:                                    '</li>';
 9420:                 } else {
 9421:                     $resulttext .= '<li>'.
 9422:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
 9423:                                    '</li>';
 9424:                 }
 9425:             }
 9426:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
 9427:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
 9428:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9429:                 foreach my $type (@{$changes{'selfcreate'}}) {
 9430:                     my $typename = $type;
 9431:                     if (ref($usertypes) eq 'HASH') {
 9432:                         if ($usertypes->{$type} ne '') {
 9433:                             $typename = $usertypes->{$type};
 9434:                         }
 9435:                     }
 9436:                     my @modifiable;
 9437:                     $resulttext .= '<li>'.
 9438:                                     &mt('Self-creation of account by users with status: [_1]',
 9439:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
 9440:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
 9441:                     foreach my $field (@fields) {
 9442:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
 9443:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
 9444:                         }
 9445:                     }
 9446:                     if (@modifiable > 0) {
 9447:                         $resulttext .= join(', ',@modifiable);
 9448:                     } else {
 9449:                         $resulttext .= &mt('none');
 9450:                     }
 9451:                     $resulttext .= '</li>';
 9452:                 }
 9453:                 $resulttext .= '</ul></li>';
 9454:             }
 9455:             $resulttext .= '</ul>';
 9456:         } else {
 9457:             $resulttext = &mt('No changes made to self-creation settings');
 9458:         }
 9459:     } else {
 9460:         $resulttext = '<span class="LC_error">'.
 9461:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9462:     }
 9463:     if ($warningmsg ne '') {
 9464:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 9465:     }
 9466:     return $resulttext;
 9467: }
 9468: 
 9469: sub process_captcha {
 9470:     my ($container,$changes,$newsettings,$current) = @_;
 9471:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 9472:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 9473:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 9474:         $newsettings->{'captcha'} = 'original';
 9475:     }
 9476:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 9477:         if ($container eq 'cancreate') {
 9478:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9479:                 push(@{$changes->{'cancreate'}},'captcha');
 9480:             } elsif (!defined($changes->{'cancreate'})) {
 9481:                 $changes->{'cancreate'} = ['captcha'];
 9482:             }
 9483:         } else {
 9484:             $changes->{'captcha'} = 1;
 9485:         }
 9486:     }
 9487:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
 9488:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 9489:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 9490:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 9491:         $newpub =~ s/[^\w\-]//g;
 9492:         $newpriv =~ s/[^\w\-]//g;
 9493:         $newsettings->{'recaptchakeys'} = {
 9494:                                              public  => $newpub,
 9495:                                              private => $newpriv,
 9496:                                           };
 9497:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
 9498:         $newversion =~ s/\D//g;
 9499:         if ($newversion ne '2') {
 9500:             $newversion = 1;
 9501:         }
 9502:         $newsettings->{'recaptchaversion'} = $newversion;
 9503:     }
 9504:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 9505:         $currpub = $current->{'recaptchakeys'}{'public'};
 9506:         $currpriv = $current->{'recaptchakeys'}{'private'};
 9507:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 9508:             $newsettings->{'recaptchakeys'} = {
 9509:                                                  public  => '',
 9510:                                                  private => '',
 9511:                                               }
 9512:         }
 9513:     }
 9514:     if ($current->{'captcha'} eq 'recaptcha') {
 9515:         $currversion = $current->{'recaptchaversion'};
 9516:         if ($currversion ne '2') {
 9517:             $currversion = 1;
 9518:         }
 9519:     }
 9520:     if ($currversion ne $newversion) {
 9521:         if ($container eq 'cancreate') {
 9522:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9523:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
 9524:             } elsif (!defined($changes->{'cancreate'})) {
 9525:                 $changes->{'cancreate'} = ['recaptchaversion'];
 9526:             }
 9527:         } else {
 9528:             $changes->{'recaptchaversion'} = 1;
 9529:         }
 9530:     }
 9531:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 9532:         if ($container eq 'cancreate') {
 9533:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9534:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 9535:             } elsif (!defined($changes->{'cancreate'})) {
 9536:                 $changes->{'cancreate'} = ['recaptchakeys'];
 9537:             }
 9538:         } else {
 9539:             $changes->{'recaptchakeys'} = 1;
 9540:         }
 9541:     }
 9542:     return;
 9543: }
 9544: 
 9545: sub modify_usermodification {
 9546:     my ($dom,%domconfig) = @_;
 9547:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
 9548:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 9549:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 9550:             if ($key eq 'selfcreate') {
 9551:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
 9552:             } else {  
 9553:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 9554:             }
 9555:         }
 9556:     }
 9557:     my @contexts = ('author','course');
 9558:     my %context_title = (
 9559:                            author => 'In author context',
 9560:                            course => 'In course context',
 9561:                         );
 9562:     my @fields = ('lastname','firstname','middlename','generation',
 9563:                   'permanentemail','id');
 9564:     my %roles = (
 9565:                   author => ['ca','aa'],
 9566:                   course => ['st','ep','ta','in','cr'],
 9567:                 );
 9568:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9569:     foreach my $context (@contexts) {
 9570:         foreach my $role (@{$roles{$context}}) {
 9571:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 9572:             foreach my $item (@fields) {
 9573:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 9574:                     $modifyhash{$context}{$role}{$item} = 1;
 9575:                 } else {
 9576:                     $modifyhash{$context}{$role}{$item} = 0;
 9577:                 }
 9578:             }
 9579:         }
 9580:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 9581:             foreach my $role (@{$roles{$context}}) {
 9582:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 9583:                     foreach my $field (@fields) {
 9584:                         if ($modifyhash{$context}{$role}{$field} ne 
 9585:                                 $curr_usermodification{$context}{$role}{$field}) {
 9586:                             push(@{$changes{$context}},$role);
 9587:                             last;
 9588:                         }
 9589:                     }
 9590:                 }
 9591:             }
 9592:         } else {
 9593:             foreach my $context (@contexts) {
 9594:                 foreach my $role (@{$roles{$context}}) {
 9595:                     push(@{$changes{$context}},$role);
 9596:                 }
 9597:             }
 9598:         }
 9599:     }
 9600:     my %usermodification_hash =  (
 9601:                                    usermodification => \%modifyhash,
 9602:                                  );
 9603:     my $putresult = &Apache::lonnet::put_dom('configuration',
 9604:                                              \%usermodification_hash,$dom);
 9605:     if ($putresult eq 'ok') {
 9606:         if (keys(%changes) > 0) {
 9607:             $resulttext = &mt('Changes made: ').'<ul>';
 9608:             foreach my $context (@contexts) {
 9609:                 if (ref($changes{$context}) eq 'ARRAY') {
 9610:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 9611:                     if (ref($changes{$context}) eq 'ARRAY') {
 9612:                         foreach my $role (@{$changes{$context}}) {
 9613:                             my $rolename;
 9614:                             if ($role eq 'cr') {
 9615:                                 $rolename = &mt('Custom');
 9616:                             } else {
 9617:                                 $rolename = &Apache::lonnet::plaintext($role);
 9618:                             }
 9619:                             my @modifiable;
 9620:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 9621:                             foreach my $field (@fields) {
 9622:                                 if ($modifyhash{$context}{$role}{$field}) {
 9623:                                     push(@modifiable,$fieldtitles{$field});
 9624:                                 }
 9625:                             }
 9626:                             if (@modifiable > 0) {
 9627:                                 $resulttext .= join(', ',@modifiable);
 9628:                             } else {
 9629:                                 $resulttext .= &mt('none'); 
 9630:                             }
 9631:                             $resulttext .= '</li>';
 9632:                         }
 9633:                         $resulttext .= '</ul></li>';
 9634:                     }
 9635:                 }
 9636:             }
 9637:             $resulttext .= '</ul>';
 9638:         } else {
 9639:             $resulttext = &mt('No changes made to user modification settings');
 9640:         }
 9641:     } else {
 9642:         $resulttext = '<span class="LC_error">'.
 9643:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9644:     }
 9645:     return $resulttext;
 9646: }
 9647: 
 9648: sub modify_defaults {
 9649:     my ($dom,$lastactref,%domconfig) = @_;
 9650:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 9651:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9652:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 9653:     my @authtypes = ('internal','krb4','krb5','localauth');
 9654:     foreach my $item (@items) {
 9655:         $newvalues{$item} = $env{'form.'.$item};
 9656:         if ($item eq 'auth_def') {
 9657:             if ($newvalues{$item} ne '') {
 9658:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 9659:                     push(@errors,$item);
 9660:                 }
 9661:             }
 9662:         } elsif ($item eq 'lang_def') {
 9663:             if ($newvalues{$item} ne '') {
 9664:                 if ($newvalues{$item} =~ /^(\w+)/) {
 9665:                     my $langcode = $1;
 9666:                     if ($langcode ne 'x_chef') {
 9667:                         if (code2language($langcode) eq '') {
 9668:                             push(@errors,$item);
 9669:                         }
 9670:                     }
 9671:                 } else {
 9672:                     push(@errors,$item);
 9673:                 }
 9674:             }
 9675:         } elsif ($item eq 'timezone_def') {
 9676:             if ($newvalues{$item} ne '') {
 9677:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 9678:                     push(@errors,$item);   
 9679:                 }
 9680:             }
 9681:         } elsif ($item eq 'datelocale_def') {
 9682:             if ($newvalues{$item} ne '') {
 9683:                 my @datelocale_ids = DateTime::Locale->ids();
 9684:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 9685:                     push(@errors,$item);
 9686:                 }
 9687:             }
 9688:         } elsif ($item eq 'portal_def') {
 9689:             if ($newvalues{$item} ne '') {
 9690:                 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])\/?$/) {
 9691:                     push(@errors,$item);
 9692:                 }
 9693:             }
 9694:         }
 9695:         if (grep(/^\Q$item\E$/,@errors)) {
 9696:             $newvalues{$item} = $domdefaults{$item};
 9697:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 9698:             $changes{$item} = 1;
 9699:         }
 9700:         $domdefaults{$item} = $newvalues{$item};
 9701:     }
 9702:     my %defaults_hash = (
 9703:                          defaults => \%newvalues,
 9704:                         );
 9705:     my $title = &defaults_titles();
 9706: 
 9707:     my $currinststatus;
 9708:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 9709:         $currinststatus = $domconfig{'inststatus'};
 9710:     } else {
 9711:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9712:         $currinststatus = {
 9713:                              inststatustypes => $usertypes,
 9714:                              inststatusorder => $types,
 9715:                              inststatusguest => [],
 9716:                           };
 9717:     }
 9718:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
 9719:     my @allpos;
 9720:     my %guests;
 9721:     my %alltypes;
 9722:     my ($currtitles,$currguests,$currorder);
 9723:     if (ref($currinststatus) eq 'HASH') {
 9724:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
 9725:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
 9726:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
 9727:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
 9728:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
 9729:                     }
 9730:                 }
 9731:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
 9732:                     my $position = $env{'form.inststatus_pos_'.$type};
 9733:                     $position =~ s/\D+//g;
 9734:                     $allpos[$position] = $type;
 9735:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
 9736:                     $alltypes{$type} =~ s/`//g;
 9737:                     if ($env{'form.inststatus_guest_'.$type}) {
 9738:                         $guests{$type} = 1;
 9739:                     }
 9740:                 }
 9741:             }
 9742:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
 9743:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
 9744:             }
 9745:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
 9746:             $currtitles =~ s/,$//;
 9747:         }
 9748:     }
 9749:     if ($env{'form.addinststatus'}) {
 9750:         my $newtype = $env{'form.addinststatus'};
 9751:         $newtype =~ s/\W//g;
 9752:         unless (exists($alltypes{$newtype})) {
 9753:             if ($env{'form.addinststatus_guest'}) {
 9754:                 $guests{$newtype} = 1;
 9755:             }
 9756:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
 9757:             $alltypes{$newtype} =~ s/`//g; 
 9758:             my $position = $env{'form.addinststatus_pos'};
 9759:             $position =~ s/\D+//g;
 9760:             if ($position ne '') {
 9761:                 $allpos[$position] = $newtype;
 9762:             }
 9763:         }
 9764:     }
 9765:     my (@orderedstatus,@orderedguests);
 9766:     foreach my $type (@allpos) {
 9767:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
 9768:             push(@orderedstatus,$type);
 9769:             if ($guests{$type}) {
 9770:                 push(@orderedguests,$type);
 9771:             }
 9772:         }
 9773:     }
 9774:     foreach my $type (keys(%alltypes)) {
 9775:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
 9776:             delete($alltypes{$type});
 9777:         }
 9778:     }
 9779:     $defaults_hash{'inststatus'} = {
 9780:                                      inststatustypes => \%alltypes,
 9781:                                      inststatusorder => \@orderedstatus,
 9782:                                      inststatusguest => \@orderedguests,
 9783:                                    };
 9784:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
 9785:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 9786:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
 9787:         }
 9788:     }
 9789:     if ($currorder ne join(',',@orderedstatus)) {
 9790:         $changes{'inststatus'}{'inststatusorder'} = 1;
 9791:     }
 9792:     if ($currguests ne join(',',@orderedguests)) {
 9793:         $changes{'inststatus'}{'inststatusguest'} = 1;
 9794:     }
 9795:     my $newtitles;
 9796:     foreach my $item (@orderedstatus) {
 9797:         $newtitles .= $alltypes{$item}.',';
 9798:     }
 9799:     $newtitles =~ s/,$//;
 9800:     if ($currtitles ne $newtitles) {
 9801:         $changes{'inststatus'}{'inststatustypes'} = 1;
 9802:     }
 9803:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 9804:                                              $dom);
 9805:     if ($putresult eq 'ok') {
 9806:         if (keys(%changes) > 0) {
 9807:             $resulttext = &mt('Changes made:').'<ul>';
 9808:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 9809:             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";
 9810:             foreach my $item (sort(keys(%changes))) {
 9811:                 if ($item eq 'inststatus') {
 9812:                     if (ref($changes{'inststatus'}) eq 'HASH') {
 9813:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
 9814:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
 9815:                             foreach my $type (@orderedstatus) { 
 9816:                                 $resulttext .= $alltypes{$type}.', ';
 9817:                             }
 9818:                             $resulttext =~ s/, $//;
 9819:                             $resulttext .= '</li>';
 9820:                         }
 9821:                         if ($changes{'inststatus'}{'inststatusguest'}) {
 9822:                             $resulttext .= '<li>'; 
 9823:                             if (@orderedguests) {
 9824:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
 9825:                                 foreach my $type (@orderedguests) {
 9826:                                     $resulttext .= $alltypes{$type}.', ';
 9827:                                 }
 9828:                                 $resulttext =~ s/, $//;
 9829:                             } else {
 9830:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
 9831:                             }
 9832:                             $resulttext .= '</li>';
 9833:                         }
 9834:                     }
 9835:                 } else {
 9836:                     my $value = $env{'form.'.$item};
 9837:                     if ($value eq '') {
 9838:                         $value = &mt('none');
 9839:                     } elsif ($item eq 'auth_def') {
 9840:                         my %authnames = &authtype_names();
 9841:                         my %shortauth = (
 9842:                                           internal   => 'int',
 9843:                                           krb4       => 'krb4',
 9844:                                           krb5       => 'krb5',
 9845:                                           localauth  => 'loc',
 9846:                         );
 9847:                         $value = $authnames{$shortauth{$value}};
 9848:                     }
 9849:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 9850:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
 9851:                 }
 9852:             }
 9853:             $resulttext .= '</ul>';
 9854:             $mailmsgtext .= "\n";
 9855:             my $cachetime = 24*60*60;
 9856:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9857:             if (ref($lastactref) eq 'HASH') {
 9858:                 $lastactref->{'domdefaults'} = 1;
 9859:             }
 9860:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 9861:                 my $notify = 1;
 9862:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 9863:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 9864:                         $notify = 0;
 9865:                     }
 9866:                 }
 9867:                 if ($notify) {
 9868:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 9869:                                                "LON-CAPA Domain Settings Change - $dom",
 9870:                                                $mailmsgtext);
 9871:                 }
 9872:             }
 9873:         } else {
 9874:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 9875:         }
 9876:     } else {
 9877:         $resulttext = '<span class="LC_error">'.
 9878:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9879:     }
 9880:     if (@errors > 0) {
 9881:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 9882:         foreach my $item (@errors) {
 9883:             $resulttext .= ' "'.$title->{$item}.'",';
 9884:         }
 9885:         $resulttext =~ s/,$//;
 9886:     }
 9887:     return $resulttext;
 9888: }
 9889: 
 9890: sub modify_scantron {
 9891:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 9892:     my ($resulttext,%confhash,%changes,$errors);
 9893:     my $custom = 'custom.tab';
 9894:     my $default = 'default.tab';
 9895:     my $servadm = $r->dir_config('lonAdmEMail');
 9896:     my ($configuserok,$author_ok,$switchserver) = 
 9897:         &config_check($dom,$confname,$servadm);
 9898:     if ($env{'form.scantronformat.filename'} ne '') {
 9899:         my $error;
 9900:         if ($configuserok eq 'ok') {
 9901:             if ($switchserver) {
 9902:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 9903:             } else {
 9904:                 if ($author_ok eq 'ok') {
 9905:                     my ($result,$scantronurl) =
 9906:                         &publishlogo($r,'upload','scantronformat',$dom,
 9907:                                      $confname,'scantron','','',$custom);
 9908:                     if ($result eq 'ok') {
 9909:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 9910:                         $changes{'scantronformat'} = 1;
 9911:                     } else {
 9912:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 9913:                     }
 9914:                 } else {
 9915:                     $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);
 9916:                 }
 9917:             }
 9918:         } else {
 9919:             $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);
 9920:         }
 9921:         if ($error) {
 9922:             &Apache::lonnet::logthis($error);
 9923:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9924:         }
 9925:     }
 9926:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 9927:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 9928:             if ($env{'form.scantronformat_del'}) {
 9929:                 $confhash{'scantron'}{'scantronformat'} = '';
 9930:                 $changes{'scantronformat'} = 1;
 9931:             }
 9932:         }
 9933:     }
 9934:     if (keys(%confhash) > 0) {
 9935:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 9936:                                                  $dom);
 9937:         if ($putresult eq 'ok') {
 9938:             if (keys(%changes) > 0) {
 9939:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 9940:                     $resulttext = &mt('Changes made:').'<ul>';
 9941:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 9942:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 9943:                     } else {
 9944:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 9945:                     }
 9946:                     $resulttext .= '</ul>';
 9947:                 } else {
 9948:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 9949:                 }
 9950:                 $resulttext .= '</ul>';
 9951:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9952:                 if (ref($lastactref) eq 'HASH') {
 9953:                     $lastactref->{'domainconfig'} = 1;
 9954:                 }
 9955:             } else {
 9956:                 $resulttext = &mt('No changes made to bubblesheet format file');
 9957:             }
 9958:         } else {
 9959:             $resulttext = '<span class="LC_error">'.
 9960:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 9961:         }
 9962:     } else {
 9963:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 9964:     }
 9965:     if ($errors) {
 9966:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9967:                        $errors.'</ul>';
 9968:     }
 9969:     return $resulttext;
 9970: }
 9971: 
 9972: sub modify_coursecategories {
 9973:     my ($dom,$lastactref,%domconfig) = @_;
 9974:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 9975:         $cathash);
 9976:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 9977:     my @catitems = ('unauth','auth');
 9978:     my @cattypes = ('std','domonly','codesrch','none');
 9979:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9980:         $cathash = $domconfig{'coursecategories'}{'cats'};
 9981:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 9982:             $changes{'togglecats'} = 1;
 9983:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 9984:         }
 9985:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 9986:             $changes{'categorize'} = 1;
 9987:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 9988:         }
 9989:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 9990:             $changes{'togglecatscomm'} = 1;
 9991:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 9992:         }
 9993:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 9994:             $changes{'categorizecomm'} = 1;
 9995:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 9996:         }
 9997:         foreach my $item (@catitems) {
 9998:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9999:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
10000:                     $changes{$item} = 1;
10001:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
10002:                 }
10003:             }
10004:         }
10005:     } else {
10006:         $changes{'togglecats'} = 1;
10007:         $changes{'categorize'} = 1;
10008:         $changes{'togglecatscomm'} = 1;
10009:         $changes{'categorizecomm'} = 1;
10010:         $domconfig{'coursecategories'} = {
10011:                                              togglecats => $env{'form.togglecats'},
10012:                                              categorize => $env{'form.categorize'},
10013:                                              togglecatscomm => $env{'form.togglecatscomm'},
10014:                                              categorizecomm => $env{'form.categorizecomm'},
10015:                                          };
10016:         foreach my $item (@catitems) {
10017:             if ($env{'form.coursecat_'.$item} ne 'std') {
10018:                 $changes{$item} = 1;
10019:             }
10020:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
10021:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
10022:             }
10023:         }
10024:     }
10025:     if (ref($cathash) eq 'HASH') {
10026:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
10027:             push (@deletecategory,'instcode::0');
10028:         }
10029:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
10030:             push(@deletecategory,'communities::0');
10031:         }
10032:     }
10033:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
10034:     if (ref($cathash) eq 'HASH') {
10035:         if (@deletecategory > 0) {
10036:             #FIXME Need to remove category from all courses using a deleted category 
10037:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
10038:             foreach my $item (@deletecategory) {
10039:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
10040:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
10041:                     $deletions{$item} = 1;
10042:                     &recurse_cat_deletes($item,$cathash,\%deletions);
10043:                 }
10044:             }
10045:         }
10046:         foreach my $item (keys(%{$cathash})) {
10047:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
10048:             if ($cathash->{$item} ne $env{'form.'.$item}) {
10049:                 $reorderings{$item} = 1;
10050:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
10051:             }
10052:             if ($env{'form.addcategory_name_'.$item} ne '') {
10053:                 my $newcat = $env{'form.addcategory_name_'.$item};
10054:                 my $newdepth = $depth+1;
10055:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
10056:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
10057:                 $adds{$newitem} = 1; 
10058:             }
10059:             if ($env{'form.subcat_'.$item} ne '') {
10060:                 my $newcat = $env{'form.subcat_'.$item};
10061:                 my $newdepth = $depth+1;
10062:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
10063:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
10064:                 $adds{$newitem} = 1;
10065:             }
10066:         }
10067:     }
10068:     if ($env{'form.instcode'} eq '1') {
10069:         if (ref($cathash) eq 'HASH') {
10070:             my $newitem = 'instcode::0';
10071:             if ($cathash->{$newitem} eq '') {  
10072:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
10073:                 $adds{$newitem} = 1;
10074:             }
10075:         } else {
10076:             my $newitem = 'instcode::0';
10077:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
10078:             $adds{$newitem} = 1;
10079:         }
10080:     }
10081:     if ($env{'form.communities'} eq '1') {
10082:         if (ref($cathash) eq 'HASH') {
10083:             my $newitem = 'communities::0';
10084:             if ($cathash->{$newitem} eq '') {
10085:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
10086:                 $adds{$newitem} = 1;
10087:             }
10088:         } else {
10089:             my $newitem = 'communities::0';
10090:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
10091:             $adds{$newitem} = 1;
10092:         }
10093:     }
10094:     if ($env{'form.addcategory_name'} ne '') {
10095:         if (($env{'form.addcategory_name'} ne 'instcode') &&
10096:             ($env{'form.addcategory_name'} ne 'communities')) {
10097:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
10098:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
10099:             $adds{$newitem} = 1;
10100:         }
10101:     }
10102:     my $putresult;
10103:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
10104:         if (keys(%deletions) > 0) {
10105:             foreach my $key (keys(%deletions)) {
10106:                 if ($predelallitems{$key} ne '') {
10107:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
10108:                 }
10109:             }
10110:         }
10111:         my (@chkcats,@chktrails,%chkallitems);
10112:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
10113:         if (ref($chkcats[0]) eq 'ARRAY') {
10114:             my $depth = 0;
10115:             my $chg = 0;
10116:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
10117:                 my $name = $chkcats[0][$i];
10118:                 my $item;
10119:                 if ($name eq '') {
10120:                     $chg ++;
10121:                 } else {
10122:                     $item = &escape($name).'::0';
10123:                     if ($chg) {
10124:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
10125:                     }
10126:                     $depth ++; 
10127:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
10128:                     $depth --;
10129:                 }
10130:             }
10131:         }
10132:     }
10133:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
10134:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
10135:         if ($putresult eq 'ok') {
10136:             my %title = (
10137:                          togglecats     => 'Show/Hide a course in catalog',
10138:                          categorize     => 'Assign a category to a course',
10139:                          togglecatscomm => 'Show/Hide a community in catalog',
10140:                          categorizecomm => 'Assign a category to a community',
10141:                         );
10142:             my %level = (
10143:                          dom  => 'set in Domain ("Modify Course/Community")',
10144:                          crs  => 'set in Course ("Course Configuration")',
10145:                          comm => 'set in Community ("Community Configuration")',
10146:                          none     => 'No catalog',
10147:                          std      => 'Standard catalog',
10148:                          domonly  => 'Domain-only catalog',
10149:                          codesrch => 'Code search form',
10150:                         );
10151:             $resulttext = &mt('Changes made:').'<ul>';
10152:             if ($changes{'togglecats'}) {
10153:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
10154:             }
10155:             if ($changes{'categorize'}) {
10156:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
10157:             }
10158:             if ($changes{'togglecatscomm'}) {
10159:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
10160:             }
10161:             if ($changes{'categorizecomm'}) {
10162:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
10163:             }
10164:             if ($changes{'unauth'}) {
10165:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
10166:             }
10167:             if ($changes{'auth'}) {
10168:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
10169:             }
10170:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
10171:                 my $cathash;
10172:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
10173:                     $cathash = $domconfig{'coursecategories'}{'cats'};
10174:                 } else {
10175:                     $cathash = {};
10176:                 } 
10177:                 my (@cats,@trails,%allitems);
10178:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
10179:                 if (keys(%deletions) > 0) {
10180:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
10181:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
10182:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
10183:                     }
10184:                     $resulttext .= '</ul></li>';
10185:                 }
10186:                 if (keys(%reorderings) > 0) {
10187:                     my %sort_by_trail;
10188:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
10189:                     foreach my $key (keys(%reorderings)) {
10190:                         if ($allitems{$key} ne '') {
10191:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
10192:                         }
10193:                     }
10194:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
10195:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
10196:                     }
10197:                     $resulttext .= '</ul></li>';
10198:                 }
10199:                 if (keys(%adds) > 0) {
10200:                     my %sort_by_trail;
10201:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
10202:                     foreach my $key (keys(%adds)) {
10203:                         if ($allitems{$key} ne '') {
10204:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
10205:                         }
10206:                     }
10207:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
10208:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
10209:                     }
10210:                     $resulttext .= '</ul></li>';
10211:                 }
10212:             }
10213:             $resulttext .= '</ul>';
10214:             if ($changes{'unauth'} || $changes{'auth'}) {
10215:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
10216:                 if ($changes{'auth'}) {
10217:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
10218:                 }
10219:                 if ($changes{'unauth'}) {
10220:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
10221:                 }
10222:                 my $cachetime = 24*60*60;
10223:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10224:                 if (ref($lastactref) eq 'HASH') {
10225:                     $lastactref->{'domdefaults'} = 1;
10226:                 }
10227:             }
10228:         } else {
10229:             $resulttext = '<span class="LC_error">'.
10230:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10231:         }
10232:     } else {
10233:         $resulttext = &mt('No changes made to course and community categories');
10234:     }
10235:     return $resulttext;
10236: }
10237: 
10238: sub modify_serverstatuses {
10239:     my ($dom,%domconfig) = @_;
10240:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
10241:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
10242:         %currserverstatus = %{$domconfig{'serverstatuses'}};
10243:     }
10244:     my @pages = &serverstatus_pages();
10245:     foreach my $type (@pages) {
10246:         $newserverstatus{$type}{'namedusers'} = '';
10247:         $newserverstatus{$type}{'machines'} = '';
10248:         if (defined($env{'form.'.$type.'_namedusers'})) {
10249:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
10250:             my @okusers;
10251:             foreach my $user (@users) {
10252:                 my ($uname,$udom) = split(/:/,$user);
10253:                 if (($udom =~ /^$match_domain$/) &&   
10254:                     (&Apache::lonnet::domain($udom)) &&
10255:                     ($uname =~ /^$match_username$/)) {
10256:                     if (!grep(/^\Q$user\E/,@okusers)) {
10257:                         push(@okusers,$user);
10258:                     }
10259:                 }
10260:             }
10261:             if (@okusers > 0) {
10262:                  @okusers = sort(@okusers);
10263:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
10264:             }
10265:         }
10266:         if (defined($env{'form.'.$type.'_machines'})) {
10267:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
10268:             my @okmachines;
10269:             foreach my $ip (@machines) {
10270:                 my @parts = split(/\./,$ip);
10271:                 next if (@parts < 4);
10272:                 my $badip = 0;
10273:                 for (my $i=0; $i<4; $i++) {
10274:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
10275:                         $badip = 1;
10276:                         last;
10277:                     }
10278:                 }
10279:                 if (!$badip) {
10280:                     push(@okmachines,$ip);     
10281:                 }
10282:             }
10283:             @okmachines = sort(@okmachines);
10284:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
10285:         }
10286:     }
10287:     my %serverstatushash =  (
10288:                                 serverstatuses => \%newserverstatus,
10289:                             );
10290:     foreach my $type (@pages) {
10291:         foreach my $setting ('namedusers','machines') {
10292:             my (@current,@new);
10293:             if (ref($currserverstatus{$type}) eq 'HASH') {
10294:                 if ($currserverstatus{$type}{$setting} ne '') { 
10295:                     @current = split(/,/,$currserverstatus{$type}{$setting});
10296:                 }
10297:             }
10298:             if ($newserverstatus{$type}{$setting} ne '') {
10299:                 @new = split(/,/,$newserverstatus{$type}{$setting});
10300:             }
10301:             if (@current > 0) {
10302:                 if (@new > 0) {
10303:                     foreach my $item (@current) {
10304:                         if (!grep(/^\Q$item\E$/,@new)) {
10305:                             $changes{$type}{$setting} = 1;
10306:                             last;
10307:                         }
10308:                     }
10309:                     foreach my $item (@new) {
10310:                         if (!grep(/^\Q$item\E$/,@current)) {
10311:                             $changes{$type}{$setting} = 1;
10312:                             last;
10313:                         }
10314:                     }
10315:                 } else {
10316:                     $changes{$type}{$setting} = 1;
10317:                 }
10318:             } elsif (@new > 0) {
10319:                 $changes{$type}{$setting} = 1;
10320:             }
10321:         }
10322:     }
10323:     if (keys(%changes) > 0) {
10324:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
10325:         my $putresult = &Apache::lonnet::put_dom('configuration',
10326:                                                  \%serverstatushash,$dom);
10327:         if ($putresult eq 'ok') {
10328:             $resulttext .= &mt('Changes made:').'<ul>';
10329:             foreach my $type (@pages) {
10330:                 if (ref($changes{$type}) eq 'HASH') {
10331:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
10332:                     if ($changes{$type}{'namedusers'}) {
10333:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
10334:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
10335:                         } else {
10336:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
10337:                         }
10338:                     }
10339:                     if ($changes{$type}{'machines'}) {
10340:                         if ($newserverstatus{$type}{'machines'} eq '') {
10341:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
10342:                         } else {
10343:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
10344:                         }
10345: 
10346:                     }
10347:                     $resulttext .= '</ul></li>';
10348:                 }
10349:             }
10350:             $resulttext .= '</ul>';
10351:         } else {
10352:             $resulttext = '<span class="LC_error">'.
10353:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
10354: 
10355:         }
10356:     } else {
10357:         $resulttext = &mt('No changes made to access to server status pages');
10358:     }
10359:     return $resulttext;
10360: }
10361: 
10362: sub modify_helpsettings {
10363:     my ($r,$dom,$confname,%domconfig) = @_;
10364:     my ($resulttext,$errors,%changes,%helphash);
10365:     my %defaultchecked = ('submitbugs' => 'on');
10366:     my @offon = ('off','on');
10367:     my @toggles = ('submitbugs');
10368:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
10369:         foreach my $item (@toggles) {
10370:             if ($defaultchecked{$item} eq 'on') { 
10371:                 if ($domconfig{'helpsettings'}{$item} eq '') {
10372:                     if ($env{'form.'.$item} eq '0') {
10373:                         $changes{$item} = 1;
10374:                     }
10375:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
10376:                     $changes{$item} = 1;
10377:                 }
10378:             } elsif ($defaultchecked{$item} eq 'off') {
10379:                 if ($domconfig{'helpsettings'}{$item} eq '') {
10380:                     if ($env{'form.'.$item} eq '1') {
10381:                         $changes{$item} = 1;
10382:                     }
10383:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
10384:                     $changes{$item} = 1;
10385:                 }
10386:             }
10387:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
10388:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
10389:             }
10390:         }
10391:     }
10392:     my $putresult;
10393:     if (keys(%changes) > 0) {
10394:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
10395:         if ($putresult eq 'ok') {
10396:             $resulttext = &mt('Changes made:').'<ul>';
10397:             foreach my $item (sort(keys(%changes))) {
10398:                 if ($item eq 'submitbugs') {
10399:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
10400:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
10401:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
10402:                 }
10403:             }
10404:             $resulttext .= '</ul>';
10405:         } else {
10406:             $resulttext = &mt('No changes made to help settings');
10407:             $errors .= '<li><span class="LC_error">'.
10408:                        &mt('An error occurred storing the settings: [_1]',
10409:                            $putresult).'</span></li>';
10410:         }
10411:     }
10412:     if ($errors) {
10413:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
10414:                        $errors.'</ul>';
10415:     }
10416:     return $resulttext;
10417: }
10418: 
10419: sub modify_coursedefaults {
10420:     my ($dom,$lastactref,%domconfig) = @_;
10421:     my ($resulttext,$errors,%changes,%defaultshash);
10422:     my %defaultchecked = (
10423:                            'uselcmath'       => 'on',
10424:                            'usejsme'         => 'on'
10425:                          );
10426:     my @toggles = ('uselcmath','usejsme');
10427:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
10428:                    'uploadquota_community','uploadquota_textbook','mysqltables_official',
10429:                    'mysqltables_unofficial','mysqltables_community','mysqltables_textbook');
10430:     my @types = ('official','unofficial','community','textbook');
10431:     my %staticdefaults = (
10432:                            anonsurvey_threshold => 10,
10433:                            uploadquota          => 500,
10434:                            postsubmit           => 60,
10435:                            mysqltables          => 172800,
10436:                          );
10437: 
10438:     $defaultshash{'coursedefaults'} = {};
10439: 
10440:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
10441:         if ($domconfig{'coursedefaults'} eq '') {
10442:             $domconfig{'coursedefaults'} = {};
10443:         }
10444:     }
10445: 
10446:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
10447:         foreach my $item (@toggles) {
10448:             if ($defaultchecked{$item} eq 'on') {
10449:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
10450:                     ($env{'form.'.$item} eq '0')) {
10451:                     $changes{$item} = 1;
10452:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
10453:                     $changes{$item} = 1;
10454:                 }
10455:             } elsif ($defaultchecked{$item} eq 'off') {
10456:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
10457:                     ($env{'form.'.$item} eq '1')) {
10458:                     $changes{$item} = 1;
10459:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
10460:                     $changes{$item} = 1;
10461:                 }
10462:             }
10463:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
10464:         }
10465:         foreach my $item (@numbers) {
10466:             my ($currdef,$newdef);
10467:             $newdef = $env{'form.'.$item};
10468:             if ($item eq 'anonsurvey_threshold') {
10469:                 $currdef = $domconfig{'coursedefaults'}{$item};
10470:                 $newdef =~ s/\D//g;
10471:                 if ($newdef eq '' || $newdef < 1) {
10472:                     $newdef = 1;
10473:                 }
10474:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
10475:             } else {
10476:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
10477:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
10478:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
10479:                 }
10480:                 $newdef =~ s/[^\w.\-]//g;
10481:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
10482:             }
10483:             if ($currdef ne $newdef) {
10484:                 my $staticdef;
10485:                 if ($item eq 'anonsurvey_threshold') {
10486:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
10487:                         $changes{$item} = 1;
10488:                     }
10489:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
10490:                     my $setting = $1;
10491:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
10492:                         $changes{$setting} = 1;
10493:                     }
10494:                 }
10495:             }
10496:         }
10497:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
10498:         my @currclonecode;
10499:         if (ref($currclone) eq 'HASH') {
10500:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
10501:                 @currclonecode = @{$currclone->{'instcode'}};
10502:             }
10503:         }
10504:         my $newclone;
10505:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
10506:             $newclone = $env{'form.canclone'};
10507:         }
10508:         if ($newclone eq 'instcode') {
10509:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
10510:             my (%codedefaults,@code_order,@clonecode);
10511:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
10512:                                                     \@code_order);
10513:             foreach my $item (@code_order) {
10514:                 if (grep(/^\Q$item\E$/,@newcodes)) {
10515:                     push(@clonecode,$item);
10516:                 }
10517:             }
10518:             if (@clonecode) {
10519:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
10520:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
10521:                 if (@diffs) {
10522:                     $changes{'canclone'} = 1;
10523:                 }
10524:             } else {
10525:                 $newclone eq '';
10526:             }
10527:         } elsif ($newclone ne '') {
10528:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
10529:         }
10530:         if ($newclone ne $currclone) {
10531:             $changes{'canclone'} = 1;
10532:         }
10533:         my %credits;
10534:         foreach my $type (@types) {
10535:             unless ($type eq 'community') {
10536:                 $credits{$type} = $env{'form.'.$type.'_credits'};
10537:                 $credits{$type} =~ s/[^\d.]+//g;
10538:             }
10539:         }
10540:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
10541:             ($env{'form.coursecredits'} eq '1')) {
10542:             $changes{'coursecredits'} = 1;
10543:             foreach my $type (keys(%credits)) {
10544:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
10545:             }
10546:         } else {
10547:             if ($env{'form.coursecredits'} eq '1') {
10548:                 foreach my $type (@types) {
10549:                     unless ($type eq 'community') {
10550:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
10551:                             $changes{'coursecredits'} = 1;
10552:                         }
10553:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
10554:                     }
10555:                 }
10556:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10557:                 foreach my $type (@types) {
10558:                     unless ($type eq 'community') {
10559:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
10560:                             $changes{'coursecredits'} = 1;
10561:                             last;
10562:                         }
10563:                     }
10564:                 }
10565:             }
10566:         }
10567:         if ($env{'form.postsubmit'} eq '1') {
10568:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
10569:             my %currtimeout;
10570:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
10571:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
10572:                     $changes{'postsubmit'} = 1;
10573:                 }
10574:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
10575:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
10576:                 }
10577:             } else {
10578:                 $changes{'postsubmit'} = 1;
10579:             }
10580:             foreach my $type (@types) {
10581:                 my $timeout = $env{'form.'.$type.'_timeout'};
10582:                 $timeout =~ s/\D//g;
10583:                 if ($timeout == $staticdefaults{'postsubmit'}) {
10584:                     $timeout = '';
10585:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
10586:                     $timeout = '0';
10587:                 }
10588:                 unless ($timeout eq '') {
10589:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
10590:                 }
10591:                 if (exists($currtimeout{$type})) {
10592:                     if ($timeout ne $currtimeout{$type}) {
10593:                         $changes{'postsubmit'} = 1;
10594:                     }
10595:                 } elsif ($timeout ne '') {
10596:                     $changes{'postsubmit'} = 1;
10597:                 }
10598:             }
10599:         } else {
10600:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
10601:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
10602:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
10603:                     $changes{'postsubmit'} = 1;
10604:                 }
10605:             } else {
10606:                 $changes{'postsubmit'} = 1;
10607:             }
10608:         }
10609:     }
10610:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
10611:                                              $dom);
10612:     if ($putresult eq 'ok') {
10613:         if (keys(%changes) > 0) {
10614:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10615:             if (($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
10616:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
10617:                 ($changes{'canclone'}) || ($changes{'mysqltables'})) {
10618:                 foreach my $item ('uselcmath','usejsme') {
10619:                     if ($changes{$item}) {
10620:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
10621:                     }
10622:                 }
10623:                 if ($changes{'coursecredits'}) {
10624:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10625:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
10626:                             $domdefaults{$type.'credits'} =
10627:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
10628:                         }
10629:                     }
10630:                 }
10631:                 if ($changes{'postsubmit'}) {
10632:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
10633:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
10634:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
10635:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
10636:                                 $domdefaults{$type.'postsubtimeout'} =
10637:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
10638:                             }
10639:                         }
10640:                     }
10641:                 }
10642:                 if ($changes{'uploadquota'}) {
10643:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10644:                         foreach my $type (@types) {
10645:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
10646:                         }
10647:                     }
10648:                 }
10649:                 if ($changes{'canclone'}) {
10650:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
10651:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
10652:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
10653:                             if (@clonecodes) {
10654:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
10655:                             }
10656:                         }
10657:                     } else {
10658:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
10659:                     }
10660:                 }
10661:                 my $cachetime = 24*60*60;
10662:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10663:                 if (ref($lastactref) eq 'HASH') {
10664:                     $lastactref->{'domdefaults'} = 1;
10665:                 }
10666:             }
10667:             $resulttext = &mt('Changes made:').'<ul>';
10668:             foreach my $item (sort(keys(%changes))) {
10669:                 if ($item eq 'uselcmath') {
10670:                     if ($env{'form.'.$item} eq '1') {
10671:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
10672:                     } else {
10673:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
10674:                     }
10675:                 } elsif ($item eq 'usejsme') {
10676:                     if ($env{'form.'.$item} eq '1') {
10677:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
10678:                     } else {
10679:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
10680:                     }
10681:                 } elsif ($item eq 'anonsurvey_threshold') {
10682:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
10683:                 } elsif ($item eq 'uploadquota') {
10684:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10685:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
10686:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
10687:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
10688:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
10689: 
10690:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
10691:                                        '</ul>'.
10692:                                        '</li>';
10693:                     } else {
10694:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
10695:                     }
10696:                 } elsif ($item eq 'mysqltables') {
10697:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
10698:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
10699:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
10700:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
10701:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
10702:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
10703:                                        '</ul>'.
10704:                                        '</li>';
10705:                     } else {
10706:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
10707:                     }
10708:                 } elsif ($item eq 'postsubmit') {
10709:                     if ($domdefaults{'postsubmit'} eq 'off') {
10710:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
10711:                     } else {
10712:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
10713:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
10714:                             $resulttext .= &mt('durations:').'<ul>';
10715:                             foreach my $type (@types) {
10716:                                 $resulttext .= '<li>';
10717:                                 my $timeout;
10718:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
10719:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
10720:                                 }
10721:                                 my $display;
10722:                                 if ($timeout eq '0') {
10723:                                     $display = &mt('unlimited');
10724:                                 } elsif ($timeout eq '') {
10725:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
10726:                                 } else {
10727:                                     $display = &mt('[quant,_1,second]',$timeout);
10728:                                 }
10729:                                 if ($type eq 'community') {
10730:                                     $resulttext .= &mt('Communities');
10731:                                 } elsif ($type eq 'official') {
10732:                                     $resulttext .= &mt('Official courses');
10733:                                 } elsif ($type eq 'unofficial') {
10734:                                     $resulttext .= &mt('Unofficial courses');
10735:                                 } elsif ($type eq 'textbook') {
10736:                                     $resulttext .= &mt('Textbook courses');
10737:                                 }
10738:                                 $resulttext .= ' -- '.$display.'</li>';
10739:                             }
10740:                             $resulttext .= '</ul>';
10741:                         }
10742:                         $resulttext .= '</li>';
10743:                     }
10744:                 } elsif ($item eq 'coursecredits') {
10745:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10746:                         if (($domdefaults{'officialcredits'} eq '') &&
10747:                             ($domdefaults{'unofficialcredits'} eq '') &&
10748:                             ($domdefaults{'textbookcredits'} eq '')) {
10749:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10750:                         } else {
10751:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
10752:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
10753:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
10754:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
10755:                                            '</ul>'.
10756:                                            '</li>';
10757:                         }
10758:                     } else {
10759:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10760:                     }
10761:                 } elsif ($item eq 'canclone') {
10762:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
10763:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
10764:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
10765:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
10766:                         }
10767:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
10768:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
10769:                     } else {
10770:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
10771:                     }
10772:                 }
10773:             }
10774:             $resulttext .= '</ul>';
10775:         } else {
10776:             $resulttext = &mt('No changes made to course defaults');
10777:         }
10778:     } else {
10779:         $resulttext = '<span class="LC_error">'.
10780:             &mt('An error occurred: [_1]',$putresult).'</span>';
10781:     }
10782:     return $resulttext;
10783: }
10784: 
10785: sub modify_selfenrollment {
10786:     my ($dom,$lastactref,%domconfig) = @_;
10787:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
10788:     my @types = ('official','unofficial','community','textbook');
10789:     my %titles = &tool_titles();
10790:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
10791:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
10792:     $ordered{'default'} = ['types','registered','approval','limit'];
10793: 
10794:     my (%roles,%shown,%toplevel);
10795:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
10796: 
10797:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
10798:         if ($domconfig{'selfenrollment'} eq '') {
10799:             $domconfig{'selfenrollment'} = {};
10800:         }
10801:     }
10802:     %toplevel = (
10803:                   admin      => 'Configuration Rights',
10804:                   default    => 'Default settings',
10805:                   validation => 'Validation of self-enrollment requests',
10806:                 );
10807:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
10808: 
10809:     if (ref($ordered{'admin'}) eq 'ARRAY') {
10810:         foreach my $item (@{$ordered{'admin'}}) {
10811:             foreach my $type (@types) {
10812:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
10813:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
10814:                 } else {
10815:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
10816:                 }
10817:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
10818:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
10819:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
10820:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
10821:                             push(@{$changes{'admin'}{$type}},$item);
10822:                         }
10823:                     } else {
10824:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
10825:                             push(@{$changes{'admin'}{$type}},$item);
10826:                         }
10827:                     }
10828:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
10829:                     push(@{$changes{'admin'}{$type}},$item);
10830:                 }
10831:             }
10832:         }
10833:     }
10834: 
10835:     foreach my $item (@{$ordered{'default'}}) {
10836:         foreach my $type (@types) {
10837:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
10838:             if ($item eq 'types') {
10839:                 unless (($value eq 'all') || ($value eq 'dom')) {
10840:                     $value = '';
10841:                 }
10842:             } elsif ($item eq 'registered') {
10843:                 unless ($value eq '1') {
10844:                     $value = 0;
10845:                 }
10846:             } elsif ($item eq 'approval') {
10847:                 unless ($value =~ /^[012]$/) {
10848:                     $value = 0;
10849:                 }
10850:             } else {
10851:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10852:                     $value = 'none';
10853:                 }
10854:             }
10855:             $selfenrollhash{'default'}{$type}{$item} = $value;
10856:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
10857:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10858:                     if ($selfenrollhash{'default'}{$type}{$item} ne
10859:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
10860:                          push(@{$changes{'default'}{$type}},$item);
10861:                     }
10862:                 } else {
10863:                     push(@{$changes{'default'}{$type}},$item);
10864:                 }
10865:             } else {
10866:                 push(@{$changes{'default'}{$type}},$item);
10867:             }
10868:             if ($item eq 'limit') {
10869:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10870:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
10871:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
10872:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
10873:                     }
10874:                 } else {
10875:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
10876:                 }
10877:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10878:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
10879:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
10880:                          push(@{$changes{'default'}{$type}},'cap');
10881:                     }
10882:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
10883:                     push(@{$changes{'default'}{$type}},'cap');
10884:                 }
10885:             }
10886:         }
10887:     }
10888: 
10889:     foreach my $item (@{$itemsref}) {
10890:         if ($item eq 'fields') {
10891:             my @changed;
10892:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
10893:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
10894:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
10895:             }
10896:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10897:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
10898:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
10899:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
10900:                 } else {
10901:                     @changed = @{$selfenrollhash{'validation'}{$item}};
10902:                 }
10903:             } else {
10904:                 @changed = @{$selfenrollhash{'validation'}{$item}};
10905:             }
10906:             if (@changed) {
10907:                 if ($selfenrollhash{'validation'}{$item}) { 
10908:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
10909:                 } else {
10910:                     $changes{'validation'}{$item} = &mt('None');
10911:                 }
10912:             }
10913:         } else {
10914:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
10915:             if ($item eq 'markup') {
10916:                if ($env{'form.selfenroll_validation_'.$item}) {
10917:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
10918:                }
10919:             }
10920:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10921:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
10922:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
10923:                 }
10924:             }
10925:         }
10926:     }
10927: 
10928:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
10929:                                              $dom);
10930:     if ($putresult eq 'ok') {
10931:         if (keys(%changes) > 0) {
10932:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10933:             $resulttext = &mt('Changes made:').'<ul>';
10934:             foreach my $key ('admin','default','validation') {
10935:                 if (ref($changes{$key}) eq 'HASH') {
10936:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
10937:                     if ($key eq 'validation') {
10938:                         foreach my $item (@{$itemsref}) {
10939:                             if (exists($changes{$key}{$item})) {
10940:                                 if ($item eq 'markup') {
10941:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10942:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
10943:                                 } else {  
10944:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10945:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
10946:                                 }
10947:                             }
10948:                         }
10949:                     } else {
10950:                         foreach my $type (@types) {
10951:                             if ($type eq 'community') {
10952:                                 $roles{'1'} = &mt('Community personnel');
10953:                             } else {
10954:                                 $roles{'1'} = &mt('Course personnel');
10955:                             }
10956:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
10957:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
10958:                                     if ($key eq 'admin') {
10959:                                         my @mgrdc = ();
10960:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10961:                                             foreach my $item (@{$ordered{'admin'}}) {
10962:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
10963:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
10964:                                                         push(@mgrdc,$item);
10965:                                                     }
10966:                                                 }
10967:                                             }
10968:                                             if (@mgrdc) {
10969:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
10970:                                             } else {
10971:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
10972:                                             }
10973:                                         }
10974:                                     } else {
10975:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10976:                                             foreach my $item (@{$ordered{$key}}) {
10977:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10978:                                                     $domdefaults{$type.'selfenroll'.$item} =
10979:                                                         $selfenrollhash{$key}{$type}{$item};
10980:                                                 }
10981:                                             }
10982:                                         }
10983:                                     }
10984:                                 }
10985:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
10986:                                 foreach my $item (@{$ordered{$key}}) {
10987:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10988:                                         $resulttext .= '<li>';
10989:                                         if ($key eq 'admin') {
10990:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
10991:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
10992:                                         } else {
10993:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
10994:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
10995:                                         }
10996:                                         $resulttext .= '</li>';
10997:                                     }
10998:                                 }
10999:                                 $resulttext .= '</ul></li>';
11000:                             }
11001:                         }
11002:                         $resulttext .= '</ul></li>'; 
11003:                     }
11004:                 }
11005:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
11006:                     my $cachetime = 24*60*60;
11007:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11008:                     if (ref($lastactref) eq 'HASH') {
11009:                         $lastactref->{'domdefaults'} = 1;
11010:                     }
11011:                 }
11012:             }
11013:             $resulttext .= '</ul>';
11014:         } else {
11015:             $resulttext = &mt('No changes made to self-enrollment settings');
11016:         }
11017:     } else {
11018:         $resulttext = '<span class="LC_error">'.
11019:             &mt('An error occurred: [_1]',$putresult).'</span>';
11020:     }
11021:     return $resulttext;
11022: }
11023: 
11024: sub modify_usersessions {
11025:     my ($dom,$lastactref,%domconfig) = @_;
11026:     my @hostingtypes = ('version','excludedomain','includedomain');
11027:     my @offloadtypes = ('primary','default');
11028:     my %types = (
11029:                   remote => \@hostingtypes,
11030:                   hosted => \@hostingtypes,
11031:                   spares => \@offloadtypes,
11032:                 );
11033:     my @prefixes = ('remote','hosted','spares');
11034:     my @lcversions = &Apache::lonnet::all_loncaparevs();
11035:     my (%by_ip,%by_location,@intdoms);
11036:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
11037:     my @locations = sort(keys(%by_location));
11038:     my (%defaultshash,%changes);
11039:     foreach my $prefix (@prefixes) {
11040:         $defaultshash{'usersessions'}{$prefix} = {};
11041:     }
11042:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11043:     my $resulttext;
11044:     my %iphost = &Apache::lonnet::get_iphost();
11045:     foreach my $prefix (@prefixes) {
11046:         next if ($prefix eq 'spares');
11047:         foreach my $type (@{$types{$prefix}}) {
11048:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
11049:             if ($type eq 'version') {
11050:                 my $value = $env{'form.'.$prefix.'_'.$type};
11051:                 my $okvalue;
11052:                 if ($value ne '') {
11053:                     if (grep(/^\Q$value\E$/,@lcversions)) {
11054:                         $okvalue = $value;
11055:                     }
11056:                 }
11057:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
11058:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
11059:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
11060:                             if ($inuse == 0) {
11061:                                 $changes{$prefix}{$type} = 1;
11062:                             } else {
11063:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
11064:                                     $changes{$prefix}{$type} = 1;
11065:                                 }
11066:                                 if ($okvalue ne '') {
11067:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
11068:                                 } 
11069:                             }
11070:                         } else {
11071:                             if (($inuse == 1) && ($okvalue ne '')) {
11072:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
11073:                                 $changes{$prefix}{$type} = 1;
11074:                             }
11075:                         }
11076:                     } else {
11077:                         if (($inuse == 1) && ($okvalue ne '')) {
11078:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
11079:                             $changes{$prefix}{$type} = 1;
11080:                         }
11081:                     }
11082:                 } else {
11083:                     if (($inuse == 1) && ($okvalue ne '')) {
11084:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
11085:                         $changes{$prefix}{$type} = 1;
11086:                     }
11087:                 }
11088:             } else {
11089:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
11090:                 my @okvals;
11091:                 foreach my $val (@vals) {
11092:                     if ($val =~ /:/) {
11093:                         my @items = split(/:/,$val);
11094:                         foreach my $item (@items) {
11095:                             if (ref($by_location{$item}) eq 'ARRAY') {
11096:                                 push(@okvals,$item);
11097:                             }
11098:                         }
11099:                     } else {
11100:                         if (ref($by_location{$val}) eq 'ARRAY') {
11101:                             push(@okvals,$val);
11102:                         }
11103:                     }
11104:                 }
11105:                 @okvals = sort(@okvals);
11106:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
11107:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
11108:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
11109:                             if ($inuse == 0) {
11110:                                 $changes{$prefix}{$type} = 1; 
11111:                             } else {
11112:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
11113:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
11114:                                 if (@changed > 0) {
11115:                                     $changes{$prefix}{$type} = 1;
11116:                                 }
11117:                             }
11118:                         } else {
11119:                             if ($inuse == 1) {
11120:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
11121:                                 $changes{$prefix}{$type} = 1;
11122:                             }
11123:                         } 
11124:                     } else {
11125:                         if ($inuse == 1) {
11126:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
11127:                             $changes{$prefix}{$type} = 1;
11128:                         }
11129:                     }
11130:                 } else {
11131:                     if ($inuse == 1) {
11132:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
11133:                         $changes{$prefix}{$type} = 1;
11134:                     }
11135:                 }
11136:             }
11137:         }
11138:     }
11139: 
11140:     my @alldoms = &Apache::lonnet::all_domains();
11141:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
11142:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
11143:     my $savespares;
11144: 
11145:     foreach my $lonhost (sort(keys(%servers))) {
11146:         my $serverhomeID =
11147:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
11148:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
11149:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
11150:         my %spareschg;
11151:         foreach my $type (@{$types{'spares'}}) {
11152:             my @okspares;
11153:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
11154:             foreach my $server (@checked) {
11155:                 if (&Apache::lonnet::hostname($server) ne '') {
11156:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
11157:                         unless (grep(/^\Q$server\E$/,@okspares)) {
11158:                             push(@okspares,$server);
11159:                         }
11160:                     }
11161:                 }
11162:             }
11163:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
11164:             my $newspare;
11165:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
11166:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
11167:                     $newspare = $new;
11168:                 }
11169:             }
11170:             my @spares;
11171:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
11172:                 @spares = sort(@okspares,$newspare);
11173:             } else {
11174:                 @spares = sort(@okspares);
11175:             }
11176:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
11177:             if (ref($spareid{$lonhost}) eq 'HASH') {
11178:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
11179:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
11180:                     if (@diffs > 0) {
11181:                         $spareschg{$type} = 1;
11182:                     }
11183:                 }
11184:             }
11185:         }
11186:         if (keys(%spareschg) > 0) {
11187:             $changes{'spares'}{$lonhost} = \%spareschg;
11188:         }
11189:     }
11190:     $defaultshash{'usersessions'}{'offloadnow'} = {};
11191:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
11192:     my @okoffload;
11193:     if (@offloadnow) {
11194:         foreach my $server (@offloadnow) {
11195:             if (&Apache::lonnet::hostname($server) ne '') {
11196:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
11197:                     push(@okoffload,$server);
11198:                 }
11199:             }
11200:         }
11201:         if (@okoffload) {
11202:             foreach my $lonhost (@okoffload) {
11203:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
11204:             }
11205:         }
11206:     }
11207:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
11208:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
11209:             if (ref($changes{'spares'}) eq 'HASH') {
11210:                 if (keys(%{$changes{'spares'}}) > 0) {
11211:                     $savespares = 1;
11212:                 }
11213:             }
11214:         } else {
11215:             $savespares = 1;
11216:         }
11217:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
11218:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
11219:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
11220:                     $changes{'offloadnow'} = 1;
11221:                     last;
11222:                 }
11223:             }
11224:             unless ($changes{'offloadnow'}) {
11225:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
11226:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
11227:                         $changes{'offloadnow'} = 1;
11228:                         last;
11229:                     }
11230:                 }
11231:             }
11232:         } elsif (@okoffload) {
11233:             $changes{'offloadnow'} = 1;
11234:         }
11235:     } elsif (@okoffload) {
11236:         $changes{'offloadnow'} = 1;
11237:     }
11238:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
11239:     if ((keys(%changes) > 0) || ($savespares)) {
11240:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
11241:                                                  $dom);
11242:         if ($putresult eq 'ok') {
11243:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
11244:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
11245:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
11246:                 }
11247:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
11248:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
11249:                 }
11250:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
11251:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
11252:                 }
11253:             }
11254:             my $cachetime = 24*60*60;
11255:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11256:             if (ref($lastactref) eq 'HASH') {
11257:                 $lastactref->{'domdefaults'} = 1;
11258:             }
11259:             if (keys(%changes) > 0) {
11260:                 my %lt = &usersession_titles();
11261:                 $resulttext = &mt('Changes made:').'<ul>';
11262:                 foreach my $prefix (@prefixes) {
11263:                     if (ref($changes{$prefix}) eq 'HASH') {
11264:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
11265:                         if ($prefix eq 'spares') {
11266:                             if (ref($changes{$prefix}) eq 'HASH') {
11267:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
11268:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
11269:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
11270:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
11271:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
11272:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
11273:                                         foreach my $type (@{$types{$prefix}}) {
11274:                                             if ($changes{$prefix}{$lonhost}{$type}) {
11275:                                                 my $offloadto = &mt('None');
11276:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
11277:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
11278:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
11279:                                                     }
11280:                                                 }
11281:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
11282:                                             }
11283:                                         }
11284:                                     }
11285:                                     $resulttext .= '</li>';
11286:                                 }
11287:                             }
11288:                         } else {
11289:                             foreach my $type (@{$types{$prefix}}) {
11290:                                 if (defined($changes{$prefix}{$type})) {
11291:                                     my $newvalue;
11292:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
11293:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
11294:                                             if ($type eq 'version') {
11295:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
11296:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
11297:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
11298:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
11299:                                                 }
11300:                                             }
11301:                                         }
11302:                                     }
11303:                                     if ($newvalue eq '') {
11304:                                         if ($type eq 'version') {
11305:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
11306:                                         } else {
11307:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
11308:                                         }
11309:                                     } else {
11310:                                         if ($type eq 'version') {
11311:                                             $newvalue .= ' '.&mt('(or later)'); 
11312:                                         }
11313:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
11314:                                     }
11315:                                 }
11316:                             }
11317:                         }
11318:                         $resulttext .= '</ul>';
11319:                     }
11320:                 }
11321:                 if ($changes{'offloadnow'}) {
11322:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
11323:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
11324:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
11325:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
11326:                                 $resulttext .= '<li>'.$lonhost.'</li>';
11327:                             }
11328:                             $resulttext .= '</ul>';
11329:                         } else {
11330:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
11331:                         }
11332:                     } else {
11333:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
11334:                     }
11335:                 }
11336:                 $resulttext .= '</ul>';
11337:             } else {
11338:                 $resulttext = $nochgmsg;
11339:             }
11340:         } else {
11341:             $resulttext = '<span class="LC_error">'.
11342:                           &mt('An error occurred: [_1]',$putresult).'</span>';
11343:         }
11344:     } else {
11345:         $resulttext = $nochgmsg;
11346:     }
11347:     return $resulttext;
11348: }
11349: 
11350: sub modify_loadbalancing {
11351:     my ($dom,%domconfig) = @_;
11352:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
11353:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
11354:     my ($othertitle,$usertypes,$types) =
11355:         &Apache::loncommon::sorted_inst_types($dom);
11356:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
11357:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
11358:     my @sparestypes = ('primary','default');
11359:     my %typetitles = &sparestype_titles();
11360:     my $resulttext;
11361:     my (%currbalancer,%currtargets,%currrules,%existing);
11362:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
11363:         %existing = %{$domconfig{'loadbalancing'}};
11364:     }
11365:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
11366:                               \%currtargets,\%currrules);
11367:     my ($saveloadbalancing,%defaultshash,%changes);
11368:     my ($alltypes,$othertypes,$titles) =
11369:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
11370:     my %ruletitles = &offloadtype_text();
11371:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
11372:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
11373:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
11374:         if ($balancer eq '') {
11375:             next;
11376:         }
11377:         if (!exists($servers{$balancer})) {
11378:             if (exists($currbalancer{$balancer})) {
11379:                 push(@{$changes{'delete'}},$balancer);
11380:             }
11381:             next;
11382:         }
11383:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
11384:             push(@{$changes{'delete'}},$balancer);
11385:             next;
11386:         }
11387:         if (!exists($currbalancer{$balancer})) {
11388:             push(@{$changes{'add'}},$balancer);
11389:         }
11390:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
11391:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
11392:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
11393:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
11394:             $saveloadbalancing = 1;
11395:         }
11396:         foreach my $sparetype (@sparestypes) {
11397:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
11398:             my @offloadto;
11399:             foreach my $target (@targets) {
11400:                 if (($servers{$target}) && ($target ne $balancer)) {
11401:                     if ($sparetype eq 'default') {
11402:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
11403:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
11404:                         }
11405:                     }
11406:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
11407:                         push(@offloadto,$target);
11408:                     }
11409:                 }
11410:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
11411:             }
11412:         }
11413:         if (ref($currtargets{$balancer}) eq 'HASH') {
11414:             foreach my $sparetype (@sparestypes) {
11415:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
11416:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
11417:                     if (@targetdiffs > 0) {
11418:                         $changes{'curr'}{$balancer}{'targets'} = 1;
11419:                     }
11420:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
11421:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
11422:                         $changes{'curr'}{$balancer}{'targets'} = 1;
11423:                     }
11424:                 }
11425:             }
11426:         } else {
11427:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
11428:                 foreach my $sparetype (@sparestypes) {
11429:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
11430:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
11431:                             $changes{'curr'}{$balancer}{'targets'} = 1;
11432:                         }
11433:                     }
11434:                 }
11435:             }
11436:         }
11437:         my $ishomedom;
11438:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
11439:             $ishomedom = 1;
11440:         }
11441:         if (ref($alltypes) eq 'ARRAY') {
11442:             foreach my $type (@{$alltypes}) {
11443:                 my $rule;
11444:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
11445:                          (!$ishomedom)) {
11446:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
11447:                 }
11448:                 if ($rule eq 'specific') {
11449:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
11450:                     if (exists($servers{$specifiedhost})) {
11451:                         $rule = $specifiedhost;
11452:                     }
11453:                 }
11454:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
11455:                 if (ref($currrules{$balancer}) eq 'HASH') {
11456:                     if ($rule ne $currrules{$balancer}{$type}) {
11457:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
11458:                     }
11459:                 } elsif ($rule ne '') {
11460:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
11461:                 }
11462:             }
11463:         }
11464:     }
11465:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
11466:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
11467:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
11468:             $defaultshash{'loadbalancing'} = {};
11469:         }
11470:         my $putresult = &Apache::lonnet::put_dom('configuration',
11471:                                                  \%defaultshash,$dom);
11472:         if ($putresult eq 'ok') {
11473:             if (keys(%changes) > 0) {
11474:                 my %toupdate;
11475:                 if (ref($changes{'delete'}) eq 'ARRAY') {
11476:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
11477:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
11478:                         $toupdate{$balancer} = 1;
11479:                     }
11480:                 }
11481:                 if (ref($changes{'add'}) eq 'ARRAY') {
11482:                     foreach my $balancer (sort(@{$changes{'add'}})) {
11483:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
11484:                         $toupdate{$balancer} = 1;
11485:                     }
11486:                 }
11487:                 if (ref($changes{'curr'}) eq 'HASH') {
11488:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
11489:                         $toupdate{$balancer} = 1;
11490:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
11491:                             if ($changes{'curr'}{$balancer}{'targets'}) {
11492:                                 my %offloadstr;
11493:                                 foreach my $sparetype (@sparestypes) {
11494:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
11495:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
11496:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
11497:                                         }
11498:                                     }
11499:                                 }
11500:                                 if (keys(%offloadstr) == 0) {
11501:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
11502:                                 } else {
11503:                                     my $showoffload;
11504:                                     foreach my $sparetype (@sparestypes) {
11505:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
11506:                                         if (defined($offloadstr{$sparetype})) {
11507:                                             $showoffload .= $offloadstr{$sparetype};
11508:                                         } else {
11509:                                             $showoffload .= &mt('None');
11510:                                         }
11511:                                         $showoffload .= ('&nbsp;'x3);
11512:                                     }
11513:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
11514:                                 }
11515:                             }
11516:                         }
11517:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
11518:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
11519:                                 foreach my $type (@{$alltypes}) {
11520:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
11521:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
11522:                                         my $balancetext;
11523:                                         if ($rule eq '') {
11524:                                             $balancetext =  $ruletitles{'default'};
11525:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
11526:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
11527:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
11528:                                                 foreach my $sparetype (@sparestypes) {
11529:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
11530:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
11531:                                                     }
11532:                                                 }
11533:                                                 foreach my $item (@{$alltypes}) {
11534:                                                     next if ($item =~  /^_LC_ipchange/);
11535:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
11536:                                                     if ($hasrule eq 'homeserver') {
11537:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
11538:                                                     } else {
11539:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
11540:                                                             if ($servers{$hasrule}) {
11541:                                                                 $toupdate{$hasrule} = 1;
11542:                                                             }
11543:                                                         }
11544:                                                     }
11545:                                                 }
11546:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
11547:                                                     $balancetext =  $ruletitles{$rule};
11548:                                                 } else {
11549:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
11550:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
11551:                                                     if ($receiver) {
11552:                                                         $toupdate{$receiver};
11553:                                                     }
11554:                                                 }
11555:                                             } else {
11556:                                                 $balancetext =  $ruletitles{$rule};
11557:                                             }
11558:                                         } else {
11559:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
11560:                                         }
11561:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
11562:                                     }
11563:                                 }
11564:                             }
11565:                         }
11566:                         if (keys(%toupdate)) {
11567:                             my %thismachine;
11568:                             my $updatedhere;
11569:                             my $cachetime = 60*60*24;
11570:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
11571:                             foreach my $lonhost (keys(%toupdate)) {
11572:                                 if ($thismachine{$lonhost}) {
11573:                                     unless ($updatedhere) {
11574:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
11575:                                                                       $defaultshash{'loadbalancing'},
11576:                                                                       $cachetime);
11577:                                         $updatedhere = 1;
11578:                                     }
11579:                                 } else {
11580:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
11581:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
11582:                                 }
11583:                             }
11584:                         }
11585:                     }
11586:                 }
11587:                 if ($resulttext ne '') {
11588:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
11589:                 } else {
11590:                     $resulttext = $nochgmsg;
11591:                 }
11592:             } else {
11593:                 $resulttext = $nochgmsg;
11594:             }
11595:         } else {
11596:             $resulttext = '<span class="LC_error">'.
11597:                           &mt('An error occurred: [_1]',$putresult).'</span>';
11598:         }
11599:     } else {
11600:         $resulttext = $nochgmsg;
11601:     }
11602:     return $resulttext;
11603: }
11604: 
11605: sub recurse_check {
11606:     my ($chkcats,$categories,$depth,$name) = @_;
11607:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
11608:         my $chg = 0;
11609:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
11610:             my $category = $chkcats->[$depth]{$name}[$j];
11611:             my $item;
11612:             if ($category eq '') {
11613:                 $chg ++;
11614:             } else {
11615:                 my $deeper = $depth + 1;
11616:                 $item = &escape($category).':'.&escape($name).':'.$depth;
11617:                 if ($chg) {
11618:                     $categories->{$item} -= $chg;
11619:                 }
11620:                 &recurse_check($chkcats,$categories,$deeper,$category);
11621:                 $deeper --;
11622:             }
11623:         }
11624:     }
11625:     return;
11626: }
11627: 
11628: sub recurse_cat_deletes {
11629:     my ($item,$coursecategories,$deletions) = @_;
11630:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
11631:     my $subdepth = $depth + 1;
11632:     if (ref($coursecategories) eq 'HASH') {
11633:         foreach my $subitem (keys(%{$coursecategories})) {
11634:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
11635:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
11636:                 delete($coursecategories->{$subitem});
11637:                 $deletions->{$subitem} = 1;
11638:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
11639:             }
11640:         }
11641:     }
11642:     return;
11643: }
11644: 
11645: sub get_active_dcs {
11646:     my ($dom) = @_;
11647:     my $now = time;
11648:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
11649:     my %domcoords;
11650:     my $numdcs = 0;
11651:     foreach my $server (keys(%dompersonnel)) {
11652:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
11653:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
11654:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
11655:         }
11656:     }
11657:     return %domcoords;
11658: }
11659: 
11660: sub active_dc_picker {
11661:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
11662:     my %domcoords = &get_active_dcs($dom);
11663:     my @domcoord = keys(%domcoords);
11664:     if (keys(%currhash)) {
11665:         foreach my $dc (keys(%currhash)) {
11666:             unless (exists($domcoords{$dc})) {
11667:                 push(@domcoord,$dc);
11668:             }
11669:         }
11670:     }
11671:     @domcoord = sort(@domcoord);
11672:     my $numdcs = scalar(@domcoord);
11673:     my $rows = 0;
11674:     my $table;
11675:     if ($numdcs > 1) {
11676:         $table = '<table>';
11677:         for (my $i=0; $i<@domcoord; $i++) {
11678:             my $rem = $i%($numinrow);
11679:             if ($rem == 0) {
11680:                 if ($i > 0) {
11681:                     $table .= '</tr>';
11682:                 }
11683:                 $table .= '<tr>';
11684:                 $rows ++;
11685:             }
11686:             my $check = '';
11687:             if ($inputtype eq 'radio') {
11688:                 if (keys(%currhash) == 0) {
11689:                     if (!$i) {
11690:                         $check = ' checked="checked"';
11691:                     }
11692:                 } elsif (exists($currhash{$domcoord[$i]})) {
11693:                     $check = ' checked="checked"';
11694:                 }
11695:             } else {
11696:                 if (exists($currhash{$domcoord[$i]})) {
11697:                     $check = ' checked="checked"';
11698:                 }
11699:             }
11700:             if ($i == @domcoord - 1) {
11701:                 my $colsleft = $numinrow - $rem;
11702:                 if ($colsleft > 1) {
11703:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
11704:                 } else {
11705:                     $table .= '<td class="LC_left_item">';
11706:                 }
11707:             } else {
11708:                 $table .= '<td class="LC_left_item">';
11709:             }
11710:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
11711:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
11712:             $table .= '<span class="LC_nobreak"><label>'.
11713:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
11714:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
11715:             if ($user ne $dcname.':'.$dcdom) {
11716:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
11717:             }
11718:             $table .= '</label></span></td>';
11719:         }
11720:         $table .= '</tr></table>';
11721:     } elsif ($numdcs == 1) {
11722:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
11723:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
11724:         if ($inputtype eq 'radio') {
11725:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
11726:             if ($user ne $dcname.':'.$dcdom) {
11727:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
11728:             }
11729:         } else {
11730:             my $check;
11731:             if (exists($currhash{$domcoord[0]})) {
11732:                 $check = ' checked="checked"';
11733:             }
11734:             $table = '<span class="LC_nobreak"><label>'.
11735:                      '<input type="checkbox" name="'.$name.'" '.
11736:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
11737:             if ($user ne $dcname.':'.$dcdom) {
11738:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
11739:             }
11740:             $table .= '</label></span>';
11741:             $rows ++;
11742:         }
11743:     }
11744:     return ($numdcs,$table,$rows);
11745: }
11746: 
11747: sub usersession_titles {
11748:     return &Apache::lonlocal::texthash(
11749:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
11750:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
11751:                spares => 'Servers offloaded to, when busy',
11752:                version => 'LON-CAPA version requirement',
11753:                excludedomain => 'Allow all, but exclude specific domains',
11754:                includedomain => 'Deny all, but include specific domains',
11755:                primary => 'Primary (checked first)',
11756:                default => 'Default',
11757:            );
11758: }
11759: 
11760: sub id_for_thisdom {
11761:     my (%servers) = @_;
11762:     my %altids;
11763:     foreach my $server (keys(%servers)) {
11764:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
11765:         if ($serverhome ne $server) {
11766:             $altids{$serverhome} = $server;
11767:         }
11768:     }
11769:     return %altids;
11770: }
11771: 
11772: sub count_servers {
11773:     my ($currbalancer,%servers) = @_;
11774:     my (@spares,$numspares);
11775:     foreach my $lonhost (sort(keys(%servers))) {
11776:         next if ($currbalancer eq $lonhost);
11777:         push(@spares,$lonhost);
11778:     }
11779:     if ($currbalancer) {
11780:         $numspares = scalar(@spares);
11781:     } else {
11782:         $numspares = scalar(@spares) - 1;
11783:     }
11784:     return ($numspares,@spares);
11785: }
11786: 
11787: sub lonbalance_targets_js {
11788:     my ($dom,$types,$servers,$settings) = @_;
11789:     my $select = &mt('Select');
11790:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
11791:     if (ref($servers) eq 'HASH') {
11792:         $alltargets = join("','",sort(keys(%{$servers})));
11793:         my @homedoms;
11794:         foreach my $server (sort(keys(%{$servers}))) {
11795:             if (&Apache::lonnet::host_domain($server) eq $dom) {
11796:                 push(@homedoms,'1');
11797:             } else {
11798:                 push(@homedoms,'0');
11799:             }
11800:         }
11801:         $allishome = join("','",@homedoms);
11802:     }
11803:     if (ref($types) eq 'ARRAY') {
11804:         if (@{$types} > 0) {
11805:             @alltypes = @{$types};
11806:         }
11807:     }
11808:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
11809:     $allinsttypes = join("','",@alltypes);
11810:     my (%currbalancer,%currtargets,%currrules,%existing);
11811:     if (ref($settings) eq 'HASH') {
11812:         %existing = %{$settings};
11813:     }
11814:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
11815:                               \%currtargets,\%currrules);
11816:     my $balancers = join("','",sort(keys(%currbalancer)));
11817:     return <<"END";
11818: 
11819: <script type="text/javascript">
11820: // <![CDATA[
11821: 
11822: currBalancers = new Array('$balancers');
11823: 
11824: function toggleTargets(balnum) {
11825:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
11826:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
11827:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
11828:     var prevbalancer = prevhostitem.value;
11829:     var baltotal = document.getElementById('loadbalancing_total').value;
11830:     prevhostitem.value = balancer;
11831:     if (prevbalancer != '') {
11832:         var prevIdx = currBalancers.indexOf(prevbalancer);
11833:         if (prevIdx != -1) {
11834:             currBalancers.splice(prevIdx,1);
11835:         }
11836:     }
11837:     if (balancer == '') {
11838:         hideSpares(balnum);
11839:     } else {
11840:         var currIdx = currBalancers.indexOf(balancer);
11841:         if (currIdx == -1) {
11842:             currBalancers.push(balancer);
11843:         }
11844:         var homedoms = new Array('$allishome');
11845:         var ishomedom = homedoms[lonhostitem.selectedIndex];
11846:         showSpares(balancer,ishomedom,balnum);
11847:     }
11848:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
11849:     return;
11850: }
11851: 
11852: function showSpares(balancer,ishomedom,balnum) {
11853:     var alltargets = new Array('$alltargets');
11854:     var insttypes = new Array('$allinsttypes');
11855:     var offloadtypes = new Array('primary','default');
11856: 
11857:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
11858:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
11859:  
11860:     for (var i=0; i<offloadtypes.length; i++) {
11861:         var count = 0;
11862:         for (var j=0; j<alltargets.length; j++) {
11863:             if (alltargets[j] != balancer) {
11864:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
11865:                 item.value = alltargets[j];
11866:                 item.style.textAlign='left';
11867:                 item.style.textFace='normal';
11868:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
11869:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
11870:                     item.disabled = '';
11871:                 } else {
11872:                     item.disabled = 'disabled';
11873:                     item.checked = false;
11874:                 }
11875:                 count ++;
11876:             }
11877:         }
11878:     }
11879:     for (var k=0; k<insttypes.length; k++) {
11880:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
11881:             if (ishomedom == 1) {
11882:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11883:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11884:             } else {
11885:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11886:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11887:             }
11888:         } else {
11889:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11890:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11891:         }
11892:         if ((insttypes[k] != '_LC_external') && 
11893:             ((insttypes[k] != '_LC_internetdom') ||
11894:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
11895:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
11896:             item.options.length = 0;
11897:             item.options[0] = new Option("","",true,true);
11898:             var idx = 0;
11899:             for (var m=0; m<alltargets.length; m++) {
11900:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
11901:                     idx ++;
11902:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
11903:                 }
11904:             }
11905:         }
11906:     }
11907:     return;
11908: }
11909: 
11910: function hideSpares(balnum) {
11911:     var alltargets = new Array('$alltargets');
11912:     var insttypes = new Array('$allinsttypes');
11913:     var offloadtypes = new Array('primary','default');
11914: 
11915:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
11916:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
11917: 
11918:     var total = alltargets.length - 1;
11919:     for (var i=0; i<offloadtypes; i++) {
11920:         for (var j=0; j<total; j++) {
11921:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
11922:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
11923:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
11924:         }
11925:     }
11926:     for (var k=0; k<insttypes.length; k++) {
11927:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11928:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11929:         if (insttypes[k] != '_LC_external') {
11930:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
11931:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
11932:         }
11933:     }
11934:     return;
11935: }
11936: 
11937: function checkOffloads(item,balnum,type) {
11938:     var alltargets = new Array('$alltargets');
11939:     var offloadtypes = new Array('primary','default');
11940:     if (item.checked) {
11941:         var total = alltargets.length - 1;
11942:         var other;
11943:         if (type == offloadtypes[0]) {
11944:             other = offloadtypes[1];
11945:         } else {
11946:             other = offloadtypes[0];
11947:         }
11948:         for (var i=0; i<total; i++) {
11949:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
11950:             if (server == item.value) {
11951:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
11952:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
11953:                 }
11954:             }
11955:         }
11956:     }
11957:     return;
11958: }
11959: 
11960: function singleServerToggle(balnum,type) {
11961:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
11962:     if (offloadtoSelIdx == 0) {
11963:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
11964:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11965: 
11966:     } else {
11967:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
11968:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11969:     }
11970:     return;
11971: }
11972: 
11973: function balanceruleChange(formname,balnum,type) {
11974:     if (type == '_LC_external') {
11975:         return;
11976:     }
11977:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
11978:     for (var i=0; i<typesRules.length; i++) {
11979:         if (formname.elements[typesRules[i]].checked) {
11980:             if (formname.elements[typesRules[i]].value != 'specific') {
11981:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
11982:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11983:             } else {
11984:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11985:             }
11986:         }
11987:     }
11988:     return;
11989: }
11990: 
11991: function balancerDeleteChange(balnum) {
11992:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
11993:     var baltotal = document.getElementById('loadbalancing_total').value;
11994:     var addtarget;
11995:     var removetarget;
11996:     var action = 'delete';
11997:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
11998:         var lonhost = hostitem.value;
11999:         var currIdx = currBalancers.indexOf(lonhost);
12000:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
12001:             if (currIdx != -1) {
12002:                 currBalancers.splice(currIdx,1);
12003:             }
12004:             addtarget = lonhost;
12005:         } else {
12006:             if (currIdx == -1) {
12007:                 currBalancers.push(lonhost);
12008:             }
12009:             removetarget = lonhost;
12010:             action = 'undelete';
12011:         }
12012:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
12013:     }
12014:     return;
12015: }
12016: 
12017: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
12018:     if (baltotal > 1) {
12019:         var offloadtypes = new Array('primary','default');
12020:         var alltargets = new Array('$alltargets');
12021:         var insttypes = new Array('$allinsttypes');
12022:         for (var i=0; i<baltotal; i++) {
12023:             if (i != balnum) {
12024:                 for (var j=0; j<offloadtypes.length; j++) {
12025:                     var total = alltargets.length - 1;
12026:                     for (var k=0; k<total; k++) {
12027:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
12028:                         var server = serveritem.value;
12029:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
12030:                             if (server == addtarget) {
12031:                                 serveritem.disabled = '';
12032:                             }
12033:                         }
12034:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
12035:                             if (server == removetarget) {
12036:                                 serveritem.disabled = 'disabled';
12037:                                 serveritem.checked = false;
12038:                             }
12039:                         }
12040:                     }
12041:                 }
12042:                 for (var j=0; j<insttypes.length; j++) {
12043:                     if (insttypes[j] != '_LC_external') {
12044:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
12045:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
12046:                             var currSel = singleserver.selectedIndex;
12047:                             var currVal = singleserver.options[currSel].value;
12048:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
12049:                                 var numoptions = singleserver.options.length;
12050:                                 var needsnew = 1;
12051:                                 for (var k=0; k<numoptions; k++) {
12052:                                     if (singleserver.options[k] == addtarget) {
12053:                                         needsnew = 0;
12054:                                         break;
12055:                                     }
12056:                                 }
12057:                                 if (needsnew == 1) {
12058:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
12059:                                 }
12060:                             }
12061:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
12062:                                 singleserver.options.length = 0;
12063:                                 if ((currVal) && (currVal != removetarget)) {
12064:                                     singleserver.options[0] = new Option("","",false,false);
12065:                                 } else {
12066:                                     singleserver.options[0] = new Option("","",true,true);
12067:                                 }
12068:                                 var idx = 0;
12069:                                 for (var m=0; m<alltargets.length; m++) {
12070:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
12071:                                         idx ++;
12072:                                         if (currVal == alltargets[m]) {
12073:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
12074:                                         } else {
12075:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
12076:                                         }
12077:                                     }
12078:                                 }
12079:                             }
12080:                         }
12081:                     }
12082:                 }
12083:             }
12084:         }
12085:     }
12086:     return;
12087: }
12088: 
12089: // ]]>
12090: </script>
12091: 
12092: END
12093: }
12094: 
12095: sub new_spares_js {
12096:     my @sparestypes = ('primary','default');
12097:     my $types = join("','",@sparestypes);
12098:     my $select = &mt('Select');
12099:     return <<"END";
12100: 
12101: <script type="text/javascript">
12102: // <![CDATA[
12103: 
12104: function updateNewSpares(formname,lonhost) {
12105:     var types = new Array('$types');
12106:     var include = new Array();
12107:     var exclude = new Array();
12108:     for (var i=0; i<types.length; i++) {
12109:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
12110:         for (var j=0; j<spareboxes.length; j++) {
12111:             if (formname.elements[spareboxes[j]].checked) {
12112:                 exclude.push(formname.elements[spareboxes[j]].value);
12113:             } else {
12114:                 include.push(formname.elements[spareboxes[j]].value);
12115:             }
12116:         }
12117:     }
12118:     for (var i=0; i<types.length; i++) {
12119:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
12120:         var selIdx = newSpare.selectedIndex;
12121:         var currnew = newSpare.options[selIdx].value;
12122:         var okSpares = new Array();
12123:         for (var j=0; j<newSpare.options.length; j++) {
12124:             var possible = newSpare.options[j].value;
12125:             if (possible != '') {
12126:                 if (exclude.indexOf(possible) == -1) {
12127:                     okSpares.push(possible);
12128:                 } else {
12129:                     if (currnew == possible) {
12130:                         selIdx = 0;
12131:                     }
12132:                 }
12133:             }
12134:         }
12135:         for (var k=0; k<include.length; k++) {
12136:             if (okSpares.indexOf(include[k]) == -1) {
12137:                 okSpares.push(include[k]);
12138:             }
12139:         }
12140:         okSpares.sort();
12141:         newSpare.options.length = 0;
12142:         if (selIdx == 0) {
12143:             newSpare.options[0] = new Option("$select","",true,true);
12144:         } else {
12145:             newSpare.options[0] = new Option("$select","",false,false);
12146:         }
12147:         for (var m=0; m<okSpares.length; m++) {
12148:             var idx = m+1;
12149:             var selThis = 0;
12150:             if (selIdx != 0) {
12151:                 if (okSpares[m] == currnew) {
12152:                     selThis = 1;
12153:                 }
12154:             }
12155:             if (selThis == 1) {
12156:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
12157:             } else {
12158:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
12159:             }
12160:         }
12161:     }
12162:     return;
12163: }
12164: 
12165: function checkNewSpares(lonhost,type) {
12166:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
12167:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
12168:     if (chosen != '') { 
12169:         var othertype;
12170:         var othernewSpare;
12171:         if (type == 'primary') {
12172:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
12173:         }
12174:         if (type == 'default') {
12175:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
12176:         }
12177:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
12178:             othernewSpare.selectedIndex = 0;
12179:         }
12180:     }
12181:     return;
12182: }
12183: 
12184: // ]]>
12185: </script>
12186: 
12187: END
12188: 
12189: }
12190: 
12191: sub common_domprefs_js {
12192:     return <<"END";
12193: 
12194: <script type="text/javascript">
12195: // <![CDATA[
12196: 
12197: function getIndicesByName(formname,item) {
12198:     var group = new Array();
12199:     for (var i=0;i<formname.elements.length;i++) {
12200:         if (formname.elements[i].name == item) {
12201:             group.push(formname.elements[i].id);
12202:         }
12203:     }
12204:     return group;
12205: }
12206: 
12207: // ]]>
12208: </script>
12209: 
12210: END
12211: 
12212: }
12213: 
12214: sub recaptcha_js {
12215:     my %lt = &captcha_phrases();
12216:     return <<"END";
12217: 
12218: <script type="text/javascript">
12219: // <![CDATA[
12220: 
12221: function updateCaptcha(caller,context) {
12222:     var privitem;
12223:     var pubitem;
12224:     var privtext;
12225:     var pubtext;
12226:     var versionitem;
12227:     var versiontext;
12228:     if (document.getElementById(context+'_recaptchapub')) {
12229:         pubitem = document.getElementById(context+'_recaptchapub');
12230:     } else {
12231:         return;
12232:     }
12233:     if (document.getElementById(context+'_recaptchapriv')) {
12234:         privitem = document.getElementById(context+'_recaptchapriv');
12235:     } else {
12236:         return;
12237:     }
12238:     if (document.getElementById(context+'_recaptchapubtxt')) {
12239:         pubtext = document.getElementById(context+'_recaptchapubtxt');
12240:     } else {
12241:         return;
12242:     }
12243:     if (document.getElementById(context+'_recaptchaprivtxt')) {
12244:         privtext = document.getElementById(context+'_recaptchaprivtxt');
12245:     } else {
12246:         return;
12247:     }
12248:     if (document.getElementById(context+'_recaptchaversion')) {
12249:         versionitem = document.getElementById(context+'_recaptchaversion');
12250:     } else {
12251:         return;
12252:     }
12253:     if (document.getElementById(context+'_recaptchavertxt')) {
12254:         versiontext = document.getElementById(context+'_recaptchavertxt');
12255:     } else {
12256:         return;
12257:     }
12258:     if (caller.checked) {
12259:         if (caller.value == 'recaptcha') {
12260:             pubitem.type = 'text';
12261:             privitem.type = 'text';
12262:             pubitem.size = '40';
12263:             privitem.size = '40';
12264:             pubtext.innerHTML = "$lt{'pub'}";
12265:             privtext.innerHTML = "$lt{'priv'}";
12266:             versionitem.type = 'text';
12267:             versionitem.size = '3';
12268:             versiontext.innerHTML = "$lt{'ver'}";
12269:         } else {
12270:             pubitem.type = 'hidden';
12271:             privitem.type = 'hidden';
12272:             versionitem.type = 'hidden';
12273:             pubtext.innerHTML = '';
12274:             privtext.innerHTML = '';
12275:             versiontext.innerHTML = '';
12276:         }
12277:     }
12278:     return;
12279: }
12280: 
12281: // ]]>
12282: </script>
12283: 
12284: END
12285: 
12286: }
12287: 
12288: sub toggle_display_js {
12289:     return <<"END";
12290: 
12291: <script type="text/javascript">
12292: // <![CDATA[
12293: 
12294: function toggleDisplay(domForm,caller) {
12295:     if (document.getElementById(caller)) {
12296:         var divitem = document.getElementById(caller);
12297:         var optionsElement = domForm.coursecredits;
12298:         var checkval = 1;
12299:         var dispval = 'block';
12300:         if (caller == 'emailoptions') {
12301:             optionsElement = domForm.cancreate_email; 
12302:         }
12303:         if (caller == 'studentsubmission') {
12304:             optionsElement = domForm.postsubmit;
12305:         }
12306:         if (caller == 'cloneinstcode') {
12307:             optionsElement = domForm.canclone;
12308:             checkval = 'instcode';
12309:         }
12310:         if (optionsElement.length) {
12311:             var currval;
12312:             for (var i=0; i<optionsElement.length; i++) {
12313:                 if (optionsElement[i].checked) {
12314:                    currval = optionsElement[i].value;
12315:                 }
12316:             }
12317:             if (currval == checkval) {
12318:                 divitem.style.display = dispval;
12319:             } else {
12320:                 divitem.style.display = 'none';
12321:             }
12322:         }
12323:     }
12324:     return;
12325: }
12326: 
12327: // ]]>
12328: </script>
12329: 
12330: END
12331: 
12332: }
12333: 
12334: sub captcha_phrases {
12335:     return &Apache::lonlocal::texthash (
12336:                  priv => 'Private key',
12337:                  pub  => 'Public key',
12338:                  original  => 'original (CAPTCHA)',
12339:                  recaptcha => 'successor (ReCAPTCHA)',
12340:                  notused   => 'unused',
12341:                  ver => 'ReCAPTCHA version (1 or 2)',
12342:     );
12343: }
12344: 
12345: sub devalidate_remote_domconfs {
12346:     my ($dom,$cachekeys) = @_;
12347:     return unless (ref($cachekeys) eq 'HASH');
12348:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
12349:     my %thismachine;
12350:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
12351:     my @posscached = ('domainconfig','domdefaults');
12352:     if (keys(%servers)) {
12353:         foreach my $server (keys(%servers)) {
12354:             next if ($thismachine{$server});
12355:             my @cached;
12356:             foreach my $name (@posscached) {
12357:                 if ($cachekeys->{$name}) {
12358:                     push(@cached,&escape($name).':'.&escape($dom));
12359:                 }
12360:             }
12361:             if (@cached) {
12362:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
12363:             }
12364:         }
12365:     }
12366:     return;
12367: }
12368: 
12369: 1;

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