File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.67: download - view: text, annotated - select for diffs
Fri Aug 5 20:12:25 2016 UTC (7 years, 9 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11
  - Backport 1.268

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.67 2016/08/05 20:12:25 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);
 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:     } else {
 2436:         if ($autorun) {
 2437:             $runon = ' checked="checked" ';
 2438:             $runoff = ' ';
 2439:         } else {
 2440:             $runoff = ' checked="checked" ';
 2441:             $runon = ' ';
 2442:         }
 2443:     }
 2444:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2445:     my $notif_sender;
 2446:     if (ref($settings) eq 'HASH') {
 2447:         $notif_sender = $settings->{'sender_uname'};
 2448:     }
 2449:     my $datatable='<tr class="LC_odd_row">'.
 2450:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2451:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2452:                   '<input type="radio" name="autoenroll_run"'.
 2453:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2454:                   '<label><input type="radio" name="autoenroll_run"'.
 2455:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2456:                   '</tr><tr>'.
 2457:                   '<td>'.&mt('Notification messages - sender').
 2458:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2459:                   &mt('username').':&nbsp;'.
 2460:                   '<input type="text" name="sender_uname" value="'.
 2461:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2462:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2463:                   '<tr class="LC_odd_row">'.
 2464:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2465:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2466:                   '<input type="radio" name="autoassign_coowners"'.
 2467:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2468:                   '<label><input type="radio" name="autoassign_coowners"'.
 2469:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2470:                   '</tr>';
 2471:     $$rowtotal += 3;
 2472:     return $datatable;
 2473: }
 2474: 
 2475: sub print_autoupdate {
 2476:     my ($position,$dom,$settings,$rowtotal) = @_;
 2477:     my $datatable;
 2478:     if ($position eq 'top') {
 2479:         my $updateon = ' ';
 2480:         my $updateoff = ' checked="checked" ';
 2481:         my $classlistson = ' ';
 2482:         my $classlistsoff = ' checked="checked" ';
 2483:         if (ref($settings) eq 'HASH') {
 2484:             if ($settings->{'run'} eq '1') {
 2485:                 $updateon = $updateoff;
 2486:                 $updateoff = ' ';
 2487:             }
 2488:             if ($settings->{'classlists'} eq '1') {
 2489:                 $classlistson = $classlistsoff;
 2490:                 $classlistsoff = ' ';
 2491:             }
 2492:         }
 2493:         my %title = (
 2494:                    run => 'Auto-update active?',
 2495:                    classlists => 'Update information in classlists?',
 2496:                     );
 2497:         $datatable = '<tr class="LC_odd_row">'. 
 2498:                   '<td>'.&mt($title{'run'}).'</td>'.
 2499:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2500:                   '<input type="radio" name="autoupdate_run"'.
 2501:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2502:                   '<label><input type="radio" name="autoupdate_run"'.
 2503:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2504:                   '</tr><tr>'.
 2505:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2506:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2507:                   '<label><input type="radio" name="classlists"'.
 2508:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2509:                   '<label><input type="radio" name="classlists"'.
 2510:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2511:                   '</tr>';
 2512:         $$rowtotal += 2;
 2513:     } elsif ($position eq 'middle') {
 2514:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2515:         my $numinrow = 3;
 2516:         my $locknamesettings;
 2517:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2518:                                      $dom,$numinrow,$othertitle,
 2519:                                     'lockablenames');
 2520:         $$rowtotal ++;
 2521:     } else {
 2522:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2523:         my @fields = ('lastname','firstname','middlename','generation',
 2524:                       'permanentemail','id');
 2525:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2526:         my $numrows = 0;
 2527:         if (ref($types) eq 'ARRAY') {
 2528:             if (@{$types} > 0) {
 2529:                 $datatable = 
 2530:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2531:                                          \@fields,$types,\$numrows);
 2532:                     $$rowtotal += @{$types}; 
 2533:             }
 2534:         }
 2535:         $datatable .= 
 2536:             &usertype_update_row($settings,{'default' => $othertitle},
 2537:                                  \%fieldtitles,\@fields,['default'],
 2538:                                  \$numrows);
 2539:         $$rowtotal ++;     
 2540:     }
 2541:     return $datatable;
 2542: }
 2543: 
 2544: sub print_autocreate {
 2545:     my ($dom,$settings,$rowtotal) = @_;
 2546:     my (%createon,%createoff,%currhash);
 2547:     my @types = ('xml','req');
 2548:     if (ref($settings) eq 'HASH') {
 2549:         foreach my $item (@types) {
 2550:             $createoff{$item} = ' checked="checked" ';
 2551:             $createon{$item} = ' ';
 2552:             if (exists($settings->{$item})) {
 2553:                 if ($settings->{$item}) {
 2554:                     $createon{$item} = ' checked="checked" ';
 2555:                     $createoff{$item} = ' ';
 2556:                 }
 2557:             }
 2558:         }
 2559:         if ($settings->{'xmldc'} ne '') {
 2560:             $currhash{$settings->{'xmldc'}} = 1;
 2561:         }
 2562:     } else {
 2563:         foreach my $item (@types) {
 2564:             $createoff{$item} = ' checked="checked" ';
 2565:             $createon{$item} = ' ';
 2566:         }
 2567:     }
 2568:     $$rowtotal += 2;
 2569:     my $numinrow = 2;
 2570:     my $datatable='<tr class="LC_odd_row">'.
 2571:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2572:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2573:                   '<input type="radio" name="autocreate_xml"'.
 2574:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2575:                   '<label><input type="radio" name="autocreate_xml"'.
 2576:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2577:                   '</td></tr><tr>'.
 2578:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2579:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2580:                   '<input type="radio" name="autocreate_req"'.
 2581:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2582:                   '<label><input type="radio" name="autocreate_req"'.
 2583:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2584:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2585:                                                    'autocreate_xmldc',%currhash);
 2586:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2587:     if ($numdc > 1) {
 2588:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2589:                       '</td><td class="LC_left_item">';
 2590:     } else {
 2591:         $datatable .= &mt('Course creation processed as:').
 2592:                       '</td><td class="LC_right_item">';
 2593:     }
 2594:     $datatable .= $dctable.'</td></tr>';
 2595:     $$rowtotal += $rows;
 2596:     return $datatable;
 2597: }
 2598: 
 2599: sub print_directorysrch {
 2600:     my ($dom,$settings,$rowtotal) = @_;
 2601:     my $srchon = ' ';
 2602:     my $srchoff = ' checked="checked" ';
 2603:     my ($exacton,$containson,$beginson);
 2604:     my $localon = ' ';
 2605:     my $localoff = ' checked="checked" ';
 2606:     if (ref($settings) eq 'HASH') {
 2607:         if ($settings->{'available'} eq '1') {
 2608:             $srchon = $srchoff;
 2609:             $srchoff = ' ';
 2610:         }
 2611:         if ($settings->{'localonly'} eq '1') {
 2612:             $localon = $localoff;
 2613:             $localoff = ' ';
 2614:         }
 2615:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2616:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2617:                 if ($type eq 'exact') {
 2618:                     $exacton = ' checked="checked" ';
 2619:                 } elsif ($type eq 'contains') {
 2620:                     $containson = ' checked="checked" ';
 2621:                 } elsif ($type eq 'begins') {
 2622:                     $beginson = ' checked="checked" ';
 2623:                 }
 2624:             }
 2625:         } else {
 2626:             if ($settings->{'searchtypes'} eq 'exact') {
 2627:                 $exacton = ' checked="checked" ';
 2628:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2629:                 $containson = ' checked="checked" ';
 2630:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2631:                 $exacton = ' checked="checked" ';
 2632:                 $containson = ' checked="checked" ';
 2633:             }
 2634:         }
 2635:     }
 2636:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2637:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2638: 
 2639:     my $numinrow = 4;
 2640:     my $cansrchrow = 0;
 2641:     my $datatable='<tr class="LC_odd_row">'.
 2642:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2643:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2644:                   '<input type="radio" name="dirsrch_available"'.
 2645:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2646:                   '<label><input type="radio" name="dirsrch_available"'.
 2647:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2648:                   '</tr><tr>'.
 2649:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2650:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2651:                   '<input type="radio" name="dirsrch_localonly"'.
 2652:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2653:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2654:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2655:                   '</tr>';
 2656:     $$rowtotal += 2;
 2657:     if (ref($usertypes) eq 'HASH') {
 2658:         if (keys(%{$usertypes}) > 0) {
 2659:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2660:                                          $numinrow,$othertitle,'cansearch');
 2661:             $cansrchrow = 1;
 2662:         }
 2663:     }
 2664:     if ($cansrchrow) {
 2665:         $$rowtotal ++;
 2666:         $datatable .= '<tr>';
 2667:     } else {
 2668:         $datatable .= '<tr class="LC_odd_row">';
 2669:     }
 2670:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2671:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2672:     foreach my $title (@{$titleorder}) {
 2673:         if (defined($searchtitles->{$title})) {
 2674:             my $check = ' ';
 2675:             if (ref($settings) eq 'HASH') {
 2676:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2677:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2678:                         $check = ' checked="checked" ';
 2679:                     }
 2680:                 }
 2681:             }
 2682:             $datatable .= '<td class="LC_left_item">'.
 2683:                           '<span class="LC_nobreak"><label>'.
 2684:                           '<input type="checkbox" name="searchby" '.
 2685:                           'value="'.$title.'"'.$check.'/>'.
 2686:                           $searchtitles->{$title}.'</label></span></td>';
 2687:         }
 2688:     }
 2689:     $datatable .= '</tr></table></td></tr>';
 2690:     $$rowtotal ++;
 2691:     if ($cansrchrow) {
 2692:         $datatable .= '<tr class="LC_odd_row">';
 2693:     } else {
 2694:         $datatable .= '<tr>';
 2695:     }
 2696:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2697:                   '<td class="LC_left_item" colspan="2">'.
 2698:                   '<span class="LC_nobreak"><label>'.
 2699:                   '<input type="checkbox" name="searchtypes" '.
 2700:                   $exacton.' value="exact" />'.&mt('Exact match').
 2701:                   '</label>&nbsp;'.
 2702:                   '<label><input type="checkbox" name="searchtypes" '.
 2703:                   $beginson.' value="begins" />'.&mt('Begins with').
 2704:                   '</label>&nbsp;'.
 2705:                   '<label><input type="checkbox" name="searchtypes" '.
 2706:                   $containson.' value="contains" />'.&mt('Contains').
 2707:                   '</label></span></td></tr>';
 2708:     $$rowtotal ++;
 2709:     return $datatable;
 2710: }
 2711: 
 2712: sub print_contacts {
 2713:     my ($dom,$settings,$rowtotal) = @_;
 2714:     my $datatable;
 2715:     my @contacts = ('adminemail','supportemail');
 2716:     my (%checked,%to,%otheremails,%bccemails);
 2717:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2718:                     'requestsmail','updatesmail','idconflictsmail');
 2719:     foreach my $type (@mailings) {
 2720:         $otheremails{$type} = '';
 2721:     }
 2722:     $bccemails{'helpdeskmail'} = '';
 2723:     if (ref($settings) eq 'HASH') {
 2724:         foreach my $item (@contacts) {
 2725:             if (exists($settings->{$item})) {
 2726:                 $to{$item} = $settings->{$item};
 2727:             }
 2728:         }
 2729:         foreach my $type (@mailings) {
 2730:             if (exists($settings->{$type})) {
 2731:                 if (ref($settings->{$type}) eq 'HASH') {
 2732:                     foreach my $item (@contacts) {
 2733:                         if ($settings->{$type}{$item}) {
 2734:                             $checked{$type}{$item} = ' checked="checked" ';
 2735:                         }
 2736:                     }
 2737:                     $otheremails{$type} = $settings->{$type}{'others'};
 2738:                     if ($type eq 'helpdeskmail') {
 2739:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2740:                     }
 2741:                 }
 2742:             } elsif ($type eq 'lonstatusmail') {
 2743:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2744:             }
 2745:         }
 2746:     } else {
 2747:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2748:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2749:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2750:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2751:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2752:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2753:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2754:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2755:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2756:     }
 2757:     my ($titles,$short_titles) = &contact_titles();
 2758:     my $rownum = 0;
 2759:     my $css_class;
 2760:     foreach my $item (@contacts) {
 2761:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2762:         $datatable .= '<tr'.$css_class.'>'. 
 2763:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2764:                   '</span></td><td class="LC_right_item">'.
 2765:                   '<input type="text" name="'.$item.'" value="'.
 2766:                   $to{$item}.'" /></td></tr>';
 2767:         $rownum ++;
 2768:     }
 2769:     foreach my $type (@mailings) {
 2770:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2771:         $datatable .= '<tr'.$css_class.'>'.
 2772:                       '<td><span class="LC_nobreak">'.
 2773:                       $titles->{$type}.': </span></td>'.
 2774:                       '<td class="LC_left_item">'.
 2775:                       '<span class="LC_nobreak">';
 2776:         foreach my $item (@contacts) {
 2777:             $datatable .= '<label>'.
 2778:                           '<input type="checkbox" name="'.$type.'"'.
 2779:                           $checked{$type}{$item}.
 2780:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2781:                           '</label>&nbsp;';
 2782:         }
 2783:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2784:                       '<input type="text" name="'.$type.'_others" '.
 2785:                       'value="'.$otheremails{$type}.'"  />';
 2786:         if ($type eq 'helpdeskmail') {
 2787:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2788:                           '<input type="text" name="'.$type.'_bcc" '.
 2789:                           'value="'.$bccemails{$type}.'"  />';
 2790:         }
 2791:         $datatable .= '</td></tr>'."\n";
 2792:         $rownum ++;
 2793:     }
 2794:     my %choices;
 2795:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2796:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2797:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2798:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2799:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2800:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2801:     my @toggles = ('reporterrors','reportupdates');
 2802:     my %defaultchecked = ('reporterrors'  => 'on',
 2803:                           'reportupdates' => 'on');
 2804:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2805:                                                \%choices,$rownum);
 2806:     $datatable .= $reports;
 2807:     $$rowtotal += $rownum;
 2808:     return $datatable;
 2809: }
 2810: 
 2811: sub print_helpsettings {
 2812:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2813:     my ($datatable,$itemcount);
 2814:     $itemcount = 1;
 2815:     my (%choices,%defaultchecked,@toggles);
 2816:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2817:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2818:                                  &mt('LON-CAPA bug tracker'),600,500));
 2819:     %defaultchecked = ('submitbugs' => 'on');
 2820:     @toggles = ('submitbugs',);
 2821: 
 2822:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2823:                                                  \%choices,$itemcount);
 2824:     return $datatable;
 2825: }
 2826: 
 2827: sub radiobutton_prefs {
 2828:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2829:         $additional,$align) = @_;
 2830:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2831:                    (ref($choices) eq 'HASH'));
 2832: 
 2833:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2834: 
 2835:     foreach my $item (@{$toggles}) {
 2836:         if ($defaultchecked->{$item} eq 'on') {
 2837:             $checkedon{$item} = ' checked="checked" ';
 2838:             $checkedoff{$item} = ' ';
 2839:         } elsif ($defaultchecked->{$item} eq 'off') {
 2840:             $checkedoff{$item} = ' checked="checked" ';
 2841:             $checkedon{$item} = ' ';
 2842:         }
 2843:     }
 2844:     if (ref($settings) eq 'HASH') {
 2845:         foreach my $item (@{$toggles}) {
 2846:             if ($settings->{$item} eq '1') {
 2847:                 $checkedon{$item} =  ' checked="checked" ';
 2848:                 $checkedoff{$item} = ' ';
 2849:             } elsif ($settings->{$item} eq '0') {
 2850:                 $checkedoff{$item} =  ' checked="checked" ';
 2851:                 $checkedon{$item} = ' ';
 2852:             }
 2853:         }
 2854:     }
 2855:     if ($onclick) {
 2856:         $onclick = ' onclick="'.$onclick.'"';
 2857:     }
 2858:     foreach my $item (@{$toggles}) {
 2859:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2860:         $datatable .=
 2861:             '<tr'.$css_class.'><td valign="top">'.
 2862:             '<span class="LC_nobreak">'.$choices->{$item}.
 2863:             '</span></td>';
 2864:         if ($align eq 'left') {
 2865:             $datatable .= '<td class="LC_left_item">';
 2866:         } else {
 2867:             $datatable .= '<td class="LC_right_item">';
 2868:         }
 2869:         $datatable .=
 2870:             '<span class="LC_nobreak">'.
 2871:             '<label><input type="radio" name="'.
 2872:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2873:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2874:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2875:             '</span>'.$additional.
 2876:             '</td>'.
 2877:             '</tr>';
 2878:         $itemcount ++;
 2879:     }
 2880:     return ($datatable,$itemcount);
 2881: }
 2882: 
 2883: sub print_coursedefaults {
 2884:     my ($position,$dom,$settings,$rowtotal) = @_;
 2885:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2886:     my $itemcount = 1;
 2887:     my %choices =  &Apache::lonlocal::texthash (
 2888:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2889:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2890:         coursecredits        => 'Credits can be specified for courses',
 2891:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 2892:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 2893:         postsubmit           => 'Disable submit button/keypress following student submission',
 2894:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 2895:     );
 2896:     my %staticdefaults = (
 2897:                            anonsurvey_threshold => 10,
 2898:                            uploadquota          => 500,
 2899:                            postsubmit           => 60,
 2900:                          );
 2901:     if ($position eq 'top') {
 2902:         %defaultchecked = (
 2903:                             'uselcmath'       => 'on',
 2904:                             'usejsme'         => 'on',
 2905:                             'canclone'        => 'none',
 2906:                           );
 2907:         @toggles = ('uselcmath','usejsme');
 2908:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2909:                                                      \%choices,$itemcount);
 2910:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2911:         $datatable .=
 2912:             '<tr'.$css_class.'><td valign="top">'.
 2913:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 2914:             '</span></td><td class="LC_left_item">';
 2915:         my $currcanclone = 'none';
 2916:         my $onclick;
 2917:         my @cloneoptions = ('none','domain');
 2918:         my %clonetitles = (
 2919:                              none     => 'No additional course requesters',
 2920:                              domain   => "Any course requester in course's domain",
 2921:                              instcode => 'Course requests for official courses ...',
 2922:                           );
 2923:         my (%codedefaults,@code_order,@posscodes);
 2924:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 2925:                                                     \@code_order) eq 'ok') {
 2926:             if (@code_order > 0) {
 2927:                 push(@cloneoptions,'instcode');
 2928:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 2929:             }
 2930:         }
 2931:         if (ref($settings) eq 'HASH') {
 2932:             if ($settings->{'canclone'}) {
 2933:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 2934:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 2935:                         if (@code_order > 0) {
 2936:                             $currcanclone = 'instcode';
 2937:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 2938:                         }
 2939:                     }
 2940:                 } elsif ($settings->{'canclone'} eq 'domain') {
 2941:                     $currcanclone = $settings->{'canclone'};
 2942:                 }
 2943:             }
 2944:         }
 2945:         foreach my $option (@cloneoptions) {
 2946:             my ($checked,$additional);
 2947:             if ($currcanclone eq $option) {
 2948:                 $checked = ' checked="checked"';
 2949:             }
 2950:             if ($option eq 'instcode') {
 2951:                 if (@code_order) {
 2952:                     my $show = 'none';
 2953:                     if ($checked) {
 2954:                         $show = 'block';
 2955:                     }
 2956:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 2957:                                   &mt('Institutional codes for new and cloned course have identical:').
 2958:                                   '<br />';
 2959:                     foreach my $item (@code_order) {
 2960:                         my $codechk;
 2961:                         if ($checked) {
 2962:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 2963:                                 $codechk = ' checked="checked"';
 2964:                             }
 2965:                         }
 2966:                         $additional .= '<label>'.
 2967:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 2968:                                        $item.'</label>';
 2969:                     }
 2970:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 2971:                 }
 2972:             }
 2973:             $datatable .=
 2974:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 2975:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 2976:                 '</label>&nbsp;'.$additional.'</span><br />';
 2977:         }
 2978:         $datatable .= '</td>'.
 2979:                       '</tr>';
 2980:         $itemcount ++;
 2981:     } else {
 2982:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2983:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout);
 2984:         my $currusecredits = 0;
 2985:         my $postsubmitclient = 1;
 2986:         my @types = ('official','unofficial','community','textbook');
 2987:         if (ref($settings) eq 'HASH') {
 2988:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2989:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2990:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 2991:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 2992:                 }
 2993:             }
 2994:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2995:                 foreach my $type (@types) {
 2996:                     next if ($type eq 'community');
 2997:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 2998:                     if ($defcredits{$type} ne '') {
 2999:                         $currusecredits = 1;
 3000:                     }
 3001:                 }
 3002:             }
 3003:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 3004:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 3005:                     $postsubmitclient = 0;
 3006:                     foreach my $type (@types) {
 3007:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3008:                     }
 3009:                 } else {
 3010:                     foreach my $type (@types) {
 3011:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 3012:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 3013:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 3014:                             } else {
 3015:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3016:                             }
 3017:                         } else {
 3018:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3019:                         }
 3020:                     }
 3021:                 }
 3022:             } else {
 3023:                 foreach my $type (@types) {
 3024:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3025:                 }
 3026:             }
 3027:         } else {
 3028:             foreach my $type (@types) {
 3029:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 3030:             }
 3031:         }
 3032:         if (!$currdefresponder) {
 3033:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 3034:         } elsif ($currdefresponder < 1) {
 3035:             $currdefresponder = 1;
 3036:         }
 3037:         foreach my $type (@types) {
 3038:             if ($curruploadquota{$type} eq '') {
 3039:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 3040:             }
 3041:         }
 3042:         $datatable .=
 3043:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3044:                 $choices{'anonsurvey_threshold'}.
 3045:                 '</span></td>'.
 3046:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 3047:                 '<input type="text" name="anonsurvey_threshold"'.
 3048:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 3049:                 '</td></tr>'."\n";
 3050:         $itemcount ++;
 3051:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3052:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3053:                       $choices{'uploadquota'}.
 3054:                       '</span></td>'.
 3055:                       '<td align="right" class="LC_right_item">'.
 3056:                       '<table><tr>';
 3057:         foreach my $type (@types) {
 3058:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 3059:                            '<input type="text" name="uploadquota_'.$type.'"'.
 3060:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 3061:         }
 3062:         $datatable .= '</tr></table></td></tr>'."\n";
 3063:         $itemcount ++;
 3064:         my $onclick = "toggleDisplay(this.form,'credits');";
 3065:         my $display = 'none';
 3066:         if ($currusecredits) {
 3067:             $display = 'block';
 3068:         }
 3069:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 3070:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 3071:         foreach my $type (@types) {
 3072:             next if ($type eq 'community');
 3073:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 3074:                            '<input type="text" name="'.$type.'_credits"'.
 3075:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 3076:         }
 3077:         $additional .= '</tr></table></div>'."\n";
 3078:         %defaultchecked = ('coursecredits' => 'off');
 3079:         @toggles = ('coursecredits');
 3080:         my $current = {
 3081:                         'coursecredits' => $currusecredits,
 3082:                       };
 3083:         (my $table,$itemcount) =
 3084:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 3085:                                \%choices,$itemcount,$onclick,$additional,'left');
 3086:         $datatable .= $table;
 3087:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 3088:         my $display = 'none';
 3089:         if ($postsubmitclient) {
 3090:             $display = 'block';
 3091:         }
 3092:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 3093:                       &mt('Number of seconds submit is disabled').'<br />'.
 3094:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 3095:                       '<table><tr>';
 3096:         foreach my $type (@types) {
 3097:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 3098:                            '<input type="text" name="'.$type.'_timeout" value="'.
 3099:                            $deftimeout{$type}.'" size="5" /></td>';
 3100:         }
 3101:         $additional .= '</tr></table></div>'."\n";
 3102:         %defaultchecked = ('postsubmit' => 'on');
 3103:         @toggles = ('postsubmit');
 3104:         my $current = {
 3105:                         'postsubmit' => $postsubmitclient,
 3106:                       };
 3107:         ($table,$itemcount) =
 3108:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 3109:                                \%choices,$itemcount,$onclick,$additional,'left');
 3110:         $datatable .= $table;
 3111:     }
 3112:     $$rowtotal += $itemcount;
 3113:     return $datatable;
 3114: }
 3115: 
 3116: sub print_selfenrollment {
 3117:     my ($position,$dom,$settings,$rowtotal) = @_;
 3118:     my ($css_class,$datatable);
 3119:     my $itemcount = 1;
 3120:     my @types = ('official','unofficial','community','textbook');
 3121:     if (($position eq 'top') || ($position eq 'middle')) {
 3122:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 3123:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 3124:         my @rows;
 3125:         my $key;
 3126:         if ($position eq 'top') {
 3127:             $key = 'admin'; 
 3128:             if (ref($rowsref) eq 'ARRAY') {
 3129:                 @rows = @{$rowsref};
 3130:             }
 3131:         } elsif ($position eq 'middle') {
 3132:             $key = 'default';
 3133:             @rows = ('types','registered','approval','limit');
 3134:         }
 3135:         foreach my $row (@rows) {
 3136:             if (defined($titlesref->{$row})) {
 3137:                 $itemcount ++;
 3138:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3139:                 $datatable .= '<tr'.$css_class.'>'.
 3140:                               '<td>'.$titlesref->{$row}.'</td>'.
 3141:                               '<td class="LC_left_item">'.
 3142:                               '<table><tr>';
 3143:                 my (%current,%currentcap);
 3144:                 if (ref($settings) eq 'HASH') {
 3145:                     if (ref($settings->{$key}) eq 'HASH') {
 3146:                         foreach my $type (@types) {
 3147:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 3148:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 3149:                             }
 3150:                             if (($row eq 'limit') && ($key eq 'default')) {
 3151:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 3152:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 3153:                                 }
 3154:                             }
 3155:                         }
 3156:                     }
 3157:                 }
 3158:                 my %roles = (
 3159:                              '0' => &Apache::lonnet::plaintext('dc'),
 3160:                             ); 
 3161:             
 3162:                 foreach my $type (@types) {
 3163:                     unless (($row eq 'registered') && ($key eq 'default')) {
 3164:                         $datatable .= '<th>'.&mt($type).'</th>';
 3165:                     }
 3166:                 }
 3167:                 unless (($row eq 'registered') && ($key eq 'default')) {
 3168:                     $datatable .= '</tr><tr>';
 3169:                 }
 3170:                 foreach my $type (@types) {
 3171:                     if ($type eq 'community') {
 3172:                         $roles{'1'} = &mt('Community personnel');
 3173:                     } else {
 3174:                         $roles{'1'} = &mt('Course personnel');
 3175:                     }
 3176:                     $datatable .= '<td style="vertical-align: top">';
 3177:                     if ($position eq 'top') {
 3178:                         my %checked;
 3179:                         if ($current{$type} eq '0') {
 3180:                             $checked{'0'} = ' checked="checked"';
 3181:                         } else {
 3182:                             $checked{'1'} = ' checked="checked"';
 3183:                         }
 3184:                         foreach my $role ('1','0') {
 3185:                             $datatable .= '<span class="LC_nobreak"><label>'.
 3186:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 3187:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 3188:                                           $roles{$role}.'</label></span> ';
 3189:                         }
 3190:                     } else {
 3191:                         if ($row eq 'types') {
 3192:                             my %checked;
 3193:                             if ($current{$type} =~ /^(all|dom)$/) {
 3194:                                 $checked{$1} = ' checked="checked"';
 3195:                             } else {
 3196:                                 $checked{''} = ' checked="checked"';
 3197:                             }
 3198:                             foreach my $val ('','dom','all') {
 3199:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3200:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3201:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3202:                             }
 3203:                         } elsif ($row eq 'registered') {
 3204:                             my %checked;
 3205:                             if ($current{$type} eq '1') {
 3206:                                 $checked{'1'} = ' checked="checked"';
 3207:                             } else {
 3208:                                 $checked{'0'} = ' checked="checked"';
 3209:                             }
 3210:                             foreach my $val ('0','1') {
 3211:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3212:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3213:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3214:                             }
 3215:                         } elsif ($row eq 'approval') {
 3216:                             my %checked;
 3217:                             if ($current{$type} =~ /^([12])$/) {
 3218:                                 $checked{$1} = ' checked="checked"';
 3219:                             } else {
 3220:                                 $checked{'0'} = ' checked="checked"';
 3221:                             }
 3222:                             for my $val (0..2) {
 3223:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3224:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3225:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3226:                             }
 3227:                         } elsif ($row eq 'limit') {
 3228:                             my %checked;
 3229:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 3230:                                 $checked{$1} = ' checked="checked"';
 3231:                             } else {
 3232:                                 $checked{'none'} = ' checked="checked"';
 3233:                             }
 3234:                             my $cap;
 3235:                             if ($currentcap{$type} =~ /^\d+$/) {
 3236:                                 $cap = $currentcap{$type};
 3237:                             }
 3238:                             foreach my $val ('none','allstudents','selfenrolled') {
 3239:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 3240:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 3241:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 3242:                             }
 3243:                             $datatable .= '<br />'.
 3244:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 3245:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 3246:                                           '</span>'; 
 3247:                         }
 3248:                     }
 3249:                     $datatable .= '</td>';
 3250:                 }
 3251:                 $datatable .= '</tr>';
 3252:             }
 3253:             $datatable .= '</table></td></tr>';
 3254:         }
 3255:     } elsif ($position eq 'bottom') {
 3256:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 3257:     }
 3258:     $$rowtotal += $itemcount;
 3259:     return $datatable;
 3260: }
 3261: 
 3262: sub print_validation_rows {
 3263:     my ($caller,$dom,$settings,$rowtotal) = @_;
 3264:     my ($itemsref,$namesref,$fieldsref);
 3265:     if ($caller eq 'selfenroll') { 
 3266:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 3267:     } elsif ($caller eq 'requestcourses') {
 3268:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 3269:     }
 3270:     my %currvalidation;
 3271:     if (ref($settings) eq 'HASH') {
 3272:         if (ref($settings->{'validation'}) eq 'HASH') {
 3273:             %currvalidation = %{$settings->{'validation'}};
 3274:         }
 3275:     }
 3276:     my $datatable;
 3277:     my $itemcount = 0;
 3278:     foreach my $item (@{$itemsref}) {
 3279:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3280:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 3281:                       $namesref->{$item}.
 3282:                       '</span></td>'.
 3283:                       '<td class="LC_left_item">';
 3284:         if (($item eq 'url') || ($item eq 'button')) {
 3285:             $datatable .= '<span class="LC_nobreak">'.
 3286:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 3287:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 3288:         } elsif ($item eq 'fields') {
 3289:             my @currfields;
 3290:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 3291:                 @currfields = @{$currvalidation{$item}};
 3292:             }
 3293:             foreach my $field (@{$fieldsref}) {
 3294:                 my $check = '';
 3295:                 if (grep(/^\Q$field\E$/,@currfields)) {
 3296:                     $check = ' checked="checked"';
 3297:                 }
 3298:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3299:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 3300:                               ' value="'.$field.'"'.$check.' />'.$field.
 3301:                               '</label></span> ';
 3302:             }
 3303:         } elsif ($item eq 'markup') {
 3304:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 3305:                            $currvalidation{$item}.
 3306:                               '</textarea>';
 3307:         }
 3308:         $datatable .= '</td></tr>'."\n";
 3309:         if (ref($rowtotal)) {
 3310:             $itemcount ++;
 3311:         }
 3312:     }
 3313:     if ($caller eq 'requestcourses') {
 3314:         my %currhash;
 3315:         if (ref($settings) eq 'HASH') {
 3316:             if (ref($settings->{'validation'}) eq 'HASH') {
 3317:                 if ($settings->{'validation'}{'dc'} ne '') {
 3318:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 3319:                 }
 3320:             }
 3321:         }
 3322:         my $numinrow = 2;
 3323:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3324:                                                        'validationdc',%currhash);
 3325:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3326:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 3327:         if ($numdc > 1) {
 3328:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 3329:         } else {
 3330:             $datatable .=  &mt('Course creation processed as: ');
 3331:         }
 3332:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 3333:         $itemcount ++;
 3334:     }
 3335:     if (ref($rowtotal)) {
 3336:         $$rowtotal += $itemcount;
 3337:     }
 3338:     return $datatable;
 3339: }
 3340: 
 3341: sub print_usersessions {
 3342:     my ($position,$dom,$settings,$rowtotal) = @_;
 3343:     my ($css_class,$datatable,%checked,%choices);
 3344:     my (%by_ip,%by_location,@intdoms);
 3345:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 3346: 
 3347:     my @alldoms = &Apache::lonnet::all_domains();
 3348:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 3349:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3350:     my %altids = &id_for_thisdom(%servers);
 3351:     my $itemcount = 1;
 3352:     if ($position eq 'top') {
 3353:         if (keys(%serverhomes) > 1) {
 3354:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 3355:             my $curroffloadnow;
 3356:             if (ref($settings) eq 'HASH') {
 3357:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 3358:                     $curroffloadnow = $settings->{'offloadnow'};
 3359:                 }
 3360:             }
 3361:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 3362:         } else {
 3363:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3364:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 3365:         }
 3366:     } else {
 3367:         if (keys(%by_location) == 0) {
 3368:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 3369:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 3370:         } else {
 3371:             my %lt = &usersession_titles();
 3372:             my $numinrow = 5;
 3373:             my $prefix;
 3374:             my @types;
 3375:             if ($position eq 'bottom') {
 3376:                 $prefix = 'remote';
 3377:                 @types = ('version','excludedomain','includedomain');
 3378:             } else {
 3379:                 $prefix = 'hosted';
 3380:                 @types = ('excludedomain','includedomain');
 3381:             }
 3382:             my (%current,%checkedon,%checkedoff);
 3383:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 3384:             my @locations = sort(keys(%by_location));
 3385:             foreach my $type (@types) {
 3386:                 $checkedon{$type} = '';
 3387:                 $checkedoff{$type} = ' checked="checked"';
 3388:             }
 3389:             if (ref($settings) eq 'HASH') {
 3390:                 if (ref($settings->{$prefix}) eq 'HASH') {
 3391:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 3392:                         $current{$key} = $settings->{$prefix}{$key};
 3393:                         if ($key eq 'version') {
 3394:                             if ($current{$key} ne '') {
 3395:                                 $checkedon{$key} = ' checked="checked"';
 3396:                                 $checkedoff{$key} = '';
 3397:                             }
 3398:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 3399:                             $checkedon{$key} = ' checked="checked"';
 3400:                             $checkedoff{$key} = '';
 3401:                         }
 3402:                     }
 3403:                 }
 3404:             }
 3405:             foreach my $type (@types) {
 3406:                 next if ($type ne 'version' && !@locations);
 3407:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3408:                 $datatable .= '<tr'.$css_class.'>
 3409:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 3410:                                <span class="LC_nobreak">&nbsp;
 3411:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 3412:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 3413:                 if ($type eq 'version') {
 3414:                     my $selector = '<select name="'.$prefix.'_version">';
 3415:                     foreach my $version (@lcversions) {
 3416:                         my $selected = '';
 3417:                         if ($current{'version'} eq $version) {
 3418:                             $selected = ' selected="selected"';
 3419:                         }
 3420:                         $selector .= ' <option value="'.$version.'"'.
 3421:                                      $selected.'>'.$version.'</option>';
 3422:                     }
 3423:                     $selector .= '</select> ';
 3424:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 3425:                 } else {
 3426:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 3427:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 3428:                                  ' />'.('&nbsp;'x2).
 3429:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 3430:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 3431:                                  "\n".
 3432:                                  '</div><div><table>';
 3433:                     my $rem;
 3434:                     for (my $i=0; $i<@locations; $i++) {
 3435:                         my ($showloc,$value,$checkedtype);
 3436:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 3437:                             my $ip = $by_location{$locations[$i]}->[0];
 3438:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 3439:                                  $value = join(':',@{$by_ip{$ip}});
 3440:                                 $showloc = join(', ',@{$by_ip{$ip}});
 3441:                                 if (ref($current{$type}) eq 'ARRAY') {
 3442:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 3443:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 3444:                                             $checkedtype = ' checked="checked"';
 3445:                                             last;
 3446:                                         }
 3447:                                     }
 3448:                                 }
 3449:                             }
 3450:                         }
 3451:                         $rem = $i%($numinrow);
 3452:                         if ($rem == 0) {
 3453:                             if ($i > 0) {
 3454:                                 $datatable .= '</tr>';
 3455:                             }
 3456:                             $datatable .= '<tr>';
 3457:                         }
 3458:                         $datatable .= '<td class="LC_left_item">'.
 3459:                                       '<span class="LC_nobreak"><label>'.
 3460:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 3461:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 3462:                                       '</label></span></td>';
 3463:                     }
 3464:                     $rem = @locations%($numinrow);
 3465:                     my $colsleft = $numinrow - $rem;
 3466:                     if ($colsleft > 1 ) {
 3467:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3468:                                       '&nbsp;</td>';
 3469:                     } elsif ($colsleft == 1) {
 3470:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3471:                     }
 3472:                     $datatable .= '</tr></table>';
 3473:                 }
 3474:                 $datatable .= '</td></tr>';
 3475:                 $itemcount ++;
 3476:             }
 3477:         }
 3478:     }
 3479:     $$rowtotal += $itemcount;
 3480:     return $datatable;
 3481: }
 3482: 
 3483: sub build_location_hashes {
 3484:     my ($intdoms,$by_ip,$by_location) = @_;
 3485:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 3486:                   (ref($by_location) eq 'HASH')); 
 3487:     my %iphost = &Apache::lonnet::get_iphost();
 3488:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 3489:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 3490:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 3491:         foreach my $id (@{$iphost{$primary_ip}}) {
 3492:             my $intdom = &Apache::lonnet::internet_dom($id);
 3493:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 3494:                 push(@{$intdoms},$intdom);
 3495:             }
 3496:         }
 3497:     }
 3498:     foreach my $ip (keys(%iphost)) {
 3499:         if (ref($iphost{$ip}) eq 'ARRAY') {
 3500:             foreach my $id (@{$iphost{$ip}}) {
 3501:                 my $location = &Apache::lonnet::internet_dom($id);
 3502:                 if ($location) {
 3503:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 3504:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3505:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 3506:                             push(@{$by_ip->{$ip}},$location);
 3507:                         }
 3508:                     } else {
 3509:                         $by_ip->{$ip} = [$location];
 3510:                     }
 3511:                 }
 3512:             }
 3513:         }
 3514:     }
 3515:     foreach my $ip (sort(keys(%{$by_ip}))) {
 3516:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 3517:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 3518:             my $first = $by_ip->{$ip}->[0];
 3519:             if (ref($by_location->{$first}) eq 'ARRAY') {
 3520:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 3521:                     push(@{$by_location->{$first}},$ip);
 3522:                 }
 3523:             } else {
 3524:                 $by_location->{$first} = [$ip];
 3525:             }
 3526:         }
 3527:     }
 3528:     return;
 3529: }
 3530: 
 3531: sub current_offloads_to {
 3532:     my ($dom,$settings,$servers) = @_;
 3533:     my (%spareid,%otherdomconfigs);
 3534:     if (ref($servers) eq 'HASH') {
 3535:         foreach my $lonhost (sort(keys(%{$servers}))) {
 3536:             my $gotspares;
 3537:             if (ref($settings) eq 'HASH') {
 3538:                 if (ref($settings->{'spares'}) eq 'HASH') {
 3539:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 3540:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 3541:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 3542:                         $gotspares = 1;
 3543:                     }
 3544:                 }
 3545:             }
 3546:             unless ($gotspares) {
 3547:                 my $gotspares;
 3548:                 my $serverhomeID =
 3549:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 3550:                 my $serverhomedom =
 3551:                     &Apache::lonnet::host_domain($serverhomeID);
 3552:                 if ($serverhomedom ne $dom) {
 3553:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 3554:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3555:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3556:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3557:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3558:                                 $gotspares = 1;
 3559:                             }
 3560:                         }
 3561:                     } else {
 3562:                         $otherdomconfigs{$serverhomedom} =
 3563:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 3564:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 3565:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3566:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3567:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 3568:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3569:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3570:                                         $gotspares = 1;
 3571:                                     }
 3572:                                 }
 3573:                             }
 3574:                         }
 3575:                     }
 3576:                 }
 3577:             }
 3578:             unless ($gotspares) {
 3579:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3580:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3581:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3582:                } else {
 3583:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 3584:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 3585:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3586:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3587:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3588:                     } else {
 3589:                         my %what = (
 3590:                              spareid => 1,
 3591:                         );
 3592:                         my ($result,$returnhash) = 
 3593:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 3594:                         if ($result eq 'ok') { 
 3595:                             if (ref($returnhash) eq 'HASH') {
 3596:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 3597:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 3598:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 3599:                                 }
 3600:                             }
 3601:                         }
 3602:                     }
 3603:                 }
 3604:             }
 3605:         }
 3606:     }
 3607:     return %spareid;
 3608: }
 3609: 
 3610: sub spares_row {
 3611:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 3612:     my $css_class;
 3613:     my $numinrow = 4;
 3614:     my $itemcount = 1;
 3615:     my $datatable;
 3616:     my %typetitles = &sparestype_titles();
 3617:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3618:         foreach my $server (sort(keys(%{$servers}))) {
 3619:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 3620:             my ($othercontrol,$serverdom);
 3621:             if ($serverhome ne $server) {
 3622:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 3623:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3624:             } else {
 3625:                 $serverdom = &Apache::lonnet::host_domain($server);
 3626:                 if ($serverdom ne $dom) {
 3627:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3628:                 }
 3629:             }
 3630:             next unless (ref($spareid->{$server}) eq 'HASH');
 3631:             my $checkednow;
 3632:             if (ref($curroffloadnow) eq 'HASH') {
 3633:                 if ($curroffloadnow->{$server}) {
 3634:                     $checkednow = ' checked="checked"';
 3635:                 }
 3636:             }
 3637:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3638:             $datatable .= '<tr'.$css_class.'>
 3639:                            <td rowspan="2">
 3640:                             <span class="LC_nobreak">'.
 3641:                           &mt('[_1] when busy, offloads to:'
 3642:                               ,'<b>'.$server.'</b>').'</span><br />'.
 3643:                           '<span class="LC_nobreak">'."\n".
 3644:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 3645:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 3646:                           "\n";
 3647:             my (%current,%canselect);
 3648:             my @choices = 
 3649:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 3650:             foreach my $type ('primary','default') {
 3651:                 if (ref($spareid->{$server}) eq 'HASH') {
 3652:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 3653:                         my @spares = @{$spareid->{$server}{$type}};
 3654:                         if (@spares > 0) {
 3655:                             if ($othercontrol) {
 3656:                                 $current{$type} = join(', ',@spares);
 3657:                             } else {
 3658:                                 $current{$type} .= '<table>';
 3659:                                 my $numspares = scalar(@spares);
 3660:                                 for (my $i=0;  $i<@spares; $i++) {
 3661:                                     my $rem = $i%($numinrow);
 3662:                                     if ($rem == 0) {
 3663:                                         if ($i > 0) {
 3664:                                             $current{$type} .= '</tr>';
 3665:                                         }
 3666:                                         $current{$type} .= '<tr>';
 3667:                                     }
 3668:                                     $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;'.
 3669:                                                        $spareid->{$server}{$type}[$i].
 3670:                                                        '</label></td>'."\n";
 3671:                                 }
 3672:                                 my $rem = @spares%($numinrow);
 3673:                                 my $colsleft = $numinrow - $rem;
 3674:                                 if ($colsleft > 1 ) {
 3675:                                     $current{$type} .= '<td colspan="'.$colsleft.
 3676:                                                        '" class="LC_left_item">'.
 3677:                                                        '&nbsp;</td>';
 3678:                                 } elsif ($colsleft == 1) {
 3679:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 3680:                                 }
 3681:                                 $current{$type} .= '</tr></table>';
 3682:                             }
 3683:                         }
 3684:                     }
 3685:                     if ($current{$type} eq '') {
 3686:                         $current{$type} = &mt('None specified');
 3687:                     }
 3688:                     if ($othercontrol) {
 3689:                         if ($type eq 'primary') {
 3690:                             $canselect{$type} = $othercontrol;
 3691:                         }
 3692:                     } else {
 3693:                         $canselect{$type} = 
 3694:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 3695:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 3696:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 3697:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 3698:                         if (@choices > 0) {
 3699:                             foreach my $lonhost (@choices) {
 3700:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 3701:                             }
 3702:                         }
 3703:                         $canselect{$type} .= '</select>'."\n";
 3704:                     }
 3705:                 } else {
 3706:                     $current{$type} = &mt('Could not be determined');
 3707:                     if ($type eq 'primary') {
 3708:                         $canselect{$type} =  $othercontrol;
 3709:                     }
 3710:                 }
 3711:                 if ($type eq 'default') {
 3712:                     $datatable .= '<tr'.$css_class.'>';
 3713:                 }
 3714:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 3715:                               '<td>'.$current{$type}.'</td>'."\n".
 3716:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 3717:             }
 3718:             $itemcount ++;
 3719:         }
 3720:     }
 3721:     $$rowtotal += $itemcount;
 3722:     return $datatable;
 3723: }
 3724: 
 3725: sub possible_newspares {
 3726:     my ($server,$currspares,$serverhomes,$altids) = @_;
 3727:     my $serverhostname = &Apache::lonnet::hostname($server);
 3728:     my %excluded;
 3729:     if ($serverhostname ne '') {
 3730:         %excluded = (
 3731:                        $serverhostname => 1,
 3732:                     );
 3733:     }
 3734:     if (ref($currspares) eq 'HASH') {
 3735:         foreach my $type (keys(%{$currspares})) {
 3736:             if (ref($currspares->{$type}) eq 'ARRAY') {
 3737:                 if (@{$currspares->{$type}} > 0) {
 3738:                     foreach my $curr (@{$currspares->{$type}}) {
 3739:                         my $hostname = &Apache::lonnet::hostname($curr);
 3740:                         $excluded{$hostname} = 1;
 3741:                     }
 3742:                 }
 3743:             }
 3744:         }
 3745:     }
 3746:     my @choices;
 3747:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3748:         if (keys(%{$serverhomes}) > 1) {
 3749:             foreach my $name (sort(keys(%{$serverhomes}))) {
 3750:                 unless ($excluded{$name}) {
 3751:                     if (exists($altids->{$serverhomes->{$name}})) {
 3752:                         push(@choices,$altids->{$serverhomes->{$name}});
 3753:                     } else {
 3754:                         push(@choices,$serverhomes->{$name});
 3755:                     }
 3756:                 }
 3757:             }
 3758:         }
 3759:     }
 3760:     return sort(@choices);
 3761: }
 3762: 
 3763: sub print_loadbalancing {
 3764:     my ($dom,$settings,$rowtotal) = @_;
 3765:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 3766:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 3767:     my $numinrow = 1;
 3768:     my $datatable;
 3769:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3770:     my (%currbalancer,%currtargets,%currrules,%existing);
 3771:     if (ref($settings) eq 'HASH') {
 3772:         %existing = %{$settings};
 3773:     }
 3774:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 3775:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 3776:                                   \%currtargets,\%currrules);
 3777:     } else {
 3778:         return;
 3779:     }
 3780:     my ($othertitle,$usertypes,$types) =
 3781:         &Apache::loncommon::sorted_inst_types($dom);
 3782:     my $rownum = 8;
 3783:     if (ref($types) eq 'ARRAY') {
 3784:         $rownum += scalar(@{$types});
 3785:     }
 3786:     my @css_class = ('LC_odd_row','LC_even_row');
 3787:     my $balnum = 0;
 3788:     my $islast;
 3789:     my (@toshow,$disabledtext);
 3790:     if (keys(%currbalancer) > 0) {
 3791:         @toshow = sort(keys(%currbalancer));
 3792:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3793:             push(@toshow,'');
 3794:         }
 3795:     } else {
 3796:         @toshow = ('');
 3797:         $disabledtext = &mt('No existing load balancer');
 3798:     }
 3799:     foreach my $lonhost (@toshow) {
 3800:         if ($balnum == scalar(@toshow)-1) {
 3801:             $islast = 1;
 3802:         } else {
 3803:             $islast = 0;
 3804:         }
 3805:         my $cssidx = $balnum%2;
 3806:         my $targets_div_style = 'display: none';
 3807:         my $disabled_div_style = 'display: block';
 3808:         my $homedom_div_style = 'display: none';
 3809:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3810:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3811:                       '<p>';
 3812:         if ($lonhost eq '') {
 3813:             $datatable .= '<span class="LC_nobreak">';
 3814:             if (keys(%currbalancer) > 0) {
 3815:                 $datatable .= &mt('Add balancer:');
 3816:             } else {
 3817:                 $datatable .= &mt('Enable balancer:');
 3818:             }
 3819:             $datatable .= '&nbsp;'.
 3820:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3821:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3822:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3823:                           '<option value="" selected="selected">'.&mt('None').
 3824:                           '</option>'."\n";
 3825:             foreach my $server (sort(keys(%servers))) {
 3826:                 next if ($currbalancer{$server});
 3827:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3828:             }
 3829:             $datatable .=
 3830:                 '</select>'."\n".
 3831:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3832:         } else {
 3833:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3834:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3835:                            &mt('Stop balancing').'</label>'.
 3836:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3837:             $targets_div_style = 'display: block';
 3838:             $disabled_div_style = 'display: none';
 3839:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3840:                 $homedom_div_style = 'display: block';
 3841:             }
 3842:         }
 3843:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3844:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3845:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3846:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3847:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3848:         my @sparestypes = ('primary','default');
 3849:         my %typetitles = &sparestype_titles();
 3850:         foreach my $sparetype (@sparestypes) {
 3851:             my $targettable;
 3852:             for (my $i=0; $i<$numspares; $i++) {
 3853:                 my $checked;
 3854:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3855:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3856:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3857:                             $checked = ' checked="checked"';
 3858:                         }
 3859:                     }
 3860:                 }
 3861:                 my ($chkboxval,$disabled);
 3862:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3863:                     $chkboxval = $spares[$i];
 3864:                 }
 3865:                 if (exists($currbalancer{$spares[$i]})) {
 3866:                     $disabled = ' disabled="disabled"';
 3867:                 }
 3868:                 $targettable .=
 3869:                     '<td><span class="LC_nobreak"><label>'.
 3870:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3871:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3872:                     '</span></label></span></td>';
 3873:                 my $rem = $i%($numinrow);
 3874:                 if ($rem == 0) {
 3875:                     if (($i > 0) && ($i < $numspares-1)) {
 3876:                         $targettable .= '</tr>';
 3877:                     }
 3878:                     if ($i < $numspares-1) {
 3879:                         $targettable .= '<tr>';
 3880:                     }
 3881:                 }
 3882:             }
 3883:             if ($targettable ne '') {
 3884:                 my $rem = $numspares%($numinrow);
 3885:                 my $colsleft = $numinrow - $rem;
 3886:                 if ($colsleft > 1 ) {
 3887:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3888:                                     '&nbsp;</td>';
 3889:                 } elsif ($colsleft == 1) {
 3890:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3891:                 }
 3892:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3893:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3894:             }
 3895:         }
 3896:         $datatable .= '</div></td></tr>'.
 3897:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3898:                                            $othertitle,$usertypes,$types,\%servers,
 3899:                                            \%currbalancer,$lonhost,
 3900:                                            $targets_div_style,$homedom_div_style,
 3901:                                            $css_class[$cssidx],$balnum,$islast);
 3902:         $$rowtotal += $rownum;
 3903:         $balnum ++;
 3904:     }
 3905:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3906:     return $datatable;
 3907: }
 3908: 
 3909: sub get_loadbalancers_config {
 3910:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3911:     return unless ((ref($servers) eq 'HASH') &&
 3912:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3913:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3914:     if (keys(%{$existing}) > 0) {
 3915:         my $oldlonhost;
 3916:         foreach my $key (sort(keys(%{$existing}))) {
 3917:             if ($key eq 'lonhost') {
 3918:                 $oldlonhost = $existing->{'lonhost'};
 3919:                 $currbalancer->{$oldlonhost} = 1;
 3920:             } elsif ($key eq 'targets') {
 3921:                 if ($oldlonhost) {
 3922:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3923:                 }
 3924:             } elsif ($key eq 'rules') {
 3925:                 if ($oldlonhost) {
 3926:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3927:                 }
 3928:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3929:                 $currbalancer->{$key} = 1;
 3930:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3931:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3932:             }
 3933:         }
 3934:     } else {
 3935:         my ($balancerref,$targetsref) =
 3936:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3937:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3938:             foreach my $server (sort(keys(%{$balancerref}))) {
 3939:                 $currbalancer->{$server} = 1;
 3940:                 $currtargets->{$server} = $targetsref->{$server};
 3941:             }
 3942:         }
 3943:     }
 3944:     return;
 3945: }
 3946: 
 3947: sub loadbalancing_rules {
 3948:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3949:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3950:         $css_class,$balnum,$islast) = @_;
 3951:     my $output;
 3952:     my $num = 0;
 3953:     my ($alltypes,$othertypes,$titles) =
 3954:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3955:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3956:         foreach my $type (@{$alltypes}) {
 3957:             $num ++;
 3958:             my $current;
 3959:             if (ref($currrules) eq 'HASH') {
 3960:                 $current = $currrules->{$type};
 3961:             }
 3962:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3963:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3964:                     $current = '';
 3965:                 }
 3966:             }
 3967:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3968:                                              $servers,$currbalancer,$lonhost,$dom,
 3969:                                              $targets_div_style,$homedom_div_style,
 3970:                                              $css_class,$balnum,$num,$islast);
 3971:         }
 3972:     }
 3973:     return $output;
 3974: }
 3975: 
 3976: sub loadbalancing_titles {
 3977:     my ($dom,$intdom,$usertypes,$types) = @_;
 3978:     my %othertypes = (
 3979:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3980:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3981:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3982:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3983:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 3984:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 3985:                      );
 3986:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 3987:     if (ref($types) eq 'ARRAY') {
 3988:         unshift(@alltypes,@{$types},'default');
 3989:     }
 3990:     my %titles;
 3991:     foreach my $type (@alltypes) {
 3992:         if ($type =~ /^_LC_/) {
 3993:             $titles{$type} = $othertypes{$type};
 3994:         } elsif ($type eq 'default') {
 3995:             $titles{$type} = &mt('All users from [_1]',$dom);
 3996:             if (ref($types) eq 'ARRAY') {
 3997:                 if (@{$types} > 0) {
 3998:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3999:                 }
 4000:             }
 4001:         } elsif (ref($usertypes) eq 'HASH') {
 4002:             $titles{$type} = $usertypes->{$type};
 4003:         }
 4004:     }
 4005:     return (\@alltypes,\%othertypes,\%titles);
 4006: }
 4007: 
 4008: sub loadbalance_rule_row {
 4009:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 4010:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 4011:     my @rulenames;
 4012:     my %ruletitles = &offloadtype_text();
 4013:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 4014:         @rulenames = ('balancer','offloadedto','specific');
 4015:     } else {
 4016:         @rulenames = ('default','homeserver');
 4017:         if ($type eq '_LC_external') {
 4018:             push(@rulenames,'externalbalancer');
 4019:         } else {
 4020:             push(@rulenames,'specific');
 4021:         }
 4022:         push(@rulenames,'none');
 4023:     }
 4024:     my $style = $targets_div_style;
 4025:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 4026:         $style = $homedom_div_style;
 4027:     }
 4028:     my $space;
 4029:     if ($islast && $num == 1) {
 4030:         $space = '<div display="inline-block">&nbsp;</div>';
 4031:     }
 4032:     my $output =
 4033:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 4034:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 4035:         '<td valaign="top">'.$space.
 4036:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 4037:     for (my $i=0; $i<@rulenames; $i++) {
 4038:         my $rule = $rulenames[$i];
 4039:         my ($checked,$extra);
 4040:         if ($rulenames[$i] eq 'default') {
 4041:             $rule = '';
 4042:         }
 4043:         if ($rulenames[$i] eq 'specific') {
 4044:             if (ref($servers) eq 'HASH') {
 4045:                 my $default;
 4046:                 if (($current ne '') && (exists($servers->{$current}))) {
 4047:                     $checked = ' checked="checked"';
 4048:                 }
 4049:                 unless ($checked) {
 4050:                     $default = ' selected="selected"';
 4051:                 }
 4052:                 $extra =
 4053:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 4054:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 4055:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 4056:                     '<option value=""'.$default.'></option>'."\n";
 4057:                 foreach my $server (sort(keys(%{$servers}))) {
 4058:                     if (ref($currbalancer) eq 'HASH') {
 4059:                         next if (exists($currbalancer->{$server}));
 4060:                     }
 4061:                     my $selected;
 4062:                     if ($server eq $current) {
 4063:                         $selected = ' selected="selected"';
 4064:                     }
 4065:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 4066:                 }
 4067:                 $extra .= '</select>';
 4068:             }
 4069:         } elsif ($rule eq $current) {
 4070:             $checked = ' checked="checked"';
 4071:         }
 4072:         $output .= '<span class="LC_nobreak"><label>'.
 4073:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 4074:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 4075:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 4076:                    ')"'.$checked.' />&nbsp;';
 4077:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 4078:             $output .= $ruletitles{'particular'};
 4079:         } else {
 4080:             $output .= $ruletitles{$rulenames[$i]};
 4081:         }
 4082:         $output .= '</label>'.$extra.'</span><br />'."\n";
 4083:     }
 4084:     $output .= '</div></td></tr>'."\n";
 4085:     return $output;
 4086: }
 4087: 
 4088: sub offloadtype_text {
 4089:     my %ruletitles = &Apache::lonlocal::texthash (
 4090:            'default'          => 'Offloads to default destinations',
 4091:            'homeserver'       => "Offloads to user's home server",
 4092:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 4093:            'specific'         => 'Offloads to specific server',
 4094:            'none'             => 'No offload',
 4095:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 4096:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 4097:            'particular'       => 'Session hosted (after re-auth) on server:',
 4098:     );
 4099:     return %ruletitles;
 4100: }
 4101: 
 4102: sub sparestype_titles {
 4103:     my %typestitles = &Apache::lonlocal::texthash (
 4104:                           'primary' => 'primary',
 4105:                           'default' => 'default',
 4106:                       );
 4107:     return %typestitles;
 4108: }
 4109: 
 4110: sub contact_titles {
 4111:     my %titles = &Apache::lonlocal::texthash (
 4112:                    'supportemail' => 'Support E-mail address',
 4113:                    'adminemail'   => 'Default Server Admin E-mail address',
 4114:                    'errormail'    => 'Error reports to be e-mailed to',
 4115:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 4116:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 4117:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 4118:                    'requestsmail' => 'E-mail from course requests requiring approval',
 4119:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 4120:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 4121:                  );
 4122:     my %short_titles = &Apache::lonlocal::texthash (
 4123:                            adminemail   => 'Admin E-mail address',
 4124:                            supportemail => 'Support E-mail',
 4125:                        );   
 4126:     return (\%titles,\%short_titles);
 4127: }
 4128: 
 4129: sub tool_titles {
 4130:     my %titles = &Apache::lonlocal::texthash (
 4131:                      aboutme    => 'Personal web page',
 4132:                      blog       => 'Blog',
 4133:                      webdav     => 'WebDAV',
 4134:                      portfolio  => 'Portfolio',
 4135:                      official   => 'Official courses (with institutional codes)',
 4136:                      unofficial => 'Unofficial courses',
 4137:                      community  => 'Communities',
 4138:                      textbook   => 'Textbook courses',
 4139:                  );
 4140:     return %titles;
 4141: }
 4142: 
 4143: sub courserequest_titles {
 4144:     my %titles = &Apache::lonlocal::texthash (
 4145:                                    official   => 'Official',
 4146:                                    unofficial => 'Unofficial',
 4147:                                    community  => 'Communities',
 4148:                                    textbook   => 'Textbook',
 4149:                                    norequest  => 'Not allowed',
 4150:                                    approval   => 'Approval by Dom. Coord.',
 4151:                                    validate   => 'With validation',
 4152:                                    autolimit  => 'Numerical limit',
 4153:                                    unlimited  => '(blank for unlimited)',
 4154:                  );
 4155:     return %titles;
 4156: }
 4157: 
 4158: sub authorrequest_titles {
 4159:     my %titles = &Apache::lonlocal::texthash (
 4160:                                    norequest  => 'Not allowed',
 4161:                                    approval   => 'Approval by Dom. Coord.',
 4162:                                    automatic  => 'Automatic approval',
 4163:                  );
 4164:     return %titles;
 4165: }
 4166: 
 4167: sub courserequest_conditions {
 4168:     my %conditions = &Apache::lonlocal::texthash (
 4169:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 4170:        validate   => '(Processing of request subject to institutional validation).',
 4171:                  );
 4172:     return %conditions;
 4173: }
 4174: 
 4175: 
 4176: sub print_usercreation {
 4177:     my ($position,$dom,$settings,$rowtotal) = @_;
 4178:     my $numinrow = 4;
 4179:     my $datatable;
 4180:     if ($position eq 'top') {
 4181:         $$rowtotal ++;
 4182:         my $rowcount = 0;
 4183:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 4184:         if (ref($rules) eq 'HASH') {
 4185:             if (keys(%{$rules}) > 0) {
 4186:                 $datatable .= &user_formats_row('username',$settings,$rules,
 4187:                                                 $ruleorder,$numinrow,$rowcount);
 4188:                 $$rowtotal ++;
 4189:                 $rowcount ++;
 4190:             }
 4191:         }
 4192:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 4193:         if (ref($idrules) eq 'HASH') {
 4194:             if (keys(%{$idrules}) > 0) {
 4195:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 4196:                                                 $idruleorder,$numinrow,$rowcount);
 4197:                 $$rowtotal ++;
 4198:                 $rowcount ++;
 4199:             }
 4200:         }
 4201:         if ($rowcount == 0) {
 4202:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 4203:             $$rowtotal ++;
 4204:             $rowcount ++;
 4205:         }
 4206:     } elsif ($position eq 'middle') {
 4207:         my @creators = ('author','course','requestcrs');
 4208:         my ($rules,$ruleorder) =
 4209:             &Apache::lonnet::inst_userrules($dom,'username');
 4210:         my %lt = &usercreation_types();
 4211:         my %checked;
 4212:         if (ref($settings) eq 'HASH') {
 4213:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 4214:                 foreach my $item (@creators) {
 4215:                     $checked{$item} = $settings->{'cancreate'}{$item};
 4216:                 }
 4217:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 4218:                 foreach my $item (@creators) {
 4219:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 4220:                         $checked{$item} = 'none';
 4221:                     }
 4222:                 }
 4223:             }
 4224:         }
 4225:         my $rownum = 0;
 4226:         foreach my $item (@creators) {
 4227:             $rownum ++;
 4228:             if ($checked{$item} eq '') {
 4229:                 $checked{$item} = 'any';
 4230:             }
 4231:             my $css_class;
 4232:             if ($rownum%2) {
 4233:                 $css_class = '';
 4234:             } else {
 4235:                 $css_class = ' class="LC_odd_row" ';
 4236:             }
 4237:             $datatable .= '<tr'.$css_class.'>'.
 4238:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 4239:                          '</span></td><td align="right">';
 4240:             my @options = ('any');
 4241:             if (ref($rules) eq 'HASH') {
 4242:                 if (keys(%{$rules}) > 0) {
 4243:                     push(@options,('official','unofficial'));
 4244:                 }
 4245:             }
 4246:             push(@options,'none');
 4247:             foreach my $option (@options) {
 4248:                 my $type = 'radio';
 4249:                 my $check = ' ';
 4250:                 if ($checked{$item} eq $option) {
 4251:                     $check = ' checked="checked" ';
 4252:                 } 
 4253:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4254:                               '<input type="'.$type.'" name="can_createuser_'.
 4255:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 4256:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 4257:             }
 4258:             $datatable .= '</td></tr>';
 4259:         }
 4260:     } else {
 4261:         my @contexts = ('author','course','domain');
 4262:         my @authtypes = ('int','krb4','krb5','loc');
 4263:         my %checked;
 4264:         if (ref($settings) eq 'HASH') {
 4265:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 4266:                 foreach my $item (@contexts) {
 4267:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 4268:                         foreach my $auth (@authtypes) {
 4269:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 4270:                                 $checked{$item}{$auth} = ' checked="checked" ';
 4271:                             }
 4272:                         }
 4273:                     }
 4274:                 }
 4275:             }
 4276:         } else {
 4277:             foreach my $item (@contexts) {
 4278:                 foreach my $auth (@authtypes) {
 4279:                     $checked{$item}{$auth} = ' checked="checked" ';
 4280:                 }
 4281:             }
 4282:         }
 4283:         my %title = &context_names();
 4284:         my %authname = &authtype_names();
 4285:         my $rownum = 0;
 4286:         my $css_class; 
 4287:         foreach my $item (@contexts) {
 4288:             if ($rownum%2) {
 4289:                 $css_class = '';
 4290:             } else {
 4291:                 $css_class = ' class="LC_odd_row" ';
 4292:             }
 4293:             $datatable .=   '<tr'.$css_class.'>'.
 4294:                             '<td>'.$title{$item}.
 4295:                             '</td><td class="LC_left_item">'.
 4296:                             '<span class="LC_nobreak">';
 4297:             foreach my $auth (@authtypes) {
 4298:                 $datatable .= '<label>'. 
 4299:                               '<input type="checkbox" name="'.$item.'_auth" '.
 4300:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 4301:                               $authname{$auth}.'</label>&nbsp;';
 4302:             }
 4303:             $datatable .= '</span></td></tr>';
 4304:             $rownum ++;
 4305:         }
 4306:         $$rowtotal += $rownum;
 4307:     }
 4308:     return $datatable;
 4309: }
 4310: 
 4311: sub print_selfcreation {
 4312:     my ($position,$dom,$settings,$rowtotal) = @_;
 4313:     my (@selfcreate,$createsettings,$processing,$datatable);
 4314:     if (ref($settings) eq 'HASH') {
 4315:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 4316:             $createsettings = $settings->{'cancreate'};
 4317:             if (ref($createsettings) eq 'HASH') {
 4318:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 4319:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 4320:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 4321:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 4322:                         @selfcreate = ('email','login','sso');
 4323:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 4324:                         @selfcreate = ($createsettings->{'selfcreate'});
 4325:                     }
 4326:                 }
 4327:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 4328:                     $processing = $createsettings->{'selfcreateprocessing'};
 4329:                 }
 4330:             }
 4331:         }
 4332:     }
 4333:     my %radiohash;
 4334:     my $numinrow = 4;
 4335:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 4336:     if ($position eq 'top') {
 4337:         my %choices = &Apache::lonlocal::texthash (
 4338:                                                       cancreate_login      => 'Institutional Login',
 4339:                                                       cancreate_sso        => 'Institutional Single Sign On',
 4340:                                                   );
 4341:         my @toggles = sort(keys(%choices));
 4342:         my %defaultchecked = (
 4343:                                'cancreate_login' => 'off',
 4344:                                'cancreate_sso'   => 'off',
 4345:                              );
 4346:         my ($onclick,$itemcount);
 4347:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4348:                                                      \%choices,$itemcount,$onclick);
 4349:         $$rowtotal += $itemcount;
 4350:         
 4351:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4352: 
 4353:         if (ref($usertypes) eq 'HASH') {
 4354:             if (keys(%{$usertypes}) > 0) {
 4355:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 4356:                                              $dom,$numinrow,$othertitle,
 4357:                                              'statustocreate',$$rowtotal);
 4358:                 $$rowtotal ++;
 4359:             }
 4360:         }
 4361:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 4362:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4363:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 4364:         my $rem;
 4365:         my $numperrow = 2;
 4366:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 4367:         $datatable .= '<tr'.$css_class.'>'.
 4368:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 4369:                      '<td class="LC_left_item">'."\n".
 4370:                      '<table><tr><td>'."\n";
 4371:         for (my $i=0; $i<@fields; $i++) {
 4372:             $rem = $i%($numperrow);
 4373:             if ($rem == 0) {
 4374:                 if ($i > 0) {
 4375:                     $datatable .= '</tr>';
 4376:                 }
 4377:                 $datatable .= '<tr>';
 4378:             }
 4379:             my $currval;
 4380:             if (ref($createsettings) eq 'HASH') {
 4381:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 4382:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 4383:                 }
 4384:             }
 4385:             $datatable .= '<td class="LC_left_item">'.
 4386:                           '<span class="LC_nobreak">'.
 4387:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 4388:                           'value="'.$currval.'" size="10" />&nbsp;'.
 4389:                           $fieldtitles{$fields[$i]}.'</span></td>';
 4390:         }
 4391:         my $colsleft = $numperrow - $rem;
 4392:         if ($colsleft > 1 ) {
 4393:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4394:                          '&nbsp;</td>';
 4395:         } elsif ($colsleft == 1) {
 4396:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4397:         }
 4398:         $datatable .= '</tr></table></td></tr>';
 4399:         $$rowtotal ++;
 4400:     } elsif ($position eq 'middle') {
 4401:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 4402:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4403:         $usertypes->{'default'} = $othertitle;
 4404:         if (ref($types) eq 'ARRAY') {
 4405:             push(@{$types},'default');
 4406:             $usertypes->{'default'} = $othertitle;
 4407:             foreach my $status (@{$types}) {
 4408:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 4409:                                                        $numinrow,$$rowtotal,$usertypes);
 4410:                 $$rowtotal ++;
 4411:             }
 4412:         }
 4413:     } else {
 4414:         my %choices = &Apache::lonlocal::texthash (
 4415:                                                       cancreate_email => 'E-mail address as username',
 4416:                                                   );
 4417:         my @toggles = sort(keys(%choices));
 4418:         my %defaultchecked = (
 4419:                                'cancreate_email' => 'off',
 4420:                              );
 4421:         my $itemcount = 0;
 4422:         my $display = 'none';
 4423:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 4424:             $display = 'block';
 4425:         }
 4426:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 4427:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 4428:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 4429:         my $usertypes = {};
 4430:         my $order = [];
 4431:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 4432:             $usertypes = $domdefaults{'inststatustypes'};
 4433:             $order = $domdefaults{'inststatusguest'};
 4434:         }
 4435:         if (ref($order) eq 'ARRAY') {
 4436:             push(@{$order},'default');
 4437:             if (@{$order} > 1) {
 4438:                 $usertypes->{'default'} = &mt('Other users');
 4439:                 $additional .= '<table><tr>';
 4440:                 foreach my $status (@{$order}) {
 4441:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 4442:                 }
 4443:                 $additional .= '</tr><tr>';
 4444:                 foreach my $status (@{$order}) {
 4445:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 4446:                 }
 4447:                 $additional .= '</tr></table>';
 4448:             } else {
 4449:                 $usertypes->{'default'} = &mt('All users');
 4450:                 $additional .= &email_as_username($rowtotal,$processing);
 4451:             }
 4452:         }
 4453:         $additional .= '</div>'."\n";
 4454: 
 4455:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 4456:                                                      \%choices,$$rowtotal,$onclick,$additional);
 4457:         $$rowtotal ++;
 4458:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 4459:         $$rowtotal ++;
 4460:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 4461:         $numinrow = 1;
 4462:         if (ref($order) eq 'ARRAY') {
 4463:             foreach my $status (@{$order}) {
 4464:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 4465:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 4466:                 $$rowtotal ++;
 4467:             }
 4468:         }
 4469:         my ($emailrules,$emailruleorder) =
 4470:             &Apache::lonnet::inst_userrules($dom,'email');
 4471:         if (ref($emailrules) eq 'HASH') {
 4472:             if (keys(%{$emailrules}) > 0) {
 4473:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 4474:                                                 $emailruleorder,$numinrow,$$rowtotal);
 4475:                 $$rowtotal ++;
 4476:             }
 4477:         }
 4478:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 4479:     }
 4480:     return $datatable;
 4481: }
 4482: 
 4483: sub email_as_username {
 4484:     my ($rowtotal,$processing,$type) = @_;
 4485:     my %choices =
 4486:         &Apache::lonlocal::texthash (
 4487:                                       automatic => 'Automatic approval',
 4488:                                       approval  => 'Queued for approval',
 4489:                                     );
 4490:     my $output;
 4491:     foreach my $option ('automatic','approval') {
 4492:         my $checked;
 4493:         if (ref($processing) eq 'HASH') {
 4494:             if ($type eq '') {   
 4495:                 if (!exists($processing->{'default'})) {
 4496:                     if ($option eq 'automatic') {
 4497:                         $checked = ' checked="checked"';
 4498:                     }
 4499:                 } else {
 4500:                     if ($processing->{'default'} eq $option) {
 4501:                         $checked = ' checked="checked"';
 4502:                     }
 4503:                 }
 4504:             } else {
 4505:                 if (!exists($processing->{$type})) {
 4506:                     if ($option eq 'automatic') {
 4507:                         $checked = ' checked="checked"';
 4508:                     }
 4509:                 } else {
 4510:                     if ($processing->{$type} eq $option) {
 4511:                         $checked = ' checked="checked"';
 4512:                     }
 4513:                 }
 4514:             }
 4515:         } elsif ($option eq 'automatic') {
 4516:             $checked = ' checked="checked"'; 
 4517:         }
 4518:         my $name = 'cancreate_emailprocess';
 4519:         if (($type ne '') && ($type ne 'default')) {
 4520:             $name .= '_'.$type;
 4521:         }
 4522:         $output .= '<span class="LC_nobreak"><label>'.
 4523:                    '<input type="radio" name="'.$name.'"'.
 4524:                    $checked.' value="'.$option.'" />'.
 4525:                    $choices{$option}.'</label></span>';
 4526:         if ($type eq '') {
 4527:             $output .= '&nbsp;';
 4528:         } else {
 4529:             $output .= '<br />';
 4530:         }
 4531:     }
 4532:     $$rowtotal ++;
 4533:     return $output;
 4534: }
 4535: 
 4536: sub captcha_choice {
 4537:     my ($context,$settings,$itemcount) = @_;
 4538:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 4539:     my %lt = &captcha_phrases();
 4540:     $keyentry = 'hidden';
 4541:     if ($context eq 'cancreate') {
 4542:         $rowname = &mt('CAPTCHA validation');
 4543:     } elsif ($context eq 'login') {
 4544:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 4545:     }
 4546:     if (ref($settings) eq 'HASH') {
 4547:         if ($settings->{'captcha'}) {
 4548:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 4549:         } else {
 4550:             $checked{'original'} = ' checked="checked"';
 4551:         }
 4552:         if ($settings->{'captcha'} eq 'recaptcha') {
 4553:             $pubtext = $lt{'pub'};
 4554:             $privtext = $lt{'priv'};
 4555:             $keyentry = 'text';
 4556:         }
 4557:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 4558:             $currpub = $settings->{'recaptchakeys'}{'public'};
 4559:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 4560:         }
 4561:     } else {
 4562:         $checked{'original'} = ' checked="checked"';
 4563:     }
 4564:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4565:     my $output = '<tr'.$css_class.'>'.
 4566:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 4567:                  '<table><tr><td>'."\n";
 4568:     foreach my $option ('original','recaptcha','notused') {
 4569:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 4570:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 4571:                    $lt{$option}.'</label></span>';
 4572:         unless ($option eq 'notused') {
 4573:             $output .= ('&nbsp;'x2)."\n";
 4574:         }
 4575:     }
 4576: #
 4577: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 4578: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 4579: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 4580: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 4581: #
 4582:     $output .= '</td></tr>'."\n".
 4583:                '<tr><td>'."\n".
 4584:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 4585:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 4586:                $currpub.'" size="40" /></span><br />'."\n".
 4587:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 4588:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 4589:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 4590:                '</td></tr>';
 4591:     return $output;
 4592: }
 4593: 
 4594: sub user_formats_row {
 4595:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 4596:     my $output;
 4597:     my %text = (
 4598:                    'username' => 'new usernames',
 4599:                    'id'       => 'IDs',
 4600:                    'email'    => 'self-created accounts (e-mail)',
 4601:                );
 4602:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4603:     $output = '<tr '.$css_class.'>'.
 4604:               '<td><span class="LC_nobreak">';
 4605:     if ($type eq 'email') {
 4606:         $output .= &mt("Formats disallowed for $text{$type}: ");
 4607:     } else {
 4608:         $output .= &mt("Format rules to check for $text{$type}: ");
 4609:     }
 4610:     $output .= '</span></td>'.
 4611:                '<td class="LC_left_item" colspan="2"><table>';
 4612:     my $rem;
 4613:     if (ref($ruleorder) eq 'ARRAY') {
 4614:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 4615:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 4616:                 my $rem = $i%($numinrow);
 4617:                 if ($rem == 0) {
 4618:                     if ($i > 0) {
 4619:                         $output .= '</tr>';
 4620:                     }
 4621:                     $output .= '<tr>';
 4622:                 }
 4623:                 my $check = ' ';
 4624:                 if (ref($settings) eq 'HASH') {
 4625:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 4626:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 4627:                             $check = ' checked="checked" ';
 4628:                         }
 4629:                     }
 4630:                 }
 4631:                 $output .= '<td class="LC_left_item">'.
 4632:                            '<span class="LC_nobreak"><label>'.
 4633:                            '<input type="checkbox" name="'.$type.'_rule" '.
 4634:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 4635:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 4636:             }
 4637:         }
 4638:         $rem = @{$ruleorder}%($numinrow);
 4639:     }
 4640:     my $colsleft = $numinrow - $rem;
 4641:     if ($colsleft > 1 ) {
 4642:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4643:                    '&nbsp;</td>';
 4644:     } elsif ($colsleft == 1) {
 4645:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4646:     }
 4647:     $output .= '</tr></table></td></tr>';
 4648:     return $output;
 4649: }
 4650: 
 4651: sub usercreation_types {
 4652:     my %lt = &Apache::lonlocal::texthash (
 4653:                     author     => 'When adding a co-author',
 4654:                     course     => 'When adding a user to a course',
 4655:                     requestcrs => 'When requesting a course',
 4656:                     any        => 'Any',
 4657:                     official   => 'Institutional only ',
 4658:                     unofficial => 'Non-institutional only',
 4659:                     none       => 'None',
 4660:     );
 4661:     return %lt;
 4662: }
 4663: 
 4664: sub selfcreation_types {
 4665:     my %lt = &Apache::lonlocal::texthash (
 4666:                     selfcreate => 'User creates own account',
 4667:                     any        => 'Any',
 4668:                     official   => 'Institutional only ',
 4669:                     unofficial => 'Non-institutional only',
 4670:                     email      => 'E-mail address',
 4671:                     login      => 'Institutional Login',
 4672:                     sso        => 'SSO',
 4673:              );
 4674: }
 4675: 
 4676: sub authtype_names {
 4677:     my %lt = &Apache::lonlocal::texthash(
 4678:                       int    => 'Internal',
 4679:                       krb4   => 'Kerberos 4',
 4680:                       krb5   => 'Kerberos 5',
 4681:                       loc    => 'Local',
 4682:                   );
 4683:     return %lt;
 4684: }
 4685: 
 4686: sub context_names {
 4687:     my %context_title = &Apache::lonlocal::texthash(
 4688:        author => 'Creating users when an Author',
 4689:        course => 'Creating users when in a course',
 4690:        domain => 'Creating users when a Domain Coordinator',
 4691:     );
 4692:     return %context_title;
 4693: }
 4694: 
 4695: sub print_usermodification {
 4696:     my ($position,$dom,$settings,$rowtotal) = @_;
 4697:     my $numinrow = 4;
 4698:     my ($context,$datatable,$rowcount);
 4699:     if ($position eq 'top') {
 4700:         $rowcount = 0;
 4701:         $context = 'author'; 
 4702:         foreach my $role ('ca','aa') {
 4703:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4704:                                                    $numinrow,$rowcount);
 4705:             $$rowtotal ++;
 4706:             $rowcount ++;
 4707:         }
 4708:     } elsif ($position eq 'bottom') {
 4709:         $context = 'course';
 4710:         $rowcount = 0;
 4711:         foreach my $role ('st','ep','ta','in','cr') {
 4712:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4713:                                                    $numinrow,$rowcount);
 4714:             $$rowtotal ++;
 4715:             $rowcount ++;
 4716:         }
 4717:     }
 4718:     return $datatable;
 4719: }
 4720: 
 4721: sub print_defaults {
 4722:     my ($position,$dom,$settings,$rowtotal) = @_;
 4723:     my $rownum = 0;
 4724:     my ($datatable,$css_class);
 4725:     if ($position eq 'top') {
 4726:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 4727:                      'datelocale_def','portal_def');
 4728:         my %defaults;
 4729:         if (ref($settings) eq 'HASH') {
 4730:             %defaults = %{$settings};
 4731:         } else {
 4732:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 4733:             foreach my $item (@items) {
 4734:                 $defaults{$item} = $domdefaults{$item};
 4735:             }
 4736:         }
 4737:         my $titles = &defaults_titles($dom);
 4738:         foreach my $item (@items) {
 4739:             if ($rownum%2) {
 4740:                 $css_class = '';
 4741:             } else {
 4742:                 $css_class = ' class="LC_odd_row" ';
 4743:             }
 4744:             $datatable .= '<tr'.$css_class.'>'.
 4745:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4746:                           '</span></td><td class="LC_right_item" colspan="3">';
 4747:             if ($item eq 'auth_def') {
 4748:                 my @authtypes = ('internal','krb4','krb5','localauth');
 4749:                 my %shortauth = (
 4750:                                  internal => 'int',
 4751:                                  krb4 => 'krb4',
 4752:                                  krb5 => 'krb5',
 4753:                                  localauth  => 'loc'
 4754:                                 );
 4755:                 my %authnames = &authtype_names();
 4756:                 foreach my $auth (@authtypes) {
 4757:                     my $checked = ' ';
 4758:                     if ($defaults{$item} eq $auth) {
 4759:                         $checked = ' checked="checked" ';
 4760:                     }
 4761:                     $datatable .= '<label><input type="radio" name="'.$item.
 4762:                                   '" value="'.$auth.'"'.$checked.'/>'.
 4763:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 4764:                 }
 4765:             } elsif ($item eq 'timezone_def') {
 4766:                 my $includeempty = 1;
 4767:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 4768:             } elsif ($item eq 'datelocale_def') {
 4769:                 my $includeempty = 1;
 4770:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 4771:             } elsif ($item eq 'lang_def') {
 4772:                 my $includeempty = 1;
 4773:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 4774:             } else {
 4775:                 my $size;
 4776:                 if ($item eq 'portal_def') {
 4777:                     $size = ' size="25"';
 4778:                 }
 4779:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 4780:                               $defaults{$item}.'"'.$size.' />';
 4781:             }
 4782:             $datatable .= '</td></tr>';
 4783:             $rownum ++;
 4784:         }
 4785:     } else {
 4786:         my (%defaults);
 4787:         if (ref($settings) eq 'HASH') {
 4788:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 4789:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 4790:                 my $maxnum = @{$settings->{'inststatusorder'}};
 4791:                 for (my $i=0; $i<$maxnum; $i++) {
 4792:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 4793:                     my $item = $settings->{'inststatusorder'}->[$i];
 4794:                     my $title = $settings->{'inststatustypes'}->{$item};
 4795:                     my $guestok;
 4796:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 4797:                         $guestok = 1;
 4798:                     }
 4799:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 4800:                     $datatable .= '<tr'.$css_class.'>'.
 4801:                                   '<td><span class="LC_nobreak">'.
 4802:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 4803:                     for (my $k=0; $k<=$maxnum; $k++) {
 4804:                         my $vpos = $k+1;
 4805:                         my $selstr;
 4806:                         if ($k == $i) {
 4807:                             $selstr = ' selected="selected" ';
 4808:                         }
 4809:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4810:                     }
 4811:                     my ($checkedon,$checkedoff);
 4812:                     $checkedoff = ' checked="checked"';
 4813:                     if ($guestok) {
 4814:                         $checkedon = $checkedoff;
 4815:                         $checkedoff = ''; 
 4816:                     }
 4817:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 4818:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 4819:                                   &mt('delete').'</span></td>'.
 4820:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 4821:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 4822:                                   '</span></td>'.
 4823:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4824:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 4825:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 4826:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 4827:                                   &mt('No').'</label></span></td></tr>';
 4828:                 }
 4829:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 4830:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 4831:                 $datatable .= '<tr '.$css_class.'>'.
 4832:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 4833:                 for (my $k=0; $k<=$maxnum; $k++) {
 4834:                     my $vpos = $k+1;
 4835:                     my $selstr;
 4836:                     if ($k == $maxnum) {
 4837:                         $selstr = ' selected="selected" ';
 4838:                     }
 4839:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4840:                 }
 4841:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 4842:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 4843:                               '&nbsp;'.&mt('(new)').
 4844:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 4845:                               &mt('Name displayed:').
 4846:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 4847:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 4848:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 4849:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 4850:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 4851:                               &mt('No').'</label></span></td></tr>';
 4852:                               '</tr>'."\n";
 4853:                 $rownum ++;
 4854:             }
 4855:         }
 4856:     }
 4857:     $$rowtotal += $rownum;
 4858:     return $datatable;
 4859: }
 4860: 
 4861: sub get_languages_hash {
 4862:     my %langchoices;
 4863:     foreach my $id (&Apache::loncommon::languageids()) {
 4864:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 4865:         if ($code ne '') {
 4866:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 4867:         }
 4868:     }
 4869:     return %langchoices;
 4870: }
 4871: 
 4872: sub defaults_titles {
 4873:     my ($dom) = @_;
 4874:     my %titles = &Apache::lonlocal::texthash (
 4875:                    'auth_def'      => 'Default authentication type',
 4876:                    'auth_arg_def'  => 'Default authentication argument',
 4877:                    'lang_def'      => 'Default language',
 4878:                    'timezone_def'  => 'Default timezone',
 4879:                    'datelocale_def' => 'Default locale for dates',
 4880:                    'portal_def'     => 'Portal/Default URL',
 4881:                  );
 4882:     if ($dom) {
 4883:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 4884:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 4885:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 4886:         $protocol = 'http' if ($protocol ne 'https');
 4887:         if ($uint_dom) {
 4888:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 4889:                                          $uint_dom);
 4890:         }
 4891:     }
 4892:     return (\%titles);
 4893: }
 4894: 
 4895: sub print_scantronformat {
 4896:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 4897:     my $itemcount = 1;
 4898:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 4899:         %confhash);
 4900:     my $switchserver = &check_switchserver($dom,$confname);
 4901:     my %lt = &Apache::lonlocal::texthash (
 4902:                 default => 'Default bubblesheet format file error',
 4903:                 custom  => 'Custom bubblesheet format file error',
 4904:              );
 4905:     my %scantronfiles = (
 4906:         default => 'default.tab',
 4907:         custom => 'custom.tab',
 4908:     );
 4909:     foreach my $key (keys(%scantronfiles)) {
 4910:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 4911:                               .$scantronfiles{$key};
 4912:     }
 4913:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 4914:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 4915:         if (!$switchserver) {
 4916:             my $servadm = $r->dir_config('lonAdmEMail');
 4917:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 4918:             if ($configuserok eq 'ok') {
 4919:                 if ($author_ok eq 'ok') {
 4920:                     my %legacyfile = (
 4921:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 4922:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 4923:                     );
 4924:                     my %md5chk;
 4925:                     foreach my $type (keys(%legacyfile)) {
 4926:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 4927:                         chomp($md5chk{$type});
 4928:                     }
 4929:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 4930:                         foreach my $type (keys(%legacyfile)) {
 4931:                             ($scantronurls{$type},my $error) = 
 4932:                                 &legacy_scantronformat($r,$dom,$confname,
 4933:                                                  $type,$legacyfile{$type},
 4934:                                                  $scantronurls{$type},
 4935:                                                  $scantronfiles{$type});
 4936:                             if ($error ne '') {
 4937:                                 $error{$type} = $error;
 4938:                             }
 4939:                         }
 4940:                         if (keys(%error) == 0) {
 4941:                             $is_custom = 1;
 4942:                             $confhash{'scantron'}{'scantronformat'} = 
 4943:                                 $scantronurls{'custom'};
 4944:                             my $putresult = 
 4945:                                 &Apache::lonnet::put_dom('configuration',
 4946:                                                          \%confhash,$dom);
 4947:                             if ($putresult ne 'ok') {
 4948:                                 $error{'custom'} = 
 4949:                                     '<span class="LC_error">'.
 4950:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4951:                             }
 4952:                         }
 4953:                     } else {
 4954:                         ($scantronurls{'default'},my $error) =
 4955:                             &legacy_scantronformat($r,$dom,$confname,
 4956:                                           'default',$legacyfile{'default'},
 4957:                                           $scantronurls{'default'},
 4958:                                           $scantronfiles{'default'});
 4959:                         if ($error eq '') {
 4960:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 4961:                             my $putresult =
 4962:                                 &Apache::lonnet::put_dom('configuration',
 4963:                                                          \%confhash,$dom);
 4964:                             if ($putresult ne 'ok') {
 4965:                                 $error{'default'} =
 4966:                                     '<span class="LC_error">'.
 4967:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4968:                             }
 4969:                         } else {
 4970:                             $error{'default'} = $error;
 4971:                         }
 4972:                     }
 4973:                 }
 4974:             }
 4975:         } else {
 4976:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 4977:         }
 4978:     }
 4979:     if (ref($settings) eq 'HASH') {
 4980:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 4981:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 4982:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 4983:                 $scantronurl = '';
 4984:             } else {
 4985:                 $scantronurl = $settings->{'scantronformat'};
 4986:             }
 4987:             $is_custom = 1;
 4988:         } else {
 4989:             $scantronurl = $scantronurls{'default'};
 4990:         }
 4991:     } else {
 4992:         if ($is_custom) {
 4993:             $scantronurl = $scantronurls{'custom'};
 4994:         } else {
 4995:             $scantronurl = $scantronurls{'default'};
 4996:         }
 4997:     }
 4998:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4999:     $datatable .= '<tr'.$css_class.'>';
 5000:     if (!$is_custom) {
 5001:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 5002:                       '<span class="LC_nobreak">';
 5003:         if ($scantronurl) {
 5004:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 5005:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 5006:         } else {
 5007:             $datatable = &mt('File unavailable for display');
 5008:         }
 5009:         $datatable .= '</span></td>';
 5010:         if (keys(%error) == 0) { 
 5011:             $datatable .= '<td valign="bottom">';
 5012:             if (!$switchserver) {
 5013:                 $datatable .= &mt('Upload:').'<br />';
 5014:             }
 5015:         } else {
 5016:             my $errorstr;
 5017:             foreach my $key (sort(keys(%error))) {
 5018:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 5019:             }
 5020:             $datatable .= '<td>'.$errorstr;
 5021:         }
 5022:     } else {
 5023:         if (keys(%error) > 0) {
 5024:             my $errorstr;
 5025:             foreach my $key (sort(keys(%error))) {
 5026:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 5027:             } 
 5028:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 5029:         } elsif ($scantronurl) {
 5030:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 5031:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 5032:             $datatable .= '<td><span class="LC_nobreak">'.
 5033:                           $link.
 5034:                           '<label><input type="checkbox" name="scantronformat_del"'.
 5035:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 5036:                           '<td><span class="LC_nobreak">&nbsp;'.
 5037:                           &mt('Replace:').'</span><br />';
 5038:         }
 5039:     }
 5040:     if (keys(%error) == 0) {
 5041:         if ($switchserver) {
 5042:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5043:         } else {
 5044:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 5045:                          '<input type="file" name="scantronformat" /></span>';
 5046:         }
 5047:     }
 5048:     $datatable .= '</td></tr>';
 5049:     $$rowtotal ++;
 5050:     return $datatable;
 5051: }
 5052: 
 5053: sub legacy_scantronformat {
 5054:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 5055:     my ($url,$error);
 5056:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 5057:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 5058:         (my $result,$url) =
 5059:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 5060:                          '','',$newfile);
 5061:         if ($result ne 'ok') {
 5062:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 5063:         }
 5064:     }
 5065:     return ($url,$error);
 5066: }
 5067: 
 5068: sub print_coursecategories {
 5069:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 5070:     my $datatable;
 5071:     if ($position eq 'top') {
 5072:         my (%checked);
 5073:         my @catitems = ('unauth','auth');
 5074:         my @cattypes = ('std','domonly','codesrch','none');
 5075:         $checked{'unauth'} = 'std';
 5076:         $checked{'auth'} = 'std';
 5077:         if (ref($settings) eq 'HASH') {
 5078:             foreach my $type (@cattypes) {
 5079:                 if ($type eq $settings->{'unauth'}) {
 5080:                     $checked{'unauth'} = $type;
 5081:                 }
 5082:                 if ($type eq $settings->{'auth'}) {
 5083:                     $checked{'auth'} = $type;
 5084:                 }
 5085:             }
 5086:         }
 5087:         my %lt = &Apache::lonlocal::texthash (
 5088:                                                unauth   => 'Catalog type for unauthenticated users',
 5089:                                                auth     => 'Catalog type for authenticated users',
 5090:                                                none     => 'No catalog',
 5091:                                                std      => 'Standard catalog',
 5092:                                                domonly  => 'Domain-only catalog',
 5093:                                                codesrch => "Code search form",
 5094:                                              );
 5095:        my $itemcount = 0;
 5096:        foreach my $item (@catitems) {
 5097:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 5098:            $datatable .= '<tr '.$css_class.'>'.
 5099:                          '<td>'.$lt{$item}.'</td>'.
 5100:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 5101:            foreach my $type (@cattypes) {
 5102:                my $ischecked;
 5103:                if ($checked{$item} eq $type) {
 5104:                    $ischecked=' checked="checked"';
 5105:                }
 5106:                $datatable .= '<label>'.
 5107:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 5108:                              ' />'.$lt{$type}.'</label>&nbsp;';
 5109:            }
 5110:            $datatable .= '</td></tr>';
 5111:            $itemcount ++;
 5112:         }
 5113:         $$rowtotal += $itemcount;
 5114:     } elsif ($position eq 'middle') {
 5115:         my $toggle_cats_crs = ' ';
 5116:         my $toggle_cats_dom = ' checked="checked" ';
 5117:         my $can_cat_crs = ' ';
 5118:         my $can_cat_dom = ' checked="checked" ';
 5119:         my $toggle_catscomm_comm = ' ';
 5120:         my $toggle_catscomm_dom = ' checked="checked" ';
 5121:         my $can_catcomm_comm = ' ';
 5122:         my $can_catcomm_dom = ' checked="checked" ';
 5123: 
 5124:         if (ref($settings) eq 'HASH') {
 5125:             if ($settings->{'togglecats'} eq 'crs') {
 5126:                 $toggle_cats_crs = $toggle_cats_dom;
 5127:                 $toggle_cats_dom = ' ';
 5128:             }
 5129:             if ($settings->{'categorize'} eq 'crs') {
 5130:                 $can_cat_crs = $can_cat_dom;
 5131:                 $can_cat_dom = ' ';
 5132:             }
 5133:             if ($settings->{'togglecatscomm'} eq 'comm') {
 5134:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 5135:                 $toggle_catscomm_dom = ' ';
 5136:             }
 5137:             if ($settings->{'categorizecomm'} eq 'comm') {
 5138:                 $can_catcomm_comm = $can_catcomm_dom;
 5139:                 $can_catcomm_dom = ' ';
 5140:             }
 5141:         }
 5142:         my %title = &Apache::lonlocal::texthash (
 5143:                      togglecats     => 'Show/Hide a course in catalog',
 5144:                      togglecatscomm => 'Show/Hide a community in catalog',
 5145:                      categorize     => 'Assign a category to a course',
 5146:                      categorizecomm => 'Assign a category to a community',
 5147:                     );
 5148:         my %level = &Apache::lonlocal::texthash (
 5149:                      dom  => 'Set in Domain',
 5150:                      crs  => 'Set in Course',
 5151:                      comm => 'Set in Community',
 5152:                     );
 5153:         $datatable = '<tr class="LC_odd_row">'.
 5154:                   '<td>'.$title{'togglecats'}.'</td>'.
 5155:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 5156:                   '<input type="radio" name="togglecats"'.
 5157:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5158:                   '<label><input type="radio" name="togglecats"'.
 5159:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 5160:                   '</tr><tr>'.
 5161:                   '<td>'.$title{'categorize'}.'</td>'.
 5162:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 5163:                   '<label><input type="radio" name="categorize"'.
 5164:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5165:                   '<label><input type="radio" name="categorize"'.
 5166:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 5167:                   '</tr><tr class="LC_odd_row">'.
 5168:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 5169:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 5170:                   '<input type="radio" name="togglecatscomm"'.
 5171:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5172:                   '<label><input type="radio" name="togglecatscomm"'.
 5173:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 5174:                   '</tr><tr>'.
 5175:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 5176:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 5177:                   '<label><input type="radio" name="categorizecomm"'.
 5178:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 5179:                   '<label><input type="radio" name="categorizecomm"'.
 5180:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 5181:                   '</tr>';
 5182:         $$rowtotal += 4;
 5183:     } else {
 5184:         my $css_class;
 5185:         my $itemcount = 1;
 5186:         my $cathash; 
 5187:         if (ref($settings) eq 'HASH') {
 5188:             $cathash = $settings->{'cats'};
 5189:         }
 5190:         if (ref($cathash) eq 'HASH') {
 5191:             my (@cats,@trails,%allitems,%idx,@jsarray);
 5192:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 5193:                                                    \%allitems,\%idx,\@jsarray);
 5194:             my $maxdepth = scalar(@cats);
 5195:             my $colattrib = '';
 5196:             if ($maxdepth > 2) {
 5197:                 $colattrib = ' colspan="2" ';
 5198:             }
 5199:             my @path;
 5200:             if (@cats > 0) {
 5201:                 if (ref($cats[0]) eq 'ARRAY') {
 5202:                     my $numtop = @{$cats[0]};
 5203:                     my $maxnum = $numtop;
 5204:                     my %default_names = (
 5205:                           instcode    => &mt('Official courses'),
 5206:                           communities => &mt('Communities'),
 5207:                     );
 5208: 
 5209:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 5210:                         ($cathash->{'instcode::0'} eq '') ||
 5211:                         (!grep(/^communities$/,@{$cats[0]})) || 
 5212:                         ($cathash->{'communities::0'} eq '')) {
 5213:                         $maxnum ++;
 5214:                     }
 5215:                     my $lastidx;
 5216:                     for (my $i=0; $i<$numtop; $i++) {
 5217:                         my $parent = $cats[0][$i];
 5218:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5219:                         my $item = &escape($parent).'::0';
 5220:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 5221:                         $lastidx = $idx{$item};
 5222:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5223:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 5224:                         for (my $k=0; $k<=$maxnum; $k++) {
 5225:                             my $vpos = $k+1;
 5226:                             my $selstr;
 5227:                             if ($k == $i) {
 5228:                                 $selstr = ' selected="selected" ';
 5229:                             }
 5230:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5231:                         }
 5232:                         $datatable .= '</select></span></td><td>';
 5233:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 5234:                             $datatable .=  '<span class="LC_nobreak">'
 5235:                                            .$default_names{$parent}.'</span>';
 5236:                             if ($parent eq 'instcode') {
 5237:                                 $datatable .= '<br /><span class="LC_nobreak">('
 5238:                                               .&mt('with institutional codes')
 5239:                                               .')</span></td><td'.$colattrib.'>';
 5240:                             } else {
 5241:                                 $datatable .= '<table><tr><td>';
 5242:                             }
 5243:                             $datatable .= '<span class="LC_nobreak">'
 5244:                                           .'<label><input type="radio" name="'
 5245:                                           .$parent.'" value="1" checked="checked" />'
 5246:                                           .&mt('Display').'</label>';
 5247:                             if ($parent eq 'instcode') {
 5248:                                 $datatable .= '&nbsp;';
 5249:                             } else {
 5250:                                 $datatable .= '</span></td></tr><tr><td>'
 5251:                                               .'<span class="LC_nobreak">';
 5252:                             }
 5253:                             $datatable .= '<label><input type="radio" name="'
 5254:                                           .$parent.'" value="0" />'
 5255:                                           .&mt('Do not display').'</label></span>';
 5256:                             if ($parent eq 'communities') {
 5257:                                 $datatable .= '</td></tr></table>';
 5258:                             }
 5259:                             $datatable .= '</td>';
 5260:                         } else {
 5261:                             $datatable .= $parent
 5262:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 5263:                                           .'<input type="checkbox" name="deletecategory" '
 5264:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 5265:                         }
 5266:                         my $depth = 1;
 5267:                         push(@path,$parent);
 5268:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 5269:                         pop(@path);
 5270:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 5271:                         $itemcount ++;
 5272:                     }
 5273:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5274:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 5275:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 5276:                     for (my $k=0; $k<=$maxnum; $k++) {
 5277:                         my $vpos = $k+1;
 5278:                         my $selstr;
 5279:                         if ($k == $numtop) {
 5280:                             $selstr = ' selected="selected" ';
 5281:                         }
 5282:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5283:                     }
 5284:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 5285:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 5286:                                   .'</tr>'."\n";
 5287:                     $itemcount ++;
 5288:                     foreach my $default ('instcode','communities') {
 5289:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 5290:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5291:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 5292:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 5293:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 5294:                             for (my $k=0; $k<=$maxnum; $k++) {
 5295:                                 my $vpos = $k+1;
 5296:                                 my $selstr;
 5297:                                 if ($k == $maxnum) {
 5298:                                     $selstr = ' selected="selected" ';
 5299:                                 }
 5300:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5301:                             }
 5302:                             $datatable .= '</select></span></td>'.
 5303:                                           '<td><span class="LC_nobreak">'.
 5304:                                           $default_names{$default}.'</span>';
 5305:                             if ($default eq 'instcode') {
 5306:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 5307:                                               .&mt('with institutional codes').')</span>';
 5308:                             }
 5309:                             $datatable .= '</td>'
 5310:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 5311:                                           .&mt('Display').'</label>&nbsp;'
 5312:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 5313:                                           .&mt('Do not display').'</label></span></td></tr>';
 5314:                         }
 5315:                     }
 5316:                 }
 5317:             } else {
 5318:                 $datatable .= &initialize_categories($itemcount);
 5319:             }
 5320:         } else {
 5321:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 5322:                           .&initialize_categories($itemcount);
 5323:         }
 5324:         $$rowtotal += $itemcount;
 5325:     }
 5326:     return $datatable;
 5327: }
 5328: 
 5329: sub print_serverstatuses {
 5330:     my ($dom,$settings,$rowtotal) = @_;
 5331:     my $datatable;
 5332:     my @pages = &serverstatus_pages();
 5333:     my (%namedaccess,%machineaccess);
 5334:     foreach my $type (@pages) {
 5335:         $namedaccess{$type} = '';
 5336:         $machineaccess{$type}= '';
 5337:     }
 5338:     if (ref($settings) eq 'HASH') {
 5339:         foreach my $type (@pages) {
 5340:             if (exists($settings->{$type})) {
 5341:                 if (ref($settings->{$type}) eq 'HASH') {
 5342:                     foreach my $key (keys(%{$settings->{$type}})) {
 5343:                         if ($key eq 'namedusers') {
 5344:                             $namedaccess{$type} = $settings->{$type}->{$key};
 5345:                         } elsif ($key eq 'machines') {
 5346:                             $machineaccess{$type} = $settings->{$type}->{$key};
 5347:                         }
 5348:                     }
 5349:                 }
 5350:             }
 5351:         }
 5352:     }
 5353:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 5354:     my $rownum = 0;
 5355:     my $css_class;
 5356:     foreach my $type (@pages) {
 5357:         $rownum ++;
 5358:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 5359:         $datatable .= '<tr'.$css_class.'>'.
 5360:                       '<td><span class="LC_nobreak">'.
 5361:                       $titles->{$type}.'</span></td>'.
 5362:                       '<td class="LC_left_item">'.
 5363:                       '<input type="text" name="'.$type.'_namedusers" '.
 5364:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 5365:                       '<td class="LC_right_item">'.
 5366:                       '<span class="LC_nobreak">'.
 5367:                       '<input type="text" name="'.$type.'_machines" '.
 5368:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 5369:                       '</td></tr>'."\n";
 5370:     }
 5371:     $$rowtotal += $rownum;
 5372:     return $datatable;
 5373: }
 5374: 
 5375: sub serverstatus_pages {
 5376:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 5377:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 5378:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 5379:             'uniquecodes','diskusage','coursecatalog');
 5380: }
 5381: 
 5382: sub defaults_javascript {
 5383:     my ($settings) = @_;
 5384:     return unless (ref($settings) eq 'HASH');
 5385:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 5386:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 5387:         if ($maxnum eq '') {
 5388:             $maxnum = 0;
 5389:         }
 5390:         $maxnum ++;
 5391:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 5392:         return <<"ENDSCRIPT";
 5393: <script type="text/javascript">
 5394: // <![CDATA[
 5395: function reorderTypes(form,caller) {
 5396:     var changedVal;
 5397: $jstext 
 5398:     var newpos = 'addinststatus_pos';
 5399:     var current = new Array;
 5400:     var maxh = $maxnum;
 5401:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5402:     var oldVal;
 5403:     if (caller == newpos) {
 5404:         changedVal = newitemVal;
 5405:     } else {
 5406:         var curritem = 'inststatus_pos_'+caller;
 5407:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 5408:         current[newitemVal] = newpos;
 5409:     }
 5410:     for (var i=0; i<inststatuses.length; i++) {
 5411:         if (inststatuses[i] != caller) {
 5412:             var elementName = 'inststatus_pos_'+inststatuses[i];
 5413:             if (form.elements[elementName]) {
 5414:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5415:                 current[currVal] = elementName;
 5416:             }
 5417:         }
 5418:     }
 5419:     for (var j=0; j<maxh; j++) {
 5420:         if (current[j] == undefined) {
 5421:             oldVal = j;
 5422:         }
 5423:     }
 5424:     if (oldVal < changedVal) {
 5425:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5426:            var elementName = current[k];
 5427:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5428:         }
 5429:     } else {
 5430:         for (var k=changedVal; k<oldVal; k++) {
 5431:             var elementName = current[k];
 5432:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5433:         }
 5434:     }
 5435:     return;
 5436: }
 5437: 
 5438: // ]]>
 5439: </script>
 5440: 
 5441: ENDSCRIPT
 5442:     }
 5443: }
 5444: 
 5445: sub coursecategories_javascript {
 5446:     my ($settings) = @_;
 5447:     my ($output,$jstext,$cathash);
 5448:     if (ref($settings) eq 'HASH') {
 5449:         $cathash = $settings->{'cats'};
 5450:     }
 5451:     if (ref($cathash) eq 'HASH') {
 5452:         my (@cats,@jsarray,%idx);
 5453:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 5454:         if (@jsarray > 0) {
 5455:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 5456:             for (my $i=0; $i<@jsarray; $i++) {
 5457:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 5458:                     my $catstr = join('","',@{$jsarray[$i]});
 5459:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 5460:                 }
 5461:             }
 5462:         }
 5463:     } else {
 5464:         $jstext  = '    var categories = Array(1);'."\n".
 5465:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 5466:     }
 5467:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 5468:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 5469:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 5470:     &js_escape(\$instcode_reserved);
 5471:     &js_escape(\$communities_reserved);
 5472:     &js_escape(\$choose_again);
 5473:     $output = <<"ENDSCRIPT";
 5474: <script type="text/javascript">
 5475: // <![CDATA[
 5476: function reorderCats(form,parent,item,idx) {
 5477:     var changedVal;
 5478: $jstext
 5479:     var newpos = 'addcategory_pos';
 5480:     if (parent == '') {
 5481:         var has_instcode = 0;
 5482:         var maxtop = categories[idx].length;
 5483:         for (var j=0; j<maxtop; j++) {
 5484:             if (categories[idx][j] == 'instcode::0') {
 5485:                 has_instcode == 1;
 5486:             }
 5487:         }
 5488:         if (has_instcode == 0) {
 5489:             categories[idx][maxtop] = 'instcode_pos';
 5490:         }
 5491:     } else {
 5492:         newpos += '_'+parent;
 5493:     }
 5494:     var maxh = 1 + categories[idx].length;
 5495:     var current = new Array;
 5496:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5497:     if (item == newpos) {
 5498:         changedVal = newitemVal;
 5499:     } else {
 5500:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 5501:         current[newitemVal] = newpos;
 5502:     }
 5503:     for (var i=0; i<categories[idx].length; i++) {
 5504:         var elementName = categories[idx][i];
 5505:         if (elementName != item) {
 5506:             if (form.elements[elementName]) {
 5507:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5508:                 current[currVal] = elementName;
 5509:             }
 5510:         }
 5511:     }
 5512:     var oldVal;
 5513:     for (var j=0; j<maxh; j++) {
 5514:         if (current[j] == undefined) {
 5515:             oldVal = j;
 5516:         }
 5517:     }
 5518:     if (oldVal < changedVal) {
 5519:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5520:            var elementName = current[k];
 5521:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5522:         }
 5523:     } else {
 5524:         for (var k=changedVal; k<oldVal; k++) {
 5525:             var elementName = current[k];
 5526:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5527:         }
 5528:     }
 5529:     return;
 5530: }
 5531: 
 5532: function categoryCheck(form) {
 5533:     if (form.elements['addcategory_name'].value == 'instcode') {
 5534:         alert('$instcode_reserved\\n$choose_again');
 5535:         return false;
 5536:     }
 5537:     if (form.elements['addcategory_name'].value == 'communities') {
 5538:         alert('$communities_reserved\\n$choose_again');
 5539:         return false;
 5540:     }
 5541:     return true;
 5542: }
 5543: 
 5544: // ]]>
 5545: </script>
 5546: 
 5547: ENDSCRIPT
 5548:     return $output;
 5549: }
 5550: 
 5551: sub initialize_categories {
 5552:     my ($itemcount) = @_;
 5553:     my ($datatable,$css_class,$chgstr);
 5554:     my %default_names = (
 5555:                       instcode    => 'Official courses (with institutional codes)',
 5556:                       communities => 'Communities',
 5557:                         );
 5558:     my $select0 = ' selected="selected"';
 5559:     my $select1 = '';
 5560:     foreach my $default ('instcode','communities') {
 5561:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5562:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 5563:         if ($default eq 'communities') {
 5564:             $select1 = $select0;
 5565:             $select0 = '';
 5566:         }
 5567:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5568:                      .'<select name="'.$default.'_pos">'
 5569:                      .'<option value="0"'.$select0.'>1</option>'
 5570:                      .'<option value="1"'.$select1.'>2</option>'
 5571:                      .'<option value="2">3</option></select>&nbsp;'
 5572:                      .$default_names{$default}
 5573:                      .'</span></td><td><span class="LC_nobreak">'
 5574:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 5575:                      .&mt('Display').'</label>&nbsp;<label>'
 5576:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 5577:                  .'</label></span></td></tr>';
 5578:         $itemcount ++;
 5579:     }
 5580:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5581:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 5582:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5583:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 5584:                   .'<option value="0">1</option>'
 5585:                   .'<option value="1">2</option>'
 5586:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 5587:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 5588:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 5589:     return $datatable;
 5590: }
 5591: 
 5592: sub build_category_rows {
 5593:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 5594:     my ($text,$name,$item,$chgstr);
 5595:     if (ref($cats) eq 'ARRAY') {
 5596:         my $maxdepth = scalar(@{$cats});
 5597:         if (ref($cats->[$depth]) eq 'HASH') {
 5598:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 5599:                 my $numchildren = @{$cats->[$depth]{$parent}};
 5600:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5601:                 $text .= '<td><table class="LC_data_table">';
 5602:                 my ($idxnum,$parent_name,$parent_item);
 5603:                 my $higher = $depth - 1;
 5604:                 if ($higher == 0) {
 5605:                     $parent_name = &escape($parent).'::'.$higher;
 5606:                 } else {
 5607:                     if (ref($path) eq 'ARRAY') {
 5608:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5609:                     }
 5610:                 }
 5611:                 $parent_item = 'addcategory_pos_'.$parent_name;
 5612:                 for (my $j=0; $j<=$numchildren; $j++) {
 5613:                     if ($j < $numchildren) {
 5614:                         $name = $cats->[$depth]{$parent}[$j];
 5615:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 5616:                         $idxnum = $idx->{$item};
 5617:                     } else {
 5618:                         $name = $parent_name;
 5619:                         $item = $parent_item;
 5620:                     }
 5621:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 5622:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 5623:                     for (my $i=0; $i<=$numchildren; $i++) {
 5624:                         my $vpos = $i+1;
 5625:                         my $selstr;
 5626:                         if ($j == $i) {
 5627:                             $selstr = ' selected="selected" ';
 5628:                         }
 5629:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 5630:                     }
 5631:                     $text .= '</select>&nbsp;';
 5632:                     if ($j < $numchildren) {
 5633:                         my $deeper = $depth+1;
 5634:                         $text .= $name.'&nbsp;'
 5635:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 5636:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 5637:                         if(ref($path) eq 'ARRAY') {
 5638:                             push(@{$path},$name);
 5639:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 5640:                             pop(@{$path});
 5641:                         }
 5642:                     } else {
 5643:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 5644:                         if ($j == $numchildren) {
 5645:                             $text .= $name;
 5646:                         } else {
 5647:                             $text .= $item;
 5648:                         }
 5649:                         $text .= '" value="" />';
 5650:                     }
 5651:                     $text .= '</td></tr>';
 5652:                 }
 5653:                 $text .= '</table></td>';
 5654:             } else {
 5655:                 my $higher = $depth-1;
 5656:                 if ($higher == 0) {
 5657:                     $name = &escape($parent).'::'.$higher;
 5658:                 } else {
 5659:                     if (ref($path) eq 'ARRAY') {
 5660:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 5661:                     }
 5662:                 }
 5663:                 my $colspan;
 5664:                 if ($parent ne 'instcode') {
 5665:                     $colspan = $maxdepth - $depth - 1;
 5666:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 5667:                 }
 5668:             }
 5669:         }
 5670:     }
 5671:     return $text;
 5672: }
 5673: 
 5674: sub modifiable_userdata_row {
 5675:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 5676:     my ($role,$rolename,$statustype);
 5677:     $role = $item;
 5678:     if ($context eq 'cancreate') {
 5679:         if ($item =~ /^emailusername_(.+)$/) {
 5680:             $statustype = $1;
 5681:             $role = 'emailusername';
 5682:             if (ref($usertypes) eq 'HASH') {
 5683:                 if ($usertypes->{$statustype}) {
 5684:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 5685:                 } else {
 5686:                     $rolename = &mt('Data provided by user');
 5687:                 }
 5688:             }
 5689:         }
 5690:     } elsif ($context eq 'selfcreate') {
 5691:         if (ref($usertypes) eq 'HASH') {
 5692:             $rolename = $usertypes->{$role};
 5693:         } else {
 5694:             $rolename = $role;
 5695:         }
 5696:     } else {
 5697:         if ($role eq 'cr') {
 5698:             $rolename = &mt('Custom role');
 5699:         } else {
 5700:             $rolename = &Apache::lonnet::plaintext($role);
 5701:         }
 5702:     }
 5703:     my (@fields,%fieldtitles);
 5704:     if (ref($fieldsref) eq 'ARRAY') {
 5705:         @fields = @{$fieldsref};
 5706:     } else {
 5707:         @fields = ('lastname','firstname','middlename','generation',
 5708:                    'permanentemail','id');
 5709:     }
 5710:     if ((ref($titlesref) eq 'HASH')) {
 5711:         %fieldtitles = %{$titlesref};
 5712:     } else {
 5713:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5714:     }
 5715:     my $output;
 5716:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 5717:     $output = '<tr '.$css_class.'>'.
 5718:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 5719:               '<td class="LC_left_item" colspan="2"><table>';
 5720:     my $rem;
 5721:     my %checks;
 5722:     if (ref($settings) eq 'HASH') {
 5723:         if (ref($settings->{$context}) eq 'HASH') {
 5724:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 5725:                 my $hashref = $settings->{$context}->{$role};
 5726:                 if ($role eq 'emailusername') {
 5727:                     if ($statustype) {
 5728:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 5729:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 5730:                             if (ref($hashref) eq 'HASH') { 
 5731:                                 foreach my $field (@fields) {
 5732:                                     if ($hashref->{$field}) {
 5733:                                         $checks{$field} = $hashref->{$field};
 5734:                                     }
 5735:                                 }
 5736:                             }
 5737:                         }
 5738:                     }
 5739:                 } else {
 5740:                     if (ref($hashref) eq 'HASH') {
 5741:                         foreach my $field (@fields) {
 5742:                             if ($hashref->{$field}) {
 5743:                                 $checks{$field} = ' checked="checked" ';
 5744:                             }
 5745:                         }
 5746:                     }
 5747:                 }
 5748:             }
 5749:         }
 5750:     }
 5751:      
 5752:     for (my $i=0; $i<@fields; $i++) {
 5753:         my $rem = $i%($numinrow);
 5754:         if ($rem == 0) {
 5755:             if ($i > 0) {
 5756:                 $output .= '</tr>';
 5757:             }
 5758:             $output .= '<tr>';
 5759:         }
 5760:         my $check = ' ';
 5761:         unless ($role eq 'emailusername') {
 5762:             if (exists($checks{$fields[$i]})) {
 5763:                 $check = $checks{$fields[$i]}
 5764:             } else {
 5765:                 if ($role eq 'st') {
 5766:                     if (ref($settings) ne 'HASH') {
 5767:                         $check = ' checked="checked" '; 
 5768:                     }
 5769:                 }
 5770:             }
 5771:         }
 5772:         $output .= '<td class="LC_left_item">'.
 5773:                    '<span class="LC_nobreak">';
 5774:         if ($role eq 'emailusername') {
 5775:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 5776:                 $checks{$fields[$i]} = 'omit';
 5777:             }
 5778:             foreach my $option ('required','optional','omit') {
 5779:                 my $checked='';
 5780:                 if ($checks{$fields[$i]} eq $option) {
 5781:                     $checked='checked="checked" ';
 5782:                 }
 5783:                 $output .= '<label>'.
 5784:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 5785:                            &mt($option).'</label>'.('&nbsp;' x2);
 5786:             }
 5787:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 5788:         } else {
 5789:             $output .= '<label>'.
 5790:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 5791:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 5792:                        '</label>';
 5793:         }
 5794:         $output .= '</span></td>';
 5795:         $rem = @fields%($numinrow);
 5796:     }
 5797:     my $colsleft = $numinrow - $rem;
 5798:     if ($colsleft > 1 ) {
 5799:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5800:                    '&nbsp;</td>';
 5801:     } elsif ($colsleft == 1) {
 5802:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 5803:     }
 5804:     $output .= '</tr></table></td></tr>';
 5805:     return $output;
 5806: }
 5807: 
 5808: sub insttypes_row {
 5809:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 5810:     my %lt = &Apache::lonlocal::texthash (
 5811:                       cansearch => 'Users allowed to search',
 5812:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 5813:                       lockablenames => 'User preference to lock name',
 5814:              );
 5815:     my $showdom;
 5816:     if ($context eq 'cansearch') {
 5817:         $showdom = ' ('.$dom.')';
 5818:     }
 5819:     my $class = 'LC_left_item';
 5820:     if ($context eq 'statustocreate') {
 5821:         $class = 'LC_right_item';
 5822:     }
 5823:     my $css_class = ' class="LC_odd_row"';
 5824:     if ($rownum ne '') { 
 5825:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 5826:     }
 5827:     my $output = '<tr'.$css_class.'>'.
 5828:                  '<td>'.$lt{$context}.$showdom.
 5829:                  '</td><td class="'.$class.'" colspan="2"><table>';
 5830:     my $rem;
 5831:     if (ref($types) eq 'ARRAY') {
 5832:         for (my $i=0; $i<@{$types}; $i++) {
 5833:             if (defined($usertypes->{$types->[$i]})) {
 5834:                 my $rem = $i%($numinrow);
 5835:                 if ($rem == 0) {
 5836:                     if ($i > 0) {
 5837:                         $output .= '</tr>';
 5838:                     }
 5839:                     $output .= '<tr>';
 5840:                 }
 5841:                 my $check = ' ';
 5842:                 if (ref($settings) eq 'HASH') {
 5843:                     if (ref($settings->{$context}) eq 'ARRAY') {
 5844:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 5845:                             $check = ' checked="checked" ';
 5846:                         }
 5847:                     } elsif ($context eq 'statustocreate') {
 5848:                         $check = ' checked="checked" ';
 5849:                     }
 5850:                 }
 5851:                 $output .= '<td class="LC_left_item">'.
 5852:                            '<span class="LC_nobreak"><label>'.
 5853:                            '<input type="checkbox" name="'.$context.'" '.
 5854:                            'value="'.$types->[$i].'"'.$check.'/>'.
 5855:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 5856:             }
 5857:         }
 5858:         $rem = @{$types}%($numinrow);
 5859:     }
 5860:     my $colsleft = $numinrow - $rem;
 5861:     if (($rem == 0) && (@{$types} > 0)) {
 5862:         $output .= '<tr>';
 5863:     }
 5864:     if ($colsleft > 1) {
 5865:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5866:     } else {
 5867:         $output .= '<td class="LC_left_item">';
 5868:     }
 5869:     my $defcheck = ' ';
 5870:     if (ref($settings) eq 'HASH') {  
 5871:         if (ref($settings->{$context}) eq 'ARRAY') {
 5872:             if (grep(/^default$/,@{$settings->{$context}})) {
 5873:                 $defcheck = ' checked="checked" ';
 5874:             }
 5875:         } elsif ($context eq 'statustocreate') {
 5876:             $defcheck = ' checked="checked" ';
 5877:         }
 5878:     }
 5879:     $output .= '<span class="LC_nobreak"><label>'.
 5880:                '<input type="checkbox" name="'.$context.'" '.
 5881:                'value="default"'.$defcheck.'/>'.
 5882:                $othertitle.'</label></span></td>'.
 5883:                '</tr></table></td></tr>';
 5884:     return $output;
 5885: }
 5886: 
 5887: sub sorted_searchtitles {
 5888:     my %searchtitles = &Apache::lonlocal::texthash(
 5889:                          'uname' => 'username',
 5890:                          'lastname' => 'last name',
 5891:                          'lastfirst' => 'last name, first name',
 5892:                      );
 5893:     my @titleorder = ('uname','lastname','lastfirst');
 5894:     return (\%searchtitles,\@titleorder);
 5895: }
 5896: 
 5897: sub sorted_searchtypes {
 5898:     my %srchtypes_desc = (
 5899:                            exact    => 'is exact match',
 5900:                            contains => 'contains ..',
 5901:                            begins   => 'begins with ..',
 5902:                          );
 5903:     my @srchtypeorder = ('exact','begins','contains');
 5904:     return (\%srchtypes_desc,\@srchtypeorder);
 5905: }
 5906: 
 5907: sub usertype_update_row {
 5908:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 5909:     my $datatable;
 5910:     my $numinrow = 4;
 5911:     foreach my $type (@{$types}) {
 5912:         if (defined($usertypes->{$type})) {
 5913:             $$rownums ++;
 5914:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 5915:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 5916:                           '</td><td class="LC_left_item"><table>';
 5917:             for (my $i=0; $i<@{$fields}; $i++) {
 5918:                 my $rem = $i%($numinrow);
 5919:                 if ($rem == 0) {
 5920:                     if ($i > 0) {
 5921:                         $datatable .= '</tr>';
 5922:                     }
 5923:                     $datatable .= '<tr>';
 5924:                 }
 5925:                 my $check = ' ';
 5926:                 if (ref($settings) eq 'HASH') {
 5927:                     if (ref($settings->{'fields'}) eq 'HASH') {
 5928:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 5929:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 5930:                                 $check = ' checked="checked" ';
 5931:                             }
 5932:                         }
 5933:                     }
 5934:                 }
 5935: 
 5936:                 if ($i == @{$fields}-1) {
 5937:                     my $colsleft = $numinrow - $rem;
 5938:                     if ($colsleft > 1) {
 5939:                         $datatable .= '<td colspan="'.$colsleft.'">';
 5940:                     } else {
 5941:                         $datatable .= '<td>';
 5942:                     }
 5943:                 } else {
 5944:                     $datatable .= '<td>';
 5945:                 }
 5946:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5947:                               '<input type="checkbox" name="updateable_'.$type.
 5948:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 5949:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 5950:             }
 5951:             $datatable .= '</tr></table></td></tr>';
 5952:         }
 5953:     }
 5954:     return $datatable;
 5955: }
 5956: 
 5957: sub modify_login {
 5958:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 5959:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 5960:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 5961:     %title = ( coursecatalog => 'Display course catalog',
 5962:                adminmail => 'Display administrator E-mail address',
 5963:                helpdesk  => 'Display "Contact Helpdesk" link',
 5964:                newuser => 'Link for visitors to create a user account',
 5965:                loginheader => 'Log-in box header');
 5966:     @offon = ('off','on');
 5967:     if (ref($domconfig{login}) eq 'HASH') {
 5968:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 5969:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 5970:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 5971:             }
 5972:         }
 5973:     }
 5974:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 5975:                                            \%domconfig,\%loginhash);
 5976:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5977:     foreach my $item (@toggles) {
 5978:         $loginhash{login}{$item} = $env{'form.'.$item};
 5979:     }
 5980:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 5981:     if (ref($colchanges{'login'}) eq 'HASH') {  
 5982:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 5983:                                          \%loginhash);
 5984:     }
 5985: 
 5986:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5987:     my %domservers = &Apache::lonnet::get_servers($dom);
 5988:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 5989:     if (keys(%servers) > 1) {
 5990:         foreach my $lonhost (keys(%servers)) {
 5991:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 5992:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 5993:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 5994:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 5995:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 5996:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5997:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5998:                         $changes{'loginvia'}{$lonhost} = 1;
 5999:                     } else {
 6000:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 6001:                         $changes{'loginvia'}{$lonhost} = 1;
 6002:                     }
 6003:                 } else {
 6004:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 6005:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 6006:                         $changes{'loginvia'}{$lonhost} = 1;
 6007:                     }
 6008:                 }
 6009:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 6010:                     foreach my $item (@loginvia_attribs) {
 6011:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 6012:                     }
 6013:                 } else {
 6014:                     foreach my $item (@loginvia_attribs) {
 6015:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 6016:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 6017:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 6018:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 6019:                                 $new = '/';
 6020:                             }
 6021:                         }
 6022:                         if (($item eq 'custompath') && 
 6023:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 6024:                             $new = '';
 6025:                         }
 6026:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 6027:                             $changes{'loginvia'}{$lonhost} = 1;
 6028:                         }
 6029:                         if ($item eq 'exempt') {
 6030:                             $new = &check_exempt_addresses($new);
 6031:                         }
 6032:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 6033:                     }
 6034:                 }
 6035:             } else {
 6036:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 6037:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 6038:                     $changes{'loginvia'}{$lonhost} = 1;
 6039:                     foreach my $item (@loginvia_attribs) {
 6040:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 6041:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 6042:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 6043:                                 $new = '/';
 6044:                             }
 6045:                         }
 6046:                         if (($item eq 'custompath') && 
 6047:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 6048:                             $new = '';
 6049:                         }
 6050:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 6051:                     }
 6052:                 }
 6053:             }
 6054:         }
 6055:     }
 6056: 
 6057:     my $servadm = $r->dir_config('lonAdmEMail');
 6058:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 6059:     if (ref($domconfig{'login'}) eq 'HASH') {
 6060:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 6061:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 6062:                 if ($lang eq 'nolang') {
 6063:                     push(@currlangs,$lang);
 6064:                 } elsif (defined($langchoices{$lang})) {
 6065:                     push(@currlangs,$lang);
 6066:                 } else {
 6067:                     next;
 6068:                 }
 6069:             }
 6070:         }
 6071:     }
 6072:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 6073:     if (@currlangs > 0) {
 6074:         foreach my $lang (@currlangs) {
 6075:             if (grep(/^\Q$lang\E$/,@delurls)) {
 6076:                 $changes{'helpurl'}{$lang} = 1;
 6077:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 6078:                 $changes{'helpurl'}{$lang} = 1;
 6079:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 6080:                 push(@newlangs,$lang);
 6081:             } else {
 6082:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 6083:             }
 6084:         }
 6085:     }
 6086:     unless (grep(/^nolang$/,@currlangs)) {
 6087:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 6088:             $changes{'helpurl'}{'nolang'} = 1;
 6089:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 6090:             push(@newlangs,'nolang');
 6091:         }
 6092:     }
 6093:     if ($env{'form.loginhelpurl_add_lang'}) {
 6094:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 6095:             ($env{'form.loginhelpurl_add_file.filename'})) {
 6096:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 6097:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 6098:         }
 6099:     }
 6100:     if ((@newlangs > 0) || ($addedfile)) {
 6101:         my $error;
 6102:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6103:         if ($configuserok eq 'ok') {
 6104:             if ($switchserver) {
 6105:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 6106:             } elsif ($author_ok eq 'ok') {
 6107:                 my @allnew = @newlangs;
 6108:                 if ($addedfile ne '') {
 6109:                     push(@allnew,$addedfile);
 6110:                 }
 6111:                 foreach my $lang (@allnew) {
 6112:                     my $formelem = 'loginhelpurl_'.$lang;
 6113:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 6114:                         $formelem = 'loginhelpurl_add_file';
 6115:                     }
 6116:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 6117:                                                                "help/$lang",'','',$newfile{$lang});
 6118:                     if ($result eq 'ok') {
 6119:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 6120:                         $changes{'helpurl'}{$lang} = 1;
 6121:                     } else {
 6122:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 6123:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 6124:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 6125:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 6126: 
 6127:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 6128:                         }
 6129:                     }
 6130:                 }
 6131:             } else {
 6132:                 $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);
 6133:             }
 6134:         } else {
 6135:             $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);
 6136:         }
 6137:         if ($error) {
 6138:             &Apache::lonnet::logthis($error);
 6139:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6140:         }
 6141:     }
 6142: 
 6143:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 6144:     if (ref($domconfig{'login'}) eq 'HASH') {
 6145:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 6146:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 6147:                 if ($domservers{$lonhost}) {
 6148:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 6149:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 6150:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'}
 6151:                     }
 6152:                 }
 6153:             }
 6154:         }
 6155:     }
 6156:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 6157:     foreach my $lonhost (sort(keys(%domservers))) {
 6158:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 6159:             $changes{'headtag'}{$lonhost} = 1;
 6160:         } else {
 6161:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 6162:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 6163:             }
 6164:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 6165:                 push(@newhosts,$lonhost);
 6166:             } elsif ($currheadtagurls{$lonhost}) {
 6167:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 6168:                 if ($currexempt{$lonhost}) {
 6169:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 6170:                         $changes{'headtag'}{$lonhost} = 1;
 6171:                     }
 6172:                 } elsif ($possexempt{$lonhost}) {
 6173:                     $changes{'headtag'}{$lonhost} = 1;
 6174:                 }
 6175:                 if ($possexempt{$lonhost}) {
 6176:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 6177:                 }
 6178:             }
 6179:         }
 6180:     }
 6181:     if (@newhosts) {
 6182:         my $error;
 6183:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6184:         if ($configuserok eq 'ok') {
 6185:             if ($switchserver) {
 6186:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 6187:             } elsif ($author_ok eq 'ok') {
 6188:                 foreach my $lonhost (@newhosts) {
 6189:                     my $formelem = 'loginheadtag_'.$lonhost;
 6190:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 6191:                                                                           "login/headtag/$lonhost",'','',
 6192:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 6193:                     if ($result eq 'ok') {
 6194:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 6195:                         $changes{'headtag'}{$lonhost} = 1;
 6196:                         if ($possexempt{$lonhost}) {
 6197:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 6198:                         }
 6199:                     } else {
 6200:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 6201:                                            $newheadtagurls{$lonhost},$result);
 6202:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 6203:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 6204:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 6205:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 6206:                         }
 6207:                     }
 6208:                 }
 6209:             } else {
 6210:                 $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);
 6211:             }
 6212:         } else {
 6213:             $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);
 6214:         }
 6215:         if ($error) {
 6216:             &Apache::lonnet::logthis($error);
 6217:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6218:         }
 6219:     }
 6220: 
 6221:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 6222: 
 6223:     my $defaulthelpfile = '/adm/loginproblems.html';
 6224:     my $defaulttext = &mt('Default in use');
 6225: 
 6226:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 6227:                                              $dom);
 6228:     if ($putresult eq 'ok') {
 6229:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 6230:         my %defaultchecked = (
 6231:                     'coursecatalog' => 'on',
 6232:                     'helpdesk'      => 'on',
 6233:                     'adminmail'     => 'off',
 6234:                     'newuser'       => 'off',
 6235:         );
 6236:         if (ref($domconfig{'login'}) eq 'HASH') {
 6237:             foreach my $item (@toggles) {
 6238:                 if ($defaultchecked{$item} eq 'on') { 
 6239:                     if (($domconfig{'login'}{$item} eq '0') &&
 6240:                         ($env{'form.'.$item} eq '1')) {
 6241:                         $changes{$item} = 1;
 6242:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 6243:                               $domconfig{'login'}{$item} eq '1') &&
 6244:                              ($env{'form.'.$item} eq '0')) {
 6245:                         $changes{$item} = 1;
 6246:                     }
 6247:                 } elsif ($defaultchecked{$item} eq 'off') {
 6248:                     if (($domconfig{'login'}{$item} eq '1') &&
 6249:                         ($env{'form.'.$item} eq '0')) {
 6250:                         $changes{$item} = 1;
 6251:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 6252:                               $domconfig{'login'}{$item} eq '0') &&
 6253:                              ($env{'form.'.$item} eq '1')) {
 6254:                         $changes{$item} = 1;
 6255:                     }
 6256:                 }
 6257:             }
 6258:         }
 6259:         if (keys(%changes) > 0 || $colchgtext) {
 6260:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6261:             if (ref($lastactref) eq 'HASH') {
 6262:                 $lastactref->{'domainconfig'} = 1;
 6263:             }
 6264:             $resulttext = &mt('Changes made:').'<ul>';
 6265:             foreach my $item (sort(keys(%changes))) {
 6266:                 if ($item eq 'loginvia') {
 6267:                     if (ref($changes{$item}) eq 'HASH') {
 6268:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 6269:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 6270:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 6271:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 6272:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 6273:                                     $protocol = 'http' if ($protocol ne 'https');
 6274:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 6275: 
 6276:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 6277:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 6278:                                     } else {
 6279:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 6280:                                     }
 6281:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 6282:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 6283:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 6284:                                     }
 6285:                                     $resulttext .= '</li>';
 6286:                                 } else {
 6287:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 6288:                                 }
 6289:                             } else {
 6290:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 6291:                             }
 6292:                         }
 6293:                         $resulttext .= '</ul></li>';
 6294:                     }
 6295:                 } elsif ($item eq 'helpurl') {
 6296:                     if (ref($changes{$item}) eq 'HASH') {
 6297:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 6298:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 6299:                                 my ($chg,$link);
 6300:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 6301:                                 if ($lang eq 'nolang') {
 6302:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 6303:                                 } else {
 6304:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 6305:                                 }
 6306:                                 $resulttext .= '<li>'.$chg.'</li>';
 6307:                             } else {
 6308:                                 my $chg;
 6309:                                 if ($lang eq 'nolang') {
 6310:                                     $chg = &mt('custom log-in help file for no preferred language');
 6311:                                 } else {
 6312:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 6313:                                 }
 6314:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 6315:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 6316:                                                       '?inhibitmenu=yes',$chg,600,500).
 6317:                                                '</li>';
 6318:                             }
 6319:                         }
 6320:                     }
 6321:                 } elsif ($item eq 'headtag') {
 6322:                     if (ref($changes{$item}) eq 'HASH') {
 6323:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 6324:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 6325:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 6326:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 6327:                                 $resulttext .= '<li><a href="'.
 6328:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 6329:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 6330:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 6331:                                 if ($possexempt{$lonhost}) {
 6332:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 6333:                                 } else {
 6334:                                     $resulttext .= &mt('included for any client IP');
 6335:                                 }
 6336:                                 $resulttext .= '</li>';
 6337:                             }
 6338:                         }
 6339:                     }
 6340:                 } elsif ($item eq 'captcha') {
 6341:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6342:                         my $chgtxt;
 6343:                         if ($loginhash{'login'}{$item} eq 'notused') {
 6344:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 6345:                         } else {
 6346:                             my %captchas = &captcha_phrases();
 6347:                             if ($captchas{$loginhash{'login'}{$item}}) {
 6348:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 6349:                             } else {
 6350:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 6351:                             }
 6352:                         }
 6353:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6354:                     }
 6355:                 } elsif ($item eq 'recaptchakeys') {
 6356:                     if (ref($loginhash{'login'}) eq 'HASH') {
 6357:                         my ($privkey,$pubkey);
 6358:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 6359:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 6360:                             $privkey = $loginhash{'login'}{$item}{'private'};
 6361:                         }
 6362:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 6363:                         if (!$pubkey) {
 6364:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 6365:                         } else {
 6366:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6367:                         }
 6368:                         if (!$privkey) {
 6369:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 6370:                         } else {
 6371:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 6372:                         }
 6373:                         $chgtxt .= '</ul>';
 6374:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 6375:                     }
 6376:                 } else {
 6377:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 6378:                 }
 6379:             }
 6380:             $resulttext .= $colchgtext.'</ul>';
 6381:         } else {
 6382:             $resulttext = &mt('No changes made to log-in page settings');
 6383:         }
 6384:     } else {
 6385:         $resulttext = '<span class="LC_error">'.
 6386: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6387:     }
 6388:     if ($errors) {
 6389:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 6390:                        $errors.'</ul>';
 6391:     }
 6392:     return $resulttext;
 6393: }
 6394: 
 6395: sub check_exempt_addresses {
 6396:     my ($iplist) = @_;
 6397:     $iplist =~ s/^\s+//;
 6398:     $iplist =~ s/\s+$//;
 6399:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 6400:     my (@okips,$new);
 6401:     foreach my $ip (@poss_ips) {
 6402:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 6403:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 6404:                 push(@okips,$ip);
 6405:             }
 6406:         }
 6407:     }
 6408:     if (@okips > 0) {
 6409:         $new = join(',',@okips);
 6410:     } else {
 6411:         $new = '';
 6412:     }
 6413:     return $new;
 6414: }
 6415: 
 6416: sub color_font_choices {
 6417:     my %choices =
 6418:         &Apache::lonlocal::texthash (
 6419:             img => "Header",
 6420:             bgs => "Background colors",
 6421:             links => "Link colors",
 6422:             images => "Images",
 6423:             font => "Font color",
 6424:             fontmenu => "Font menu",
 6425:             pgbg => "Page",
 6426:             tabbg => "Header",
 6427:             sidebg => "Border",
 6428:             link => "Link",
 6429:             alink => "Active link",
 6430:             vlink => "Visited link",
 6431:         );
 6432:     return %choices;
 6433: }
 6434: 
 6435: sub modify_rolecolors {
 6436:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 6437:     my ($resulttext,%rolehash);
 6438:     $rolehash{'rolecolors'} = {};
 6439:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 6440:         if ($domconfig{'rolecolors'} eq '') {
 6441:             $domconfig{'rolecolors'} = {};
 6442:         }
 6443:     }
 6444:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 6445:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 6446:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 6447:                                              $dom);
 6448:     if ($putresult eq 'ok') {
 6449:         if (keys(%changes) > 0) {
 6450:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6451:             if (ref($lastactref) eq 'HASH') {
 6452:                 $lastactref->{'domainconfig'} = 1;
 6453:             }
 6454:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 6455:                                              $rolehash{'rolecolors'});
 6456:         } else {
 6457:             $resulttext = &mt('No changes made to default color schemes');
 6458:         }
 6459:     } else {
 6460:         $resulttext = '<span class="LC_error">'.
 6461: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6462:     }
 6463:     if ($errors) {
 6464:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 6465:                        $errors.'</ul>';
 6466:     }
 6467:     return $resulttext;
 6468: }
 6469: 
 6470: sub modify_colors {
 6471:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 6472:     my (%changes,%choices);
 6473:     my @bgs;
 6474:     my @links = ('link','alink','vlink');
 6475:     my @logintext;
 6476:     my @images;
 6477:     my $servadm = $r->dir_config('lonAdmEMail');
 6478:     my $errors;
 6479:     my %defaults;
 6480:     foreach my $role (@{$roles}) {
 6481:         if ($role eq 'login') {
 6482:             %choices = &login_choices();
 6483:             @logintext = ('textcol','bgcol');
 6484:         } else {
 6485:             %choices = &color_font_choices();
 6486:         }
 6487:         if ($role eq 'login') {
 6488:             @images = ('img','logo','domlogo','login');
 6489:             @bgs = ('pgbg','mainbg','sidebg');
 6490:         } else {
 6491:             @images = ('img');
 6492:             @bgs = ('pgbg','tabbg','sidebg');
 6493:         }
 6494:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 6495:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 6496:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 6497:         }
 6498:         if ($role eq 'login') {
 6499:             foreach my $item (@logintext) {
 6500:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6501:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6502:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6503:                 }
 6504:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 6505:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6506:                 }
 6507:             }
 6508:         } else {
 6509:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 6510:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 6511:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 6512:             }
 6513:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 6514:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 6515:             }
 6516:         }
 6517:         foreach my $item (@bgs) {
 6518:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6519:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6520:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6521:             }
 6522:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 6523:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6524:             }
 6525:         }
 6526:         foreach my $item (@links) {
 6527:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 6528:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 6529:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 6530:             }
 6531:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 6532:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 6533:             }
 6534:         }
 6535:         my ($configuserok,$author_ok,$switchserver) = 
 6536:             &config_check($dom,$confname,$servadm);
 6537:         my ($width,$height) = &thumb_dimensions();
 6538:         if (ref($domconfig->{$role}) ne 'HASH') {
 6539:             $domconfig->{$role} = {};
 6540:         }
 6541:         foreach my $img (@images) {
 6542:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 6543:                 if (defined($env{'form.login_showlogo_'.$img})) {
 6544:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 6545:                 } else { 
 6546:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 6547:                 }
 6548:             } 
 6549: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 6550: 		 && !defined($domconfig->{$role}{$img})
 6551: 		 && !$env{'form.'.$role.'_del_'.$img}
 6552: 		 && $env{'form.'.$role.'_import_'.$img}) {
 6553: 		# import the old configured image from the .tab setting
 6554: 		# if they haven't provided a new one 
 6555: 		$domconfig->{$role}{$img} = 
 6556: 		    $env{'form.'.$role.'_import_'.$img};
 6557: 	    }
 6558:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 6559:                 my $error;
 6560:                 if ($configuserok eq 'ok') {
 6561:                     if ($switchserver) {
 6562:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 6563:                     } else {
 6564:                         if ($author_ok eq 'ok') {
 6565:                             my ($result,$logourl) = 
 6566:                                 &publishlogo($r,'upload',$role.'_'.$img,
 6567:                                            $dom,$confname,$img,$width,$height);
 6568:                             if ($result eq 'ok') {
 6569:                                 $confhash->{$role}{$img} = $logourl;
 6570:                                 $changes{$role}{'images'}{$img} = 1;
 6571:                             } else {
 6572:                                 $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);
 6573:                             }
 6574:                         } else {
 6575:                             $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);
 6576:                         }
 6577:                     }
 6578:                 } else {
 6579:                     $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);
 6580:                 }
 6581:                 if ($error) {
 6582:                     &Apache::lonnet::logthis($error);
 6583:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6584:                 }
 6585:             } elsif ($domconfig->{$role}{$img} ne '') {
 6586:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 6587:                     my $error;
 6588:                     if ($configuserok eq 'ok') {
 6589: # is confname an author?
 6590:                         if ($switchserver eq '') {
 6591:                             if ($author_ok eq 'ok') {
 6592:                                 my ($result,$logourl) = 
 6593:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 6594:                                             $dom,$confname,$img,$width,$height);
 6595:                                 if ($result eq 'ok') {
 6596:                                     $confhash->{$role}{$img} = $logourl;
 6597: 				    $changes{$role}{'images'}{$img} = 1;
 6598:                                 }
 6599:                             }
 6600:                         }
 6601:                     }
 6602:                 }
 6603:             }
 6604:         }
 6605:         if (ref($domconfig) eq 'HASH') {
 6606:             if (ref($domconfig->{$role}) eq 'HASH') {
 6607:                 foreach my $img (@images) {
 6608:                     if ($domconfig->{$role}{$img} ne '') {
 6609:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6610:                             $confhash->{$role}{$img} = '';
 6611:                             $changes{$role}{'images'}{$img} = 1;
 6612:                         } else {
 6613:                             if ($confhash->{$role}{$img} eq '') {
 6614:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 6615:                             }
 6616:                         }
 6617:                     } else {
 6618:                         if ($env{'form.'.$role.'_del_'.$img}) {
 6619:                             $confhash->{$role}{$img} = '';
 6620:                             $changes{$role}{'images'}{$img} = 1;
 6621:                         } 
 6622:                     }
 6623:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 6624:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 6625:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 6626:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 6627:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 6628:                             }
 6629:                         } else {
 6630:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6631:                                 $changes{$role}{'showlogo'}{$img} = 1;
 6632:                             }
 6633:                         }
 6634:                     }
 6635:                 }
 6636:                 if ($domconfig->{$role}{'font'} ne '') {
 6637:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 6638:                         $changes{$role}{'font'} = 1;
 6639:                     }
 6640:                 } else {
 6641:                     if ($confhash->{$role}{'font'}) {
 6642:                         $changes{$role}{'font'} = 1;
 6643:                     }
 6644:                 }
 6645:                 if ($role ne 'login') {
 6646:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 6647:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 6648:                             $changes{$role}{'fontmenu'} = 1;
 6649:                         }
 6650:                     } else {
 6651:                         if ($confhash->{$role}{'fontmenu'}) {
 6652:                             $changes{$role}{'fontmenu'} = 1;
 6653:                         }
 6654:                     }
 6655:                 }
 6656:                 foreach my $item (@bgs) {
 6657:                     if ($domconfig->{$role}{$item} ne '') {
 6658:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6659:                             $changes{$role}{'bgs'}{$item} = 1;
 6660:                         } 
 6661:                     } else {
 6662:                         if ($confhash->{$role}{$item}) {
 6663:                             $changes{$role}{'bgs'}{$item} = 1;
 6664:                         }
 6665:                     }
 6666:                 }
 6667:                 foreach my $item (@links) {
 6668:                     if ($domconfig->{$role}{$item} ne '') {
 6669:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6670:                             $changes{$role}{'links'}{$item} = 1;
 6671:                         }
 6672:                     } else {
 6673:                         if ($confhash->{$role}{$item}) {
 6674:                             $changes{$role}{'links'}{$item} = 1;
 6675:                         }
 6676:                     }
 6677:                 }
 6678:                 foreach my $item (@logintext) {
 6679:                     if ($domconfig->{$role}{$item} ne '') {
 6680:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 6681:                             $changes{$role}{'logintext'}{$item} = 1;
 6682:                         }
 6683:                     } else {
 6684:                         if ($confhash->{$role}{$item}) {
 6685:                             $changes{$role}{'logintext'}{$item} = 1;
 6686:                         }
 6687:                     }
 6688:                 }
 6689:             } else {
 6690:                 &default_change_checker($role,\@images,\@links,\@bgs,
 6691:                                         \@logintext,$confhash,\%changes); 
 6692:             }
 6693:         } else {
 6694:             &default_change_checker($role,\@images,\@links,\@bgs,
 6695:                                     \@logintext,$confhash,\%changes); 
 6696:         }
 6697:     }
 6698:     return ($errors,%changes);
 6699: }
 6700: 
 6701: sub config_check {
 6702:     my ($dom,$confname,$servadm) = @_;
 6703:     my ($configuserok,$author_ok,$switchserver,%currroles);
 6704:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 6705:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 6706:                                                    $confname,$servadm);
 6707:     if ($configuserok eq 'ok') {
 6708:         $switchserver = &check_switchserver($dom,$confname);
 6709:         if ($switchserver eq '') {
 6710:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 6711:         }
 6712:     }
 6713:     return ($configuserok,$author_ok,$switchserver);
 6714: }
 6715: 
 6716: sub default_change_checker {
 6717:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 6718:     foreach my $item (@{$links}) {
 6719:         if ($confhash->{$role}{$item}) {
 6720:             $changes->{$role}{'links'}{$item} = 1;
 6721:         }
 6722:     }
 6723:     foreach my $item (@{$bgs}) {
 6724:         if ($confhash->{$role}{$item}) {
 6725:             $changes->{$role}{'bgs'}{$item} = 1;
 6726:         }
 6727:     }
 6728:     foreach my $item (@{$logintext}) {
 6729:         if ($confhash->{$role}{$item}) {
 6730:             $changes->{$role}{'logintext'}{$item} = 1;
 6731:         }
 6732:     }
 6733:     foreach my $img (@{$images}) {
 6734:         if ($env{'form.'.$role.'_del_'.$img}) {
 6735:             $confhash->{$role}{$img} = '';
 6736:             $changes->{$role}{'images'}{$img} = 1;
 6737:         }
 6738:         if ($role eq 'login') {
 6739:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 6740:                 $changes->{$role}{'showlogo'}{$img} = 1;
 6741:             }
 6742:         }
 6743:     }
 6744:     if ($confhash->{$role}{'font'}) {
 6745:         $changes->{$role}{'font'} = 1;
 6746:     }
 6747: }
 6748: 
 6749: sub display_colorchgs {
 6750:     my ($dom,$changes,$roles,$confhash) = @_;
 6751:     my (%choices,$resulttext);
 6752:     if (!grep(/^login$/,@{$roles})) {
 6753:         $resulttext = &mt('Changes made:').'<br />';
 6754:     }
 6755:     foreach my $role (@{$roles}) {
 6756:         if ($role eq 'login') {
 6757:             %choices = &login_choices();
 6758:         } else {
 6759:             %choices = &color_font_choices();
 6760:         }
 6761:         if (ref($changes->{$role}) eq 'HASH') {
 6762:             if ($role ne 'login') {
 6763:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 6764:             }
 6765:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 6766:                 if ($role ne 'login') {
 6767:                     $resulttext .= '<ul>';
 6768:                 }
 6769:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 6770:                     if ($role ne 'login') {
 6771:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 6772:                     }
 6773:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 6774:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 6775:                             if ($confhash->{$role}{$key}{$item}) {
 6776:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 6777:                             } else {
 6778:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 6779:                             }
 6780:                         } elsif ($confhash->{$role}{$item} eq '') {
 6781:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 6782:                         } else {
 6783:                             my $newitem = $confhash->{$role}{$item};
 6784:                             if ($key eq 'images') {
 6785:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 6786:                             }
 6787:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 6788:                         }
 6789:                     }
 6790:                     if ($role ne 'login') {
 6791:                         $resulttext .= '</ul></li>';
 6792:                     }
 6793:                 } else {
 6794:                     if ($confhash->{$role}{$key} eq '') {
 6795:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 6796:                     } else {
 6797:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 6798:                     }
 6799:                 }
 6800:                 if ($role ne 'login') {
 6801:                     $resulttext .= '</ul>';
 6802:                 }
 6803:             }
 6804:         }
 6805:     }
 6806:     return $resulttext;
 6807: }
 6808: 
 6809: sub thumb_dimensions {
 6810:     return ('200','50');
 6811: }
 6812: 
 6813: sub check_dimensions {
 6814:     my ($inputfile) = @_;
 6815:     my ($fullwidth,$fullheight);
 6816:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 6817:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 6818:             my $imageinfo = <PIPE>;
 6819:             if (!close(PIPE)) {
 6820:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 6821:             }
 6822:             chomp($imageinfo);
 6823:             my ($fullsize) = 
 6824:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 6825:             if ($fullsize) {
 6826:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 6827:             }
 6828:         }
 6829:     }
 6830:     return ($fullwidth,$fullheight);
 6831: }
 6832: 
 6833: sub check_configuser {
 6834:     my ($uhome,$dom,$confname,$servadm) = @_;
 6835:     my ($configuserok,%currroles);
 6836:     if ($uhome eq 'no_host') {
 6837:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 6838:         my $configpass = &LONCAPA::Enrollment::create_password();
 6839:         $configuserok = 
 6840:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 6841:                              $configpass,'','','','','',undef,$servadm);
 6842:     } else {
 6843:         $configuserok = 'ok';
 6844:         %currroles = 
 6845:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 6846:     }
 6847:     return ($configuserok,%currroles);
 6848: }
 6849: 
 6850: sub check_authorstatus {
 6851:     my ($dom,$confname,%currroles) = @_;
 6852:     my $author_ok;
 6853:     if (!$currroles{':'.$dom.':au'}) {
 6854:         my $start = time;
 6855:         my $end = 0;
 6856:         $author_ok = 
 6857:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 6858:                                         'au',$end,$start,'','','domconfig');
 6859:     } else {
 6860:         $author_ok = 'ok';
 6861:     }
 6862:     return $author_ok;
 6863: }
 6864: 
 6865: sub publishlogo {
 6866:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 6867:     my ($output,$fname,$logourl);
 6868:     if ($action eq 'upload') {
 6869:         $fname=$env{'form.'.$formname.'.filename'};
 6870:         chop($env{'form.'.$formname});
 6871:     } else {
 6872:         ($fname) = ($formname =~ /([^\/]+)$/);
 6873:     }
 6874:     if ($savefileas ne '') {
 6875:         $fname = $savefileas;
 6876:     }
 6877:     $fname=&Apache::lonnet::clean_filename($fname);
 6878: # See if there is anything left
 6879:     unless ($fname) { return ('error: no uploaded file'); }
 6880:     $fname="$subdir/$fname";
 6881:     my $docroot=$r->dir_config('lonDocRoot');
 6882:     my $filepath="$docroot/priv";
 6883:     my $relpath = "$dom/$confname";
 6884:     my ($fnamepath,$file,$fetchthumb);
 6885:     $file=$fname;
 6886:     if ($fname=~m|/|) {
 6887:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 6888:     }
 6889:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 6890:     my $count;
 6891:     for ($count=5;$count<=$#parts;$count++) {
 6892:         $filepath.="/$parts[$count]";
 6893:         if ((-e $filepath)!=1) {
 6894:             mkdir($filepath,02770);
 6895:         }
 6896:     }
 6897:     # Check for bad extension and disallow upload
 6898:     if ($file=~/\.(\w+)$/ &&
 6899:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 6900:         $output = 
 6901:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 6902:     } elsif ($file=~/\.(\w+)$/ &&
 6903:         !defined(&Apache::loncommon::fileembstyle($1))) {
 6904:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 6905:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 6906:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 6907:     } elsif (-d "$filepath/$file") {
 6908:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 6909:     } else {
 6910:         my $source = $filepath.'/'.$file;
 6911:         my $logfile;
 6912:         if (!open($logfile,">>$source".'.log')) {
 6913:             return (&mt('No write permission to Authoring Space'));
 6914:         }
 6915:         print $logfile
 6916: "\n================= Publish ".localtime()." ================\n".
 6917: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 6918: # Save the file
 6919:         if (!open(FH,'>'.$source)) {
 6920:             &Apache::lonnet::logthis('Failed to create '.$source);
 6921:             return (&mt('Failed to create file'));
 6922:         }
 6923:         if ($action eq 'upload') {
 6924:             if (!print FH ($env{'form.'.$formname})) {
 6925:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 6926:                 return (&mt('Failed to write file'));
 6927:             }
 6928:         } else {
 6929:             my $original = &Apache::lonnet::filelocation('',$formname);
 6930:             if(!copy($original,$source)) {
 6931:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 6932:                 return (&mt('Failed to write file'));
 6933:             }
 6934:         }
 6935:         close(FH);
 6936:         chmod(0660, $source); # Permissions to rw-rw---.
 6937: 
 6938:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 6939:         my $copyfile=$targetdir.'/'.$file;
 6940: 
 6941:         my @parts=split(/\//,$targetdir);
 6942:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 6943:         for (my $count=5;$count<=$#parts;$count++) {
 6944:             $path.="/$parts[$count]";
 6945:             if (!-e $path) {
 6946:                 print $logfile "\nCreating directory ".$path;
 6947:                 mkdir($path,02770);
 6948:             }
 6949:         }
 6950:         my $versionresult;
 6951:         if (-e $copyfile) {
 6952:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 6953:         } else {
 6954:             $versionresult = 'ok';
 6955:         }
 6956:         if ($versionresult eq 'ok') {
 6957:             if (copy($source,$copyfile)) {
 6958:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 6959:                 $output = 'ok';
 6960:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 6961:                 push(@{$modified_urls},[$copyfile,$source]);
 6962:                 my $metaoutput = 
 6963:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 6964:                 unless ($registered_cleanup) {
 6965:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6966:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6967:                     $registered_cleanup=1;
 6968:                 }
 6969:             } else {
 6970:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 6971:                 $output = &mt('Failed to copy file to RES space').", $!";
 6972:             }
 6973:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 6974:                 my $inputfile = $filepath.'/'.$file;
 6975:                 my $outfile = $filepath.'/'.'tn-'.$file;
 6976:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 6977:                 if ($fullwidth ne '' && $fullheight ne '') { 
 6978:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 6979:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 6980:                         system("convert -sample $thumbsize $inputfile $outfile");
 6981:                         chmod(0660, $filepath.'/tn-'.$file);
 6982:                         if (-e $outfile) {
 6983:                             my $copyfile=$targetdir.'/tn-'.$file;
 6984:                             if (copy($outfile,$copyfile)) {
 6985:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 6986:                                 my $thumb_metaoutput = 
 6987:                                     &write_metadata($dom,$confname,$formname,
 6988:                                                     $targetdir,'tn-'.$file,$logfile);
 6989:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 6990:                                 unless ($registered_cleanup) {
 6991:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6992:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6993:                                     $registered_cleanup=1;
 6994:                                 }
 6995:                             } else {
 6996:                                 print $logfile "\nUnable to write ".$copyfile.
 6997:                                                ':'.$!."\n";
 6998:                             }
 6999:                         }
 7000:                     }
 7001:                 }
 7002:             }
 7003:         } else {
 7004:             $output = $versionresult;
 7005:         }
 7006:     }
 7007:     return ($output,$logourl);
 7008: }
 7009: 
 7010: sub logo_versioning {
 7011:     my ($targetdir,$file,$logfile) = @_;
 7012:     my $target = $targetdir.'/'.$file;
 7013:     my ($maxversion,$fn,$extn,$output);
 7014:     $maxversion = 0;
 7015:     if ($file =~ /^(.+)\.(\w+)$/) {
 7016:         $fn=$1;
 7017:         $extn=$2;
 7018:     }
 7019:     opendir(DIR,$targetdir);
 7020:     while (my $filename=readdir(DIR)) {
 7021:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 7022:             $maxversion=($1>$maxversion)?$1:$maxversion;
 7023:         }
 7024:     }
 7025:     $maxversion++;
 7026:     print $logfile "\nCreating old version ".$maxversion."\n";
 7027:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 7028:     if (copy($target,$copyfile)) {
 7029:         print $logfile "Copied old target to ".$copyfile."\n";
 7030:         $copyfile=$copyfile.'.meta';
 7031:         if (copy($target.'.meta',$copyfile)) {
 7032:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 7033:             $output = 'ok';
 7034:         } else {
 7035:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 7036:             $output = &mt('Failed to copy old meta').", $!, ";
 7037:         }
 7038:     } else {
 7039:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 7040:         $output = &mt('Failed to copy old target').", $!, ";
 7041:     }
 7042:     return $output;
 7043: }
 7044: 
 7045: sub write_metadata {
 7046:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 7047:     my (%metadatafields,%metadatakeys,$output);
 7048:     $metadatafields{'title'}=$formname;
 7049:     $metadatafields{'creationdate'}=time;
 7050:     $metadatafields{'lastrevisiondate'}=time;
 7051:     $metadatafields{'copyright'}='public';
 7052:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 7053:                                          $env{'user.domain'};
 7054:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 7055:     $metadatafields{'domain'}=$dom;
 7056:     {
 7057:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 7058:         my $mfh;
 7059:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 7060:             foreach (sort(keys(%metadatafields))) {
 7061:                 unless ($_=~/\./) {
 7062:                     my $unikey=$_;
 7063:                     $unikey=~/^([A-Za-z]+)/;
 7064:                     my $tag=$1;
 7065:                     $tag=~tr/A-Z/a-z/;
 7066:                     print $mfh "\n\<$tag";
 7067:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 7068:                         my $value=$metadatafields{$unikey.'.'.$_};
 7069:                         $value=~s/\"/\'\'/g;
 7070:                         print $mfh ' '.$_.'="'.$value.'"';
 7071:                     }
 7072:                     print $mfh '>'.
 7073:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 7074:                             .'</'.$tag.'>';
 7075:                 }
 7076:             }
 7077:             $output = 'ok';
 7078:             print $logfile "\nWrote metadata";
 7079:             close($mfh);
 7080:         } else {
 7081:             print $logfile "\nFailed to open metadata file";
 7082:             $output = &mt('Could not write metadata');
 7083:         }
 7084:     }
 7085:     return $output;
 7086: }
 7087: 
 7088: sub notifysubscribed {
 7089:     foreach my $targetsource (@{$modified_urls}){
 7090:         next unless (ref($targetsource) eq 'ARRAY');
 7091:         my ($target,$source)=@{$targetsource};
 7092:         if ($source ne '') {
 7093:             if (open(my $logfh,'>>'.$source.'.log')) {
 7094:                 print $logfh "\nCleanup phase: Notifications\n";
 7095:                 my @subscribed=&subscribed_hosts($target);
 7096:                 foreach my $subhost (@subscribed) {
 7097:                     print $logfh "\nNotifying host ".$subhost.':';
 7098:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 7099:                     print $logfh $reply;
 7100:                 }
 7101:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 7102:                 foreach my $subhost (@subscribedmeta) {
 7103:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 7104:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 7105:                                                         $subhost);
 7106:                     print $logfh $reply;
 7107:                 }
 7108:                 print $logfh "\n============ Done ============\n";
 7109:                 close($logfh);
 7110:             }
 7111:         }
 7112:     }
 7113:     return OK;
 7114: }
 7115: 
 7116: sub subscribed_hosts {
 7117:     my ($target) = @_;
 7118:     my @subscribed;
 7119:     if (open(my $fh,"<$target.subscription")) {
 7120:         while (my $subline=<$fh>) {
 7121:             if ($subline =~ /^($match_lonid):/) {
 7122:                 my $host = $1;
 7123:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 7124:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 7125:                         push(@subscribed,$host);
 7126:                     }
 7127:                 }
 7128:             }
 7129:         }
 7130:     }
 7131:     return @subscribed;
 7132: }
 7133: 
 7134: sub check_switchserver {
 7135:     my ($dom,$confname) = @_;
 7136:     my ($allowed,$switchserver);
 7137:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 7138:     if ($home eq 'no_host') {
 7139:         $home = &Apache::lonnet::domain($dom,'primary');
 7140:     }
 7141:     my @ids=&Apache::lonnet::current_machine_ids();
 7142:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 7143:     if (!$allowed) {
 7144: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 7145:     }
 7146:     return $switchserver;
 7147: }
 7148: 
 7149: sub modify_quotas {
 7150:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 7151:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 7152:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 7153:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 7154:         $validationfieldsref);
 7155:     if ($action eq 'quotas') {
 7156:         $context = 'tools'; 
 7157:     } else {
 7158:         $context = $action;
 7159:     }
 7160:     if ($context eq 'requestcourses') {
 7161:         @usertools = ('official','unofficial','community','textbook');
 7162:         @options =('norequest','approval','validate','autolimit');
 7163:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 7164:         %titles = &courserequest_titles();
 7165:         $toolregexp = join('|',@usertools);
 7166:         %conditions = &courserequest_conditions();
 7167:         $confname = $dom.'-domainconfig';
 7168:         my $servadm = $r->dir_config('lonAdmEMail');
 7169:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7170:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 7171:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 7172:     } elsif ($context eq 'requestauthor') {
 7173:         @usertools = ('author');
 7174:         %titles = &authorrequest_titles();
 7175:     } else {
 7176:         @usertools = ('aboutme','blog','webdav','portfolio');
 7177:         %titles = &tool_titles();
 7178:     }
 7179:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 7180:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7181:     foreach my $key (keys(%env)) {
 7182:         if ($context eq 'requestcourses') {
 7183:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 7184:                 my $item = $1;
 7185:                 my $type = $2;
 7186:                 if ($type =~ /^limit_(.+)/) {
 7187:                     $limithash{$item}{$1} = $env{$key};
 7188:                 } else {
 7189:                     $confhash{$item}{$type} = $env{$key};
 7190:                 }
 7191:             }
 7192:         } elsif ($context eq 'requestauthor') {
 7193:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 7194:                 $confhash{$1} = $env{$key};
 7195:             }
 7196:         } else {
 7197:             if ($key =~ /^form\.quota_(.+)$/) {
 7198:                 $confhash{'defaultquota'}{$1} = $env{$key};
 7199:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 7200:                 $confhash{'authorquota'}{$1} = $env{$key};
 7201:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 7202:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 7203:             }
 7204:         }
 7205:     }
 7206:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 7207:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 7208:         @approvalnotify = sort(@approvalnotify);
 7209:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 7210:         my @crstypes = ('official','unofficial','community','textbook');
 7211:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 7212:         foreach my $type (@hasuniquecode) {
 7213:             if (grep(/^\Q$type\E$/,@crstypes)) {
 7214:                 $confhash{'uniquecode'}{$type} = 1;
 7215:             }
 7216:         }
 7217:         my (%newbook,%allpos);
 7218:         if ($context eq 'requestcourses') {
 7219:             foreach my $type ('textbooks','templates') {
 7220:                 @{$allpos{$type}} = (); 
 7221:                 my $invalid;
 7222:                 if ($type eq 'textbooks') {
 7223:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 7224:                 } else {
 7225:                     $invalid = &mt('Invalid LON-CAPA course for template');
 7226:                 }
 7227:                 if ($env{'form.'.$type.'_addbook'}) {
 7228:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 7229:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 7230:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 7231:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 7232:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 7233:                         } else {
 7234:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 7235:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 7236:                             $position =~ s/\D+//g;
 7237:                             if ($position ne '') {
 7238:                                 $allpos{$type}[$position] = $newbook{$type};
 7239:                             }
 7240:                         }
 7241:                     } else {
 7242:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 7243:                     }
 7244:                 }
 7245:             } 
 7246:         }
 7247:         if (ref($domconfig{$action}) eq 'HASH') {
 7248:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 7249:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 7250:                     $changes{'notify'}{'approval'} = 1;
 7251:                 }
 7252:             } else {
 7253:                 if ($confhash{'notify'}{'approval'}) {
 7254:                     $changes{'notify'}{'approval'} = 1;
 7255:                 }
 7256:             }
 7257:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 7258:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 7259:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 7260:                         unless ($confhash{'uniquecode'}{$crstype}) {
 7261:                             $changes{'uniquecode'} = 1;
 7262:                         }
 7263:                     }
 7264:                     unless ($changes{'uniquecode'}) {
 7265:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 7266:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 7267:                                 $changes{'uniquecode'} = 1;
 7268:                             }
 7269:                         }
 7270:                     }
 7271:                } else {
 7272:                    $changes{'uniquecode'} = 1;
 7273:                }
 7274:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 7275:                 $changes{'uniquecode'} = 1;
 7276:             }
 7277:             if ($context eq 'requestcourses') {
 7278:                 foreach my $type ('textbooks','templates') {
 7279:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 7280:                         my %deletions;
 7281:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 7282:                         if (@todelete) {
 7283:                             map { $deletions{$_} = 1; } @todelete;
 7284:                         }
 7285:                         my %imgdeletions;
 7286:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 7287:                         if (@todeleteimages) {
 7288:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 7289:                         }
 7290:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 7291:                         for (my $i=0; $i<=$maxnum; $i++) {
 7292:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 7293:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 7294:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 7295:                                 if ($deletions{$key}) {
 7296:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 7297:                                         #FIXME need to obsolete item in RES space
 7298:                                     }
 7299:                                     next;
 7300:                                 } else {
 7301:                                     my $newpos = $env{'form.'.$itemid};
 7302:                                     $newpos =~ s/\D+//g;
 7303:                                     foreach my $item ('subject','title','publisher','author') {
 7304:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7305:                                                  ($type eq 'templates'));
 7306:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 7307:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 7308:                                             $changes{$type}{$key} = 1;
 7309:                                         }
 7310:                                     }
 7311:                                     $allpos{$type}[$newpos] = $key;
 7312:                                 }
 7313:                                 if ($imgdeletions{$key}) {
 7314:                                     $changes{$type}{$key} = 1;
 7315:                                     #FIXME need to obsolete item in RES space
 7316:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 7317:                                     my ($cdom,$cnum) = split(/_/,$key);
 7318:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 7319:                                                                                   $cdom,$cnum,$type,$configuserok,
 7320:                                                                                   $switchserver,$author_ok);
 7321:                                     if ($imgurl) {
 7322:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 7323:                                         $changes{$type}{$key} = 1; 
 7324:                                     }
 7325:                                     if ($error) {
 7326:                                         &Apache::lonnet::logthis($error);
 7327:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7328:                                     } 
 7329:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 7330:                                     $confhash{$type}{$key}{'image'} = 
 7331:                                         $domconfig{$action}{$type}{$key}{'image'};
 7332:                                 }
 7333:                             }
 7334:                         }
 7335:                     }
 7336:                 }
 7337:             }
 7338:         } else {
 7339:             if ($confhash{'notify'}{'approval'}) {
 7340:                 $changes{'notify'}{'approval'} = 1;
 7341:             }
 7342:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 7343:                 $changes{'uniquecode'} = 1;
 7344:             }
 7345:         }
 7346:         if ($context eq 'requestcourses') {
 7347:             foreach my $type ('textbooks','templates') {
 7348:                 if ($newbook{$type}) {
 7349:                     $changes{$type}{$newbook{$type}} = 1;
 7350:                     foreach my $item ('subject','title','publisher','author') {
 7351:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7352:                                  ($type eq 'template'));
 7353:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 7354:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 7355:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 7356:                         }
 7357:                     }
 7358:                     if ($type eq 'textbooks') {
 7359:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 7360:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 7361:                             my ($imageurl,$error) =
 7362:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 7363:                                                         $configuserok,$switchserver,$author_ok);
 7364:                             if ($imageurl) {
 7365:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 7366:                             }
 7367:                             if ($error) {
 7368:                                 &Apache::lonnet::logthis($error);
 7369:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7370:                             }
 7371:                         }
 7372:                     }
 7373:                 }
 7374:                 if (@{$allpos{$type}} > 0) {
 7375:                     my $idx = 0;
 7376:                     foreach my $item (@{$allpos{$type}}) {
 7377:                         if ($item ne '') {
 7378:                             $confhash{$type}{$item}{'order'} = $idx;
 7379:                             if (ref($domconfig{$action}) eq 'HASH') {
 7380:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 7381:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 7382:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 7383:                                             $changes{$type}{$item} = 1;
 7384:                                         }
 7385:                                     }
 7386:                                 }
 7387:                             }
 7388:                             $idx ++;
 7389:                         }
 7390:                     }
 7391:                 }
 7392:             }
 7393:             if (ref($validationitemsref) eq 'ARRAY') {
 7394:                 foreach my $item (@{$validationitemsref}) {
 7395:                     if ($item eq 'fields') {
 7396:                         my @changed;
 7397:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 7398:                         if (@{$confhash{'validation'}{$item}} > 0) {
 7399:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 7400:                         }
 7401:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 7402:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7403:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 7404:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 7405:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 7406:                                 } else {
 7407:                                     @changed = @{$confhash{'validation'}{$item}};
 7408:                                 }
 7409:                             } else {
 7410:                                 @changed = @{$confhash{'validation'}{$item}};
 7411:                             }
 7412:                         } else {
 7413:                             @changed = @{$confhash{'validation'}{$item}};
 7414:                         }
 7415:                         if (@changed) {
 7416:                             if ($confhash{'validation'}{$item}) {
 7417:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 7418:                             } else {
 7419:                                 $changes{'validation'}{$item} = &mt('None');
 7420:                             }
 7421:                         }
 7422:                     } else {
 7423:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 7424:                         if ($item eq 'markup') {
 7425:                             if ($env{'form.requestcourses_validation_'.$item}) {
 7426:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 7427:                             }
 7428:                         }
 7429:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 7430:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7431:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 7432:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7433:                                 }
 7434:                             } else {
 7435:                                 if ($confhash{'validation'}{$item} ne '') {
 7436:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7437:                                 }
 7438:                             }
 7439:                         } else {
 7440:                             if ($confhash{'validation'}{$item} ne '') {
 7441:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 7442:                             }
 7443:                         }
 7444:                     }
 7445:                 }
 7446:             }
 7447:             if ($env{'form.validationdc'}) {
 7448:                 my $newval = $env{'form.validationdc'};
 7449:                 my %domcoords = &get_active_dcs($dom);
 7450:                 if (exists($domcoords{$newval})) {
 7451:                     $confhash{'validation'}{'dc'} = $newval;
 7452:                 }
 7453:             }
 7454:             if (ref($confhash{'validation'}) eq 'HASH') {
 7455:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 7456:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7457:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7458:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7459:                                 if ($confhash{'validation'}{'dc'} eq '') {
 7460:                                     $changes{'validation'}{'dc'} = &mt('None');
 7461:                                 } else {
 7462:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7463:                                 }
 7464:                             }
 7465:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 7466:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7467:                         }
 7468:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 7469:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7470:                     }
 7471:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 7472:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 7473:                 }
 7474:             } else {
 7475:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 7476:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 7477:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 7478:                             $changes{'validation'}{'dc'} = &mt('None');
 7479:                         }
 7480:                     }
 7481:                 }
 7482:             }
 7483:         }
 7484:     } else {
 7485:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 7486:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 7487:     }
 7488:     foreach my $item (@usertools) {
 7489:         foreach my $type (@{$types},'default','_LC_adv') {
 7490:             my $unset; 
 7491:             if ($context eq 'requestcourses') {
 7492:                 $unset = '0';
 7493:                 if ($type eq '_LC_adv') {
 7494:                     $unset = '';
 7495:                 }
 7496:                 if ($confhash{$item}{$type} eq 'autolimit') {
 7497:                     $confhash{$item}{$type} .= '=';
 7498:                     unless ($limithash{$item}{$type} =~ /\D/) {
 7499:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 7500:                     }
 7501:                 }
 7502:             } elsif ($context eq 'requestauthor') {
 7503:                 $unset = '0';
 7504:                 if ($type eq '_LC_adv') {
 7505:                     $unset = '';
 7506:                 }
 7507:             } else {
 7508:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 7509:                     $confhash{$item}{$type} = 1;
 7510:                 } else {
 7511:                     $confhash{$item}{$type} = 0;
 7512:                 }
 7513:             }
 7514:             if (ref($domconfig{$action}) eq 'HASH') {
 7515:                 if ($action eq 'requestauthor') {
 7516:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 7517:                         $changes{$type} = 1;
 7518:                     }
 7519:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 7520:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 7521:                         $changes{$item}{$type} = 1;
 7522:                     }
 7523:                 } else {
 7524:                     if ($context eq 'requestcourses') {
 7525:                         if ($confhash{$item}{$type} ne $unset) {
 7526:                             $changes{$item}{$type} = 1;
 7527:                         }
 7528:                     } else {
 7529:                         if (!$confhash{$item}{$type}) {
 7530:                             $changes{$item}{$type} = 1;
 7531:                         }
 7532:                     }
 7533:                 }
 7534:             } else {
 7535:                 if ($context eq 'requestcourses') {
 7536:                     if ($confhash{$item}{$type} ne $unset) {
 7537:                         $changes{$item}{$type} = 1;
 7538:                     }
 7539:                 } elsif ($context eq 'requestauthor') {
 7540:                     if ($confhash{$type} ne $unset) {
 7541:                         $changes{$type} = 1;
 7542:                     }
 7543:                 } else {
 7544:                     if (!$confhash{$item}{$type}) {
 7545:                         $changes{$item}{$type} = 1;
 7546:                     }
 7547:                 }
 7548:             }
 7549:         }
 7550:     }
 7551:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 7552:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 7553:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7554:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 7555:                     if (exists($confhash{'defaultquota'}{$key})) {
 7556:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 7557:                             $changes{'defaultquota'}{$key} = 1;
 7558:                         }
 7559:                     } else {
 7560:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 7561:                     }
 7562:                 }
 7563:             } else {
 7564:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 7565:                     if (exists($confhash{'defaultquota'}{$key})) {
 7566:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 7567:                             $changes{'defaultquota'}{$key} = 1;
 7568:                         }
 7569:                     } else {
 7570:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 7571:                     }
 7572:                 }
 7573:             }
 7574:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7575:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 7576:                     if (exists($confhash{'authorquota'}{$key})) {
 7577:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 7578:                             $changes{'authorquota'}{$key} = 1;
 7579:                         }
 7580:                     } else {
 7581:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 7582:                     }
 7583:                 }
 7584:             }
 7585:         }
 7586:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 7587:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 7588:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7589:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 7590:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 7591:                             $changes{'defaultquota'}{$key} = 1;
 7592:                         }
 7593:                     } else {
 7594:                         if (!exists($domconfig{'quotas'}{$key})) {
 7595:                             $changes{'defaultquota'}{$key} = 1;
 7596:                         }
 7597:                     }
 7598:                 } else {
 7599:                     $changes{'defaultquota'}{$key} = 1;
 7600:                 }
 7601:             }
 7602:         }
 7603:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 7604:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 7605:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 7606:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 7607:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 7608:                             $changes{'authorquota'}{$key} = 1;
 7609:                         }
 7610:                     } else {
 7611:                         $changes{'authorquota'}{$key} = 1;
 7612:                     }
 7613:                 } else {
 7614:                     $changes{'authorquota'}{$key} = 1;
 7615:                 }
 7616:             }
 7617:         }
 7618:     }
 7619: 
 7620:     if ($context eq 'requestauthor') {
 7621:         $domdefaults{'requestauthor'} = \%confhash;
 7622:     } else {
 7623:         foreach my $key (keys(%confhash)) {
 7624:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 7625:                 $domdefaults{$key} = $confhash{$key};
 7626:             }
 7627:         }
 7628:     }
 7629: 
 7630:     my %quotahash = (
 7631:                       $action => { %confhash }
 7632:                     );
 7633:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 7634:                                              $dom);
 7635:     if ($putresult eq 'ok') {
 7636:         if (keys(%changes) > 0) {
 7637:             my $cachetime = 24*60*60;
 7638:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7639:             if (ref($lastactref) eq 'HASH') {
 7640:                 $lastactref->{'domdefaults'} = 1;
 7641:             }
 7642:             $resulttext = &mt('Changes made:').'<ul>';
 7643:             unless (($context eq 'requestcourses') ||
 7644:                     ($context eq 'requestauthor')) {
 7645:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 7646:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 7647:                     foreach my $type (@{$types},'default') {
 7648:                         if (defined($changes{'defaultquota'}{$type})) {
 7649:                             my $typetitle = $usertypes->{$type};
 7650:                             if ($type eq 'default') {
 7651:                                 $typetitle = $othertitle;
 7652:                             }
 7653:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 7654:                         }
 7655:                     }
 7656:                     $resulttext .= '</ul></li>';
 7657:                 }
 7658:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 7659:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 7660:                     foreach my $type (@{$types},'default') {
 7661:                         if (defined($changes{'authorquota'}{$type})) {
 7662:                             my $typetitle = $usertypes->{$type};
 7663:                             if ($type eq 'default') {
 7664:                                 $typetitle = $othertitle;
 7665:                             }
 7666:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 7667:                         }
 7668:                     }
 7669:                     $resulttext .= '</ul></li>';
 7670:                 }
 7671:             }
 7672:             my %newenv;
 7673:             foreach my $item (@usertools) {
 7674:                 my (%haschgs,%inconf);
 7675:                 if ($context eq 'requestauthor') {
 7676:                     %haschgs = %changes;
 7677:                     %inconf = %confhash;
 7678:                 } else {
 7679:                     if (ref($changes{$item}) eq 'HASH') {
 7680:                         %haschgs = %{$changes{$item}};
 7681:                     }
 7682:                     if (ref($confhash{$item}) eq 'HASH') {
 7683:                         %inconf = %{$confhash{$item}};
 7684:                     }
 7685:                 }
 7686:                 if (keys(%haschgs) > 0) {
 7687:                     my $newacc = 
 7688:                         &Apache::lonnet::usertools_access($env{'user.name'},
 7689:                                                           $env{'user.domain'},
 7690:                                                           $item,'reload',$context);
 7691:                     if (($context eq 'requestcourses') ||
 7692:                         ($context eq 'requestauthor')) {
 7693:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 7694:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 7695:                         }
 7696:                     } else {
 7697:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 7698:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 7699:                         }
 7700:                     }
 7701:                     unless ($context eq 'requestauthor') {
 7702:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 7703:                     }
 7704:                     foreach my $type (@{$types},'default','_LC_adv') {
 7705:                         if ($haschgs{$type}) {
 7706:                             my $typetitle = $usertypes->{$type};
 7707:                             if ($type eq 'default') {
 7708:                                 $typetitle = $othertitle;
 7709:                             } elsif ($type eq '_LC_adv') {
 7710:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 7711:                             }
 7712:                             if ($inconf{$type}) {
 7713:                                 if ($context eq 'requestcourses') {
 7714:                                     my $cond;
 7715:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 7716:                                         if ($1 eq '') {
 7717:                                             $cond = &mt('(Automatic processing of any request).');
 7718:                                         } else {
 7719:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 7720:                                         }
 7721:                                     } else { 
 7722:                                         $cond = $conditions{$inconf{$type}};
 7723:                                     }
 7724:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 7725:                                 } elsif ($context eq 'requestauthor') {
 7726:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 7727:                                                              $titles{$inconf{$type}},$typetitle);
 7728: 
 7729:                                 } else {
 7730:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 7731:                                 }
 7732:                             } else {
 7733:                                 if ($type eq '_LC_adv') {
 7734:                                     if ($inconf{$type} eq '0') {
 7735:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7736:                                     } else { 
 7737:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 7738:                                     }
 7739:                                 } else {
 7740:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 7741:                                 }
 7742:                             }
 7743:                         }
 7744:                     }
 7745:                     unless ($context eq 'requestauthor') {
 7746:                         $resulttext .= '</ul></li>';
 7747:                     }
 7748:                 }
 7749:             }
 7750:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 7751:                 if (ref($changes{'notify'}) eq 'HASH') {
 7752:                     if ($changes{'notify'}{'approval'}) {
 7753:                         if (ref($confhash{'notify'}) eq 'HASH') {
 7754:                             if ($confhash{'notify'}{'approval'}) {
 7755:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 7756:                             } else {
 7757:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 7758:                             }
 7759:                         }
 7760:                     }
 7761:                 }
 7762:             }
 7763:             if ($action eq 'requestcourses') {
 7764:                 my @offon = ('off','on');
 7765:                 if ($changes{'uniquecode'}) {
 7766:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 7767:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 7768:                         $resulttext .= '<li>'.
 7769:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 7770:                                        '</li>';
 7771:                     } else {
 7772:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 7773:                                        '</li>';
 7774:                     }
 7775:                 }
 7776:                 foreach my $type ('textbooks','templates') {
 7777:                     if (ref($changes{$type}) eq 'HASH') {
 7778:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 7779:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 7780:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 7781:                             my $coursetitle = $coursehash{'description'};
 7782:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 7783:                             $resulttext .= '<li>';
 7784:                             foreach my $item ('subject','title','publisher','author') {
 7785:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 7786:                                          ($type eq 'templates'));
 7787:                                 my $name = $item.':';
 7788:                                 $name =~ s/^(\w)/\U$1/;
 7789:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 7790:                             }
 7791:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 7792:                             if ($type eq 'textbooks') {
 7793:                                 if ($confhash{$type}{$key}{'image'}) {
 7794:                                     $resulttext .= ' '.&mt('Image: [_1]',
 7795:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 7796:                                                    ' alt="Textbook cover" />').'<br />';
 7797:                                 }
 7798:                             }
 7799:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 7800:                         }
 7801:                         $resulttext .= '</ul></li>';
 7802:                     }
 7803:                 }
 7804:                 if (ref($changes{'validation'}) eq 'HASH') {
 7805:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 7806:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 7807:                         foreach my $item (@{$validationitemsref}) {
 7808:                             if (exists($changes{'validation'}{$item})) {
 7809:                                 if ($item eq 'markup') {
 7810:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7811:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 7812:                                 } else {
 7813:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 7814:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 7815:                                 }
 7816:                             }
 7817:                         }
 7818:                         if (exists($changes{'validation'}{'dc'})) {
 7819:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 7820:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 7821:                         }
 7822:                     }
 7823:                 }
 7824:             }
 7825:             $resulttext .= '</ul>';
 7826:             if (keys(%newenv)) {
 7827:                 &Apache::lonnet::appenv(\%newenv);
 7828:             }
 7829:         } else {
 7830:             if ($context eq 'requestcourses') {
 7831:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 7832:             } elsif ($context eq 'requestauthor') {
 7833:                 $resulttext = &mt('No changes made to rights to request author space.');
 7834:             } else {
 7835:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 7836:             }
 7837:         }
 7838:     } else {
 7839:         $resulttext = '<span class="LC_error">'.
 7840: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7841:     }
 7842:     if ($errors) {
 7843:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 7844:                        '<ul>'.$errors.'</ul></p>';
 7845:     }
 7846:     return $resulttext;
 7847: }
 7848: 
 7849: sub process_textbook_image {
 7850:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 7851:     my $filename = $env{'form.'.$caller.'.filename'};
 7852:     my ($error,$url);
 7853:     my ($width,$height) = (50,50);
 7854:     if ($configuserok eq 'ok') {
 7855:         if ($switchserver) {
 7856:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 7857:                          $switchserver);
 7858:         } elsif ($author_ok eq 'ok') {
 7859:             my ($result,$imageurl) =
 7860:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 7861:                              "$type/$dom/$cnum/cover",$width,$height);
 7862:             if ($result eq 'ok') {
 7863:                 $url = $imageurl;
 7864:             } else {
 7865:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 7866:             }
 7867:         } else {
 7868:             $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);
 7869:         }
 7870:     } else {
 7871:         $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);
 7872:     }
 7873:     return ($url,$error);
 7874: }
 7875: 
 7876: sub modify_autoenroll {
 7877:     my ($dom,$lastactref,%domconfig) = @_;
 7878:     my ($resulttext,%changes);
 7879:     my %currautoenroll;
 7880:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 7881:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 7882:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 7883:         }
 7884:     }
 7885:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 7886:     my %title = ( run => 'Auto-enrollment active',
 7887:                   sender => 'Sender for notification messages',
 7888:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 7889:     my @offon = ('off','on');
 7890:     my $sender_uname = $env{'form.sender_uname'};
 7891:     my $sender_domain = $env{'form.sender_domain'};
 7892:     if ($sender_domain eq '') {
 7893:         $sender_uname = '';
 7894:     } elsif ($sender_uname eq '') {
 7895:         $sender_domain = '';
 7896:     }
 7897:     my $coowners = $env{'form.autoassign_coowners'};
 7898:     my %autoenrollhash =  (
 7899:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 7900:                                        'sender_uname' => $sender_uname,
 7901:                                        'sender_domain' => $sender_domain,
 7902:                                        'co-owners' => $coowners,
 7903:                                 }
 7904:                      );
 7905:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 7906:                                              $dom);
 7907:     if ($putresult eq 'ok') {
 7908:         if (exists($currautoenroll{'run'})) {
 7909:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 7910:                  $changes{'run'} = 1;
 7911:              }
 7912:         } elsif ($autorun) {
 7913:             if ($env{'form.autoenroll_run'} ne '1') {
 7914:                  $changes{'run'} = 1;
 7915:             }
 7916:         }
 7917:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 7918:             $changes{'sender'} = 1;
 7919:         }
 7920:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 7921:             $changes{'sender'} = 1;
 7922:         }
 7923:         if ($currautoenroll{'co-owners'} ne '') {
 7924:             if ($currautoenroll{'co-owners'} ne $coowners) {
 7925:                 $changes{'coowners'} = 1;
 7926:             }
 7927:         } elsif ($coowners) {
 7928:             $changes{'coowners'} = 1;
 7929:         }      
 7930:         if (keys(%changes) > 0) {
 7931:             $resulttext = &mt('Changes made:').'<ul>';
 7932:             if ($changes{'run'}) {
 7933:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 7934:             }
 7935:             if ($changes{'sender'}) {
 7936:                 if ($sender_uname eq '' || $sender_domain eq '') {
 7937:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 7938:                 } else {
 7939:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 7940:                 }
 7941:             }
 7942:             if ($changes{'coowners'}) {
 7943:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 7944:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7945:                 if (ref($lastactref) eq 'HASH') {
 7946:                     $lastactref->{'domainconfig'} = 1;
 7947:                 }
 7948:             }
 7949:             $resulttext .= '</ul>';
 7950:         } else {
 7951:             $resulttext = &mt('No changes made to auto-enrollment settings');
 7952:         }
 7953:     } else {
 7954:         $resulttext = '<span class="LC_error">'.
 7955: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7956:     }
 7957:     return $resulttext;
 7958: }
 7959: 
 7960: sub modify_autoupdate {
 7961:     my ($dom,%domconfig) = @_;
 7962:     my ($resulttext,%currautoupdate,%fields,%changes);
 7963:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 7964:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 7965:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 7966:         }
 7967:     }
 7968:     my @offon = ('off','on');
 7969:     my %title = &Apache::lonlocal::texthash (
 7970:                    run => 'Auto-update:',
 7971:                    classlists => 'Updates to user information in classlists?'
 7972:                 );
 7973:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7974:     my %fieldtitles = &Apache::lonlocal::texthash (
 7975:                         id => 'Student/Employee ID',
 7976:                         permanentemail => 'E-mail address',
 7977:                         lastname => 'Last Name',
 7978:                         firstname => 'First Name',
 7979:                         middlename => 'Middle Name',
 7980:                         generation => 'Generation',
 7981:                       );
 7982:     $othertitle = &mt('All users');
 7983:     if (keys(%{$usertypes}) >  0) {
 7984:         $othertitle = &mt('Other users');
 7985:     }
 7986:     foreach my $key (keys(%env)) {
 7987:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 7988:             my ($usertype,$item) = ($1,$2);
 7989:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 7990:                 if ($usertype eq 'default') {   
 7991:                     push(@{$fields{$1}},$2);
 7992:                 } elsif (ref($types) eq 'ARRAY') {
 7993:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 7994:                         push(@{$fields{$1}},$2);
 7995:                     }
 7996:                 }
 7997:             }
 7998:         }
 7999:     }
 8000:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 8001:     @lockablenames = sort(@lockablenames);
 8002:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 8003:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 8004:         if (@changed) {
 8005:             $changes{'lockablenames'} = 1;
 8006:         }
 8007:     } else {
 8008:         if (@lockablenames) {
 8009:             $changes{'lockablenames'} = 1;
 8010:         }
 8011:     }
 8012:     my %updatehash = (
 8013:                       autoupdate => { run => $env{'form.autoupdate_run'},
 8014:                                       classlists => $env{'form.classlists'},
 8015:                                       fields => {%fields},
 8016:                                       lockablenames => \@lockablenames,
 8017:                                     }
 8018:                      );
 8019:     foreach my $key (keys(%currautoupdate)) {
 8020:         if (($key eq 'run') || ($key eq 'classlists')) {
 8021:             if (exists($updatehash{autoupdate}{$key})) {
 8022:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 8023:                     $changes{$key} = 1;
 8024:                 }
 8025:             }
 8026:         } elsif ($key eq 'fields') {
 8027:             if (ref($currautoupdate{$key}) eq 'HASH') {
 8028:                 foreach my $item (@{$types},'default') {
 8029:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 8030:                         my $change = 0;
 8031:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 8032:                             if (!exists($fields{$item})) {
 8033:                                 $change = 1;
 8034:                                 last;
 8035:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 8036:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 8037:                                     $change = 1;
 8038:                                     last;
 8039:                                 }
 8040:                             }
 8041:                         }
 8042:                         if ($change) {
 8043:                             push(@{$changes{$key}},$item);
 8044:                         }
 8045:                     } 
 8046:                 }
 8047:             }
 8048:         } elsif ($key eq 'lockablenames') {
 8049:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 8050:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 8051:                 if (@changed) {
 8052:                     $changes{'lockablenames'} = 1;
 8053:                 }
 8054:             } else {
 8055:                 if (@lockablenames) {
 8056:                     $changes{'lockablenames'} = 1;
 8057:                 }
 8058:             }
 8059:         }
 8060:     }
 8061:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 8062:         if (@lockablenames) {
 8063:             $changes{'lockablenames'} = 1;
 8064:         }
 8065:     }
 8066:     foreach my $item (@{$types},'default') {
 8067:         if (defined($fields{$item})) {
 8068:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 8069:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 8070:                     my $change = 0;
 8071:                     if (ref($fields{$item}) eq 'ARRAY') {
 8072:                         foreach my $type (@{$fields{$item}}) {
 8073:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 8074:                                 $change = 1;
 8075:                                 last;
 8076:                             }
 8077:                         }
 8078:                     }
 8079:                     if ($change) {
 8080:                         push(@{$changes{'fields'}},$item);
 8081:                     }
 8082:                 } else {
 8083:                     push(@{$changes{'fields'}},$item);
 8084:                 }
 8085:             } else {
 8086:                 push(@{$changes{'fields'}},$item);
 8087:             }
 8088:         }
 8089:     }
 8090:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 8091:                                              $dom);
 8092:     if ($putresult eq 'ok') {
 8093:         if (keys(%changes) > 0) {
 8094:             $resulttext = &mt('Changes made:').'<ul>';
 8095:             foreach my $key (sort(keys(%changes))) {
 8096:                 if ($key eq 'lockablenames') {
 8097:                     $resulttext .= '<li>';
 8098:                     if (@lockablenames) {
 8099:                         $usertypes->{'default'} = $othertitle;
 8100:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 8101:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 8102:                     } else {
 8103:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 8104:                     }
 8105:                     $resulttext .= '</li>';
 8106:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 8107:                     foreach my $item (@{$changes{$key}}) {
 8108:                         my @newvalues;
 8109:                         foreach my $type (@{$fields{$item}}) {
 8110:                             push(@newvalues,$fieldtitles{$type});
 8111:                         }
 8112:                         my $newvaluestr;
 8113:                         if (@newvalues > 0) {
 8114:                             $newvaluestr = join(', ',@newvalues);
 8115:                         } else {
 8116:                             $newvaluestr = &mt('none');
 8117:                         }
 8118:                         if ($item eq 'default') {
 8119:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 8120:                         } else {
 8121:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 8122:                         }
 8123:                     }
 8124:                 } else {
 8125:                     my $newvalue;
 8126:                     if ($key eq 'run') {
 8127:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 8128:                     } else {
 8129:                         $newvalue = $offon[$env{'form.'.$key}];
 8130:                     }
 8131:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 8132:                 }
 8133:             }
 8134:             $resulttext .= '</ul>';
 8135:         } else {
 8136:             $resulttext = &mt('No changes made to autoupdates');
 8137:         }
 8138:     } else {
 8139:         $resulttext = '<span class="LC_error">'.
 8140: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8141:     }
 8142:     return $resulttext;
 8143: }
 8144: 
 8145: sub modify_autocreate {
 8146:     my ($dom,%domconfig) = @_;
 8147:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 8148:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 8149:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 8150:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 8151:         }
 8152:     }
 8153:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 8154:                  req => 'Auto-creation of validated requests for official courses',
 8155:                  xmldc => 'Identity of course creator of courses from XML files',
 8156:                );
 8157:     my @types = ('xml','req');
 8158:     foreach my $item (@types) {
 8159:         $newvals{$item} = $env{'form.autocreate_'.$item};
 8160:         $newvals{$item} =~ s/\D//g;
 8161:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 8162:     }
 8163:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 8164:     my %domcoords = &get_active_dcs($dom);
 8165:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 8166:         $newvals{'xmldc'} = '';
 8167:     } 
 8168:     %autocreatehash =  (
 8169:                         autocreate => { xml => $newvals{'xml'},
 8170:                                         req => $newvals{'req'},
 8171:                                       }
 8172:                        );
 8173:     if ($newvals{'xmldc'} ne '') {
 8174:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 8175:     }
 8176:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 8177:                                              $dom);
 8178:     if ($putresult eq 'ok') {
 8179:         my @items = @types;
 8180:         if ($newvals{'xml'}) {
 8181:             push(@items,'xmldc');
 8182:         }
 8183:         foreach my $item (@items) {
 8184:             if (exists($currautocreate{$item})) {
 8185:                 if ($currautocreate{$item} ne $newvals{$item}) {
 8186:                     $changes{$item} = 1;
 8187:                 }
 8188:             } elsif ($newvals{$item}) {
 8189:                 $changes{$item} = 1;
 8190:             }
 8191:         }
 8192:         if (keys(%changes) > 0) {
 8193:             my @offon = ('off','on'); 
 8194:             $resulttext = &mt('Changes made:').'<ul>';
 8195:             foreach my $item (@types) {
 8196:                 if ($changes{$item}) {
 8197:                     my $newtxt = $offon[$newvals{$item}];
 8198:                     $resulttext .= '<li>'.
 8199:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 8200:                                        '<b>','</b>').
 8201:                                    '</li>';
 8202:                 }
 8203:             }
 8204:             if ($changes{'xmldc'}) {
 8205:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 8206:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 8207:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 8208:             }
 8209:             $resulttext .= '</ul>';
 8210:         } else {
 8211:             $resulttext = &mt('No changes made to auto-creation settings');
 8212:         }
 8213:     } else {
 8214:         $resulttext = '<span class="LC_error">'.
 8215:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8216:     }
 8217:     return $resulttext;
 8218: }
 8219: 
 8220: sub modify_directorysrch {
 8221:     my ($dom,%domconfig) = @_;
 8222:     my ($resulttext,%changes);
 8223:     my %currdirsrch;
 8224:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 8225:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 8226:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 8227:         }
 8228:     }
 8229:     my %title = ( available => 'Directory search available',
 8230:                   localonly => 'Other domains can search',
 8231:                   searchby => 'Search types',
 8232:                   searchtypes => 'Search latitude');
 8233:     my @offon = ('off','on');
 8234:     my @otherdoms = ('Yes','No');
 8235: 
 8236:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 8237:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 8238:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 8239: 
 8240:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8241:     if (keys(%{$usertypes}) == 0) {
 8242:         @cansearch = ('default');
 8243:     } else {
 8244:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 8245:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 8246:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 8247:                     push(@{$changes{'cansearch'}},$type);
 8248:                 }
 8249:             }
 8250:             foreach my $type (@cansearch) {
 8251:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 8252:                     push(@{$changes{'cansearch'}},$type);
 8253:                 }
 8254:             }
 8255:         } else {
 8256:             push(@{$changes{'cansearch'}},@cansearch);
 8257:         }
 8258:     }
 8259: 
 8260:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 8261:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 8262:             if (!grep(/^\Q$by\E$/,@searchby)) {
 8263:                 push(@{$changes{'searchby'}},$by);
 8264:             }
 8265:         }
 8266:         foreach my $by (@searchby) {
 8267:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 8268:                 push(@{$changes{'searchby'}},$by);
 8269:             }
 8270:         }
 8271:     } else {
 8272:         push(@{$changes{'searchby'}},@searchby);
 8273:     }
 8274: 
 8275:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 8276:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 8277:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 8278:                 push(@{$changes{'searchtypes'}},$type);
 8279:             }
 8280:         }
 8281:         foreach my $type (@searchtypes) {
 8282:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 8283:                 push(@{$changes{'searchtypes'}},$type);
 8284:             }
 8285:         }
 8286:     } else {
 8287:         if (exists($currdirsrch{'searchtypes'})) {
 8288:             foreach my $type (@searchtypes) {  
 8289:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 8290:                     push(@{$changes{'searchtypes'}},$type);
 8291:                 }
 8292:             }
 8293:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 8294:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 8295:             }   
 8296:         } else {
 8297:             push(@{$changes{'searchtypes'}},@searchtypes); 
 8298:         }
 8299:     }
 8300: 
 8301:     my %dirsrch_hash =  (
 8302:             directorysrch => { available => $env{'form.dirsrch_available'},
 8303:                                cansearch => \@cansearch,
 8304:                                localonly => $env{'form.dirsrch_localonly'},
 8305:                                searchby => \@searchby,
 8306:                                searchtypes => \@searchtypes,
 8307:                              }
 8308:             );
 8309:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 8310:                                              $dom);
 8311:     if ($putresult eq 'ok') {
 8312:         if (exists($currdirsrch{'available'})) {
 8313:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 8314:                  $changes{'available'} = 1;
 8315:              }
 8316:         } else {
 8317:             if ($env{'form.dirsrch_available'} eq '1') {
 8318:                 $changes{'available'} = 1;
 8319:             }
 8320:         }
 8321:         if (exists($currdirsrch{'localonly'})) {
 8322:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 8323:                  $changes{'localonly'} = 1;
 8324:              }
 8325:         } else {
 8326:             if ($env{'form.dirsrch_localonly'} eq '1') {
 8327:                 $changes{'localonly'} = 1;
 8328:             }
 8329:         }
 8330:         if (keys(%changes) > 0) {
 8331:             $resulttext = &mt('Changes made:').'<ul>';
 8332:             if ($changes{'available'}) {
 8333:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 8334:             }
 8335:             if ($changes{'localonly'}) {
 8336:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 8337:             }
 8338: 
 8339:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 8340:                 my $chgtext;
 8341:                 if (ref($usertypes) eq 'HASH') {
 8342:                     if (keys(%{$usertypes}) > 0) {
 8343:                         foreach my $type (@{$types}) {
 8344:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 8345:                                 $chgtext .= $usertypes->{$type}.'; ';
 8346:                             }
 8347:                         }
 8348:                         if (grep(/^default$/,@cansearch)) {
 8349:                             $chgtext .= $othertitle;
 8350:                         } else {
 8351:                             $chgtext =~ s/\; $//;
 8352:                         }
 8353:                         $resulttext .=
 8354:                             '<li>'.
 8355:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 8356:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 8357:                             '</li>';
 8358:                     }
 8359:                 }
 8360:             }
 8361:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 8362:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 8363:                 my $chgtext;
 8364:                 foreach my $type (@{$titleorder}) {
 8365:                     if (grep(/^\Q$type\E$/,@searchby)) {
 8366:                         if (defined($searchtitles->{$type})) {
 8367:                             $chgtext .= $searchtitles->{$type}.'; ';
 8368:                         }
 8369:                     }
 8370:                 }
 8371:                 $chgtext =~ s/\; $//;
 8372:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 8373:             }
 8374:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 8375:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 8376:                 my $chgtext;
 8377:                 foreach my $type (@{$srchtypeorder}) {
 8378:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 8379:                         if (defined($srchtypes_desc->{$type})) {
 8380:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 8381:                         }
 8382:                     }
 8383:                 }
 8384:                 $chgtext =~ s/\; $//;
 8385:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 8386:             }
 8387:             $resulttext .= '</ul>';
 8388:         } else {
 8389:             $resulttext = &mt('No changes made to institution directory search settings');
 8390:         }
 8391:     } else {
 8392:         $resulttext = '<span class="LC_error">'.
 8393:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 8394:     }
 8395:     return $resulttext;
 8396: }
 8397: 
 8398: sub modify_contacts {
 8399:     my ($dom,$lastactref,%domconfig) = @_;
 8400:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 8401:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 8402:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 8403:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 8404:         }
 8405:     }
 8406:     my (%others,%to,%bcc);
 8407:     my @contacts = ('supportemail','adminemail');
 8408:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 8409:                     'requestsmail','updatesmail','idconflictsmail');
 8410:     my @toggles = ('reporterrors','reportupdates');
 8411:     foreach my $type (@mailings) {
 8412:         @{$newsetting{$type}} = 
 8413:             &Apache::loncommon::get_env_multiple('form.'.$type);
 8414:         foreach my $item (@contacts) {
 8415:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 8416:                 $contacts_hash{contacts}{$type}{$item} = 1;
 8417:             } else {
 8418:                 $contacts_hash{contacts}{$type}{$item} = 0;
 8419:             }
 8420:         }  
 8421:         $others{$type} = $env{'form.'.$type.'_others'};
 8422:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 8423:         if ($type eq 'helpdeskmail') {
 8424:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 8425:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 8426:         }
 8427:     }
 8428:     foreach my $item (@contacts) {
 8429:         $to{$item} = $env{'form.'.$item};
 8430:         $contacts_hash{'contacts'}{$item} = $to{$item};
 8431:     }
 8432:     foreach my $item (@toggles) {
 8433:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 8434:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 8435:         }
 8436:     }
 8437:     if (keys(%currsetting) > 0) {
 8438:         foreach my $item (@contacts) {
 8439:             if ($to{$item} ne $currsetting{$item}) {
 8440:                 $changes{$item} = 1;
 8441:             }
 8442:         }
 8443:         foreach my $type (@mailings) {
 8444:             foreach my $item (@contacts) {
 8445:                 if (ref($currsetting{$type}) eq 'HASH') {
 8446:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 8447:                         push(@{$changes{$type}},$item);
 8448:                     }
 8449:                 } else {
 8450:                     push(@{$changes{$type}},@{$newsetting{$type}});
 8451:                 }
 8452:             }
 8453:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 8454:                 push(@{$changes{$type}},'others');
 8455:             }
 8456:             if ($type eq 'helpdeskmail') {   
 8457:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 8458:                     push(@{$changes{$type}},'bcc'); 
 8459:                 }
 8460:             }
 8461:         }
 8462:     } else {
 8463:         my %default;
 8464:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 8465:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 8466:         $default{'errormail'} = 'adminemail';
 8467:         $default{'packagesmail'} = 'adminemail';
 8468:         $default{'helpdeskmail'} = 'supportemail';
 8469:         $default{'lonstatusmail'} = 'adminemail';
 8470:         $default{'requestsmail'} = 'adminemail';
 8471:         $default{'updatesmail'} = 'adminemail';
 8472:         foreach my $item (@contacts) {
 8473:            if ($to{$item} ne $default{$item}) {
 8474:               $changes{$item} = 1;
 8475:            }
 8476:         }
 8477:         foreach my $type (@mailings) {
 8478:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 8479:                
 8480:                 push(@{$changes{$type}},@{$newsetting{$type}});
 8481:             }
 8482:             if ($others{$type} ne '') {
 8483:                 push(@{$changes{$type}},'others');
 8484:             }
 8485:             if ($type eq 'helpdeskmail') {
 8486:                 if ($bcc{$type} ne '') {
 8487:                     push(@{$changes{$type}},'bcc');
 8488:                 }
 8489:             }
 8490:         }
 8491:     }
 8492:     foreach my $item (@toggles) {
 8493:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 8494:             $changes{$item} = 1;
 8495:         } elsif ((!$env{'form.'.$item}) &&
 8496:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 8497:             $changes{$item} = 1;
 8498:         }
 8499:     }
 8500:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 8501:                                              $dom);
 8502:     if ($putresult eq 'ok') {
 8503:         if (keys(%changes) > 0) {
 8504:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8505:             if (ref($lastactref) eq 'HASH') {
 8506:                 $lastactref->{'domainconfig'} = 1;
 8507:             }
 8508:             my ($titles,$short_titles)  = &contact_titles();
 8509:             $resulttext = &mt('Changes made:').'<ul>';
 8510:             foreach my $item (@contacts) {
 8511:                 if ($changes{$item}) {
 8512:                     $resulttext .= '<li>'.$titles->{$item}.
 8513:                                     &mt(' set to: ').
 8514:                                     '<span class="LC_cusr_emph">'.
 8515:                                     $to{$item}.'</span></li>';
 8516:                 }
 8517:             }
 8518:             foreach my $type (@mailings) {
 8519:                 if (ref($changes{$type}) eq 'ARRAY') {
 8520:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 8521:                     my @text;
 8522:                     foreach my $item (@{$newsetting{$type}}) {
 8523:                         push(@text,$short_titles->{$item});
 8524:                     }
 8525:                     if ($others{$type} ne '') {
 8526:                         push(@text,$others{$type});
 8527:                     }
 8528:                     $resulttext .= '<span class="LC_cusr_emph">'.
 8529:                                    join(', ',@text).'</span>';
 8530:                     if ($type eq 'helpdeskmail') {
 8531:                         if ($bcc{$type} ne '') {
 8532:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 8533:                         }
 8534:                     }
 8535:                     $resulttext .= '</li>';
 8536:                 }
 8537:             }
 8538:             my @offon = ('off','on');
 8539:             if ($changes{'reporterrors'}) {
 8540:                 $resulttext .= '<li>'.
 8541:                                &mt('E-mail error reports to [_1] set to "'.
 8542:                                    $offon[$env{'form.reporterrors'}].'".',
 8543:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8544:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 8545:                                '</li>';
 8546:             }
 8547:             if ($changes{'reportupdates'}) {
 8548:                 $resulttext .= '<li>'.
 8549:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 8550:                                     $offon[$env{'form.reportupdates'}].'".',
 8551:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 8552:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 8553:                                 '</li>';
 8554:             }
 8555:             $resulttext .= '</ul>';
 8556:         } else {
 8557:             $resulttext = &mt('No changes made to contact information');
 8558:         }
 8559:     } else {
 8560:         $resulttext = '<span class="LC_error">'.
 8561:             &mt('An error occurred: [_1].',$putresult).'</span>';
 8562:     }
 8563:     return $resulttext;
 8564: }
 8565: 
 8566: sub modify_usercreation {
 8567:     my ($dom,%domconfig) = @_;
 8568:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 8569:     my $warningmsg;
 8570:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8571:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8572:             if ($key eq 'cancreate') {
 8573:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8574:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8575:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8576:                             ($item eq 'captcha') || ($item eq 'recaptchakeys')) {
 8577:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8578:                         } else {
 8579:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8580:                         }
 8581:                     }
 8582:                 }
 8583:             } elsif ($key eq 'email_rule') {
 8584:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8585:             } else {
 8586:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8587:             }
 8588:         }
 8589:     }
 8590:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 8591:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 8592:     my @contexts = ('author','course','requestcrs');
 8593:     foreach my $item(@contexts) {
 8594:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 8595:     }
 8596:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8597:         foreach my $item (@contexts) {
 8598:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 8599:                 push(@{$changes{'cancreate'}},$item);
 8600:             }
 8601:         }
 8602:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 8603:         foreach my $item (@contexts) {
 8604:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 8605:                 if ($cancreate{$item} ne 'any') {
 8606:                     push(@{$changes{'cancreate'}},$item);
 8607:                 }
 8608:             } else {
 8609:                 if ($cancreate{$item} ne 'none') {
 8610:                     push(@{$changes{'cancreate'}},$item);
 8611:                 }
 8612:             }
 8613:         }
 8614:     } else {
 8615:         foreach my $item (@contexts)  {
 8616:             push(@{$changes{'cancreate'}},$item);
 8617:         }
 8618:     }
 8619: 
 8620:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 8621:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 8622:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 8623:                 push(@{$changes{'username_rule'}},$type);
 8624:             }
 8625:         }
 8626:         foreach my $type (@username_rule) {
 8627:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 8628:                 push(@{$changes{'username_rule'}},$type);
 8629:             }
 8630:         }
 8631:     } else {
 8632:         push(@{$changes{'username_rule'}},@username_rule);
 8633:     }
 8634: 
 8635:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 8636:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 8637:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 8638:                 push(@{$changes{'id_rule'}},$type);
 8639:             }
 8640:         }
 8641:         foreach my $type (@id_rule) {
 8642:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 8643:                 push(@{$changes{'id_rule'}},$type);
 8644:             }
 8645:         }
 8646:     } else {
 8647:         push(@{$changes{'id_rule'}},@id_rule);
 8648:     }
 8649: 
 8650:     my @authen_contexts = ('author','course','domain');
 8651:     my @authtypes = ('int','krb4','krb5','loc');
 8652:     my %authhash;
 8653:     foreach my $item (@authen_contexts) {
 8654:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 8655:         foreach my $auth (@authtypes) {
 8656:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 8657:                 $authhash{$item}{$auth} = 1;
 8658:             } else {
 8659:                 $authhash{$item}{$auth} = 0;
 8660:             }
 8661:         }
 8662:     }
 8663:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 8664:         foreach my $item (@authen_contexts) {
 8665:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 8666:                 foreach my $auth (@authtypes) {
 8667:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 8668:                         push(@{$changes{'authtypes'}},$item);
 8669:                         last;
 8670:                     }
 8671:                 }
 8672:             }
 8673:         }
 8674:     } else {
 8675:         foreach my $item (@authen_contexts) {
 8676:             push(@{$changes{'authtypes'}},$item);
 8677:         }
 8678:     }
 8679: 
 8680:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
 8681:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
 8682:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
 8683:     $save_usercreate{'id_rule'} = \@id_rule;
 8684:     $save_usercreate{'username_rule'} = \@username_rule,
 8685:     $save_usercreate{'authtypes'} = \%authhash;
 8686: 
 8687:     my %usercreation_hash =  (
 8688:         usercreation     => \%save_usercreate,
 8689:     );
 8690: 
 8691:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 8692:                                              $dom);
 8693: 
 8694:     if ($putresult eq 'ok') {
 8695:         if (keys(%changes) > 0) {
 8696:             $resulttext = &mt('Changes made:').'<ul>';
 8697:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8698:                 my %lt = &usercreation_types();
 8699:                 foreach my $type (@{$changes{'cancreate'}}) {
 8700:                     my $chgtext = $lt{$type}.', ';
 8701:                     if ($cancreate{$type} eq 'none') {
 8702:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 8703:                     } elsif ($cancreate{$type} eq 'any') {
 8704:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 8705:                     } elsif ($cancreate{$type} eq 'official') {
 8706:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 8707:                     } elsif ($cancreate{$type} eq 'unofficial') {
 8708:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 8709:                     }
 8710:                     $resulttext .= '<li>'.$chgtext.'</li>';
 8711:                 }
 8712:             }
 8713:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 8714:                 my ($rules,$ruleorder) = 
 8715:                     &Apache::lonnet::inst_userrules($dom,'username');
 8716:                 my $chgtext = '<ul>';
 8717:                 foreach my $type (@username_rule) {
 8718:                     if (ref($rules->{$type}) eq 'HASH') {
 8719:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 8720:                     }
 8721:                 }
 8722:                 $chgtext .= '</ul>';
 8723:                 if (@username_rule > 0) {
 8724:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 8725:                 } else {
 8726:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 8727:                 }
 8728:             }
 8729:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 8730:                 my ($idrules,$idruleorder) = 
 8731:                     &Apache::lonnet::inst_userrules($dom,'id');
 8732:                 my $chgtext = '<ul>';
 8733:                 foreach my $type (@id_rule) {
 8734:                     if (ref($idrules->{$type}) eq 'HASH') {
 8735:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 8736:                     }
 8737:                 }
 8738:                 $chgtext .= '</ul>';
 8739:                 if (@id_rule > 0) {
 8740:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 8741:                 } else {
 8742:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 8743:                 }
 8744:             }
 8745:             my %authname = &authtype_names();
 8746:             my %context_title = &context_names();
 8747:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 8748:                 my $chgtext = '<ul>';
 8749:                 foreach my $type (@{$changes{'authtypes'}}) {
 8750:                     my @allowed;
 8751:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 8752:                     foreach my $auth (@authtypes) {
 8753:                         if ($authhash{$type}{$auth}) {
 8754:                             push(@allowed,$authname{$auth});
 8755:                         }
 8756:                     }
 8757:                     if (@allowed > 0) {
 8758:                         $chgtext .= join(', ',@allowed).'</li>';
 8759:                     } else {
 8760:                         $chgtext .= &mt('none').'</li>';
 8761:                     }
 8762:                 }
 8763:                 $chgtext .= '</ul>';
 8764:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 8765:                 $resulttext .= '</li>';
 8766:             }
 8767:             $resulttext .= '</ul>';
 8768:         } else {
 8769:             $resulttext = &mt('No changes made to user creation settings');
 8770:         }
 8771:     } else {
 8772:         $resulttext = '<span class="LC_error">'.
 8773:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8774:     }
 8775:     if ($warningmsg ne '') {
 8776:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8777:     }
 8778:     return $resulttext;
 8779: }
 8780: 
 8781: sub modify_selfcreation {
 8782:     my ($dom,%domconfig) = @_;
 8783:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
 8784:     my (%save_usercreate,%save_usermodify);
 8785:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8786:     if (ref($types) eq 'ARRAY') {
 8787:         $usertypes->{'default'} = $othertitle;
 8788:         push(@{$types},'default');
 8789:     }
 8790: #
 8791: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
 8792: #
 8793:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 8794:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 8795:             if ($key eq 'cancreate') {
 8796:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 8797:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 8798:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 8799:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
 8800:                             ($item eq 'emailusername') || ($item eq 'notify') ||
 8801:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
 8802:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8803:                         } else {
 8804:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 8805:                         }
 8806:                     }
 8807:                 }
 8808:             } elsif ($key eq 'email_rule') {
 8809:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 8810:             } else {
 8811:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 8812:             }
 8813:         }
 8814:     }
 8815: #
 8816: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
 8817: #
 8818:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 8819:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 8820:             if ($key eq 'selfcreate') {
 8821:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8822:             } else {
 8823:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
 8824:             }
 8825:         }
 8826:     }
 8827: 
 8828:     my @contexts = ('selfcreate');
 8829:     @{$cancreate{'selfcreate'}} = ();
 8830:     %{$cancreate{'emailusername'}} = ();
 8831:     @{$cancreate{'statustocreate'}} = ();
 8832:     %{$cancreate{'selfcreateprocessing'}} = ();
 8833:     %{$cancreate{'shibenv'}} = ();
 8834:     my %selfcreatetypes = (
 8835:                              sso   => 'users authenticated by institutional single sign on',
 8836:                              login => 'users authenticated by institutional log-in',
 8837:                              email => 'users who provide a valid e-mail address for use as username',
 8838:                           );
 8839: #
 8840: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
 8841: # is permitted.
 8842: #
 8843: 
 8844:     my @statuses;
 8845:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 8846:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
 8847:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
 8848:         }
 8849:     }
 8850:     push(@statuses,'default');
 8851: 
 8852:     foreach my $item ('login','sso','email') {
 8853:         if ($item eq 'email') {
 8854:             if ($env{'form.cancreate_email'}) {
 8855:                 push(@{$cancreate{'selfcreate'}},'email');
 8856:                 push(@contexts,'selfcreateprocessing');
 8857:                 foreach my $type (@statuses) {
 8858:                     if ($type eq 'default') {
 8859:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
 8860:                     } else { 
 8861:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
 8862:                     }
 8863:                 }
 8864:             }
 8865:         } else {
 8866:             if ($env{'form.cancreate_'.$item}) {
 8867:                 push(@{$cancreate{'selfcreate'}},$item);
 8868:             }
 8869:         }
 8870:     }
 8871:     my (@email_rule,%userinfo,%savecaptcha);
 8872:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 8873: #
 8874: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
 8875: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
 8876: #
 8877: 
 8878:     if ($env{'form.cancreate_email'}) {
 8879:         push(@contexts,'emailusername');
 8880:         if (ref($types) eq 'ARRAY') {
 8881:             foreach my $type (@{$types}) {
 8882:                 if (ref($infofields) eq 'ARRAY') {
 8883:                     foreach my $field (@{$infofields}) {
 8884:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
 8885:                             $cancreate{'emailusername'}{$type}{$field} = $1;
 8886:                         }
 8887:                     }
 8888:                 }
 8889:             }
 8890:         }
 8891: #
 8892: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
 8893: # queued requests for self-creation of account using e-mail address as username
 8894: #
 8895: 
 8896:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
 8897:         @approvalnotify = sort(@approvalnotify);
 8898:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
 8899:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 8900:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
 8901:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
 8902:                     push(@{$changes{'cancreate'}},'notify');
 8903:                 }
 8904:             } else {
 8905:                 if ($cancreate{'notify'}{'approval'}) {
 8906:                     push(@{$changes{'cancreate'}},'notify');
 8907:                 }
 8908:             }
 8909:         } elsif ($cancreate{'notify'}{'approval'}) {
 8910:             push(@{$changes{'cancreate'}},'notify');
 8911:         }
 8912: 
 8913: #
 8914: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
 8915: #
 8916:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 8917:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
 8918:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 8919:             if (@{$curr_usercreation{'email_rule'}} > 0) {
 8920:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 8921:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
 8922:                         push(@{$changes{'email_rule'}},$type);
 8923:                     }
 8924:                 }
 8925:             }
 8926:             if (@email_rule > 0) {
 8927:                 foreach my $type (@email_rule) {
 8928:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 8929:                         push(@{$changes{'email_rule'}},$type);
 8930:                     }
 8931:                 }
 8932:             }
 8933:         } elsif (@email_rule > 0) {
 8934:             push(@{$changes{'email_rule'}},@email_rule);
 8935:         }
 8936:     }
 8937: #  
 8938: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
 8939: # institutional log-in.
 8940: #
 8941:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
 8942:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8943:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
 8944:                ($domdefaults{'auth_def'} eq 'localauth'))) {
 8945:             $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.').' '.
 8946:                           &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.');
 8947:         }
 8948:     }
 8949:     my @fields = ('lastname','firstname','middlename','generation',
 8950:                   'permanentemail','id');
 8951:     my @shibfields = (@fields,'inststatus');
 8952:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8953: #
 8954: # Where usernames may created for institutional log-in and/or institutional single sign on:
 8955: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
 8956: # may self-create accounts 
 8957: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
 8958: # which the user may supply, if institutional data is unavailable.
 8959: #
 8960:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
 8961:         if (ref($types) eq 'ARRAY') {
 8962:             if (@{$types} > 1) {
 8963:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
 8964:                 push(@contexts,'statustocreate');
 8965:             } else {
 8966:                 undef($cancreate{'statustocreate'});
 8967:             } 
 8968:             foreach my $type (@{$types}) {
 8969:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
 8970:                 foreach my $field (@fields) {
 8971:                     if (grep(/^\Q$field\E$/,@modifiable)) {
 8972:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
 8973:                     } else {
 8974:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
 8975:                     }
 8976:                 }
 8977:             }
 8978:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
 8979:                 foreach my $type (@{$types}) {
 8980:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
 8981:                         foreach my $field (@fields) {
 8982:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
 8983:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
 8984:                                 push(@{$changes{'selfcreate'}},$type);
 8985:                                 last;
 8986:                             }
 8987:                         }
 8988:                     }
 8989:                 }
 8990:             } else {
 8991:                 foreach my $type (@{$types}) {
 8992:                     push(@{$changes{'selfcreate'}},$type);
 8993:                 }
 8994:             }
 8995:         }
 8996:         foreach my $field (@shibfields) {
 8997:             if ($env{'form.shibenv_'.$field} ne '') {
 8998:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
 8999:             }
 9000:         }
 9001:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 9002:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
 9003:                 foreach my $field (@shibfields) {
 9004:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
 9005:                         push(@{$changes{'cancreate'}},'shibenv');
 9006:                     }
 9007:                 }
 9008:             } else {
 9009:                 foreach my $field (@shibfields) {
 9010:                     if ($env{'form.shibenv_'.$field}) {
 9011:                         push(@{$changes{'cancreate'}},'shibenv');
 9012:                         last;
 9013:                     }
 9014:                 }
 9015:             }
 9016:         }
 9017:     }
 9018:     foreach my $item (@contexts) {
 9019:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 9020:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 9021:                 if (ref($cancreate{$item}) eq 'ARRAY') {
 9022:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 9023:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9024:                             push(@{$changes{'cancreate'}},$item);
 9025:                         }
 9026:                     }
 9027:                 }
 9028:             }
 9029:             if (ref($cancreate{$item}) eq 'ARRAY') {
 9030:                 foreach my $type (@{$cancreate{$item}}) {
 9031:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 9032:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9033:                             push(@{$changes{'cancreate'}},$item);
 9034:                         }
 9035:                     }
 9036:                 }
 9037:             }
 9038:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
 9039:             if (ref($cancreate{$item}) eq 'HASH') {
 9040:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
 9041:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
 9042:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
 9043:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
 9044:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9045:                                     push(@{$changes{'cancreate'}},$item);
 9046:                                 }
 9047:                             }
 9048:                         }
 9049:                     } elsif ($item eq 'selfcreateprocessing') {
 9050:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
 9051:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9052:                                 push(@{$changes{'cancreate'}},$item);
 9053:                             }
 9054:                         }
 9055:                     } else {
 9056:                         if (!$cancreate{$item}{$curr}) {
 9057:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9058:                                 push(@{$changes{'cancreate'}},$item);
 9059:                             }
 9060:                         }
 9061:                     }
 9062:                 }
 9063:                 foreach my $field (keys(%{$cancreate{$item}})) {
 9064:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
 9065:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
 9066:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
 9067:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
 9068:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9069:                                         push(@{$changes{'cancreate'}},$item);
 9070:                                     }
 9071:                                 }
 9072:                             } else {
 9073:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9074:                                     push(@{$changes{'cancreate'}},$item);
 9075:                                 }
 9076:                             }
 9077:                         }
 9078:                     } elsif ($item eq 'selfcreateprocessing') {
 9079:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
 9080:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9081:                                 push(@{$changes{'cancreate'}},$item);
 9082:                             }
 9083:                         }
 9084:                     } else {
 9085:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
 9086:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9087:                                 push(@{$changes{'cancreate'}},$item);
 9088:                             }
 9089:                         }
 9090:                     }
 9091:                 }
 9092:             }
 9093:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
 9094:             if (ref($cancreate{$item}) eq 'ARRAY') {
 9095:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
 9096:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9097:                         push(@{$changes{'cancreate'}},$item);
 9098:                     }
 9099:                 }
 9100:             } elsif (ref($cancreate{$item}) eq 'HASH') {
 9101:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
 9102:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9103:                         push(@{$changes{'cancreate'}},$item);
 9104:                     }
 9105:                 }
 9106:             }
 9107:         } elsif ($item eq 'emailusername') {
 9108:             if (ref($cancreate{$item}) eq 'HASH') {
 9109:                 foreach my $type (keys(%{$cancreate{$item}})) {
 9110:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
 9111:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
 9112:                             if ($cancreate{$item}{$type}{$field}) {
 9113:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 9114:                                     push(@{$changes{'cancreate'}},$item);
 9115:                                 }
 9116:                                 last;
 9117:                             }
 9118:                         }
 9119:                     }
 9120:                 }
 9121:             }
 9122:         }
 9123:     }
 9124: #
 9125: # Populate %save_usercreate hash with updates to self-creation configuration.
 9126: #
 9127:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
 9128:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
 9129:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
 9130:     if (ref($cancreate{'notify'}) eq 'HASH') {
 9131:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
 9132:     }
 9133:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
 9134:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
 9135:     }
 9136:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 9137:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
 9138:     }
 9139:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
 9140:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
 9141:     }
 9142:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
 9143:     $save_usercreate{'emailrule'} = \@email_rule;
 9144: 
 9145:     my %userconfig_hash = (
 9146:             usercreation     => \%save_usercreate,
 9147:             usermodification => \%save_usermodify,
 9148:     );
 9149:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
 9150:                                              $dom);
 9151: #
 9152: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
 9153: #
 9154:     if ($putresult eq 'ok') {
 9155:         if (keys(%changes) > 0) {
 9156:             $resulttext = &mt('Changes made:').'<ul>';
 9157:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 9158:                 my %lt = &selfcreation_types();
 9159:                 foreach my $type (@{$changes{'cancreate'}}) {
 9160:                     my $chgtext;
 9161:                     if ($type eq 'selfcreate') {
 9162:                         if (@{$cancreate{$type}} == 0) {
 9163:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
 9164:                         } else {
 9165:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
 9166:                                         '<ul>';
 9167:                             foreach my $case (@{$cancreate{$type}}) {
 9168:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 9169:                             }
 9170:                             $chgtext .= '</ul>';
 9171:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 9172:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 9173:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 9174:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 9175:                                             $chgtext .= '<br />'.
 9176:                                                         '<span class="LC_warning">'.
 9177:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 9178:                                                         '</span>';
 9179:                                         }
 9180:                                     }
 9181:                                 }
 9182:                             }
 9183:                         }
 9184:                     } elsif ($type eq 'shibenv') {
 9185:                         if (keys(%{$cancreate{$type}}) == 0) {
 9186:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
 9187:                         } else {
 9188:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
 9189:                                         '<ul>';
 9190:                             foreach my $field (@shibfields) {
 9191:                                 next if ($cancreate{$type}{$field} eq '');
 9192:                                 if ($field eq 'inststatus') {
 9193:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
 9194:                                 } else {
 9195:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
 9196:                                 }
 9197:                             }
 9198:                             $chgtext .= '</ul>';
 9199:                         }  
 9200:                     } elsif ($type eq 'statustocreate') {
 9201:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 9202:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 9203:                             if (@{$cancreate{'selfcreate'}} > 0) {
 9204:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 9205:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 9206:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 9207:                                         $chgtext .= '<br />'.
 9208:                                                     '<span class="LC_warning">'.
 9209:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 9210:                                                     '</span>';
 9211:                                     }
 9212:                                 } elsif (ref($usertypes) eq 'HASH') {
 9213:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 9214:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 9215:                                     } else {
 9216:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 9217:                                     }
 9218:                                     $chgtext .= '<ul>';
 9219:                                     foreach my $case (@{$cancreate{$type}}) {
 9220:                                         if ($case eq 'default') {
 9221:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 9222:                                         } else {
 9223:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 9224:                                         }
 9225:                                     }
 9226:                                     $chgtext .= '</ul>';
 9227:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 9228:                                         $chgtext .= '<br /><span class="LC_warning">'.
 9229:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
 9230:                                                     '</span>';
 9231:                                     }
 9232:                                 }
 9233:                             } else {
 9234:                                 if (@{$cancreate{$type}} == 0) {
 9235:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 9236:                                 } else {
 9237:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 9238:                                 }
 9239:                             }
 9240:                         }
 9241:                     } elsif ($type eq 'selfcreateprocessing') {
 9242:                         my %choices = &Apache::lonlocal::texthash (
 9243:                                                                     automatic => 'Automatic approval',
 9244:                                                                     approval  => 'Queued for approval',
 9245:                                                                   );
 9246:                         if (@statuses > 1) {
 9247:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
 9248:                                         '<ul>';
 9249:                            foreach my $type (@statuses) {
 9250:                                if ($type eq 'default') {
 9251:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 9252:                                } else {
 9253:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
 9254:                                }
 9255:                            }
 9256:                            $chgtext .= '</ul>';
 9257:                         } else {
 9258:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
 9259:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
 9260:                         }
 9261:                     } elsif ($type eq 'captcha') {
 9262:                         if ($savecaptcha{$type} eq 'notused') {
 9263:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 9264:                         } else {
 9265:                             my %captchas = &captcha_phrases();
 9266:                             if ($captchas{$savecaptcha{$type}}) {
 9267:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
 9268:                             } else {
 9269:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 9270:                             }
 9271:                         }
 9272:                     } elsif ($type eq 'recaptchakeys') {
 9273:                         my ($privkey,$pubkey);
 9274:                         if (ref($savecaptcha{$type}) eq 'HASH') {
 9275:                             $pubkey = $savecaptcha{$type}{'public'};
 9276:                             $privkey = $savecaptcha{$type}{'private'};
 9277:                         }
 9278:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 9279:                         if (!$pubkey) {
 9280:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 9281:                         } else {
 9282:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 9283:                         }
 9284:                         if (!$privkey) {
 9285:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 9286:                         } else {
 9287:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 9288:                         }
 9289:                         $chgtext .= '</ul>';
 9290:                     } elsif ($type eq 'emailusername') {
 9291:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
 9292:                             if (ref($types) eq 'ARRAY') {
 9293:                                 foreach my $type (@{$types}) {
 9294:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
 9295:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
 9296:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
 9297:                                                     '<ul>';
 9298:                                             foreach my $field (@{$infofields}) {
 9299:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
 9300:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
 9301:                                                 }
 9302:                                             }
 9303:                                             $chgtext .= '</ul>';
 9304:                                         } else {
 9305:                                             $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 />';
 9306:                                         }
 9307:                                     } else {
 9308:                                         $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 />';
 9309:                                     }
 9310:                                 }
 9311:                             }
 9312:                         }
 9313:                     } elsif ($type eq 'notify') {
 9314:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
 9315:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
 9316:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
 9317:                                 if ($cancreate{'notify'}{'approval'}) {
 9318:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
 9319:                                 }
 9320:                             }
 9321:                         }
 9322:                     }
 9323:                     if ($chgtext) {
 9324:                         $resulttext .= '<li>'.$chgtext.'</li>';
 9325:                     }
 9326:                 }
 9327:             }
 9328:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 9329:                 my ($emailrules,$emailruleorder) =
 9330:                     &Apache::lonnet::inst_userrules($dom,'email');
 9331:                 my $chgtext = '<ul>';
 9332:                 foreach my $type (@email_rule) {
 9333:                     if (ref($emailrules->{$type}) eq 'HASH') {
 9334:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 9335:                     }
 9336:                 }
 9337:                 $chgtext .= '</ul>';
 9338:                 if (@email_rule > 0) {
 9339:                     $resulttext .= '<li>'.
 9340:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
 9341:                                        $chgtext.
 9342:                                    '</li>';
 9343:                 } else {
 9344:                     $resulttext .= '<li>'.
 9345:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
 9346:                                    '</li>';
 9347:                 }
 9348:             }
 9349:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
 9350:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
 9351:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9352:                 foreach my $type (@{$changes{'selfcreate'}}) {
 9353:                     my $typename = $type;
 9354:                     if (ref($usertypes) eq 'HASH') {
 9355:                         if ($usertypes->{$type} ne '') {
 9356:                             $typename = $usertypes->{$type};
 9357:                         }
 9358:                     }
 9359:                     my @modifiable;
 9360:                     $resulttext .= '<li>'.
 9361:                                     &mt('Self-creation of account by users with status: [_1]',
 9362:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
 9363:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
 9364:                     foreach my $field (@fields) {
 9365:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
 9366:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
 9367:                         }
 9368:                     }
 9369:                     if (@modifiable > 0) {
 9370:                         $resulttext .= join(', ',@modifiable);
 9371:                     } else {
 9372:                         $resulttext .= &mt('none');
 9373:                     }
 9374:                     $resulttext .= '</li>';
 9375:                 }
 9376:                 $resulttext .= '</ul></li>';
 9377:             }
 9378:             $resulttext .= '</ul>';
 9379:         } else {
 9380:             $resulttext = &mt('No changes made to self-creation settings');
 9381:         }
 9382:     } else {
 9383:         $resulttext = '<span class="LC_error">'.
 9384:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9385:     }
 9386:     if ($warningmsg ne '') {
 9387:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 9388:     }
 9389:     return $resulttext;
 9390: }
 9391: 
 9392: sub process_captcha {
 9393:     my ($container,$changes,$newsettings,$current) = @_;
 9394:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 9395:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 9396:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 9397:         $newsettings->{'captcha'} = 'original';
 9398:     }
 9399:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 9400:         if ($container eq 'cancreate') {
 9401:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9402:                 push(@{$changes->{'cancreate'}},'captcha');
 9403:             } elsif (!defined($changes->{'cancreate'})) {
 9404:                 $changes->{'cancreate'} = ['captcha'];
 9405:             }
 9406:         } else {
 9407:             $changes->{'captcha'} = 1;
 9408:         }
 9409:     }
 9410:     my ($newpub,$newpriv,$currpub,$currpriv);
 9411:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 9412:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 9413:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 9414:         $newpub =~ s/[^\w\-]//g;
 9415:         $newpriv =~ s/[^\w\-]//g;
 9416:         $newsettings->{'recaptchakeys'} = {
 9417:                                              public  => $newpub,
 9418:                                              private => $newpriv,
 9419:                                           };
 9420:     }
 9421:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 9422:         $currpub = $current->{'recaptchakeys'}{'public'};
 9423:         $currpriv = $current->{'recaptchakeys'}{'private'};
 9424:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 9425:             $newsettings->{'recaptchakeys'} = {
 9426:                                                  public  => '',
 9427:                                                  private => '',
 9428:                                               }
 9429:         }
 9430:     }
 9431:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 9432:         if ($container eq 'cancreate') {
 9433:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 9434:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 9435:             } elsif (!defined($changes->{'cancreate'})) {
 9436:                 $changes->{'cancreate'} = ['recaptchakeys'];
 9437:             }
 9438:         } else {
 9439:             $changes->{'recaptchakeys'} = 1;
 9440:         }
 9441:     }
 9442:     return;
 9443: }
 9444: 
 9445: sub modify_usermodification {
 9446:     my ($dom,%domconfig) = @_;
 9447:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
 9448:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 9449:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 9450:             if ($key eq 'selfcreate') {
 9451:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
 9452:             } else {  
 9453:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 9454:             }
 9455:         }
 9456:     }
 9457:     my @contexts = ('author','course');
 9458:     my %context_title = (
 9459:                            author => 'In author context',
 9460:                            course => 'In course context',
 9461:                         );
 9462:     my @fields = ('lastname','firstname','middlename','generation',
 9463:                   'permanentemail','id');
 9464:     my %roles = (
 9465:                   author => ['ca','aa'],
 9466:                   course => ['st','ep','ta','in','cr'],
 9467:                 );
 9468:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9469:     foreach my $context (@contexts) {
 9470:         foreach my $role (@{$roles{$context}}) {
 9471:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 9472:             foreach my $item (@fields) {
 9473:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 9474:                     $modifyhash{$context}{$role}{$item} = 1;
 9475:                 } else {
 9476:                     $modifyhash{$context}{$role}{$item} = 0;
 9477:                 }
 9478:             }
 9479:         }
 9480:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 9481:             foreach my $role (@{$roles{$context}}) {
 9482:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 9483:                     foreach my $field (@fields) {
 9484:                         if ($modifyhash{$context}{$role}{$field} ne 
 9485:                                 $curr_usermodification{$context}{$role}{$field}) {
 9486:                             push(@{$changes{$context}},$role);
 9487:                             last;
 9488:                         }
 9489:                     }
 9490:                 }
 9491:             }
 9492:         } else {
 9493:             foreach my $context (@contexts) {
 9494:                 foreach my $role (@{$roles{$context}}) {
 9495:                     push(@{$changes{$context}},$role);
 9496:                 }
 9497:             }
 9498:         }
 9499:     }
 9500:     my %usermodification_hash =  (
 9501:                                    usermodification => \%modifyhash,
 9502:                                  );
 9503:     my $putresult = &Apache::lonnet::put_dom('configuration',
 9504:                                              \%usermodification_hash,$dom);
 9505:     if ($putresult eq 'ok') {
 9506:         if (keys(%changes) > 0) {
 9507:             $resulttext = &mt('Changes made: ').'<ul>';
 9508:             foreach my $context (@contexts) {
 9509:                 if (ref($changes{$context}) eq 'ARRAY') {
 9510:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 9511:                     if (ref($changes{$context}) eq 'ARRAY') {
 9512:                         foreach my $role (@{$changes{$context}}) {
 9513:                             my $rolename;
 9514:                             if ($role eq 'cr') {
 9515:                                 $rolename = &mt('Custom');
 9516:                             } else {
 9517:                                 $rolename = &Apache::lonnet::plaintext($role);
 9518:                             }
 9519:                             my @modifiable;
 9520:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 9521:                             foreach my $field (@fields) {
 9522:                                 if ($modifyhash{$context}{$role}{$field}) {
 9523:                                     push(@modifiable,$fieldtitles{$field});
 9524:                                 }
 9525:                             }
 9526:                             if (@modifiable > 0) {
 9527:                                 $resulttext .= join(', ',@modifiable);
 9528:                             } else {
 9529:                                 $resulttext .= &mt('none'); 
 9530:                             }
 9531:                             $resulttext .= '</li>';
 9532:                         }
 9533:                         $resulttext .= '</ul></li>';
 9534:                     }
 9535:                 }
 9536:             }
 9537:             $resulttext .= '</ul>';
 9538:         } else {
 9539:             $resulttext = &mt('No changes made to user modification settings');
 9540:         }
 9541:     } else {
 9542:         $resulttext = '<span class="LC_error">'.
 9543:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9544:     }
 9545:     return $resulttext;
 9546: }
 9547: 
 9548: sub modify_defaults {
 9549:     my ($dom,$lastactref,%domconfig) = @_;
 9550:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 9551:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9552:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 9553:     my @authtypes = ('internal','krb4','krb5','localauth');
 9554:     foreach my $item (@items) {
 9555:         $newvalues{$item} = $env{'form.'.$item};
 9556:         if ($item eq 'auth_def') {
 9557:             if ($newvalues{$item} ne '') {
 9558:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 9559:                     push(@errors,$item);
 9560:                 }
 9561:             }
 9562:         } elsif ($item eq 'lang_def') {
 9563:             if ($newvalues{$item} ne '') {
 9564:                 if ($newvalues{$item} =~ /^(\w+)/) {
 9565:                     my $langcode = $1;
 9566:                     if ($langcode ne 'x_chef') {
 9567:                         if (code2language($langcode) eq '') {
 9568:                             push(@errors,$item);
 9569:                         }
 9570:                     }
 9571:                 } else {
 9572:                     push(@errors,$item);
 9573:                 }
 9574:             }
 9575:         } elsif ($item eq 'timezone_def') {
 9576:             if ($newvalues{$item} ne '') {
 9577:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 9578:                     push(@errors,$item);   
 9579:                 }
 9580:             }
 9581:         } elsif ($item eq 'datelocale_def') {
 9582:             if ($newvalues{$item} ne '') {
 9583:                 my @datelocale_ids = DateTime::Locale->ids();
 9584:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 9585:                     push(@errors,$item);
 9586:                 }
 9587:             }
 9588:         } elsif ($item eq 'portal_def') {
 9589:             if ($newvalues{$item} ne '') {
 9590:                 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])\/?$/) {
 9591:                     push(@errors,$item);
 9592:                 }
 9593:             }
 9594:         }
 9595:         if (grep(/^\Q$item\E$/,@errors)) {
 9596:             $newvalues{$item} = $domdefaults{$item};
 9597:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 9598:             $changes{$item} = 1;
 9599:         }
 9600:         $domdefaults{$item} = $newvalues{$item};
 9601:     }
 9602:     my %defaults_hash = (
 9603:                          defaults => \%newvalues,
 9604:                         );
 9605:     my $title = &defaults_titles();
 9606: 
 9607:     my $currinststatus;
 9608:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 9609:         $currinststatus = $domconfig{'inststatus'};
 9610:     } else {
 9611:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9612:         $currinststatus = {
 9613:                              inststatustypes => $usertypes,
 9614:                              inststatusorder => $types,
 9615:                              inststatusguest => [],
 9616:                           };
 9617:     }
 9618:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
 9619:     my @allpos;
 9620:     my %guests;
 9621:     my %alltypes;
 9622:     my ($currtitles,$currguests,$currorder);
 9623:     if (ref($currinststatus) eq 'HASH') {
 9624:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
 9625:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
 9626:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
 9627:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
 9628:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
 9629:                     }
 9630:                 }
 9631:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
 9632:                     my $position = $env{'form.inststatus_pos_'.$type};
 9633:                     $position =~ s/\D+//g;
 9634:                     $allpos[$position] = $type;
 9635:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
 9636:                     $alltypes{$type} =~ s/`//g;
 9637:                     if ($env{'form.inststatus_guest_'.$type}) {
 9638:                         $guests{$type} = 1;
 9639:                     }
 9640:                 }
 9641:             }
 9642:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
 9643:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
 9644:             }
 9645:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
 9646:             $currtitles =~ s/,$//;
 9647:         }
 9648:     }
 9649:     if ($env{'form.addinststatus'}) {
 9650:         my $newtype = $env{'form.addinststatus'};
 9651:         $newtype =~ s/\W//g;
 9652:         unless (exists($alltypes{$newtype})) {
 9653:             if ($env{'form.addinststatus_guest'}) {
 9654:                 $guests{$newtype} = 1;
 9655:             }
 9656:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
 9657:             $alltypes{$newtype} =~ s/`//g; 
 9658:             my $position = $env{'form.addinststatus_pos'};
 9659:             $position =~ s/\D+//g;
 9660:             if ($position ne '') {
 9661:                 $allpos[$position] = $newtype;
 9662:             }
 9663:         }
 9664:     }
 9665:     my (@orderedstatus,@orderedguests);
 9666:     foreach my $type (@allpos) {
 9667:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
 9668:             push(@orderedstatus,$type);
 9669:             if ($guests{$type}) {
 9670:                 push(@orderedguests,$type);
 9671:             }
 9672:         }
 9673:     }
 9674:     foreach my $type (keys(%alltypes)) {
 9675:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
 9676:             delete($alltypes{$type});
 9677:         }
 9678:     }
 9679:     $defaults_hash{'inststatus'} = {
 9680:                                      inststatustypes => \%alltypes,
 9681:                                      inststatusorder => \@orderedstatus,
 9682:                                      inststatusguest => \@orderedguests,
 9683:                                    };
 9684:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
 9685:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 9686:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
 9687:         }
 9688:     }
 9689:     if ($currorder ne join(',',@orderedstatus)) {
 9690:         $changes{'inststatus'}{'inststatusorder'} = 1;
 9691:     }
 9692:     if ($currguests ne join(',',@orderedguests)) {
 9693:         $changes{'inststatus'}{'inststatusguest'} = 1;
 9694:     }
 9695:     my $newtitles;
 9696:     foreach my $item (@orderedstatus) {
 9697:         $newtitles .= $alltypes{$item}.',';
 9698:     }
 9699:     $newtitles =~ s/,$//;
 9700:     if ($currtitles ne $newtitles) {
 9701:         $changes{'inststatus'}{'inststatustypes'} = 1;
 9702:     }
 9703:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 9704:                                              $dom);
 9705:     if ($putresult eq 'ok') {
 9706:         if (keys(%changes) > 0) {
 9707:             $resulttext = &mt('Changes made:').'<ul>';
 9708:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 9709:             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";
 9710:             foreach my $item (sort(keys(%changes))) {
 9711:                 if ($item eq 'inststatus') {
 9712:                     if (ref($changes{'inststatus'}) eq 'HASH') {
 9713:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
 9714:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
 9715:                             foreach my $type (@orderedstatus) { 
 9716:                                 $resulttext .= $alltypes{$type}.', ';
 9717:                             }
 9718:                             $resulttext =~ s/, $//;
 9719:                             $resulttext .= '</li>';
 9720:                         }
 9721:                         if ($changes{'inststatus'}{'inststatusguest'}) {
 9722:                             $resulttext .= '<li>'; 
 9723:                             if (@orderedguests) {
 9724:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
 9725:                                 foreach my $type (@orderedguests) {
 9726:                                     $resulttext .= $alltypes{$type}.', ';
 9727:                                 }
 9728:                                 $resulttext =~ s/, $//;
 9729:                             } else {
 9730:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
 9731:                             }
 9732:                             $resulttext .= '</li>';
 9733:                         }
 9734:                     }
 9735:                 } else {
 9736:                     my $value = $env{'form.'.$item};
 9737:                     if ($value eq '') {
 9738:                         $value = &mt('none');
 9739:                     } elsif ($item eq 'auth_def') {
 9740:                         my %authnames = &authtype_names();
 9741:                         my %shortauth = (
 9742:                                           internal   => 'int',
 9743:                                           krb4       => 'krb4',
 9744:                                           krb5       => 'krb5',
 9745:                                           localauth  => 'loc',
 9746:                         );
 9747:                         $value = $authnames{$shortauth{$value}};
 9748:                     }
 9749:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 9750:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
 9751:                 }
 9752:             }
 9753:             $resulttext .= '</ul>';
 9754:             $mailmsgtext .= "\n";
 9755:             my $cachetime = 24*60*60;
 9756:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9757:             if (ref($lastactref) eq 'HASH') {
 9758:                 $lastactref->{'domdefaults'} = 1;
 9759:             }
 9760:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 9761:                 my $notify = 1;
 9762:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 9763:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 9764:                         $notify = 0;
 9765:                     }
 9766:                 }
 9767:                 if ($notify) {
 9768:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 9769:                                                "LON-CAPA Domain Settings Change - $dom",
 9770:                                                $mailmsgtext);
 9771:                 }
 9772:             }
 9773:         } else {
 9774:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 9775:         }
 9776:     } else {
 9777:         $resulttext = '<span class="LC_error">'.
 9778:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9779:     }
 9780:     if (@errors > 0) {
 9781:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 9782:         foreach my $item (@errors) {
 9783:             $resulttext .= ' "'.$title->{$item}.'",';
 9784:         }
 9785:         $resulttext =~ s/,$//;
 9786:     }
 9787:     return $resulttext;
 9788: }
 9789: 
 9790: sub modify_scantron {
 9791:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 9792:     my ($resulttext,%confhash,%changes,$errors);
 9793:     my $custom = 'custom.tab';
 9794:     my $default = 'default.tab';
 9795:     my $servadm = $r->dir_config('lonAdmEMail');
 9796:     my ($configuserok,$author_ok,$switchserver) = 
 9797:         &config_check($dom,$confname,$servadm);
 9798:     if ($env{'form.scantronformat.filename'} ne '') {
 9799:         my $error;
 9800:         if ($configuserok eq 'ok') {
 9801:             if ($switchserver) {
 9802:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 9803:             } else {
 9804:                 if ($author_ok eq 'ok') {
 9805:                     my ($result,$scantronurl) =
 9806:                         &publishlogo($r,'upload','scantronformat',$dom,
 9807:                                      $confname,'scantron','','',$custom);
 9808:                     if ($result eq 'ok') {
 9809:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 9810:                         $changes{'scantronformat'} = 1;
 9811:                     } else {
 9812:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 9813:                     }
 9814:                 } else {
 9815:                     $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);
 9816:                 }
 9817:             }
 9818:         } else {
 9819:             $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);
 9820:         }
 9821:         if ($error) {
 9822:             &Apache::lonnet::logthis($error);
 9823:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9824:         }
 9825:     }
 9826:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 9827:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 9828:             if ($env{'form.scantronformat_del'}) {
 9829:                 $confhash{'scantron'}{'scantronformat'} = '';
 9830:                 $changes{'scantronformat'} = 1;
 9831:             }
 9832:         }
 9833:     }
 9834:     if (keys(%confhash) > 0) {
 9835:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 9836:                                                  $dom);
 9837:         if ($putresult eq 'ok') {
 9838:             if (keys(%changes) > 0) {
 9839:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 9840:                     $resulttext = &mt('Changes made:').'<ul>';
 9841:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 9842:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 9843:                     } else {
 9844:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 9845:                     }
 9846:                     $resulttext .= '</ul>';
 9847:                 } else {
 9848:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 9849:                 }
 9850:                 $resulttext .= '</ul>';
 9851:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 9852:                 if (ref($lastactref) eq 'HASH') {
 9853:                     $lastactref->{'domainconfig'} = 1;
 9854:                 }
 9855:             } else {
 9856:                 $resulttext = &mt('No changes made to bubblesheet format file');
 9857:             }
 9858:         } else {
 9859:             $resulttext = '<span class="LC_error">'.
 9860:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 9861:         }
 9862:     } else {
 9863:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 9864:     }
 9865:     if ($errors) {
 9866:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9867:                        $errors.'</ul>';
 9868:     }
 9869:     return $resulttext;
 9870: }
 9871: 
 9872: sub modify_coursecategories {
 9873:     my ($dom,$lastactref,%domconfig) = @_;
 9874:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 9875:         $cathash);
 9876:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 9877:     my @catitems = ('unauth','auth');
 9878:     my @cattypes = ('std','domonly','codesrch','none');
 9879:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 9880:         $cathash = $domconfig{'coursecategories'}{'cats'};
 9881:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 9882:             $changes{'togglecats'} = 1;
 9883:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 9884:         }
 9885:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 9886:             $changes{'categorize'} = 1;
 9887:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 9888:         }
 9889:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 9890:             $changes{'togglecatscomm'} = 1;
 9891:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 9892:         }
 9893:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 9894:             $changes{'categorizecomm'} = 1;
 9895:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 9896:         }
 9897:         foreach my $item (@catitems) {
 9898:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9899:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
 9900:                     $changes{$item} = 1;
 9901:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9902:                 }
 9903:             }
 9904:         }
 9905:     } else {
 9906:         $changes{'togglecats'} = 1;
 9907:         $changes{'categorize'} = 1;
 9908:         $changes{'togglecatscomm'} = 1;
 9909:         $changes{'categorizecomm'} = 1;
 9910:         $domconfig{'coursecategories'} = {
 9911:                                              togglecats => $env{'form.togglecats'},
 9912:                                              categorize => $env{'form.categorize'},
 9913:                                              togglecatscomm => $env{'form.togglecatscomm'},
 9914:                                              categorizecomm => $env{'form.categorizecomm'},
 9915:                                          };
 9916:         foreach my $item (@catitems) {
 9917:             if ($env{'form.coursecat_'.$item} ne 'std') {
 9918:                 $changes{$item} = 1;
 9919:             }
 9920:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
 9921:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
 9922:             }
 9923:         }
 9924:     }
 9925:     if (ref($cathash) eq 'HASH') {
 9926:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 9927:             push (@deletecategory,'instcode::0');
 9928:         }
 9929:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 9930:             push(@deletecategory,'communities::0');
 9931:         }
 9932:     }
 9933:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 9934:     if (ref($cathash) eq 'HASH') {
 9935:         if (@deletecategory > 0) {
 9936:             #FIXME Need to remove category from all courses using a deleted category 
 9937:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 9938:             foreach my $item (@deletecategory) {
 9939:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 9940:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 9941:                     $deletions{$item} = 1;
 9942:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 9943:                 }
 9944:             }
 9945:         }
 9946:         foreach my $item (keys(%{$cathash})) {
 9947:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 9948:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 9949:                 $reorderings{$item} = 1;
 9950:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 9951:             }
 9952:             if ($env{'form.addcategory_name_'.$item} ne '') {
 9953:                 my $newcat = $env{'form.addcategory_name_'.$item};
 9954:                 my $newdepth = $depth+1;
 9955:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9956:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 9957:                 $adds{$newitem} = 1; 
 9958:             }
 9959:             if ($env{'form.subcat_'.$item} ne '') {
 9960:                 my $newcat = $env{'form.subcat_'.$item};
 9961:                 my $newdepth = $depth+1;
 9962:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 9963:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 9964:                 $adds{$newitem} = 1;
 9965:             }
 9966:         }
 9967:     }
 9968:     if ($env{'form.instcode'} eq '1') {
 9969:         if (ref($cathash) eq 'HASH') {
 9970:             my $newitem = 'instcode::0';
 9971:             if ($cathash->{$newitem} eq '') {  
 9972:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9973:                 $adds{$newitem} = 1;
 9974:             }
 9975:         } else {
 9976:             my $newitem = 'instcode::0';
 9977:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 9978:             $adds{$newitem} = 1;
 9979:         }
 9980:     }
 9981:     if ($env{'form.communities'} eq '1') {
 9982:         if (ref($cathash) eq 'HASH') {
 9983:             my $newitem = 'communities::0';
 9984:             if ($cathash->{$newitem} eq '') {
 9985:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9986:                 $adds{$newitem} = 1;
 9987:             }
 9988:         } else {
 9989:             my $newitem = 'communities::0';
 9990:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 9991:             $adds{$newitem} = 1;
 9992:         }
 9993:     }
 9994:     if ($env{'form.addcategory_name'} ne '') {
 9995:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 9996:             ($env{'form.addcategory_name'} ne 'communities')) {
 9997:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 9998:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 9999:             $adds{$newitem} = 1;
10000:         }
10001:     }
10002:     my $putresult;
10003:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
10004:         if (keys(%deletions) > 0) {
10005:             foreach my $key (keys(%deletions)) {
10006:                 if ($predelallitems{$key} ne '') {
10007:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
10008:                 }
10009:             }
10010:         }
10011:         my (@chkcats,@chktrails,%chkallitems);
10012:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
10013:         if (ref($chkcats[0]) eq 'ARRAY') {
10014:             my $depth = 0;
10015:             my $chg = 0;
10016:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
10017:                 my $name = $chkcats[0][$i];
10018:                 my $item;
10019:                 if ($name eq '') {
10020:                     $chg ++;
10021:                 } else {
10022:                     $item = &escape($name).'::0';
10023:                     if ($chg) {
10024:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
10025:                     }
10026:                     $depth ++; 
10027:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
10028:                     $depth --;
10029:                 }
10030:             }
10031:         }
10032:     }
10033:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
10034:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
10035:         if ($putresult eq 'ok') {
10036:             my %title = (
10037:                          togglecats     => 'Show/Hide a course in catalog',
10038:                          categorize     => 'Assign a category to a course',
10039:                          togglecatscomm => 'Show/Hide a community in catalog',
10040:                          categorizecomm => 'Assign a category to a community',
10041:                         );
10042:             my %level = (
10043:                          dom  => 'set in Domain ("Modify Course/Community")',
10044:                          crs  => 'set in Course ("Course Configuration")',
10045:                          comm => 'set in Community ("Community Configuration")',
10046:                          none     => 'No catalog',
10047:                          std      => 'Standard catalog',
10048:                          domonly  => 'Domain-only catalog',
10049:                          codesrch => 'Code search form',
10050:                         );
10051:             $resulttext = &mt('Changes made:').'<ul>';
10052:             if ($changes{'togglecats'}) {
10053:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
10054:             }
10055:             if ($changes{'categorize'}) {
10056:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
10057:             }
10058:             if ($changes{'togglecatscomm'}) {
10059:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
10060:             }
10061:             if ($changes{'categorizecomm'}) {
10062:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
10063:             }
10064:             if ($changes{'unauth'}) {
10065:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
10066:             }
10067:             if ($changes{'auth'}) {
10068:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
10069:             }
10070:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
10071:                 my $cathash;
10072:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
10073:                     $cathash = $domconfig{'coursecategories'}{'cats'};
10074:                 } else {
10075:                     $cathash = {};
10076:                 } 
10077:                 my (@cats,@trails,%allitems);
10078:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
10079:                 if (keys(%deletions) > 0) {
10080:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
10081:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
10082:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
10083:                     }
10084:                     $resulttext .= '</ul></li>';
10085:                 }
10086:                 if (keys(%reorderings) > 0) {
10087:                     my %sort_by_trail;
10088:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
10089:                     foreach my $key (keys(%reorderings)) {
10090:                         if ($allitems{$key} ne '') {
10091:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
10092:                         }
10093:                     }
10094:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
10095:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
10096:                     }
10097:                     $resulttext .= '</ul></li>';
10098:                 }
10099:                 if (keys(%adds) > 0) {
10100:                     my %sort_by_trail;
10101:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
10102:                     foreach my $key (keys(%adds)) {
10103:                         if ($allitems{$key} ne '') {
10104:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
10105:                         }
10106:                     }
10107:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
10108:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
10109:                     }
10110:                     $resulttext .= '</ul></li>';
10111:                 }
10112:             }
10113:             $resulttext .= '</ul>';
10114:             if ($changes{'unauth'} || $changes{'auth'}) {
10115:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
10116:                 if ($changes{'auth'}) {
10117:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
10118:                 }
10119:                 if ($changes{'unauth'}) {
10120:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
10121:                 }
10122:                 my $cachetime = 24*60*60;
10123:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10124:                 if (ref($lastactref) eq 'HASH') {
10125:                     $lastactref->{'domdefaults'} = 1;
10126:                 }
10127:             }
10128:         } else {
10129:             $resulttext = '<span class="LC_error">'.
10130:                           &mt('An error occurred: [_1]',$putresult).'</span>';
10131:         }
10132:     } else {
10133:         $resulttext = &mt('No changes made to course and community categories');
10134:     }
10135:     return $resulttext;
10136: }
10137: 
10138: sub modify_serverstatuses {
10139:     my ($dom,%domconfig) = @_;
10140:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
10141:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
10142:         %currserverstatus = %{$domconfig{'serverstatuses'}};
10143:     }
10144:     my @pages = &serverstatus_pages();
10145:     foreach my $type (@pages) {
10146:         $newserverstatus{$type}{'namedusers'} = '';
10147:         $newserverstatus{$type}{'machines'} = '';
10148:         if (defined($env{'form.'.$type.'_namedusers'})) {
10149:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
10150:             my @okusers;
10151:             foreach my $user (@users) {
10152:                 my ($uname,$udom) = split(/:/,$user);
10153:                 if (($udom =~ /^$match_domain$/) &&   
10154:                     (&Apache::lonnet::domain($udom)) &&
10155:                     ($uname =~ /^$match_username$/)) {
10156:                     if (!grep(/^\Q$user\E/,@okusers)) {
10157:                         push(@okusers,$user);
10158:                     }
10159:                 }
10160:             }
10161:             if (@okusers > 0) {
10162:                  @okusers = sort(@okusers);
10163:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
10164:             }
10165:         }
10166:         if (defined($env{'form.'.$type.'_machines'})) {
10167:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
10168:             my @okmachines;
10169:             foreach my $ip (@machines) {
10170:                 my @parts = split(/\./,$ip);
10171:                 next if (@parts < 4);
10172:                 my $badip = 0;
10173:                 for (my $i=0; $i<4; $i++) {
10174:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
10175:                         $badip = 1;
10176:                         last;
10177:                     }
10178:                 }
10179:                 if (!$badip) {
10180:                     push(@okmachines,$ip);     
10181:                 }
10182:             }
10183:             @okmachines = sort(@okmachines);
10184:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
10185:         }
10186:     }
10187:     my %serverstatushash =  (
10188:                                 serverstatuses => \%newserverstatus,
10189:                             );
10190:     foreach my $type (@pages) {
10191:         foreach my $setting ('namedusers','machines') {
10192:             my (@current,@new);
10193:             if (ref($currserverstatus{$type}) eq 'HASH') {
10194:                 if ($currserverstatus{$type}{$setting} ne '') { 
10195:                     @current = split(/,/,$currserverstatus{$type}{$setting});
10196:                 }
10197:             }
10198:             if ($newserverstatus{$type}{$setting} ne '') {
10199:                 @new = split(/,/,$newserverstatus{$type}{$setting});
10200:             }
10201:             if (@current > 0) {
10202:                 if (@new > 0) {
10203:                     foreach my $item (@current) {
10204:                         if (!grep(/^\Q$item\E$/,@new)) {
10205:                             $changes{$type}{$setting} = 1;
10206:                             last;
10207:                         }
10208:                     }
10209:                     foreach my $item (@new) {
10210:                         if (!grep(/^\Q$item\E$/,@current)) {
10211:                             $changes{$type}{$setting} = 1;
10212:                             last;
10213:                         }
10214:                     }
10215:                 } else {
10216:                     $changes{$type}{$setting} = 1;
10217:                 }
10218:             } elsif (@new > 0) {
10219:                 $changes{$type}{$setting} = 1;
10220:             }
10221:         }
10222:     }
10223:     if (keys(%changes) > 0) {
10224:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
10225:         my $putresult = &Apache::lonnet::put_dom('configuration',
10226:                                                  \%serverstatushash,$dom);
10227:         if ($putresult eq 'ok') {
10228:             $resulttext .= &mt('Changes made:').'<ul>';
10229:             foreach my $type (@pages) {
10230:                 if (ref($changes{$type}) eq 'HASH') {
10231:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
10232:                     if ($changes{$type}{'namedusers'}) {
10233:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
10234:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
10235:                         } else {
10236:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
10237:                         }
10238:                     }
10239:                     if ($changes{$type}{'machines'}) {
10240:                         if ($newserverstatus{$type}{'machines'} eq '') {
10241:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
10242:                         } else {
10243:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
10244:                         }
10245: 
10246:                     }
10247:                     $resulttext .= '</ul></li>';
10248:                 }
10249:             }
10250:             $resulttext .= '</ul>';
10251:         } else {
10252:             $resulttext = '<span class="LC_error">'.
10253:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
10254: 
10255:         }
10256:     } else {
10257:         $resulttext = &mt('No changes made to access to server status pages');
10258:     }
10259:     return $resulttext;
10260: }
10261: 
10262: sub modify_helpsettings {
10263:     my ($r,$dom,$confname,%domconfig) = @_;
10264:     my ($resulttext,$errors,%changes,%helphash);
10265:     my %defaultchecked = ('submitbugs' => 'on');
10266:     my @offon = ('off','on');
10267:     my @toggles = ('submitbugs');
10268:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
10269:         foreach my $item (@toggles) {
10270:             if ($defaultchecked{$item} eq 'on') { 
10271:                 if ($domconfig{'helpsettings'}{$item} eq '') {
10272:                     if ($env{'form.'.$item} eq '0') {
10273:                         $changes{$item} = 1;
10274:                     }
10275:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
10276:                     $changes{$item} = 1;
10277:                 }
10278:             } elsif ($defaultchecked{$item} eq 'off') {
10279:                 if ($domconfig{'helpsettings'}{$item} eq '') {
10280:                     if ($env{'form.'.$item} eq '1') {
10281:                         $changes{$item} = 1;
10282:                     }
10283:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
10284:                     $changes{$item} = 1;
10285:                 }
10286:             }
10287:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
10288:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
10289:             }
10290:         }
10291:     }
10292:     my $putresult;
10293:     if (keys(%changes) > 0) {
10294:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
10295:         if ($putresult eq 'ok') {
10296:             $resulttext = &mt('Changes made:').'<ul>';
10297:             foreach my $item (sort(keys(%changes))) {
10298:                 if ($item eq 'submitbugs') {
10299:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
10300:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
10301:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
10302:                 }
10303:             }
10304:             $resulttext .= '</ul>';
10305:         } else {
10306:             $resulttext = &mt('No changes made to help settings');
10307:             $errors .= '<li><span class="LC_error">'.
10308:                        &mt('An error occurred storing the settings: [_1]',
10309:                            $putresult).'</span></li>';
10310:         }
10311:     }
10312:     if ($errors) {
10313:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
10314:                        $errors.'</ul>';
10315:     }
10316:     return $resulttext;
10317: }
10318: 
10319: sub modify_coursedefaults {
10320:     my ($dom,$lastactref,%domconfig) = @_;
10321:     my ($resulttext,$errors,%changes,%defaultshash);
10322:     my %defaultchecked = (
10323:                            'uselcmath'       => 'on',
10324:                            'usejsme'         => 'on'
10325:                          );
10326:     my @toggles = ('uselcmath','usejsme');
10327:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
10328:                    'uploadquota_community','uploadquota_textbook');
10329:     my @types = ('official','unofficial','community','textbook');
10330:     my %staticdefaults = (
10331:                            anonsurvey_threshold => 10,
10332:                            uploadquota          => 500,
10333:                            postsubmit           => 60,
10334:                          );
10335: 
10336:     $defaultshash{'coursedefaults'} = {};
10337: 
10338:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
10339:         if ($domconfig{'coursedefaults'} eq '') {
10340:             $domconfig{'coursedefaults'} = {};
10341:         }
10342:     }
10343: 
10344:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
10345:         foreach my $item (@toggles) {
10346:             if ($defaultchecked{$item} eq 'on') {
10347:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
10348:                     ($env{'form.'.$item} eq '0')) {
10349:                     $changes{$item} = 1;
10350:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
10351:                     $changes{$item} = 1;
10352:                 }
10353:             } elsif ($defaultchecked{$item} eq 'off') {
10354:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
10355:                     ($env{'form.'.$item} eq '1')) {
10356:                     $changes{$item} = 1;
10357:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
10358:                     $changes{$item} = 1;
10359:                 }
10360:             }
10361:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
10362:         }
10363:         foreach my $item (@numbers) {
10364:             my ($currdef,$newdef);
10365:             $newdef = $env{'form.'.$item};
10366:             if ($item eq 'anonsurvey_threshold') {
10367:                 $currdef = $domconfig{'coursedefaults'}{$item};
10368:                 $newdef =~ s/\D//g;
10369:                 if ($newdef eq '' || $newdef < 1) {
10370:                     $newdef = 1;
10371:                 }
10372:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
10373:             } else {
10374:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
10375:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10376:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
10377:                 }
10378:                 $newdef =~ s/[^\w.\-]//g;
10379:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
10380:             }
10381:             if ($currdef ne $newdef) {
10382:                 my $staticdef;
10383:                 if ($item eq 'anonsurvey_threshold') {
10384:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
10385:                         $changes{$item} = 1;
10386:                     }
10387:                 } else {
10388:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
10389:                         $changes{'uploadquota'} = 1;
10390:                     }
10391:                 }
10392:             }
10393:         }
10394:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
10395:         my @currclonecode;
10396:         if (ref($currclone) eq 'HASH') {
10397:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
10398:                 @currclonecode = @{$currclone->{'instcode'}};
10399:             }
10400:         }
10401:         my $newclone;
10402:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
10403:             $newclone = $env{'form.canclone'};
10404:         }
10405:         if ($newclone eq 'instcode') {
10406:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
10407:             my (%codedefaults,@code_order,@clonecode);
10408:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
10409:                                                     \@code_order);
10410:             foreach my $item (@code_order) {
10411:                 if (grep(/^\Q$item\E$/,@newcodes)) {
10412:                     push(@clonecode,$item);
10413:                 }
10414:             }
10415:             if (@clonecode) {
10416:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
10417:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
10418:                 if (@diffs) {
10419:                     $changes{'canclone'} = 1;
10420:                 }
10421:             } else {
10422:                 $newclone eq '';
10423:             }
10424:         } elsif ($newclone ne '') {
10425:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
10426:         }
10427:         if ($newclone ne $currclone) {
10428:             $changes{'canclone'} = 1;
10429:         }
10430:         my %credits;
10431:         foreach my $type (@types) {
10432:             unless ($type eq 'community') {
10433:                 $credits{$type} = $env{'form.'.$type.'_credits'};
10434:                 $credits{$type} =~ s/[^\d.]+//g;
10435:             }
10436:         }
10437:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
10438:             ($env{'form.coursecredits'} eq '1')) {
10439:             $changes{'coursecredits'} = 1;
10440:             foreach my $type (keys(%credits)) {
10441:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
10442:             }
10443:         } else {
10444:             if ($env{'form.coursecredits'} eq '1') {
10445:                 foreach my $type (@types) {
10446:                     unless ($type eq 'community') {
10447:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
10448:                             $changes{'coursecredits'} = 1;
10449:                         }
10450:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
10451:                     }
10452:                 }
10453:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10454:                 foreach my $type (@types) {
10455:                     unless ($type eq 'community') {
10456:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
10457:                             $changes{'coursecredits'} = 1;
10458:                             last;
10459:                         }
10460:                     }
10461:                 }
10462:             }
10463:         }
10464:         if ($env{'form.postsubmit'} eq '1') {
10465:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
10466:             my %currtimeout;
10467:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
10468:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
10469:                     $changes{'postsubmit'} = 1;
10470:                 }
10471:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
10472:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
10473:                 }
10474:             } else {
10475:                 $changes{'postsubmit'} = 1;
10476:             }
10477:             foreach my $type (@types) {
10478:                 my $timeout = $env{'form.'.$type.'_timeout'};
10479:                 $timeout =~ s/\D//g;
10480:                 if ($timeout == $staticdefaults{'postsubmit'}) {
10481:                     $timeout = '';
10482:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
10483:                     $timeout = '0';
10484:                 }
10485:                 unless ($timeout eq '') {
10486:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
10487:                 }
10488:                 if (exists($currtimeout{$type})) {
10489:                     if ($timeout ne $currtimeout{$type}) {
10490:                         $changes{'postsubmit'} = 1;
10491:                     }
10492:                 } elsif ($timeout ne '') {
10493:                     $changes{'postsubmit'} = 1;
10494:                 }
10495:             }
10496:         } else {
10497:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
10498:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
10499:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
10500:                     $changes{'postsubmit'} = 1;
10501:                 }
10502:             } else {
10503:                 $changes{'postsubmit'} = 1;
10504:             }
10505:         }
10506:     }
10507:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
10508:                                              $dom);
10509:     if ($putresult eq 'ok') {
10510:         if (keys(%changes) > 0) {
10511:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10512:             if (($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
10513:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
10514:                 ($changes{'canclone'})) {
10515:                 foreach my $item ('uselcmath','usejsme') {
10516:                     if ($changes{$item}) {
10517:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
10518:                     }
10519:                 }
10520:                 if ($changes{'coursecredits'}) {
10521:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10522:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
10523:                             $domdefaults{$type.'credits'} =
10524:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
10525:                         }
10526:                     }
10527:                 }
10528:                 if ($changes{'postsubmit'}) {
10529:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
10530:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
10531:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
10532:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
10533:                                 $domdefaults{$type.'postsubtimeout'} =
10534:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
10535:                             }
10536:                         }
10537:                     }
10538:                 }
10539:                 if ($changes{'uploadquota'}) {
10540:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10541:                         foreach my $type (@types) {
10542:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
10543:                         }
10544:                     }
10545:                 }
10546:                 if ($changes{'canclone'}) {
10547:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
10548:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
10549:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
10550:                             if (@clonecodes) {
10551:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
10552:                             }
10553:                         }
10554:                     } else {
10555:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
10556:                     }
10557:                 }
10558:                 my $cachetime = 24*60*60;
10559:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10560:                 if (ref($lastactref) eq 'HASH') {
10561:                     $lastactref->{'domdefaults'} = 1;
10562:                 }
10563:             }
10564:             $resulttext = &mt('Changes made:').'<ul>';
10565:             foreach my $item (sort(keys(%changes))) {
10566:                 if ($item eq 'uselcmath') {
10567:                     if ($env{'form.'.$item} eq '1') {
10568:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
10569:                     } else {
10570:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
10571:                     }
10572:                 } elsif ($item eq 'usejsme') {
10573:                     if ($env{'form.'.$item} eq '1') {
10574:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
10575:                     } else {
10576:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
10577:                     }
10578:                 } elsif ($item eq 'anonsurvey_threshold') {
10579:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
10580:                 } elsif ($item eq 'uploadquota') {
10581:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
10582:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
10583:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
10584:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
10585:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
10586: 
10587:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
10588:                                        '</ul>'.
10589:                                        '</li>';
10590:                     } else {
10591:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
10592:                     }
10593:                 } elsif ($item eq 'postsubmit') {
10594:                     if ($domdefaults{'postsubmit'} eq 'off') {
10595:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
10596:                     } else {
10597:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
10598:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
10599:                             $resulttext .= &mt('durations:').'<ul>';
10600:                             foreach my $type (@types) {
10601:                                 $resulttext .= '<li>';
10602:                                 my $timeout;
10603:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
10604:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
10605:                                 }
10606:                                 my $display;
10607:                                 if ($timeout eq '0') {
10608:                                     $display = &mt('unlimited');
10609:                                 } elsif ($timeout eq '') {
10610:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
10611:                                 } else {
10612:                                     $display = &mt('[quant,_1,second]',$timeout);
10613:                                 }
10614:                                 if ($type eq 'community') {
10615:                                     $resulttext .= &mt('Communities');
10616:                                 } elsif ($type eq 'official') {
10617:                                     $resulttext .= &mt('Official courses');
10618:                                 } elsif ($type eq 'unofficial') {
10619:                                     $resulttext .= &mt('Unofficial courses');
10620:                                 } elsif ($type eq 'textbook') {
10621:                                     $resulttext .= &mt('Textbook courses');
10622:                                 }
10623:                                 $resulttext .= ' -- '.$display.'</li>';
10624:                             }
10625:                             $resulttext .= '</ul>';
10626:                         }
10627:                         $resulttext .= '</li>';
10628:                     }
10629:                 } elsif ($item eq 'coursecredits') {
10630:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
10631:                         if (($domdefaults{'officialcredits'} eq '') &&
10632:                             ($domdefaults{'unofficialcredits'} eq '') &&
10633:                             ($domdefaults{'textbookcredits'} eq '')) {
10634:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10635:                         } else {
10636:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
10637:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
10638:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
10639:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
10640:                                            '</ul>'.
10641:                                            '</li>';
10642:                         }
10643:                     } else {
10644:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
10645:                     }
10646:                 } elsif ($item eq 'canclone') {
10647:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
10648:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
10649:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
10650:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
10651:                         }
10652:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
10653:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
10654:                     } else {
10655:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
10656:                     }
10657:                 }
10658:             }
10659:             $resulttext .= '</ul>';
10660:         } else {
10661:             $resulttext = &mt('No changes made to course defaults');
10662:         }
10663:     } else {
10664:         $resulttext = '<span class="LC_error">'.
10665:             &mt('An error occurred: [_1]',$putresult).'</span>';
10666:     }
10667:     return $resulttext;
10668: }
10669: 
10670: sub modify_selfenrollment {
10671:     my ($dom,$lastactref,%domconfig) = @_;
10672:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
10673:     my @types = ('official','unofficial','community','textbook');
10674:     my %titles = &tool_titles();
10675:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
10676:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
10677:     $ordered{'default'} = ['types','registered','approval','limit'];
10678: 
10679:     my (%roles,%shown,%toplevel);
10680:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
10681: 
10682:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
10683:         if ($domconfig{'selfenrollment'} eq '') {
10684:             $domconfig{'selfenrollment'} = {};
10685:         }
10686:     }
10687:     %toplevel = (
10688:                   admin      => 'Configuration Rights',
10689:                   default    => 'Default settings',
10690:                   validation => 'Validation of self-enrollment requests',
10691:                 );
10692:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
10693: 
10694:     if (ref($ordered{'admin'}) eq 'ARRAY') {
10695:         foreach my $item (@{$ordered{'admin'}}) {
10696:             foreach my $type (@types) {
10697:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
10698:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
10699:                 } else {
10700:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
10701:                 }
10702:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
10703:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
10704:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
10705:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
10706:                             push(@{$changes{'admin'}{$type}},$item);
10707:                         }
10708:                     } else {
10709:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
10710:                             push(@{$changes{'admin'}{$type}},$item);
10711:                         }
10712:                     }
10713:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
10714:                     push(@{$changes{'admin'}{$type}},$item);
10715:                 }
10716:             }
10717:         }
10718:     }
10719: 
10720:     foreach my $item (@{$ordered{'default'}}) {
10721:         foreach my $type (@types) {
10722:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
10723:             if ($item eq 'types') {
10724:                 unless (($value eq 'all') || ($value eq 'dom')) {
10725:                     $value = '';
10726:                 }
10727:             } elsif ($item eq 'registered') {
10728:                 unless ($value eq '1') {
10729:                     $value = 0;
10730:                 }
10731:             } elsif ($item eq 'approval') {
10732:                 unless ($value =~ /^[012]$/) {
10733:                     $value = 0;
10734:                 }
10735:             } else {
10736:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10737:                     $value = 'none';
10738:                 }
10739:             }
10740:             $selfenrollhash{'default'}{$type}{$item} = $value;
10741:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
10742:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10743:                     if ($selfenrollhash{'default'}{$type}{$item} ne
10744:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
10745:                          push(@{$changes{'default'}{$type}},$item);
10746:                     }
10747:                 } else {
10748:                     push(@{$changes{'default'}{$type}},$item);
10749:                 }
10750:             } else {
10751:                 push(@{$changes{'default'}{$type}},$item);
10752:             }
10753:             if ($item eq 'limit') {
10754:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
10755:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
10756:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
10757:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
10758:                     }
10759:                 } else {
10760:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
10761:                 }
10762:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
10763:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
10764:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
10765:                          push(@{$changes{'default'}{$type}},'cap');
10766:                     }
10767:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
10768:                     push(@{$changes{'default'}{$type}},'cap');
10769:                 }
10770:             }
10771:         }
10772:     }
10773: 
10774:     foreach my $item (@{$itemsref}) {
10775:         if ($item eq 'fields') {
10776:             my @changed;
10777:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
10778:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
10779:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
10780:             }
10781:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10782:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
10783:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
10784:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
10785:                 } else {
10786:                     @changed = @{$selfenrollhash{'validation'}{$item}};
10787:                 }
10788:             } else {
10789:                 @changed = @{$selfenrollhash{'validation'}{$item}};
10790:             }
10791:             if (@changed) {
10792:                 if ($selfenrollhash{'validation'}{$item}) { 
10793:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
10794:                 } else {
10795:                     $changes{'validation'}{$item} = &mt('None');
10796:                 }
10797:             }
10798:         } else {
10799:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
10800:             if ($item eq 'markup') {
10801:                if ($env{'form.selfenroll_validation_'.$item}) {
10802:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
10803:                }
10804:             }
10805:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
10806:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
10807:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
10808:                 }
10809:             }
10810:         }
10811:     }
10812: 
10813:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
10814:                                              $dom);
10815:     if ($putresult eq 'ok') {
10816:         if (keys(%changes) > 0) {
10817:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10818:             $resulttext = &mt('Changes made:').'<ul>';
10819:             foreach my $key ('admin','default','validation') {
10820:                 if (ref($changes{$key}) eq 'HASH') {
10821:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
10822:                     if ($key eq 'validation') {
10823:                         foreach my $item (@{$itemsref}) {
10824:                             if (exists($changes{$key}{$item})) {
10825:                                 if ($item eq 'markup') {
10826:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10827:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
10828:                                 } else {  
10829:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
10830:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
10831:                                 }
10832:                             }
10833:                         }
10834:                     } else {
10835:                         foreach my $type (@types) {
10836:                             if ($type eq 'community') {
10837:                                 $roles{'1'} = &mt('Community personnel');
10838:                             } else {
10839:                                 $roles{'1'} = &mt('Course personnel');
10840:                             }
10841:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
10842:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
10843:                                     if ($key eq 'admin') {
10844:                                         my @mgrdc = ();
10845:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10846:                                             foreach my $item (@{$ordered{'admin'}}) {
10847:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
10848:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
10849:                                                         push(@mgrdc,$item);
10850:                                                     }
10851:                                                 }
10852:                                             }
10853:                                             if (@mgrdc) {
10854:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
10855:                                             } else {
10856:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
10857:                                             }
10858:                                         }
10859:                                     } else {
10860:                                         if (ref($ordered{$key}) eq 'ARRAY') {
10861:                                             foreach my $item (@{$ordered{$key}}) {
10862:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10863:                                                     $domdefaults{$type.'selfenroll'.$item} =
10864:                                                         $selfenrollhash{$key}{$type}{$item};
10865:                                                 }
10866:                                             }
10867:                                         }
10868:                                     }
10869:                                 }
10870:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
10871:                                 foreach my $item (@{$ordered{$key}}) {
10872:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
10873:                                         $resulttext .= '<li>';
10874:                                         if ($key eq 'admin') {
10875:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
10876:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
10877:                                         } else {
10878:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
10879:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
10880:                                         }
10881:                                         $resulttext .= '</li>';
10882:                                     }
10883:                                 }
10884:                                 $resulttext .= '</ul></li>';
10885:                             }
10886:                         }
10887:                         $resulttext .= '</ul></li>'; 
10888:                     }
10889:                 }
10890:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
10891:                     my $cachetime = 24*60*60;
10892:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
10893:                     if (ref($lastactref) eq 'HASH') {
10894:                         $lastactref->{'domdefaults'} = 1;
10895:                     }
10896:                 }
10897:             }
10898:             $resulttext .= '</ul>';
10899:         } else {
10900:             $resulttext = &mt('No changes made to self-enrollment settings');
10901:         }
10902:     } else {
10903:         $resulttext = '<span class="LC_error">'.
10904:             &mt('An error occurred: [_1]',$putresult).'</span>';
10905:     }
10906:     return $resulttext;
10907: }
10908: 
10909: sub modify_usersessions {
10910:     my ($dom,$lastactref,%domconfig) = @_;
10911:     my @hostingtypes = ('version','excludedomain','includedomain');
10912:     my @offloadtypes = ('primary','default');
10913:     my %types = (
10914:                   remote => \@hostingtypes,
10915:                   hosted => \@hostingtypes,
10916:                   spares => \@offloadtypes,
10917:                 );
10918:     my @prefixes = ('remote','hosted','spares');
10919:     my @lcversions = &Apache::lonnet::all_loncaparevs();
10920:     my (%by_ip,%by_location,@intdoms);
10921:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
10922:     my @locations = sort(keys(%by_location));
10923:     my (%defaultshash,%changes);
10924:     foreach my $prefix (@prefixes) {
10925:         $defaultshash{'usersessions'}{$prefix} = {};
10926:     }
10927:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10928:     my $resulttext;
10929:     my %iphost = &Apache::lonnet::get_iphost();
10930:     foreach my $prefix (@prefixes) {
10931:         next if ($prefix eq 'spares');
10932:         foreach my $type (@{$types{$prefix}}) {
10933:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
10934:             if ($type eq 'version') {
10935:                 my $value = $env{'form.'.$prefix.'_'.$type};
10936:                 my $okvalue;
10937:                 if ($value ne '') {
10938:                     if (grep(/^\Q$value\E$/,@lcversions)) {
10939:                         $okvalue = $value;
10940:                     }
10941:                 }
10942:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
10943:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
10944:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
10945:                             if ($inuse == 0) {
10946:                                 $changes{$prefix}{$type} = 1;
10947:                             } else {
10948:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
10949:                                     $changes{$prefix}{$type} = 1;
10950:                                 }
10951:                                 if ($okvalue ne '') {
10952:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10953:                                 } 
10954:                             }
10955:                         } else {
10956:                             if (($inuse == 1) && ($okvalue ne '')) {
10957:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10958:                                 $changes{$prefix}{$type} = 1;
10959:                             }
10960:                         }
10961:                     } else {
10962:                         if (($inuse == 1) && ($okvalue ne '')) {
10963:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10964:                             $changes{$prefix}{$type} = 1;
10965:                         }
10966:                     }
10967:                 } else {
10968:                     if (($inuse == 1) && ($okvalue ne '')) {
10969:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
10970:                         $changes{$prefix}{$type} = 1;
10971:                     }
10972:                 }
10973:             } else {
10974:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
10975:                 my @okvals;
10976:                 foreach my $val (@vals) {
10977:                     if ($val =~ /:/) {
10978:                         my @items = split(/:/,$val);
10979:                         foreach my $item (@items) {
10980:                             if (ref($by_location{$item}) eq 'ARRAY') {
10981:                                 push(@okvals,$item);
10982:                             }
10983:                         }
10984:                     } else {
10985:                         if (ref($by_location{$val}) eq 'ARRAY') {
10986:                             push(@okvals,$val);
10987:                         }
10988:                     }
10989:                 }
10990:                 @okvals = sort(@okvals);
10991:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
10992:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
10993:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
10994:                             if ($inuse == 0) {
10995:                                 $changes{$prefix}{$type} = 1; 
10996:                             } else {
10997:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
10998:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
10999:                                 if (@changed > 0) {
11000:                                     $changes{$prefix}{$type} = 1;
11001:                                 }
11002:                             }
11003:                         } else {
11004:                             if ($inuse == 1) {
11005:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
11006:                                 $changes{$prefix}{$type} = 1;
11007:                             }
11008:                         } 
11009:                     } else {
11010:                         if ($inuse == 1) {
11011:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
11012:                             $changes{$prefix}{$type} = 1;
11013:                         }
11014:                     }
11015:                 } else {
11016:                     if ($inuse == 1) {
11017:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
11018:                         $changes{$prefix}{$type} = 1;
11019:                     }
11020:                 }
11021:             }
11022:         }
11023:     }
11024: 
11025:     my @alldoms = &Apache::lonnet::all_domains();
11026:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
11027:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
11028:     my $savespares;
11029: 
11030:     foreach my $lonhost (sort(keys(%servers))) {
11031:         my $serverhomeID =
11032:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
11033:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
11034:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
11035:         my %spareschg;
11036:         foreach my $type (@{$types{'spares'}}) {
11037:             my @okspares;
11038:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
11039:             foreach my $server (@checked) {
11040:                 if (&Apache::lonnet::hostname($server) ne '') {
11041:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
11042:                         unless (grep(/^\Q$server\E$/,@okspares)) {
11043:                             push(@okspares,$server);
11044:                         }
11045:                     }
11046:                 }
11047:             }
11048:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
11049:             my $newspare;
11050:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
11051:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
11052:                     $newspare = $new;
11053:                 }
11054:             }
11055:             my @spares;
11056:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
11057:                 @spares = sort(@okspares,$newspare);
11058:             } else {
11059:                 @spares = sort(@okspares);
11060:             }
11061:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
11062:             if (ref($spareid{$lonhost}) eq 'HASH') {
11063:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
11064:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
11065:                     if (@diffs > 0) {
11066:                         $spareschg{$type} = 1;
11067:                     }
11068:                 }
11069:             }
11070:         }
11071:         if (keys(%spareschg) > 0) {
11072:             $changes{'spares'}{$lonhost} = \%spareschg;
11073:         }
11074:     }
11075:     $defaultshash{'usersessions'}{'offloadnow'} = {};
11076:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
11077:     my @okoffload;
11078:     if (@offloadnow) {
11079:         foreach my $server (@offloadnow) {
11080:             if (&Apache::lonnet::hostname($server) ne '') {
11081:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
11082:                     push(@okoffload,$server);
11083:                 }
11084:             }
11085:         }
11086:         if (@okoffload) {
11087:             foreach my $lonhost (@okoffload) {
11088:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
11089:             }
11090:         }
11091:     }
11092:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
11093:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
11094:             if (ref($changes{'spares'}) eq 'HASH') {
11095:                 if (keys(%{$changes{'spares'}}) > 0) {
11096:                     $savespares = 1;
11097:                 }
11098:             }
11099:         } else {
11100:             $savespares = 1;
11101:         }
11102:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
11103:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
11104:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
11105:                     $changes{'offloadnow'} = 1;
11106:                     last;
11107:                 }
11108:             }
11109:             unless ($changes{'offloadnow'}) {
11110:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
11111:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
11112:                         $changes{'offloadnow'} = 1;
11113:                         last;
11114:                     }
11115:                 }
11116:             }
11117:         } elsif (@okoffload) {
11118:             $changes{'offloadnow'} = 1;
11119:         }
11120:     } elsif (@okoffload) {
11121:         $changes{'offloadnow'} = 1;
11122:     }
11123:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
11124:     if ((keys(%changes) > 0) || ($savespares)) {
11125:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
11126:                                                  $dom);
11127:         if ($putresult eq 'ok') {
11128:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
11129:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
11130:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
11131:                 }
11132:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
11133:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
11134:                 }
11135:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
11136:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
11137:                 }
11138:             }
11139:             my $cachetime = 24*60*60;
11140:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
11141:             if (ref($lastactref) eq 'HASH') {
11142:                 $lastactref->{'domdefaults'} = 1;
11143:             }
11144:             if (keys(%changes) > 0) {
11145:                 my %lt = &usersession_titles();
11146:                 $resulttext = &mt('Changes made:').'<ul>';
11147:                 foreach my $prefix (@prefixes) {
11148:                     if (ref($changes{$prefix}) eq 'HASH') {
11149:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
11150:                         if ($prefix eq 'spares') {
11151:                             if (ref($changes{$prefix}) eq 'HASH') {
11152:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
11153:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
11154:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
11155:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
11156:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
11157:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
11158:                                         foreach my $type (@{$types{$prefix}}) {
11159:                                             if ($changes{$prefix}{$lonhost}{$type}) {
11160:                                                 my $offloadto = &mt('None');
11161:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
11162:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
11163:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
11164:                                                     }
11165:                                                 }
11166:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
11167:                                             }
11168:                                         }
11169:                                     }
11170:                                     $resulttext .= '</li>';
11171:                                 }
11172:                             }
11173:                         } else {
11174:                             foreach my $type (@{$types{$prefix}}) {
11175:                                 if (defined($changes{$prefix}{$type})) {
11176:                                     my $newvalue;
11177:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
11178:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
11179:                                             if ($type eq 'version') {
11180:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
11181:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
11182:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
11183:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
11184:                                                 }
11185:                                             }
11186:                                         }
11187:                                     }
11188:                                     if ($newvalue eq '') {
11189:                                         if ($type eq 'version') {
11190:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
11191:                                         } else {
11192:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
11193:                                         }
11194:                                     } else {
11195:                                         if ($type eq 'version') {
11196:                                             $newvalue .= ' '.&mt('(or later)'); 
11197:                                         }
11198:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
11199:                                     }
11200:                                 }
11201:                             }
11202:                         }
11203:                         $resulttext .= '</ul>';
11204:                     }
11205:                 }
11206:                 if ($changes{'offloadnow'}) {
11207:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
11208:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
11209:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
11210:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
11211:                                 $resulttext .= '<li>'.$lonhost.'</li>';
11212:                             }
11213:                             $resulttext .= '</ul>';
11214:                         } else {
11215:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
11216:                         }
11217:                     } else {
11218:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
11219:                     }
11220:                 }
11221:                 $resulttext .= '</ul>';
11222:             } else {
11223:                 $resulttext = $nochgmsg;
11224:             }
11225:         } else {
11226:             $resulttext = '<span class="LC_error">'.
11227:                           &mt('An error occurred: [_1]',$putresult).'</span>';
11228:         }
11229:     } else {
11230:         $resulttext = $nochgmsg;
11231:     }
11232:     return $resulttext;
11233: }
11234: 
11235: sub modify_loadbalancing {
11236:     my ($dom,%domconfig) = @_;
11237:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
11238:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
11239:     my ($othertitle,$usertypes,$types) =
11240:         &Apache::loncommon::sorted_inst_types($dom);
11241:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
11242:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
11243:     my @sparestypes = ('primary','default');
11244:     my %typetitles = &sparestype_titles();
11245:     my $resulttext;
11246:     my (%currbalancer,%currtargets,%currrules,%existing);
11247:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
11248:         %existing = %{$domconfig{'loadbalancing'}};
11249:     }
11250:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
11251:                               \%currtargets,\%currrules);
11252:     my ($saveloadbalancing,%defaultshash,%changes);
11253:     my ($alltypes,$othertypes,$titles) =
11254:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
11255:     my %ruletitles = &offloadtype_text();
11256:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
11257:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
11258:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
11259:         if ($balancer eq '') {
11260:             next;
11261:         }
11262:         if (!exists($servers{$balancer})) {
11263:             if (exists($currbalancer{$balancer})) {
11264:                 push(@{$changes{'delete'}},$balancer);
11265:             }
11266:             next;
11267:         }
11268:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
11269:             push(@{$changes{'delete'}},$balancer);
11270:             next;
11271:         }
11272:         if (!exists($currbalancer{$balancer})) {
11273:             push(@{$changes{'add'}},$balancer);
11274:         }
11275:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
11276:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
11277:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
11278:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
11279:             $saveloadbalancing = 1;
11280:         }
11281:         foreach my $sparetype (@sparestypes) {
11282:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
11283:             my @offloadto;
11284:             foreach my $target (@targets) {
11285:                 if (($servers{$target}) && ($target ne $balancer)) {
11286:                     if ($sparetype eq 'default') {
11287:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
11288:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
11289:                         }
11290:                     }
11291:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
11292:                         push(@offloadto,$target);
11293:                     }
11294:                 }
11295:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
11296:             }
11297:         }
11298:         if (ref($currtargets{$balancer}) eq 'HASH') {
11299:             foreach my $sparetype (@sparestypes) {
11300:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
11301:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
11302:                     if (@targetdiffs > 0) {
11303:                         $changes{'curr'}{$balancer}{'targets'} = 1;
11304:                     }
11305:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
11306:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
11307:                         $changes{'curr'}{$balancer}{'targets'} = 1;
11308:                     }
11309:                 }
11310:             }
11311:         } else {
11312:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
11313:                 foreach my $sparetype (@sparestypes) {
11314:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
11315:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
11316:                             $changes{'curr'}{$balancer}{'targets'} = 1;
11317:                         }
11318:                     }
11319:                 }
11320:             }
11321:         }
11322:         my $ishomedom;
11323:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
11324:             $ishomedom = 1;
11325:         }
11326:         if (ref($alltypes) eq 'ARRAY') {
11327:             foreach my $type (@{$alltypes}) {
11328:                 my $rule;
11329:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
11330:                          (!$ishomedom)) {
11331:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
11332:                 }
11333:                 if ($rule eq 'specific') {
11334:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
11335:                     if (exists($servers{$specifiedhost})) {
11336:                         $rule = $specifiedhost;
11337:                     }
11338:                 }
11339:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
11340:                 if (ref($currrules{$balancer}) eq 'HASH') {
11341:                     if ($rule ne $currrules{$balancer}{$type}) {
11342:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
11343:                     }
11344:                 } elsif ($rule ne '') {
11345:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
11346:                 }
11347:             }
11348:         }
11349:     }
11350:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
11351:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
11352:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
11353:             $defaultshash{'loadbalancing'} = {};
11354:         }
11355:         my $putresult = &Apache::lonnet::put_dom('configuration',
11356:                                                  \%defaultshash,$dom);
11357:         if ($putresult eq 'ok') {
11358:             if (keys(%changes) > 0) {
11359:                 my %toupdate;
11360:                 if (ref($changes{'delete'}) eq 'ARRAY') {
11361:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
11362:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
11363:                         $toupdate{$balancer} = 1;
11364:                     }
11365:                 }
11366:                 if (ref($changes{'add'}) eq 'ARRAY') {
11367:                     foreach my $balancer (sort(@{$changes{'add'}})) {
11368:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
11369:                         $toupdate{$balancer} = 1;
11370:                     }
11371:                 }
11372:                 if (ref($changes{'curr'}) eq 'HASH') {
11373:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
11374:                         $toupdate{$balancer} = 1;
11375:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
11376:                             if ($changes{'curr'}{$balancer}{'targets'}) {
11377:                                 my %offloadstr;
11378:                                 foreach my $sparetype (@sparestypes) {
11379:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
11380:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
11381:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
11382:                                         }
11383:                                     }
11384:                                 }
11385:                                 if (keys(%offloadstr) == 0) {
11386:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
11387:                                 } else {
11388:                                     my $showoffload;
11389:                                     foreach my $sparetype (@sparestypes) {
11390:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
11391:                                         if (defined($offloadstr{$sparetype})) {
11392:                                             $showoffload .= $offloadstr{$sparetype};
11393:                                         } else {
11394:                                             $showoffload .= &mt('None');
11395:                                         }
11396:                                         $showoffload .= ('&nbsp;'x3);
11397:                                     }
11398:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
11399:                                 }
11400:                             }
11401:                         }
11402:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
11403:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
11404:                                 foreach my $type (@{$alltypes}) {
11405:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
11406:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
11407:                                         my $balancetext;
11408:                                         if ($rule eq '') {
11409:                                             $balancetext =  $ruletitles{'default'};
11410:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
11411:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
11412:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
11413:                                                 foreach my $sparetype (@sparestypes) {
11414:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
11415:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
11416:                                                     }
11417:                                                 }
11418:                                                 foreach my $item (@{$alltypes}) {
11419:                                                     next if ($item =~  /^_LC_ipchange/);
11420:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
11421:                                                     if ($hasrule eq 'homeserver') {
11422:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
11423:                                                     } else {
11424:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
11425:                                                             if ($servers{$hasrule}) {
11426:                                                                 $toupdate{$hasrule} = 1;
11427:                                                             }
11428:                                                         }
11429:                                                     }
11430:                                                 }
11431:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
11432:                                                     $balancetext =  $ruletitles{$rule};
11433:                                                 } else {
11434:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
11435:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
11436:                                                     if ($receiver) {
11437:                                                         $toupdate{$receiver};
11438:                                                     }
11439:                                                 }
11440:                                             } else {
11441:                                                 $balancetext =  $ruletitles{$rule};
11442:                                             }
11443:                                         } else {
11444:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
11445:                                         }
11446:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
11447:                                     }
11448:                                 }
11449:                             }
11450:                         }
11451:                         if (keys(%toupdate)) {
11452:                             my %thismachine;
11453:                             my $updatedhere;
11454:                             my $cachetime = 60*60*24;
11455:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
11456:                             foreach my $lonhost (keys(%toupdate)) {
11457:                                 if ($thismachine{$lonhost}) {
11458:                                     unless ($updatedhere) {
11459:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
11460:                                                                       $defaultshash{'loadbalancing'},
11461:                                                                       $cachetime);
11462:                                         $updatedhere = 1;
11463:                                     }
11464:                                 } else {
11465:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
11466:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
11467:                                 }
11468:                             }
11469:                         }
11470:                     }
11471:                 }
11472:                 if ($resulttext ne '') {
11473:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
11474:                 } else {
11475:                     $resulttext = $nochgmsg;
11476:                 }
11477:             } else {
11478:                 $resulttext = $nochgmsg;
11479:             }
11480:         } else {
11481:             $resulttext = '<span class="LC_error">'.
11482:                           &mt('An error occurred: [_1]',$putresult).'</span>';
11483:         }
11484:     } else {
11485:         $resulttext = $nochgmsg;
11486:     }
11487:     return $resulttext;
11488: }
11489: 
11490: sub recurse_check {
11491:     my ($chkcats,$categories,$depth,$name) = @_;
11492:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
11493:         my $chg = 0;
11494:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
11495:             my $category = $chkcats->[$depth]{$name}[$j];
11496:             my $item;
11497:             if ($category eq '') {
11498:                 $chg ++;
11499:             } else {
11500:                 my $deeper = $depth + 1;
11501:                 $item = &escape($category).':'.&escape($name).':'.$depth;
11502:                 if ($chg) {
11503:                     $categories->{$item} -= $chg;
11504:                 }
11505:                 &recurse_check($chkcats,$categories,$deeper,$category);
11506:                 $deeper --;
11507:             }
11508:         }
11509:     }
11510:     return;
11511: }
11512: 
11513: sub recurse_cat_deletes {
11514:     my ($item,$coursecategories,$deletions) = @_;
11515:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
11516:     my $subdepth = $depth + 1;
11517:     if (ref($coursecategories) eq 'HASH') {
11518:         foreach my $subitem (keys(%{$coursecategories})) {
11519:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
11520:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
11521:                 delete($coursecategories->{$subitem});
11522:                 $deletions->{$subitem} = 1;
11523:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
11524:             }
11525:         }
11526:     }
11527:     return;
11528: }
11529: 
11530: sub get_active_dcs {
11531:     my ($dom) = @_;
11532:     my $now = time;
11533:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
11534:     my %domcoords;
11535:     my $numdcs = 0;
11536:     foreach my $server (keys(%dompersonnel)) {
11537:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
11538:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
11539:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
11540:         }
11541:     }
11542:     return %domcoords;
11543: }
11544: 
11545: sub active_dc_picker {
11546:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
11547:     my %domcoords = &get_active_dcs($dom);
11548:     my @domcoord = keys(%domcoords);
11549:     if (keys(%currhash)) {
11550:         foreach my $dc (keys(%currhash)) {
11551:             unless (exists($domcoords{$dc})) {
11552:                 push(@domcoord,$dc);
11553:             }
11554:         }
11555:     }
11556:     @domcoord = sort(@domcoord);
11557:     my $numdcs = scalar(@domcoord);
11558:     my $rows = 0;
11559:     my $table;
11560:     if ($numdcs > 1) {
11561:         $table = '<table>';
11562:         for (my $i=0; $i<@domcoord; $i++) {
11563:             my $rem = $i%($numinrow);
11564:             if ($rem == 0) {
11565:                 if ($i > 0) {
11566:                     $table .= '</tr>';
11567:                 }
11568:                 $table .= '<tr>';
11569:                 $rows ++;
11570:             }
11571:             my $check = '';
11572:             if ($inputtype eq 'radio') {
11573:                 if (keys(%currhash) == 0) {
11574:                     if (!$i) {
11575:                         $check = ' checked="checked"';
11576:                     }
11577:                 } elsif (exists($currhash{$domcoord[$i]})) {
11578:                     $check = ' checked="checked"';
11579:                 }
11580:             } else {
11581:                 if (exists($currhash{$domcoord[$i]})) {
11582:                     $check = ' checked="checked"';
11583:                 }
11584:             }
11585:             if ($i == @domcoord - 1) {
11586:                 my $colsleft = $numinrow - $rem;
11587:                 if ($colsleft > 1) {
11588:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
11589:                 } else {
11590:                     $table .= '<td class="LC_left_item">';
11591:                 }
11592:             } else {
11593:                 $table .= '<td class="LC_left_item">';
11594:             }
11595:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
11596:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
11597:             $table .= '<span class="LC_nobreak"><label>'.
11598:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
11599:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
11600:             if ($user ne $dcname.':'.$dcdom) {
11601:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
11602:             }
11603:             $table .= '</label></span></td>';
11604:         }
11605:         $table .= '</tr></table>';
11606:     } elsif ($numdcs == 1) {
11607:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
11608:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
11609:         if ($inputtype eq 'radio') {
11610:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
11611:             if ($user ne $dcname.':'.$dcdom) {
11612:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
11613:             }
11614:         } else {
11615:             my $check;
11616:             if (exists($currhash{$domcoord[0]})) {
11617:                 $check = ' checked="checked"';
11618:             }
11619:             $table = '<span class="LC_nobreak"><label>'.
11620:                      '<input type="checkbox" name="'.$name.'" '.
11621:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
11622:             if ($user ne $dcname.':'.$dcdom) {
11623:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
11624:             }
11625:             $table .= '</label></span>';
11626:             $rows ++;
11627:         }
11628:     }
11629:     return ($numdcs,$table,$rows);
11630: }
11631: 
11632: sub usersession_titles {
11633:     return &Apache::lonlocal::texthash(
11634:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
11635:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
11636:                spares => 'Servers offloaded to, when busy',
11637:                version => 'LON-CAPA version requirement',
11638:                excludedomain => 'Allow all, but exclude specific domains',
11639:                includedomain => 'Deny all, but include specific domains',
11640:                primary => 'Primary (checked first)',
11641:                default => 'Default',
11642:            );
11643: }
11644: 
11645: sub id_for_thisdom {
11646:     my (%servers) = @_;
11647:     my %altids;
11648:     foreach my $server (keys(%servers)) {
11649:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
11650:         if ($serverhome ne $server) {
11651:             $altids{$serverhome} = $server;
11652:         }
11653:     }
11654:     return %altids;
11655: }
11656: 
11657: sub count_servers {
11658:     my ($currbalancer,%servers) = @_;
11659:     my (@spares,$numspares);
11660:     foreach my $lonhost (sort(keys(%servers))) {
11661:         next if ($currbalancer eq $lonhost);
11662:         push(@spares,$lonhost);
11663:     }
11664:     if ($currbalancer) {
11665:         $numspares = scalar(@spares);
11666:     } else {
11667:         $numspares = scalar(@spares) - 1;
11668:     }
11669:     return ($numspares,@spares);
11670: }
11671: 
11672: sub lonbalance_targets_js {
11673:     my ($dom,$types,$servers,$settings) = @_;
11674:     my $select = &mt('Select');
11675:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
11676:     if (ref($servers) eq 'HASH') {
11677:         $alltargets = join("','",sort(keys(%{$servers})));
11678:         my @homedoms;
11679:         foreach my $server (sort(keys(%{$servers}))) {
11680:             if (&Apache::lonnet::host_domain($server) eq $dom) {
11681:                 push(@homedoms,'1');
11682:             } else {
11683:                 push(@homedoms,'0');
11684:             }
11685:         }
11686:         $allishome = join("','",@homedoms);
11687:     }
11688:     if (ref($types) eq 'ARRAY') {
11689:         if (@{$types} > 0) {
11690:             @alltypes = @{$types};
11691:         }
11692:     }
11693:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
11694:     $allinsttypes = join("','",@alltypes);
11695:     my (%currbalancer,%currtargets,%currrules,%existing);
11696:     if (ref($settings) eq 'HASH') {
11697:         %existing = %{$settings};
11698:     }
11699:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
11700:                               \%currtargets,\%currrules);
11701:     my $balancers = join("','",sort(keys(%currbalancer)));
11702:     return <<"END";
11703: 
11704: <script type="text/javascript">
11705: // <![CDATA[
11706: 
11707: currBalancers = new Array('$balancers');
11708: 
11709: function toggleTargets(balnum) {
11710:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
11711:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
11712:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
11713:     var prevbalancer = prevhostitem.value;
11714:     var baltotal = document.getElementById('loadbalancing_total').value;
11715:     prevhostitem.value = balancer;
11716:     if (prevbalancer != '') {
11717:         var prevIdx = currBalancers.indexOf(prevbalancer);
11718:         if (prevIdx != -1) {
11719:             currBalancers.splice(prevIdx,1);
11720:         }
11721:     }
11722:     if (balancer == '') {
11723:         hideSpares(balnum);
11724:     } else {
11725:         var currIdx = currBalancers.indexOf(balancer);
11726:         if (currIdx == -1) {
11727:             currBalancers.push(balancer);
11728:         }
11729:         var homedoms = new Array('$allishome');
11730:         var ishomedom = homedoms[lonhostitem.selectedIndex];
11731:         showSpares(balancer,ishomedom,balnum);
11732:     }
11733:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
11734:     return;
11735: }
11736: 
11737: function showSpares(balancer,ishomedom,balnum) {
11738:     var alltargets = new Array('$alltargets');
11739:     var insttypes = new Array('$allinsttypes');
11740:     var offloadtypes = new Array('primary','default');
11741: 
11742:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
11743:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
11744:  
11745:     for (var i=0; i<offloadtypes.length; i++) {
11746:         var count = 0;
11747:         for (var j=0; j<alltargets.length; j++) {
11748:             if (alltargets[j] != balancer) {
11749:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
11750:                 item.value = alltargets[j];
11751:                 item.style.textAlign='left';
11752:                 item.style.textFace='normal';
11753:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
11754:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
11755:                     item.disabled = '';
11756:                 } else {
11757:                     item.disabled = 'disabled';
11758:                     item.checked = false;
11759:                 }
11760:                 count ++;
11761:             }
11762:         }
11763:     }
11764:     for (var k=0; k<insttypes.length; k++) {
11765:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
11766:             if (ishomedom == 1) {
11767:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11768:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11769:             } else {
11770:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11771:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11772:             }
11773:         } else {
11774:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
11775:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
11776:         }
11777:         if ((insttypes[k] != '_LC_external') && 
11778:             ((insttypes[k] != '_LC_internetdom') ||
11779:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
11780:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
11781:             item.options.length = 0;
11782:             item.options[0] = new Option("","",true,true);
11783:             var idx = 0;
11784:             for (var m=0; m<alltargets.length; m++) {
11785:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
11786:                     idx ++;
11787:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
11788:                 }
11789:             }
11790:         }
11791:     }
11792:     return;
11793: }
11794: 
11795: function hideSpares(balnum) {
11796:     var alltargets = new Array('$alltargets');
11797:     var insttypes = new Array('$allinsttypes');
11798:     var offloadtypes = new Array('primary','default');
11799: 
11800:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
11801:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
11802: 
11803:     var total = alltargets.length - 1;
11804:     for (var i=0; i<offloadtypes; i++) {
11805:         for (var j=0; j<total; j++) {
11806:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
11807:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
11808:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
11809:         }
11810:     }
11811:     for (var k=0; k<insttypes.length; k++) {
11812:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
11813:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
11814:         if (insttypes[k] != '_LC_external') {
11815:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
11816:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
11817:         }
11818:     }
11819:     return;
11820: }
11821: 
11822: function checkOffloads(item,balnum,type) {
11823:     var alltargets = new Array('$alltargets');
11824:     var offloadtypes = new Array('primary','default');
11825:     if (item.checked) {
11826:         var total = alltargets.length - 1;
11827:         var other;
11828:         if (type == offloadtypes[0]) {
11829:             other = offloadtypes[1];
11830:         } else {
11831:             other = offloadtypes[0];
11832:         }
11833:         for (var i=0; i<total; i++) {
11834:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
11835:             if (server == item.value) {
11836:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
11837:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
11838:                 }
11839:             }
11840:         }
11841:     }
11842:     return;
11843: }
11844: 
11845: function singleServerToggle(balnum,type) {
11846:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
11847:     if (offloadtoSelIdx == 0) {
11848:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
11849:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11850: 
11851:     } else {
11852:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
11853:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11854:     }
11855:     return;
11856: }
11857: 
11858: function balanceruleChange(formname,balnum,type) {
11859:     if (type == '_LC_external') {
11860:         return;
11861:     }
11862:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
11863:     for (var i=0; i<typesRules.length; i++) {
11864:         if (formname.elements[typesRules[i]].checked) {
11865:             if (formname.elements[typesRules[i]].value != 'specific') {
11866:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
11867:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
11868:             } else {
11869:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
11870:             }
11871:         }
11872:     }
11873:     return;
11874: }
11875: 
11876: function balancerDeleteChange(balnum) {
11877:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
11878:     var baltotal = document.getElementById('loadbalancing_total').value;
11879:     var addtarget;
11880:     var removetarget;
11881:     var action = 'delete';
11882:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
11883:         var lonhost = hostitem.value;
11884:         var currIdx = currBalancers.indexOf(lonhost);
11885:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
11886:             if (currIdx != -1) {
11887:                 currBalancers.splice(currIdx,1);
11888:             }
11889:             addtarget = lonhost;
11890:         } else {
11891:             if (currIdx == -1) {
11892:                 currBalancers.push(lonhost);
11893:             }
11894:             removetarget = lonhost;
11895:             action = 'undelete';
11896:         }
11897:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
11898:     }
11899:     return;
11900: }
11901: 
11902: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
11903:     if (baltotal > 1) {
11904:         var offloadtypes = new Array('primary','default');
11905:         var alltargets = new Array('$alltargets');
11906:         var insttypes = new Array('$allinsttypes');
11907:         for (var i=0; i<baltotal; i++) {
11908:             if (i != balnum) {
11909:                 for (var j=0; j<offloadtypes.length; j++) {
11910:                     var total = alltargets.length - 1;
11911:                     for (var k=0; k<total; k++) {
11912:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
11913:                         var server = serveritem.value;
11914:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
11915:                             if (server == addtarget) {
11916:                                 serveritem.disabled = '';
11917:                             }
11918:                         }
11919:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
11920:                             if (server == removetarget) {
11921:                                 serveritem.disabled = 'disabled';
11922:                                 serveritem.checked = false;
11923:                             }
11924:                         }
11925:                     }
11926:                 }
11927:                 for (var j=0; j<insttypes.length; j++) {
11928:                     if (insttypes[j] != '_LC_external') {
11929:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
11930:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
11931:                             var currSel = singleserver.selectedIndex;
11932:                             var currVal = singleserver.options[currSel].value;
11933:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
11934:                                 var numoptions = singleserver.options.length;
11935:                                 var needsnew = 1;
11936:                                 for (var k=0; k<numoptions; k++) {
11937:                                     if (singleserver.options[k] == addtarget) {
11938:                                         needsnew = 0;
11939:                                         break;
11940:                                     }
11941:                                 }
11942:                                 if (needsnew == 1) {
11943:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
11944:                                 }
11945:                             }
11946:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
11947:                                 singleserver.options.length = 0;
11948:                                 if ((currVal) && (currVal != removetarget)) {
11949:                                     singleserver.options[0] = new Option("","",false,false);
11950:                                 } else {
11951:                                     singleserver.options[0] = new Option("","",true,true);
11952:                                 }
11953:                                 var idx = 0;
11954:                                 for (var m=0; m<alltargets.length; m++) {
11955:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
11956:                                         idx ++;
11957:                                         if (currVal == alltargets[m]) {
11958:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
11959:                                         } else {
11960:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
11961:                                         }
11962:                                     }
11963:                                 }
11964:                             }
11965:                         }
11966:                     }
11967:                 }
11968:             }
11969:         }
11970:     }
11971:     return;
11972: }
11973: 
11974: // ]]>
11975: </script>
11976: 
11977: END
11978: }
11979: 
11980: sub new_spares_js {
11981:     my @sparestypes = ('primary','default');
11982:     my $types = join("','",@sparestypes);
11983:     my $select = &mt('Select');
11984:     return <<"END";
11985: 
11986: <script type="text/javascript">
11987: // <![CDATA[
11988: 
11989: function updateNewSpares(formname,lonhost) {
11990:     var types = new Array('$types');
11991:     var include = new Array();
11992:     var exclude = new Array();
11993:     for (var i=0; i<types.length; i++) {
11994:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
11995:         for (var j=0; j<spareboxes.length; j++) {
11996:             if (formname.elements[spareboxes[j]].checked) {
11997:                 exclude.push(formname.elements[spareboxes[j]].value);
11998:             } else {
11999:                 include.push(formname.elements[spareboxes[j]].value);
12000:             }
12001:         }
12002:     }
12003:     for (var i=0; i<types.length; i++) {
12004:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
12005:         var selIdx = newSpare.selectedIndex;
12006:         var currnew = newSpare.options[selIdx].value;
12007:         var okSpares = new Array();
12008:         for (var j=0; j<newSpare.options.length; j++) {
12009:             var possible = newSpare.options[j].value;
12010:             if (possible != '') {
12011:                 if (exclude.indexOf(possible) == -1) {
12012:                     okSpares.push(possible);
12013:                 } else {
12014:                     if (currnew == possible) {
12015:                         selIdx = 0;
12016:                     }
12017:                 }
12018:             }
12019:         }
12020:         for (var k=0; k<include.length; k++) {
12021:             if (okSpares.indexOf(include[k]) == -1) {
12022:                 okSpares.push(include[k]);
12023:             }
12024:         }
12025:         okSpares.sort();
12026:         newSpare.options.length = 0;
12027:         if (selIdx == 0) {
12028:             newSpare.options[0] = new Option("$select","",true,true);
12029:         } else {
12030:             newSpare.options[0] = new Option("$select","",false,false);
12031:         }
12032:         for (var m=0; m<okSpares.length; m++) {
12033:             var idx = m+1;
12034:             var selThis = 0;
12035:             if (selIdx != 0) {
12036:                 if (okSpares[m] == currnew) {
12037:                     selThis = 1;
12038:                 }
12039:             }
12040:             if (selThis == 1) {
12041:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
12042:             } else {
12043:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
12044:             }
12045:         }
12046:     }
12047:     return;
12048: }
12049: 
12050: function checkNewSpares(lonhost,type) {
12051:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
12052:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
12053:     if (chosen != '') { 
12054:         var othertype;
12055:         var othernewSpare;
12056:         if (type == 'primary') {
12057:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
12058:         }
12059:         if (type == 'default') {
12060:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
12061:         }
12062:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
12063:             othernewSpare.selectedIndex = 0;
12064:         }
12065:     }
12066:     return;
12067: }
12068: 
12069: // ]]>
12070: </script>
12071: 
12072: END
12073: 
12074: }
12075: 
12076: sub common_domprefs_js {
12077:     return <<"END";
12078: 
12079: <script type="text/javascript">
12080: // <![CDATA[
12081: 
12082: function getIndicesByName(formname,item) {
12083:     var group = new Array();
12084:     for (var i=0;i<formname.elements.length;i++) {
12085:         if (formname.elements[i].name == item) {
12086:             group.push(formname.elements[i].id);
12087:         }
12088:     }
12089:     return group;
12090: }
12091: 
12092: // ]]>
12093: </script>
12094: 
12095: END
12096: 
12097: }
12098: 
12099: sub recaptcha_js {
12100:     my %lt = &captcha_phrases();
12101:     return <<"END";
12102: 
12103: <script type="text/javascript">
12104: // <![CDATA[
12105: 
12106: function updateCaptcha(caller,context) {
12107:     var privitem;
12108:     var pubitem;
12109:     var privtext;
12110:     var pubtext;
12111:     if (document.getElementById(context+'_recaptchapub')) {
12112:         pubitem = document.getElementById(context+'_recaptchapub');
12113:     } else {
12114:         return;
12115:     }
12116:     if (document.getElementById(context+'_recaptchapriv')) {
12117:         privitem = document.getElementById(context+'_recaptchapriv');
12118:     } else {
12119:         return;
12120:     }
12121:     if (document.getElementById(context+'_recaptchapubtxt')) {
12122:         pubtext = document.getElementById(context+'_recaptchapubtxt');
12123:     } else {
12124:         return;
12125:     }
12126:     if (document.getElementById(context+'_recaptchaprivtxt')) {
12127:         privtext = document.getElementById(context+'_recaptchaprivtxt');
12128:     } else {
12129:         return;
12130:     }
12131:     if (caller.checked) {
12132:         if (caller.value == 'recaptcha') {
12133:             pubitem.type = 'text';
12134:             privitem.type = 'text';
12135:             pubitem.size = '40';
12136:             privitem.size = '40';
12137:             pubtext.innerHTML = "$lt{'pub'}";
12138:             privtext.innerHTML = "$lt{'priv'}";
12139:         } else {
12140:             pubitem.type = 'hidden';
12141:             privitem.type = 'hidden';
12142:             pubtext.innerHTML = '';
12143:             privtext.innerHTML = '';
12144:         }
12145:     }
12146:     return;
12147: }
12148: 
12149: // ]]>
12150: </script>
12151: 
12152: END
12153: 
12154: }
12155: 
12156: sub toggle_display_js {
12157:     return <<"END";
12158: 
12159: <script type="text/javascript">
12160: // <![CDATA[
12161: 
12162: function toggleDisplay(domForm,caller) {
12163:     if (document.getElementById(caller)) {
12164:         var divitem = document.getElementById(caller);
12165:         var optionsElement = domForm.coursecredits;
12166:         var checkval = 1;
12167:         var dispval = 'block';
12168:         if (caller == 'emailoptions') {
12169:             optionsElement = domForm.cancreate_email; 
12170:         }
12171:         if (caller == 'studentsubmission') {
12172:             optionsElement = domForm.postsubmit;
12173:         }
12174:         if (caller == 'cloneinstcode') {
12175:             optionsElement = domForm.canclone;
12176:             checkval = 'instcode';
12177:         }
12178:         if (optionsElement.length) {
12179:             var currval;
12180:             for (var i=0; i<optionsElement.length; i++) {
12181:                 if (optionsElement[i].checked) {
12182:                    currval = optionsElement[i].value;
12183:                 }
12184:             }
12185:             if (currval == checkval) {
12186:                 divitem.style.display = dispval;
12187:             } else {
12188:                 divitem.style.display = 'none';
12189:             }
12190:         }
12191:     }
12192:     return;
12193: }
12194: 
12195: // ]]>
12196: </script>
12197: 
12198: END
12199: 
12200: }
12201: 
12202: sub captcha_phrases {
12203:     return &Apache::lonlocal::texthash (
12204:                  priv => 'Private key',
12205:                  pub  => 'Public key',
12206:                  original  => 'original (CAPTCHA)',
12207:                  recaptcha => 'successor (ReCAPTCHA)',
12208:                  notused   => 'unused',
12209:     );
12210: }
12211: 
12212: sub devalidate_remote_domconfs {
12213:     my ($dom,$cachekeys) = @_;
12214:     return unless (ref($cachekeys) eq 'HASH');
12215:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
12216:     my %thismachine;
12217:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
12218:     my @posscached = ('domainconfig','domdefaults');
12219:     if (keys(%servers)) {
12220:         foreach my $server (keys(%servers)) {
12221:             next if ($thismachine{$server});
12222:             my @cached;
12223:             foreach my $name (@posscached) {
12224:                 if ($cachekeys->{$name}) {
12225:                     push(@cached,&escape($name).':'.&escape($dom));
12226:                 }
12227:             }
12228:             if (@cached) {
12229:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
12230:             }
12231:         }
12232:     }
12233:     return;
12234: }
12235: 
12236: 1;

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