File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.192: download - view: text, annotated - select for diffs
Fri Mar 1 04:17:15 2013 UTC (11 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Domain configuration to specify defaults for credits earned in a course
  (Course defaults) for official or unofficial courses.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.192 2013/03/01 04:17:15 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.
   98: 
   99: Outputs: 1
  100: 
  101: $datatable  - HTML containing form elements which allow settings to be changed. 
  102: 
  103: In the case of course requests, radio buttons are displayed for each institutional
  104: affiliate type (and also default, and _LC_adv) for each of the course types 
  105: (official, unofficial and community).  In each case the radio buttons allow the 
  106: selection of one of four values:
  107: 
  108: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  109: which have the following effects:
  110: 
  111: 0
  112: 
  113: =over
  114: 
  115: - course requests are not allowed for this course types/affiliation
  116: 
  117: =back
  118: 
  119: approval 
  120: 
  121: =over 
  122: 
  123: - course requests must be approved by a Doman Coordinator in the 
  124: course's domain
  125: 
  126: =back
  127: 
  128: validate 
  129: 
  130: =over
  131: 
  132: - an institutional validation (e.g., check requestor is instructor
  133: of record) needs to be passed before the course will be created.  The required
  134: validation is in localenroll.pm on the primary library server for the course 
  135: domain.
  136: 
  137: =back
  138: 
  139: autolimit 
  140: 
  141: =over
  142:  
  143: - course requests will be processed automatically up to a limit of
  144: N requests for the course type for the particular requestor.
  145: If N is undefined, there is no limit to the number of course requests
  146: which a course owner may submit and have processed automatically. 
  147: 
  148: =back
  149: 
  150: =item modify_quotas() 
  151: 
  152: =back
  153: 
  154: =cut
  155: 
  156: package Apache::domainprefs;
  157: 
  158: use strict;
  159: use Apache::Constants qw(:common :http);
  160: use Apache::lonnet;
  161: use Apache::loncommon();
  162: use Apache::lonhtmlcommon();
  163: use Apache::lonlocal;
  164: use Apache::lonmsg();
  165: use Apache::lonconfigsettings;
  166: use LONCAPA qw(:DEFAULT :match);
  167: use LONCAPA::Enrollment;
  168: use LONCAPA::lonauthcgi();
  169: use File::Copy;
  170: use Locale::Language;
  171: use DateTime::TimeZone;
  172: use DateTime::Locale;
  173: 
  174: my $registered_cleanup;
  175: my $modified_urls;
  176: 
  177: sub handler {
  178:     my $r=shift;
  179:     if ($r->header_only) {
  180:         &Apache::loncommon::content_type($r,'text/html');
  181:         $r->send_http_header;
  182:         return OK;
  183:     }
  184: 
  185:     my $context = 'domain';
  186:     my $dom = $env{'request.role.domain'};
  187:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  188:     if (&Apache::lonnet::allowed('mau',$dom)) {
  189:         &Apache::loncommon::content_type($r,'text/html');
  190:         $r->send_http_header;
  191:     } else {
  192:         $env{'user.error.msg'}=
  193:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  194:         return HTTP_NOT_ACCEPTABLE;
  195:     }
  196: 
  197:     $registered_cleanup=0;
  198:     @{$modified_urls}=();
  199: 
  200:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  201:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  202:                                             ['phase','actions']);
  203:     my $phase = 'pickactions';
  204:     if ( exists($env{'form.phase'}) ) {
  205:         $phase = $env{'form.phase'};
  206:     }
  207:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  208:     my %domconfig =
  209:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  210:                 'quotas','autoenroll','autoupdate','autocreate',
  211:                 'directorysrch','usercreation','usermodification',
  212:                 'contacts','defaults','scantron','coursecategories',
  213:                 'serverstatuses','requestcourses','helpsettings',
  214:                 'coursedefaults','usersessions','loadbalancing',
  215:                 'requestauthor'],$dom);
  216:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  217:                        'autoupdate','autocreate','directorysrch','contacts',
  218:                        'usercreation','usermodification','scantron',
  219:                        'requestcourses','requestauthor','coursecategories',
  220:                        'serverstatuses','helpsettings',
  221:                        'coursedefaults','usersessions');
  222:     my %existing;
  223:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  224:         %existing = %{$domconfig{'loadbalancing'}};
  225:     }
  226:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  227:         push(@prefs_order,'loadbalancing');
  228:     }
  229:     my %prefs = (
  230:         'rolecolors' =>
  231:                    { text => 'Default color schemes',
  232:                      help => 'Domain_Configuration_Color_Schemes',
  233:                      header => [{col1 => 'Student Settings',
  234:                                  col2 => '',},
  235:                                 {col1 => 'Coordinator Settings',
  236:                                  col2 => '',},
  237:                                 {col1 => 'Author Settings',
  238:                                  col2 => '',},
  239:                                 {col1 => 'Administrator Settings',
  240:                                  col2 => '',}],
  241:                     },
  242:         'login' =>
  243:                     { text => 'Log-in page options',
  244:                       help => 'Domain_Configuration_Login_Page',
  245:                       header => [{col1 => 'Log-in Page Items',
  246:                                   col2 => '',},
  247:                                  {col1 => 'Log-in Help',
  248:                                   col2 => 'Value'}],
  249:                     },
  250:         'defaults' => 
  251:                     { text => 'Default authentication/language/timezone/portal',
  252:                       help => 'Domain_Configuration_LangTZAuth',
  253:                       header => [{col1 => 'Setting',
  254:                                   col2 => 'Value'}],
  255:                     },
  256:         'quotas' => 
  257:                     { text => 'Blogs, personal web pages, webDAV, portfolios',
  258:                       help => 'Domain_Configuration_Quotas',
  259:                       header => [{col1 => 'User affiliation',
  260:                                   col2 => 'Available tools',
  261:                                   col3 => 'Portfolio quota',}],
  262:                     },
  263:         'autoenroll' =>
  264:                    { text => 'Auto-enrollment settings',
  265:                      help => 'Domain_Configuration_Auto_Enrollment',
  266:                      header => [{col1 => 'Configuration setting',
  267:                                  col2 => 'Value(s)'}],
  268:                    },
  269:         'autoupdate' => 
  270:                    { text => 'Auto-update settings',
  271:                      help => 'Domain_Configuration_Auto_Updates',
  272:                      header => [{col1 => 'Setting',
  273:                                  col2 => 'Value',},
  274:                                 {col1 => 'Setting',
  275:                                  col2 => 'Affiliation'},
  276:                                 {col1 => 'User population',
  277:                                  col2 => 'Updateable user data'}],
  278:                   },
  279:         'autocreate' => 
  280:                   { text => 'Auto-course creation settings',
  281:                      help => 'Domain_Configuration_Auto_Creation',
  282:                      header => [{col1 => 'Configuration Setting',
  283:                                  col2 => 'Value',}],
  284:                   },
  285:         'directorysrch' => 
  286:                   { text => 'Institutional directory searches',
  287:                     help => 'Domain_Configuration_InstDirectory_Search',
  288:                     header => [{col1 => 'Setting',
  289:                                 col2 => 'Value',}],
  290:                   },
  291:         'contacts' =>
  292:                   { text => 'Contact Information',
  293:                     help => 'Domain_Configuration_Contact_Info',
  294:                     header => [{col1 => 'Setting',
  295:                                 col2 => 'Value',}],
  296:                   },
  297: 
  298:         'usercreation' => 
  299:                   { text => 'User creation',
  300:                     help => 'Domain_Configuration_User_Creation',
  301:                     header => [{col1 => 'Format rule type',
  302:                                 col2 => 'Format rules in force'},
  303:                                {col1 => 'User account creation',
  304:                                 col2 => 'Usernames which may be created',},
  305:                                {col1 => 'Context',
  306:                                 col2 => 'Assignable authentication types'}],
  307:                   },
  308:         'usermodification' =>
  309:                   { text => 'User modification',
  310:                     help => 'Domain_Configuration_User_Modification',
  311:                     header => [{col1 => 'Target user has role',
  312:                                 col2 => 'User information updateable in author context'},
  313:                                {col1 => 'Target user has role',
  314:                                 col2 => 'User information updateable in course context'},
  315:                                {col1 => "Status of user",
  316:                                 col2 => 'Information settable when self-creating account (if directory data blank)'}],
  317:                   },
  318:         'scantron' =>
  319:                   { text => 'Bubblesheet format file',
  320:                     help => 'Domain_Configuration_Scantron_Format',
  321:                     header => [ {col1 => 'Item',
  322:                                  col2 => '',
  323:                               }],
  324:                   },
  325:         'requestcourses' => 
  326:                  {text => 'Request creation of courses',
  327:                   help => 'Domain_Configuration_Request_Courses',
  328:                   header => [{col1 => 'User affiliation',
  329:                               col2 => 'Availability/Processing of requests',},
  330:                              {col1 => 'Setting',
  331:                               col2 => 'Value'}],
  332:                  },
  333:         'requestauthor' =>
  334:                  {text => 'Request authoring space',
  335:                   help => 'Domain_Configuration_Request_Author',
  336:                   header => [{col1 => 'User affiliation',
  337:                               col2 => 'Availability/Processing of requests',},
  338:                              {col1 => 'Setting',
  339:                               col2 => 'Value'}],
  340:                  },
  341:         'coursecategories' =>
  342:                   { text => 'Cataloging of courses/communities',
  343:                     help => 'Domain_Configuration_Cataloging_Courses',
  344:                     header => [{col1 => 'Category settings',
  345:                                 col2 => '',},
  346:                                {col1 => 'Categories',
  347:                                 col2 => '',
  348:                                }],
  349:                   },
  350:         'serverstatuses' =>
  351:                  {text   => 'Access to server status pages',
  352:                   help   => 'Domain_Configuration_Server_Status',
  353:                   header => [{col1 => 'Status Page',
  354:                               col2 => 'Other named users',
  355:                               col3 => 'Specific IPs',
  356:                             }],
  357:                  },
  358:         'helpsettings' =>
  359:                  {text   => 'Help page settings',
  360:                   help   => 'Domain_Configuration_Help_Settings',
  361:                   header => [{col1 => 'Help Settings (logged-in users)',
  362:                               col2 => 'Value'}],
  363:                  },
  364:         'coursedefaults' => 
  365:                  {text => 'Course/Community defaults',
  366:                   help => 'Domain_Configuration_Course_Defaults',
  367:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  368:                               col2 => 'Value',},
  369:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  370:                               col2 => 'Value',},],
  371:                  },
  372:         'privacy' => 
  373:                  {text   => 'User Privacy',
  374:                   help   => 'Domain_Configuration_User_Privacy',
  375:                   header => [{col1 => 'Setting',
  376:                               col2 => 'Value',}],
  377:                  },
  378:         'usersessions' =>
  379:                  {text  => 'User session hosting/offloading',
  380:                   help  => 'Domain_Configuration_User_Sessions',
  381:                   header => [{col1 => 'Domain server',
  382:                               col2 => 'Servers to offload sessions to when busy'},
  383:                              {col1 => 'Hosting of users from other domains',
  384:                               col2 => 'Rules'},
  385:                              {col1 => "Hosting domain's own users elsewhere",
  386:                               col2 => 'Rules'}],
  387:                  },
  388:          'loadbalancing' =>
  389:                  {text  => 'Dedicated Load Balancer(s)',
  390:                   help  => 'Domain_Configuration_Load_Balancing',
  391:                   header => [{col1 => 'Balancers',
  392:                               col2 => 'Default destinations',
  393:                               col3 => 'User affiliation',
  394:                               col4 => 'Overrides'},
  395:                             ],
  396:                  },
  397:     );
  398:     if (keys(%servers) > 1) {
  399:         $prefs{'login'}  = { text   => 'Log-in page options',
  400:                              help   => 'Domain_Configuration_Login_Page',
  401:                             header => [{col1 => 'Log-in Service',
  402:                                         col2 => 'Server Setting',},
  403:                                        {col1 => 'Log-in Page Items',
  404:                                         col2 => ''},
  405:                                        {col1 => 'Log-in Help',
  406:                                         col2 => 'Value'}],
  407:                            };
  408:     }
  409: 
  410:     my @roles = ('student','coordinator','author','admin');
  411:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  412:     &Apache::lonhtmlcommon::add_breadcrumb
  413:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  414:       text=>"Settings to display/modify"});
  415:     my $confname = $dom.'-domainconfig';
  416: 
  417:     if ($phase eq 'process') {
  418:         &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,\@roles);
  419:     } elsif ($phase eq 'display') {
  420:         my $js = &recaptcha_js().
  421:                  &credits_js();
  422:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  423:             my ($othertitle,$usertypes,$types) =
  424:                 &Apache::loncommon::sorted_inst_types($dom);
  425:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  426:                                           $domconfig{'loadbalancing'}).
  427:                    &new_spares_js().
  428:                    &common_domprefs_js().
  429:                    &Apache::loncommon::javascript_array_indexof();
  430:         }
  431:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  432:     } else {
  433: # check if domconfig user exists for the domain.
  434:         my $servadm = $r->dir_config('lonAdmEMail');
  435:         my ($configuserok,$author_ok,$switchserver) =
  436:             &config_check($dom,$confname,$servadm);
  437:         unless ($configuserok eq 'ok') {
  438:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  439:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  440:                            $confname).
  441:                       '<br />'
  442:             );
  443:             if ($switchserver) {
  444:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  445:                           '<br />'.
  446:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  447:                           '<br />'.
  448:                           &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).
  449:                           '<br />'.
  450:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  451:                 );
  452:             } else {
  453:                 $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.').
  454:                           '<br />'.
  455:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  456:                 );
  457:             }
  458:             $r->print(&Apache::loncommon::end_page());
  459:             return OK;
  460:         }
  461:         if (keys(%domconfig) == 0) {
  462:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  463:             my @ids=&Apache::lonnet::current_machine_ids();
  464:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  465:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  466:                 my @loginimages = ('img','logo','domlogo','login');
  467:                 my $custom_img_count = 0;
  468:                 foreach my $img (@loginimages) {
  469:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  470:                         $custom_img_count ++;
  471:                     }
  472:                 }
  473:                 foreach my $role (@roles) {
  474:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  475:                         $custom_img_count ++;
  476:                     }
  477:                 }
  478:                 if ($custom_img_count > 0) {
  479:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  480:                     my $switch_server = &check_switchserver($dom,$confname);
  481:                     $r->print(
  482:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  483:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  484:     &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 />'.
  485:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  486:                     if ($switch_server) {
  487:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  488:                     }
  489:                     $r->print(&Apache::loncommon::end_page());
  490:                     return OK;
  491:                 }
  492:             }
  493:         }
  494:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  495:     }
  496:     return OK;
  497: }
  498: 
  499: sub process_changes {
  500:     my ($r,$dom,$confname,$action,$roles,$values) = @_;
  501:     my %domconfig;
  502:     if (ref($values) eq 'HASH') {
  503:         %domconfig = %{$values};
  504:     }
  505:     my $output;
  506:     if ($action eq 'login') {
  507:         $output = &modify_login($r,$dom,$confname,%domconfig);
  508:     } elsif ($action eq 'rolecolors') {
  509:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  510:                                      %domconfig);
  511:     } elsif ($action eq 'quotas') {
  512:         $output = &modify_quotas($dom,$action,%domconfig);
  513:     } elsif ($action eq 'autoenroll') {
  514:         $output = &modify_autoenroll($dom,%domconfig);
  515:     } elsif ($action eq 'autoupdate') {
  516:         $output = &modify_autoupdate($dom,%domconfig);
  517:     } elsif ($action eq 'autocreate') {
  518:         $output = &modify_autocreate($dom,%domconfig);
  519:     } elsif ($action eq 'directorysrch') {
  520:         $output = &modify_directorysrch($dom,%domconfig);
  521:     } elsif ($action eq 'usercreation') {
  522:         $output = &modify_usercreation($dom,%domconfig);
  523:     } elsif ($action eq 'usermodification') {
  524:         $output = &modify_usermodification($dom,%domconfig);
  525:     } elsif ($action eq 'contacts') {
  526:         $output = &modify_contacts($dom,%domconfig);
  527:     } elsif ($action eq 'defaults') {
  528:         $output = &modify_defaults($dom,$r);
  529:     } elsif ($action eq 'scantron') {
  530:         $output = &modify_scantron($r,$dom,$confname,%domconfig);
  531:     } elsif ($action eq 'coursecategories') {
  532:         $output = &modify_coursecategories($dom,%domconfig);
  533:     } elsif ($action eq 'serverstatuses') {
  534:         $output = &modify_serverstatuses($dom,%domconfig);
  535:     } elsif ($action eq 'requestcourses') {
  536:         $output = &modify_quotas($dom,$action,%domconfig);
  537:     } elsif ($action eq 'requestauthor') {
  538:         $output = &modify_quotas($dom,$action,%domconfig);
  539:     } elsif ($action eq 'helpsettings') {
  540:         $output = &modify_helpsettings($r,$dom,$confname,%domconfig);
  541:     } elsif ($action eq 'coursedefaults') {
  542:         $output = &modify_coursedefaults($dom,%domconfig);
  543:     } elsif ($action eq 'usersessions') {
  544:         $output = &modify_usersessions($dom,%domconfig);
  545:     } elsif ($action eq 'loadbalancing') {
  546:         $output = &modify_loadbalancing($dom,%domconfig);
  547:     }
  548:     return $output;
  549: }
  550: 
  551: sub print_config_box {
  552:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  553:     my $rowtotal = 0;
  554:     my $output;
  555:     if ($action eq 'coursecategories') {
  556:         $output = &coursecategories_javascript($settings);
  557:     }
  558:     $output .= 
  559:          '<table class="LC_nested_outer">
  560:           <tr>
  561:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  562:            &mt($item->{text}).'&nbsp;'.
  563:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  564:           '</tr>';
  565:     $rowtotal ++;
  566:     my $numheaders = 1;
  567:     if (ref($item->{'header'}) eq 'ARRAY') {
  568:         $numheaders = scalar(@{$item->{'header'}});
  569:     }
  570:     if ($numheaders > 1) {
  571:         my $colspan = '';
  572:         my $rightcolspan = '';
  573:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') || 
  574:             (($action eq 'login') && ($numheaders < 3))) {
  575:             $colspan = ' colspan="2"';
  576:         }
  577:         if ($action eq 'usersessions') {
  578:             $rightcolspan = ' colspan="3"'; 
  579:         }
  580:         $output .= '
  581:           <tr>
  582:            <td>
  583:             <table class="LC_nested">
  584:              <tr class="LC_info_row">
  585:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  586:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  587:              </tr>';
  588:         $rowtotal ++;
  589:         if ($action eq 'autoupdate') {
  590:             $output .= &print_autoupdate('top',$dom,$settings,\$rowtotal);
  591:         } elsif ($action eq 'usercreation') {
  592:             $output .= &print_usercreation('top',$dom,$settings,\$rowtotal);
  593:         } elsif ($action eq 'usermodification') {
  594:             $output .= &print_usermodification('top',$dom,$settings,\$rowtotal);
  595:         } elsif ($action eq 'coursecategories') {
  596:             $output .= &print_coursecategories('top',$dom,$item,$settings,\$rowtotal);
  597:         } elsif ($action eq 'login') {
  598:             if ($numheaders == 3) {
  599:                 $colspan = ' colspan="2"';
  600:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  601:             } else {
  602:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  603:             }
  604:         } elsif ($action eq 'requestcourses') {
  605:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  606:         } elsif ($action eq 'requestauthor') {
  607:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  608:         } elsif ($action eq 'usersessions') {
  609:             $output .= &print_usersessions('top',$dom,$settings,\$rowtotal); 
  610:         } elsif ($action eq 'rolecolors') {
  611:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  612:         } elsif ($action eq 'coursedefaults') {
  613:             $output .= &print_coursedefaults('top',$dom,$settings,\$rowtotal);
  614:         }
  615:         $output .= '
  616:            </table>
  617:           </td>
  618:          </tr>
  619:          <tr>
  620:            <td>
  621:             <table class="LC_nested">
  622:              <tr class="LC_info_row">
  623:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>';
  624:         $output .= '
  625:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  626:              </tr>';
  627:             $rowtotal ++;
  628:         if ($action eq 'autoupdate') {
  629:             $output .= &print_autoupdate('middle',$dom,$settings,\$rowtotal).'
  630:            </table>
  631:           </td>
  632:          </tr>
  633:          <tr>
  634:            <td>
  635:             <table class="LC_nested">
  636:              <tr class="LC_info_row">
  637:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  638:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  639:             &print_autoupdate('bottom',$dom,$settings,\$rowtotal);
  640:             $rowtotal ++;
  641:         } elsif ($action eq 'usercreation') {
  642:             $output .= &print_usercreation('middle',$dom,$settings,\$rowtotal).'
  643:            </table>
  644:           </td>
  645:          </tr>
  646:          <tr>
  647:            <td>
  648:             <table class="LC_nested">
  649:              <tr class="LC_info_row">
  650:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  651:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>             </tr>'.
  652:             &print_usercreation('bottom',$dom,$settings,\$rowtotal);
  653:             $rowtotal ++;
  654:         } elsif ($action eq 'usermodification') {
  655:             $output .= &print_usermodification('middle',$dom,$settings,\$rowtotal).'
  656:            </table>
  657:           </td>
  658:          </tr>
  659:          <tr>
  660:            <td>
  661:             <table class="LC_nested">
  662:              <tr class="LC_info_row">
  663:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  664:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  665:                        &print_usermodification('bottom',$dom,$settings,\$rowtotal);
  666:             $rowtotal ++;
  667:         } elsif ($action eq 'coursecategories') {
  668:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  669:         } elsif ($action eq 'login') {
  670:             if ($numheaders == 3) {
  671:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  672:            </table>
  673:           </td>
  674:          </tr>
  675:          <tr>
  676:            <td>
  677:             <table class="LC_nested">
  678:              <tr class="LC_info_row">
  679:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  680:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  681:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  682:                 $rowtotal ++;
  683:             } else {
  684:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  685:             }
  686:         } elsif ($action eq 'requestcourses') {
  687:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  688:         } elsif ($action eq 'requestauthor') {
  689:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  690:         } elsif ($action eq 'usersessions') {
  691:             $output .= &print_usersessions('middle',$dom,$settings,\$rowtotal).'
  692:            </table>
  693:           </td>
  694:          </tr>
  695:          <tr>
  696:            <td>
  697:             <table class="LC_nested">
  698:              <tr class="LC_info_row">
  699:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  700:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  701:                        &print_usersessions('bottom',$dom,$settings,\$rowtotal);
  702:             $rowtotal ++;
  703:         } elsif ($action eq 'coursedefaults') {
  704:             $output .= &print_coursedefaults('bottom',$dom,$settings,\$rowtotal);
  705:         } elsif ($action eq 'rolecolors') {
  706:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  707:            </table>
  708:           </td>
  709:          </tr>
  710:          <tr>
  711:            <td>
  712:             <table class="LC_nested">
  713:              <tr class="LC_info_row">
  714:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  715:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  716:               <td class="LC_right_item" valign="top">'.
  717:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  718:              </tr>'.
  719:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  720:            </table>
  721:           </td>
  722:          </tr>
  723:          <tr>
  724:            <td>
  725:             <table class="LC_nested">
  726:              <tr class="LC_info_row">
  727:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  728:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  729:              </tr>'.
  730:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  731:             $rowtotal += 2;
  732:         }
  733:     } else {
  734:         $output .= '
  735:           <tr>
  736:            <td>
  737:             <table class="LC_nested">
  738:              <tr class="LC_info_row">';
  739:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  740:             $output .= '  
  741:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  742:         } elsif ($action eq 'serverstatuses') {
  743:             $output .= '
  744:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  745:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  746: 
  747:         } else {
  748:             $output .= '
  749:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  750:         }
  751:         if (defined($item->{'header'}->[0]->{'col3'})) {
  752:             $output .= '<td class="LC_left_item" valign="top">'.
  753:                        &mt($item->{'header'}->[0]->{'col2'});
  754:             if ($action eq 'serverstatuses') {
  755:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  756:             } 
  757:         } else {
  758:             $output .= '<td class="LC_right_item" valign="top">'.
  759:                        &mt($item->{'header'}->[0]->{'col2'});
  760:         }
  761:         $output .= '</td>';
  762:         if ($item->{'header'}->[0]->{'col3'}) {
  763:             if (defined($item->{'header'}->[0]->{'col4'})) {
  764:                 $output .= '<td class="LC_left_item" valign="top">'.
  765:                             &mt($item->{'header'}->[0]->{'col3'});
  766:             } else {
  767:                 $output .= '<td class="LC_right_item" valign="top">'.
  768:                            &mt($item->{'header'}->[0]->{'col3'});
  769:             }
  770:             if ($action eq 'serverstatuses') {
  771:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  772:             }
  773:             $output .= '</td>';
  774:         }
  775:         if ($item->{'header'}->[0]->{'col4'}) {
  776:             $output .= '<td class="LC_right_item" valign="top">'.
  777:                        &mt($item->{'header'}->[0]->{'col4'});
  778:         }
  779:         $output .= '</tr>';
  780:         $rowtotal ++;
  781:         if ($action eq 'quotas') {
  782:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  783:         } elsif ($action eq 'autoenroll') {
  784:             $output .= &print_autoenroll($dom,$settings,\$rowtotal);
  785:         } elsif ($action eq 'autocreate') {
  786:             $output .= &print_autocreate($dom,$settings,\$rowtotal);
  787:         } elsif ($action eq 'directorysrch') {
  788:             $output .= &print_directorysrch($dom,$settings,\$rowtotal);
  789:         } elsif ($action eq 'contacts') {
  790:             $output .= &print_contacts($dom,$settings,\$rowtotal);
  791:         } elsif ($action eq 'defaults') {
  792:             $output .= &print_defaults($dom,\$rowtotal);
  793:         } elsif ($action eq 'scantron') {
  794:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  795:         } elsif ($action eq 'serverstatuses') {
  796:             $output .= &print_serverstatuses($dom,$settings,\$rowtotal);
  797:         } elsif ($action eq 'helpsettings') {
  798:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  799:         } elsif ($action eq 'loadbalancing') {
  800:             $output .= &print_loadbalancing($dom,$settings,\$rowtotal);
  801:         }
  802:     }
  803:     $output .= '
  804:    </table>
  805:   </td>
  806:  </tr>
  807: </table><br />';
  808:     return ($output,$rowtotal);
  809: }
  810: 
  811: sub print_login {
  812:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  813:     my ($css_class,$datatable);
  814:     my %choices = &login_choices();
  815: 
  816:     if ($caller eq 'service') {
  817:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  818:         my $choice = $choices{'disallowlogin'};
  819:         $css_class = ' class="LC_odd_row"';
  820:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  821:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  822:                       '<th>'.$choices{'server'}.'</th>'.
  823:                       '<th>'.$choices{'serverpath'}.'</th>'.
  824:                       '<th>'.$choices{'custompath'}.'</th>'.
  825:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  826:         my %disallowed;
  827:         if (ref($settings) eq 'HASH') {
  828:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  829:                %disallowed = %{$settings->{'loginvia'}};
  830:             }
  831:         }
  832:         foreach my $lonhost (sort(keys(%servers))) {
  833:             my $direct = 'selected="selected"';
  834:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  835:                 if ($disallowed{$lonhost}{'server'} ne '') {
  836:                     $direct = '';
  837:                 }
  838:             }
  839:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  840:                           '<td><select name="'.$lonhost.'_server">'.
  841:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  842:                           '</option>';
  843:             foreach my $hostid (sort(keys(%servers))) {
  844:                 next if ($servers{$hostid} eq $servers{$lonhost});
  845:                 my $selected = '';
  846:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  847:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  848:                         $selected = 'selected="selected"';
  849:                     }
  850:                 }
  851:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  852:                               $servers{$hostid}.'</option>';
  853:             }
  854:             $datatable .= '</select></td>'.
  855:                           '<td><select name="'.$lonhost.'_serverpath">';
  856:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  857:                 my $pathname = $path;
  858:                 if ($path eq 'custom') {
  859:                     $pathname = &mt('Custom Path').' ->';
  860:                 }
  861:                 my $selected = '';
  862:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  863:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  864:                         $selected = 'selected="selected"';
  865:                     }
  866:                 } elsif ($path eq '') {
  867:                     $selected = 'selected="selected"';
  868:                 }
  869:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  870:             }
  871:             $datatable .= '</select></td>';
  872:             my ($custom,$exempt);
  873:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  874:                 $custom = $disallowed{$lonhost}{'custompath'};
  875:                 $exempt = $disallowed{$lonhost}{'exempt'};
  876:             }
  877:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  878:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  879:                           '</tr>';
  880:         }
  881:         $datatable .= '</table></td></tr>';
  882:         return $datatable;
  883:     } elsif ($caller eq 'page') {
  884:         my %defaultchecked = ( 
  885:                                'coursecatalog' => 'on',
  886:                                'helpdesk'      => 'on',
  887:                                'adminmail'     => 'off',
  888:                                'newuser'       => 'off',
  889:                              );
  890:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  891:         my (%checkedon,%checkedoff);
  892:         foreach my $item (@toggles) {
  893:             if ($defaultchecked{$item} eq 'on') { 
  894:                 $checkedon{$item} = ' checked="checked" ';
  895:                 $checkedoff{$item} = ' ';
  896:             } elsif ($defaultchecked{$item} eq 'off') {
  897:                 $checkedoff{$item} = ' checked="checked" ';
  898:                 $checkedon{$item} = ' ';
  899:             }
  900:         }
  901:         my @images = ('img','logo','domlogo','login');
  902:         my @logintext = ('textcol','bgcol');
  903:         my @bgs = ('pgbg','mainbg','sidebg');
  904:         my @links = ('link','alink','vlink');
  905:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  906:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  907:         my (%is_custom,%designs);
  908:         my %defaults = (
  909:                        font => $defaultdesign{'login.font'},
  910:                        );
  911:         foreach my $item (@images) {
  912:             $defaults{$item} = $defaultdesign{'login.'.$item};
  913:             $defaults{'showlogo'}{$item} = 1;
  914:         }
  915:         foreach my $item (@bgs) {
  916:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  917:         }
  918:         foreach my $item (@logintext) {
  919:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  920:         }
  921:         foreach my $item (@links) {
  922:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  923:         }
  924:         if (ref($settings) eq 'HASH') {
  925:             foreach my $item (@toggles) {
  926:                 if ($settings->{$item} eq '1') {
  927:                     $checkedon{$item} =  ' checked="checked" ';
  928:                     $checkedoff{$item} = ' ';
  929:                 } elsif ($settings->{$item} eq '0') {
  930:                     $checkedoff{$item} =  ' checked="checked" ';
  931:                     $checkedon{$item} = ' ';
  932:                 }
  933:             }
  934:             foreach my $item (@images) {
  935:                 if (defined($settings->{$item})) {
  936:                     $designs{$item} = $settings->{$item};
  937:                     $is_custom{$item} = 1;
  938:                 }
  939:                 if (defined($settings->{'showlogo'}{$item})) {
  940:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  941:                 }
  942:             }
  943:             foreach my $item (@logintext) {
  944:                 if ($settings->{$item} ne '') {
  945:                     $designs{'logintext'}{$item} = $settings->{$item};
  946:                     $is_custom{$item} = 1;
  947:                 }
  948:             }
  949:             if ($settings->{'font'} ne '') {
  950:                 $designs{'font'} = $settings->{'font'};
  951:                 $is_custom{'font'} = 1;
  952:             }
  953:             foreach my $item (@bgs) {
  954:                 if ($settings->{$item} ne '') {
  955:                     $designs{'bgs'}{$item} = $settings->{$item};
  956:                     $is_custom{$item} = 1;
  957:                 }
  958:             }
  959:             foreach my $item (@links) {
  960:                 if ($settings->{$item} ne '') {
  961:                     $designs{'links'}{$item} = $settings->{$item};
  962:                     $is_custom{$item} = 1;
  963:                 }
  964:             }
  965:         } else {
  966:             if ($designhash{$dom.'.login.font'} ne '') {
  967:                 $designs{'font'} = $designhash{$dom.'.login.font'};
  968:                 $is_custom{'font'} = 1;
  969:             }
  970:             foreach my $item (@images) {
  971:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  972:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
  973:                     $is_custom{$item} = 1;
  974:                 }
  975:             }
  976:             foreach my $item (@bgs) {
  977:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  978:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
  979:                     $is_custom{$item} = 1;
  980:                 }
  981:             }
  982:             foreach my $item (@links) {
  983:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  984:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
  985:                     $is_custom{$item} = 1;
  986:                 }
  987:             }
  988:         }
  989:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
  990:                                                       logo => 'Institution Logo',
  991:                                                       domlogo => 'Domain Logo',
  992:                                                       login => 'Login box');
  993:         my $itemcount = 1;
  994:         foreach my $item (@toggles) {
  995:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
  996:             $datatable .=  
  997:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
  998:                 '</td><td>'.
  999:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1000:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1001:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1002:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1003:                 '</tr>';
 1004:             $itemcount ++;
 1005:         }
 1006:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1007:         $datatable .= '</tr></table></td></tr>';
 1008:     } elsif ($caller eq 'help') {
 1009:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1010:         my $switchserver = &check_switchserver($dom,$confname);
 1011:         my $itemcount = 1;
 1012:         $defaulturl = '/adm/loginproblems.html';
 1013:         $defaulttype = 'default';
 1014:         %lt = &Apache::lonlocal::texthash (
 1015:                      del     => 'Delete?',
 1016:                      rep     => 'Replace:',
 1017:                      upl     => 'Upload:',
 1018:                      default => 'Default',
 1019:                      custom  => 'Custom',
 1020:                                              );
 1021:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1022:         my @currlangs;
 1023:         if (ref($settings) eq 'HASH') {
 1024:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1025:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1026:                     next if ($settings->{'helpurl'}{$key} eq '');
 1027:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1028:                     $type{$key} = 'custom';
 1029:                     unless ($key eq 'nolang') {
 1030:                         push(@currlangs,$key);
 1031:                     }
 1032:                 }
 1033:             } elsif ($settings->{'helpurl'} ne '') {
 1034:                 $type{'nolang'} = 'custom';
 1035:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1036:             }
 1037:         }
 1038:         foreach my $lang ('nolang',sort(@currlangs)) {
 1039:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1040:             $datatable .= '<tr'.$css_class.'>';
 1041:             if ($url{$lang} eq '') {
 1042:                 $url{$lang} = $defaulturl;
 1043:             }
 1044:             if ($type{$lang} eq '') {
 1045:                 $type{$lang} = $defaulttype;
 1046:             }
 1047:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1048:             if ($lang eq 'nolang') {
 1049:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1050:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1051:             } else {
 1052:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1053:                                   $langchoices{$lang},
 1054:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1055:             }
 1056:             $datatable .= '</span></td>'."\n".
 1057:                           '<td class="LC_left_item">';
 1058:             if ($type{$lang} eq 'custom') {
 1059:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1060:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1061:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1062:             } else {
 1063:                 $datatable .= $lt{'upl'};
 1064:             }
 1065:             $datatable .='<br />';
 1066:             if ($switchserver) {
 1067:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1068:             } else {
 1069:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1070:             }
 1071:             $datatable .= '</td></tr>';
 1072:             $itemcount ++;
 1073:         }
 1074:         my @addlangs;
 1075:         foreach my $lang (sort(keys(%langchoices))) {
 1076:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1077:             push(@addlangs,$lang);
 1078:         }
 1079:         if (@addlangs > 0) {
 1080:             my %toadd;
 1081:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1082:             $toadd{''} = &mt('Select');
 1083:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1084:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1085:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1086:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1087:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1088:             if ($switchserver) {
 1089:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1090:             } else {
 1091:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1092:             }
 1093:             $datatable .= '</td></tr>';
 1094:             $itemcount ++;
 1095:         }
 1096:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1097:     }
 1098:     return $datatable;
 1099: }
 1100: 
 1101: sub login_choices {
 1102:     my %choices =
 1103:         &Apache::lonlocal::texthash (
 1104:             coursecatalog => 'Display Course/Community Catalog link?',
 1105:             adminmail     => "Display Administrator's E-mail Address?",
 1106:             helpdesk      => 'Display "Contact Helpdesk" link',
 1107:             disallowlogin => "Login page requests redirected",
 1108:             hostid        => "Server",
 1109:             server        => "Redirect to:",
 1110:             serverpath    => "Path",
 1111:             custompath    => "Custom", 
 1112:             exempt        => "Exempt IP(s)",
 1113:             directlogin   => "No redirect",
 1114:             newuser       => "Link to create a user account",
 1115:             img           => "Header",
 1116:             logo          => "Main Logo",
 1117:             domlogo       => "Domain Logo",
 1118:             login         => "Log-in Header", 
 1119:             textcol       => "Text color",
 1120:             bgcol         => "Box color",
 1121:             bgs           => "Background colors",
 1122:             links         => "Link colors",
 1123:             font          => "Font color",
 1124:             pgbg          => "Header",
 1125:             mainbg        => "Page",
 1126:             sidebg        => "Login box",
 1127:             link          => "Link",
 1128:             alink         => "Active link",
 1129:             vlink         => "Visited link",
 1130:         );
 1131:     return %choices;
 1132: }
 1133: 
 1134: sub print_rolecolors {
 1135:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1136:     my %choices = &color_font_choices();
 1137:     my @bgs = ('pgbg','tabbg','sidebg');
 1138:     my @links = ('link','alink','vlink');
 1139:     my @images = ('img');
 1140:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1141:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1142:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1143:     my (%is_custom,%designs);
 1144:     my %defaults = (
 1145:                    img => $defaultdesign{$role.'.img'},
 1146:                    font => $defaultdesign{$role.'.font'},
 1147: 		   fontmenu => $defaultdesign{$role.'.fontmenu'},
 1148:                    );
 1149:     foreach my $item (@bgs) {
 1150:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1151:     }
 1152:     foreach my $item (@links) {
 1153:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1154:     }
 1155:     if (ref($settings) eq 'HASH') {
 1156:         if (ref($settings->{$role}) eq 'HASH') {
 1157:             if ($settings->{$role}->{'img'} ne '') {
 1158:                 $designs{'img'} = $settings->{$role}->{'img'};
 1159:                 $is_custom{'img'} = 1;
 1160:             }
 1161:             if ($settings->{$role}->{'font'} ne '') {
 1162:                 $designs{'font'} = $settings->{$role}->{'font'};
 1163:                 $is_custom{'font'} = 1;
 1164:             }
 1165:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1166:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1167:                 $is_custom{'fontmenu'} = 1;
 1168:             }
 1169:             foreach my $item (@bgs) {
 1170:                 if ($settings->{$role}->{$item} ne '') {
 1171:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1172:                     $is_custom{$item} = 1;
 1173:                 }
 1174:             }
 1175:             foreach my $item (@links) {
 1176:                 if ($settings->{$role}->{$item} ne '') {
 1177:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1178:                     $is_custom{$item} = 1;
 1179:                 }
 1180:             }
 1181:         }
 1182:     } else {
 1183:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1184:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1185:             $is_custom{'img'} = 1;
 1186:         }
 1187:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1188:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1189:             $is_custom{'fontmenu'} = 1; 
 1190:         }
 1191:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1192:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1193:             $is_custom{'font'} = 1;
 1194:         }
 1195:         foreach my $item (@bgs) {
 1196:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1197:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1198:                 $is_custom{$item} = 1;
 1199:             
 1200:             }
 1201:         }
 1202:         foreach my $item (@links) {
 1203:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1204:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1205:                 $is_custom{$item} = 1;
 1206:             }
 1207:         }
 1208:     }
 1209:     my $itemcount = 1;
 1210:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1211:     $datatable .= '</tr></table></td></tr>';
 1212:     return $datatable;
 1213: }
 1214: 
 1215: sub display_color_options {
 1216:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1217:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1218:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1219:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1220:     my $datatable = '<tr'.$css_class.'>'.
 1221:         '<td>'.$choices->{'font'}.'</td>';
 1222:     if (!$is_custom->{'font'}) {
 1223:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1224:     } else {
 1225:         $datatable .= '<td>&nbsp;</td>';
 1226:     }
 1227:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1228: 
 1229:     $datatable .= '<td><span class="LC_nobreak">'.
 1230:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1231:                   ' value="'.$current_color.'" />&nbsp;'. 
 1232:                   '&nbsp;</td></tr>';
 1233:     unless ($role eq 'login') { 
 1234:         $datatable .= '<tr'.$css_class.'>'.
 1235:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1236:         if (!$is_custom->{'fontmenu'}) {
 1237:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1238:         } else {
 1239:             $datatable .= '<td>&nbsp;</td>';
 1240:         }
 1241: 	$current_color = $designs->{'fontmenu'} ? 
 1242: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1243:         $datatable .= '<td><span class="LC_nobreak">'.
 1244:                       '<input class="colorchooser" type="text" size="10" name="'
 1245: 		      .$role.'_fontmenu"'.
 1246:                       ' value="'.$current_color.'" />&nbsp;'.
 1247:                       '&nbsp;</td></tr>';
 1248:     }
 1249:     my $switchserver = &check_switchserver($dom,$confname);
 1250:     foreach my $img (@{$images}) {
 1251: 	$itemcount ++;
 1252:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1253:         $datatable .= '<tr'.$css_class.'>'.
 1254:                       '<td>'.$choices->{$img};
 1255:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1256:         if ($role eq 'login') {
 1257:             if ($img eq 'login') {
 1258:                 $login_hdr_pick =
 1259:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1260:                 $logincolors =
 1261:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1262:                                             $designs);
 1263:             } elsif ($img ne 'domlogo') {
 1264:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1265:             }
 1266:         }
 1267:         $datatable .= '</td>';
 1268:         if ($designs->{$img} ne '') {
 1269:             $imgfile = $designs->{$img};
 1270: 	    $img_import = ($imgfile =~ m{^/adm/});
 1271:         } else {
 1272:             $imgfile = $defaults->{$img};
 1273:         }
 1274:         if ($imgfile) {
 1275:             my ($showfile,$fullsize);
 1276:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1277:                 my $urldir = $1;
 1278:                 my $filename = $2;
 1279:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1280:                 if (@info) {
 1281:                     my $thumbfile = 'tn-'.$filename;
 1282:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1283:                     if (@thumb) {
 1284:                         $showfile = $urldir.'/'.$thumbfile;
 1285:                     } else {
 1286:                         $showfile = $imgfile;
 1287:                     }
 1288:                 } else {
 1289:                     $showfile = '';
 1290:                 }
 1291:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1292:                 $showfile = $imgfile;
 1293:                 my $imgdir = $1;
 1294:                 my $filename = $2;
 1295:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1296:                     $showfile = "/$imgdir/tn-".$filename;
 1297:                 } else {
 1298:                     my $input = $londocroot.$imgfile;
 1299:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1300:                     if (!-e $output) {
 1301:                         my ($width,$height) = &thumb_dimensions();
 1302:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1303:                         if ($fullwidth ne '' && $fullheight ne '') {
 1304:                             if ($fullwidth > $width && $fullheight > $height) { 
 1305:                                 my $size = $width.'x'.$height;
 1306:                                 system("convert -sample $size $input $output");
 1307:                                 $showfile = "/$imgdir/tn-".$filename;
 1308:                             }
 1309:                         }
 1310:                     }
 1311:                 }
 1312:             }
 1313:             if ($showfile) {
 1314:                 if ($showfile =~ m{^/(adm|res)/}) {
 1315:                     if ($showfile =~ m{^/res/}) {
 1316:                         my $local_showfile =
 1317:                             &Apache::lonnet::filelocation('',$showfile);
 1318:                         &Apache::lonnet::repcopy($local_showfile);
 1319:                     }
 1320:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1321:                 }
 1322:                 if ($imgfile) {
 1323:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1324:                         if ($imgfile =~ m{^/res/}) {
 1325:                             my $local_imgfile =
 1326:                                 &Apache::lonnet::filelocation('',$imgfile);
 1327:                             &Apache::lonnet::repcopy($local_imgfile);
 1328:                         }
 1329:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1330:                     } else {
 1331:                         $fullsize = $imgfile;
 1332:                     }
 1333:                 }
 1334:                 $datatable .= '<td>';
 1335:                 if ($img eq 'login') {
 1336:                     $datatable .= $login_hdr_pick;
 1337:                 } 
 1338:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1339:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1340:             } else {
 1341:                 $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1342:                               &mt('Upload:');
 1343:             }
 1344:         } else {
 1345:             $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1346:                           &mt('Upload:');
 1347:         }
 1348:         if ($switchserver) {
 1349:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1350:         } else {
 1351:             if ($img ne 'login') { # suppress file selection for Log-in header
 1352:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1353:             }
 1354:         }
 1355:         $datatable .= '</td></tr>';
 1356:     }
 1357:     $itemcount ++;
 1358:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1359:     $datatable .= '<tr'.$css_class.'>'.
 1360:                   '<td>'.$choices->{'bgs'}.'</td>';
 1361:     my $bgs_def;
 1362:     foreach my $item (@{$bgs}) {
 1363:         if (!$is_custom->{$item}) {
 1364:             $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>';
 1365:         }
 1366:     }
 1367:     if ($bgs_def) {
 1368:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1369:     } else {
 1370:         $datatable .= '<td>&nbsp;</td>';
 1371:     }
 1372:     $datatable .= '<td class="LC_right_item">'.
 1373:                   '<table border="0"><tr>';
 1374: 
 1375:     foreach my $item (@{$bgs}) {
 1376:         $datatable .= '<td align="center">';
 1377: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1378:         if ($designs->{'bgs'}{$item}) {
 1379:             $datatable .= '&nbsp;';
 1380:         }
 1381:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1382:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1383:     }
 1384:     $datatable .= '</tr></table></td></tr>';
 1385:     $itemcount ++;
 1386:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1387:     $datatable .= '<tr'.$css_class.'>'.
 1388:                   '<td>'.$choices->{'links'}.'</td>';
 1389:     my $links_def;
 1390:     foreach my $item (@{$links}) {
 1391:         if (!$is_custom->{$item}) {
 1392:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1393:         }
 1394:     }
 1395:     if ($links_def) {
 1396:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1397:     } else {
 1398:         $datatable .= '<td>&nbsp;</td>';
 1399:     }
 1400:     $datatable .= '<td class="LC_right_item">'.
 1401:                   '<table border="0"><tr>';
 1402:     foreach my $item (@{$links}) {
 1403: 	my $color = $designs->{'link'}{$item} ? $designs->{'link'}{$item} : $defaults->{'links'}{$item};
 1404:         $datatable .= '<td align="center">'."\n";
 1405: 
 1406:         if ($designs->{'links'}{$item}) {
 1407:             $datatable.='&nbsp;';
 1408:         }
 1409:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1410:                       '" /></td>';
 1411:     }
 1412:     $$rowtotal += $itemcount;
 1413:     return $datatable;
 1414: }
 1415: 
 1416: sub logo_display_options {
 1417:     my ($img,$defaults,$designs) = @_;
 1418:     my $checkedon;
 1419:     if (ref($defaults) eq 'HASH') {
 1420:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1421:             if ($defaults->{'showlogo'}{$img}) {
 1422:                 $checkedon = 'checked="checked" ';     
 1423:             }
 1424:         } 
 1425:     }
 1426:     if (ref($designs) eq 'HASH') {
 1427:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1428:             if (defined($designs->{'showlogo'}{$img})) {
 1429:                 if ($designs->{'showlogo'}{$img} == 0) {
 1430:                     $checkedon = '';
 1431:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1432:                     $checkedon = 'checked="checked" ';
 1433:                 }
 1434:             }
 1435:         }
 1436:     }
 1437:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1438:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1439:            &mt('show').'</label>'."\n";
 1440: }
 1441: 
 1442: sub login_header_options  {
 1443:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1444:     my $output = '';
 1445:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1446:         $output .= &mt('Text default(s):').'<br />';
 1447:         if (!$is_custom->{'textcol'}) {
 1448:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1449:                        '&nbsp;&nbsp;&nbsp;';
 1450:         }
 1451:         if (!$is_custom->{'bgcol'}) {
 1452:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1453:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1454:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1455:         }
 1456:         $output .= '<br />';
 1457:     }
 1458:     $output .='<br />';
 1459:     return $output;
 1460: }
 1461: 
 1462: sub login_text_colors {
 1463:     my ($img,$role,$logintext,$phase,$choices,$designs) = @_;
 1464:     my $color_menu = '<table border="0"><tr>';
 1465:     foreach my $item (@{$logintext}) {
 1466:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'logintext'}{$item});
 1467:         $color_menu .= '<td align="center">'.$link;
 1468:         if ($designs->{'logintext'}{$item}) {
 1469:             $color_menu .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'logintext'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1470:         }
 1471:         $color_menu .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.
 1472:                        $designs->{'logintext'}{$item}.'" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>'.
 1473:                        '<td>&nbsp;</td>';
 1474:     }
 1475:     $color_menu .= '</tr></table><br />';
 1476:     return $color_menu;
 1477: }
 1478: 
 1479: sub image_changes {
 1480:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1481:     my $output;
 1482:     if ($img eq 'login') {
 1483:             # suppress image for Log-in header
 1484:     } elsif (!$is_custom) {
 1485:         if ($img ne 'domlogo') {
 1486:             $output .= &mt('Default image:').'<br />';
 1487:         } else {
 1488:             $output .= &mt('Default in use:').'<br />';
 1489:         }
 1490:     }
 1491:     if ($img eq 'login') { # suppress image for Log-in header
 1492:         $output .= '<td>'.$logincolors;
 1493:     } else {
 1494:         if ($img_import) {
 1495:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1496:         }
 1497:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1498:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1499:         if ($is_custom) {
 1500:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1501:                        '<input type="checkbox" name="'.
 1502:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1503:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1504:         } else {
 1505:             $output .= '<td valign="bottom">'.$logincolors.&mt('Upload:').'<br />';
 1506:         }
 1507:     }
 1508:     return $output;
 1509: }
 1510: 
 1511: sub color_pick {
 1512:     my ($phase,$role,$item,$desc,$curcol) = @_;
 1513:     my $link = '<a href="javascript:pjump('."'color_custom','".$desc.
 1514:                "','".$curcol."','".$role.'_'.$item."','parmform.pres','psub'".
 1515:                ');">'.$desc.'</a>';
 1516:     return $link;
 1517: }
 1518: 
 1519: sub print_quotas {
 1520:     my ($dom,$settings,$rowtotal,$action) = @_;
 1521:     my $context;
 1522:     if ($action eq 'quotas') {
 1523:         $context = 'tools';
 1524:     } else {
 1525:         $context = $action;
 1526:     }
 1527:     my ($datatable,$defaultquota,@usertools,@options,%validations);
 1528:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1529:     my $typecount = 0;
 1530:     my ($css_class,%titles);
 1531:     if ($context eq 'requestcourses') {
 1532:         @usertools = ('official','unofficial','community');
 1533:         @options =('norequest','approval','validate','autolimit');
 1534:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1535:         %titles = &courserequest_titles();
 1536:     } elsif ($context eq 'requestauthor') {
 1537:         @usertools = ('author');
 1538:         @options = ('norequest','approval','automatic');
 1539:         %titles = &authorrequest_titles(); 
 1540:     } else {
 1541:         @usertools = ('aboutme','blog','webdav','portfolio');
 1542:         %titles = &tool_titles();
 1543:     }
 1544:     if (ref($types) eq 'ARRAY') {
 1545:         foreach my $type (@{$types}) {
 1546:             my $currdefquota;
 1547:             unless (($context eq 'requestcourses') ||
 1548:                     ($context eq 'requestauthor')) {
 1549:                 if (ref($settings) eq 'HASH') {
 1550:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1551:                         $currdefquota = $settings->{defaultquota}->{$type}; 
 1552:                     } else {
 1553:                         $currdefquota = $settings->{$type};
 1554:                     }
 1555:                 }
 1556:             }
 1557:             if (defined($usertypes->{$type})) {
 1558:                 $typecount ++;
 1559:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1560:                 $datatable .= '<tr'.$css_class.'>'.
 1561:                               '<td>'.$usertypes->{$type}.'</td>'.
 1562:                               '<td class="LC_left_item">';
 1563:                 if ($context eq 'requestcourses') {
 1564:                     $datatable .= '<table><tr>';
 1565:                 }
 1566:                 my %cell;  
 1567:                 foreach my $item (@usertools) {
 1568:                     if ($context eq 'requestcourses') {
 1569:                         my ($curroption,$currlimit);
 1570:                         if (ref($settings) eq 'HASH') {
 1571:                             if (ref($settings->{$item}) eq 'HASH') {
 1572:                                 $curroption = $settings->{$item}->{$type};
 1573:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1574:                                     $currlimit = $1; 
 1575:                                 }
 1576:                             }
 1577:                         }
 1578:                         if (!$curroption) {
 1579:                             $curroption = 'norequest';
 1580:                         }
 1581:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1582:                         foreach my $option (@options) {
 1583:                             my $val = $option;
 1584:                             if ($option eq 'norequest') {
 1585:                                 $val = 0;  
 1586:                             }
 1587:                             if ($option eq 'validate') {
 1588:                                 my $canvalidate = 0;
 1589:                                 if (ref($validations{$item}) eq 'HASH') { 
 1590:                                     if ($validations{$item}{$type}) {
 1591:                                         $canvalidate = 1;
 1592:                                     }
 1593:                                 }
 1594:                                 next if (!$canvalidate);
 1595:                             }
 1596:                             my $checked = '';
 1597:                             if ($option eq $curroption) {
 1598:                                 $checked = ' checked="checked"';
 1599:                             } elsif ($option eq 'autolimit') {
 1600:                                 if ($curroption =~ /^autolimit/) {
 1601:                                     $checked = ' checked="checked"';
 1602:                                 }                       
 1603:                             } 
 1604:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1605:                                   '<input type="radio" name="crsreq_'.$item.
 1606:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1607:                                   $titles{$option}.'</label>';
 1608:                             if ($option eq 'autolimit') {
 1609:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1610:                                                 $item.'_limit_'.$type.'" size="1" '.
 1611:                                                 'value="'.$currlimit.'" />';
 1612:                             }
 1613:                             $cell{$item} .= '</span> ';
 1614:                             if ($option eq 'autolimit') {
 1615:                                 $cell{$item} .= $titles{'unlimited'};
 1616:                             }
 1617:                         }
 1618:                     } elsif ($context eq 'requestauthor') {
 1619:                         my $curroption;
 1620:                         if (ref($settings) eq 'HASH') {
 1621:                             $curroption = $settings->{$type};
 1622:                         }
 1623:                         if (!$curroption) {
 1624:                             $curroption = 'norequest';
 1625:                         }
 1626:                         foreach my $option (@options) {
 1627:                             my $val = $option;
 1628:                             if ($option eq 'norequest') {
 1629:                                 $val = 0;
 1630:                             }
 1631:                             my $checked = '';
 1632:                             if ($option eq $curroption) {
 1633:                                 $checked = ' checked="checked"';
 1634:                             }
 1635:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1636:                                   '<input type="radio" name="authorreq_'.$type.
 1637:                                   '" value="'.$val.'"'.$checked.' />'.
 1638:                                   $titles{$option}.'</label></span>&nbsp; ';
 1639:                         }
 1640:                     } else {
 1641:                         my $checked = 'checked="checked" ';
 1642:                         if (ref($settings) eq 'HASH') {
 1643:                             if (ref($settings->{$item}) eq 'HASH') {
 1644:                                 if ($settings->{$item}->{$type} == 0) {
 1645:                                     $checked = '';
 1646:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1647:                                     $checked =  'checked="checked" ';
 1648:                                 }
 1649:                             }
 1650:                         }
 1651:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1652:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1653:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1654:                                       '</label></span>&nbsp; ';
 1655:                     }
 1656:                 }
 1657:                 if ($context eq 'requestcourses') {
 1658:                     $datatable .= '</tr><tr>';
 1659:                     foreach my $item (@usertools) {
 1660:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1661:                     }
 1662:                     $datatable .= '</tr></table>';
 1663:                 }
 1664:                 $datatable .= '</td>';
 1665:                 unless (($context eq 'requestcourses') ||
 1666:                         ($context eq 'requestauthor')) {
 1667:                     $datatable .= 
 1668:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 1669:                               '<input type="text" name="quota_'.$type.
 1670:                               '" value="'.$currdefquota.
 1671:                               '" size="5" /> Mb</span></td>';
 1672:                 }
 1673:                 $datatable .= '</tr>';
 1674:             }
 1675:         }
 1676:     }
 1677:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1678:         $defaultquota = '20';
 1679:         if (ref($settings) eq 'HASH') {
 1680:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1681:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1682:             } elsif (defined($settings->{'default'})) {
 1683:                 $defaultquota = $settings->{'default'};
 1684:             }
 1685:         }
 1686:     }
 1687:     $typecount ++;
 1688:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1689:     $datatable .= '<tr'.$css_class.'>'.
 1690:                   '<td>'.$othertitle.'</td>'.
 1691:                   '<td class="LC_left_item">';
 1692:     if ($context eq 'requestcourses') {
 1693:         $datatable .= '<table><tr>';
 1694:     }
 1695:     my %defcell;
 1696:     foreach my $item (@usertools) {
 1697:         if ($context eq 'requestcourses') {
 1698:             my ($curroption,$currlimit);
 1699:             if (ref($settings) eq 'HASH') {
 1700:                 if (ref($settings->{$item}) eq 'HASH') {
 1701:                     $curroption = $settings->{$item}->{'default'};
 1702:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1703:                         $currlimit = $1;
 1704:                     }
 1705:                 }
 1706:             }
 1707:             if (!$curroption) {
 1708:                 $curroption = 'norequest';
 1709:             }
 1710:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1711:             foreach my $option (@options) {
 1712:                 my $val = $option;
 1713:                 if ($option eq 'norequest') {
 1714:                     $val = 0;
 1715:                 }
 1716:                 if ($option eq 'validate') {
 1717:                     my $canvalidate = 0;
 1718:                     if (ref($validations{$item}) eq 'HASH') {
 1719:                         if ($validations{$item}{'default'}) {
 1720:                             $canvalidate = 1;
 1721:                         }
 1722:                     }
 1723:                     next if (!$canvalidate);
 1724:                 }
 1725:                 my $checked = '';
 1726:                 if ($option eq $curroption) {
 1727:                     $checked = ' checked="checked"';
 1728:                 } elsif ($option eq 'autolimit') {
 1729:                     if ($curroption =~ /^autolimit/) {
 1730:                         $checked = ' checked="checked"';
 1731:                     }
 1732:                 }
 1733:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1734:                                   '<input type="radio" name="crsreq_'.$item.
 1735:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1736:                                   $titles{$option}.'</label>';
 1737:                 if ($option eq 'autolimit') {
 1738:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1739:                                        $item.'_limit_default" size="1" '.
 1740:                                        'value="'.$currlimit.'" />';
 1741:                 }
 1742:                 $defcell{$item} .= '</span> ';
 1743:                 if ($option eq 'autolimit') {
 1744:                     $defcell{$item} .= $titles{'unlimited'};
 1745:                 }
 1746:             }
 1747:         } elsif ($context eq 'requestauthor') {
 1748:             my $curroption;
 1749:             if (ref($settings) eq 'HASH') {
 1750:                 $curroption = $settings->{'default'};
 1751:             }
 1752:             if (!$curroption) {
 1753:                 $curroption = 'norequest';
 1754:             }
 1755:             foreach my $option (@options) {
 1756:                 my $val = $option;
 1757:                 if ($option eq 'norequest') {
 1758:                     $val = 0;
 1759:                 }
 1760:                 my $checked = '';
 1761:                 if ($option eq $curroption) {
 1762:                     $checked = ' checked="checked"';
 1763:                 }
 1764:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1765:                               '<input type="radio" name="authorreq_default"'.
 1766:                               ' value="'.$val.'"'.$checked.' />'.
 1767:                               $titles{$option}.'</label></span>&nbsp; ';
 1768:             }
 1769:         } else {
 1770:             my $checked = 'checked="checked" ';
 1771:             if (ref($settings) eq 'HASH') {
 1772:                 if (ref($settings->{$item}) eq 'HASH') {
 1773:                     if ($settings->{$item}->{'default'} == 0) {
 1774:                         $checked = '';
 1775:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1776:                         $checked = 'checked="checked" ';
 1777:                     }
 1778:                 }
 1779:             }
 1780:             $datatable .= '<span class="LC_nobreak"><label>'.
 1781:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1782:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1783:                           '</label></span>&nbsp; ';
 1784:         }
 1785:     }
 1786:     if ($context eq 'requestcourses') {
 1787:         $datatable .= '</tr><tr>';
 1788:         foreach my $item (@usertools) {
 1789:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1790:         }
 1791:         $datatable .= '</tr></table>';
 1792:     }
 1793:     $datatable .= '</td>';
 1794:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1795:         $datatable .= '<td class="LC_right_item"><span class="LC_nobreak">'.
 1796:                       '<input type="text" name="defaultquota" value="'.
 1797:                       $defaultquota.'" size="5" /> Mb</span></td>';
 1798:     }
 1799:     $datatable .= '</tr>';
 1800:     $typecount ++;
 1801:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1802:     $datatable .= '<tr'.$css_class.'>'.
 1803:                   '<td>'.&mt('LON-CAPA Advanced Users').' ';
 1804:     if ($context eq 'requestcourses') {
 1805:         $datatable .= &mt('(overrides affiliation, if set)').
 1806:                       '</td>'.
 1807:                       '<td class="LC_left_item">'.
 1808:                       '<table><tr>';
 1809:     } else {
 1810:         $datatable .= &mt('(overrides affiliation, if checked)').
 1811:                       '</td>'.
 1812:                       '<td class="LC_left_item" colspan="2">'.
 1813:                       '<br />';
 1814:     }
 1815:     my %advcell;
 1816:     foreach my $item (@usertools) {
 1817:         if ($context eq 'requestcourses') {
 1818:             my ($curroption,$currlimit);
 1819:             if (ref($settings) eq 'HASH') {
 1820:                 if (ref($settings->{$item}) eq 'HASH') {
 1821:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1822:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1823:                         $currlimit = $1;
 1824:                     }
 1825:                 }
 1826:             }
 1827:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1828:             my $checked = '';
 1829:             if ($curroption eq '') {
 1830:                 $checked = ' checked="checked"';
 1831:             }
 1832:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1833:                                '<input type="radio" name="crsreq_'.$item.
 1834:                                '__LC_adv" value=""'.$checked.' />'.
 1835:                                &mt('No override set').'</label></span>&nbsp; ';
 1836:             foreach my $option (@options) {
 1837:                 my $val = $option;
 1838:                 if ($option eq 'norequest') {
 1839:                     $val = 0;
 1840:                 }
 1841:                 if ($option eq 'validate') {
 1842:                     my $canvalidate = 0;
 1843:                     if (ref($validations{$item}) eq 'HASH') {
 1844:                         if ($validations{$item}{'_LC_adv'}) {
 1845:                             $canvalidate = 1;
 1846:                         }
 1847:                     }
 1848:                     next if (!$canvalidate);
 1849:                 }
 1850:                 my $checked = '';
 1851:                 if ($val eq $curroption) {
 1852:                     $checked = ' checked="checked"';
 1853:                 } elsif ($option eq 'autolimit') {
 1854:                     if ($curroption =~ /^autolimit/) {
 1855:                         $checked = ' checked="checked"';
 1856:                     }
 1857:                 }
 1858:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1859:                                   '<input type="radio" name="crsreq_'.$item.
 1860:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1861:                                   $titles{$option}.'</label>';
 1862:                 if ($option eq 'autolimit') {
 1863:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1864:                                        $item.'_limit__LC_adv" size="1" '.
 1865:                                        'value="'.$currlimit.'" />';
 1866:                 }
 1867:                 $advcell{$item} .= '</span> ';
 1868:                 if ($option eq 'autolimit') {
 1869:                     $advcell{$item} .= $titles{'unlimited'};
 1870:                 }
 1871:             }
 1872:         } elsif ($context eq 'requestauthor') {
 1873:             my $curroption;
 1874:             if (ref($settings) eq 'HASH') {
 1875:                 $curroption = $settings->{'_LC_adv'};
 1876:             }
 1877:             my $checked = '';
 1878:             if ($curroption eq '') {
 1879:                 $checked = ' checked="checked"';
 1880:             }
 1881:             $datatable .= '<span class="LC_nobreak"><label>'.
 1882:                           '<input type="radio" name="authorreq__LC_adv"'.
 1883:                           ' value=""'.$checked.' />'.
 1884:                           &mt('No override set').'</label></span>&nbsp; ';
 1885:             foreach my $option (@options) {
 1886:                 my $val = $option;
 1887:                 if ($option eq 'norequest') {
 1888:                     $val = 0;
 1889:                 }
 1890:                 my $checked = '';
 1891:                 if ($val eq $curroption) {
 1892:                     $checked = ' checked="checked"';
 1893:                 }
 1894:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1895:                               '<input type="radio" name="authorreq__LC_adv"'.
 1896:                               ' value="'.$val.'"'.$checked.' />'.
 1897:                               $titles{$option}.'</label></span>&nbsp; ';
 1898:             }
 1899:         } else {
 1900:             my $checked = 'checked="checked" ';
 1901:             if (ref($settings) eq 'HASH') {
 1902:                 if (ref($settings->{$item}) eq 'HASH') {
 1903:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1904:                         $checked = '';
 1905:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1906:                         $checked = 'checked="checked" ';
 1907:                     }
 1908:                 }
 1909:             }
 1910:             $datatable .= '<span class="LC_nobreak"><label>'.
 1911:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1912:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1913:                           '</label></span>&nbsp; ';
 1914:         }
 1915:     }
 1916:     if ($context eq 'requestcourses') {
 1917:         $datatable .= '</tr><tr>';
 1918:         foreach my $item (@usertools) {
 1919:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1920:         }
 1921:         $datatable .= '</tr></table>';
 1922:     }
 1923:     $datatable .= '</td></tr>';
 1924:     $$rowtotal += $typecount;
 1925:     return $datatable;
 1926: }
 1927: 
 1928: sub print_requestmail {
 1929:     my ($dom,$action,$settings,$rowtotal) = @_;
 1930:     my ($now,$datatable,%currapp,$rows);
 1931:     $now = time;
 1932:     if (ref($settings) eq 'HASH') {
 1933:         if (ref($settings->{'notify'}) eq 'HASH') {
 1934:             if ($settings->{'notify'}{'approval'} ne '') {
 1935:                map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 1936:             }
 1937:         }
 1938:     }
 1939:     my $numinrow = 2;
 1940:     my $css_class = 'class="LC_odd_row"';
 1941:     my $text;
 1942:     if ($action eq 'requestcourses') {
 1943:         $text = &mt('Receive notification of course requests requiring approval');
 1944:     } else {
 1945:         $text = &mt('Receive notification of authoring space requests requiring approval')
 1946:     }
 1947:     $datatable = '<tr '.$css_class.'>'.
 1948:                  ' <td>'.$text.'</td>'.
 1949:                  ' <td class="LC_left_item">';
 1950:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 1951:                                                  'reqapprovalnotify',%currapp);
 1952:     if ($numdc > 0) {
 1953:         $datatable .= $table;
 1954:     } else {
 1955:         $datatable .= &mt('There are no active Domain Coordinators');
 1956:     }
 1957:     $datatable .='</td></tr>';
 1958:     $$rowtotal += $rows;
 1959:     return $datatable;
 1960: }
 1961: 
 1962: sub print_autoenroll {
 1963:     my ($dom,$settings,$rowtotal) = @_;
 1964:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 1965:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 1966:     if (ref($settings) eq 'HASH') {
 1967:         if (exists($settings->{'run'})) {
 1968:             if ($settings->{'run'} eq '0') {
 1969:                 $runoff = ' checked="checked" ';
 1970:                 $runon = ' ';
 1971:             } else {
 1972:                 $runon = ' checked="checked" ';
 1973:                 $runoff = ' ';
 1974:             }
 1975:         } else {
 1976:             if ($autorun) {
 1977:                 $runon = ' checked="checked" ';
 1978:                 $runoff = ' ';
 1979:             } else {
 1980:                 $runoff = ' checked="checked" ';
 1981:                 $runon = ' ';
 1982:             }
 1983:         }
 1984:         if (exists($settings->{'co-owners'})) {
 1985:             if ($settings->{'co-owners'} eq '0') {
 1986:                 $coownersoff = ' checked="checked" ';
 1987:                 $coownerson = ' ';
 1988:             } else {
 1989:                 $coownerson = ' checked="checked" ';
 1990:                 $coownersoff = ' ';
 1991:             }
 1992:         } else {
 1993:             $coownersoff = ' checked="checked" ';
 1994:             $coownerson = ' ';
 1995:         }
 1996:         if (exists($settings->{'sender_domain'})) {
 1997:             $defdom = $settings->{'sender_domain'};
 1998:         }
 1999:     } else {
 2000:         if ($autorun) {
 2001:             $runon = ' checked="checked" ';
 2002:             $runoff = ' ';
 2003:         } else {
 2004:             $runoff = ' checked="checked" ';
 2005:             $runon = ' ';
 2006:         }
 2007:     }
 2008:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2009:     my $notif_sender;
 2010:     if (ref($settings) eq 'HASH') {
 2011:         $notif_sender = $settings->{'sender_uname'};
 2012:     }
 2013:     my $datatable='<tr class="LC_odd_row">'.
 2014:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2015:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2016:                   '<input type="radio" name="autoenroll_run"'.
 2017:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2018:                   '<label><input type="radio" name="autoenroll_run"'.
 2019:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2020:                   '</tr><tr>'.
 2021:                   '<td>'.&mt('Notification messages - sender').
 2022:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2023:                   &mt('username').':&nbsp;'.
 2024:                   '<input type="text" name="sender_uname" value="'.
 2025:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2026:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2027:                   '<tr class="LC_odd_row">'.
 2028:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2029:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2030:                   '<input type="radio" name="autoassign_coowners"'.
 2031:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2032:                   '<label><input type="radio" name="autoassign_coowners"'.
 2033:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2034:                   '</tr>';
 2035:     $$rowtotal += 3;
 2036:     return $datatable;
 2037: }
 2038: 
 2039: sub print_autoupdate {
 2040:     my ($position,$dom,$settings,$rowtotal) = @_;
 2041:     my $datatable;
 2042:     if ($position eq 'top') {
 2043:         my $updateon = ' ';
 2044:         my $updateoff = ' checked="checked" ';
 2045:         my $classlistson = ' ';
 2046:         my $classlistsoff = ' checked="checked" ';
 2047:         if (ref($settings) eq 'HASH') {
 2048:             if ($settings->{'run'} eq '1') {
 2049:                 $updateon = $updateoff;
 2050:                 $updateoff = ' ';
 2051:             }
 2052:             if ($settings->{'classlists'} eq '1') {
 2053:                 $classlistson = $classlistsoff;
 2054:                 $classlistsoff = ' ';
 2055:             }
 2056:         }
 2057:         my %title = (
 2058:                    run => 'Auto-update active?',
 2059:                    classlists => 'Update information in classlists?',
 2060:                     );
 2061:         $datatable = '<tr class="LC_odd_row">'. 
 2062:                   '<td>'.&mt($title{'run'}).'</td>'.
 2063:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2064:                   '<input type="radio" name="autoupdate_run"'.
 2065:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2066:                   '<label><input type="radio" name="autoupdate_run"'.
 2067:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2068:                   '</tr><tr>'.
 2069:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2070:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2071:                   '<label><input type="radio" name="classlists"'.
 2072:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2073:                   '<label><input type="radio" name="classlists"'.
 2074:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2075:                   '</tr>';
 2076:         $$rowtotal += 2;
 2077:     } elsif ($position eq 'middle') {
 2078:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2079:         my $numinrow = 3;
 2080:         my $locknamesettings;
 2081:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2082:                                      $dom,$numinrow,$othertitle,
 2083:                                     'lockablenames');
 2084:         $$rowtotal ++;
 2085:     } else {
 2086:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2087:         my @fields = ('lastname','firstname','middlename','generation',
 2088:                       'permanentemail','id');
 2089:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2090:         my $numrows = 0;
 2091:         if (ref($types) eq 'ARRAY') {
 2092:             if (@{$types} > 0) {
 2093:                 $datatable = 
 2094:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2095:                                          \@fields,$types,\$numrows);
 2096:                     $$rowtotal += @{$types}; 
 2097:             }
 2098:         }
 2099:         $datatable .= 
 2100:             &usertype_update_row($settings,{'default' => $othertitle},
 2101:                                  \%fieldtitles,\@fields,['default'],
 2102:                                  \$numrows);
 2103:         $$rowtotal ++;     
 2104:     }
 2105:     return $datatable;
 2106: }
 2107: 
 2108: sub print_autocreate {
 2109:     my ($dom,$settings,$rowtotal) = @_;
 2110:     my (%createon,%createoff,%currhash);
 2111:     my @types = ('xml','req');
 2112:     if (ref($settings) eq 'HASH') {
 2113:         foreach my $item (@types) {
 2114:             $createoff{$item} = ' checked="checked" ';
 2115:             $createon{$item} = ' ';
 2116:             if (exists($settings->{$item})) {
 2117:                 if ($settings->{$item}) {
 2118:                     $createon{$item} = ' checked="checked" ';
 2119:                     $createoff{$item} = ' ';
 2120:                 }
 2121:             }
 2122:         }
 2123:         if ($settings->{'xmldc'} ne '') { 
 2124:             $currhash{$settings->{'xmldc'}} = 1;
 2125:         }
 2126:     } else {
 2127:         foreach my $item (@types) {
 2128:             $createoff{$item} = ' checked="checked" ';
 2129:             $createon{$item} = ' ';
 2130:         }
 2131:     }
 2132:     $$rowtotal += 2;
 2133:     my $numinrow = 2;
 2134:     my $datatable='<tr class="LC_odd_row">'.
 2135:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2136:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2137:                   '<input type="radio" name="autocreate_xml"'.
 2138:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2139:                   '<label><input type="radio" name="autocreate_xml"'.
 2140:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2141:                   '</td></tr><tr>'.
 2142:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2143:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2144:                   '<input type="radio" name="autocreate_req"'.
 2145:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2146:                   '<label><input type="radio" name="autocreate_req"'.
 2147:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2148:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2149:                                                    'autocreate_xmldc',%currhash);
 2150:     if ($numdc > 1) {
 2151:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2152:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2153:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2154:     } else {
 2155:         $datatable .= $dctable.'</td></tr>';
 2156:     }
 2157:     $$rowtotal += $rows;
 2158:     return $datatable;
 2159: }
 2160: 
 2161: sub print_directorysrch {
 2162:     my ($dom,$settings,$rowtotal) = @_;
 2163:     my $srchon = ' ';
 2164:     my $srchoff = ' checked="checked" ';
 2165:     my ($exacton,$containson,$beginson);
 2166:     my $localon = ' ';
 2167:     my $localoff = ' checked="checked" ';
 2168:     if (ref($settings) eq 'HASH') {
 2169:         if ($settings->{'available'} eq '1') {
 2170:             $srchon = $srchoff;
 2171:             $srchoff = ' ';
 2172:         }
 2173:         if ($settings->{'localonly'} eq '1') {
 2174:             $localon = $localoff;
 2175:             $localoff = ' ';
 2176:         }
 2177:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2178:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2179:                 if ($type eq 'exact') {
 2180:                     $exacton = ' checked="checked" ';
 2181:                 } elsif ($type eq 'contains') {
 2182:                     $containson = ' checked="checked" ';
 2183:                 } elsif ($type eq 'begins') {
 2184:                     $beginson = ' checked="checked" ';
 2185:                 }
 2186:             }
 2187:         } else {
 2188:             if ($settings->{'searchtypes'} eq 'exact') {
 2189:                 $exacton = ' checked="checked" ';
 2190:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2191:                 $containson = ' checked="checked" ';
 2192:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2193:                 $exacton = ' checked="checked" ';
 2194:                 $containson = ' checked="checked" ';
 2195:             }
 2196:         }
 2197:     }
 2198:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2199:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2200: 
 2201:     my $numinrow = 4;
 2202:     my $cansrchrow = 0;
 2203:     my $datatable='<tr class="LC_odd_row">'.
 2204:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2205:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2206:                   '<input type="radio" name="dirsrch_available"'.
 2207:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2208:                   '<label><input type="radio" name="dirsrch_available"'.
 2209:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2210:                   '</tr><tr>'.
 2211:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2212:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2213:                   '<input type="radio" name="dirsrch_localonly"'.
 2214:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2215:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2216:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2217:                   '</tr>';
 2218:     $$rowtotal += 2;
 2219:     if (ref($usertypes) eq 'HASH') {
 2220:         if (keys(%{$usertypes}) > 0) {
 2221:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2222:                                          $numinrow,$othertitle,'cansearch');
 2223:             $cansrchrow = 1;
 2224:         }
 2225:     }
 2226:     if ($cansrchrow) {
 2227:         $$rowtotal ++;
 2228:         $datatable .= '<tr>';
 2229:     } else {
 2230:         $datatable .= '<tr class="LC_odd_row">';
 2231:     }
 2232:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2233:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2234:     foreach my $title (@{$titleorder}) {
 2235:         if (defined($searchtitles->{$title})) {
 2236:             my $check = ' ';
 2237:             if (ref($settings) eq 'HASH') {
 2238:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2239:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2240:                         $check = ' checked="checked" ';
 2241:                     }
 2242:                 }
 2243:             }
 2244:             $datatable .= '<td class="LC_left_item">'.
 2245:                           '<span class="LC_nobreak"><label>'.
 2246:                           '<input type="checkbox" name="searchby" '.
 2247:                           'value="'.$title.'"'.$check.'/>'.
 2248:                           $searchtitles->{$title}.'</label></span></td>';
 2249:         }
 2250:     }
 2251:     $datatable .= '</tr></table></td></tr>';
 2252:     $$rowtotal ++;
 2253:     if ($cansrchrow) {
 2254:         $datatable .= '<tr class="LC_odd_row">';
 2255:     } else {
 2256:         $datatable .= '<tr>';
 2257:     }
 2258:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2259:                   '<td class="LC_left_item" colspan="2">'.
 2260:                   '<span class="LC_nobreak"><label>'.
 2261:                   '<input type="checkbox" name="searchtypes" '.
 2262:                   $exacton.' value="exact" />'.&mt('Exact match').
 2263:                   '</label>&nbsp;'.
 2264:                   '<label><input type="checkbox" name="searchtypes" '.
 2265:                   $beginson.' value="begins" />'.&mt('Begins with').
 2266:                   '</label>&nbsp;'.
 2267:                   '<label><input type="checkbox" name="searchtypes" '.
 2268:                   $containson.' value="contains" />'.&mt('Contains').
 2269:                   '</label></span></td></tr>';
 2270:     $$rowtotal ++;
 2271:     return $datatable;
 2272: }
 2273: 
 2274: sub print_contacts {
 2275:     my ($dom,$settings,$rowtotal) = @_;
 2276:     my $datatable;
 2277:     my @contacts = ('adminemail','supportemail');
 2278:     my (%checked,%to,%otheremails,%bccemails);
 2279:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2280:                     'requestsmail','updatesmail');
 2281:     foreach my $type (@mailings) {
 2282:         $otheremails{$type} = '';
 2283:     }
 2284:     $bccemails{'helpdeskmail'} = '';
 2285:     if (ref($settings) eq 'HASH') {
 2286:         foreach my $item (@contacts) {
 2287:             if (exists($settings->{$item})) {
 2288:                 $to{$item} = $settings->{$item};
 2289:             }
 2290:         }
 2291:         foreach my $type (@mailings) {
 2292:             if (exists($settings->{$type})) {
 2293:                 if (ref($settings->{$type}) eq 'HASH') {
 2294:                     foreach my $item (@contacts) {
 2295:                         if ($settings->{$type}{$item}) {
 2296:                             $checked{$type}{$item} = ' checked="checked" ';
 2297:                         }
 2298:                     }
 2299:                     $otheremails{$type} = $settings->{$type}{'others'};
 2300:                     if ($type eq 'helpdeskmail') {
 2301:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2302:                     }
 2303:                 }
 2304:             } elsif ($type eq 'lonstatusmail') {
 2305:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2306:             }
 2307:         }
 2308:     } else {
 2309:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2310:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2311:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2312:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2313:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2314:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2315:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2316:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2317:     }
 2318:     my ($titles,$short_titles) = &contact_titles();
 2319:     my $rownum = 0;
 2320:     my $css_class;
 2321:     foreach my $item (@contacts) {
 2322:         $rownum ++;
 2323:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2324:         $datatable .= '<tr'.$css_class.'>'. 
 2325:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2326:                   '</span></td><td class="LC_right_item">'.
 2327:                   '<input type="text" name="'.$item.'" value="'.
 2328:                   $to{$item}.'" /></td></tr>';
 2329:     }
 2330:     foreach my $type (@mailings) {
 2331:         $rownum ++;
 2332:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2333:         $datatable .= '<tr'.$css_class.'>'.
 2334:                       '<td><span class="LC_nobreak">'.
 2335:                       $titles->{$type}.': </span></td>'.
 2336:                       '<td class="LC_left_item">'.
 2337:                       '<span class="LC_nobreak">';
 2338:         foreach my $item (@contacts) {
 2339:             $datatable .= '<label>'.
 2340:                           '<input type="checkbox" name="'.$type.'"'.
 2341:                           $checked{$type}{$item}.
 2342:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2343:                           '</label>&nbsp;';
 2344:         }
 2345:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2346:                       '<input type="text" name="'.$type.'_others" '.
 2347:                       'value="'.$otheremails{$type}.'"  />';
 2348:         if ($type eq 'helpdeskmail') {
 2349:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2350:                           '<input type="text" name="'.$type.'_bcc" '.
 2351:                           'value="'.$bccemails{$type}.'"  />';
 2352:         }
 2353:         $datatable .= '</td></tr>'."\n";
 2354:     }
 2355:     $$rowtotal += $rownum;
 2356:     return $datatable;
 2357: }
 2358: 
 2359: sub print_helpsettings {
 2360:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2361:     my ($datatable,$itemcount);
 2362:     $itemcount = 1;
 2363:     my (%choices,%defaultchecked,@toggles);
 2364:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2365:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2366:                                  &mt('LON-CAPA bug tracker'),600,500));
 2367:     %defaultchecked = ('submitbugs' => 'on');
 2368:     @toggles = ('submitbugs',);
 2369: 
 2370:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2371:                                                  \%choices,$itemcount);
 2372:     return $datatable;
 2373: }
 2374: 
 2375: sub radiobutton_prefs {
 2376:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2377:         $additional) = @_;
 2378:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2379:                    (ref($choices) eq 'HASH'));
 2380: 
 2381:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2382: 
 2383:     foreach my $item (@{$toggles}) {
 2384:         if ($defaultchecked->{$item} eq 'on') {
 2385:             $checkedon{$item} = ' checked="checked" ';
 2386:             $checkedoff{$item} = ' ';
 2387:         } elsif ($defaultchecked->{$item} eq 'off') {
 2388:             $checkedoff{$item} = ' checked="checked" ';
 2389:             $checkedon{$item} = ' ';
 2390:         }
 2391:     }
 2392:     if (ref($settings) eq 'HASH') {
 2393:         foreach my $item (@{$toggles}) {
 2394:             if ($settings->{$item} eq '1') {
 2395:                 $checkedon{$item} =  ' checked="checked" ';
 2396:                 $checkedoff{$item} = ' ';
 2397:             } elsif ($settings->{$item} eq '0') {
 2398:                 $checkedoff{$item} =  ' checked="checked" ';
 2399:                 $checkedon{$item} = ' ';
 2400:             }
 2401:         }
 2402:     }
 2403:     if ($onclick) {
 2404:         $onclick = ' onclick="'.$onclick.'"';
 2405:     }
 2406:     foreach my $item (@{$toggles}) {
 2407:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2408:         $datatable .=
 2409:             '<tr'.$css_class.'><td valign="top">'.
 2410:             '<span class="LC_nobreak">'.$choices->{$item}.
 2411:             '</span></td>'.
 2412:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2413:             '<label><input type="radio" name="'.
 2414:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2415:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2416:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2417:             '</span>'.$additional.
 2418:             '</td>'.
 2419:             '</tr>';
 2420:         $itemcount ++;
 2421:     }
 2422:     return ($datatable,$itemcount);
 2423: }
 2424: 
 2425: sub print_coursedefaults {
 2426:     my ($position,$dom,$settings,$rowtotal) = @_;
 2427:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2428:     my $itemcount = 1;
 2429:     my %choices =  &Apache::lonlocal::texthash (
 2430:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2431:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2432:         coursecredits        => 'Credits can be specified for courses',
 2433:     );
 2434:     if ($position eq 'top') {
 2435:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2436:         @toggles = ('canuse_pdfforms');
 2437:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2438:                                                  \%choices,$itemcount);
 2439:     } else {
 2440:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2441:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits);
 2442:         my $currusecredits = 0;
 2443:         if (ref($settings) eq 'HASH') {
 2444:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2445:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2446:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2447:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2448:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '')) {
 2449:                     $currusecredits = 1;
 2450:                 }
 2451:             }
 2452:         }
 2453:         if (!$currdefresponder) {
 2454:             $currdefresponder = 10;
 2455:         } elsif ($currdefresponder < 1) {
 2456:             $currdefresponder = 1;
 2457:         }
 2458:         $datatable .=
 2459:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2460:                 $choices{'anonsurvey_threshold'}.
 2461:                 '</span></td>'.
 2462:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2463:                 '<input type="text" name="anonsurvey_threshold"'.
 2464:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2465:                 '</td></tr>'."\n";
 2466:         $itemcount ++;
 2467:         my $onclick = 'toggleCredits(this.form);';
 2468:         my $display = 'none';  
 2469:         if ($currusecredits) {
 2470:             $display = 'block';
 2471:         }
 2472:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2473:                          '<span class="LC_nobreak">'.
 2474:                          &mt('Default credits for official courses [_1]',
 2475:                          '<input type="text" name="official_credits" value="'.
 2476:                          $def_official_credits.'" size="3" />').
 2477:                          '</span><br />'.
 2478:                          '<span class="LC_nobreak">'.
 2479:                          &mt('Default credits for unofficial courses [_1]',
 2480:                          '<input type="text" name="unofficial_credits" value="'.
 2481:                          $def_unofficial_credits.'" size="3" />').
 2482:                          '</span></div>'."\n";
 2483:         %defaultchecked = ('coursecredits' => 'off');
 2484:         @toggles = ('coursecredits');
 2485:         my $current = {
 2486:                         'coursecredits' => $currusecredits,
 2487:                       };
 2488:         (my $table,$itemcount) =
 2489:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2490:                                \%choices,$itemcount,$onclick,$additional);
 2491:         $datatable .= $table;
 2492:     }
 2493:     $$rowtotal += $itemcount;
 2494:     return $datatable;
 2495: }
 2496: 
 2497: sub print_usersessions {
 2498:     my ($position,$dom,$settings,$rowtotal) = @_;
 2499:     my ($css_class,$datatable,%checked,%choices);
 2500:     my (%by_ip,%by_location,@intdoms);
 2501:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 2502: 
 2503:     my @alldoms = &Apache::lonnet::all_domains();
 2504:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 2505:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2506:     my %altids = &id_for_thisdom(%servers);
 2507:     my $itemcount = 1;
 2508:     if ($position eq 'top') {
 2509:         if (keys(%serverhomes) > 1) {
 2510:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 2511:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 2512:         } else {
 2513:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2514:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 2515:         }
 2516:     } else {
 2517:         if (keys(%by_location) == 0) {
 2518:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2519:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 2520:         } else {
 2521:             my %lt = &usersession_titles();
 2522:             my $numinrow = 5;
 2523:             my $prefix;
 2524:             my @types;
 2525:             if ($position eq 'bottom') {
 2526:                 $prefix = 'remote';
 2527:                 @types = ('version','excludedomain','includedomain');
 2528:             } else {
 2529:                 $prefix = 'hosted';
 2530:                 @types = ('excludedomain','includedomain');
 2531:             }
 2532:             my (%current,%checkedon,%checkedoff);
 2533:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 2534:             my @locations = sort(keys(%by_location));
 2535:             foreach my $type (@types) {
 2536:                 $checkedon{$type} = '';
 2537:                 $checkedoff{$type} = ' checked="checked"';
 2538:             }
 2539:             if (ref($settings) eq 'HASH') {
 2540:                 if (ref($settings->{$prefix}) eq 'HASH') {
 2541:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 2542:                         $current{$key} = $settings->{$prefix}{$key};
 2543:                         if ($key eq 'version') {
 2544:                             if ($current{$key} ne '') {
 2545:                                 $checkedon{$key} = ' checked="checked"';
 2546:                                 $checkedoff{$key} = '';
 2547:                             }
 2548:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 2549:                             $checkedon{$key} = ' checked="checked"';
 2550:                             $checkedoff{$key} = '';
 2551:                         }
 2552:                     }
 2553:                 }
 2554:             }
 2555:             foreach my $type (@types) {
 2556:                 next if ($type ne 'version' && !@locations);
 2557:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2558:                 $datatable .= '<tr'.$css_class.'>
 2559:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 2560:                                <span class="LC_nobreak">&nbsp;
 2561:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 2562:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 2563:                 if ($type eq 'version') {
 2564:                     my $selector = '<select name="'.$prefix.'_version">';
 2565:                     foreach my $version (@lcversions) {
 2566:                         my $selected = '';
 2567:                         if ($current{'version'} eq $version) {
 2568:                             $selected = ' selected="selected"';
 2569:                         }
 2570:                         $selector .= ' <option value="'.$version.'"'.
 2571:                                      $selected.'>'.$version.'</option>';
 2572:                     }
 2573:                     $selector .= '</select> ';
 2574:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 2575:                 } else {
 2576:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 2577:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 2578:                                  ' />'.('&nbsp;'x2).
 2579:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 2580:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 2581:                                  "\n".
 2582:                                  '</div><div><table>';
 2583:                     my $rem;
 2584:                     for (my $i=0; $i<@locations; $i++) {
 2585:                         my ($showloc,$value,$checkedtype);
 2586:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 2587:                             my $ip = $by_location{$locations[$i]}->[0];
 2588:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 2589:                                  $value = join(':',@{$by_ip{$ip}});
 2590:                                 $showloc = join(', ',@{$by_ip{$ip}});
 2591:                                 if (ref($current{$type}) eq 'ARRAY') {
 2592:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 2593:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 2594:                                             $checkedtype = ' checked="checked"';
 2595:                                             last;
 2596:                                         }
 2597:                                     }
 2598:                                 }
 2599:                             }
 2600:                         }
 2601:                         $rem = $i%($numinrow);
 2602:                         if ($rem == 0) {
 2603:                             if ($i > 0) {
 2604:                                 $datatable .= '</tr>';
 2605:                             }
 2606:                             $datatable .= '<tr>';
 2607:                         }
 2608:                         $datatable .= '<td class="LC_left_item">'.
 2609:                                       '<span class="LC_nobreak"><label>'.
 2610:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 2611:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 2612:                                       '</label></span></td>';
 2613:                     }
 2614:                     $rem = @locations%($numinrow);
 2615:                     my $colsleft = $numinrow - $rem;
 2616:                     if ($colsleft > 1 ) {
 2617:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2618:                                       '&nbsp;</td>';
 2619:                     } elsif ($colsleft == 1) {
 2620:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 2621:                     }
 2622:                     $datatable .= '</tr></table>';
 2623:                 }
 2624:                 $datatable .= '</td></tr>';
 2625:                 $itemcount ++;
 2626:             }
 2627:         }
 2628:     }
 2629:     $$rowtotal += $itemcount;
 2630:     return $datatable;
 2631: }
 2632: 
 2633: sub build_location_hashes {
 2634:     my ($intdoms,$by_ip,$by_location) = @_;
 2635:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 2636:                   (ref($by_location) eq 'HASH')); 
 2637:     my %iphost = &Apache::lonnet::get_iphost();
 2638:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2639:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 2640:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 2641:         foreach my $id (@{$iphost{$primary_ip}}) {
 2642:             my $intdom = &Apache::lonnet::internet_dom($id);
 2643:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 2644:                 push(@{$intdoms},$intdom);
 2645:             }
 2646:         }
 2647:     }
 2648:     foreach my $ip (keys(%iphost)) {
 2649:         if (ref($iphost{$ip}) eq 'ARRAY') {
 2650:             foreach my $id (@{$iphost{$ip}}) {
 2651:                 my $location = &Apache::lonnet::internet_dom($id);
 2652:                 if ($location) {
 2653:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 2654:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2655:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 2656:                             push(@{$by_ip->{$ip}},$location);
 2657:                         }
 2658:                     } else {
 2659:                         $by_ip->{$ip} = [$location];
 2660:                     }
 2661:                 }
 2662:             }
 2663:         }
 2664:     }
 2665:     foreach my $ip (sort(keys(%{$by_ip}))) {
 2666:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2667:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 2668:             my $first = $by_ip->{$ip}->[0];
 2669:             if (ref($by_location->{$first}) eq 'ARRAY') {
 2670:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 2671:                     push(@{$by_location->{$first}},$ip);
 2672:                 }
 2673:             } else {
 2674:                 $by_location->{$first} = [$ip];
 2675:             }
 2676:         }
 2677:     }
 2678:     return;
 2679: }
 2680: 
 2681: sub current_offloads_to {
 2682:     my ($dom,$settings,$servers) = @_;
 2683:     my (%spareid,%otherdomconfigs);
 2684:     if (ref($servers) eq 'HASH') {
 2685:         foreach my $lonhost (sort(keys(%{$servers}))) {
 2686:             my $gotspares;
 2687:             if (ref($settings) eq 'HASH') {
 2688:                 if (ref($settings->{'spares'}) eq 'HASH') {
 2689:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 2690:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 2691:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 2692:                         $gotspares = 1;
 2693:                     }
 2694:                 }
 2695:             }
 2696:             unless ($gotspares) {
 2697:                 my $gotspares;
 2698:                 my $serverhomeID =
 2699:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 2700:                 my $serverhomedom =
 2701:                     &Apache::lonnet::host_domain($serverhomeID);
 2702:                 if ($serverhomedom ne $dom) {
 2703:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 2704:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2705:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2706:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2707:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2708:                                 $gotspares = 1;
 2709:                             }
 2710:                         }
 2711:                     } else {
 2712:                         $otherdomconfigs{$serverhomedom} =
 2713:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 2714:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 2715:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2716:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2717:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 2718:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2719:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2720:                                         $gotspares = 1;
 2721:                                     }
 2722:                                 }
 2723:                             }
 2724:                         }
 2725:                     }
 2726:                 }
 2727:             }
 2728:             unless ($gotspares) {
 2729:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2730:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2731:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2732:                } else {
 2733:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 2734:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 2735:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2736:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2737:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2738:                     } else {
 2739:                         my %what = (
 2740:                              spareid => 1,
 2741:                         );
 2742:                         my ($result,$returnhash) = 
 2743:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 2744:                         if ($result eq 'ok') { 
 2745:                             if (ref($returnhash) eq 'HASH') {
 2746:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 2747:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 2748:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 2749:                                 }
 2750:                             }
 2751:                         }
 2752:                     }
 2753:                 }
 2754:             }
 2755:         }
 2756:     }
 2757:     return %spareid;
 2758: }
 2759: 
 2760: sub spares_row {
 2761:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 2762:     my $css_class;
 2763:     my $numinrow = 4;
 2764:     my $itemcount = 1;
 2765:     my $datatable;
 2766:     my %typetitles = &sparestype_titles();
 2767:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2768:         foreach my $server (sort(keys(%{$servers}))) {
 2769:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 2770:             my ($othercontrol,$serverdom);
 2771:             if ($serverhome ne $server) {
 2772:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 2773:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2774:             } else {
 2775:                 $serverdom = &Apache::lonnet::host_domain($server);
 2776:                 if ($serverdom ne $dom) {
 2777:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2778:                 }
 2779:             }
 2780:             next unless (ref($spareid->{$server}) eq 'HASH');
 2781:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2782:             $datatable .= '<tr'.$css_class.'>
 2783:                            <td rowspan="2">
 2784:                             <span class="LC_nobreak">'.
 2785:                           &mt('[_1] when busy, offloads to:'
 2786:                               ,'<b>'.$server.'</b>').
 2787:                           "\n";
 2788:             my (%current,%canselect);
 2789:             my @choices = 
 2790:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 2791:             foreach my $type ('primary','default') {
 2792:                 if (ref($spareid->{$server}) eq 'HASH') {
 2793:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 2794:                         my @spares = @{$spareid->{$server}{$type}};
 2795:                         if (@spares > 0) {
 2796:                             if ($othercontrol) {
 2797:                                 $current{$type} = join(', ',@spares);
 2798:                             } else {
 2799:                                 $current{$type} .= '<table>';
 2800:                                 my $numspares = scalar(@spares);
 2801:                                 for (my $i=0;  $i<@spares; $i++) {
 2802:                                     my $rem = $i%($numinrow);
 2803:                                     if ($rem == 0) {
 2804:                                         if ($i > 0) {
 2805:                                             $current{$type} .= '</tr>';
 2806:                                         }
 2807:                                         $current{$type} .= '<tr>';
 2808:                                     }
 2809:                                     $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;'.
 2810:                                                        $spareid->{$server}{$type}[$i].
 2811:                                                        '</label></td>'."\n";
 2812:                                 }
 2813:                                 my $rem = @spares%($numinrow);
 2814:                                 my $colsleft = $numinrow - $rem;
 2815:                                 if ($colsleft > 1 ) {
 2816:                                     $current{$type} .= '<td colspan="'.$colsleft.
 2817:                                                        '" class="LC_left_item">'.
 2818:                                                        '&nbsp;</td>';
 2819:                                 } elsif ($colsleft == 1) {
 2820:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 2821:                                 }
 2822:                                 $current{$type} .= '</tr></table>';
 2823:                             }
 2824:                         }
 2825:                     }
 2826:                     if ($current{$type} eq '') {
 2827:                         $current{$type} = &mt('None specified');
 2828:                     }
 2829:                     if ($othercontrol) {
 2830:                         if ($type eq 'primary') {
 2831:                             $canselect{$type} = $othercontrol;
 2832:                         }
 2833:                     } else {
 2834:                         $canselect{$type} = 
 2835:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 2836:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 2837:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 2838:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 2839:                         if (@choices > 0) {
 2840:                             foreach my $lonhost (@choices) {
 2841:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 2842:                             }
 2843:                         }
 2844:                         $canselect{$type} .= '</select>'."\n";
 2845:                     }
 2846:                 } else {
 2847:                     $current{$type} = &mt('Could not be determined');
 2848:                     if ($type eq 'primary') {
 2849:                         $canselect{$type} =  $othercontrol;
 2850:                     }
 2851:                 }
 2852:                 if ($type eq 'default') {
 2853:                     $datatable .= '<tr'.$css_class.'>';
 2854:                 }
 2855:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 2856:                               '<td>'.$current{$type}.'</td>'."\n".
 2857:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 2858:             }
 2859:             $itemcount ++;
 2860:         }
 2861:     }
 2862:     $$rowtotal += $itemcount;
 2863:     return $datatable;
 2864: }
 2865: 
 2866: sub possible_newspares {
 2867:     my ($server,$currspares,$serverhomes,$altids) = @_;
 2868:     my $serverhostname = &Apache::lonnet::hostname($server);
 2869:     my %excluded;
 2870:     if ($serverhostname ne '') {
 2871:         %excluded = (
 2872:                        $serverhostname => 1,
 2873:                     );
 2874:     }
 2875:     if (ref($currspares) eq 'HASH') {
 2876:         foreach my $type (keys(%{$currspares})) {
 2877:             if (ref($currspares->{$type}) eq 'ARRAY') {
 2878:                 if (@{$currspares->{$type}} > 0) {
 2879:                     foreach my $curr (@{$currspares->{$type}}) {
 2880:                         my $hostname = &Apache::lonnet::hostname($curr);
 2881:                         $excluded{$hostname} = 1;
 2882:                     }
 2883:                 }
 2884:             }
 2885:         }
 2886:     }
 2887:     my @choices;
 2888:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2889:         if (keys(%{$serverhomes}) > 1) {
 2890:             foreach my $name (sort(keys(%{$serverhomes}))) {
 2891:                 unless ($excluded{$name}) {
 2892:                     if (exists($altids->{$serverhomes->{$name}})) {
 2893:                         push(@choices,$altids->{$serverhomes->{$name}});
 2894:                     } else {
 2895:                         push(@choices,$serverhomes->{$name});
 2896:                     }
 2897:                 }
 2898:             }
 2899:         }
 2900:     }
 2901:     return sort(@choices);
 2902: }
 2903: 
 2904: sub print_loadbalancing {
 2905:     my ($dom,$settings,$rowtotal) = @_;
 2906:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 2907:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 2908:     my $numinrow = 1;
 2909:     my $datatable;
 2910:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2911:     my (%currbalancer,%currtargets,%currrules,%existing);
 2912:     if (ref($settings) eq 'HASH') {
 2913:         %existing = %{$settings};
 2914:     }
 2915:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 2916:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 2917:                                   \%currtargets,\%currrules);
 2918:     } else {
 2919:         return;
 2920:     }
 2921:     my ($othertitle,$usertypes,$types) =
 2922:         &Apache::loncommon::sorted_inst_types($dom);
 2923:     my $rownum = 6;
 2924:     if (ref($types) eq 'ARRAY') {
 2925:         $rownum += scalar(@{$types});
 2926:     }
 2927:     my @css_class = ('LC_odd_row','LC_even_row');
 2928:     my $balnum = 0;
 2929:     my $islast;
 2930:     my (@toshow,$disabledtext);
 2931:     if (keys(%currbalancer) > 0) {
 2932:         @toshow = sort(keys(%currbalancer));
 2933:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 2934:             push(@toshow,'');
 2935:         }
 2936:     } else {
 2937:         @toshow = ('');
 2938:         $disabledtext = &mt('No existing load balancer');
 2939:     }
 2940:     foreach my $lonhost (@toshow) {
 2941:         if ($balnum == scalar(@toshow)-1) {
 2942:             $islast = 1;
 2943:         } else {
 2944:             $islast = 0;
 2945:         }
 2946:         my $cssidx = $balnum%2;
 2947:         my $targets_div_style = 'display: none';
 2948:         my $disabled_div_style = 'display: block';
 2949:         my $homedom_div_style = 'display: none';
 2950:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 2951:                       '<td rowspan="'.$rownum.'" valign="top">'.
 2952:                       '<p>';
 2953:         if ($lonhost eq '') {
 2954:             $datatable .= '<span class="LC_nobreak">'; 
 2955:             if (keys(%currbalancer) > 0) {
 2956:                 $datatable .= &mt('Add balancer:');
 2957:             } else {
 2958:                 $datatable .= &mt('Enable balancer:');
 2959:             }
 2960:             $datatable .= '&nbsp;'.
 2961:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 2962:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 2963:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 2964:                           '<option value="" selected="selected">'.&mt('None').
 2965:                           '</option>'."\n";
 2966:             foreach my $server (sort(keys(%servers))) {
 2967:                 next if ($currbalancer{$server});
 2968:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 2969:             }
 2970:             $datatable .= 
 2971:                 '</select>'."\n".
 2972:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 2973:         } else {
 2974:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 2975:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 2976:                            &mt('Stop balancing').'</label>'.
 2977:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 2978:             $targets_div_style = 'display: block';
 2979:             $disabled_div_style = 'display: none';
 2980:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 2981:                 $homedom_div_style = 'display: block';
 2982:             }
 2983:         }
 2984:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 2985:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 2986:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 2987:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 2988:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 2989:         my @sparestypes = ('primary','default');
 2990:         my %typetitles = &sparestype_titles();
 2991:         foreach my $sparetype (@sparestypes) {
 2992:             my $targettable;
 2993:             for (my $i=0; $i<$numspares; $i++) {
 2994:                 my $checked;
 2995:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 2996:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 2997:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 2998:                             $checked = ' checked="checked"';
 2999:                         }
 3000:                     }
 3001:                 }
 3002:                 my ($chkboxval,$disabled);
 3003:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3004:                     $chkboxval = $spares[$i];
 3005:                 }
 3006:                 if (exists($currbalancer{$spares[$i]})) {
 3007:                     $disabled = ' disabled="disabled"';
 3008:                 }
 3009:                 $targettable .= 
 3010:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3011:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3012:                     '</span></label></td>';
 3013:                 my $rem = $i%($numinrow);
 3014:                 if ($rem == 0) {
 3015:                     if (($i > 0) && ($i < $numspares-1)) {
 3016:                         $targettable .= '</tr>';
 3017:                     }
 3018:                     if ($i < $numspares-1) {
 3019:                         $targettable .= '<tr>';
 3020:                     }
 3021:                 }
 3022:             }
 3023:             if ($targettable ne '') {
 3024:                 my $rem = $numspares%($numinrow);
 3025:                 my $colsleft = $numinrow - $rem;
 3026:                 if ($colsleft > 1 ) {
 3027:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3028:                                     '&nbsp;</td>';
 3029:                 } elsif ($colsleft == 1) {
 3030:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3031:                 }
 3032:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3033:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3034:             }
 3035:         }
 3036:         $datatable .= '</div></td></tr>'.
 3037:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3038:                                            $othertitle,$usertypes,$types,\%servers,
 3039:                                            \%currbalancer,$lonhost,
 3040:                                            $targets_div_style,$homedom_div_style,
 3041:                                            $css_class[$cssidx],$balnum,$islast);
 3042:         $$rowtotal += $rownum;
 3043:         $balnum ++;
 3044:     }
 3045:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3046:     return $datatable;
 3047: }
 3048: 
 3049: sub get_loadbalancers_config {
 3050:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3051:     return unless ((ref($servers) eq 'HASH') &&
 3052:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3053:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3054:     if (keys(%{$existing}) > 0) {
 3055:         my $oldlonhost;
 3056:         foreach my $key (sort(keys(%{$existing}))) {
 3057:             if ($key eq 'lonhost') {
 3058:                 $oldlonhost = $existing->{'lonhost'};
 3059:                 $currbalancer->{$oldlonhost} = 1;
 3060:             } elsif ($key eq 'targets') {
 3061:                 if ($oldlonhost) {
 3062:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3063:                 }
 3064:             } elsif ($key eq 'rules') {
 3065:                 if ($oldlonhost) {
 3066:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3067:                 }
 3068:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3069:                 $currbalancer->{$key} = 1;
 3070:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3071:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3072:             }
 3073:         }
 3074:     } else {
 3075:         my ($balancerref,$targetsref) =
 3076:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3077:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3078:             foreach my $server (sort(keys(%{$balancerref}))) {
 3079:                 $currbalancer->{$server} = 1;
 3080:                 $currtargets->{$server} = $targetsref->{$server};
 3081:             }
 3082:         }
 3083:     }
 3084:     return;
 3085: }
 3086: 
 3087: sub loadbalancing_rules {
 3088:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3089:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3090:         $css_class,$balnum,$islast) = @_;
 3091:     my $output;
 3092:     my $num = 0;
 3093:     my ($alltypes,$othertypes,$titles) = 
 3094:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3095:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3096:         foreach my $type (@{$alltypes}) {
 3097:             $num ++;
 3098:             my $current;
 3099:             if (ref($currrules) eq 'HASH') {
 3100:                 $current = $currrules->{$type};
 3101:             }
 3102:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3103:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3104:                     $current = '';
 3105:                 }
 3106:             }
 3107:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3108:                                              $servers,$currbalancer,$lonhost,$dom,
 3109:                                              $targets_div_style,$homedom_div_style,
 3110:                                              $css_class,$balnum,$num,$islast);
 3111:         }
 3112:     }
 3113:     return $output;
 3114: }
 3115: 
 3116: sub loadbalancing_titles {
 3117:     my ($dom,$intdom,$usertypes,$types) = @_;
 3118:     my %othertypes = (
 3119:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3120:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3121:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3122:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3123:                      );
 3124:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 3125:     if (ref($types) eq 'ARRAY') {
 3126:         unshift(@alltypes,@{$types},'default');
 3127:     }
 3128:     my %titles;
 3129:     foreach my $type (@alltypes) {
 3130:         if ($type =~ /^_LC_/) {
 3131:             $titles{$type} = $othertypes{$type};
 3132:         } elsif ($type eq 'default') {
 3133:             $titles{$type} = &mt('All users from [_1]',$dom);
 3134:             if (ref($types) eq 'ARRAY') {
 3135:                 if (@{$types} > 0) {
 3136:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3137:                 }
 3138:             }
 3139:         } elsif (ref($usertypes) eq 'HASH') {
 3140:             $titles{$type} = $usertypes->{$type};
 3141:         }
 3142:     }
 3143:     return (\@alltypes,\%othertypes,\%titles);
 3144: }
 3145: 
 3146: sub loadbalance_rule_row {
 3147:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3148:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3149:     my @rulenames = ('default','homeserver');
 3150:     my %ruletitles = &offloadtype_text();
 3151:     if ($type eq '_LC_external') {
 3152:         push(@rulenames,'externalbalancer');
 3153:     } else {
 3154:         push(@rulenames,'specific');
 3155:     }
 3156:     push(@rulenames,'none');
 3157:     my $style = $targets_div_style;
 3158:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 3159:         $style = $homedom_div_style;
 3160:     }
 3161:     my $space;
 3162:     if ($islast && $num == 1) {
 3163:         $space = '<div display="inline-block">&nbsp;</div>';
 3164:     }
 3165:     my $output = 
 3166:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3167:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3168:         '<td valaign="top">'.$space.
 3169:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3170:     for (my $i=0; $i<@rulenames; $i++) {
 3171:         my $rule = $rulenames[$i];
 3172:         my ($checked,$extra);
 3173:         if ($rulenames[$i] eq 'default') {
 3174:             $rule = '';
 3175:         }
 3176:         if ($rulenames[$i] eq 'specific') {
 3177:             if (ref($servers) eq 'HASH') {
 3178:                 my $default;
 3179:                 if (($current ne '') && (exists($servers->{$current}))) {
 3180:                     $checked = ' checked="checked"';
 3181:                 }
 3182:                 unless ($checked) {
 3183:                     $default = ' selected="selected"';
 3184:                 }
 3185:                 $extra = 
 3186:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3187:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3188:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3189:                     '<option value=""'.$default.'></option>'."\n";
 3190:                 foreach my $server (sort(keys(%{$servers}))) {
 3191:                     if (ref($currbalancer) eq 'HASH') {
 3192:                         next if (exists($currbalancer->{$server}));
 3193:                     }
 3194:                     my $selected;
 3195:                     if ($server eq $current) {
 3196:                         $selected = ' selected="selected"';
 3197:                     }
 3198:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3199:                 }
 3200:                 $extra .= '</select>';
 3201:             }
 3202:         } elsif ($rule eq $current) {
 3203:             $checked = ' checked="checked"';
 3204:         }
 3205:         $output .= '<span class="LC_nobreak"><label>'.
 3206:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3207:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3208:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3209:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3210:                    '</label>'.$extra.'</span><br />'."\n";
 3211:     }
 3212:     $output .= '</div></td></tr>'."\n";
 3213:     return $output;
 3214: }
 3215: 
 3216: sub offloadtype_text {
 3217:     my %ruletitles = &Apache::lonlocal::texthash (
 3218:            'default'          => 'Offloads to default destinations',
 3219:            'homeserver'       => "Offloads to user's home server",
 3220:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3221:            'specific'         => 'Offloads to specific server',
 3222:            'none'             => 'No offload',
 3223:     );
 3224:     return %ruletitles;
 3225: }
 3226: 
 3227: sub sparestype_titles {
 3228:     my %typestitles = &Apache::lonlocal::texthash (
 3229:                           'primary' => 'primary',
 3230:                           'default' => 'default',
 3231:                       );
 3232:     return %typestitles;
 3233: }
 3234: 
 3235: sub contact_titles {
 3236:     my %titles = &Apache::lonlocal::texthash (
 3237:                    'supportemail' => 'Support E-mail address',
 3238:                    'adminemail'   => 'Default Server Admin E-mail address',
 3239:                    'errormail'    => 'Error reports to be e-mailed to',
 3240:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3241:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3242:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3243:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3244:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3245:                  );
 3246:     my %short_titles = &Apache::lonlocal::texthash (
 3247:                            adminemail   => 'Admin E-mail address',
 3248:                            supportemail => 'Support E-mail',
 3249:                        );   
 3250:     return (\%titles,\%short_titles);
 3251: }
 3252: 
 3253: sub tool_titles {
 3254:     my %titles = &Apache::lonlocal::texthash (
 3255:                      aboutme    => 'Personal web page',
 3256:                      blog       => 'Blog',
 3257:                      webdav     => 'WebDAV',
 3258:                      portfolio  => 'Portfolio',
 3259:                      official   => 'Official courses (with institutional codes)',
 3260:                      unofficial => 'Unofficial courses',
 3261:                      community  => 'Communities',
 3262:                  );
 3263:     return %titles;
 3264: }
 3265: 
 3266: sub courserequest_titles {
 3267:     my %titles = &Apache::lonlocal::texthash (
 3268:                                    official   => 'Official',
 3269:                                    unofficial => 'Unofficial',
 3270:                                    community  => 'Communities',
 3271:                                    norequest  => 'Not allowed',
 3272:                                    approval   => 'Approval by Dom. Coord.',
 3273:                                    validate   => 'With validation',
 3274:                                    autolimit  => 'Numerical limit',
 3275:                                    unlimited  => '(blank for unlimited)',
 3276:                  );
 3277:     return %titles;
 3278: }
 3279: 
 3280: sub authorrequest_titles {
 3281:     my %titles = &Apache::lonlocal::texthash (
 3282:                                    norequest  => 'Not allowed',
 3283:                                    approval   => 'Approval by Dom. Coord.',
 3284:                                    automatic  => 'Automatic approval',
 3285:                  );
 3286:     return %titles;
 3287: } 
 3288: 
 3289: sub courserequest_conditions {
 3290:     my %conditions = &Apache::lonlocal::texthash (
 3291:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3292:        validate   => '(Processing of request subject to instittutional validation).',
 3293:                  );
 3294:     return %conditions;
 3295: }
 3296: 
 3297: 
 3298: sub print_usercreation {
 3299:     my ($position,$dom,$settings,$rowtotal) = @_;
 3300:     my $numinrow = 4;
 3301:     my $datatable;
 3302:     if ($position eq 'top') {
 3303:         $$rowtotal ++;
 3304:         my $rowcount = 0;
 3305:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3306:         if (ref($rules) eq 'HASH') {
 3307:             if (keys(%{$rules}) > 0) {
 3308:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3309:                                                 $ruleorder,$numinrow,$rowcount);
 3310:                 $$rowtotal ++;
 3311:                 $rowcount ++;
 3312:             }
 3313:         }
 3314:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3315:         if (ref($idrules) eq 'HASH') {
 3316:             if (keys(%{$idrules}) > 0) {
 3317:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3318:                                                 $idruleorder,$numinrow,$rowcount);
 3319:                 $$rowtotal ++;
 3320:                 $rowcount ++;
 3321:             }
 3322:         }
 3323:         my ($emailrules,$emailruleorder) = 
 3324:             &Apache::lonnet::inst_userrules($dom,'email');
 3325:         if (ref($emailrules) eq 'HASH') {
 3326:             if (keys(%{$emailrules}) > 0) {
 3327:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 3328:                                                 $emailruleorder,$numinrow,$rowcount);
 3329:                 $$rowtotal ++;
 3330:                 $rowcount ++;
 3331:             }
 3332:         }
 3333:         if ($rowcount == 0) {
 3334:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3335:             $$rowtotal ++;
 3336:             $rowcount ++;
 3337:         }
 3338:     } elsif ($position eq 'middle') {
 3339:         my @creators = ('author','course','requestcrs','selfcreate');
 3340:         my ($rules,$ruleorder) =
 3341:             &Apache::lonnet::inst_userrules($dom,'username');
 3342:         my %lt = &usercreation_types();
 3343:         my %checked;
 3344:         my @selfcreate; 
 3345:         if (ref($settings) eq 'HASH') {
 3346:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3347:                 foreach my $item (@creators) {
 3348:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3349:                 }
 3350:                 if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3351:                     @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3352:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3353:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3354:                         @selfcreate = ('email','login','sso');
 3355:                     } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3356:                         @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3357:                     }
 3358:                 }
 3359:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3360:                 foreach my $item (@creators) {
 3361:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3362:                         $checked{$item} = 'none';
 3363:                     }
 3364:                 }
 3365:             }
 3366:         }
 3367:         my $rownum = 0;
 3368:         foreach my $item (@creators) {
 3369:             $rownum ++;
 3370:             if ($item ne 'selfcreate') {  
 3371:                 if ($checked{$item} eq '') {
 3372:                     $checked{$item} = 'any';
 3373:                 }
 3374:             }
 3375:             my $css_class;
 3376:             if ($rownum%2) {
 3377:                 $css_class = '';
 3378:             } else {
 3379:                 $css_class = ' class="LC_odd_row" ';
 3380:             }
 3381:             $datatable .= '<tr'.$css_class.'>'.
 3382:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3383:                          '</span></td><td align="right">';
 3384:             my @options;
 3385:             if ($item eq 'selfcreate') {
 3386:                 push(@options,('email','login','sso'));
 3387:             } else {
 3388:                 @options = ('any');
 3389:                 if (ref($rules) eq 'HASH') {
 3390:                     if (keys(%{$rules}) > 0) {
 3391:                         push(@options,('official','unofficial'));
 3392:                     }
 3393:                 }
 3394:                 push(@options,'none');
 3395:             }
 3396:             foreach my $option (@options) {
 3397:                 my $type = 'radio';
 3398:                 my $check = ' ';
 3399:                 if ($item eq 'selfcreate') {
 3400:                     $type = 'checkbox';
 3401:                     if (grep(/^\Q$option\E$/,@selfcreate)) {
 3402:                         $check = ' checked="checked" ';
 3403:                     }
 3404:                 } else {
 3405:                     if ($checked{$item} eq $option) {
 3406:                         $check = ' checked="checked" ';
 3407:                     }
 3408:                 } 
 3409:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3410:                               '<input type="'.$type.'" name="can_createuser_'.
 3411:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3412:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3413:             }
 3414:             $datatable .= '</td></tr>';
 3415:         }
 3416:         my ($othertitle,$usertypes,$types) =
 3417:             &Apache::loncommon::sorted_inst_types($dom);
 3418:         my $createsettings;
 3419:         if (ref($settings) eq 'HASH') {
 3420:             $createsettings = $settings->{cancreate};
 3421:         }
 3422:         if (ref($usertypes) eq 'HASH') {
 3423:             if (keys(%{$usertypes}) > 0) {
 3424:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3425:                                              $dom,$numinrow,$othertitle,
 3426:                                              'statustocreate');
 3427:                 $$rowtotal ++;
 3428:                 $rownum ++;
 3429:             }
 3430:         }
 3431:         $datatable .= &captcha_choice('cancreate',$createsettings,$rownum);
 3432:     } else {
 3433:         my @contexts = ('author','course','domain');
 3434:         my @authtypes = ('int','krb4','krb5','loc');
 3435:         my %checked;
 3436:         if (ref($settings) eq 'HASH') {
 3437:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3438:                 foreach my $item (@contexts) {
 3439:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3440:                         foreach my $auth (@authtypes) {
 3441:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3442:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3443:                             }
 3444:                         }
 3445:                     }
 3446:                 }
 3447:             }
 3448:         } else {
 3449:             foreach my $item (@contexts) {
 3450:                 foreach my $auth (@authtypes) {
 3451:                     $checked{$item}{$auth} = ' checked="checked" ';
 3452:                 }
 3453:             }
 3454:         }
 3455:         my %title = &context_names();
 3456:         my %authname = &authtype_names();
 3457:         my $rownum = 0;
 3458:         my $css_class; 
 3459:         foreach my $item (@contexts) {
 3460:             if ($rownum%2) {
 3461:                 $css_class = '';
 3462:             } else {
 3463:                 $css_class = ' class="LC_odd_row" ';
 3464:             }
 3465:             $datatable .=   '<tr'.$css_class.'>'.
 3466:                             '<td>'.$title{$item}.
 3467:                             '</td><td class="LC_left_item">'.
 3468:                             '<span class="LC_nobreak">';
 3469:             foreach my $auth (@authtypes) {
 3470:                 $datatable .= '<label>'. 
 3471:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3472:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3473:                               $authname{$auth}.'</label>&nbsp;';
 3474:             }
 3475:             $datatable .= '</span></td></tr>';
 3476:             $rownum ++;
 3477:         }
 3478:         $$rowtotal += $rownum;
 3479:     }
 3480:     return $datatable;
 3481: }
 3482: 
 3483: sub captcha_choice {
 3484:     my ($context,$settings,$itemcount) = @_;
 3485:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 3486:     my %lt = &captcha_phrases();
 3487:     $keyentry = 'hidden';
 3488:     if ($context eq 'cancreate') {
 3489:         $rowname = &mt('CAPTCHA validation (e-mail as username)');
 3490:     } elsif ($context eq 'login') {
 3491:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 3492:     }
 3493:     if (ref($settings) eq 'HASH') {
 3494:         if ($settings->{'captcha'}) {
 3495:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 3496:         } else {
 3497:             $checked{'original'} = ' checked="checked"';
 3498:         }
 3499:         if ($settings->{'captcha'} eq 'recaptcha') {
 3500:             $pubtext = $lt{'pub'};
 3501:             $privtext = $lt{'priv'};
 3502:             $keyentry = 'text';
 3503:         }
 3504:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 3505:             $currpub = $settings->{'recaptchakeys'}{'public'};
 3506:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 3507:         }
 3508:     } else {
 3509:         $checked{'original'} = ' checked="checked"';
 3510:     }
 3511:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3512:     my $output = '<tr'.$css_class.'>'.
 3513:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 3514:                  '<table><tr><td>'."\n";
 3515:     foreach my $option ('original','recaptcha','notused') {
 3516:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 3517:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 3518:                    $lt{$option}.'</label></span>';
 3519:         unless ($option eq 'notused') {
 3520:             $output .= ('&nbsp;'x2)."\n";
 3521:         }
 3522:     }
 3523: #
 3524: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 3525: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 3526: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu) 
 3527: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 3528: #  
 3529:     $output .= '</td></tr>'."\n".
 3530:                '<tr><td>'."\n".
 3531:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 3532:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 3533:                $currpub.'" size="40" /></span><br />'."\n".
 3534:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 3535:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 3536:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 3537:                '</td></tr>';
 3538:     return $output;
 3539: }
 3540: 
 3541: sub user_formats_row {
 3542:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 3543:     my $output;
 3544:     my %text = (
 3545:                    'username' => 'new usernames',
 3546:                    'id'       => 'IDs',
 3547:                    'email'    => 'self-created accounts (e-mail)',
 3548:                );
 3549:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3550:     $output = '<tr '.$css_class.'>'.
 3551:               '<td><span class="LC_nobreak">';
 3552:     if ($type eq 'email') {
 3553:         $output .= &mt("Formats disallowed for $text{$type}: ");
 3554:     } else {
 3555:         $output .= &mt("Format rules to check for $text{$type}: ");
 3556:     }
 3557:     $output .= '</span></td>'.
 3558:                '<td class="LC_left_item" colspan="2"><table>';
 3559:     my $rem;
 3560:     if (ref($ruleorder) eq 'ARRAY') {
 3561:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 3562:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 3563:                 my $rem = $i%($numinrow);
 3564:                 if ($rem == 0) {
 3565:                     if ($i > 0) {
 3566:                         $output .= '</tr>';
 3567:                     }
 3568:                     $output .= '<tr>';
 3569:                 }
 3570:                 my $check = ' ';
 3571:                 if (ref($settings) eq 'HASH') {
 3572:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 3573:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 3574:                             $check = ' checked="checked" ';
 3575:                         }
 3576:                     }
 3577:                 }
 3578:                 $output .= '<td class="LC_left_item">'.
 3579:                            '<span class="LC_nobreak"><label>'.
 3580:                            '<input type="checkbox" name="'.$type.'_rule" '.
 3581:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 3582:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 3583:             }
 3584:         }
 3585:         $rem = @{$ruleorder}%($numinrow);
 3586:     }
 3587:     my $colsleft = $numinrow - $rem;
 3588:     if ($colsleft > 1 ) {
 3589:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3590:                    '&nbsp;</td>';
 3591:     } elsif ($colsleft == 1) {
 3592:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 3593:     }
 3594:     $output .= '</tr></table></td></tr>';
 3595:     return $output;
 3596: }
 3597: 
 3598: sub usercreation_types {
 3599:     my %lt = &Apache::lonlocal::texthash (
 3600:                     author     => 'When adding a co-author',
 3601:                     course     => 'When adding a user to a course',
 3602:                     requestcrs => 'When requesting a course',
 3603:                     selfcreate => 'User creates own account', 
 3604:                     any        => 'Any',
 3605:                     official   => 'Institutional only ',
 3606:                     unofficial => 'Non-institutional only',
 3607:                     email      => 'E-mail address',
 3608:                     login      => 'Institutional Login',
 3609:                     sso        => 'SSO', 
 3610:                     none       => 'None',
 3611:     );
 3612:     return %lt;
 3613: }
 3614: 
 3615: sub authtype_names {
 3616:     my %lt = &Apache::lonlocal::texthash(
 3617:                       int    => 'Internal',
 3618:                       krb4   => 'Kerberos 4',
 3619:                       krb5   => 'Kerberos 5',
 3620:                       loc    => 'Local',
 3621:                   );
 3622:     return %lt;
 3623: }
 3624: 
 3625: sub context_names {
 3626:     my %context_title = &Apache::lonlocal::texthash(
 3627:        author => 'Creating users when an Author',
 3628:        course => 'Creating users when in a course',
 3629:        domain => 'Creating users when a Domain Coordinator',
 3630:     );
 3631:     return %context_title;
 3632: }
 3633: 
 3634: sub print_usermodification {
 3635:     my ($position,$dom,$settings,$rowtotal) = @_;
 3636:     my $numinrow = 4;
 3637:     my ($context,$datatable,$rowcount);
 3638:     if ($position eq 'top') {
 3639:         $rowcount = 0;
 3640:         $context = 'author'; 
 3641:         foreach my $role ('ca','aa') {
 3642:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3643:                                                    $numinrow,$rowcount);
 3644:             $$rowtotal ++;
 3645:             $rowcount ++;
 3646:         }
 3647:     } elsif ($position eq 'middle') {
 3648:         $context = 'course';
 3649:         $rowcount = 0;
 3650:         foreach my $role ('st','ep','ta','in','cr') {
 3651:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3652:                                                    $numinrow,$rowcount);
 3653:             $$rowtotal ++;
 3654:             $rowcount ++;
 3655:         }
 3656:     } elsif ($position eq 'bottom') {
 3657:         $context = 'selfcreate';
 3658:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3659:         $usertypes->{'default'} = $othertitle;
 3660:         if (ref($types) eq 'ARRAY') {
 3661:             push(@{$types},'default');
 3662:             $usertypes->{'default'} = $othertitle;
 3663:             foreach my $status (@{$types}) {
 3664:                 $datatable .= &modifiable_userdata_row($context,$status,$settings,
 3665:                                                        $numinrow,$rowcount,$usertypes);
 3666:                 $$rowtotal ++;
 3667:                 $rowcount ++;
 3668:             }
 3669:         }
 3670:     }
 3671:     return $datatable;
 3672: }
 3673: 
 3674: sub print_defaults {
 3675:     my ($dom,$rowtotal) = @_;
 3676:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 3677:                  'datelocale_def','portal_def');
 3678:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 3679:     my $titles = &defaults_titles($dom);
 3680:     my $rownum = 0;
 3681:     my ($datatable,$css_class);
 3682:     foreach my $item (@items) {
 3683:         if ($rownum%2) {
 3684:             $css_class = '';
 3685:         } else {
 3686:             $css_class = ' class="LC_odd_row" ';
 3687:         }
 3688:         $datatable .= '<tr'.$css_class.'>'.
 3689:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 3690:                   '</span></td><td class="LC_right_item">';
 3691:         if ($item eq 'auth_def') {
 3692:             my @authtypes = ('internal','krb4','krb5','localauth');
 3693:             my %shortauth = (
 3694:                              internal => 'int',
 3695:                              krb4 => 'krb4',
 3696:                              krb5 => 'krb5',
 3697:                              localauth  => 'loc'
 3698:                            );
 3699:             my %authnames = &authtype_names();
 3700:             foreach my $auth (@authtypes) {
 3701:                 my $checked = ' ';
 3702:                 if ($domdefaults{$item} eq $auth) {
 3703:                     $checked = ' checked="checked" ';
 3704:                 }
 3705:                 $datatable .= '<label><input type="radio" name="'.$item.
 3706:                               '" value="'.$auth.'"'.$checked.'/>'.
 3707:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 3708:             }
 3709:         } elsif ($item eq 'timezone_def') {
 3710:             my $includeempty = 1;
 3711:             $datatable .= &Apache::loncommon::select_timezone($item,$domdefaults{$item},undef,$includeempty);
 3712:         } elsif ($item eq 'datelocale_def') {
 3713:             my $includeempty = 1;
 3714:             $datatable .= &Apache::loncommon::select_datelocale($item,$domdefaults{$item},undef,$includeempty);
 3715:         } elsif ($item eq 'lang_def') {
 3716:             my %langchoices = &get_languages_hash();
 3717:             $langchoices{''} = 'No language preference';
 3718:             %langchoices = &Apache::lonlocal::texthash(%langchoices);
 3719:             $datatable .= &Apache::loncommon::select_form($domdefaults{$item},$item,
 3720:                                                           \%langchoices);
 3721:         } else {
 3722:             my $size;
 3723:             if ($item eq 'portal_def') {
 3724:                 $size = ' size="25"';
 3725:             }
 3726:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 3727:                           $domdefaults{$item}.'"'.$size.' />';
 3728:         }
 3729:         $datatable .= '</td></tr>';
 3730:         $rownum ++;
 3731:     }
 3732:     $$rowtotal += $rownum;
 3733:     return $datatable;
 3734: }
 3735: 
 3736: sub get_languages_hash {
 3737:     my %langchoices;
 3738:     foreach my $id (&Apache::loncommon::languageids()) {
 3739:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 3740:         if ($code ne '') {
 3741:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 3742:         }
 3743:     }
 3744:     return %langchoices;
 3745: }
 3746: 
 3747: sub defaults_titles {
 3748:     my ($dom) = @_;
 3749:     my %titles = &Apache::lonlocal::texthash (
 3750:                    'auth_def'      => 'Default authentication type',
 3751:                    'auth_arg_def'  => 'Default authentication argument',
 3752:                    'lang_def'      => 'Default language',
 3753:                    'timezone_def'  => 'Default timezone',
 3754:                    'datelocale_def' => 'Default locale for dates',
 3755:                    'portal_def'     => 'Portal/Default URL',
 3756:                  );
 3757:     if ($dom) {
 3758:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 3759:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 3760:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 3761:         $protocol = 'http' if ($protocol ne 'https');
 3762:         if ($uint_dom) {
 3763:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 3764:                                          $uint_dom);
 3765:         }
 3766:     }
 3767:     return (\%titles);
 3768: }
 3769: 
 3770: sub print_scantronformat {
 3771:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 3772:     my $itemcount = 1;
 3773:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 3774:         %confhash);
 3775:     my $switchserver = &check_switchserver($dom,$confname);
 3776:     my %lt = &Apache::lonlocal::texthash (
 3777:                 default => 'Default bubblesheet format file error',
 3778:                 custom  => 'Custom bubblesheet format file error',
 3779:              );
 3780:     my %scantronfiles = (
 3781:         default => 'default.tab',
 3782:         custom => 'custom.tab',
 3783:     );
 3784:     foreach my $key (keys(%scantronfiles)) {
 3785:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 3786:                               .$scantronfiles{$key};
 3787:     }
 3788:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 3789:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 3790:         if (!$switchserver) {
 3791:             my $servadm = $r->dir_config('lonAdmEMail');
 3792:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 3793:             if ($configuserok eq 'ok') {
 3794:                 if ($author_ok eq 'ok') {
 3795:                     my %legacyfile = (
 3796:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 3797:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 3798:                     );
 3799:                     my %md5chk;
 3800:                     foreach my $type (keys(%legacyfile)) {
 3801:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 3802:                         chomp($md5chk{$type});
 3803:                     }
 3804:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 3805:                         foreach my $type (keys(%legacyfile)) {
 3806:                             ($scantronurls{$type},my $error) = 
 3807:                                 &legacy_scantronformat($r,$dom,$confname,
 3808:                                                  $type,$legacyfile{$type},
 3809:                                                  $scantronurls{$type},
 3810:                                                  $scantronfiles{$type});
 3811:                             if ($error ne '') {
 3812:                                 $error{$type} = $error;
 3813:                             }
 3814:                         }
 3815:                         if (keys(%error) == 0) {
 3816:                             $is_custom = 1;
 3817:                             $confhash{'scantron'}{'scantronformat'} = 
 3818:                                 $scantronurls{'custom'};
 3819:                             my $putresult = 
 3820:                                 &Apache::lonnet::put_dom('configuration',
 3821:                                                          \%confhash,$dom);
 3822:                             if ($putresult ne 'ok') {
 3823:                                 $error{'custom'} = 
 3824:                                     '<span class="LC_error">'.
 3825:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3826:                             }
 3827:                         }
 3828:                     } else {
 3829:                         ($scantronurls{'default'},my $error) =
 3830:                             &legacy_scantronformat($r,$dom,$confname,
 3831:                                           'default',$legacyfile{'default'},
 3832:                                           $scantronurls{'default'},
 3833:                                           $scantronfiles{'default'});
 3834:                         if ($error eq '') {
 3835:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 3836:                             my $putresult =
 3837:                                 &Apache::lonnet::put_dom('configuration',
 3838:                                                          \%confhash,$dom);
 3839:                             if ($putresult ne 'ok') {
 3840:                                 $error{'default'} =
 3841:                                     '<span class="LC_error">'.
 3842:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3843:                             }
 3844:                         } else {
 3845:                             $error{'default'} = $error;
 3846:                         }
 3847:                     }
 3848:                 }
 3849:             }
 3850:         } else {
 3851:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 3852:         }
 3853:     }
 3854:     if (ref($settings) eq 'HASH') {
 3855:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 3856:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 3857:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 3858:                 $scantronurl = '';
 3859:             } else {
 3860:                 $scantronurl = $settings->{'scantronformat'};
 3861:             }
 3862:             $is_custom = 1;
 3863:         } else {
 3864:             $scantronurl = $scantronurls{'default'};
 3865:         }
 3866:     } else {
 3867:         if ($is_custom) {
 3868:             $scantronurl = $scantronurls{'custom'};
 3869:         } else {
 3870:             $scantronurl = $scantronurls{'default'};
 3871:         }
 3872:     }
 3873:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3874:     $datatable .= '<tr'.$css_class.'>';
 3875:     if (!$is_custom) {
 3876:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 3877:                       '<span class="LC_nobreak">';
 3878:         if ($scantronurl) {
 3879:             $datatable .= '<a href="'.$scantronurl.'" target="_blank">'.
 3880:                           &mt('Default bubblesheet format file').'</a>';
 3881:         } else {
 3882:             $datatable = &mt('File unavailable for display');
 3883:         }
 3884:         $datatable .= '</span></td>';
 3885:         if (keys(%error) == 0) { 
 3886:             $datatable .= '<td valign="bottom">';
 3887:             if (!$switchserver) {
 3888:                 $datatable .= &mt('Upload:').'<br />';
 3889:             }
 3890:         } else {
 3891:             my $errorstr;
 3892:             foreach my $key (sort(keys(%error))) {
 3893:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3894:             }
 3895:             $datatable .= '<td>'.$errorstr;
 3896:         }
 3897:     } else {
 3898:         if (keys(%error) > 0) {
 3899:             my $errorstr;
 3900:             foreach my $key (sort(keys(%error))) {
 3901:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3902:             } 
 3903:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 3904:         } elsif ($scantronurl) {
 3905:             $datatable .= '<td><span class="LC_nobreak">'.
 3906:                           '<a href="'.$scantronurl.'" target="_blank">'.
 3907:                           &mt('Custom bubblesheet format file').'</a><label>'.
 3908:                           '<input type="checkbox" name="scantronformat_del"'.
 3909:                           '" value="1" />'.&mt('Delete?').'</label></span></td>'.
 3910:                           '<td><span class="LC_nobreak">&nbsp;'.
 3911:                           &mt('Replace:').'</span><br />';
 3912:         }
 3913:     }
 3914:     if (keys(%error) == 0) {
 3915:         if ($switchserver) {
 3916:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3917:         } else {
 3918:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 3919:                          '<input type="file" name="scantronformat" /></span>';
 3920:         }
 3921:     }
 3922:     $datatable .= '</td></tr>';
 3923:     $$rowtotal ++;
 3924:     return $datatable;
 3925: }
 3926: 
 3927: sub legacy_scantronformat {
 3928:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 3929:     my ($url,$error);
 3930:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 3931:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 3932:         (my $result,$url) =
 3933:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 3934:                          '','',$newfile);
 3935:         if ($result ne 'ok') {
 3936:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 3937:         }
 3938:     }
 3939:     return ($url,$error);
 3940: }
 3941: 
 3942: sub print_coursecategories {
 3943:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 3944:     my $datatable;
 3945:     if ($position eq 'top') {
 3946:         my $toggle_cats_crs = ' ';
 3947:         my $toggle_cats_dom = ' checked="checked" ';
 3948:         my $can_cat_crs = ' ';
 3949:         my $can_cat_dom = ' checked="checked" ';
 3950:         my $toggle_catscomm_comm = ' ';
 3951:         my $toggle_catscomm_dom = ' checked="checked" ';
 3952:         my $can_catcomm_comm = ' ';
 3953:         my $can_catcomm_dom = ' checked="checked" ';
 3954: 
 3955:         if (ref($settings) eq 'HASH') {
 3956:             if ($settings->{'togglecats'} eq 'crs') {
 3957:                 $toggle_cats_crs = $toggle_cats_dom;
 3958:                 $toggle_cats_dom = ' ';
 3959:             }
 3960:             if ($settings->{'categorize'} eq 'crs') {
 3961:                 $can_cat_crs = $can_cat_dom;
 3962:                 $can_cat_dom = ' ';
 3963:             }
 3964:             if ($settings->{'togglecatscomm'} eq 'comm') {
 3965:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 3966:                 $toggle_catscomm_dom = ' ';
 3967:             }
 3968:             if ($settings->{'categorizecomm'} eq 'comm') {
 3969:                 $can_catcomm_comm = $can_catcomm_dom;
 3970:                 $can_catcomm_dom = ' ';
 3971:             }
 3972:         }
 3973:         my %title = &Apache::lonlocal::texthash (
 3974:                      togglecats     => 'Show/Hide a course in catalog',
 3975:                      togglecatscomm => 'Show/Hide a community in catalog',
 3976:                      categorize     => 'Assign a category to a course',
 3977:                      categorizecomm => 'Assign a category to a community',
 3978:                     );
 3979:         my %level = &Apache::lonlocal::texthash (
 3980:                      dom  => 'Set in Domain',
 3981:                      crs  => 'Set in Course',
 3982:                      comm => 'Set in Community',
 3983:                     );
 3984:         $datatable = '<tr class="LC_odd_row">'.
 3985:                   '<td>'.$title{'togglecats'}.'</td>'.
 3986:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3987:                   '<input type="radio" name="togglecats"'.
 3988:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3989:                   '<label><input type="radio" name="togglecats"'.
 3990:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3991:                   '</tr><tr>'.
 3992:                   '<td>'.$title{'categorize'}.'</td>'.
 3993:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3994:                   '<label><input type="radio" name="categorize"'.
 3995:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 3996:                   '<label><input type="radio" name="categorize"'.
 3997:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 3998:                   '</tr><tr class="LC_odd_row">'.
 3999:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 4000:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4001:                   '<input type="radio" name="togglecatscomm"'.
 4002:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4003:                   '<label><input type="radio" name="togglecatscomm"'.
 4004:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4005:                   '</tr><tr>'.
 4006:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 4007:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4008:                   '<label><input type="radio" name="categorizecomm"'.
 4009:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4010:                   '<label><input type="radio" name="categorizecomm"'.
 4011:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4012:                   '</tr>';
 4013:         $$rowtotal += 4;
 4014:     } else {
 4015:         my $css_class;
 4016:         my $itemcount = 1;
 4017:         my $cathash; 
 4018:         if (ref($settings) eq 'HASH') {
 4019:             $cathash = $settings->{'cats'};
 4020:         }
 4021:         if (ref($cathash) eq 'HASH') {
 4022:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4023:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4024:                                                    \%allitems,\%idx,\@jsarray);
 4025:             my $maxdepth = scalar(@cats);
 4026:             my $colattrib = '';
 4027:             if ($maxdepth > 2) {
 4028:                 $colattrib = ' colspan="2" ';
 4029:             }
 4030:             my @path;
 4031:             if (@cats > 0) {
 4032:                 if (ref($cats[0]) eq 'ARRAY') {
 4033:                     my $numtop = @{$cats[0]};
 4034:                     my $maxnum = $numtop;
 4035:                     my %default_names = (
 4036:                           instcode    => &mt('Official courses'),
 4037:                           communities => &mt('Communities'),
 4038:                     );
 4039: 
 4040:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4041:                         ($cathash->{'instcode::0'} eq '') ||
 4042:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4043:                         ($cathash->{'communities::0'} eq '')) {
 4044:                         $maxnum ++;
 4045:                     }
 4046:                     my $lastidx;
 4047:                     for (my $i=0; $i<$numtop; $i++) {
 4048:                         my $parent = $cats[0][$i];
 4049:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4050:                         my $item = &escape($parent).'::0';
 4051:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4052:                         $lastidx = $idx{$item};
 4053:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4054:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4055:                         for (my $k=0; $k<=$maxnum; $k++) {
 4056:                             my $vpos = $k+1;
 4057:                             my $selstr;
 4058:                             if ($k == $i) {
 4059:                                 $selstr = ' selected="selected" ';
 4060:                             }
 4061:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4062:                         }
 4063:                         $datatable .= '</select></td><td>';
 4064:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4065:                             $datatable .=  '<span class="LC_nobreak">'
 4066:                                            .$default_names{$parent}.'</span>';
 4067:                             if ($parent eq 'instcode') {
 4068:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4069:                                               .&mt('with institutional codes')
 4070:                                               .')</span></td><td'.$colattrib.'>';
 4071:                             } else {
 4072:                                 $datatable .= '<table><tr><td>';
 4073:                             }
 4074:                             $datatable .= '<span class="LC_nobreak">'
 4075:                                           .'<label><input type="radio" name="'
 4076:                                           .$parent.'" value="1" checked="checked" />'
 4077:                                           .&mt('Display').'</label>';
 4078:                             if ($parent eq 'instcode') {
 4079:                                 $datatable .= '&nbsp;';
 4080:                             } else {
 4081:                                 $datatable .= '</span></td></tr><tr><td>'
 4082:                                               .'<span class="LC_nobreak">';
 4083:                             }
 4084:                             $datatable .= '<label><input type="radio" name="'
 4085:                                           .$parent.'" value="0" />'
 4086:                                           .&mt('Do not display').'</label></span>';
 4087:                             if ($parent eq 'communities') {
 4088:                                 $datatable .= '</td></tr></table>';
 4089:                             }
 4090:                             $datatable .= '</td>';
 4091:                         } else {
 4092:                             $datatable .= $parent
 4093:                                           .'&nbsp;<label><input type="checkbox" name="deletecategory" '
 4094:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 4095:                         }
 4096:                         my $depth = 1;
 4097:                         push(@path,$parent);
 4098:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 4099:                         pop(@path);
 4100:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 4101:                         $itemcount ++;
 4102:                     }
 4103:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4104:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 4105:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 4106:                     for (my $k=0; $k<=$maxnum; $k++) {
 4107:                         my $vpos = $k+1;
 4108:                         my $selstr;
 4109:                         if ($k == $numtop) {
 4110:                             $selstr = ' selected="selected" ';
 4111:                         }
 4112:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4113:                     }
 4114:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 4115:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 4116:                                   .'</tr>'."\n";
 4117:                     $itemcount ++;
 4118:                     foreach my $default ('instcode','communities') {
 4119:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 4120:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4121:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 4122:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 4123:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 4124:                             for (my $k=0; $k<=$maxnum; $k++) {
 4125:                                 my $vpos = $k+1;
 4126:                                 my $selstr;
 4127:                                 if ($k == $maxnum) {
 4128:                                     $selstr = ' selected="selected" ';
 4129:                                 }
 4130:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4131:                             }
 4132:                             $datatable .= '</select></span></td>'.
 4133:                                           '<td><span class="LC_nobreak">'.
 4134:                                           $default_names{$default}.'</span>';
 4135:                             if ($default eq 'instcode') {
 4136:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 4137:                                               .&mt('with institutional codes').')</span>';
 4138:                             }
 4139:                             $datatable .= '</td>'
 4140:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 4141:                                           .&mt('Display').'</label>&nbsp;'
 4142:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 4143:                                           .&mt('Do not display').'</label></span></td></tr>';
 4144:                         }
 4145:                     }
 4146:                 }
 4147:             } else {
 4148:                 $datatable .= &initialize_categories($itemcount);
 4149:             }
 4150:         } else {
 4151:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 4152:                           .&initialize_categories($itemcount);
 4153:         }
 4154:         $$rowtotal += $itemcount;
 4155:     }
 4156:     return $datatable;
 4157: }
 4158: 
 4159: sub print_serverstatuses {
 4160:     my ($dom,$settings,$rowtotal) = @_;
 4161:     my $datatable;
 4162:     my @pages = &serverstatus_pages();
 4163:     my (%namedaccess,%machineaccess);
 4164:     foreach my $type (@pages) {
 4165:         $namedaccess{$type} = '';
 4166:         $machineaccess{$type}= '';
 4167:     }
 4168:     if (ref($settings) eq 'HASH') {
 4169:         foreach my $type (@pages) {
 4170:             if (exists($settings->{$type})) {
 4171:                 if (ref($settings->{$type}) eq 'HASH') {
 4172:                     foreach my $key (keys(%{$settings->{$type}})) {
 4173:                         if ($key eq 'namedusers') {
 4174:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4175:                         } elsif ($key eq 'machines') {
 4176:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4177:                         }
 4178:                     }
 4179:                 }
 4180:             }
 4181:         }
 4182:     }
 4183:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4184:     my $rownum = 0;
 4185:     my $css_class;
 4186:     foreach my $type (@pages) {
 4187:         $rownum ++;
 4188:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4189:         $datatable .= '<tr'.$css_class.'>'.
 4190:                       '<td><span class="LC_nobreak">'.
 4191:                       $titles->{$type}.'</span></td>'.
 4192:                       '<td class="LC_left_item">'.
 4193:                       '<input type="text" name="'.$type.'_namedusers" '.
 4194:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4195:                       '<td class="LC_right_item">'.
 4196:                       '<span class="LC_nobreak">'.
 4197:                       '<input type="text" name="'.$type.'_machines" '.
 4198:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4199:                       '</td></tr>'."\n";
 4200:     }
 4201:     $$rowtotal += $rownum;
 4202:     return $datatable;
 4203: }
 4204: 
 4205: sub serverstatus_pages {
 4206:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4207:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 4208:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf');
 4209: }
 4210: 
 4211: sub coursecategories_javascript {
 4212:     my ($settings) = @_;
 4213:     my ($output,$jstext,$cathash);
 4214:     if (ref($settings) eq 'HASH') {
 4215:         $cathash = $settings->{'cats'};
 4216:     }
 4217:     if (ref($cathash) eq 'HASH') {
 4218:         my (@cats,@jsarray,%idx);
 4219:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4220:         if (@jsarray > 0) {
 4221:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4222:             for (my $i=0; $i<@jsarray; $i++) {
 4223:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4224:                     my $catstr = join('","',@{$jsarray[$i]});
 4225:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4226:                 }
 4227:             }
 4228:         }
 4229:     } else {
 4230:         $jstext  = '    var categories = Array(1);'."\n".
 4231:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4232:     }
 4233:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4234:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4235:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4236:     $output = <<"ENDSCRIPT";
 4237: <script type="text/javascript">
 4238: // <![CDATA[
 4239: function reorderCats(form,parent,item,idx) {
 4240:     var changedVal;
 4241: $jstext
 4242:     var newpos = 'addcategory_pos';
 4243:     var current = new Array;
 4244:     if (parent == '') {
 4245:         var has_instcode = 0;
 4246:         var maxtop = categories[idx].length;
 4247:         for (var j=0; j<maxtop; j++) {
 4248:             if (categories[idx][j] == 'instcode::0') {
 4249:                 has_instcode == 1;
 4250:             }
 4251:         }
 4252:         if (has_instcode == 0) {
 4253:             categories[idx][maxtop] = 'instcode_pos';
 4254:         }
 4255:     } else {
 4256:         newpos += '_'+parent;
 4257:     }
 4258:     var maxh = 1 + categories[idx].length;
 4259:     var current = new Array;
 4260:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4261:     if (item == newpos) {
 4262:         changedVal = newitemVal;
 4263:     } else {
 4264:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4265:         current[newitemVal] = newpos;
 4266:     }
 4267:     for (var i=0; i<categories[idx].length; i++) {
 4268:         var elementName = categories[idx][i];
 4269:         if (elementName != item) {
 4270:             if (form.elements[elementName]) {
 4271:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4272:                 current[currVal] = elementName;
 4273:             }
 4274:         }
 4275:     }
 4276:     var oldVal;
 4277:     for (var j=0; j<maxh; j++) {
 4278:         if (current[j] == undefined) {
 4279:             oldVal = j;
 4280:         }
 4281:     }
 4282:     if (oldVal < changedVal) {
 4283:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4284:            var elementName = current[k];
 4285:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4286:         }
 4287:     } else {
 4288:         for (var k=changedVal; k<oldVal; k++) {
 4289:             var elementName = current[k];
 4290:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4291:         }
 4292:     }
 4293:     return;
 4294: }
 4295: 
 4296: function categoryCheck(form) {
 4297:     if (form.elements['addcategory_name'].value == 'instcode') {
 4298:         alert('$instcode_reserved\\n$choose_again');
 4299:         return false;
 4300:     }
 4301:     if (form.elements['addcategory_name'].value == 'communities') {
 4302:         alert('$communities_reserved\\n$choose_again');
 4303:         return false;
 4304:     }
 4305:     return true;
 4306: }
 4307: 
 4308: // ]]>
 4309: </script>
 4310: 
 4311: ENDSCRIPT
 4312:     return $output;
 4313: }
 4314: 
 4315: sub initialize_categories {
 4316:     my ($itemcount) = @_;
 4317:     my ($datatable,$css_class,$chgstr);
 4318:     my %default_names = (
 4319:                       instcode    => 'Official courses (with institutional codes)',
 4320:                       communities => 'Communities',
 4321:                         );
 4322:     my $select0 = ' selected="selected"';
 4323:     my $select1 = '';
 4324:     foreach my $default ('instcode','communities') {
 4325:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4326:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4327:         if ($default eq 'communities') {
 4328:             $select1 = $select0;
 4329:             $select0 = '';
 4330:         }
 4331:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4332:                      .'<select name="'.$default.'_pos">'
 4333:                      .'<option value="0"'.$select0.'>1</option>'
 4334:                      .'<option value="1"'.$select1.'>2</option>'
 4335:                      .'<option value="2">3</option></select>&nbsp;'
 4336:                      .$default_names{$default}
 4337:                      .'</span></td><td><span class="LC_nobreak">'
 4338:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4339:                      .&mt('Display').'</label>&nbsp;<label>'
 4340:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4341:                  .'</label></span></td></tr>';
 4342:         $itemcount ++;
 4343:     }
 4344:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4345:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4346:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4347:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4348:                   .'<option value="0">1</option>'
 4349:                   .'<option value="1">2</option>'
 4350:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4351:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4352:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4353:     return $datatable;
 4354: }
 4355: 
 4356: sub build_category_rows {
 4357:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 4358:     my ($text,$name,$item,$chgstr);
 4359:     if (ref($cats) eq 'ARRAY') {
 4360:         my $maxdepth = scalar(@{$cats});
 4361:         if (ref($cats->[$depth]) eq 'HASH') {
 4362:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 4363:                 my $numchildren = @{$cats->[$depth]{$parent}};
 4364:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4365:                 $text .= '<td><table class="LC_datatable">';
 4366:                 my ($idxnum,$parent_name,$parent_item);
 4367:                 my $higher = $depth - 1;
 4368:                 if ($higher == 0) {
 4369:                     $parent_name = &escape($parent).'::'.$higher;
 4370:                 } else {
 4371:                     if (ref($path) eq 'ARRAY') {
 4372:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4373:                     }
 4374:                 }
 4375:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4376:                 for (my $j=0; $j<=$numchildren; $j++) {
 4377:                     if ($j < $numchildren) {
 4378:                         $name = $cats->[$depth]{$parent}[$j];
 4379:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4380:                         $idxnum = $idx->{$item};
 4381:                     } else {
 4382:                         $name = $parent_name;
 4383:                         $item = $parent_item;
 4384:                     }
 4385:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4386:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4387:                     for (my $i=0; $i<=$numchildren; $i++) {
 4388:                         my $vpos = $i+1;
 4389:                         my $selstr;
 4390:                         if ($j == $i) {
 4391:                             $selstr = ' selected="selected" ';
 4392:                         }
 4393:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4394:                     }
 4395:                     $text .= '</select>&nbsp;';
 4396:                     if ($j < $numchildren) {
 4397:                         my $deeper = $depth+1;
 4398:                         $text .= $name.'&nbsp;'
 4399:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4400:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4401:                         if(ref($path) eq 'ARRAY') {
 4402:                             push(@{$path},$name);
 4403:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4404:                             pop(@{$path});
 4405:                         }
 4406:                     } else {
 4407:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 4408:                         if ($j == $numchildren) {
 4409:                             $text .= $name;
 4410:                         } else {
 4411:                             $text .= $item;
 4412:                         }
 4413:                         $text .= '" value="" />';
 4414:                     }
 4415:                     $text .= '</td></tr>';
 4416:                 }
 4417:                 $text .= '</table></td>';
 4418:             } else {
 4419:                 my $higher = $depth-1;
 4420:                 if ($higher == 0) {
 4421:                     $name = &escape($parent).'::'.$higher;
 4422:                 } else {
 4423:                     if (ref($path) eq 'ARRAY') {
 4424:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4425:                     }
 4426:                 }
 4427:                 my $colspan;
 4428:                 if ($parent ne 'instcode') {
 4429:                     $colspan = $maxdepth - $depth - 1;
 4430:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 4431:                 }
 4432:             }
 4433:         }
 4434:     }
 4435:     return $text;
 4436: }
 4437: 
 4438: sub modifiable_userdata_row {
 4439:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes) = @_;
 4440:     my $rolename;
 4441:     if ($context eq 'selfcreate') {
 4442:         if (ref($usertypes) eq 'HASH') {
 4443:             $rolename = $usertypes->{$role};
 4444:         } else {
 4445:             $rolename = $role;
 4446:         }
 4447:     } else {
 4448:         if ($role eq 'cr') {
 4449:             $rolename = &mt('Custom role');
 4450:         } else {
 4451:             $rolename = &Apache::lonnet::plaintext($role);
 4452:         }
 4453:     }
 4454:     my @fields = ('lastname','firstname','middlename','generation',
 4455:                   'permanentemail','id');
 4456:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4457:     my $output;
 4458:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4459:     $output = '<tr '.$css_class.'>'.
 4460:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 4461:               '<td class="LC_left_item" colspan="2"><table>';
 4462:     my $rem;
 4463:     my %checks;
 4464:     if (ref($settings) eq 'HASH') {
 4465:         if (ref($settings->{$context}) eq 'HASH') {
 4466:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 4467:                 foreach my $field (@fields) {
 4468:                     if ($settings->{$context}->{$role}->{$field}) {
 4469:                         $checks{$field} = ' checked="checked" ';
 4470:                     }
 4471:                 }
 4472:             }
 4473:         }
 4474:     }
 4475:     for (my $i=0; $i<@fields; $i++) {
 4476:         my $rem = $i%($numinrow);
 4477:         if ($rem == 0) {
 4478:             if ($i > 0) {
 4479:                 $output .= '</tr>';
 4480:             }
 4481:             $output .= '<tr>';
 4482:         }
 4483:         my $check = ' ';
 4484:         if (exists($checks{$fields[$i]})) {
 4485:             $check = $checks{$fields[$i]}
 4486:         } else {
 4487:             if ($role eq 'st') {
 4488:                 if (ref($settings) ne 'HASH') {
 4489:                     $check = ' checked="checked" '; 
 4490:                 }
 4491:             }
 4492:         }
 4493:         $output .= '<td class="LC_left_item">'.
 4494:                    '<span class="LC_nobreak"><label>'.
 4495:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 4496:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 4497:                    '</label></span></td>';
 4498:         $rem = @fields%($numinrow);
 4499:     }
 4500:     my $colsleft = $numinrow - $rem;
 4501:     if ($colsleft > 1 ) {
 4502:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4503:                    '&nbsp;</td>';
 4504:     } elsif ($colsleft == 1) {
 4505:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4506:     }
 4507:     $output .= '</tr></table></td></tr>';
 4508:     return $output;
 4509: }
 4510: 
 4511: sub insttypes_row {
 4512:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context) = @_;
 4513:     my %lt = &Apache::lonlocal::texthash (
 4514:                       cansearch => 'Users allowed to search',
 4515:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 4516:                       lockablenames => 'User preference to lock name',
 4517:              );
 4518:     my $showdom;
 4519:     if ($context eq 'cansearch') {
 4520:         $showdom = ' ('.$dom.')';
 4521:     }
 4522:     my $class = 'LC_left_item';
 4523:     if ($context eq 'statustocreate') {
 4524:         $class = 'LC_right_item';
 4525:     }
 4526:     my $output =  '<tr class="LC_odd_row">'.
 4527:                   '<td>'.$lt{$context}.$showdom.
 4528:                   '</td><td class="'.$class.'" colspan="2"><table>';
 4529:     my $rem;
 4530:     if (ref($types) eq 'ARRAY') {
 4531:         for (my $i=0; $i<@{$types}; $i++) {
 4532:             if (defined($usertypes->{$types->[$i]})) {
 4533:                 my $rem = $i%($numinrow);
 4534:                 if ($rem == 0) {
 4535:                     if ($i > 0) {
 4536:                         $output .= '</tr>';
 4537:                     }
 4538:                     $output .= '<tr>';
 4539:                 }
 4540:                 my $check = ' ';
 4541:                 if (ref($settings) eq 'HASH') {
 4542:                     if (ref($settings->{$context}) eq 'ARRAY') {
 4543:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 4544:                             $check = ' checked="checked" ';
 4545:                         }
 4546:                     } elsif ($context eq 'statustocreate') {
 4547:                         $check = ' checked="checked" ';
 4548:                     }
 4549:                 }
 4550:                 $output .= '<td class="LC_left_item">'.
 4551:                            '<span class="LC_nobreak"><label>'.
 4552:                            '<input type="checkbox" name="'.$context.'" '.
 4553:                            'value="'.$types->[$i].'"'.$check.'/>'.
 4554:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 4555:             }
 4556:         }
 4557:         $rem = @{$types}%($numinrow);
 4558:     }
 4559:     my $colsleft = $numinrow - $rem;
 4560:     if (($rem == 0) && (@{$types} > 0)) {
 4561:         $output .= '<tr>';
 4562:     }
 4563:     if ($colsleft > 1) {
 4564:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 4565:     } else {
 4566:         $output .= '<td class="LC_left_item">';
 4567:     }
 4568:     my $defcheck = ' ';
 4569:     if (ref($settings) eq 'HASH') {  
 4570:         if (ref($settings->{$context}) eq 'ARRAY') {
 4571:             if (grep(/^default$/,@{$settings->{$context}})) {
 4572:                 $defcheck = ' checked="checked" ';
 4573:             }
 4574:         } elsif ($context eq 'statustocreate') {
 4575:             $defcheck = ' checked="checked" ';
 4576:         }
 4577:     }
 4578:     $output .= '<span class="LC_nobreak"><label>'.
 4579:                '<input type="checkbox" name="'.$context.'" '.
 4580:                'value="default"'.$defcheck.'/>'.
 4581:                $othertitle.'</label></span></td>'.
 4582:                '</tr></table></td></tr>';
 4583:     return $output;
 4584: }
 4585: 
 4586: sub sorted_searchtitles {
 4587:     my %searchtitles = &Apache::lonlocal::texthash(
 4588:                          'uname' => 'username',
 4589:                          'lastname' => 'last name',
 4590:                          'lastfirst' => 'last name, first name',
 4591:                      );
 4592:     my @titleorder = ('uname','lastname','lastfirst');
 4593:     return (\%searchtitles,\@titleorder);
 4594: }
 4595: 
 4596: sub sorted_searchtypes {
 4597:     my %srchtypes_desc = (
 4598:                            exact    => 'is exact match',
 4599:                            contains => 'contains ..',
 4600:                            begins   => 'begins with ..',
 4601:                          );
 4602:     my @srchtypeorder = ('exact','begins','contains');
 4603:     return (\%srchtypes_desc,\@srchtypeorder);
 4604: }
 4605: 
 4606: sub usertype_update_row {
 4607:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 4608:     my $datatable;
 4609:     my $numinrow = 4;
 4610:     foreach my $type (@{$types}) {
 4611:         if (defined($usertypes->{$type})) {
 4612:             $$rownums ++;
 4613:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 4614:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 4615:                           '</td><td class="LC_left_item"><table>';
 4616:             for (my $i=0; $i<@{$fields}; $i++) {
 4617:                 my $rem = $i%($numinrow);
 4618:                 if ($rem == 0) {
 4619:                     if ($i > 0) {
 4620:                         $datatable .= '</tr>';
 4621:                     }
 4622:                     $datatable .= '<tr>';
 4623:                 }
 4624:                 my $check = ' ';
 4625:                 if (ref($settings) eq 'HASH') {
 4626:                     if (ref($settings->{'fields'}) eq 'HASH') {
 4627:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 4628:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 4629:                                 $check = ' checked="checked" ';
 4630:                             }
 4631:                         }
 4632:                     }
 4633:                 }
 4634: 
 4635:                 if ($i == @{$fields}-1) {
 4636:                     my $colsleft = $numinrow - $rem;
 4637:                     if ($colsleft > 1) {
 4638:                         $datatable .= '<td colspan="'.$colsleft.'">';
 4639:                     } else {
 4640:                         $datatable .= '<td>';
 4641:                     }
 4642:                 } else {
 4643:                     $datatable .= '<td>';
 4644:                 }
 4645:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4646:                               '<input type="checkbox" name="updateable_'.$type.
 4647:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 4648:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 4649:             }
 4650:             $datatable .= '</tr></table></td></tr>';
 4651:         }
 4652:     }
 4653:     return $datatable;
 4654: }
 4655: 
 4656: sub modify_login {
 4657:     my ($r,$dom,$confname,%domconfig) = @_;
 4658:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 4659:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 4660:     %title = ( coursecatalog => 'Display course catalog',
 4661:                adminmail => 'Display administrator E-mail address',
 4662:                helpdesk  => 'Display "Contact Helpdesk" link',
 4663:                newuser => 'Link for visitors to create a user account',
 4664:                loginheader => 'Log-in box header');
 4665:     @offon = ('off','on');
 4666:     if (ref($domconfig{login}) eq 'HASH') {
 4667:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 4668:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 4669:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 4670:             }
 4671:         }
 4672:     }
 4673:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 4674:                                            \%domconfig,\%loginhash);
 4675:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 4676:     foreach my $item (@toggles) {
 4677:         $loginhash{login}{$item} = $env{'form.'.$item};
 4678:     }
 4679:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 4680:     if (ref($colchanges{'login'}) eq 'HASH') {  
 4681:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 4682:                                          \%loginhash);
 4683:     }
 4684: 
 4685:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4686:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 4687:     if (keys(%servers) > 1) {
 4688:         foreach my $lonhost (keys(%servers)) {
 4689:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 4690:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 4691:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 4692:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 4693:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 4694:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4695:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4696:                         $changes{'loginvia'}{$lonhost} = 1;
 4697:                     } else {
 4698:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 4699:                         $changes{'loginvia'}{$lonhost} = 1;
 4700:                     }
 4701:                 } else {
 4702:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4703:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4704:                         $changes{'loginvia'}{$lonhost} = 1;
 4705:                     }
 4706:                 }
 4707:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 4708:                     foreach my $item (@loginvia_attribs) {
 4709:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 4710:                     }
 4711:                 } else {
 4712:                     foreach my $item (@loginvia_attribs) {
 4713:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4714:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4715:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 4716:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4717:                                 $new = '/';
 4718:                             }
 4719:                         }
 4720:                         if (($item eq 'custompath') && 
 4721:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4722:                             $new = '';
 4723:                         }
 4724:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 4725:                             $changes{'loginvia'}{$lonhost} = 1;
 4726:                         }
 4727:                         if ($item eq 'exempt') {
 4728:                             $new =~ s/^\s+//;
 4729:                             $new =~ s/\s+$//;
 4730:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 4731:                             my @okips;
 4732:                             foreach my $ip (@poss_ips) {
 4733:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 4734:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 4735:                                         push(@okips,$ip); 
 4736:                                     }
 4737:                                 }
 4738:                             }
 4739:                             if (@okips > 0) {
 4740:                                 $new = join(',',@okips); 
 4741:                             } else {
 4742:                                 $new = ''; 
 4743:                             }
 4744:                         }
 4745:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4746:                     }
 4747:                 }
 4748:             } else {
 4749:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4750:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4751:                     $changes{'loginvia'}{$lonhost} = 1;
 4752:                     foreach my $item (@loginvia_attribs) {
 4753:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4754:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4755:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4756:                                 $new = '/';
 4757:                             }
 4758:                         }
 4759:                         if (($item eq 'custompath') && 
 4760:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4761:                             $new = '';
 4762:                         }
 4763:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4764:                     }
 4765:                 }
 4766:             }
 4767:         }
 4768:     }
 4769: 
 4770:     my $servadm = $r->dir_config('lonAdmEMail');
 4771:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 4772:     if (ref($domconfig{'login'}) eq 'HASH') {
 4773:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 4774:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 4775:                 if ($lang eq 'nolang') {
 4776:                     push(@currlangs,$lang);
 4777:                 } elsif (defined($langchoices{$lang})) {
 4778:                     push(@currlangs,$lang);
 4779:                 } else {
 4780:                     next;
 4781:                 }
 4782:             }
 4783:         }
 4784:     }
 4785:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 4786:     if (@currlangs > 0) {
 4787:         foreach my $lang (@currlangs) {
 4788:             if (grep(/^\Q$lang\E$/,@delurls)) {
 4789:                 $changes{'helpurl'}{$lang} = 1;
 4790:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 4791:                 $changes{'helpurl'}{$lang} = 1;
 4792:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 4793:                 push(@newlangs,$lang);
 4794:             } else {
 4795:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4796:             }
 4797:         }
 4798:     }
 4799:     unless (grep(/^nolang$/,@currlangs)) {
 4800:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 4801:             $changes{'helpurl'}{'nolang'} = 1;
 4802:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 4803:             push(@newlangs,'nolang');
 4804:         }
 4805:     }
 4806:     if ($env{'form.loginhelpurl_add_lang'}) {
 4807:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 4808:             ($env{'form.loginhelpurl_add_file.filename'})) {
 4809:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 4810:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 4811:         }
 4812:     }
 4813:     if ((@newlangs > 0) || ($addedfile)) {
 4814:         my $error;
 4815:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 4816:         if ($configuserok eq 'ok') {
 4817:             if ($switchserver) {
 4818:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 4819:             } elsif ($author_ok eq 'ok') {
 4820:                 my @allnew = @newlangs;
 4821:                 if ($addedfile ne '') {
 4822:                     push(@allnew,$addedfile);
 4823:                 }
 4824:                 foreach my $lang (@allnew) {
 4825:                     my $formelem = 'loginhelpurl_'.$lang;
 4826:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 4827:                         $formelem = 'loginhelpurl_add_file';
 4828:                     }
 4829:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 4830:                                                                "help/$lang",'','',$newfile{$lang});
 4831:                     if ($result eq 'ok') {
 4832:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 4833:                         $changes{'helpurl'}{$lang} = 1;
 4834:                     } else {
 4835:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 4836:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 4837:                         if ((grep(/^\Q$lang\E$/,@currlangs)) && 
 4838:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 4839: 
 4840:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4841:                         }
 4842:                     }
 4843:                 }
 4844:             } else {
 4845:                 $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);
 4846:             }
 4847:         } else {
 4848:             $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);
 4849:         }
 4850:         if ($error) {
 4851:             &Apache::lonnet::logthis($error);
 4852:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 4853:         }
 4854:     }
 4855:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 4856: 
 4857:     my $defaulthelpfile = '/adm/loginproblems.html';
 4858:     my $defaulttext = &mt('Default in use');
 4859: 
 4860:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 4861:                                              $dom);
 4862:     if ($putresult eq 'ok') {
 4863:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 4864:         my %defaultchecked = (
 4865:                     'coursecatalog' => 'on',
 4866:                     'helpdesk'      => 'on',
 4867:                     'adminmail'     => 'off',
 4868:                     'newuser'       => 'off',
 4869:         );
 4870:         if (ref($domconfig{'login'}) eq 'HASH') {
 4871:             foreach my $item (@toggles) {
 4872:                 if ($defaultchecked{$item} eq 'on') { 
 4873:                     if (($domconfig{'login'}{$item} eq '0') &&
 4874:                         ($env{'form.'.$item} eq '1')) {
 4875:                         $changes{$item} = 1;
 4876:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4877:                               $domconfig{'login'}{$item} eq '1') &&
 4878:                              ($env{'form.'.$item} eq '0')) {
 4879:                         $changes{$item} = 1;
 4880:                     }
 4881:                 } elsif ($defaultchecked{$item} eq 'off') {
 4882:                     if (($domconfig{'login'}{$item} eq '1') &&
 4883:                         ($env{'form.'.$item} eq '0')) {
 4884:                         $changes{$item} = 1;
 4885:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4886:                               $domconfig{'login'}{$item} eq '0') &&
 4887:                              ($env{'form.'.$item} eq '1')) {
 4888:                         $changes{$item} = 1;
 4889:                     }
 4890:                 }
 4891:             }
 4892:         }
 4893:         if (keys(%changes) > 0 || $colchgtext) {
 4894:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4895:             $resulttext = &mt('Changes made:').'<ul>';
 4896:             foreach my $item (sort(keys(%changes))) {
 4897:                 if ($item eq 'loginvia') {
 4898:                     if (ref($changes{$item}) eq 'HASH') {
 4899:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 4900:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 4901:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 4902:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 4903:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 4904:                                     $protocol = 'http' if ($protocol ne 'https');
 4905:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 4906: 
 4907:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 4908:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 4909:                                     } else {
 4910:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 4911:                                     }
 4912:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 4913:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 4914:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 4915:                                     }
 4916:                                     $resulttext .= '</li>';
 4917:                                 } else {
 4918:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 4919:                                 }
 4920:                             } else {
 4921:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 4922:                             }
 4923:                         }
 4924:                         $resulttext .= '</ul></li>';
 4925:                     }
 4926:                 } elsif ($item eq 'helpurl') {
 4927:                     if (ref($changes{$item}) eq 'HASH') {
 4928:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 4929:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 4930:                                 my ($chg,$link);
 4931:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 4932:                                 if ($lang eq 'nolang') {
 4933:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 4934:                                 } else {
 4935:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 4936:                                 }
 4937:                                 $resulttext .= '<li>'.$chg.'</li>';
 4938:                             } else {
 4939:                                 my $chg;
 4940:                                 if ($lang eq 'nolang') {
 4941:                                     $chg = &mt('custom log-in help file for no preferred language');
 4942:                                 } else {
 4943:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 4944:                                 }
 4945:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 4946:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 4947:                                                       '?inhibitmenu=yes',$chg,600,500).
 4948:                                                '</li>';
 4949:                             }
 4950:                         }
 4951:                     }
 4952:                 } elsif ($item eq 'captcha') {
 4953:                     if (ref($loginhash{'login'}) eq 'HASH') {
 4954:                         my $chgtxt; 
 4955:                         if ($loginhash{'login'}{$item} eq 'notused') {
 4956:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 4957:                         } else {
 4958:                             my %captchas = &captcha_phrases();
 4959:                             if ($captchas{$loginhash{'login'}{$item}}) {
 4960:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 4961:                             } else {
 4962:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 4963:                             }
 4964:                         }
 4965:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 4966:                     }
 4967:                 } elsif ($item eq 'recaptchakeys') {
 4968:                     if (ref($loginhash{'login'}) eq 'HASH') {
 4969:                         my ($privkey,$pubkey);
 4970:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 4971:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 4972:                             $privkey = $loginhash{'login'}{$item}{'private'};
 4973:                         }
 4974:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 4975:                         if (!$pubkey) {
 4976:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 4977:                         } else {
 4978:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 4979:                         }
 4980:                         if (!$privkey) {
 4981:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 4982:                         } else {
 4983:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 4984:                         }
 4985:                         $chgtxt .= '</ul>';
 4986:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 4987:                     }
 4988:                 } else {
 4989:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 4990:                 }
 4991:             }
 4992:             $resulttext .= $colchgtext.'</ul>';
 4993:         } else {
 4994:             $resulttext = &mt('No changes made to log-in page settings');
 4995:         }
 4996:     } else {
 4997:         $resulttext = '<span class="LC_error">'.
 4998: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4999:     }
 5000:     if ($errors) {
 5001:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 5002:                        $errors.'</ul>';
 5003:     }
 5004:     return $resulttext;
 5005: }
 5006: 
 5007: sub color_font_choices {
 5008:     my %choices =
 5009:         &Apache::lonlocal::texthash (
 5010:             img => "Header",
 5011:             bgs => "Background colors",
 5012:             links => "Link colors",
 5013:             images => "Images",
 5014:             font => "Font color",
 5015:             fontmenu => "Font Menu",
 5016:             pgbg => "Page",
 5017:             tabbg => "Header",
 5018:             sidebg => "Border",
 5019:             link => "Link",
 5020:             alink => "Active link",
 5021:             vlink => "Visited link",
 5022:         );
 5023:     return %choices;
 5024: }
 5025: 
 5026: sub modify_rolecolors {
 5027:     my ($r,$dom,$confname,$roles,%domconfig) = @_;
 5028:     my ($resulttext,%rolehash);
 5029:     $rolehash{'rolecolors'} = {};
 5030:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 5031:         if ($domconfig{'rolecolors'} eq '') {
 5032:             $domconfig{'rolecolors'} = {};
 5033:         }
 5034:     }
 5035:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 5036:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 5037:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 5038:                                              $dom);
 5039:     if ($putresult eq 'ok') {
 5040:         if (keys(%changes) > 0) {
 5041:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5042:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 5043:                                              $rolehash{'rolecolors'});
 5044:         } else {
 5045:             $resulttext = &mt('No changes made to default color schemes');
 5046:         }
 5047:     } else {
 5048:         $resulttext = '<span class="LC_error">'.
 5049: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5050:     }
 5051:     if ($errors) {
 5052:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 5053:                        $errors.'</ul>';
 5054:     }
 5055:     return $resulttext;
 5056: }
 5057: 
 5058: sub modify_colors {
 5059:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 5060:     my (%changes,%choices);
 5061:     my @bgs;
 5062:     my @links = ('link','alink','vlink');
 5063:     my @logintext;
 5064:     my @images;
 5065:     my $servadm = $r->dir_config('lonAdmEMail');
 5066:     my $errors;
 5067:     foreach my $role (@{$roles}) {
 5068:         if ($role eq 'login') {
 5069:             %choices = &login_choices();
 5070:             @logintext = ('textcol','bgcol');
 5071:         } else {
 5072:             %choices = &color_font_choices();
 5073:             $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 5074:         }
 5075:         if ($role eq 'login') {
 5076:             @images = ('img','logo','domlogo','login');
 5077:             @bgs = ('pgbg','mainbg','sidebg');
 5078:         } else {
 5079:             @images = ('img');
 5080:             @bgs = ('pgbg','tabbg','sidebg'); 
 5081:         }
 5082:         $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 5083:         foreach my $item (@bgs,@links,@logintext) {
 5084:             $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5085:         }
 5086:         my ($configuserok,$author_ok,$switchserver) = 
 5087:             &config_check($dom,$confname,$servadm);
 5088:         my ($width,$height) = &thumb_dimensions();
 5089:         if (ref($domconfig->{$role}) ne 'HASH') {
 5090:             $domconfig->{$role} = {};
 5091:         }
 5092:         foreach my $img (@images) {
 5093:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 5094:                 if (defined($env{'form.login_showlogo_'.$img})) {
 5095:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 5096:                 } else { 
 5097:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 5098:                 }
 5099:             } 
 5100: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 5101: 		 && !defined($domconfig->{$role}{$img})
 5102: 		 && !$env{'form.'.$role.'_del_'.$img}
 5103: 		 && $env{'form.'.$role.'_import_'.$img}) {
 5104: 		# import the old configured image from the .tab setting
 5105: 		# if they haven't provided a new one 
 5106: 		$domconfig->{$role}{$img} = 
 5107: 		    $env{'form.'.$role.'_import_'.$img};
 5108: 	    }
 5109:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 5110:                 my $error;
 5111:                 if ($configuserok eq 'ok') {
 5112:                     if ($switchserver) {
 5113:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 5114:                     } else {
 5115:                         if ($author_ok eq 'ok') {
 5116:                             my ($result,$logourl) = 
 5117:                                 &publishlogo($r,'upload',$role.'_'.$img,
 5118:                                            $dom,$confname,$img,$width,$height);
 5119:                             if ($result eq 'ok') {
 5120:                                 $confhash->{$role}{$img} = $logourl;
 5121:                                 $changes{$role}{'images'}{$img} = 1;
 5122:                             } else {
 5123:                                 $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);
 5124:                             }
 5125:                         } else {
 5126:                             $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);
 5127:                         }
 5128:                     }
 5129:                 } else {
 5130:                     $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);
 5131:                 }
 5132:                 if ($error) {
 5133:                     &Apache::lonnet::logthis($error);
 5134:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5135:                 }
 5136:             } elsif ($domconfig->{$role}{$img} ne '') {
 5137:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 5138:                     my $error;
 5139:                     if ($configuserok eq 'ok') {
 5140: # is confname an author?
 5141:                         if ($switchserver eq '') {
 5142:                             if ($author_ok eq 'ok') {
 5143:                                 my ($result,$logourl) = 
 5144:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 5145:                                             $dom,$confname,$img,$width,$height);
 5146:                                 if ($result eq 'ok') {
 5147:                                     $confhash->{$role}{$img} = $logourl;
 5148: 				    $changes{$role}{'images'}{$img} = 1;
 5149:                                 }
 5150:                             }
 5151:                         }
 5152:                     }
 5153:                 }
 5154:             }
 5155:         }
 5156:         if (ref($domconfig) eq 'HASH') {
 5157:             if (ref($domconfig->{$role}) eq 'HASH') {
 5158:                 foreach my $img (@images) {
 5159:                     if ($domconfig->{$role}{$img} ne '') {
 5160:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5161:                             $confhash->{$role}{$img} = '';
 5162:                             $changes{$role}{'images'}{$img} = 1;
 5163:                         } else {
 5164:                             if ($confhash->{$role}{$img} eq '') {
 5165:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 5166:                             }
 5167:                         }
 5168:                     } else {
 5169:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5170:                             $confhash->{$role}{$img} = '';
 5171:                             $changes{$role}{'images'}{$img} = 1;
 5172:                         } 
 5173:                     }
 5174:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 5175:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 5176:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 5177:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 5178:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 5179:                             }
 5180:                         } else {
 5181:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5182:                                 $changes{$role}{'showlogo'}{$img} = 1;
 5183:                             }
 5184:                         }
 5185:                     }
 5186:                 }
 5187:                 if ($domconfig->{$role}{'font'} ne '') {
 5188:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 5189:                         $changes{$role}{'font'} = 1;
 5190:                     }
 5191:                 } else {
 5192:                     if ($confhash->{$role}{'font'}) {
 5193:                         $changes{$role}{'font'} = 1;
 5194:                     }
 5195:                 }
 5196:                 if ($role ne 'login') {
 5197:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 5198:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 5199:                             $changes{$role}{'fontmenu'} = 1;
 5200:                         }
 5201:                     } else {
 5202:                         if ($confhash->{$role}{'fontmenu'}) {
 5203:                             $changes{$role}{'fontmenu'} = 1;
 5204:                         }
 5205:                     }
 5206:                 }
 5207:                 foreach my $item (@bgs) {
 5208:                     if ($domconfig->{$role}{$item} ne '') {
 5209:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5210:                             $changes{$role}{'bgs'}{$item} = 1;
 5211:                         } 
 5212:                     } else {
 5213:                         if ($confhash->{$role}{$item}) {
 5214:                             $changes{$role}{'bgs'}{$item} = 1;
 5215:                         }
 5216:                     }
 5217:                 }
 5218:                 foreach my $item (@links) {
 5219:                     if ($domconfig->{$role}{$item} ne '') {
 5220:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5221:                             $changes{$role}{'links'}{$item} = 1;
 5222:                         }
 5223:                     } else {
 5224:                         if ($confhash->{$role}{$item}) {
 5225:                             $changes{$role}{'links'}{$item} = 1;
 5226:                         }
 5227:                     }
 5228:                 }
 5229:                 foreach my $item (@logintext) {
 5230:                     if ($domconfig->{$role}{$item} ne '') {
 5231:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5232:                             $changes{$role}{'logintext'}{$item} = 1;
 5233:                         }
 5234:                     } else {
 5235:                         if ($confhash->{$role}{$item}) {
 5236:                             $changes{$role}{'logintext'}{$item} = 1;
 5237:                         }
 5238:                     }
 5239:                 }
 5240:             } else {
 5241:                 &default_change_checker($role,\@images,\@links,\@bgs,
 5242:                                         \@logintext,$confhash,\%changes); 
 5243:             }
 5244:         } else {
 5245:             &default_change_checker($role,\@images,\@links,\@bgs,
 5246:                                     \@logintext,$confhash,\%changes); 
 5247:         }
 5248:     }
 5249:     return ($errors,%changes);
 5250: }
 5251: 
 5252: sub config_check {
 5253:     my ($dom,$confname,$servadm) = @_;
 5254:     my ($configuserok,$author_ok,$switchserver,%currroles);
 5255:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 5256:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 5257:                                                    $confname,$servadm);
 5258:     if ($configuserok eq 'ok') {
 5259:         $switchserver = &check_switchserver($dom,$confname);
 5260:         if ($switchserver eq '') {
 5261:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 5262:         }
 5263:     }
 5264:     return ($configuserok,$author_ok,$switchserver);
 5265: }
 5266: 
 5267: sub default_change_checker {
 5268:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 5269:     foreach my $item (@{$links}) {
 5270:         if ($confhash->{$role}{$item}) {
 5271:             $changes->{$role}{'links'}{$item} = 1;
 5272:         }
 5273:     }
 5274:     foreach my $item (@{$bgs}) {
 5275:         if ($confhash->{$role}{$item}) {
 5276:             $changes->{$role}{'bgs'}{$item} = 1;
 5277:         }
 5278:     }
 5279:     foreach my $item (@{$logintext}) {
 5280:         if ($confhash->{$role}{$item}) {
 5281:             $changes->{$role}{'logintext'}{$item} = 1;
 5282:         }
 5283:     }
 5284:     foreach my $img (@{$images}) {
 5285:         if ($env{'form.'.$role.'_del_'.$img}) {
 5286:             $confhash->{$role}{$img} = '';
 5287:             $changes->{$role}{'images'}{$img} = 1;
 5288:         }
 5289:         if ($role eq 'login') {
 5290:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5291:                 $changes->{$role}{'showlogo'}{$img} = 1;
 5292:             }
 5293:         }
 5294:     }
 5295:     if ($confhash->{$role}{'font'}) {
 5296:         $changes->{$role}{'font'} = 1;
 5297:     }
 5298: }
 5299: 
 5300: sub display_colorchgs {
 5301:     my ($dom,$changes,$roles,$confhash) = @_;
 5302:     my (%choices,$resulttext);
 5303:     if (!grep(/^login$/,@{$roles})) {
 5304:         $resulttext = &mt('Changes made:').'<br />';
 5305:     }
 5306:     foreach my $role (@{$roles}) {
 5307:         if ($role eq 'login') {
 5308:             %choices = &login_choices();
 5309:         } else {
 5310:             %choices = &color_font_choices();
 5311:         }
 5312:         if (ref($changes->{$role}) eq 'HASH') {
 5313:             if ($role ne 'login') {
 5314:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 5315:             }
 5316:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 5317:                 if ($role ne 'login') {
 5318:                     $resulttext .= '<ul>';
 5319:                 }
 5320:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 5321:                     if ($role ne 'login') {
 5322:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 5323:                     }
 5324:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 5325:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 5326:                             if ($confhash->{$role}{$key}{$item}) {
 5327:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 5328:                             } else {
 5329:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 5330:                             }
 5331:                         } elsif ($confhash->{$role}{$item} eq '') {
 5332:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 5333:                         } else {
 5334:                             my $newitem = $confhash->{$role}{$item};
 5335:                             if ($key eq 'images') {
 5336:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 5337:                             }
 5338:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 5339:                         }
 5340:                     }
 5341:                     if ($role ne 'login') {
 5342:                         $resulttext .= '</ul></li>';
 5343:                     }
 5344:                 } else {
 5345:                     if ($confhash->{$role}{$key} eq '') {
 5346:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 5347:                     } else {
 5348:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 5349:                     }
 5350:                 }
 5351:                 if ($role ne 'login') {
 5352:                     $resulttext .= '</ul>';
 5353:                 }
 5354:             }
 5355:         }
 5356:     }
 5357:     return $resulttext;
 5358: }
 5359: 
 5360: sub thumb_dimensions {
 5361:     return ('200','50');
 5362: }
 5363: 
 5364: sub check_dimensions {
 5365:     my ($inputfile) = @_;
 5366:     my ($fullwidth,$fullheight);
 5367:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 5368:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 5369:             my $imageinfo = <PIPE>;
 5370:             if (!close(PIPE)) {
 5371:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 5372:             }
 5373:             chomp($imageinfo);
 5374:             my ($fullsize) = 
 5375:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 5376:             if ($fullsize) {
 5377:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 5378:             }
 5379:         }
 5380:     }
 5381:     return ($fullwidth,$fullheight);
 5382: }
 5383: 
 5384: sub check_configuser {
 5385:     my ($uhome,$dom,$confname,$servadm) = @_;
 5386:     my ($configuserok,%currroles);
 5387:     if ($uhome eq 'no_host') {
 5388:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 5389:         my $configpass = &LONCAPA::Enrollment::create_password();
 5390:         $configuserok = 
 5391:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 5392:                              $configpass,'','','','','',undef,$servadm);
 5393:     } else {
 5394:         $configuserok = 'ok';
 5395:         %currroles = 
 5396:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 5397:     }
 5398:     return ($configuserok,%currroles);
 5399: }
 5400: 
 5401: sub check_authorstatus {
 5402:     my ($dom,$confname,%currroles) = @_;
 5403:     my $author_ok;
 5404:     if (!$currroles{':'.$dom.':au'}) {
 5405:         my $start = time;
 5406:         my $end = 0;
 5407:         $author_ok = 
 5408:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 5409:                                         'au',$end,$start,'','','domconfig');
 5410:     } else {
 5411:         $author_ok = 'ok';
 5412:     }
 5413:     return $author_ok;
 5414: }
 5415: 
 5416: sub publishlogo {
 5417:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 5418:     my ($output,$fname,$logourl);
 5419:     if ($action eq 'upload') {
 5420:         $fname=$env{'form.'.$formname.'.filename'};
 5421:         chop($env{'form.'.$formname});
 5422:     } else {
 5423:         ($fname) = ($formname =~ /([^\/]+)$/);
 5424:     }
 5425:     if ($savefileas ne '') {
 5426:         $fname = $savefileas;
 5427:     }
 5428:     $fname=&Apache::lonnet::clean_filename($fname);
 5429: # See if there is anything left
 5430:     unless ($fname) { return ('error: no uploaded file'); }
 5431:     $fname="$subdir/$fname";
 5432:     my $docroot=$r->dir_config('lonDocRoot'); 
 5433:     my $filepath="$docroot/priv";
 5434:     my $relpath = "$dom/$confname";
 5435:     my ($fnamepath,$file,$fetchthumb);
 5436:     $file=$fname;
 5437:     if ($fname=~m|/|) {
 5438:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 5439:     }
 5440:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 5441:     my $count;
 5442:     for ($count=5;$count<=$#parts;$count++) {
 5443:         $filepath.="/$parts[$count]";
 5444:         if ((-e $filepath)!=1) {
 5445:             mkdir($filepath,02770);
 5446:         }
 5447:     }
 5448:     # Check for bad extension and disallow upload
 5449:     if ($file=~/\.(\w+)$/ &&
 5450:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 5451:         $output = 
 5452:             &mt('Invalid file extension ([_1]) - reserved for LONCAPA use.',$1); 
 5453:     } elsif ($file=~/\.(\w+)$/ &&
 5454:         !defined(&Apache::loncommon::fileembstyle($1))) {
 5455:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 5456:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 5457:         $output = &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 5458:     } elsif (-d "$filepath/$file") {
 5459:         $output = &mt('File name is a directory name - rename the file and re-upload');
 5460:     } else {
 5461:         my $source = $filepath.'/'.$file;
 5462:         my $logfile;
 5463:         if (!open($logfile,">>$source".'.log')) {
 5464:             return (&mt('No write permission to Construction Space'));
 5465:         }
 5466:         print $logfile
 5467: "\n================= Publish ".localtime()." ================\n".
 5468: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 5469: # Save the file
 5470:         if (!open(FH,'>'.$source)) {
 5471:             &Apache::lonnet::logthis('Failed to create '.$source);
 5472:             return (&mt('Failed to create file'));
 5473:         }
 5474:         if ($action eq 'upload') {
 5475:             if (!print FH ($env{'form.'.$formname})) {
 5476:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 5477:                 return (&mt('Failed to write file'));
 5478:             }
 5479:         } else {
 5480:             my $original = &Apache::lonnet::filelocation('',$formname);
 5481:             if(!copy($original,$source)) {
 5482:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 5483:                 return (&mt('Failed to write file'));
 5484:             }
 5485:         }
 5486:         close(FH);
 5487:         chmod(0660, $source); # Permissions to rw-rw---.
 5488: 
 5489:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 5490:         my $copyfile=$targetdir.'/'.$file;
 5491: 
 5492:         my @parts=split(/\//,$targetdir);
 5493:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 5494:         for (my $count=5;$count<=$#parts;$count++) {
 5495:             $path.="/$parts[$count]";
 5496:             if (!-e $path) {
 5497:                 print $logfile "\nCreating directory ".$path;
 5498:                 mkdir($path,02770);
 5499:             }
 5500:         }
 5501:         my $versionresult;
 5502:         if (-e $copyfile) {
 5503:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 5504:         } else {
 5505:             $versionresult = 'ok';
 5506:         }
 5507:         if ($versionresult eq 'ok') {
 5508:             if (copy($source,$copyfile)) {
 5509:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 5510:                 $output = 'ok';
 5511:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 5512:                 push(@{$modified_urls},[$copyfile,$source]);
 5513:                 my $metaoutput = 
 5514:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 5515:                 unless ($registered_cleanup) {
 5516:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5517:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5518:                     $registered_cleanup=1;
 5519:                 }
 5520:             } else {
 5521:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 5522:                 $output = &mt('Failed to copy file to RES space').", $!";
 5523:             }
 5524:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 5525:                 my $inputfile = $filepath.'/'.$file;
 5526:                 my $outfile = $filepath.'/'.'tn-'.$file;
 5527:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 5528:                 if ($fullwidth ne '' && $fullheight ne '') { 
 5529:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 5530:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 5531:                         system("convert -sample $thumbsize $inputfile $outfile");
 5532:                         chmod(0660, $filepath.'/tn-'.$file);
 5533:                         if (-e $outfile) {
 5534:                             my $copyfile=$targetdir.'/tn-'.$file;
 5535:                             if (copy($outfile,$copyfile)) {
 5536:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 5537:                                 my $thumb_metaoutput = 
 5538:                                     &write_metadata($dom,$confname,$formname,
 5539:                                                     $targetdir,'tn-'.$file,$logfile);
 5540:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 5541:                                 unless ($registered_cleanup) {
 5542:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5543:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5544:                                     $registered_cleanup=1;
 5545:                                 }
 5546:                             } else {
 5547:                                 print $logfile "\nUnable to write ".$copyfile.
 5548:                                                ':'.$!."\n";
 5549:                             }
 5550:                         }
 5551:                     }
 5552:                 }
 5553:             }
 5554:         } else {
 5555:             $output = $versionresult;
 5556:         }
 5557:     }
 5558:     return ($output,$logourl);
 5559: }
 5560: 
 5561: sub logo_versioning {
 5562:     my ($targetdir,$file,$logfile) = @_;
 5563:     my $target = $targetdir.'/'.$file;
 5564:     my ($maxversion,$fn,$extn,$output);
 5565:     $maxversion = 0;
 5566:     if ($file =~ /^(.+)\.(\w+)$/) {
 5567:         $fn=$1;
 5568:         $extn=$2;
 5569:     }
 5570:     opendir(DIR,$targetdir);
 5571:     while (my $filename=readdir(DIR)) {
 5572:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 5573:             $maxversion=($1>$maxversion)?$1:$maxversion;
 5574:         }
 5575:     }
 5576:     $maxversion++;
 5577:     print $logfile "\nCreating old version ".$maxversion."\n";
 5578:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 5579:     if (copy($target,$copyfile)) {
 5580:         print $logfile "Copied old target to ".$copyfile."\n";
 5581:         $copyfile=$copyfile.'.meta';
 5582:         if (copy($target.'.meta',$copyfile)) {
 5583:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 5584:             $output = 'ok';
 5585:         } else {
 5586:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 5587:             $output = &mt('Failed to copy old meta').", $!, ";
 5588:         }
 5589:     } else {
 5590:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 5591:         $output = &mt('Failed to copy old target').", $!, ";
 5592:     }
 5593:     return $output;
 5594: }
 5595: 
 5596: sub write_metadata {
 5597:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 5598:     my (%metadatafields,%metadatakeys,$output);
 5599:     $metadatafields{'title'}=$formname;
 5600:     $metadatafields{'creationdate'}=time;
 5601:     $metadatafields{'lastrevisiondate'}=time;
 5602:     $metadatafields{'copyright'}='public';
 5603:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 5604:                                          $env{'user.domain'};
 5605:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 5606:     $metadatafields{'domain'}=$dom;
 5607:     {
 5608:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 5609:         my $mfh;
 5610:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 5611:             foreach (sort(keys(%metadatafields))) {
 5612:                 unless ($_=~/\./) {
 5613:                     my $unikey=$_;
 5614:                     $unikey=~/^([A-Za-z]+)/;
 5615:                     my $tag=$1;
 5616:                     $tag=~tr/A-Z/a-z/;
 5617:                     print $mfh "\n\<$tag";
 5618:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 5619:                         my $value=$metadatafields{$unikey.'.'.$_};
 5620:                         $value=~s/\"/\'\'/g;
 5621:                         print $mfh ' '.$_.'="'.$value.'"';
 5622:                     }
 5623:                     print $mfh '>'.
 5624:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 5625:                             .'</'.$tag.'>';
 5626:                 }
 5627:             }
 5628:             $output = 'ok';
 5629:             print $logfile "\nWrote metadata";
 5630:             close($mfh);
 5631:         } else {
 5632:             print $logfile "\nFailed to open metadata file";
 5633:             $output = &mt('Could not write metadata');
 5634:         }
 5635:     }
 5636:     return $output;
 5637: }
 5638: 
 5639: sub notifysubscribed {
 5640:     foreach my $targetsource (@{$modified_urls}){
 5641:         next unless (ref($targetsource) eq 'ARRAY');
 5642:         my ($target,$source)=@{$targetsource};
 5643:         if ($source ne '') {
 5644:             if (open(my $logfh,'>>'.$source.'.log')) {
 5645:                 print $logfh "\nCleanup phase: Notifications\n";
 5646:                 my @subscribed=&subscribed_hosts($target);
 5647:                 foreach my $subhost (@subscribed) {
 5648:                     print $logfh "\nNotifying host ".$subhost.':';
 5649:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 5650:                     print $logfh $reply;
 5651:                 }
 5652:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 5653:                 foreach my $subhost (@subscribedmeta) {
 5654:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 5655:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 5656:                                                         $subhost);
 5657:                     print $logfh $reply;
 5658:                 }
 5659:                 print $logfh "\n============ Done ============\n";
 5660:                 close($logfh);
 5661:             }
 5662:         }
 5663:     }
 5664:     return OK;
 5665: }
 5666: 
 5667: sub subscribed_hosts {
 5668:     my ($target) = @_;
 5669:     my @subscribed;
 5670:     if (open(my $fh,"<$target.subscription")) {
 5671:         while (my $subline=<$fh>) {
 5672:             if ($subline =~ /^($match_lonid):/) {
 5673:                 my $host = $1;
 5674:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 5675:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 5676:                         push(@subscribed,$host);
 5677:                     }
 5678:                 }
 5679:             }
 5680:         }
 5681:     }
 5682:     return @subscribed;
 5683: }
 5684: 
 5685: sub check_switchserver {
 5686:     my ($dom,$confname) = @_;
 5687:     my ($allowed,$switchserver);
 5688:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 5689:     if ($home eq 'no_host') {
 5690:         $home = &Apache::lonnet::domain($dom,'primary');
 5691:     }
 5692:     my @ids=&Apache::lonnet::current_machine_ids();
 5693:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 5694:     if (!$allowed) {
 5695: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 5696:     }
 5697:     return $switchserver;
 5698: }
 5699: 
 5700: sub modify_quotas {
 5701:     my ($dom,$action,%domconfig) = @_;
 5702:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 5703:         %limithash,$toolregexp,%conditions,$resulttext,%changes);
 5704:     if ($action eq 'quotas') {
 5705:         $context = 'tools'; 
 5706:     } else {
 5707:         $context = $action;
 5708:     }
 5709:     if ($context eq 'requestcourses') {
 5710:         @usertools = ('official','unofficial','community');
 5711:         @options =('norequest','approval','validate','autolimit');
 5712:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 5713:         %titles = &courserequest_titles();
 5714:         $toolregexp = join('|',@usertools);
 5715:         %conditions = &courserequest_conditions();
 5716:     } elsif ($context eq 'requestauthor') {
 5717:         @usertools = ('author');
 5718:         %titles = &authorrequest_titles();
 5719:     } else {
 5720:         @usertools = ('aboutme','blog','webdav','portfolio');
 5721:         %titles = &tool_titles();
 5722:     }
 5723:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5724:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5725:     foreach my $key (keys(%env)) {
 5726:         if ($context eq 'requestcourses') {
 5727:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 5728:                 my $item = $1;
 5729:                 my $type = $2;
 5730:                 if ($type =~ /^limit_(.+)/) {
 5731:                     $limithash{$item}{$1} = $env{$key};
 5732:                 } else {
 5733:                     $confhash{$item}{$type} = $env{$key};
 5734:                 }
 5735:             }
 5736:         } elsif ($context eq 'requestauthor') {
 5737:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 5738:                 $confhash{$1} = $env{$key};
 5739:             }
 5740:         } else {
 5741:             if ($key =~ /^form\.quota_(.+)$/) {
 5742:                 $confhash{'defaultquota'}{$1} = $env{$key};
 5743:             }
 5744:             if ($key =~ /^form\.\Q$context\E_(.+)$/) {
 5745:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 5746:             }
 5747:         }
 5748:     }
 5749:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5750:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.reqapprovalnotify');
 5751:         @approvalnotify = sort(@approvalnotify);
 5752:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 5753:         if (ref($domconfig{$action}) eq 'HASH') {
 5754:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 5755:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 5756:                     $changes{'notify'}{'approval'} = 1;
 5757:                 }
 5758:             } else {
 5759:                 if ($confhash{'notify'}{'approval'}) {
 5760:                     $changes{'notify'}{'approval'} = 1;
 5761:                 }
 5762:             }
 5763:         } else {
 5764:             if ($confhash{'notify'}{'approval'}) {
 5765:                 $changes{'notify'}{'approval'} = 1;
 5766:             }
 5767:         }
 5768:     } else {
 5769:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 5770:     }
 5771:     foreach my $item (@usertools) {
 5772:         foreach my $type (@{$types},'default','_LC_adv') {
 5773:             my $unset; 
 5774:             if ($context eq 'requestcourses') {
 5775:                 $unset = '0';
 5776:                 if ($type eq '_LC_adv') {
 5777:                     $unset = '';
 5778:                 }
 5779:                 if ($confhash{$item}{$type} eq 'autolimit') {
 5780:                     $confhash{$item}{$type} .= '=';
 5781:                     unless ($limithash{$item}{$type} =~ /\D/) {
 5782:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 5783:                     }
 5784:                 }
 5785:             } elsif ($context eq 'requestauthor') {
 5786:                 $unset = '0';
 5787:                 if ($type eq '_LC_adv') {
 5788:                     $unset = '';
 5789:                 }
 5790:             } else {
 5791:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 5792:                     $confhash{$item}{$type} = 1;
 5793:                 } else {
 5794:                     $confhash{$item}{$type} = 0;
 5795:                 }
 5796:             }
 5797:             if (ref($domconfig{$action}) eq 'HASH') {
 5798:                 if ($action eq 'requestauthor') {
 5799:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 5800:                         $changes{$type} = 1;
 5801:                     }
 5802:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 5803:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 5804:                         $changes{$item}{$type} = 1;
 5805:                     }
 5806:                 } else {
 5807:                     if ($context eq 'requestcourses') {
 5808:                         if ($confhash{$item}{$type} ne $unset) {
 5809:                             $changes{$item}{$type} = 1;
 5810:                         }
 5811:                     } else {
 5812:                         if (!$confhash{$item}{$type}) {
 5813:                             $changes{$item}{$type} = 1;
 5814:                         }
 5815:                     }
 5816:                 }
 5817:             } else {
 5818:                 if ($context eq 'requestcourses') {
 5819:                     if ($confhash{$item}{$type} ne $unset) {
 5820:                         $changes{$item}{$type} = 1;
 5821:                     }
 5822:                 } elsif ($context eq 'requestauthor') {
 5823:                     if ($confhash{$type} ne $unset) {
 5824:                         $changes{$type} = 1;
 5825:                     }
 5826:                 } else {
 5827:                     if (!$confhash{$item}{$type}) {
 5828:                         $changes{$item}{$type} = 1;
 5829:                     }
 5830:                 }
 5831:             }
 5832:         }
 5833:     }
 5834:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5835:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 5836:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5837:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 5838:                     if (exists($confhash{'defaultquota'}{$key})) {
 5839:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 5840:                             $changes{'defaultquota'}{$key} = 1;
 5841:                         }
 5842:                     } else {
 5843:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 5844:                     }
 5845:                 }
 5846:             } else {
 5847:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 5848:                     if (exists($confhash{'defaultquota'}{$key})) {
 5849:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 5850:                             $changes{'defaultquota'}{$key} = 1;
 5851:                         }
 5852:                     } else {
 5853:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 5854:                     }
 5855:                 }
 5856:             }
 5857:         }
 5858:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 5859:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 5860:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 5861:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5862:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 5863:                             $changes{'defaultquota'}{$key} = 1;
 5864:                         }
 5865:                     } else {
 5866:                         if (!exists($domconfig{'quotas'}{$key})) {
 5867:                             $changes{'defaultquota'}{$key} = 1;
 5868:                         }
 5869:                     }
 5870:                 } else {
 5871:                     $changes{'defaultquota'}{$key} = 1;
 5872:                 }
 5873:             }
 5874:         }
 5875:     }
 5876: 
 5877:     if ($context eq 'requestauthor') {
 5878:         $domdefaults{'requestauthor'} = \%confhash;
 5879:     } else {
 5880:         foreach my $key (keys(%confhash)) {
 5881:             $domdefaults{$key} = $confhash{$key};
 5882:         }
 5883:     }
 5884: 
 5885:     my %quotahash = (
 5886:                       $action => { %confhash }
 5887:                     );
 5888:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 5889:                                              $dom);
 5890:     if ($putresult eq 'ok') {
 5891:         if (keys(%changes) > 0) {
 5892:             my $cachetime = 24*60*60;
 5893:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 5894: 
 5895:             $resulttext = &mt('Changes made:').'<ul>';
 5896:             unless (($context eq 'requestcourses') || 
 5897:                     ($context eq 'requestauthor')) {
 5898:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 5899:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 5900:                     foreach my $type (@{$types},'default') {
 5901:                         if (defined($changes{'defaultquota'}{$type})) {
 5902:                             my $typetitle = $usertypes->{$type};
 5903:                             if ($type eq 'default') {
 5904:                                 $typetitle = $othertitle;
 5905:                             }
 5906:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] Mb',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 5907:                         }
 5908:                     }
 5909:                     $resulttext .= '</ul></li>';
 5910:                 }
 5911:             }
 5912:             my %newenv;
 5913:             foreach my $item (@usertools) {
 5914:                 my (%haschgs,%inconf);
 5915:                 if ($context eq 'requestauthor') {
 5916:                     %haschgs = %changes;
 5917:                     %inconf = %confhash; 
 5918:                 } else {
 5919:                     if (ref($changes{$item}) eq 'HASH') {
 5920:                         %haschgs = %{$changes{$item}};
 5921:                     }
 5922:                     if (ref($confhash{$item}) eq 'HASH') {
 5923:                         %inconf = %{$confhash{$item}};
 5924:                     }
 5925:                 }
 5926:                 if (keys(%haschgs) > 0) {
 5927:                     my $newacc = 
 5928:                         &Apache::lonnet::usertools_access($env{'user.name'},
 5929:                                                           $env{'user.domain'},
 5930:                                                           $item,'reload',$context);
 5931:                     if (($context eq 'requestcourses') || 
 5932:                         ($context eq 'requestauthor')) {
 5933:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 5934:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 5935:                         }
 5936:                     } else {
 5937:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 5938:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 5939:                         }
 5940:                     }
 5941:                     unless ($context eq 'requestauthor') {
 5942:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 5943:                     }
 5944:                     foreach my $type (@{$types},'default','_LC_adv') {
 5945:                         if ($haschgs{$type}) {
 5946:                             my $typetitle = $usertypes->{$type};
 5947:                             if ($type eq 'default') {
 5948:                                 $typetitle = $othertitle;
 5949:                             } elsif ($type eq '_LC_adv') {
 5950:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 5951:                             }
 5952:                             if ($inconf{$type}) {
 5953:                                 if ($context eq 'requestcourses') {
 5954:                                     my $cond;
 5955:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 5956:                                         if ($1 eq '') {
 5957:                                             $cond = &mt('(Automatic processing of any request).');
 5958:                                         } else {
 5959:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 5960:                                         }
 5961:                                     } else { 
 5962:                                         $cond = $conditions{$inconf{$type}};
 5963:                                     }
 5964:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 5965:                                 } elsif ($context eq 'requestauthor') {
 5966:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 5967:                                                              $titles{$inconf{$type}},$typetitle);
 5968: 
 5969:                                 } else {
 5970:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 5971:                                 }
 5972:                             } else {
 5973:                                 if ($type eq '_LC_adv') {
 5974:                                     if ($inconf{$type} eq '0') {
 5975:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5976:                                     } else { 
 5977:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 5978:                                     }
 5979:                                 } else {
 5980:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 5981:                                 }
 5982:                             }
 5983:                         }
 5984:                     }
 5985:                     unless ($context eq 'requestauthor') {
 5986:                         $resulttext .= '</ul></li>';
 5987:                     }
 5988:                 }
 5989:             }
 5990:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 5991:                 if (ref($changes{'notify'}) eq 'HASH') {
 5992:                     if ($changes{'notify'}{'approval'}) {
 5993:                         if (ref($confhash{'notify'}) eq 'HASH') {
 5994:                             if ($confhash{'notify'}{'approval'}) {
 5995:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 5996:                             } else {
 5997:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 5998:                             }
 5999:                         }
 6000:                     }
 6001:                 }
 6002:             }
 6003:             $resulttext .= '</ul>';
 6004:             if (keys(%newenv)) {
 6005:                 &Apache::lonnet::appenv(\%newenv);
 6006:             }
 6007:         } else {
 6008:             if ($context eq 'requestcourses') {
 6009:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 6010:             } elsif ($context eq 'requestauthor') {
 6011:                 $resulttext = &mt('No changes made to rights to request author space.');
 6012:             } else {
 6013:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 6014:             }
 6015:         }
 6016:     } else {
 6017:         $resulttext = '<span class="LC_error">'.
 6018: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6019:     }
 6020:     return $resulttext;
 6021: }
 6022: 
 6023: sub modify_autoenroll {
 6024:     my ($dom,%domconfig) = @_;
 6025:     my ($resulttext,%changes);
 6026:     my %currautoenroll;
 6027:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 6028:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 6029:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 6030:         }
 6031:     }
 6032:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 6033:     my %title = ( run => 'Auto-enrollment active',
 6034:                   sender => 'Sender for notification messages',
 6035:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 6036:     my @offon = ('off','on');
 6037:     my $sender_uname = $env{'form.sender_uname'};
 6038:     my $sender_domain = $env{'form.sender_domain'};
 6039:     if ($sender_domain eq '') {
 6040:         $sender_uname = '';
 6041:     } elsif ($sender_uname eq '') {
 6042:         $sender_domain = '';
 6043:     }
 6044:     my $coowners = $env{'form.autoassign_coowners'};
 6045:     my %autoenrollhash =  (
 6046:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 6047:                                        'sender_uname' => $sender_uname,
 6048:                                        'sender_domain' => $sender_domain,
 6049:                                        'co-owners' => $coowners,
 6050:                                 }
 6051:                      );
 6052:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 6053:                                              $dom);
 6054:     if ($putresult eq 'ok') {
 6055:         if (exists($currautoenroll{'run'})) {
 6056:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 6057:                  $changes{'run'} = 1;
 6058:              }
 6059:         } elsif ($autorun) {
 6060:             if ($env{'form.autoenroll_run'} ne '1') {
 6061:                  $changes{'run'} = 1;
 6062:             }
 6063:         }
 6064:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 6065:             $changes{'sender'} = 1;
 6066:         }
 6067:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 6068:             $changes{'sender'} = 1;
 6069:         }
 6070:         if ($currautoenroll{'co-owners'} ne '') {
 6071:             if ($currautoenroll{'co-owners'} ne $coowners) {
 6072:                 $changes{'coowners'} = 1;
 6073:             }
 6074:         } elsif ($coowners) {
 6075:             $changes{'coowners'} = 1;
 6076:         }      
 6077:         if (keys(%changes) > 0) {
 6078:             $resulttext = &mt('Changes made:').'<ul>';
 6079:             if ($changes{'run'}) {
 6080:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 6081:             }
 6082:             if ($changes{'sender'}) {
 6083:                 if ($sender_uname eq '' || $sender_domain eq '') {
 6084:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 6085:                 } else {
 6086:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 6087:                 }
 6088:             }
 6089:             if ($changes{'coowners'}) {
 6090:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 6091:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 6092:             }
 6093:             $resulttext .= '</ul>';
 6094:         } else {
 6095:             $resulttext = &mt('No changes made to auto-enrollment settings');
 6096:         }
 6097:     } else {
 6098:         $resulttext = '<span class="LC_error">'.
 6099: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6100:     }
 6101:     return $resulttext;
 6102: }
 6103: 
 6104: sub modify_autoupdate {
 6105:     my ($dom,%domconfig) = @_;
 6106:     my ($resulttext,%currautoupdate,%fields,%changes);
 6107:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 6108:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 6109:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 6110:         }
 6111:     }
 6112:     my @offon = ('off','on');
 6113:     my %title = &Apache::lonlocal::texthash (
 6114:                    run => 'Auto-update:',
 6115:                    classlists => 'Updates to user information in classlists?'
 6116:                 );
 6117:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6118:     my %fieldtitles = &Apache::lonlocal::texthash (
 6119:                         id => 'Student/Employee ID',
 6120:                         permanentemail => 'E-mail address',
 6121:                         lastname => 'Last Name',
 6122:                         firstname => 'First Name',
 6123:                         middlename => 'Middle Name',
 6124:                         generation => 'Generation',
 6125:                       );
 6126:     $othertitle = &mt('All users');
 6127:     if (keys(%{$usertypes}) >  0) {
 6128:         $othertitle = &mt('Other users');
 6129:     }
 6130:     foreach my $key (keys(%env)) {
 6131:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 6132:             my ($usertype,$item) = ($1,$2);
 6133:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 6134:                 if ($usertype eq 'default') {   
 6135:                     push(@{$fields{$1}},$2);
 6136:                 } elsif (ref($types) eq 'ARRAY') {
 6137:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 6138:                         push(@{$fields{$1}},$2);
 6139:                     }
 6140:                 }
 6141:             }
 6142:         }
 6143:     }
 6144:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 6145:     @lockablenames = sort(@lockablenames);
 6146:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 6147:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6148:         if (@changed) {
 6149:             $changes{'lockablenames'} = 1;
 6150:         }
 6151:     } else {
 6152:         if (@lockablenames) {
 6153:             $changes{'lockablenames'} = 1;
 6154:         }
 6155:     }
 6156:     my %updatehash = (
 6157:                       autoupdate => { run => $env{'form.autoupdate_run'},
 6158:                                       classlists => $env{'form.classlists'},
 6159:                                       fields => {%fields},
 6160:                                       lockablenames => \@lockablenames,
 6161:                                     }
 6162:                      );
 6163:     foreach my $key (keys(%currautoupdate)) {
 6164:         if (($key eq 'run') || ($key eq 'classlists')) {
 6165:             if (exists($updatehash{autoupdate}{$key})) {
 6166:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 6167:                     $changes{$key} = 1;
 6168:                 }
 6169:             }
 6170:         } elsif ($key eq 'fields') {
 6171:             if (ref($currautoupdate{$key}) eq 'HASH') {
 6172:                 foreach my $item (@{$types},'default') {
 6173:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 6174:                         my $change = 0;
 6175:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 6176:                             if (!exists($fields{$item})) {
 6177:                                 $change = 1;
 6178:                                 last;
 6179:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 6180:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 6181:                                     $change = 1;
 6182:                                     last;
 6183:                                 }
 6184:                             }
 6185:                         }
 6186:                         if ($change) {
 6187:                             push(@{$changes{$key}},$item);
 6188:                         }
 6189:                     } 
 6190:                 }
 6191:             }
 6192:         } elsif ($key eq 'lockablenames') {
 6193:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 6194:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6195:                 if (@changed) {
 6196:                     $changes{'lockablenames'} = 1;
 6197:                 }
 6198:             } else {
 6199:                 if (@lockablenames) {
 6200:                     $changes{'lockablenames'} = 1;
 6201:                 }
 6202:             }
 6203:         }
 6204:     }
 6205:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 6206:         if (@lockablenames) {
 6207:             $changes{'lockablenames'} = 1;
 6208:         }
 6209:     }
 6210:     foreach my $item (@{$types},'default') {
 6211:         if (defined($fields{$item})) {
 6212:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 6213:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 6214:                     my $change = 0;
 6215:                     if (ref($fields{$item}) eq 'ARRAY') {
 6216:                         foreach my $type (@{$fields{$item}}) {
 6217:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 6218:                                 $change = 1;
 6219:                                 last;
 6220:                             }
 6221:                         }
 6222:                     }
 6223:                     if ($change) {
 6224:                         push(@{$changes{'fields'}},$item);
 6225:                     }
 6226:                 } else {
 6227:                     push(@{$changes{'fields'}},$item);
 6228:                 }
 6229:             } else {
 6230:                 push(@{$changes{'fields'}},$item);
 6231:             }
 6232:         }
 6233:     }
 6234:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 6235:                                              $dom);
 6236:     if ($putresult eq 'ok') {
 6237:         if (keys(%changes) > 0) {
 6238:             $resulttext = &mt('Changes made:').'<ul>';
 6239:             foreach my $key (sort(keys(%changes))) {
 6240:                 if ($key eq 'lockablenames') {
 6241:                     $resulttext .= '<li>';
 6242:                     if (@lockablenames) {
 6243:                         $usertypes->{'default'} = $othertitle;
 6244:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 6245:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 6246:                     } else {
 6247:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 6248:                     }
 6249:                     $resulttext .= '</li>';
 6250:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 6251:                     foreach my $item (@{$changes{$key}}) {
 6252:                         my @newvalues;
 6253:                         foreach my $type (@{$fields{$item}}) {
 6254:                             push(@newvalues,$fieldtitles{$type});
 6255:                         }
 6256:                         my $newvaluestr;
 6257:                         if (@newvalues > 0) {
 6258:                             $newvaluestr = join(', ',@newvalues);
 6259:                         } else {
 6260:                             $newvaluestr = &mt('none');
 6261:                         }
 6262:                         if ($item eq 'default') {
 6263:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 6264:                         } else {
 6265:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 6266:                         }
 6267:                     }
 6268:                 } else {
 6269:                     my $newvalue;
 6270:                     if ($key eq 'run') {
 6271:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 6272:                     } else {
 6273:                         $newvalue = $offon[$env{'form.'.$key}];
 6274:                     }
 6275:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 6276:                 }
 6277:             }
 6278:             $resulttext .= '</ul>';
 6279:         } else {
 6280:             $resulttext = &mt('No changes made to autoupdates');
 6281:         }
 6282:     } else {
 6283:         $resulttext = '<span class="LC_error">'.
 6284: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6285:     }
 6286:     return $resulttext;
 6287: }
 6288: 
 6289: sub modify_autocreate {
 6290:     my ($dom,%domconfig) = @_;
 6291:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 6292:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 6293:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 6294:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 6295:         }
 6296:     }
 6297:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 6298:                  req => 'Auto-creation of validated requests for official courses',
 6299:                  xmldc => 'Identity of course creator of courses from XML files',
 6300:                );
 6301:     my @types = ('xml','req');
 6302:     foreach my $item (@types) {
 6303:         $newvals{$item} = $env{'form.autocreate_'.$item};
 6304:         $newvals{$item} =~ s/\D//g;
 6305:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 6306:     }
 6307:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 6308:     my %domcoords = &get_active_dcs($dom);
 6309:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 6310:         $newvals{'xmldc'} = '';
 6311:     } 
 6312:     %autocreatehash =  (
 6313:                         autocreate => { xml => $newvals{'xml'},
 6314:                                         req => $newvals{'req'},
 6315:                                       }
 6316:                        );
 6317:     if ($newvals{'xmldc'} ne '') {
 6318:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 6319:     }
 6320:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 6321:                                              $dom);
 6322:     if ($putresult eq 'ok') {
 6323:         my @items = @types;
 6324:         if ($newvals{'xml'}) {
 6325:             push(@items,'xmldc');
 6326:         }
 6327:         foreach my $item (@items) {
 6328:             if (exists($currautocreate{$item})) {
 6329:                 if ($currautocreate{$item} ne $newvals{$item}) {
 6330:                     $changes{$item} = 1;
 6331:                 }
 6332:             } elsif ($newvals{$item}) {
 6333:                 $changes{$item} = 1;
 6334:             }
 6335:         }
 6336:         if (keys(%changes) > 0) {
 6337:             my @offon = ('off','on'); 
 6338:             $resulttext = &mt('Changes made:').'<ul>';
 6339:             foreach my $item (@types) {
 6340:                 if ($changes{$item}) {
 6341:                     my $newtxt = $offon[$newvals{$item}];
 6342:                     $resulttext .= '<li>'.
 6343:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 6344:                                        '<b>','</b>').
 6345:                                    '</li>';
 6346:                 }
 6347:             }
 6348:             if ($changes{'xmldc'}) {
 6349:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 6350:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 6351:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 6352:             }
 6353:             $resulttext .= '</ul>';
 6354:         } else {
 6355:             $resulttext = &mt('No changes made to auto-creation settings');
 6356:         }
 6357:     } else {
 6358:         $resulttext = '<span class="LC_error">'.
 6359:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6360:     }
 6361:     return $resulttext;
 6362: }
 6363: 
 6364: sub modify_directorysrch {
 6365:     my ($dom,%domconfig) = @_;
 6366:     my ($resulttext,%changes);
 6367:     my %currdirsrch;
 6368:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 6369:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 6370:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 6371:         }
 6372:     }
 6373:     my %title = ( available => 'Directory search available',
 6374:                   localonly => 'Other domains can search',
 6375:                   searchby => 'Search types',
 6376:                   searchtypes => 'Search latitude');
 6377:     my @offon = ('off','on');
 6378:     my @otherdoms = ('Yes','No');
 6379: 
 6380:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 6381:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 6382:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 6383: 
 6384:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6385:     if (keys(%{$usertypes}) == 0) {
 6386:         @cansearch = ('default');
 6387:     } else {
 6388:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 6389:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 6390:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 6391:                     push(@{$changes{'cansearch'}},$type);
 6392:                 }
 6393:             }
 6394:             foreach my $type (@cansearch) {
 6395:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 6396:                     push(@{$changes{'cansearch'}},$type);
 6397:                 }
 6398:             }
 6399:         } else {
 6400:             push(@{$changes{'cansearch'}},@cansearch);
 6401:         }
 6402:     }
 6403: 
 6404:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 6405:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 6406:             if (!grep(/^\Q$by\E$/,@searchby)) {
 6407:                 push(@{$changes{'searchby'}},$by);
 6408:             }
 6409:         }
 6410:         foreach my $by (@searchby) {
 6411:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 6412:                 push(@{$changes{'searchby'}},$by);
 6413:             }
 6414:         }
 6415:     } else {
 6416:         push(@{$changes{'searchby'}},@searchby);
 6417:     }
 6418: 
 6419:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 6420:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 6421:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 6422:                 push(@{$changes{'searchtypes'}},$type);
 6423:             }
 6424:         }
 6425:         foreach my $type (@searchtypes) {
 6426:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 6427:                 push(@{$changes{'searchtypes'}},$type);
 6428:             }
 6429:         }
 6430:     } else {
 6431:         if (exists($currdirsrch{'searchtypes'})) {
 6432:             foreach my $type (@searchtypes) {  
 6433:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 6434:                     push(@{$changes{'searchtypes'}},$type);
 6435:                 }
 6436:             }
 6437:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 6438:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 6439:             }   
 6440:         } else {
 6441:             push(@{$changes{'searchtypes'}},@searchtypes); 
 6442:         }
 6443:     }
 6444: 
 6445:     my %dirsrch_hash =  (
 6446:             directorysrch => { available => $env{'form.dirsrch_available'},
 6447:                                cansearch => \@cansearch,
 6448:                                localonly => $env{'form.dirsrch_localonly'},
 6449:                                searchby => \@searchby,
 6450:                                searchtypes => \@searchtypes,
 6451:                              }
 6452:             );
 6453:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 6454:                                              $dom);
 6455:     if ($putresult eq 'ok') {
 6456:         if (exists($currdirsrch{'available'})) {
 6457:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 6458:                  $changes{'available'} = 1;
 6459:              }
 6460:         } else {
 6461:             if ($env{'form.dirsrch_available'} eq '1') {
 6462:                 $changes{'available'} = 1;
 6463:             }
 6464:         }
 6465:         if (exists($currdirsrch{'localonly'})) {
 6466:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 6467:                  $changes{'localonly'} = 1;
 6468:              }
 6469:         } else {
 6470:             if ($env{'form.dirsrch_localonly'} eq '1') {
 6471:                 $changes{'localonly'} = 1;
 6472:             }
 6473:         }
 6474:         if (keys(%changes) > 0) {
 6475:             $resulttext = &mt('Changes made:').'<ul>';
 6476:             if ($changes{'available'}) {
 6477:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 6478:             }
 6479:             if ($changes{'localonly'}) {
 6480:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 6481:             }
 6482: 
 6483:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 6484:                 my $chgtext;
 6485:                 if (ref($usertypes) eq 'HASH') {
 6486:                     if (keys(%{$usertypes}) > 0) {
 6487:                         foreach my $type (@{$types}) {
 6488:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 6489:                                 $chgtext .= $usertypes->{$type}.'; ';
 6490:                             }
 6491:                         }
 6492:                         if (grep(/^default$/,@cansearch)) {
 6493:                             $chgtext .= $othertitle;
 6494:                         } else {
 6495:                             $chgtext =~ s/\; $//;
 6496:                         }
 6497:                         $resulttext .= 
 6498:                             '<li>'.
 6499:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 6500:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 6501:                             '</li>';
 6502:                     }
 6503:                 }
 6504:             }
 6505:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 6506:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 6507:                 my $chgtext;
 6508:                 foreach my $type (@{$titleorder}) {
 6509:                     if (grep(/^\Q$type\E$/,@searchby)) {
 6510:                         if (defined($searchtitles->{$type})) {
 6511:                             $chgtext .= $searchtitles->{$type}.'; ';
 6512:                         }
 6513:                     }
 6514:                 }
 6515:                 $chgtext =~ s/\; $//;
 6516:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 6517:             }
 6518:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 6519:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 6520:                 my $chgtext;
 6521:                 foreach my $type (@{$srchtypeorder}) {
 6522:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 6523:                         if (defined($srchtypes_desc->{$type})) {
 6524:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 6525:                         }
 6526:                     }
 6527:                 }
 6528:                 $chgtext =~ s/\; $//;
 6529:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 6530:             }
 6531:             $resulttext .= '</ul>';
 6532:         } else {
 6533:             $resulttext = &mt('No changes made to institution directory search settings');
 6534:         }
 6535:     } else {
 6536:         $resulttext = '<span class="LC_error">'.
 6537:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 6538:     }
 6539:     return $resulttext;
 6540: }
 6541: 
 6542: sub modify_contacts {
 6543:     my ($dom,%domconfig) = @_;
 6544:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 6545:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 6546:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 6547:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 6548:         }
 6549:     }
 6550:     my (%others,%to,%bcc);
 6551:     my @contacts = ('supportemail','adminemail');
 6552:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 6553:                     'requestsmail','updatesmail');
 6554:     foreach my $type (@mailings) {
 6555:         @{$newsetting{$type}} = 
 6556:             &Apache::loncommon::get_env_multiple('form.'.$type);
 6557:         foreach my $item (@contacts) {
 6558:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 6559:                 $contacts_hash{contacts}{$type}{$item} = 1;
 6560:             } else {
 6561:                 $contacts_hash{contacts}{$type}{$item} = 0;
 6562:             }
 6563:         }  
 6564:         $others{$type} = $env{'form.'.$type.'_others'};
 6565:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 6566:         if ($type eq 'helpdeskmail') {
 6567:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 6568:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 6569:         }
 6570:     }
 6571:     foreach my $item (@contacts) {
 6572:         $to{$item} = $env{'form.'.$item};
 6573:         $contacts_hash{'contacts'}{$item} = $to{$item};
 6574:     }
 6575:     if (keys(%currsetting) > 0) {
 6576:         foreach my $item (@contacts) {
 6577:             if ($to{$item} ne $currsetting{$item}) {
 6578:                 $changes{$item} = 1;
 6579:             }
 6580:         }
 6581:         foreach my $type (@mailings) {
 6582:             foreach my $item (@contacts) {
 6583:                 if (ref($currsetting{$type}) eq 'HASH') {
 6584:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 6585:                         push(@{$changes{$type}},$item);
 6586:                     }
 6587:                 } else {
 6588:                     push(@{$changes{$type}},@{$newsetting{$type}});
 6589:                 }
 6590:             }
 6591:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 6592:                 push(@{$changes{$type}},'others');
 6593:             }
 6594:             if ($type eq 'helpdeskmail') {   
 6595:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 6596:                     push(@{$changes{$type}},'bcc'); 
 6597:                 }
 6598:             }
 6599:         }
 6600:     } else {
 6601:         my %default;
 6602:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 6603:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 6604:         $default{'errormail'} = 'adminemail';
 6605:         $default{'packagesmail'} = 'adminemail';
 6606:         $default{'helpdeskmail'} = 'supportemail';
 6607:         $default{'lonstatusmail'} = 'adminemail';
 6608:         $default{'requestsmail'} = 'adminemail';
 6609:         $default{'updatesmail'} = 'adminemail';
 6610:         foreach my $item (@contacts) {
 6611:            if ($to{$item} ne $default{$item}) {
 6612:               $changes{$item} = 1;
 6613:            } 
 6614:         }
 6615:         foreach my $type (@mailings) {
 6616:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 6617:                
 6618:                 push(@{$changes{$type}},@{$newsetting{$type}});
 6619:             }
 6620:             if ($others{$type} ne '') {
 6621:                 push(@{$changes{$type}},'others');
 6622:             }
 6623:             if ($type eq 'helpdeskmail') {
 6624:                 if ($bcc{$type} ne '') {
 6625:                     push(@{$changes{$type}},'bcc');
 6626:                 }
 6627:             }
 6628:         }
 6629:     }
 6630:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 6631:                                              $dom);
 6632:     if ($putresult eq 'ok') {
 6633:         if (keys(%changes) > 0) {
 6634:             my ($titles,$short_titles)  = &contact_titles();
 6635:             $resulttext = &mt('Changes made:').'<ul>';
 6636:             foreach my $item (@contacts) {
 6637:                 if ($changes{$item}) {
 6638:                     $resulttext .= '<li>'.$titles->{$item}.
 6639:                                     &mt(' set to: ').
 6640:                                     '<span class="LC_cusr_emph">'.
 6641:                                     $to{$item}.'</span></li>';
 6642:                 }
 6643:             }
 6644:             foreach my $type (@mailings) {
 6645:                 if (ref($changes{$type}) eq 'ARRAY') {
 6646:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 6647:                     my @text;
 6648:                     foreach my $item (@{$newsetting{$type}}) {
 6649:                         push(@text,$short_titles->{$item});
 6650:                     }
 6651:                     if ($others{$type} ne '') {
 6652:                         push(@text,$others{$type});
 6653:                     }
 6654:                     $resulttext .= '<span class="LC_cusr_emph">'.
 6655:                                    join(', ',@text).'</span>';
 6656:                     if ($type eq 'helpdeskmail') {
 6657:                         if ($bcc{$type} ne '') {
 6658:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 6659:                         }
 6660:                     }
 6661:                     $resulttext .= '</li>';
 6662:                 }
 6663:             }
 6664:             $resulttext .= '</ul>';
 6665:         } else {
 6666:             $resulttext = &mt('No changes made to contact information');
 6667:         }
 6668:     } else {
 6669:         $resulttext = '<span class="LC_error">'.
 6670:             &mt('An error occurred: [_1].',$putresult).'</span>';
 6671:     }
 6672:     return $resulttext;
 6673: }
 6674: 
 6675: sub modify_usercreation {
 6676:     my ($dom,%domconfig) = @_;
 6677:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate);
 6678:     my $warningmsg;
 6679:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 6680:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 6681:             $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 6682:         }
 6683:     }
 6684:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 6685:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 6686:     my @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 6687:     my @contexts = ('author','course','requestcrs','selfcreate');
 6688:     foreach my $item(@contexts) {
 6689:         if ($item eq 'selfcreate') {
 6690:             @{$cancreate{$item}} = &Apache::loncommon::get_env_multiple('form.can_createuser_'.$item);
 6691:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6692:             if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth'))) {
 6693:                 if (ref($cancreate{$item}) eq 'ARRAY') { 
 6694:                     if (grep(/^login$/,@{$cancreate{$item}})) {
 6695:                         $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.').' '.&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.');   
 6696:                     }
 6697:                 }
 6698:             }
 6699:         } else {
 6700:             $cancreate{$item} = $env{'form.can_createuser_'.$item};
 6701:         }
 6702:     }
 6703:     my ($othertitle,$usertypes,$types) = 
 6704:         &Apache::loncommon::sorted_inst_types($dom);
 6705:     if (ref($types) eq 'ARRAY') {
 6706:         if (@{$types} > 0) {
 6707:             @{$cancreate{'statustocreate'}} = 
 6708:                 &Apache::loncommon::get_env_multiple('form.statustocreate');
 6709:         } else {
 6710:             @{$cancreate{'statustocreate'}} = ();
 6711:         }
 6712:         push(@contexts,'statustocreate');
 6713:     }
 6714:     &process_captcha('cancreate',\%changes,\%cancreate,\%curr_usercreation);
 6715:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 6716:         foreach my $item (@contexts) {
 6717:             if (($item eq 'selfcreate') || ($item eq 'statustocreate')) {
 6718:                 if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6719:                     foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 6720:                         if (ref($cancreate{$item}) eq 'ARRAY') {
 6721:                             if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 6722:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6723:                                     push(@{$changes{'cancreate'}},$item);
 6724:                                 }
 6725:                             }
 6726:                         }
 6727:                     }
 6728:                 } else {
 6729:                     if ($curr_usercreation{'cancreate'}{$item} eq '') {
 6730:                         if (@{$cancreate{$item}} > 0) {
 6731:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6732:                                 push(@{$changes{'cancreate'}},$item);
 6733:                             }
 6734:                         }
 6735:                     } else {
 6736:                         if ($curr_usercreation{'cancreate'}{$item} eq 'any') {
 6737:                             if (@{$cancreate{$item}} < 3) {
 6738:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6739:                                     push(@{$changes{'cancreate'}},$item);
 6740:                                 }
 6741:                             }
 6742:                         } elsif ($curr_usercreation{'cancreate'}{$item} eq 'none') {
 6743:                             if (@{$cancreate{$item}} > 0) {
 6744:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6745:                                     push(@{$changes{'cancreate'}},$item);
 6746:                                 }
 6747:                             }
 6748:                         } elsif (!grep(/^$curr_usercreation{'cancreate'}{$item}$/,@{$cancreate{$item}})) {
 6749:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6750:                                 push(@{$changes{'cancreate'}},$item);
 6751:                             }
 6752:                         }
 6753:                     }
 6754:                 }
 6755:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6756:                     foreach my $type (@{$cancreate{$item}}) {
 6757:                         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6758:                             if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 6759:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6760:                                     push(@{$changes{'cancreate'}},$item);
 6761:                                 }
 6762:                             }
 6763:                         } elsif (($curr_usercreation{'cancreate'}{$item} ne 'any') &&
 6764:                                  ($curr_usercreation{'cancreate'}{$item} ne 'none')) {
 6765:                             if ($curr_usercreation{'cancreate'}{$item} ne $type) {
 6766:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6767:                                     push(@{$changes{'cancreate'}},$item);
 6768:                                 }
 6769:                             }
 6770:                         }
 6771:                     }
 6772:                 }
 6773:             } else {
 6774:                 if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 6775:                     push(@{$changes{'cancreate'}},$item);
 6776:                 }
 6777:             }
 6778:         }
 6779:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 6780:         foreach my $item (@contexts) {
 6781:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 6782:                 if ($cancreate{$item} ne 'any') {
 6783:                     push(@{$changes{'cancreate'}},$item);
 6784:                 }
 6785:             } else {
 6786:                 if ($cancreate{$item} ne 'none') {
 6787:                     push(@{$changes{'cancreate'}},$item);
 6788:                 }
 6789:             }
 6790:         }
 6791:     } else {
 6792:         foreach my $item (@contexts)  {
 6793:             push(@{$changes{'cancreate'}},$item);
 6794:         }
 6795:     }
 6796: 
 6797:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 6798:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 6799:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 6800:                 push(@{$changes{'username_rule'}},$type);
 6801:             }
 6802:         }
 6803:         foreach my $type (@username_rule) {
 6804:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 6805:                 push(@{$changes{'username_rule'}},$type);
 6806:             }
 6807:         }
 6808:     } else {
 6809:         push(@{$changes{'username_rule'}},@username_rule);
 6810:     }
 6811: 
 6812:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 6813:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 6814:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 6815:                 push(@{$changes{'id_rule'}},$type);
 6816:             }
 6817:         }
 6818:         foreach my $type (@id_rule) {
 6819:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 6820:                 push(@{$changes{'id_rule'}},$type);
 6821:             }
 6822:         }
 6823:     } else {
 6824:         push(@{$changes{'id_rule'}},@id_rule);
 6825:     }
 6826: 
 6827:     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 6828:         foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 6829:             if (!grep(/^\Q$type\E$/,@email_rule)) {
 6830:                 push(@{$changes{'email_rule'}},$type);
 6831:             }
 6832:         }
 6833:         foreach my $type (@email_rule) {
 6834:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 6835:                 push(@{$changes{'email_rule'}},$type);
 6836:             }
 6837:         }
 6838:     } else {
 6839:         push(@{$changes{'email_rule'}},@email_rule);
 6840:     }
 6841: 
 6842:     my @authen_contexts = ('author','course','domain');
 6843:     my @authtypes = ('int','krb4','krb5','loc');
 6844:     my %authhash;
 6845:     foreach my $item (@authen_contexts) {
 6846:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 6847:         foreach my $auth (@authtypes) {
 6848:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 6849:                 $authhash{$item}{$auth} = 1;
 6850:             } else {
 6851:                 $authhash{$item}{$auth} = 0;
 6852:             }
 6853:         }
 6854:     }
 6855:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 6856:         foreach my $item (@authen_contexts) {
 6857:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 6858:                 foreach my $auth (@authtypes) {
 6859:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 6860:                         push(@{$changes{'authtypes'}},$item);
 6861:                         last;
 6862:                     }
 6863:                 }
 6864:             }
 6865:         }
 6866:     } else {
 6867:         foreach my $item (@authen_contexts) {
 6868:             push(@{$changes{'authtypes'}},$item);
 6869:         }
 6870:     }
 6871: 
 6872:     my %usercreation_hash =  (
 6873:             usercreation => {
 6874:                               cancreate     => \%cancreate,
 6875:                               username_rule => \@username_rule,
 6876:                               id_rule       => \@id_rule,
 6877:                               email_rule    => \@email_rule,
 6878:                               authtypes     => \%authhash,
 6879:                             }
 6880:             );
 6881: 
 6882:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 6883:                                              $dom);
 6884: 
 6885:     my %selfcreatetypes = (
 6886:                              sso   => 'users authenticated by institutional single sign on',
 6887:                              login => 'users authenticated by institutional log-in',
 6888:                              email => 'users who provide a valid e-mail address for use as the username',
 6889:                           );
 6890:     if ($putresult eq 'ok') {
 6891:         if (keys(%changes) > 0) {
 6892:             $resulttext = &mt('Changes made:').'<ul>';
 6893:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 6894:                 my %lt = &usercreation_types();
 6895:                 foreach my $type (@{$changes{'cancreate'}}) {
 6896:                     my $chgtext;
 6897:                     unless (($type eq 'statustocreate') || ($type eq 'captcha') || ($type eq 'recaptchakeys')) {
 6898:                         $chgtext = $lt{$type}.', ';
 6899:                     }
 6900:                     if ($type eq 'selfcreate') {
 6901:                         if (@{$cancreate{$type}} == 0) {
 6902:                             $chgtext .= &mt('creation of a new user account is not permitted.');
 6903:                         } else {
 6904:                             $chgtext .= &mt('creation of a new account is permitted for:').'<ul>';
 6905:                             foreach my $case (@{$cancreate{$type}}) {
 6906:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 6907:                             }
 6908:                             $chgtext .= '</ul>';
 6909:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 6910:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 6911:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 6912:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 6913:                                             $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6914:                                         }
 6915:                                     }
 6916:                                 }
 6917:                             }
 6918:                         }
 6919:                     } elsif ($type eq 'statustocreate') {
 6920:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 6921:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 6922:                             if (@{$cancreate{'selfcreate'}} > 0) {
 6923:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 6924: 
 6925:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 6926:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 6927:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 6928:                                     } 
 6929:                                 } elsif (ref($usertypes) eq 'HASH') {
 6930:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6931:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 6932:                                     } else {
 6933:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 6934:                                     }
 6935:                                     $chgtext .= '<ul>';
 6936:                                     foreach my $case (@{$cancreate{$type}}) {
 6937:                                         if ($case eq 'default') {
 6938:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 6939:                                         } else {
 6940:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 6941:                                         }
 6942:                                     }
 6943:                                     $chgtext .= '</ul>';
 6944:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 6945:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').'</span>';
 6946:                                     }
 6947:                                 }
 6948:                             } else {
 6949:                                 if (@{$cancreate{$type}} == 0) {
 6950:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 6951:                                 } else {
 6952:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 6953:                                 }
 6954:                             }
 6955:                         }
 6956:                     } elsif ($type eq 'captcha') {
 6957:                         if ($cancreate{$type} eq 'notused') {
 6958:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 6959:                         } else {
 6960:                             my %captchas = &captcha_phrases();
 6961:                             if ($captchas{$cancreate{$type}}) {
 6962:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$cancreate{$type}}.");
 6963:                             } else {
 6964:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.'); 
 6965:                             }
 6966:                         }
 6967:                     } elsif ($type eq 'recaptchakeys') {
 6968:                         my ($privkey,$pubkey);
 6969:                         if (ref($cancreate{$type}) eq 'HASH') {
 6970:                             $pubkey = $cancreate{$type}{'public'};
 6971:                             $privkey = $cancreate{$type}{'private'};
 6972:                         }
 6973:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 6974:                         if (!$pubkey) {
 6975:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 6976:                         } else {
 6977:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 6978:                         }
 6979:                         if (!$privkey) {
 6980:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 6981:                         } else {
 6982:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 6983:                         }
 6984:                         $chgtext .= '</ul>';
 6985:                     } else {
 6986:                         if ($cancreate{$type} eq 'none') {
 6987:                             $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 6988:                         } elsif ($cancreate{$type} eq 'any') {
 6989:                             $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 6990:                         } elsif ($cancreate{$type} eq 'official') {
 6991:                             $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 6992:                         } elsif ($cancreate{$type} eq 'unofficial') {
 6993:                             $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 6994:                         }
 6995:                     }
 6996:                     $resulttext .= '<li>'.$chgtext.'</li>';
 6997:                 }
 6998:             }
 6999:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 7000:                 my ($rules,$ruleorder) = 
 7001:                     &Apache::lonnet::inst_userrules($dom,'username');
 7002:                 my $chgtext = '<ul>';
 7003:                 foreach my $type (@username_rule) {
 7004:                     if (ref($rules->{$type}) eq 'HASH') {
 7005:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 7006:                     }
 7007:                 }
 7008:                 $chgtext .= '</ul>';
 7009:                 if (@username_rule > 0) {
 7010:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 7011:                 } else {
 7012:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 7013:                 }
 7014:             }
 7015:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 7016:                 my ($idrules,$idruleorder) = 
 7017:                     &Apache::lonnet::inst_userrules($dom,'id');
 7018:                 my $chgtext = '<ul>';
 7019:                 foreach my $type (@id_rule) {
 7020:                     if (ref($idrules->{$type}) eq 'HASH') {
 7021:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 7022:                     }
 7023:                 }
 7024:                 $chgtext .= '</ul>';
 7025:                 if (@id_rule > 0) {
 7026:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 7027:                 } else {
 7028:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 7029:                 }
 7030:             }
 7031:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 7032:                 my ($emailrules,$emailruleorder) =
 7033:                     &Apache::lonnet::inst_userrules($dom,'email');
 7034:                 my $chgtext = '<ul>';
 7035:                 foreach my $type (@email_rule) {
 7036:                     if (ref($emailrules->{$type}) eq 'HASH') {
 7037:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 7038:                     }
 7039:                 }
 7040:                 $chgtext .= '</ul>';
 7041:                 if (@email_rule > 0) {
 7042:                     $resulttext .= '<li>'.&mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').$chgtext.'</li>';
 7043:                 } else {
 7044:                     $resulttext .= '<li>'.&mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').'</li>';
 7045:                 }
 7046:             }
 7047: 
 7048:             my %authname = &authtype_names();
 7049:             my %context_title = &context_names();
 7050:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 7051:                 my $chgtext = '<ul>';
 7052:                 foreach my $type (@{$changes{'authtypes'}}) {
 7053:                     my @allowed;
 7054:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 7055:                     foreach my $auth (@authtypes) {
 7056:                         if ($authhash{$type}{$auth}) {
 7057:                             push(@allowed,$authname{$auth});
 7058:                         }
 7059:                     }
 7060:                     if (@allowed > 0) {
 7061:                         $chgtext .= join(', ',@allowed).'</li>';
 7062:                     } else {
 7063:                         $chgtext .= &mt('none').'</li>';
 7064:                     }
 7065:                 }
 7066:                 $chgtext .= '</ul>';
 7067:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 7068:                 $resulttext .= '</li>';
 7069:             }
 7070:             $resulttext .= '</ul>';
 7071:         } else {
 7072:             $resulttext = &mt('No changes made to user creation settings');
 7073:         }
 7074:     } else {
 7075:         $resulttext = '<span class="LC_error">'.
 7076:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7077:     }
 7078:     if ($warningmsg ne '') {
 7079:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 7080:     }
 7081:     return $resulttext;
 7082: }
 7083: 
 7084: sub process_captcha {
 7085:     my ($container,$changes,$newsettings,$current) = @_;
 7086:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 7087:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 7088:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 7089:         $newsettings->{'captcha'} = 'original';
 7090:     }
 7091:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 7092:         if ($container eq 'cancreate') { 
 7093:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7094:                 push(@{$changes->{'cancreate'}},'captcha');
 7095:             } elsif (!defined($changes->{'cancreate'})) {
 7096:                 $changes->{'cancreate'} = ['captcha'];
 7097:             }
 7098:         } else {
 7099:             $changes->{'captcha'} = 1;
 7100:         }
 7101:     }
 7102:     my ($newpub,$newpriv,$currpub,$currpriv);
 7103:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 7104:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 7105:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 7106:         $newpub =~ s/\W//g;
 7107:         $newpriv =~ s/\W//g;
 7108:         $newsettings->{'recaptchakeys'} = {
 7109:                                              public  => $newpub,
 7110:                                              private => $newpriv,
 7111:                                           };
 7112:     }
 7113:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 7114:         $currpub = $current->{'recaptchakeys'}{'public'};
 7115:         $currpriv = $current->{'recaptchakeys'}{'private'};
 7116:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 7117:             $newsettings->{'recaptchakeys'} = {
 7118:                                                  public  => '',
 7119:                                                  private => '',
 7120:                                               }
 7121:         }
 7122:     }
 7123:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 7124:         if ($container eq 'cancreate') {
 7125:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7126:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 7127:             } elsif (!defined($changes->{'cancreate'})) {
 7128:                 $changes->{'cancreate'} = ['recaptchakeys'];
 7129:             }
 7130:         } else {
 7131:             $changes->{'recaptchakeys'} = 1; 
 7132:         }
 7133:     }
 7134:     return;
 7135: }
 7136: 
 7137: sub modify_usermodification {
 7138:     my ($dom,%domconfig) = @_;
 7139:     my ($resulttext,%curr_usermodification,%changes);
 7140:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 7141:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 7142:             $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 7143:         }
 7144:     }
 7145:     my @contexts = ('author','course','selfcreate');
 7146:     my %context_title = (
 7147:                            author => 'In author context',
 7148:                            course => 'In course context',
 7149:                            selfcreate => 'When self creating account', 
 7150:                         );
 7151:     my @fields = ('lastname','firstname','middlename','generation',
 7152:                   'permanentemail','id');
 7153:     my %roles = (
 7154:                   author => ['ca','aa'],
 7155:                   course => ['st','ep','ta','in','cr'],
 7156:                 );
 7157:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7158:     if (ref($types) eq 'ARRAY') {
 7159:         push(@{$types},'default');
 7160:         $usertypes->{'default'} = $othertitle;
 7161:     }
 7162:     $roles{'selfcreate'} = $types;  
 7163:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7164:     my %modifyhash;
 7165:     foreach my $context (@contexts) {
 7166:         foreach my $role (@{$roles{$context}}) {
 7167:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 7168:             foreach my $item (@fields) {
 7169:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 7170:                     $modifyhash{$context}{$role}{$item} = 1;
 7171:                 } else {
 7172:                     $modifyhash{$context}{$role}{$item} = 0;
 7173:                 }
 7174:             }
 7175:         }
 7176:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 7177:             foreach my $role (@{$roles{$context}}) {
 7178:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 7179:                     foreach my $field (@fields) {
 7180:                         if ($modifyhash{$context}{$role}{$field} ne 
 7181:                                 $curr_usermodification{$context}{$role}{$field}) {
 7182:                             push(@{$changes{$context}},$role);
 7183:                             last;
 7184:                         }
 7185:                     }
 7186:                 }
 7187:             }
 7188:         } else {
 7189:             foreach my $context (@contexts) {
 7190:                 foreach my $role (@{$roles{$context}}) {
 7191:                     push(@{$changes{$context}},$role);
 7192:                 }
 7193:             }
 7194:         }
 7195:     }
 7196:     my %usermodification_hash =  (
 7197:                                    usermodification => \%modifyhash,
 7198:                                  );
 7199:     my $putresult = &Apache::lonnet::put_dom('configuration',
 7200:                                              \%usermodification_hash,$dom);
 7201:     if ($putresult eq 'ok') {
 7202:         if (keys(%changes) > 0) {
 7203:             $resulttext = &mt('Changes made: ').'<ul>';
 7204:             foreach my $context (@contexts) {
 7205:                 if (ref($changes{$context}) eq 'ARRAY') {
 7206:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 7207:                     if (ref($changes{$context}) eq 'ARRAY') {
 7208:                         foreach my $role (@{$changes{$context}}) {
 7209:                             my $rolename;
 7210:                             if ($context eq 'selfcreate') {
 7211:                                 $rolename = $role;
 7212:                                 if (ref($usertypes) eq 'HASH') {
 7213:                                     if ($usertypes->{$role} ne '') {
 7214:                                         $rolename = $usertypes->{$role};
 7215:                                     }
 7216:                                 }
 7217:                             } else {
 7218:                                 if ($role eq 'cr') {
 7219:                                     $rolename = &mt('Custom');
 7220:                                 } else {
 7221:                                     $rolename = &Apache::lonnet::plaintext($role);
 7222:                                 }
 7223:                             }
 7224:                             my @modifiable;
 7225:                             if ($context eq 'selfcreate') {
 7226:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Self-creation of account by users with status: [_1]',$rolename).'</span> - '.&mt('modifiable fields (if institutional data blank): ');
 7227:                             } else {
 7228:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 7229:                             }
 7230:                             foreach my $field (@fields) {
 7231:                                 if ($modifyhash{$context}{$role}{$field}) {
 7232:                                     push(@modifiable,$fieldtitles{$field});
 7233:                                 }
 7234:                             }
 7235:                             if (@modifiable > 0) {
 7236:                                 $resulttext .= join(', ',@modifiable);
 7237:                             } else {
 7238:                                 $resulttext .= &mt('none'); 
 7239:                             }
 7240:                             $resulttext .= '</li>';
 7241:                         }
 7242:                         $resulttext .= '</ul></li>';
 7243:                     }
 7244:                 }
 7245:             }
 7246:             $resulttext .= '</ul>';
 7247:         } else {
 7248:             $resulttext = &mt('No changes made to user modification settings');
 7249:         }
 7250:     } else {
 7251:         $resulttext = '<span class="LC_error">'.
 7252:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7253:     }
 7254:     return $resulttext;
 7255: }
 7256: 
 7257: sub modify_defaults {
 7258:     my ($dom,$r) = @_;
 7259:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 7260:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7261:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 7262:     my @authtypes = ('internal','krb4','krb5','localauth');
 7263:     foreach my $item (@items) {
 7264:         $newvalues{$item} = $env{'form.'.$item};
 7265:         if ($item eq 'auth_def') {
 7266:             if ($newvalues{$item} ne '') {
 7267:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 7268:                     push(@errors,$item);
 7269:                 }
 7270:             }
 7271:         } elsif ($item eq 'lang_def') {
 7272:             if ($newvalues{$item} ne '') {
 7273:                 if ($newvalues{$item} =~ /^(\w+)/) {
 7274:                     my $langcode = $1;
 7275:                     if ($langcode ne 'x_chef') {
 7276:                         if (code2language($langcode) eq '') {
 7277:                             push(@errors,$item);
 7278:                         }
 7279:                     }
 7280:                 } else {
 7281:                     push(@errors,$item);
 7282:                 }
 7283:             }
 7284:         } elsif ($item eq 'timezone_def') {
 7285:             if ($newvalues{$item} ne '') {
 7286:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 7287:                     push(@errors,$item);   
 7288:                 }
 7289:             }
 7290:         } elsif ($item eq 'datelocale_def') {
 7291:             if ($newvalues{$item} ne '') {
 7292:                 my @datelocale_ids = DateTime::Locale->ids();
 7293:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 7294:                     push(@errors,$item);
 7295:                 }
 7296:             }
 7297:         } elsif ($item eq 'portal_def') {
 7298:             if ($newvalues{$item} ne '') {
 7299:                 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])\/?$/) {
 7300:                     push(@errors,$item);
 7301:                 }
 7302:             }
 7303:         }
 7304:         if (grep(/^\Q$item\E$/,@errors)) {
 7305:             $newvalues{$item} = $domdefaults{$item};
 7306:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 7307:             $changes{$item} = 1;
 7308:         }
 7309:         $domdefaults{$item} = $newvalues{$item};
 7310:     }
 7311:     my %defaults_hash = (
 7312:                          defaults => \%newvalues,
 7313:                         );
 7314:     my $title = &defaults_titles();
 7315:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 7316:                                              $dom);
 7317:     if ($putresult eq 'ok') {
 7318:         if (keys(%changes) > 0) {
 7319:             $resulttext = &mt('Changes made:').'<ul>';
 7320:             my $version = $r->dir_config('lonVersion');
 7321:             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";
 7322:             foreach my $item (sort(keys(%changes))) {
 7323:                 my $value = $env{'form.'.$item};
 7324:                 if ($value eq '') {
 7325:                     $value = &mt('none');
 7326:                 } elsif ($item eq 'auth_def') {
 7327:                     my %authnames = &authtype_names();
 7328:                     my %shortauth = (
 7329:                              internal => 'int',
 7330:                              krb4 => 'krb4',
 7331:                              krb5 => 'krb5',
 7332:                              localauth  => 'loc',
 7333:                     );
 7334:                     $value = $authnames{$shortauth{$value}};
 7335:                 }
 7336:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 7337:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 7338:             }
 7339:             $resulttext .= '</ul>';
 7340:             $mailmsgtext .= "\n";
 7341:             my $cachetime = 24*60*60;
 7342:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7343:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 7344:                 my $sysmail = $r->dir_config('lonSysEMail');
 7345:                 &Apache::lonmsg::sendemail($sysmail,"LON-CAPA Domain Settings Change - $dom",$mailmsgtext);
 7346:             }
 7347:         } else {
 7348:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 7349:         }
 7350:     } else {
 7351:         $resulttext = '<span class="LC_error">'.
 7352:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7353:     }
 7354:     if (@errors > 0) {
 7355:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 7356:         foreach my $item (@errors) {
 7357:             $resulttext .= ' "'.$title->{$item}.'",';
 7358:         }
 7359:         $resulttext =~ s/,$//;
 7360:     }
 7361:     return $resulttext;
 7362: }
 7363: 
 7364: sub modify_scantron {
 7365:     my ($r,$dom,$confname,%domconfig) = @_;
 7366:     my ($resulttext,%confhash,%changes,$errors);
 7367:     my $custom = 'custom.tab';
 7368:     my $default = 'default.tab';
 7369:     my $servadm = $r->dir_config('lonAdmEMail');
 7370:     my ($configuserok,$author_ok,$switchserver) = 
 7371:         &config_check($dom,$confname,$servadm);
 7372:     if ($env{'form.scantronformat.filename'} ne '') {
 7373:         my $error;
 7374:         if ($configuserok eq 'ok') {
 7375:             if ($switchserver) {
 7376:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 7377:             } else {
 7378:                 if ($author_ok eq 'ok') {
 7379:                     my ($result,$scantronurl) =
 7380:                         &publishlogo($r,'upload','scantronformat',$dom,
 7381:                                      $confname,'scantron','','',$custom);
 7382:                     if ($result eq 'ok') {
 7383:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 7384:                         $changes{'scantronformat'} = 1;
 7385:                     } else {
 7386:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 7387:                     }
 7388:                 } else {
 7389:                     $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);
 7390:                 }
 7391:             }
 7392:         } else {
 7393:             $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);
 7394:         }
 7395:         if ($error) {
 7396:             &Apache::lonnet::logthis($error);
 7397:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7398:         }
 7399:     }
 7400:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 7401:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 7402:             if ($env{'form.scantronformat_del'}) {
 7403:                 $confhash{'scantron'}{'scantronformat'} = '';
 7404:                 $changes{'scantronformat'} = 1;
 7405:             }
 7406:         }
 7407:     }
 7408:     if (keys(%confhash) > 0) {
 7409:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 7410:                                                  $dom);
 7411:         if ($putresult eq 'ok') {
 7412:             if (keys(%changes) > 0) {
 7413:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 7414:                     $resulttext = &mt('Changes made:').'<ul>';
 7415:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 7416:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 7417:                     } else {
 7418:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 7419:                     }
 7420:                     $resulttext .= '</ul>';
 7421:                 } else {
 7422:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 7423:                 }
 7424:                 $resulttext .= '</ul>';
 7425:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7426:             } else {
 7427:                 $resulttext = &mt('No changes made to bubblesheet format file');
 7428:             }
 7429:         } else {
 7430:             $resulttext = '<span class="LC_error">'.
 7431:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 7432:         }
 7433:     } else {
 7434:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 7435:     }
 7436:     if ($errors) {
 7437:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7438:                        $errors.'</ul>';
 7439:     }
 7440:     return $resulttext;
 7441: }
 7442: 
 7443: sub modify_coursecategories {
 7444:     my ($dom,%domconfig) = @_;
 7445:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 7446:         $cathash);
 7447:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 7448:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7449:         $cathash = $domconfig{'coursecategories'}{'cats'};
 7450:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 7451:             $changes{'togglecats'} = 1;
 7452:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 7453:         }
 7454:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 7455:             $changes{'categorize'} = 1;
 7456:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 7457:         }
 7458:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 7459:             $changes{'togglecatscomm'} = 1;
 7460:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 7461:         }
 7462:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 7463:             $changes{'categorizecomm'} = 1;
 7464:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 7465:         }
 7466:     } else {
 7467:         $changes{'togglecats'} = 1;
 7468:         $changes{'categorize'} = 1;
 7469:         $changes{'togglecatscomm'} = 1;
 7470:         $changes{'categorizecomm'} = 1;
 7471:         $domconfig{'coursecategories'} = {
 7472:                                              togglecats => $env{'form.togglecats'},
 7473:                                              categorize => $env{'form.categorize'},
 7474:                                              togglecatscomm => $env{'form.togglecatscomm'},
 7475:                                              categorizecomm => $env{'form.categorizecomm'},
 7476:                                          };
 7477:     }
 7478:     if (ref($cathash) eq 'HASH') {
 7479:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 7480:             push (@deletecategory,'instcode::0');
 7481:         }
 7482:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 7483:             push(@deletecategory,'communities::0');
 7484:         }
 7485:     }
 7486:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 7487:     if (ref($cathash) eq 'HASH') {
 7488:         if (@deletecategory > 0) {
 7489:             #FIXME Need to remove category from all courses using a deleted category 
 7490:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 7491:             foreach my $item (@deletecategory) {
 7492:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 7493:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 7494:                     $deletions{$item} = 1;
 7495:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 7496:                 }
 7497:             }
 7498:         }
 7499:         foreach my $item (keys(%{$cathash})) {
 7500:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 7501:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 7502:                 $reorderings{$item} = 1;
 7503:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 7504:             }
 7505:             if ($env{'form.addcategory_name_'.$item} ne '') {
 7506:                 my $newcat = $env{'form.addcategory_name_'.$item};
 7507:                 my $newdepth = $depth+1;
 7508:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7509:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 7510:                 $adds{$newitem} = 1; 
 7511:             }
 7512:             if ($env{'form.subcat_'.$item} ne '') {
 7513:                 my $newcat = $env{'form.subcat_'.$item};
 7514:                 my $newdepth = $depth+1;
 7515:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7516:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 7517:                 $adds{$newitem} = 1;
 7518:             }
 7519:         }
 7520:     }
 7521:     if ($env{'form.instcode'} eq '1') {
 7522:         if (ref($cathash) eq 'HASH') {
 7523:             my $newitem = 'instcode::0';
 7524:             if ($cathash->{$newitem} eq '') {  
 7525:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7526:                 $adds{$newitem} = 1;
 7527:             }
 7528:         } else {
 7529:             my $newitem = 'instcode::0';
 7530:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7531:             $adds{$newitem} = 1;
 7532:         }
 7533:     }
 7534:     if ($env{'form.communities'} eq '1') {
 7535:         if (ref($cathash) eq 'HASH') {
 7536:             my $newitem = 'communities::0';
 7537:             if ($cathash->{$newitem} eq '') {
 7538:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7539:                 $adds{$newitem} = 1;
 7540:             }
 7541:         } else {
 7542:             my $newitem = 'communities::0';
 7543:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7544:             $adds{$newitem} = 1;
 7545:         }
 7546:     }
 7547:     if ($env{'form.addcategory_name'} ne '') {
 7548:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 7549:             ($env{'form.addcategory_name'} ne 'communities')) {
 7550:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 7551:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 7552:             $adds{$newitem} = 1;
 7553:         }
 7554:     }
 7555:     my $putresult;
 7556:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7557:         if (keys(%deletions) > 0) {
 7558:             foreach my $key (keys(%deletions)) {
 7559:                 if ($predelallitems{$key} ne '') {
 7560:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 7561:                 }
 7562:             }
 7563:         }
 7564:         my (@chkcats,@chktrails,%chkallitems);
 7565:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 7566:         if (ref($chkcats[0]) eq 'ARRAY') {
 7567:             my $depth = 0;
 7568:             my $chg = 0;
 7569:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 7570:                 my $name = $chkcats[0][$i];
 7571:                 my $item;
 7572:                 if ($name eq '') {
 7573:                     $chg ++;
 7574:                 } else {
 7575:                     $item = &escape($name).'::0';
 7576:                     if ($chg) {
 7577:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 7578:                     }
 7579:                     $depth ++; 
 7580:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 7581:                     $depth --;
 7582:                 }
 7583:             }
 7584:         }
 7585:     }
 7586:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7587:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 7588:         if ($putresult eq 'ok') {
 7589:             my %title = (
 7590:                          togglecats     => 'Show/Hide a course in catalog',
 7591:                          categorize     => 'Assign a category to a course',
 7592:                          togglecatscomm => 'Show/Hide a community in catalog',
 7593:                          categorizecomm => 'Assign a category to a community',
 7594:                         );
 7595:             my %level = (
 7596:                          dom  => 'set in Domain ("Modify Course/Community")',
 7597:                          crs  => 'set in Course ("Course Configuration")',
 7598:                          comm => 'set in Community ("Community Configuration")',
 7599:                         );
 7600:             $resulttext = &mt('Changes made:').'<ul>';
 7601:             if ($changes{'togglecats'}) {
 7602:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 7603:             }
 7604:             if ($changes{'categorize'}) {
 7605:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 7606:             }
 7607:             if ($changes{'togglecatscomm'}) {
 7608:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 7609:             }
 7610:             if ($changes{'categorizecomm'}) {
 7611:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 7612:             }
 7613:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7614:                 my $cathash;
 7615:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7616:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 7617:                 } else {
 7618:                     $cathash = {};
 7619:                 } 
 7620:                 my (@cats,@trails,%allitems);
 7621:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 7622:                 if (keys(%deletions) > 0) {
 7623:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 7624:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 7625:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 7626:                     }
 7627:                     $resulttext .= '</ul></li>';
 7628:                 }
 7629:                 if (keys(%reorderings) > 0) {
 7630:                     my %sort_by_trail;
 7631:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 7632:                     foreach my $key (keys(%reorderings)) {
 7633:                         if ($allitems{$key} ne '') {
 7634:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7635:                         }
 7636:                     }
 7637:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7638:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7639:                     }
 7640:                     $resulttext .= '</ul></li>';
 7641:                 }
 7642:                 if (keys(%adds) > 0) {
 7643:                     my %sort_by_trail;
 7644:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 7645:                     foreach my $key (keys(%adds)) {
 7646:                         if ($allitems{$key} ne '') {
 7647:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7648:                         }
 7649:                     }
 7650:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7651:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7652:                     }
 7653:                     $resulttext .= '</ul></li>';
 7654:                 }
 7655:             }
 7656:             $resulttext .= '</ul>';
 7657:         } else {
 7658:             $resulttext = '<span class="LC_error">'.
 7659:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 7660:         }
 7661:     } else {
 7662:         $resulttext = &mt('No changes made to course and community categories');
 7663:     }
 7664:     return $resulttext;
 7665: }
 7666: 
 7667: sub modify_serverstatuses {
 7668:     my ($dom,%domconfig) = @_;
 7669:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 7670:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 7671:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 7672:     }
 7673:     my @pages = &serverstatus_pages();
 7674:     foreach my $type (@pages) {
 7675:         $newserverstatus{$type}{'namedusers'} = '';
 7676:         $newserverstatus{$type}{'machines'} = '';
 7677:         if (defined($env{'form.'.$type.'_namedusers'})) {
 7678:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 7679:             my @okusers;
 7680:             foreach my $user (@users) {
 7681:                 my ($uname,$udom) = split(/:/,$user);
 7682:                 if (($udom =~ /^$match_domain$/) &&   
 7683:                     (&Apache::lonnet::domain($udom)) &&
 7684:                     ($uname =~ /^$match_username$/)) {
 7685:                     if (!grep(/^\Q$user\E/,@okusers)) {
 7686:                         push(@okusers,$user);
 7687:                     }
 7688:                 }
 7689:             }
 7690:             if (@okusers > 0) {
 7691:                  @okusers = sort(@okusers);
 7692:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 7693:             }
 7694:         }
 7695:         if (defined($env{'form.'.$type.'_machines'})) {
 7696:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 7697:             my @okmachines;
 7698:             foreach my $ip (@machines) {
 7699:                 my @parts = split(/\./,$ip);
 7700:                 next if (@parts < 4);
 7701:                 my $badip = 0;
 7702:                 for (my $i=0; $i<4; $i++) {
 7703:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 7704:                         $badip = 1;
 7705:                         last;
 7706:                     }
 7707:                 }
 7708:                 if (!$badip) {
 7709:                     push(@okmachines,$ip);     
 7710:                 }
 7711:             }
 7712:             @okmachines = sort(@okmachines);
 7713:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 7714:         }
 7715:     }
 7716:     my %serverstatushash =  (
 7717:                                 serverstatuses => \%newserverstatus,
 7718:                             );
 7719:     foreach my $type (@pages) {
 7720:         foreach my $setting ('namedusers','machines') {
 7721:             my (@current,@new);
 7722:             if (ref($currserverstatus{$type}) eq 'HASH') {
 7723:                 if ($currserverstatus{$type}{$setting} ne '') { 
 7724:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 7725:                 }
 7726:             }
 7727:             if ($newserverstatus{$type}{$setting} ne '') {
 7728:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 7729:             }
 7730:             if (@current > 0) {
 7731:                 if (@new > 0) {
 7732:                     foreach my $item (@current) {
 7733:                         if (!grep(/^\Q$item\E$/,@new)) {
 7734:                             $changes{$type}{$setting} = 1;
 7735:                             last;
 7736:                         }
 7737:                     }
 7738:                     foreach my $item (@new) {
 7739:                         if (!grep(/^\Q$item\E$/,@current)) {
 7740:                             $changes{$type}{$setting} = 1;
 7741:                             last;
 7742:                         }
 7743:                     }
 7744:                 } else {
 7745:                     $changes{$type}{$setting} = 1;
 7746:                 }
 7747:             } elsif (@new > 0) {
 7748:                 $changes{$type}{$setting} = 1;
 7749:             }
 7750:         }
 7751:     }
 7752:     if (keys(%changes) > 0) {
 7753:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7754:         my $putresult = &Apache::lonnet::put_dom('configuration',
 7755:                                                  \%serverstatushash,$dom);
 7756:         if ($putresult eq 'ok') {
 7757:             $resulttext .= &mt('Changes made:').'<ul>';
 7758:             foreach my $type (@pages) {
 7759:                 if (ref($changes{$type}) eq 'HASH') {
 7760:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 7761:                     if ($changes{$type}{'namedusers'}) {
 7762:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 7763:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 7764:                         } else {
 7765:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 7766:                         }
 7767:                     }
 7768:                     if ($changes{$type}{'machines'}) {
 7769:                         if ($newserverstatus{$type}{'machines'} eq '') {
 7770:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 7771:                         } else {
 7772:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 7773:                         }
 7774: 
 7775:                     }
 7776:                     $resulttext .= '</ul></li>';
 7777:                 }
 7778:             }
 7779:             $resulttext .= '</ul>';
 7780:         } else {
 7781:             $resulttext = '<span class="LC_error">'.
 7782:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 7783: 
 7784:         }
 7785:     } else {
 7786:         $resulttext = &mt('No changes made to access to server status pages');
 7787:     }
 7788:     return $resulttext;
 7789: }
 7790: 
 7791: sub modify_helpsettings {
 7792:     my ($r,$dom,$confname,%domconfig) = @_;
 7793:     my ($resulttext,$errors,%changes,%helphash);
 7794:     my %defaultchecked = ('submitbugs' => 'on');
 7795:     my @offon = ('off','on');
 7796:     my @toggles = ('submitbugs');
 7797:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 7798:         foreach my $item (@toggles) {
 7799:             if ($defaultchecked{$item} eq 'on') { 
 7800:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7801:                     if ($env{'form.'.$item} eq '0') {
 7802:                         $changes{$item} = 1;
 7803:                     }
 7804:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7805:                     $changes{$item} = 1;
 7806:                 }
 7807:             } elsif ($defaultchecked{$item} eq 'off') {
 7808:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7809:                     if ($env{'form.'.$item} eq '1') {
 7810:                         $changes{$item} = 1;
 7811:                     }
 7812:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 7813:                     $changes{$item} = 1;
 7814:                 }
 7815:             }
 7816:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) { 
 7817:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 7818:             }
 7819:         }
 7820:     }
 7821:     my $putresult;
 7822:     if (keys(%changes) > 0) {
 7823:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 7824:         if ($putresult eq 'ok') {
 7825:             $resulttext = &mt('Changes made:').'<ul>';
 7826:             foreach my $item (sort(keys(%changes))) {
 7827:                 if ($item eq 'submitbugs') {
 7828:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 7829:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 7830:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 7831:                 }
 7832:             }
 7833:             $resulttext .= '</ul>';
 7834:         } else {
 7835:             $resulttext = &mt('No changes made to help settings');
 7836:             $errors .= '<li><span class="LC_error">'.
 7837:                        &mt('An error occurred storing the settings: [_1]',
 7838:                            $putresult).'</span></li>';
 7839:         }
 7840:     }
 7841:     if ($errors) {
 7842:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7843:                        $errors.'</ul>';
 7844:     }
 7845:     return $resulttext;
 7846: }
 7847: 
 7848: sub modify_coursedefaults {
 7849:     my ($dom,%domconfig) = @_;
 7850:     my ($resulttext,$errors,%changes,%defaultshash);
 7851:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 7852:     my @toggles = ('canuse_pdfforms');
 7853: 
 7854:     $defaultshash{'coursedefaults'} = {};
 7855: 
 7856:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 7857:         if ($domconfig{'coursedefaults'} eq '') {
 7858:             $domconfig{'coursedefaults'} = {};
 7859:         }
 7860:     }
 7861: 
 7862:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 7863:         foreach my $item (@toggles) {
 7864:             if ($defaultchecked{$item} eq 'on') {
 7865:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7866:                     ($env{'form.'.$item} eq '0')) {
 7867:                     $changes{$item} = 1;
 7868:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 7869:                     $changes{$item} = 1;
 7870:                 }
 7871:             } elsif ($defaultchecked{$item} eq 'off') {
 7872:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 7873:                     ($env{'form.'.$item} eq '1')) {
 7874:                     $changes{$item} = 1;
 7875:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 7876:                     $changes{$item} = 1;
 7877:                 }
 7878:             }
 7879:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 7880:         }
 7881:         my $currdefresponder = $domconfig{'coursedefaults'}{'anonsurvey_threshold'};
 7882:         my $newdefresponder = $env{'form.anonsurvey_threshold'};
 7883:         $newdefresponder =~ s/\D//g;
 7884:         if ($newdefresponder eq '' || $newdefresponder < 1) {
 7885:             $newdefresponder = 1;
 7886:         }
 7887:         $defaultshash{'coursedefaults'}{'anonsurvey_threshold'} = $newdefresponder;
 7888:         if ($currdefresponder ne $newdefresponder) {
 7889:             unless ($currdefresponder eq '' && $newdefresponder == 10) {
 7890:                 $changes{'anonsurvey_threshold'} = 1;
 7891:             }
 7892:         }
 7893:         my $officialcreds = $env{'form.official_credits'};
 7894:         $officialcreds =~ s/^[^\d\.]//g;
 7895:         my $unofficialcreds = $env{'form.unofficial_credits'};
 7896:         $unofficialcreds =~ s/^[^\d\.]//g;
 7897:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
 7898:                 ($env{'form.coursecredits'} eq '1')) {
 7899:                 $changes{'coursecredits'} = 1;
 7900:         } else {
 7901:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
 7902:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds)) {
 7903:                 $changes{'coursecredits'} = 1;
 7904:             }
 7905:         }
 7906:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
 7907:             official   => $officialcreds,
 7908:             unofficial => $unofficialcreds,
 7909:         }
 7910:     }
 7911:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 7912:                                              $dom);
 7913:     if ($putresult eq 'ok') {
 7914:         my %domdefaults;
 7915:         if (keys(%changes) > 0) {
 7916:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'})) {
 7917:                 %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7918:                 if ($changes{'canuse_pdfforms'}) {
 7919:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 7920:                 }
 7921:                 if ($changes{'coursecredits'}) {
 7922:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 7923:                         $domdefaults{'officialcredits'} =
 7924:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
 7925:                         $domdefaults{'unofficialcredits'} =
 7926:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
 7927:                     }
 7928:                 }
 7929:                 my $cachetime = 24*60*60;
 7930:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7931:             }
 7932:             $resulttext = &mt('Changes made:').'<ul>';
 7933:             foreach my $item (sort(keys(%changes))) {
 7934:                 if ($item eq 'canuse_pdfforms') {
 7935:                     if ($env{'form.'.$item} eq '1') {
 7936:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 7937:                     } else {
 7938:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 7939:                     }
 7940:                 } elsif ($item eq 'anonsurvey_threshold') {
 7941:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 7942:                 } elsif ($item eq 'coursecredits') {
 7943:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 7944:                         if (($domdefaults{'officialcredits'} eq '') &&
 7945:                             ($domdefaults{'unofficialcredits'} eq '')) {
 7946:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 7947:                         } else {
 7948:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
 7949:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
 7950:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
 7951:                                            '</ul>'.
 7952:                                            '</li>';
 7953:                         }
 7954:                     } else {
 7955:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 7956:                     }
 7957:                 }
 7958:             }
 7959:             $resulttext .= '</ul>';
 7960:         } else {
 7961:             $resulttext = &mt('No changes made to course defaults');
 7962:         }
 7963:     } else {
 7964:         $resulttext = '<span class="LC_error">'.
 7965:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7966:     }
 7967:     return $resulttext;
 7968: }
 7969: 
 7970: sub modify_usersessions {
 7971:     my ($dom,%domconfig) = @_;
 7972:     my @hostingtypes = ('version','excludedomain','includedomain');
 7973:     my @offloadtypes = ('primary','default');
 7974:     my %types = (
 7975:                   remote => \@hostingtypes,
 7976:                   hosted => \@hostingtypes,
 7977:                   spares => \@offloadtypes,
 7978:                 );
 7979:     my @prefixes = ('remote','hosted','spares');
 7980:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 7981:     my (%by_ip,%by_location,@intdoms);
 7982:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 7983:     my @locations = sort(keys(%by_location));
 7984:     my (%defaultshash,%changes);
 7985:     foreach my $prefix (@prefixes) {
 7986:         $defaultshash{'usersessions'}{$prefix} = {};
 7987:     }
 7988:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7989:     my $resulttext;
 7990:     my %iphost = &Apache::lonnet::get_iphost();
 7991:     foreach my $prefix (@prefixes) {
 7992:         next if ($prefix eq 'spares');
 7993:         foreach my $type (@{$types{$prefix}}) {
 7994:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 7995:             if ($type eq 'version') {
 7996:                 my $value = $env{'form.'.$prefix.'_'.$type};
 7997:                 my $okvalue;
 7998:                 if ($value ne '') {
 7999:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 8000:                         $okvalue = $value;
 8001:                     }
 8002:                 }
 8003:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8004:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 8005:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 8006:                             if ($inuse == 0) {
 8007:                                 $changes{$prefix}{$type} = 1;
 8008:                             } else {
 8009:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 8010:                                     $changes{$prefix}{$type} = 1;
 8011:                                 }
 8012:                                 if ($okvalue ne '') {
 8013:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8014:                                 } 
 8015:                             }
 8016:                         } else {
 8017:                             if (($inuse == 1) && ($okvalue ne '')) {
 8018:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8019:                                 $changes{$prefix}{$type} = 1;
 8020:                             }
 8021:                         }
 8022:                     } else {
 8023:                         if (($inuse == 1) && ($okvalue ne '')) {
 8024:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8025:                             $changes{$prefix}{$type} = 1;
 8026:                         }
 8027:                     }
 8028:                 } else {
 8029:                     if (($inuse == 1) && ($okvalue ne '')) {
 8030:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8031:                         $changes{$prefix}{$type} = 1;
 8032:                     }
 8033:                 }
 8034:             } else {
 8035:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 8036:                 my @okvals;
 8037:                 foreach my $val (@vals) {
 8038:                     if ($val =~ /:/) {
 8039:                         my @items = split(/:/,$val);
 8040:                         foreach my $item (@items) {
 8041:                             if (ref($by_location{$item}) eq 'ARRAY') {
 8042:                                 push(@okvals,$item);
 8043:                             }
 8044:                         }
 8045:                     } else {
 8046:                         if (ref($by_location{$val}) eq 'ARRAY') {
 8047:                             push(@okvals,$val);
 8048:                         }
 8049:                     }
 8050:                 }
 8051:                 @okvals = sort(@okvals);
 8052:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8053:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 8054:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 8055:                             if ($inuse == 0) {
 8056:                                 $changes{$prefix}{$type} = 1; 
 8057:                             } else {
 8058:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8059:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 8060:                                 if (@changed > 0) {
 8061:                                     $changes{$prefix}{$type} = 1;
 8062:                                 }
 8063:                             }
 8064:                         } else {
 8065:                             if ($inuse == 1) {
 8066:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8067:                                 $changes{$prefix}{$type} = 1;
 8068:                             }
 8069:                         } 
 8070:                     } else {
 8071:                         if ($inuse == 1) {
 8072:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8073:                             $changes{$prefix}{$type} = 1;
 8074:                         }
 8075:                     }
 8076:                 } else {
 8077:                     if ($inuse == 1) {
 8078:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8079:                         $changes{$prefix}{$type} = 1;
 8080:                     }
 8081:                 }
 8082:             }
 8083:         }
 8084:     }
 8085: 
 8086:     my @alldoms = &Apache::lonnet::all_domains();
 8087:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8088:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 8089:     my $savespares;
 8090: 
 8091:     foreach my $lonhost (sort(keys(%servers))) {
 8092:         my $serverhomeID =
 8093:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 8094:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 8095:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 8096:         my %spareschg;
 8097:         foreach my $type (@{$types{'spares'}}) {
 8098:             my @okspares;
 8099:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 8100:             foreach my $server (@checked) {
 8101:                 if (&Apache::lonnet::hostname($server) ne '') {
 8102:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 8103:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 8104:                             push(@okspares,$server);
 8105:                         }
 8106:                     }
 8107:                 }
 8108:             }
 8109:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 8110:             my $newspare;
 8111:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 8112:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 8113:                     $newspare = $new;
 8114:                 }
 8115:             }
 8116:             my @spares;
 8117:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 8118:                 @spares = sort(@okspares,$newspare);
 8119:             } else {
 8120:                 @spares = sort(@okspares);
 8121:             }
 8122:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 8123:             if (ref($spareid{$lonhost}) eq 'HASH') {
 8124:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 8125:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 8126:                     if (@diffs > 0) {
 8127:                         $spareschg{$type} = 1;
 8128:                     }
 8129:                 }
 8130:             }
 8131:         }
 8132:         if (keys(%spareschg) > 0) {
 8133:             $changes{'spares'}{$lonhost} = \%spareschg;
 8134:         }
 8135:     }
 8136: 
 8137:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8138:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 8139:             if (ref($changes{'spares'}) eq 'HASH') {
 8140:                 if (keys(%{$changes{'spares'}}) > 0) {
 8141:                     $savespares = 1;
 8142:                 }
 8143:             }
 8144:         } else {
 8145:             $savespares = 1;
 8146:         }
 8147:     }
 8148: 
 8149:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 8150:     if ((keys(%changes) > 0) || ($savespares)) {
 8151:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 8152:                                                  $dom);
 8153:         if ($putresult eq 'ok') {
 8154:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8155:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 8156:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 8157:                 }
 8158:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 8159:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 8160:                 }
 8161:             }
 8162:             my $cachetime = 24*60*60;
 8163:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8164:             if (keys(%changes) > 0) {
 8165:                 my %lt = &usersession_titles();
 8166:                 $resulttext = &mt('Changes made:').'<ul>';
 8167:                 foreach my $prefix (@prefixes) {
 8168:                     if (ref($changes{$prefix}) eq 'HASH') {
 8169:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 8170:                         if ($prefix eq 'spares') {
 8171:                             if (ref($changes{$prefix}) eq 'HASH') {
 8172:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 8173:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 8174:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 8175:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,'spares',$lonhostdom);
 8176:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 8177:                                         foreach my $type (@{$types{$prefix}}) {
 8178:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 8179:                                                 my $offloadto = &mt('None');
 8180:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 8181:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 8182:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 8183:                                                     }
 8184:                                                 }
 8185:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 8186:                                             }
 8187:                                         }
 8188:                                     }
 8189:                                     $resulttext .= '</li>';
 8190:                                 }
 8191:                             }
 8192:                         } else {
 8193:                             foreach my $type (@{$types{$prefix}}) {
 8194:                                 if (defined($changes{$prefix}{$type})) {
 8195:                                     my $newvalue;
 8196:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8197:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 8198:                                             if ($type eq 'version') {
 8199:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 8200:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 8201:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 8202:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 8203:                                                 }
 8204:                                             }
 8205:                                         }
 8206:                                     }
 8207:                                     if ($newvalue eq '') {
 8208:                                         if ($type eq 'version') {
 8209:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 8210:                                         } else {
 8211:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 8212:                                         }
 8213:                                     } else {
 8214:                                         if ($type eq 'version') {
 8215:                                             $newvalue .= ' '.&mt('(or later)'); 
 8216:                                         }
 8217:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 8218:                                     }
 8219:                                 }
 8220:                             }
 8221:                         }
 8222:                         $resulttext .= '</ul>';
 8223:                     }
 8224:                 }
 8225:                 $resulttext .= '</ul>';
 8226:             } else {
 8227:                 $resulttext = $nochgmsg;
 8228:             }
 8229:         } else {
 8230:             $resulttext = '<span class="LC_error">'.
 8231:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8232:         }
 8233:     } else {
 8234:         $resulttext = $nochgmsg;
 8235:     }
 8236:     return $resulttext;
 8237: }
 8238: 
 8239: sub modify_loadbalancing {
 8240:     my ($dom,%domconfig) = @_;
 8241:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8242:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8243:     my ($othertitle,$usertypes,$types) =
 8244:         &Apache::loncommon::sorted_inst_types($dom);
 8245:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8246:     my @sparestypes = ('primary','default');
 8247:     my %typetitles = &sparestype_titles();
 8248:     my $resulttext;
 8249:     my (%currbalancer,%currtargets,%currrules,%existing);
 8250:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8251:         %existing = %{$domconfig{'loadbalancing'}};
 8252:     }
 8253:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 8254:                               \%currtargets,\%currrules);
 8255:     my ($saveloadbalancing,%defaultshash,%changes);
 8256:     my ($alltypes,$othertypes,$titles) =
 8257:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 8258:     my %ruletitles = &offloadtype_text();
 8259:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
 8260:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
 8261:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
 8262:         if ($balancer eq '') {
 8263:             next;
 8264:         }
 8265:         if (!exists($servers{$balancer})) { 
 8266:             if (exists($currbalancer{$balancer})) {
 8267:                 push(@{$changes{'delete'}},$balancer);
 8268:             }
 8269:             next;
 8270:         }
 8271:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
 8272:             push(@{$changes{'delete'}},$balancer);
 8273:             next;
 8274:         }
 8275:         if (!exists($currbalancer{$balancer})) {
 8276:             push(@{$changes{'add'}},$balancer);
 8277:         }
 8278:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
 8279:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
 8280:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
 8281:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8282:             $saveloadbalancing = 1;
 8283:         }
 8284:         foreach my $sparetype (@sparestypes) {
 8285:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
 8286:             my @offloadto;
 8287:             foreach my $target (@targets) {
 8288:                 if (($servers{$target}) && ($target ne $balancer)) {
 8289:                     if ($sparetype eq 'default') {
 8290:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
 8291:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
 8292:                         }
 8293:                     }
 8294:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
 8295:                         push(@offloadto,$target);
 8296:                     }
 8297:                 }
 8298:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
 8299:             }
 8300:         }
 8301:         if (ref($currtargets{$balancer}) eq 'HASH') {
 8302:             foreach my $sparetype (@sparestypes) {
 8303:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
 8304:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
 8305:                     if (@targetdiffs > 0) {
 8306:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8307:                     }
 8308:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8309:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8310:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8311:                     }
 8312:                 }
 8313:             }
 8314:         } else {
 8315:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
 8316:                 foreach my $sparetype (@sparestypes) { 
 8317:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8318:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8319:                             $changes{'curr'}{$balancer}{'targets'} = 1;
 8320:                         }
 8321:                     }
 8322:                 }
 8323:             }  
 8324:         }
 8325:         my $ishomedom;
 8326:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 8327:             $ishomedom = 1;
 8328:         }
 8329:         if (ref($alltypes) eq 'ARRAY') {
 8330:             foreach my $type (@{$alltypes}) {
 8331:                 my $rule;
 8332:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) && 
 8333:                          (!$ishomedom)) {
 8334:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
 8335:                 }
 8336:                 if ($rule eq 'specific') {
 8337:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
 8338:                 }
 8339:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
 8340:                 if (ref($currrules{$balancer}) eq 'HASH') {
 8341:                     if ($rule ne $currrules{$balancer}{$type}) {
 8342:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8343:                     }
 8344:                 } elsif ($rule ne '') {
 8345:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8346:                 }
 8347:             }
 8348:         }
 8349:     }
 8350:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 8351:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 8352:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
 8353:             $defaultshash{'loadbalancing'} = {};
 8354:         }
 8355:         my $putresult = &Apache::lonnet::put_dom('configuration',
 8356:                                                  \%defaultshash,$dom);
 8357:  
 8358:         if ($putresult eq 'ok') {
 8359:             if (keys(%changes) > 0) {
 8360:                 if (ref($changes{'delete'}) eq 'ARRAY') {
 8361:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
 8362:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
 8363:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8364:                     }
 8365:                 }
 8366:                 if (ref($changes{'add'}) eq 'ARRAY') {
 8367:                     foreach my $balancer (sort(@{$changes{'add'}})) {   
 8368:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
 8369:                     }
 8370:                 }
 8371:                 if (ref($changes{'curr'}) eq 'HASH') {
 8372:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
 8373:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
 8374:                             if ($changes{'curr'}{$balancer}{'targets'}) {
 8375:                                 my %offloadstr;
 8376:                                 foreach my $sparetype (@sparestypes) {
 8377:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8378:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8379:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
 8380:                                         }
 8381:                                     }
 8382:                                 }
 8383:                                 if (keys(%offloadstr) == 0) {
 8384:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 8385:                                 } else {
 8386:                                     my $showoffload;
 8387:                                     foreach my $sparetype (@sparestypes) {
 8388:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
 8389:                                         if (defined($offloadstr{$sparetype})) {
 8390:                                             $showoffload .= $offloadstr{$sparetype};
 8391:                                         } else {
 8392:                                             $showoffload .= &mt('None');
 8393:                                         }
 8394:                                         $showoffload .= ('&nbsp;'x3);
 8395:                                     }
 8396:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
 8397:                                 }
 8398:                             }
 8399:                         }
 8400:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
 8401:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
 8402:                                 foreach my $type (@{$alltypes}) {
 8403:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
 8404:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
 8405:                                         my $balancetext;
 8406:                                         if ($rule eq '') {
 8407:                                             $balancetext =  $ruletitles{'default'};
 8408:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer')) {
 8409:                                             $balancetext =  $ruletitles{$rule};
 8410:                                         } else {
 8411:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
 8412:                                         }
 8413:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';     
 8414:                                     }
 8415:                                 }
 8416:                             }
 8417:                         }
 8418:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8419:                     }
 8420:                 }
 8421:                 if ($resulttext ne '') {
 8422:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
 8423:                 } else {
 8424:                     $resulttext = $nochgmsg;
 8425:                 }
 8426:             } else {
 8427:                 $resulttext = $nochgmsg;
 8428:             }
 8429:         } else {
 8430:             $resulttext = '<span class="LC_error">'.
 8431:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8432:         }
 8433:     } else {
 8434:         $resulttext = $nochgmsg;
 8435:     }
 8436:     return $resulttext;
 8437: }
 8438: 
 8439: sub recurse_check {
 8440:     my ($chkcats,$categories,$depth,$name) = @_;
 8441:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 8442:         my $chg = 0;
 8443:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 8444:             my $category = $chkcats->[$depth]{$name}[$j];
 8445:             my $item;
 8446:             if ($category eq '') {
 8447:                 $chg ++;
 8448:             } else {
 8449:                 my $deeper = $depth + 1;
 8450:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 8451:                 if ($chg) {
 8452:                     $categories->{$item} -= $chg;
 8453:                 }
 8454:                 &recurse_check($chkcats,$categories,$deeper,$category);
 8455:                 $deeper --;
 8456:             }
 8457:         }
 8458:     }
 8459:     return;
 8460: }
 8461: 
 8462: sub recurse_cat_deletes {
 8463:     my ($item,$coursecategories,$deletions) = @_;
 8464:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8465:     my $subdepth = $depth + 1;
 8466:     if (ref($coursecategories) eq 'HASH') {
 8467:         foreach my $subitem (keys(%{$coursecategories})) {
 8468:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 8469:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 8470:                 delete($coursecategories->{$subitem});
 8471:                 $deletions->{$subitem} = 1;
 8472:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 8473:             }
 8474:         }
 8475:     }
 8476:     return;
 8477: }
 8478: 
 8479: sub get_active_dcs {
 8480:     my ($dom) = @_;
 8481:     my $now = time;
 8482:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
 8483:     my %domcoords;
 8484:     my $numdcs = 0;
 8485:     my $now = time;
 8486:     foreach my $server (keys(%dompersonnel)) {
 8487:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 8488:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 8489:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 8490:         }
 8491:     }
 8492:     return %domcoords;
 8493: }
 8494: 
 8495: sub active_dc_picker {
 8496:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
 8497:     my %domcoords = &get_active_dcs($dom); 
 8498:     my @domcoord = keys(%domcoords);
 8499:     if (keys(%currhash)) {
 8500:         foreach my $dc (keys(%currhash)) {
 8501:             unless (exists($domcoords{$dc})) {
 8502:                 push(@domcoord,$dc);
 8503:             }
 8504:         }
 8505:     }
 8506:     @domcoord = sort(@domcoord);
 8507:     my $numdcs = scalar(@domcoord); 
 8508:     my $rows = 0;
 8509:     my $table;
 8510:     if ($numdcs > 1) {
 8511:         $table = '<table>';
 8512:         for (my $i=0; $i<@domcoord; $i++) {
 8513:             my $rem = $i%($numinrow);
 8514:             if ($rem == 0) {
 8515:                 if ($i > 0) {
 8516:                     $table .= '</tr>';
 8517:                 }
 8518:                 $table .= '<tr>';
 8519:                 $rows ++;
 8520:             }
 8521:             my $check = '';
 8522:             if ($inputtype eq 'radio') {
 8523:                 if (keys(%currhash) == 0) {
 8524:                     if (!$i) {
 8525:                         $check = ' checked="checked"';
 8526:                     }
 8527:                 } elsif (exists($currhash{$domcoord[$i]})) {
 8528:                     $check = ' checked="checked"';
 8529:                 }
 8530:             } else {
 8531:                 if (exists($currhash{$domcoord[$i]})) {
 8532:                     $check = ' checked="checked"';
 8533:                 }
 8534:             }
 8535:             if ($i == @domcoord - 1) {
 8536:                 my $colsleft = $numinrow - $rem;
 8537:                 if ($colsleft > 1) {
 8538:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
 8539:                 } else {
 8540:                     $table .= '<td class="LC_left_item">';
 8541:                 }
 8542:             } else {
 8543:                 $table .= '<td class="LC_left_item">';
 8544:             }
 8545:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
 8546:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
 8547:             $table .= '<span class="LC_nobreak"><label>'.
 8548:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
 8549:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
 8550:             if ($user ne $dcname.':'.$dcdom) {
 8551:                 $table .=  ' ('.$dcname.':'.$dcdom.')'.
 8552:                            '</label></span></td>';
 8553:             }
 8554:         }
 8555:         $table .= '</tr></table>';
 8556:     } elsif ($numdcs == 1) {
 8557:         if ($inputtype eq 'radio') {
 8558:             $table .= '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />';
 8559:         } else {
 8560:             my $check;
 8561:             if (exists($currhash{$domcoord[0]})) {
 8562:                 $check = ' checked="checked"';
 8563:             }
 8564:             $table .= '<input type="checkbox" name="'.$name.'" '.
 8565:                       'value="'.$domcoord[0].'"'.$check.' />';
 8566:             $rows ++;
 8567:         }
 8568:     }
 8569:     return ($numdcs,$table,$rows);
 8570: }
 8571: 
 8572: sub usersession_titles {
 8573:     return &Apache::lonlocal::texthash(
 8574:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
 8575:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
 8576:                spares => 'Servers offloaded to, when busy',
 8577:                version => 'LON-CAPA version requirement',
 8578:                excludedomain => 'Allow all, but exclude specific domains',
 8579:                includedomain => 'Deny all, but include specific domains',
 8580:                primary => 'Primary (checked first)',
 8581:                default => 'Default',
 8582:            );
 8583: }
 8584: 
 8585: sub id_for_thisdom {
 8586:     my (%servers) = @_;
 8587:     my %altids;
 8588:     foreach my $server (keys(%servers)) {
 8589:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 8590:         if ($serverhome ne $server) {
 8591:             $altids{$serverhome} = $server;
 8592:         }
 8593:     }
 8594:     return %altids;
 8595: }
 8596: 
 8597: sub count_servers {
 8598:     my ($currbalancer,%servers) = @_;
 8599:     my (@spares,$numspares);
 8600:     foreach my $lonhost (sort(keys(%servers))) {
 8601:         next if ($currbalancer eq $lonhost);
 8602:         push(@spares,$lonhost);
 8603:     }
 8604:     if ($currbalancer) {
 8605:         $numspares = scalar(@spares);
 8606:     } else {
 8607:         $numspares = scalar(@spares) - 1;
 8608:     }
 8609:     return ($numspares,@spares);
 8610: }
 8611: 
 8612: sub lonbalance_targets_js {
 8613:     my ($dom,$types,$servers,$settings) = @_;
 8614:     my $select = &mt('Select');
 8615:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
 8616:     if (ref($servers) eq 'HASH') {
 8617:         $alltargets = join("','",sort(keys(%{$servers})));
 8618:         my @homedoms;
 8619:         foreach my $server (sort(keys(%{$servers}))) {
 8620:             if (&Apache::lonnet::host_domain($server) eq $dom) {
 8621:                 push(@homedoms,'1');
 8622:             } else {
 8623:                 push(@homedoms,'0');
 8624:             }
 8625:         }
 8626:         $allishome = join("','",@homedoms);
 8627:     }
 8628:     if (ref($types) eq 'ARRAY') {
 8629:         if (@{$types} > 0) {
 8630:             @alltypes = @{$types};
 8631:         }
 8632:     }
 8633:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 8634:     $allinsttypes = join("','",@alltypes);
 8635:     my (%currbalancer,%currtargets,%currrules,%existing);
 8636:     if (ref($settings) eq 'HASH') {
 8637:         %existing = %{$settings};
 8638:     }
 8639:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
 8640:                               \%currtargets,\%currrules);
 8641:     my $balancers = join("','",sort(keys(%currbalancer))); 
 8642:     return <<"END";
 8643: 
 8644: <script type="text/javascript">
 8645: // <![CDATA[
 8646: 
 8647: currBalancers = new Array('$balancers');
 8648: 
 8649: function toggleTargets(balnum) {
 8650:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 8651:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
 8652:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
 8653:     var prevbalancer = prevhostitem.value;
 8654:     var baltotal = document.getElementById('loadbalancing_total').value;
 8655:     prevhostitem.value = balancer;
 8656:     if (prevbalancer != '') {
 8657:         var prevIdx = currBalancers.indexOf(prevbalancer);
 8658:         if (prevIdx != -1) {
 8659:             currBalancers.splice(prevIdx,1);
 8660:         }
 8661:     }
 8662:     if (balancer == '') {
 8663:         hideSpares(balnum);
 8664:     } else {
 8665:         var currIdx = currBalancers.indexOf(balancer);
 8666:         if (currIdx == -1) {
 8667:             currBalancers.push(balancer);
 8668:         }
 8669:         var homedoms = new Array('$allishome');
 8670:         var ishomedom = homedoms[lonhostitem.selectedIndex];
 8671:         showSpares(balancer,ishomedom,balnum);
 8672:     }
 8673:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
 8674:     return;
 8675: }
 8676: 
 8677: function showSpares(balancer,ishomedom,balnum) {
 8678:     var alltargets = new Array('$alltargets');
 8679:     var insttypes = new Array('$allinsttypes');
 8680:     var offloadtypes = new Array('primary','default');
 8681: 
 8682:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
 8683:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
 8684:  
 8685:     for (var i=0; i<offloadtypes.length; i++) {
 8686:         var count = 0;
 8687:         for (var j=0; j<alltargets.length; j++) {
 8688:             if (alltargets[j] != balancer) {
 8689:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
 8690:                 item.value = alltargets[j];
 8691:                 item.style.textAlign='left';
 8692:                 item.style.textFace='normal';
 8693:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
 8694:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
 8695:                     item.disabled = '';
 8696:                 } else {
 8697:                     item.disabled = 'disabled';
 8698:                     item.checked = false;
 8699:                 }
 8700:                 count ++;
 8701:             }
 8702:         }
 8703:     }
 8704:     for (var k=0; k<insttypes.length; k++) {
 8705:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
 8706:             if (ishomedom == 1) {
 8707:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8708:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8709:             } else {
 8710:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 8711:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 8712: 
 8713:             }
 8714:         } else {
 8715:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8716:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8717:         }
 8718:         if ((insttypes[k] != '_LC_external') && 
 8719:             ((insttypes[k] != '_LC_internetdom') ||
 8720:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
 8721:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
 8722:             item.options.length = 0;
 8723:             item.options[0] = new Option("","",true,true);
 8724:             var idx = 0;  
 8725:             for (var m=0; m<alltargets.length; m++) {
 8726:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
 8727:                     idx ++;
 8728:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8729:                     
 8730:                 }
 8731:             }
 8732:         }
 8733:     }
 8734:     return;
 8735: }
 8736: 
 8737: function hideSpares(balnum) {
 8738:     var alltargets = new Array('$alltargets');
 8739:     var insttypes = new Array('$allinsttypes');
 8740:     var offloadtypes = new Array('primary','default');
 8741: 
 8742:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
 8743:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
 8744: 
 8745:     var total = alltargets.length - 1;
 8746:     for (var i=0; i<offloadtypes; i++) {
 8747:         for (var j=0; j<total; j++) {
 8748:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
 8749:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
 8750:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
 8751:         }
 8752:     }
 8753:     for (var k=0; k<insttypes.length; k++) {
 8754:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 8755:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 8756:         if (insttypes[k] != '_LC_external') {
 8757:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
 8758:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
 8759:         }
 8760:     }
 8761:     return;
 8762: }
 8763: 
 8764: function checkOffloads(item,balnum,type) {
 8765:     var alltargets = new Array('$alltargets');
 8766:     var offloadtypes = new Array('primary','default');
 8767:     if (item.checked) {
 8768:         var total = alltargets.length - 1;
 8769:         var other;
 8770:         if (type == offloadtypes[0]) {
 8771:             other = offloadtypes[1];
 8772:         } else {
 8773:             other = offloadtypes[0];
 8774:         }
 8775:         for (var i=0; i<total; i++) {
 8776:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
 8777:             if (server == item.value) {
 8778:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
 8779:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
 8780:                 }
 8781:             }
 8782:         }
 8783:     }
 8784:     return;
 8785: }
 8786: 
 8787: function singleServerToggle(balnum,type) {
 8788:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
 8789:     if (offloadtoSelIdx == 0) {
 8790:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
 8791:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 8792: 
 8793:     } else {
 8794:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
 8795:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 8796:     }
 8797:     return;
 8798: }
 8799: 
 8800: function balanceruleChange(formname,balnum,type) {
 8801:     if (type == '_LC_external') {
 8802:         return;
 8803:     }
 8804:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
 8805:     for (var i=0; i<typesRules.length; i++) {
 8806:         if (formname.elements[typesRules[i]].checked) {
 8807:             if (formname.elements[typesRules[i]].value != 'specific') {
 8808:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
 8809:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 8810:             } else {
 8811:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 8812:             }
 8813:         }
 8814:     }
 8815:     return;
 8816: }
 8817: 
 8818: function balancerDeleteChange(balnum) {
 8819:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 8820:     var baltotal = document.getElementById('loadbalancing_total').value;
 8821:     var addtarget;
 8822:     var removetarget;
 8823:     var action = 'delete';
 8824:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
 8825:         var lonhost = hostitem.value;
 8826:         var currIdx = currBalancers.indexOf(lonhost);
 8827:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
 8828:             if (currIdx != -1) {
 8829:                 currBalancers.splice(currIdx,1);
 8830:             }
 8831:             addtarget = lonhost;
 8832:         } else {
 8833:             if (currIdx == -1) {
 8834:                 currBalancers.push(lonhost);
 8835:             }
 8836:             removetarget = lonhost;
 8837:             action = 'undelete';
 8838:         }
 8839:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
 8840:     }
 8841:     return;
 8842: }
 8843: 
 8844: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
 8845:     if (baltotal > 1) {
 8846:         var offloadtypes = new Array('primary','default');
 8847:         var alltargets = new Array('$alltargets');
 8848:         var insttypes = new Array('$allinsttypes');
 8849:         for (var i=0; i<baltotal; i++) {
 8850:             if (i != balnum) {
 8851:                 for (var j=0; j<offloadtypes.length; j++) {
 8852:                     var total = alltargets.length - 1;
 8853:                     for (var k=0; k<total; k++) {
 8854:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
 8855:                         var server = serveritem.value;
 8856:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
 8857:                             if (server == addtarget) {
 8858:                                 serveritem.disabled = '';
 8859:                             }
 8860:                         }
 8861:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 8862:                             if (server == removetarget) {
 8863:                                 serveritem.disabled = 'disabled';
 8864:                                 serveritem.checked = false;
 8865:                             }
 8866:                         }
 8867:                     }
 8868:                 }
 8869:                 for (var j=0; j<insttypes.length; j++) {
 8870:                     if (insttypes[j] != '_LC_external') {
 8871:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
 8872:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
 8873:                             var currSel = singleserver.selectedIndex;
 8874:                             var currVal = singleserver.options[currSel].value;
 8875:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
 8876:                                 var numoptions = singleserver.options.length;
 8877:                                 var needsnew = 1;
 8878:                                 for (var k=0; k<numoptions; k++) {
 8879:                                     if (singleserver.options[k] == addtarget) {
 8880:                                         needsnew = 0;
 8881:                                         break;
 8882:                                     }
 8883:                                 }
 8884:                                 if (needsnew == 1) {
 8885:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
 8886:                                 }
 8887:                             }
 8888:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 8889:                                 singleserver.options.length = 0;
 8890:                                 if ((currVal) && (currVal != removetarget)) {
 8891:                                     singleserver.options[0] = new Option("","",false,false);
 8892:                                 } else {
 8893:                                     singleserver.options[0] = new Option("","",true,true);
 8894:                                 }
 8895:                                 var idx = 0;
 8896:                                 for (var m=0; m<alltargets.length; m++) {
 8897:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
 8898:                                         idx ++;
 8899:                                         if (currVal == alltargets[m]) {
 8900:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
 8901:                                         } else {
 8902:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8903:                                         }
 8904:                                     }
 8905:                                 }
 8906:                             }
 8907:                         }
 8908:                     }
 8909:                 }
 8910:             }
 8911:         }
 8912:     }
 8913:     return;
 8914: }
 8915: 
 8916: // ]]>
 8917: </script>
 8918: 
 8919: END
 8920: }
 8921: 
 8922: sub new_spares_js {
 8923:     my @sparestypes = ('primary','default');
 8924:     my $types = join("','",@sparestypes);
 8925:     my $select = &mt('Select');
 8926:     return <<"END";
 8927: 
 8928: <script type="text/javascript">
 8929: // <![CDATA[
 8930: 
 8931: function updateNewSpares(formname,lonhost) {
 8932:     var types = new Array('$types');
 8933:     var include = new Array();
 8934:     var exclude = new Array();
 8935:     for (var i=0; i<types.length; i++) {
 8936:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
 8937:         for (var j=0; j<spareboxes.length; j++) {
 8938:             if (formname.elements[spareboxes[j]].checked) {
 8939:                 exclude.push(formname.elements[spareboxes[j]].value);
 8940:             } else {
 8941:                 include.push(formname.elements[spareboxes[j]].value);
 8942:             }
 8943:         }
 8944:     }
 8945:     for (var i=0; i<types.length; i++) {
 8946:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
 8947:         var selIdx = newSpare.selectedIndex;
 8948:         var currnew = newSpare.options[selIdx].value;
 8949:         var okSpares = new Array();
 8950:         for (var j=0; j<newSpare.options.length; j++) {
 8951:             var possible = newSpare.options[j].value;
 8952:             if (possible != '') {
 8953:                 if (exclude.indexOf(possible) == -1) {
 8954:                     okSpares.push(possible);
 8955:                 } else {
 8956:                     if (currnew == possible) {
 8957:                         selIdx = 0;
 8958:                     }
 8959:                 }
 8960:             }
 8961:         }
 8962:         for (var k=0; k<include.length; k++) {
 8963:             if (okSpares.indexOf(include[k]) == -1) {
 8964:                 okSpares.push(include[k]);
 8965:             }
 8966:         }
 8967:         okSpares.sort();
 8968:         newSpare.options.length = 0;
 8969:         if (selIdx == 0) {
 8970:             newSpare.options[0] = new Option("$select","",true,true);
 8971:         } else {
 8972:             newSpare.options[0] = new Option("$select","",false,false);
 8973:         }
 8974:         for (var m=0; m<okSpares.length; m++) {
 8975:             var idx = m+1;
 8976:             var selThis = 0;
 8977:             if (selIdx != 0) {
 8978:                 if (okSpares[m] == currnew) {
 8979:                     selThis = 1;
 8980:                 }
 8981:             }
 8982:             if (selThis == 1) {
 8983:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
 8984:             } else {
 8985:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
 8986:             }
 8987:         }
 8988:     }
 8989:     return;
 8990: }
 8991: 
 8992: function checkNewSpares(lonhost,type) {
 8993:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
 8994:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
 8995:     if (chosen != '') { 
 8996:         var othertype;
 8997:         var othernewSpare;
 8998:         if (type == 'primary') {
 8999:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
 9000:         }
 9001:         if (type == 'default') {
 9002:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
 9003:         }
 9004:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
 9005:             othernewSpare.selectedIndex = 0;
 9006:         }
 9007:     }
 9008:     return;
 9009: }
 9010: 
 9011: // ]]>
 9012: </script>
 9013: 
 9014: END
 9015: 
 9016: }
 9017: 
 9018: sub common_domprefs_js {
 9019:     return <<"END";
 9020: 
 9021: <script type="text/javascript">
 9022: // <![CDATA[
 9023: 
 9024: function getIndicesByName(formname,item) {
 9025:     var group = new Array();
 9026:     for (var i=0;i<formname.elements.length;i++) {
 9027:         if (formname.elements[i].name == item) {
 9028:             group.push(formname.elements[i].id);
 9029:         }
 9030:     }
 9031:     return group;
 9032: }
 9033: 
 9034: // ]]>
 9035: </script>
 9036: 
 9037: END
 9038: 
 9039: }
 9040: 
 9041: sub recaptcha_js {
 9042:     my %lt = &captcha_phrases();
 9043:     return <<"END";
 9044: 
 9045: <script type="text/javascript">
 9046: // <![CDATA[
 9047: 
 9048: function updateCaptcha(caller,context) {
 9049:     var privitem;
 9050:     var pubitem;
 9051:     var privtext;
 9052:     var pubtext;
 9053:     if (document.getElementById(context+'_recaptchapub')) {
 9054:         pubitem = document.getElementById(context+'_recaptchapub');
 9055:     } else {
 9056:         return;
 9057:     }
 9058:     if (document.getElementById(context+'_recaptchapriv')) {
 9059:         privitem = document.getElementById(context+'_recaptchapriv');
 9060:     } else {
 9061:         return;
 9062:     }
 9063:     if (document.getElementById(context+'_recaptchapubtxt')) {
 9064:         pubtext = document.getElementById(context+'_recaptchapubtxt');
 9065:     } else {
 9066:         return;
 9067:     }
 9068:     if (document.getElementById(context+'_recaptchaprivtxt')) {
 9069:         privtext = document.getElementById(context+'_recaptchaprivtxt');
 9070:     } else {
 9071:         return;
 9072:     }
 9073:     if (caller.checked) {
 9074:         if (caller.value == 'recaptcha') {
 9075:             pubitem.type = 'text';
 9076:             privitem.type = 'text';
 9077:             pubitem.size = '40';
 9078:             privitem.size = '40';
 9079:             pubtext.innerHTML = "$lt{'pub'}";
 9080:             privtext.innerHTML = "$lt{'priv'}";
 9081:         } else {
 9082:             pubitem.type = 'hidden';
 9083:             privitem.type = 'hidden';
 9084:             pubtext.innerHTML = '';
 9085:             privtext.innerHTML = '';
 9086:         }
 9087:     }
 9088:     return;
 9089: }
 9090: 
 9091: // ]]>
 9092: </script>
 9093: 
 9094: END
 9095: 
 9096: }
 9097: 
 9098: sub credits_js {
 9099:     return <<"END";
 9100: 
 9101: <script type="text/javascript">
 9102: // <![CDATA[
 9103: 
 9104: function toggleCredits(domForm) {
 9105:     if (document.getElementById('credits')) {
 9106:         creditsitem = document.getElementById('credits');
 9107:         var creditsLength = domForm.coursecredits.length;
 9108:         if (creditsLength) {
 9109:             var currval;
 9110:             for (var i=0; i<creditsLength; i++) {
 9111:                 if (domForm.coursecredits[i].checked) {
 9112:                    currval = domForm.coursecredits[i].value;
 9113:                 }
 9114:             }
 9115:             if (currval == 1) {
 9116:                 creditsitem.style.display = 'block';
 9117:             } else {
 9118:                 creditsitem.style.display = 'none';
 9119:             }
 9120:         }
 9121:     }
 9122:     return;
 9123: }
 9124: 
 9125: // ]]>
 9126: </script>
 9127: 
 9128: END
 9129: 
 9130: }
 9131: 
 9132: sub captcha_phrases {
 9133:     return &Apache::lonlocal::texthash (
 9134:                  priv => 'Private key',
 9135:                  pub  => 'Public key',
 9136:                  original  => 'original (CAPTCHA)',
 9137:                  recaptcha => 'successor (ReCAPTCHA)',
 9138:                  notused   => 'unused',
 9139:     );
 9140: }
 9141: 
 9142: 1;

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