File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.32: download - view: text, annotated - select for diffs
Fri Jan 3 20:42:51 2014 UTC (10 years, 5 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11
  - Required changes omitted in earlier backport of rev. 1.219.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.32 2014/01/03 20:42:51 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: #
   28: ###############################################################
   29: ##############################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::domainprefs.pm
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Handles configuration of a LON-CAPA domain.  
   40: 
   41: This is part of the LearningOnline Network with CAPA project
   42: described at http://www.lon-capa.org.
   43: 
   44: 
   45: =head1 OVERVIEW
   46: 
   47: Each institution using LON-CAPA will typically have a single domain designated 
   48: for use by individuals affiliated with the institution.  Accordingly, each domain
   49: may define a default set of logos and a color scheme which can be used to "brand"
   50: the LON-CAPA instance. In addition, an institution will typically have a language
   51: and timezone which are used for the majority of courses.
   52: 
   53: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   54: host of other domain-wide settings which determine the types of functionality
   55: available to users and courses in the domain.
   56: 
   57: There is also a mechanism to configure cataloging of courses in the domain, and
   58: controls on the operation of automated processes which govern such things as
   59: roster updates, user directory updates and processing of course requests.
   60: 
   61: The domain coordination manual which is built dynamically on install/update of 
   62: LON-CAPA from the relevant help items provides more information about domain 
   63: configuration.
   64: 
   65: Most of the domain settings are stored in the configuration.db GDBM file which is
   66: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   67: where $dom is the domain.  The configuration.db stores settings in a number of 
   68: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   69: the domain as files (e.g., image files for logos etc., or plain text files for
   70: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   71: session hosted on the primary library server in the domain, as these files are 
   72: stored in author space belonging to a special $dom-domainconfig user.   
   73: 
   74: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   75: the current settings, and provides an interface to make modifications.
   76: 
   77: =head1 SUBROUTINES
   78: 
   79: =over
   80: 
   81: =item print_quotas()
   82: 
   83: Inputs: 4 
   84: 
   85: $dom,$settings,$rowtotal,$action.
   86: 
   87: $dom is the domain, $settings is a reference to a hash of current settings for
   88: the current context, $rowtotal is a reference to the scalar used to record the 
   89: number of rows displayed on the page, and $action is the context (quotas, 
   90: requestcourses or requestauthor).
   91: 
   92: The print_quotas routine was orginally created to display/store information
   93: about default quota sizes for portfolio spaces for the different types of 
   94: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   95: but is now also used to manage availability of user tools: 
   96: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   97: used by course owners to request creation of a course, and to display/store
   98: default quota sizes for authoring spaces.
   99: 
  100: Outputs: 1
  101: 
  102: $datatable  - HTML containing form elements which allow settings to be changed. 
  103: 
  104: In the case of course requests, radio buttons are displayed for each institutional
  105: affiliate type (and also default, and _LC_adv) for each of the course types 
  106: (official, unofficial, community, and textbook).  In each case the radio buttons 
  107: allow the selection of one of four values:
  108: 
  109: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  110: which have the following effects:
  111: 
  112: 0
  113: 
  114: =over
  115: 
  116: - course requests are not allowed for this course types/affiliation
  117: 
  118: =back
  119: 
  120: approval 
  121: 
  122: =over 
  123: 
  124: - course requests must be approved by a Doman Coordinator in the 
  125: course's domain
  126: 
  127: =back
  128: 
  129: validate 
  130: 
  131: =over
  132: 
  133: - an institutional validation (e.g., check requestor is instructor
  134: of record) needs to be passed before the course will be created.  The required
  135: validation is in localenroll.pm on the primary library server for the course 
  136: domain.
  137: 
  138: =back
  139: 
  140: autolimit 
  141: 
  142: =over
  143:  
  144: - course requests will be processed automatically up to a limit of
  145: N requests for the course type for the particular requestor.
  146: If N is undefined, there is no limit to the number of course requests
  147: which a course owner may submit and have processed automatically. 
  148: 
  149: =back
  150: 
  151: =item modify_quotas() 
  152: 
  153: =back
  154: 
  155: =cut
  156: 
  157: package Apache::domainprefs;
  158: 
  159: use strict;
  160: use Apache::Constants qw(:common :http);
  161: use Apache::lonnet;
  162: use Apache::loncommon();
  163: use Apache::lonhtmlcommon();
  164: use Apache::lonlocal;
  165: use Apache::lonmsg();
  166: use Apache::lonconfigsettings;
  167: use 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','coursedefaults',
  215:                 'usersessions','loadbalancing','requestauthor'],$dom);
  216:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  217:                        'autoupdate','autocreate','directorysrch','contacts',
  218:                        'usercreation','usermodification','scantron',
  219:                        'requestcourses','requestauthor','coursecategories',
  220:                        'serverstatuses','coursedefaults','usersessions');
  221:     my %existing;
  222:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  223:         %existing = %{$domconfig{'loadbalancing'}};
  224:     }
  225:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  226:         push(@prefs_order,'loadbalancing');
  227:     }
  228:     my %prefs = (
  229:         'rolecolors' =>
  230:                    { text => 'Default color schemes',
  231:                      help => 'Domain_Configuration_Color_Schemes',
  232:                      header => [{col1 => 'Student Settings',
  233:                                  col2 => '',},
  234:                                 {col1 => 'Coordinator Settings',
  235:                                  col2 => '',},
  236:                                 {col1 => 'Author Settings',
  237:                                  col2 => '',},
  238:                                 {col1 => 'Administrator Settings',
  239:                                  col2 => '',}],
  240:                     },
  241:         'login' =>
  242:                     { text => 'Log-in page options',
  243:                       help => 'Domain_Configuration_Login_Page',
  244:                       header => [{col1 => 'Log-in Page Items',
  245:                                   col2 => '',},
  246:                                  {col1 => 'Log-in Help',
  247:                                   col2 => 'Value'}],
  248:                     },
  249:         'defaults' => 
  250:                     { text => 'Default authentication/language/timezone/portal',
  251:                       help => 'Domain_Configuration_LangTZAuth',
  252:                       header => [{col1 => 'Setting',
  253:                                   col2 => 'Value'}],
  254:                     },
  255:         'quotas' => 
  256:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  257:                       help => 'Domain_Configuration_Quotas',
  258:                       header => [{col1 => 'User affiliation',
  259:                                   col2 => 'Available tools',
  260:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  261:                     },
  262:         'autoenroll' =>
  263:                    { text => 'Auto-enrollment settings',
  264:                      help => 'Domain_Configuration_Auto_Enrollment',
  265:                      header => [{col1 => 'Configuration setting',
  266:                                  col2 => 'Value(s)'}],
  267:                    },
  268:         'autoupdate' => 
  269:                    { text => 'Auto-update settings',
  270:                      help => 'Domain_Configuration_Auto_Updates',
  271:                      header => [{col1 => 'Setting',
  272:                                  col2 => 'Value',},
  273:                                 {col1 => 'Setting',
  274:                                  col2 => 'Affiliation'},
  275:                                 {col1 => 'User population',
  276:                                  col2 => 'Updateable user data'}],
  277:                   },
  278:         'autocreate' => 
  279:                   { text => 'Auto-course creation settings',
  280:                      help => 'Domain_Configuration_Auto_Creation',
  281:                      header => [{col1 => 'Configuration Setting',
  282:                                  col2 => 'Value',}],
  283:                   },
  284:         'directorysrch' => 
  285:                   { text => 'Institutional directory searches',
  286:                     help => 'Domain_Configuration_InstDirectory_Search',
  287:                     header => [{col1 => 'Setting',
  288:                                 col2 => 'Value',}],
  289:                   },
  290:         'contacts' =>
  291:                   { text => 'Contact Information',
  292:                     help => 'Domain_Configuration_Contact_Info',
  293:                     header => [{col1 => 'Setting',
  294:                                 col2 => 'Value',}],
  295:                   },
  296: 
  297:         'usercreation' => 
  298:                   { text => 'User creation',
  299:                     help => 'Domain_Configuration_User_Creation',
  300:                     header => [{col1 => 'Format rule type',
  301:                                 col2 => 'Format rules in force'},
  302:                                {col1 => 'User account creation',
  303:                                 col2 => 'Usernames which may be created',},
  304:                                {col1 => 'Context',
  305:                                 col2 => 'Assignable authentication types'}],
  306:                   },
  307:         'usermodification' =>
  308:                   { text => 'User modification',
  309:                     help => 'Domain_Configuration_User_Modification',
  310:                     header => [{col1 => 'Target user has role',
  311:                                 col2 => 'User information updateable in author context'},
  312:                                {col1 => 'Target user has role',
  313:                                 col2 => 'User information updateable in course context'},
  314:                                {col1 => "Status of user",
  315:                                 col2 => 'Information settable when self-creating account (if directory data blank)'}],
  316:                   },
  317:         'scantron' =>
  318:                   { text => 'Bubblesheet format file',
  319:                     help => 'Domain_Configuration_Scantron_Format',
  320:                     header => [ {col1 => 'Item',
  321:                                  col2 => '',
  322:                               }],
  323:                   },
  324:         'requestcourses' => 
  325:                  {text => 'Request creation of courses',
  326:                   help => 'Domain_Configuration_Request_Courses',
  327:                   header => [{col1 => 'User affiliation',
  328:                               col2 => 'Availability/Processing of requests',},
  329:                              {col1 => 'Setting',
  330:                               col2 => 'Value'},
  331:                              {col1 => 'Available textbooks',
  332:                               col2 => ''}],
  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:         'coursedefaults' =>
  360:                  {text => 'Course/Community defaults',
  361:                   help => 'Domain_Configuration_Course_Defaults',
  362:                   header => [{col1 => 'Defaults which can be overridden for each course by a DC',
  363:                               col2 => 'Value',},],
  364:                  },
  365:         'usersessions' =>
  366:                  {text  => 'User session hosting/offloading',
  367:                   help  => 'Domain_Configuration_User_Sessions',
  368:                   header => [{col1 => 'Domain server',
  369:                               col2 => 'Servers to offload sessions to when busy'},
  370:                              {col1 => 'Hosting of users from other domains',
  371:                               col2 => 'Rules'},
  372:                              {col1 => "Hosting domain's own users elsewhere",
  373:                               col2 => 'Rules'}],
  374:                  },
  375:          'loadbalancing' =>
  376:                  {text  => 'Dedicated Load Balancer(s)',
  377:                   help  => 'Domain_Configuration_Load_Balancing',
  378:                   header => [{col1 => 'Balancers',
  379:                               col2 => 'Default destinations',
  380:                               col3 => 'User affiliation',
  381:                               col4 => 'Overrides'},
  382:                             ],
  383:                  },
  384:     );
  385:     if (keys(%servers) > 1) {
  386:         $prefs{'login'}  = { text   => 'Log-in page options',
  387:                              help   => 'Domain_Configuration_Login_Page',
  388:                             header => [{col1 => 'Log-in Service',
  389:                                         col2 => 'Server Setting',},
  390:                                        {col1 => 'Log-in Page Items',
  391:                                         col2 => ''},
  392:                                        {col1 => 'Log-in Help',
  393:                                         col2 => 'Value'}],
  394:                            };
  395:     }
  396: 
  397:     my @roles = ('student','coordinator','author','admin');
  398:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  399:     &Apache::lonhtmlcommon::add_breadcrumb
  400:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  401:       text=>"Settings to display/modify"});
  402:     my $confname = $dom.'-domainconfig';
  403: 
  404:     if ($phase eq 'process') {
  405:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  406:                                                               \%prefs,\%domconfig,$confname,\@roles);
  407:         if (ref($result) eq 'HASH') {
  408:             $r->rflush();
  409:             &devalidate_remote_domconfs($dom,$result);
  410:         }
  411:     } elsif ($phase eq 'display') {
  412:         my $js = &recaptcha_js().
  413:                  &credits_js();
  414:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  415:             my ($othertitle,$usertypes,$types) =
  416:                 &Apache::loncommon::sorted_inst_types($dom);
  417:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  418:                                           $domconfig{'loadbalancing'}).
  419:                    &new_spares_js().
  420:                    &common_domprefs_js().
  421:                    &Apache::loncommon::javascript_array_indexof();
  422:         }
  423:         if (grep(/^requestcourses$/,@actions)) {
  424:             my $javascript_validations;
  425:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  426:             $js .= <<END;
  427: <script type="text/javascript">
  428: $javascript_validations
  429: </script>
  430: $coursebrowserjs
  431: END
  432:         }
  433:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  434:     } else {
  435: # check if domconfig user exists for the domain.
  436:         my $servadm = $r->dir_config('lonAdmEMail');
  437:         my ($configuserok,$author_ok,$switchserver) =
  438:             &config_check($dom,$confname,$servadm);
  439:         unless ($configuserok eq 'ok') {
  440:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  441:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  442:                           $confname).
  443:                       '<br />'
  444:             );
  445:             if ($switchserver) {
  446:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  447:                           '<br />'.
  448:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  449:                           '<br />'.
  450:                           &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).
  451:                           '<br />'.
  452:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  453:                 );
  454:             } else {
  455:                 $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.').
  456:                           '<br />'.
  457:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  458:                 );
  459:             }
  460:             $r->print(&Apache::loncommon::end_page());
  461:             return OK;
  462:         }
  463:         if (keys(%domconfig) == 0) {
  464:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  465:             my @ids=&Apache::lonnet::current_machine_ids();
  466:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  467:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  468:                 my @loginimages = ('img','logo','domlogo','login');
  469:                 my $custom_img_count = 0;
  470:                 foreach my $img (@loginimages) {
  471:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  472:                         $custom_img_count ++;
  473:                     }
  474:                 }
  475:                 foreach my $role (@roles) {
  476:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  477:                         $custom_img_count ++;
  478:                     }
  479:                 }
  480:                 if ($custom_img_count > 0) {
  481:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  482:                     my $switch_server = &check_switchserver($dom,$confname);
  483:                     $r->print(
  484:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  485:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  486:     &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 />'.
  487:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  488:                     if ($switch_server) {
  489:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  490:                     }
  491:                     $r->print(&Apache::loncommon::end_page());
  492:                     return OK;
  493:                 }
  494:             }
  495:         }
  496:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  497:     }
  498:     return OK;
  499: }
  500: 
  501: sub process_changes {
  502:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  503:     my %domconfig;
  504:     if (ref($values) eq 'HASH') {
  505:         %domconfig = %{$values};
  506:     }
  507:     my $output;
  508:     if ($action eq 'login') {
  509:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  510:     } elsif ($action eq 'rolecolors') {
  511:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  512:                                      $lastactref,%domconfig);
  513:     } elsif ($action eq 'quotas') {
  514:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  515:     } elsif ($action eq 'autoenroll') {
  516:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  517:     } elsif ($action eq 'autoupdate') {
  518:         $output = &modify_autoupdate($dom,%domconfig);
  519:     } elsif ($action eq 'autocreate') {
  520:         $output = &modify_autocreate($dom,%domconfig);
  521:     } elsif ($action eq 'directorysrch') {
  522:         $output = &modify_directorysrch($dom,%domconfig);
  523:     } elsif ($action eq 'usercreation') {
  524:         $output = &modify_usercreation($dom,%domconfig);
  525:     } elsif ($action eq 'usermodification') {
  526:         $output = &modify_usermodification($dom,%domconfig);
  527:     } elsif ($action eq 'contacts') {
  528:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  529:     } elsif ($action eq 'defaults') {
  530:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  531:     } elsif ($action eq 'scantron') {
  532:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  533:     } elsif ($action eq 'coursecategories') {
  534:         $output = &modify_coursecategories($dom,%domconfig);
  535:     } elsif ($action eq 'serverstatuses') {
  536:         $output = &modify_serverstatuses($dom,%domconfig);
  537:     } elsif ($action eq 'requestcourses') {
  538:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  539:     } elsif ($action eq 'requestauthor') {
  540:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  541:     } elsif ($action eq 'coursedefaults') {
  542:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  543:     } elsif ($action eq 'usersessions') {
  544:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  545:     } elsif ($action eq 'loadbalancing') {
  546:         $output = &modify_loadbalancing($dom,%domconfig);
  547:     }
  548:     return $output;
  549: }
  550: 
  551: sub print_config_box {
  552:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  553:     my $rowtotal = 0;
  554:     my $output;
  555:     if ($action eq 'coursecategories') {
  556:         $output = &coursecategories_javascript($settings);
  557:     }
  558:     $output .= 
  559:          '<table class="LC_nested_outer">
  560:           <tr>
  561:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  562:            &mt($item->{text}).'&nbsp;'.
  563:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  564:           '</tr>';
  565:     $rowtotal ++;
  566:     my $numheaders = 1;
  567:     if (ref($item->{'header'}) eq 'ARRAY') {
  568:         $numheaders = scalar(@{$item->{'header'}});
  569:     }
  570:     if ($numheaders > 1) {
  571:         my $colspan = '';
  572:         my $rightcolspan = '';
  573:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') ||
  574:             (($action eq 'login') && ($numheaders < 3))) {
  575:             $colspan = ' colspan="2"';
  576:         }
  577:         if ($action eq 'usersessions') {
  578:             $rightcolspan = ' colspan="3"'; 
  579:         }
  580:         $output .= '
  581:           <tr>
  582:            <td>
  583:             <table class="LC_nested">
  584:              <tr class="LC_info_row">
  585:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  586:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  587:              </tr>';
  588:         $rowtotal ++;
  589:         if ($action eq 'autoupdate') {
  590:             $output .= &print_autoupdate('top',$dom,$settings,\$rowtotal);
  591:         } elsif ($action eq 'usercreation') {
  592:             $output .= &print_usercreation('top',$dom,$settings,\$rowtotal);
  593:         } elsif ($action eq 'usermodification') {
  594:             $output .= &print_usermodification('top',$dom,$settings,\$rowtotal);
  595:         } elsif ($action eq 'coursecategories') {
  596:             $output .= &print_coursecategories('top',$dom,$item,$settings,\$rowtotal);
  597:         } elsif ($action eq 'login') {
  598:             if ($numheaders == 3) {
  599:                 $colspan = ' colspan="2"';
  600:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  601:             } else {
  602:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  603:             }
  604:         } elsif ($action eq 'requestcourses') {
  605:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  606:         } elsif ($action eq 'requestauthor') {
  607:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  608:         } elsif ($action eq 'usersessions') {
  609:             $output .= &print_usersessions('top',$dom,$settings,\$rowtotal); 
  610:         } elsif ($action eq 'rolecolors') {
  611:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  612:         }
  613:         $output .= '
  614:            </table>
  615:           </td>
  616:          </tr>
  617:          <tr>
  618:            <td>
  619:             <table class="LC_nested">
  620:              <tr class="LC_info_row">
  621:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>';
  622:         $output .= '
  623:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  624:              </tr>';
  625:             $rowtotal ++;
  626:         if ($action eq 'autoupdate') {
  627:             $output .= &print_autoupdate('middle',$dom,$settings,\$rowtotal).'
  628:            </table>
  629:           </td>
  630:          </tr>
  631:          <tr>
  632:            <td>
  633:             <table class="LC_nested">
  634:              <tr class="LC_info_row">
  635:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  636:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  637:             &print_autoupdate('bottom',$dom,$settings,\$rowtotal);
  638:             $rowtotal ++;
  639:         } elsif ($action eq 'usercreation') {
  640:             $output .= &print_usercreation('middle',$dom,$settings,\$rowtotal).'
  641:            </table>
  642:           </td>
  643:          </tr>
  644:          <tr>
  645:            <td>
  646:             <table class="LC_nested">
  647:              <tr class="LC_info_row">
  648:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  649:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>             </tr>'.
  650:             &print_usercreation('bottom',$dom,$settings,\$rowtotal);
  651:             $rowtotal ++;
  652:         } elsif ($action eq 'usermodification') {
  653:             $output .= &print_usermodification('middle',$dom,$settings,\$rowtotal).'
  654:            </table>
  655:           </td>
  656:          </tr>
  657:          <tr>
  658:            <td>
  659:             <table class="LC_nested">
  660:              <tr class="LC_info_row">
  661:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  662:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  663:                        &print_usermodification('bottom',$dom,$settings,\$rowtotal);
  664:             $rowtotal ++;
  665:         } elsif ($action eq 'coursecategories') {
  666:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  667:         } elsif ($action eq 'login') {
  668:             if ($numheaders == 3) {
  669:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  670:            </table>
  671:           </td>
  672:          </tr>
  673:          <tr>
  674:            <td>
  675:             <table class="LC_nested">
  676:              <tr class="LC_info_row">
  677:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  678:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  679:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  680:                 $rowtotal ++;
  681:             } else {
  682:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  683:             }
  684:         } elsif ($action eq 'requestcourses') {
  685:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal).
  686:                        &print_studentcode($settings,\$rowtotal).'
  687:            </table>
  688:           </td>
  689:          </tr>
  690:          <tr>
  691:            <td>
  692:             <table class="LC_nested">
  693:              <tr class="LC_info_row">
  694:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  695:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  696:                        &print_textbookcourses($dom,$settings,\$rowtotal);
  697:         } elsif ($action eq 'requestauthor') {
  698:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  699:         } elsif ($action eq 'usersessions') {
  700:             $output .= &print_usersessions('middle',$dom,$settings,\$rowtotal).'
  701:            </table>
  702:           </td>
  703:          </tr>
  704:          <tr>
  705:            <td>
  706:             <table class="LC_nested">
  707:              <tr class="LC_info_row">
  708:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  709:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  710:                        &print_usersessions('bottom',$dom,$settings,\$rowtotal);
  711:             $rowtotal ++;
  712:         } elsif ($action eq 'rolecolors') {
  713:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  714:            </table>
  715:           </td>
  716:          </tr>
  717:          <tr>
  718:            <td>
  719:             <table class="LC_nested">
  720:              <tr class="LC_info_row">
  721:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  722:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  723:               <td class="LC_right_item" valign="top">'.
  724:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  725:              </tr>'.
  726:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  727:            </table>
  728:           </td>
  729:          </tr>
  730:          <tr>
  731:            <td>
  732:             <table class="LC_nested">
  733:              <tr class="LC_info_row">
  734:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  735:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  736:              </tr>'.
  737:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  738:             $rowtotal += 2;
  739:         }
  740:     } else {
  741:         $output .= '
  742:           <tr>
  743:            <td>
  744:             <table class="LC_nested">
  745:              <tr class="LC_info_row">';
  746:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  747:             $output .= '  
  748:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  749:         } elsif ($action eq 'serverstatuses') {
  750:             $output .= '
  751:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  752:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  753: 
  754:         } else {
  755:             $output .= '
  756:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  757:         }
  758:         if (defined($item->{'header'}->[0]->{'col3'})) {
  759:             $output .= '<td class="LC_left_item" valign="top">'.
  760:                        &mt($item->{'header'}->[0]->{'col2'});
  761:             if ($action eq 'serverstatuses') {
  762:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  763:             } 
  764:         } else {
  765:             $output .= '<td class="LC_right_item" valign="top">'.
  766:                        &mt($item->{'header'}->[0]->{'col2'});
  767:         }
  768:         $output .= '</td>';
  769:         if ($item->{'header'}->[0]->{'col3'}) {
  770:             if (defined($item->{'header'}->[0]->{'col4'})) {
  771:                 $output .= '<td class="LC_left_item" valign="top">'.
  772:                             &mt($item->{'header'}->[0]->{'col3'});
  773:             } else {
  774:                 $output .= '<td class="LC_right_item" valign="top">'.
  775:                            &mt($item->{'header'}->[0]->{'col3'});
  776:             }
  777:             if ($action eq 'serverstatuses') {
  778:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  779:             }
  780:             $output .= '</td>';
  781:         }
  782:         if ($item->{'header'}->[0]->{'col4'}) {
  783:             $output .= '<td class="LC_right_item" valign="top">'.
  784:                        &mt($item->{'header'}->[0]->{'col4'});
  785:         }
  786:         $output .= '</tr>';
  787:         $rowtotal ++;
  788:         if ($action eq 'quotas') {
  789:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  790:         } elsif ($action eq 'autoenroll') {
  791:             $output .= &print_autoenroll($dom,$settings,\$rowtotal);
  792:         } elsif ($action eq 'autocreate') {
  793:             $output .= &print_autocreate($dom,$settings,\$rowtotal);
  794:         } elsif ($action eq 'directorysrch') {
  795:             $output .= &print_directorysrch($dom,$settings,\$rowtotal);
  796:         } elsif ($action eq 'contacts') {
  797:             $output .= &print_contacts($dom,$settings,\$rowtotal);
  798:         } elsif ($action eq 'defaults') {
  799:             $output .= &print_defaults($dom,$settings,\$rowtotal);
  800:         } elsif ($action eq 'scantron') {
  801:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  802:         } elsif ($action eq 'serverstatuses') {
  803:             $output .= &print_serverstatuses($dom,$settings,\$rowtotal);
  804:         } elsif ($action eq 'helpsettings') {
  805:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  806:         } elsif ($action eq 'loadbalancing') {
  807:             $output .= &print_loadbalancing($dom,$settings,\$rowtotal);
  808:         } elsif ($action eq 'coursedefaults') {
  809:             $output .= &print_coursedefaults('bottom',$dom,$settings,\$rowtotal);
  810:         }
  811:     }
  812:     $output .= '
  813:    </table>
  814:   </td>
  815:  </tr>
  816: </table><br />';
  817:     return ($output,$rowtotal);
  818: }
  819: 
  820: sub print_login {
  821:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  822:     my ($css_class,$datatable);
  823:     my %choices = &login_choices();
  824: 
  825:     if ($caller eq 'service') {
  826:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  827:         my $choice = $choices{'disallowlogin'};
  828:         $css_class = ' class="LC_odd_row"';
  829:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  830:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  831:                       '<th>'.$choices{'server'}.'</th>'.
  832:                       '<th>'.$choices{'serverpath'}.'</th>'.
  833:                       '<th>'.$choices{'custompath'}.'</th>'.
  834:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  835:         my %disallowed;
  836:         if (ref($settings) eq 'HASH') {
  837:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  838:                %disallowed = %{$settings->{'loginvia'}};
  839:             }
  840:         }
  841:         foreach my $lonhost (sort(keys(%servers))) {
  842:             my $direct = 'selected="selected"';
  843:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  844:                 if ($disallowed{$lonhost}{'server'} ne '') {
  845:                     $direct = '';
  846:                 }
  847:             }
  848:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  849:                           '<td><select name="'.$lonhost.'_server">'.
  850:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  851:                           '</option>';
  852:             foreach my $hostid (sort(keys(%servers))) {
  853:                 next if ($servers{$hostid} eq $servers{$lonhost});
  854:                 my $selected = '';
  855:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  856:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  857:                         $selected = 'selected="selected"';
  858:                     }
  859:                 }
  860:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  861:                               $servers{$hostid}.'</option>';
  862:             }
  863:             $datatable .= '</select></td>'.
  864:                           '<td><select name="'.$lonhost.'_serverpath">';
  865:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  866:                 my $pathname = $path;
  867:                 if ($path eq 'custom') {
  868:                     $pathname = &mt('Custom Path').' ->';
  869:                 }
  870:                 my $selected = '';
  871:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  872:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  873:                         $selected = 'selected="selected"';
  874:                     }
  875:                 } elsif ($path eq '') {
  876:                     $selected = 'selected="selected"';
  877:                 }
  878:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  879:             }
  880:             $datatable .= '</select></td>';
  881:             my ($custom,$exempt);
  882:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  883:                 $custom = $disallowed{$lonhost}{'custompath'};
  884:                 $exempt = $disallowed{$lonhost}{'exempt'};
  885:             }
  886:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  887:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  888:                           '</tr>';
  889:         }
  890:         $datatable .= '</table></td></tr>';
  891:         return $datatable;
  892:     } elsif ($caller eq 'page') {
  893:         my %defaultchecked = ( 
  894:                                'coursecatalog' => 'on',
  895:                                'helpdesk'      => 'on',
  896:                                'adminmail'     => 'off',
  897:                                'newuser'       => 'off',
  898:                              );
  899:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  900:         my (%checkedon,%checkedoff);
  901:         foreach my $item (@toggles) {
  902:             if ($defaultchecked{$item} eq 'on') { 
  903:                 $checkedon{$item} = ' checked="checked" ';
  904:                 $checkedoff{$item} = ' ';
  905:             } elsif ($defaultchecked{$item} eq 'off') {
  906:                 $checkedoff{$item} = ' checked="checked" ';
  907:                 $checkedon{$item} = ' ';
  908:             }
  909:         }
  910:         my @images = ('img','logo','domlogo','login');
  911:         my @logintext = ('textcol','bgcol');
  912:         my @bgs = ('pgbg','mainbg','sidebg');
  913:         my @links = ('link','alink','vlink');
  914:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  915:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  916:         my (%is_custom,%designs);
  917:         my %defaults = (
  918:                        font => $defaultdesign{'login.font'},
  919:                        );
  920:         foreach my $item (@images) {
  921:             $defaults{$item} = $defaultdesign{'login.'.$item};
  922:             $defaults{'showlogo'}{$item} = 1;
  923:         }
  924:         foreach my $item (@bgs) {
  925:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  926:         }
  927:         foreach my $item (@logintext) {
  928:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  929:         }
  930:         foreach my $item (@links) {
  931:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  932:         }
  933:         if (ref($settings) eq 'HASH') {
  934:             foreach my $item (@toggles) {
  935:                 if ($settings->{$item} eq '1') {
  936:                     $checkedon{$item} =  ' checked="checked" ';
  937:                     $checkedoff{$item} = ' ';
  938:                 } elsif ($settings->{$item} eq '0') {
  939:                     $checkedoff{$item} =  ' checked="checked" ';
  940:                     $checkedon{$item} = ' ';
  941:                 }
  942:             }
  943:             foreach my $item (@images) {
  944:                 if (defined($settings->{$item})) {
  945:                     $designs{$item} = $settings->{$item};
  946:                     $is_custom{$item} = 1;
  947:                 }
  948:                 if (defined($settings->{'showlogo'}{$item})) {
  949:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  950:                 }
  951:             }
  952:             foreach my $item (@logintext) {
  953:                 if ($settings->{$item} ne '') {
  954:                     $designs{'logintext'}{$item} = $settings->{$item};
  955:                     $is_custom{$item} = 1;
  956:                 }
  957:             }
  958:             if ($settings->{'font'} ne '') {
  959:                 $designs{'font'} = $settings->{'font'};
  960:                 $is_custom{'font'} = 1;
  961:             }
  962:             foreach my $item (@bgs) {
  963:                 if ($settings->{$item} ne '') {
  964:                     $designs{'bgs'}{$item} = $settings->{$item};
  965:                     $is_custom{$item} = 1;
  966:                 }
  967:             }
  968:             foreach my $item (@links) {
  969:                 if ($settings->{$item} ne '') {
  970:                     $designs{'links'}{$item} = $settings->{$item};
  971:                     $is_custom{$item} = 1;
  972:                 }
  973:             }
  974:         } else {
  975:             if ($designhash{$dom.'.login.font'} ne '') {
  976:                 $designs{'font'} = $designhash{$dom.'.login.font'};
  977:                 $is_custom{'font'} = 1;
  978:             }
  979:             foreach my $item (@images) {
  980:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  981:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
  982:                     $is_custom{$item} = 1;
  983:                 }
  984:             }
  985:             foreach my $item (@bgs) {
  986:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  987:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
  988:                     $is_custom{$item} = 1;
  989:                 }
  990:             }
  991:             foreach my $item (@links) {
  992:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  993:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
  994:                     $is_custom{$item} = 1;
  995:                 }
  996:             }
  997:         }
  998:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
  999:                                                       logo => 'Institution Logo',
 1000:                                                       domlogo => 'Domain Logo',
 1001:                                                       login => 'Login box');
 1002:         my $itemcount = 1;
 1003:         foreach my $item (@toggles) {
 1004:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1005:             $datatable .=  
 1006:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1007:                 '</td><td>'.
 1008:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1009:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1010:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1011:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1012:                 '</tr>';
 1013:             $itemcount ++;
 1014:         }
 1015:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1016:         $datatable .= '</tr></table></td></tr>';
 1017:     } elsif ($caller eq 'help') {
 1018:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1019:         my $switchserver = &check_switchserver($dom,$confname);
 1020:         my $itemcount = 1;
 1021:         $defaulturl = '/adm/loginproblems.html';
 1022:         $defaulttype = 'default';
 1023:         %lt = &Apache::lonlocal::texthash (
 1024:                      del     => 'Delete?',
 1025:                      rep     => 'Replace:',
 1026:                      upl     => 'Upload:',
 1027:                      default => 'Default',
 1028:                      custom  => 'Custom',
 1029:                                              );
 1030:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1031:         my @currlangs;
 1032:         if (ref($settings) eq 'HASH') {
 1033:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1034:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1035:                     next if ($settings->{'helpurl'}{$key} eq '');
 1036:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1037:                     $type{$key} = 'custom';
 1038:                     unless ($key eq 'nolang') {
 1039:                         push(@currlangs,$key);
 1040:                     }
 1041:                 }
 1042:             } elsif ($settings->{'helpurl'} ne '') {
 1043:                 $type{'nolang'} = 'custom';
 1044:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1045:             }
 1046:         }
 1047:         foreach my $lang ('nolang',sort(@currlangs)) {
 1048:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1049:             $datatable .= '<tr'.$css_class.'>';
 1050:             if ($url{$lang} eq '') {
 1051:                 $url{$lang} = $defaulturl;
 1052:             }
 1053:             if ($type{$lang} eq '') {
 1054:                 $type{$lang} = $defaulttype;
 1055:             }
 1056:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1057:             if ($lang eq 'nolang') {
 1058:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1059:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1060:             } else {
 1061:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1062:                                   $langchoices{$lang},
 1063:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1064:             }
 1065:             $datatable .= '</span></td>'."\n".
 1066:                           '<td class="LC_left_item">';
 1067:             if ($type{$lang} eq 'custom') {
 1068:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1069:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1070:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1071:             } else {
 1072:                 $datatable .= $lt{'upl'};
 1073:             }
 1074:             $datatable .='<br />';
 1075:             if ($switchserver) {
 1076:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1077:             } else {
 1078:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1079:             }
 1080:             $datatable .= '</td></tr>';
 1081:             $itemcount ++;
 1082:         }
 1083:         my @addlangs;
 1084:         foreach my $lang (sort(keys(%langchoices))) {
 1085:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1086:             push(@addlangs,$lang);
 1087:         }
 1088:         if (@addlangs > 0) {
 1089:             my %toadd;
 1090:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1091:             $toadd{''} = &mt('Select');
 1092:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1093:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1094:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1095:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1096:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1097:             if ($switchserver) {
 1098:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1099:             } else {
 1100:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1101:             }
 1102:             $datatable .= '</td></tr>';
 1103:             $itemcount ++;
 1104:         }
 1105:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1106:     }
 1107:     return $datatable;
 1108: }
 1109: 
 1110: sub login_choices {
 1111:     my %choices =
 1112:         &Apache::lonlocal::texthash (
 1113:             coursecatalog => 'Display Course/Community Catalog link?',
 1114:             adminmail     => "Display Administrator's E-mail Address?",
 1115:             helpdesk      => 'Display "Contact Helpdesk" link',
 1116:             disallowlogin => "Login page requests redirected",
 1117:             hostid        => "Server",
 1118:             server        => "Redirect to:",
 1119:             serverpath    => "Path",
 1120:             custompath    => "Custom", 
 1121:             exempt        => "Exempt IP(s)",
 1122:             directlogin   => "No redirect",
 1123:             newuser       => "Link to create a user account",
 1124:             img           => "Header",
 1125:             logo          => "Main Logo",
 1126:             domlogo       => "Domain Logo",
 1127:             login         => "Log-in Header", 
 1128:             textcol       => "Text color",
 1129:             bgcol         => "Box color",
 1130:             bgs           => "Background colors",
 1131:             links         => "Link colors",
 1132:             font          => "Font color",
 1133:             pgbg          => "Header",
 1134:             mainbg        => "Page",
 1135:             sidebg        => "Login box",
 1136:             link          => "Link",
 1137:             alink         => "Active link",
 1138:             vlink         => "Visited link",
 1139:         );
 1140:     return %choices;
 1141: }
 1142: 
 1143: sub print_rolecolors {
 1144:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1145:     my %choices = &color_font_choices();
 1146:     my @bgs = ('pgbg','tabbg','sidebg');
 1147:     my @links = ('link','alink','vlink');
 1148:     my @images = ('img');
 1149:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1150:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1151:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1152:     my (%is_custom,%designs);
 1153:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1154:     if (ref($settings) eq 'HASH') {
 1155:         if (ref($settings->{$role}) eq 'HASH') {
 1156:             if ($settings->{$role}->{'img'} ne '') {
 1157:                 $designs{'img'} = $settings->{$role}->{'img'};
 1158:                 $is_custom{'img'} = 1;
 1159:             }
 1160:             if ($settings->{$role}->{'font'} ne '') {
 1161:                 $designs{'font'} = $settings->{$role}->{'font'};
 1162:                 $is_custom{'font'} = 1;
 1163:             }
 1164:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1165:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1166:                 $is_custom{'fontmenu'} = 1;
 1167:             }
 1168:             foreach my $item (@bgs) {
 1169:                 if ($settings->{$role}->{$item} ne '') {
 1170:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1171:                     $is_custom{$item} = 1;
 1172:                 }
 1173:             }
 1174:             foreach my $item (@links) {
 1175:                 if ($settings->{$role}->{$item} ne '') {
 1176:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1177:                     $is_custom{$item} = 1;
 1178:                 }
 1179:             }
 1180:         }
 1181:     } else {
 1182:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1183:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1184:             $is_custom{'img'} = 1;
 1185:         }
 1186:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1187:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1188:             $is_custom{'fontmenu'} = 1; 
 1189:         }
 1190:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1191:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1192:             $is_custom{'font'} = 1;
 1193:         }
 1194:         foreach my $item (@bgs) {
 1195:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1196:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1197:                 $is_custom{$item} = 1;
 1198:             
 1199:             }
 1200:         }
 1201:         foreach my $item (@links) {
 1202:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1203:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1204:                 $is_custom{$item} = 1;
 1205:             }
 1206:         }
 1207:     }
 1208:     my $itemcount = 1;
 1209:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1210:     $datatable .= '</tr></table></td></tr>';
 1211:     return $datatable;
 1212: }
 1213: 
 1214: sub role_defaults {
 1215:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1216:     my %defaults;
 1217:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1218:         return %defaults;
 1219:     }
 1220:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1221:     if ($role eq 'login') {
 1222:         %defaults = (
 1223:                        font => $defaultdesign{$role.'.font'},
 1224:                     );
 1225:         if (ref($logintext) eq 'ARRAY') {
 1226:             foreach my $item (@{$logintext}) {
 1227:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1228:             }
 1229:         }
 1230:         foreach my $item (@{$images}) {
 1231:             $defaults{'showlogo'}{$item} = 1;
 1232:         }
 1233:     } else {
 1234:         %defaults = (
 1235:                        img => $defaultdesign{$role.'.img'},
 1236:                        font => $defaultdesign{$role.'.font'},
 1237:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1238:                     );
 1239:     }
 1240:     foreach my $item (@{$bgs}) {
 1241:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1242:     }
 1243:     foreach my $item (@{$links}) {
 1244:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1245:     }
 1246:     foreach my $item (@{$images}) {
 1247:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1248:     }
 1249:     return %defaults;
 1250: }
 1251: 
 1252: sub display_color_options {
 1253:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1254:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1255:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1256:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1257:     my $datatable = '<tr'.$css_class.'>'.
 1258:         '<td>'.$choices->{'font'}.'</td>';
 1259:     if (!$is_custom->{'font'}) {
 1260:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1261:     } else {
 1262:         $datatable .= '<td>&nbsp;</td>';
 1263:     }
 1264:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1265: 
 1266:     $datatable .= '<td><span class="LC_nobreak">'.
 1267:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1268:                   ' value="'.$current_color.'" />&nbsp;'.
 1269:                   '&nbsp;</td></tr>';
 1270:     unless ($role eq 'login') { 
 1271:         $datatable .= '<tr'.$css_class.'>'.
 1272:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1273:         if (!$is_custom->{'fontmenu'}) {
 1274:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1275:         } else {
 1276:             $datatable .= '<td>&nbsp;</td>';
 1277:         }
 1278: 	$current_color = $designs->{'fontmenu'} ?
 1279: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1280:         $datatable .= '<td><span class="LC_nobreak">'.
 1281:                       '<input class="colorchooser" type="text" size="10" name="'
 1282: 		      .$role.'_fontmenu"'.
 1283:                       ' value="'.$current_color.'" />&nbsp;'.
 1284:                       '&nbsp;</td></tr>';
 1285:     }
 1286:     my $switchserver = &check_switchserver($dom,$confname);
 1287:     foreach my $img (@{$images}) {
 1288: 	$itemcount ++;
 1289:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1290:         $datatable .= '<tr'.$css_class.'>'.
 1291:                       '<td>'.$choices->{$img};
 1292:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1293:         if ($role eq 'login') {
 1294:             if ($img eq 'login') {
 1295:                 $login_hdr_pick =
 1296:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1297:                 $logincolors =
 1298:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1299:                                        $designs,$defaults);
 1300:             } elsif ($img ne 'domlogo') {
 1301:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1302:             }
 1303:         }
 1304:         $datatable .= '</td>';
 1305:         if ($designs->{$img} ne '') {
 1306:             $imgfile = $designs->{$img};
 1307: 	    $img_import = ($imgfile =~ m{^/adm/});
 1308:         } else {
 1309:             $imgfile = $defaults->{$img};
 1310:         }
 1311:         if ($imgfile) {
 1312:             my ($showfile,$fullsize);
 1313:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1314:                 my $urldir = $1;
 1315:                 my $filename = $2;
 1316:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1317:                 if (@info) {
 1318:                     my $thumbfile = 'tn-'.$filename;
 1319:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1320:                     if (@thumb) {
 1321:                         $showfile = $urldir.'/'.$thumbfile;
 1322:                     } else {
 1323:                         $showfile = $imgfile;
 1324:                     }
 1325:                 } else {
 1326:                     $showfile = '';
 1327:                 }
 1328:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1329:                 $showfile = $imgfile;
 1330:                 my $imgdir = $1;
 1331:                 my $filename = $2;
 1332:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1333:                     $showfile = "/$imgdir/tn-".$filename;
 1334:                 } else {
 1335:                     my $input = $londocroot.$imgfile;
 1336:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1337:                     if (!-e $output) {
 1338:                         my ($width,$height) = &thumb_dimensions();
 1339:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1340:                         if ($fullwidth ne '' && $fullheight ne '') {
 1341:                             if ($fullwidth > $width && $fullheight > $height) { 
 1342:                                 my $size = $width.'x'.$height;
 1343:                                 system("convert -sample $size $input $output");
 1344:                                 $showfile = "/$imgdir/tn-".$filename;
 1345:                             }
 1346:                         }
 1347:                     }
 1348:                 }
 1349:             }
 1350:             if ($showfile) {
 1351:                 if ($showfile =~ m{^/(adm|res)/}) {
 1352:                     if ($showfile =~ m{^/res/}) {
 1353:                         my $local_showfile =
 1354:                             &Apache::lonnet::filelocation('',$showfile);
 1355:                         &Apache::lonnet::repcopy($local_showfile);
 1356:                     }
 1357:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1358:                 }
 1359:                 if ($imgfile) {
 1360:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1361:                         if ($imgfile =~ m{^/res/}) {
 1362:                             my $local_imgfile =
 1363:                                 &Apache::lonnet::filelocation('',$imgfile);
 1364:                             &Apache::lonnet::repcopy($local_imgfile);
 1365:                         }
 1366:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1367:                     } else {
 1368:                         $fullsize = $imgfile;
 1369:                     }
 1370:                 }
 1371:                 $datatable .= '<td>';
 1372:                 if ($img eq 'login') {
 1373:                     $datatable .= $login_hdr_pick;
 1374:                 } 
 1375:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1376:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1377:             } else {
 1378:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1379:                               &mt('Upload:').'<br />';
 1380:             }
 1381:         } else {
 1382:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1383:                           &mt('Upload:').'<br />';
 1384:         }
 1385:         if ($switchserver) {
 1386:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1387:         } else {
 1388:             if ($img ne 'login') { # suppress file selection for Log-in header
 1389:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1390:             }
 1391:         }
 1392:         $datatable .= '</td></tr>';
 1393:     }
 1394:     $itemcount ++;
 1395:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1396:     $datatable .= '<tr'.$css_class.'>'.
 1397:                   '<td>'.$choices->{'bgs'}.'</td>';
 1398:     my $bgs_def;
 1399:     foreach my $item (@{$bgs}) {
 1400:         if (!$is_custom->{$item}) {
 1401:             $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>';
 1402:         }
 1403:     }
 1404:     if ($bgs_def) {
 1405:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1406:     } else {
 1407:         $datatable .= '<td>&nbsp;</td>';
 1408:     }
 1409:     $datatable .= '<td class="LC_right_item">'.
 1410:                   '<table border="0"><tr>';
 1411: 
 1412:     foreach my $item (@{$bgs}) {
 1413:         $datatable .= '<td align="center">'.$choices->{$item};
 1414: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1415:         if ($designs->{'bgs'}{$item}) {
 1416:             $datatable .= '&nbsp;';
 1417:         }
 1418:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1419:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1420:     }
 1421:     $datatable .= '</tr></table></td></tr>';
 1422:     $itemcount ++;
 1423:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1424:     $datatable .= '<tr'.$css_class.'>'.
 1425:                   '<td>'.$choices->{'links'}.'</td>';
 1426:     my $links_def;
 1427:     foreach my $item (@{$links}) {
 1428:         if (!$is_custom->{$item}) {
 1429:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1430:         }
 1431:     }
 1432:     if ($links_def) {
 1433:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1434:     } else {
 1435:         $datatable .= '<td>&nbsp;</td>';
 1436:     }
 1437:     $datatable .= '<td class="LC_right_item">'.
 1438:                   '<table border="0"><tr>';
 1439:     foreach my $item (@{$links}) {
 1440: 	my $color = $designs->{'link'}{$item} ? $designs->{'link'}{$item} : $defaults->{'links'}{$item};
 1441:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1442:         if ($designs->{'links'}{$item}) {
 1443:             $datatable.='&nbsp;';
 1444:         }
 1445:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1446:                       '" /></td>';
 1447:     }
 1448:     $$rowtotal += $itemcount;
 1449:     return $datatable;
 1450: }
 1451: 
 1452: sub logo_display_options {
 1453:     my ($img,$defaults,$designs) = @_;
 1454:     my $checkedon;
 1455:     if (ref($defaults) eq 'HASH') {
 1456:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1457:             if ($defaults->{'showlogo'}{$img}) {
 1458:                 $checkedon = 'checked="checked" ';     
 1459:             }
 1460:         } 
 1461:     }
 1462:     if (ref($designs) eq 'HASH') {
 1463:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1464:             if (defined($designs->{'showlogo'}{$img})) {
 1465:                 if ($designs->{'showlogo'}{$img} == 0) {
 1466:                     $checkedon = '';
 1467:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1468:                     $checkedon = 'checked="checked" ';
 1469:                 }
 1470:             }
 1471:         }
 1472:     }
 1473:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1474:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1475:            &mt('show').'</label>'."\n";
 1476: }
 1477: 
 1478: sub login_header_options  {
 1479:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1480:     my $output = '';
 1481:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1482:         $output .= &mt('Text default(s):').'<br />';
 1483:         if (!$is_custom->{'textcol'}) {
 1484:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1485:                        '&nbsp;&nbsp;&nbsp;';
 1486:         }
 1487:         if (!$is_custom->{'bgcol'}) {
 1488:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1489:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1490:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1491:         }
 1492:         $output .= '<br />';
 1493:     }
 1494:     $output .='<br />';
 1495:     return $output;
 1496: }
 1497: 
 1498: sub login_text_colors {
 1499:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1500:     my $color_menu = '<table border="0"><tr>';
 1501:     foreach my $item (@{$logintext}) {
 1502:         $color_menu .= '<td align="center">'.$choices->{$item};
 1503:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1504:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1505:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1506:     }
 1507:     $color_menu .= '</tr></table><br />';
 1508:     return $color_menu;
 1509: }
 1510: 
 1511: sub image_changes {
 1512:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1513:     my $output;
 1514:     if ($img eq 'login') {
 1515:             # suppress image for Log-in header
 1516:     } elsif (!$is_custom) {
 1517:         if ($img ne 'domlogo') {
 1518:             $output .= &mt('Default image:').'<br />';
 1519:         } else {
 1520:             $output .= &mt('Default in use:').'<br />';
 1521:         }
 1522:     }
 1523:     if ($img eq 'login') { # suppress image for Log-in header
 1524:         $output .= '<td>'.$logincolors;
 1525:     } else {
 1526:         if ($img_import) {
 1527:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1528:         }
 1529:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1530:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1531:         if ($is_custom) {
 1532:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1533:                        '<input type="checkbox" name="'.
 1534:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1535:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1536:         } else {
 1537:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1538:         }
 1539:     }
 1540:     return $output;
 1541: }
 1542: 
 1543: sub print_quotas {
 1544:     my ($dom,$settings,$rowtotal,$action) = @_;
 1545:     my $context;
 1546:     if ($action eq 'quotas') {
 1547:         $context = 'tools';
 1548:     } else {
 1549:         $context = $action;
 1550:     }
 1551:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1552:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1553:     my $typecount = 0;
 1554:     my ($css_class,%titles);
 1555:     if ($context eq 'requestcourses') {
 1556:         @usertools = ('official','unofficial','community','textbook');
 1557:         @options =('norequest','approval','validate','autolimit');
 1558:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1559:         %titles = &courserequest_titles();
 1560:     } elsif ($context eq 'requestauthor') {
 1561:         @usertools = ('author');
 1562:         @options = ('norequest','approval','automatic');
 1563:         %titles = &authorrequest_titles();
 1564:     } else {
 1565:         @usertools = ('aboutme','blog','webdav','portfolio');
 1566:         %titles = &tool_titles();
 1567:     }
 1568:     if (ref($types) eq 'ARRAY') {
 1569:         foreach my $type (@{$types}) {
 1570:             my ($currdefquota,$currauthorquota);
 1571:             unless (($context eq 'requestcourses') ||
 1572:                     ($context eq 'requestauthor')) {
 1573:                 if (ref($settings) eq 'HASH') {
 1574:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1575:                         $currdefquota = $settings->{defaultquota}->{$type};
 1576:                     } else {
 1577:                         $currdefquota = $settings->{$type};
 1578:                     }
 1579:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1580:                         $currauthorquota = $settings->{authorquota}->{$type};
 1581:                     }
 1582:                 }
 1583:             }
 1584:             if (defined($usertypes->{$type})) {
 1585:                 $typecount ++;
 1586:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1587:                 $datatable .= '<tr'.$css_class.'>'.
 1588:                               '<td>'.$usertypes->{$type}.'</td>'.
 1589:                               '<td class="LC_left_item">';
 1590:                 if ($context eq 'requestcourses') {
 1591:                     $datatable .= '<table><tr>';
 1592:                 }
 1593:                 my %cell;  
 1594:                 foreach my $item (@usertools) {
 1595:                     if ($context eq 'requestcourses') {
 1596:                         my ($curroption,$currlimit);
 1597:                         if (ref($settings) eq 'HASH') {
 1598:                             if (ref($settings->{$item}) eq 'HASH') {
 1599:                                 $curroption = $settings->{$item}->{$type};
 1600:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1601:                                     $currlimit = $1; 
 1602:                                 }
 1603:                             }
 1604:                         }
 1605:                         if (!$curroption) {
 1606:                             $curroption = 'norequest';
 1607:                         }
 1608:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1609:                         foreach my $option (@options) {
 1610:                             my $val = $option;
 1611:                             if ($option eq 'norequest') {
 1612:                                 $val = 0;  
 1613:                             }
 1614:                             if ($option eq 'validate') {
 1615:                                 my $canvalidate = 0;
 1616:                                 if (ref($validations{$item}) eq 'HASH') { 
 1617:                                     if ($validations{$item}{$type}) {
 1618:                                         $canvalidate = 1;
 1619:                                     }
 1620:                                 }
 1621:                                 next if (!$canvalidate);
 1622:                             }
 1623:                             my $checked = '';
 1624:                             if ($option eq $curroption) {
 1625:                                 $checked = ' checked="checked"';
 1626:                             } elsif ($option eq 'autolimit') {
 1627:                                 if ($curroption =~ /^autolimit/) {
 1628:                                     $checked = ' checked="checked"';
 1629:                                 }                       
 1630:                             } 
 1631:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1632:                                   '<input type="radio" name="crsreq_'.$item.
 1633:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1634:                                   $titles{$option}.'</label>';
 1635:                             if ($option eq 'autolimit') {
 1636:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1637:                                                 $item.'_limit_'.$type.'" size="1" '.
 1638:                                                 'value="'.$currlimit.'" />';
 1639:                             }
 1640:                             $cell{$item} .= '</span> ';
 1641:                             if ($option eq 'autolimit') {
 1642:                                 $cell{$item} .= $titles{'unlimited'};
 1643:                             }
 1644:                         }
 1645:                     } elsif ($context eq 'requestauthor') {
 1646:                         my $curroption;
 1647:                         if (ref($settings) eq 'HASH') {
 1648:                             $curroption = $settings->{$type};
 1649:                         }
 1650:                         if (!$curroption) {
 1651:                             $curroption = 'norequest';
 1652:                         }
 1653:                         foreach my $option (@options) {
 1654:                             my $val = $option;
 1655:                             if ($option eq 'norequest') {
 1656:                                 $val = 0;
 1657:                             }
 1658:                             my $checked = '';
 1659:                             if ($option eq $curroption) {
 1660:                                 $checked = ' checked="checked"';
 1661:                             }
 1662:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1663:                                   '<input type="radio" name="authorreq_'.$type.
 1664:                                   '" value="'.$val.'"'.$checked.' />'.
 1665:                                   $titles{$option}.'</label></span>&nbsp; ';
 1666:                         }
 1667:                     } else {
 1668:                         my $checked = 'checked="checked" ';
 1669:                         if (ref($settings) eq 'HASH') {
 1670:                             if (ref($settings->{$item}) eq 'HASH') {
 1671:                                 if ($settings->{$item}->{$type} == 0) {
 1672:                                     $checked = '';
 1673:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1674:                                     $checked =  'checked="checked" ';
 1675:                                 }
 1676:                             }
 1677:                         }
 1678:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1679:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1680:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1681:                                       '</label></span>&nbsp; ';
 1682:                     }
 1683:                 }
 1684:                 if ($context eq 'requestcourses') {
 1685:                     $datatable .= '</tr><tr>';
 1686:                     foreach my $item (@usertools) {
 1687:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1688:                     }
 1689:                     $datatable .= '</tr></table>';
 1690:                 }
 1691:                 $datatable .= '</td>';
 1692:                 unless (($context eq 'requestcourses') ||
 1693:                         ($context eq 'requestauthor')) {
 1694:                     $datatable .= 
 1695:                               '<td class="LC_right_item">'.
 1696:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1697:                               '<input type="text" name="quota_'.$type.
 1698:                               '" value="'.$currdefquota.
 1699:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1700:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1701:                               '<input type="text" name="authorquota_'.$type.
 1702:                               '" value="'.$currauthorquota.
 1703:                               '" size="5" /></span></td>';
 1704:                 }
 1705:                 $datatable .= '</tr>';
 1706:             }
 1707:         }
 1708:     }
 1709:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1710:         $defaultquota = '20';
 1711:         $authorquota = '500';
 1712:         if (ref($settings) eq 'HASH') {
 1713:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1714:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1715:             } elsif (defined($settings->{'default'})) {
 1716:                 $defaultquota = $settings->{'default'};
 1717:             }
 1718:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1719:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1720:             }
 1721:         }
 1722:     }
 1723:     $typecount ++;
 1724:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1725:     $datatable .= '<tr'.$css_class.'>'.
 1726:                   '<td>'.$othertitle.'</td>'.
 1727:                   '<td class="LC_left_item">';
 1728:     if ($context eq 'requestcourses') {
 1729:         $datatable .= '<table><tr>';
 1730:     }
 1731:     my %defcell;
 1732:     foreach my $item (@usertools) {
 1733:         if ($context eq 'requestcourses') {
 1734:             my ($curroption,$currlimit);
 1735:             if (ref($settings) eq 'HASH') {
 1736:                 if (ref($settings->{$item}) eq 'HASH') {
 1737:                     $curroption = $settings->{$item}->{'default'};
 1738:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1739:                         $currlimit = $1;
 1740:                     }
 1741:                 }
 1742:             }
 1743:             if (!$curroption) {
 1744:                 $curroption = 'norequest';
 1745:             }
 1746:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1747:             foreach my $option (@options) {
 1748:                 my $val = $option;
 1749:                 if ($option eq 'norequest') {
 1750:                     $val = 0;
 1751:                 }
 1752:                 if ($option eq 'validate') {
 1753:                     my $canvalidate = 0;
 1754:                     if (ref($validations{$item}) eq 'HASH') {
 1755:                         if ($validations{$item}{'default'}) {
 1756:                             $canvalidate = 1;
 1757:                         }
 1758:                     }
 1759:                     next if (!$canvalidate);
 1760:                 }
 1761:                 my $checked = '';
 1762:                 if ($option eq $curroption) {
 1763:                     $checked = ' checked="checked"';
 1764:                 } elsif ($option eq 'autolimit') {
 1765:                     if ($curroption =~ /^autolimit/) {
 1766:                         $checked = ' checked="checked"';
 1767:                     }
 1768:                 }
 1769:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1770:                                   '<input type="radio" name="crsreq_'.$item.
 1771:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1772:                                   $titles{$option}.'</label>';
 1773:                 if ($option eq 'autolimit') {
 1774:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1775:                                        $item.'_limit_default" size="1" '.
 1776:                                        'value="'.$currlimit.'" />';
 1777:                 }
 1778:                 $defcell{$item} .= '</span> ';
 1779:                 if ($option eq 'autolimit') {
 1780:                     $defcell{$item} .= $titles{'unlimited'};
 1781:                 }
 1782:             }
 1783:         } elsif ($context eq 'requestauthor') {
 1784:             my $curroption;
 1785:             if (ref($settings) eq 'HASH') {
 1786:                 $curroption = $settings->{'default'};
 1787:             }
 1788:             if (!$curroption) {
 1789:                 $curroption = 'norequest';
 1790:             }
 1791:             foreach my $option (@options) {
 1792:                 my $val = $option;
 1793:                 if ($option eq 'norequest') {
 1794:                     $val = 0;
 1795:                 }
 1796:                 my $checked = '';
 1797:                 if ($option eq $curroption) {
 1798:                     $checked = ' checked="checked"';
 1799:                 }
 1800:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1801:                               '<input type="radio" name="authorreq_default"'.
 1802:                               ' value="'.$val.'"'.$checked.' />'.
 1803:                               $titles{$option}.'</label></span>&nbsp; ';
 1804:             }
 1805:         } else {
 1806:             my $checked = 'checked="checked" ';
 1807:             if (ref($settings) eq 'HASH') {
 1808:                 if (ref($settings->{$item}) eq 'HASH') {
 1809:                     if ($settings->{$item}->{'default'} == 0) {
 1810:                         $checked = '';
 1811:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1812:                         $checked = 'checked="checked" ';
 1813:                     }
 1814:                 }
 1815:             }
 1816:             $datatable .= '<span class="LC_nobreak"><label>'.
 1817:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1818:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1819:                           '</label></span>&nbsp; ';
 1820:         }
 1821:     }
 1822:     if ($context eq 'requestcourses') {
 1823:         $datatable .= '</tr><tr>';
 1824:         foreach my $item (@usertools) {
 1825:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1826:         }
 1827:         $datatable .= '</tr></table>';
 1828:     }
 1829:     $datatable .= '</td>';
 1830:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1831:         $datatable .= '<td class="LC_right_item">'.
 1832:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1833:                       '<input type="text" name="defaultquota" value="'.
 1834:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1835:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1836:                       '<input type="text" name="authorquota" value="'.
 1837:                       $authorquota.'" size="5" /></span></td>';
 1838:     }
 1839:     $datatable .= '</tr>';
 1840:     $typecount ++;
 1841:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1842:     $datatable .= '<tr'.$css_class.'>'.
 1843:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 1844:     if ($context eq 'requestcourses') {
 1845:         $datatable .= &mt('(overrides affiliation, if set)').
 1846:                       '</td>'.
 1847:                       '<td class="LC_left_item">'.
 1848:                       '<table><tr>';
 1849:     } else {
 1850:         $datatable .= &mt('(overrides affiliation, if checked)').
 1851:                       '</td>'.
 1852:                       '<td class="LC_left_item" colspan="2">'.
 1853:                       '<br />';
 1854:     }
 1855:     my %advcell;
 1856:     foreach my $item (@usertools) {
 1857:         if ($context eq 'requestcourses') {
 1858:             my ($curroption,$currlimit);
 1859:             if (ref($settings) eq 'HASH') {
 1860:                 if (ref($settings->{$item}) eq 'HASH') {
 1861:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1862:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1863:                         $currlimit = $1;
 1864:                     }
 1865:                 }
 1866:             }
 1867:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1868:             my $checked = '';
 1869:             if ($curroption eq '') {
 1870:                 $checked = ' checked="checked"';
 1871:             }
 1872:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1873:                                '<input type="radio" name="crsreq_'.$item.
 1874:                                '__LC_adv" value=""'.$checked.' />'.
 1875:                                &mt('No override set').'</label></span>&nbsp; ';
 1876:             foreach my $option (@options) {
 1877:                 my $val = $option;
 1878:                 if ($option eq 'norequest') {
 1879:                     $val = 0;
 1880:                 }
 1881:                 if ($option eq 'validate') {
 1882:                     my $canvalidate = 0;
 1883:                     if (ref($validations{$item}) eq 'HASH') {
 1884:                         if ($validations{$item}{'_LC_adv'}) {
 1885:                             $canvalidate = 1;
 1886:                         }
 1887:                     }
 1888:                     next if (!$canvalidate);
 1889:                 }
 1890:                 my $checked = '';
 1891:                 if ($val eq $curroption) {
 1892:                     $checked = ' checked="checked"';
 1893:                 } elsif ($option eq 'autolimit') {
 1894:                     if ($curroption =~ /^autolimit/) {
 1895:                         $checked = ' checked="checked"';
 1896:                     }
 1897:                 }
 1898:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1899:                                   '<input type="radio" name="crsreq_'.$item.
 1900:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1901:                                   $titles{$option}.'</label>';
 1902:                 if ($option eq 'autolimit') {
 1903:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1904:                                        $item.'_limit__LC_adv" size="1" '.
 1905:                                        'value="'.$currlimit.'" />';
 1906:                 }
 1907:                 $advcell{$item} .= '</span> ';
 1908:                 if ($option eq 'autolimit') {
 1909:                     $advcell{$item} .= $titles{'unlimited'};
 1910:                 }
 1911:             }
 1912:         } elsif ($context eq 'requestauthor') {
 1913:             my $curroption;
 1914:             if (ref($settings) eq 'HASH') {
 1915:                 $curroption = $settings->{'_LC_adv'};
 1916:             }
 1917:             my $checked = '';
 1918:             if ($curroption eq '') {
 1919:                 $checked = ' checked="checked"';
 1920:             }
 1921:             $datatable .= '<span class="LC_nobreak"><label>'.
 1922:                           '<input type="radio" name="authorreq__LC_adv"'.
 1923:                           ' value=""'.$checked.' />'.
 1924:                           &mt('No override set').'</label></span>&nbsp; ';
 1925:             foreach my $option (@options) {
 1926:                 my $val = $option;
 1927:                 if ($option eq 'norequest') {
 1928:                     $val = 0;
 1929:                 }
 1930:                 my $checked = '';
 1931:                 if ($val eq $curroption) {
 1932:                     $checked = ' checked="checked"';
 1933:                 }
 1934:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1935:                               '<input type="radio" name="authorreq__LC_adv"'.
 1936:                               ' value="'.$val.'"'.$checked.' />'.
 1937:                               $titles{$option}.'</label></span>&nbsp; ';
 1938:             }
 1939:         } else {
 1940:             my $checked = 'checked="checked" ';
 1941:             if (ref($settings) eq 'HASH') {
 1942:                 if (ref($settings->{$item}) eq 'HASH') {
 1943:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1944:                         $checked = '';
 1945:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1946:                         $checked = 'checked="checked" ';
 1947:                     }
 1948:                 }
 1949:             }
 1950:             $datatable .= '<span class="LC_nobreak"><label>'.
 1951:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1952:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1953:                           '</label></span>&nbsp; ';
 1954:         }
 1955:     }
 1956:     if ($context eq 'requestcourses') {
 1957:         $datatable .= '</tr><tr>';
 1958:         foreach my $item (@usertools) {
 1959:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1960:         }
 1961:         $datatable .= '</tr></table>';
 1962:     }
 1963:     $datatable .= '</td></tr>';
 1964:     $$rowtotal += $typecount;
 1965:     return $datatable;
 1966: }
 1967: 
 1968: sub print_requestmail {
 1969:     my ($dom,$action,$settings,$rowtotal) = @_;
 1970:     my ($now,$datatable,%currapp);
 1971:     $now = time;
 1972:     if (ref($settings) eq 'HASH') {
 1973:         if (ref($settings->{'notify'}) eq 'HASH') {
 1974:             if ($settings->{'notify'}{'approval'} ne '') {
 1975:                map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 1976:             }
 1977:         }
 1978:     }
 1979:     my $numinrow = 2;
 1980:     my $css_class = 'class="LC_odd_row"';
 1981:     my $text;
 1982:     if ($action eq 'requestcourses') {
 1983:         $text = &mt('Receive notification of course requests requiring approval');
 1984:     } else {
 1985:         $text = &mt('Receive notification of authoring space requests requiring approval')
 1986:     }
 1987:     $datatable = '<tr '.$css_class.'>'.
 1988:                  ' <td>'.$text.'</td>'.
 1989:                  ' <td class="LC_left_item">';
 1990:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 1991:                                                  'reqapprovalnotify',%currapp);
 1992:     if ($numdc > 0) {
 1993:         $datatable .= $table;
 1994:     } else {
 1995:         $datatable .= &mt('There are no active Domain Coordinators');
 1996:     }
 1997:     $datatable .='</td></tr>';
 1998:     $$rowtotal += $rows;
 1999:     return $datatable;
 2000: }
 2001: 
 2002: sub print_studentcode {
 2003:     my ($settings,$rowtotal) = @_;
 2004:     my $rownum = 0; 
 2005:     my ($output,%current);
 2006:     my @crstypes = ('official','unofficial','community','textbook');
 2007:     if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2008:         foreach my $type (@crstypes) {
 2009:             $current{$type} = $settings->{'uniquecode'}{$type};
 2010:         }
 2011:     }
 2012:     $output .= '<tr>'.
 2013:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2014:                '<td class="LC_left_item">';
 2015:     foreach my $type (@crstypes) {
 2016:         my $check = ' ';
 2017:         if ($current{$type}) {
 2018:             $check = ' checked="checked" ';
 2019:         }
 2020:         $output .= '<span class="LC_nobreak"><label>'.
 2021:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2022:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2023:     }
 2024:     $output .= '</td></tr>';
 2025:     $$rowtotal ++;
 2026:     return $output;
 2027: }
 2028: 
 2029: sub print_textbookcourses {
 2030:     my ($dom,$settings,$rowtotal) = @_;
 2031:     my $rownum = 0;
 2032:     my $css_class;
 2033:     my $itemcount = 1;
 2034:     my $maxnum = 0;
 2035:     my $bookshash;
 2036:     if (ref($settings) eq 'HASH') {
 2037:         $bookshash = $settings->{'textbooks'};
 2038:     }
 2039:     my %ordered;
 2040:     if (ref($bookshash) eq 'HASH') {
 2041:         foreach my $item (keys(%{$bookshash})) {
 2042:             if (ref($bookshash->{$item}) eq 'HASH') {
 2043:                 my $num = $bookshash->{$item}{'order'};
 2044:                 $ordered{$num} = $item;
 2045:             }
 2046:         }
 2047:     }
 2048:     my $confname = $dom.'-domainconfig';
 2049:     my $switchserver = &check_switchserver($dom,$confname);
 2050:     my $maxnum = scalar(keys(%ordered));
 2051:     my $datatable = &textbookcourses_javascript(\%ordered);
 2052:     if (keys(%ordered)) {
 2053:         my @items = sort { $a <=> $b } keys(%ordered);
 2054:         for (my $i=0; $i<@items; $i++) {
 2055:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2056:             my $key = $ordered{$items[$i]};
 2057:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2058:             my $coursetitle = $coursehash{'description'};
 2059:             my ($subject,$title,$author,$image,$imgsrc,$cdom,$cnum);
 2060:             if (ref($bookshash->{$key}) eq 'HASH') {
 2061:                 $subject = $bookshash->{$key}->{'subject'};
 2062:                 $title = $bookshash->{$key}->{'title'};
 2063:                 $author = $bookshash->{$key}->{'author'};
 2064:                 $image = $bookshash->{$key}->{'image'};
 2065:                 if ($image ne '') {
 2066:                     my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2067:                     my $imagethumb = "$path/tn-".$imagefile;
 2068:                     $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2069:                 }
 2070:             }
 2071:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$key'".');"';
 2072:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2073:                          .'<select name="'.$key.'"'.$chgstr.'>';
 2074:             for (my $k=0; $k<=$maxnum; $k++) {
 2075:                 my $vpos = $k+1;
 2076:                 my $selstr;
 2077:                 if ($k == $i) {
 2078:                     $selstr = ' selected="selected" ';
 2079:                 }
 2080:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2081:             }
 2082:             $datatable .= '</select>'.('&nbsp;'x2).
 2083:                 '<label><input type="checkbox" name="book_del" value="'.$key.'" />'.
 2084:                 &mt('Delete?').'</label></span></td>'.
 2085:                 '<td colspan="2">'.
 2086:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="book_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2087:                 ('&nbsp;'x2).
 2088:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="book_title_'.$i.'" value="'.$title.'" /></span> '.
 2089:                 ('&nbsp;'x2).
 2090:                 '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="book_author_'.$i.'" value="'.$author.'" /></span> '.
 2091:                 ('&nbsp;'x2).
 2092:                 '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2093:             if ($image) {
 2094:                 $datatable .= '<span class="LC_nobreak">'.
 2095:                               $imgsrc.
 2096:                               '<label><input type="checkbox" name="book_image_del"'.
 2097:                               ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2098:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2099:             }
 2100:             if ($switchserver) {
 2101:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2102:             } else {
 2103:                 $datatable .= '<input type="file" name="book_image_'.$i.'" value="" />';
 2104:             }
 2105:             $datatable .= '<input type="hidden" name="book_id_'.$i.'" value="'.$key.'" /></span> '.
 2106:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2107:                           $coursetitle.'</span></td></tr>'."\n";
 2108:             $itemcount ++;
 2109:         }
 2110:     }
 2111:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2112:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'addbook_pos'".');"';
 2113:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2114:                   '<input type="hidden" name="book_maxnum" value="'.$maxnum.'" />'."\n".
 2115:                   '<select name="addbook_pos"'.$chgstr.'>';
 2116:     for (my $k=0; $k<$maxnum+1; $k++) {
 2117:         my $vpos = $k+1;
 2118:         my $selstr;
 2119:         if ($k == $maxnum) {
 2120:             $selstr = ' selected="selected" ';
 2121:         }
 2122:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2123:     }
 2124:     $datatable .= '</select>&nbsp;'."\n".
 2125:                   '<input type="checkbox" name="addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2126:                   '<td colspan="2">'.
 2127:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="addbook_subject" value="" /></span> '."\n".
 2128:                   ('&nbsp;'x2).
 2129:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="addbook_title" value="" /></span> '."\n".
 2130:                   ('&nbsp;'x2).
 2131:                   '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="addbook_author" value="" /></span> '."\n".
 2132:                   ('&nbsp;'x2).
 2133:                   '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2134:     if ($switchserver) {
 2135:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2136:     } else {
 2137:         $datatable .= '<input type="file" name="addbook_image" value="" />';
 2138:     }
 2139:     $datatable .= '</span>'."\n".
 2140:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2141:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},'addbook_cdom').
 2142:                   '<input type="text" size="25" name="addbook_cnum" value="" />'.
 2143:                   &Apache::loncommon::selectcourse_link
 2144:                       ('display','addbook_cnum','addbook_cdom',undef,undef,undef,'Course');
 2145:                   '</span></td>'."\n".
 2146:                   '</tr>'."\n";
 2147:     $itemcount ++;
 2148:     return $datatable;
 2149: }
 2150: 
 2151: sub textbookcourses_javascript {
 2152:     my ($textbooks) = @_;
 2153:     return unless(ref($textbooks) eq 'HASH');
 2154:     my $num = scalar(keys(%{$textbooks}));
 2155:     my @jsarray;
 2156:     foreach my $item (sort {$a <=> $b } (keys(%{$textbooks}))) {
 2157:         push(@jsarray,$textbooks->{$item});
 2158:     }
 2159:     my $jstext = '    var textbooks = Array('."'".join("','",@jsarray)."'".');'."\n";
 2160:     return <<"ENDSCRIPT";
 2161: <script type="text/javascript">
 2162: // <![CDATA[
 2163: function reorderBooks(form,item) {
 2164:     var changedVal;
 2165: $jstext 
 2166:     var newpos = 'addbook_pos';
 2167:     var current = new Array;
 2168:     var maxh = 1 + $num;
 2169:     var current = new Array;
 2170:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2171:     if (item == newpos) {
 2172:         changedVal = newitemVal;
 2173:     } else {
 2174:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2175:         current[newitemVal] = newpos;
 2176:     }
 2177:     for (var i=0; i<textbooks.length; i++) {
 2178:         var elementName = textbooks[i];
 2179:         if (elementName != item) {
 2180:             if (form.elements[elementName]) {
 2181:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2182:                 current[currVal] = elementName;
 2183:             }
 2184:         }
 2185:     }
 2186:     var oldVal;
 2187:     for (var j=0; j<maxh; j++) {
 2188:         if (current[j] == undefined) {
 2189:             oldVal = j;
 2190:         }
 2191:     }
 2192:     if (oldVal < changedVal) {
 2193:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2194:            var elementName = current[k];
 2195:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2196:         }
 2197:     } else {
 2198:         for (var k=changedVal; k<oldVal; k++) {
 2199:             var elementName = current[k];
 2200:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2201:         }
 2202:     }
 2203:     return;
 2204: }
 2205: 
 2206: // ]]>
 2207: </script>
 2208: 
 2209: ENDSCRIPT
 2210: }
 2211: 
 2212: sub print_autoenroll {
 2213:     my ($dom,$settings,$rowtotal) = @_;
 2214:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2215:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 2216:     if (ref($settings) eq 'HASH') {
 2217:         if (exists($settings->{'run'})) {
 2218:             if ($settings->{'run'} eq '0') {
 2219:                 $runoff = ' checked="checked" ';
 2220:                 $runon = ' ';
 2221:             } else {
 2222:                 $runon = ' checked="checked" ';
 2223:                 $runoff = ' ';
 2224:             }
 2225:         } else {
 2226:             if ($autorun) {
 2227:                 $runon = ' checked="checked" ';
 2228:                 $runoff = ' ';
 2229:             } else {
 2230:                 $runoff = ' checked="checked" ';
 2231:                 $runon = ' ';
 2232:             }
 2233:         }
 2234:         if (exists($settings->{'co-owners'})) {
 2235:             if ($settings->{'co-owners'} eq '0') {
 2236:                 $coownersoff = ' checked="checked" ';
 2237:                 $coownerson = ' ';
 2238:             } else {
 2239:                 $coownerson = ' checked="checked" ';
 2240:                 $coownersoff = ' ';
 2241:             }
 2242:         } else {
 2243:             $coownersoff = ' checked="checked" ';
 2244:             $coownerson = ' ';
 2245:         }
 2246:         if (exists($settings->{'sender_domain'})) {
 2247:             $defdom = $settings->{'sender_domain'};
 2248:         }
 2249:     } else {
 2250:         if ($autorun) {
 2251:             $runon = ' checked="checked" ';
 2252:             $runoff = ' ';
 2253:         } else {
 2254:             $runoff = ' checked="checked" ';
 2255:             $runon = ' ';
 2256:         }
 2257:     }
 2258:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2259:     my $notif_sender;
 2260:     if (ref($settings) eq 'HASH') {
 2261:         $notif_sender = $settings->{'sender_uname'};
 2262:     }
 2263:     my $datatable='<tr class="LC_odd_row">'.
 2264:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2265:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2266:                   '<input type="radio" name="autoenroll_run"'.
 2267:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2268:                   '<label><input type="radio" name="autoenroll_run"'.
 2269:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2270:                   '</tr><tr>'.
 2271:                   '<td>'.&mt('Notification messages - sender').
 2272:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2273:                   &mt('username').':&nbsp;'.
 2274:                   '<input type="text" name="sender_uname" value="'.
 2275:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2276:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2277:                   '<tr class="LC_odd_row">'.
 2278:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2279:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2280:                   '<input type="radio" name="autoassign_coowners"'.
 2281:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2282:                   '<label><input type="radio" name="autoassign_coowners"'.
 2283:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2284:                   '</tr>';
 2285:     $$rowtotal += 3;
 2286:     return $datatable;
 2287: }
 2288: 
 2289: sub print_autoupdate {
 2290:     my ($position,$dom,$settings,$rowtotal) = @_;
 2291:     my $datatable;
 2292:     if ($position eq 'top') {
 2293:         my $updateon = ' ';
 2294:         my $updateoff = ' checked="checked" ';
 2295:         my $classlistson = ' ';
 2296:         my $classlistsoff = ' checked="checked" ';
 2297:         if (ref($settings) eq 'HASH') {
 2298:             if ($settings->{'run'} eq '1') {
 2299:                 $updateon = $updateoff;
 2300:                 $updateoff = ' ';
 2301:             }
 2302:             if ($settings->{'classlists'} eq '1') {
 2303:                 $classlistson = $classlistsoff;
 2304:                 $classlistsoff = ' ';
 2305:             }
 2306:         }
 2307:         my %title = (
 2308:                    run => 'Auto-update active?',
 2309:                    classlists => 'Update information in classlists?',
 2310:                     );
 2311:         $datatable = '<tr class="LC_odd_row">'. 
 2312:                   '<td>'.&mt($title{'run'}).'</td>'.
 2313:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2314:                   '<input type="radio" name="autoupdate_run"'.
 2315:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2316:                   '<label><input type="radio" name="autoupdate_run"'.
 2317:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2318:                   '</tr><tr>'.
 2319:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2320:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2321:                   '<label><input type="radio" name="classlists"'.
 2322:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2323:                   '<label><input type="radio" name="classlists"'.
 2324:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2325:                   '</tr>';
 2326:         $$rowtotal += 2;
 2327:     } elsif ($position eq 'middle') {
 2328:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2329:         my $numinrow = 3;
 2330:         my $locknamesettings;
 2331:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2332:                                      $dom,$numinrow,$othertitle,
 2333:                                     'lockablenames');
 2334:         $$rowtotal ++;
 2335:     } else {
 2336:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2337:         my @fields = ('lastname','firstname','middlename','generation',
 2338:                       'permanentemail','id');
 2339:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2340:         my $numrows = 0;
 2341:         if (ref($types) eq 'ARRAY') {
 2342:             if (@{$types} > 0) {
 2343:                 $datatable = 
 2344:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2345:                                          \@fields,$types,\$numrows);
 2346:                     $$rowtotal += @{$types}; 
 2347:             }
 2348:         }
 2349:         $datatable .= 
 2350:             &usertype_update_row($settings,{'default' => $othertitle},
 2351:                                  \%fieldtitles,\@fields,['default'],
 2352:                                  \$numrows);
 2353:         $$rowtotal ++;     
 2354:     }
 2355:     return $datatable;
 2356: }
 2357: 
 2358: sub print_autocreate {
 2359:     my ($dom,$settings,$rowtotal) = @_;
 2360:     my (%createon,%createoff,%currhash);
 2361:     my @types = ('xml','req');
 2362:     if (ref($settings) eq 'HASH') {
 2363:         foreach my $item (@types) {
 2364:             $createoff{$item} = ' checked="checked" ';
 2365:             $createon{$item} = ' ';
 2366:             if (exists($settings->{$item})) {
 2367:                 if ($settings->{$item}) {
 2368:                     $createon{$item} = ' checked="checked" ';
 2369:                     $createoff{$item} = ' ';
 2370:                 }
 2371:             }
 2372:         }
 2373:         if ($settings->{'xmldc'} ne '') {
 2374:             $currhash{$settings->{'xmldc'}} = 1;
 2375:         }
 2376:     } else {
 2377:         foreach my $item (@types) {
 2378:             $createoff{$item} = ' checked="checked" ';
 2379:             $createon{$item} = ' ';
 2380:         }
 2381:     }
 2382:     $$rowtotal += 2;
 2383:     my $numinrow = 2;
 2384:     my $datatable='<tr class="LC_odd_row">'.
 2385:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2386:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2387:                   '<input type="radio" name="autocreate_xml"'.
 2388:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2389:                   '<label><input type="radio" name="autocreate_xml"'.
 2390:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2391:                   '</td></tr><tr>'.
 2392:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2393:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2394:                   '<input type="radio" name="autocreate_req"'.
 2395:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2396:                   '<label><input type="radio" name="autocreate_req"'.
 2397:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2398:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2399:                                                    'autocreate_xmldc',%currhash);
 2400:     if ($numdc > 1) {
 2401:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2402:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2403:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2404:     } else {
 2405:         $datatable .= $dctable.'</td></tr>';
 2406:     }
 2407:     $$rowtotal += $rows;
 2408:     return $datatable;
 2409: }
 2410: 
 2411: sub print_directorysrch {
 2412:     my ($dom,$settings,$rowtotal) = @_;
 2413:     my $srchon = ' ';
 2414:     my $srchoff = ' checked="checked" ';
 2415:     my ($exacton,$containson,$beginson);
 2416:     my $localon = ' ';
 2417:     my $localoff = ' checked="checked" ';
 2418:     if (ref($settings) eq 'HASH') {
 2419:         if ($settings->{'available'} eq '1') {
 2420:             $srchon = $srchoff;
 2421:             $srchoff = ' ';
 2422:         }
 2423:         if ($settings->{'localonly'} eq '1') {
 2424:             $localon = $localoff;
 2425:             $localoff = ' ';
 2426:         }
 2427:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2428:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2429:                 if ($type eq 'exact') {
 2430:                     $exacton = ' checked="checked" ';
 2431:                 } elsif ($type eq 'contains') {
 2432:                     $containson = ' checked="checked" ';
 2433:                 } elsif ($type eq 'begins') {
 2434:                     $beginson = ' checked="checked" ';
 2435:                 }
 2436:             }
 2437:         } else {
 2438:             if ($settings->{'searchtypes'} eq 'exact') {
 2439:                 $exacton = ' checked="checked" ';
 2440:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2441:                 $containson = ' checked="checked" ';
 2442:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2443:                 $exacton = ' checked="checked" ';
 2444:                 $containson = ' checked="checked" ';
 2445:             }
 2446:         }
 2447:     }
 2448:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2449:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2450: 
 2451:     my $numinrow = 4;
 2452:     my $cansrchrow = 0;
 2453:     my $datatable='<tr class="LC_odd_row">'.
 2454:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2455:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2456:                   '<input type="radio" name="dirsrch_available"'.
 2457:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2458:                   '<label><input type="radio" name="dirsrch_available"'.
 2459:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2460:                   '</tr><tr>'.
 2461:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2462:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2463:                   '<input type="radio" name="dirsrch_localonly"'.
 2464:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2465:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2466:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2467:                   '</tr>';
 2468:     $$rowtotal += 2;
 2469:     if (ref($usertypes) eq 'HASH') {
 2470:         if (keys(%{$usertypes}) > 0) {
 2471:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2472:                                          $numinrow,$othertitle,'cansearch');
 2473:             $cansrchrow = 1;
 2474:         }
 2475:     }
 2476:     if ($cansrchrow) {
 2477:         $$rowtotal ++;
 2478:         $datatable .= '<tr>';
 2479:     } else {
 2480:         $datatable .= '<tr class="LC_odd_row">';
 2481:     }
 2482:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2483:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2484:     foreach my $title (@{$titleorder}) {
 2485:         if (defined($searchtitles->{$title})) {
 2486:             my $check = ' ';
 2487:             if (ref($settings) eq 'HASH') {
 2488:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2489:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2490:                         $check = ' checked="checked" ';
 2491:                     }
 2492:                 }
 2493:             }
 2494:             $datatable .= '<td class="LC_left_item">'.
 2495:                           '<span class="LC_nobreak"><label>'.
 2496:                           '<input type="checkbox" name="searchby" '.
 2497:                           'value="'.$title.'"'.$check.'/>'.
 2498:                           $searchtitles->{$title}.'</label></span></td>';
 2499:         }
 2500:     }
 2501:     $datatable .= '</tr></table></td></tr>';
 2502:     $$rowtotal ++;
 2503:     if ($cansrchrow) {
 2504:         $datatable .= '<tr class="LC_odd_row">';
 2505:     } else {
 2506:         $datatable .= '<tr>';
 2507:     }
 2508:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2509:                   '<td class="LC_left_item" colspan="2">'.
 2510:                   '<span class="LC_nobreak"><label>'.
 2511:                   '<input type="checkbox" name="searchtypes" '.
 2512:                   $exacton.' value="exact" />'.&mt('Exact match').
 2513:                   '</label>&nbsp;'.
 2514:                   '<label><input type="checkbox" name="searchtypes" '.
 2515:                   $beginson.' value="begins" />'.&mt('Begins with').
 2516:                   '</label>&nbsp;'.
 2517:                   '<label><input type="checkbox" name="searchtypes" '.
 2518:                   $containson.' value="contains" />'.&mt('Contains').
 2519:                   '</label></span></td></tr>';
 2520:     $$rowtotal ++;
 2521:     return $datatable;
 2522: }
 2523: 
 2524: sub print_contacts {
 2525:     my ($dom,$settings,$rowtotal) = @_;
 2526:     my $datatable;
 2527:     my @contacts = ('adminemail','supportemail');
 2528:     my (%checked,%to,%otheremails,%bccemails);
 2529:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2530:                     'requestsmail','updatesmail','idconflictsmail');
 2531:     foreach my $type (@mailings) {
 2532:         $otheremails{$type} = '';
 2533:     }
 2534:     $bccemails{'helpdeskmail'} = '';
 2535:     if (ref($settings) eq 'HASH') {
 2536:         foreach my $item (@contacts) {
 2537:             if (exists($settings->{$item})) {
 2538:                 $to{$item} = $settings->{$item};
 2539:             }
 2540:         }
 2541:         foreach my $type (@mailings) {
 2542:             if (exists($settings->{$type})) {
 2543:                 if (ref($settings->{$type}) eq 'HASH') {
 2544:                     foreach my $item (@contacts) {
 2545:                         if ($settings->{$type}{$item}) {
 2546:                             $checked{$type}{$item} = ' checked="checked" ';
 2547:                         }
 2548:                     }
 2549:                     $otheremails{$type} = $settings->{$type}{'others'};
 2550:                     if ($type eq 'helpdeskmail') {
 2551:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2552:                     }
 2553:                 }
 2554:             } elsif ($type eq 'lonstatusmail') {
 2555:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2556:             }
 2557:         }
 2558:     } else {
 2559:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2560:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2561:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2562:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2563:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2564:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2565:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2566:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2567:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2568:     }
 2569:     my ($titles,$short_titles) = &contact_titles();
 2570:     my $rownum = 0;
 2571:     my $css_class;
 2572:     foreach my $item (@contacts) {
 2573:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2574:         $datatable .= '<tr'.$css_class.'>'. 
 2575:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2576:                   '</span></td><td class="LC_right_item">'.
 2577:                   '<input type="text" name="'.$item.'" value="'.
 2578:                   $to{$item}.'" /></td></tr>';
 2579:         $rownum ++;
 2580:     }
 2581:     foreach my $type (@mailings) {
 2582:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2583:         $datatable .= '<tr'.$css_class.'>'.
 2584:                       '<td><span class="LC_nobreak">'.
 2585:                       $titles->{$type}.': </span></td>'.
 2586:                       '<td class="LC_left_item">'.
 2587:                       '<span class="LC_nobreak">';
 2588:         foreach my $item (@contacts) {
 2589:             $datatable .= '<label>'.
 2590:                           '<input type="checkbox" name="'.$type.'"'.
 2591:                           $checked{$type}{$item}.
 2592:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2593:                           '</label>&nbsp;';
 2594:         }
 2595:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2596:                       '<input type="text" name="'.$type.'_others" '.
 2597:                       'value="'.$otheremails{$type}.'"  />';
 2598:         if ($type eq 'helpdeskmail') {
 2599:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2600:                           '<input type="text" name="'.$type.'_bcc" '.
 2601:                           'value="'.$bccemails{$type}.'"  />';
 2602:         }
 2603:         $datatable .= '</td></tr>'."\n";
 2604:         $rownum ++;
 2605:     }
 2606:     my %choices;
 2607:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2608:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2609:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2610:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2611:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2612:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2613:     my @toggles = ('reporterrors','reportupdates');
 2614:     my %defaultchecked = ('reporterrors'  => 'on',
 2615:                           'reportupdates' => 'on');
 2616:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2617:                                                \%choices,$rownum);
 2618:     $datatable .= $reports;
 2619:     $$rowtotal += $rownum;
 2620:     return $datatable;
 2621: }
 2622: 
 2623: sub print_helpsettings {
 2624:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2625:     my ($datatable,$itemcount);
 2626:     $itemcount = 1;
 2627:     my (%choices,%defaultchecked,@toggles);
 2628:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2629:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2630:                                  &mt('LON-CAPA bug tracker'),600,500));
 2631:     %defaultchecked = ('submitbugs' => 'on');
 2632:     @toggles = ('submitbugs',);
 2633: 
 2634:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2635:                                                  \%choices,$itemcount);
 2636:     return $datatable;
 2637: }
 2638: 
 2639: sub radiobutton_prefs {
 2640:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2641:         $additional) = @_;
 2642:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2643:                    (ref($choices) eq 'HASH'));
 2644: 
 2645:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2646: 
 2647:     foreach my $item (@{$toggles}) {
 2648:         if ($defaultchecked->{$item} eq 'on') {
 2649:             $checkedon{$item} = ' checked="checked" ';
 2650:             $checkedoff{$item} = ' ';
 2651:         } elsif ($defaultchecked->{$item} eq 'off') {
 2652:             $checkedoff{$item} = ' checked="checked" ';
 2653:             $checkedon{$item} = ' ';
 2654:         }
 2655:     }
 2656:     if (ref($settings) eq 'HASH') {
 2657:         foreach my $item (@{$toggles}) {
 2658:             if ($settings->{$item} eq '1') {
 2659:                 $checkedon{$item} =  ' checked="checked" ';
 2660:                 $checkedoff{$item} = ' ';
 2661:             } elsif ($settings->{$item} eq '0') {
 2662:                 $checkedoff{$item} =  ' checked="checked" ';
 2663:                 $checkedon{$item} = ' ';
 2664:             }
 2665:         }
 2666:     }
 2667:     if ($onclick) {
 2668:         $onclick = ' onclick="'.$onclick.'"';
 2669:     }
 2670:     foreach my $item (@{$toggles}) {
 2671:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2672:         $datatable .=
 2673:             '<tr'.$css_class.'><td valign="top">'.
 2674:             '<span class="LC_nobreak">'.$choices->{$item}.
 2675:             '</span></td>'.
 2676:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2677:             '<label><input type="radio" name="'.
 2678:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2679:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2680:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2681:             '</span>'.$additional.
 2682:             '</td>'.
 2683:             '</tr>';
 2684:         $itemcount ++;
 2685:     }
 2686:     return ($datatable,$itemcount);
 2687: }
 2688: 
 2689: sub print_coursedefaults {
 2690:     my ($position,$dom,$settings,$rowtotal) = @_;
 2691:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2692:     my $itemcount = 1;
 2693:     my %choices =  &Apache::lonlocal::texthash (
 2694:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2695:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2696:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2697:         coursecredits        => 'Credits can be specified for courses',
 2698:     );
 2699:     my %staticdefaults = (
 2700:                            anonsurvey_threshold => 10,
 2701:                            uploadquota          => 500,
 2702:                          );
 2703:     if ($position eq 'top') {
 2704:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2705:         @toggles = ('canuse_pdfforms');
 2706:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2707:                                                  \%choices,$itemcount);
 2708:     } else {
 2709:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2710:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits,$def_textbook_credits,
 2711:             %curruploadquota);
 2712:         my $currusecredits = 0;
 2713:         my @types = ('official','unofficial','community','textbook');
 2714:         if (ref($settings) eq 'HASH') {
 2715:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2716:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2717:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 2718:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 2719:                 }
 2720:             }
 2721:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2722:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2723:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2724:                 $def_textbook_credits = $settings->{'coursecredits'}->{'textbook'};
 2725:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '') ||
 2726:                     ($def_textbook_credits ne '')) {
 2727:                     $currusecredits = 1;
 2728:                 }
 2729:             }
 2730:         }
 2731:         if (!$currdefresponder) {
 2732:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 2733:         } elsif ($currdefresponder < 1) {
 2734:             $currdefresponder = 1;
 2735:         }
 2736:         foreach my $type (@types) {
 2737:             if ($curruploadquota{$type} eq '') {
 2738:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 2739:             }
 2740:         }
 2741:         $datatable .=
 2742:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2743:                 $choices{'anonsurvey_threshold'}.
 2744:                 '</span></td>'.
 2745:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2746:                 '<input type="text" name="anonsurvey_threshold"'.
 2747:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2748:                 '</td></tr>'."\n".
 2749:                 '<tr><td><span class="LC_nobreak">'.
 2750:                 $choices{'uploadquota'}.
 2751:                 '</span></td>'.
 2752:                 '<td align="right" class="LC_right_item">'.
 2753:                 '<table><tr>';
 2754:         foreach my $type (@types) {
 2755:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 2756:                            '<input type="text" name="uploadquota_'.$type.'"'.
 2757:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 2758:         }
 2759:         $datatable .= '</tr></table></td></tr>'."\n";
 2760:         $itemcount += 2;
 2761:         my $onclick = 'toggleCredits(this.form);';
 2762:         my $display = 'none';
 2763:         if ($currusecredits) {
 2764:             $display = 'block';
 2765:         }
 2766:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2767:                          '<span class="LC_nobreak">'.
 2768:                          &mt('Default credits for official courses [_1]',
 2769:                          '<input type="text" name="official_credits" value="'.
 2770:                          $def_official_credits.'" size="3" />').
 2771:                          '</span><br />'.
 2772:                          '<span class="LC_nobreak">'.
 2773:                          &mt('Default credits for unofficial courses [_1]',
 2774:                          '<input type="text" name="unofficial_credits" value="'.
 2775:                          $def_unofficial_credits.'" size="3" />').
 2776:                          '</span><br />'.
 2777:                          '<span class="LC_nobreak">'.
 2778:                          &mt('Default credits for textbook courses [_1]',
 2779:                          '<input type="text" name="textbook_credits" value="'.
 2780:                          $def_textbook_credits.'" size="3" />').
 2781:                          '</span></div>'."\n";
 2782:         %defaultchecked = ('coursecredits' => 'off');
 2783:         @toggles = ('coursecredits');
 2784:         my $current = {
 2785:                         'coursecredits' => $currusecredits,
 2786:                       };
 2787:         (my $table,$itemcount) =
 2788:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2789:                                \%choices,$itemcount,$onclick,$additional);
 2790:         $datatable .= $table;
 2791:     }
 2792:     $$rowtotal += $itemcount;
 2793:     return $datatable;
 2794: }
 2795: 
 2796: sub print_usersessions {
 2797:     my ($position,$dom,$settings,$rowtotal) = @_;
 2798:     my ($css_class,$datatable,%checked,%choices);
 2799:     my (%by_ip,%by_location,@intdoms);
 2800:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 2801: 
 2802:     my @alldoms = &Apache::lonnet::all_domains();
 2803:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 2804:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2805:     my %altids = &id_for_thisdom(%servers);
 2806:     my $itemcount = 1;
 2807:     if ($position eq 'top') {
 2808:         if (keys(%serverhomes) > 1) {
 2809:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 2810:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 2811:         } else {
 2812:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2813:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 2814:         }
 2815:     } else {
 2816:         if (keys(%by_location) == 0) {
 2817:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2818:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 2819:         } else {
 2820:             my %lt = &usersession_titles();
 2821:             my $numinrow = 5;
 2822:             my $prefix;
 2823:             my @types;
 2824:             if ($position eq 'bottom') {
 2825:                 $prefix = 'remote';
 2826:                 @types = ('version','excludedomain','includedomain');
 2827:             } else {
 2828:                 $prefix = 'hosted';
 2829:                 @types = ('excludedomain','includedomain');
 2830:             }
 2831:             my (%current,%checkedon,%checkedoff);
 2832:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 2833:             my @locations = sort(keys(%by_location));
 2834:             foreach my $type (@types) {
 2835:                 $checkedon{$type} = '';
 2836:                 $checkedoff{$type} = ' checked="checked"';
 2837:             }
 2838:             if (ref($settings) eq 'HASH') {
 2839:                 if (ref($settings->{$prefix}) eq 'HASH') {
 2840:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 2841:                         $current{$key} = $settings->{$prefix}{$key};
 2842:                         if ($key eq 'version') {
 2843:                             if ($current{$key} ne '') {
 2844:                                 $checkedon{$key} = ' checked="checked"';
 2845:                                 $checkedoff{$key} = '';
 2846:                             }
 2847:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 2848:                             $checkedon{$key} = ' checked="checked"';
 2849:                             $checkedoff{$key} = '';
 2850:                         }
 2851:                     }
 2852:                 }
 2853:             }
 2854:             foreach my $type (@types) {
 2855:                 next if ($type ne 'version' && !@locations);
 2856:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2857:                 $datatable .= '<tr'.$css_class.'>
 2858:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 2859:                                <span class="LC_nobreak">&nbsp;
 2860:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 2861:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 2862:                 if ($type eq 'version') {
 2863:                     my $selector = '<select name="'.$prefix.'_version">';
 2864:                     foreach my $version (@lcversions) {
 2865:                         my $selected = '';
 2866:                         if ($current{'version'} eq $version) {
 2867:                             $selected = ' selected="selected"';
 2868:                         }
 2869:                         $selector .= ' <option value="'.$version.'"'.
 2870:                                      $selected.'>'.$version.'</option>';
 2871:                     }
 2872:                     $selector .= '</select> ';
 2873:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 2874:                 } else {
 2875:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 2876:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 2877:                                  ' />'.('&nbsp;'x2).
 2878:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 2879:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 2880:                                  "\n".
 2881:                                  '</div><div><table>';
 2882:                     my $rem;
 2883:                     for (my $i=0; $i<@locations; $i++) {
 2884:                         my ($showloc,$value,$checkedtype);
 2885:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 2886:                             my $ip = $by_location{$locations[$i]}->[0];
 2887:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 2888:                                  $value = join(':',@{$by_ip{$ip}});
 2889:                                 $showloc = join(', ',@{$by_ip{$ip}});
 2890:                                 if (ref($current{$type}) eq 'ARRAY') {
 2891:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 2892:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 2893:                                             $checkedtype = ' checked="checked"';
 2894:                                             last;
 2895:                                         }
 2896:                                     }
 2897:                                 }
 2898:                             }
 2899:                         }
 2900:                         $rem = $i%($numinrow);
 2901:                         if ($rem == 0) {
 2902:                             if ($i > 0) {
 2903:                                 $datatable .= '</tr>';
 2904:                             }
 2905:                             $datatable .= '<tr>';
 2906:                         }
 2907:                         $datatable .= '<td class="LC_left_item">'.
 2908:                                       '<span class="LC_nobreak"><label>'.
 2909:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 2910:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 2911:                                       '</label></span></td>';
 2912:                     }
 2913:                     $rem = @locations%($numinrow);
 2914:                     my $colsleft = $numinrow - $rem;
 2915:                     if ($colsleft > 1 ) {
 2916:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2917:                                       '&nbsp;</td>';
 2918:                     } elsif ($colsleft == 1) {
 2919:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 2920:                     }
 2921:                     $datatable .= '</tr></table>';
 2922:                 }
 2923:                 $datatable .= '</td></tr>';
 2924:                 $itemcount ++;
 2925:             }
 2926:         }
 2927:     }
 2928:     $$rowtotal += $itemcount;
 2929:     return $datatable;
 2930: }
 2931: 
 2932: sub build_location_hashes {
 2933:     my ($intdoms,$by_ip,$by_location) = @_;
 2934:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 2935:                   (ref($by_location) eq 'HASH')); 
 2936:     my %iphost = &Apache::lonnet::get_iphost();
 2937:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2938:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 2939:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 2940:         foreach my $id (@{$iphost{$primary_ip}}) {
 2941:             my $intdom = &Apache::lonnet::internet_dom($id);
 2942:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 2943:                 push(@{$intdoms},$intdom);
 2944:             }
 2945:         }
 2946:     }
 2947:     foreach my $ip (keys(%iphost)) {
 2948:         if (ref($iphost{$ip}) eq 'ARRAY') {
 2949:             foreach my $id (@{$iphost{$ip}}) {
 2950:                 my $location = &Apache::lonnet::internet_dom($id);
 2951:                 if ($location) {
 2952:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 2953:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2954:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 2955:                             push(@{$by_ip->{$ip}},$location);
 2956:                         }
 2957:                     } else {
 2958:                         $by_ip->{$ip} = [$location];
 2959:                     }
 2960:                 }
 2961:             }
 2962:         }
 2963:     }
 2964:     foreach my $ip (sort(keys(%{$by_ip}))) {
 2965:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2966:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 2967:             my $first = $by_ip->{$ip}->[0];
 2968:             if (ref($by_location->{$first}) eq 'ARRAY') {
 2969:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 2970:                     push(@{$by_location->{$first}},$ip);
 2971:                 }
 2972:             } else {
 2973:                 $by_location->{$first} = [$ip];
 2974:             }
 2975:         }
 2976:     }
 2977:     return;
 2978: }
 2979: 
 2980: sub current_offloads_to {
 2981:     my ($dom,$settings,$servers) = @_;
 2982:     my (%spareid,%otherdomconfigs);
 2983:     if (ref($servers) eq 'HASH') {
 2984:         foreach my $lonhost (sort(keys(%{$servers}))) {
 2985:             my $gotspares;
 2986:             if (ref($settings) eq 'HASH') {
 2987:                 if (ref($settings->{'spares'}) eq 'HASH') {
 2988:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 2989:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 2990:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 2991:                         $gotspares = 1;
 2992:                     }
 2993:                 }
 2994:             }
 2995:             unless ($gotspares) {
 2996:                 my $gotspares;
 2997:                 my $serverhomeID =
 2998:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 2999:                 my $serverhomedom =
 3000:                     &Apache::lonnet::host_domain($serverhomeID);
 3001:                 if ($serverhomedom ne $dom) {
 3002:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 3003:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3004:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3005:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3006:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3007:                                 $gotspares = 1;
 3008:                             }
 3009:                         }
 3010:                     } else {
 3011:                         $otherdomconfigs{$serverhomedom} =
 3012:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 3013:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 3014:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3015:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3016:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 3017:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3018:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3019:                                         $gotspares = 1;
 3020:                                     }
 3021:                                 }
 3022:                             }
 3023:                         }
 3024:                     }
 3025:                 }
 3026:             }
 3027:             unless ($gotspares) {
 3028:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3029:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3030:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3031:                } else {
 3032:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 3033:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 3034:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3035:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3036:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3037:                     } else {
 3038:                         my %what = (
 3039:                              spareid => 1,
 3040:                         );
 3041:                         my ($result,$returnhash) = 
 3042:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 3043:                         if ($result eq 'ok') { 
 3044:                             if (ref($returnhash) eq 'HASH') {
 3045:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 3046:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 3047:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 3048:                                 }
 3049:                             }
 3050:                         }
 3051:                     }
 3052:                 }
 3053:             }
 3054:         }
 3055:     }
 3056:     return %spareid;
 3057: }
 3058: 
 3059: sub spares_row {
 3060:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 3061:     my $css_class;
 3062:     my $numinrow = 4;
 3063:     my $itemcount = 1;
 3064:     my $datatable;
 3065:     my %typetitles = &sparestype_titles();
 3066:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3067:         foreach my $server (sort(keys(%{$servers}))) {
 3068:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 3069:             my ($othercontrol,$serverdom);
 3070:             if ($serverhome ne $server) {
 3071:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 3072:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3073:             } else {
 3074:                 $serverdom = &Apache::lonnet::host_domain($server);
 3075:                 if ($serverdom ne $dom) {
 3076:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3077:                 }
 3078:             }
 3079:             next unless (ref($spareid->{$server}) eq 'HASH');
 3080:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3081:             $datatable .= '<tr'.$css_class.'>
 3082:                            <td rowspan="2">
 3083:                             <span class="LC_nobreak">'.
 3084:                           &mt('[_1] when busy, offloads to:'
 3085:                               ,'<b>'.$server.'</b>').
 3086:                           "\n";
 3087:             my (%current,%canselect);
 3088:             my @choices = 
 3089:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 3090:             foreach my $type ('primary','default') {
 3091:                 if (ref($spareid->{$server}) eq 'HASH') {
 3092:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 3093:                         my @spares = @{$spareid->{$server}{$type}};
 3094:                         if (@spares > 0) {
 3095:                             if ($othercontrol) {
 3096:                                 $current{$type} = join(', ',@spares);
 3097:                             } else {
 3098:                                 $current{$type} .= '<table>';
 3099:                                 my $numspares = scalar(@spares);
 3100:                                 for (my $i=0;  $i<@spares; $i++) {
 3101:                                     my $rem = $i%($numinrow);
 3102:                                     if ($rem == 0) {
 3103:                                         if ($i > 0) {
 3104:                                             $current{$type} .= '</tr>';
 3105:                                         }
 3106:                                         $current{$type} .= '<tr>';
 3107:                                     }
 3108:                                     $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;'.
 3109:                                                        $spareid->{$server}{$type}[$i].
 3110:                                                        '</label></td>'."\n";
 3111:                                 }
 3112:                                 my $rem = @spares%($numinrow);
 3113:                                 my $colsleft = $numinrow - $rem;
 3114:                                 if ($colsleft > 1 ) {
 3115:                                     $current{$type} .= '<td colspan="'.$colsleft.
 3116:                                                        '" class="LC_left_item">'.
 3117:                                                        '&nbsp;</td>';
 3118:                                 } elsif ($colsleft == 1) {
 3119:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 3120:                                 }
 3121:                                 $current{$type} .= '</tr></table>';
 3122:                             }
 3123:                         }
 3124:                     }
 3125:                     if ($current{$type} eq '') {
 3126:                         $current{$type} = &mt('None specified');
 3127:                     }
 3128:                     if ($othercontrol) {
 3129:                         if ($type eq 'primary') {
 3130:                             $canselect{$type} = $othercontrol;
 3131:                         }
 3132:                     } else {
 3133:                         $canselect{$type} = 
 3134:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 3135:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 3136:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 3137:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 3138:                         if (@choices > 0) {
 3139:                             foreach my $lonhost (@choices) {
 3140:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 3141:                             }
 3142:                         }
 3143:                         $canselect{$type} .= '</select>'."\n";
 3144:                     }
 3145:                 } else {
 3146:                     $current{$type} = &mt('Could not be determined');
 3147:                     if ($type eq 'primary') {
 3148:                         $canselect{$type} =  $othercontrol;
 3149:                     }
 3150:                 }
 3151:                 if ($type eq 'default') {
 3152:                     $datatable .= '<tr'.$css_class.'>';
 3153:                 }
 3154:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 3155:                               '<td>'.$current{$type}.'</td>'."\n".
 3156:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 3157:             }
 3158:             $itemcount ++;
 3159:         }
 3160:     }
 3161:     $$rowtotal += $itemcount;
 3162:     return $datatable;
 3163: }
 3164: 
 3165: sub possible_newspares {
 3166:     my ($server,$currspares,$serverhomes,$altids) = @_;
 3167:     my $serverhostname = &Apache::lonnet::hostname($server);
 3168:     my %excluded;
 3169:     if ($serverhostname ne '') {
 3170:         %excluded = (
 3171:                        $serverhostname => 1,
 3172:                     );
 3173:     }
 3174:     if (ref($currspares) eq 'HASH') {
 3175:         foreach my $type (keys(%{$currspares})) {
 3176:             if (ref($currspares->{$type}) eq 'ARRAY') {
 3177:                 if (@{$currspares->{$type}} > 0) {
 3178:                     foreach my $curr (@{$currspares->{$type}}) {
 3179:                         my $hostname = &Apache::lonnet::hostname($curr);
 3180:                         $excluded{$hostname} = 1;
 3181:                     }
 3182:                 }
 3183:             }
 3184:         }
 3185:     }
 3186:     my @choices;
 3187:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3188:         if (keys(%{$serverhomes}) > 1) {
 3189:             foreach my $name (sort(keys(%{$serverhomes}))) {
 3190:                 unless ($excluded{$name}) {
 3191:                     if (exists($altids->{$serverhomes->{$name}})) {
 3192:                         push(@choices,$altids->{$serverhomes->{$name}});
 3193:                     } else {
 3194:                         push(@choices,$serverhomes->{$name});
 3195:                     }
 3196:                 }
 3197:             }
 3198:         }
 3199:     }
 3200:     return sort(@choices);
 3201: }
 3202: 
 3203: sub print_loadbalancing {
 3204:     my ($dom,$settings,$rowtotal) = @_;
 3205:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 3206:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 3207:     my $numinrow = 1;
 3208:     my $datatable;
 3209:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3210:     my (%currbalancer,%currtargets,%currrules,%existing);
 3211:     if (ref($settings) eq 'HASH') {
 3212:         %existing = %{$settings};
 3213:     }
 3214:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 3215:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 3216:                                   \%currtargets,\%currrules);
 3217:     } else {
 3218:         return;
 3219:     }
 3220:     my ($othertitle,$usertypes,$types) =
 3221:         &Apache::loncommon::sorted_inst_types($dom);
 3222:     my $rownum = 8;
 3223:     if (ref($types) eq 'ARRAY') {
 3224:         $rownum += scalar(@{$types});
 3225:     }
 3226:     my @css_class = ('LC_odd_row','LC_even_row');
 3227:     my $balnum = 0;
 3228:     my $islast;
 3229:     my (@toshow,$disabledtext);
 3230:     if (keys(%currbalancer) > 0) {
 3231:         @toshow = sort(keys(%currbalancer));
 3232:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3233:             push(@toshow,'');
 3234:         }
 3235:     } else {
 3236:         @toshow = ('');
 3237:         $disabledtext = &mt('No existing load balancer');
 3238:     }
 3239:     foreach my $lonhost (@toshow) {
 3240:         if ($balnum == scalar(@toshow)-1) {
 3241:             $islast = 1;
 3242:         } else {
 3243:             $islast = 0;
 3244:         }
 3245:         my $cssidx = $balnum%2;
 3246:         my $targets_div_style = 'display: none';
 3247:         my $disabled_div_style = 'display: block';
 3248:         my $homedom_div_style = 'display: none';
 3249:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3250:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3251:                       '<p>';
 3252:         if ($lonhost eq '') {
 3253:             $datatable .= '<span class="LC_nobreak">';
 3254:             if (keys(%currbalancer) > 0) {
 3255:                 $datatable .= &mt('Add balancer:');
 3256:             } else {
 3257:                 $datatable .= &mt('Enable balancer:');
 3258:             }
 3259:             $datatable .= '&nbsp;'.
 3260:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3261:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3262:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3263:                           '<option value="" selected="selected">'.&mt('None').
 3264:                           '</option>'."\n";
 3265:             foreach my $server (sort(keys(%servers))) {
 3266:                 next if ($currbalancer{$server});
 3267:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3268:             }
 3269:             $datatable .=
 3270:                 '</select>'."\n".
 3271:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3272:         } else {
 3273:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3274:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3275:                            &mt('Stop balancing').'</label>'.
 3276:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3277:             $targets_div_style = 'display: block';
 3278:             $disabled_div_style = 'display: none';
 3279:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3280:                 $homedom_div_style = 'display: block';
 3281:             }
 3282:         }
 3283:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3284:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3285:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3286:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3287:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3288:         my @sparestypes = ('primary','default');
 3289:         my %typetitles = &sparestype_titles();
 3290:         foreach my $sparetype (@sparestypes) {
 3291:             my $targettable;
 3292:             for (my $i=0; $i<$numspares; $i++) {
 3293:                 my $checked;
 3294:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3295:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3296:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3297:                             $checked = ' checked="checked"';
 3298:                         }
 3299:                     }
 3300:                 }
 3301:                 my ($chkboxval,$disabled);
 3302:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3303:                     $chkboxval = $spares[$i];
 3304:                 }
 3305:                 if (exists($currbalancer{$spares[$i]})) {
 3306:                     $disabled = ' disabled="disabled"';
 3307:                 }
 3308:                 $targettable .=
 3309:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3310:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3311:                     '</span></label></td>';
 3312:                 my $rem = $i%($numinrow);
 3313:                 if ($rem == 0) {
 3314:                     if (($i > 0) && ($i < $numspares-1)) {
 3315:                         $targettable .= '</tr>';
 3316:                     }
 3317:                     if ($i < $numspares-1) {
 3318:                         $targettable .= '<tr>';
 3319:                     }
 3320:                 }
 3321:             }
 3322:             if ($targettable ne '') {
 3323:                 my $rem = $numspares%($numinrow);
 3324:                 my $colsleft = $numinrow - $rem;
 3325:                 if ($colsleft > 1 ) {
 3326:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3327:                                     '&nbsp;</td>';
 3328:                 } elsif ($colsleft == 1) {
 3329:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3330:                 }
 3331:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3332:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3333:             }
 3334:         }
 3335:         $datatable .= '</div></td></tr>'.
 3336:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3337:                                            $othertitle,$usertypes,$types,\%servers,
 3338:                                            \%currbalancer,$lonhost,
 3339:                                            $targets_div_style,$homedom_div_style,
 3340:                                            $css_class[$cssidx],$balnum,$islast);
 3341:         $$rowtotal += $rownum;
 3342:         $balnum ++;
 3343:     }
 3344:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3345:     return $datatable;
 3346: }
 3347: 
 3348: sub get_loadbalancers_config {
 3349:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3350:     return unless ((ref($servers) eq 'HASH') &&
 3351:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3352:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3353:     if (keys(%{$existing}) > 0) {
 3354:         my $oldlonhost;
 3355:         foreach my $key (sort(keys(%{$existing}))) {
 3356:             if ($key eq 'lonhost') {
 3357:                 $oldlonhost = $existing->{'lonhost'};
 3358:                 $currbalancer->{$oldlonhost} = 1;
 3359:             } elsif ($key eq 'targets') {
 3360:                 if ($oldlonhost) {
 3361:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3362:                 }
 3363:             } elsif ($key eq 'rules') {
 3364:                 if ($oldlonhost) {
 3365:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3366:                 }
 3367:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3368:                 $currbalancer->{$key} = 1;
 3369:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3370:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3371:             }
 3372:         }
 3373:     } else {
 3374:         my ($balancerref,$targetsref) =
 3375:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3376:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3377:             foreach my $server (sort(keys(%{$balancerref}))) {
 3378:                 $currbalancer->{$server} = 1;
 3379:                 $currtargets->{$server} = $targetsref->{$server};
 3380:             }
 3381:         }
 3382:     }
 3383:     return;
 3384: }
 3385: 
 3386: sub loadbalancing_rules {
 3387:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3388:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3389:         $css_class,$balnum,$islast) = @_;
 3390:     my $output;
 3391:     my $num = 0;
 3392:     my ($alltypes,$othertypes,$titles) =
 3393:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3394:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3395:         foreach my $type (@{$alltypes}) {
 3396:             $num ++;
 3397:             my $current;
 3398:             if (ref($currrules) eq 'HASH') {
 3399:                 $current = $currrules->{$type};
 3400:             }
 3401:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3402:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3403:                     $current = '';
 3404:                 }
 3405:             }
 3406:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3407:                                              $servers,$currbalancer,$lonhost,$dom,
 3408:                                              $targets_div_style,$homedom_div_style,
 3409:                                              $css_class,$balnum,$num,$islast);
 3410:         }
 3411:     }
 3412:     return $output;
 3413: }
 3414: 
 3415: sub loadbalancing_titles {
 3416:     my ($dom,$intdom,$usertypes,$types) = @_;
 3417:     my %othertypes = (
 3418:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3419:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3420:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3421:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3422:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 3423:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 3424:                      );
 3425:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 3426:     if (ref($types) eq 'ARRAY') {
 3427:         unshift(@alltypes,@{$types},'default');
 3428:     }
 3429:     my %titles;
 3430:     foreach my $type (@alltypes) {
 3431:         if ($type =~ /^_LC_/) {
 3432:             $titles{$type} = $othertypes{$type};
 3433:         } elsif ($type eq 'default') {
 3434:             $titles{$type} = &mt('All users from [_1]',$dom);
 3435:             if (ref($types) eq 'ARRAY') {
 3436:                 if (@{$types} > 0) {
 3437:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3438:                 }
 3439:             }
 3440:         } elsif (ref($usertypes) eq 'HASH') {
 3441:             $titles{$type} = $usertypes->{$type};
 3442:         }
 3443:     }
 3444:     return (\@alltypes,\%othertypes,\%titles);
 3445: }
 3446: 
 3447: sub loadbalance_rule_row {
 3448:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3449:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3450:     my @rulenames;
 3451:     my %ruletitles = &offloadtype_text();
 3452:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 3453:         @rulenames = ('balancer','offloadedto');
 3454:     } else {
 3455:         @rulenames = ('default','homeserver');
 3456:         if ($type eq '_LC_external') {
 3457:             push(@rulenames,'externalbalancer');
 3458:         } else {
 3459:             push(@rulenames,'specific');
 3460:         }
 3461:         push(@rulenames,'none');
 3462:     }
 3463:     my $style = $targets_div_style;
 3464:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3465:         $style = $homedom_div_style;
 3466:     }
 3467:     my $space;
 3468:     if ($islast && $num == 1) {
 3469:         $space = '<div display="inline-block">&nbsp;</div>';
 3470:     }
 3471:     my $output =
 3472:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3473:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3474:         '<td valaign="top">'.$space.
 3475:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3476:     for (my $i=0; $i<@rulenames; $i++) {
 3477:         my $rule = $rulenames[$i];
 3478:         my ($checked,$extra);
 3479:         if ($rulenames[$i] eq 'default') {
 3480:             $rule = '';
 3481:         }
 3482:         if ($rulenames[$i] eq 'specific') {
 3483:             if (ref($servers) eq 'HASH') {
 3484:                 my $default;
 3485:                 if (($current ne '') && (exists($servers->{$current}))) {
 3486:                     $checked = ' checked="checked"';
 3487:                 }
 3488:                 unless ($checked) {
 3489:                     $default = ' selected="selected"';
 3490:                 }
 3491:                 $extra =
 3492:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3493:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3494:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3495:                     '<option value=""'.$default.'></option>'."\n";
 3496:                 foreach my $server (sort(keys(%{$servers}))) {
 3497:                     if (ref($currbalancer) eq 'HASH') {
 3498:                         next if (exists($currbalancer->{$server}));
 3499:                     }
 3500:                     my $selected;
 3501:                     if ($server eq $current) {
 3502:                         $selected = ' selected="selected"';
 3503:                     }
 3504:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3505:                 }
 3506:                 $extra .= '</select>';
 3507:             }
 3508:         } elsif ($rule eq $current) {
 3509:             $checked = ' checked="checked"';
 3510:         }
 3511:         $output .= '<span class="LC_nobreak"><label>'.
 3512:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3513:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3514:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3515:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3516:                    '</label>'.$extra.'</span><br />'."\n";
 3517:     }
 3518:     $output .= '</div></td></tr>'."\n";
 3519:     return $output;
 3520: }
 3521: 
 3522: sub offloadtype_text {
 3523:     my %ruletitles = &Apache::lonlocal::texthash (
 3524:            'default'          => 'Offloads to default destinations',
 3525:            'homeserver'       => "Offloads to user's home server",
 3526:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3527:            'specific'         => 'Offloads to specific server',
 3528:            'none'             => 'No offload',
 3529:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 3530:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 3531:     );
 3532:     return %ruletitles;
 3533: }
 3534: 
 3535: sub sparestype_titles {
 3536:     my %typestitles = &Apache::lonlocal::texthash (
 3537:                           'primary' => 'primary',
 3538:                           'default' => 'default',
 3539:                       );
 3540:     return %typestitles;
 3541: }
 3542: 
 3543: sub contact_titles {
 3544:     my %titles = &Apache::lonlocal::texthash (
 3545:                    'supportemail' => 'Support E-mail address',
 3546:                    'adminemail'   => 'Default Server Admin E-mail address',
 3547:                    'errormail'    => 'Error reports to be e-mailed to',
 3548:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3549:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3550:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3551:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3552:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3553:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 3554:                  );
 3555:     my %short_titles = &Apache::lonlocal::texthash (
 3556:                            adminemail   => 'Admin E-mail address',
 3557:                            supportemail => 'Support E-mail',
 3558:                        );   
 3559:     return (\%titles,\%short_titles);
 3560: }
 3561: 
 3562: sub tool_titles {
 3563:     my %titles = &Apache::lonlocal::texthash (
 3564:                      aboutme    => 'Personal web page',
 3565:                      blog       => 'Blog',
 3566:                      webdav     => 'WebDAV',
 3567:                      portfolio  => 'Portfolio',
 3568:                      official   => 'Official courses (with institutional codes)',
 3569:                      unofficial => 'Unofficial courses',
 3570:                      community  => 'Communities',
 3571:                      textbook   => 'Textbook courses',
 3572:                  );
 3573:     return %titles;
 3574: }
 3575: 
 3576: sub courserequest_titles {
 3577:     my %titles = &Apache::lonlocal::texthash (
 3578:                                    official   => 'Official',
 3579:                                    unofficial => 'Unofficial',
 3580:                                    community  => 'Communities',
 3581:                                    textbook   => 'Textbook',
 3582:                                    norequest  => 'Not allowed',
 3583:                                    approval   => 'Approval by Dom. Coord.',
 3584:                                    validate   => 'With validation',
 3585:                                    autolimit  => 'Numerical limit',
 3586:                                    unlimited  => '(blank for unlimited)',
 3587:                  );
 3588:     return %titles;
 3589: }
 3590: 
 3591: sub authorrequest_titles {
 3592:     my %titles = &Apache::lonlocal::texthash (
 3593:                                    norequest  => 'Not allowed',
 3594:                                    approval   => 'Approval by Dom. Coord.',
 3595:                                    automatic  => 'Automatic approval',
 3596:                  );
 3597:     return %titles;
 3598: }
 3599: 
 3600: sub courserequest_conditions {
 3601:     my %conditions = &Apache::lonlocal::texthash (
 3602:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3603:        validate   => '(Processing of request subject to institutional validation).',
 3604:                  );
 3605:     return %conditions;
 3606: }
 3607: 
 3608: 
 3609: sub print_usercreation {
 3610:     my ($position,$dom,$settings,$rowtotal) = @_;
 3611:     my $numinrow = 4;
 3612:     my $datatable;
 3613:     if ($position eq 'top') {
 3614:         $$rowtotal ++;
 3615:         my $rowcount = 0;
 3616:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3617:         if (ref($rules) eq 'HASH') {
 3618:             if (keys(%{$rules}) > 0) {
 3619:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3620:                                                 $ruleorder,$numinrow,$rowcount);
 3621:                 $$rowtotal ++;
 3622:                 $rowcount ++;
 3623:             }
 3624:         }
 3625:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3626:         if (ref($idrules) eq 'HASH') {
 3627:             if (keys(%{$idrules}) > 0) {
 3628:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3629:                                                 $idruleorder,$numinrow,$rowcount);
 3630:                 $$rowtotal ++;
 3631:                 $rowcount ++;
 3632:             }
 3633:         }
 3634:         my ($emailrules,$emailruleorder) = 
 3635:             &Apache::lonnet::inst_userrules($dom,'email');
 3636:         if (ref($emailrules) eq 'HASH') {
 3637:             if (keys(%{$emailrules}) > 0) {
 3638:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 3639:                                                 $emailruleorder,$numinrow,$rowcount);
 3640:                 $$rowtotal ++;
 3641:                 $rowcount ++;
 3642:             }
 3643:         }
 3644:         if ($rowcount == 0) {
 3645:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3646:             $$rowtotal ++;
 3647:             $rowcount ++;
 3648:         }
 3649:     } elsif ($position eq 'middle') {
 3650:         my @creators = ('author','course','requestcrs','selfcreate');
 3651:         my ($rules,$ruleorder) =
 3652:             &Apache::lonnet::inst_userrules($dom,'username');
 3653:         my %lt = &usercreation_types();
 3654:         my %checked;
 3655:         my @selfcreate; 
 3656:         if (ref($settings) eq 'HASH') {
 3657:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3658:                 foreach my $item (@creators) {
 3659:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3660:                 }
 3661:                 if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3662:                     @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3663:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3664:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3665:                         @selfcreate = ('email','login','sso');
 3666:                     } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3667:                         @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3668:                     }
 3669:                 }
 3670:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3671:                 foreach my $item (@creators) {
 3672:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3673:                         $checked{$item} = 'none';
 3674:                     }
 3675:                 }
 3676:             }
 3677:         }
 3678:         my $rownum = 0;
 3679:         foreach my $item (@creators) {
 3680:             $rownum ++;
 3681:             if ($item ne 'selfcreate') {  
 3682:                 if ($checked{$item} eq '') {
 3683:                     $checked{$item} = 'any';
 3684:                 }
 3685:             }
 3686:             my $css_class;
 3687:             if ($rownum%2) {
 3688:                 $css_class = '';
 3689:             } else {
 3690:                 $css_class = ' class="LC_odd_row" ';
 3691:             }
 3692:             $datatable .= '<tr'.$css_class.'>'.
 3693:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3694:                          '</span></td><td align="right">';
 3695:             my @options;
 3696:             if ($item eq 'selfcreate') {
 3697:                 push(@options,('email','login','sso'));
 3698:             } else {
 3699:                 @options = ('any');
 3700:                 if (ref($rules) eq 'HASH') {
 3701:                     if (keys(%{$rules}) > 0) {
 3702:                         push(@options,('official','unofficial'));
 3703:                     }
 3704:                 }
 3705:                 push(@options,'none');
 3706:             }
 3707:             foreach my $option (@options) {
 3708:                 my $type = 'radio';
 3709:                 my $check = ' ';
 3710:                 if ($item eq 'selfcreate') {
 3711:                     $type = 'checkbox';
 3712:                     if (grep(/^\Q$option\E$/,@selfcreate)) {
 3713:                         $check = ' checked="checked" ';
 3714:                     }
 3715:                 } else {
 3716:                     if ($checked{$item} eq $option) {
 3717:                         $check = ' checked="checked" ';
 3718:                     }
 3719:                 } 
 3720:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3721:                               '<input type="'.$type.'" name="can_createuser_'.
 3722:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3723:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3724:             }
 3725:             $datatable .= '</td></tr>';
 3726:         }
 3727:         my ($othertitle,$usertypes,$types) =
 3728:             &Apache::loncommon::sorted_inst_types($dom);
 3729:         my $createsettings;
 3730:         if (ref($settings) eq 'HASH') {
 3731:             $createsettings = $settings->{cancreate};
 3732:         }
 3733:         if (ref($usertypes) eq 'HASH') {
 3734:             if (keys(%{$usertypes}) > 0) {
 3735:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3736:                                              $dom,$numinrow,$othertitle,
 3737:                                              'statustocreate');
 3738:                 $$rowtotal ++;
 3739:                 $rownum ++;
 3740:             }
 3741:         }
 3742:         $datatable .= &captcha_choice('cancreate',$createsettings,$rownum);
 3743:     } else {
 3744:         my @contexts = ('author','course','domain');
 3745:         my @authtypes = ('int','krb4','krb5','loc');
 3746:         my %checked;
 3747:         if (ref($settings) eq 'HASH') {
 3748:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3749:                 foreach my $item (@contexts) {
 3750:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3751:                         foreach my $auth (@authtypes) {
 3752:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3753:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3754:                             }
 3755:                         }
 3756:                     }
 3757:                 }
 3758:             }
 3759:         } else {
 3760:             foreach my $item (@contexts) {
 3761:                 foreach my $auth (@authtypes) {
 3762:                     $checked{$item}{$auth} = ' checked="checked" ';
 3763:                 }
 3764:             }
 3765:         }
 3766:         my %title = &context_names();
 3767:         my %authname = &authtype_names();
 3768:         my $rownum = 0;
 3769:         my $css_class; 
 3770:         foreach my $item (@contexts) {
 3771:             if ($rownum%2) {
 3772:                 $css_class = '';
 3773:             } else {
 3774:                 $css_class = ' class="LC_odd_row" ';
 3775:             }
 3776:             $datatable .=   '<tr'.$css_class.'>'.
 3777:                             '<td>'.$title{$item}.
 3778:                             '</td><td class="LC_left_item">'.
 3779:                             '<span class="LC_nobreak">';
 3780:             foreach my $auth (@authtypes) {
 3781:                 $datatable .= '<label>'. 
 3782:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3783:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3784:                               $authname{$auth}.'</label>&nbsp;';
 3785:             }
 3786:             $datatable .= '</span></td></tr>';
 3787:             $rownum ++;
 3788:         }
 3789:         $$rowtotal += $rownum;
 3790:     }
 3791:     return $datatable;
 3792: }
 3793: 
 3794: sub captcha_choice {
 3795:     my ($context,$settings,$itemcount) = @_;
 3796:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 3797:     my %lt = &captcha_phrases();
 3798:     $keyentry = 'hidden';
 3799:     if ($context eq 'cancreate') {
 3800:         $rowname = &mt('CAPTCHA validation (e-mail as username)');
 3801:     } elsif ($context eq 'login') {
 3802:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 3803:     }
 3804:     if (ref($settings) eq 'HASH') {
 3805:         if ($settings->{'captcha'}) {
 3806:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 3807:         } else {
 3808:             $checked{'original'} = ' checked="checked"';
 3809:         }
 3810:         if ($settings->{'captcha'} eq 'recaptcha') {
 3811:             $pubtext = $lt{'pub'};
 3812:             $privtext = $lt{'priv'};
 3813:             $keyentry = 'text';
 3814:         }
 3815:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 3816:             $currpub = $settings->{'recaptchakeys'}{'public'};
 3817:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 3818:         }
 3819:     } else {
 3820:         $checked{'original'} = ' checked="checked"';
 3821:     }
 3822:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3823:     my $output = '<tr'.$css_class.'>'.
 3824:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 3825:                  '<table><tr><td>'."\n";
 3826:     foreach my $option ('original','recaptcha','notused') {
 3827:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 3828:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 3829:                    $lt{$option}.'</label></span>';
 3830:         unless ($option eq 'notused') {
 3831:             $output .= ('&nbsp;'x2)."\n";
 3832:         }
 3833:     }
 3834: #
 3835: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 3836: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 3837: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 3838: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 3839: #
 3840:     $output .= '</td></tr>'."\n".
 3841:                '<tr><td>'."\n".
 3842:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 3843:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 3844:                $currpub.'" size="40" /></span><br />'."\n".
 3845:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 3846:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 3847:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 3848:                '</td></tr>';
 3849:     return $output;
 3850: }
 3851: 
 3852: sub user_formats_row {
 3853:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 3854:     my $output;
 3855:     my %text = (
 3856:                    'username' => 'new usernames',
 3857:                    'id'       => 'IDs',
 3858:                    'email'    => 'self-created accounts (e-mail)',
 3859:                );
 3860:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3861:     $output = '<tr '.$css_class.'>'.
 3862:               '<td><span class="LC_nobreak">';
 3863:     if ($type eq 'email') {
 3864:         $output .= &mt("Formats disallowed for $text{$type}: ");
 3865:     } else {
 3866:         $output .= &mt("Format rules to check for $text{$type}: ");
 3867:     }
 3868:     $output .= '</span></td>'.
 3869:                '<td class="LC_left_item" colspan="2"><table>';
 3870:     my $rem;
 3871:     if (ref($ruleorder) eq 'ARRAY') {
 3872:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 3873:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 3874:                 my $rem = $i%($numinrow);
 3875:                 if ($rem == 0) {
 3876:                     if ($i > 0) {
 3877:                         $output .= '</tr>';
 3878:                     }
 3879:                     $output .= '<tr>';
 3880:                 }
 3881:                 my $check = ' ';
 3882:                 if (ref($settings) eq 'HASH') {
 3883:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 3884:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 3885:                             $check = ' checked="checked" ';
 3886:                         }
 3887:                     }
 3888:                 }
 3889:                 $output .= '<td class="LC_left_item">'.
 3890:                            '<span class="LC_nobreak"><label>'.
 3891:                            '<input type="checkbox" name="'.$type.'_rule" '.
 3892:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 3893:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 3894:             }
 3895:         }
 3896:         $rem = @{$ruleorder}%($numinrow);
 3897:     }
 3898:     my $colsleft = $numinrow - $rem;
 3899:     if ($colsleft > 1 ) {
 3900:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3901:                    '&nbsp;</td>';
 3902:     } elsif ($colsleft == 1) {
 3903:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 3904:     }
 3905:     $output .= '</tr></table></td></tr>';
 3906:     return $output;
 3907: }
 3908: 
 3909: sub usercreation_types {
 3910:     my %lt = &Apache::lonlocal::texthash (
 3911:                     author     => 'When adding a co-author',
 3912:                     course     => 'When adding a user to a course',
 3913:                     requestcrs => 'When requesting a course',
 3914:                     selfcreate => 'User creates own account', 
 3915:                     any        => 'Any',
 3916:                     official   => 'Institutional only ',
 3917:                     unofficial => 'Non-institutional only',
 3918:                     email      => 'E-mail address',
 3919:                     login      => 'Institutional Login',
 3920:                     sso        => 'SSO', 
 3921:                     none       => 'None',
 3922:     );
 3923:     return %lt;
 3924: }
 3925: 
 3926: sub authtype_names {
 3927:     my %lt = &Apache::lonlocal::texthash(
 3928:                       int    => 'Internal',
 3929:                       krb4   => 'Kerberos 4',
 3930:                       krb5   => 'Kerberos 5',
 3931:                       loc    => 'Local',
 3932:                   );
 3933:     return %lt;
 3934: }
 3935: 
 3936: sub context_names {
 3937:     my %context_title = &Apache::lonlocal::texthash(
 3938:        author => 'Creating users when an Author',
 3939:        course => 'Creating users when in a course',
 3940:        domain => 'Creating users when a Domain Coordinator',
 3941:     );
 3942:     return %context_title;
 3943: }
 3944: 
 3945: sub print_usermodification {
 3946:     my ($position,$dom,$settings,$rowtotal) = @_;
 3947:     my $numinrow = 4;
 3948:     my ($context,$datatable,$rowcount);
 3949:     if ($position eq 'top') {
 3950:         $rowcount = 0;
 3951:         $context = 'author'; 
 3952:         foreach my $role ('ca','aa') {
 3953:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3954:                                                    $numinrow,$rowcount);
 3955:             $$rowtotal ++;
 3956:             $rowcount ++;
 3957:         }
 3958:     } elsif ($position eq 'middle') {
 3959:         $context = 'course';
 3960:         $rowcount = 0;
 3961:         foreach my $role ('st','ep','ta','in','cr') {
 3962:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3963:                                                    $numinrow,$rowcount);
 3964:             $$rowtotal ++;
 3965:             $rowcount ++;
 3966:         }
 3967:     } elsif ($position eq 'bottom') {
 3968:         $context = 'selfcreate';
 3969:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3970:         $usertypes->{'default'} = $othertitle;
 3971:         if (ref($types) eq 'ARRAY') {
 3972:             push(@{$types},'default');
 3973:             $usertypes->{'default'} = $othertitle;
 3974:             foreach my $status (@{$types}) {
 3975:                 $datatable .= &modifiable_userdata_row($context,$status,$settings,
 3976:                                                        $numinrow,$rowcount,$usertypes);
 3977:                 $$rowtotal ++;
 3978:                 $rowcount ++;
 3979:             }
 3980:         }
 3981:     }
 3982:     return $datatable;
 3983: }
 3984: 
 3985: sub print_defaults {
 3986:     my ($dom,$settings,$rowtotal) = @_;
 3987:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 3988:                  'datelocale_def','portal_def');
 3989:     my %defaults;
 3990:     if (ref($settings) eq 'HASH') {
 3991:         %defaults = %{$settings};
 3992:     } else {
 3993:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 3994:         foreach my $item (@items) {
 3995:             $defaults{$item} = $domdefaults{$item};
 3996:         }
 3997:     }
 3998:     my $titles = &defaults_titles($dom);
 3999:     my $rownum = 0;
 4000:     my ($datatable,$css_class);
 4001:     foreach my $item (@items) {
 4002:         if ($rownum%2) {
 4003:             $css_class = '';
 4004:         } else {
 4005:             $css_class = ' class="LC_odd_row" ';
 4006:         }
 4007:         $datatable .= '<tr'.$css_class.'>'.
 4008:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 4009:                   '</span></td><td class="LC_right_item">';
 4010:         if ($item eq 'auth_def') {
 4011:             my @authtypes = ('internal','krb4','krb5','localauth');
 4012:             my %shortauth = (
 4013:                              internal => 'int',
 4014:                              krb4 => 'krb4',
 4015:                              krb5 => 'krb5',
 4016:                              localauth  => 'loc'
 4017:                            );
 4018:             my %authnames = &authtype_names();
 4019:             foreach my $auth (@authtypes) {
 4020:                 my $checked = ' ';
 4021:                 if ($defaults{$item} eq $auth) {
 4022:                     $checked = ' checked="checked" ';
 4023:                 }
 4024:                 $datatable .= '<label><input type="radio" name="'.$item.
 4025:                               '" value="'.$auth.'"'.$checked.'/>'.
 4026:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 4027:             }
 4028:         } elsif ($item eq 'timezone_def') {
 4029:             my $includeempty = 1;
 4030:             $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 4031:         } elsif ($item eq 'datelocale_def') {
 4032:             my $includeempty = 1;
 4033:             $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 4034:         } elsif ($item eq 'lang_def') {
 4035:             my %langchoices = &get_languages_hash();
 4036:             $langchoices{''} = 'No language preference';
 4037:             %langchoices = &Apache::lonlocal::texthash(%langchoices);
 4038:             $datatable .= &Apache::loncommon::select_form($defaults{$item},$item,
 4039:                                                           \%langchoices);
 4040:         } else {
 4041:             my $size;
 4042:             if ($item eq 'portal_def') {
 4043:                 $size = ' size="25"';
 4044:             }
 4045:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 4046:                           $defaults{$item}.'"'.$size.' />';
 4047:         }
 4048:         $datatable .= '</td></tr>';
 4049:         $rownum ++;
 4050:     }
 4051:     $$rowtotal += $rownum;
 4052:     return $datatable;
 4053: }
 4054: 
 4055: sub get_languages_hash {
 4056:     my %langchoices;
 4057:     foreach my $id (&Apache::loncommon::languageids()) {
 4058:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 4059:         if ($code ne '') {
 4060:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 4061:         }
 4062:     }
 4063:     return %langchoices;
 4064: }
 4065: 
 4066: sub defaults_titles {
 4067:     my ($dom) = @_;
 4068:     my %titles = &Apache::lonlocal::texthash (
 4069:                    'auth_def'      => 'Default authentication type',
 4070:                    'auth_arg_def'  => 'Default authentication argument',
 4071:                    'lang_def'      => 'Default language',
 4072:                    'timezone_def'  => 'Default timezone',
 4073:                    'datelocale_def' => 'Default locale for dates',
 4074:                    'portal_def'     => 'Portal/Default URL',
 4075:                  );
 4076:     if ($dom) {
 4077:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 4078:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 4079:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 4080:         $protocol = 'http' if ($protocol ne 'https');
 4081:         if ($uint_dom) {
 4082:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 4083:                                          $uint_dom);
 4084:         }
 4085:     }
 4086:     return (\%titles);
 4087: }
 4088: 
 4089: sub print_scantronformat {
 4090:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 4091:     my $itemcount = 1;
 4092:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 4093:         %confhash);
 4094:     my $switchserver = &check_switchserver($dom,$confname);
 4095:     my %lt = &Apache::lonlocal::texthash (
 4096:                 default => 'Default bubblesheet format file error',
 4097:                 custom  => 'Custom bubblesheet format file error',
 4098:              );
 4099:     my %scantronfiles = (
 4100:         default => 'default.tab',
 4101:         custom => 'custom.tab',
 4102:     );
 4103:     foreach my $key (keys(%scantronfiles)) {
 4104:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 4105:                               .$scantronfiles{$key};
 4106:     }
 4107:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 4108:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 4109:         if (!$switchserver) {
 4110:             my $servadm = $r->dir_config('lonAdmEMail');
 4111:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 4112:             if ($configuserok eq 'ok') {
 4113:                 if ($author_ok eq 'ok') {
 4114:                     my %legacyfile = (
 4115:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 4116:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 4117:                     );
 4118:                     my %md5chk;
 4119:                     foreach my $type (keys(%legacyfile)) {
 4120:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 4121:                         chomp($md5chk{$type});
 4122:                     }
 4123:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 4124:                         foreach my $type (keys(%legacyfile)) {
 4125:                             ($scantronurls{$type},my $error) = 
 4126:                                 &legacy_scantronformat($r,$dom,$confname,
 4127:                                                  $type,$legacyfile{$type},
 4128:                                                  $scantronurls{$type},
 4129:                                                  $scantronfiles{$type});
 4130:                             if ($error ne '') {
 4131:                                 $error{$type} = $error;
 4132:                             }
 4133:                         }
 4134:                         if (keys(%error) == 0) {
 4135:                             $is_custom = 1;
 4136:                             $confhash{'scantron'}{'scantronformat'} = 
 4137:                                 $scantronurls{'custom'};
 4138:                             my $putresult = 
 4139:                                 &Apache::lonnet::put_dom('configuration',
 4140:                                                          \%confhash,$dom);
 4141:                             if ($putresult ne 'ok') {
 4142:                                 $error{'custom'} = 
 4143:                                     '<span class="LC_error">'.
 4144:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4145:                             }
 4146:                         }
 4147:                     } else {
 4148:                         ($scantronurls{'default'},my $error) =
 4149:                             &legacy_scantronformat($r,$dom,$confname,
 4150:                                           'default',$legacyfile{'default'},
 4151:                                           $scantronurls{'default'},
 4152:                                           $scantronfiles{'default'});
 4153:                         if ($error eq '') {
 4154:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 4155:                             my $putresult =
 4156:                                 &Apache::lonnet::put_dom('configuration',
 4157:                                                          \%confhash,$dom);
 4158:                             if ($putresult ne 'ok') {
 4159:                                 $error{'default'} =
 4160:                                     '<span class="LC_error">'.
 4161:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4162:                             }
 4163:                         } else {
 4164:                             $error{'default'} = $error;
 4165:                         }
 4166:                     }
 4167:                 }
 4168:             }
 4169:         } else {
 4170:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 4171:         }
 4172:     }
 4173:     if (ref($settings) eq 'HASH') {
 4174:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 4175:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 4176:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 4177:                 $scantronurl = '';
 4178:             } else {
 4179:                 $scantronurl = $settings->{'scantronformat'};
 4180:             }
 4181:             $is_custom = 1;
 4182:         } else {
 4183:             $scantronurl = $scantronurls{'default'};
 4184:         }
 4185:     } else {
 4186:         if ($is_custom) {
 4187:             $scantronurl = $scantronurls{'custom'};
 4188:         } else {
 4189:             $scantronurl = $scantronurls{'default'};
 4190:         }
 4191:     }
 4192:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4193:     $datatable .= '<tr'.$css_class.'>';
 4194:     if (!$is_custom) {
 4195:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 4196:                       '<span class="LC_nobreak">';
 4197:         if ($scantronurl) {
 4198:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 4199:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 4200:         } else {
 4201:             $datatable = &mt('File unavailable for display');
 4202:         }
 4203:         $datatable .= '</span></td>';
 4204:         if (keys(%error) == 0) { 
 4205:             $datatable .= '<td valign="bottom">';
 4206:             if (!$switchserver) {
 4207:                 $datatable .= &mt('Upload:').'<br />';
 4208:             }
 4209:         } else {
 4210:             my $errorstr;
 4211:             foreach my $key (sort(keys(%error))) {
 4212:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4213:             }
 4214:             $datatable .= '<td>'.$errorstr;
 4215:         }
 4216:     } else {
 4217:         if (keys(%error) > 0) {
 4218:             my $errorstr;
 4219:             foreach my $key (sort(keys(%error))) {
 4220:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4221:             } 
 4222:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 4223:         } elsif ($scantronurl) {
 4224:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 4225:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4226:             $datatable .= '<td><span class="LC_nobreak">'.
 4227:                           $link.
 4228:                           '<label><input type="checkbox" name="scantronformat_del"'.
 4229:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 4230:                           '<td><span class="LC_nobreak">&nbsp;'.
 4231:                           &mt('Replace:').'</span><br />';
 4232:         }
 4233:     }
 4234:     if (keys(%error) == 0) {
 4235:         if ($switchserver) {
 4236:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4237:         } else {
 4238:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4239:                          '<input type="file" name="scantronformat" /></span>';
 4240:         }
 4241:     }
 4242:     $datatable .= '</td></tr>';
 4243:     $$rowtotal ++;
 4244:     return $datatable;
 4245: }
 4246: 
 4247: sub legacy_scantronformat {
 4248:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 4249:     my ($url,$error);
 4250:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 4251:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 4252:         (my $result,$url) =
 4253:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 4254:                          '','',$newfile);
 4255:         if ($result ne 'ok') {
 4256:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 4257:         }
 4258:     }
 4259:     return ($url,$error);
 4260: }
 4261: 
 4262: sub print_coursecategories {
 4263:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 4264:     my $datatable;
 4265:     if ($position eq 'top') {
 4266:         my $toggle_cats_crs = ' ';
 4267:         my $toggle_cats_dom = ' checked="checked" ';
 4268:         my $can_cat_crs = ' ';
 4269:         my $can_cat_dom = ' checked="checked" ';
 4270:         my $toggle_catscomm_comm = ' ';
 4271:         my $toggle_catscomm_dom = ' checked="checked" ';
 4272:         my $can_catcomm_comm = ' ';
 4273:         my $can_catcomm_dom = ' checked="checked" ';
 4274: 
 4275:         if (ref($settings) eq 'HASH') {
 4276:             if ($settings->{'togglecats'} eq 'crs') {
 4277:                 $toggle_cats_crs = $toggle_cats_dom;
 4278:                 $toggle_cats_dom = ' ';
 4279:             }
 4280:             if ($settings->{'categorize'} eq 'crs') {
 4281:                 $can_cat_crs = $can_cat_dom;
 4282:                 $can_cat_dom = ' ';
 4283:             }
 4284:             if ($settings->{'togglecatscomm'} eq 'comm') {
 4285:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 4286:                 $toggle_catscomm_dom = ' ';
 4287:             }
 4288:             if ($settings->{'categorizecomm'} eq 'comm') {
 4289:                 $can_catcomm_comm = $can_catcomm_dom;
 4290:                 $can_catcomm_dom = ' ';
 4291:             }
 4292:         }
 4293:         my %title = &Apache::lonlocal::texthash (
 4294:                      togglecats     => 'Show/Hide a course in catalog',
 4295:                      togglecatscomm => 'Show/Hide a community in catalog',
 4296:                      categorize     => 'Assign a category to a course',
 4297:                      categorizecomm => 'Assign a category to a community',
 4298:                     );
 4299:         my %level = &Apache::lonlocal::texthash (
 4300:                      dom  => 'Set in Domain',
 4301:                      crs  => 'Set in Course',
 4302:                      comm => 'Set in Community',
 4303:                     );
 4304:         $datatable = '<tr class="LC_odd_row">'.
 4305:                   '<td>'.$title{'togglecats'}.'</td>'.
 4306:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4307:                   '<input type="radio" name="togglecats"'.
 4308:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4309:                   '<label><input type="radio" name="togglecats"'.
 4310:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4311:                   '</tr><tr>'.
 4312:                   '<td>'.$title{'categorize'}.'</td>'.
 4313:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4314:                   '<label><input type="radio" name="categorize"'.
 4315:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4316:                   '<label><input type="radio" name="categorize"'.
 4317:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4318:                   '</tr><tr class="LC_odd_row">'.
 4319:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 4320:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4321:                   '<input type="radio" name="togglecatscomm"'.
 4322:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4323:                   '<label><input type="radio" name="togglecatscomm"'.
 4324:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4325:                   '</tr><tr>'.
 4326:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 4327:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4328:                   '<label><input type="radio" name="categorizecomm"'.
 4329:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4330:                   '<label><input type="radio" name="categorizecomm"'.
 4331:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4332:                   '</tr>';
 4333:         $$rowtotal += 4;
 4334:     } else {
 4335:         my $css_class;
 4336:         my $itemcount = 1;
 4337:         my $cathash; 
 4338:         if (ref($settings) eq 'HASH') {
 4339:             $cathash = $settings->{'cats'};
 4340:         }
 4341:         if (ref($cathash) eq 'HASH') {
 4342:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4343:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4344:                                                    \%allitems,\%idx,\@jsarray);
 4345:             my $maxdepth = scalar(@cats);
 4346:             my $colattrib = '';
 4347:             if ($maxdepth > 2) {
 4348:                 $colattrib = ' colspan="2" ';
 4349:             }
 4350:             my @path;
 4351:             if (@cats > 0) {
 4352:                 if (ref($cats[0]) eq 'ARRAY') {
 4353:                     my $numtop = @{$cats[0]};
 4354:                     my $maxnum = $numtop;
 4355:                     my %default_names = (
 4356:                           instcode    => &mt('Official courses'),
 4357:                           communities => &mt('Communities'),
 4358:                     );
 4359: 
 4360:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4361:                         ($cathash->{'instcode::0'} eq '') ||
 4362:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4363:                         ($cathash->{'communities::0'} eq '')) {
 4364:                         $maxnum ++;
 4365:                     }
 4366:                     my $lastidx;
 4367:                     for (my $i=0; $i<$numtop; $i++) {
 4368:                         my $parent = $cats[0][$i];
 4369:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4370:                         my $item = &escape($parent).'::0';
 4371:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4372:                         $lastidx = $idx{$item};
 4373:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4374:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4375:                         for (my $k=0; $k<=$maxnum; $k++) {
 4376:                             my $vpos = $k+1;
 4377:                             my $selstr;
 4378:                             if ($k == $i) {
 4379:                                 $selstr = ' selected="selected" ';
 4380:                             }
 4381:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4382:                         }
 4383:                         $datatable .= '</select></span></td><td>';
 4384:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4385:                             $datatable .=  '<span class="LC_nobreak">'
 4386:                                            .$default_names{$parent}.'</span>';
 4387:                             if ($parent eq 'instcode') {
 4388:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4389:                                               .&mt('with institutional codes')
 4390:                                               .')</span></td><td'.$colattrib.'>';
 4391:                             } else {
 4392:                                 $datatable .= '<table><tr><td>';
 4393:                             }
 4394:                             $datatable .= '<span class="LC_nobreak">'
 4395:                                           .'<label><input type="radio" name="'
 4396:                                           .$parent.'" value="1" checked="checked" />'
 4397:                                           .&mt('Display').'</label>';
 4398:                             if ($parent eq 'instcode') {
 4399:                                 $datatable .= '&nbsp;';
 4400:                             } else {
 4401:                                 $datatable .= '</span></td></tr><tr><td>'
 4402:                                               .'<span class="LC_nobreak">';
 4403:                             }
 4404:                             $datatable .= '<label><input type="radio" name="'
 4405:                                           .$parent.'" value="0" />'
 4406:                                           .&mt('Do not display').'</label></span>';
 4407:                             if ($parent eq 'communities') {
 4408:                                 $datatable .= '</td></tr></table>';
 4409:                             }
 4410:                             $datatable .= '</td>';
 4411:                         } else {
 4412:                             $datatable .= $parent
 4413:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 4414:                                           .'<input type="checkbox" name="deletecategory" '
 4415:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 4416:                         }
 4417:                         my $depth = 1;
 4418:                         push(@path,$parent);
 4419:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 4420:                         pop(@path);
 4421:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 4422:                         $itemcount ++;
 4423:                     }
 4424:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4425:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 4426:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 4427:                     for (my $k=0; $k<=$maxnum; $k++) {
 4428:                         my $vpos = $k+1;
 4429:                         my $selstr;
 4430:                         if ($k == $numtop) {
 4431:                             $selstr = ' selected="selected" ';
 4432:                         }
 4433:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4434:                     }
 4435:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 4436:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 4437:                                   .'</tr>'."\n";
 4438:                     $itemcount ++;
 4439:                     foreach my $default ('instcode','communities') {
 4440:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 4441:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4442:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 4443:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 4444:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 4445:                             for (my $k=0; $k<=$maxnum; $k++) {
 4446:                                 my $vpos = $k+1;
 4447:                                 my $selstr;
 4448:                                 if ($k == $maxnum) {
 4449:                                     $selstr = ' selected="selected" ';
 4450:                                 }
 4451:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4452:                             }
 4453:                             $datatable .= '</select></span></td>'.
 4454:                                           '<td><span class="LC_nobreak">'.
 4455:                                           $default_names{$default}.'</span>';
 4456:                             if ($default eq 'instcode') {
 4457:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 4458:                                               .&mt('with institutional codes').')</span>';
 4459:                             }
 4460:                             $datatable .= '</td>'
 4461:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 4462:                                           .&mt('Display').'</label>&nbsp;'
 4463:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 4464:                                           .&mt('Do not display').'</label></span></td></tr>';
 4465:                         }
 4466:                     }
 4467:                 }
 4468:             } else {
 4469:                 $datatable .= &initialize_categories($itemcount);
 4470:             }
 4471:         } else {
 4472:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 4473:                           .&initialize_categories($itemcount);
 4474:         }
 4475:         $$rowtotal += $itemcount;
 4476:     }
 4477:     return $datatable;
 4478: }
 4479: 
 4480: sub print_serverstatuses {
 4481:     my ($dom,$settings,$rowtotal) = @_;
 4482:     my $datatable;
 4483:     my @pages = &serverstatus_pages();
 4484:     my (%namedaccess,%machineaccess);
 4485:     foreach my $type (@pages) {
 4486:         $namedaccess{$type} = '';
 4487:         $machineaccess{$type}= '';
 4488:     }
 4489:     if (ref($settings) eq 'HASH') {
 4490:         foreach my $type (@pages) {
 4491:             if (exists($settings->{$type})) {
 4492:                 if (ref($settings->{$type}) eq 'HASH') {
 4493:                     foreach my $key (keys(%{$settings->{$type}})) {
 4494:                         if ($key eq 'namedusers') {
 4495:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4496:                         } elsif ($key eq 'machines') {
 4497:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4498:                         }
 4499:                     }
 4500:                 }
 4501:             }
 4502:         }
 4503:     }
 4504:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4505:     my $rownum = 0;
 4506:     my $css_class;
 4507:     foreach my $type (@pages) {
 4508:         $rownum ++;
 4509:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4510:         $datatable .= '<tr'.$css_class.'>'.
 4511:                       '<td><span class="LC_nobreak">'.
 4512:                       $titles->{$type}.'</span></td>'.
 4513:                       '<td class="LC_left_item">'.
 4514:                       '<input type="text" name="'.$type.'_namedusers" '.
 4515:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4516:                       '<td class="LC_right_item">'.
 4517:                       '<span class="LC_nobreak">'.
 4518:                       '<input type="text" name="'.$type.'_machines" '.
 4519:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4520:                       '</td></tr>'."\n";
 4521:     }
 4522:     $$rowtotal += $rownum;
 4523:     return $datatable;
 4524: }
 4525: 
 4526: sub serverstatus_pages {
 4527:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4528:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 4529:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 4530:             'uniquecodes');
 4531: }
 4532: 
 4533: sub coursecategories_javascript {
 4534:     my ($settings) = @_;
 4535:     my ($output,$jstext,$cathash);
 4536:     if (ref($settings) eq 'HASH') {
 4537:         $cathash = $settings->{'cats'};
 4538:     }
 4539:     if (ref($cathash) eq 'HASH') {
 4540:         my (@cats,@jsarray,%idx);
 4541:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4542:         if (@jsarray > 0) {
 4543:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4544:             for (my $i=0; $i<@jsarray; $i++) {
 4545:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4546:                     my $catstr = join('","',@{$jsarray[$i]});
 4547:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4548:                 }
 4549:             }
 4550:         }
 4551:     } else {
 4552:         $jstext  = '    var categories = Array(1);'."\n".
 4553:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4554:     }
 4555:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4556:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4557:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4558:     $output = <<"ENDSCRIPT";
 4559: <script type="text/javascript">
 4560: // <![CDATA[
 4561: function reorderCats(form,parent,item,idx) {
 4562:     var changedVal;
 4563: $jstext
 4564:     var newpos = 'addcategory_pos';
 4565:     var current = new Array;
 4566:     if (parent == '') {
 4567:         var has_instcode = 0;
 4568:         var maxtop = categories[idx].length;
 4569:         for (var j=0; j<maxtop; j++) {
 4570:             if (categories[idx][j] == 'instcode::0') {
 4571:                 has_instcode == 1;
 4572:             }
 4573:         }
 4574:         if (has_instcode == 0) {
 4575:             categories[idx][maxtop] = 'instcode_pos';
 4576:         }
 4577:     } else {
 4578:         newpos += '_'+parent;
 4579:     }
 4580:     var maxh = 1 + categories[idx].length;
 4581:     var current = new Array;
 4582:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4583:     if (item == newpos) {
 4584:         changedVal = newitemVal;
 4585:     } else {
 4586:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4587:         current[newitemVal] = newpos;
 4588:     }
 4589:     for (var i=0; i<categories[idx].length; i++) {
 4590:         var elementName = categories[idx][i];
 4591:         if (elementName != item) {
 4592:             if (form.elements[elementName]) {
 4593:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4594:                 current[currVal] = elementName;
 4595:             }
 4596:         }
 4597:     }
 4598:     var oldVal;
 4599:     for (var j=0; j<maxh; j++) {
 4600:         if (current[j] == undefined) {
 4601:             oldVal = j;
 4602:         }
 4603:     }
 4604:     if (oldVal < changedVal) {
 4605:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4606:            var elementName = current[k];
 4607:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4608:         }
 4609:     } else {
 4610:         for (var k=changedVal; k<oldVal; k++) {
 4611:             var elementName = current[k];
 4612:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4613:         }
 4614:     }
 4615:     return;
 4616: }
 4617: 
 4618: function categoryCheck(form) {
 4619:     if (form.elements['addcategory_name'].value == 'instcode') {
 4620:         alert('$instcode_reserved\\n$choose_again');
 4621:         return false;
 4622:     }
 4623:     if (form.elements['addcategory_name'].value == 'communities') {
 4624:         alert('$communities_reserved\\n$choose_again');
 4625:         return false;
 4626:     }
 4627:     return true;
 4628: }
 4629: 
 4630: // ]]>
 4631: </script>
 4632: 
 4633: ENDSCRIPT
 4634:     return $output;
 4635: }
 4636: 
 4637: sub initialize_categories {
 4638:     my ($itemcount) = @_;
 4639:     my ($datatable,$css_class,$chgstr);
 4640:     my %default_names = (
 4641:                       instcode    => 'Official courses (with institutional codes)',
 4642:                       communities => 'Communities',
 4643:                         );
 4644:     my $select0 = ' selected="selected"';
 4645:     my $select1 = '';
 4646:     foreach my $default ('instcode','communities') {
 4647:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4648:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4649:         if ($default eq 'communities') {
 4650:             $select1 = $select0;
 4651:             $select0 = '';
 4652:         }
 4653:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4654:                      .'<select name="'.$default.'_pos">'
 4655:                      .'<option value="0"'.$select0.'>1</option>'
 4656:                      .'<option value="1"'.$select1.'>2</option>'
 4657:                      .'<option value="2">3</option></select>&nbsp;'
 4658:                      .$default_names{$default}
 4659:                      .'</span></td><td><span class="LC_nobreak">'
 4660:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4661:                      .&mt('Display').'</label>&nbsp;<label>'
 4662:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4663:                  .'</label></span></td></tr>';
 4664:         $itemcount ++;
 4665:     }
 4666:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4667:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4668:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4669:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4670:                   .'<option value="0">1</option>'
 4671:                   .'<option value="1">2</option>'
 4672:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4673:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4674:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4675:     return $datatable;
 4676: }
 4677: 
 4678: sub build_category_rows {
 4679:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 4680:     my ($text,$name,$item,$chgstr);
 4681:     if (ref($cats) eq 'ARRAY') {
 4682:         my $maxdepth = scalar(@{$cats});
 4683:         if (ref($cats->[$depth]) eq 'HASH') {
 4684:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 4685:                 my $numchildren = @{$cats->[$depth]{$parent}};
 4686:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4687:                 $text .= '<td><table class="LC_data_table">';
 4688:                 my ($idxnum,$parent_name,$parent_item);
 4689:                 my $higher = $depth - 1;
 4690:                 if ($higher == 0) {
 4691:                     $parent_name = &escape($parent).'::'.$higher;
 4692:                 } else {
 4693:                     if (ref($path) eq 'ARRAY') {
 4694:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4695:                     }
 4696:                 }
 4697:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4698:                 for (my $j=0; $j<=$numchildren; $j++) {
 4699:                     if ($j < $numchildren) {
 4700:                         $name = $cats->[$depth]{$parent}[$j];
 4701:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4702:                         $idxnum = $idx->{$item};
 4703:                     } else {
 4704:                         $name = $parent_name;
 4705:                         $item = $parent_item;
 4706:                     }
 4707:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4708:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4709:                     for (my $i=0; $i<=$numchildren; $i++) {
 4710:                         my $vpos = $i+1;
 4711:                         my $selstr;
 4712:                         if ($j == $i) {
 4713:                             $selstr = ' selected="selected" ';
 4714:                         }
 4715:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4716:                     }
 4717:                     $text .= '</select>&nbsp;';
 4718:                     if ($j < $numchildren) {
 4719:                         my $deeper = $depth+1;
 4720:                         $text .= $name.'&nbsp;'
 4721:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4722:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4723:                         if(ref($path) eq 'ARRAY') {
 4724:                             push(@{$path},$name);
 4725:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4726:                             pop(@{$path});
 4727:                         }
 4728:                     } else {
 4729:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 4730:                         if ($j == $numchildren) {
 4731:                             $text .= $name;
 4732:                         } else {
 4733:                             $text .= $item;
 4734:                         }
 4735:                         $text .= '" value="" />';
 4736:                     }
 4737:                     $text .= '</td></tr>';
 4738:                 }
 4739:                 $text .= '</table></td>';
 4740:             } else {
 4741:                 my $higher = $depth-1;
 4742:                 if ($higher == 0) {
 4743:                     $name = &escape($parent).'::'.$higher;
 4744:                 } else {
 4745:                     if (ref($path) eq 'ARRAY') {
 4746:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4747:                     }
 4748:                 }
 4749:                 my $colspan;
 4750:                 if ($parent ne 'instcode') {
 4751:                     $colspan = $maxdepth - $depth - 1;
 4752:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 4753:                 }
 4754:             }
 4755:         }
 4756:     }
 4757:     return $text;
 4758: }
 4759: 
 4760: sub modifiable_userdata_row {
 4761:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes) = @_;
 4762:     my $rolename;
 4763:     if ($context eq 'selfcreate') {
 4764:         if (ref($usertypes) eq 'HASH') {
 4765:             $rolename = $usertypes->{$role};
 4766:         } else {
 4767:             $rolename = $role;
 4768:         }
 4769:     } else {
 4770:         if ($role eq 'cr') {
 4771:             $rolename = &mt('Custom role');
 4772:         } else {
 4773:             $rolename = &Apache::lonnet::plaintext($role);
 4774:         }
 4775:     }
 4776:     my @fields = ('lastname','firstname','middlename','generation',
 4777:                   'permanentemail','id');
 4778:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4779:     my $output;
 4780:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4781:     $output = '<tr '.$css_class.'>'.
 4782:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 4783:               '<td class="LC_left_item" colspan="2"><table>';
 4784:     my $rem;
 4785:     my %checks;
 4786:     if (ref($settings) eq 'HASH') {
 4787:         if (ref($settings->{$context}) eq 'HASH') {
 4788:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 4789:                 foreach my $field (@fields) {
 4790:                     if ($settings->{$context}->{$role}->{$field}) {
 4791:                         $checks{$field} = ' checked="checked" ';
 4792:                     }
 4793:                 }
 4794:             }
 4795:         }
 4796:     }
 4797:     for (my $i=0; $i<@fields; $i++) {
 4798:         my $rem = $i%($numinrow);
 4799:         if ($rem == 0) {
 4800:             if ($i > 0) {
 4801:                 $output .= '</tr>';
 4802:             }
 4803:             $output .= '<tr>';
 4804:         }
 4805:         my $check = ' ';
 4806:         if (exists($checks{$fields[$i]})) {
 4807:             $check = $checks{$fields[$i]}
 4808:         } else {
 4809:             if ($role eq 'st') {
 4810:                 if (ref($settings) ne 'HASH') {
 4811:                     $check = ' checked="checked" '; 
 4812:                 }
 4813:             }
 4814:         }
 4815:         $output .= '<td class="LC_left_item">'.
 4816:                    '<span class="LC_nobreak"><label>'.
 4817:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 4818:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 4819:                    '</label></span></td>';
 4820:         $rem = @fields%($numinrow);
 4821:     }
 4822:     my $colsleft = $numinrow - $rem;
 4823:     if ($colsleft > 1 ) {
 4824:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4825:                    '&nbsp;</td>';
 4826:     } elsif ($colsleft == 1) {
 4827:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4828:     }
 4829:     $output .= '</tr></table></td></tr>';
 4830:     return $output;
 4831: }
 4832: 
 4833: sub insttypes_row {
 4834:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context) = @_;
 4835:     my %lt = &Apache::lonlocal::texthash (
 4836:                       cansearch => 'Users allowed to search',
 4837:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 4838:                       lockablenames => 'User preference to lock name',
 4839:              );
 4840:     my $showdom;
 4841:     if ($context eq 'cansearch') {
 4842:         $showdom = ' ('.$dom.')';
 4843:     }
 4844:     my $class = 'LC_left_item';
 4845:     if ($context eq 'statustocreate') {
 4846:         $class = 'LC_right_item';
 4847:     }
 4848:     my $output =  '<tr class="LC_odd_row">'.
 4849:                   '<td>'.$lt{$context}.$showdom.
 4850:                   '</td><td class="'.$class.'" colspan="2"><table>';
 4851:     my $rem;
 4852:     if (ref($types) eq 'ARRAY') {
 4853:         for (my $i=0; $i<@{$types}; $i++) {
 4854:             if (defined($usertypes->{$types->[$i]})) {
 4855:                 my $rem = $i%($numinrow);
 4856:                 if ($rem == 0) {
 4857:                     if ($i > 0) {
 4858:                         $output .= '</tr>';
 4859:                     }
 4860:                     $output .= '<tr>';
 4861:                 }
 4862:                 my $check = ' ';
 4863:                 if (ref($settings) eq 'HASH') {
 4864:                     if (ref($settings->{$context}) eq 'ARRAY') {
 4865:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 4866:                             $check = ' checked="checked" ';
 4867:                         }
 4868:                     } elsif ($context eq 'statustocreate') {
 4869:                         $check = ' checked="checked" ';
 4870:                     }
 4871:                 }
 4872:                 $output .= '<td class="LC_left_item">'.
 4873:                            '<span class="LC_nobreak"><label>'.
 4874:                            '<input type="checkbox" name="'.$context.'" '.
 4875:                            'value="'.$types->[$i].'"'.$check.'/>'.
 4876:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 4877:             }
 4878:         }
 4879:         $rem = @{$types}%($numinrow);
 4880:     }
 4881:     my $colsleft = $numinrow - $rem;
 4882:     if (($rem == 0) && (@{$types} > 0)) {
 4883:         $output .= '<tr>';
 4884:     }
 4885:     if ($colsleft > 1) {
 4886:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 4887:     } else {
 4888:         $output .= '<td class="LC_left_item">';
 4889:     }
 4890:     my $defcheck = ' ';
 4891:     if (ref($settings) eq 'HASH') {  
 4892:         if (ref($settings->{$context}) eq 'ARRAY') {
 4893:             if (grep(/^default$/,@{$settings->{$context}})) {
 4894:                 $defcheck = ' checked="checked" ';
 4895:             }
 4896:         } elsif ($context eq 'statustocreate') {
 4897:             $defcheck = ' checked="checked" ';
 4898:         }
 4899:     }
 4900:     $output .= '<span class="LC_nobreak"><label>'.
 4901:                '<input type="checkbox" name="'.$context.'" '.
 4902:                'value="default"'.$defcheck.'/>'.
 4903:                $othertitle.'</label></span></td>'.
 4904:                '</tr></table></td></tr>';
 4905:     return $output;
 4906: }
 4907: 
 4908: sub sorted_searchtitles {
 4909:     my %searchtitles = &Apache::lonlocal::texthash(
 4910:                          'uname' => 'username',
 4911:                          'lastname' => 'last name',
 4912:                          'lastfirst' => 'last name, first name',
 4913:                      );
 4914:     my @titleorder = ('uname','lastname','lastfirst');
 4915:     return (\%searchtitles,\@titleorder);
 4916: }
 4917: 
 4918: sub sorted_searchtypes {
 4919:     my %srchtypes_desc = (
 4920:                            exact    => 'is exact match',
 4921:                            contains => 'contains ..',
 4922:                            begins   => 'begins with ..',
 4923:                          );
 4924:     my @srchtypeorder = ('exact','begins','contains');
 4925:     return (\%srchtypes_desc,\@srchtypeorder);
 4926: }
 4927: 
 4928: sub usertype_update_row {
 4929:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 4930:     my $datatable;
 4931:     my $numinrow = 4;
 4932:     foreach my $type (@{$types}) {
 4933:         if (defined($usertypes->{$type})) {
 4934:             $$rownums ++;
 4935:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 4936:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 4937:                           '</td><td class="LC_left_item"><table>';
 4938:             for (my $i=0; $i<@{$fields}; $i++) {
 4939:                 my $rem = $i%($numinrow);
 4940:                 if ($rem == 0) {
 4941:                     if ($i > 0) {
 4942:                         $datatable .= '</tr>';
 4943:                     }
 4944:                     $datatable .= '<tr>';
 4945:                 }
 4946:                 my $check = ' ';
 4947:                 if (ref($settings) eq 'HASH') {
 4948:                     if (ref($settings->{'fields'}) eq 'HASH') {
 4949:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 4950:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 4951:                                 $check = ' checked="checked" ';
 4952:                             }
 4953:                         }
 4954:                     }
 4955:                 }
 4956: 
 4957:                 if ($i == @{$fields}-1) {
 4958:                     my $colsleft = $numinrow - $rem;
 4959:                     if ($colsleft > 1) {
 4960:                         $datatable .= '<td colspan="'.$colsleft.'">';
 4961:                     } else {
 4962:                         $datatable .= '<td>';
 4963:                     }
 4964:                 } else {
 4965:                     $datatable .= '<td>';
 4966:                 }
 4967:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4968:                               '<input type="checkbox" name="updateable_'.$type.
 4969:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 4970:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 4971:             }
 4972:             $datatable .= '</tr></table></td></tr>';
 4973:         }
 4974:     }
 4975:     return $datatable;
 4976: }
 4977: 
 4978: sub modify_login {
 4979:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 4980:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 4981:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 4982:     %title = ( coursecatalog => 'Display course catalog',
 4983:                adminmail => 'Display administrator E-mail address',
 4984:                helpdesk  => 'Display "Contact Helpdesk" link',
 4985:                newuser => 'Link for visitors to create a user account',
 4986:                loginheader => 'Log-in box header');
 4987:     @offon = ('off','on');
 4988:     if (ref($domconfig{login}) eq 'HASH') {
 4989:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 4990:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 4991:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 4992:             }
 4993:         }
 4994:     }
 4995:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 4996:                                            \%domconfig,\%loginhash);
 4997:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 4998:     foreach my $item (@toggles) {
 4999:         $loginhash{login}{$item} = $env{'form.'.$item};
 5000:     }
 5001:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 5002:     if (ref($colchanges{'login'}) eq 'HASH') {  
 5003:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 5004:                                          \%loginhash);
 5005:     }
 5006: 
 5007:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5008:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 5009:     if (keys(%servers) > 1) {
 5010:         foreach my $lonhost (keys(%servers)) {
 5011:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 5012:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 5013:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 5014:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 5015:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 5016:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5017:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5018:                         $changes{'loginvia'}{$lonhost} = 1;
 5019:                     } else {
 5020:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 5021:                         $changes{'loginvia'}{$lonhost} = 1;
 5022:                     }
 5023:                 } else {
 5024:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5025:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5026:                         $changes{'loginvia'}{$lonhost} = 1;
 5027:                     }
 5028:                 }
 5029:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 5030:                     foreach my $item (@loginvia_attribs) {
 5031:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 5032:                     }
 5033:                 } else {
 5034:                     foreach my $item (@loginvia_attribs) {
 5035:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5036:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5037:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 5038:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5039:                                 $new = '/';
 5040:                             }
 5041:                         }
 5042:                         if (($item eq 'custompath') && 
 5043:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5044:                             $new = '';
 5045:                         }
 5046:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 5047:                             $changes{'loginvia'}{$lonhost} = 1;
 5048:                         }
 5049:                         if ($item eq 'exempt') {
 5050:                             $new =~ s/^\s+//;
 5051:                             $new =~ s/\s+$//;
 5052:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 5053:                             my @okips;
 5054:                             foreach my $ip (@poss_ips) {
 5055:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 5056:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 5057:                                         push(@okips,$ip); 
 5058:                                     }
 5059:                                 }
 5060:                             }
 5061:                             if (@okips > 0) {
 5062:                                 $new = join(',',@okips); 
 5063:                             } else {
 5064:                                 $new = ''; 
 5065:                             }
 5066:                         }
 5067:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5068:                     }
 5069:                 }
 5070:             } else {
 5071:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5072:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5073:                     $changes{'loginvia'}{$lonhost} = 1;
 5074:                     foreach my $item (@loginvia_attribs) {
 5075:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5076:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5077:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5078:                                 $new = '/';
 5079:                             }
 5080:                         }
 5081:                         if (($item eq 'custompath') && 
 5082:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5083:                             $new = '';
 5084:                         }
 5085:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5086:                     }
 5087:                 }
 5088:             }
 5089:         }
 5090:     }
 5091: 
 5092:     my $servadm = $r->dir_config('lonAdmEMail');
 5093:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 5094:     if (ref($domconfig{'login'}) eq 'HASH') {
 5095:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 5096:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 5097:                 if ($lang eq 'nolang') {
 5098:                     push(@currlangs,$lang);
 5099:                 } elsif (defined($langchoices{$lang})) {
 5100:                     push(@currlangs,$lang);
 5101:                 } else {
 5102:                     next;
 5103:                 }
 5104:             }
 5105:         }
 5106:     }
 5107:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 5108:     if (@currlangs > 0) {
 5109:         foreach my $lang (@currlangs) {
 5110:             if (grep(/^\Q$lang\E$/,@delurls)) {
 5111:                 $changes{'helpurl'}{$lang} = 1;
 5112:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 5113:                 $changes{'helpurl'}{$lang} = 1;
 5114:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 5115:                 push(@newlangs,$lang);
 5116:             } else {
 5117:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5118:             }
 5119:         }
 5120:     }
 5121:     unless (grep(/^nolang$/,@currlangs)) {
 5122:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 5123:             $changes{'helpurl'}{'nolang'} = 1;
 5124:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 5125:             push(@newlangs,'nolang');
 5126:         }
 5127:     }
 5128:     if ($env{'form.loginhelpurl_add_lang'}) {
 5129:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 5130:             ($env{'form.loginhelpurl_add_file.filename'})) {
 5131:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 5132:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 5133:         }
 5134:     }
 5135:     if ((@newlangs > 0) || ($addedfile)) {
 5136:         my $error;
 5137:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 5138:         if ($configuserok eq 'ok') {
 5139:             if ($switchserver) {
 5140:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 5141:             } elsif ($author_ok eq 'ok') {
 5142:                 my @allnew = @newlangs;
 5143:                 if ($addedfile ne '') {
 5144:                     push(@allnew,$addedfile);
 5145:                 }
 5146:                 foreach my $lang (@allnew) {
 5147:                     my $formelem = 'loginhelpurl_'.$lang;
 5148:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 5149:                         $formelem = 'loginhelpurl_add_file';
 5150:                     }
 5151:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 5152:                                                                "help/$lang",'','',$newfile{$lang});
 5153:                     if ($result eq 'ok') {
 5154:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 5155:                         $changes{'helpurl'}{$lang} = 1;
 5156:                     } else {
 5157:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 5158:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 5159:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 5160:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 5161: 
 5162:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5163:                         }
 5164:                     }
 5165:                 }
 5166:             } else {
 5167:                 $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);
 5168:             }
 5169:         } else {
 5170:             $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);
 5171:         }
 5172:         if ($error) {
 5173:             &Apache::lonnet::logthis($error);
 5174:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5175:         }
 5176:     }
 5177:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 5178: 
 5179:     my $defaulthelpfile = '/adm/loginproblems.html';
 5180:     my $defaulttext = &mt('Default in use');
 5181: 
 5182:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 5183:                                              $dom);
 5184:     if ($putresult eq 'ok') {
 5185:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5186:         my %defaultchecked = (
 5187:                     'coursecatalog' => 'on',
 5188:                     'helpdesk'      => 'on',
 5189:                     'adminmail'     => 'off',
 5190:                     'newuser'       => 'off',
 5191:         );
 5192:         if (ref($domconfig{'login'}) eq 'HASH') {
 5193:             foreach my $item (@toggles) {
 5194:                 if ($defaultchecked{$item} eq 'on') { 
 5195:                     if (($domconfig{'login'}{$item} eq '0') &&
 5196:                         ($env{'form.'.$item} eq '1')) {
 5197:                         $changes{$item} = 1;
 5198:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5199:                               $domconfig{'login'}{$item} eq '1') &&
 5200:                              ($env{'form.'.$item} eq '0')) {
 5201:                         $changes{$item} = 1;
 5202:                     }
 5203:                 } elsif ($defaultchecked{$item} eq 'off') {
 5204:                     if (($domconfig{'login'}{$item} eq '1') &&
 5205:                         ($env{'form.'.$item} eq '0')) {
 5206:                         $changes{$item} = 1;
 5207:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5208:                               $domconfig{'login'}{$item} eq '0') &&
 5209:                              ($env{'form.'.$item} eq '1')) {
 5210:                         $changes{$item} = 1;
 5211:                     }
 5212:                 }
 5213:             }
 5214:         }
 5215:         if (keys(%changes) > 0 || $colchgtext) {
 5216:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5217:             if (ref($lastactref) eq 'HASH') {
 5218:                 $lastactref->{'domainconfig'} = 1;
 5219:             }
 5220:             $resulttext = &mt('Changes made:').'<ul>';
 5221:             foreach my $item (sort(keys(%changes))) {
 5222:                 if ($item eq 'loginvia') {
 5223:                     if (ref($changes{$item}) eq 'HASH') {
 5224:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 5225:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 5226:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 5227:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 5228:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 5229:                                     $protocol = 'http' if ($protocol ne 'https');
 5230:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 5231: 
 5232:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 5233:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 5234:                                     } else {
 5235:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 5236:                                     }
 5237:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 5238:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 5239:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 5240:                                     }
 5241:                                     $resulttext .= '</li>';
 5242:                                 } else {
 5243:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 5244:                                 }
 5245:                             } else {
 5246:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 5247:                             }
 5248:                         }
 5249:                         $resulttext .= '</ul></li>';
 5250:                     }
 5251:                 } elsif ($item eq 'helpurl') {
 5252:                     if (ref($changes{$item}) eq 'HASH') {
 5253:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 5254:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 5255:                                 my ($chg,$link);
 5256:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 5257:                                 if ($lang eq 'nolang') {
 5258:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 5259:                                 } else {
 5260:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 5261:                                 }
 5262:                                 $resulttext .= '<li>'.$chg.'</li>';
 5263:                             } else {
 5264:                                 my $chg;
 5265:                                 if ($lang eq 'nolang') {
 5266:                                     $chg = &mt('custom log-in help file for no preferred language');
 5267:                                 } else {
 5268:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 5269:                                 }
 5270:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 5271:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 5272:                                                       '?inhibitmenu=yes',$chg,600,500).
 5273:                                                '</li>';
 5274:                             }
 5275:                         }
 5276:                     }
 5277:                 } elsif ($item eq 'captcha') {
 5278:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5279:                         my $chgtxt;
 5280:                         if ($loginhash{'login'}{$item} eq 'notused') {
 5281:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 5282:                         } else {
 5283:                             my %captchas = &captcha_phrases();
 5284:                             if ($captchas{$loginhash{'login'}{$item}}) {
 5285:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 5286:                             } else {
 5287:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 5288:                             }
 5289:                         }
 5290:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5291:                     }
 5292:                 } elsif ($item eq 'recaptchakeys') {
 5293:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5294:                         my ($privkey,$pubkey);
 5295:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 5296:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 5297:                             $privkey = $loginhash{'login'}{$item}{'private'};
 5298:                         }
 5299:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 5300:                         if (!$pubkey) {
 5301:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 5302:                         } else {
 5303:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 5304:                         }
 5305:                         if (!$privkey) {
 5306:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 5307:                         } else {
 5308:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 5309:                         }
 5310:                         $chgtxt .= '</ul>';
 5311:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5312:                     }
 5313:                 } else {
 5314:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 5315:                 }
 5316:             }
 5317:             $resulttext .= $colchgtext.'</ul>';
 5318:         } else {
 5319:             $resulttext = &mt('No changes made to log-in page settings');
 5320:         }
 5321:     } else {
 5322:         $resulttext = '<span class="LC_error">'.
 5323: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5324:     }
 5325:     if ($errors) {
 5326:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 5327:                        $errors.'</ul>';
 5328:     }
 5329:     return $resulttext;
 5330: }
 5331: 
 5332: sub color_font_choices {
 5333:     my %choices =
 5334:         &Apache::lonlocal::texthash (
 5335:             img => "Header",
 5336:             bgs => "Background colors",
 5337:             links => "Link colors",
 5338:             images => "Images",
 5339:             font => "Font color",
 5340:             fontmenu => "Font menu",
 5341:             pgbg => "Page",
 5342:             tabbg => "Header",
 5343:             sidebg => "Border",
 5344:             link => "Link",
 5345:             alink => "Active link",
 5346:             vlink => "Visited link",
 5347:         );
 5348:     return %choices;
 5349: }
 5350: 
 5351: sub modify_rolecolors {
 5352:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 5353:     my ($resulttext,%rolehash);
 5354:     $rolehash{'rolecolors'} = {};
 5355:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 5356:         if ($domconfig{'rolecolors'} eq '') {
 5357:             $domconfig{'rolecolors'} = {};
 5358:         }
 5359:     }
 5360:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 5361:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 5362:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 5363:                                              $dom);
 5364:     if ($putresult eq 'ok') {
 5365:         if (keys(%changes) > 0) {
 5366:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5367:             if (ref($lastactref) eq 'HASH') {
 5368:                 $lastactref->{'domainconfig'} = 1;
 5369:             }
 5370:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 5371:                                              $rolehash{'rolecolors'});
 5372:         } else {
 5373:             $resulttext = &mt('No changes made to default color schemes');
 5374:         }
 5375:     } else {
 5376:         $resulttext = '<span class="LC_error">'.
 5377: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5378:     }
 5379:     if ($errors) {
 5380:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 5381:                        $errors.'</ul>';
 5382:     }
 5383:     return $resulttext;
 5384: }
 5385: 
 5386: sub modify_colors {
 5387:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 5388:     my (%changes,%choices);
 5389:     my @bgs;
 5390:     my @links = ('link','alink','vlink');
 5391:     my @logintext;
 5392:     my @images;
 5393:     my $servadm = $r->dir_config('lonAdmEMail');
 5394:     my $errors;
 5395:     my %defaults;
 5396:     foreach my $role (@{$roles}) {
 5397:         if ($role eq 'login') {
 5398:             %choices = &login_choices();
 5399:             @logintext = ('textcol','bgcol');
 5400:         } else {
 5401:             %choices = &color_font_choices();
 5402:         }
 5403:         if ($role eq 'login') {
 5404:             @images = ('img','logo','domlogo','login');
 5405:             @bgs = ('pgbg','mainbg','sidebg');
 5406:         } else {
 5407:             @images = ('img');
 5408:             @bgs = ('pgbg','tabbg','sidebg');
 5409:         }
 5410:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 5411:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 5412:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 5413:         }
 5414:         if ($role eq 'login') {
 5415:             foreach my $item (@logintext) {
 5416:                 unless ($env{'form.'.$role.'_'.$item} eq  $defaults{'logintext'}{$item}) {
 5417:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5418:                 }
 5419:             }
 5420:         } else {
 5421:             unless($env{'form.'.$role.'_fontmenu'} eq $defaults{'fontmenu'}) {
 5422:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 5423:             }
 5424:         }
 5425:         foreach my $item (@bgs) {
 5426:             unless ($env{'form.'.$role.'_'.$item} eq $defaults{'bgs'}{$item} ) {
 5427:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5428:             }
 5429:         }
 5430:         foreach my $item (@links) {
 5431:             unless ($env{'form.'.$role.'_'.$item} eq  $defaults{'links'}{$item}) {
 5432:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5433:             }
 5434:         }
 5435:         my ($configuserok,$author_ok,$switchserver) = 
 5436:             &config_check($dom,$confname,$servadm);
 5437:         my ($width,$height) = &thumb_dimensions();
 5438:         if (ref($domconfig->{$role}) ne 'HASH') {
 5439:             $domconfig->{$role} = {};
 5440:         }
 5441:         foreach my $img (@images) {
 5442:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 5443:                 if (defined($env{'form.login_showlogo_'.$img})) {
 5444:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 5445:                 } else { 
 5446:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 5447:                 }
 5448:             } 
 5449: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 5450: 		 && !defined($domconfig->{$role}{$img})
 5451: 		 && !$env{'form.'.$role.'_del_'.$img}
 5452: 		 && $env{'form.'.$role.'_import_'.$img}) {
 5453: 		# import the old configured image from the .tab setting
 5454: 		# if they haven't provided a new one 
 5455: 		$domconfig->{$role}{$img} = 
 5456: 		    $env{'form.'.$role.'_import_'.$img};
 5457: 	    }
 5458:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 5459:                 my $error;
 5460:                 if ($configuserok eq 'ok') {
 5461:                     if ($switchserver) {
 5462:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 5463:                     } else {
 5464:                         if ($author_ok eq 'ok') {
 5465:                             my ($result,$logourl) = 
 5466:                                 &publishlogo($r,'upload',$role.'_'.$img,
 5467:                                            $dom,$confname,$img,$width,$height);
 5468:                             if ($result eq 'ok') {
 5469:                                 $confhash->{$role}{$img} = $logourl;
 5470:                                 $changes{$role}{'images'}{$img} = 1;
 5471:                             } else {
 5472:                                 $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);
 5473:                             }
 5474:                         } else {
 5475:                             $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);
 5476:                         }
 5477:                     }
 5478:                 } else {
 5479:                     $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);
 5480:                 }
 5481:                 if ($error) {
 5482:                     &Apache::lonnet::logthis($error);
 5483:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5484:                 }
 5485:             } elsif ($domconfig->{$role}{$img} ne '') {
 5486:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 5487:                     my $error;
 5488:                     if ($configuserok eq 'ok') {
 5489: # is confname an author?
 5490:                         if ($switchserver eq '') {
 5491:                             if ($author_ok eq 'ok') {
 5492:                                 my ($result,$logourl) = 
 5493:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 5494:                                             $dom,$confname,$img,$width,$height);
 5495:                                 if ($result eq 'ok') {
 5496:                                     $confhash->{$role}{$img} = $logourl;
 5497: 				    $changes{$role}{'images'}{$img} = 1;
 5498:                                 }
 5499:                             }
 5500:                         }
 5501:                     }
 5502:                 }
 5503:             }
 5504:         }
 5505:         if (ref($domconfig) eq 'HASH') {
 5506:             if (ref($domconfig->{$role}) eq 'HASH') {
 5507:                 foreach my $img (@images) {
 5508:                     if ($domconfig->{$role}{$img} ne '') {
 5509:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5510:                             $confhash->{$role}{$img} = '';
 5511:                             $changes{$role}{'images'}{$img} = 1;
 5512:                         } else {
 5513:                             if ($confhash->{$role}{$img} eq '') {
 5514:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 5515:                             }
 5516:                         }
 5517:                     } else {
 5518:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5519:                             $confhash->{$role}{$img} = '';
 5520:                             $changes{$role}{'images'}{$img} = 1;
 5521:                         } 
 5522:                     }
 5523:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 5524:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 5525:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 5526:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 5527:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 5528:                             }
 5529:                         } else {
 5530:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5531:                                 $changes{$role}{'showlogo'}{$img} = 1;
 5532:                             }
 5533:                         }
 5534:                     }
 5535:                 }
 5536:                 if ($domconfig->{$role}{'font'} ne '') {
 5537:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 5538:                         $changes{$role}{'font'} = 1;
 5539:                     }
 5540:                 } else {
 5541:                     if ($confhash->{$role}{'font'}) {
 5542:                         $changes{$role}{'font'} = 1;
 5543:                     }
 5544:                 }
 5545:                 if ($role ne 'login') {
 5546:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 5547:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 5548:                             $changes{$role}{'fontmenu'} = 1;
 5549:                         }
 5550:                     } else {
 5551:                         if ($confhash->{$role}{'fontmenu'}) {
 5552:                             $changes{$role}{'fontmenu'} = 1;
 5553:                         }
 5554:                     }
 5555:                 }
 5556:                 foreach my $item (@bgs) {
 5557:                     if ($domconfig->{$role}{$item} ne '') {
 5558:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5559:                             $changes{$role}{'bgs'}{$item} = 1;
 5560:                         } 
 5561:                     } else {
 5562:                         if ($confhash->{$role}{$item}) {
 5563:                             $changes{$role}{'bgs'}{$item} = 1;
 5564:                         }
 5565:                     }
 5566:                 }
 5567:                 foreach my $item (@links) {
 5568:                     if ($domconfig->{$role}{$item} ne '') {
 5569:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5570:                             $changes{$role}{'links'}{$item} = 1;
 5571:                         }
 5572:                     } else {
 5573:                         if ($confhash->{$role}{$item}) {
 5574:                             $changes{$role}{'links'}{$item} = 1;
 5575:                         }
 5576:                     }
 5577:                 }
 5578:                 foreach my $item (@logintext) {
 5579:                     if ($domconfig->{$role}{$item} ne '') {
 5580:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5581:                             $changes{$role}{'logintext'}{$item} = 1;
 5582:                         }
 5583:                     } else {
 5584:                         if ($confhash->{$role}{$item}) {
 5585:                             $changes{$role}{'logintext'}{$item} = 1;
 5586:                         }
 5587:                     }
 5588:                 }
 5589:             } else {
 5590:                 &default_change_checker($role,\@images,\@links,\@bgs,
 5591:                                         \@logintext,$confhash,\%changes); 
 5592:             }
 5593:         } else {
 5594:             &default_change_checker($role,\@images,\@links,\@bgs,
 5595:                                     \@logintext,$confhash,\%changes); 
 5596:         }
 5597:     }
 5598:     return ($errors,%changes);
 5599: }
 5600: 
 5601: sub config_check {
 5602:     my ($dom,$confname,$servadm) = @_;
 5603:     my ($configuserok,$author_ok,$switchserver,%currroles);
 5604:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 5605:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 5606:                                                    $confname,$servadm);
 5607:     if ($configuserok eq 'ok') {
 5608:         $switchserver = &check_switchserver($dom,$confname);
 5609:         if ($switchserver eq '') {
 5610:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 5611:         }
 5612:     }
 5613:     return ($configuserok,$author_ok,$switchserver);
 5614: }
 5615: 
 5616: sub default_change_checker {
 5617:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 5618:     foreach my $item (@{$links}) {
 5619:         if ($confhash->{$role}{$item}) {
 5620:             $changes->{$role}{'links'}{$item} = 1;
 5621:         }
 5622:     }
 5623:     foreach my $item (@{$bgs}) {
 5624:         if ($confhash->{$role}{$item}) {
 5625:             $changes->{$role}{'bgs'}{$item} = 1;
 5626:         }
 5627:     }
 5628:     foreach my $item (@{$logintext}) {
 5629:         if ($confhash->{$role}{$item}) {
 5630:             $changes->{$role}{'logintext'}{$item} = 1;
 5631:         }
 5632:     }
 5633:     foreach my $img (@{$images}) {
 5634:         if ($env{'form.'.$role.'_del_'.$img}) {
 5635:             $confhash->{$role}{$img} = '';
 5636:             $changes->{$role}{'images'}{$img} = 1;
 5637:         }
 5638:         if ($role eq 'login') {
 5639:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5640:                 $changes->{$role}{'showlogo'}{$img} = 1;
 5641:             }
 5642:         }
 5643:     }
 5644:     if ($confhash->{$role}{'font'}) {
 5645:         $changes->{$role}{'font'} = 1;
 5646:     }
 5647: }
 5648: 
 5649: sub display_colorchgs {
 5650:     my ($dom,$changes,$roles,$confhash) = @_;
 5651:     my (%choices,$resulttext);
 5652:     if (!grep(/^login$/,@{$roles})) {
 5653:         $resulttext = &mt('Changes made:').'<br />';
 5654:     }
 5655:     foreach my $role (@{$roles}) {
 5656:         if ($role eq 'login') {
 5657:             %choices = &login_choices();
 5658:         } else {
 5659:             %choices = &color_font_choices();
 5660:         }
 5661:         if (ref($changes->{$role}) eq 'HASH') {
 5662:             if ($role ne 'login') {
 5663:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 5664:             }
 5665:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 5666:                 if ($role ne 'login') {
 5667:                     $resulttext .= '<ul>';
 5668:                 }
 5669:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 5670:                     if ($role ne 'login') {
 5671:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 5672:                     }
 5673:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 5674:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 5675:                             if ($confhash->{$role}{$key}{$item}) {
 5676:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 5677:                             } else {
 5678:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 5679:                             }
 5680:                         } elsif ($confhash->{$role}{$item} eq '') {
 5681:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 5682:                         } else {
 5683:                             my $newitem = $confhash->{$role}{$item};
 5684:                             if ($key eq 'images') {
 5685:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 5686:                             }
 5687:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 5688:                         }
 5689:                     }
 5690:                     if ($role ne 'login') {
 5691:                         $resulttext .= '</ul></li>';
 5692:                     }
 5693:                 } else {
 5694:                     if ($confhash->{$role}{$key} eq '') {
 5695:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 5696:                     } else {
 5697:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 5698:                     }
 5699:                 }
 5700:                 if ($role ne 'login') {
 5701:                     $resulttext .= '</ul>';
 5702:                 }
 5703:             }
 5704:         }
 5705:     }
 5706:     return $resulttext;
 5707: }
 5708: 
 5709: sub thumb_dimensions {
 5710:     return ('200','50');
 5711: }
 5712: 
 5713: sub check_dimensions {
 5714:     my ($inputfile) = @_;
 5715:     my ($fullwidth,$fullheight);
 5716:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 5717:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 5718:             my $imageinfo = <PIPE>;
 5719:             if (!close(PIPE)) {
 5720:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 5721:             }
 5722:             chomp($imageinfo);
 5723:             my ($fullsize) = 
 5724:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 5725:             if ($fullsize) {
 5726:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 5727:             }
 5728:         }
 5729:     }
 5730:     return ($fullwidth,$fullheight);
 5731: }
 5732: 
 5733: sub check_configuser {
 5734:     my ($uhome,$dom,$confname,$servadm) = @_;
 5735:     my ($configuserok,%currroles);
 5736:     if ($uhome eq 'no_host') {
 5737:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 5738:         my $configpass = &LONCAPA::Enrollment::create_password();
 5739:         $configuserok = 
 5740:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 5741:                              $configpass,'','','','','',undef,$servadm);
 5742:     } else {
 5743:         $configuserok = 'ok';
 5744:         %currroles = 
 5745:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 5746:     }
 5747:     return ($configuserok,%currroles);
 5748: }
 5749: 
 5750: sub check_authorstatus {
 5751:     my ($dom,$confname,%currroles) = @_;
 5752:     my $author_ok;
 5753:     if (!$currroles{':'.$dom.':au'}) {
 5754:         my $start = time;
 5755:         my $end = 0;
 5756:         $author_ok = 
 5757:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 5758:                                         'au',$end,$start,'','','domconfig');
 5759:     } else {
 5760:         $author_ok = 'ok';
 5761:     }
 5762:     return $author_ok;
 5763: }
 5764: 
 5765: sub publishlogo {
 5766:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 5767:     my ($output,$fname,$logourl);
 5768:     if ($action eq 'upload') {
 5769:         $fname=$env{'form.'.$formname.'.filename'};
 5770:         chop($env{'form.'.$formname});
 5771:     } else {
 5772:         ($fname) = ($formname =~ /([^\/]+)$/);
 5773:     }
 5774:     if ($savefileas ne '') {
 5775:         $fname = $savefileas;
 5776:     }
 5777:     $fname=&Apache::lonnet::clean_filename($fname);
 5778: # See if there is anything left
 5779:     unless ($fname) { return ('error: no uploaded file'); }
 5780:     $fname="$subdir/$fname";
 5781:     my $docroot=$r->dir_config('lonDocRoot');
 5782:     my $filepath="$docroot/priv";
 5783:     my $relpath = "$dom/$confname";
 5784:     my ($fnamepath,$file,$fetchthumb);
 5785:     $file=$fname;
 5786:     if ($fname=~m|/|) {
 5787:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 5788:     }
 5789:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 5790:     my $count;
 5791:     for ($count=5;$count<=$#parts;$count++) {
 5792:         $filepath.="/$parts[$count]";
 5793:         if ((-e $filepath)!=1) {
 5794:             mkdir($filepath,02770);
 5795:         }
 5796:     }
 5797:     # Check for bad extension and disallow upload
 5798:     if ($file=~/\.(\w+)$/ &&
 5799:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 5800:         $output = 
 5801:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 5802:     } elsif ($file=~/\.(\w+)$/ &&
 5803:         !defined(&Apache::loncommon::fileembstyle($1))) {
 5804:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 5805:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 5806:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 5807:     } elsif (-d "$filepath/$file") {
 5808:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 5809:     } else {
 5810:         my $source = $filepath.'/'.$file;
 5811:         my $logfile;
 5812:         if (!open($logfile,">>$source".'.log')) {
 5813:             return (&mt('No write permission to Authoring Space'));
 5814:         }
 5815:         print $logfile
 5816: "\n================= Publish ".localtime()." ================\n".
 5817: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 5818: # Save the file
 5819:         if (!open(FH,'>'.$source)) {
 5820:             &Apache::lonnet::logthis('Failed to create '.$source);
 5821:             return (&mt('Failed to create file'));
 5822:         }
 5823:         if ($action eq 'upload') {
 5824:             if (!print FH ($env{'form.'.$formname})) {
 5825:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 5826:                 return (&mt('Failed to write file'));
 5827:             }
 5828:         } else {
 5829:             my $original = &Apache::lonnet::filelocation('',$formname);
 5830:             if(!copy($original,$source)) {
 5831:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 5832:                 return (&mt('Failed to write file'));
 5833:             }
 5834:         }
 5835:         close(FH);
 5836:         chmod(0660, $source); # Permissions to rw-rw---.
 5837: 
 5838:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 5839:         my $copyfile=$targetdir.'/'.$file;
 5840: 
 5841:         my @parts=split(/\//,$targetdir);
 5842:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 5843:         for (my $count=5;$count<=$#parts;$count++) {
 5844:             $path.="/$parts[$count]";
 5845:             if (!-e $path) {
 5846:                 print $logfile "\nCreating directory ".$path;
 5847:                 mkdir($path,02770);
 5848:             }
 5849:         }
 5850:         my $versionresult;
 5851:         if (-e $copyfile) {
 5852:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 5853:         } else {
 5854:             $versionresult = 'ok';
 5855:         }
 5856:         if ($versionresult eq 'ok') {
 5857:             if (copy($source,$copyfile)) {
 5858:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 5859:                 $output = 'ok';
 5860:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 5861:                 push(@{$modified_urls},[$copyfile,$source]);
 5862:                 my $metaoutput = 
 5863:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 5864:                 unless ($registered_cleanup) {
 5865:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5866:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5867:                     $registered_cleanup=1;
 5868:                 }
 5869:             } else {
 5870:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 5871:                 $output = &mt('Failed to copy file to RES space').", $!";
 5872:             }
 5873:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 5874:                 my $inputfile = $filepath.'/'.$file;
 5875:                 my $outfile = $filepath.'/'.'tn-'.$file;
 5876:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 5877:                 if ($fullwidth ne '' && $fullheight ne '') { 
 5878:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 5879:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 5880:                         system("convert -sample $thumbsize $inputfile $outfile");
 5881:                         chmod(0660, $filepath.'/tn-'.$file);
 5882:                         if (-e $outfile) {
 5883:                             my $copyfile=$targetdir.'/tn-'.$file;
 5884:                             if (copy($outfile,$copyfile)) {
 5885:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 5886:                                 my $thumb_metaoutput = 
 5887:                                     &write_metadata($dom,$confname,$formname,
 5888:                                                     $targetdir,'tn-'.$file,$logfile);
 5889:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 5890:                                 unless ($registered_cleanup) {
 5891:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5892:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5893:                                     $registered_cleanup=1;
 5894:                                 }
 5895:                             } else {
 5896:                                 print $logfile "\nUnable to write ".$copyfile.
 5897:                                                ':'.$!."\n";
 5898:                             }
 5899:                         }
 5900:                     }
 5901:                 }
 5902:             }
 5903:         } else {
 5904:             $output = $versionresult;
 5905:         }
 5906:     }
 5907:     return ($output,$logourl);
 5908: }
 5909: 
 5910: sub logo_versioning {
 5911:     my ($targetdir,$file,$logfile) = @_;
 5912:     my $target = $targetdir.'/'.$file;
 5913:     my ($maxversion,$fn,$extn,$output);
 5914:     $maxversion = 0;
 5915:     if ($file =~ /^(.+)\.(\w+)$/) {
 5916:         $fn=$1;
 5917:         $extn=$2;
 5918:     }
 5919:     opendir(DIR,$targetdir);
 5920:     while (my $filename=readdir(DIR)) {
 5921:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 5922:             $maxversion=($1>$maxversion)?$1:$maxversion;
 5923:         }
 5924:     }
 5925:     $maxversion++;
 5926:     print $logfile "\nCreating old version ".$maxversion."\n";
 5927:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 5928:     if (copy($target,$copyfile)) {
 5929:         print $logfile "Copied old target to ".$copyfile."\n";
 5930:         $copyfile=$copyfile.'.meta';
 5931:         if (copy($target.'.meta',$copyfile)) {
 5932:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 5933:             $output = 'ok';
 5934:         } else {
 5935:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 5936:             $output = &mt('Failed to copy old meta').", $!, ";
 5937:         }
 5938:     } else {
 5939:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 5940:         $output = &mt('Failed to copy old target').", $!, ";
 5941:     }
 5942:     return $output;
 5943: }
 5944: 
 5945: sub write_metadata {
 5946:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 5947:     my (%metadatafields,%metadatakeys,$output);
 5948:     $metadatafields{'title'}=$formname;
 5949:     $metadatafields{'creationdate'}=time;
 5950:     $metadatafields{'lastrevisiondate'}=time;
 5951:     $metadatafields{'copyright'}='public';
 5952:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 5953:                                          $env{'user.domain'};
 5954:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 5955:     $metadatafields{'domain'}=$dom;
 5956:     {
 5957:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 5958:         my $mfh;
 5959:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 5960:             foreach (sort(keys(%metadatafields))) {
 5961:                 unless ($_=~/\./) {
 5962:                     my $unikey=$_;
 5963:                     $unikey=~/^([A-Za-z]+)/;
 5964:                     my $tag=$1;
 5965:                     $tag=~tr/A-Z/a-z/;
 5966:                     print $mfh "\n\<$tag";
 5967:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 5968:                         my $value=$metadatafields{$unikey.'.'.$_};
 5969:                         $value=~s/\"/\'\'/g;
 5970:                         print $mfh ' '.$_.'="'.$value.'"';
 5971:                     }
 5972:                     print $mfh '>'.
 5973:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 5974:                             .'</'.$tag.'>';
 5975:                 }
 5976:             }
 5977:             $output = 'ok';
 5978:             print $logfile "\nWrote metadata";
 5979:             close($mfh);
 5980:         } else {
 5981:             print $logfile "\nFailed to open metadata file";
 5982:             $output = &mt('Could not write metadata');
 5983:         }
 5984:     }
 5985:     return $output;
 5986: }
 5987: 
 5988: sub notifysubscribed {
 5989:     foreach my $targetsource (@{$modified_urls}){
 5990:         next unless (ref($targetsource) eq 'ARRAY');
 5991:         my ($target,$source)=@{$targetsource};
 5992:         if ($source ne '') {
 5993:             if (open(my $logfh,'>>'.$source.'.log')) {
 5994:                 print $logfh "\nCleanup phase: Notifications\n";
 5995:                 my @subscribed=&subscribed_hosts($target);
 5996:                 foreach my $subhost (@subscribed) {
 5997:                     print $logfh "\nNotifying host ".$subhost.':';
 5998:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 5999:                     print $logfh $reply;
 6000:                 }
 6001:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 6002:                 foreach my $subhost (@subscribedmeta) {
 6003:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 6004:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 6005:                                                         $subhost);
 6006:                     print $logfh $reply;
 6007:                 }
 6008:                 print $logfh "\n============ Done ============\n";
 6009:                 close($logfh);
 6010:             }
 6011:         }
 6012:     }
 6013:     return OK;
 6014: }
 6015: 
 6016: sub subscribed_hosts {
 6017:     my ($target) = @_;
 6018:     my @subscribed;
 6019:     if (open(my $fh,"<$target.subscription")) {
 6020:         while (my $subline=<$fh>) {
 6021:             if ($subline =~ /^($match_lonid):/) {
 6022:                 my $host = $1;
 6023:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 6024:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 6025:                         push(@subscribed,$host);
 6026:                     }
 6027:                 }
 6028:             }
 6029:         }
 6030:     }
 6031:     return @subscribed;
 6032: }
 6033: 
 6034: sub check_switchserver {
 6035:     my ($dom,$confname) = @_;
 6036:     my ($allowed,$switchserver);
 6037:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 6038:     if ($home eq 'no_host') {
 6039:         $home = &Apache::lonnet::domain($dom,'primary');
 6040:     }
 6041:     my @ids=&Apache::lonnet::current_machine_ids();
 6042:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 6043:     if (!$allowed) {
 6044: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 6045:     }
 6046:     return $switchserver;
 6047: }
 6048: 
 6049: sub modify_quotas {
 6050:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 6051:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 6052:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 6053:         $author_ok,$switchserver,$errors);
 6054:     if ($action eq 'quotas') {
 6055:         $context = 'tools'; 
 6056:     } else {
 6057:         $context = $action;
 6058:     }
 6059:     if ($context eq 'requestcourses') {
 6060:         @usertools = ('official','unofficial','community','textbook');
 6061:         @options =('norequest','approval','validate','autolimit');
 6062:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 6063:         %titles = &courserequest_titles();
 6064:         $toolregexp = join('|',@usertools);
 6065:         %conditions = &courserequest_conditions();
 6066:         $confname = $dom.'-domainconfig';
 6067:         my $servadm = $r->dir_config('lonAdmEMail');
 6068:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6069:     } elsif ($context eq 'requestauthor') {
 6070:         @usertools = ('author');
 6071:         %titles = &authorrequest_titles();
 6072:     } else {
 6073:         @usertools = ('aboutme','blog','webdav','portfolio');
 6074:         %titles = &tool_titles();
 6075:     }
 6076:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6077:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6078:     foreach my $key (keys(%env)) {
 6079:         if ($context eq 'requestcourses') {
 6080:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 6081:                 my $item = $1;
 6082:                 my $type = $2;
 6083:                 if ($type =~ /^limit_(.+)/) {
 6084:                     $limithash{$item}{$1} = $env{$key};
 6085:                 } else {
 6086:                     $confhash{$item}{$type} = $env{$key};
 6087:                 }
 6088:             }
 6089:         } elsif ($context eq 'requestauthor') {
 6090:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 6091:                 $confhash{$1} = $env{$key};
 6092:             }
 6093:         } else {
 6094:             if ($key =~ /^form\.quota_(.+)$/) {
 6095:                 $confhash{'defaultquota'}{$1} = $env{$key};
 6096:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 6097:                 $confhash{'authorquota'}{$1} = $env{$key};
 6098:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 6099:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 6100:             }
 6101:         }
 6102:     }
 6103:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6104:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.reqapprovalnotify');
 6105:         @approvalnotify = sort(@approvalnotify);
 6106:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 6107:         my @crstypes = ('official','unofficial','community','textbook');
 6108:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 6109:         foreach my $type (@hasuniquecode) {
 6110:             if (grep(/^\Q$type\E$/,@crstypes)) {
 6111:                 $confhash{'uniquecode'}{$type} = 1;
 6112:             }
 6113:         }
 6114:         my ($newbook,@allpos);
 6115:         if ($context eq 'requestcourses') {
 6116:             if ($env{'form.addbook'}) {
 6117:                 if (($env{'form.addbook_cnum'} =~ /^$match_courseid$/) &&
 6118:                     ($env{'form.addbook_cdom'} =~ /^$match_domain$/)) {
 6119:                     if (&Apache::lonnet::homeserver($env{'form.addbook_cnum'},
 6120:                                                     $env{'form.addbook_cdom'}) eq 'no_host') {
 6121:                         $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
 6122:                                    '</span></li>';
 6123:                     } else {
 6124:                         $newbook = $env{'form.addbook_cdom'}.'_'.$env{'form.addbook_cnum'};
 6125:                         my $position = $env{'form.addbook_pos'};
 6126:                         $position =~ s/\D+//g;
 6127:                         if ($position ne '') {
 6128:                             $allpos[$position] = $newbook;
 6129:                         }
 6130:                     }
 6131:                 } else {
 6132:                     $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
 6133:                                '</span></li>';
 6134:                 }
 6135:             }
 6136:         }
 6137:         if (ref($domconfig{$action}) eq 'HASH') {
 6138:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 6139:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 6140:                     $changes{'notify'}{'approval'} = 1;
 6141:                 }
 6142:             } else {
 6143:                 if ($confhash{'notify'}{'approval'}) {
 6144:                     $changes{'notify'}{'approval'} = 1;
 6145:                 }
 6146:             }
 6147:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 6148:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 6149:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 6150:                         unless ($confhash{'uniquecode'}{$crstype}) {
 6151:                             $changes{'uniquecode'} = 1;
 6152:                         }
 6153:                     }
 6154:                     unless ($changes{'uniquecode'}) {
 6155:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 6156:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 6157:                                 $changes{'uniquecode'} = 1;
 6158:                             }
 6159:                         }
 6160:                     }
 6161:                } else {
 6162:                    $changes{'uniquecode'} = 1;
 6163:                }
 6164:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 6165:                 $changes{'uniquecode'} = 1;
 6166:             }
 6167:             if ($context eq 'requestcourses') {
 6168:                 if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
 6169:                     my %deletions;
 6170:                     my @todelete = &Apache::loncommon::get_env_multiple('form.book_del');
 6171:                     if (@todelete) {
 6172:                         map { $deletions{$_} = 1; } @todelete;
 6173:                     }
 6174:                     my %imgdeletions;
 6175:                     my @todeleteimages = &Apache::loncommon::get_env_multiple('form.book_image_del');
 6176:                     if (@todeleteimages) {
 6177:                         map { $imgdeletions{$_} = 1; } @todeleteimages;
 6178:                     }
 6179:                     my $maxnum = $env{'form.book_maxnum'};
 6180:                     for (my $i=0; $i<=$maxnum; $i++) {
 6181:                         my $key = $env{'form.book_id_'.$i};
 6182:                         if (ref($domconfig{$action}{'textbooks'}{$key}) eq 'HASH') {
 6183:                             if ($deletions{$key}) {
 6184:                                 if ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
 6185:                                     #FIXME need to obsolete item in RES space
 6186:                                 }
 6187:                                 next;
 6188:                             } else {
 6189:                                 my $newpos = $env{'form.'.$key};
 6190:                                 $newpos =~ s/\D+//g;
 6191:                                 foreach my $item ('subject','title','author') {
 6192:                                     $confhash{'textbooks'}{$key}{$item} = $env{'form.book_'.$item.'_'.$i};
 6193:                                     if ($domconfig{$action}{'textbooks'}{$key}{$item} ne $confhash{'textbooks'}{$key}{$item}) {
 6194:                                         $changes{'textbooks'}{$key} = 1;
 6195:                                     }
 6196:                                 }
 6197:                                 $allpos[$newpos] = $key;
 6198:                             }
 6199:                             if ($imgdeletions{$key}) {
 6200:                                 $changes{'textbooks'}{$key} = 1;
 6201:                                 #FIXME need to obsolete item in RES space
 6202:                             } elsif ($env{'form.book_image_'.$i.'.filename'}) {
 6203:                                 my ($cdom,$cnum) = split(/_/,$key);
 6204:                                 my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,'book_image_'.$i,
 6205:                                                                               $cdom,$cnum,$configuserok,
 6206:                                                                               $switchserver,$author_ok);
 6207:                                 if ($imgurl) {
 6208:                                     $confhash{'textbooks'}{$key}{'image'} = $imgurl;
 6209:                                     $changes{'textbooks'}{$key} = 1; 
 6210:                                 }
 6211:                                 if ($error) {
 6212:                                     &Apache::lonnet::logthis($error);
 6213:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6214:                                 } 
 6215:                             } elsif ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
 6216:                                 $confhash{'textbooks'}{$key}{'image'} = 
 6217:                                     $domconfig{$action}{'textbooks'}{$key}{'image'};
 6218:                             }
 6219:                         }
 6220:                     }
 6221:                 }
 6222:             }
 6223:         } else {
 6224:             if ($confhash{'notify'}{'approval'}) {
 6225:                 $changes{'notify'}{'approval'} = 1;
 6226:             }
 6227:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 6228:                 $changes{'uniquecode'} = 1;
 6229:             }
 6230:         }
 6231:         if ($context eq 'requestcourses') {
 6232:             if ($newbook) {
 6233:                 $changes{'textbooks'}{$newbook} = 1;
 6234:                 foreach my $item ('subject','title','author') {
 6235:                     $env{'form.addbook_'.$item} =~ s/(`)/'/g;
 6236:                     if ($env{'form.addbook_'.$item}) {
 6237:                         $confhash{'textbooks'}{$newbook}{$item} = $env{'form.addbook_'.$item};
 6238:                     }
 6239:                 }
 6240:                 if ($env{'form.addbook_image.filename'} ne '') {
 6241:                     my ($cdom,$cnum) = split(/_/,$newbook);
 6242:                     my ($imageurl,$error) = 
 6243:                         &process_textbook_image($r,$dom,$confname,'addbook_image',$cdom,$cnum,$configuserok,
 6244:                                                 $switchserver,$author_ok);
 6245:                     if ($imageurl) {
 6246:                         $confhash{'textbooks'}{$newbook}{'image'} = $imageurl;
 6247:                     }
 6248:                     if ($error) {
 6249:                         &Apache::lonnet::logthis($error);
 6250:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6251:                     }
 6252:                 }
 6253:             }
 6254:             if (@allpos > 0) {
 6255:                 my $idx = 0;
 6256:                 foreach my $item (@allpos) {
 6257:                     if ($item ne '') {
 6258:                         $confhash{'textbooks'}{$item}{'order'} = $idx;
 6259:                         if (ref($domconfig{$action}) eq 'HASH') {
 6260:                             if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
 6261:                                 if (ref($domconfig{$action}{'textbooks'}{$item}) eq 'HASH') {
 6262:                                     if ($domconfig{$action}{'textbooks'}{$item}{'order'} ne $idx) {
 6263:                                         $changes{'textbooks'}{$item} = 1;
 6264:                                     }
 6265:                                 }
 6266:                             }
 6267:                         }
 6268:                         $idx ++;
 6269:                     }
 6270:                 }
 6271:             }
 6272:         }
 6273:     } else {
 6274:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 6275:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 6276:     }
 6277:     foreach my $item (@usertools) {
 6278:         foreach my $type (@{$types},'default','_LC_adv') {
 6279:             my $unset; 
 6280:             if ($context eq 'requestcourses') {
 6281:                 $unset = '0';
 6282:                 if ($type eq '_LC_adv') {
 6283:                     $unset = '';
 6284:                 }
 6285:                 if ($confhash{$item}{$type} eq 'autolimit') {
 6286:                     $confhash{$item}{$type} .= '=';
 6287:                     unless ($limithash{$item}{$type} =~ /\D/) {
 6288:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 6289:                     }
 6290:                 }
 6291:             } elsif ($context eq 'requestauthor') {
 6292:                 $unset = '0';
 6293:                 if ($type eq '_LC_adv') {
 6294:                     $unset = '';
 6295:                 }
 6296:             } else {
 6297:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 6298:                     $confhash{$item}{$type} = 1;
 6299:                 } else {
 6300:                     $confhash{$item}{$type} = 0;
 6301:                 }
 6302:             }
 6303:             if (ref($domconfig{$action}) eq 'HASH') {
 6304:                 if ($action eq 'requestauthor') {
 6305:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 6306:                         $changes{$type} = 1;
 6307:                     }
 6308:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 6309:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 6310:                         $changes{$item}{$type} = 1;
 6311:                     }
 6312:                 } else {
 6313:                     if ($context eq 'requestcourses') {
 6314:                         if ($confhash{$item}{$type} ne $unset) {
 6315:                             $changes{$item}{$type} = 1;
 6316:                         }
 6317:                     } else {
 6318:                         if (!$confhash{$item}{$type}) {
 6319:                             $changes{$item}{$type} = 1;
 6320:                         }
 6321:                     }
 6322:                 }
 6323:             } else {
 6324:                 if ($context eq 'requestcourses') {
 6325:                     if ($confhash{$item}{$type} ne $unset) {
 6326:                         $changes{$item}{$type} = 1;
 6327:                     }
 6328:                 } elsif ($context eq 'requestauthor') {
 6329:                     if ($confhash{$type} ne $unset) {
 6330:                         $changes{$type} = 1;
 6331:                     }
 6332:                 } else {
 6333:                     if (!$confhash{$item}{$type}) {
 6334:                         $changes{$item}{$type} = 1;
 6335:                     }
 6336:                 }
 6337:             }
 6338:         }
 6339:     }
 6340:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6341:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 6342:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 6343:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 6344:                     if (exists($confhash{'defaultquota'}{$key})) {
 6345:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 6346:                             $changes{'defaultquota'}{$key} = 1;
 6347:                         }
 6348:                     } else {
 6349:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 6350:                     }
 6351:                 }
 6352:             } else {
 6353:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 6354:                     if (exists($confhash{'defaultquota'}{$key})) {
 6355:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 6356:                             $changes{'defaultquota'}{$key} = 1;
 6357:                         }
 6358:                     } else {
 6359:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 6360:                     }
 6361:                 }
 6362:             }
 6363:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 6364:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 6365:                     if (exists($confhash{'authorquota'}{$key})) {
 6366:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 6367:                             $changes{'authorquota'}{$key} = 1;
 6368:                         }
 6369:                     } else {
 6370:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 6371:                     }
 6372:                 }
 6373:             }
 6374:         }
 6375:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 6376:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 6377:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 6378:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 6379:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 6380:                             $changes{'defaultquota'}{$key} = 1;
 6381:                         }
 6382:                     } else {
 6383:                         if (!exists($domconfig{'quotas'}{$key})) {
 6384:                             $changes{'defaultquota'}{$key} = 1;
 6385:                         }
 6386:                     }
 6387:                 } else {
 6388:                     $changes{'defaultquota'}{$key} = 1;
 6389:                 }
 6390:             }
 6391:         }
 6392:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 6393:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 6394:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 6395:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 6396:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 6397:                             $changes{'authorquota'}{$key} = 1;
 6398:                         }
 6399:                     } else {
 6400:                         $changes{'authorquota'}{$key} = 1;
 6401:                     }
 6402:                 } else {
 6403:                     $changes{'authorquota'}{$key} = 1;
 6404:                 }
 6405:             }
 6406:         }
 6407:     }
 6408: 
 6409:     if ($context eq 'requestauthor') {
 6410:         $domdefaults{'requestauthor'} = \%confhash;
 6411:     } else {
 6412:         foreach my $key (keys(%confhash)) {
 6413:             unless (($context eq 'requestcourses') && ($key eq 'textbooks')) {
 6414:                 $domdefaults{$key} = $confhash{$key};
 6415:             }
 6416:         }
 6417:     }
 6418: 
 6419:     my %quotahash = (
 6420:                       $action => { %confhash }
 6421:                     );
 6422:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 6423:                                              $dom);
 6424:     if ($putresult eq 'ok') {
 6425:         if (keys(%changes) > 0) {
 6426:             my $cachetime = 24*60*60;
 6427:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 6428:             if (ref($lastactref) eq 'HASH') {
 6429:                 $lastactref->{'domdefaults'} = 1;
 6430:             }
 6431:             $resulttext = &mt('Changes made:').'<ul>';
 6432:             unless (($context eq 'requestcourses') ||
 6433:                     ($context eq 'requestauthor')) {
 6434:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 6435:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 6436:                     foreach my $type (@{$types},'default') {
 6437:                         if (defined($changes{'defaultquota'}{$type})) {
 6438:                             my $typetitle = $usertypes->{$type};
 6439:                             if ($type eq 'default') {
 6440:                                 $typetitle = $othertitle;
 6441:                             }
 6442:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 6443:                         }
 6444:                     }
 6445:                     $resulttext .= '</ul></li>';
 6446:                 }
 6447:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 6448:                     $resulttext .= '<li>'.&mt('Authoring space default quotas').'<ul>';
 6449:                     foreach my $type (@{$types},'default') {
 6450:                         if (defined($changes{'authorquota'}{$type})) {
 6451:                             my $typetitle = $usertypes->{$type};
 6452:                             if ($type eq 'default') {
 6453:                                 $typetitle = $othertitle;
 6454:                             }
 6455:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 6456:                         }
 6457:                     }
 6458:                     $resulttext .= '</ul></li>';
 6459:                 }
 6460:             }
 6461:             my %newenv;
 6462:             foreach my $item (@usertools) {
 6463:                 my (%haschgs,%inconf);
 6464:                 if ($context eq 'requestauthor') {
 6465:                     %haschgs = %changes;
 6466:                     %inconf = %confhash;
 6467:                 } else {
 6468:                     if (ref($changes{$item}) eq 'HASH') {
 6469:                         %haschgs = %{$changes{$item}};
 6470:                     }
 6471:                     if (ref($confhash{$item}) eq 'HASH') {
 6472:                         %inconf = %{$confhash{$item}};
 6473:                     }
 6474:                 }
 6475:                 if (keys(%haschgs) > 0) {
 6476:                     my $newacc = 
 6477:                         &Apache::lonnet::usertools_access($env{'user.name'},
 6478:                                                           $env{'user.domain'},
 6479:                                                           $item,'reload',$context);
 6480:                     if (($context eq 'requestcourses') ||
 6481:                         ($context eq 'requestauthor')) {
 6482:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 6483:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 6484:                         }
 6485:                     } else {
 6486:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 6487:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 6488:                         }
 6489:                     }
 6490:                     unless ($context eq 'requestauthor') {
 6491:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 6492:                     }
 6493:                     foreach my $type (@{$types},'default','_LC_adv') {
 6494:                         if ($haschgs{$type}) {
 6495:                             my $typetitle = $usertypes->{$type};
 6496:                             if ($type eq 'default') {
 6497:                                 $typetitle = $othertitle;
 6498:                             } elsif ($type eq '_LC_adv') {
 6499:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 6500:                             }
 6501:                             if ($inconf{$type}) {
 6502:                                 if ($context eq 'requestcourses') {
 6503:                                     my $cond;
 6504:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 6505:                                         if ($1 eq '') {
 6506:                                             $cond = &mt('(Automatic processing of any request).');
 6507:                                         } else {
 6508:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 6509:                                         }
 6510:                                     } else { 
 6511:                                         $cond = $conditions{$inconf{$type}};
 6512:                                     }
 6513:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 6514:                                 } elsif ($context eq 'requestauthor') {
 6515:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 6516:                                                              $titles{$inconf{$type}},$typetitle);
 6517: 
 6518:                                 } else {
 6519:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 6520:                                 }
 6521:                             } else {
 6522:                                 if ($type eq '_LC_adv') {
 6523:                                     if ($inconf{$type} eq '0') {
 6524:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 6525:                                     } else { 
 6526:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 6527:                                     }
 6528:                                 } else {
 6529:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 6530:                                 }
 6531:                             }
 6532:                         }
 6533:                     }
 6534:                     unless ($context eq 'requestauthor') {
 6535:                         $resulttext .= '</ul></li>';
 6536:                     }
 6537:                 }
 6538:             }
 6539:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 6540:                 if (ref($changes{'notify'}) eq 'HASH') {
 6541:                     if ($changes{'notify'}{'approval'}) {
 6542:                         if (ref($confhash{'notify'}) eq 'HASH') {
 6543:                             if ($confhash{'notify'}{'approval'}) {
 6544:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 6545:                             } else {
 6546:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 6547:                             }
 6548:                         }
 6549:                     }
 6550:                 }
 6551:             }
 6552:             if ($action eq 'requestcourses') {
 6553:                 my @offon = ('off','on');
 6554:                 if ($changes{'uniquecode'}) {
 6555:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 6556:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 6557:                         $resulttext .= '<li>'.
 6558:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 6559:                                        '</li>';
 6560:                     } else {
 6561:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 6562:                                        '</li>';
 6563:                     }
 6564:                 }
 6565:                 if (ref($changes{'textbooks'}) eq 'HASH') {
 6566:                     $resulttext .= '<li>'.&mt('Available textbooks updated').'<ul>';
 6567:                     foreach my $key (sort(keys(%{$changes{'textbooks'}}))) {
 6568:                         my %coursehash = &Apache::lonnet::coursedescription($key);
 6569:                         my $coursetitle = $coursehash{'description'};
 6570:                         my $position = $confhash{'textbooks'}{$key}{'order'} + 1;
 6571:                         $resulttext .= '<li>';
 6572:                         foreach my $item ('subject','title','author') {
 6573:                             my $name = $item.':';
 6574:                             $name =~ s/^(\w)/\U$1/;
 6575:                             $resulttext .= &mt($name).' '.$confhash{'textbooks'}{$key}{$item}.'<br />';
 6576:                         }
 6577:                         $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 6578:                         if ($confhash{'textbooks'}{$key}{'image'}) {
 6579:                             $resulttext .= ' '.&mt('Image: [_1]',
 6580:                                                '<img src="'.$confhash{'textbooks'}{$key}{'image'}.'"'.
 6581:                                                ' alt="Textbook cover" />').'<br />';
 6582:                         }
 6583:                         $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 6584:                     }
 6585:                     $resulttext .= '</ul></li>';
 6586:                 }
 6587:             }
 6588:             $resulttext .= '</ul>';
 6589:             if (keys(%newenv)) {
 6590:                 &Apache::lonnet::appenv(\%newenv);
 6591:             }
 6592:         } else {
 6593:             if ($context eq 'requestcourses') {
 6594:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 6595:             } elsif ($context eq 'requestauthor') {
 6596:                 $resulttext = &mt('No changes made to rights to request author space.');
 6597:             } else {
 6598:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 6599:             }
 6600:         }
 6601:     } else {
 6602:         $resulttext = '<span class="LC_error">'.
 6603: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6604:     }
 6605:     if ($errors) {
 6606:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 6607:                        '<ul>'.$errors.'</ul></p>';
 6608:     }
 6609:     return $resulttext;
 6610: }
 6611: 
 6612: sub process_textbook_image {
 6613:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$configuserok,$switchserver,$author_ok) = @_;
 6614:     my $filename = $env{'form.'.$caller.'.filename'};
 6615:     my ($error,$url);
 6616:     my ($width,$height) = (50,50);
 6617:     if ($configuserok eq 'ok') {
 6618:         if ($switchserver) {
 6619:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 6620:                          $switchserver);
 6621:         } elsif ($author_ok eq 'ok') {
 6622:             my ($result,$imageurl) =
 6623:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 6624:                              "textbooks/$dom/$cnum/cover",$width,$height);
 6625:             if ($result eq 'ok') {
 6626:                 $url = $imageurl;
 6627:             } else {
 6628:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 6629:             }
 6630:         } else {
 6631:             $error = &mt("Upload of [_1] failed because an author role could not be assigned to a Domain Configuration user ([_2]) in domain: [_3].  Error was: [_4].",$filename,$confname,$dom,$author_ok);
 6632:         }
 6633:     } else {
 6634:         $error = &mt("Upload of [_1] failed because a Domain Configuration user ([_2]) could not be created in domain: [_3].  Error was: [_4].",$filename,$confname,$dom,$configuserok);
 6635:     }
 6636:     return ($url,$error);
 6637: }
 6638: 
 6639: sub modify_autoenroll {
 6640:     my ($dom,$lastactref,%domconfig) = @_;
 6641:     my ($resulttext,%changes);
 6642:     my %currautoenroll;
 6643:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 6644:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 6645:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 6646:         }
 6647:     }
 6648:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 6649:     my %title = ( run => 'Auto-enrollment active',
 6650:                   sender => 'Sender for notification messages',
 6651:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 6652:     my @offon = ('off','on');
 6653:     my $sender_uname = $env{'form.sender_uname'};
 6654:     my $sender_domain = $env{'form.sender_domain'};
 6655:     if ($sender_domain eq '') {
 6656:         $sender_uname = '';
 6657:     } elsif ($sender_uname eq '') {
 6658:         $sender_domain = '';
 6659:     }
 6660:     my $coowners = $env{'form.autoassign_coowners'};
 6661:     my %autoenrollhash =  (
 6662:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 6663:                                        'sender_uname' => $sender_uname,
 6664:                                        'sender_domain' => $sender_domain,
 6665:                                        'co-owners' => $coowners,
 6666:                                 }
 6667:                      );
 6668:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 6669:                                              $dom);
 6670:     if ($putresult eq 'ok') {
 6671:         if (exists($currautoenroll{'run'})) {
 6672:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 6673:                  $changes{'run'} = 1;
 6674:              }
 6675:         } elsif ($autorun) {
 6676:             if ($env{'form.autoenroll_run'} ne '1') {
 6677:                  $changes{'run'} = 1;
 6678:             }
 6679:         }
 6680:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 6681:             $changes{'sender'} = 1;
 6682:         }
 6683:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 6684:             $changes{'sender'} = 1;
 6685:         }
 6686:         if ($currautoenroll{'co-owners'} ne '') {
 6687:             if ($currautoenroll{'co-owners'} ne $coowners) {
 6688:                 $changes{'coowners'} = 1;
 6689:             }
 6690:         } elsif ($coowners) {
 6691:             $changes{'coowners'} = 1;
 6692:         }      
 6693:         if (keys(%changes) > 0) {
 6694:             $resulttext = &mt('Changes made:').'<ul>';
 6695:             if ($changes{'run'}) {
 6696:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 6697:             }
 6698:             if ($changes{'sender'}) {
 6699:                 if ($sender_uname eq '' || $sender_domain eq '') {
 6700:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 6701:                 } else {
 6702:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 6703:                 }
 6704:             }
 6705:             if ($changes{'coowners'}) {
 6706:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 6707:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 6708:                 if (ref($lastactref) eq 'HASH') {
 6709:                     $lastactref->{'domainconfig'} = 1;
 6710:                 }
 6711:             }
 6712:             $resulttext .= '</ul>';
 6713:         } else {
 6714:             $resulttext = &mt('No changes made to auto-enrollment settings');
 6715:         }
 6716:     } else {
 6717:         $resulttext = '<span class="LC_error">'.
 6718: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6719:     }
 6720:     return $resulttext;
 6721: }
 6722: 
 6723: sub modify_autoupdate {
 6724:     my ($dom,%domconfig) = @_;
 6725:     my ($resulttext,%currautoupdate,%fields,%changes);
 6726:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 6727:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 6728:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 6729:         }
 6730:     }
 6731:     my @offon = ('off','on');
 6732:     my %title = &Apache::lonlocal::texthash (
 6733:                    run => 'Auto-update:',
 6734:                    classlists => 'Updates to user information in classlists?'
 6735:                 );
 6736:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6737:     my %fieldtitles = &Apache::lonlocal::texthash (
 6738:                         id => 'Student/Employee ID',
 6739:                         permanentemail => 'E-mail address',
 6740:                         lastname => 'Last Name',
 6741:                         firstname => 'First Name',
 6742:                         middlename => 'Middle Name',
 6743:                         generation => 'Generation',
 6744:                       );
 6745:     $othertitle = &mt('All users');
 6746:     if (keys(%{$usertypes}) >  0) {
 6747:         $othertitle = &mt('Other users');
 6748:     }
 6749:     foreach my $key (keys(%env)) {
 6750:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 6751:             my ($usertype,$item) = ($1,$2);
 6752:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 6753:                 if ($usertype eq 'default') {   
 6754:                     push(@{$fields{$1}},$2);
 6755:                 } elsif (ref($types) eq 'ARRAY') {
 6756:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 6757:                         push(@{$fields{$1}},$2);
 6758:                     }
 6759:                 }
 6760:             }
 6761:         }
 6762:     }
 6763:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 6764:     @lockablenames = sort(@lockablenames);
 6765:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 6766:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6767:         if (@changed) {
 6768:             $changes{'lockablenames'} = 1;
 6769:         }
 6770:     } else {
 6771:         if (@lockablenames) {
 6772:             $changes{'lockablenames'} = 1;
 6773:         }
 6774:     }
 6775:     my %updatehash = (
 6776:                       autoupdate => { run => $env{'form.autoupdate_run'},
 6777:                                       classlists => $env{'form.classlists'},
 6778:                                       fields => {%fields},
 6779:                                       lockablenames => \@lockablenames,
 6780:                                     }
 6781:                      );
 6782:     foreach my $key (keys(%currautoupdate)) {
 6783:         if (($key eq 'run') || ($key eq 'classlists')) {
 6784:             if (exists($updatehash{autoupdate}{$key})) {
 6785:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 6786:                     $changes{$key} = 1;
 6787:                 }
 6788:             }
 6789:         } elsif ($key eq 'fields') {
 6790:             if (ref($currautoupdate{$key}) eq 'HASH') {
 6791:                 foreach my $item (@{$types},'default') {
 6792:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 6793:                         my $change = 0;
 6794:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 6795:                             if (!exists($fields{$item})) {
 6796:                                 $change = 1;
 6797:                                 last;
 6798:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 6799:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 6800:                                     $change = 1;
 6801:                                     last;
 6802:                                 }
 6803:                             }
 6804:                         }
 6805:                         if ($change) {
 6806:                             push(@{$changes{$key}},$item);
 6807:                         }
 6808:                     } 
 6809:                 }
 6810:             }
 6811:         } elsif ($key eq 'lockablenames') {
 6812:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 6813:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6814:                 if (@changed) {
 6815:                     $changes{'lockablenames'} = 1;
 6816:                 }
 6817:             } else {
 6818:                 if (@lockablenames) {
 6819:                     $changes{'lockablenames'} = 1;
 6820:                 }
 6821:             }
 6822:         }
 6823:     }
 6824:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 6825:         if (@lockablenames) {
 6826:             $changes{'lockablenames'} = 1;
 6827:         }
 6828:     }
 6829:     foreach my $item (@{$types},'default') {
 6830:         if (defined($fields{$item})) {
 6831:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 6832:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 6833:                     my $change = 0;
 6834:                     if (ref($fields{$item}) eq 'ARRAY') {
 6835:                         foreach my $type (@{$fields{$item}}) {
 6836:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 6837:                                 $change = 1;
 6838:                                 last;
 6839:                             }
 6840:                         }
 6841:                     }
 6842:                     if ($change) {
 6843:                         push(@{$changes{'fields'}},$item);
 6844:                     }
 6845:                 } else {
 6846:                     push(@{$changes{'fields'}},$item);
 6847:                 }
 6848:             } else {
 6849:                 push(@{$changes{'fields'}},$item);
 6850:             }
 6851:         }
 6852:     }
 6853:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 6854:                                              $dom);
 6855:     if ($putresult eq 'ok') {
 6856:         if (keys(%changes) > 0) {
 6857:             $resulttext = &mt('Changes made:').'<ul>';
 6858:             foreach my $key (sort(keys(%changes))) {
 6859:                 if ($key eq 'lockablenames') {
 6860:                     $resulttext .= '<li>';
 6861:                     if (@lockablenames) {
 6862:                         $usertypes->{'default'} = $othertitle;
 6863:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 6864:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 6865:                     } else {
 6866:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 6867:                     }
 6868:                     $resulttext .= '</li>';
 6869:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 6870:                     foreach my $item (@{$changes{$key}}) {
 6871:                         my @newvalues;
 6872:                         foreach my $type (@{$fields{$item}}) {
 6873:                             push(@newvalues,$fieldtitles{$type});
 6874:                         }
 6875:                         my $newvaluestr;
 6876:                         if (@newvalues > 0) {
 6877:                             $newvaluestr = join(', ',@newvalues);
 6878:                         } else {
 6879:                             $newvaluestr = &mt('none');
 6880:                         }
 6881:                         if ($item eq 'default') {
 6882:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 6883:                         } else {
 6884:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 6885:                         }
 6886:                     }
 6887:                 } else {
 6888:                     my $newvalue;
 6889:                     if ($key eq 'run') {
 6890:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 6891:                     } else {
 6892:                         $newvalue = $offon[$env{'form.'.$key}];
 6893:                     }
 6894:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 6895:                 }
 6896:             }
 6897:             $resulttext .= '</ul>';
 6898:         } else {
 6899:             $resulttext = &mt('No changes made to autoupdates');
 6900:         }
 6901:     } else {
 6902:         $resulttext = '<span class="LC_error">'.
 6903: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6904:     }
 6905:     return $resulttext;
 6906: }
 6907: 
 6908: sub modify_autocreate {
 6909:     my ($dom,%domconfig) = @_;
 6910:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 6911:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 6912:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 6913:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 6914:         }
 6915:     }
 6916:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 6917:                  req => 'Auto-creation of validated requests for official courses',
 6918:                  xmldc => 'Identity of course creator of courses from XML files',
 6919:                );
 6920:     my @types = ('xml','req');
 6921:     foreach my $item (@types) {
 6922:         $newvals{$item} = $env{'form.autocreate_'.$item};
 6923:         $newvals{$item} =~ s/\D//g;
 6924:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 6925:     }
 6926:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 6927:     my %domcoords = &get_active_dcs($dom);
 6928:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 6929:         $newvals{'xmldc'} = '';
 6930:     } 
 6931:     %autocreatehash =  (
 6932:                         autocreate => { xml => $newvals{'xml'},
 6933:                                         req => $newvals{'req'},
 6934:                                       }
 6935:                        );
 6936:     if ($newvals{'xmldc'} ne '') {
 6937:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 6938:     }
 6939:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 6940:                                              $dom);
 6941:     if ($putresult eq 'ok') {
 6942:         my @items = @types;
 6943:         if ($newvals{'xml'}) {
 6944:             push(@items,'xmldc');
 6945:         }
 6946:         foreach my $item (@items) {
 6947:             if (exists($currautocreate{$item})) {
 6948:                 if ($currautocreate{$item} ne $newvals{$item}) {
 6949:                     $changes{$item} = 1;
 6950:                 }
 6951:             } elsif ($newvals{$item}) {
 6952:                 $changes{$item} = 1;
 6953:             }
 6954:         }
 6955:         if (keys(%changes) > 0) {
 6956:             my @offon = ('off','on'); 
 6957:             $resulttext = &mt('Changes made:').'<ul>';
 6958:             foreach my $item (@types) {
 6959:                 if ($changes{$item}) {
 6960:                     my $newtxt = $offon[$newvals{$item}];
 6961:                     $resulttext .= '<li>'.
 6962:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 6963:                                        '<b>','</b>').
 6964:                                    '</li>';
 6965:                 }
 6966:             }
 6967:             if ($changes{'xmldc'}) {
 6968:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 6969:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 6970:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 6971:             }
 6972:             $resulttext .= '</ul>';
 6973:         } else {
 6974:             $resulttext = &mt('No changes made to auto-creation settings');
 6975:         }
 6976:     } else {
 6977:         $resulttext = '<span class="LC_error">'.
 6978:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6979:     }
 6980:     return $resulttext;
 6981: }
 6982: 
 6983: sub modify_directorysrch {
 6984:     my ($dom,%domconfig) = @_;
 6985:     my ($resulttext,%changes);
 6986:     my %currdirsrch;
 6987:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 6988:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 6989:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 6990:         }
 6991:     }
 6992:     my %title = ( available => 'Directory search available',
 6993:                   localonly => 'Other domains can search',
 6994:                   searchby => 'Search types',
 6995:                   searchtypes => 'Search latitude');
 6996:     my @offon = ('off','on');
 6997:     my @otherdoms = ('Yes','No');
 6998: 
 6999:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 7000:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 7001:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 7002: 
 7003:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7004:     if (keys(%{$usertypes}) == 0) {
 7005:         @cansearch = ('default');
 7006:     } else {
 7007:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 7008:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 7009:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 7010:                     push(@{$changes{'cansearch'}},$type);
 7011:                 }
 7012:             }
 7013:             foreach my $type (@cansearch) {
 7014:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 7015:                     push(@{$changes{'cansearch'}},$type);
 7016:                 }
 7017:             }
 7018:         } else {
 7019:             push(@{$changes{'cansearch'}},@cansearch);
 7020:         }
 7021:     }
 7022: 
 7023:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 7024:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 7025:             if (!grep(/^\Q$by\E$/,@searchby)) {
 7026:                 push(@{$changes{'searchby'}},$by);
 7027:             }
 7028:         }
 7029:         foreach my $by (@searchby) {
 7030:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 7031:                 push(@{$changes{'searchby'}},$by);
 7032:             }
 7033:         }
 7034:     } else {
 7035:         push(@{$changes{'searchby'}},@searchby);
 7036:     }
 7037: 
 7038:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 7039:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 7040:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 7041:                 push(@{$changes{'searchtypes'}},$type);
 7042:             }
 7043:         }
 7044:         foreach my $type (@searchtypes) {
 7045:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 7046:                 push(@{$changes{'searchtypes'}},$type);
 7047:             }
 7048:         }
 7049:     } else {
 7050:         if (exists($currdirsrch{'searchtypes'})) {
 7051:             foreach my $type (@searchtypes) {  
 7052:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 7053:                     push(@{$changes{'searchtypes'}},$type);
 7054:                 }
 7055:             }
 7056:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 7057:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 7058:             }   
 7059:         } else {
 7060:             push(@{$changes{'searchtypes'}},@searchtypes); 
 7061:         }
 7062:     }
 7063: 
 7064:     my %dirsrch_hash =  (
 7065:             directorysrch => { available => $env{'form.dirsrch_available'},
 7066:                                cansearch => \@cansearch,
 7067:                                localonly => $env{'form.dirsrch_localonly'},
 7068:                                searchby => \@searchby,
 7069:                                searchtypes => \@searchtypes,
 7070:                              }
 7071:             );
 7072:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 7073:                                              $dom);
 7074:     if ($putresult eq 'ok') {
 7075:         if (exists($currdirsrch{'available'})) {
 7076:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 7077:                  $changes{'available'} = 1;
 7078:              }
 7079:         } else {
 7080:             if ($env{'form.dirsrch_available'} eq '1') {
 7081:                 $changes{'available'} = 1;
 7082:             }
 7083:         }
 7084:         if (exists($currdirsrch{'localonly'})) {
 7085:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 7086:                  $changes{'localonly'} = 1;
 7087:              }
 7088:         } else {
 7089:             if ($env{'form.dirsrch_localonly'} eq '1') {
 7090:                 $changes{'localonly'} = 1;
 7091:             }
 7092:         }
 7093:         if (keys(%changes) > 0) {
 7094:             $resulttext = &mt('Changes made:').'<ul>';
 7095:             if ($changes{'available'}) {
 7096:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 7097:             }
 7098:             if ($changes{'localonly'}) {
 7099:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 7100:             }
 7101: 
 7102:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 7103:                 my $chgtext;
 7104:                 if (ref($usertypes) eq 'HASH') {
 7105:                     if (keys(%{$usertypes}) > 0) {
 7106:                         foreach my $type (@{$types}) {
 7107:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 7108:                                 $chgtext .= $usertypes->{$type}.'; ';
 7109:                             }
 7110:                         }
 7111:                         if (grep(/^default$/,@cansearch)) {
 7112:                             $chgtext .= $othertitle;
 7113:                         } else {
 7114:                             $chgtext =~ s/\; $//;
 7115:                         }
 7116:                         $resulttext .=
 7117:                             '<li>'.
 7118:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 7119:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 7120:                             '</li>';
 7121:                     }
 7122:                 }
 7123:             }
 7124:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 7125:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 7126:                 my $chgtext;
 7127:                 foreach my $type (@{$titleorder}) {
 7128:                     if (grep(/^\Q$type\E$/,@searchby)) {
 7129:                         if (defined($searchtitles->{$type})) {
 7130:                             $chgtext .= $searchtitles->{$type}.'; ';
 7131:                         }
 7132:                     }
 7133:                 }
 7134:                 $chgtext =~ s/\; $//;
 7135:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 7136:             }
 7137:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 7138:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 7139:                 my $chgtext;
 7140:                 foreach my $type (@{$srchtypeorder}) {
 7141:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 7142:                         if (defined($srchtypes_desc->{$type})) {
 7143:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 7144:                         }
 7145:                     }
 7146:                 }
 7147:                 $chgtext =~ s/\; $//;
 7148:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 7149:             }
 7150:             $resulttext .= '</ul>';
 7151:         } else {
 7152:             $resulttext = &mt('No changes made to institution directory search settings');
 7153:         }
 7154:     } else {
 7155:         $resulttext = '<span class="LC_error">'.
 7156:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 7157:     }
 7158:     return $resulttext;
 7159: }
 7160: 
 7161: sub modify_contacts {
 7162:     my ($dom,$lastactref,%domconfig) = @_;
 7163:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 7164:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 7165:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 7166:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 7167:         }
 7168:     }
 7169:     my (%others,%to,%bcc);
 7170:     my @contacts = ('supportemail','adminemail');
 7171:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 7172:                     'requestsmail','updatesmail','idconflictsmail');
 7173:     my @toggles = ('reporterrors','reportupdates');
 7174:     foreach my $type (@mailings) {
 7175:         @{$newsetting{$type}} = 
 7176:             &Apache::loncommon::get_env_multiple('form.'.$type);
 7177:         foreach my $item (@contacts) {
 7178:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 7179:                 $contacts_hash{contacts}{$type}{$item} = 1;
 7180:             } else {
 7181:                 $contacts_hash{contacts}{$type}{$item} = 0;
 7182:             }
 7183:         }  
 7184:         $others{$type} = $env{'form.'.$type.'_others'};
 7185:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 7186:         if ($type eq 'helpdeskmail') {
 7187:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 7188:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 7189:         }
 7190:     }
 7191:     foreach my $item (@contacts) {
 7192:         $to{$item} = $env{'form.'.$item};
 7193:         $contacts_hash{'contacts'}{$item} = $to{$item};
 7194:     }
 7195:     foreach my $item (@toggles) {
 7196:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 7197:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 7198:         }
 7199:     }
 7200:     if (keys(%currsetting) > 0) {
 7201:         foreach my $item (@contacts) {
 7202:             if ($to{$item} ne $currsetting{$item}) {
 7203:                 $changes{$item} = 1;
 7204:             }
 7205:         }
 7206:         foreach my $type (@mailings) {
 7207:             foreach my $item (@contacts) {
 7208:                 if (ref($currsetting{$type}) eq 'HASH') {
 7209:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 7210:                         push(@{$changes{$type}},$item);
 7211:                     }
 7212:                 } else {
 7213:                     push(@{$changes{$type}},@{$newsetting{$type}});
 7214:                 }
 7215:             }
 7216:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 7217:                 push(@{$changes{$type}},'others');
 7218:             }
 7219:             if ($type eq 'helpdeskmail') {   
 7220:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 7221:                     push(@{$changes{$type}},'bcc'); 
 7222:                 }
 7223:             }
 7224:         }
 7225:     } else {
 7226:         my %default;
 7227:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 7228:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 7229:         $default{'errormail'} = 'adminemail';
 7230:         $default{'packagesmail'} = 'adminemail';
 7231:         $default{'helpdeskmail'} = 'supportemail';
 7232:         $default{'lonstatusmail'} = 'adminemail';
 7233:         $default{'requestsmail'} = 'adminemail';
 7234:         $default{'updatesmail'} = 'adminemail';
 7235:         foreach my $item (@contacts) {
 7236:            if ($to{$item} ne $default{$item}) {
 7237:               $changes{$item} = 1;
 7238:            }
 7239:         }
 7240:         foreach my $type (@mailings) {
 7241:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 7242:                
 7243:                 push(@{$changes{$type}},@{$newsetting{$type}});
 7244:             }
 7245:             if ($others{$type} ne '') {
 7246:                 push(@{$changes{$type}},'others');
 7247:             }
 7248:             if ($type eq 'helpdeskmail') {
 7249:                 if ($bcc{$type} ne '') {
 7250:                     push(@{$changes{$type}},'bcc');
 7251:                 }
 7252:             }
 7253:         }
 7254:     }
 7255:     foreach my $item (@toggles) {
 7256:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 7257:             $changes{$item} = 1;
 7258:         } elsif ((!$env{'form.'.$item}) &&
 7259:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 7260:             $changes{$item} = 1;
 7261:         }
 7262:     }
 7263:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 7264:                                              $dom);
 7265:     if ($putresult eq 'ok') {
 7266:         if (keys(%changes) > 0) {
 7267:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7268:             if (ref($lastactref) eq 'HASH') {
 7269:                 $lastactref->{'domainconfig'} = 1;
 7270:             }
 7271:             my ($titles,$short_titles)  = &contact_titles();
 7272:             $resulttext = &mt('Changes made:').'<ul>';
 7273:             foreach my $item (@contacts) {
 7274:                 if ($changes{$item}) {
 7275:                     $resulttext .= '<li>'.$titles->{$item}.
 7276:                                     &mt(' set to: ').
 7277:                                     '<span class="LC_cusr_emph">'.
 7278:                                     $to{$item}.'</span></li>';
 7279:                 }
 7280:             }
 7281:             foreach my $type (@mailings) {
 7282:                 if (ref($changes{$type}) eq 'ARRAY') {
 7283:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 7284:                     my @text;
 7285:                     foreach my $item (@{$newsetting{$type}}) {
 7286:                         push(@text,$short_titles->{$item});
 7287:                     }
 7288:                     if ($others{$type} ne '') {
 7289:                         push(@text,$others{$type});
 7290:                     }
 7291:                     $resulttext .= '<span class="LC_cusr_emph">'.
 7292:                                    join(', ',@text).'</span>';
 7293:                     if ($type eq 'helpdeskmail') {
 7294:                         if ($bcc{$type} ne '') {
 7295:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 7296:                         }
 7297:                     }
 7298:                     $resulttext .= '</li>';
 7299:                 }
 7300:             }
 7301:             my @offon = ('off','on');
 7302:             if ($changes{'reporterrors'}) {
 7303:                 $resulttext .= '<li>'.
 7304:                                &mt('E-mail error reports to [_1] set to "'.
 7305:                                    $offon[$env{'form.reporterrors'}].'".',
 7306:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 7307:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 7308:                                '</li>';
 7309:             }
 7310:             if ($changes{'reportupdates'}) {
 7311:                 $resulttext .= '<li>'.
 7312:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 7313:                                     $offon[$env{'form.reportupdates'}].'".',
 7314:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 7315:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 7316:                                 '</li>';
 7317:             }
 7318:             $resulttext .= '</ul>';
 7319:         } else {
 7320:             $resulttext = &mt('No changes made to contact information');
 7321:         }
 7322:     } else {
 7323:         $resulttext = '<span class="LC_error">'.
 7324:             &mt('An error occurred: [_1].',$putresult).'</span>';
 7325:     }
 7326:     return $resulttext;
 7327: }
 7328: 
 7329: sub modify_usercreation {
 7330:     my ($dom,%domconfig) = @_;
 7331:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate);
 7332:     my $warningmsg;
 7333:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 7334:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 7335:             $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 7336:         }
 7337:     }
 7338:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 7339:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 7340:     my @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 7341:     my @contexts = ('author','course','requestcrs','selfcreate');
 7342:     foreach my $item(@contexts) {
 7343:         if ($item eq 'selfcreate') {
 7344:             @{$cancreate{$item}} = &Apache::loncommon::get_env_multiple('form.can_createuser_'.$item);
 7345:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 7346:             if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth'))) {
 7347:                 if (ref($cancreate{$item}) eq 'ARRAY') { 
 7348:                     if (grep(/^login$/,@{$cancreate{$item}})) {
 7349:                         $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.');   
 7350:                     }
 7351:                 }
 7352:             }
 7353:         } else {
 7354:             $cancreate{$item} = $env{'form.can_createuser_'.$item};
 7355:         }
 7356:     }
 7357:     my ($othertitle,$usertypes,$types) = 
 7358:         &Apache::loncommon::sorted_inst_types($dom);
 7359:     if (ref($types) eq 'ARRAY') {
 7360:         if (@{$types} > 0) {
 7361:             @{$cancreate{'statustocreate'}} = 
 7362:                 &Apache::loncommon::get_env_multiple('form.statustocreate');
 7363:         } else {
 7364:             @{$cancreate{'statustocreate'}} = ();
 7365:         }
 7366:         push(@contexts,'statustocreate');
 7367:     }
 7368:     &process_captcha('cancreate',\%changes,\%cancreate,\%curr_usercreation);
 7369:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 7370:         foreach my $item (@contexts) {
 7371:             if (($item eq 'selfcreate') || ($item eq 'statustocreate')) {
 7372:                 if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 7373:                     foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 7374:                         if (ref($cancreate{$item}) eq 'ARRAY') {
 7375:                             if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 7376:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7377:                                     push(@{$changes{'cancreate'}},$item);
 7378:                                 }
 7379:                             }
 7380:                         }
 7381:                     }
 7382:                 } else {
 7383:                     if ($curr_usercreation{'cancreate'}{$item} eq '') {
 7384:                         if (@{$cancreate{$item}} > 0) {
 7385:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7386:                                 push(@{$changes{'cancreate'}},$item);
 7387:                             }
 7388:                         }
 7389:                     } else {
 7390:                         if ($curr_usercreation{'cancreate'}{$item} eq 'any') {
 7391:                             if (@{$cancreate{$item}} < 3) {
 7392:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7393:                                     push(@{$changes{'cancreate'}},$item);
 7394:                                 }
 7395:                             }
 7396:                         } elsif ($curr_usercreation{'cancreate'}{$item} eq 'none') {
 7397:                             if (@{$cancreate{$item}} > 0) {
 7398:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7399:                                     push(@{$changes{'cancreate'}},$item);
 7400:                                 }
 7401:                             }
 7402:                         } elsif (!grep(/^$curr_usercreation{'cancreate'}{$item}$/,@{$cancreate{$item}})) {
 7403:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7404:                                 push(@{$changes{'cancreate'}},$item);
 7405:                             }
 7406:                         }
 7407:                     }
 7408:                 }
 7409:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7410:                     foreach my $type (@{$cancreate{$item}}) {
 7411:                         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 7412:                             if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 7413:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7414:                                     push(@{$changes{'cancreate'}},$item);
 7415:                                 }
 7416:                             }
 7417:                         } elsif (($curr_usercreation{'cancreate'}{$item} ne 'any') &&
 7418:                                  ($curr_usercreation{'cancreate'}{$item} ne 'none')) {
 7419:                             if ($curr_usercreation{'cancreate'}{$item} ne $type) {
 7420:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7421:                                     push(@{$changes{'cancreate'}},$item);
 7422:                                 }
 7423:                             }
 7424:                         }
 7425:                     }
 7426:                 }
 7427:             } else {
 7428:                 if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 7429:                     push(@{$changes{'cancreate'}},$item);
 7430:                 }
 7431:             }
 7432:         }
 7433:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 7434:         foreach my $item (@contexts) {
 7435:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 7436:                 if ($cancreate{$item} ne 'any') {
 7437:                     push(@{$changes{'cancreate'}},$item);
 7438:                 }
 7439:             } else {
 7440:                 if ($cancreate{$item} ne 'none') {
 7441:                     push(@{$changes{'cancreate'}},$item);
 7442:                 }
 7443:             }
 7444:         }
 7445:     } else {
 7446:         foreach my $item (@contexts)  {
 7447:             push(@{$changes{'cancreate'}},$item);
 7448:         }
 7449:     }
 7450: 
 7451:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 7452:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 7453:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 7454:                 push(@{$changes{'username_rule'}},$type);
 7455:             }
 7456:         }
 7457:         foreach my $type (@username_rule) {
 7458:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 7459:                 push(@{$changes{'username_rule'}},$type);
 7460:             }
 7461:         }
 7462:     } else {
 7463:         push(@{$changes{'username_rule'}},@username_rule);
 7464:     }
 7465: 
 7466:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 7467:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 7468:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 7469:                 push(@{$changes{'id_rule'}},$type);
 7470:             }
 7471:         }
 7472:         foreach my $type (@id_rule) {
 7473:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 7474:                 push(@{$changes{'id_rule'}},$type);
 7475:             }
 7476:         }
 7477:     } else {
 7478:         push(@{$changes{'id_rule'}},@id_rule);
 7479:     }
 7480: 
 7481:     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 7482:         foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 7483:             if (!grep(/^\Q$type\E$/,@email_rule)) {
 7484:                 push(@{$changes{'email_rule'}},$type);
 7485:             }
 7486:         }
 7487:         foreach my $type (@email_rule) {
 7488:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 7489:                 push(@{$changes{'email_rule'}},$type);
 7490:             }
 7491:         }
 7492:     } else {
 7493:         push(@{$changes{'email_rule'}},@email_rule);
 7494:     }
 7495: 
 7496:     my @authen_contexts = ('author','course','domain');
 7497:     my @authtypes = ('int','krb4','krb5','loc');
 7498:     my %authhash;
 7499:     foreach my $item (@authen_contexts) {
 7500:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 7501:         foreach my $auth (@authtypes) {
 7502:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 7503:                 $authhash{$item}{$auth} = 1;
 7504:             } else {
 7505:                 $authhash{$item}{$auth} = 0;
 7506:             }
 7507:         }
 7508:     }
 7509:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 7510:         foreach my $item (@authen_contexts) {
 7511:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 7512:                 foreach my $auth (@authtypes) {
 7513:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 7514:                         push(@{$changes{'authtypes'}},$item);
 7515:                         last;
 7516:                     }
 7517:                 }
 7518:             }
 7519:         }
 7520:     } else {
 7521:         foreach my $item (@authen_contexts) {
 7522:             push(@{$changes{'authtypes'}},$item);
 7523:         }
 7524:     }
 7525: 
 7526:     my %usercreation_hash =  (
 7527:             usercreation => {
 7528:                               cancreate     => \%cancreate,
 7529:                               username_rule => \@username_rule,
 7530:                               id_rule       => \@id_rule,
 7531:                               email_rule    => \@email_rule,
 7532:                               authtypes     => \%authhash,
 7533:                             }
 7534:             );
 7535: 
 7536:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 7537:                                              $dom);
 7538: 
 7539:     my %selfcreatetypes = (
 7540:                              sso   => 'users authenticated by institutional single sign on',
 7541:                              login => 'users authenticated by institutional log-in',
 7542:                              email => 'users who provide a valid e-mail address for use as the username',
 7543:                           );
 7544:     if ($putresult eq 'ok') {
 7545:         if (keys(%changes) > 0) {
 7546:             $resulttext = &mt('Changes made:').'<ul>';
 7547:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 7548:                 my %lt = &usercreation_types();
 7549:                 foreach my $type (@{$changes{'cancreate'}}) {
 7550:                     my $chgtext;
 7551:                     unless (($type eq 'statustocreate') || ($type eq 'captcha') || ($type eq 'recaptchakeys')) {
 7552:                         $chgtext = $lt{$type}.', ';
 7553:                     }
 7554:                     if ($type eq 'selfcreate') {
 7555:                         if (@{$cancreate{$type}} == 0) {
 7556:                             $chgtext .= &mt('creation of a new user account is not permitted.');
 7557:                         } else {
 7558:                             $chgtext .= &mt('creation of a new account is permitted for:').'<ul>';
 7559:                             foreach my $case (@{$cancreate{$type}}) {
 7560:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 7561:                             }
 7562:                             $chgtext .= '</ul>';
 7563:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 7564:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 7565:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 7566:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 7567:                                             $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 7568:                                         }
 7569:                                     }
 7570:                                 }
 7571:                             }
 7572:                         }
 7573:                     } elsif ($type eq 'statustocreate') {
 7574:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 7575:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 7576:                             if (@{$cancreate{'selfcreate'}} > 0) {
 7577:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 7578: 
 7579:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 7580:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 7581:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 7582:                                     } 
 7583:                                 } elsif (ref($usertypes) eq 'HASH') {
 7584:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 7585:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 7586:                                     } else {
 7587:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 7588:                                     }
 7589:                                     $chgtext .= '<ul>';
 7590:                                     foreach my $case (@{$cancreate{$type}}) {
 7591:                                         if ($case eq 'default') {
 7592:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 7593:                                         } else {
 7594:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 7595:                                         }
 7596:                                     }
 7597:                                     $chgtext .= '</ul>';
 7598:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 7599:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').'</span>';
 7600:                                     }
 7601:                                 }
 7602:                             } else {
 7603:                                 if (@{$cancreate{$type}} == 0) {
 7604:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 7605:                                 } else {
 7606:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 7607:                                 }
 7608:                             }
 7609:                         }
 7610:                     } elsif ($type eq 'captcha') {
 7611:                         if ($cancreate{$type} eq 'notused') {
 7612:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 7613:                         } else {
 7614:                             my %captchas = &captcha_phrases();
 7615:                             if ($captchas{$cancreate{$type}}) {
 7616:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$cancreate{$type}}.");
 7617:                             } else {
 7618:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 7619:                             }
 7620:                         }
 7621:                     } elsif ($type eq 'recaptchakeys') {
 7622:                         my ($privkey,$pubkey);
 7623:                         if (ref($cancreate{$type}) eq 'HASH') {
 7624:                             $pubkey = $cancreate{$type}{'public'};
 7625:                             $privkey = $cancreate{$type}{'private'};
 7626:                         }
 7627:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 7628:                         if (!$pubkey) {
 7629:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 7630:                         } else {
 7631:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 7632:                         }
 7633:                         if (!$privkey) {
 7634:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 7635:                         } else {
 7636:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 7637:                         }
 7638:                         $chgtext .= '</ul>';
 7639:                     } else {
 7640:                         if ($cancreate{$type} eq 'none') {
 7641:                             $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 7642:                         } elsif ($cancreate{$type} eq 'any') {
 7643:                             $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 7644:                         } elsif ($cancreate{$type} eq 'official') {
 7645:                             $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 7646:                         } elsif ($cancreate{$type} eq 'unofficial') {
 7647:                             $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 7648:                         }
 7649:                     }
 7650:                     $resulttext .= '<li>'.$chgtext.'</li>';
 7651:                 }
 7652:             }
 7653:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 7654:                 my ($rules,$ruleorder) = 
 7655:                     &Apache::lonnet::inst_userrules($dom,'username');
 7656:                 my $chgtext = '<ul>';
 7657:                 foreach my $type (@username_rule) {
 7658:                     if (ref($rules->{$type}) eq 'HASH') {
 7659:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 7660:                     }
 7661:                 }
 7662:                 $chgtext .= '</ul>';
 7663:                 if (@username_rule > 0) {
 7664:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 7665:                 } else {
 7666:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 7667:                 }
 7668:             }
 7669:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 7670:                 my ($idrules,$idruleorder) = 
 7671:                     &Apache::lonnet::inst_userrules($dom,'id');
 7672:                 my $chgtext = '<ul>';
 7673:                 foreach my $type (@id_rule) {
 7674:                     if (ref($idrules->{$type}) eq 'HASH') {
 7675:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 7676:                     }
 7677:                 }
 7678:                 $chgtext .= '</ul>';
 7679:                 if (@id_rule > 0) {
 7680:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 7681:                 } else {
 7682:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 7683:                 }
 7684:             }
 7685:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 7686:                 my ($emailrules,$emailruleorder) =
 7687:                     &Apache::lonnet::inst_userrules($dom,'email');
 7688:                 my $chgtext = '<ul>';
 7689:                 foreach my $type (@email_rule) {
 7690:                     if (ref($emailrules->{$type}) eq 'HASH') {
 7691:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 7692:                     }
 7693:                 }
 7694:                 $chgtext .= '</ul>';
 7695:                 if (@email_rule > 0) {
 7696:                     $resulttext .= '<li>'.&mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').$chgtext.'</li>';
 7697:                 } else {
 7698:                     $resulttext .= '<li>'.&mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').'</li>';
 7699:                 }
 7700:             }
 7701: 
 7702:             my %authname = &authtype_names();
 7703:             my %context_title = &context_names();
 7704:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 7705:                 my $chgtext = '<ul>';
 7706:                 foreach my $type (@{$changes{'authtypes'}}) {
 7707:                     my @allowed;
 7708:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 7709:                     foreach my $auth (@authtypes) {
 7710:                         if ($authhash{$type}{$auth}) {
 7711:                             push(@allowed,$authname{$auth});
 7712:                         }
 7713:                     }
 7714:                     if (@allowed > 0) {
 7715:                         $chgtext .= join(', ',@allowed).'</li>';
 7716:                     } else {
 7717:                         $chgtext .= &mt('none').'</li>';
 7718:                     }
 7719:                 }
 7720:                 $chgtext .= '</ul>';
 7721:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 7722:                 $resulttext .= '</li>';
 7723:             }
 7724:             $resulttext .= '</ul>';
 7725:         } else {
 7726:             $resulttext = &mt('No changes made to user creation settings');
 7727:         }
 7728:     } else {
 7729:         $resulttext = '<span class="LC_error">'.
 7730:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7731:     }
 7732:     if ($warningmsg ne '') {
 7733:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 7734:     }
 7735:     return $resulttext;
 7736: }
 7737: 
 7738: sub process_captcha {
 7739:     my ($container,$changes,$newsettings,$current) = @_;
 7740:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 7741:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 7742:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 7743:         $newsettings->{'captcha'} = 'original';
 7744:     }
 7745:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 7746:         if ($container eq 'cancreate') {
 7747:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7748:                 push(@{$changes->{'cancreate'}},'captcha');
 7749:             } elsif (!defined($changes->{'cancreate'})) {
 7750:                 $changes->{'cancreate'} = ['captcha'];
 7751:             }
 7752:         } else {
 7753:             $changes->{'captcha'} = 1;
 7754:         }
 7755:     }
 7756:     my ($newpub,$newpriv,$currpub,$currpriv);
 7757:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 7758:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 7759:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 7760:         $newpub =~ s/\W//g;
 7761:         $newpriv =~ s/\W//g;
 7762:         $newsettings->{'recaptchakeys'} = {
 7763:                                              public  => $newpub,
 7764:                                              private => $newpriv,
 7765:                                           };
 7766:     }
 7767:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 7768:         $currpub = $current->{'recaptchakeys'}{'public'};
 7769:         $currpriv = $current->{'recaptchakeys'}{'private'};
 7770:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 7771:             $newsettings->{'recaptchakeys'} = {
 7772:                                                  public  => '',
 7773:                                                  private => '',
 7774:                                               }
 7775:         }
 7776:     }
 7777:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 7778:         if ($container eq 'cancreate') {
 7779:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7780:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 7781:             } elsif (!defined($changes->{'cancreate'})) {
 7782:                 $changes->{'cancreate'} = ['recaptchakeys'];
 7783:             }
 7784:         } else {
 7785:             $changes->{'recaptchakeys'} = 1;
 7786:         }
 7787:     }
 7788:     return;
 7789: }
 7790: 
 7791: sub modify_usermodification {
 7792:     my ($dom,%domconfig) = @_;
 7793:     my ($resulttext,%curr_usermodification,%changes);
 7794:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 7795:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 7796:             $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 7797:         }
 7798:     }
 7799:     my @contexts = ('author','course','selfcreate');
 7800:     my %context_title = (
 7801:                            author => 'In author context',
 7802:                            course => 'In course context',
 7803:                            selfcreate => 'When self creating account', 
 7804:                         );
 7805:     my @fields = ('lastname','firstname','middlename','generation',
 7806:                   'permanentemail','id');
 7807:     my %roles = (
 7808:                   author => ['ca','aa'],
 7809:                   course => ['st','ep','ta','in','cr'],
 7810:                 );
 7811:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7812:     if (ref($types) eq 'ARRAY') {
 7813:         push(@{$types},'default');
 7814:         $usertypes->{'default'} = $othertitle;
 7815:     }
 7816:     $roles{'selfcreate'} = $types;  
 7817:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7818:     my %modifyhash;
 7819:     foreach my $context (@contexts) {
 7820:         foreach my $role (@{$roles{$context}}) {
 7821:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 7822:             foreach my $item (@fields) {
 7823:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 7824:                     $modifyhash{$context}{$role}{$item} = 1;
 7825:                 } else {
 7826:                     $modifyhash{$context}{$role}{$item} = 0;
 7827:                 }
 7828:             }
 7829:         }
 7830:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 7831:             foreach my $role (@{$roles{$context}}) {
 7832:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 7833:                     foreach my $field (@fields) {
 7834:                         if ($modifyhash{$context}{$role}{$field} ne 
 7835:                                 $curr_usermodification{$context}{$role}{$field}) {
 7836:                             push(@{$changes{$context}},$role);
 7837:                             last;
 7838:                         }
 7839:                     }
 7840:                 }
 7841:             }
 7842:         } else {
 7843:             foreach my $context (@contexts) {
 7844:                 foreach my $role (@{$roles{$context}}) {
 7845:                     push(@{$changes{$context}},$role);
 7846:                 }
 7847:             }
 7848:         }
 7849:     }
 7850:     my %usermodification_hash =  (
 7851:                                    usermodification => \%modifyhash,
 7852:                                  );
 7853:     my $putresult = &Apache::lonnet::put_dom('configuration',
 7854:                                              \%usermodification_hash,$dom);
 7855:     if ($putresult eq 'ok') {
 7856:         if (keys(%changes) > 0) {
 7857:             $resulttext = &mt('Changes made: ').'<ul>';
 7858:             foreach my $context (@contexts) {
 7859:                 if (ref($changes{$context}) eq 'ARRAY') {
 7860:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 7861:                     if (ref($changes{$context}) eq 'ARRAY') {
 7862:                         foreach my $role (@{$changes{$context}}) {
 7863:                             my $rolename;
 7864:                             if ($context eq 'selfcreate') {
 7865:                                 $rolename = $role;
 7866:                                 if (ref($usertypes) eq 'HASH') {
 7867:                                     if ($usertypes->{$role} ne '') {
 7868:                                         $rolename = $usertypes->{$role};
 7869:                                     }
 7870:                                 }
 7871:                             } else {
 7872:                                 if ($role eq 'cr') {
 7873:                                     $rolename = &mt('Custom');
 7874:                                 } else {
 7875:                                     $rolename = &Apache::lonnet::plaintext($role);
 7876:                                 }
 7877:                             }
 7878:                             my @modifiable;
 7879:                             if ($context eq 'selfcreate') {
 7880:                                 $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): ');
 7881:                             } else {
 7882:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 7883:                             }
 7884:                             foreach my $field (@fields) {
 7885:                                 if ($modifyhash{$context}{$role}{$field}) {
 7886:                                     push(@modifiable,$fieldtitles{$field});
 7887:                                 }
 7888:                             }
 7889:                             if (@modifiable > 0) {
 7890:                                 $resulttext .= join(', ',@modifiable);
 7891:                             } else {
 7892:                                 $resulttext .= &mt('none'); 
 7893:                             }
 7894:                             $resulttext .= '</li>';
 7895:                         }
 7896:                         $resulttext .= '</ul></li>';
 7897:                     }
 7898:                 }
 7899:             }
 7900:             $resulttext .= '</ul>';
 7901:         } else {
 7902:             $resulttext = &mt('No changes made to user modification settings');
 7903:         }
 7904:     } else {
 7905:         $resulttext = '<span class="LC_error">'.
 7906:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7907:     }
 7908:     return $resulttext;
 7909: }
 7910: 
 7911: sub modify_defaults {
 7912:     my ($dom,$lastactref,%domconfig) = @_;
 7913:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 7914:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 7915:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 7916:     my @authtypes = ('internal','krb4','krb5','localauth');
 7917:     foreach my $item (@items) {
 7918:         $newvalues{$item} = $env{'form.'.$item};
 7919:         if ($item eq 'auth_def') {
 7920:             if ($newvalues{$item} ne '') {
 7921:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 7922:                     push(@errors,$item);
 7923:                 }
 7924:             }
 7925:         } elsif ($item eq 'lang_def') {
 7926:             if ($newvalues{$item} ne '') {
 7927:                 if ($newvalues{$item} =~ /^(\w+)/) {
 7928:                     my $langcode = $1;
 7929:                     if ($langcode ne 'x_chef') {
 7930:                         if (code2language($langcode) eq '') {
 7931:                             push(@errors,$item);
 7932:                         }
 7933:                     }
 7934:                 } else {
 7935:                     push(@errors,$item);
 7936:                 }
 7937:             }
 7938:         } elsif ($item eq 'timezone_def') {
 7939:             if ($newvalues{$item} ne '') {
 7940:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 7941:                     push(@errors,$item);   
 7942:                 }
 7943:             }
 7944:         } elsif ($item eq 'datelocale_def') {
 7945:             if ($newvalues{$item} ne '') {
 7946:                 my @datelocale_ids = DateTime::Locale->ids();
 7947:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 7948:                     push(@errors,$item);
 7949:                 }
 7950:             }
 7951:         } elsif ($item eq 'portal_def') {
 7952:             if ($newvalues{$item} ne '') {
 7953:                 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])\/?$/) {
 7954:                     push(@errors,$item);
 7955:                 }
 7956:             }
 7957:         }
 7958:         if (grep(/^\Q$item\E$/,@errors)) {
 7959:             $newvalues{$item} = $domdefaults{$item};
 7960:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 7961:             $changes{$item} = 1;
 7962:         }
 7963:         $domdefaults{$item} = $newvalues{$item};
 7964:     }
 7965:     my %defaults_hash = (
 7966:                          defaults => \%newvalues,
 7967:                         );
 7968:     my $title = &defaults_titles();
 7969:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 7970:                                              $dom);
 7971:     if ($putresult eq 'ok') {
 7972:         if (keys(%changes) > 0) {
 7973:             $resulttext = &mt('Changes made:').'<ul>';
 7974:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 7975:             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";
 7976:             foreach my $item (sort(keys(%changes))) {
 7977:                 my $value = $env{'form.'.$item};
 7978:                 if ($value eq '') {
 7979:                     $value = &mt('none');
 7980:                 } elsif ($item eq 'auth_def') {
 7981:                     my %authnames = &authtype_names();
 7982:                     my %shortauth = (
 7983:                              internal => 'int',
 7984:                              krb4 => 'krb4',
 7985:                              krb5 => 'krb5',
 7986:                              localauth  => 'loc',
 7987:                     );
 7988:                     $value = $authnames{$shortauth{$value}};
 7989:                 }
 7990:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 7991:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 7992:             }
 7993:             $resulttext .= '</ul>';
 7994:             $mailmsgtext .= "\n";
 7995:             my $cachetime = 24*60*60;
 7996:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7997:             if (ref($lastactref) eq 'HASH') {
 7998:                 $lastactref->{'domdefaults'} = 1;
 7999:             }
 8000:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 8001:                 my $notify = 1;
 8002:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 8003:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 8004:                         $notify = 0;
 8005:                     }
 8006:                 }
 8007:                 if ($notify) {
 8008:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 8009:                                                "LON-CAPA Domain Settings Change - $dom",
 8010:                                                $mailmsgtext);
 8011:                 }
 8012:             }
 8013:         } else {
 8014:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 8015:         }
 8016:     } else {
 8017:         $resulttext = '<span class="LC_error">'.
 8018:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8019:     }
 8020:     if (@errors > 0) {
 8021:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 8022:         foreach my $item (@errors) {
 8023:             $resulttext .= ' "'.$title->{$item}.'",';
 8024:         }
 8025:         $resulttext =~ s/,$//;
 8026:     }
 8027:     return $resulttext;
 8028: }
 8029: 
 8030: sub modify_scantron {
 8031:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 8032:     my ($resulttext,%confhash,%changes,$errors);
 8033:     my $custom = 'custom.tab';
 8034:     my $default = 'default.tab';
 8035:     my $servadm = $r->dir_config('lonAdmEMail');
 8036:     my ($configuserok,$author_ok,$switchserver) = 
 8037:         &config_check($dom,$confname,$servadm);
 8038:     if ($env{'form.scantronformat.filename'} ne '') {
 8039:         my $error;
 8040:         if ($configuserok eq 'ok') {
 8041:             if ($switchserver) {
 8042:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 8043:             } else {
 8044:                 if ($author_ok eq 'ok') {
 8045:                     my ($result,$scantronurl) =
 8046:                         &publishlogo($r,'upload','scantronformat',$dom,
 8047:                                      $confname,'scantron','','',$custom);
 8048:                     if ($result eq 'ok') {
 8049:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 8050:                         $changes{'scantronformat'} = 1;
 8051:                     } else {
 8052:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 8053:                     }
 8054:                 } else {
 8055:                     $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);
 8056:                 }
 8057:             }
 8058:         } else {
 8059:             $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);
 8060:         }
 8061:         if ($error) {
 8062:             &Apache::lonnet::logthis($error);
 8063:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8064:         }
 8065:     }
 8066:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 8067:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 8068:             if ($env{'form.scantronformat_del'}) {
 8069:                 $confhash{'scantron'}{'scantronformat'} = '';
 8070:                 $changes{'scantronformat'} = 1;
 8071:             }
 8072:         }
 8073:     }
 8074:     if (keys(%confhash) > 0) {
 8075:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 8076:                                                  $dom);
 8077:         if ($putresult eq 'ok') {
 8078:             if (keys(%changes) > 0) {
 8079:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 8080:                     $resulttext = &mt('Changes made:').'<ul>';
 8081:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 8082:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 8083:                     } else {
 8084:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 8085:                     }
 8086:                     $resulttext .= '</ul>';
 8087:                 } else {
 8088:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 8089:                 }
 8090:                 $resulttext .= '</ul>';
 8091:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 8092:                 if (ref($lastactref) eq 'HASH') {
 8093:                     $lastactref->{'domainconfig'} = 1;
 8094:                 }
 8095:             } else {
 8096:                 $resulttext = &mt('No changes made to bubblesheet format file');
 8097:             }
 8098:         } else {
 8099:             $resulttext = '<span class="LC_error">'.
 8100:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 8101:         }
 8102:     } else {
 8103:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 8104:     }
 8105:     if ($errors) {
 8106:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 8107:                        $errors.'</ul>';
 8108:     }
 8109:     return $resulttext;
 8110: }
 8111: 
 8112: sub modify_coursecategories {
 8113:     my ($dom,%domconfig) = @_;
 8114:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 8115:         $cathash);
 8116:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 8117:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 8118:         $cathash = $domconfig{'coursecategories'}{'cats'};
 8119:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 8120:             $changes{'togglecats'} = 1;
 8121:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 8122:         }
 8123:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 8124:             $changes{'categorize'} = 1;
 8125:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 8126:         }
 8127:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 8128:             $changes{'togglecatscomm'} = 1;
 8129:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 8130:         }
 8131:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 8132:             $changes{'categorizecomm'} = 1;
 8133:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 8134:         }
 8135:     } else {
 8136:         $changes{'togglecats'} = 1;
 8137:         $changes{'categorize'} = 1;
 8138:         $changes{'togglecatscomm'} = 1;
 8139:         $changes{'categorizecomm'} = 1;
 8140:         $domconfig{'coursecategories'} = {
 8141:                                              togglecats => $env{'form.togglecats'},
 8142:                                              categorize => $env{'form.categorize'},
 8143:                                              togglecatscomm => $env{'form.togglecatscomm'},
 8144:                                              categorizecomm => $env{'form.categorizecomm'},
 8145:                                          };
 8146:     }
 8147:     if (ref($cathash) eq 'HASH') {
 8148:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 8149:             push (@deletecategory,'instcode::0');
 8150:         }
 8151:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 8152:             push(@deletecategory,'communities::0');
 8153:         }
 8154:     }
 8155:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 8156:     if (ref($cathash) eq 'HASH') {
 8157:         if (@deletecategory > 0) {
 8158:             #FIXME Need to remove category from all courses using a deleted category 
 8159:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 8160:             foreach my $item (@deletecategory) {
 8161:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 8162:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 8163:                     $deletions{$item} = 1;
 8164:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 8165:                 }
 8166:             }
 8167:         }
 8168:         foreach my $item (keys(%{$cathash})) {
 8169:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8170:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 8171:                 $reorderings{$item} = 1;
 8172:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 8173:             }
 8174:             if ($env{'form.addcategory_name_'.$item} ne '') {
 8175:                 my $newcat = $env{'form.addcategory_name_'.$item};
 8176:                 my $newdepth = $depth+1;
 8177:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 8178:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 8179:                 $adds{$newitem} = 1; 
 8180:             }
 8181:             if ($env{'form.subcat_'.$item} ne '') {
 8182:                 my $newcat = $env{'form.subcat_'.$item};
 8183:                 my $newdepth = $depth+1;
 8184:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 8185:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 8186:                 $adds{$newitem} = 1;
 8187:             }
 8188:         }
 8189:     }
 8190:     if ($env{'form.instcode'} eq '1') {
 8191:         if (ref($cathash) eq 'HASH') {
 8192:             my $newitem = 'instcode::0';
 8193:             if ($cathash->{$newitem} eq '') {  
 8194:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 8195:                 $adds{$newitem} = 1;
 8196:             }
 8197:         } else {
 8198:             my $newitem = 'instcode::0';
 8199:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 8200:             $adds{$newitem} = 1;
 8201:         }
 8202:     }
 8203:     if ($env{'form.communities'} eq '1') {
 8204:         if (ref($cathash) eq 'HASH') {
 8205:             my $newitem = 'communities::0';
 8206:             if ($cathash->{$newitem} eq '') {
 8207:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 8208:                 $adds{$newitem} = 1;
 8209:             }
 8210:         } else {
 8211:             my $newitem = 'communities::0';
 8212:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 8213:             $adds{$newitem} = 1;
 8214:         }
 8215:     }
 8216:     if ($env{'form.addcategory_name'} ne '') {
 8217:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 8218:             ($env{'form.addcategory_name'} ne 'communities')) {
 8219:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 8220:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 8221:             $adds{$newitem} = 1;
 8222:         }
 8223:     }
 8224:     my $putresult;
 8225:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 8226:         if (keys(%deletions) > 0) {
 8227:             foreach my $key (keys(%deletions)) {
 8228:                 if ($predelallitems{$key} ne '') {
 8229:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 8230:                 }
 8231:             }
 8232:         }
 8233:         my (@chkcats,@chktrails,%chkallitems);
 8234:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 8235:         if (ref($chkcats[0]) eq 'ARRAY') {
 8236:             my $depth = 0;
 8237:             my $chg = 0;
 8238:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 8239:                 my $name = $chkcats[0][$i];
 8240:                 my $item;
 8241:                 if ($name eq '') {
 8242:                     $chg ++;
 8243:                 } else {
 8244:                     $item = &escape($name).'::0';
 8245:                     if ($chg) {
 8246:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 8247:                     }
 8248:                     $depth ++; 
 8249:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 8250:                     $depth --;
 8251:                 }
 8252:             }
 8253:         }
 8254:     }
 8255:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 8256:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 8257:         if ($putresult eq 'ok') {
 8258:             my %title = (
 8259:                          togglecats     => 'Show/Hide a course in catalog',
 8260:                          categorize     => 'Assign a category to a course',
 8261:                          togglecatscomm => 'Show/Hide a community in catalog',
 8262:                          categorizecomm => 'Assign a category to a community',
 8263:                         );
 8264:             my %level = (
 8265:                          dom  => 'set in Domain ("Modify Course/Community")',
 8266:                          crs  => 'set in Course ("Course Configuration")',
 8267:                          comm => 'set in Community ("Community Configuration")',
 8268:                         );
 8269:             $resulttext = &mt('Changes made:').'<ul>';
 8270:             if ($changes{'togglecats'}) {
 8271:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 8272:             }
 8273:             if ($changes{'categorize'}) {
 8274:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 8275:             }
 8276:             if ($changes{'togglecatscomm'}) {
 8277:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 8278:             }
 8279:             if ($changes{'categorizecomm'}) {
 8280:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 8281:             }
 8282:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 8283:                 my $cathash;
 8284:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 8285:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 8286:                 } else {
 8287:                     $cathash = {};
 8288:                 } 
 8289:                 my (@cats,@trails,%allitems);
 8290:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 8291:                 if (keys(%deletions) > 0) {
 8292:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 8293:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 8294:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 8295:                     }
 8296:                     $resulttext .= '</ul></li>';
 8297:                 }
 8298:                 if (keys(%reorderings) > 0) {
 8299:                     my %sort_by_trail;
 8300:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 8301:                     foreach my $key (keys(%reorderings)) {
 8302:                         if ($allitems{$key} ne '') {
 8303:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 8304:                         }
 8305:                     }
 8306:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 8307:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 8308:                     }
 8309:                     $resulttext .= '</ul></li>';
 8310:                 }
 8311:                 if (keys(%adds) > 0) {
 8312:                     my %sort_by_trail;
 8313:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 8314:                     foreach my $key (keys(%adds)) {
 8315:                         if ($allitems{$key} ne '') {
 8316:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 8317:                         }
 8318:                     }
 8319:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 8320:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 8321:                     }
 8322:                     $resulttext .= '</ul></li>';
 8323:                 }
 8324:             }
 8325:             $resulttext .= '</ul>';
 8326:         } else {
 8327:             $resulttext = '<span class="LC_error">'.
 8328:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8329:         }
 8330:     } else {
 8331:         $resulttext = &mt('No changes made to course and community categories');
 8332:     }
 8333:     return $resulttext;
 8334: }
 8335: 
 8336: sub modify_serverstatuses {
 8337:     my ($dom,%domconfig) = @_;
 8338:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 8339:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 8340:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 8341:     }
 8342:     my @pages = &serverstatus_pages();
 8343:     foreach my $type (@pages) {
 8344:         $newserverstatus{$type}{'namedusers'} = '';
 8345:         $newserverstatus{$type}{'machines'} = '';
 8346:         if (defined($env{'form.'.$type.'_namedusers'})) {
 8347:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 8348:             my @okusers;
 8349:             foreach my $user (@users) {
 8350:                 my ($uname,$udom) = split(/:/,$user);
 8351:                 if (($udom =~ /^$match_domain$/) &&   
 8352:                     (&Apache::lonnet::domain($udom)) &&
 8353:                     ($uname =~ /^$match_username$/)) {
 8354:                     if (!grep(/^\Q$user\E/,@okusers)) {
 8355:                         push(@okusers,$user);
 8356:                     }
 8357:                 }
 8358:             }
 8359:             if (@okusers > 0) {
 8360:                  @okusers = sort(@okusers);
 8361:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 8362:             }
 8363:         }
 8364:         if (defined($env{'form.'.$type.'_machines'})) {
 8365:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 8366:             my @okmachines;
 8367:             foreach my $ip (@machines) {
 8368:                 my @parts = split(/\./,$ip);
 8369:                 next if (@parts < 4);
 8370:                 my $badip = 0;
 8371:                 for (my $i=0; $i<4; $i++) {
 8372:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 8373:                         $badip = 1;
 8374:                         last;
 8375:                     }
 8376:                 }
 8377:                 if (!$badip) {
 8378:                     push(@okmachines,$ip);     
 8379:                 }
 8380:             }
 8381:             @okmachines = sort(@okmachines);
 8382:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 8383:         }
 8384:     }
 8385:     my %serverstatushash =  (
 8386:                                 serverstatuses => \%newserverstatus,
 8387:                             );
 8388:     foreach my $type (@pages) {
 8389:         foreach my $setting ('namedusers','machines') {
 8390:             my (@current,@new);
 8391:             if (ref($currserverstatus{$type}) eq 'HASH') {
 8392:                 if ($currserverstatus{$type}{$setting} ne '') { 
 8393:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 8394:                 }
 8395:             }
 8396:             if ($newserverstatus{$type}{$setting} ne '') {
 8397:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 8398:             }
 8399:             if (@current > 0) {
 8400:                 if (@new > 0) {
 8401:                     foreach my $item (@current) {
 8402:                         if (!grep(/^\Q$item\E$/,@new)) {
 8403:                             $changes{$type}{$setting} = 1;
 8404:                             last;
 8405:                         }
 8406:                     }
 8407:                     foreach my $item (@new) {
 8408:                         if (!grep(/^\Q$item\E$/,@current)) {
 8409:                             $changes{$type}{$setting} = 1;
 8410:                             last;
 8411:                         }
 8412:                     }
 8413:                 } else {
 8414:                     $changes{$type}{$setting} = 1;
 8415:                 }
 8416:             } elsif (@new > 0) {
 8417:                 $changes{$type}{$setting} = 1;
 8418:             }
 8419:         }
 8420:     }
 8421:     if (keys(%changes) > 0) {
 8422:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 8423:         my $putresult = &Apache::lonnet::put_dom('configuration',
 8424:                                                  \%serverstatushash,$dom);
 8425:         if ($putresult eq 'ok') {
 8426:             $resulttext .= &mt('Changes made:').'<ul>';
 8427:             foreach my $type (@pages) {
 8428:                 if (ref($changes{$type}) eq 'HASH') {
 8429:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 8430:                     if ($changes{$type}{'namedusers'}) {
 8431:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 8432:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 8433:                         } else {
 8434:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 8435:                         }
 8436:                     }
 8437:                     if ($changes{$type}{'machines'}) {
 8438:                         if ($newserverstatus{$type}{'machines'} eq '') {
 8439:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 8440:                         } else {
 8441:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 8442:                         }
 8443: 
 8444:                     }
 8445:                     $resulttext .= '</ul></li>';
 8446:                 }
 8447:             }
 8448:             $resulttext .= '</ul>';
 8449:         } else {
 8450:             $resulttext = '<span class="LC_error">'.
 8451:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 8452: 
 8453:         }
 8454:     } else {
 8455:         $resulttext = &mt('No changes made to access to server status pages');
 8456:     }
 8457:     return $resulttext;
 8458: }
 8459: 
 8460: sub modify_helpsettings {
 8461:     my ($r,$dom,$confname,%domconfig) = @_;
 8462:     my ($resulttext,$errors,%changes,%helphash);
 8463:     my %defaultchecked = ('submitbugs' => 'on');
 8464:     my @offon = ('off','on');
 8465:     my @toggles = ('submitbugs');
 8466:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 8467:         foreach my $item (@toggles) {
 8468:             if ($defaultchecked{$item} eq 'on') { 
 8469:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 8470:                     if ($env{'form.'.$item} eq '0') {
 8471:                         $changes{$item} = 1;
 8472:                     }
 8473:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 8474:                     $changes{$item} = 1;
 8475:                 }
 8476:             } elsif ($defaultchecked{$item} eq 'off') {
 8477:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 8478:                     if ($env{'form.'.$item} eq '1') {
 8479:                         $changes{$item} = 1;
 8480:                     }
 8481:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 8482:                     $changes{$item} = 1;
 8483:                 }
 8484:             }
 8485:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 8486:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 8487:             }
 8488:         }
 8489:     }
 8490:     my $putresult;
 8491:     if (keys(%changes) > 0) {
 8492:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 8493:         if ($putresult eq 'ok') {
 8494:             $resulttext = &mt('Changes made:').'<ul>';
 8495:             foreach my $item (sort(keys(%changes))) {
 8496:                 if ($item eq 'submitbugs') {
 8497:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 8498:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 8499:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 8500:                 }
 8501:             }
 8502:             $resulttext .= '</ul>';
 8503:         } else {
 8504:             $resulttext = &mt('No changes made to help settings');
 8505:             $errors .= '<li><span class="LC_error">'.
 8506:                        &mt('An error occurred storing the settings: [_1]',
 8507:                            $putresult).'</span></li>';
 8508:         }
 8509:     }
 8510:     if ($errors) {
 8511:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 8512:                        $errors.'</ul>';
 8513:     }
 8514:     return $resulttext;
 8515: }
 8516: 
 8517: sub modify_coursedefaults {
 8518:     my ($dom,$lastactref,%domconfig) = @_;
 8519:     my ($resulttext,$errors,%changes,%defaultshash);
 8520:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 8521:     my @toggles = ('canuse_pdfforms');
 8522:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
 8523:                    'uploadquota_community','uploadquota_textbook');
 8524:     my @types = ('official','unofficial','community','textbook');
 8525:     my %staticdefaults = (
 8526:                            anonsurvey_threshold => 10,
 8527:                            uploadquota          => 500,
 8528:                          );
 8529: 
 8530:     $defaultshash{'coursedefaults'} = {};
 8531: 
 8532:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 8533:         if ($domconfig{'coursedefaults'} eq '') {
 8534:             $domconfig{'coursedefaults'} = {};
 8535:         }
 8536:     }
 8537: 
 8538:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 8539:         foreach my $item (@toggles) {
 8540:             if ($defaultchecked{$item} eq 'on') {
 8541:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 8542:                     ($env{'form.'.$item} eq '0')) {
 8543:                     $changes{$item} = 1;
 8544:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 8545:                     $changes{$item} = 1;
 8546:                 }
 8547:             } elsif ($defaultchecked{$item} eq 'off') {
 8548:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 8549:                     ($env{'form.'.$item} eq '1')) {
 8550:                     $changes{$item} = 1;
 8551:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 8552:                     $changes{$item} = 1;
 8553:                 }
 8554:             }
 8555:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 8556:         }
 8557:         foreach my $item (@numbers) {
 8558:             my ($currdef,$newdef);
 8559:             $newdef = $env{'form.'.$item};
 8560:             if ($item eq 'anonsurvey_threshold') {
 8561:                 $currdef = $domconfig{'coursedefaults'}{$item};
 8562:                 $newdef =~ s/\D//g;
 8563:                 if ($newdef eq '' || $newdef < 1) {
 8564:                     $newdef = 1;
 8565:                 }
 8566:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
 8567:             } else {
 8568:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
 8569:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 8570:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 8571:                 }
 8572:                 $newdef =~ s/[^\w.\-]//g;
 8573:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
 8574:             }
 8575:             if ($currdef ne $newdef) {
 8576:                 my $staticdef;
 8577:                 if ($item eq 'anonsurvey_threshold') {
 8578:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
 8579:                         $changes{$item} = 1;
 8580:                     }
 8581:                 } else {
 8582:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
 8583:                         $changes{'uploadquota'} = 1;
 8584:                     }
 8585:                 }
 8586:             }
 8587:         }
 8588:         my $officialcreds = $env{'form.official_credits'};
 8589:         $officialcreds =~ s/[^\d.]+//g;
 8590:         my $unofficialcreds = $env{'form.unofficial_credits'};
 8591:         $unofficialcreds =~ s/[^\d.]+//g;
 8592:         my $textbookcreds = $env{'form.textbook_credits'};
 8593:         $textbookcreds =~ s/[^\d.]+//g;
 8594:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
 8595:                 ($env{'form.coursecredits'} eq '1')) {
 8596:                 $changes{'coursecredits'} = 1;
 8597:         } else {
 8598:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
 8599:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds) ||
 8600:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'textbook'} ne $textbookcreds)) {
 8601:                 $changes{'coursecredits'} = 1;
 8602:             }
 8603:         }
 8604:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
 8605:             official   => $officialcreds,
 8606:             unofficial => $unofficialcreds,
 8607:             textbook   => $textbookcreds,
 8608:         }
 8609:     }
 8610:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 8611:                                              $dom);
 8612:     if ($putresult eq 'ok') {
 8613:         if (keys(%changes) > 0) {
 8614:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8615:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'}) || ($changes{'uploadquota'})) {
 8616:                 if ($changes{'canuse_pdfforms'}) {
 8617:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 8618:                 }
 8619:                 if ($changes{'coursecredits'}) {
 8620:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 8621:                         $domdefaults{'officialcredits'} =
 8622:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
 8623:                         $domdefaults{'unofficialcredits'} =
 8624:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
 8625:                         $domdefaults{'textbookcredits'} =
 8626:                             $domdefaults{'coursedefaults'}{'coursecredits'}{'textbook'};
 8627:                     }
 8628:                 }
 8629:                 if ($changes{'uploadquota'}) {
 8630:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 8631:                         foreach my $type (@types) {
 8632:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
 8633:                         }
 8634:                     }
 8635:                 }
 8636:                 my $cachetime = 24*60*60;
 8637:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8638:                 if (ref($lastactref) eq 'HASH') {
 8639:                     $lastactref->{'domdefaults'} = 1;
 8640:                 }
 8641:             }
 8642:             $resulttext = &mt('Changes made:').'<ul>';
 8643:             foreach my $item (sort(keys(%changes))) {
 8644:                 if ($item eq 'canuse_pdfforms') {
 8645:                     if ($env{'form.'.$item} eq '1') {
 8646:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 8647:                     } else {
 8648:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 8649:                     }
 8650:                 } elsif ($item eq 'anonsurvey_threshold') {
 8651:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 8652:                 } elsif ($item eq 'uploadquota') {
 8653:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 8654:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
 8655:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
 8656:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
 8657:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
 8658: 
 8659:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
 8660:                                        '</ul>'.
 8661:                                        '</li>';
 8662:                     } else {
 8663:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
 8664:                     }
 8665:                 } elsif ($item eq 'coursecredits') {
 8666:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 8667:                         if (($domdefaults{'officialcredits'} eq '') &&
 8668:                             ($domdefaults{'unofficialcredits'} eq '') &&
 8669:                             ($domdefaults{'textbookcredits'} eq '')) {
 8670:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 8671:                         } else {
 8672:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
 8673:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
 8674:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
 8675:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
 8676:                                            '</ul>'.
 8677:                                            '</li>';
 8678:                         }
 8679:                     } else {
 8680:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 8681:                     }
 8682:                 }
 8683:             }
 8684:             $resulttext .= '</ul>';
 8685:         } else {
 8686:             $resulttext = &mt('No changes made to course defaults');
 8687:         }
 8688:     } else {
 8689:         $resulttext = '<span class="LC_error">'.
 8690:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8691:     }
 8692:     return $resulttext;
 8693: }
 8694: 
 8695: sub modify_usersessions {
 8696:     my ($dom,$lastactref,%domconfig) = @_;
 8697:     my @hostingtypes = ('version','excludedomain','includedomain');
 8698:     my @offloadtypes = ('primary','default');
 8699:     my %types = (
 8700:                   remote => \@hostingtypes,
 8701:                   hosted => \@hostingtypes,
 8702:                   spares => \@offloadtypes,
 8703:                 );
 8704:     my @prefixes = ('remote','hosted','spares');
 8705:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 8706:     my (%by_ip,%by_location,@intdoms);
 8707:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 8708:     my @locations = sort(keys(%by_location));
 8709:     my (%defaultshash,%changes);
 8710:     foreach my $prefix (@prefixes) {
 8711:         $defaultshash{'usersessions'}{$prefix} = {};
 8712:     }
 8713:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8714:     my $resulttext;
 8715:     my %iphost = &Apache::lonnet::get_iphost();
 8716:     foreach my $prefix (@prefixes) {
 8717:         next if ($prefix eq 'spares');
 8718:         foreach my $type (@{$types{$prefix}}) {
 8719:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 8720:             if ($type eq 'version') {
 8721:                 my $value = $env{'form.'.$prefix.'_'.$type};
 8722:                 my $okvalue;
 8723:                 if ($value ne '') {
 8724:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 8725:                         $okvalue = $value;
 8726:                     }
 8727:                 }
 8728:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8729:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 8730:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 8731:                             if ($inuse == 0) {
 8732:                                 $changes{$prefix}{$type} = 1;
 8733:                             } else {
 8734:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 8735:                                     $changes{$prefix}{$type} = 1;
 8736:                                 }
 8737:                                 if ($okvalue ne '') {
 8738:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8739:                                 } 
 8740:                             }
 8741:                         } else {
 8742:                             if (($inuse == 1) && ($okvalue ne '')) {
 8743:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8744:                                 $changes{$prefix}{$type} = 1;
 8745:                             }
 8746:                         }
 8747:                     } else {
 8748:                         if (($inuse == 1) && ($okvalue ne '')) {
 8749:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8750:                             $changes{$prefix}{$type} = 1;
 8751:                         }
 8752:                     }
 8753:                 } else {
 8754:                     if (($inuse == 1) && ($okvalue ne '')) {
 8755:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8756:                         $changes{$prefix}{$type} = 1;
 8757:                     }
 8758:                 }
 8759:             } else {
 8760:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 8761:                 my @okvals;
 8762:                 foreach my $val (@vals) {
 8763:                     if ($val =~ /:/) {
 8764:                         my @items = split(/:/,$val);
 8765:                         foreach my $item (@items) {
 8766:                             if (ref($by_location{$item}) eq 'ARRAY') {
 8767:                                 push(@okvals,$item);
 8768:                             }
 8769:                         }
 8770:                     } else {
 8771:                         if (ref($by_location{$val}) eq 'ARRAY') {
 8772:                             push(@okvals,$val);
 8773:                         }
 8774:                     }
 8775:                 }
 8776:                 @okvals = sort(@okvals);
 8777:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8778:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 8779:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 8780:                             if ($inuse == 0) {
 8781:                                 $changes{$prefix}{$type} = 1; 
 8782:                             } else {
 8783:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8784:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 8785:                                 if (@changed > 0) {
 8786:                                     $changes{$prefix}{$type} = 1;
 8787:                                 }
 8788:                             }
 8789:                         } else {
 8790:                             if ($inuse == 1) {
 8791:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8792:                                 $changes{$prefix}{$type} = 1;
 8793:                             }
 8794:                         } 
 8795:                     } else {
 8796:                         if ($inuse == 1) {
 8797:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8798:                             $changes{$prefix}{$type} = 1;
 8799:                         }
 8800:                     }
 8801:                 } else {
 8802:                     if ($inuse == 1) {
 8803:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8804:                         $changes{$prefix}{$type} = 1;
 8805:                     }
 8806:                 }
 8807:             }
 8808:         }
 8809:     }
 8810: 
 8811:     my @alldoms = &Apache::lonnet::all_domains();
 8812:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8813:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 8814:     my $savespares;
 8815: 
 8816:     foreach my $lonhost (sort(keys(%servers))) {
 8817:         my $serverhomeID =
 8818:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 8819:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 8820:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 8821:         my %spareschg;
 8822:         foreach my $type (@{$types{'spares'}}) {
 8823:             my @okspares;
 8824:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 8825:             foreach my $server (@checked) {
 8826:                 if (&Apache::lonnet::hostname($server) ne '') {
 8827:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 8828:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 8829:                             push(@okspares,$server);
 8830:                         }
 8831:                     }
 8832:                 }
 8833:             }
 8834:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 8835:             my $newspare;
 8836:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 8837:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 8838:                     $newspare = $new;
 8839:                 }
 8840:             }
 8841:             my @spares;
 8842:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 8843:                 @spares = sort(@okspares,$newspare);
 8844:             } else {
 8845:                 @spares = sort(@okspares);
 8846:             }
 8847:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 8848:             if (ref($spareid{$lonhost}) eq 'HASH') {
 8849:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 8850:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 8851:                     if (@diffs > 0) {
 8852:                         $spareschg{$type} = 1;
 8853:                     }
 8854:                 }
 8855:             }
 8856:         }
 8857:         if (keys(%spareschg) > 0) {
 8858:             $changes{'spares'}{$lonhost} = \%spareschg;
 8859:         }
 8860:     }
 8861: 
 8862:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8863:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 8864:             if (ref($changes{'spares'}) eq 'HASH') {
 8865:                 if (keys(%{$changes{'spares'}}) > 0) {
 8866:                     $savespares = 1;
 8867:                 }
 8868:             }
 8869:         } else {
 8870:             $savespares = 1;
 8871:         }
 8872:     }
 8873: 
 8874:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 8875:     if ((keys(%changes) > 0) || ($savespares)) {
 8876:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 8877:                                                  $dom);
 8878:         if ($putresult eq 'ok') {
 8879:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8880:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 8881:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 8882:                 }
 8883:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 8884:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 8885:                 }
 8886:             }
 8887:             my $cachetime = 24*60*60;
 8888:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8889:             if (ref($lastactref) eq 'HASH') {
 8890:                 $lastactref->{'domdefaults'} = 1;
 8891:             }
 8892:             if (keys(%changes) > 0) {
 8893:                 my %lt = &usersession_titles();
 8894:                 $resulttext = &mt('Changes made:').'<ul>';
 8895:                 foreach my $prefix (@prefixes) {
 8896:                     if (ref($changes{$prefix}) eq 'HASH') {
 8897:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 8898:                         if ($prefix eq 'spares') {
 8899:                             if (ref($changes{$prefix}) eq 'HASH') {
 8900:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 8901:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 8902:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 8903:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
 8904:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
 8905:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 8906:                                         foreach my $type (@{$types{$prefix}}) {
 8907:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 8908:                                                 my $offloadto = &mt('None');
 8909:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 8910:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 8911:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 8912:                                                     }
 8913:                                                 }
 8914:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 8915:                                             }
 8916:                                         }
 8917:                                     }
 8918:                                     $resulttext .= '</li>';
 8919:                                 }
 8920:                             }
 8921:                         } else {
 8922:                             foreach my $type (@{$types{$prefix}}) {
 8923:                                 if (defined($changes{$prefix}{$type})) {
 8924:                                     my $newvalue;
 8925:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8926:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 8927:                                             if ($type eq 'version') {
 8928:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 8929:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 8930:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 8931:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 8932:                                                 }
 8933:                                             }
 8934:                                         }
 8935:                                     }
 8936:                                     if ($newvalue eq '') {
 8937:                                         if ($type eq 'version') {
 8938:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 8939:                                         } else {
 8940:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 8941:                                         }
 8942:                                     } else {
 8943:                                         if ($type eq 'version') {
 8944:                                             $newvalue .= ' '.&mt('(or later)'); 
 8945:                                         }
 8946:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 8947:                                     }
 8948:                                 }
 8949:                             }
 8950:                         }
 8951:                         $resulttext .= '</ul>';
 8952:                     }
 8953:                 }
 8954:                 $resulttext .= '</ul>';
 8955:             } else {
 8956:                 $resulttext = $nochgmsg;
 8957:             }
 8958:         } else {
 8959:             $resulttext = '<span class="LC_error">'.
 8960:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8961:         }
 8962:     } else {
 8963:         $resulttext = $nochgmsg;
 8964:     }
 8965:     return $resulttext;
 8966: }
 8967: 
 8968: sub modify_loadbalancing {
 8969:     my ($dom,%domconfig) = @_;
 8970:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8971:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8972:     my ($othertitle,$usertypes,$types) =
 8973:         &Apache::loncommon::sorted_inst_types($dom);
 8974:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8975:     my @sparestypes = ('primary','default');
 8976:     my %typetitles = &sparestype_titles();
 8977:     my $resulttext;
 8978:     my (%currbalancer,%currtargets,%currrules,%existing);
 8979:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8980:         %existing = %{$domconfig{'loadbalancing'}};
 8981:     }
 8982:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 8983:                               \%currtargets,\%currrules);
 8984:     my ($saveloadbalancing,%defaultshash,%changes);
 8985:     my ($alltypes,$othertypes,$titles) =
 8986:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 8987:     my %ruletitles = &offloadtype_text();
 8988:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
 8989:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
 8990:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
 8991:         if ($balancer eq '') {
 8992:             next;
 8993:         }
 8994:         if (!exists($servers{$balancer})) {
 8995:             if (exists($currbalancer{$balancer})) {
 8996:                 push(@{$changes{'delete'}},$balancer);
 8997:             }
 8998:             next;
 8999:         }
 9000:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
 9001:             push(@{$changes{'delete'}},$balancer);
 9002:             next;
 9003:         }
 9004:         if (!exists($currbalancer{$balancer})) {
 9005:             push(@{$changes{'add'}},$balancer);
 9006:         }
 9007:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
 9008:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
 9009:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
 9010:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 9011:             $saveloadbalancing = 1;
 9012:         }
 9013:         foreach my $sparetype (@sparestypes) {
 9014:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
 9015:             my @offloadto;
 9016:             foreach my $target (@targets) {
 9017:                 if (($servers{$target}) && ($target ne $balancer)) {
 9018:                     if ($sparetype eq 'default') {
 9019:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
 9020:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
 9021:                         }
 9022:                     }
 9023:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
 9024:                         push(@offloadto,$target);
 9025:                     }
 9026:                 }
 9027:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
 9028:             }
 9029:         }
 9030:         if (ref($currtargets{$balancer}) eq 'HASH') {
 9031:             foreach my $sparetype (@sparestypes) {
 9032:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
 9033:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
 9034:                     if (@targetdiffs > 0) {
 9035:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 9036:                     }
 9037:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 9038:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 9039:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 9040:                     }
 9041:                 }
 9042:             }
 9043:         } else {
 9044:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
 9045:                 foreach my $sparetype (@sparestypes) {
 9046:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 9047:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 9048:                             $changes{'curr'}{$balancer}{'targets'} = 1;
 9049:                         }
 9050:                     }
 9051:                 }
 9052:             }
 9053:         }
 9054:         my $ishomedom;
 9055:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 9056:             $ishomedom = 1;
 9057:         }
 9058:         if (ref($alltypes) eq 'ARRAY') {
 9059:             foreach my $type (@{$alltypes}) {
 9060:                 my $rule;
 9061:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
 9062:                          (!$ishomedom)) {
 9063:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
 9064:                 }
 9065:                 if ($rule eq 'specific') {
 9066:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
 9067:                 }
 9068:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
 9069:                 if (ref($currrules{$balancer}) eq 'HASH') {
 9070:                     if ($rule ne $currrules{$balancer}{$type}) {
 9071:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 9072:                     }
 9073:                 } elsif ($rule ne '') {
 9074:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 9075:                 }
 9076:             }
 9077:         }
 9078:     }
 9079:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 9080:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 9081:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
 9082:             $defaultshash{'loadbalancing'} = {};
 9083:         }
 9084:         my $putresult = &Apache::lonnet::put_dom('configuration',
 9085:                                                  \%defaultshash,$dom);
 9086:         if ($putresult eq 'ok') {
 9087:             if (keys(%changes) > 0) {
 9088:                 if (ref($changes{'delete'}) eq 'ARRAY') {
 9089:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
 9090:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
 9091:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
 9092:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
 9093:                     }
 9094:                 }
 9095:                 if (ref($changes{'add'}) eq 'ARRAY') {
 9096:                     foreach my $balancer (sort(@{$changes{'add'}})) {
 9097:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
 9098:                     }
 9099:                 }
 9100:                 if (ref($changes{'curr'}) eq 'HASH') {
 9101:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
 9102:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
 9103:                             if ($changes{'curr'}{$balancer}{'targets'}) {
 9104:                                 my %offloadstr;
 9105:                                 foreach my $sparetype (@sparestypes) {
 9106:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 9107:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 9108:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
 9109:                                         }
 9110:                                     }
 9111:                                 }
 9112:                                 if (keys(%offloadstr) == 0) {
 9113:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 9114:                                 } else {
 9115:                                     my $showoffload;
 9116:                                     foreach my $sparetype (@sparestypes) {
 9117:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
 9118:                                         if (defined($offloadstr{$sparetype})) {
 9119:                                             $showoffload .= $offloadstr{$sparetype};
 9120:                                         } else {
 9121:                                             $showoffload .= &mt('None');
 9122:                                         }
 9123:                                         $showoffload .= ('&nbsp;'x3);
 9124:                                     }
 9125:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
 9126:                                 }
 9127:                             }
 9128:                         }
 9129:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
 9130:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
 9131:                                 foreach my $type (@{$alltypes}) {
 9132:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
 9133:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
 9134:                                         my $balancetext;
 9135:                                         if ($rule eq '') {
 9136:                                             $balancetext =  $ruletitles{'default'};
 9137:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
 9138:                                                  ($rule eq 'balancer') || ($rule eq 'offloadedto')) {
 9139:                                             $balancetext =  $ruletitles{$rule};
 9140:                                         } else {
 9141:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
 9142:                                         }
 9143:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
 9144:                                     }
 9145:                                 }
 9146:                             }
 9147:                         }
 9148:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
 9149:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
 9150:                     }
 9151:                 }
 9152:                 if ($resulttext ne '') {
 9153:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
 9154:                 } else {
 9155:                     $resulttext = $nochgmsg;
 9156:                 }
 9157:             } else {
 9158:                 $resulttext = $nochgmsg;
 9159:             }
 9160:         } else {
 9161:             $resulttext = '<span class="LC_error">'.
 9162:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 9163:         }
 9164:     } else {
 9165:         $resulttext = $nochgmsg;
 9166:     }
 9167:     return $resulttext;
 9168: }
 9169: 
 9170: sub recurse_check {
 9171:     my ($chkcats,$categories,$depth,$name) = @_;
 9172:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 9173:         my $chg = 0;
 9174:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 9175:             my $category = $chkcats->[$depth]{$name}[$j];
 9176:             my $item;
 9177:             if ($category eq '') {
 9178:                 $chg ++;
 9179:             } else {
 9180:                 my $deeper = $depth + 1;
 9181:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 9182:                 if ($chg) {
 9183:                     $categories->{$item} -= $chg;
 9184:                 }
 9185:                 &recurse_check($chkcats,$categories,$deeper,$category);
 9186:                 $deeper --;
 9187:             }
 9188:         }
 9189:     }
 9190:     return;
 9191: }
 9192: 
 9193: sub recurse_cat_deletes {
 9194:     my ($item,$coursecategories,$deletions) = @_;
 9195:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 9196:     my $subdepth = $depth + 1;
 9197:     if (ref($coursecategories) eq 'HASH') {
 9198:         foreach my $subitem (keys(%{$coursecategories})) {
 9199:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 9200:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 9201:                 delete($coursecategories->{$subitem});
 9202:                 $deletions->{$subitem} = 1;
 9203:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 9204:             }
 9205:         }
 9206:     }
 9207:     return;
 9208: }
 9209: 
 9210: sub get_active_dcs {
 9211:     my ($dom) = @_;
 9212:     my $now = time;
 9213:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
 9214:     my %domcoords;
 9215:     my $numdcs = 0;
 9216:     foreach my $server (keys(%dompersonnel)) {
 9217:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 9218:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 9219:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 9220:         }
 9221:     }
 9222:     return %domcoords;
 9223: }
 9224: 
 9225: sub active_dc_picker {
 9226:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
 9227:     my %domcoords = &get_active_dcs($dom); 
 9228:     my @domcoord = keys(%domcoords);
 9229:     if (keys(%currhash)) {
 9230:         foreach my $dc (keys(%currhash)) {
 9231:             unless (exists($domcoords{$dc})) {
 9232:                 push(@domcoord,$dc);
 9233:             }
 9234:         }
 9235:     }
 9236:     @domcoord = sort(@domcoord);
 9237:     my $numdcs = scalar(@domcoord);
 9238:     my $rows = 0;
 9239:     my $table;
 9240:     if ($numdcs > 1) {
 9241:         $table = '<table>';
 9242:         for (my $i=0; $i<@domcoord; $i++) {
 9243:             my $rem = $i%($numinrow);
 9244:             if ($rem == 0) {
 9245:                 if ($i > 0) {
 9246:                     $table .= '</tr>';
 9247:                 }
 9248:                 $table .= '<tr>';
 9249:                 $rows ++;
 9250:             }
 9251:             my $check = '';
 9252:             if ($inputtype eq 'radio') {
 9253:                 if (keys(%currhash) == 0) {
 9254:                     if (!$i) {
 9255:                         $check = ' checked="checked"';
 9256:                     }
 9257:                 } elsif (exists($currhash{$domcoord[$i]})) {
 9258:                     $check = ' checked="checked"';
 9259:                 }
 9260:             } else {
 9261:                 if (exists($currhash{$domcoord[$i]})) {
 9262:                     $check = ' checked="checked"';
 9263:                 }
 9264:             }
 9265:             if ($i == @domcoord - 1) {
 9266:                 my $colsleft = $numinrow - $rem;
 9267:                 if ($colsleft > 1) {
 9268:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
 9269:                 } else {
 9270:                     $table .= '<td class="LC_left_item">';
 9271:                 }
 9272:             } else {
 9273:                 $table .= '<td class="LC_left_item">';
 9274:             }
 9275:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
 9276:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
 9277:             $table .= '<span class="LC_nobreak"><label>'.
 9278:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
 9279:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
 9280:             if ($user ne $dcname.':'.$dcdom) {
 9281:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
 9282:             }
 9283:             $table .= '</label></span></td>'; 
 9284:         }
 9285:         $table .= '</tr></table>';
 9286:     } elsif ($numdcs == 1) {
 9287:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
 9288:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
 9289:         if ($inputtype eq 'radio') {
 9290:             $table .= '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
 9291:             if ($user ne $dcname.':'.$dcdom) {
 9292:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
 9293:             }
 9294:         } else {
 9295:             my $check;
 9296:             if (exists($currhash{$domcoord[0]})) {
 9297:                 $check = ' checked="checked"';
 9298:             }
 9299:             $table .= '<span class="LC_nobreak"><label>'.
 9300:                       '<input type="checkbox" name="'.$name.'" '.
 9301:                       'value="'.$domcoord[0].'"'.$check.' />'.$user;
 9302:             if ($user ne $dcname.':'.$dcdom) {
 9303:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
 9304:             }
 9305:             $table .= '</label></span>';
 9306:             $rows ++;
 9307:         }
 9308:     }
 9309:     return ($numdcs,$table,$rows);
 9310: }
 9311: 
 9312: sub usersession_titles {
 9313:     return &Apache::lonlocal::texthash(
 9314:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
 9315:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
 9316:                spares => 'Servers offloaded to, when busy',
 9317:                version => 'LON-CAPA version requirement',
 9318:                excludedomain => 'Allow all, but exclude specific domains',
 9319:                includedomain => 'Deny all, but include specific domains',
 9320:                primary => 'Primary (checked first)',
 9321:                default => 'Default',
 9322:            );
 9323: }
 9324: 
 9325: sub id_for_thisdom {
 9326:     my (%servers) = @_;
 9327:     my %altids;
 9328:     foreach my $server (keys(%servers)) {
 9329:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 9330:         if ($serverhome ne $server) {
 9331:             $altids{$serverhome} = $server;
 9332:         }
 9333:     }
 9334:     return %altids;
 9335: }
 9336: 
 9337: sub count_servers {
 9338:     my ($currbalancer,%servers) = @_;
 9339:     my (@spares,$numspares);
 9340:     foreach my $lonhost (sort(keys(%servers))) {
 9341:         next if ($currbalancer eq $lonhost);
 9342:         push(@spares,$lonhost);
 9343:     }
 9344:     if ($currbalancer) {
 9345:         $numspares = scalar(@spares);
 9346:     } else {
 9347:         $numspares = scalar(@spares) - 1;
 9348:     }
 9349:     return ($numspares,@spares);
 9350: }
 9351: 
 9352: sub lonbalance_targets_js {
 9353:     my ($dom,$types,$servers,$settings) = @_;
 9354:     my $select = &mt('Select');
 9355:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
 9356:     if (ref($servers) eq 'HASH') {
 9357:         $alltargets = join("','",sort(keys(%{$servers})));
 9358:         my @homedoms;
 9359:         foreach my $server (sort(keys(%{$servers}))) {
 9360:             if (&Apache::lonnet::host_domain($server) eq $dom) {
 9361:                 push(@homedoms,'1');
 9362:             } else {
 9363:                 push(@homedoms,'0');
 9364:             }
 9365:         }
 9366:         $allishome = join("','",@homedoms);
 9367:     }
 9368:     if (ref($types) eq 'ARRAY') {
 9369:         if (@{$types} > 0) {
 9370:             @alltypes = @{$types};
 9371:         }
 9372:     }
 9373:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 9374:     $allinsttypes = join("','",@alltypes);
 9375:     my (%currbalancer,%currtargets,%currrules,%existing);
 9376:     if (ref($settings) eq 'HASH') {
 9377:         %existing = %{$settings};
 9378:     }
 9379:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
 9380:                               \%currtargets,\%currrules);
 9381:     my $balancers = join("','",sort(keys(%currbalancer)));
 9382:     return <<"END";
 9383: 
 9384: <script type="text/javascript">
 9385: // <![CDATA[
 9386: 
 9387: currBalancers = new Array('$balancers');
 9388: 
 9389: function toggleTargets(balnum) {
 9390:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 9391:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
 9392:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
 9393:     var prevbalancer = prevhostitem.value;
 9394:     var baltotal = document.getElementById('loadbalancing_total').value;
 9395:     prevhostitem.value = balancer;
 9396:     if (prevbalancer != '') {
 9397:         var prevIdx = currBalancers.indexOf(prevbalancer);
 9398:         if (prevIdx != -1) {
 9399:             currBalancers.splice(prevIdx,1);
 9400:         }
 9401:     }
 9402:     if (balancer == '') {
 9403:         hideSpares(balnum);
 9404:     } else {
 9405:         var currIdx = currBalancers.indexOf(balancer);
 9406:         if (currIdx == -1) {
 9407:             currBalancers.push(balancer);
 9408:         }
 9409:         var homedoms = new Array('$allishome');
 9410:         var ishomedom = homedoms[lonhostitem.selectedIndex];
 9411:         showSpares(balancer,ishomedom,balnum);
 9412:     }
 9413:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
 9414:     return;
 9415: }
 9416: 
 9417: function showSpares(balancer,ishomedom,balnum) {
 9418:     var alltargets = new Array('$alltargets');
 9419:     var insttypes = new Array('$allinsttypes');
 9420:     var offloadtypes = new Array('primary','default');
 9421: 
 9422:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
 9423:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
 9424:  
 9425:     for (var i=0; i<offloadtypes.length; i++) {
 9426:         var count = 0;
 9427:         for (var j=0; j<alltargets.length; j++) {
 9428:             if (alltargets[j] != balancer) {
 9429:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
 9430:                 item.value = alltargets[j];
 9431:                 item.style.textAlign='left';
 9432:                 item.style.textFace='normal';
 9433:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
 9434:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
 9435:                     item.disabled = '';
 9436:                 } else {
 9437:                     item.disabled = 'disabled';
 9438:                     item.checked = false;
 9439:                 }
 9440:                 count ++;
 9441:             }
 9442:         }
 9443:     }
 9444:     for (var k=0; k<insttypes.length; k++) {
 9445:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
 9446:             if (ishomedom == 1) {
 9447:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 9448:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 9449:             } else {
 9450:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 9451:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 9452:             }
 9453:         } else {
 9454:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 9455:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 9456:         }
 9457:         if ((insttypes[k] != '_LC_external') && 
 9458:             ((insttypes[k] != '_LC_internetdom') ||
 9459:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
 9460:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
 9461:             item.options.length = 0;
 9462:             item.options[0] = new Option("","",true,true);
 9463:             var idx = 0;
 9464:             for (var m=0; m<alltargets.length; m++) {
 9465:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
 9466:                     idx ++;
 9467:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 9468:                 }
 9469:             }
 9470:         }
 9471:     }
 9472:     return;
 9473: }
 9474: 
 9475: function hideSpares(balnum) {
 9476:     var alltargets = new Array('$alltargets');
 9477:     var insttypes = new Array('$allinsttypes');
 9478:     var offloadtypes = new Array('primary','default');
 9479: 
 9480:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
 9481:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
 9482: 
 9483:     var total = alltargets.length - 1;
 9484:     for (var i=0; i<offloadtypes; i++) {
 9485:         for (var j=0; j<total; j++) {
 9486:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
 9487:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
 9488:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
 9489:         }
 9490:     }
 9491:     for (var k=0; k<insttypes.length; k++) {
 9492:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 9493:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 9494:         if (insttypes[k] != '_LC_external') {
 9495:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
 9496:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
 9497:         }
 9498:     }
 9499:     return;
 9500: }
 9501: 
 9502: function checkOffloads(item,balnum,type) {
 9503:     var alltargets = new Array('$alltargets');
 9504:     var offloadtypes = new Array('primary','default');
 9505:     if (item.checked) {
 9506:         var total = alltargets.length - 1;
 9507:         var other;
 9508:         if (type == offloadtypes[0]) {
 9509:             other = offloadtypes[1];
 9510:         } else {
 9511:             other = offloadtypes[0];
 9512:         }
 9513:         for (var i=0; i<total; i++) {
 9514:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
 9515:             if (server == item.value) {
 9516:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
 9517:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
 9518:                 }
 9519:             }
 9520:         }
 9521:     }
 9522:     return;
 9523: }
 9524: 
 9525: function singleServerToggle(balnum,type) {
 9526:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
 9527:     if (offloadtoSelIdx == 0) {
 9528:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
 9529:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 9530: 
 9531:     } else {
 9532:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
 9533:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 9534:     }
 9535:     return;
 9536: }
 9537: 
 9538: function balanceruleChange(formname,balnum,type) {
 9539:     if (type == '_LC_external') {
 9540:         return;
 9541:     }
 9542:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
 9543:     for (var i=0; i<typesRules.length; i++) {
 9544:         if (formname.elements[typesRules[i]].checked) {
 9545:             if (formname.elements[typesRules[i]].value != 'specific') {
 9546:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
 9547:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 9548:             } else {
 9549:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 9550:             }
 9551:         }
 9552:     }
 9553:     return;
 9554: }
 9555: 
 9556: function balancerDeleteChange(balnum) {
 9557:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 9558:     var baltotal = document.getElementById('loadbalancing_total').value;
 9559:     var addtarget;
 9560:     var removetarget;
 9561:     var action = 'delete';
 9562:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
 9563:         var lonhost = hostitem.value;
 9564:         var currIdx = currBalancers.indexOf(lonhost);
 9565:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
 9566:             if (currIdx != -1) {
 9567:                 currBalancers.splice(currIdx,1);
 9568:             }
 9569:             addtarget = lonhost;
 9570:         } else {
 9571:             if (currIdx == -1) {
 9572:                 currBalancers.push(lonhost);
 9573:             }
 9574:             removetarget = lonhost;
 9575:             action = 'undelete';
 9576:         }
 9577:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
 9578:     }
 9579:     return;
 9580: }
 9581: 
 9582: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
 9583:     if (baltotal > 1) {
 9584:         var offloadtypes = new Array('primary','default');
 9585:         var alltargets = new Array('$alltargets');
 9586:         var insttypes = new Array('$allinsttypes');
 9587:         for (var i=0; i<baltotal; i++) {
 9588:             if (i != balnum) {
 9589:                 for (var j=0; j<offloadtypes.length; j++) {
 9590:                     var total = alltargets.length - 1;
 9591:                     for (var k=0; k<total; k++) {
 9592:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
 9593:                         var server = serveritem.value;
 9594:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
 9595:                             if (server == addtarget) {
 9596:                                 serveritem.disabled = '';
 9597:                             }
 9598:                         }
 9599:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 9600:                             if (server == removetarget) {
 9601:                                 serveritem.disabled = 'disabled';
 9602:                                 serveritem.checked = false;
 9603:                             }
 9604:                         }
 9605:                     }
 9606:                 }
 9607:                 for (var j=0; j<insttypes.length; j++) {
 9608:                     if (insttypes[j] != '_LC_external') {
 9609:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
 9610:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
 9611:                             var currSel = singleserver.selectedIndex;
 9612:                             var currVal = singleserver.options[currSel].value;
 9613:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
 9614:                                 var numoptions = singleserver.options.length;
 9615:                                 var needsnew = 1;
 9616:                                 for (var k=0; k<numoptions; k++) {
 9617:                                     if (singleserver.options[k] == addtarget) {
 9618:                                         needsnew = 0;
 9619:                                         break;
 9620:                                     }
 9621:                                 }
 9622:                                 if (needsnew == 1) {
 9623:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
 9624:                                 }
 9625:                             }
 9626:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 9627:                                 singleserver.options.length = 0;
 9628:                                 if ((currVal) && (currVal != removetarget)) {
 9629:                                     singleserver.options[0] = new Option("","",false,false);
 9630:                                 } else {
 9631:                                     singleserver.options[0] = new Option("","",true,true);
 9632:                                 }
 9633:                                 var idx = 0;
 9634:                                 for (var m=0; m<alltargets.length; m++) {
 9635:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
 9636:                                         idx ++;
 9637:                                         if (currVal == alltargets[m]) {
 9638:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
 9639:                                         } else {
 9640:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 9641:                                         }
 9642:                                     }
 9643:                                 }
 9644:                             }
 9645:                         }
 9646:                     }
 9647:                 }
 9648:             }
 9649:         }
 9650:     }
 9651:     return;
 9652: }
 9653: 
 9654: // ]]>
 9655: </script>
 9656: 
 9657: END
 9658: }
 9659: 
 9660: sub new_spares_js {
 9661:     my @sparestypes = ('primary','default');
 9662:     my $types = join("','",@sparestypes);
 9663:     my $select = &mt('Select');
 9664:     return <<"END";
 9665: 
 9666: <script type="text/javascript">
 9667: // <![CDATA[
 9668: 
 9669: function updateNewSpares(formname,lonhost) {
 9670:     var types = new Array('$types');
 9671:     var include = new Array();
 9672:     var exclude = new Array();
 9673:     for (var i=0; i<types.length; i++) {
 9674:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
 9675:         for (var j=0; j<spareboxes.length; j++) {
 9676:             if (formname.elements[spareboxes[j]].checked) {
 9677:                 exclude.push(formname.elements[spareboxes[j]].value);
 9678:             } else {
 9679:                 include.push(formname.elements[spareboxes[j]].value);
 9680:             }
 9681:         }
 9682:     }
 9683:     for (var i=0; i<types.length; i++) {
 9684:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
 9685:         var selIdx = newSpare.selectedIndex;
 9686:         var currnew = newSpare.options[selIdx].value;
 9687:         var okSpares = new Array();
 9688:         for (var j=0; j<newSpare.options.length; j++) {
 9689:             var possible = newSpare.options[j].value;
 9690:             if (possible != '') {
 9691:                 if (exclude.indexOf(possible) == -1) {
 9692:                     okSpares.push(possible);
 9693:                 } else {
 9694:                     if (currnew == possible) {
 9695:                         selIdx = 0;
 9696:                     }
 9697:                 }
 9698:             }
 9699:         }
 9700:         for (var k=0; k<include.length; k++) {
 9701:             if (okSpares.indexOf(include[k]) == -1) {
 9702:                 okSpares.push(include[k]);
 9703:             }
 9704:         }
 9705:         okSpares.sort();
 9706:         newSpare.options.length = 0;
 9707:         if (selIdx == 0) {
 9708:             newSpare.options[0] = new Option("$select","",true,true);
 9709:         } else {
 9710:             newSpare.options[0] = new Option("$select","",false,false);
 9711:         }
 9712:         for (var m=0; m<okSpares.length; m++) {
 9713:             var idx = m+1;
 9714:             var selThis = 0;
 9715:             if (selIdx != 0) {
 9716:                 if (okSpares[m] == currnew) {
 9717:                     selThis = 1;
 9718:                 }
 9719:             }
 9720:             if (selThis == 1) {
 9721:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
 9722:             } else {
 9723:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
 9724:             }
 9725:         }
 9726:     }
 9727:     return;
 9728: }
 9729: 
 9730: function checkNewSpares(lonhost,type) {
 9731:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
 9732:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
 9733:     if (chosen != '') { 
 9734:         var othertype;
 9735:         var othernewSpare;
 9736:         if (type == 'primary') {
 9737:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
 9738:         }
 9739:         if (type == 'default') {
 9740:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
 9741:         }
 9742:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
 9743:             othernewSpare.selectedIndex = 0;
 9744:         }
 9745:     }
 9746:     return;
 9747: }
 9748: 
 9749: // ]]>
 9750: </script>
 9751: 
 9752: END
 9753: 
 9754: }
 9755: 
 9756: sub common_domprefs_js {
 9757:     return <<"END";
 9758: 
 9759: <script type="text/javascript">
 9760: // <![CDATA[
 9761: 
 9762: function getIndicesByName(formname,item) {
 9763:     var group = new Array();
 9764:     for (var i=0;i<formname.elements.length;i++) {
 9765:         if (formname.elements[i].name == item) {
 9766:             group.push(formname.elements[i].id);
 9767:         }
 9768:     }
 9769:     return group;
 9770: }
 9771: 
 9772: // ]]>
 9773: </script>
 9774: 
 9775: END
 9776: 
 9777: }
 9778: 
 9779: sub recaptcha_js {
 9780:     my %lt = &captcha_phrases();
 9781:     return <<"END";
 9782: 
 9783: <script type="text/javascript">
 9784: // <![CDATA[
 9785: 
 9786: function updateCaptcha(caller,context) {
 9787:     var privitem;
 9788:     var pubitem;
 9789:     var privtext;
 9790:     var pubtext;
 9791:     if (document.getElementById(context+'_recaptchapub')) {
 9792:         pubitem = document.getElementById(context+'_recaptchapub');
 9793:     } else {
 9794:         return;
 9795:     }
 9796:     if (document.getElementById(context+'_recaptchapriv')) {
 9797:         privitem = document.getElementById(context+'_recaptchapriv');
 9798:     } else {
 9799:         return;
 9800:     }
 9801:     if (document.getElementById(context+'_recaptchapubtxt')) {
 9802:         pubtext = document.getElementById(context+'_recaptchapubtxt');
 9803:     } else {
 9804:         return;
 9805:     }
 9806:     if (document.getElementById(context+'_recaptchaprivtxt')) {
 9807:         privtext = document.getElementById(context+'_recaptchaprivtxt');
 9808:     } else {
 9809:         return;
 9810:     }
 9811:     if (caller.checked) {
 9812:         if (caller.value == 'recaptcha') {
 9813:             pubitem.type = 'text';
 9814:             privitem.type = 'text';
 9815:             pubitem.size = '40';
 9816:             privitem.size = '40';
 9817:             pubtext.innerHTML = "$lt{'pub'}";
 9818:             privtext.innerHTML = "$lt{'priv'}";
 9819:         } else {
 9820:             pubitem.type = 'hidden';
 9821:             privitem.type = 'hidden';
 9822:             pubtext.innerHTML = '';
 9823:             privtext.innerHTML = '';
 9824:         }
 9825:     }
 9826:     return;
 9827: }
 9828: 
 9829: // ]]>
 9830: </script>
 9831: 
 9832: END
 9833: 
 9834: }
 9835: 
 9836: sub credits_js {
 9837:     return <<"END";
 9838: 
 9839: <script type="text/javascript">
 9840: // <![CDATA[
 9841: 
 9842: function toggleCredits(domForm) {
 9843:     if (document.getElementById('credits')) {
 9844:         creditsitem = document.getElementById('credits');
 9845:         var creditsLength = domForm.coursecredits.length;
 9846:         if (creditsLength) {
 9847:             var currval;
 9848:             for (var i=0; i<creditsLength; i++) {
 9849:                 if (domForm.coursecredits[i].checked) {
 9850:                    currval = domForm.coursecredits[i].value;
 9851:                 }
 9852:             }
 9853:             if (currval == 1) {
 9854:                 creditsitem.style.display = 'block';
 9855:             } else {
 9856:                 creditsitem.style.display = 'none';
 9857:             }
 9858:         }
 9859:     }
 9860:     return;
 9861: }
 9862: 
 9863: // ]]>
 9864: </script>
 9865: 
 9866: END
 9867: 
 9868: }
 9869: 
 9870: sub captcha_phrases {
 9871:     return &Apache::lonlocal::texthash (
 9872:                  priv => 'Private key',
 9873:                  pub  => 'Public key',
 9874:                  original  => 'original (CAPTCHA)',
 9875:                  recaptcha => 'successor (ReCAPTCHA)',
 9876:                  notused   => 'unused',
 9877:     );
 9878: }
 9879: 
 9880: sub devalidate_remote_domconfs {
 9881:     my ($dom,$cachekeys) = @_;
 9882:     return unless (ref($cachekeys) eq 'HASH');
 9883:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9884:     my %thismachine;
 9885:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
 9886:     my @posscached = ('domainconfig','domdefaults');
 9887:     if (keys(%servers) > 1) {
 9888:         foreach my $server (keys(%servers)) {
 9889:             next if ($thismachine{$server});
 9890:             my @cached;
 9891:             foreach my $name (@posscached) {
 9892:                 if ($cachekeys->{$name}) {
 9893:                     push(@cached,&escape($name).':'.&escape($dom));
 9894:                 }
 9895:             }
 9896:             if (@cached) {
 9897:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
 9898:             }
 9899:         }
 9900:     }
 9901:     return;
 9902: }
 9903: 
 9904: 1;

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