File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.209: download - view: text, annotated - select for diffs
Sun Sep 29 00:49:28 2013 UTC (10 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6675
- Case where $env{'REMOTE_ADDR'} as reported to server selected to host
  user session, is different from $env{'REMOTE_ADDR'} as reported to
  server which handled original authentication request.
  - Domain configuration for a load balancing server can be set to one of
    the following, if an IP mismatch is detected by /adm/migrateuser
    during credentials checking after redirect via /adm/switchserver
    (i) Session will be hosted on Load Balancer
    (ii) Session will be hosted on offload server
    for each of (a) SSO users from load balancer's domain, (b) non-SSO users
  - Setting to host on load balancer will be ignored if switch server was called
    by an author or co-author switching to server housing the authoring space.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.209 2013/09/29 00:49:28 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: #
   28: ###############################################################
   29: ##############################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::domainprefs.pm
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Handles configuration of a LON-CAPA domain.  
   40: 
   41: This is part of the LearningOnline Network with CAPA project
   42: described at http://www.lon-capa.org.
   43: 
   44: 
   45: =head1 OVERVIEW
   46: 
   47: Each institution using LON-CAPA will typically have a single domain designated 
   48: for use by individuals affiliated with the institution.  Accordingly, each domain
   49: may define a default set of logos and a color scheme which can be used to "brand"
   50: the LON-CAPA instance. In addition, an institution will typically have a language
   51: and timezone which are used for the majority of courses.
   52: 
   53: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   54: host of other domain-wide settings which determine the types of functionality
   55: available to users and courses in the domain.
   56: 
   57: There is also a mechanism to configure cataloging of courses in the domain, and
   58: controls on the operation of automated processes which govern such things as
   59: roster updates, user directory updates and processing of course requests.
   60: 
   61: The domain coordination manual which is built dynamically on install/update of 
   62: LON-CAPA from the relevant help items provides more information about domain 
   63: configuration.
   64: 
   65: Most of the domain settings are stored in the configuration.db GDBM file which is
   66: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   67: where $dom is the domain.  The configuration.db stores settings in a number of 
   68: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   69: the domain as files (e.g., image files for logos etc., or plain text files for
   70: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   71: session hosted on the primary library server in the domain, as these files are 
   72: stored in author space belonging to a special $dom-domainconfig user.   
   73: 
   74: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   75: the current settings, and provides an interface to make modifications.
   76: 
   77: =head1 SUBROUTINES
   78: 
   79: =over
   80: 
   81: =item print_quotas()
   82: 
   83: Inputs: 4 
   84: 
   85: $dom,$settings,$rowtotal,$action.
   86: 
   87: $dom is the domain, $settings is a reference to a hash of current settings for
   88: the current context, $rowtotal is a reference to the scalar used to record the 
   89: number of rows displayed on the page, and $action is the context (quotas,  
   90: requestcourses or requestauthor).
   91: 
   92: The print_quotas routine was orginally created to display/store information
   93: about default quota sizes for portfolio spaces for the different types of 
   94: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   95: but is now also used to manage availability of user tools: 
   96: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   97: used by course owners to request creation of a course, and to display/store
   98: default quota sizes for authoring spaces.
   99: 
  100: Outputs: 1
  101: 
  102: $datatable  - HTML containing form elements which allow settings to be changed. 
  103: 
  104: In the case of course requests, radio buttons are displayed for each institutional
  105: affiliate type (and also default, and _LC_adv) for each of the course types 
  106: (official, unofficial and community).  In each case the radio buttons allow the 
  107: selection of one of four values:
  108: 
  109: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  110: which have the following effects:
  111: 
  112: 0
  113: 
  114: =over
  115: 
  116: - course requests are not allowed for this course types/affiliation
  117: 
  118: =back
  119: 
  120: approval 
  121: 
  122: =over 
  123: 
  124: - course requests must be approved by a Doman Coordinator in the 
  125: course's domain
  126: 
  127: =back
  128: 
  129: validate 
  130: 
  131: =over
  132: 
  133: - an institutional validation (e.g., check requestor is instructor
  134: of record) needs to be passed before the course will be created.  The required
  135: validation is in localenroll.pm on the primary library server for the course 
  136: domain.
  137: 
  138: =back
  139: 
  140: autolimit 
  141: 
  142: =over
  143:  
  144: - course requests will be processed automatically up to a limit of
  145: N requests for the course type for the particular requestor.
  146: If N is undefined, there is no limit to the number of course requests
  147: which a course owner may submit and have processed automatically. 
  148: 
  149: =back
  150: 
  151: =item modify_quotas() 
  152: 
  153: =back
  154: 
  155: =cut
  156: 
  157: package Apache::domainprefs;
  158: 
  159: use strict;
  160: use Apache::Constants qw(:common :http);
  161: use Apache::lonnet;
  162: use Apache::loncommon();
  163: use Apache::lonhtmlcommon();
  164: use Apache::lonlocal;
  165: use Apache::lonmsg();
  166: use Apache::lonconfigsettings;
  167: use LONCAPA qw(:DEFAULT :match);
  168: use LONCAPA::Enrollment;
  169: use LONCAPA::lonauthcgi();
  170: use File::Copy;
  171: use Locale::Language;
  172: use DateTime::TimeZone;
  173: use DateTime::Locale;
  174: 
  175: my $registered_cleanup;
  176: my $modified_urls;
  177: 
  178: sub handler {
  179:     my $r=shift;
  180:     if ($r->header_only) {
  181:         &Apache::loncommon::content_type($r,'text/html');
  182:         $r->send_http_header;
  183:         return OK;
  184:     }
  185: 
  186:     my $context = 'domain';
  187:     my $dom = $env{'request.role.domain'};
  188:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  189:     if (&Apache::lonnet::allowed('mau',$dom)) {
  190:         &Apache::loncommon::content_type($r,'text/html');
  191:         $r->send_http_header;
  192:     } else {
  193:         $env{'user.error.msg'}=
  194:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  195:         return HTTP_NOT_ACCEPTABLE;
  196:     }
  197: 
  198:     $registered_cleanup=0;
  199:     @{$modified_urls}=();
  200: 
  201:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  202:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  203:                                             ['phase','actions']);
  204:     my $phase = 'pickactions';
  205:     if ( exists($env{'form.phase'}) ) {
  206:         $phase = $env{'form.phase'};
  207:     }
  208:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  209:     my %domconfig =
  210:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  211:                 'quotas','autoenroll','autoupdate','autocreate',
  212:                 'directorysrch','usercreation','usermodification',
  213:                 'contacts','defaults','scantron','coursecategories',
  214:                 'serverstatuses','requestcourses','helpsettings',
  215:                 'coursedefaults','usersessions','loadbalancing',
  216:                 'requestauthor'],$dom);
  217:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  218:                        'autoupdate','autocreate','directorysrch','contacts',
  219:                        'usercreation','usermodification','scantron',
  220:                        'requestcourses','requestauthor','coursecategories',
  221:                        'serverstatuses','helpsettings',
  222:                        'coursedefaults','usersessions');
  223:     my %existing;
  224:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  225:         %existing = %{$domconfig{'loadbalancing'}};
  226:     }
  227:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  228:         push(@prefs_order,'loadbalancing');
  229:     }
  230:     my %prefs = (
  231:         'rolecolors' =>
  232:                    { text => 'Default color schemes',
  233:                      help => 'Domain_Configuration_Color_Schemes',
  234:                      header => [{col1 => 'Student Settings',
  235:                                  col2 => '',},
  236:                                 {col1 => 'Coordinator Settings',
  237:                                  col2 => '',},
  238:                                 {col1 => 'Author Settings',
  239:                                  col2 => '',},
  240:                                 {col1 => 'Administrator Settings',
  241:                                  col2 => '',}],
  242:                     },
  243:         'login' =>
  244:                     { text => 'Log-in page options',
  245:                       help => 'Domain_Configuration_Login_Page',
  246:                       header => [{col1 => 'Log-in Page Items',
  247:                                   col2 => '',},
  248:                                  {col1 => 'Log-in Help',
  249:                                   col2 => 'Value'}],
  250:                     },
  251:         'defaults' => 
  252:                     { text => 'Default authentication/language/timezone/portal',
  253:                       help => 'Domain_Configuration_LangTZAuth',
  254:                       header => [{col1 => 'Setting',
  255:                                   col2 => 'Value'}],
  256:                     },
  257:         'quotas' => 
  258:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  259:                       help => 'Domain_Configuration_Quotas',
  260:                       header => [{col1 => 'User affiliation',
  261:                                   col2 => 'Available tools',
  262:                                   col3 => 'Quotas, Mb; (Authoring requires role)',}],
  263:                     },
  264:         'autoenroll' =>
  265:                    { text => 'Auto-enrollment settings',
  266:                      help => 'Domain_Configuration_Auto_Enrollment',
  267:                      header => [{col1 => 'Configuration setting',
  268:                                  col2 => 'Value(s)'}],
  269:                    },
  270:         'autoupdate' => 
  271:                    { text => 'Auto-update settings',
  272:                      help => 'Domain_Configuration_Auto_Updates',
  273:                      header => [{col1 => 'Setting',
  274:                                  col2 => 'Value',},
  275:                                 {col1 => 'Setting',
  276:                                  col2 => 'Affiliation'},
  277:                                 {col1 => 'User population',
  278:                                  col2 => 'Updateable user data'}],
  279:                   },
  280:         'autocreate' => 
  281:                   { text => 'Auto-course creation settings',
  282:                      help => 'Domain_Configuration_Auto_Creation',
  283:                      header => [{col1 => 'Configuration Setting',
  284:                                  col2 => 'Value',}],
  285:                   },
  286:         'directorysrch' => 
  287:                   { text => 'Institutional directory searches',
  288:                     help => 'Domain_Configuration_InstDirectory_Search',
  289:                     header => [{col1 => 'Setting',
  290:                                 col2 => 'Value',}],
  291:                   },
  292:         'contacts' =>
  293:                   { text => 'Contact Information',
  294:                     help => 'Domain_Configuration_Contact_Info',
  295:                     header => [{col1 => 'Setting',
  296:                                 col2 => 'Value',}],
  297:                   },
  298: 
  299:         'usercreation' => 
  300:                   { text => 'User creation',
  301:                     help => 'Domain_Configuration_User_Creation',
  302:                     header => [{col1 => 'Format rule type',
  303:                                 col2 => 'Format rules in force'},
  304:                                {col1 => 'User account creation',
  305:                                 col2 => 'Usernames which may be created',},
  306:                                {col1 => 'Context',
  307:                                 col2 => 'Assignable authentication types'}],
  308:                   },
  309:         'usermodification' =>
  310:                   { text => 'User modification',
  311:                     help => 'Domain_Configuration_User_Modification',
  312:                     header => [{col1 => 'Target user has role',
  313:                                 col2 => 'User information updateable in author context'},
  314:                                {col1 => 'Target user has role',
  315:                                 col2 => 'User information updateable in course context'},
  316:                                {col1 => "Status of user",
  317:                                 col2 => 'Information settable when self-creating account (if directory data blank)'}],
  318:                   },
  319:         'scantron' =>
  320:                   { text => 'Bubblesheet format file',
  321:                     help => 'Domain_Configuration_Scantron_Format',
  322:                     header => [ {col1 => 'Item',
  323:                                  col2 => '',
  324:                               }],
  325:                   },
  326:         'requestcourses' => 
  327:                  {text => 'Request creation of courses',
  328:                   help => 'Domain_Configuration_Request_Courses',
  329:                   header => [{col1 => 'User affiliation',
  330:                               col2 => 'Availability/Processing of requests',},
  331:                              {col1 => 'Setting',
  332:                               col2 => 'Value'}],
  333:                  },
  334:         'requestauthor' =>
  335:                  {text => 'Request authoring space',
  336:                   help => 'Domain_Configuration_Request_Author',
  337:                   header => [{col1 => 'User affiliation',
  338:                               col2 => 'Availability/Processing of requests',},
  339:                              {col1 => 'Setting',
  340:                               col2 => 'Value'}],
  341:                  },
  342:         'coursecategories' =>
  343:                   { text => 'Cataloging of courses/communities',
  344:                     help => 'Domain_Configuration_Cataloging_Courses',
  345:                     header => [{col1 => 'Category settings',
  346:                                 col2 => '',},
  347:                                {col1 => 'Categories',
  348:                                 col2 => '',
  349:                                }],
  350:                   },
  351:         'serverstatuses' =>
  352:                  {text   => 'Access to server status pages',
  353:                   help   => 'Domain_Configuration_Server_Status',
  354:                   header => [{col1 => 'Status Page',
  355:                               col2 => 'Other named users',
  356:                               col3 => 'Specific IPs',
  357:                             }],
  358:                  },
  359:         'helpsettings' =>
  360:                  {text   => 'Help page settings',
  361:                   help   => 'Domain_Configuration_Help_Settings',
  362:                   header => [{col1 => 'Help Settings (logged-in users)',
  363:                               col2 => 'Value'}],
  364:                  },
  365:         'coursedefaults' => 
  366:                  {text => 'Course/Community defaults',
  367:                   help => 'Domain_Configuration_Course_Defaults',
  368:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  369:                               col2 => 'Value',},
  370:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  371:                               col2 => 'Value',},],
  372:                  },
  373:         'privacy' => 
  374:                  {text   => 'User Privacy',
  375:                   help   => 'Domain_Configuration_User_Privacy',
  376:                   header => [{col1 => 'Setting',
  377:                               col2 => 'Value',}],
  378:                  },
  379:         'usersessions' =>
  380:                  {text  => 'User session hosting/offloading',
  381:                   help  => 'Domain_Configuration_User_Sessions',
  382:                   header => [{col1 => 'Domain server',
  383:                               col2 => 'Servers to offload sessions to when busy'},
  384:                              {col1 => 'Hosting of users from other domains',
  385:                               col2 => 'Rules'},
  386:                              {col1 => "Hosting domain's own users elsewhere",
  387:                               col2 => 'Rules'}],
  388:                  },
  389:          'loadbalancing' =>
  390:                  {text  => 'Dedicated Load Balancer(s)',
  391:                   help  => 'Domain_Configuration_Load_Balancing',
  392:                   header => [{col1 => 'Balancers',
  393:                               col2 => 'Default destinations',
  394:                               col3 => 'User affiliation',
  395:                               col4 => 'Overrides'},
  396:                             ],
  397:                  },
  398:     );
  399:     if (keys(%servers) > 1) {
  400:         $prefs{'login'}  = { text   => 'Log-in page options',
  401:                              help   => 'Domain_Configuration_Login_Page',
  402:                             header => [{col1 => 'Log-in Service',
  403:                                         col2 => 'Server Setting',},
  404:                                        {col1 => 'Log-in Page Items',
  405:                                         col2 => ''},
  406:                                        {col1 => 'Log-in Help',
  407:                                         col2 => 'Value'}],
  408:                            };
  409:     }
  410: 
  411:     my @roles = ('student','coordinator','author','admin');
  412:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  413:     &Apache::lonhtmlcommon::add_breadcrumb
  414:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  415:       text=>"Settings to display/modify"});
  416:     my $confname = $dom.'-domainconfig';
  417: 
  418:     if ($phase eq 'process') {
  419:         if (&Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  420:                                                      \%prefs,\%domconfig,$confname,\@roles) eq 'update') {
  421:             $r->rflush();
  422:             &devalidate_remote_domconfs($dom);
  423:         }
  424:     } elsif ($phase eq 'display') {
  425:         my $js = &recaptcha_js().
  426:                  &credits_js();
  427:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  428:             my ($othertitle,$usertypes,$types) =
  429:                 &Apache::loncommon::sorted_inst_types($dom);
  430:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  431:                                           $domconfig{'loadbalancing'}).
  432:                    &new_spares_js().
  433:                    &common_domprefs_js().
  434:                    &Apache::loncommon::javascript_array_indexof();
  435:         }
  436:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  437:     } else {
  438: # check if domconfig user exists for the domain.
  439:         my $servadm = $r->dir_config('lonAdmEMail');
  440:         my ($configuserok,$author_ok,$switchserver) =
  441:             &config_check($dom,$confname,$servadm);
  442:         unless ($configuserok eq 'ok') {
  443:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  444:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  445:                            $confname).
  446:                       '<br />'
  447:             );
  448:             if ($switchserver) {
  449:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  450:                           '<br />'.
  451:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  452:                           '<br />'.
  453:                           &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).
  454:                           '<br />'.
  455:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  456:                 );
  457:             } else {
  458:                 $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.').
  459:                           '<br />'.
  460:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  461:                 );
  462:             }
  463:             $r->print(&Apache::loncommon::end_page());
  464:             return OK;
  465:         }
  466:         if (keys(%domconfig) == 0) {
  467:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  468:             my @ids=&Apache::lonnet::current_machine_ids();
  469:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  470:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  471:                 my @loginimages = ('img','logo','domlogo','login');
  472:                 my $custom_img_count = 0;
  473:                 foreach my $img (@loginimages) {
  474:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  475:                         $custom_img_count ++;
  476:                     }
  477:                 }
  478:                 foreach my $role (@roles) {
  479:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  480:                         $custom_img_count ++;
  481:                     }
  482:                 }
  483:                 if ($custom_img_count > 0) {
  484:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  485:                     my $switch_server = &check_switchserver($dom,$confname);
  486:                     $r->print(
  487:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  488:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  489:     &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 />'.
  490:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  491:                     if ($switch_server) {
  492:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  493:                     }
  494:                     $r->print(&Apache::loncommon::end_page());
  495:                     return OK;
  496:                 }
  497:             }
  498:         }
  499:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  500:     }
  501:     return OK;
  502: }
  503: 
  504: sub process_changes {
  505:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  506:     my %domconfig;
  507:     if (ref($values) eq 'HASH') {
  508:         %domconfig = %{$values};
  509:     }
  510:     my $output;
  511:     if ($action eq 'login') {
  512:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  513:     } elsif ($action eq 'rolecolors') {
  514:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  515:                                      $lastactref,%domconfig);
  516:     } elsif ($action eq 'quotas') {
  517:         $output = &modify_quotas($dom,$action,%domconfig);
  518:     } elsif ($action eq 'autoenroll') {
  519:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  520:     } elsif ($action eq 'autoupdate') {
  521:         $output = &modify_autoupdate($dom,%domconfig);
  522:     } elsif ($action eq 'autocreate') {
  523:         $output = &modify_autocreate($dom,%domconfig);
  524:     } elsif ($action eq 'directorysrch') {
  525:         $output = &modify_directorysrch($dom,%domconfig);
  526:     } elsif ($action eq 'usercreation') {
  527:         $output = &modify_usercreation($dom,%domconfig);
  528:     } elsif ($action eq 'usermodification') {
  529:         $output = &modify_usermodification($dom,%domconfig);
  530:     } elsif ($action eq 'contacts') {
  531:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  532:     } elsif ($action eq 'defaults') {
  533:         $output = &modify_defaults($dom,$r,%domconfig);
  534:     } elsif ($action eq 'scantron') {
  535:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  536:     } elsif ($action eq 'coursecategories') {
  537:         $output = &modify_coursecategories($dom,%domconfig);
  538:     } elsif ($action eq 'serverstatuses') {
  539:         $output = &modify_serverstatuses($dom,%domconfig);
  540:     } elsif ($action eq 'requestcourses') {
  541:         $output = &modify_quotas($dom,$action,%domconfig);
  542:     } elsif ($action eq 'requestauthor') {
  543:         $output = &modify_quotas($dom,$action,%domconfig);
  544:     } elsif ($action eq 'helpsettings') {
  545:         $output = &modify_helpsettings($r,$dom,$confname,%domconfig);
  546:     } elsif ($action eq 'coursedefaults') {
  547:         $output = &modify_coursedefaults($dom,%domconfig);
  548:     } elsif ($action eq 'usersessions') {
  549:         $output = &modify_usersessions($dom,%domconfig);
  550:     } elsif ($action eq 'loadbalancing') {
  551:         $output = &modify_loadbalancing($dom,%domconfig);
  552:     }
  553:     return $output;
  554: }
  555: 
  556: sub print_config_box {
  557:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  558:     my $rowtotal = 0;
  559:     my $output;
  560:     if ($action eq 'coursecategories') {
  561:         $output = &coursecategories_javascript($settings);
  562:     }
  563:     $output .= 
  564:          '<table class="LC_nested_outer">
  565:           <tr>
  566:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  567:            &mt($item->{text}).'&nbsp;'.
  568:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  569:           '</tr>';
  570:     $rowtotal ++;
  571:     my $numheaders = 1;
  572:     if (ref($item->{'header'}) eq 'ARRAY') {
  573:         $numheaders = scalar(@{$item->{'header'}});
  574:     }
  575:     if ($numheaders > 1) {
  576:         my $colspan = '';
  577:         my $rightcolspan = '';
  578:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') || 
  579:             (($action eq 'login') && ($numheaders < 3))) {
  580:             $colspan = ' colspan="2"';
  581:         }
  582:         if ($action eq 'usersessions') {
  583:             $rightcolspan = ' colspan="3"'; 
  584:         }
  585:         $output .= '
  586:           <tr>
  587:            <td>
  588:             <table class="LC_nested">
  589:              <tr class="LC_info_row">
  590:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  591:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  592:              </tr>';
  593:         $rowtotal ++;
  594:         if ($action eq 'autoupdate') {
  595:             $output .= &print_autoupdate('top',$dom,$settings,\$rowtotal);
  596:         } elsif ($action eq 'usercreation') {
  597:             $output .= &print_usercreation('top',$dom,$settings,\$rowtotal);
  598:         } elsif ($action eq 'usermodification') {
  599:             $output .= &print_usermodification('top',$dom,$settings,\$rowtotal);
  600:         } elsif ($action eq 'coursecategories') {
  601:             $output .= &print_coursecategories('top',$dom,$item,$settings,\$rowtotal);
  602:         } elsif ($action eq 'login') {
  603:             if ($numheaders == 3) {
  604:                 $colspan = ' colspan="2"';
  605:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  606:             } else {
  607:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  608:             }
  609:         } elsif ($action eq 'requestcourses') {
  610:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  611:         } elsif ($action eq 'requestauthor') {
  612:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  613:         } elsif ($action eq 'usersessions') {
  614:             $output .= &print_usersessions('top',$dom,$settings,\$rowtotal); 
  615:         } elsif ($action eq 'rolecolors') {
  616:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  617:         } elsif ($action eq 'coursedefaults') {
  618:             $output .= &print_coursedefaults('top',$dom,$settings,\$rowtotal);
  619:         }
  620:         $output .= '
  621:            </table>
  622:           </td>
  623:          </tr>
  624:          <tr>
  625:            <td>
  626:             <table class="LC_nested">
  627:              <tr class="LC_info_row">
  628:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>';
  629:         $output .= '
  630:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  631:              </tr>';
  632:             $rowtotal ++;
  633:         if ($action eq 'autoupdate') {
  634:             $output .= &print_autoupdate('middle',$dom,$settings,\$rowtotal).'
  635:            </table>
  636:           </td>
  637:          </tr>
  638:          <tr>
  639:            <td>
  640:             <table class="LC_nested">
  641:              <tr class="LC_info_row">
  642:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  643:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  644:             &print_autoupdate('bottom',$dom,$settings,\$rowtotal);
  645:             $rowtotal ++;
  646:         } elsif ($action eq 'usercreation') {
  647:             $output .= &print_usercreation('middle',$dom,$settings,\$rowtotal).'
  648:            </table>
  649:           </td>
  650:          </tr>
  651:          <tr>
  652:            <td>
  653:             <table class="LC_nested">
  654:              <tr class="LC_info_row">
  655:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  656:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>             </tr>'.
  657:             &print_usercreation('bottom',$dom,$settings,\$rowtotal);
  658:             $rowtotal ++;
  659:         } elsif ($action eq 'usermodification') {
  660:             $output .= &print_usermodification('middle',$dom,$settings,\$rowtotal).'
  661:            </table>
  662:           </td>
  663:          </tr>
  664:          <tr>
  665:            <td>
  666:             <table class="LC_nested">
  667:              <tr class="LC_info_row">
  668:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  669:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  670:                        &print_usermodification('bottom',$dom,$settings,\$rowtotal);
  671:             $rowtotal ++;
  672:         } elsif ($action eq 'coursecategories') {
  673:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  674:         } elsif ($action eq 'login') {
  675:             if ($numheaders == 3) {
  676:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  677:            </table>
  678:           </td>
  679:          </tr>
  680:          <tr>
  681:            <td>
  682:             <table class="LC_nested">
  683:              <tr class="LC_info_row">
  684:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  685:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  686:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  687:                 $rowtotal ++;
  688:             } else {
  689:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  690:             }
  691:         } elsif ($action eq 'requestcourses') {
  692:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  693:         } elsif ($action eq 'requestauthor') {
  694:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  695:         } elsif ($action eq 'usersessions') {
  696:             $output .= &print_usersessions('middle',$dom,$settings,\$rowtotal).'
  697:            </table>
  698:           </td>
  699:          </tr>
  700:          <tr>
  701:            <td>
  702:             <table class="LC_nested">
  703:              <tr class="LC_info_row">
  704:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  705:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  706:                        &print_usersessions('bottom',$dom,$settings,\$rowtotal);
  707:             $rowtotal ++;
  708:         } elsif ($action eq 'coursedefaults') {
  709:             $output .= &print_coursedefaults('bottom',$dom,$settings,\$rowtotal);
  710:         } elsif ($action eq 'rolecolors') {
  711:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  712:            </table>
  713:           </td>
  714:          </tr>
  715:          <tr>
  716:            <td>
  717:             <table class="LC_nested">
  718:              <tr class="LC_info_row">
  719:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  720:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  721:               <td class="LC_right_item" valign="top">'.
  722:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  723:              </tr>'.
  724:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  725:            </table>
  726:           </td>
  727:          </tr>
  728:          <tr>
  729:            <td>
  730:             <table class="LC_nested">
  731:              <tr class="LC_info_row">
  732:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  733:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  734:              </tr>'.
  735:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  736:             $rowtotal += 2;
  737:         }
  738:     } else {
  739:         $output .= '
  740:           <tr>
  741:            <td>
  742:             <table class="LC_nested">
  743:              <tr class="LC_info_row">';
  744:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  745:             $output .= '  
  746:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  747:         } elsif ($action eq 'serverstatuses') {
  748:             $output .= '
  749:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  750:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  751: 
  752:         } else {
  753:             $output .= '
  754:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  755:         }
  756:         if (defined($item->{'header'}->[0]->{'col3'})) {
  757:             $output .= '<td class="LC_left_item" valign="top">'.
  758:                        &mt($item->{'header'}->[0]->{'col2'});
  759:             if ($action eq 'serverstatuses') {
  760:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  761:             } 
  762:         } else {
  763:             $output .= '<td class="LC_right_item" valign="top">'.
  764:                        &mt($item->{'header'}->[0]->{'col2'});
  765:         }
  766:         $output .= '</td>';
  767:         if ($item->{'header'}->[0]->{'col3'}) {
  768:             if (defined($item->{'header'}->[0]->{'col4'})) {
  769:                 $output .= '<td class="LC_left_item" valign="top">'.
  770:                             &mt($item->{'header'}->[0]->{'col3'});
  771:             } else {
  772:                 $output .= '<td class="LC_right_item" valign="top">'.
  773:                            &mt($item->{'header'}->[0]->{'col3'});
  774:             }
  775:             if ($action eq 'serverstatuses') {
  776:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  777:             }
  778:             $output .= '</td>';
  779:         }
  780:         if ($item->{'header'}->[0]->{'col4'}) {
  781:             $output .= '<td class="LC_right_item" valign="top">'.
  782:                        &mt($item->{'header'}->[0]->{'col4'});
  783:         }
  784:         $output .= '</tr>';
  785:         $rowtotal ++;
  786:         if ($action eq 'quotas') {
  787:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  788:         } elsif ($action eq 'autoenroll') {
  789:             $output .= &print_autoenroll($dom,$settings,\$rowtotal);
  790:         } elsif ($action eq 'autocreate') {
  791:             $output .= &print_autocreate($dom,$settings,\$rowtotal);
  792:         } elsif ($action eq 'directorysrch') {
  793:             $output .= &print_directorysrch($dom,$settings,\$rowtotal);
  794:         } elsif ($action eq 'contacts') {
  795:             $output .= &print_contacts($dom,$settings,\$rowtotal);
  796:         } elsif ($action eq 'defaults') {
  797:             $output .= &print_defaults($dom,\$rowtotal);
  798:         } elsif ($action eq 'scantron') {
  799:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  800:         } elsif ($action eq 'serverstatuses') {
  801:             $output .= &print_serverstatuses($dom,$settings,\$rowtotal);
  802:         } elsif ($action eq 'helpsettings') {
  803:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  804:         } elsif ($action eq 'loadbalancing') {
  805:             $output .= &print_loadbalancing($dom,$settings,\$rowtotal);
  806:         }
  807:     }
  808:     $output .= '
  809:    </table>
  810:   </td>
  811:  </tr>
  812: </table><br />';
  813:     return ($output,$rowtotal);
  814: }
  815: 
  816: sub print_login {
  817:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  818:     my ($css_class,$datatable);
  819:     my %choices = &login_choices();
  820: 
  821:     if ($caller eq 'service') {
  822:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  823:         my $choice = $choices{'disallowlogin'};
  824:         $css_class = ' class="LC_odd_row"';
  825:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  826:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  827:                       '<th>'.$choices{'server'}.'</th>'.
  828:                       '<th>'.$choices{'serverpath'}.'</th>'.
  829:                       '<th>'.$choices{'custompath'}.'</th>'.
  830:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  831:         my %disallowed;
  832:         if (ref($settings) eq 'HASH') {
  833:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  834:                %disallowed = %{$settings->{'loginvia'}};
  835:             }
  836:         }
  837:         foreach my $lonhost (sort(keys(%servers))) {
  838:             my $direct = 'selected="selected"';
  839:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  840:                 if ($disallowed{$lonhost}{'server'} ne '') {
  841:                     $direct = '';
  842:                 }
  843:             }
  844:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  845:                           '<td><select name="'.$lonhost.'_server">'.
  846:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  847:                           '</option>';
  848:             foreach my $hostid (sort(keys(%servers))) {
  849:                 next if ($servers{$hostid} eq $servers{$lonhost});
  850:                 my $selected = '';
  851:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  852:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  853:                         $selected = 'selected="selected"';
  854:                     }
  855:                 }
  856:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  857:                               $servers{$hostid}.'</option>';
  858:             }
  859:             $datatable .= '</select></td>'.
  860:                           '<td><select name="'.$lonhost.'_serverpath">';
  861:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  862:                 my $pathname = $path;
  863:                 if ($path eq 'custom') {
  864:                     $pathname = &mt('Custom Path').' ->';
  865:                 }
  866:                 my $selected = '';
  867:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  868:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  869:                         $selected = 'selected="selected"';
  870:                     }
  871:                 } elsif ($path eq '') {
  872:                     $selected = 'selected="selected"';
  873:                 }
  874:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  875:             }
  876:             $datatable .= '</select></td>';
  877:             my ($custom,$exempt);
  878:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  879:                 $custom = $disallowed{$lonhost}{'custompath'};
  880:                 $exempt = $disallowed{$lonhost}{'exempt'};
  881:             }
  882:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  883:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  884:                           '</tr>';
  885:         }
  886:         $datatable .= '</table></td></tr>';
  887:         return $datatable;
  888:     } elsif ($caller eq 'page') {
  889:         my %defaultchecked = ( 
  890:                                'coursecatalog' => 'on',
  891:                                'helpdesk'      => 'on',
  892:                                'adminmail'     => 'off',
  893:                                'newuser'       => 'off',
  894:                              );
  895:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  896:         my (%checkedon,%checkedoff);
  897:         foreach my $item (@toggles) {
  898:             if ($defaultchecked{$item} eq 'on') { 
  899:                 $checkedon{$item} = ' checked="checked" ';
  900:                 $checkedoff{$item} = ' ';
  901:             } elsif ($defaultchecked{$item} eq 'off') {
  902:                 $checkedoff{$item} = ' checked="checked" ';
  903:                 $checkedon{$item} = ' ';
  904:             }
  905:         }
  906:         my @images = ('img','logo','domlogo','login');
  907:         my @logintext = ('textcol','bgcol');
  908:         my @bgs = ('pgbg','mainbg','sidebg');
  909:         my @links = ('link','alink','vlink');
  910:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  911:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  912:         my (%is_custom,%designs);
  913:         my %defaults = (
  914:                        font => $defaultdesign{'login.font'},
  915:                        );
  916:         foreach my $item (@images) {
  917:             $defaults{$item} = $defaultdesign{'login.'.$item};
  918:             $defaults{'showlogo'}{$item} = 1;
  919:         }
  920:         foreach my $item (@bgs) {
  921:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  922:         }
  923:         foreach my $item (@logintext) {
  924:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  925:         }
  926:         foreach my $item (@links) {
  927:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  928:         }
  929:         if (ref($settings) eq 'HASH') {
  930:             foreach my $item (@toggles) {
  931:                 if ($settings->{$item} eq '1') {
  932:                     $checkedon{$item} =  ' checked="checked" ';
  933:                     $checkedoff{$item} = ' ';
  934:                 } elsif ($settings->{$item} eq '0') {
  935:                     $checkedoff{$item} =  ' checked="checked" ';
  936:                     $checkedon{$item} = ' ';
  937:                 }
  938:             }
  939:             foreach my $item (@images) {
  940:                 if (defined($settings->{$item})) {
  941:                     $designs{$item} = $settings->{$item};
  942:                     $is_custom{$item} = 1;
  943:                 }
  944:                 if (defined($settings->{'showlogo'}{$item})) {
  945:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  946:                 }
  947:             }
  948:             foreach my $item (@logintext) {
  949:                 if ($settings->{$item} ne '') {
  950:                     $designs{'logintext'}{$item} = $settings->{$item};
  951:                     $is_custom{$item} = 1;
  952:                 }
  953:             }
  954:             if ($settings->{'font'} ne '') {
  955:                 $designs{'font'} = $settings->{'font'};
  956:                 $is_custom{'font'} = 1;
  957:             }
  958:             foreach my $item (@bgs) {
  959:                 if ($settings->{$item} ne '') {
  960:                     $designs{'bgs'}{$item} = $settings->{$item};
  961:                     $is_custom{$item} = 1;
  962:                 }
  963:             }
  964:             foreach my $item (@links) {
  965:                 if ($settings->{$item} ne '') {
  966:                     $designs{'links'}{$item} = $settings->{$item};
  967:                     $is_custom{$item} = 1;
  968:                 }
  969:             }
  970:         } else {
  971:             if ($designhash{$dom.'.login.font'} ne '') {
  972:                 $designs{'font'} = $designhash{$dom.'.login.font'};
  973:                 $is_custom{'font'} = 1;
  974:             }
  975:             foreach my $item (@images) {
  976:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  977:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
  978:                     $is_custom{$item} = 1;
  979:                 }
  980:             }
  981:             foreach my $item (@bgs) {
  982:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  983:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
  984:                     $is_custom{$item} = 1;
  985:                 }
  986:             }
  987:             foreach my $item (@links) {
  988:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  989:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
  990:                     $is_custom{$item} = 1;
  991:                 }
  992:             }
  993:         }
  994:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
  995:                                                       logo => 'Institution Logo',
  996:                                                       domlogo => 'Domain Logo',
  997:                                                       login => 'Login box');
  998:         my $itemcount = 1;
  999:         foreach my $item (@toggles) {
 1000:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1001:             $datatable .=  
 1002:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1003:                 '</td><td>'.
 1004:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1005:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1006:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1007:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1008:                 '</tr>';
 1009:             $itemcount ++;
 1010:         }
 1011:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1012:         $datatable .= '</tr></table></td></tr>';
 1013:     } elsif ($caller eq 'help') {
 1014:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1015:         my $switchserver = &check_switchserver($dom,$confname);
 1016:         my $itemcount = 1;
 1017:         $defaulturl = '/adm/loginproblems.html';
 1018:         $defaulttype = 'default';
 1019:         %lt = &Apache::lonlocal::texthash (
 1020:                      del     => 'Delete?',
 1021:                      rep     => 'Replace:',
 1022:                      upl     => 'Upload:',
 1023:                      default => 'Default',
 1024:                      custom  => 'Custom',
 1025:                                              );
 1026:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1027:         my @currlangs;
 1028:         if (ref($settings) eq 'HASH') {
 1029:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1030:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1031:                     next if ($settings->{'helpurl'}{$key} eq '');
 1032:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1033:                     $type{$key} = 'custom';
 1034:                     unless ($key eq 'nolang') {
 1035:                         push(@currlangs,$key);
 1036:                     }
 1037:                 }
 1038:             } elsif ($settings->{'helpurl'} ne '') {
 1039:                 $type{'nolang'} = 'custom';
 1040:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1041:             }
 1042:         }
 1043:         foreach my $lang ('nolang',sort(@currlangs)) {
 1044:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1045:             $datatable .= '<tr'.$css_class.'>';
 1046:             if ($url{$lang} eq '') {
 1047:                 $url{$lang} = $defaulturl;
 1048:             }
 1049:             if ($type{$lang} eq '') {
 1050:                 $type{$lang} = $defaulttype;
 1051:             }
 1052:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1053:             if ($lang eq 'nolang') {
 1054:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1055:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1056:             } else {
 1057:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1058:                                   $langchoices{$lang},
 1059:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1060:             }
 1061:             $datatable .= '</span></td>'."\n".
 1062:                           '<td class="LC_left_item">';
 1063:             if ($type{$lang} eq 'custom') {
 1064:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1065:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1066:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1067:             } else {
 1068:                 $datatable .= $lt{'upl'};
 1069:             }
 1070:             $datatable .='<br />';
 1071:             if ($switchserver) {
 1072:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1073:             } else {
 1074:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1075:             }
 1076:             $datatable .= '</td></tr>';
 1077:             $itemcount ++;
 1078:         }
 1079:         my @addlangs;
 1080:         foreach my $lang (sort(keys(%langchoices))) {
 1081:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1082:             push(@addlangs,$lang);
 1083:         }
 1084:         if (@addlangs > 0) {
 1085:             my %toadd;
 1086:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1087:             $toadd{''} = &mt('Select');
 1088:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1089:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1090:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1091:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1092:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1093:             if ($switchserver) {
 1094:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1095:             } else {
 1096:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1097:             }
 1098:             $datatable .= '</td></tr>';
 1099:             $itemcount ++;
 1100:         }
 1101:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1102:     }
 1103:     return $datatable;
 1104: }
 1105: 
 1106: sub login_choices {
 1107:     my %choices =
 1108:         &Apache::lonlocal::texthash (
 1109:             coursecatalog => 'Display Course/Community Catalog link?',
 1110:             adminmail     => "Display Administrator's E-mail Address?",
 1111:             helpdesk      => 'Display "Contact Helpdesk" link',
 1112:             disallowlogin => "Login page requests redirected",
 1113:             hostid        => "Server",
 1114:             server        => "Redirect to:",
 1115:             serverpath    => "Path",
 1116:             custompath    => "Custom", 
 1117:             exempt        => "Exempt IP(s)",
 1118:             directlogin   => "No redirect",
 1119:             newuser       => "Link to create a user account",
 1120:             img           => "Header",
 1121:             logo          => "Main Logo",
 1122:             domlogo       => "Domain Logo",
 1123:             login         => "Log-in Header", 
 1124:             textcol       => "Text color",
 1125:             bgcol         => "Box color",
 1126:             bgs           => "Background colors",
 1127:             links         => "Link colors",
 1128:             font          => "Font color",
 1129:             pgbg          => "Header",
 1130:             mainbg        => "Page",
 1131:             sidebg        => "Login box",
 1132:             link          => "Link",
 1133:             alink         => "Active link",
 1134:             vlink         => "Visited link",
 1135:         );
 1136:     return %choices;
 1137: }
 1138: 
 1139: sub print_rolecolors {
 1140:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1141:     my %choices = &color_font_choices();
 1142:     my @bgs = ('pgbg','tabbg','sidebg');
 1143:     my @links = ('link','alink','vlink');
 1144:     my @images = ('img');
 1145:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1146:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1147:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1148:     my (%is_custom,%designs);
 1149:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1150:     if (ref($settings) eq 'HASH') {
 1151:         if (ref($settings->{$role}) eq 'HASH') {
 1152:             if ($settings->{$role}->{'img'} ne '') {
 1153:                 $designs{'img'} = $settings->{$role}->{'img'};
 1154:                 $is_custom{'img'} = 1;
 1155:             }
 1156:             if ($settings->{$role}->{'font'} ne '') {
 1157:                 $designs{'font'} = $settings->{$role}->{'font'};
 1158:                 $is_custom{'font'} = 1;
 1159:             }
 1160:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1161:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1162:                 $is_custom{'fontmenu'} = 1;
 1163:             }
 1164:             foreach my $item (@bgs) {
 1165:                 if ($settings->{$role}->{$item} ne '') {
 1166:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1167:                     $is_custom{$item} = 1;
 1168:                 }
 1169:             }
 1170:             foreach my $item (@links) {
 1171:                 if ($settings->{$role}->{$item} ne '') {
 1172:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1173:                     $is_custom{$item} = 1;
 1174:                 }
 1175:             }
 1176:         }
 1177:     } else {
 1178:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1179:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1180:             $is_custom{'img'} = 1;
 1181:         }
 1182:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1183:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1184:             $is_custom{'fontmenu'} = 1; 
 1185:         }
 1186:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1187:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1188:             $is_custom{'font'} = 1;
 1189:         }
 1190:         foreach my $item (@bgs) {
 1191:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1192:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1193:                 $is_custom{$item} = 1;
 1194:             
 1195:             }
 1196:         }
 1197:         foreach my $item (@links) {
 1198:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1199:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1200:                 $is_custom{$item} = 1;
 1201:             }
 1202:         }
 1203:     }
 1204:     my $itemcount = 1;
 1205:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1206:     $datatable .= '</tr></table></td></tr>';
 1207:     return $datatable;
 1208: }
 1209: 
 1210: sub role_defaults {
 1211:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1212:     my %defaults;
 1213:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1214:         return %defaults;
 1215:     }
 1216:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1217:     if ($role eq 'login') {
 1218:         %defaults = (
 1219:                        font => $defaultdesign{$role.'.font'},
 1220:                     );
 1221:         if (ref($logintext) eq 'ARRAY') {
 1222:             foreach my $item (@{$logintext}) {
 1223:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1224:             }
 1225:         }
 1226:         foreach my $item (@{$images}) {
 1227:             $defaults{'showlogo'}{$item} = 1;
 1228:         }
 1229:     } else {
 1230:         %defaults = (
 1231:                        img => $defaultdesign{$role.'.img'},
 1232:                        font => $defaultdesign{$role.'.font'},
 1233:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1234:                     );
 1235:     }
 1236:     foreach my $item (@{$bgs}) {
 1237:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1238:     }
 1239:     foreach my $item (@{$links}) {
 1240:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1241:     }
 1242:     foreach my $item (@{$images}) {
 1243:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1244:     }
 1245:     return %defaults;
 1246: }
 1247: 
 1248: sub display_color_options {
 1249:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1250:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1251:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1252:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1253:     my $datatable = '<tr'.$css_class.'>'.
 1254:         '<td>'.$choices->{'font'}.'</td>';
 1255:     if (!$is_custom->{'font'}) {
 1256:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1257:     } else {
 1258:         $datatable .= '<td>&nbsp;</td>';
 1259:     }
 1260:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1261: 
 1262:     $datatable .= '<td><span class="LC_nobreak">'.
 1263:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1264:                   ' value="'.$current_color.'" />&nbsp;'.
 1265:                   '&nbsp;</td></tr>';
 1266:     unless ($role eq 'login') { 
 1267:         $datatable .= '<tr'.$css_class.'>'.
 1268:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1269:         if (!$is_custom->{'fontmenu'}) {
 1270:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1271:         } else {
 1272:             $datatable .= '<td>&nbsp;</td>';
 1273:         }
 1274: 	$current_color = $designs->{'fontmenu'} ?
 1275: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1276:         $datatable .= '<td><span class="LC_nobreak">'.
 1277:                       '<input class="colorchooser" type="text" size="10" name="'
 1278: 		      .$role.'_fontmenu"'.
 1279:                       ' value="'.$current_color.'" />&nbsp;'.
 1280:                       '&nbsp;</td></tr>';
 1281:     }
 1282:     my $switchserver = &check_switchserver($dom,$confname);
 1283:     foreach my $img (@{$images}) {
 1284: 	$itemcount ++;
 1285:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1286:         $datatable .= '<tr'.$css_class.'>'.
 1287:                       '<td>'.$choices->{$img};
 1288:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1289:         if ($role eq 'login') {
 1290:             if ($img eq 'login') {
 1291:                 $login_hdr_pick =
 1292:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1293:                 $logincolors =
 1294:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1295:                                        $designs,$defaults);
 1296:             } elsif ($img ne 'domlogo') {
 1297:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1298:             }
 1299:         }
 1300:         $datatable .= '</td>';
 1301:         if ($designs->{$img} ne '') {
 1302:             $imgfile = $designs->{$img};
 1303: 	    $img_import = ($imgfile =~ m{^/adm/});
 1304:         } else {
 1305:             $imgfile = $defaults->{$img};
 1306:         }
 1307:         if ($imgfile) {
 1308:             my ($showfile,$fullsize);
 1309:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1310:                 my $urldir = $1;
 1311:                 my $filename = $2;
 1312:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1313:                 if (@info) {
 1314:                     my $thumbfile = 'tn-'.$filename;
 1315:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1316:                     if (@thumb) {
 1317:                         $showfile = $urldir.'/'.$thumbfile;
 1318:                     } else {
 1319:                         $showfile = $imgfile;
 1320:                     }
 1321:                 } else {
 1322:                     $showfile = '';
 1323:                 }
 1324:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1325:                 $showfile = $imgfile;
 1326:                 my $imgdir = $1;
 1327:                 my $filename = $2;
 1328:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1329:                     $showfile = "/$imgdir/tn-".$filename;
 1330:                 } else {
 1331:                     my $input = $londocroot.$imgfile;
 1332:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1333:                     if (!-e $output) {
 1334:                         my ($width,$height) = &thumb_dimensions();
 1335:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1336:                         if ($fullwidth ne '' && $fullheight ne '') {
 1337:                             if ($fullwidth > $width && $fullheight > $height) { 
 1338:                                 my $size = $width.'x'.$height;
 1339:                                 system("convert -sample $size $input $output");
 1340:                                 $showfile = "/$imgdir/tn-".$filename;
 1341:                             }
 1342:                         }
 1343:                     }
 1344:                 }
 1345:             }
 1346:             if ($showfile) {
 1347:                 if ($showfile =~ m{^/(adm|res)/}) {
 1348:                     if ($showfile =~ m{^/res/}) {
 1349:                         my $local_showfile =
 1350:                             &Apache::lonnet::filelocation('',$showfile);
 1351:                         &Apache::lonnet::repcopy($local_showfile);
 1352:                     }
 1353:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1354:                 }
 1355:                 if ($imgfile) {
 1356:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1357:                         if ($imgfile =~ m{^/res/}) {
 1358:                             my $local_imgfile =
 1359:                                 &Apache::lonnet::filelocation('',$imgfile);
 1360:                             &Apache::lonnet::repcopy($local_imgfile);
 1361:                         }
 1362:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1363:                     } else {
 1364:                         $fullsize = $imgfile;
 1365:                     }
 1366:                 }
 1367:                 $datatable .= '<td>';
 1368:                 if ($img eq 'login') {
 1369:                     $datatable .= $login_hdr_pick;
 1370:                 } 
 1371:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1372:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1373:             } else {
 1374:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1375:                               &mt('Upload:').'<br />';
 1376:             }
 1377:         } else {
 1378:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1379:                           &mt('Upload:').'<br />';
 1380:         }
 1381:         if ($switchserver) {
 1382:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1383:         } else {
 1384:             if ($img ne 'login') { # suppress file selection for Log-in header
 1385:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1386:             }
 1387:         }
 1388:         $datatable .= '</td></tr>';
 1389:     }
 1390:     $itemcount ++;
 1391:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1392:     $datatable .= '<tr'.$css_class.'>'.
 1393:                   '<td>'.$choices->{'bgs'}.'</td>';
 1394:     my $bgs_def;
 1395:     foreach my $item (@{$bgs}) {
 1396:         if (!$is_custom->{$item}) {
 1397:             $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>';
 1398:         }
 1399:     }
 1400:     if ($bgs_def) {
 1401:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1402:     } else {
 1403:         $datatable .= '<td>&nbsp;</td>';
 1404:     }
 1405:     $datatable .= '<td class="LC_right_item">'.
 1406:                   '<table border="0"><tr>';
 1407: 
 1408:     foreach my $item (@{$bgs}) {
 1409:         $datatable .= '<td align="center">'.$choices->{$item};
 1410: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1411:         if ($designs->{'bgs'}{$item}) {
 1412:             $datatable .= '&nbsp;';
 1413:         }
 1414:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1415:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1416:     }
 1417:     $datatable .= '</tr></table></td></tr>';
 1418:     $itemcount ++;
 1419:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1420:     $datatable .= '<tr'.$css_class.'>'.
 1421:                   '<td>'.$choices->{'links'}.'</td>';
 1422:     my $links_def;
 1423:     foreach my $item (@{$links}) {
 1424:         if (!$is_custom->{$item}) {
 1425:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1426:         }
 1427:     }
 1428:     if ($links_def) {
 1429:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1430:     } else {
 1431:         $datatable .= '<td>&nbsp;</td>';
 1432:     }
 1433:     $datatable .= '<td class="LC_right_item">'.
 1434:                   '<table border="0"><tr>';
 1435:     foreach my $item (@{$links}) {
 1436: 	my $color = $designs->{'link'}{$item} ? $designs->{'link'}{$item} : $defaults->{'links'}{$item};
 1437:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1438:         if ($designs->{'links'}{$item}) {
 1439:             $datatable.='&nbsp;';
 1440:         }
 1441:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1442:                       '" /></td>';
 1443:     }
 1444:     $$rowtotal += $itemcount;
 1445:     return $datatable;
 1446: }
 1447: 
 1448: sub logo_display_options {
 1449:     my ($img,$defaults,$designs) = @_;
 1450:     my $checkedon;
 1451:     if (ref($defaults) eq 'HASH') {
 1452:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1453:             if ($defaults->{'showlogo'}{$img}) {
 1454:                 $checkedon = 'checked="checked" ';     
 1455:             }
 1456:         } 
 1457:     }
 1458:     if (ref($designs) eq 'HASH') {
 1459:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1460:             if (defined($designs->{'showlogo'}{$img})) {
 1461:                 if ($designs->{'showlogo'}{$img} == 0) {
 1462:                     $checkedon = '';
 1463:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1464:                     $checkedon = 'checked="checked" ';
 1465:                 }
 1466:             }
 1467:         }
 1468:     }
 1469:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1470:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1471:            &mt('show').'</label>'."\n";
 1472: }
 1473: 
 1474: sub login_header_options  {
 1475:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1476:     my $output = '';
 1477:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1478:         $output .= &mt('Text default(s):').'<br />';
 1479:         if (!$is_custom->{'textcol'}) {
 1480:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1481:                        '&nbsp;&nbsp;&nbsp;';
 1482:         }
 1483:         if (!$is_custom->{'bgcol'}) {
 1484:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1485:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1486:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1487:         }
 1488:         $output .= '<br />';
 1489:     }
 1490:     $output .='<br />';
 1491:     return $output;
 1492: }
 1493: 
 1494: sub login_text_colors {
 1495:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1496:     my $color_menu = '<table border="0"><tr>';
 1497:     foreach my $item (@{$logintext}) {
 1498:         $color_menu .= '<td align="center">'.$choices->{$item};
 1499:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1500:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1501:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1502:     }
 1503:     $color_menu .= '</tr></table><br />';
 1504:     return $color_menu;
 1505: }
 1506: 
 1507: sub image_changes {
 1508:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1509:     my $output;
 1510:     if ($img eq 'login') {
 1511:             # suppress image for Log-in header
 1512:     } elsif (!$is_custom) {
 1513:         if ($img ne 'domlogo') {
 1514:             $output .= &mt('Default image:').'<br />';
 1515:         } else {
 1516:             $output .= &mt('Default in use:').'<br />';
 1517:         }
 1518:     }
 1519:     if ($img eq 'login') { # suppress image for Log-in header
 1520:         $output .= '<td>'.$logincolors;
 1521:     } else {
 1522:         if ($img_import) {
 1523:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1524:         }
 1525:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1526:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1527:         if ($is_custom) {
 1528:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1529:                        '<input type="checkbox" name="'.
 1530:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1531:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1532:         } else {
 1533:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1534:         }
 1535:     }
 1536:     return $output;
 1537: }
 1538: 
 1539: sub print_quotas {
 1540:     my ($dom,$settings,$rowtotal,$action) = @_;
 1541:     my $context;
 1542:     if ($action eq 'quotas') {
 1543:         $context = 'tools';
 1544:     } else {
 1545:         $context = $action;
 1546:     }
 1547:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1548:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1549:     my $typecount = 0;
 1550:     my ($css_class,%titles);
 1551:     if ($context eq 'requestcourses') {
 1552:         @usertools = ('official','unofficial','community');
 1553:         @options =('norequest','approval','validate','autolimit');
 1554:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1555:         %titles = &courserequest_titles();
 1556:     } elsif ($context eq 'requestauthor') {
 1557:         @usertools = ('author');
 1558:         @options = ('norequest','approval','automatic');
 1559:         %titles = &authorrequest_titles(); 
 1560:     } else {
 1561:         @usertools = ('aboutme','blog','webdav','portfolio');
 1562:         %titles = &tool_titles();
 1563:     }
 1564:     if (ref($types) eq 'ARRAY') {
 1565:         foreach my $type (@{$types}) {
 1566:             my ($currdefquota,$currauthorquota);
 1567:             unless (($context eq 'requestcourses') ||
 1568:                     ($context eq 'requestauthor')) {
 1569:                 if (ref($settings) eq 'HASH') {
 1570:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1571:                         $currdefquota = $settings->{defaultquota}->{$type};
 1572:                     } else {
 1573:                         $currdefquota = $settings->{$type};
 1574:                     }
 1575:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1576:                         $currauthorquota = $settings->{authorquota}->{$type};
 1577:                     }
 1578:                 }
 1579:             }
 1580:             if (defined($usertypes->{$type})) {
 1581:                 $typecount ++;
 1582:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1583:                 $datatable .= '<tr'.$css_class.'>'.
 1584:                               '<td>'.$usertypes->{$type}.'</td>'.
 1585:                               '<td class="LC_left_item">';
 1586:                 if ($context eq 'requestcourses') {
 1587:                     $datatable .= '<table><tr>';
 1588:                 }
 1589:                 my %cell;  
 1590:                 foreach my $item (@usertools) {
 1591:                     if ($context eq 'requestcourses') {
 1592:                         my ($curroption,$currlimit);
 1593:                         if (ref($settings) eq 'HASH') {
 1594:                             if (ref($settings->{$item}) eq 'HASH') {
 1595:                                 $curroption = $settings->{$item}->{$type};
 1596:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1597:                                     $currlimit = $1; 
 1598:                                 }
 1599:                             }
 1600:                         }
 1601:                         if (!$curroption) {
 1602:                             $curroption = 'norequest';
 1603:                         }
 1604:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1605:                         foreach my $option (@options) {
 1606:                             my $val = $option;
 1607:                             if ($option eq 'norequest') {
 1608:                                 $val = 0;  
 1609:                             }
 1610:                             if ($option eq 'validate') {
 1611:                                 my $canvalidate = 0;
 1612:                                 if (ref($validations{$item}) eq 'HASH') { 
 1613:                                     if ($validations{$item}{$type}) {
 1614:                                         $canvalidate = 1;
 1615:                                     }
 1616:                                 }
 1617:                                 next if (!$canvalidate);
 1618:                             }
 1619:                             my $checked = '';
 1620:                             if ($option eq $curroption) {
 1621:                                 $checked = ' checked="checked"';
 1622:                             } elsif ($option eq 'autolimit') {
 1623:                                 if ($curroption =~ /^autolimit/) {
 1624:                                     $checked = ' checked="checked"';
 1625:                                 }                       
 1626:                             } 
 1627:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1628:                                   '<input type="radio" name="crsreq_'.$item.
 1629:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1630:                                   $titles{$option}.'</label>';
 1631:                             if ($option eq 'autolimit') {
 1632:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1633:                                                 $item.'_limit_'.$type.'" size="1" '.
 1634:                                                 'value="'.$currlimit.'" />';
 1635:                             }
 1636:                             $cell{$item} .= '</span> ';
 1637:                             if ($option eq 'autolimit') {
 1638:                                 $cell{$item} .= $titles{'unlimited'};
 1639:                             }
 1640:                         }
 1641:                     } elsif ($context eq 'requestauthor') {
 1642:                         my $curroption;
 1643:                         if (ref($settings) eq 'HASH') {
 1644:                             $curroption = $settings->{$type};
 1645:                         }
 1646:                         if (!$curroption) {
 1647:                             $curroption = 'norequest';
 1648:                         }
 1649:                         foreach my $option (@options) {
 1650:                             my $val = $option;
 1651:                             if ($option eq 'norequest') {
 1652:                                 $val = 0;
 1653:                             }
 1654:                             my $checked = '';
 1655:                             if ($option eq $curroption) {
 1656:                                 $checked = ' checked="checked"';
 1657:                             }
 1658:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1659:                                   '<input type="radio" name="authorreq_'.$type.
 1660:                                   '" value="'.$val.'"'.$checked.' />'.
 1661:                                   $titles{$option}.'</label></span>&nbsp; ';
 1662:                         }
 1663:                     } else {
 1664:                         my $checked = 'checked="checked" ';
 1665:                         if (ref($settings) eq 'HASH') {
 1666:                             if (ref($settings->{$item}) eq 'HASH') {
 1667:                                 if ($settings->{$item}->{$type} == 0) {
 1668:                                     $checked = '';
 1669:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1670:                                     $checked =  'checked="checked" ';
 1671:                                 }
 1672:                             }
 1673:                         }
 1674:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1675:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1676:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1677:                                       '</label></span>&nbsp; ';
 1678:                     }
 1679:                 }
 1680:                 if ($context eq 'requestcourses') {
 1681:                     $datatable .= '</tr><tr>';
 1682:                     foreach my $item (@usertools) {
 1683:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1684:                     }
 1685:                     $datatable .= '</tr></table>';
 1686:                 }
 1687:                 $datatable .= '</td>';
 1688:                 unless (($context eq 'requestcourses') ||
 1689:                         ($context eq 'requestauthor')) {
 1690:                     $datatable .= 
 1691:                               '<td class="LC_right_item">'.
 1692:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1693:                               '<input type="text" name="quota_'.$type.
 1694:                               '" value="'.$currdefquota.
 1695:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1696:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1697:                               '<input type="text" name="authorquota_'.$type.
 1698:                               '" value="'.$currauthorquota.
 1699:                               '" size="5" /></span></td>';
 1700:                 }
 1701:                 $datatable .= '</tr>';
 1702:             }
 1703:         }
 1704:     }
 1705:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1706:         $defaultquota = '20';
 1707:         $authorquota = '500';
 1708:         if (ref($settings) eq 'HASH') {
 1709:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1710:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1711:             } elsif (defined($settings->{'default'})) {
 1712:                 $defaultquota = $settings->{'default'};
 1713:             }
 1714:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1715:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1716:             }
 1717:         }
 1718:     }
 1719:     $typecount ++;
 1720:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1721:     $datatable .= '<tr'.$css_class.'>'.
 1722:                   '<td>'.$othertitle.'</td>'.
 1723:                   '<td class="LC_left_item">';
 1724:     if ($context eq 'requestcourses') {
 1725:         $datatable .= '<table><tr>';
 1726:     }
 1727:     my %defcell;
 1728:     foreach my $item (@usertools) {
 1729:         if ($context eq 'requestcourses') {
 1730:             my ($curroption,$currlimit);
 1731:             if (ref($settings) eq 'HASH') {
 1732:                 if (ref($settings->{$item}) eq 'HASH') {
 1733:                     $curroption = $settings->{$item}->{'default'};
 1734:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1735:                         $currlimit = $1;
 1736:                     }
 1737:                 }
 1738:             }
 1739:             if (!$curroption) {
 1740:                 $curroption = 'norequest';
 1741:             }
 1742:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1743:             foreach my $option (@options) {
 1744:                 my $val = $option;
 1745:                 if ($option eq 'norequest') {
 1746:                     $val = 0;
 1747:                 }
 1748:                 if ($option eq 'validate') {
 1749:                     my $canvalidate = 0;
 1750:                     if (ref($validations{$item}) eq 'HASH') {
 1751:                         if ($validations{$item}{'default'}) {
 1752:                             $canvalidate = 1;
 1753:                         }
 1754:                     }
 1755:                     next if (!$canvalidate);
 1756:                 }
 1757:                 my $checked = '';
 1758:                 if ($option eq $curroption) {
 1759:                     $checked = ' checked="checked"';
 1760:                 } elsif ($option eq 'autolimit') {
 1761:                     if ($curroption =~ /^autolimit/) {
 1762:                         $checked = ' checked="checked"';
 1763:                     }
 1764:                 }
 1765:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1766:                                   '<input type="radio" name="crsreq_'.$item.
 1767:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1768:                                   $titles{$option}.'</label>';
 1769:                 if ($option eq 'autolimit') {
 1770:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1771:                                        $item.'_limit_default" size="1" '.
 1772:                                        'value="'.$currlimit.'" />';
 1773:                 }
 1774:                 $defcell{$item} .= '</span> ';
 1775:                 if ($option eq 'autolimit') {
 1776:                     $defcell{$item} .= $titles{'unlimited'};
 1777:                 }
 1778:             }
 1779:         } elsif ($context eq 'requestauthor') {
 1780:             my $curroption;
 1781:             if (ref($settings) eq 'HASH') {
 1782:                 $curroption = $settings->{'default'};
 1783:             }
 1784:             if (!$curroption) {
 1785:                 $curroption = 'norequest';
 1786:             }
 1787:             foreach my $option (@options) {
 1788:                 my $val = $option;
 1789:                 if ($option eq 'norequest') {
 1790:                     $val = 0;
 1791:                 }
 1792:                 my $checked = '';
 1793:                 if ($option eq $curroption) {
 1794:                     $checked = ' checked="checked"';
 1795:                 }
 1796:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1797:                               '<input type="radio" name="authorreq_default"'.
 1798:                               ' value="'.$val.'"'.$checked.' />'.
 1799:                               $titles{$option}.'</label></span>&nbsp; ';
 1800:             }
 1801:         } else {
 1802:             my $checked = 'checked="checked" ';
 1803:             if (ref($settings) eq 'HASH') {
 1804:                 if (ref($settings->{$item}) eq 'HASH') {
 1805:                     if ($settings->{$item}->{'default'} == 0) {
 1806:                         $checked = '';
 1807:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1808:                         $checked = 'checked="checked" ';
 1809:                     }
 1810:                 }
 1811:             }
 1812:             $datatable .= '<span class="LC_nobreak"><label>'.
 1813:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1814:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1815:                           '</label></span>&nbsp; ';
 1816:         }
 1817:     }
 1818:     if ($context eq 'requestcourses') {
 1819:         $datatable .= '</tr><tr>';
 1820:         foreach my $item (@usertools) {
 1821:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1822:         }
 1823:         $datatable .= '</tr></table>';
 1824:     }
 1825:     $datatable .= '</td>';
 1826:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1827:         $datatable .= '<td class="LC_right_item">'.
 1828:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1829:                       '<input type="text" name="defaultquota" value="'.
 1830:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1831:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1832:                       '<input type="text" name="authorquota" value="'.
 1833:                       $authorquota.'" size="5" /></span></td>';
 1834:     }
 1835:     $datatable .= '</tr>';
 1836:     $typecount ++;
 1837:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1838:     $datatable .= '<tr'.$css_class.'>'.
 1839:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 1840:     if ($context eq 'requestcourses') {
 1841:         $datatable .= &mt('(overrides affiliation, if set)').
 1842:                       '</td>'.
 1843:                       '<td class="LC_left_item">'.
 1844:                       '<table><tr>';
 1845:     } else {
 1846:         $datatable .= &mt('(overrides affiliation, if checked)').
 1847:                       '</td>'.
 1848:                       '<td class="LC_left_item" colspan="2">'.
 1849:                       '<br />';
 1850:     }
 1851:     my %advcell;
 1852:     foreach my $item (@usertools) {
 1853:         if ($context eq 'requestcourses') {
 1854:             my ($curroption,$currlimit);
 1855:             if (ref($settings) eq 'HASH') {
 1856:                 if (ref($settings->{$item}) eq 'HASH') {
 1857:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1858:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1859:                         $currlimit = $1;
 1860:                     }
 1861:                 }
 1862:             }
 1863:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1864:             my $checked = '';
 1865:             if ($curroption eq '') {
 1866:                 $checked = ' checked="checked"';
 1867:             }
 1868:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1869:                                '<input type="radio" name="crsreq_'.$item.
 1870:                                '__LC_adv" value=""'.$checked.' />'.
 1871:                                &mt('No override set').'</label></span>&nbsp; ';
 1872:             foreach my $option (@options) {
 1873:                 my $val = $option;
 1874:                 if ($option eq 'norequest') {
 1875:                     $val = 0;
 1876:                 }
 1877:                 if ($option eq 'validate') {
 1878:                     my $canvalidate = 0;
 1879:                     if (ref($validations{$item}) eq 'HASH') {
 1880:                         if ($validations{$item}{'_LC_adv'}) {
 1881:                             $canvalidate = 1;
 1882:                         }
 1883:                     }
 1884:                     next if (!$canvalidate);
 1885:                 }
 1886:                 my $checked = '';
 1887:                 if ($val eq $curroption) {
 1888:                     $checked = ' checked="checked"';
 1889:                 } elsif ($option eq 'autolimit') {
 1890:                     if ($curroption =~ /^autolimit/) {
 1891:                         $checked = ' checked="checked"';
 1892:                     }
 1893:                 }
 1894:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1895:                                   '<input type="radio" name="crsreq_'.$item.
 1896:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1897:                                   $titles{$option}.'</label>';
 1898:                 if ($option eq 'autolimit') {
 1899:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1900:                                        $item.'_limit__LC_adv" size="1" '.
 1901:                                        'value="'.$currlimit.'" />';
 1902:                 }
 1903:                 $advcell{$item} .= '</span> ';
 1904:                 if ($option eq 'autolimit') {
 1905:                     $advcell{$item} .= $titles{'unlimited'};
 1906:                 }
 1907:             }
 1908:         } elsif ($context eq 'requestauthor') {
 1909:             my $curroption;
 1910:             if (ref($settings) eq 'HASH') {
 1911:                 $curroption = $settings->{'_LC_adv'};
 1912:             }
 1913:             my $checked = '';
 1914:             if ($curroption eq '') {
 1915:                 $checked = ' checked="checked"';
 1916:             }
 1917:             $datatable .= '<span class="LC_nobreak"><label>'.
 1918:                           '<input type="radio" name="authorreq__LC_adv"'.
 1919:                           ' value=""'.$checked.' />'.
 1920:                           &mt('No override set').'</label></span>&nbsp; ';
 1921:             foreach my $option (@options) {
 1922:                 my $val = $option;
 1923:                 if ($option eq 'norequest') {
 1924:                     $val = 0;
 1925:                 }
 1926:                 my $checked = '';
 1927:                 if ($val eq $curroption) {
 1928:                     $checked = ' checked="checked"';
 1929:                 }
 1930:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1931:                               '<input type="radio" name="authorreq__LC_adv"'.
 1932:                               ' value="'.$val.'"'.$checked.' />'.
 1933:                               $titles{$option}.'</label></span>&nbsp; ';
 1934:             }
 1935:         } else {
 1936:             my $checked = 'checked="checked" ';
 1937:             if (ref($settings) eq 'HASH') {
 1938:                 if (ref($settings->{$item}) eq 'HASH') {
 1939:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1940:                         $checked = '';
 1941:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1942:                         $checked = 'checked="checked" ';
 1943:                     }
 1944:                 }
 1945:             }
 1946:             $datatable .= '<span class="LC_nobreak"><label>'.
 1947:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1948:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1949:                           '</label></span>&nbsp; ';
 1950:         }
 1951:     }
 1952:     if ($context eq 'requestcourses') {
 1953:         $datatable .= '</tr><tr>';
 1954:         foreach my $item (@usertools) {
 1955:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1956:         }
 1957:         $datatable .= '</tr></table>';
 1958:     }
 1959:     $datatable .= '</td></tr>';
 1960:     $$rowtotal += $typecount;
 1961:     return $datatable;
 1962: }
 1963: 
 1964: sub print_requestmail {
 1965:     my ($dom,$action,$settings,$rowtotal) = @_;
 1966:     my ($now,$datatable,%currapp);
 1967:     $now = time;
 1968:     if (ref($settings) eq 'HASH') {
 1969:         if (ref($settings->{'notify'}) eq 'HASH') {
 1970:             if ($settings->{'notify'}{'approval'} ne '') {
 1971:                map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 1972:             }
 1973:         }
 1974:     }
 1975:     my $numinrow = 2;
 1976:     my $css_class = 'class="LC_odd_row"';
 1977:     my $text;
 1978:     if ($action eq 'requestcourses') {
 1979:         $text = &mt('Receive notification of course requests requiring approval');
 1980:     } else {
 1981:         $text = &mt('Receive notification of authoring space requests requiring approval')
 1982:     }
 1983:     $datatable = '<tr '.$css_class.'>'.
 1984:                  ' <td>'.$text.'</td>'.
 1985:                  ' <td class="LC_left_item">';
 1986:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 1987:                                                  'reqapprovalnotify',%currapp);
 1988:     if ($numdc > 0) {
 1989:         $datatable .= $table;
 1990:     } else {
 1991:         $datatable .= &mt('There are no active Domain Coordinators');
 1992:     }
 1993:     $datatable .='</td></tr>';
 1994:     $$rowtotal += $rows;
 1995:     return $datatable;
 1996: }
 1997: 
 1998: sub print_autoenroll {
 1999:     my ($dom,$settings,$rowtotal) = @_;
 2000:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2001:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 2002:     if (ref($settings) eq 'HASH') {
 2003:         if (exists($settings->{'run'})) {
 2004:             if ($settings->{'run'} eq '0') {
 2005:                 $runoff = ' checked="checked" ';
 2006:                 $runon = ' ';
 2007:             } else {
 2008:                 $runon = ' checked="checked" ';
 2009:                 $runoff = ' ';
 2010:             }
 2011:         } else {
 2012:             if ($autorun) {
 2013:                 $runon = ' checked="checked" ';
 2014:                 $runoff = ' ';
 2015:             } else {
 2016:                 $runoff = ' checked="checked" ';
 2017:                 $runon = ' ';
 2018:             }
 2019:         }
 2020:         if (exists($settings->{'co-owners'})) {
 2021:             if ($settings->{'co-owners'} eq '0') {
 2022:                 $coownersoff = ' checked="checked" ';
 2023:                 $coownerson = ' ';
 2024:             } else {
 2025:                 $coownerson = ' checked="checked" ';
 2026:                 $coownersoff = ' ';
 2027:             }
 2028:         } else {
 2029:             $coownersoff = ' checked="checked" ';
 2030:             $coownerson = ' ';
 2031:         }
 2032:         if (exists($settings->{'sender_domain'})) {
 2033:             $defdom = $settings->{'sender_domain'};
 2034:         }
 2035:     } else {
 2036:         if ($autorun) {
 2037:             $runon = ' checked="checked" ';
 2038:             $runoff = ' ';
 2039:         } else {
 2040:             $runoff = ' checked="checked" ';
 2041:             $runon = ' ';
 2042:         }
 2043:     }
 2044:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2045:     my $notif_sender;
 2046:     if (ref($settings) eq 'HASH') {
 2047:         $notif_sender = $settings->{'sender_uname'};
 2048:     }
 2049:     my $datatable='<tr class="LC_odd_row">'.
 2050:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2051:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2052:                   '<input type="radio" name="autoenroll_run"'.
 2053:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2054:                   '<label><input type="radio" name="autoenroll_run"'.
 2055:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2056:                   '</tr><tr>'.
 2057:                   '<td>'.&mt('Notification messages - sender').
 2058:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2059:                   &mt('username').':&nbsp;'.
 2060:                   '<input type="text" name="sender_uname" value="'.
 2061:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2062:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2063:                   '<tr class="LC_odd_row">'.
 2064:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2065:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2066:                   '<input type="radio" name="autoassign_coowners"'.
 2067:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2068:                   '<label><input type="radio" name="autoassign_coowners"'.
 2069:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2070:                   '</tr>';
 2071:     $$rowtotal += 3;
 2072:     return $datatable;
 2073: }
 2074: 
 2075: sub print_autoupdate {
 2076:     my ($position,$dom,$settings,$rowtotal) = @_;
 2077:     my $datatable;
 2078:     if ($position eq 'top') {
 2079:         my $updateon = ' ';
 2080:         my $updateoff = ' checked="checked" ';
 2081:         my $classlistson = ' ';
 2082:         my $classlistsoff = ' checked="checked" ';
 2083:         if (ref($settings) eq 'HASH') {
 2084:             if ($settings->{'run'} eq '1') {
 2085:                 $updateon = $updateoff;
 2086:                 $updateoff = ' ';
 2087:             }
 2088:             if ($settings->{'classlists'} eq '1') {
 2089:                 $classlistson = $classlistsoff;
 2090:                 $classlistsoff = ' ';
 2091:             }
 2092:         }
 2093:         my %title = (
 2094:                    run => 'Auto-update active?',
 2095:                    classlists => 'Update information in classlists?',
 2096:                     );
 2097:         $datatable = '<tr class="LC_odd_row">'. 
 2098:                   '<td>'.&mt($title{'run'}).'</td>'.
 2099:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2100:                   '<input type="radio" name="autoupdate_run"'.
 2101:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2102:                   '<label><input type="radio" name="autoupdate_run"'.
 2103:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2104:                   '</tr><tr>'.
 2105:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2106:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2107:                   '<label><input type="radio" name="classlists"'.
 2108:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2109:                   '<label><input type="radio" name="classlists"'.
 2110:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2111:                   '</tr>';
 2112:         $$rowtotal += 2;
 2113:     } elsif ($position eq 'middle') {
 2114:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2115:         my $numinrow = 3;
 2116:         my $locknamesettings;
 2117:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2118:                                      $dom,$numinrow,$othertitle,
 2119:                                     'lockablenames');
 2120:         $$rowtotal ++;
 2121:     } else {
 2122:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2123:         my @fields = ('lastname','firstname','middlename','generation',
 2124:                       'permanentemail','id');
 2125:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2126:         my $numrows = 0;
 2127:         if (ref($types) eq 'ARRAY') {
 2128:             if (@{$types} > 0) {
 2129:                 $datatable = 
 2130:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2131:                                          \@fields,$types,\$numrows);
 2132:                     $$rowtotal += @{$types}; 
 2133:             }
 2134:         }
 2135:         $datatable .= 
 2136:             &usertype_update_row($settings,{'default' => $othertitle},
 2137:                                  \%fieldtitles,\@fields,['default'],
 2138:                                  \$numrows);
 2139:         $$rowtotal ++;     
 2140:     }
 2141:     return $datatable;
 2142: }
 2143: 
 2144: sub print_autocreate {
 2145:     my ($dom,$settings,$rowtotal) = @_;
 2146:     my (%createon,%createoff,%currhash);
 2147:     my @types = ('xml','req');
 2148:     if (ref($settings) eq 'HASH') {
 2149:         foreach my $item (@types) {
 2150:             $createoff{$item} = ' checked="checked" ';
 2151:             $createon{$item} = ' ';
 2152:             if (exists($settings->{$item})) {
 2153:                 if ($settings->{$item}) {
 2154:                     $createon{$item} = ' checked="checked" ';
 2155:                     $createoff{$item} = ' ';
 2156:                 }
 2157:             }
 2158:         }
 2159:         if ($settings->{'xmldc'} ne '') { 
 2160:             $currhash{$settings->{'xmldc'}} = 1;
 2161:         }
 2162:     } else {
 2163:         foreach my $item (@types) {
 2164:             $createoff{$item} = ' checked="checked" ';
 2165:             $createon{$item} = ' ';
 2166:         }
 2167:     }
 2168:     $$rowtotal += 2;
 2169:     my $numinrow = 2;
 2170:     my $datatable='<tr class="LC_odd_row">'.
 2171:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2172:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2173:                   '<input type="radio" name="autocreate_xml"'.
 2174:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2175:                   '<label><input type="radio" name="autocreate_xml"'.
 2176:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2177:                   '</td></tr><tr>'.
 2178:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2179:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2180:                   '<input type="radio" name="autocreate_req"'.
 2181:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2182:                   '<label><input type="radio" name="autocreate_req"'.
 2183:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2184:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2185:                                                    'autocreate_xmldc',%currhash);
 2186:     if ($numdc > 1) {
 2187:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2188:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2189:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2190:     } else {
 2191:         $datatable .= $dctable.'</td></tr>';
 2192:     }
 2193:     $$rowtotal += $rows;
 2194:     return $datatable;
 2195: }
 2196: 
 2197: sub print_directorysrch {
 2198:     my ($dom,$settings,$rowtotal) = @_;
 2199:     my $srchon = ' ';
 2200:     my $srchoff = ' checked="checked" ';
 2201:     my ($exacton,$containson,$beginson);
 2202:     my $localon = ' ';
 2203:     my $localoff = ' checked="checked" ';
 2204:     if (ref($settings) eq 'HASH') {
 2205:         if ($settings->{'available'} eq '1') {
 2206:             $srchon = $srchoff;
 2207:             $srchoff = ' ';
 2208:         }
 2209:         if ($settings->{'localonly'} eq '1') {
 2210:             $localon = $localoff;
 2211:             $localoff = ' ';
 2212:         }
 2213:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2214:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2215:                 if ($type eq 'exact') {
 2216:                     $exacton = ' checked="checked" ';
 2217:                 } elsif ($type eq 'contains') {
 2218:                     $containson = ' checked="checked" ';
 2219:                 } elsif ($type eq 'begins') {
 2220:                     $beginson = ' checked="checked" ';
 2221:                 }
 2222:             }
 2223:         } else {
 2224:             if ($settings->{'searchtypes'} eq 'exact') {
 2225:                 $exacton = ' checked="checked" ';
 2226:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2227:                 $containson = ' checked="checked" ';
 2228:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2229:                 $exacton = ' checked="checked" ';
 2230:                 $containson = ' checked="checked" ';
 2231:             }
 2232:         }
 2233:     }
 2234:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2235:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2236: 
 2237:     my $numinrow = 4;
 2238:     my $cansrchrow = 0;
 2239:     my $datatable='<tr class="LC_odd_row">'.
 2240:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2241:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2242:                   '<input type="radio" name="dirsrch_available"'.
 2243:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2244:                   '<label><input type="radio" name="dirsrch_available"'.
 2245:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2246:                   '</tr><tr>'.
 2247:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2248:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2249:                   '<input type="radio" name="dirsrch_localonly"'.
 2250:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2251:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2252:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2253:                   '</tr>';
 2254:     $$rowtotal += 2;
 2255:     if (ref($usertypes) eq 'HASH') {
 2256:         if (keys(%{$usertypes}) > 0) {
 2257:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2258:                                          $numinrow,$othertitle,'cansearch');
 2259:             $cansrchrow = 1;
 2260:         }
 2261:     }
 2262:     if ($cansrchrow) {
 2263:         $$rowtotal ++;
 2264:         $datatable .= '<tr>';
 2265:     } else {
 2266:         $datatable .= '<tr class="LC_odd_row">';
 2267:     }
 2268:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2269:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2270:     foreach my $title (@{$titleorder}) {
 2271:         if (defined($searchtitles->{$title})) {
 2272:             my $check = ' ';
 2273:             if (ref($settings) eq 'HASH') {
 2274:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2275:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2276:                         $check = ' checked="checked" ';
 2277:                     }
 2278:                 }
 2279:             }
 2280:             $datatable .= '<td class="LC_left_item">'.
 2281:                           '<span class="LC_nobreak"><label>'.
 2282:                           '<input type="checkbox" name="searchby" '.
 2283:                           'value="'.$title.'"'.$check.'/>'.
 2284:                           $searchtitles->{$title}.'</label></span></td>';
 2285:         }
 2286:     }
 2287:     $datatable .= '</tr></table></td></tr>';
 2288:     $$rowtotal ++;
 2289:     if ($cansrchrow) {
 2290:         $datatable .= '<tr class="LC_odd_row">';
 2291:     } else {
 2292:         $datatable .= '<tr>';
 2293:     }
 2294:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2295:                   '<td class="LC_left_item" colspan="2">'.
 2296:                   '<span class="LC_nobreak"><label>'.
 2297:                   '<input type="checkbox" name="searchtypes" '.
 2298:                   $exacton.' value="exact" />'.&mt('Exact match').
 2299:                   '</label>&nbsp;'.
 2300:                   '<label><input type="checkbox" name="searchtypes" '.
 2301:                   $beginson.' value="begins" />'.&mt('Begins with').
 2302:                   '</label>&nbsp;'.
 2303:                   '<label><input type="checkbox" name="searchtypes" '.
 2304:                   $containson.' value="contains" />'.&mt('Contains').
 2305:                   '</label></span></td></tr>';
 2306:     $$rowtotal ++;
 2307:     return $datatable;
 2308: }
 2309: 
 2310: sub print_contacts {
 2311:     my ($dom,$settings,$rowtotal) = @_;
 2312:     my $datatable;
 2313:     my @contacts = ('adminemail','supportemail');
 2314:     my (%checked,%to,%otheremails,%bccemails);
 2315:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2316:                     'requestsmail','updatesmail','idconflictsmail');
 2317:     foreach my $type (@mailings) {
 2318:         $otheremails{$type} = '';
 2319:     }
 2320:     $bccemails{'helpdeskmail'} = '';
 2321:     if (ref($settings) eq 'HASH') {
 2322:         foreach my $item (@contacts) {
 2323:             if (exists($settings->{$item})) {
 2324:                 $to{$item} = $settings->{$item};
 2325:             }
 2326:         }
 2327:         foreach my $type (@mailings) {
 2328:             if (exists($settings->{$type})) {
 2329:                 if (ref($settings->{$type}) eq 'HASH') {
 2330:                     foreach my $item (@contacts) {
 2331:                         if ($settings->{$type}{$item}) {
 2332:                             $checked{$type}{$item} = ' checked="checked" ';
 2333:                         }
 2334:                     }
 2335:                     $otheremails{$type} = $settings->{$type}{'others'};
 2336:                     if ($type eq 'helpdeskmail') {
 2337:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2338:                     }
 2339:                 }
 2340:             } elsif ($type eq 'lonstatusmail') {
 2341:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2342:             }
 2343:         }
 2344:     } else {
 2345:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2346:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2347:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2348:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2349:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2350:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2351:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2352:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2353:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2354:     }
 2355:     my ($titles,$short_titles) = &contact_titles();
 2356:     my $rownum = 0;
 2357:     my $css_class;
 2358:     foreach my $item (@contacts) {
 2359:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2360:         $datatable .= '<tr'.$css_class.'>'. 
 2361:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2362:                   '</span></td><td class="LC_right_item">'.
 2363:                   '<input type="text" name="'.$item.'" value="'.
 2364:                   $to{$item}.'" /></td></tr>';
 2365:         $rownum ++;
 2366:     }
 2367:     foreach my $type (@mailings) {
 2368:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2369:         $datatable .= '<tr'.$css_class.'>'.
 2370:                       '<td><span class="LC_nobreak">'.
 2371:                       $titles->{$type}.': </span></td>'.
 2372:                       '<td class="LC_left_item">'.
 2373:                       '<span class="LC_nobreak">';
 2374:         foreach my $item (@contacts) {
 2375:             $datatable .= '<label>'.
 2376:                           '<input type="checkbox" name="'.$type.'"'.
 2377:                           $checked{$type}{$item}.
 2378:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2379:                           '</label>&nbsp;';
 2380:         }
 2381:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2382:                       '<input type="text" name="'.$type.'_others" '.
 2383:                       'value="'.$otheremails{$type}.'"  />';
 2384:         if ($type eq 'helpdeskmail') {
 2385:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2386:                           '<input type="text" name="'.$type.'_bcc" '.
 2387:                           'value="'.$bccemails{$type}.'"  />';
 2388:         }
 2389:         $datatable .= '</td></tr>'."\n";
 2390:         $rownum ++;
 2391:     }
 2392:     my %choices;
 2393:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2394:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2395:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2396:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2397:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2398:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2399:     my @toggles = ('reporterrors','reportupdates');
 2400:     my %defaultchecked = ('reporterrors'  => 'on',
 2401:                           'reportupdates' => 'on');
 2402:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2403:                                                \%choices,$rownum);
 2404:     $datatable .= $reports;
 2405:     $$rowtotal += $rownum;
 2406:     return $datatable;
 2407: }
 2408: 
 2409: sub print_helpsettings {
 2410:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2411:     my ($datatable,$itemcount);
 2412:     $itemcount = 1;
 2413:     my (%choices,%defaultchecked,@toggles);
 2414:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2415:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2416:                                  &mt('LON-CAPA bug tracker'),600,500));
 2417:     %defaultchecked = ('submitbugs' => 'on');
 2418:     @toggles = ('submitbugs',);
 2419: 
 2420:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2421:                                                  \%choices,$itemcount);
 2422:     return $datatable;
 2423: }
 2424: 
 2425: sub radiobutton_prefs {
 2426:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2427:         $additional) = @_;
 2428:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2429:                    (ref($choices) eq 'HASH'));
 2430: 
 2431:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2432: 
 2433:     foreach my $item (@{$toggles}) {
 2434:         if ($defaultchecked->{$item} eq 'on') {
 2435:             $checkedon{$item} = ' checked="checked" ';
 2436:             $checkedoff{$item} = ' ';
 2437:         } elsif ($defaultchecked->{$item} eq 'off') {
 2438:             $checkedoff{$item} = ' checked="checked" ';
 2439:             $checkedon{$item} = ' ';
 2440:         }
 2441:     }
 2442:     if (ref($settings) eq 'HASH') {
 2443:         foreach my $item (@{$toggles}) {
 2444:             if ($settings->{$item} eq '1') {
 2445:                 $checkedon{$item} =  ' checked="checked" ';
 2446:                 $checkedoff{$item} = ' ';
 2447:             } elsif ($settings->{$item} eq '0') {
 2448:                 $checkedoff{$item} =  ' checked="checked" ';
 2449:                 $checkedon{$item} = ' ';
 2450:             }
 2451:         }
 2452:     }
 2453:     if ($onclick) {
 2454:         $onclick = ' onclick="'.$onclick.'"';
 2455:     }
 2456:     foreach my $item (@{$toggles}) {
 2457:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2458:         $datatable .=
 2459:             '<tr'.$css_class.'><td valign="top">'.
 2460:             '<span class="LC_nobreak">'.$choices->{$item}.
 2461:             '</span></td>'.
 2462:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2463:             '<label><input type="radio" name="'.
 2464:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2465:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2466:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2467:             '</span>'.$additional.
 2468:             '</td>'.
 2469:             '</tr>';
 2470:         $itemcount ++;
 2471:     }
 2472:     return ($datatable,$itemcount);
 2473: }
 2474: 
 2475: sub print_coursedefaults {
 2476:     my ($position,$dom,$settings,$rowtotal) = @_;
 2477:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2478:     my $itemcount = 1;
 2479:     my %choices =  &Apache::lonlocal::texthash (
 2480:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2481:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2482:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2483:         coursecredits        => 'Credits can be specified for courses',
 2484:     );
 2485:     my %staticdefaults = (
 2486:                            anonsurvey_threshold => 10,
 2487:                            uploadquota          => 500,
 2488:                          );
 2489:     if ($position eq 'top') {
 2490:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2491:         @toggles = ('canuse_pdfforms');
 2492:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2493:                                                  \%choices,$itemcount);
 2494:     } else {
 2495:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2496:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits,%curruploadquota);
 2497:         my $currusecredits = 0;
 2498:         my @types = ('official','unofficial','community');
 2499:         if (ref($settings) eq 'HASH') {
 2500:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2501:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2502:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 2503:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 2504:                 }
 2505:             }
 2506:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2507:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2508:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2509:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '')) {
 2510:                     $currusecredits = 1;
 2511:                 }
 2512:             }
 2513:         }
 2514:         if (!$currdefresponder) {
 2515:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 2516:         } elsif ($currdefresponder < 1) {
 2517:             $currdefresponder = 1;
 2518:         }
 2519:         foreach my $type (@types) {
 2520:             if ($curruploadquota{$type} eq '') {
 2521:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 2522:             }
 2523:         }
 2524:         $datatable .=
 2525:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2526:                 $choices{'anonsurvey_threshold'}.
 2527:                 '</span></td>'.
 2528:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2529:                 '<input type="text" name="anonsurvey_threshold"'.
 2530:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2531:                 '</td></tr>'."\n".
 2532:                 '<tr><td><span class="LC_nobreak">'.
 2533:                 $choices{'uploadquota'}.
 2534:                 '</span></td>'.
 2535:                 '<td align="right" class="LC_right_item">'.
 2536:                 '<table><tr>';
 2537:         foreach my $type (@types) {
 2538:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 2539:                            '<input type="text" name="uploadquota_'.$type.'"'.
 2540:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 2541:         }
 2542:         $datatable .= '</tr></table></td></tr>'."\n";
 2543:         $itemcount += 2;
 2544:         my $onclick = 'toggleCredits(this.form);';
 2545:         my $display = 'none';  
 2546:         if ($currusecredits) {
 2547:             $display = 'block';
 2548:         }
 2549:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2550:                          '<span class="LC_nobreak">'.
 2551:                          &mt('Default credits for official courses [_1]',
 2552:                          '<input type="text" name="official_credits" value="'.
 2553:                          $def_official_credits.'" size="3" />').
 2554:                          '</span><br />'.
 2555:                          '<span class="LC_nobreak">'.
 2556:                          &mt('Default credits for unofficial courses [_1]',
 2557:                          '<input type="text" name="unofficial_credits" value="'.
 2558:                          $def_unofficial_credits.'" size="3" />').
 2559:                          '</span></div>'."\n";
 2560:         %defaultchecked = ('coursecredits' => 'off');
 2561:         @toggles = ('coursecredits');
 2562:         my $current = {
 2563:                         'coursecredits' => $currusecredits,
 2564:                       };
 2565:         (my $table,$itemcount) =
 2566:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2567:                                \%choices,$itemcount,$onclick,$additional);
 2568:         $datatable .= $table;
 2569:     }
 2570:     $$rowtotal += $itemcount;
 2571:     return $datatable;
 2572: }
 2573: 
 2574: sub print_usersessions {
 2575:     my ($position,$dom,$settings,$rowtotal) = @_;
 2576:     my ($css_class,$datatable,%checked,%choices);
 2577:     my (%by_ip,%by_location,@intdoms);
 2578:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 2579: 
 2580:     my @alldoms = &Apache::lonnet::all_domains();
 2581:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 2582:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2583:     my %altids = &id_for_thisdom(%servers);
 2584:     my $itemcount = 1;
 2585:     if ($position eq 'top') {
 2586:         if (keys(%serverhomes) > 1) {
 2587:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 2588:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 2589:         } else {
 2590:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2591:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 2592:         }
 2593:     } else {
 2594:         if (keys(%by_location) == 0) {
 2595:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2596:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 2597:         } else {
 2598:             my %lt = &usersession_titles();
 2599:             my $numinrow = 5;
 2600:             my $prefix;
 2601:             my @types;
 2602:             if ($position eq 'bottom') {
 2603:                 $prefix = 'remote';
 2604:                 @types = ('version','excludedomain','includedomain');
 2605:             } else {
 2606:                 $prefix = 'hosted';
 2607:                 @types = ('excludedomain','includedomain');
 2608:             }
 2609:             my (%current,%checkedon,%checkedoff);
 2610:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 2611:             my @locations = sort(keys(%by_location));
 2612:             foreach my $type (@types) {
 2613:                 $checkedon{$type} = '';
 2614:                 $checkedoff{$type} = ' checked="checked"';
 2615:             }
 2616:             if (ref($settings) eq 'HASH') {
 2617:                 if (ref($settings->{$prefix}) eq 'HASH') {
 2618:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 2619:                         $current{$key} = $settings->{$prefix}{$key};
 2620:                         if ($key eq 'version') {
 2621:                             if ($current{$key} ne '') {
 2622:                                 $checkedon{$key} = ' checked="checked"';
 2623:                                 $checkedoff{$key} = '';
 2624:                             }
 2625:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 2626:                             $checkedon{$key} = ' checked="checked"';
 2627:                             $checkedoff{$key} = '';
 2628:                         }
 2629:                     }
 2630:                 }
 2631:             }
 2632:             foreach my $type (@types) {
 2633:                 next if ($type ne 'version' && !@locations);
 2634:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2635:                 $datatable .= '<tr'.$css_class.'>
 2636:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 2637:                                <span class="LC_nobreak">&nbsp;
 2638:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 2639:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 2640:                 if ($type eq 'version') {
 2641:                     my $selector = '<select name="'.$prefix.'_version">';
 2642:                     foreach my $version (@lcversions) {
 2643:                         my $selected = '';
 2644:                         if ($current{'version'} eq $version) {
 2645:                             $selected = ' selected="selected"';
 2646:                         }
 2647:                         $selector .= ' <option value="'.$version.'"'.
 2648:                                      $selected.'>'.$version.'</option>';
 2649:                     }
 2650:                     $selector .= '</select> ';
 2651:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 2652:                 } else {
 2653:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 2654:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 2655:                                  ' />'.('&nbsp;'x2).
 2656:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 2657:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 2658:                                  "\n".
 2659:                                  '</div><div><table>';
 2660:                     my $rem;
 2661:                     for (my $i=0; $i<@locations; $i++) {
 2662:                         my ($showloc,$value,$checkedtype);
 2663:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 2664:                             my $ip = $by_location{$locations[$i]}->[0];
 2665:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 2666:                                  $value = join(':',@{$by_ip{$ip}});
 2667:                                 $showloc = join(', ',@{$by_ip{$ip}});
 2668:                                 if (ref($current{$type}) eq 'ARRAY') {
 2669:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 2670:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 2671:                                             $checkedtype = ' checked="checked"';
 2672:                                             last;
 2673:                                         }
 2674:                                     }
 2675:                                 }
 2676:                             }
 2677:                         }
 2678:                         $rem = $i%($numinrow);
 2679:                         if ($rem == 0) {
 2680:                             if ($i > 0) {
 2681:                                 $datatable .= '</tr>';
 2682:                             }
 2683:                             $datatable .= '<tr>';
 2684:                         }
 2685:                         $datatable .= '<td class="LC_left_item">'.
 2686:                                       '<span class="LC_nobreak"><label>'.
 2687:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 2688:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 2689:                                       '</label></span></td>';
 2690:                     }
 2691:                     $rem = @locations%($numinrow);
 2692:                     my $colsleft = $numinrow - $rem;
 2693:                     if ($colsleft > 1 ) {
 2694:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2695:                                       '&nbsp;</td>';
 2696:                     } elsif ($colsleft == 1) {
 2697:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 2698:                     }
 2699:                     $datatable .= '</tr></table>';
 2700:                 }
 2701:                 $datatable .= '</td></tr>';
 2702:                 $itemcount ++;
 2703:             }
 2704:         }
 2705:     }
 2706:     $$rowtotal += $itemcount;
 2707:     return $datatable;
 2708: }
 2709: 
 2710: sub build_location_hashes {
 2711:     my ($intdoms,$by_ip,$by_location) = @_;
 2712:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 2713:                   (ref($by_location) eq 'HASH')); 
 2714:     my %iphost = &Apache::lonnet::get_iphost();
 2715:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2716:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 2717:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 2718:         foreach my $id (@{$iphost{$primary_ip}}) {
 2719:             my $intdom = &Apache::lonnet::internet_dom($id);
 2720:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 2721:                 push(@{$intdoms},$intdom);
 2722:             }
 2723:         }
 2724:     }
 2725:     foreach my $ip (keys(%iphost)) {
 2726:         if (ref($iphost{$ip}) eq 'ARRAY') {
 2727:             foreach my $id (@{$iphost{$ip}}) {
 2728:                 my $location = &Apache::lonnet::internet_dom($id);
 2729:                 if ($location) {
 2730:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 2731:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2732:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 2733:                             push(@{$by_ip->{$ip}},$location);
 2734:                         }
 2735:                     } else {
 2736:                         $by_ip->{$ip} = [$location];
 2737:                     }
 2738:                 }
 2739:             }
 2740:         }
 2741:     }
 2742:     foreach my $ip (sort(keys(%{$by_ip}))) {
 2743:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2744:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 2745:             my $first = $by_ip->{$ip}->[0];
 2746:             if (ref($by_location->{$first}) eq 'ARRAY') {
 2747:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 2748:                     push(@{$by_location->{$first}},$ip);
 2749:                 }
 2750:             } else {
 2751:                 $by_location->{$first} = [$ip];
 2752:             }
 2753:         }
 2754:     }
 2755:     return;
 2756: }
 2757: 
 2758: sub current_offloads_to {
 2759:     my ($dom,$settings,$servers) = @_;
 2760:     my (%spareid,%otherdomconfigs);
 2761:     if (ref($servers) eq 'HASH') {
 2762:         foreach my $lonhost (sort(keys(%{$servers}))) {
 2763:             my $gotspares;
 2764:             if (ref($settings) eq 'HASH') {
 2765:                 if (ref($settings->{'spares'}) eq 'HASH') {
 2766:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 2767:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 2768:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 2769:                         $gotspares = 1;
 2770:                     }
 2771:                 }
 2772:             }
 2773:             unless ($gotspares) {
 2774:                 my $gotspares;
 2775:                 my $serverhomeID =
 2776:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 2777:                 my $serverhomedom =
 2778:                     &Apache::lonnet::host_domain($serverhomeID);
 2779:                 if ($serverhomedom ne $dom) {
 2780:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 2781:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2782:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2783:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2784:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2785:                                 $gotspares = 1;
 2786:                             }
 2787:                         }
 2788:                     } else {
 2789:                         $otherdomconfigs{$serverhomedom} =
 2790:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 2791:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 2792:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 2793:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 2794:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 2795:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 2796:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 2797:                                         $gotspares = 1;
 2798:                                     }
 2799:                                 }
 2800:                             }
 2801:                         }
 2802:                     }
 2803:                 }
 2804:             }
 2805:             unless ($gotspares) {
 2806:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2807:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2808:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2809:                } else {
 2810:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 2811:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 2812:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 2813:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 2814:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 2815:                     } else {
 2816:                         my %what = (
 2817:                              spareid => 1,
 2818:                         );
 2819:                         my ($result,$returnhash) = 
 2820:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 2821:                         if ($result eq 'ok') { 
 2822:                             if (ref($returnhash) eq 'HASH') {
 2823:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 2824:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 2825:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 2826:                                 }
 2827:                             }
 2828:                         }
 2829:                     }
 2830:                 }
 2831:             }
 2832:         }
 2833:     }
 2834:     return %spareid;
 2835: }
 2836: 
 2837: sub spares_row {
 2838:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 2839:     my $css_class;
 2840:     my $numinrow = 4;
 2841:     my $itemcount = 1;
 2842:     my $datatable;
 2843:     my %typetitles = &sparestype_titles();
 2844:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2845:         foreach my $server (sort(keys(%{$servers}))) {
 2846:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 2847:             my ($othercontrol,$serverdom);
 2848:             if ($serverhome ne $server) {
 2849:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 2850:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2851:             } else {
 2852:                 $serverdom = &Apache::lonnet::host_domain($server);
 2853:                 if ($serverdom ne $dom) {
 2854:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 2855:                 }
 2856:             }
 2857:             next unless (ref($spareid->{$server}) eq 'HASH');
 2858:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2859:             $datatable .= '<tr'.$css_class.'>
 2860:                            <td rowspan="2">
 2861:                             <span class="LC_nobreak">'.
 2862:                           &mt('[_1] when busy, offloads to:'
 2863:                               ,'<b>'.$server.'</b>').
 2864:                           "\n";
 2865:             my (%current,%canselect);
 2866:             my @choices = 
 2867:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 2868:             foreach my $type ('primary','default') {
 2869:                 if (ref($spareid->{$server}) eq 'HASH') {
 2870:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 2871:                         my @spares = @{$spareid->{$server}{$type}};
 2872:                         if (@spares > 0) {
 2873:                             if ($othercontrol) {
 2874:                                 $current{$type} = join(', ',@spares);
 2875:                             } else {
 2876:                                 $current{$type} .= '<table>';
 2877:                                 my $numspares = scalar(@spares);
 2878:                                 for (my $i=0;  $i<@spares; $i++) {
 2879:                                     my $rem = $i%($numinrow);
 2880:                                     if ($rem == 0) {
 2881:                                         if ($i > 0) {
 2882:                                             $current{$type} .= '</tr>';
 2883:                                         }
 2884:                                         $current{$type} .= '<tr>';
 2885:                                     }
 2886:                                     $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;'.
 2887:                                                        $spareid->{$server}{$type}[$i].
 2888:                                                        '</label></td>'."\n";
 2889:                                 }
 2890:                                 my $rem = @spares%($numinrow);
 2891:                                 my $colsleft = $numinrow - $rem;
 2892:                                 if ($colsleft > 1 ) {
 2893:                                     $current{$type} .= '<td colspan="'.$colsleft.
 2894:                                                        '" class="LC_left_item">'.
 2895:                                                        '&nbsp;</td>';
 2896:                                 } elsif ($colsleft == 1) {
 2897:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 2898:                                 }
 2899:                                 $current{$type} .= '</tr></table>';
 2900:                             }
 2901:                         }
 2902:                     }
 2903:                     if ($current{$type} eq '') {
 2904:                         $current{$type} = &mt('None specified');
 2905:                     }
 2906:                     if ($othercontrol) {
 2907:                         if ($type eq 'primary') {
 2908:                             $canselect{$type} = $othercontrol;
 2909:                         }
 2910:                     } else {
 2911:                         $canselect{$type} = 
 2912:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 2913:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 2914:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 2915:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 2916:                         if (@choices > 0) {
 2917:                             foreach my $lonhost (@choices) {
 2918:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 2919:                             }
 2920:                         }
 2921:                         $canselect{$type} .= '</select>'."\n";
 2922:                     }
 2923:                 } else {
 2924:                     $current{$type} = &mt('Could not be determined');
 2925:                     if ($type eq 'primary') {
 2926:                         $canselect{$type} =  $othercontrol;
 2927:                     }
 2928:                 }
 2929:                 if ($type eq 'default') {
 2930:                     $datatable .= '<tr'.$css_class.'>';
 2931:                 }
 2932:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 2933:                               '<td>'.$current{$type}.'</td>'."\n".
 2934:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 2935:             }
 2936:             $itemcount ++;
 2937:         }
 2938:     }
 2939:     $$rowtotal += $itemcount;
 2940:     return $datatable;
 2941: }
 2942: 
 2943: sub possible_newspares {
 2944:     my ($server,$currspares,$serverhomes,$altids) = @_;
 2945:     my $serverhostname = &Apache::lonnet::hostname($server);
 2946:     my %excluded;
 2947:     if ($serverhostname ne '') {
 2948:         %excluded = (
 2949:                        $serverhostname => 1,
 2950:                     );
 2951:     }
 2952:     if (ref($currspares) eq 'HASH') {
 2953:         foreach my $type (keys(%{$currspares})) {
 2954:             if (ref($currspares->{$type}) eq 'ARRAY') {
 2955:                 if (@{$currspares->{$type}} > 0) {
 2956:                     foreach my $curr (@{$currspares->{$type}}) {
 2957:                         my $hostname = &Apache::lonnet::hostname($curr);
 2958:                         $excluded{$hostname} = 1;
 2959:                     }
 2960:                 }
 2961:             }
 2962:         }
 2963:     }
 2964:     my @choices;
 2965:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 2966:         if (keys(%{$serverhomes}) > 1) {
 2967:             foreach my $name (sort(keys(%{$serverhomes}))) {
 2968:                 unless ($excluded{$name}) {
 2969:                     if (exists($altids->{$serverhomes->{$name}})) {
 2970:                         push(@choices,$altids->{$serverhomes->{$name}});
 2971:                     } else {
 2972:                         push(@choices,$serverhomes->{$name});
 2973:                     }
 2974:                 }
 2975:             }
 2976:         }
 2977:     }
 2978:     return sort(@choices);
 2979: }
 2980: 
 2981: sub print_loadbalancing {
 2982:     my ($dom,$settings,$rowtotal) = @_;
 2983:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 2984:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 2985:     my $numinrow = 1;
 2986:     my $datatable;
 2987:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2988:     my (%currbalancer,%currtargets,%currrules,%existing);
 2989:     if (ref($settings) eq 'HASH') {
 2990:         %existing = %{$settings};
 2991:     }
 2992:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 2993:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 2994:                                   \%currtargets,\%currrules);
 2995:     } else {
 2996:         return;
 2997:     }
 2998:     my ($othertitle,$usertypes,$types) =
 2999:         &Apache::loncommon::sorted_inst_types($dom);
 3000:     my $rownum = 8;
 3001:     if (ref($types) eq 'ARRAY') {
 3002:         $rownum += scalar(@{$types});
 3003:     }
 3004:     my @css_class = ('LC_odd_row','LC_even_row');
 3005:     my $balnum = 0;
 3006:     my $islast;
 3007:     my (@toshow,$disabledtext);
 3008:     if (keys(%currbalancer) > 0) {
 3009:         @toshow = sort(keys(%currbalancer));
 3010:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3011:             push(@toshow,'');
 3012:         }
 3013:     } else {
 3014:         @toshow = ('');
 3015:         $disabledtext = &mt('No existing load balancer');
 3016:     }
 3017:     foreach my $lonhost (@toshow) {
 3018:         if ($balnum == scalar(@toshow)-1) {
 3019:             $islast = 1;
 3020:         } else {
 3021:             $islast = 0;
 3022:         }
 3023:         my $cssidx = $balnum%2;
 3024:         my $targets_div_style = 'display: none';
 3025:         my $disabled_div_style = 'display: block';
 3026:         my $homedom_div_style = 'display: none';
 3027:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3028:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3029:                       '<p>';
 3030:         if ($lonhost eq '') {
 3031:             $datatable .= '<span class="LC_nobreak">'; 
 3032:             if (keys(%currbalancer) > 0) {
 3033:                 $datatable .= &mt('Add balancer:');
 3034:             } else {
 3035:                 $datatable .= &mt('Enable balancer:');
 3036:             }
 3037:             $datatable .= '&nbsp;'.
 3038:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3039:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3040:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3041:                           '<option value="" selected="selected">'.&mt('None').
 3042:                           '</option>'."\n";
 3043:             foreach my $server (sort(keys(%servers))) {
 3044:                 next if ($currbalancer{$server});
 3045:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3046:             }
 3047:             $datatable .= 
 3048:                 '</select>'."\n".
 3049:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3050:         } else {
 3051:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3052:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3053:                            &mt('Stop balancing').'</label>'.
 3054:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3055:             $targets_div_style = 'display: block';
 3056:             $disabled_div_style = 'display: none';
 3057:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3058:                 $homedom_div_style = 'display: block';
 3059:             }
 3060:         }
 3061:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3062:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3063:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3064:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3065:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3066:         my @sparestypes = ('primary','default');
 3067:         my %typetitles = &sparestype_titles();
 3068:         foreach my $sparetype (@sparestypes) {
 3069:             my $targettable;
 3070:             for (my $i=0; $i<$numspares; $i++) {
 3071:                 my $checked;
 3072:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3073:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3074:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3075:                             $checked = ' checked="checked"';
 3076:                         }
 3077:                     }
 3078:                 }
 3079:                 my ($chkboxval,$disabled);
 3080:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3081:                     $chkboxval = $spares[$i];
 3082:                 }
 3083:                 if (exists($currbalancer{$spares[$i]})) {
 3084:                     $disabled = ' disabled="disabled"';
 3085:                 }
 3086:                 $targettable .= 
 3087:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3088:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3089:                     '</span></label></td>';
 3090:                 my $rem = $i%($numinrow);
 3091:                 if ($rem == 0) {
 3092:                     if (($i > 0) && ($i < $numspares-1)) {
 3093:                         $targettable .= '</tr>';
 3094:                     }
 3095:                     if ($i < $numspares-1) {
 3096:                         $targettable .= '<tr>';
 3097:                     }
 3098:                 }
 3099:             }
 3100:             if ($targettable ne '') {
 3101:                 my $rem = $numspares%($numinrow);
 3102:                 my $colsleft = $numinrow - $rem;
 3103:                 if ($colsleft > 1 ) {
 3104:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3105:                                     '&nbsp;</td>';
 3106:                 } elsif ($colsleft == 1) {
 3107:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3108:                 }
 3109:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3110:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3111:             }
 3112:         }
 3113:         $datatable .= '</div></td></tr>'.
 3114:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3115:                                            $othertitle,$usertypes,$types,\%servers,
 3116:                                            \%currbalancer,$lonhost,
 3117:                                            $targets_div_style,$homedom_div_style,
 3118:                                            $css_class[$cssidx],$balnum,$islast);
 3119:         $$rowtotal += $rownum;
 3120:         $balnum ++;
 3121:     }
 3122:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3123:     return $datatable;
 3124: }
 3125: 
 3126: sub get_loadbalancers_config {
 3127:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3128:     return unless ((ref($servers) eq 'HASH') &&
 3129:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3130:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3131:     if (keys(%{$existing}) > 0) {
 3132:         my $oldlonhost;
 3133:         foreach my $key (sort(keys(%{$existing}))) {
 3134:             if ($key eq 'lonhost') {
 3135:                 $oldlonhost = $existing->{'lonhost'};
 3136:                 $currbalancer->{$oldlonhost} = 1;
 3137:             } elsif ($key eq 'targets') {
 3138:                 if ($oldlonhost) {
 3139:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3140:                 }
 3141:             } elsif ($key eq 'rules') {
 3142:                 if ($oldlonhost) {
 3143:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3144:                 }
 3145:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3146:                 $currbalancer->{$key} = 1;
 3147:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3148:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3149:             }
 3150:         }
 3151:     } else {
 3152:         my ($balancerref,$targetsref) =
 3153:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3154:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3155:             foreach my $server (sort(keys(%{$balancerref}))) {
 3156:                 $currbalancer->{$server} = 1;
 3157:                 $currtargets->{$server} = $targetsref->{$server};
 3158:             }
 3159:         }
 3160:     }
 3161:     return;
 3162: }
 3163: 
 3164: sub loadbalancing_rules {
 3165:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3166:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3167:         $css_class,$balnum,$islast) = @_;
 3168:     my $output;
 3169:     my $num = 0;
 3170:     my ($alltypes,$othertypes,$titles) = 
 3171:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3172:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3173:         foreach my $type (@{$alltypes}) {
 3174:             $num ++;
 3175:             my $current;
 3176:             if (ref($currrules) eq 'HASH') {
 3177:                 $current = $currrules->{$type};
 3178:             }
 3179:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3180:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3181:                     $current = '';
 3182:                 }
 3183:             }
 3184:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3185:                                              $servers,$currbalancer,$lonhost,$dom,
 3186:                                              $targets_div_style,$homedom_div_style,
 3187:                                              $css_class,$balnum,$num,$islast);
 3188:         }
 3189:     }
 3190:     return $output;
 3191: }
 3192: 
 3193: sub loadbalancing_titles {
 3194:     my ($dom,$intdom,$usertypes,$types) = @_;
 3195:     my %othertypes = (
 3196:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3197:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3198:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3199:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3200:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 3201:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 3202:                      );
 3203:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 3204:     if (ref($types) eq 'ARRAY') {
 3205:         unshift(@alltypes,@{$types},'default');
 3206:     }
 3207:     my %titles;
 3208:     foreach my $type (@alltypes) {
 3209:         if ($type =~ /^_LC_/) {
 3210:             $titles{$type} = $othertypes{$type};
 3211:         } elsif ($type eq 'default') {
 3212:             $titles{$type} = &mt('All users from [_1]',$dom);
 3213:             if (ref($types) eq 'ARRAY') {
 3214:                 if (@{$types} > 0) {
 3215:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3216:                 }
 3217:             }
 3218:         } elsif (ref($usertypes) eq 'HASH') {
 3219:             $titles{$type} = $usertypes->{$type};
 3220:         }
 3221:     }
 3222:     return (\@alltypes,\%othertypes,\%titles);
 3223: }
 3224: 
 3225: sub loadbalance_rule_row {
 3226:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3227:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3228:     my @rulenames;
 3229:     my %ruletitles = &offloadtype_text();
 3230:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 3231:         @rulenames = ('balancer','offloadedto');
 3232:     } else {
 3233:         @rulenames = ('default','homeserver');
 3234:         if ($type eq '_LC_external') {
 3235:             push(@rulenames,'externalbalancer');
 3236:         } else {
 3237:             push(@rulenames,'specific');
 3238:         }
 3239:         push(@rulenames,'none');
 3240:     }
 3241:     my $style = $targets_div_style;
 3242:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3243:         $style = $homedom_div_style;
 3244:     }
 3245:     my $space;
 3246:     if ($islast && $num == 1) {
 3247:         $space = '<div display="inline-block">&nbsp;</div>';
 3248:     }
 3249:     my $output = 
 3250:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3251:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3252:         '<td valaign="top">'.$space.
 3253:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3254:     for (my $i=0; $i<@rulenames; $i++) {
 3255:         my $rule = $rulenames[$i];
 3256:         my ($checked,$extra);
 3257:         if ($rulenames[$i] eq 'default') {
 3258:             $rule = '';
 3259:         }
 3260:         if ($rulenames[$i] eq 'specific') {
 3261:             if (ref($servers) eq 'HASH') {
 3262:                 my $default;
 3263:                 if (($current ne '') && (exists($servers->{$current}))) {
 3264:                     $checked = ' checked="checked"';
 3265:                 }
 3266:                 unless ($checked) {
 3267:                     $default = ' selected="selected"';
 3268:                 }
 3269:                 $extra = 
 3270:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3271:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3272:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3273:                     '<option value=""'.$default.'></option>'."\n";
 3274:                 foreach my $server (sort(keys(%{$servers}))) {
 3275:                     if (ref($currbalancer) eq 'HASH') {
 3276:                         next if (exists($currbalancer->{$server}));
 3277:                     }
 3278:                     my $selected;
 3279:                     if ($server eq $current) {
 3280:                         $selected = ' selected="selected"';
 3281:                     }
 3282:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3283:                 }
 3284:                 $extra .= '</select>';
 3285:             }
 3286:         } elsif ($rule eq $current) {
 3287:             $checked = ' checked="checked"';
 3288:         }
 3289:         $output .= '<span class="LC_nobreak"><label>'.
 3290:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3291:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3292:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3293:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3294:                    '</label>'.$extra.'</span><br />'."\n";
 3295:     }
 3296:     $output .= '</div></td></tr>'."\n";
 3297:     return $output;
 3298: }
 3299: 
 3300: sub offloadtype_text {
 3301:     my %ruletitles = &Apache::lonlocal::texthash (
 3302:            'default'          => 'Offloads to default destinations',
 3303:            'homeserver'       => "Offloads to user's home server",
 3304:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3305:            'specific'         => 'Offloads to specific server',
 3306:            'none'             => 'No offload',
 3307:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 3308:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 3309:     );
 3310:     return %ruletitles;
 3311: }
 3312: 
 3313: sub sparestype_titles {
 3314:     my %typestitles = &Apache::lonlocal::texthash (
 3315:                           'primary' => 'primary',
 3316:                           'default' => 'default',
 3317:                       );
 3318:     return %typestitles;
 3319: }
 3320: 
 3321: sub contact_titles {
 3322:     my %titles = &Apache::lonlocal::texthash (
 3323:                    'supportemail' => 'Support E-mail address',
 3324:                    'adminemail'   => 'Default Server Admin E-mail address',
 3325:                    'errormail'    => 'Error reports to be e-mailed to',
 3326:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3327:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3328:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3329:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3330:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3331:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 3332:                  );
 3333:     my %short_titles = &Apache::lonlocal::texthash (
 3334:                            adminemail   => 'Admin E-mail address',
 3335:                            supportemail => 'Support E-mail',
 3336:                        );   
 3337:     return (\%titles,\%short_titles);
 3338: }
 3339: 
 3340: sub tool_titles {
 3341:     my %titles = &Apache::lonlocal::texthash (
 3342:                      aboutme    => 'Personal web page',
 3343:                      blog       => 'Blog',
 3344:                      webdav     => 'WebDAV',
 3345:                      portfolio  => 'Portfolio',
 3346:                      official   => 'Official courses (with institutional codes)',
 3347:                      unofficial => 'Unofficial courses',
 3348:                      community  => 'Communities',
 3349:                  );
 3350:     return %titles;
 3351: }
 3352: 
 3353: sub courserequest_titles {
 3354:     my %titles = &Apache::lonlocal::texthash (
 3355:                                    official   => 'Official',
 3356:                                    unofficial => 'Unofficial',
 3357:                                    community  => 'Communities',
 3358:                                    norequest  => 'Not allowed',
 3359:                                    approval   => 'Approval by Dom. Coord.',
 3360:                                    validate   => 'With validation',
 3361:                                    autolimit  => 'Numerical limit',
 3362:                                    unlimited  => '(blank for unlimited)',
 3363:                  );
 3364:     return %titles;
 3365: }
 3366: 
 3367: sub authorrequest_titles {
 3368:     my %titles = &Apache::lonlocal::texthash (
 3369:                                    norequest  => 'Not allowed',
 3370:                                    approval   => 'Approval by Dom. Coord.',
 3371:                                    automatic  => 'Automatic approval',
 3372:                  );
 3373:     return %titles;
 3374: } 
 3375: 
 3376: sub courserequest_conditions {
 3377:     my %conditions = &Apache::lonlocal::texthash (
 3378:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3379:        validate   => '(Processing of request subject to institutional validation).',
 3380:                  );
 3381:     return %conditions;
 3382: }
 3383: 
 3384: 
 3385: sub print_usercreation {
 3386:     my ($position,$dom,$settings,$rowtotal) = @_;
 3387:     my $numinrow = 4;
 3388:     my $datatable;
 3389:     if ($position eq 'top') {
 3390:         $$rowtotal ++;
 3391:         my $rowcount = 0;
 3392:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3393:         if (ref($rules) eq 'HASH') {
 3394:             if (keys(%{$rules}) > 0) {
 3395:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3396:                                                 $ruleorder,$numinrow,$rowcount);
 3397:                 $$rowtotal ++;
 3398:                 $rowcount ++;
 3399:             }
 3400:         }
 3401:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3402:         if (ref($idrules) eq 'HASH') {
 3403:             if (keys(%{$idrules}) > 0) {
 3404:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3405:                                                 $idruleorder,$numinrow,$rowcount);
 3406:                 $$rowtotal ++;
 3407:                 $rowcount ++;
 3408:             }
 3409:         }
 3410:         my ($emailrules,$emailruleorder) = 
 3411:             &Apache::lonnet::inst_userrules($dom,'email');
 3412:         if (ref($emailrules) eq 'HASH') {
 3413:             if (keys(%{$emailrules}) > 0) {
 3414:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 3415:                                                 $emailruleorder,$numinrow,$rowcount);
 3416:                 $$rowtotal ++;
 3417:                 $rowcount ++;
 3418:             }
 3419:         }
 3420:         if ($rowcount == 0) {
 3421:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3422:             $$rowtotal ++;
 3423:             $rowcount ++;
 3424:         }
 3425:     } elsif ($position eq 'middle') {
 3426:         my @creators = ('author','course','requestcrs','selfcreate');
 3427:         my ($rules,$ruleorder) =
 3428:             &Apache::lonnet::inst_userrules($dom,'username');
 3429:         my %lt = &usercreation_types();
 3430:         my %checked;
 3431:         my @selfcreate; 
 3432:         if (ref($settings) eq 'HASH') {
 3433:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3434:                 foreach my $item (@creators) {
 3435:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3436:                 }
 3437:                 if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3438:                     @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3439:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3440:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3441:                         @selfcreate = ('email','login','sso');
 3442:                     } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3443:                         @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3444:                     }
 3445:                 }
 3446:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3447:                 foreach my $item (@creators) {
 3448:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3449:                         $checked{$item} = 'none';
 3450:                     }
 3451:                 }
 3452:             }
 3453:         }
 3454:         my $rownum = 0;
 3455:         foreach my $item (@creators) {
 3456:             $rownum ++;
 3457:             if ($item ne 'selfcreate') {  
 3458:                 if ($checked{$item} eq '') {
 3459:                     $checked{$item} = 'any';
 3460:                 }
 3461:             }
 3462:             my $css_class;
 3463:             if ($rownum%2) {
 3464:                 $css_class = '';
 3465:             } else {
 3466:                 $css_class = ' class="LC_odd_row" ';
 3467:             }
 3468:             $datatable .= '<tr'.$css_class.'>'.
 3469:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3470:                          '</span></td><td align="right">';
 3471:             my @options;
 3472:             if ($item eq 'selfcreate') {
 3473:                 push(@options,('email','login','sso'));
 3474:             } else {
 3475:                 @options = ('any');
 3476:                 if (ref($rules) eq 'HASH') {
 3477:                     if (keys(%{$rules}) > 0) {
 3478:                         push(@options,('official','unofficial'));
 3479:                     }
 3480:                 }
 3481:                 push(@options,'none');
 3482:             }
 3483:             foreach my $option (@options) {
 3484:                 my $type = 'radio';
 3485:                 my $check = ' ';
 3486:                 if ($item eq 'selfcreate') {
 3487:                     $type = 'checkbox';
 3488:                     if (grep(/^\Q$option\E$/,@selfcreate)) {
 3489:                         $check = ' checked="checked" ';
 3490:                     }
 3491:                 } else {
 3492:                     if ($checked{$item} eq $option) {
 3493:                         $check = ' checked="checked" ';
 3494:                     }
 3495:                 } 
 3496:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3497:                               '<input type="'.$type.'" name="can_createuser_'.
 3498:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3499:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3500:             }
 3501:             $datatable .= '</td></tr>';
 3502:         }
 3503:         my ($othertitle,$usertypes,$types) =
 3504:             &Apache::loncommon::sorted_inst_types($dom);
 3505:         my $createsettings;
 3506:         if (ref($settings) eq 'HASH') {
 3507:             $createsettings = $settings->{cancreate};
 3508:         }
 3509:         if (ref($usertypes) eq 'HASH') {
 3510:             if (keys(%{$usertypes}) > 0) {
 3511:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3512:                                              $dom,$numinrow,$othertitle,
 3513:                                              'statustocreate');
 3514:                 $$rowtotal ++;
 3515:                 $rownum ++;
 3516:             }
 3517:         }
 3518:         $datatable .= &captcha_choice('cancreate',$createsettings,$rownum);
 3519:     } else {
 3520:         my @contexts = ('author','course','domain');
 3521:         my @authtypes = ('int','krb4','krb5','loc');
 3522:         my %checked;
 3523:         if (ref($settings) eq 'HASH') {
 3524:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3525:                 foreach my $item (@contexts) {
 3526:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3527:                         foreach my $auth (@authtypes) {
 3528:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3529:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3530:                             }
 3531:                         }
 3532:                     }
 3533:                 }
 3534:             }
 3535:         } else {
 3536:             foreach my $item (@contexts) {
 3537:                 foreach my $auth (@authtypes) {
 3538:                     $checked{$item}{$auth} = ' checked="checked" ';
 3539:                 }
 3540:             }
 3541:         }
 3542:         my %title = &context_names();
 3543:         my %authname = &authtype_names();
 3544:         my $rownum = 0;
 3545:         my $css_class; 
 3546:         foreach my $item (@contexts) {
 3547:             if ($rownum%2) {
 3548:                 $css_class = '';
 3549:             } else {
 3550:                 $css_class = ' class="LC_odd_row" ';
 3551:             }
 3552:             $datatable .=   '<tr'.$css_class.'>'.
 3553:                             '<td>'.$title{$item}.
 3554:                             '</td><td class="LC_left_item">'.
 3555:                             '<span class="LC_nobreak">';
 3556:             foreach my $auth (@authtypes) {
 3557:                 $datatable .= '<label>'. 
 3558:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3559:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3560:                               $authname{$auth}.'</label>&nbsp;';
 3561:             }
 3562:             $datatable .= '</span></td></tr>';
 3563:             $rownum ++;
 3564:         }
 3565:         $$rowtotal += $rownum;
 3566:     }
 3567:     return $datatable;
 3568: }
 3569: 
 3570: sub captcha_choice {
 3571:     my ($context,$settings,$itemcount) = @_;
 3572:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 3573:     my %lt = &captcha_phrases();
 3574:     $keyentry = 'hidden';
 3575:     if ($context eq 'cancreate') {
 3576:         $rowname = &mt('CAPTCHA validation (e-mail as username)');
 3577:     } elsif ($context eq 'login') {
 3578:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 3579:     }
 3580:     if (ref($settings) eq 'HASH') {
 3581:         if ($settings->{'captcha'}) {
 3582:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 3583:         } else {
 3584:             $checked{'original'} = ' checked="checked"';
 3585:         }
 3586:         if ($settings->{'captcha'} eq 'recaptcha') {
 3587:             $pubtext = $lt{'pub'};
 3588:             $privtext = $lt{'priv'};
 3589:             $keyentry = 'text';
 3590:         }
 3591:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 3592:             $currpub = $settings->{'recaptchakeys'}{'public'};
 3593:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 3594:         }
 3595:     } else {
 3596:         $checked{'original'} = ' checked="checked"';
 3597:     }
 3598:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3599:     my $output = '<tr'.$css_class.'>'.
 3600:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 3601:                  '<table><tr><td>'."\n";
 3602:     foreach my $option ('original','recaptcha','notused') {
 3603:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 3604:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 3605:                    $lt{$option}.'</label></span>';
 3606:         unless ($option eq 'notused') {
 3607:             $output .= ('&nbsp;'x2)."\n";
 3608:         }
 3609:     }
 3610: #
 3611: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 3612: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 3613: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu) 
 3614: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 3615: #  
 3616:     $output .= '</td></tr>'."\n".
 3617:                '<tr><td>'."\n".
 3618:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 3619:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 3620:                $currpub.'" size="40" /></span><br />'."\n".
 3621:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 3622:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 3623:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 3624:                '</td></tr>';
 3625:     return $output;
 3626: }
 3627: 
 3628: sub user_formats_row {
 3629:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 3630:     my $output;
 3631:     my %text = (
 3632:                    'username' => 'new usernames',
 3633:                    'id'       => 'IDs',
 3634:                    'email'    => 'self-created accounts (e-mail)',
 3635:                );
 3636:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3637:     $output = '<tr '.$css_class.'>'.
 3638:               '<td><span class="LC_nobreak">';
 3639:     if ($type eq 'email') {
 3640:         $output .= &mt("Formats disallowed for $text{$type}: ");
 3641:     } else {
 3642:         $output .= &mt("Format rules to check for $text{$type}: ");
 3643:     }
 3644:     $output .= '</span></td>'.
 3645:                '<td class="LC_left_item" colspan="2"><table>';
 3646:     my $rem;
 3647:     if (ref($ruleorder) eq 'ARRAY') {
 3648:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 3649:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 3650:                 my $rem = $i%($numinrow);
 3651:                 if ($rem == 0) {
 3652:                     if ($i > 0) {
 3653:                         $output .= '</tr>';
 3654:                     }
 3655:                     $output .= '<tr>';
 3656:                 }
 3657:                 my $check = ' ';
 3658:                 if (ref($settings) eq 'HASH') {
 3659:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 3660:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 3661:                             $check = ' checked="checked" ';
 3662:                         }
 3663:                     }
 3664:                 }
 3665:                 $output .= '<td class="LC_left_item">'.
 3666:                            '<span class="LC_nobreak"><label>'.
 3667:                            '<input type="checkbox" name="'.$type.'_rule" '.
 3668:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 3669:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 3670:             }
 3671:         }
 3672:         $rem = @{$ruleorder}%($numinrow);
 3673:     }
 3674:     my $colsleft = $numinrow - $rem;
 3675:     if ($colsleft > 1 ) {
 3676:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3677:                    '&nbsp;</td>';
 3678:     } elsif ($colsleft == 1) {
 3679:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 3680:     }
 3681:     $output .= '</tr></table></td></tr>';
 3682:     return $output;
 3683: }
 3684: 
 3685: sub usercreation_types {
 3686:     my %lt = &Apache::lonlocal::texthash (
 3687:                     author     => 'When adding a co-author',
 3688:                     course     => 'When adding a user to a course',
 3689:                     requestcrs => 'When requesting a course',
 3690:                     selfcreate => 'User creates own account', 
 3691:                     any        => 'Any',
 3692:                     official   => 'Institutional only ',
 3693:                     unofficial => 'Non-institutional only',
 3694:                     email      => 'E-mail address',
 3695:                     login      => 'Institutional Login',
 3696:                     sso        => 'SSO', 
 3697:                     none       => 'None',
 3698:     );
 3699:     return %lt;
 3700: }
 3701: 
 3702: sub authtype_names {
 3703:     my %lt = &Apache::lonlocal::texthash(
 3704:                       int    => 'Internal',
 3705:                       krb4   => 'Kerberos 4',
 3706:                       krb5   => 'Kerberos 5',
 3707:                       loc    => 'Local',
 3708:                   );
 3709:     return %lt;
 3710: }
 3711: 
 3712: sub context_names {
 3713:     my %context_title = &Apache::lonlocal::texthash(
 3714:        author => 'Creating users when an Author',
 3715:        course => 'Creating users when in a course',
 3716:        domain => 'Creating users when a Domain Coordinator',
 3717:     );
 3718:     return %context_title;
 3719: }
 3720: 
 3721: sub print_usermodification {
 3722:     my ($position,$dom,$settings,$rowtotal) = @_;
 3723:     my $numinrow = 4;
 3724:     my ($context,$datatable,$rowcount);
 3725:     if ($position eq 'top') {
 3726:         $rowcount = 0;
 3727:         $context = 'author'; 
 3728:         foreach my $role ('ca','aa') {
 3729:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3730:                                                    $numinrow,$rowcount);
 3731:             $$rowtotal ++;
 3732:             $rowcount ++;
 3733:         }
 3734:     } elsif ($position eq 'middle') {
 3735:         $context = 'course';
 3736:         $rowcount = 0;
 3737:         foreach my $role ('st','ep','ta','in','cr') {
 3738:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3739:                                                    $numinrow,$rowcount);
 3740:             $$rowtotal ++;
 3741:             $rowcount ++;
 3742:         }
 3743:     } elsif ($position eq 'bottom') {
 3744:         $context = 'selfcreate';
 3745:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3746:         $usertypes->{'default'} = $othertitle;
 3747:         if (ref($types) eq 'ARRAY') {
 3748:             push(@{$types},'default');
 3749:             $usertypes->{'default'} = $othertitle;
 3750:             foreach my $status (@{$types}) {
 3751:                 $datatable .= &modifiable_userdata_row($context,$status,$settings,
 3752:                                                        $numinrow,$rowcount,$usertypes);
 3753:                 $$rowtotal ++;
 3754:                 $rowcount ++;
 3755:             }
 3756:         }
 3757:     }
 3758:     return $datatable;
 3759: }
 3760: 
 3761: sub print_defaults {
 3762:     my ($dom,$rowtotal) = @_;
 3763:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 3764:                  'datelocale_def','portal_def');
 3765:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 3766:     my $titles = &defaults_titles($dom);
 3767:     my $rownum = 0;
 3768:     my ($datatable,$css_class);
 3769:     foreach my $item (@items) {
 3770:         if ($rownum%2) {
 3771:             $css_class = '';
 3772:         } else {
 3773:             $css_class = ' class="LC_odd_row" ';
 3774:         }
 3775:         $datatable .= '<tr'.$css_class.'>'.
 3776:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 3777:                   '</span></td><td class="LC_right_item">';
 3778:         if ($item eq 'auth_def') {
 3779:             my @authtypes = ('internal','krb4','krb5','localauth');
 3780:             my %shortauth = (
 3781:                              internal => 'int',
 3782:                              krb4 => 'krb4',
 3783:                              krb5 => 'krb5',
 3784:                              localauth  => 'loc'
 3785:                            );
 3786:             my %authnames = &authtype_names();
 3787:             foreach my $auth (@authtypes) {
 3788:                 my $checked = ' ';
 3789:                 if ($domdefaults{$item} eq $auth) {
 3790:                     $checked = ' checked="checked" ';
 3791:                 }
 3792:                 $datatable .= '<label><input type="radio" name="'.$item.
 3793:                               '" value="'.$auth.'"'.$checked.'/>'.
 3794:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 3795:             }
 3796:         } elsif ($item eq 'timezone_def') {
 3797:             my $includeempty = 1;
 3798:             $datatable .= &Apache::loncommon::select_timezone($item,$domdefaults{$item},undef,$includeempty);
 3799:         } elsif ($item eq 'datelocale_def') {
 3800:             my $includeempty = 1;
 3801:             $datatable .= &Apache::loncommon::select_datelocale($item,$domdefaults{$item},undef,$includeempty);
 3802:         } elsif ($item eq 'lang_def') {
 3803:             my %langchoices = &get_languages_hash();
 3804:             $langchoices{''} = 'No language preference';
 3805:             %langchoices = &Apache::lonlocal::texthash(%langchoices);
 3806:             $datatable .= &Apache::loncommon::select_form($domdefaults{$item},$item,
 3807:                                                           \%langchoices);
 3808:         } else {
 3809:             my $size;
 3810:             if ($item eq 'portal_def') {
 3811:                 $size = ' size="25"';
 3812:             }
 3813:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 3814:                           $domdefaults{$item}.'"'.$size.' />';
 3815:         }
 3816:         $datatable .= '</td></tr>';
 3817:         $rownum ++;
 3818:     }
 3819:     $$rowtotal += $rownum;
 3820:     return $datatable;
 3821: }
 3822: 
 3823: sub get_languages_hash {
 3824:     my %langchoices;
 3825:     foreach my $id (&Apache::loncommon::languageids()) {
 3826:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 3827:         if ($code ne '') {
 3828:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 3829:         }
 3830:     }
 3831:     return %langchoices;
 3832: }
 3833: 
 3834: sub defaults_titles {
 3835:     my ($dom) = @_;
 3836:     my %titles = &Apache::lonlocal::texthash (
 3837:                    'auth_def'      => 'Default authentication type',
 3838:                    'auth_arg_def'  => 'Default authentication argument',
 3839:                    'lang_def'      => 'Default language',
 3840:                    'timezone_def'  => 'Default timezone',
 3841:                    'datelocale_def' => 'Default locale for dates',
 3842:                    'portal_def'     => 'Portal/Default URL',
 3843:                  );
 3844:     if ($dom) {
 3845:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 3846:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 3847:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 3848:         $protocol = 'http' if ($protocol ne 'https');
 3849:         if ($uint_dom) {
 3850:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 3851:                                          $uint_dom);
 3852:         }
 3853:     }
 3854:     return (\%titles);
 3855: }
 3856: 
 3857: sub print_scantronformat {
 3858:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 3859:     my $itemcount = 1;
 3860:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 3861:         %confhash);
 3862:     my $switchserver = &check_switchserver($dom,$confname);
 3863:     my %lt = &Apache::lonlocal::texthash (
 3864:                 default => 'Default bubblesheet format file error',
 3865:                 custom  => 'Custom bubblesheet format file error',
 3866:              );
 3867:     my %scantronfiles = (
 3868:         default => 'default.tab',
 3869:         custom => 'custom.tab',
 3870:     );
 3871:     foreach my $key (keys(%scantronfiles)) {
 3872:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 3873:                               .$scantronfiles{$key};
 3874:     }
 3875:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 3876:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 3877:         if (!$switchserver) {
 3878:             my $servadm = $r->dir_config('lonAdmEMail');
 3879:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 3880:             if ($configuserok eq 'ok') {
 3881:                 if ($author_ok eq 'ok') {
 3882:                     my %legacyfile = (
 3883:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 3884:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 3885:                     );
 3886:                     my %md5chk;
 3887:                     foreach my $type (keys(%legacyfile)) {
 3888:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 3889:                         chomp($md5chk{$type});
 3890:                     }
 3891:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 3892:                         foreach my $type (keys(%legacyfile)) {
 3893:                             ($scantronurls{$type},my $error) = 
 3894:                                 &legacy_scantronformat($r,$dom,$confname,
 3895:                                                  $type,$legacyfile{$type},
 3896:                                                  $scantronurls{$type},
 3897:                                                  $scantronfiles{$type});
 3898:                             if ($error ne '') {
 3899:                                 $error{$type} = $error;
 3900:                             }
 3901:                         }
 3902:                         if (keys(%error) == 0) {
 3903:                             $is_custom = 1;
 3904:                             $confhash{'scantron'}{'scantronformat'} = 
 3905:                                 $scantronurls{'custom'};
 3906:                             my $putresult = 
 3907:                                 &Apache::lonnet::put_dom('configuration',
 3908:                                                          \%confhash,$dom);
 3909:                             if ($putresult ne 'ok') {
 3910:                                 $error{'custom'} = 
 3911:                                     '<span class="LC_error">'.
 3912:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3913:                             }
 3914:                         }
 3915:                     } else {
 3916:                         ($scantronurls{'default'},my $error) =
 3917:                             &legacy_scantronformat($r,$dom,$confname,
 3918:                                           'default',$legacyfile{'default'},
 3919:                                           $scantronurls{'default'},
 3920:                                           $scantronfiles{'default'});
 3921:                         if ($error eq '') {
 3922:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 3923:                             my $putresult =
 3924:                                 &Apache::lonnet::put_dom('configuration',
 3925:                                                          \%confhash,$dom);
 3926:                             if ($putresult ne 'ok') {
 3927:                                 $error{'default'} =
 3928:                                     '<span class="LC_error">'.
 3929:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 3930:                             }
 3931:                         } else {
 3932:                             $error{'default'} = $error;
 3933:                         }
 3934:                     }
 3935:                 }
 3936:             }
 3937:         } else {
 3938:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 3939:         }
 3940:     }
 3941:     if (ref($settings) eq 'HASH') {
 3942:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 3943:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 3944:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 3945:                 $scantronurl = '';
 3946:             } else {
 3947:                 $scantronurl = $settings->{'scantronformat'};
 3948:             }
 3949:             $is_custom = 1;
 3950:         } else {
 3951:             $scantronurl = $scantronurls{'default'};
 3952:         }
 3953:     } else {
 3954:         if ($is_custom) {
 3955:             $scantronurl = $scantronurls{'custom'};
 3956:         } else {
 3957:             $scantronurl = $scantronurls{'default'};
 3958:         }
 3959:     }
 3960:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3961:     $datatable .= '<tr'.$css_class.'>';
 3962:     if (!$is_custom) {
 3963:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 3964:                       '<span class="LC_nobreak">';
 3965:         if ($scantronurl) {
 3966:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 3967:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 3968:         } else {
 3969:             $datatable = &mt('File unavailable for display');
 3970:         }
 3971:         $datatable .= '</span></td>';
 3972:         if (keys(%error) == 0) { 
 3973:             $datatable .= '<td valign="bottom">';
 3974:             if (!$switchserver) {
 3975:                 $datatable .= &mt('Upload:').'<br />';
 3976:             }
 3977:         } else {
 3978:             my $errorstr;
 3979:             foreach my $key (sort(keys(%error))) {
 3980:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3981:             }
 3982:             $datatable .= '<td>'.$errorstr;
 3983:         }
 3984:     } else {
 3985:         if (keys(%error) > 0) {
 3986:             my $errorstr;
 3987:             foreach my $key (sort(keys(%error))) {
 3988:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 3989:             } 
 3990:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 3991:         } elsif ($scantronurl) {
 3992:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 3993:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 3994:             $datatable .= '<td><span class="LC_nobreak">'.
 3995:                           $link.
 3996:                           '<label><input type="checkbox" name="scantronformat_del"'.
 3997:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 3998:                           '<td><span class="LC_nobreak">&nbsp;'.
 3999:                           &mt('Replace:').'</span><br />';
 4000:         }
 4001:     }
 4002:     if (keys(%error) == 0) {
 4003:         if ($switchserver) {
 4004:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4005:         } else {
 4006:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4007:                          '<input type="file" name="scantronformat" /></span>';
 4008:         }
 4009:     }
 4010:     $datatable .= '</td></tr>';
 4011:     $$rowtotal ++;
 4012:     return $datatable;
 4013: }
 4014: 
 4015: sub legacy_scantronformat {
 4016:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 4017:     my ($url,$error);
 4018:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 4019:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 4020:         (my $result,$url) =
 4021:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 4022:                          '','',$newfile);
 4023:         if ($result ne 'ok') {
 4024:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 4025:         }
 4026:     }
 4027:     return ($url,$error);
 4028: }
 4029: 
 4030: sub print_coursecategories {
 4031:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 4032:     my $datatable;
 4033:     if ($position eq 'top') {
 4034:         my $toggle_cats_crs = ' ';
 4035:         my $toggle_cats_dom = ' checked="checked" ';
 4036:         my $can_cat_crs = ' ';
 4037:         my $can_cat_dom = ' checked="checked" ';
 4038:         my $toggle_catscomm_comm = ' ';
 4039:         my $toggle_catscomm_dom = ' checked="checked" ';
 4040:         my $can_catcomm_comm = ' ';
 4041:         my $can_catcomm_dom = ' checked="checked" ';
 4042: 
 4043:         if (ref($settings) eq 'HASH') {
 4044:             if ($settings->{'togglecats'} eq 'crs') {
 4045:                 $toggle_cats_crs = $toggle_cats_dom;
 4046:                 $toggle_cats_dom = ' ';
 4047:             }
 4048:             if ($settings->{'categorize'} eq 'crs') {
 4049:                 $can_cat_crs = $can_cat_dom;
 4050:                 $can_cat_dom = ' ';
 4051:             }
 4052:             if ($settings->{'togglecatscomm'} eq 'comm') {
 4053:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 4054:                 $toggle_catscomm_dom = ' ';
 4055:             }
 4056:             if ($settings->{'categorizecomm'} eq 'comm') {
 4057:                 $can_catcomm_comm = $can_catcomm_dom;
 4058:                 $can_catcomm_dom = ' ';
 4059:             }
 4060:         }
 4061:         my %title = &Apache::lonlocal::texthash (
 4062:                      togglecats     => 'Show/Hide a course in catalog',
 4063:                      togglecatscomm => 'Show/Hide a community in catalog',
 4064:                      categorize     => 'Assign a category to a course',
 4065:                      categorizecomm => 'Assign a category to a community',
 4066:                     );
 4067:         my %level = &Apache::lonlocal::texthash (
 4068:                      dom  => 'Set in Domain',
 4069:                      crs  => 'Set in Course',
 4070:                      comm => 'Set in Community',
 4071:                     );
 4072:         $datatable = '<tr class="LC_odd_row">'.
 4073:                   '<td>'.$title{'togglecats'}.'</td>'.
 4074:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4075:                   '<input type="radio" name="togglecats"'.
 4076:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4077:                   '<label><input type="radio" name="togglecats"'.
 4078:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4079:                   '</tr><tr>'.
 4080:                   '<td>'.$title{'categorize'}.'</td>'.
 4081:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4082:                   '<label><input type="radio" name="categorize"'.
 4083:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4084:                   '<label><input type="radio" name="categorize"'.
 4085:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4086:                   '</tr><tr class="LC_odd_row">'.
 4087:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 4088:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4089:                   '<input type="radio" name="togglecatscomm"'.
 4090:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4091:                   '<label><input type="radio" name="togglecatscomm"'.
 4092:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4093:                   '</tr><tr>'.
 4094:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 4095:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4096:                   '<label><input type="radio" name="categorizecomm"'.
 4097:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4098:                   '<label><input type="radio" name="categorizecomm"'.
 4099:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4100:                   '</tr>';
 4101:         $$rowtotal += 4;
 4102:     } else {
 4103:         my $css_class;
 4104:         my $itemcount = 1;
 4105:         my $cathash; 
 4106:         if (ref($settings) eq 'HASH') {
 4107:             $cathash = $settings->{'cats'};
 4108:         }
 4109:         if (ref($cathash) eq 'HASH') {
 4110:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4111:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4112:                                                    \%allitems,\%idx,\@jsarray);
 4113:             my $maxdepth = scalar(@cats);
 4114:             my $colattrib = '';
 4115:             if ($maxdepth > 2) {
 4116:                 $colattrib = ' colspan="2" ';
 4117:             }
 4118:             my @path;
 4119:             if (@cats > 0) {
 4120:                 if (ref($cats[0]) eq 'ARRAY') {
 4121:                     my $numtop = @{$cats[0]};
 4122:                     my $maxnum = $numtop;
 4123:                     my %default_names = (
 4124:                           instcode    => &mt('Official courses'),
 4125:                           communities => &mt('Communities'),
 4126:                     );
 4127: 
 4128:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4129:                         ($cathash->{'instcode::0'} eq '') ||
 4130:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4131:                         ($cathash->{'communities::0'} eq '')) {
 4132:                         $maxnum ++;
 4133:                     }
 4134:                     my $lastidx;
 4135:                     for (my $i=0; $i<$numtop; $i++) {
 4136:                         my $parent = $cats[0][$i];
 4137:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4138:                         my $item = &escape($parent).'::0';
 4139:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4140:                         $lastidx = $idx{$item};
 4141:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4142:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4143:                         for (my $k=0; $k<=$maxnum; $k++) {
 4144:                             my $vpos = $k+1;
 4145:                             my $selstr;
 4146:                             if ($k == $i) {
 4147:                                 $selstr = ' selected="selected" ';
 4148:                             }
 4149:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4150:                         }
 4151:                         $datatable .= '</select></td><td>';
 4152:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4153:                             $datatable .=  '<span class="LC_nobreak">'
 4154:                                            .$default_names{$parent}.'</span>';
 4155:                             if ($parent eq 'instcode') {
 4156:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4157:                                               .&mt('with institutional codes')
 4158:                                               .')</span></td><td'.$colattrib.'>';
 4159:                             } else {
 4160:                                 $datatable .= '<table><tr><td>';
 4161:                             }
 4162:                             $datatable .= '<span class="LC_nobreak">'
 4163:                                           .'<label><input type="radio" name="'
 4164:                                           .$parent.'" value="1" checked="checked" />'
 4165:                                           .&mt('Display').'</label>';
 4166:                             if ($parent eq 'instcode') {
 4167:                                 $datatable .= '&nbsp;';
 4168:                             } else {
 4169:                                 $datatable .= '</span></td></tr><tr><td>'
 4170:                                               .'<span class="LC_nobreak">';
 4171:                             }
 4172:                             $datatable .= '<label><input type="radio" name="'
 4173:                                           .$parent.'" value="0" />'
 4174:                                           .&mt('Do not display').'</label></span>';
 4175:                             if ($parent eq 'communities') {
 4176:                                 $datatable .= '</td></tr></table>';
 4177:                             }
 4178:                             $datatable .= '</td>';
 4179:                         } else {
 4180:                             $datatable .= $parent
 4181:                                           .'&nbsp;<label><input type="checkbox" name="deletecategory" '
 4182:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 4183:                         }
 4184:                         my $depth = 1;
 4185:                         push(@path,$parent);
 4186:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 4187:                         pop(@path);
 4188:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 4189:                         $itemcount ++;
 4190:                     }
 4191:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4192:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 4193:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 4194:                     for (my $k=0; $k<=$maxnum; $k++) {
 4195:                         my $vpos = $k+1;
 4196:                         my $selstr;
 4197:                         if ($k == $numtop) {
 4198:                             $selstr = ' selected="selected" ';
 4199:                         }
 4200:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4201:                     }
 4202:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 4203:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 4204:                                   .'</tr>'."\n";
 4205:                     $itemcount ++;
 4206:                     foreach my $default ('instcode','communities') {
 4207:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 4208:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4209:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 4210:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 4211:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 4212:                             for (my $k=0; $k<=$maxnum; $k++) {
 4213:                                 my $vpos = $k+1;
 4214:                                 my $selstr;
 4215:                                 if ($k == $maxnum) {
 4216:                                     $selstr = ' selected="selected" ';
 4217:                                 }
 4218:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4219:                             }
 4220:                             $datatable .= '</select></span></td>'.
 4221:                                           '<td><span class="LC_nobreak">'.
 4222:                                           $default_names{$default}.'</span>';
 4223:                             if ($default eq 'instcode') {
 4224:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 4225:                                               .&mt('with institutional codes').')</span>';
 4226:                             }
 4227:                             $datatable .= '</td>'
 4228:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 4229:                                           .&mt('Display').'</label>&nbsp;'
 4230:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 4231:                                           .&mt('Do not display').'</label></span></td></tr>';
 4232:                         }
 4233:                     }
 4234:                 }
 4235:             } else {
 4236:                 $datatable .= &initialize_categories($itemcount);
 4237:             }
 4238:         } else {
 4239:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 4240:                           .&initialize_categories($itemcount);
 4241:         }
 4242:         $$rowtotal += $itemcount;
 4243:     }
 4244:     return $datatable;
 4245: }
 4246: 
 4247: sub print_serverstatuses {
 4248:     my ($dom,$settings,$rowtotal) = @_;
 4249:     my $datatable;
 4250:     my @pages = &serverstatus_pages();
 4251:     my (%namedaccess,%machineaccess);
 4252:     foreach my $type (@pages) {
 4253:         $namedaccess{$type} = '';
 4254:         $machineaccess{$type}= '';
 4255:     }
 4256:     if (ref($settings) eq 'HASH') {
 4257:         foreach my $type (@pages) {
 4258:             if (exists($settings->{$type})) {
 4259:                 if (ref($settings->{$type}) eq 'HASH') {
 4260:                     foreach my $key (keys(%{$settings->{$type}})) {
 4261:                         if ($key eq 'namedusers') {
 4262:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4263:                         } elsif ($key eq 'machines') {
 4264:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4265:                         }
 4266:                     }
 4267:                 }
 4268:             }
 4269:         }
 4270:     }
 4271:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4272:     my $rownum = 0;
 4273:     my $css_class;
 4274:     foreach my $type (@pages) {
 4275:         $rownum ++;
 4276:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4277:         $datatable .= '<tr'.$css_class.'>'.
 4278:                       '<td><span class="LC_nobreak">'.
 4279:                       $titles->{$type}.'</span></td>'.
 4280:                       '<td class="LC_left_item">'.
 4281:                       '<input type="text" name="'.$type.'_namedusers" '.
 4282:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4283:                       '<td class="LC_right_item">'.
 4284:                       '<span class="LC_nobreak">'.
 4285:                       '<input type="text" name="'.$type.'_machines" '.
 4286:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4287:                       '</td></tr>'."\n";
 4288:     }
 4289:     $$rowtotal += $rownum;
 4290:     return $datatable;
 4291: }
 4292: 
 4293: sub serverstatus_pages {
 4294:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4295:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 4296:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf');
 4297: }
 4298: 
 4299: sub coursecategories_javascript {
 4300:     my ($settings) = @_;
 4301:     my ($output,$jstext,$cathash);
 4302:     if (ref($settings) eq 'HASH') {
 4303:         $cathash = $settings->{'cats'};
 4304:     }
 4305:     if (ref($cathash) eq 'HASH') {
 4306:         my (@cats,@jsarray,%idx);
 4307:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4308:         if (@jsarray > 0) {
 4309:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4310:             for (my $i=0; $i<@jsarray; $i++) {
 4311:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4312:                     my $catstr = join('","',@{$jsarray[$i]});
 4313:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4314:                 }
 4315:             }
 4316:         }
 4317:     } else {
 4318:         $jstext  = '    var categories = Array(1);'."\n".
 4319:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4320:     }
 4321:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4322:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4323:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4324:     $output = <<"ENDSCRIPT";
 4325: <script type="text/javascript">
 4326: // <![CDATA[
 4327: function reorderCats(form,parent,item,idx) {
 4328:     var changedVal;
 4329: $jstext
 4330:     var newpos = 'addcategory_pos';
 4331:     var current = new Array;
 4332:     if (parent == '') {
 4333:         var has_instcode = 0;
 4334:         var maxtop = categories[idx].length;
 4335:         for (var j=0; j<maxtop; j++) {
 4336:             if (categories[idx][j] == 'instcode::0') {
 4337:                 has_instcode == 1;
 4338:             }
 4339:         }
 4340:         if (has_instcode == 0) {
 4341:             categories[idx][maxtop] = 'instcode_pos';
 4342:         }
 4343:     } else {
 4344:         newpos += '_'+parent;
 4345:     }
 4346:     var maxh = 1 + categories[idx].length;
 4347:     var current = new Array;
 4348:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4349:     if (item == newpos) {
 4350:         changedVal = newitemVal;
 4351:     } else {
 4352:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4353:         current[newitemVal] = newpos;
 4354:     }
 4355:     for (var i=0; i<categories[idx].length; i++) {
 4356:         var elementName = categories[idx][i];
 4357:         if (elementName != item) {
 4358:             if (form.elements[elementName]) {
 4359:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4360:                 current[currVal] = elementName;
 4361:             }
 4362:         }
 4363:     }
 4364:     var oldVal;
 4365:     for (var j=0; j<maxh; j++) {
 4366:         if (current[j] == undefined) {
 4367:             oldVal = j;
 4368:         }
 4369:     }
 4370:     if (oldVal < changedVal) {
 4371:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4372:            var elementName = current[k];
 4373:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4374:         }
 4375:     } else {
 4376:         for (var k=changedVal; k<oldVal; k++) {
 4377:             var elementName = current[k];
 4378:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4379:         }
 4380:     }
 4381:     return;
 4382: }
 4383: 
 4384: function categoryCheck(form) {
 4385:     if (form.elements['addcategory_name'].value == 'instcode') {
 4386:         alert('$instcode_reserved\\n$choose_again');
 4387:         return false;
 4388:     }
 4389:     if (form.elements['addcategory_name'].value == 'communities') {
 4390:         alert('$communities_reserved\\n$choose_again');
 4391:         return false;
 4392:     }
 4393:     return true;
 4394: }
 4395: 
 4396: // ]]>
 4397: </script>
 4398: 
 4399: ENDSCRIPT
 4400:     return $output;
 4401: }
 4402: 
 4403: sub initialize_categories {
 4404:     my ($itemcount) = @_;
 4405:     my ($datatable,$css_class,$chgstr);
 4406:     my %default_names = (
 4407:                       instcode    => 'Official courses (with institutional codes)',
 4408:                       communities => 'Communities',
 4409:                         );
 4410:     my $select0 = ' selected="selected"';
 4411:     my $select1 = '';
 4412:     foreach my $default ('instcode','communities') {
 4413:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4414:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4415:         if ($default eq 'communities') {
 4416:             $select1 = $select0;
 4417:             $select0 = '';
 4418:         }
 4419:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4420:                      .'<select name="'.$default.'_pos">'
 4421:                      .'<option value="0"'.$select0.'>1</option>'
 4422:                      .'<option value="1"'.$select1.'>2</option>'
 4423:                      .'<option value="2">3</option></select>&nbsp;'
 4424:                      .$default_names{$default}
 4425:                      .'</span></td><td><span class="LC_nobreak">'
 4426:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4427:                      .&mt('Display').'</label>&nbsp;<label>'
 4428:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4429:                  .'</label></span></td></tr>';
 4430:         $itemcount ++;
 4431:     }
 4432:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4433:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4434:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4435:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4436:                   .'<option value="0">1</option>'
 4437:                   .'<option value="1">2</option>'
 4438:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4439:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4440:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4441:     return $datatable;
 4442: }
 4443: 
 4444: sub build_category_rows {
 4445:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 4446:     my ($text,$name,$item,$chgstr);
 4447:     if (ref($cats) eq 'ARRAY') {
 4448:         my $maxdepth = scalar(@{$cats});
 4449:         if (ref($cats->[$depth]) eq 'HASH') {
 4450:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 4451:                 my $numchildren = @{$cats->[$depth]{$parent}};
 4452:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4453:                 $text .= '<td><table class="LC_data_table">';
 4454:                 my ($idxnum,$parent_name,$parent_item);
 4455:                 my $higher = $depth - 1;
 4456:                 if ($higher == 0) {
 4457:                     $parent_name = &escape($parent).'::'.$higher;
 4458:                 } else {
 4459:                     if (ref($path) eq 'ARRAY') {
 4460:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4461:                     }
 4462:                 }
 4463:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4464:                 for (my $j=0; $j<=$numchildren; $j++) {
 4465:                     if ($j < $numchildren) {
 4466:                         $name = $cats->[$depth]{$parent}[$j];
 4467:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4468:                         $idxnum = $idx->{$item};
 4469:                     } else {
 4470:                         $name = $parent_name;
 4471:                         $item = $parent_item;
 4472:                     }
 4473:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4474:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4475:                     for (my $i=0; $i<=$numchildren; $i++) {
 4476:                         my $vpos = $i+1;
 4477:                         my $selstr;
 4478:                         if ($j == $i) {
 4479:                             $selstr = ' selected="selected" ';
 4480:                         }
 4481:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4482:                     }
 4483:                     $text .= '</select>&nbsp;';
 4484:                     if ($j < $numchildren) {
 4485:                         my $deeper = $depth+1;
 4486:                         $text .= $name.'&nbsp;'
 4487:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4488:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4489:                         if(ref($path) eq 'ARRAY') {
 4490:                             push(@{$path},$name);
 4491:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4492:                             pop(@{$path});
 4493:                         }
 4494:                     } else {
 4495:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 4496:                         if ($j == $numchildren) {
 4497:                             $text .= $name;
 4498:                         } else {
 4499:                             $text .= $item;
 4500:                         }
 4501:                         $text .= '" value="" />';
 4502:                     }
 4503:                     $text .= '</td></tr>';
 4504:                 }
 4505:                 $text .= '</table></td>';
 4506:             } else {
 4507:                 my $higher = $depth-1;
 4508:                 if ($higher == 0) {
 4509:                     $name = &escape($parent).'::'.$higher;
 4510:                 } else {
 4511:                     if (ref($path) eq 'ARRAY') {
 4512:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4513:                     }
 4514:                 }
 4515:                 my $colspan;
 4516:                 if ($parent ne 'instcode') {
 4517:                     $colspan = $maxdepth - $depth - 1;
 4518:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 4519:                 }
 4520:             }
 4521:         }
 4522:     }
 4523:     return $text;
 4524: }
 4525: 
 4526: sub modifiable_userdata_row {
 4527:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes) = @_;
 4528:     my $rolename;
 4529:     if ($context eq 'selfcreate') {
 4530:         if (ref($usertypes) eq 'HASH') {
 4531:             $rolename = $usertypes->{$role};
 4532:         } else {
 4533:             $rolename = $role;
 4534:         }
 4535:     } else {
 4536:         if ($role eq 'cr') {
 4537:             $rolename = &mt('Custom role');
 4538:         } else {
 4539:             $rolename = &Apache::lonnet::plaintext($role);
 4540:         }
 4541:     }
 4542:     my @fields = ('lastname','firstname','middlename','generation',
 4543:                   'permanentemail','id');
 4544:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4545:     my $output;
 4546:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4547:     $output = '<tr '.$css_class.'>'.
 4548:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 4549:               '<td class="LC_left_item" colspan="2"><table>';
 4550:     my $rem;
 4551:     my %checks;
 4552:     if (ref($settings) eq 'HASH') {
 4553:         if (ref($settings->{$context}) eq 'HASH') {
 4554:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 4555:                 foreach my $field (@fields) {
 4556:                     if ($settings->{$context}->{$role}->{$field}) {
 4557:                         $checks{$field} = ' checked="checked" ';
 4558:                     }
 4559:                 }
 4560:             }
 4561:         }
 4562:     }
 4563:     for (my $i=0; $i<@fields; $i++) {
 4564:         my $rem = $i%($numinrow);
 4565:         if ($rem == 0) {
 4566:             if ($i > 0) {
 4567:                 $output .= '</tr>';
 4568:             }
 4569:             $output .= '<tr>';
 4570:         }
 4571:         my $check = ' ';
 4572:         if (exists($checks{$fields[$i]})) {
 4573:             $check = $checks{$fields[$i]}
 4574:         } else {
 4575:             if ($role eq 'st') {
 4576:                 if (ref($settings) ne 'HASH') {
 4577:                     $check = ' checked="checked" '; 
 4578:                 }
 4579:             }
 4580:         }
 4581:         $output .= '<td class="LC_left_item">'.
 4582:                    '<span class="LC_nobreak"><label>'.
 4583:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 4584:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 4585:                    '</label></span></td>';
 4586:         $rem = @fields%($numinrow);
 4587:     }
 4588:     my $colsleft = $numinrow - $rem;
 4589:     if ($colsleft > 1 ) {
 4590:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4591:                    '&nbsp;</td>';
 4592:     } elsif ($colsleft == 1) {
 4593:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4594:     }
 4595:     $output .= '</tr></table></td></tr>';
 4596:     return $output;
 4597: }
 4598: 
 4599: sub insttypes_row {
 4600:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context) = @_;
 4601:     my %lt = &Apache::lonlocal::texthash (
 4602:                       cansearch => 'Users allowed to search',
 4603:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 4604:                       lockablenames => 'User preference to lock name',
 4605:              );
 4606:     my $showdom;
 4607:     if ($context eq 'cansearch') {
 4608:         $showdom = ' ('.$dom.')';
 4609:     }
 4610:     my $class = 'LC_left_item';
 4611:     if ($context eq 'statustocreate') {
 4612:         $class = 'LC_right_item';
 4613:     }
 4614:     my $output =  '<tr class="LC_odd_row">'.
 4615:                   '<td>'.$lt{$context}.$showdom.
 4616:                   '</td><td class="'.$class.'" colspan="2"><table>';
 4617:     my $rem;
 4618:     if (ref($types) eq 'ARRAY') {
 4619:         for (my $i=0; $i<@{$types}; $i++) {
 4620:             if (defined($usertypes->{$types->[$i]})) {
 4621:                 my $rem = $i%($numinrow);
 4622:                 if ($rem == 0) {
 4623:                     if ($i > 0) {
 4624:                         $output .= '</tr>';
 4625:                     }
 4626:                     $output .= '<tr>';
 4627:                 }
 4628:                 my $check = ' ';
 4629:                 if (ref($settings) eq 'HASH') {
 4630:                     if (ref($settings->{$context}) eq 'ARRAY') {
 4631:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 4632:                             $check = ' checked="checked" ';
 4633:                         }
 4634:                     } elsif ($context eq 'statustocreate') {
 4635:                         $check = ' checked="checked" ';
 4636:                     }
 4637:                 }
 4638:                 $output .= '<td class="LC_left_item">'.
 4639:                            '<span class="LC_nobreak"><label>'.
 4640:                            '<input type="checkbox" name="'.$context.'" '.
 4641:                            'value="'.$types->[$i].'"'.$check.'/>'.
 4642:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 4643:             }
 4644:         }
 4645:         $rem = @{$types}%($numinrow);
 4646:     }
 4647:     my $colsleft = $numinrow - $rem;
 4648:     if (($rem == 0) && (@{$types} > 0)) {
 4649:         $output .= '<tr>';
 4650:     }
 4651:     if ($colsleft > 1) {
 4652:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 4653:     } else {
 4654:         $output .= '<td class="LC_left_item">';
 4655:     }
 4656:     my $defcheck = ' ';
 4657:     if (ref($settings) eq 'HASH') {  
 4658:         if (ref($settings->{$context}) eq 'ARRAY') {
 4659:             if (grep(/^default$/,@{$settings->{$context}})) {
 4660:                 $defcheck = ' checked="checked" ';
 4661:             }
 4662:         } elsif ($context eq 'statustocreate') {
 4663:             $defcheck = ' checked="checked" ';
 4664:         }
 4665:     }
 4666:     $output .= '<span class="LC_nobreak"><label>'.
 4667:                '<input type="checkbox" name="'.$context.'" '.
 4668:                'value="default"'.$defcheck.'/>'.
 4669:                $othertitle.'</label></span></td>'.
 4670:                '</tr></table></td></tr>';
 4671:     return $output;
 4672: }
 4673: 
 4674: sub sorted_searchtitles {
 4675:     my %searchtitles = &Apache::lonlocal::texthash(
 4676:                          'uname' => 'username',
 4677:                          'lastname' => 'last name',
 4678:                          'lastfirst' => 'last name, first name',
 4679:                      );
 4680:     my @titleorder = ('uname','lastname','lastfirst');
 4681:     return (\%searchtitles,\@titleorder);
 4682: }
 4683: 
 4684: sub sorted_searchtypes {
 4685:     my %srchtypes_desc = (
 4686:                            exact    => 'is exact match',
 4687:                            contains => 'contains ..',
 4688:                            begins   => 'begins with ..',
 4689:                          );
 4690:     my @srchtypeorder = ('exact','begins','contains');
 4691:     return (\%srchtypes_desc,\@srchtypeorder);
 4692: }
 4693: 
 4694: sub usertype_update_row {
 4695:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 4696:     my $datatable;
 4697:     my $numinrow = 4;
 4698:     foreach my $type (@{$types}) {
 4699:         if (defined($usertypes->{$type})) {
 4700:             $$rownums ++;
 4701:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 4702:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 4703:                           '</td><td class="LC_left_item"><table>';
 4704:             for (my $i=0; $i<@{$fields}; $i++) {
 4705:                 my $rem = $i%($numinrow);
 4706:                 if ($rem == 0) {
 4707:                     if ($i > 0) {
 4708:                         $datatable .= '</tr>';
 4709:                     }
 4710:                     $datatable .= '<tr>';
 4711:                 }
 4712:                 my $check = ' ';
 4713:                 if (ref($settings) eq 'HASH') {
 4714:                     if (ref($settings->{'fields'}) eq 'HASH') {
 4715:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 4716:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 4717:                                 $check = ' checked="checked" ';
 4718:                             }
 4719:                         }
 4720:                     }
 4721:                 }
 4722: 
 4723:                 if ($i == @{$fields}-1) {
 4724:                     my $colsleft = $numinrow - $rem;
 4725:                     if ($colsleft > 1) {
 4726:                         $datatable .= '<td colspan="'.$colsleft.'">';
 4727:                     } else {
 4728:                         $datatable .= '<td>';
 4729:                     }
 4730:                 } else {
 4731:                     $datatable .= '<td>';
 4732:                 }
 4733:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4734:                               '<input type="checkbox" name="updateable_'.$type.
 4735:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 4736:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 4737:             }
 4738:             $datatable .= '</tr></table></td></tr>';
 4739:         }
 4740:     }
 4741:     return $datatable;
 4742: }
 4743: 
 4744: sub modify_login {
 4745:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 4746:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 4747:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 4748:     %title = ( coursecatalog => 'Display course catalog',
 4749:                adminmail => 'Display administrator E-mail address',
 4750:                helpdesk  => 'Display "Contact Helpdesk" link',
 4751:                newuser => 'Link for visitors to create a user account',
 4752:                loginheader => 'Log-in box header');
 4753:     @offon = ('off','on');
 4754:     if (ref($domconfig{login}) eq 'HASH') {
 4755:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 4756:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 4757:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 4758:             }
 4759:         }
 4760:     }
 4761:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 4762:                                            \%domconfig,\%loginhash);
 4763:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 4764:     foreach my $item (@toggles) {
 4765:         $loginhash{login}{$item} = $env{'form.'.$item};
 4766:     }
 4767:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 4768:     if (ref($colchanges{'login'}) eq 'HASH') {  
 4769:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 4770:                                          \%loginhash);
 4771:     }
 4772: 
 4773:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4774:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 4775:     if (keys(%servers) > 1) {
 4776:         foreach my $lonhost (keys(%servers)) {
 4777:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 4778:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 4779:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 4780:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 4781:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 4782:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4783:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4784:                         $changes{'loginvia'}{$lonhost} = 1;
 4785:                     } else {
 4786:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 4787:                         $changes{'loginvia'}{$lonhost} = 1;
 4788:                     }
 4789:                 } else {
 4790:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4791:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4792:                         $changes{'loginvia'}{$lonhost} = 1;
 4793:                     }
 4794:                 }
 4795:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 4796:                     foreach my $item (@loginvia_attribs) {
 4797:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 4798:                     }
 4799:                 } else {
 4800:                     foreach my $item (@loginvia_attribs) {
 4801:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4802:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4803:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 4804:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4805:                                 $new = '/';
 4806:                             }
 4807:                         }
 4808:                         if (($item eq 'custompath') && 
 4809:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4810:                             $new = '';
 4811:                         }
 4812:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 4813:                             $changes{'loginvia'}{$lonhost} = 1;
 4814:                         }
 4815:                         if ($item eq 'exempt') {
 4816:                             $new =~ s/^\s+//;
 4817:                             $new =~ s/\s+$//;
 4818:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 4819:                             my @okips;
 4820:                             foreach my $ip (@poss_ips) {
 4821:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 4822:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 4823:                                         push(@okips,$ip); 
 4824:                                     }
 4825:                                 }
 4826:                             }
 4827:                             if (@okips > 0) {
 4828:                                 $new = join(',',@okips); 
 4829:                             } else {
 4830:                                 $new = ''; 
 4831:                             }
 4832:                         }
 4833:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4834:                     }
 4835:                 }
 4836:             } else {
 4837:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 4838:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 4839:                     $changes{'loginvia'}{$lonhost} = 1;
 4840:                     foreach my $item (@loginvia_attribs) {
 4841:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 4842:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 4843:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 4844:                                 $new = '/';
 4845:                             }
 4846:                         }
 4847:                         if (($item eq 'custompath') && 
 4848:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 4849:                             $new = '';
 4850:                         }
 4851:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 4852:                     }
 4853:                 }
 4854:             }
 4855:         }
 4856:     }
 4857: 
 4858:     my $servadm = $r->dir_config('lonAdmEMail');
 4859:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 4860:     if (ref($domconfig{'login'}) eq 'HASH') {
 4861:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 4862:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 4863:                 if ($lang eq 'nolang') {
 4864:                     push(@currlangs,$lang);
 4865:                 } elsif (defined($langchoices{$lang})) {
 4866:                     push(@currlangs,$lang);
 4867:                 } else {
 4868:                     next;
 4869:                 }
 4870:             }
 4871:         }
 4872:     }
 4873:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 4874:     if (@currlangs > 0) {
 4875:         foreach my $lang (@currlangs) {
 4876:             if (grep(/^\Q$lang\E$/,@delurls)) {
 4877:                 $changes{'helpurl'}{$lang} = 1;
 4878:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 4879:                 $changes{'helpurl'}{$lang} = 1;
 4880:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 4881:                 push(@newlangs,$lang);
 4882:             } else {
 4883:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4884:             }
 4885:         }
 4886:     }
 4887:     unless (grep(/^nolang$/,@currlangs)) {
 4888:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 4889:             $changes{'helpurl'}{'nolang'} = 1;
 4890:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 4891:             push(@newlangs,'nolang');
 4892:         }
 4893:     }
 4894:     if ($env{'form.loginhelpurl_add_lang'}) {
 4895:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 4896:             ($env{'form.loginhelpurl_add_file.filename'})) {
 4897:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 4898:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 4899:         }
 4900:     }
 4901:     if ((@newlangs > 0) || ($addedfile)) {
 4902:         my $error;
 4903:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 4904:         if ($configuserok eq 'ok') {
 4905:             if ($switchserver) {
 4906:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 4907:             } elsif ($author_ok eq 'ok') {
 4908:                 my @allnew = @newlangs;
 4909:                 if ($addedfile ne '') {
 4910:                     push(@allnew,$addedfile);
 4911:                 }
 4912:                 foreach my $lang (@allnew) {
 4913:                     my $formelem = 'loginhelpurl_'.$lang;
 4914:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 4915:                         $formelem = 'loginhelpurl_add_file';
 4916:                     }
 4917:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 4918:                                                                "help/$lang",'','',$newfile{$lang});
 4919:                     if ($result eq 'ok') {
 4920:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 4921:                         $changes{'helpurl'}{$lang} = 1;
 4922:                     } else {
 4923:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 4924:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 4925:                         if ((grep(/^\Q$lang\E$/,@currlangs)) && 
 4926:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 4927: 
 4928:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 4929:                         }
 4930:                     }
 4931:                 }
 4932:             } else {
 4933:                 $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);
 4934:             }
 4935:         } else {
 4936:             $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);
 4937:         }
 4938:         if ($error) {
 4939:             &Apache::lonnet::logthis($error);
 4940:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 4941:         }
 4942:     }
 4943:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 4944: 
 4945:     my $defaulthelpfile = '/adm/loginproblems.html';
 4946:     my $defaulttext = &mt('Default in use');
 4947: 
 4948:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 4949:                                              $dom);
 4950:     if ($putresult eq 'ok') {
 4951:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 4952:         my %defaultchecked = (
 4953:                     'coursecatalog' => 'on',
 4954:                     'helpdesk'      => 'on',
 4955:                     'adminmail'     => 'off',
 4956:                     'newuser'       => 'off',
 4957:         );
 4958:         if (ref($domconfig{'login'}) eq 'HASH') {
 4959:             foreach my $item (@toggles) {
 4960:                 if ($defaultchecked{$item} eq 'on') { 
 4961:                     if (($domconfig{'login'}{$item} eq '0') &&
 4962:                         ($env{'form.'.$item} eq '1')) {
 4963:                         $changes{$item} = 1;
 4964:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4965:                               $domconfig{'login'}{$item} eq '1') &&
 4966:                              ($env{'form.'.$item} eq '0')) {
 4967:                         $changes{$item} = 1;
 4968:                     }
 4969:                 } elsif ($defaultchecked{$item} eq 'off') {
 4970:                     if (($domconfig{'login'}{$item} eq '1') &&
 4971:                         ($env{'form.'.$item} eq '0')) {
 4972:                         $changes{$item} = 1;
 4973:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 4974:                               $domconfig{'login'}{$item} eq '0') &&
 4975:                              ($env{'form.'.$item} eq '1')) {
 4976:                         $changes{$item} = 1;
 4977:                     }
 4978:                 }
 4979:             }
 4980:         }
 4981:         if (keys(%changes) > 0 || $colchgtext) {
 4982:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 4983:             $$lastactref = 'update';
 4984:             $resulttext = &mt('Changes made:').'<ul>';
 4985:             foreach my $item (sort(keys(%changes))) {
 4986:                 if ($item eq 'loginvia') {
 4987:                     if (ref($changes{$item}) eq 'HASH') {
 4988:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 4989:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 4990:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 4991:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 4992:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 4993:                                     $protocol = 'http' if ($protocol ne 'https');
 4994:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 4995: 
 4996:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 4997:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 4998:                                     } else {
 4999:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 5000:                                     }
 5001:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 5002:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 5003:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 5004:                                     }
 5005:                                     $resulttext .= '</li>';
 5006:                                 } else {
 5007:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 5008:                                 }
 5009:                             } else {
 5010:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 5011:                             }
 5012:                         }
 5013:                         $resulttext .= '</ul></li>';
 5014:                     }
 5015:                 } elsif ($item eq 'helpurl') {
 5016:                     if (ref($changes{$item}) eq 'HASH') {
 5017:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 5018:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 5019:                                 my ($chg,$link);
 5020:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 5021:                                 if ($lang eq 'nolang') {
 5022:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 5023:                                 } else {
 5024:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 5025:                                 }
 5026:                                 $resulttext .= '<li>'.$chg.'</li>';
 5027:                             } else {
 5028:                                 my $chg;
 5029:                                 if ($lang eq 'nolang') {
 5030:                                     $chg = &mt('custom log-in help file for no preferred language');
 5031:                                 } else {
 5032:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 5033:                                 }
 5034:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 5035:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 5036:                                                       '?inhibitmenu=yes',$chg,600,500).
 5037:                                                '</li>';
 5038:                             }
 5039:                         }
 5040:                     }
 5041:                 } elsif ($item eq 'captcha') {
 5042:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5043:                         my $chgtxt; 
 5044:                         if ($loginhash{'login'}{$item} eq 'notused') {
 5045:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 5046:                         } else {
 5047:                             my %captchas = &captcha_phrases();
 5048:                             if ($captchas{$loginhash{'login'}{$item}}) {
 5049:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 5050:                             } else {
 5051:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 5052:                             }
 5053:                         }
 5054:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5055:                     }
 5056:                 } elsif ($item eq 'recaptchakeys') {
 5057:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5058:                         my ($privkey,$pubkey);
 5059:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 5060:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 5061:                             $privkey = $loginhash{'login'}{$item}{'private'};
 5062:                         }
 5063:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 5064:                         if (!$pubkey) {
 5065:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 5066:                         } else {
 5067:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 5068:                         }
 5069:                         if (!$privkey) {
 5070:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 5071:                         } else {
 5072:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 5073:                         }
 5074:                         $chgtxt .= '</ul>';
 5075:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5076:                     }
 5077:                 } else {
 5078:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 5079:                 }
 5080:             }
 5081:             $resulttext .= $colchgtext.'</ul>';
 5082:         } else {
 5083:             $resulttext = &mt('No changes made to log-in page settings');
 5084:         }
 5085:     } else {
 5086:         $resulttext = '<span class="LC_error">'.
 5087: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5088:     }
 5089:     if ($errors) {
 5090:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 5091:                        $errors.'</ul>';
 5092:     }
 5093:     return $resulttext;
 5094: }
 5095: 
 5096: sub color_font_choices {
 5097:     my %choices =
 5098:         &Apache::lonlocal::texthash (
 5099:             img => "Header",
 5100:             bgs => "Background colors",
 5101:             links => "Link colors",
 5102:             images => "Images",
 5103:             font => "Font color",
 5104:             fontmenu => "Font menu",
 5105:             pgbg => "Page",
 5106:             tabbg => "Header",
 5107:             sidebg => "Border",
 5108:             link => "Link",
 5109:             alink => "Active link",
 5110:             vlink => "Visited link",
 5111:         );
 5112:     return %choices;
 5113: }
 5114: 
 5115: sub modify_rolecolors {
 5116:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 5117:     my ($resulttext,%rolehash);
 5118:     $rolehash{'rolecolors'} = {};
 5119:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 5120:         if ($domconfig{'rolecolors'} eq '') {
 5121:             $domconfig{'rolecolors'} = {};
 5122:         }
 5123:     }
 5124:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 5125:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 5126:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 5127:                                              $dom);
 5128:     if ($putresult eq 'ok') {
 5129:         if (keys(%changes) > 0) {
 5130:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5131:             $$lastactref = 'update';
 5132:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 5133:                                              $rolehash{'rolecolors'});
 5134:         } else {
 5135:             $resulttext = &mt('No changes made to default color schemes');
 5136:         }
 5137:     } else {
 5138:         $resulttext = '<span class="LC_error">'.
 5139: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5140:     }
 5141:     if ($errors) {
 5142:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 5143:                        $errors.'</ul>';
 5144:     }
 5145:     return $resulttext;
 5146: }
 5147: 
 5148: sub modify_colors {
 5149:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 5150:     my (%changes,%choices);
 5151:     my @bgs;
 5152:     my @links = ('link','alink','vlink');
 5153:     my @logintext;
 5154:     my @images;
 5155:     my $servadm = $r->dir_config('lonAdmEMail');
 5156:     my $errors;
 5157:     my %defaults;
 5158:     foreach my $role (@{$roles}) {
 5159:         if ($role eq 'login') {
 5160:             %choices = &login_choices();
 5161:             @logintext = ('textcol','bgcol');
 5162:         } else {
 5163:             %choices = &color_font_choices();
 5164:         }
 5165:         if ($role eq 'login') {
 5166:             @images = ('img','logo','domlogo','login');
 5167:             @bgs = ('pgbg','mainbg','sidebg');
 5168:         } else {
 5169:             @images = ('img');
 5170:             @bgs = ('pgbg','tabbg','sidebg');
 5171:         }
 5172:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 5173:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 5174:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 5175:         }
 5176:         if ($role eq 'login') {
 5177:             foreach my $item (@logintext) {
 5178:                 unless ($env{'form.'.$role.'_'.$item} eq  $defaults{'logintext'}{$item}) {
 5179:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5180:                 }
 5181:             }
 5182:         } else {
 5183:             unless($env{'form.'.$role.'_fontmenu'} eq $defaults{'fontmenu'}) {
 5184:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 5185:             }
 5186:         }
 5187:         foreach my $item (@bgs) {
 5188:             unless ($env{'form.'.$role.'_'.$item} eq $defaults{'bgs'}{$item} ) {
 5189:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5190:             }
 5191:         }
 5192:         foreach my $item (@links) {
 5193:             unless ($env{'form.'.$role.'_'.$item} eq  $defaults{'links'}{$item}) {
 5194:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5195:             }
 5196:         }
 5197:         my ($configuserok,$author_ok,$switchserver) = 
 5198:             &config_check($dom,$confname,$servadm);
 5199:         my ($width,$height) = &thumb_dimensions();
 5200:         if (ref($domconfig->{$role}) ne 'HASH') {
 5201:             $domconfig->{$role} = {};
 5202:         }
 5203:         foreach my $img (@images) {
 5204:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 5205:                 if (defined($env{'form.login_showlogo_'.$img})) {
 5206:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 5207:                 } else { 
 5208:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 5209:                 }
 5210:             } 
 5211: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 5212: 		 && !defined($domconfig->{$role}{$img})
 5213: 		 && !$env{'form.'.$role.'_del_'.$img}
 5214: 		 && $env{'form.'.$role.'_import_'.$img}) {
 5215: 		# import the old configured image from the .tab setting
 5216: 		# if they haven't provided a new one 
 5217: 		$domconfig->{$role}{$img} = 
 5218: 		    $env{'form.'.$role.'_import_'.$img};
 5219: 	    }
 5220:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 5221:                 my $error;
 5222:                 if ($configuserok eq 'ok') {
 5223:                     if ($switchserver) {
 5224:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 5225:                     } else {
 5226:                         if ($author_ok eq 'ok') {
 5227:                             my ($result,$logourl) = 
 5228:                                 &publishlogo($r,'upload',$role.'_'.$img,
 5229:                                            $dom,$confname,$img,$width,$height);
 5230:                             if ($result eq 'ok') {
 5231:                                 $confhash->{$role}{$img} = $logourl;
 5232:                                 $changes{$role}{'images'}{$img} = 1;
 5233:                             } else {
 5234:                                 $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);
 5235:                             }
 5236:                         } else {
 5237:                             $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);
 5238:                         }
 5239:                     }
 5240:                 } else {
 5241:                     $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);
 5242:                 }
 5243:                 if ($error) {
 5244:                     &Apache::lonnet::logthis($error);
 5245:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5246:                 }
 5247:             } elsif ($domconfig->{$role}{$img} ne '') {
 5248:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 5249:                     my $error;
 5250:                     if ($configuserok eq 'ok') {
 5251: # is confname an author?
 5252:                         if ($switchserver eq '') {
 5253:                             if ($author_ok eq 'ok') {
 5254:                                 my ($result,$logourl) = 
 5255:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 5256:                                             $dom,$confname,$img,$width,$height);
 5257:                                 if ($result eq 'ok') {
 5258:                                     $confhash->{$role}{$img} = $logourl;
 5259: 				    $changes{$role}{'images'}{$img} = 1;
 5260:                                 }
 5261:                             }
 5262:                         }
 5263:                     }
 5264:                 }
 5265:             }
 5266:         }
 5267:         if (ref($domconfig) eq 'HASH') {
 5268:             if (ref($domconfig->{$role}) eq 'HASH') {
 5269:                 foreach my $img (@images) {
 5270:                     if ($domconfig->{$role}{$img} ne '') {
 5271:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5272:                             $confhash->{$role}{$img} = '';
 5273:                             $changes{$role}{'images'}{$img} = 1;
 5274:                         } else {
 5275:                             if ($confhash->{$role}{$img} eq '') {
 5276:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 5277:                             }
 5278:                         }
 5279:                     } else {
 5280:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5281:                             $confhash->{$role}{$img} = '';
 5282:                             $changes{$role}{'images'}{$img} = 1;
 5283:                         } 
 5284:                     }
 5285:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 5286:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 5287:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 5288:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 5289:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 5290:                             }
 5291:                         } else {
 5292:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5293:                                 $changes{$role}{'showlogo'}{$img} = 1;
 5294:                             }
 5295:                         }
 5296:                     }
 5297:                 }
 5298:                 if ($domconfig->{$role}{'font'} ne '') {
 5299:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 5300:                         $changes{$role}{'font'} = 1;
 5301:                     }
 5302:                 } else {
 5303:                     if ($confhash->{$role}{'font'}) {
 5304:                         $changes{$role}{'font'} = 1;
 5305:                     }
 5306:                 }
 5307:                 if ($role ne 'login') {
 5308:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 5309:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 5310:                             $changes{$role}{'fontmenu'} = 1;
 5311:                         }
 5312:                     } else {
 5313:                         if ($confhash->{$role}{'fontmenu'}) {
 5314:                             $changes{$role}{'fontmenu'} = 1;
 5315:                         }
 5316:                     }
 5317:                 }
 5318:                 foreach my $item (@bgs) {
 5319:                     if ($domconfig->{$role}{$item} ne '') {
 5320:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5321:                             $changes{$role}{'bgs'}{$item} = 1;
 5322:                         } 
 5323:                     } else {
 5324:                         if ($confhash->{$role}{$item}) {
 5325:                             $changes{$role}{'bgs'}{$item} = 1;
 5326:                         }
 5327:                     }
 5328:                 }
 5329:                 foreach my $item (@links) {
 5330:                     if ($domconfig->{$role}{$item} ne '') {
 5331:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5332:                             $changes{$role}{'links'}{$item} = 1;
 5333:                         }
 5334:                     } else {
 5335:                         if ($confhash->{$role}{$item}) {
 5336:                             $changes{$role}{'links'}{$item} = 1;
 5337:                         }
 5338:                     }
 5339:                 }
 5340:                 foreach my $item (@logintext) {
 5341:                     if ($domconfig->{$role}{$item} ne '') {
 5342:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5343:                             $changes{$role}{'logintext'}{$item} = 1;
 5344:                         }
 5345:                     } else {
 5346:                         if ($confhash->{$role}{$item}) {
 5347:                             $changes{$role}{'logintext'}{$item} = 1;
 5348:                         }
 5349:                     }
 5350:                 }
 5351:             } else {
 5352:                 &default_change_checker($role,\@images,\@links,\@bgs,
 5353:                                         \@logintext,$confhash,\%changes); 
 5354:             }
 5355:         } else {
 5356:             &default_change_checker($role,\@images,\@links,\@bgs,
 5357:                                     \@logintext,$confhash,\%changes); 
 5358:         }
 5359:     }
 5360:     return ($errors,%changes);
 5361: }
 5362: 
 5363: sub config_check {
 5364:     my ($dom,$confname,$servadm) = @_;
 5365:     my ($configuserok,$author_ok,$switchserver,%currroles);
 5366:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 5367:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 5368:                                                    $confname,$servadm);
 5369:     if ($configuserok eq 'ok') {
 5370:         $switchserver = &check_switchserver($dom,$confname);
 5371:         if ($switchserver eq '') {
 5372:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 5373:         }
 5374:     }
 5375:     return ($configuserok,$author_ok,$switchserver);
 5376: }
 5377: 
 5378: sub default_change_checker {
 5379:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 5380:     foreach my $item (@{$links}) {
 5381:         if ($confhash->{$role}{$item}) {
 5382:             $changes->{$role}{'links'}{$item} = 1;
 5383:         }
 5384:     }
 5385:     foreach my $item (@{$bgs}) {
 5386:         if ($confhash->{$role}{$item}) {
 5387:             $changes->{$role}{'bgs'}{$item} = 1;
 5388:         }
 5389:     }
 5390:     foreach my $item (@{$logintext}) {
 5391:         if ($confhash->{$role}{$item}) {
 5392:             $changes->{$role}{'logintext'}{$item} = 1;
 5393:         }
 5394:     }
 5395:     foreach my $img (@{$images}) {
 5396:         if ($env{'form.'.$role.'_del_'.$img}) {
 5397:             $confhash->{$role}{$img} = '';
 5398:             $changes->{$role}{'images'}{$img} = 1;
 5399:         }
 5400:         if ($role eq 'login') {
 5401:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5402:                 $changes->{$role}{'showlogo'}{$img} = 1;
 5403:             }
 5404:         }
 5405:     }
 5406:     if ($confhash->{$role}{'font'}) {
 5407:         $changes->{$role}{'font'} = 1;
 5408:     }
 5409: }
 5410: 
 5411: sub display_colorchgs {
 5412:     my ($dom,$changes,$roles,$confhash) = @_;
 5413:     my (%choices,$resulttext);
 5414:     if (!grep(/^login$/,@{$roles})) {
 5415:         $resulttext = &mt('Changes made:').'<br />';
 5416:     }
 5417:     foreach my $role (@{$roles}) {
 5418:         if ($role eq 'login') {
 5419:             %choices = &login_choices();
 5420:         } else {
 5421:             %choices = &color_font_choices();
 5422:         }
 5423:         if (ref($changes->{$role}) eq 'HASH') {
 5424:             if ($role ne 'login') {
 5425:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 5426:             }
 5427:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 5428:                 if ($role ne 'login') {
 5429:                     $resulttext .= '<ul>';
 5430:                 }
 5431:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 5432:                     if ($role ne 'login') {
 5433:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 5434:                     }
 5435:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 5436:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 5437:                             if ($confhash->{$role}{$key}{$item}) {
 5438:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 5439:                             } else {
 5440:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 5441:                             }
 5442:                         } elsif ($confhash->{$role}{$item} eq '') {
 5443:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 5444:                         } else {
 5445:                             my $newitem = $confhash->{$role}{$item};
 5446:                             if ($key eq 'images') {
 5447:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 5448:                             }
 5449:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 5450:                         }
 5451:                     }
 5452:                     if ($role ne 'login') {
 5453:                         $resulttext .= '</ul></li>';
 5454:                     }
 5455:                 } else {
 5456:                     if ($confhash->{$role}{$key} eq '') {
 5457:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 5458:                     } else {
 5459:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 5460:                     }
 5461:                 }
 5462:                 if ($role ne 'login') {
 5463:                     $resulttext .= '</ul>';
 5464:                 }
 5465:             }
 5466:         }
 5467:     }
 5468:     return $resulttext;
 5469: }
 5470: 
 5471: sub thumb_dimensions {
 5472:     return ('200','50');
 5473: }
 5474: 
 5475: sub check_dimensions {
 5476:     my ($inputfile) = @_;
 5477:     my ($fullwidth,$fullheight);
 5478:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 5479:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 5480:             my $imageinfo = <PIPE>;
 5481:             if (!close(PIPE)) {
 5482:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 5483:             }
 5484:             chomp($imageinfo);
 5485:             my ($fullsize) = 
 5486:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 5487:             if ($fullsize) {
 5488:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 5489:             }
 5490:         }
 5491:     }
 5492:     return ($fullwidth,$fullheight);
 5493: }
 5494: 
 5495: sub check_configuser {
 5496:     my ($uhome,$dom,$confname,$servadm) = @_;
 5497:     my ($configuserok,%currroles);
 5498:     if ($uhome eq 'no_host') {
 5499:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 5500:         my $configpass = &LONCAPA::Enrollment::create_password();
 5501:         $configuserok = 
 5502:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 5503:                              $configpass,'','','','','',undef,$servadm);
 5504:     } else {
 5505:         $configuserok = 'ok';
 5506:         %currroles = 
 5507:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 5508:     }
 5509:     return ($configuserok,%currroles);
 5510: }
 5511: 
 5512: sub check_authorstatus {
 5513:     my ($dom,$confname,%currroles) = @_;
 5514:     my $author_ok;
 5515:     if (!$currroles{':'.$dom.':au'}) {
 5516:         my $start = time;
 5517:         my $end = 0;
 5518:         $author_ok = 
 5519:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 5520:                                         'au',$end,$start,'','','domconfig');
 5521:     } else {
 5522:         $author_ok = 'ok';
 5523:     }
 5524:     return $author_ok;
 5525: }
 5526: 
 5527: sub publishlogo {
 5528:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 5529:     my ($output,$fname,$logourl);
 5530:     if ($action eq 'upload') {
 5531:         $fname=$env{'form.'.$formname.'.filename'};
 5532:         chop($env{'form.'.$formname});
 5533:     } else {
 5534:         ($fname) = ($formname =~ /([^\/]+)$/);
 5535:     }
 5536:     if ($savefileas ne '') {
 5537:         $fname = $savefileas;
 5538:     }
 5539:     $fname=&Apache::lonnet::clean_filename($fname);
 5540: # See if there is anything left
 5541:     unless ($fname) { return ('error: no uploaded file'); }
 5542:     $fname="$subdir/$fname";
 5543:     my $docroot=$r->dir_config('lonDocRoot'); 
 5544:     my $filepath="$docroot/priv";
 5545:     my $relpath = "$dom/$confname";
 5546:     my ($fnamepath,$file,$fetchthumb);
 5547:     $file=$fname;
 5548:     if ($fname=~m|/|) {
 5549:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 5550:     }
 5551:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 5552:     my $count;
 5553:     for ($count=5;$count<=$#parts;$count++) {
 5554:         $filepath.="/$parts[$count]";
 5555:         if ((-e $filepath)!=1) {
 5556:             mkdir($filepath,02770);
 5557:         }
 5558:     }
 5559:     # Check for bad extension and disallow upload
 5560:     if ($file=~/\.(\w+)$/ &&
 5561:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 5562:         $output = 
 5563:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 5564:     } elsif ($file=~/\.(\w+)$/ &&
 5565:         !defined(&Apache::loncommon::fileembstyle($1))) {
 5566:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 5567:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 5568:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 5569:     } elsif (-d "$filepath/$file") {
 5570:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 5571:     } else {
 5572:         my $source = $filepath.'/'.$file;
 5573:         my $logfile;
 5574:         if (!open($logfile,">>$source".'.log')) {
 5575:             return (&mt('No write permission to Authoring Space'));
 5576:         }
 5577:         print $logfile
 5578: "\n================= Publish ".localtime()." ================\n".
 5579: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 5580: # Save the file
 5581:         if (!open(FH,'>'.$source)) {
 5582:             &Apache::lonnet::logthis('Failed to create '.$source);
 5583:             return (&mt('Failed to create file'));
 5584:         }
 5585:         if ($action eq 'upload') {
 5586:             if (!print FH ($env{'form.'.$formname})) {
 5587:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 5588:                 return (&mt('Failed to write file'));
 5589:             }
 5590:         } else {
 5591:             my $original = &Apache::lonnet::filelocation('',$formname);
 5592:             if(!copy($original,$source)) {
 5593:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 5594:                 return (&mt('Failed to write file'));
 5595:             }
 5596:         }
 5597:         close(FH);
 5598:         chmod(0660, $source); # Permissions to rw-rw---.
 5599: 
 5600:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 5601:         my $copyfile=$targetdir.'/'.$file;
 5602: 
 5603:         my @parts=split(/\//,$targetdir);
 5604:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 5605:         for (my $count=5;$count<=$#parts;$count++) {
 5606:             $path.="/$parts[$count]";
 5607:             if (!-e $path) {
 5608:                 print $logfile "\nCreating directory ".$path;
 5609:                 mkdir($path,02770);
 5610:             }
 5611:         }
 5612:         my $versionresult;
 5613:         if (-e $copyfile) {
 5614:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 5615:         } else {
 5616:             $versionresult = 'ok';
 5617:         }
 5618:         if ($versionresult eq 'ok') {
 5619:             if (copy($source,$copyfile)) {
 5620:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 5621:                 $output = 'ok';
 5622:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 5623:                 push(@{$modified_urls},[$copyfile,$source]);
 5624:                 my $metaoutput = 
 5625:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 5626:                 unless ($registered_cleanup) {
 5627:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5628:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5629:                     $registered_cleanup=1;
 5630:                 }
 5631:             } else {
 5632:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 5633:                 $output = &mt('Failed to copy file to RES space').", $!";
 5634:             }
 5635:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 5636:                 my $inputfile = $filepath.'/'.$file;
 5637:                 my $outfile = $filepath.'/'.'tn-'.$file;
 5638:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 5639:                 if ($fullwidth ne '' && $fullheight ne '') { 
 5640:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 5641:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 5642:                         system("convert -sample $thumbsize $inputfile $outfile");
 5643:                         chmod(0660, $filepath.'/tn-'.$file);
 5644:                         if (-e $outfile) {
 5645:                             my $copyfile=$targetdir.'/tn-'.$file;
 5646:                             if (copy($outfile,$copyfile)) {
 5647:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 5648:                                 my $thumb_metaoutput = 
 5649:                                     &write_metadata($dom,$confname,$formname,
 5650:                                                     $targetdir,'tn-'.$file,$logfile);
 5651:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 5652:                                 unless ($registered_cleanup) {
 5653:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5654:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5655:                                     $registered_cleanup=1;
 5656:                                 }
 5657:                             } else {
 5658:                                 print $logfile "\nUnable to write ".$copyfile.
 5659:                                                ':'.$!."\n";
 5660:                             }
 5661:                         }
 5662:                     }
 5663:                 }
 5664:             }
 5665:         } else {
 5666:             $output = $versionresult;
 5667:         }
 5668:     }
 5669:     return ($output,$logourl);
 5670: }
 5671: 
 5672: sub logo_versioning {
 5673:     my ($targetdir,$file,$logfile) = @_;
 5674:     my $target = $targetdir.'/'.$file;
 5675:     my ($maxversion,$fn,$extn,$output);
 5676:     $maxversion = 0;
 5677:     if ($file =~ /^(.+)\.(\w+)$/) {
 5678:         $fn=$1;
 5679:         $extn=$2;
 5680:     }
 5681:     opendir(DIR,$targetdir);
 5682:     while (my $filename=readdir(DIR)) {
 5683:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 5684:             $maxversion=($1>$maxversion)?$1:$maxversion;
 5685:         }
 5686:     }
 5687:     $maxversion++;
 5688:     print $logfile "\nCreating old version ".$maxversion."\n";
 5689:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 5690:     if (copy($target,$copyfile)) {
 5691:         print $logfile "Copied old target to ".$copyfile."\n";
 5692:         $copyfile=$copyfile.'.meta';
 5693:         if (copy($target.'.meta',$copyfile)) {
 5694:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 5695:             $output = 'ok';
 5696:         } else {
 5697:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 5698:             $output = &mt('Failed to copy old meta').", $!, ";
 5699:         }
 5700:     } else {
 5701:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 5702:         $output = &mt('Failed to copy old target').", $!, ";
 5703:     }
 5704:     return $output;
 5705: }
 5706: 
 5707: sub write_metadata {
 5708:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 5709:     my (%metadatafields,%metadatakeys,$output);
 5710:     $metadatafields{'title'}=$formname;
 5711:     $metadatafields{'creationdate'}=time;
 5712:     $metadatafields{'lastrevisiondate'}=time;
 5713:     $metadatafields{'copyright'}='public';
 5714:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 5715:                                          $env{'user.domain'};
 5716:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 5717:     $metadatafields{'domain'}=$dom;
 5718:     {
 5719:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 5720:         my $mfh;
 5721:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 5722:             foreach (sort(keys(%metadatafields))) {
 5723:                 unless ($_=~/\./) {
 5724:                     my $unikey=$_;
 5725:                     $unikey=~/^([A-Za-z]+)/;
 5726:                     my $tag=$1;
 5727:                     $tag=~tr/A-Z/a-z/;
 5728:                     print $mfh "\n\<$tag";
 5729:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 5730:                         my $value=$metadatafields{$unikey.'.'.$_};
 5731:                         $value=~s/\"/\'\'/g;
 5732:                         print $mfh ' '.$_.'="'.$value.'"';
 5733:                     }
 5734:                     print $mfh '>'.
 5735:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 5736:                             .'</'.$tag.'>';
 5737:                 }
 5738:             }
 5739:             $output = 'ok';
 5740:             print $logfile "\nWrote metadata";
 5741:             close($mfh);
 5742:         } else {
 5743:             print $logfile "\nFailed to open metadata file";
 5744:             $output = &mt('Could not write metadata');
 5745:         }
 5746:     }
 5747:     return $output;
 5748: }
 5749: 
 5750: sub notifysubscribed {
 5751:     foreach my $targetsource (@{$modified_urls}){
 5752:         next unless (ref($targetsource) eq 'ARRAY');
 5753:         my ($target,$source)=@{$targetsource};
 5754:         if ($source ne '') {
 5755:             if (open(my $logfh,'>>'.$source.'.log')) {
 5756:                 print $logfh "\nCleanup phase: Notifications\n";
 5757:                 my @subscribed=&subscribed_hosts($target);
 5758:                 foreach my $subhost (@subscribed) {
 5759:                     print $logfh "\nNotifying host ".$subhost.':';
 5760:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 5761:                     print $logfh $reply;
 5762:                 }
 5763:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 5764:                 foreach my $subhost (@subscribedmeta) {
 5765:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 5766:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 5767:                                                         $subhost);
 5768:                     print $logfh $reply;
 5769:                 }
 5770:                 print $logfh "\n============ Done ============\n";
 5771:                 close($logfh);
 5772:             }
 5773:         }
 5774:     }
 5775:     return OK;
 5776: }
 5777: 
 5778: sub subscribed_hosts {
 5779:     my ($target) = @_;
 5780:     my @subscribed;
 5781:     if (open(my $fh,"<$target.subscription")) {
 5782:         while (my $subline=<$fh>) {
 5783:             if ($subline =~ /^($match_lonid):/) {
 5784:                 my $host = $1;
 5785:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 5786:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 5787:                         push(@subscribed,$host);
 5788:                     }
 5789:                 }
 5790:             }
 5791:         }
 5792:     }
 5793:     return @subscribed;
 5794: }
 5795: 
 5796: sub check_switchserver {
 5797:     my ($dom,$confname) = @_;
 5798:     my ($allowed,$switchserver);
 5799:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 5800:     if ($home eq 'no_host') {
 5801:         $home = &Apache::lonnet::domain($dom,'primary');
 5802:     }
 5803:     my @ids=&Apache::lonnet::current_machine_ids();
 5804:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 5805:     if (!$allowed) {
 5806: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 5807:     }
 5808:     return $switchserver;
 5809: }
 5810: 
 5811: sub modify_quotas {
 5812:     my ($dom,$action,%domconfig) = @_;
 5813:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 5814:         %limithash,$toolregexp,%conditions,$resulttext,%changes);
 5815:     if ($action eq 'quotas') {
 5816:         $context = 'tools'; 
 5817:     } else {
 5818:         $context = $action;
 5819:     }
 5820:     if ($context eq 'requestcourses') {
 5821:         @usertools = ('official','unofficial','community');
 5822:         @options =('norequest','approval','validate','autolimit');
 5823:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 5824:         %titles = &courserequest_titles();
 5825:         $toolregexp = join('|',@usertools);
 5826:         %conditions = &courserequest_conditions();
 5827:     } elsif ($context eq 'requestauthor') {
 5828:         @usertools = ('author');
 5829:         %titles = &authorrequest_titles();
 5830:     } else {
 5831:         @usertools = ('aboutme','blog','webdav','portfolio');
 5832:         %titles = &tool_titles();
 5833:     }
 5834:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5835:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5836:     foreach my $key (keys(%env)) {
 5837:         if ($context eq 'requestcourses') {
 5838:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 5839:                 my $item = $1;
 5840:                 my $type = $2;
 5841:                 if ($type =~ /^limit_(.+)/) {
 5842:                     $limithash{$item}{$1} = $env{$key};
 5843:                 } else {
 5844:                     $confhash{$item}{$type} = $env{$key};
 5845:                 }
 5846:             }
 5847:         } elsif ($context eq 'requestauthor') {
 5848:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 5849:                 $confhash{$1} = $env{$key};
 5850:             }
 5851:         } else {
 5852:             if ($key =~ /^form\.quota_(.+)$/) {
 5853:                 $confhash{'defaultquota'}{$1} = $env{$key};
 5854:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 5855:                 $confhash{'authorquota'}{$1} = $env{$key};
 5856:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 5857:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 5858:             }
 5859:         }
 5860:     }
 5861:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5862:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.reqapprovalnotify');
 5863:         @approvalnotify = sort(@approvalnotify);
 5864:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 5865:         if (ref($domconfig{$action}) eq 'HASH') {
 5866:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 5867:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 5868:                     $changes{'notify'}{'approval'} = 1;
 5869:                 }
 5870:             } else {
 5871:                 if ($confhash{'notify'}{'approval'}) {
 5872:                     $changes{'notify'}{'approval'} = 1;
 5873:                 }
 5874:             }
 5875:         } else {
 5876:             if ($confhash{'notify'}{'approval'}) {
 5877:                 $changes{'notify'}{'approval'} = 1;
 5878:             }
 5879:         }
 5880:     } else {
 5881:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 5882:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 5883:     }
 5884:     foreach my $item (@usertools) {
 5885:         foreach my $type (@{$types},'default','_LC_adv') {
 5886:             my $unset; 
 5887:             if ($context eq 'requestcourses') {
 5888:                 $unset = '0';
 5889:                 if ($type eq '_LC_adv') {
 5890:                     $unset = '';
 5891:                 }
 5892:                 if ($confhash{$item}{$type} eq 'autolimit') {
 5893:                     $confhash{$item}{$type} .= '=';
 5894:                     unless ($limithash{$item}{$type} =~ /\D/) {
 5895:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 5896:                     }
 5897:                 }
 5898:             } elsif ($context eq 'requestauthor') {
 5899:                 $unset = '0';
 5900:                 if ($type eq '_LC_adv') {
 5901:                     $unset = '';
 5902:                 }
 5903:             } else {
 5904:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 5905:                     $confhash{$item}{$type} = 1;
 5906:                 } else {
 5907:                     $confhash{$item}{$type} = 0;
 5908:                 }
 5909:             }
 5910:             if (ref($domconfig{$action}) eq 'HASH') {
 5911:                 if ($action eq 'requestauthor') {
 5912:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 5913:                         $changes{$type} = 1;
 5914:                     }
 5915:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 5916:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 5917:                         $changes{$item}{$type} = 1;
 5918:                     }
 5919:                 } else {
 5920:                     if ($context eq 'requestcourses') {
 5921:                         if ($confhash{$item}{$type} ne $unset) {
 5922:                             $changes{$item}{$type} = 1;
 5923:                         }
 5924:                     } else {
 5925:                         if (!$confhash{$item}{$type}) {
 5926:                             $changes{$item}{$type} = 1;
 5927:                         }
 5928:                     }
 5929:                 }
 5930:             } else {
 5931:                 if ($context eq 'requestcourses') {
 5932:                     if ($confhash{$item}{$type} ne $unset) {
 5933:                         $changes{$item}{$type} = 1;
 5934:                     }
 5935:                 } elsif ($context eq 'requestauthor') {
 5936:                     if ($confhash{$type} ne $unset) {
 5937:                         $changes{$type} = 1;
 5938:                     }
 5939:                 } else {
 5940:                     if (!$confhash{$item}{$type}) {
 5941:                         $changes{$item}{$type} = 1;
 5942:                     }
 5943:                 }
 5944:             }
 5945:         }
 5946:     }
 5947:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 5948:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 5949:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5950:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 5951:                     if (exists($confhash{'defaultquota'}{$key})) {
 5952:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 5953:                             $changes{'defaultquota'}{$key} = 1;
 5954:                         }
 5955:                     } else {
 5956:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 5957:                     }
 5958:                 }
 5959:             } else {
 5960:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 5961:                     if (exists($confhash{'defaultquota'}{$key})) {
 5962:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 5963:                             $changes{'defaultquota'}{$key} = 1;
 5964:                         }
 5965:                     } else {
 5966:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 5967:                     }
 5968:                 }
 5969:             }
 5970:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 5971:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 5972:                     if (exists($confhash{'authorquota'}{$key})) {
 5973:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 5974:                             $changes{'authorquota'}{$key} = 1;
 5975:                         }
 5976:                     } else {
 5977:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 5978:                     }
 5979:                 }
 5980:             }
 5981:         }
 5982:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 5983:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 5984:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 5985:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 5986:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 5987:                             $changes{'defaultquota'}{$key} = 1;
 5988:                         }
 5989:                     } else {
 5990:                         if (!exists($domconfig{'quotas'}{$key})) {
 5991:                             $changes{'defaultquota'}{$key} = 1;
 5992:                         }
 5993:                     }
 5994:                 } else {
 5995:                     $changes{'defaultquota'}{$key} = 1;
 5996:                 }
 5997:             }
 5998:         }
 5999:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 6000:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 6001:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 6002:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 6003:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 6004:                             $changes{'authorquota'}{$key} = 1;
 6005:                         }
 6006:                     } else {
 6007:                         $changes{'authorquota'}{$key} = 1;
 6008:                     }
 6009:                 } else {
 6010:                     $changes{'authorquota'}{$key} = 1;
 6011:                 }
 6012:             }
 6013:         }
 6014:     }
 6015: 
 6016:     if ($context eq 'requestauthor') {
 6017:         $domdefaults{'requestauthor'} = \%confhash;
 6018:     } else {
 6019:         foreach my $key (keys(%confhash)) {
 6020:             $domdefaults{$key} = $confhash{$key};
 6021:         }
 6022:     }
 6023: 
 6024:     my %quotahash = (
 6025:                       $action => { %confhash }
 6026:                     );
 6027:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 6028:                                              $dom);
 6029:     if ($putresult eq 'ok') {
 6030:         if (keys(%changes) > 0) {
 6031:             my $cachetime = 24*60*60;
 6032:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 6033: 
 6034:             $resulttext = &mt('Changes made:').'<ul>';
 6035:             unless (($context eq 'requestcourses') || 
 6036:                     ($context eq 'requestauthor')) {
 6037:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 6038:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 6039:                     foreach my $type (@{$types},'default') {
 6040:                         if (defined($changes{'defaultquota'}{$type})) {
 6041:                             my $typetitle = $usertypes->{$type};
 6042:                             if ($type eq 'default') {
 6043:                                 $typetitle = $othertitle;
 6044:                             }
 6045:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] Mb',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 6046:                         }
 6047:                     }
 6048:                     $resulttext .= '</ul></li>';
 6049:                 }
 6050:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 6051:                     $resulttext .= '<li>'.&mt('Authoring space default quotas').'<ul>';
 6052:                     foreach my $type (@{$types},'default') {
 6053:                         if (defined($changes{'authorquota'}{$type})) {
 6054:                             my $typetitle = $usertypes->{$type};
 6055:                             if ($type eq 'default') {
 6056:                                 $typetitle = $othertitle;
 6057:                             }
 6058:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] Mb',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 6059:                         }
 6060:                     }
 6061:                     $resulttext .= '</ul></li>';
 6062:                 }
 6063:             }
 6064:             my %newenv;
 6065:             foreach my $item (@usertools) {
 6066:                 my (%haschgs,%inconf);
 6067:                 if ($context eq 'requestauthor') {
 6068:                     %haschgs = %changes;
 6069:                     %inconf = %confhash; 
 6070:                 } else {
 6071:                     if (ref($changes{$item}) eq 'HASH') {
 6072:                         %haschgs = %{$changes{$item}};
 6073:                     }
 6074:                     if (ref($confhash{$item}) eq 'HASH') {
 6075:                         %inconf = %{$confhash{$item}};
 6076:                     }
 6077:                 }
 6078:                 if (keys(%haschgs) > 0) {
 6079:                     my $newacc = 
 6080:                         &Apache::lonnet::usertools_access($env{'user.name'},
 6081:                                                           $env{'user.domain'},
 6082:                                                           $item,'reload',$context);
 6083:                     if (($context eq 'requestcourses') || 
 6084:                         ($context eq 'requestauthor')) {
 6085:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 6086:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 6087:                         }
 6088:                     } else {
 6089:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 6090:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 6091:                         }
 6092:                     }
 6093:                     unless ($context eq 'requestauthor') {
 6094:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 6095:                     }
 6096:                     foreach my $type (@{$types},'default','_LC_adv') {
 6097:                         if ($haschgs{$type}) {
 6098:                             my $typetitle = $usertypes->{$type};
 6099:                             if ($type eq 'default') {
 6100:                                 $typetitle = $othertitle;
 6101:                             } elsif ($type eq '_LC_adv') {
 6102:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 6103:                             }
 6104:                             if ($inconf{$type}) {
 6105:                                 if ($context eq 'requestcourses') {
 6106:                                     my $cond;
 6107:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 6108:                                         if ($1 eq '') {
 6109:                                             $cond = &mt('(Automatic processing of any request).');
 6110:                                         } else {
 6111:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 6112:                                         }
 6113:                                     } else { 
 6114:                                         $cond = $conditions{$inconf{$type}};
 6115:                                     }
 6116:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 6117:                                 } elsif ($context eq 'requestauthor') {
 6118:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 6119:                                                              $titles{$inconf{$type}},$typetitle);
 6120: 
 6121:                                 } else {
 6122:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 6123:                                 }
 6124:                             } else {
 6125:                                 if ($type eq '_LC_adv') {
 6126:                                     if ($inconf{$type} eq '0') {
 6127:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 6128:                                     } else { 
 6129:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 6130:                                     }
 6131:                                 } else {
 6132:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 6133:                                 }
 6134:                             }
 6135:                         }
 6136:                     }
 6137:                     unless ($context eq 'requestauthor') {
 6138:                         $resulttext .= '</ul></li>';
 6139:                     }
 6140:                 }
 6141:             }
 6142:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 6143:                 if (ref($changes{'notify'}) eq 'HASH') {
 6144:                     if ($changes{'notify'}{'approval'}) {
 6145:                         if (ref($confhash{'notify'}) eq 'HASH') {
 6146:                             if ($confhash{'notify'}{'approval'}) {
 6147:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 6148:                             } else {
 6149:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 6150:                             }
 6151:                         }
 6152:                     }
 6153:                 }
 6154:             }
 6155:             $resulttext .= '</ul>';
 6156:             if (keys(%newenv)) {
 6157:                 &Apache::lonnet::appenv(\%newenv);
 6158:             }
 6159:         } else {
 6160:             if ($context eq 'requestcourses') {
 6161:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 6162:             } elsif ($context eq 'requestauthor') {
 6163:                 $resulttext = &mt('No changes made to rights to request author space.');
 6164:             } else {
 6165:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 6166:             }
 6167:         }
 6168:     } else {
 6169:         $resulttext = '<span class="LC_error">'.
 6170: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6171:     }
 6172:     return $resulttext;
 6173: }
 6174: 
 6175: sub modify_autoenroll {
 6176:     my ($dom,$lastactref,%domconfig) = @_;
 6177:     my ($resulttext,%changes);
 6178:     my %currautoenroll;
 6179:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 6180:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 6181:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 6182:         }
 6183:     }
 6184:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 6185:     my %title = ( run => 'Auto-enrollment active',
 6186:                   sender => 'Sender for notification messages',
 6187:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 6188:     my @offon = ('off','on');
 6189:     my $sender_uname = $env{'form.sender_uname'};
 6190:     my $sender_domain = $env{'form.sender_domain'};
 6191:     if ($sender_domain eq '') {
 6192:         $sender_uname = '';
 6193:     } elsif ($sender_uname eq '') {
 6194:         $sender_domain = '';
 6195:     }
 6196:     my $coowners = $env{'form.autoassign_coowners'};
 6197:     my %autoenrollhash =  (
 6198:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 6199:                                        'sender_uname' => $sender_uname,
 6200:                                        'sender_domain' => $sender_domain,
 6201:                                        'co-owners' => $coowners,
 6202:                                 }
 6203:                      );
 6204:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 6205:                                              $dom);
 6206:     if ($putresult eq 'ok') {
 6207:         if (exists($currautoenroll{'run'})) {
 6208:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 6209:                  $changes{'run'} = 1;
 6210:              }
 6211:         } elsif ($autorun) {
 6212:             if ($env{'form.autoenroll_run'} ne '1') {
 6213:                  $changes{'run'} = 1;
 6214:             }
 6215:         }
 6216:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 6217:             $changes{'sender'} = 1;
 6218:         }
 6219:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 6220:             $changes{'sender'} = 1;
 6221:         }
 6222:         if ($currautoenroll{'co-owners'} ne '') {
 6223:             if ($currautoenroll{'co-owners'} ne $coowners) {
 6224:                 $changes{'coowners'} = 1;
 6225:             }
 6226:         } elsif ($coowners) {
 6227:             $changes{'coowners'} = 1;
 6228:         }      
 6229:         if (keys(%changes) > 0) {
 6230:             $resulttext = &mt('Changes made:').'<ul>';
 6231:             if ($changes{'run'}) {
 6232:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 6233:             }
 6234:             if ($changes{'sender'}) {
 6235:                 if ($sender_uname eq '' || $sender_domain eq '') {
 6236:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 6237:                 } else {
 6238:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 6239:                 }
 6240:             }
 6241:             if ($changes{'coowners'}) {
 6242:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 6243:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 6244:                 $$lastactref = 'update';
 6245:             }
 6246:             $resulttext .= '</ul>';
 6247:         } else {
 6248:             $resulttext = &mt('No changes made to auto-enrollment settings');
 6249:         }
 6250:     } else {
 6251:         $resulttext = '<span class="LC_error">'.
 6252: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6253:     }
 6254:     return $resulttext;
 6255: }
 6256: 
 6257: sub modify_autoupdate {
 6258:     my ($dom,%domconfig) = @_;
 6259:     my ($resulttext,%currautoupdate,%fields,%changes);
 6260:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 6261:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 6262:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 6263:         }
 6264:     }
 6265:     my @offon = ('off','on');
 6266:     my %title = &Apache::lonlocal::texthash (
 6267:                    run => 'Auto-update:',
 6268:                    classlists => 'Updates to user information in classlists?'
 6269:                 );
 6270:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6271:     my %fieldtitles = &Apache::lonlocal::texthash (
 6272:                         id => 'Student/Employee ID',
 6273:                         permanentemail => 'E-mail address',
 6274:                         lastname => 'Last Name',
 6275:                         firstname => 'First Name',
 6276:                         middlename => 'Middle Name',
 6277:                         generation => 'Generation',
 6278:                       );
 6279:     $othertitle = &mt('All users');
 6280:     if (keys(%{$usertypes}) >  0) {
 6281:         $othertitle = &mt('Other users');
 6282:     }
 6283:     foreach my $key (keys(%env)) {
 6284:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 6285:             my ($usertype,$item) = ($1,$2);
 6286:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 6287:                 if ($usertype eq 'default') {   
 6288:                     push(@{$fields{$1}},$2);
 6289:                 } elsif (ref($types) eq 'ARRAY') {
 6290:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 6291:                         push(@{$fields{$1}},$2);
 6292:                     }
 6293:                 }
 6294:             }
 6295:         }
 6296:     }
 6297:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 6298:     @lockablenames = sort(@lockablenames);
 6299:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 6300:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6301:         if (@changed) {
 6302:             $changes{'lockablenames'} = 1;
 6303:         }
 6304:     } else {
 6305:         if (@lockablenames) {
 6306:             $changes{'lockablenames'} = 1;
 6307:         }
 6308:     }
 6309:     my %updatehash = (
 6310:                       autoupdate => { run => $env{'form.autoupdate_run'},
 6311:                                       classlists => $env{'form.classlists'},
 6312:                                       fields => {%fields},
 6313:                                       lockablenames => \@lockablenames,
 6314:                                     }
 6315:                      );
 6316:     foreach my $key (keys(%currautoupdate)) {
 6317:         if (($key eq 'run') || ($key eq 'classlists')) {
 6318:             if (exists($updatehash{autoupdate}{$key})) {
 6319:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 6320:                     $changes{$key} = 1;
 6321:                 }
 6322:             }
 6323:         } elsif ($key eq 'fields') {
 6324:             if (ref($currautoupdate{$key}) eq 'HASH') {
 6325:                 foreach my $item (@{$types},'default') {
 6326:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 6327:                         my $change = 0;
 6328:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 6329:                             if (!exists($fields{$item})) {
 6330:                                 $change = 1;
 6331:                                 last;
 6332:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 6333:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 6334:                                     $change = 1;
 6335:                                     last;
 6336:                                 }
 6337:                             }
 6338:                         }
 6339:                         if ($change) {
 6340:                             push(@{$changes{$key}},$item);
 6341:                         }
 6342:                     } 
 6343:                 }
 6344:             }
 6345:         } elsif ($key eq 'lockablenames') {
 6346:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 6347:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6348:                 if (@changed) {
 6349:                     $changes{'lockablenames'} = 1;
 6350:                 }
 6351:             } else {
 6352:                 if (@lockablenames) {
 6353:                     $changes{'lockablenames'} = 1;
 6354:                 }
 6355:             }
 6356:         }
 6357:     }
 6358:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 6359:         if (@lockablenames) {
 6360:             $changes{'lockablenames'} = 1;
 6361:         }
 6362:     }
 6363:     foreach my $item (@{$types},'default') {
 6364:         if (defined($fields{$item})) {
 6365:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 6366:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 6367:                     my $change = 0;
 6368:                     if (ref($fields{$item}) eq 'ARRAY') {
 6369:                         foreach my $type (@{$fields{$item}}) {
 6370:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 6371:                                 $change = 1;
 6372:                                 last;
 6373:                             }
 6374:                         }
 6375:                     }
 6376:                     if ($change) {
 6377:                         push(@{$changes{'fields'}},$item);
 6378:                     }
 6379:                 } else {
 6380:                     push(@{$changes{'fields'}},$item);
 6381:                 }
 6382:             } else {
 6383:                 push(@{$changes{'fields'}},$item);
 6384:             }
 6385:         }
 6386:     }
 6387:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 6388:                                              $dom);
 6389:     if ($putresult eq 'ok') {
 6390:         if (keys(%changes) > 0) {
 6391:             $resulttext = &mt('Changes made:').'<ul>';
 6392:             foreach my $key (sort(keys(%changes))) {
 6393:                 if ($key eq 'lockablenames') {
 6394:                     $resulttext .= '<li>';
 6395:                     if (@lockablenames) {
 6396:                         $usertypes->{'default'} = $othertitle;
 6397:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 6398:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 6399:                     } else {
 6400:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 6401:                     }
 6402:                     $resulttext .= '</li>';
 6403:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 6404:                     foreach my $item (@{$changes{$key}}) {
 6405:                         my @newvalues;
 6406:                         foreach my $type (@{$fields{$item}}) {
 6407:                             push(@newvalues,$fieldtitles{$type});
 6408:                         }
 6409:                         my $newvaluestr;
 6410:                         if (@newvalues > 0) {
 6411:                             $newvaluestr = join(', ',@newvalues);
 6412:                         } else {
 6413:                             $newvaluestr = &mt('none');
 6414:                         }
 6415:                         if ($item eq 'default') {
 6416:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 6417:                         } else {
 6418:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 6419:                         }
 6420:                     }
 6421:                 } else {
 6422:                     my $newvalue;
 6423:                     if ($key eq 'run') {
 6424:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 6425:                     } else {
 6426:                         $newvalue = $offon[$env{'form.'.$key}];
 6427:                     }
 6428:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 6429:                 }
 6430:             }
 6431:             $resulttext .= '</ul>';
 6432:         } else {
 6433:             $resulttext = &mt('No changes made to autoupdates');
 6434:         }
 6435:     } else {
 6436:         $resulttext = '<span class="LC_error">'.
 6437: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6438:     }
 6439:     return $resulttext;
 6440: }
 6441: 
 6442: sub modify_autocreate {
 6443:     my ($dom,%domconfig) = @_;
 6444:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 6445:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 6446:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 6447:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 6448:         }
 6449:     }
 6450:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 6451:                  req => 'Auto-creation of validated requests for official courses',
 6452:                  xmldc => 'Identity of course creator of courses from XML files',
 6453:                );
 6454:     my @types = ('xml','req');
 6455:     foreach my $item (@types) {
 6456:         $newvals{$item} = $env{'form.autocreate_'.$item};
 6457:         $newvals{$item} =~ s/\D//g;
 6458:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 6459:     }
 6460:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 6461:     my %domcoords = &get_active_dcs($dom);
 6462:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 6463:         $newvals{'xmldc'} = '';
 6464:     } 
 6465:     %autocreatehash =  (
 6466:                         autocreate => { xml => $newvals{'xml'},
 6467:                                         req => $newvals{'req'},
 6468:                                       }
 6469:                        );
 6470:     if ($newvals{'xmldc'} ne '') {
 6471:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 6472:     }
 6473:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 6474:                                              $dom);
 6475:     if ($putresult eq 'ok') {
 6476:         my @items = @types;
 6477:         if ($newvals{'xml'}) {
 6478:             push(@items,'xmldc');
 6479:         }
 6480:         foreach my $item (@items) {
 6481:             if (exists($currautocreate{$item})) {
 6482:                 if ($currautocreate{$item} ne $newvals{$item}) {
 6483:                     $changes{$item} = 1;
 6484:                 }
 6485:             } elsif ($newvals{$item}) {
 6486:                 $changes{$item} = 1;
 6487:             }
 6488:         }
 6489:         if (keys(%changes) > 0) {
 6490:             my @offon = ('off','on'); 
 6491:             $resulttext = &mt('Changes made:').'<ul>';
 6492:             foreach my $item (@types) {
 6493:                 if ($changes{$item}) {
 6494:                     my $newtxt = $offon[$newvals{$item}];
 6495:                     $resulttext .= '<li>'.
 6496:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 6497:                                        '<b>','</b>').
 6498:                                    '</li>';
 6499:                 }
 6500:             }
 6501:             if ($changes{'xmldc'}) {
 6502:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 6503:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 6504:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 6505:             }
 6506:             $resulttext .= '</ul>';
 6507:         } else {
 6508:             $resulttext = &mt('No changes made to auto-creation settings');
 6509:         }
 6510:     } else {
 6511:         $resulttext = '<span class="LC_error">'.
 6512:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6513:     }
 6514:     return $resulttext;
 6515: }
 6516: 
 6517: sub modify_directorysrch {
 6518:     my ($dom,%domconfig) = @_;
 6519:     my ($resulttext,%changes);
 6520:     my %currdirsrch;
 6521:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 6522:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 6523:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 6524:         }
 6525:     }
 6526:     my %title = ( available => 'Directory search available',
 6527:                   localonly => 'Other domains can search',
 6528:                   searchby => 'Search types',
 6529:                   searchtypes => 'Search latitude');
 6530:     my @offon = ('off','on');
 6531:     my @otherdoms = ('Yes','No');
 6532: 
 6533:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 6534:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 6535:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 6536: 
 6537:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6538:     if (keys(%{$usertypes}) == 0) {
 6539:         @cansearch = ('default');
 6540:     } else {
 6541:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 6542:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 6543:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 6544:                     push(@{$changes{'cansearch'}},$type);
 6545:                 }
 6546:             }
 6547:             foreach my $type (@cansearch) {
 6548:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 6549:                     push(@{$changes{'cansearch'}},$type);
 6550:                 }
 6551:             }
 6552:         } else {
 6553:             push(@{$changes{'cansearch'}},@cansearch);
 6554:         }
 6555:     }
 6556: 
 6557:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 6558:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 6559:             if (!grep(/^\Q$by\E$/,@searchby)) {
 6560:                 push(@{$changes{'searchby'}},$by);
 6561:             }
 6562:         }
 6563:         foreach my $by (@searchby) {
 6564:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 6565:                 push(@{$changes{'searchby'}},$by);
 6566:             }
 6567:         }
 6568:     } else {
 6569:         push(@{$changes{'searchby'}},@searchby);
 6570:     }
 6571: 
 6572:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 6573:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 6574:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 6575:                 push(@{$changes{'searchtypes'}},$type);
 6576:             }
 6577:         }
 6578:         foreach my $type (@searchtypes) {
 6579:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 6580:                 push(@{$changes{'searchtypes'}},$type);
 6581:             }
 6582:         }
 6583:     } else {
 6584:         if (exists($currdirsrch{'searchtypes'})) {
 6585:             foreach my $type (@searchtypes) {  
 6586:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 6587:                     push(@{$changes{'searchtypes'}},$type);
 6588:                 }
 6589:             }
 6590:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 6591:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 6592:             }   
 6593:         } else {
 6594:             push(@{$changes{'searchtypes'}},@searchtypes); 
 6595:         }
 6596:     }
 6597: 
 6598:     my %dirsrch_hash =  (
 6599:             directorysrch => { available => $env{'form.dirsrch_available'},
 6600:                                cansearch => \@cansearch,
 6601:                                localonly => $env{'form.dirsrch_localonly'},
 6602:                                searchby => \@searchby,
 6603:                                searchtypes => \@searchtypes,
 6604:                              }
 6605:             );
 6606:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 6607:                                              $dom);
 6608:     if ($putresult eq 'ok') {
 6609:         if (exists($currdirsrch{'available'})) {
 6610:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 6611:                  $changes{'available'} = 1;
 6612:              }
 6613:         } else {
 6614:             if ($env{'form.dirsrch_available'} eq '1') {
 6615:                 $changes{'available'} = 1;
 6616:             }
 6617:         }
 6618:         if (exists($currdirsrch{'localonly'})) {
 6619:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 6620:                  $changes{'localonly'} = 1;
 6621:              }
 6622:         } else {
 6623:             if ($env{'form.dirsrch_localonly'} eq '1') {
 6624:                 $changes{'localonly'} = 1;
 6625:             }
 6626:         }
 6627:         if (keys(%changes) > 0) {
 6628:             $resulttext = &mt('Changes made:').'<ul>';
 6629:             if ($changes{'available'}) {
 6630:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 6631:             }
 6632:             if ($changes{'localonly'}) {
 6633:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 6634:             }
 6635: 
 6636:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 6637:                 my $chgtext;
 6638:                 if (ref($usertypes) eq 'HASH') {
 6639:                     if (keys(%{$usertypes}) > 0) {
 6640:                         foreach my $type (@{$types}) {
 6641:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 6642:                                 $chgtext .= $usertypes->{$type}.'; ';
 6643:                             }
 6644:                         }
 6645:                         if (grep(/^default$/,@cansearch)) {
 6646:                             $chgtext .= $othertitle;
 6647:                         } else {
 6648:                             $chgtext =~ s/\; $//;
 6649:                         }
 6650:                         $resulttext .= 
 6651:                             '<li>'.
 6652:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 6653:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 6654:                             '</li>';
 6655:                     }
 6656:                 }
 6657:             }
 6658:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 6659:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 6660:                 my $chgtext;
 6661:                 foreach my $type (@{$titleorder}) {
 6662:                     if (grep(/^\Q$type\E$/,@searchby)) {
 6663:                         if (defined($searchtitles->{$type})) {
 6664:                             $chgtext .= $searchtitles->{$type}.'; ';
 6665:                         }
 6666:                     }
 6667:                 }
 6668:                 $chgtext =~ s/\; $//;
 6669:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 6670:             }
 6671:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 6672:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 6673:                 my $chgtext;
 6674:                 foreach my $type (@{$srchtypeorder}) {
 6675:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 6676:                         if (defined($srchtypes_desc->{$type})) {
 6677:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 6678:                         }
 6679:                     }
 6680:                 }
 6681:                 $chgtext =~ s/\; $//;
 6682:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 6683:             }
 6684:             $resulttext .= '</ul>';
 6685:         } else {
 6686:             $resulttext = &mt('No changes made to institution directory search settings');
 6687:         }
 6688:     } else {
 6689:         $resulttext = '<span class="LC_error">'.
 6690:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 6691:     }
 6692:     return $resulttext;
 6693: }
 6694: 
 6695: sub modify_contacts {
 6696:     my ($dom,$lastactref,%domconfig) = @_;
 6697:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 6698:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 6699:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 6700:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 6701:         }
 6702:     }
 6703:     my (%others,%to,%bcc);
 6704:     my @contacts = ('supportemail','adminemail');
 6705:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 6706:                     'requestsmail','updatesmail','idconflictsmail');
 6707:     my @toggles = ('reporterrors','reportupdates');
 6708:     foreach my $type (@mailings) {
 6709:         @{$newsetting{$type}} = 
 6710:             &Apache::loncommon::get_env_multiple('form.'.$type);
 6711:         foreach my $item (@contacts) {
 6712:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 6713:                 $contacts_hash{contacts}{$type}{$item} = 1;
 6714:             } else {
 6715:                 $contacts_hash{contacts}{$type}{$item} = 0;
 6716:             }
 6717:         }  
 6718:         $others{$type} = $env{'form.'.$type.'_others'};
 6719:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 6720:         if ($type eq 'helpdeskmail') {
 6721:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 6722:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 6723:         }
 6724:     }
 6725:     foreach my $item (@contacts) {
 6726:         $to{$item} = $env{'form.'.$item};
 6727:         $contacts_hash{'contacts'}{$item} = $to{$item};
 6728:     }
 6729:     foreach my $item (@toggles) {
 6730:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 6731:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 6732:         }
 6733:     }
 6734:     if (keys(%currsetting) > 0) {
 6735:         foreach my $item (@contacts) {
 6736:             if ($to{$item} ne $currsetting{$item}) {
 6737:                 $changes{$item} = 1;
 6738:             }
 6739:         }
 6740:         foreach my $type (@mailings) {
 6741:             foreach my $item (@contacts) {
 6742:                 if (ref($currsetting{$type}) eq 'HASH') {
 6743:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 6744:                         push(@{$changes{$type}},$item);
 6745:                     }
 6746:                 } else {
 6747:                     push(@{$changes{$type}},@{$newsetting{$type}});
 6748:                 }
 6749:             }
 6750:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 6751:                 push(@{$changes{$type}},'others');
 6752:             }
 6753:             if ($type eq 'helpdeskmail') {   
 6754:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 6755:                     push(@{$changes{$type}},'bcc'); 
 6756:                 }
 6757:             }
 6758:         }
 6759:     } else {
 6760:         my %default;
 6761:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 6762:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 6763:         $default{'errormail'} = 'adminemail';
 6764:         $default{'packagesmail'} = 'adminemail';
 6765:         $default{'helpdeskmail'} = 'supportemail';
 6766:         $default{'lonstatusmail'} = 'adminemail';
 6767:         $default{'requestsmail'} = 'adminemail';
 6768:         $default{'updatesmail'} = 'adminemail';
 6769:         foreach my $item (@contacts) {
 6770:            if ($to{$item} ne $default{$item}) {
 6771:               $changes{$item} = 1;
 6772:            }
 6773:         }
 6774:         foreach my $type (@mailings) {
 6775:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 6776:                
 6777:                 push(@{$changes{$type}},@{$newsetting{$type}});
 6778:             }
 6779:             if ($others{$type} ne '') {
 6780:                 push(@{$changes{$type}},'others');
 6781:             }
 6782:             if ($type eq 'helpdeskmail') {
 6783:                 if ($bcc{$type} ne '') {
 6784:                     push(@{$changes{$type}},'bcc');
 6785:                 }
 6786:             }
 6787:         }
 6788:     }
 6789:     foreach my $item (@toggles) {
 6790:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 6791:             $changes{$item} = 1;
 6792:         } elsif ((!$env{'form.'.$item}) &&
 6793:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 6794:             $changes{$item} = 1;
 6795:         }
 6796:     }
 6797:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 6798:                                              $dom);
 6799:     if ($putresult eq 'ok') {
 6800:         if (keys(%changes) > 0) {
 6801:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 6802:             $$lastactref = 'update';
 6803:             my ($titles,$short_titles)  = &contact_titles();
 6804:             $resulttext = &mt('Changes made:').'<ul>';
 6805:             foreach my $item (@contacts) {
 6806:                 if ($changes{$item}) {
 6807:                     $resulttext .= '<li>'.$titles->{$item}.
 6808:                                     &mt(' set to: ').
 6809:                                     '<span class="LC_cusr_emph">'.
 6810:                                     $to{$item}.'</span></li>';
 6811:                 }
 6812:             }
 6813:             foreach my $type (@mailings) {
 6814:                 if (ref($changes{$type}) eq 'ARRAY') {
 6815:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 6816:                     my @text;
 6817:                     foreach my $item (@{$newsetting{$type}}) {
 6818:                         push(@text,$short_titles->{$item});
 6819:                     }
 6820:                     if ($others{$type} ne '') {
 6821:                         push(@text,$others{$type});
 6822:                     }
 6823:                     $resulttext .= '<span class="LC_cusr_emph">'.
 6824:                                    join(', ',@text).'</span>';
 6825:                     if ($type eq 'helpdeskmail') {
 6826:                         if ($bcc{$type} ne '') {
 6827:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 6828:                         }
 6829:                     }
 6830:                     $resulttext .= '</li>';
 6831:                 }
 6832:             }
 6833:             my @offon = ('off','on');
 6834:             if ($changes{'reporterrors'}) {
 6835:                 $resulttext .= '<li>'.
 6836:                                &mt('E-mail error reports to [_1] set to "'.
 6837:                                    $offon[$env{'form.reporterrors'}].'".',
 6838:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 6839:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 6840:                                '</li>';
 6841:             }
 6842:             if ($changes{'reportupdates'}) {
 6843:                 $resulttext .= '<li>'.
 6844:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 6845:                                     $offon[$env{'form.reportupdates'}].'".',
 6846:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 6847:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 6848:                                 '</li>';
 6849:             }
 6850:             $resulttext .= '</ul>';
 6851:         } else {
 6852:             $resulttext = &mt('No changes made to contact information');
 6853:         }
 6854:     } else {
 6855:         $resulttext = '<span class="LC_error">'.
 6856:             &mt('An error occurred: [_1].',$putresult).'</span>';
 6857:     }
 6858:     return $resulttext;
 6859: }
 6860: 
 6861: sub modify_usercreation {
 6862:     my ($dom,%domconfig) = @_;
 6863:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate);
 6864:     my $warningmsg;
 6865:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 6866:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 6867:             $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 6868:         }
 6869:     }
 6870:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 6871:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 6872:     my @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 6873:     my @contexts = ('author','course','requestcrs','selfcreate');
 6874:     foreach my $item(@contexts) {
 6875:         if ($item eq 'selfcreate') {
 6876:             @{$cancreate{$item}} = &Apache::loncommon::get_env_multiple('form.can_createuser_'.$item);
 6877:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6878:             if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth'))) {
 6879:                 if (ref($cancreate{$item}) eq 'ARRAY') { 
 6880:                     if (grep(/^login$/,@{$cancreate{$item}})) {
 6881:                         $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.');   
 6882:                     }
 6883:                 }
 6884:             }
 6885:         } else {
 6886:             $cancreate{$item} = $env{'form.can_createuser_'.$item};
 6887:         }
 6888:     }
 6889:     my ($othertitle,$usertypes,$types) = 
 6890:         &Apache::loncommon::sorted_inst_types($dom);
 6891:     if (ref($types) eq 'ARRAY') {
 6892:         if (@{$types} > 0) {
 6893:             @{$cancreate{'statustocreate'}} = 
 6894:                 &Apache::loncommon::get_env_multiple('form.statustocreate');
 6895:         } else {
 6896:             @{$cancreate{'statustocreate'}} = ();
 6897:         }
 6898:         push(@contexts,'statustocreate');
 6899:     }
 6900:     &process_captcha('cancreate',\%changes,\%cancreate,\%curr_usercreation);
 6901:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 6902:         foreach my $item (@contexts) {
 6903:             if (($item eq 'selfcreate') || ($item eq 'statustocreate')) {
 6904:                 if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6905:                     foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 6906:                         if (ref($cancreate{$item}) eq 'ARRAY') {
 6907:                             if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 6908:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6909:                                     push(@{$changes{'cancreate'}},$item);
 6910:                                 }
 6911:                             }
 6912:                         }
 6913:                     }
 6914:                 } else {
 6915:                     if ($curr_usercreation{'cancreate'}{$item} eq '') {
 6916:                         if (@{$cancreate{$item}} > 0) {
 6917:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6918:                                 push(@{$changes{'cancreate'}},$item);
 6919:                             }
 6920:                         }
 6921:                     } else {
 6922:                         if ($curr_usercreation{'cancreate'}{$item} eq 'any') {
 6923:                             if (@{$cancreate{$item}} < 3) {
 6924:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6925:                                     push(@{$changes{'cancreate'}},$item);
 6926:                                 }
 6927:                             }
 6928:                         } elsif ($curr_usercreation{'cancreate'}{$item} eq 'none') {
 6929:                             if (@{$cancreate{$item}} > 0) {
 6930:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6931:                                     push(@{$changes{'cancreate'}},$item);
 6932:                                 }
 6933:                             }
 6934:                         } elsif (!grep(/^$curr_usercreation{'cancreate'}{$item}$/,@{$cancreate{$item}})) {
 6935:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6936:                                 push(@{$changes{'cancreate'}},$item);
 6937:                             }
 6938:                         }
 6939:                     }
 6940:                 }
 6941:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6942:                     foreach my $type (@{$cancreate{$item}}) {
 6943:                         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 6944:                             if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 6945:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6946:                                     push(@{$changes{'cancreate'}},$item);
 6947:                                 }
 6948:                             }
 6949:                         } elsif (($curr_usercreation{'cancreate'}{$item} ne 'any') &&
 6950:                                  ($curr_usercreation{'cancreate'}{$item} ne 'none')) {
 6951:                             if ($curr_usercreation{'cancreate'}{$item} ne $type) {
 6952:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 6953:                                     push(@{$changes{'cancreate'}},$item);
 6954:                                 }
 6955:                             }
 6956:                         }
 6957:                     }
 6958:                 }
 6959:             } else {
 6960:                 if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 6961:                     push(@{$changes{'cancreate'}},$item);
 6962:                 }
 6963:             }
 6964:         }
 6965:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 6966:         foreach my $item (@contexts) {
 6967:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 6968:                 if ($cancreate{$item} ne 'any') {
 6969:                     push(@{$changes{'cancreate'}},$item);
 6970:                 }
 6971:             } else {
 6972:                 if ($cancreate{$item} ne 'none') {
 6973:                     push(@{$changes{'cancreate'}},$item);
 6974:                 }
 6975:             }
 6976:         }
 6977:     } else {
 6978:         foreach my $item (@contexts)  {
 6979:             push(@{$changes{'cancreate'}},$item);
 6980:         }
 6981:     }
 6982: 
 6983:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 6984:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 6985:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 6986:                 push(@{$changes{'username_rule'}},$type);
 6987:             }
 6988:         }
 6989:         foreach my $type (@username_rule) {
 6990:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 6991:                 push(@{$changes{'username_rule'}},$type);
 6992:             }
 6993:         }
 6994:     } else {
 6995:         push(@{$changes{'username_rule'}},@username_rule);
 6996:     }
 6997: 
 6998:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 6999:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 7000:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 7001:                 push(@{$changes{'id_rule'}},$type);
 7002:             }
 7003:         }
 7004:         foreach my $type (@id_rule) {
 7005:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 7006:                 push(@{$changes{'id_rule'}},$type);
 7007:             }
 7008:         }
 7009:     } else {
 7010:         push(@{$changes{'id_rule'}},@id_rule);
 7011:     }
 7012: 
 7013:     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 7014:         foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 7015:             if (!grep(/^\Q$type\E$/,@email_rule)) {
 7016:                 push(@{$changes{'email_rule'}},$type);
 7017:             }
 7018:         }
 7019:         foreach my $type (@email_rule) {
 7020:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 7021:                 push(@{$changes{'email_rule'}},$type);
 7022:             }
 7023:         }
 7024:     } else {
 7025:         push(@{$changes{'email_rule'}},@email_rule);
 7026:     }
 7027: 
 7028:     my @authen_contexts = ('author','course','domain');
 7029:     my @authtypes = ('int','krb4','krb5','loc');
 7030:     my %authhash;
 7031:     foreach my $item (@authen_contexts) {
 7032:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 7033:         foreach my $auth (@authtypes) {
 7034:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 7035:                 $authhash{$item}{$auth} = 1;
 7036:             } else {
 7037:                 $authhash{$item}{$auth} = 0;
 7038:             }
 7039:         }
 7040:     }
 7041:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 7042:         foreach my $item (@authen_contexts) {
 7043:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 7044:                 foreach my $auth (@authtypes) {
 7045:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 7046:                         push(@{$changes{'authtypes'}},$item);
 7047:                         last;
 7048:                     }
 7049:                 }
 7050:             }
 7051:         }
 7052:     } else {
 7053:         foreach my $item (@authen_contexts) {
 7054:             push(@{$changes{'authtypes'}},$item);
 7055:         }
 7056:     }
 7057: 
 7058:     my %usercreation_hash =  (
 7059:             usercreation => {
 7060:                               cancreate     => \%cancreate,
 7061:                               username_rule => \@username_rule,
 7062:                               id_rule       => \@id_rule,
 7063:                               email_rule    => \@email_rule,
 7064:                               authtypes     => \%authhash,
 7065:                             }
 7066:             );
 7067: 
 7068:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 7069:                                              $dom);
 7070: 
 7071:     my %selfcreatetypes = (
 7072:                              sso   => 'users authenticated by institutional single sign on',
 7073:                              login => 'users authenticated by institutional log-in',
 7074:                              email => 'users who provide a valid e-mail address for use as the username',
 7075:                           );
 7076:     if ($putresult eq 'ok') {
 7077:         if (keys(%changes) > 0) {
 7078:             $resulttext = &mt('Changes made:').'<ul>';
 7079:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 7080:                 my %lt = &usercreation_types();
 7081:                 foreach my $type (@{$changes{'cancreate'}}) {
 7082:                     my $chgtext;
 7083:                     unless (($type eq 'statustocreate') || ($type eq 'captcha') || ($type eq 'recaptchakeys')) {
 7084:                         $chgtext = $lt{$type}.', ';
 7085:                     }
 7086:                     if ($type eq 'selfcreate') {
 7087:                         if (@{$cancreate{$type}} == 0) {
 7088:                             $chgtext .= &mt('creation of a new user account is not permitted.');
 7089:                         } else {
 7090:                             $chgtext .= &mt('creation of a new account is permitted for:').'<ul>';
 7091:                             foreach my $case (@{$cancreate{$type}}) {
 7092:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 7093:                             }
 7094:                             $chgtext .= '</ul>';
 7095:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 7096:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 7097:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 7098:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 7099:                                             $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 7100:                                         }
 7101:                                     }
 7102:                                 }
 7103:                             }
 7104:                         }
 7105:                     } elsif ($type eq 'statustocreate') {
 7106:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 7107:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 7108:                             if (@{$cancreate{'selfcreate'}} > 0) {
 7109:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 7110: 
 7111:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 7112:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 7113:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 7114:                                     } 
 7115:                                 } elsif (ref($usertypes) eq 'HASH') {
 7116:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 7117:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 7118:                                     } else {
 7119:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 7120:                                     }
 7121:                                     $chgtext .= '<ul>';
 7122:                                     foreach my $case (@{$cancreate{$type}}) {
 7123:                                         if ($case eq 'default') {
 7124:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 7125:                                         } else {
 7126:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 7127:                                         }
 7128:                                     }
 7129:                                     $chgtext .= '</ul>';
 7130:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 7131:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').'</span>';
 7132:                                     }
 7133:                                 }
 7134:                             } else {
 7135:                                 if (@{$cancreate{$type}} == 0) {
 7136:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 7137:                                 } else {
 7138:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 7139:                                 }
 7140:                             }
 7141:                         }
 7142:                     } elsif ($type eq 'captcha') {
 7143:                         if ($cancreate{$type} eq 'notused') {
 7144:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 7145:                         } else {
 7146:                             my %captchas = &captcha_phrases();
 7147:                             if ($captchas{$cancreate{$type}}) {
 7148:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$cancreate{$type}}.");
 7149:                             } else {
 7150:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.'); 
 7151:                             }
 7152:                         }
 7153:                     } elsif ($type eq 'recaptchakeys') {
 7154:                         my ($privkey,$pubkey);
 7155:                         if (ref($cancreate{$type}) eq 'HASH') {
 7156:                             $pubkey = $cancreate{$type}{'public'};
 7157:                             $privkey = $cancreate{$type}{'private'};
 7158:                         }
 7159:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 7160:                         if (!$pubkey) {
 7161:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 7162:                         } else {
 7163:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 7164:                         }
 7165:                         if (!$privkey) {
 7166:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 7167:                         } else {
 7168:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 7169:                         }
 7170:                         $chgtext .= '</ul>';
 7171:                     } else {
 7172:                         if ($cancreate{$type} eq 'none') {
 7173:                             $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 7174:                         } elsif ($cancreate{$type} eq 'any') {
 7175:                             $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 7176:                         } elsif ($cancreate{$type} eq 'official') {
 7177:                             $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 7178:                         } elsif ($cancreate{$type} eq 'unofficial') {
 7179:                             $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 7180:                         }
 7181:                     }
 7182:                     $resulttext .= '<li>'.$chgtext.'</li>';
 7183:                 }
 7184:             }
 7185:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 7186:                 my ($rules,$ruleorder) = 
 7187:                     &Apache::lonnet::inst_userrules($dom,'username');
 7188:                 my $chgtext = '<ul>';
 7189:                 foreach my $type (@username_rule) {
 7190:                     if (ref($rules->{$type}) eq 'HASH') {
 7191:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 7192:                     }
 7193:                 }
 7194:                 $chgtext .= '</ul>';
 7195:                 if (@username_rule > 0) {
 7196:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 7197:                 } else {
 7198:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 7199:                 }
 7200:             }
 7201:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 7202:                 my ($idrules,$idruleorder) = 
 7203:                     &Apache::lonnet::inst_userrules($dom,'id');
 7204:                 my $chgtext = '<ul>';
 7205:                 foreach my $type (@id_rule) {
 7206:                     if (ref($idrules->{$type}) eq 'HASH') {
 7207:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 7208:                     }
 7209:                 }
 7210:                 $chgtext .= '</ul>';
 7211:                 if (@id_rule > 0) {
 7212:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 7213:                 } else {
 7214:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 7215:                 }
 7216:             }
 7217:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 7218:                 my ($emailrules,$emailruleorder) =
 7219:                     &Apache::lonnet::inst_userrules($dom,'email');
 7220:                 my $chgtext = '<ul>';
 7221:                 foreach my $type (@email_rule) {
 7222:                     if (ref($emailrules->{$type}) eq 'HASH') {
 7223:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 7224:                     }
 7225:                 }
 7226:                 $chgtext .= '</ul>';
 7227:                 if (@email_rule > 0) {
 7228:                     $resulttext .= '<li>'.&mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').$chgtext.'</li>';
 7229:                 } else {
 7230:                     $resulttext .= '<li>'.&mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').'</li>';
 7231:                 }
 7232:             }
 7233: 
 7234:             my %authname = &authtype_names();
 7235:             my %context_title = &context_names();
 7236:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 7237:                 my $chgtext = '<ul>';
 7238:                 foreach my $type (@{$changes{'authtypes'}}) {
 7239:                     my @allowed;
 7240:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 7241:                     foreach my $auth (@authtypes) {
 7242:                         if ($authhash{$type}{$auth}) {
 7243:                             push(@allowed,$authname{$auth});
 7244:                         }
 7245:                     }
 7246:                     if (@allowed > 0) {
 7247:                         $chgtext .= join(', ',@allowed).'</li>';
 7248:                     } else {
 7249:                         $chgtext .= &mt('none').'</li>';
 7250:                     }
 7251:                 }
 7252:                 $chgtext .= '</ul>';
 7253:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 7254:                 $resulttext .= '</li>';
 7255:             }
 7256:             $resulttext .= '</ul>';
 7257:         } else {
 7258:             $resulttext = &mt('No changes made to user creation settings');
 7259:         }
 7260:     } else {
 7261:         $resulttext = '<span class="LC_error">'.
 7262:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7263:     }
 7264:     if ($warningmsg ne '') {
 7265:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 7266:     }
 7267:     return $resulttext;
 7268: }
 7269: 
 7270: sub process_captcha {
 7271:     my ($container,$changes,$newsettings,$current) = @_;
 7272:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 7273:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 7274:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 7275:         $newsettings->{'captcha'} = 'original';
 7276:     }
 7277:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 7278:         if ($container eq 'cancreate') { 
 7279:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7280:                 push(@{$changes->{'cancreate'}},'captcha');
 7281:             } elsif (!defined($changes->{'cancreate'})) {
 7282:                 $changes->{'cancreate'} = ['captcha'];
 7283:             }
 7284:         } else {
 7285:             $changes->{'captcha'} = 1;
 7286:         }
 7287:     }
 7288:     my ($newpub,$newpriv,$currpub,$currpriv);
 7289:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 7290:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 7291:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 7292:         $newpub =~ s/\W//g;
 7293:         $newpriv =~ s/\W//g;
 7294:         $newsettings->{'recaptchakeys'} = {
 7295:                                              public  => $newpub,
 7296:                                              private => $newpriv,
 7297:                                           };
 7298:     }
 7299:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 7300:         $currpub = $current->{'recaptchakeys'}{'public'};
 7301:         $currpriv = $current->{'recaptchakeys'}{'private'};
 7302:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 7303:             $newsettings->{'recaptchakeys'} = {
 7304:                                                  public  => '',
 7305:                                                  private => '',
 7306:                                               }
 7307:         }
 7308:     }
 7309:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 7310:         if ($container eq 'cancreate') {
 7311:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7312:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 7313:             } elsif (!defined($changes->{'cancreate'})) {
 7314:                 $changes->{'cancreate'} = ['recaptchakeys'];
 7315:             }
 7316:         } else {
 7317:             $changes->{'recaptchakeys'} = 1; 
 7318:         }
 7319:     }
 7320:     return;
 7321: }
 7322: 
 7323: sub modify_usermodification {
 7324:     my ($dom,%domconfig) = @_;
 7325:     my ($resulttext,%curr_usermodification,%changes);
 7326:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 7327:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 7328:             $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 7329:         }
 7330:     }
 7331:     my @contexts = ('author','course','selfcreate');
 7332:     my %context_title = (
 7333:                            author => 'In author context',
 7334:                            course => 'In course context',
 7335:                            selfcreate => 'When self creating account', 
 7336:                         );
 7337:     my @fields = ('lastname','firstname','middlename','generation',
 7338:                   'permanentemail','id');
 7339:     my %roles = (
 7340:                   author => ['ca','aa'],
 7341:                   course => ['st','ep','ta','in','cr'],
 7342:                 );
 7343:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7344:     if (ref($types) eq 'ARRAY') {
 7345:         push(@{$types},'default');
 7346:         $usertypes->{'default'} = $othertitle;
 7347:     }
 7348:     $roles{'selfcreate'} = $types;  
 7349:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7350:     my %modifyhash;
 7351:     foreach my $context (@contexts) {
 7352:         foreach my $role (@{$roles{$context}}) {
 7353:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 7354:             foreach my $item (@fields) {
 7355:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 7356:                     $modifyhash{$context}{$role}{$item} = 1;
 7357:                 } else {
 7358:                     $modifyhash{$context}{$role}{$item} = 0;
 7359:                 }
 7360:             }
 7361:         }
 7362:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 7363:             foreach my $role (@{$roles{$context}}) {
 7364:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 7365:                     foreach my $field (@fields) {
 7366:                         if ($modifyhash{$context}{$role}{$field} ne 
 7367:                                 $curr_usermodification{$context}{$role}{$field}) {
 7368:                             push(@{$changes{$context}},$role);
 7369:                             last;
 7370:                         }
 7371:                     }
 7372:                 }
 7373:             }
 7374:         } else {
 7375:             foreach my $context (@contexts) {
 7376:                 foreach my $role (@{$roles{$context}}) {
 7377:                     push(@{$changes{$context}},$role);
 7378:                 }
 7379:             }
 7380:         }
 7381:     }
 7382:     my %usermodification_hash =  (
 7383:                                    usermodification => \%modifyhash,
 7384:                                  );
 7385:     my $putresult = &Apache::lonnet::put_dom('configuration',
 7386:                                              \%usermodification_hash,$dom);
 7387:     if ($putresult eq 'ok') {
 7388:         if (keys(%changes) > 0) {
 7389:             $resulttext = &mt('Changes made: ').'<ul>';
 7390:             foreach my $context (@contexts) {
 7391:                 if (ref($changes{$context}) eq 'ARRAY') {
 7392:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 7393:                     if (ref($changes{$context}) eq 'ARRAY') {
 7394:                         foreach my $role (@{$changes{$context}}) {
 7395:                             my $rolename;
 7396:                             if ($context eq 'selfcreate') {
 7397:                                 $rolename = $role;
 7398:                                 if (ref($usertypes) eq 'HASH') {
 7399:                                     if ($usertypes->{$role} ne '') {
 7400:                                         $rolename = $usertypes->{$role};
 7401:                                     }
 7402:                                 }
 7403:                             } else {
 7404:                                 if ($role eq 'cr') {
 7405:                                     $rolename = &mt('Custom');
 7406:                                 } else {
 7407:                                     $rolename = &Apache::lonnet::plaintext($role);
 7408:                                 }
 7409:                             }
 7410:                             my @modifiable;
 7411:                             if ($context eq 'selfcreate') {
 7412:                                 $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): ');
 7413:                             } else {
 7414:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 7415:                             }
 7416:                             foreach my $field (@fields) {
 7417:                                 if ($modifyhash{$context}{$role}{$field}) {
 7418:                                     push(@modifiable,$fieldtitles{$field});
 7419:                                 }
 7420:                             }
 7421:                             if (@modifiable > 0) {
 7422:                                 $resulttext .= join(', ',@modifiable);
 7423:                             } else {
 7424:                                 $resulttext .= &mt('none'); 
 7425:                             }
 7426:                             $resulttext .= '</li>';
 7427:                         }
 7428:                         $resulttext .= '</ul></li>';
 7429:                     }
 7430:                 }
 7431:             }
 7432:             $resulttext .= '</ul>';
 7433:         } else {
 7434:             $resulttext = &mt('No changes made to user modification settings');
 7435:         }
 7436:     } else {
 7437:         $resulttext = '<span class="LC_error">'.
 7438:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7439:     }
 7440:     return $resulttext;
 7441: }
 7442: 
 7443: sub modify_defaults {
 7444:     my ($dom,$r,%domconfig) = @_;
 7445:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 7446:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 7447:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 7448:     my @authtypes = ('internal','krb4','krb5','localauth');
 7449:     foreach my $item (@items) {
 7450:         $newvalues{$item} = $env{'form.'.$item};
 7451:         if ($item eq 'auth_def') {
 7452:             if ($newvalues{$item} ne '') {
 7453:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 7454:                     push(@errors,$item);
 7455:                 }
 7456:             }
 7457:         } elsif ($item eq 'lang_def') {
 7458:             if ($newvalues{$item} ne '') {
 7459:                 if ($newvalues{$item} =~ /^(\w+)/) {
 7460:                     my $langcode = $1;
 7461:                     if ($langcode ne 'x_chef') {
 7462:                         if (code2language($langcode) eq '') {
 7463:                             push(@errors,$item);
 7464:                         }
 7465:                     }
 7466:                 } else {
 7467:                     push(@errors,$item);
 7468:                 }
 7469:             }
 7470:         } elsif ($item eq 'timezone_def') {
 7471:             if ($newvalues{$item} ne '') {
 7472:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 7473:                     push(@errors,$item);   
 7474:                 }
 7475:             }
 7476:         } elsif ($item eq 'datelocale_def') {
 7477:             if ($newvalues{$item} ne '') {
 7478:                 my @datelocale_ids = DateTime::Locale->ids();
 7479:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 7480:                     push(@errors,$item);
 7481:                 }
 7482:             }
 7483:         } elsif ($item eq 'portal_def') {
 7484:             if ($newvalues{$item} ne '') {
 7485:                 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])\/?$/) {
 7486:                     push(@errors,$item);
 7487:                 }
 7488:             }
 7489:         }
 7490:         if (grep(/^\Q$item\E$/,@errors)) {
 7491:             $newvalues{$item} = $domdefaults{$item};
 7492:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 7493:             $changes{$item} = 1;
 7494:         }
 7495:         $domdefaults{$item} = $newvalues{$item};
 7496:     }
 7497:     my %defaults_hash = (
 7498:                          defaults => \%newvalues,
 7499:                         );
 7500:     my $title = &defaults_titles();
 7501:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 7502:                                              $dom);
 7503:     if ($putresult eq 'ok') {
 7504:         if (keys(%changes) > 0) {
 7505:             $resulttext = &mt('Changes made:').'<ul>';
 7506:             my $version = $r->dir_config('lonVersion');
 7507:             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";
 7508:             foreach my $item (sort(keys(%changes))) {
 7509:                 my $value = $env{'form.'.$item};
 7510:                 if ($value eq '') {
 7511:                     $value = &mt('none');
 7512:                 } elsif ($item eq 'auth_def') {
 7513:                     my %authnames = &authtype_names();
 7514:                     my %shortauth = (
 7515:                              internal => 'int',
 7516:                              krb4 => 'krb4',
 7517:                              krb5 => 'krb5',
 7518:                              localauth  => 'loc',
 7519:                     );
 7520:                     $value = $authnames{$shortauth{$value}};
 7521:                 }
 7522:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 7523:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 7524:             }
 7525:             $resulttext .= '</ul>';
 7526:             $mailmsgtext .= "\n";
 7527:             my $cachetime = 24*60*60;
 7528:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7529:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 7530:                 my $notify = 1;
 7531:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 7532:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 7533:                         $notify = 0;
 7534:                     }
 7535:                 }
 7536:                 if ($notify) {
 7537:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 7538:                                                "LON-CAPA Domain Settings Change - $dom",
 7539:                                                $mailmsgtext);
 7540:                 }
 7541:             }
 7542:         } else {
 7543:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 7544:         }
 7545:     } else {
 7546:         $resulttext = '<span class="LC_error">'.
 7547:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7548:     }
 7549:     if (@errors > 0) {
 7550:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 7551:         foreach my $item (@errors) {
 7552:             $resulttext .= ' "'.$title->{$item}.'",';
 7553:         }
 7554:         $resulttext =~ s/,$//;
 7555:     }
 7556:     return $resulttext;
 7557: }
 7558: 
 7559: sub modify_scantron {
 7560:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 7561:     my ($resulttext,%confhash,%changes,$errors);
 7562:     my $custom = 'custom.tab';
 7563:     my $default = 'default.tab';
 7564:     my $servadm = $r->dir_config('lonAdmEMail');
 7565:     my ($configuserok,$author_ok,$switchserver) = 
 7566:         &config_check($dom,$confname,$servadm);
 7567:     if ($env{'form.scantronformat.filename'} ne '') {
 7568:         my $error;
 7569:         if ($configuserok eq 'ok') {
 7570:             if ($switchserver) {
 7571:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 7572:             } else {
 7573:                 if ($author_ok eq 'ok') {
 7574:                     my ($result,$scantronurl) =
 7575:                         &publishlogo($r,'upload','scantronformat',$dom,
 7576:                                      $confname,'scantron','','',$custom);
 7577:                     if ($result eq 'ok') {
 7578:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 7579:                         $changes{'scantronformat'} = 1;
 7580:                     } else {
 7581:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 7582:                     }
 7583:                 } else {
 7584:                     $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);
 7585:                 }
 7586:             }
 7587:         } else {
 7588:             $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);
 7589:         }
 7590:         if ($error) {
 7591:             &Apache::lonnet::logthis($error);
 7592:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7593:         }
 7594:     }
 7595:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 7596:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 7597:             if ($env{'form.scantronformat_del'}) {
 7598:                 $confhash{'scantron'}{'scantronformat'} = '';
 7599:                 $changes{'scantronformat'} = 1;
 7600:             }
 7601:         }
 7602:     }
 7603:     if (keys(%confhash) > 0) {
 7604:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 7605:                                                  $dom);
 7606:         if ($putresult eq 'ok') {
 7607:             if (keys(%changes) > 0) {
 7608:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 7609:                     $resulttext = &mt('Changes made:').'<ul>';
 7610:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 7611:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 7612:                     } else {
 7613:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 7614:                     }
 7615:                     $resulttext .= '</ul>';
 7616:                 } else {
 7617:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 7618:                 }
 7619:                 $resulttext .= '</ul>';
 7620:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 7621:                 $$lastactref = 'update';
 7622:             } else {
 7623:                 $resulttext = &mt('No changes made to bubblesheet format file');
 7624:             }
 7625:         } else {
 7626:             $resulttext = '<span class="LC_error">'.
 7627:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 7628:         }
 7629:     } else {
 7630:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 7631:     }
 7632:     if ($errors) {
 7633:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 7634:                        $errors.'</ul>';
 7635:     }
 7636:     return $resulttext;
 7637: }
 7638: 
 7639: sub modify_coursecategories {
 7640:     my ($dom,%domconfig) = @_;
 7641:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 7642:         $cathash);
 7643:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 7644:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7645:         $cathash = $domconfig{'coursecategories'}{'cats'};
 7646:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 7647:             $changes{'togglecats'} = 1;
 7648:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 7649:         }
 7650:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 7651:             $changes{'categorize'} = 1;
 7652:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 7653:         }
 7654:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 7655:             $changes{'togglecatscomm'} = 1;
 7656:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 7657:         }
 7658:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 7659:             $changes{'categorizecomm'} = 1;
 7660:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 7661:         }
 7662:     } else {
 7663:         $changes{'togglecats'} = 1;
 7664:         $changes{'categorize'} = 1;
 7665:         $changes{'togglecatscomm'} = 1;
 7666:         $changes{'categorizecomm'} = 1;
 7667:         $domconfig{'coursecategories'} = {
 7668:                                              togglecats => $env{'form.togglecats'},
 7669:                                              categorize => $env{'form.categorize'},
 7670:                                              togglecatscomm => $env{'form.togglecatscomm'},
 7671:                                              categorizecomm => $env{'form.categorizecomm'},
 7672:                                          };
 7673:     }
 7674:     if (ref($cathash) eq 'HASH') {
 7675:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 7676:             push (@deletecategory,'instcode::0');
 7677:         }
 7678:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 7679:             push(@deletecategory,'communities::0');
 7680:         }
 7681:     }
 7682:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 7683:     if (ref($cathash) eq 'HASH') {
 7684:         if (@deletecategory > 0) {
 7685:             #FIXME Need to remove category from all courses using a deleted category 
 7686:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 7687:             foreach my $item (@deletecategory) {
 7688:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 7689:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 7690:                     $deletions{$item} = 1;
 7691:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 7692:                 }
 7693:             }
 7694:         }
 7695:         foreach my $item (keys(%{$cathash})) {
 7696:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 7697:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 7698:                 $reorderings{$item} = 1;
 7699:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 7700:             }
 7701:             if ($env{'form.addcategory_name_'.$item} ne '') {
 7702:                 my $newcat = $env{'form.addcategory_name_'.$item};
 7703:                 my $newdepth = $depth+1;
 7704:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7705:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 7706:                 $adds{$newitem} = 1; 
 7707:             }
 7708:             if ($env{'form.subcat_'.$item} ne '') {
 7709:                 my $newcat = $env{'form.subcat_'.$item};
 7710:                 my $newdepth = $depth+1;
 7711:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 7712:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 7713:                 $adds{$newitem} = 1;
 7714:             }
 7715:         }
 7716:     }
 7717:     if ($env{'form.instcode'} eq '1') {
 7718:         if (ref($cathash) eq 'HASH') {
 7719:             my $newitem = 'instcode::0';
 7720:             if ($cathash->{$newitem} eq '') {  
 7721:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7722:                 $adds{$newitem} = 1;
 7723:             }
 7724:         } else {
 7725:             my $newitem = 'instcode::0';
 7726:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 7727:             $adds{$newitem} = 1;
 7728:         }
 7729:     }
 7730:     if ($env{'form.communities'} eq '1') {
 7731:         if (ref($cathash) eq 'HASH') {
 7732:             my $newitem = 'communities::0';
 7733:             if ($cathash->{$newitem} eq '') {
 7734:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7735:                 $adds{$newitem} = 1;
 7736:             }
 7737:         } else {
 7738:             my $newitem = 'communities::0';
 7739:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 7740:             $adds{$newitem} = 1;
 7741:         }
 7742:     }
 7743:     if ($env{'form.addcategory_name'} ne '') {
 7744:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 7745:             ($env{'form.addcategory_name'} ne 'communities')) {
 7746:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 7747:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 7748:             $adds{$newitem} = 1;
 7749:         }
 7750:     }
 7751:     my $putresult;
 7752:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7753:         if (keys(%deletions) > 0) {
 7754:             foreach my $key (keys(%deletions)) {
 7755:                 if ($predelallitems{$key} ne '') {
 7756:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 7757:                 }
 7758:             }
 7759:         }
 7760:         my (@chkcats,@chktrails,%chkallitems);
 7761:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 7762:         if (ref($chkcats[0]) eq 'ARRAY') {
 7763:             my $depth = 0;
 7764:             my $chg = 0;
 7765:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 7766:                 my $name = $chkcats[0][$i];
 7767:                 my $item;
 7768:                 if ($name eq '') {
 7769:                     $chg ++;
 7770:                 } else {
 7771:                     $item = &escape($name).'::0';
 7772:                     if ($chg) {
 7773:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 7774:                     }
 7775:                     $depth ++; 
 7776:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 7777:                     $depth --;
 7778:                 }
 7779:             }
 7780:         }
 7781:     }
 7782:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7783:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 7784:         if ($putresult eq 'ok') {
 7785:             my %title = (
 7786:                          togglecats     => 'Show/Hide a course in catalog',
 7787:                          categorize     => 'Assign a category to a course',
 7788:                          togglecatscomm => 'Show/Hide a community in catalog',
 7789:                          categorizecomm => 'Assign a category to a community',
 7790:                         );
 7791:             my %level = (
 7792:                          dom  => 'set in Domain ("Modify Course/Community")',
 7793:                          crs  => 'set in Course ("Course Configuration")',
 7794:                          comm => 'set in Community ("Community Configuration")',
 7795:                         );
 7796:             $resulttext = &mt('Changes made:').'<ul>';
 7797:             if ($changes{'togglecats'}) {
 7798:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 7799:             }
 7800:             if ($changes{'categorize'}) {
 7801:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 7802:             }
 7803:             if ($changes{'togglecatscomm'}) {
 7804:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 7805:             }
 7806:             if ($changes{'categorizecomm'}) {
 7807:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 7808:             }
 7809:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 7810:                 my $cathash;
 7811:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 7812:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 7813:                 } else {
 7814:                     $cathash = {};
 7815:                 } 
 7816:                 my (@cats,@trails,%allitems);
 7817:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 7818:                 if (keys(%deletions) > 0) {
 7819:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 7820:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 7821:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 7822:                     }
 7823:                     $resulttext .= '</ul></li>';
 7824:                 }
 7825:                 if (keys(%reorderings) > 0) {
 7826:                     my %sort_by_trail;
 7827:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 7828:                     foreach my $key (keys(%reorderings)) {
 7829:                         if ($allitems{$key} ne '') {
 7830:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7831:                         }
 7832:                     }
 7833:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7834:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7835:                     }
 7836:                     $resulttext .= '</ul></li>';
 7837:                 }
 7838:                 if (keys(%adds) > 0) {
 7839:                     my %sort_by_trail;
 7840:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 7841:                     foreach my $key (keys(%adds)) {
 7842:                         if ($allitems{$key} ne '') {
 7843:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 7844:                         }
 7845:                     }
 7846:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 7847:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 7848:                     }
 7849:                     $resulttext .= '</ul></li>';
 7850:                 }
 7851:             }
 7852:             $resulttext .= '</ul>';
 7853:         } else {
 7854:             $resulttext = '<span class="LC_error">'.
 7855:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 7856:         }
 7857:     } else {
 7858:         $resulttext = &mt('No changes made to course and community categories');
 7859:     }
 7860:     return $resulttext;
 7861: }
 7862: 
 7863: sub modify_serverstatuses {
 7864:     my ($dom,%domconfig) = @_;
 7865:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 7866:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 7867:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 7868:     }
 7869:     my @pages = &serverstatus_pages();
 7870:     foreach my $type (@pages) {
 7871:         $newserverstatus{$type}{'namedusers'} = '';
 7872:         $newserverstatus{$type}{'machines'} = '';
 7873:         if (defined($env{'form.'.$type.'_namedusers'})) {
 7874:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 7875:             my @okusers;
 7876:             foreach my $user (@users) {
 7877:                 my ($uname,$udom) = split(/:/,$user);
 7878:                 if (($udom =~ /^$match_domain$/) &&   
 7879:                     (&Apache::lonnet::domain($udom)) &&
 7880:                     ($uname =~ /^$match_username$/)) {
 7881:                     if (!grep(/^\Q$user\E/,@okusers)) {
 7882:                         push(@okusers,$user);
 7883:                     }
 7884:                 }
 7885:             }
 7886:             if (@okusers > 0) {
 7887:                  @okusers = sort(@okusers);
 7888:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 7889:             }
 7890:         }
 7891:         if (defined($env{'form.'.$type.'_machines'})) {
 7892:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 7893:             my @okmachines;
 7894:             foreach my $ip (@machines) {
 7895:                 my @parts = split(/\./,$ip);
 7896:                 next if (@parts < 4);
 7897:                 my $badip = 0;
 7898:                 for (my $i=0; $i<4; $i++) {
 7899:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 7900:                         $badip = 1;
 7901:                         last;
 7902:                     }
 7903:                 }
 7904:                 if (!$badip) {
 7905:                     push(@okmachines,$ip);     
 7906:                 }
 7907:             }
 7908:             @okmachines = sort(@okmachines);
 7909:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 7910:         }
 7911:     }
 7912:     my %serverstatushash =  (
 7913:                                 serverstatuses => \%newserverstatus,
 7914:                             );
 7915:     foreach my $type (@pages) {
 7916:         foreach my $setting ('namedusers','machines') {
 7917:             my (@current,@new);
 7918:             if (ref($currserverstatus{$type}) eq 'HASH') {
 7919:                 if ($currserverstatus{$type}{$setting} ne '') { 
 7920:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 7921:                 }
 7922:             }
 7923:             if ($newserverstatus{$type}{$setting} ne '') {
 7924:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 7925:             }
 7926:             if (@current > 0) {
 7927:                 if (@new > 0) {
 7928:                     foreach my $item (@current) {
 7929:                         if (!grep(/^\Q$item\E$/,@new)) {
 7930:                             $changes{$type}{$setting} = 1;
 7931:                             last;
 7932:                         }
 7933:                     }
 7934:                     foreach my $item (@new) {
 7935:                         if (!grep(/^\Q$item\E$/,@current)) {
 7936:                             $changes{$type}{$setting} = 1;
 7937:                             last;
 7938:                         }
 7939:                     }
 7940:                 } else {
 7941:                     $changes{$type}{$setting} = 1;
 7942:                 }
 7943:             } elsif (@new > 0) {
 7944:                 $changes{$type}{$setting} = 1;
 7945:             }
 7946:         }
 7947:     }
 7948:     if (keys(%changes) > 0) {
 7949:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7950:         my $putresult = &Apache::lonnet::put_dom('configuration',
 7951:                                                  \%serverstatushash,$dom);
 7952:         if ($putresult eq 'ok') {
 7953:             $resulttext .= &mt('Changes made:').'<ul>';
 7954:             foreach my $type (@pages) {
 7955:                 if (ref($changes{$type}) eq 'HASH') {
 7956:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 7957:                     if ($changes{$type}{'namedusers'}) {
 7958:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 7959:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 7960:                         } else {
 7961:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 7962:                         }
 7963:                     }
 7964:                     if ($changes{$type}{'machines'}) {
 7965:                         if ($newserverstatus{$type}{'machines'} eq '') {
 7966:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 7967:                         } else {
 7968:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 7969:                         }
 7970: 
 7971:                     }
 7972:                     $resulttext .= '</ul></li>';
 7973:                 }
 7974:             }
 7975:             $resulttext .= '</ul>';
 7976:         } else {
 7977:             $resulttext = '<span class="LC_error">'.
 7978:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 7979: 
 7980:         }
 7981:     } else {
 7982:         $resulttext = &mt('No changes made to access to server status pages');
 7983:     }
 7984:     return $resulttext;
 7985: }
 7986: 
 7987: sub modify_helpsettings {
 7988:     my ($r,$dom,$confname,%domconfig) = @_;
 7989:     my ($resulttext,$errors,%changes,%helphash);
 7990:     my %defaultchecked = ('submitbugs' => 'on');
 7991:     my @offon = ('off','on');
 7992:     my @toggles = ('submitbugs');
 7993:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 7994:         foreach my $item (@toggles) {
 7995:             if ($defaultchecked{$item} eq 'on') { 
 7996:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 7997:                     if ($env{'form.'.$item} eq '0') {
 7998:                         $changes{$item} = 1;
 7999:                     }
 8000:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 8001:                     $changes{$item} = 1;
 8002:                 }
 8003:             } elsif ($defaultchecked{$item} eq 'off') {
 8004:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 8005:                     if ($env{'form.'.$item} eq '1') {
 8006:                         $changes{$item} = 1;
 8007:                     }
 8008:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 8009:                     $changes{$item} = 1;
 8010:                 }
 8011:             }
 8012:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) { 
 8013:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 8014:             }
 8015:         }
 8016:     }
 8017:     my $putresult;
 8018:     if (keys(%changes) > 0) {
 8019:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 8020:         if ($putresult eq 'ok') {
 8021:             $resulttext = &mt('Changes made:').'<ul>';
 8022:             foreach my $item (sort(keys(%changes))) {
 8023:                 if ($item eq 'submitbugs') {
 8024:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 8025:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 8026:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 8027:                 }
 8028:             }
 8029:             $resulttext .= '</ul>';
 8030:         } else {
 8031:             $resulttext = &mt('No changes made to help settings');
 8032:             $errors .= '<li><span class="LC_error">'.
 8033:                        &mt('An error occurred storing the settings: [_1]',
 8034:                            $putresult).'</span></li>';
 8035:         }
 8036:     }
 8037:     if ($errors) {
 8038:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 8039:                        $errors.'</ul>';
 8040:     }
 8041:     return $resulttext;
 8042: }
 8043: 
 8044: sub modify_coursedefaults {
 8045:     my ($dom,%domconfig) = @_;
 8046:     my ($resulttext,$errors,%changes,%defaultshash);
 8047:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 8048:     my @toggles = ('canuse_pdfforms');
 8049:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
 8050:                    'uploadquota_community');
 8051:     my @types = ('official','unofficial','community');
 8052:     my %staticdefaults = (
 8053:                            anonsurvey_threshold => 10,
 8054:                            uploadquota          => 500,
 8055:                          );
 8056: 
 8057:     $defaultshash{'coursedefaults'} = {};
 8058: 
 8059:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 8060:         if ($domconfig{'coursedefaults'} eq '') {
 8061:             $domconfig{'coursedefaults'} = {};
 8062:         }
 8063:     }
 8064: 
 8065:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 8066:         foreach my $item (@toggles) {
 8067:             if ($defaultchecked{$item} eq 'on') {
 8068:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 8069:                     ($env{'form.'.$item} eq '0')) {
 8070:                     $changes{$item} = 1;
 8071:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 8072:                     $changes{$item} = 1;
 8073:                 }
 8074:             } elsif ($defaultchecked{$item} eq 'off') {
 8075:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 8076:                     ($env{'form.'.$item} eq '1')) {
 8077:                     $changes{$item} = 1;
 8078:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 8079:                     $changes{$item} = 1;
 8080:                 }
 8081:             }
 8082:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 8083:         }
 8084:         foreach my $item (@numbers) {
 8085:             my ($currdef,$newdef);
 8086:             $newdef = $env{'form.'.$item};
 8087:             if ($item eq 'anonsurvey_threshold') {
 8088:                 $currdef = $domconfig{'coursedefaults'}{$item};
 8089:                 $newdef =~ s/\D//g;
 8090:                 if ($newdef eq '' || $newdef < 1) {
 8091:                     $newdef = 1;
 8092:                 }
 8093:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
 8094:             } else {
 8095:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
 8096:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 8097:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 8098:                 }
 8099:                 $newdef =~ s/[^\w.\-]//g;
 8100:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
 8101:             }
 8102:             if ($currdef ne $newdef) {
 8103:                 my $staticdef;
 8104:                 if ($item eq 'anonsurvey_threshold') {
 8105:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
 8106:                         $changes{$item} = 1;
 8107:                     }
 8108:                 } else {
 8109:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
 8110:                         $changes{'uploadquota'} = 1;
 8111:                     }
 8112:                 }
 8113:             }
 8114:         }
 8115:         my $officialcreds = $env{'form.official_credits'};
 8116:         $officialcreds =~ s/^[^\d\.]//g;
 8117:         my $unofficialcreds = $env{'form.unofficial_credits'};
 8118:         $unofficialcreds =~ s/^[^\d\.]//g;
 8119:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
 8120:                 ($env{'form.coursecredits'} eq '1')) {
 8121:                 $changes{'coursecredits'} = 1;
 8122:         } else {
 8123:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
 8124:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds)) {
 8125:                 $changes{'coursecredits'} = 1;
 8126:             }
 8127:         }
 8128:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
 8129:             official   => $officialcreds,
 8130:             unofficial => $unofficialcreds,
 8131:         }
 8132:     }
 8133:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 8134:                                              $dom);
 8135:     if ($putresult eq 'ok') {
 8136:         my %domdefaults;
 8137:         if (keys(%changes) > 0) {
 8138:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'}) || ($changes{'uploadquota'})) {
 8139:                 %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 8140:                 if ($changes{'canuse_pdfforms'}) {
 8141:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 8142:                 }
 8143:                 if ($changes{'coursecredits'}) {
 8144:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 8145:                         $domdefaults{'officialcredits'} =
 8146:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
 8147:                         $domdefaults{'unofficialcredits'} =
 8148:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
 8149:                     }
 8150:                 }
 8151:                 if ($changes{'uploadquota'}) {
 8152:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 8153:                         foreach my $type (@types) {
 8154:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
 8155:                         }
 8156:                     }
 8157:                 }
 8158:                 my $cachetime = 24*60*60;
 8159:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8160:             }
 8161:             $resulttext = &mt('Changes made:').'<ul>';
 8162:             foreach my $item (sort(keys(%changes))) {
 8163:                 if ($item eq 'canuse_pdfforms') {
 8164:                     if ($env{'form.'.$item} eq '1') {
 8165:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 8166:                     } else {
 8167:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 8168:                     }
 8169:                 } elsif ($item eq 'anonsurvey_threshold') {
 8170:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 8171:                 } elsif ($item eq 'uploadquota') {
 8172:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 8173:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
 8174:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
 8175:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
 8176:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
 8177:                                        '</ul>'.
 8178:                                        '</li>';
 8179:                     } else {
 8180:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
 8181:                     }
 8182:                 } elsif ($item eq 'coursecredits') {
 8183:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 8184:                         if (($domdefaults{'officialcredits'} eq '') &&
 8185:                             ($domdefaults{'unofficialcredits'} eq '')) {
 8186:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 8187:                         } else {
 8188:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
 8189:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
 8190:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
 8191:                                            '</ul>'.
 8192:                                            '</li>';
 8193:                         }
 8194:                     } else {
 8195:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 8196:                     }
 8197:                 }
 8198:             }
 8199:             $resulttext .= '</ul>';
 8200:         } else {
 8201:             $resulttext = &mt('No changes made to course defaults');
 8202:         }
 8203:     } else {
 8204:         $resulttext = '<span class="LC_error">'.
 8205:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8206:     }
 8207:     return $resulttext;
 8208: }
 8209: 
 8210: sub modify_usersessions {
 8211:     my ($dom,%domconfig) = @_;
 8212:     my @hostingtypes = ('version','excludedomain','includedomain');
 8213:     my @offloadtypes = ('primary','default');
 8214:     my %types = (
 8215:                   remote => \@hostingtypes,
 8216:                   hosted => \@hostingtypes,
 8217:                   spares => \@offloadtypes,
 8218:                 );
 8219:     my @prefixes = ('remote','hosted','spares');
 8220:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 8221:     my (%by_ip,%by_location,@intdoms);
 8222:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 8223:     my @locations = sort(keys(%by_location));
 8224:     my (%defaultshash,%changes);
 8225:     foreach my $prefix (@prefixes) {
 8226:         $defaultshash{'usersessions'}{$prefix} = {};
 8227:     }
 8228:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 8229:     my $resulttext;
 8230:     my %iphost = &Apache::lonnet::get_iphost();
 8231:     foreach my $prefix (@prefixes) {
 8232:         next if ($prefix eq 'spares');
 8233:         foreach my $type (@{$types{$prefix}}) {
 8234:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 8235:             if ($type eq 'version') {
 8236:                 my $value = $env{'form.'.$prefix.'_'.$type};
 8237:                 my $okvalue;
 8238:                 if ($value ne '') {
 8239:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 8240:                         $okvalue = $value;
 8241:                     }
 8242:                 }
 8243:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8244:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 8245:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 8246:                             if ($inuse == 0) {
 8247:                                 $changes{$prefix}{$type} = 1;
 8248:                             } else {
 8249:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 8250:                                     $changes{$prefix}{$type} = 1;
 8251:                                 }
 8252:                                 if ($okvalue ne '') {
 8253:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8254:                                 } 
 8255:                             }
 8256:                         } else {
 8257:                             if (($inuse == 1) && ($okvalue ne '')) {
 8258:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8259:                                 $changes{$prefix}{$type} = 1;
 8260:                             }
 8261:                         }
 8262:                     } else {
 8263:                         if (($inuse == 1) && ($okvalue ne '')) {
 8264:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8265:                             $changes{$prefix}{$type} = 1;
 8266:                         }
 8267:                     }
 8268:                 } else {
 8269:                     if (($inuse == 1) && ($okvalue ne '')) {
 8270:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8271:                         $changes{$prefix}{$type} = 1;
 8272:                     }
 8273:                 }
 8274:             } else {
 8275:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 8276:                 my @okvals;
 8277:                 foreach my $val (@vals) {
 8278:                     if ($val =~ /:/) {
 8279:                         my @items = split(/:/,$val);
 8280:                         foreach my $item (@items) {
 8281:                             if (ref($by_location{$item}) eq 'ARRAY') {
 8282:                                 push(@okvals,$item);
 8283:                             }
 8284:                         }
 8285:                     } else {
 8286:                         if (ref($by_location{$val}) eq 'ARRAY') {
 8287:                             push(@okvals,$val);
 8288:                         }
 8289:                     }
 8290:                 }
 8291:                 @okvals = sort(@okvals);
 8292:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8293:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 8294:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 8295:                             if ($inuse == 0) {
 8296:                                 $changes{$prefix}{$type} = 1; 
 8297:                             } else {
 8298:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8299:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 8300:                                 if (@changed > 0) {
 8301:                                     $changes{$prefix}{$type} = 1;
 8302:                                 }
 8303:                             }
 8304:                         } else {
 8305:                             if ($inuse == 1) {
 8306:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8307:                                 $changes{$prefix}{$type} = 1;
 8308:                             }
 8309:                         } 
 8310:                     } else {
 8311:                         if ($inuse == 1) {
 8312:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8313:                             $changes{$prefix}{$type} = 1;
 8314:                         }
 8315:                     }
 8316:                 } else {
 8317:                     if ($inuse == 1) {
 8318:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8319:                         $changes{$prefix}{$type} = 1;
 8320:                     }
 8321:                 }
 8322:             }
 8323:         }
 8324:     }
 8325: 
 8326:     my @alldoms = &Apache::lonnet::all_domains();
 8327:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8328:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 8329:     my $savespares;
 8330: 
 8331:     foreach my $lonhost (sort(keys(%servers))) {
 8332:         my $serverhomeID =
 8333:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 8334:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 8335:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 8336:         my %spareschg;
 8337:         foreach my $type (@{$types{'spares'}}) {
 8338:             my @okspares;
 8339:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 8340:             foreach my $server (@checked) {
 8341:                 if (&Apache::lonnet::hostname($server) ne '') {
 8342:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 8343:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 8344:                             push(@okspares,$server);
 8345:                         }
 8346:                     }
 8347:                 }
 8348:             }
 8349:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 8350:             my $newspare;
 8351:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 8352:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 8353:                     $newspare = $new;
 8354:                 }
 8355:             }
 8356:             my @spares;
 8357:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 8358:                 @spares = sort(@okspares,$newspare);
 8359:             } else {
 8360:                 @spares = sort(@okspares);
 8361:             }
 8362:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 8363:             if (ref($spareid{$lonhost}) eq 'HASH') {
 8364:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 8365:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 8366:                     if (@diffs > 0) {
 8367:                         $spareschg{$type} = 1;
 8368:                     }
 8369:                 }
 8370:             }
 8371:         }
 8372:         if (keys(%spareschg) > 0) {
 8373:             $changes{'spares'}{$lonhost} = \%spareschg;
 8374:         }
 8375:     }
 8376: 
 8377:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8378:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 8379:             if (ref($changes{'spares'}) eq 'HASH') {
 8380:                 if (keys(%{$changes{'spares'}}) > 0) {
 8381:                     $savespares = 1;
 8382:                 }
 8383:             }
 8384:         } else {
 8385:             $savespares = 1;
 8386:         }
 8387:     }
 8388: 
 8389:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 8390:     if ((keys(%changes) > 0) || ($savespares)) {
 8391:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 8392:                                                  $dom);
 8393:         if ($putresult eq 'ok') {
 8394:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8395:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 8396:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 8397:                 }
 8398:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 8399:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 8400:                 }
 8401:             }
 8402:             my $cachetime = 24*60*60;
 8403:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8404:             if (keys(%changes) > 0) {
 8405:                 my %lt = &usersession_titles();
 8406:                 $resulttext = &mt('Changes made:').'<ul>';
 8407:                 foreach my $prefix (@prefixes) {
 8408:                     if (ref($changes{$prefix}) eq 'HASH') {
 8409:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 8410:                         if ($prefix eq 'spares') {
 8411:                             if (ref($changes{$prefix}) eq 'HASH') {
 8412:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 8413:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 8414:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 8415:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,'spares',$lonhostdom);
 8416:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 8417:                                         foreach my $type (@{$types{$prefix}}) {
 8418:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 8419:                                                 my $offloadto = &mt('None');
 8420:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 8421:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 8422:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 8423:                                                     }
 8424:                                                 }
 8425:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 8426:                                             }
 8427:                                         }
 8428:                                     }
 8429:                                     $resulttext .= '</li>';
 8430:                                 }
 8431:                             }
 8432:                         } else {
 8433:                             foreach my $type (@{$types{$prefix}}) {
 8434:                                 if (defined($changes{$prefix}{$type})) {
 8435:                                     my $newvalue;
 8436:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8437:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 8438:                                             if ($type eq 'version') {
 8439:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 8440:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 8441:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 8442:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 8443:                                                 }
 8444:                                             }
 8445:                                         }
 8446:                                     }
 8447:                                     if ($newvalue eq '') {
 8448:                                         if ($type eq 'version') {
 8449:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 8450:                                         } else {
 8451:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 8452:                                         }
 8453:                                     } else {
 8454:                                         if ($type eq 'version') {
 8455:                                             $newvalue .= ' '.&mt('(or later)'); 
 8456:                                         }
 8457:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 8458:                                     }
 8459:                                 }
 8460:                             }
 8461:                         }
 8462:                         $resulttext .= '</ul>';
 8463:                     }
 8464:                 }
 8465:                 $resulttext .= '</ul>';
 8466:             } else {
 8467:                 $resulttext = $nochgmsg;
 8468:             }
 8469:         } else {
 8470:             $resulttext = '<span class="LC_error">'.
 8471:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8472:         }
 8473:     } else {
 8474:         $resulttext = $nochgmsg;
 8475:     }
 8476:     return $resulttext;
 8477: }
 8478: 
 8479: sub modify_loadbalancing {
 8480:     my ($dom,%domconfig) = @_;
 8481:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8482:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8483:     my ($othertitle,$usertypes,$types) =
 8484:         &Apache::loncommon::sorted_inst_types($dom);
 8485:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8486:     my @sparestypes = ('primary','default');
 8487:     my %typetitles = &sparestype_titles();
 8488:     my $resulttext;
 8489:     my (%currbalancer,%currtargets,%currrules,%existing);
 8490:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8491:         %existing = %{$domconfig{'loadbalancing'}};
 8492:     }
 8493:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 8494:                               \%currtargets,\%currrules);
 8495:     my ($saveloadbalancing,%defaultshash,%changes);
 8496:     my ($alltypes,$othertypes,$titles) =
 8497:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 8498:     my %ruletitles = &offloadtype_text();
 8499:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
 8500:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
 8501:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
 8502:         if ($balancer eq '') {
 8503:             next;
 8504:         }
 8505:         if (!exists($servers{$balancer})) { 
 8506:             if (exists($currbalancer{$balancer})) {
 8507:                 push(@{$changes{'delete'}},$balancer);
 8508:             }
 8509:             next;
 8510:         }
 8511:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
 8512:             push(@{$changes{'delete'}},$balancer);
 8513:             next;
 8514:         }
 8515:         if (!exists($currbalancer{$balancer})) {
 8516:             push(@{$changes{'add'}},$balancer);
 8517:         }
 8518:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
 8519:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
 8520:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
 8521:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8522:             $saveloadbalancing = 1;
 8523:         }
 8524:         foreach my $sparetype (@sparestypes) {
 8525:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
 8526:             my @offloadto;
 8527:             foreach my $target (@targets) {
 8528:                 if (($servers{$target}) && ($target ne $balancer)) {
 8529:                     if ($sparetype eq 'default') {
 8530:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
 8531:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
 8532:                         }
 8533:                     }
 8534:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
 8535:                         push(@offloadto,$target);
 8536:                     }
 8537:                 }
 8538:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
 8539:             }
 8540:         }
 8541:         if (ref($currtargets{$balancer}) eq 'HASH') {
 8542:             foreach my $sparetype (@sparestypes) {
 8543:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
 8544:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
 8545:                     if (@targetdiffs > 0) {
 8546:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8547:                     }
 8548:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8549:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8550:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 8551:                     }
 8552:                 }
 8553:             }
 8554:         } else {
 8555:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
 8556:                 foreach my $sparetype (@sparestypes) { 
 8557:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8558:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8559:                             $changes{'curr'}{$balancer}{'targets'} = 1;
 8560:                         }
 8561:                     }
 8562:                 }
 8563:             }  
 8564:         }
 8565:         my $ishomedom;
 8566:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 8567:             $ishomedom = 1;
 8568:         }
 8569:         if (ref($alltypes) eq 'ARRAY') {
 8570:             foreach my $type (@{$alltypes}) {
 8571:                 my $rule;
 8572:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) && 
 8573:                          (!$ishomedom)) {
 8574:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
 8575:                 }
 8576:                 if ($rule eq 'specific') {
 8577:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
 8578:                 }
 8579:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
 8580:                 if (ref($currrules{$balancer}) eq 'HASH') {
 8581:                     if ($rule ne $currrules{$balancer}{$type}) {
 8582:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8583:                     }
 8584:                 } elsif ($rule ne '') {
 8585:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 8586:                 }
 8587:             }
 8588:         }
 8589:     }
 8590:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 8591:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 8592:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
 8593:             $defaultshash{'loadbalancing'} = {};
 8594:         }
 8595:         my $putresult = &Apache::lonnet::put_dom('configuration',
 8596:                                                  \%defaultshash,$dom);
 8597:  
 8598:         if ($putresult eq 'ok') {
 8599:             if (keys(%changes) > 0) {
 8600:                 if (ref($changes{'delete'}) eq 'ARRAY') {
 8601:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
 8602:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
 8603:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8604:                     }
 8605:                 }
 8606:                 if (ref($changes{'add'}) eq 'ARRAY') {
 8607:                     foreach my $balancer (sort(@{$changes{'add'}})) {   
 8608:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
 8609:                     }
 8610:                 }
 8611:                 if (ref($changes{'curr'}) eq 'HASH') {
 8612:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
 8613:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
 8614:                             if ($changes{'curr'}{$balancer}{'targets'}) {
 8615:                                 my %offloadstr;
 8616:                                 foreach my $sparetype (@sparestypes) {
 8617:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 8618:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 8619:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
 8620:                                         }
 8621:                                     }
 8622:                                 }
 8623:                                 if (keys(%offloadstr) == 0) {
 8624:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 8625:                                 } else {
 8626:                                     my $showoffload;
 8627:                                     foreach my $sparetype (@sparestypes) {
 8628:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
 8629:                                         if (defined($offloadstr{$sparetype})) {
 8630:                                             $showoffload .= $offloadstr{$sparetype};
 8631:                                         } else {
 8632:                                             $showoffload .= &mt('None');
 8633:                                         }
 8634:                                         $showoffload .= ('&nbsp;'x3);
 8635:                                     }
 8636:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
 8637:                                 }
 8638:                             }
 8639:                         }
 8640:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
 8641:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
 8642:                                 foreach my $type (@{$alltypes}) {
 8643:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
 8644:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
 8645:                                         my $balancetext;
 8646:                                         if ($rule eq '') {
 8647:                                             $balancetext =  $ruletitles{'default'};
 8648:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
 8649:                                                  ($rule eq 'balancer') || ($rule eq 'offloadedto')) {
 8650:                                             $balancetext =  $ruletitles{$rule};
 8651:                                         } else {
 8652:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
 8653:                                         }
 8654:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';     
 8655:                                     }
 8656:                                 }
 8657:                             }
 8658:                         }
 8659:                         &Apache::lonnet::remote_devalidate_cache($balancer,'loadbalancing',$dom);
 8660:                     }
 8661:                 }
 8662:                 if ($resulttext ne '') {
 8663:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
 8664:                 } else {
 8665:                     $resulttext = $nochgmsg;
 8666:                 }
 8667:             } else {
 8668:                 $resulttext = $nochgmsg;
 8669:             }
 8670:         } else {
 8671:             $resulttext = '<span class="LC_error">'.
 8672:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8673:         }
 8674:     } else {
 8675:         $resulttext = $nochgmsg;
 8676:     }
 8677:     return $resulttext;
 8678: }
 8679: 
 8680: sub recurse_check {
 8681:     my ($chkcats,$categories,$depth,$name) = @_;
 8682:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 8683:         my $chg = 0;
 8684:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 8685:             my $category = $chkcats->[$depth]{$name}[$j];
 8686:             my $item;
 8687:             if ($category eq '') {
 8688:                 $chg ++;
 8689:             } else {
 8690:                 my $deeper = $depth + 1;
 8691:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 8692:                 if ($chg) {
 8693:                     $categories->{$item} -= $chg;
 8694:                 }
 8695:                 &recurse_check($chkcats,$categories,$deeper,$category);
 8696:                 $deeper --;
 8697:             }
 8698:         }
 8699:     }
 8700:     return;
 8701: }
 8702: 
 8703: sub recurse_cat_deletes {
 8704:     my ($item,$coursecategories,$deletions) = @_;
 8705:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8706:     my $subdepth = $depth + 1;
 8707:     if (ref($coursecategories) eq 'HASH') {
 8708:         foreach my $subitem (keys(%{$coursecategories})) {
 8709:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 8710:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 8711:                 delete($coursecategories->{$subitem});
 8712:                 $deletions->{$subitem} = 1;
 8713:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 8714:             }
 8715:         }
 8716:     }
 8717:     return;
 8718: }
 8719: 
 8720: sub get_active_dcs {
 8721:     my ($dom) = @_;
 8722:     my $now = time;
 8723:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
 8724:     my %domcoords;
 8725:     my $numdcs = 0;
 8726:     foreach my $server (keys(%dompersonnel)) {
 8727:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 8728:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 8729:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 8730:         }
 8731:     }
 8732:     return %domcoords;
 8733: }
 8734: 
 8735: sub active_dc_picker {
 8736:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
 8737:     my %domcoords = &get_active_dcs($dom); 
 8738:     my @domcoord = keys(%domcoords);
 8739:     if (keys(%currhash)) {
 8740:         foreach my $dc (keys(%currhash)) {
 8741:             unless (exists($domcoords{$dc})) {
 8742:                 push(@domcoord,$dc);
 8743:             }
 8744:         }
 8745:     }
 8746:     @domcoord = sort(@domcoord);
 8747:     my $numdcs = scalar(@domcoord); 
 8748:     my $rows = 0;
 8749:     my $table;
 8750:     if ($numdcs > 1) {
 8751:         $table = '<table>';
 8752:         for (my $i=0; $i<@domcoord; $i++) {
 8753:             my $rem = $i%($numinrow);
 8754:             if ($rem == 0) {
 8755:                 if ($i > 0) {
 8756:                     $table .= '</tr>';
 8757:                 }
 8758:                 $table .= '<tr>';
 8759:                 $rows ++;
 8760:             }
 8761:             my $check = '';
 8762:             if ($inputtype eq 'radio') {
 8763:                 if (keys(%currhash) == 0) {
 8764:                     if (!$i) {
 8765:                         $check = ' checked="checked"';
 8766:                     }
 8767:                 } elsif (exists($currhash{$domcoord[$i]})) {
 8768:                     $check = ' checked="checked"';
 8769:                 }
 8770:             } else {
 8771:                 if (exists($currhash{$domcoord[$i]})) {
 8772:                     $check = ' checked="checked"';
 8773:                 }
 8774:             }
 8775:             if ($i == @domcoord - 1) {
 8776:                 my $colsleft = $numinrow - $rem;
 8777:                 if ($colsleft > 1) {
 8778:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
 8779:                 } else {
 8780:                     $table .= '<td class="LC_left_item">';
 8781:                 }
 8782:             } else {
 8783:                 $table .= '<td class="LC_left_item">';
 8784:             }
 8785:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
 8786:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
 8787:             $table .= '<span class="LC_nobreak"><label>'.
 8788:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
 8789:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
 8790:             if ($user ne $dcname.':'.$dcdom) {
 8791:                 $table .=  ' ('.$dcname.':'.$dcdom.')'.
 8792:                            '</label></span></td>';
 8793:             }
 8794:         }
 8795:         $table .= '</tr></table>';
 8796:     } elsif ($numdcs == 1) {
 8797:         if ($inputtype eq 'radio') {
 8798:             $table .= '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />';
 8799:         } else {
 8800:             my $check;
 8801:             if (exists($currhash{$domcoord[0]})) {
 8802:                 $check = ' checked="checked"';
 8803:             }
 8804:             $table .= '<input type="checkbox" name="'.$name.'" '.
 8805:                       'value="'.$domcoord[0].'"'.$check.' />';
 8806:             $rows ++;
 8807:         }
 8808:     }
 8809:     return ($numdcs,$table,$rows);
 8810: }
 8811: 
 8812: sub usersession_titles {
 8813:     return &Apache::lonlocal::texthash(
 8814:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
 8815:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
 8816:                spares => 'Servers offloaded to, when busy',
 8817:                version => 'LON-CAPA version requirement',
 8818:                excludedomain => 'Allow all, but exclude specific domains',
 8819:                includedomain => 'Deny all, but include specific domains',
 8820:                primary => 'Primary (checked first)',
 8821:                default => 'Default',
 8822:            );
 8823: }
 8824: 
 8825: sub id_for_thisdom {
 8826:     my (%servers) = @_;
 8827:     my %altids;
 8828:     foreach my $server (keys(%servers)) {
 8829:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 8830:         if ($serverhome ne $server) {
 8831:             $altids{$serverhome} = $server;
 8832:         }
 8833:     }
 8834:     return %altids;
 8835: }
 8836: 
 8837: sub count_servers {
 8838:     my ($currbalancer,%servers) = @_;
 8839:     my (@spares,$numspares);
 8840:     foreach my $lonhost (sort(keys(%servers))) {
 8841:         next if ($currbalancer eq $lonhost);
 8842:         push(@spares,$lonhost);
 8843:     }
 8844:     if ($currbalancer) {
 8845:         $numspares = scalar(@spares);
 8846:     } else {
 8847:         $numspares = scalar(@spares) - 1;
 8848:     }
 8849:     return ($numspares,@spares);
 8850: }
 8851: 
 8852: sub lonbalance_targets_js {
 8853:     my ($dom,$types,$servers,$settings) = @_;
 8854:     my $select = &mt('Select');
 8855:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
 8856:     if (ref($servers) eq 'HASH') {
 8857:         $alltargets = join("','",sort(keys(%{$servers})));
 8858:         my @homedoms;
 8859:         foreach my $server (sort(keys(%{$servers}))) {
 8860:             if (&Apache::lonnet::host_domain($server) eq $dom) {
 8861:                 push(@homedoms,'1');
 8862:             } else {
 8863:                 push(@homedoms,'0');
 8864:             }
 8865:         }
 8866:         $allishome = join("','",@homedoms);
 8867:     }
 8868:     if (ref($types) eq 'ARRAY') {
 8869:         if (@{$types} > 0) {
 8870:             @alltypes = @{$types};
 8871:         }
 8872:     }
 8873:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 8874:     $allinsttypes = join("','",@alltypes);
 8875:     my (%currbalancer,%currtargets,%currrules,%existing);
 8876:     if (ref($settings) eq 'HASH') {
 8877:         %existing = %{$settings};
 8878:     }
 8879:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
 8880:                               \%currtargets,\%currrules);
 8881:     my $balancers = join("','",sort(keys(%currbalancer))); 
 8882:     return <<"END";
 8883: 
 8884: <script type="text/javascript">
 8885: // <![CDATA[
 8886: 
 8887: currBalancers = new Array('$balancers');
 8888: 
 8889: function toggleTargets(balnum) {
 8890:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 8891:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
 8892:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
 8893:     var prevbalancer = prevhostitem.value;
 8894:     var baltotal = document.getElementById('loadbalancing_total').value;
 8895:     prevhostitem.value = balancer;
 8896:     if (prevbalancer != '') {
 8897:         var prevIdx = currBalancers.indexOf(prevbalancer);
 8898:         if (prevIdx != -1) {
 8899:             currBalancers.splice(prevIdx,1);
 8900:         }
 8901:     }
 8902:     if (balancer == '') {
 8903:         hideSpares(balnum);
 8904:     } else {
 8905:         var currIdx = currBalancers.indexOf(balancer);
 8906:         if (currIdx == -1) {
 8907:             currBalancers.push(balancer);
 8908:         }
 8909:         var homedoms = new Array('$allishome');
 8910:         var ishomedom = homedoms[lonhostitem.selectedIndex];
 8911:         showSpares(balancer,ishomedom,balnum);
 8912:     }
 8913:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
 8914:     return;
 8915: }
 8916: 
 8917: function showSpares(balancer,ishomedom,balnum) {
 8918:     var alltargets = new Array('$alltargets');
 8919:     var insttypes = new Array('$allinsttypes');
 8920:     var offloadtypes = new Array('primary','default');
 8921: 
 8922:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
 8923:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
 8924:  
 8925:     for (var i=0; i<offloadtypes.length; i++) {
 8926:         var count = 0;
 8927:         for (var j=0; j<alltargets.length; j++) {
 8928:             if (alltargets[j] != balancer) {
 8929:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
 8930:                 item.value = alltargets[j];
 8931:                 item.style.textAlign='left';
 8932:                 item.style.textFace='normal';
 8933:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
 8934:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
 8935:                     item.disabled = '';
 8936:                 } else {
 8937:                     item.disabled = 'disabled';
 8938:                     item.checked = false;
 8939:                 }
 8940:                 count ++;
 8941:             }
 8942:         }
 8943:     }
 8944:     for (var k=0; k<insttypes.length; k++) {
 8945:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
 8946:             if (ishomedom == 1) {
 8947:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8948:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8949:             } else {
 8950:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 8951:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 8952: 
 8953:             }
 8954:         } else {
 8955:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 8956:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 8957:         }
 8958:         if ((insttypes[k] != '_LC_external') && 
 8959:             ((insttypes[k] != '_LC_internetdom') ||
 8960:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
 8961:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
 8962:             item.options.length = 0;
 8963:             item.options[0] = new Option("","",true,true);
 8964:             var idx = 0;  
 8965:             for (var m=0; m<alltargets.length; m++) {
 8966:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
 8967:                     idx ++;
 8968:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 8969:                     
 8970:                 }
 8971:             }
 8972:         }
 8973:     }
 8974:     return;
 8975: }
 8976: 
 8977: function hideSpares(balnum) {
 8978:     var alltargets = new Array('$alltargets');
 8979:     var insttypes = new Array('$allinsttypes');
 8980:     var offloadtypes = new Array('primary','default');
 8981: 
 8982:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
 8983:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
 8984: 
 8985:     var total = alltargets.length - 1;
 8986:     for (var i=0; i<offloadtypes; i++) {
 8987:         for (var j=0; j<total; j++) {
 8988:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
 8989:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
 8990:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
 8991:         }
 8992:     }
 8993:     for (var k=0; k<insttypes.length; k++) {
 8994:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 8995:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 8996:         if (insttypes[k] != '_LC_external') {
 8997:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
 8998:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
 8999:         }
 9000:     }
 9001:     return;
 9002: }
 9003: 
 9004: function checkOffloads(item,balnum,type) {
 9005:     var alltargets = new Array('$alltargets');
 9006:     var offloadtypes = new Array('primary','default');
 9007:     if (item.checked) {
 9008:         var total = alltargets.length - 1;
 9009:         var other;
 9010:         if (type == offloadtypes[0]) {
 9011:             other = offloadtypes[1];
 9012:         } else {
 9013:             other = offloadtypes[0];
 9014:         }
 9015:         for (var i=0; i<total; i++) {
 9016:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
 9017:             if (server == item.value) {
 9018:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
 9019:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
 9020:                 }
 9021:             }
 9022:         }
 9023:     }
 9024:     return;
 9025: }
 9026: 
 9027: function singleServerToggle(balnum,type) {
 9028:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
 9029:     if (offloadtoSelIdx == 0) {
 9030:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
 9031:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 9032: 
 9033:     } else {
 9034:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
 9035:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 9036:     }
 9037:     return;
 9038: }
 9039: 
 9040: function balanceruleChange(formname,balnum,type) {
 9041:     if (type == '_LC_external') {
 9042:         return;
 9043:     }
 9044:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
 9045:     for (var i=0; i<typesRules.length; i++) {
 9046:         if (formname.elements[typesRules[i]].checked) {
 9047:             if (formname.elements[typesRules[i]].value != 'specific') {
 9048:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
 9049:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 9050:             } else {
 9051:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 9052:             }
 9053:         }
 9054:     }
 9055:     return;
 9056: }
 9057: 
 9058: function balancerDeleteChange(balnum) {
 9059:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 9060:     var baltotal = document.getElementById('loadbalancing_total').value;
 9061:     var addtarget;
 9062:     var removetarget;
 9063:     var action = 'delete';
 9064:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
 9065:         var lonhost = hostitem.value;
 9066:         var currIdx = currBalancers.indexOf(lonhost);
 9067:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
 9068:             if (currIdx != -1) {
 9069:                 currBalancers.splice(currIdx,1);
 9070:             }
 9071:             addtarget = lonhost;
 9072:         } else {
 9073:             if (currIdx == -1) {
 9074:                 currBalancers.push(lonhost);
 9075:             }
 9076:             removetarget = lonhost;
 9077:             action = 'undelete';
 9078:         }
 9079:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
 9080:     }
 9081:     return;
 9082: }
 9083: 
 9084: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
 9085:     if (baltotal > 1) {
 9086:         var offloadtypes = new Array('primary','default');
 9087:         var alltargets = new Array('$alltargets');
 9088:         var insttypes = new Array('$allinsttypes');
 9089:         for (var i=0; i<baltotal; i++) {
 9090:             if (i != balnum) {
 9091:                 for (var j=0; j<offloadtypes.length; j++) {
 9092:                     var total = alltargets.length - 1;
 9093:                     for (var k=0; k<total; k++) {
 9094:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
 9095:                         var server = serveritem.value;
 9096:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
 9097:                             if (server == addtarget) {
 9098:                                 serveritem.disabled = '';
 9099:                             }
 9100:                         }
 9101:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 9102:                             if (server == removetarget) {
 9103:                                 serveritem.disabled = 'disabled';
 9104:                                 serveritem.checked = false;
 9105:                             }
 9106:                         }
 9107:                     }
 9108:                 }
 9109:                 for (var j=0; j<insttypes.length; j++) {
 9110:                     if (insttypes[j] != '_LC_external') {
 9111:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
 9112:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
 9113:                             var currSel = singleserver.selectedIndex;
 9114:                             var currVal = singleserver.options[currSel].value;
 9115:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
 9116:                                 var numoptions = singleserver.options.length;
 9117:                                 var needsnew = 1;
 9118:                                 for (var k=0; k<numoptions; k++) {
 9119:                                     if (singleserver.options[k] == addtarget) {
 9120:                                         needsnew = 0;
 9121:                                         break;
 9122:                                     }
 9123:                                 }
 9124:                                 if (needsnew == 1) {
 9125:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
 9126:                                 }
 9127:                             }
 9128:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 9129:                                 singleserver.options.length = 0;
 9130:                                 if ((currVal) && (currVal != removetarget)) {
 9131:                                     singleserver.options[0] = new Option("","",false,false);
 9132:                                 } else {
 9133:                                     singleserver.options[0] = new Option("","",true,true);
 9134:                                 }
 9135:                                 var idx = 0;
 9136:                                 for (var m=0; m<alltargets.length; m++) {
 9137:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
 9138:                                         idx ++;
 9139:                                         if (currVal == alltargets[m]) {
 9140:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
 9141:                                         } else {
 9142:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 9143:                                         }
 9144:                                     }
 9145:                                 }
 9146:                             }
 9147:                         }
 9148:                     }
 9149:                 }
 9150:             }
 9151:         }
 9152:     }
 9153:     return;
 9154: }
 9155: 
 9156: // ]]>
 9157: </script>
 9158: 
 9159: END
 9160: }
 9161: 
 9162: sub new_spares_js {
 9163:     my @sparestypes = ('primary','default');
 9164:     my $types = join("','",@sparestypes);
 9165:     my $select = &mt('Select');
 9166:     return <<"END";
 9167: 
 9168: <script type="text/javascript">
 9169: // <![CDATA[
 9170: 
 9171: function updateNewSpares(formname,lonhost) {
 9172:     var types = new Array('$types');
 9173:     var include = new Array();
 9174:     var exclude = new Array();
 9175:     for (var i=0; i<types.length; i++) {
 9176:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
 9177:         for (var j=0; j<spareboxes.length; j++) {
 9178:             if (formname.elements[spareboxes[j]].checked) {
 9179:                 exclude.push(formname.elements[spareboxes[j]].value);
 9180:             } else {
 9181:                 include.push(formname.elements[spareboxes[j]].value);
 9182:             }
 9183:         }
 9184:     }
 9185:     for (var i=0; i<types.length; i++) {
 9186:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
 9187:         var selIdx = newSpare.selectedIndex;
 9188:         var currnew = newSpare.options[selIdx].value;
 9189:         var okSpares = new Array();
 9190:         for (var j=0; j<newSpare.options.length; j++) {
 9191:             var possible = newSpare.options[j].value;
 9192:             if (possible != '') {
 9193:                 if (exclude.indexOf(possible) == -1) {
 9194:                     okSpares.push(possible);
 9195:                 } else {
 9196:                     if (currnew == possible) {
 9197:                         selIdx = 0;
 9198:                     }
 9199:                 }
 9200:             }
 9201:         }
 9202:         for (var k=0; k<include.length; k++) {
 9203:             if (okSpares.indexOf(include[k]) == -1) {
 9204:                 okSpares.push(include[k]);
 9205:             }
 9206:         }
 9207:         okSpares.sort();
 9208:         newSpare.options.length = 0;
 9209:         if (selIdx == 0) {
 9210:             newSpare.options[0] = new Option("$select","",true,true);
 9211:         } else {
 9212:             newSpare.options[0] = new Option("$select","",false,false);
 9213:         }
 9214:         for (var m=0; m<okSpares.length; m++) {
 9215:             var idx = m+1;
 9216:             var selThis = 0;
 9217:             if (selIdx != 0) {
 9218:                 if (okSpares[m] == currnew) {
 9219:                     selThis = 1;
 9220:                 }
 9221:             }
 9222:             if (selThis == 1) {
 9223:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
 9224:             } else {
 9225:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
 9226:             }
 9227:         }
 9228:     }
 9229:     return;
 9230: }
 9231: 
 9232: function checkNewSpares(lonhost,type) {
 9233:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
 9234:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
 9235:     if (chosen != '') { 
 9236:         var othertype;
 9237:         var othernewSpare;
 9238:         if (type == 'primary') {
 9239:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
 9240:         }
 9241:         if (type == 'default') {
 9242:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
 9243:         }
 9244:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
 9245:             othernewSpare.selectedIndex = 0;
 9246:         }
 9247:     }
 9248:     return;
 9249: }
 9250: 
 9251: // ]]>
 9252: </script>
 9253: 
 9254: END
 9255: 
 9256: }
 9257: 
 9258: sub common_domprefs_js {
 9259:     return <<"END";
 9260: 
 9261: <script type="text/javascript">
 9262: // <![CDATA[
 9263: 
 9264: function getIndicesByName(formname,item) {
 9265:     var group = new Array();
 9266:     for (var i=0;i<formname.elements.length;i++) {
 9267:         if (formname.elements[i].name == item) {
 9268:             group.push(formname.elements[i].id);
 9269:         }
 9270:     }
 9271:     return group;
 9272: }
 9273: 
 9274: // ]]>
 9275: </script>
 9276: 
 9277: END
 9278: 
 9279: }
 9280: 
 9281: sub recaptcha_js {
 9282:     my %lt = &captcha_phrases();
 9283:     return <<"END";
 9284: 
 9285: <script type="text/javascript">
 9286: // <![CDATA[
 9287: 
 9288: function updateCaptcha(caller,context) {
 9289:     var privitem;
 9290:     var pubitem;
 9291:     var privtext;
 9292:     var pubtext;
 9293:     if (document.getElementById(context+'_recaptchapub')) {
 9294:         pubitem = document.getElementById(context+'_recaptchapub');
 9295:     } else {
 9296:         return;
 9297:     }
 9298:     if (document.getElementById(context+'_recaptchapriv')) {
 9299:         privitem = document.getElementById(context+'_recaptchapriv');
 9300:     } else {
 9301:         return;
 9302:     }
 9303:     if (document.getElementById(context+'_recaptchapubtxt')) {
 9304:         pubtext = document.getElementById(context+'_recaptchapubtxt');
 9305:     } else {
 9306:         return;
 9307:     }
 9308:     if (document.getElementById(context+'_recaptchaprivtxt')) {
 9309:         privtext = document.getElementById(context+'_recaptchaprivtxt');
 9310:     } else {
 9311:         return;
 9312:     }
 9313:     if (caller.checked) {
 9314:         if (caller.value == 'recaptcha') {
 9315:             pubitem.type = 'text';
 9316:             privitem.type = 'text';
 9317:             pubitem.size = '40';
 9318:             privitem.size = '40';
 9319:             pubtext.innerHTML = "$lt{'pub'}";
 9320:             privtext.innerHTML = "$lt{'priv'}";
 9321:         } else {
 9322:             pubitem.type = 'hidden';
 9323:             privitem.type = 'hidden';
 9324:             pubtext.innerHTML = '';
 9325:             privtext.innerHTML = '';
 9326:         }
 9327:     }
 9328:     return;
 9329: }
 9330: 
 9331: // ]]>
 9332: </script>
 9333: 
 9334: END
 9335: 
 9336: }
 9337: 
 9338: sub credits_js {
 9339:     return <<"END";
 9340: 
 9341: <script type="text/javascript">
 9342: // <![CDATA[
 9343: 
 9344: function toggleCredits(domForm) {
 9345:     if (document.getElementById('credits')) {
 9346:         creditsitem = document.getElementById('credits');
 9347:         var creditsLength = domForm.coursecredits.length;
 9348:         if (creditsLength) {
 9349:             var currval;
 9350:             for (var i=0; i<creditsLength; i++) {
 9351:                 if (domForm.coursecredits[i].checked) {
 9352:                    currval = domForm.coursecredits[i].value;
 9353:                 }
 9354:             }
 9355:             if (currval == 1) {
 9356:                 creditsitem.style.display = 'block';
 9357:             } else {
 9358:                 creditsitem.style.display = 'none';
 9359:             }
 9360:         }
 9361:     }
 9362:     return;
 9363: }
 9364: 
 9365: // ]]>
 9366: </script>
 9367: 
 9368: END
 9369: 
 9370: }
 9371: 
 9372: sub captcha_phrases {
 9373:     return &Apache::lonlocal::texthash (
 9374:                  priv => 'Private key',
 9375:                  pub  => 'Public key',
 9376:                  original  => 'original (CAPTCHA)',
 9377:                  recaptcha => 'successor (ReCAPTCHA)',
 9378:                  notused   => 'unused',
 9379:     );
 9380: }
 9381: 
 9382: sub devalidate_remote_domconfs {
 9383:     my ($dom) = @_;
 9384:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 9385:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 9386:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9387:     my %thismachine;
 9388:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
 9389:     if (keys(%servers) > 1) {
 9390:         foreach my $server (keys(%servers)) {
 9391:             next if ($thismachine{$server});
 9392:             &Apache::lonnet::remote_devalidate_cache($server,'domainconfig',$dom);
 9393:         }
 9394:     }
 9395:     return;
 9396: }
 9397: 
 9398: 1;

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