File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.160.6.36: download - view: text, annotated - select for diffs
Tue Mar 18 02:41:29 2014 UTC (10 years, 2 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.160: preferred, unified
- For 2.11
  - Backport 1.229

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.160.6.36 2014/03/18 02:41:29 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','selfcreation','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 => 'Updatable 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:         'usercreation' => 
  297:                   { text => 'User creation',
  298:                     help => 'Domain_Configuration_User_Creation',
  299:                     header => [{col1 => 'Format rule type',
  300:                                 col2 => 'Format rules in force'},
  301:                                {col1 => 'User account creation',
  302:                                 col2 => 'Usernames which may be created',},
  303:                                {col1 => 'Context',
  304:                                 col2 => 'Assignable authentication types'}],
  305:                   },
  306:         'selfcreation' => 
  307:                   { text => 'Users self-creating accounts',
  308:                     help => 'Domain_Configuration_Self_Creation', 
  309:                     header => [{col1 => 'Self-creation with institutional username',
  310:                                 col2 => 'Enabled?'},
  311:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  312:                                 col2 => 'Information user can enter'},
  313:                                {col1 => 'Self-creation with e-mail as username',
  314:                                 col2 => 'Settings'}],
  315:                   },
  316:         'usermodification' =>
  317:                   { text => 'User modification',
  318:                     help => 'Domain_Configuration_User_Modification',
  319:                     header => [{col1 => 'Target user has role',
  320:                                 col2 => 'User information updatable in author context'},
  321:                                {col1 => 'Target user has role',
  322:                                 col2 => 'User information updatable in course context'}],
  323:                   },
  324:         'scantron' =>
  325:                   { text => 'Bubblesheet format file',
  326:                     help => 'Domain_Configuration_Scantron_Format',
  327:                     header => [ {col1 => 'Item',
  328:                                  col2 => '',
  329:                               }],
  330:                   },
  331:         'requestcourses' => 
  332:                  {text => 'Request creation of courses',
  333:                   help => 'Domain_Configuration_Request_Courses',
  334:                   header => [{col1 => 'User affiliation',
  335:                               col2 => 'Availability/Processing of requests',},
  336:                              {col1 => 'Setting',
  337:                               col2 => 'Value'},
  338:                              {col1 => 'Available textbooks',
  339:                               col2 => ''}],
  340:                  },
  341:         'requestauthor' =>
  342:                  {text => 'Request Authoring Space',
  343:                   help => 'Domain_Configuration_Request_Author',
  344:                   header => [{col1 => 'User affiliation',
  345:                               col2 => 'Availability/Processing of requests',},
  346:                              {col1 => 'Setting',
  347:                               col2 => 'Value'}],
  348:                  },
  349:         'coursecategories' =>
  350:                   { text => 'Cataloging of courses/communities',
  351:                     help => 'Domain_Configuration_Cataloging_Courses',
  352:                     header => [{col1 => 'Category settings',
  353:                                 col2 => '',},
  354:                                {col1 => 'Categories',
  355:                                 col2 => '',
  356:                                }],
  357:                   },
  358:         'serverstatuses' =>
  359:                  {text   => 'Access to server status pages',
  360:                   help   => 'Domain_Configuration_Server_Status',
  361:                   header => [{col1 => 'Status Page',
  362:                               col2 => 'Other named users',
  363:                               col3 => 'Specific IPs',
  364:                             }],
  365:                  },
  366:         'coursedefaults' =>
  367:                  {text => 'Course/Community defaults',
  368:                   help => 'Domain_Configuration_Course_Defaults',
  369:                   header => [{col1 => 'Defaults which can be overridden for each course by a DC',
  370:                               col2 => 'Value',},],
  371:                  },
  372:         'usersessions' =>
  373:                  {text  => 'User session hosting/offloading',
  374:                   help  => 'Domain_Configuration_User_Sessions',
  375:                   header => [{col1 => 'Domain server',
  376:                               col2 => 'Servers to offload sessions to when busy'},
  377:                              {col1 => 'Hosting of users from other domains',
  378:                               col2 => 'Rules'},
  379:                              {col1 => "Hosting domain's own users elsewhere",
  380:                               col2 => 'Rules'}],
  381:                  },
  382:          'loadbalancing' =>
  383:                  {text  => 'Dedicated Load Balancer(s)',
  384:                   help  => 'Domain_Configuration_Load_Balancing',
  385:                   header => [{col1 => 'Balancers',
  386:                               col2 => 'Default destinations',
  387:                               col3 => 'User affiliation',
  388:                               col4 => 'Overrides'},
  389:                             ],
  390:                  },
  391:     );
  392:     if (keys(%servers) > 1) {
  393:         $prefs{'login'}  = { text   => 'Log-in page options',
  394:                              help   => 'Domain_Configuration_Login_Page',
  395:                             header => [{col1 => 'Log-in Service',
  396:                                         col2 => 'Server Setting',},
  397:                                        {col1 => 'Log-in Page Items',
  398:                                         col2 => ''},
  399:                                        {col1 => 'Log-in Help',
  400:                                         col2 => 'Value'}],
  401:                            };
  402:     }
  403: 
  404:     my @roles = ('student','coordinator','author','admin');
  405:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  406:     &Apache::lonhtmlcommon::add_breadcrumb
  407:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  408:       text=>"Settings to display/modify"});
  409:     my $confname = $dom.'-domainconfig';
  410: 
  411:     if ($phase eq 'process') {
  412:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  413:                                                               \%prefs,\%domconfig,$confname,\@roles);
  414:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  415:             $r->rflush();
  416:             &devalidate_remote_domconfs($dom,$result);
  417:         }
  418:     } elsif ($phase eq 'display') {
  419:         my $js = &recaptcha_js().
  420:                  &credits_js();
  421:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  422:             my ($othertitle,$usertypes,$types) =
  423:                 &Apache::loncommon::sorted_inst_types($dom);
  424:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  425:                                           $domconfig{'loadbalancing'}).
  426:                    &new_spares_js().
  427:                    &common_domprefs_js().
  428:                    &Apache::loncommon::javascript_array_indexof();
  429:         }
  430:         if (grep(/^requestcourses$/,@actions)) {
  431:             my $javascript_validations;
  432:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  433:             $js .= <<END;
  434: <script type="text/javascript">
  435: $javascript_validations
  436: </script>
  437: $coursebrowserjs
  438: END
  439:         }
  440:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  441:     } else {
  442: # check if domconfig user exists for the domain.
  443:         my $servadm = $r->dir_config('lonAdmEMail');
  444:         my ($configuserok,$author_ok,$switchserver) =
  445:             &config_check($dom,$confname,$servadm);
  446:         unless ($configuserok eq 'ok') {
  447:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  448:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  449:                           $confname).
  450:                       '<br />'
  451:             );
  452:             if ($switchserver) {
  453:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  454:                           '<br />'.
  455:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  456:                           '<br />'.
  457:                           &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).
  458:                           '<br />'.
  459:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  460:                 );
  461:             } else {
  462:                 $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.').
  463:                           '<br />'.
  464:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  465:                 );
  466:             }
  467:             $r->print(&Apache::loncommon::end_page());
  468:             return OK;
  469:         }
  470:         if (keys(%domconfig) == 0) {
  471:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  472:             my @ids=&Apache::lonnet::current_machine_ids();
  473:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  474:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  475:                 my @loginimages = ('img','logo','domlogo','login');
  476:                 my $custom_img_count = 0;
  477:                 foreach my $img (@loginimages) {
  478:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  479:                         $custom_img_count ++;
  480:                     }
  481:                 }
  482:                 foreach my $role (@roles) {
  483:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  484:                         $custom_img_count ++;
  485:                     }
  486:                 }
  487:                 if ($custom_img_count > 0) {
  488:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  489:                     my $switch_server = &check_switchserver($dom,$confname);
  490:                     $r->print(
  491:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  492:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  493:     &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 />'.
  494:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  495:                     if ($switch_server) {
  496:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  497:                     }
  498:                     $r->print(&Apache::loncommon::end_page());
  499:                     return OK;
  500:                 }
  501:             }
  502:         }
  503:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  504:     }
  505:     return OK;
  506: }
  507: 
  508: sub process_changes {
  509:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  510:     my %domconfig;
  511:     if (ref($values) eq 'HASH') {
  512:         %domconfig = %{$values};
  513:     }
  514:     my $output;
  515:     if ($action eq 'login') {
  516:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  517:     } elsif ($action eq 'rolecolors') {
  518:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  519:                                      $lastactref,%domconfig);
  520:     } elsif ($action eq 'quotas') {
  521:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  522:     } elsif ($action eq 'autoenroll') {
  523:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  524:     } elsif ($action eq 'autoupdate') {
  525:         $output = &modify_autoupdate($dom,%domconfig);
  526:     } elsif ($action eq 'autocreate') {
  527:         $output = &modify_autocreate($dom,%domconfig);
  528:     } elsif ($action eq 'directorysrch') {
  529:         $output = &modify_directorysrch($dom,%domconfig);
  530:     } elsif ($action eq 'usercreation') {
  531:         $output = &modify_usercreation($dom,%domconfig);
  532:     } elsif ($action eq 'selfcreation') {
  533:         $output = &modify_selfcreation($dom,%domconfig);
  534:     } elsif ($action eq 'usermodification') {
  535:         $output = &modify_usermodification($dom,%domconfig);
  536:     } elsif ($action eq 'contacts') {
  537:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  538:     } elsif ($action eq 'defaults') {
  539:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  540:     } elsif ($action eq 'scantron') {
  541:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  542:     } elsif ($action eq 'coursecategories') {
  543:         $output = &modify_coursecategories($dom,%domconfig);
  544:     } elsif ($action eq 'serverstatuses') {
  545:         $output = &modify_serverstatuses($dom,%domconfig);
  546:     } elsif ($action eq 'requestcourses') {
  547:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  548:     } elsif ($action eq 'requestauthor') {
  549:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  550:     } elsif ($action eq 'coursedefaults') {
  551:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  552:     } elsif ($action eq 'usersessions') {
  553:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  554:     } elsif ($action eq 'loadbalancing') {
  555:         $output = &modify_loadbalancing($dom,%domconfig);
  556:     }
  557:     return $output;
  558: }
  559: 
  560: sub print_config_box {
  561:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  562:     my $rowtotal = 0;
  563:     my $output;
  564:     if ($action eq 'coursecategories') {
  565:         $output = &coursecategories_javascript($settings);
  566:     }
  567:     $output .= 
  568:          '<table class="LC_nested_outer">
  569:           <tr>
  570:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  571:            &mt($item->{text}).'&nbsp;'.
  572:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  573:           '</tr>';
  574:     $rowtotal ++;
  575:     my $numheaders = 1;
  576:     if (ref($item->{'header'}) eq 'ARRAY') {
  577:         $numheaders = scalar(@{$item->{'header'}});
  578:     }
  579:     if ($numheaders > 1) {
  580:         my $colspan = '';
  581:         my $rightcolspan = '';
  582:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') ||
  583:             (($action eq 'login') && ($numheaders < 3))) {
  584:             $colspan = ' colspan="2"';
  585:         }
  586:         if ($action eq 'usersessions') {
  587:             $rightcolspan = ' colspan="3"'; 
  588:         }
  589:         $output .= '
  590:           <tr>
  591:            <td>
  592:             <table class="LC_nested">
  593:              <tr class="LC_info_row">
  594:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  595:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  596:              </tr>';
  597:         $rowtotal ++;
  598:         if ($action eq 'autoupdate') {
  599:             $output .= &print_autoupdate('top',$dom,$settings,\$rowtotal);
  600:         } elsif ($action eq 'usercreation') {
  601:             $output .= &print_usercreation('top',$dom,$settings,\$rowtotal);
  602:         } elsif ($action eq 'selfcreation') {
  603:             $output .= &print_selfcreation('top',$dom,$settings,\$rowtotal);
  604:         } elsif ($action eq 'usermodification') {
  605:             $output .= &print_usermodification('top',$dom,$settings,\$rowtotal);
  606:         } elsif ($action eq 'coursecategories') {
  607:             $output .= &print_coursecategories('top',$dom,$item,$settings,\$rowtotal);
  608:         } elsif ($action eq 'login') {
  609:             if ($numheaders == 3) {
  610:                 $colspan = ' colspan="2"';
  611:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  612:             } else {
  613:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  614:             }
  615:         } elsif ($action eq 'requestcourses') {
  616:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  617:         } elsif ($action eq 'requestauthor') {
  618:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  619:         } elsif ($action eq 'usersessions') {
  620:             $output .= &print_usersessions('top',$dom,$settings,\$rowtotal); 
  621:         } elsif ($action eq 'rolecolors') {
  622:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  623:         }
  624:         $output .= '
  625:            </table>
  626:           </td>
  627:          </tr>
  628:          <tr>
  629:            <td>
  630:             <table class="LC_nested">
  631:              <tr class="LC_info_row">
  632:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>';
  633:         $output .= '
  634:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  635:              </tr>';
  636:             $rowtotal ++;
  637:         if ($action eq 'autoupdate') {
  638:             $output .= &print_autoupdate('middle',$dom,$settings,\$rowtotal).'
  639:            </table>
  640:           </td>
  641:          </tr>
  642:          <tr>
  643:            <td>
  644:             <table class="LC_nested">
  645:              <tr class="LC_info_row">
  646:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  647:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  648:             &print_autoupdate('bottom',$dom,$settings,\$rowtotal);
  649:             $rowtotal ++;
  650:         } elsif ($action eq 'usercreation') {
  651:             $output .= &print_usercreation('middle',$dom,$settings,\$rowtotal).'
  652:            </table>
  653:           </td>
  654:          </tr>
  655:          <tr>
  656:            <td>
  657:             <table class="LC_nested">
  658:              <tr class="LC_info_row">
  659:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  660:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>             </tr>'.
  661:             &print_usercreation('bottom',$dom,$settings,\$rowtotal);
  662:             $rowtotal ++;
  663:         } elsif ($action eq 'selfcreation') {
  664:             $output .= &print_selfcreation('middle',$dom,$settings,\$rowtotal).'
  665:            </table>
  666:           </td>
  667:          </tr>
  668:          <tr>
  669:            <td>
  670:             <table class="LC_nested">
  671:              <tr class="LC_info_row">
  672:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  673:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  674:              </tr>'.
  675:             &print_selfcreation('bottom',$dom,$settings,\$rowtotal);
  676:             $rowtotal ++;
  677:         } elsif ($action eq 'usermodification') {
  678:             $output .= &print_usermodification('middle',$dom,$settings,\$rowtotal);
  679:         } elsif ($action eq 'coursecategories') {
  680:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  681:         } elsif ($action eq 'login') {
  682:             if ($numheaders == 3) {
  683:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  684:            </table>
  685:           </td>
  686:          </tr>
  687:          <tr>
  688:            <td>
  689:             <table class="LC_nested">
  690:              <tr class="LC_info_row">
  691:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  692:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  693:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  694:                 $rowtotal ++;
  695:             } else {
  696:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  697:             }
  698:         } elsif ($action eq 'requestcourses') {
  699:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal).
  700:                        &print_studentcode($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_textbookcourses($dom,$settings,\$rowtotal);
  711:         } elsif ($action eq 'requestauthor') {
  712:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  713:         } elsif ($action eq 'usersessions') {
  714:             $output .= &print_usersessions('middle',$dom,$settings,\$rowtotal).'
  715:            </table>
  716:           </td>
  717:          </tr>
  718:          <tr>
  719:            <td>
  720:             <table class="LC_nested">
  721:              <tr class="LC_info_row">
  722:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  723:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  724:                        &print_usersessions('bottom',$dom,$settings,\$rowtotal);
  725:             $rowtotal ++;
  726:         } elsif ($action eq 'rolecolors') {
  727:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  728:            </table>
  729:           </td>
  730:          </tr>
  731:          <tr>
  732:            <td>
  733:             <table class="LC_nested">
  734:              <tr class="LC_info_row">
  735:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  736:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  737:               <td class="LC_right_item" valign="top">'.
  738:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  739:              </tr>'.
  740:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  741:            </table>
  742:           </td>
  743:          </tr>
  744:          <tr>
  745:            <td>
  746:             <table class="LC_nested">
  747:              <tr class="LC_info_row">
  748:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  749:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  750:              </tr>'.
  751:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  752:             $rowtotal += 2;
  753:         }
  754:     } else {
  755:         $output .= '
  756:           <tr>
  757:            <td>
  758:             <table class="LC_nested">
  759:              <tr class="LC_info_row">';
  760:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  761:             $output .= '  
  762:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  763:         } elsif ($action eq 'serverstatuses') {
  764:             $output .= '
  765:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  766:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  767: 
  768:         } else {
  769:             $output .= '
  770:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  771:         }
  772:         if (defined($item->{'header'}->[0]->{'col3'})) {
  773:             $output .= '<td class="LC_left_item" valign="top">'.
  774:                        &mt($item->{'header'}->[0]->{'col2'});
  775:             if ($action eq 'serverstatuses') {
  776:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  777:             } 
  778:         } else {
  779:             $output .= '<td class="LC_right_item" valign="top">'.
  780:                        &mt($item->{'header'}->[0]->{'col2'});
  781:         }
  782:         $output .= '</td>';
  783:         if ($item->{'header'}->[0]->{'col3'}) {
  784:             if (defined($item->{'header'}->[0]->{'col4'})) {
  785:                 $output .= '<td class="LC_left_item" valign="top">'.
  786:                             &mt($item->{'header'}->[0]->{'col3'});
  787:             } else {
  788:                 $output .= '<td class="LC_right_item" valign="top">'.
  789:                            &mt($item->{'header'}->[0]->{'col3'});
  790:             }
  791:             if ($action eq 'serverstatuses') {
  792:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  793:             }
  794:             $output .= '</td>';
  795:         }
  796:         if ($item->{'header'}->[0]->{'col4'}) {
  797:             $output .= '<td class="LC_right_item" valign="top">'.
  798:                        &mt($item->{'header'}->[0]->{'col4'});
  799:         }
  800:         $output .= '</tr>';
  801:         $rowtotal ++;
  802:         if ($action eq 'quotas') {
  803:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  804:         } elsif ($action eq 'autoenroll') {
  805:             $output .= &print_autoenroll($dom,$settings,\$rowtotal);
  806:         } elsif ($action eq 'autocreate') {
  807:             $output .= &print_autocreate($dom,$settings,\$rowtotal);
  808:         } elsif ($action eq 'directorysrch') {
  809:             $output .= &print_directorysrch($dom,$settings,\$rowtotal);
  810:         } elsif ($action eq 'contacts') {
  811:             $output .= &print_contacts($dom,$settings,\$rowtotal);
  812:         } elsif ($action eq 'defaults') {
  813:             $output .= &print_defaults($dom,$settings,\$rowtotal);
  814:         } elsif ($action eq 'scantron') {
  815:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  816:         } elsif ($action eq 'serverstatuses') {
  817:             $output .= &print_serverstatuses($dom,$settings,\$rowtotal);
  818:         } elsif ($action eq 'helpsettings') {
  819:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  820:         } elsif ($action eq 'loadbalancing') {
  821:             $output .= &print_loadbalancing($dom,$settings,\$rowtotal);
  822:         } elsif ($action eq 'coursedefaults') {
  823:             $output .= &print_coursedefaults('bottom',$dom,$settings,\$rowtotal);
  824:         }
  825:     }
  826:     $output .= '
  827:    </table>
  828:   </td>
  829:  </tr>
  830: </table><br />';
  831:     return ($output,$rowtotal);
  832: }
  833: 
  834: sub print_login {
  835:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  836:     my ($css_class,$datatable);
  837:     my %choices = &login_choices();
  838: 
  839:     if ($caller eq 'service') {
  840:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  841:         my $choice = $choices{'disallowlogin'};
  842:         $css_class = ' class="LC_odd_row"';
  843:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  844:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  845:                       '<th>'.$choices{'server'}.'</th>'.
  846:                       '<th>'.$choices{'serverpath'}.'</th>'.
  847:                       '<th>'.$choices{'custompath'}.'</th>'.
  848:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  849:         my %disallowed;
  850:         if (ref($settings) eq 'HASH') {
  851:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  852:                %disallowed = %{$settings->{'loginvia'}};
  853:             }
  854:         }
  855:         foreach my $lonhost (sort(keys(%servers))) {
  856:             my $direct = 'selected="selected"';
  857:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  858:                 if ($disallowed{$lonhost}{'server'} ne '') {
  859:                     $direct = '';
  860:                 }
  861:             }
  862:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  863:                           '<td><select name="'.$lonhost.'_server">'.
  864:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  865:                           '</option>';
  866:             foreach my $hostid (sort(keys(%servers))) {
  867:                 next if ($servers{$hostid} eq $servers{$lonhost});
  868:                 my $selected = '';
  869:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  870:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  871:                         $selected = 'selected="selected"';
  872:                     }
  873:                 }
  874:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  875:                               $servers{$hostid}.'</option>';
  876:             }
  877:             $datatable .= '</select></td>'.
  878:                           '<td><select name="'.$lonhost.'_serverpath">';
  879:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  880:                 my $pathname = $path;
  881:                 if ($path eq 'custom') {
  882:                     $pathname = &mt('Custom Path').' ->';
  883:                 }
  884:                 my $selected = '';
  885:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  886:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  887:                         $selected = 'selected="selected"';
  888:                     }
  889:                 } elsif ($path eq '') {
  890:                     $selected = 'selected="selected"';
  891:                 }
  892:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  893:             }
  894:             $datatable .= '</select></td>';
  895:             my ($custom,$exempt);
  896:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  897:                 $custom = $disallowed{$lonhost}{'custompath'};
  898:                 $exempt = $disallowed{$lonhost}{'exempt'};
  899:             }
  900:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  901:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  902:                           '</tr>';
  903:         }
  904:         $datatable .= '</table></td></tr>';
  905:         return $datatable;
  906:     } elsif ($caller eq 'page') {
  907:         my %defaultchecked = ( 
  908:                                'coursecatalog' => 'on',
  909:                                'helpdesk'      => 'on',
  910:                                'adminmail'     => 'off',
  911:                                'newuser'       => 'off',
  912:                              );
  913:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  914:         my (%checkedon,%checkedoff);
  915:         foreach my $item (@toggles) {
  916:             if ($defaultchecked{$item} eq 'on') { 
  917:                 $checkedon{$item} = ' checked="checked" ';
  918:                 $checkedoff{$item} = ' ';
  919:             } elsif ($defaultchecked{$item} eq 'off') {
  920:                 $checkedoff{$item} = ' checked="checked" ';
  921:                 $checkedon{$item} = ' ';
  922:             }
  923:         }
  924:         my @images = ('img','logo','domlogo','login');
  925:         my @logintext = ('textcol','bgcol');
  926:         my @bgs = ('pgbg','mainbg','sidebg');
  927:         my @links = ('link','alink','vlink');
  928:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  929:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  930:         my (%is_custom,%designs);
  931:         my %defaults = (
  932:                        font => $defaultdesign{'login.font'},
  933:                        );
  934:         foreach my $item (@images) {
  935:             $defaults{$item} = $defaultdesign{'login.'.$item};
  936:             $defaults{'showlogo'}{$item} = 1;
  937:         }
  938:         foreach my $item (@bgs) {
  939:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  940:         }
  941:         foreach my $item (@logintext) {
  942:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  943:         }
  944:         foreach my $item (@links) {
  945:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  946:         }
  947:         if (ref($settings) eq 'HASH') {
  948:             foreach my $item (@toggles) {
  949:                 if ($settings->{$item} eq '1') {
  950:                     $checkedon{$item} =  ' checked="checked" ';
  951:                     $checkedoff{$item} = ' ';
  952:                 } elsif ($settings->{$item} eq '0') {
  953:                     $checkedoff{$item} =  ' checked="checked" ';
  954:                     $checkedon{$item} = ' ';
  955:                 }
  956:             }
  957:             foreach my $item (@images) {
  958:                 if (defined($settings->{$item})) {
  959:                     $designs{$item} = $settings->{$item};
  960:                     $is_custom{$item} = 1;
  961:                 }
  962:                 if (defined($settings->{'showlogo'}{$item})) {
  963:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  964:                 }
  965:             }
  966:             foreach my $item (@logintext) {
  967:                 if ($settings->{$item} ne '') {
  968:                     $designs{'logintext'}{$item} = $settings->{$item};
  969:                     $is_custom{$item} = 1;
  970:                 }
  971:             }
  972:             if ($settings->{'font'} ne '') {
  973:                 $designs{'font'} = $settings->{'font'};
  974:                 $is_custom{'font'} = 1;
  975:             }
  976:             foreach my $item (@bgs) {
  977:                 if ($settings->{$item} ne '') {
  978:                     $designs{'bgs'}{$item} = $settings->{$item};
  979:                     $is_custom{$item} = 1;
  980:                 }
  981:             }
  982:             foreach my $item (@links) {
  983:                 if ($settings->{$item} ne '') {
  984:                     $designs{'links'}{$item} = $settings->{$item};
  985:                     $is_custom{$item} = 1;
  986:                 }
  987:             }
  988:         } else {
  989:             if ($designhash{$dom.'.login.font'} ne '') {
  990:                 $designs{'font'} = $designhash{$dom.'.login.font'};
  991:                 $is_custom{'font'} = 1;
  992:             }
  993:             foreach my $item (@images) {
  994:                 if ($designhash{$dom.'.login.'.$item} ne '') {
  995:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
  996:                     $is_custom{$item} = 1;
  997:                 }
  998:             }
  999:             foreach my $item (@bgs) {
 1000:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1001:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1002:                     $is_custom{$item} = 1;
 1003:                 }
 1004:             }
 1005:             foreach my $item (@links) {
 1006:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1007:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1008:                     $is_custom{$item} = 1;
 1009:                 }
 1010:             }
 1011:         }
 1012:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1013:                                                       logo => 'Institution Logo',
 1014:                                                       domlogo => 'Domain Logo',
 1015:                                                       login => 'Login box');
 1016:         my $itemcount = 1;
 1017:         foreach my $item (@toggles) {
 1018:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1019:             $datatable .=  
 1020:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1021:                 '</td><td>'.
 1022:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1023:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1024:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1025:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1026:                 '</tr>';
 1027:             $itemcount ++;
 1028:         }
 1029:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1030:         $datatable .= '</tr></table></td></tr>';
 1031:     } elsif ($caller eq 'help') {
 1032:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1033:         my $switchserver = &check_switchserver($dom,$confname);
 1034:         my $itemcount = 1;
 1035:         $defaulturl = '/adm/loginproblems.html';
 1036:         $defaulttype = 'default';
 1037:         %lt = &Apache::lonlocal::texthash (
 1038:                      del     => 'Delete?',
 1039:                      rep     => 'Replace:',
 1040:                      upl     => 'Upload:',
 1041:                      default => 'Default',
 1042:                      custom  => 'Custom',
 1043:                                              );
 1044:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1045:         my @currlangs;
 1046:         if (ref($settings) eq 'HASH') {
 1047:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1048:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1049:                     next if ($settings->{'helpurl'}{$key} eq '');
 1050:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1051:                     $type{$key} = 'custom';
 1052:                     unless ($key eq 'nolang') {
 1053:                         push(@currlangs,$key);
 1054:                     }
 1055:                 }
 1056:             } elsif ($settings->{'helpurl'} ne '') {
 1057:                 $type{'nolang'} = 'custom';
 1058:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1059:             }
 1060:         }
 1061:         foreach my $lang ('nolang',sort(@currlangs)) {
 1062:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1063:             $datatable .= '<tr'.$css_class.'>';
 1064:             if ($url{$lang} eq '') {
 1065:                 $url{$lang} = $defaulturl;
 1066:             }
 1067:             if ($type{$lang} eq '') {
 1068:                 $type{$lang} = $defaulttype;
 1069:             }
 1070:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1071:             if ($lang eq 'nolang') {
 1072:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1073:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1074:             } else {
 1075:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1076:                                   $langchoices{$lang},
 1077:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1078:             }
 1079:             $datatable .= '</span></td>'."\n".
 1080:                           '<td class="LC_left_item">';
 1081:             if ($type{$lang} eq 'custom') {
 1082:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1083:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1084:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1085:             } else {
 1086:                 $datatable .= $lt{'upl'};
 1087:             }
 1088:             $datatable .='<br />';
 1089:             if ($switchserver) {
 1090:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1091:             } else {
 1092:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1093:             }
 1094:             $datatable .= '</td></tr>';
 1095:             $itemcount ++;
 1096:         }
 1097:         my @addlangs;
 1098:         foreach my $lang (sort(keys(%langchoices))) {
 1099:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1100:             push(@addlangs,$lang);
 1101:         }
 1102:         if (@addlangs > 0) {
 1103:             my %toadd;
 1104:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1105:             $toadd{''} = &mt('Select');
 1106:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1107:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1108:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1109:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1110:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1111:             if ($switchserver) {
 1112:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1113:             } else {
 1114:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1115:             }
 1116:             $datatable .= '</td></tr>';
 1117:             $itemcount ++;
 1118:         }
 1119:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1120:     }
 1121:     return $datatable;
 1122: }
 1123: 
 1124: sub login_choices {
 1125:     my %choices =
 1126:         &Apache::lonlocal::texthash (
 1127:             coursecatalog => 'Display Course/Community Catalog link?',
 1128:             adminmail     => "Display Administrator's E-mail Address?",
 1129:             helpdesk      => 'Display "Contact Helpdesk" link',
 1130:             disallowlogin => "Login page requests redirected",
 1131:             hostid        => "Server",
 1132:             server        => "Redirect to:",
 1133:             serverpath    => "Path",
 1134:             custompath    => "Custom", 
 1135:             exempt        => "Exempt IP(s)",
 1136:             directlogin   => "No redirect",
 1137:             newuser       => "Link to create a user account",
 1138:             img           => "Header",
 1139:             logo          => "Main Logo",
 1140:             domlogo       => "Domain Logo",
 1141:             login         => "Log-in Header", 
 1142:             textcol       => "Text color",
 1143:             bgcol         => "Box color",
 1144:             bgs           => "Background colors",
 1145:             links         => "Link colors",
 1146:             font          => "Font color",
 1147:             pgbg          => "Header",
 1148:             mainbg        => "Page",
 1149:             sidebg        => "Login box",
 1150:             link          => "Link",
 1151:             alink         => "Active link",
 1152:             vlink         => "Visited link",
 1153:         );
 1154:     return %choices;
 1155: }
 1156: 
 1157: sub print_rolecolors {
 1158:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1159:     my %choices = &color_font_choices();
 1160:     my @bgs = ('pgbg','tabbg','sidebg');
 1161:     my @links = ('link','alink','vlink');
 1162:     my @images = ('img');
 1163:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1164:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1165:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1166:     my (%is_custom,%designs);
 1167:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1168:     if (ref($settings) eq 'HASH') {
 1169:         if (ref($settings->{$role}) eq 'HASH') {
 1170:             if ($settings->{$role}->{'img'} ne '') {
 1171:                 $designs{'img'} = $settings->{$role}->{'img'};
 1172:                 $is_custom{'img'} = 1;
 1173:             }
 1174:             if ($settings->{$role}->{'font'} ne '') {
 1175:                 $designs{'font'} = $settings->{$role}->{'font'};
 1176:                 $is_custom{'font'} = 1;
 1177:             }
 1178:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1179:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1180:                 $is_custom{'fontmenu'} = 1;
 1181:             }
 1182:             foreach my $item (@bgs) {
 1183:                 if ($settings->{$role}->{$item} ne '') {
 1184:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1185:                     $is_custom{$item} = 1;
 1186:                 }
 1187:             }
 1188:             foreach my $item (@links) {
 1189:                 if ($settings->{$role}->{$item} ne '') {
 1190:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1191:                     $is_custom{$item} = 1;
 1192:                 }
 1193:             }
 1194:         }
 1195:     } else {
 1196:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1197:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1198:             $is_custom{'img'} = 1;
 1199:         }
 1200:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1201:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1202:             $is_custom{'fontmenu'} = 1; 
 1203:         }
 1204:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1205:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1206:             $is_custom{'font'} = 1;
 1207:         }
 1208:         foreach my $item (@bgs) {
 1209:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1210:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1211:                 $is_custom{$item} = 1;
 1212:             
 1213:             }
 1214:         }
 1215:         foreach my $item (@links) {
 1216:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1217:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1218:                 $is_custom{$item} = 1;
 1219:             }
 1220:         }
 1221:     }
 1222:     my $itemcount = 1;
 1223:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1224:     $datatable .= '</tr></table></td></tr>';
 1225:     return $datatable;
 1226: }
 1227: 
 1228: sub role_defaults {
 1229:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1230:     my %defaults;
 1231:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1232:         return %defaults;
 1233:     }
 1234:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1235:     if ($role eq 'login') {
 1236:         %defaults = (
 1237:                        font => $defaultdesign{$role.'.font'},
 1238:                     );
 1239:         if (ref($logintext) eq 'ARRAY') {
 1240:             foreach my $item (@{$logintext}) {
 1241:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1242:             }
 1243:         }
 1244:         foreach my $item (@{$images}) {
 1245:             $defaults{'showlogo'}{$item} = 1;
 1246:         }
 1247:     } else {
 1248:         %defaults = (
 1249:                        img => $defaultdesign{$role.'.img'},
 1250:                        font => $defaultdesign{$role.'.font'},
 1251:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1252:                     );
 1253:     }
 1254:     foreach my $item (@{$bgs}) {
 1255:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1256:     }
 1257:     foreach my $item (@{$links}) {
 1258:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1259:     }
 1260:     foreach my $item (@{$images}) {
 1261:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1262:     }
 1263:     return %defaults;
 1264: }
 1265: 
 1266: sub display_color_options {
 1267:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1268:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1269:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1270:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1271:     my $datatable = '<tr'.$css_class.'>'.
 1272:         '<td>'.$choices->{'font'}.'</td>';
 1273:     if (!$is_custom->{'font'}) {
 1274:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1275:     } else {
 1276:         $datatable .= '<td>&nbsp;</td>';
 1277:     }
 1278:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1279: 
 1280:     $datatable .= '<td><span class="LC_nobreak">'.
 1281:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1282:                   ' value="'.$current_color.'" />&nbsp;'.
 1283:                   '&nbsp;</td></tr>';
 1284:     unless ($role eq 'login') { 
 1285:         $datatable .= '<tr'.$css_class.'>'.
 1286:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1287:         if (!$is_custom->{'fontmenu'}) {
 1288:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1289:         } else {
 1290:             $datatable .= '<td>&nbsp;</td>';
 1291:         }
 1292: 	$current_color = $designs->{'fontmenu'} ?
 1293: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1294:         $datatable .= '<td><span class="LC_nobreak">'.
 1295:                       '<input class="colorchooser" type="text" size="10" name="'
 1296: 		      .$role.'_fontmenu"'.
 1297:                       ' value="'.$current_color.'" />&nbsp;'.
 1298:                       '&nbsp;</td></tr>';
 1299:     }
 1300:     my $switchserver = &check_switchserver($dom,$confname);
 1301:     foreach my $img (@{$images}) {
 1302: 	$itemcount ++;
 1303:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1304:         $datatable .= '<tr'.$css_class.'>'.
 1305:                       '<td>'.$choices->{$img};
 1306:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1307:         if ($role eq 'login') {
 1308:             if ($img eq 'login') {
 1309:                 $login_hdr_pick =
 1310:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1311:                 $logincolors =
 1312:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1313:                                        $designs,$defaults);
 1314:             } elsif ($img ne 'domlogo') {
 1315:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1316:             }
 1317:         }
 1318:         $datatable .= '</td>';
 1319:         if ($designs->{$img} ne '') {
 1320:             $imgfile = $designs->{$img};
 1321: 	    $img_import = ($imgfile =~ m{^/adm/});
 1322:         } else {
 1323:             $imgfile = $defaults->{$img};
 1324:         }
 1325:         if ($imgfile) {
 1326:             my ($showfile,$fullsize);
 1327:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1328:                 my $urldir = $1;
 1329:                 my $filename = $2;
 1330:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1331:                 if (@info) {
 1332:                     my $thumbfile = 'tn-'.$filename;
 1333:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1334:                     if (@thumb) {
 1335:                         $showfile = $urldir.'/'.$thumbfile;
 1336:                     } else {
 1337:                         $showfile = $imgfile;
 1338:                     }
 1339:                 } else {
 1340:                     $showfile = '';
 1341:                 }
 1342:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1343:                 $showfile = $imgfile;
 1344:                 my $imgdir = $1;
 1345:                 my $filename = $2;
 1346:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1347:                     $showfile = "/$imgdir/tn-".$filename;
 1348:                 } else {
 1349:                     my $input = $londocroot.$imgfile;
 1350:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1351:                     if (!-e $output) {
 1352:                         my ($width,$height) = &thumb_dimensions();
 1353:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1354:                         if ($fullwidth ne '' && $fullheight ne '') {
 1355:                             if ($fullwidth > $width && $fullheight > $height) { 
 1356:                                 my $size = $width.'x'.$height;
 1357:                                 system("convert -sample $size $input $output");
 1358:                                 $showfile = "/$imgdir/tn-".$filename;
 1359:                             }
 1360:                         }
 1361:                     }
 1362:                 }
 1363:             }
 1364:             if ($showfile) {
 1365:                 if ($showfile =~ m{^/(adm|res)/}) {
 1366:                     if ($showfile =~ m{^/res/}) {
 1367:                         my $local_showfile =
 1368:                             &Apache::lonnet::filelocation('',$showfile);
 1369:                         &Apache::lonnet::repcopy($local_showfile);
 1370:                     }
 1371:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1372:                 }
 1373:                 if ($imgfile) {
 1374:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1375:                         if ($imgfile =~ m{^/res/}) {
 1376:                             my $local_imgfile =
 1377:                                 &Apache::lonnet::filelocation('',$imgfile);
 1378:                             &Apache::lonnet::repcopy($local_imgfile);
 1379:                         }
 1380:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1381:                     } else {
 1382:                         $fullsize = $imgfile;
 1383:                     }
 1384:                 }
 1385:                 $datatable .= '<td>';
 1386:                 if ($img eq 'login') {
 1387:                     $datatable .= $login_hdr_pick;
 1388:                 } 
 1389:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1390:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1391:             } else {
 1392:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1393:                               &mt('Upload:').'<br />';
 1394:             }
 1395:         } else {
 1396:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1397:                           &mt('Upload:').'<br />';
 1398:         }
 1399:         if ($switchserver) {
 1400:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1401:         } else {
 1402:             if ($img ne 'login') { # suppress file selection for Log-in header
 1403:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1404:             }
 1405:         }
 1406:         $datatable .= '</td></tr>';
 1407:     }
 1408:     $itemcount ++;
 1409:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1410:     $datatable .= '<tr'.$css_class.'>'.
 1411:                   '<td>'.$choices->{'bgs'}.'</td>';
 1412:     my $bgs_def;
 1413:     foreach my $item (@{$bgs}) {
 1414:         if (!$is_custom->{$item}) {
 1415:             $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>';
 1416:         }
 1417:     }
 1418:     if ($bgs_def) {
 1419:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1420:     } else {
 1421:         $datatable .= '<td>&nbsp;</td>';
 1422:     }
 1423:     $datatable .= '<td class="LC_right_item">'.
 1424:                   '<table border="0"><tr>';
 1425: 
 1426:     foreach my $item (@{$bgs}) {
 1427:         $datatable .= '<td align="center">'.$choices->{$item};
 1428: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1429:         if ($designs->{'bgs'}{$item}) {
 1430:             $datatable .= '&nbsp;';
 1431:         }
 1432:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1433:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1434:     }
 1435:     $datatable .= '</tr></table></td></tr>';
 1436:     $itemcount ++;
 1437:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1438:     $datatable .= '<tr'.$css_class.'>'.
 1439:                   '<td>'.$choices->{'links'}.'</td>';
 1440:     my $links_def;
 1441:     foreach my $item (@{$links}) {
 1442:         if (!$is_custom->{$item}) {
 1443:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1444:         }
 1445:     }
 1446:     if ($links_def) {
 1447:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1448:     } else {
 1449:         $datatable .= '<td>&nbsp;</td>';
 1450:     }
 1451:     $datatable .= '<td class="LC_right_item">'.
 1452:                   '<table border="0"><tr>';
 1453:     foreach my $item (@{$links}) {
 1454: 	my $color = $designs->{'link'}{$item} ? $designs->{'link'}{$item} : $defaults->{'links'}{$item};
 1455:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1456:         if ($designs->{'links'}{$item}) {
 1457:             $datatable.='&nbsp;';
 1458:         }
 1459:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1460:                       '" /></td>';
 1461:     }
 1462:     $$rowtotal += $itemcount;
 1463:     return $datatable;
 1464: }
 1465: 
 1466: sub logo_display_options {
 1467:     my ($img,$defaults,$designs) = @_;
 1468:     my $checkedon;
 1469:     if (ref($defaults) eq 'HASH') {
 1470:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1471:             if ($defaults->{'showlogo'}{$img}) {
 1472:                 $checkedon = 'checked="checked" ';     
 1473:             }
 1474:         } 
 1475:     }
 1476:     if (ref($designs) eq 'HASH') {
 1477:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1478:             if (defined($designs->{'showlogo'}{$img})) {
 1479:                 if ($designs->{'showlogo'}{$img} == 0) {
 1480:                     $checkedon = '';
 1481:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1482:                     $checkedon = 'checked="checked" ';
 1483:                 }
 1484:             }
 1485:         }
 1486:     }
 1487:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1488:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1489:            &mt('show').'</label>'."\n";
 1490: }
 1491: 
 1492: sub login_header_options  {
 1493:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1494:     my $output = '';
 1495:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1496:         $output .= &mt('Text default(s):').'<br />';
 1497:         if (!$is_custom->{'textcol'}) {
 1498:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1499:                        '&nbsp;&nbsp;&nbsp;';
 1500:         }
 1501:         if (!$is_custom->{'bgcol'}) {
 1502:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1503:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1504:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1505:         }
 1506:         $output .= '<br />';
 1507:     }
 1508:     $output .='<br />';
 1509:     return $output;
 1510: }
 1511: 
 1512: sub login_text_colors {
 1513:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1514:     my $color_menu = '<table border="0"><tr>';
 1515:     foreach my $item (@{$logintext}) {
 1516:         $color_menu .= '<td align="center">'.$choices->{$item};
 1517:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1518:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1519:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1520:     }
 1521:     $color_menu .= '</tr></table><br />';
 1522:     return $color_menu;
 1523: }
 1524: 
 1525: sub image_changes {
 1526:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1527:     my $output;
 1528:     if ($img eq 'login') {
 1529:             # suppress image for Log-in header
 1530:     } elsif (!$is_custom) {
 1531:         if ($img ne 'domlogo') {
 1532:             $output .= &mt('Default image:').'<br />';
 1533:         } else {
 1534:             $output .= &mt('Default in use:').'<br />';
 1535:         }
 1536:     }
 1537:     if ($img eq 'login') { # suppress image for Log-in header
 1538:         $output .= '<td>'.$logincolors;
 1539:     } else {
 1540:         if ($img_import) {
 1541:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1542:         }
 1543:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1544:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1545:         if ($is_custom) {
 1546:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1547:                        '<input type="checkbox" name="'.
 1548:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1549:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1550:         } else {
 1551:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1552:         }
 1553:     }
 1554:     return $output;
 1555: }
 1556: 
 1557: sub print_quotas {
 1558:     my ($dom,$settings,$rowtotal,$action) = @_;
 1559:     my $context;
 1560:     if ($action eq 'quotas') {
 1561:         $context = 'tools';
 1562:     } else {
 1563:         $context = $action;
 1564:     }
 1565:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1566:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1567:     my $typecount = 0;
 1568:     my ($css_class,%titles);
 1569:     if ($context eq 'requestcourses') {
 1570:         @usertools = ('official','unofficial','community','textbook');
 1571:         @options =('norequest','approval','validate','autolimit');
 1572:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1573:         %titles = &courserequest_titles();
 1574:     } elsif ($context eq 'requestauthor') {
 1575:         @usertools = ('author');
 1576:         @options = ('norequest','approval','automatic');
 1577:         %titles = &authorrequest_titles();
 1578:     } else {
 1579:         @usertools = ('aboutme','blog','webdav','portfolio');
 1580:         %titles = &tool_titles();
 1581:     }
 1582:     if (ref($types) eq 'ARRAY') {
 1583:         foreach my $type (@{$types}) {
 1584:             my ($currdefquota,$currauthorquota);
 1585:             unless (($context eq 'requestcourses') ||
 1586:                     ($context eq 'requestauthor')) {
 1587:                 if (ref($settings) eq 'HASH') {
 1588:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1589:                         $currdefquota = $settings->{defaultquota}->{$type};
 1590:                     } else {
 1591:                         $currdefquota = $settings->{$type};
 1592:                     }
 1593:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1594:                         $currauthorquota = $settings->{authorquota}->{$type};
 1595:                     }
 1596:                 }
 1597:             }
 1598:             if (defined($usertypes->{$type})) {
 1599:                 $typecount ++;
 1600:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1601:                 $datatable .= '<tr'.$css_class.'>'.
 1602:                               '<td>'.$usertypes->{$type}.'</td>'.
 1603:                               '<td class="LC_left_item">';
 1604:                 if ($context eq 'requestcourses') {
 1605:                     $datatable .= '<table><tr>';
 1606:                 }
 1607:                 my %cell;  
 1608:                 foreach my $item (@usertools) {
 1609:                     if ($context eq 'requestcourses') {
 1610:                         my ($curroption,$currlimit);
 1611:                         if (ref($settings) eq 'HASH') {
 1612:                             if (ref($settings->{$item}) eq 'HASH') {
 1613:                                 $curroption = $settings->{$item}->{$type};
 1614:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1615:                                     $currlimit = $1; 
 1616:                                 }
 1617:                             }
 1618:                         }
 1619:                         if (!$curroption) {
 1620:                             $curroption = 'norequest';
 1621:                         }
 1622:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1623:                         foreach my $option (@options) {
 1624:                             my $val = $option;
 1625:                             if ($option eq 'norequest') {
 1626:                                 $val = 0;  
 1627:                             }
 1628:                             if ($option eq 'validate') {
 1629:                                 my $canvalidate = 0;
 1630:                                 if (ref($validations{$item}) eq 'HASH') { 
 1631:                                     if ($validations{$item}{$type}) {
 1632:                                         $canvalidate = 1;
 1633:                                     }
 1634:                                 }
 1635:                                 next if (!$canvalidate);
 1636:                             }
 1637:                             my $checked = '';
 1638:                             if ($option eq $curroption) {
 1639:                                 $checked = ' checked="checked"';
 1640:                             } elsif ($option eq 'autolimit') {
 1641:                                 if ($curroption =~ /^autolimit/) {
 1642:                                     $checked = ' checked="checked"';
 1643:                                 }                       
 1644:                             } 
 1645:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1646:                                   '<input type="radio" name="crsreq_'.$item.
 1647:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1648:                                   $titles{$option}.'</label>';
 1649:                             if ($option eq 'autolimit') {
 1650:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1651:                                                 $item.'_limit_'.$type.'" size="1" '.
 1652:                                                 'value="'.$currlimit.'" />';
 1653:                             }
 1654:                             $cell{$item} .= '</span> ';
 1655:                             if ($option eq 'autolimit') {
 1656:                                 $cell{$item} .= $titles{'unlimited'};
 1657:                             }
 1658:                         }
 1659:                     } elsif ($context eq 'requestauthor') {
 1660:                         my $curroption;
 1661:                         if (ref($settings) eq 'HASH') {
 1662:                             $curroption = $settings->{$type};
 1663:                         }
 1664:                         if (!$curroption) {
 1665:                             $curroption = 'norequest';
 1666:                         }
 1667:                         foreach my $option (@options) {
 1668:                             my $val = $option;
 1669:                             if ($option eq 'norequest') {
 1670:                                 $val = 0;
 1671:                             }
 1672:                             my $checked = '';
 1673:                             if ($option eq $curroption) {
 1674:                                 $checked = ' checked="checked"';
 1675:                             }
 1676:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1677:                                   '<input type="radio" name="authorreq_'.$type.
 1678:                                   '" value="'.$val.'"'.$checked.' />'.
 1679:                                   $titles{$option}.'</label></span>&nbsp; ';
 1680:                         }
 1681:                     } else {
 1682:                         my $checked = 'checked="checked" ';
 1683:                         if (ref($settings) eq 'HASH') {
 1684:                             if (ref($settings->{$item}) eq 'HASH') {
 1685:                                 if ($settings->{$item}->{$type} == 0) {
 1686:                                     $checked = '';
 1687:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1688:                                     $checked =  'checked="checked" ';
 1689:                                 }
 1690:                             }
 1691:                         }
 1692:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1693:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1694:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1695:                                       '</label></span>&nbsp; ';
 1696:                     }
 1697:                 }
 1698:                 if ($context eq 'requestcourses') {
 1699:                     $datatable .= '</tr><tr>';
 1700:                     foreach my $item (@usertools) {
 1701:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1702:                     }
 1703:                     $datatable .= '</tr></table>';
 1704:                 }
 1705:                 $datatable .= '</td>';
 1706:                 unless (($context eq 'requestcourses') ||
 1707:                         ($context eq 'requestauthor')) {
 1708:                     $datatable .= 
 1709:                               '<td class="LC_right_item">'.
 1710:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1711:                               '<input type="text" name="quota_'.$type.
 1712:                               '" value="'.$currdefquota.
 1713:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1714:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1715:                               '<input type="text" name="authorquota_'.$type.
 1716:                               '" value="'.$currauthorquota.
 1717:                               '" size="5" /></span></td>';
 1718:                 }
 1719:                 $datatable .= '</tr>';
 1720:             }
 1721:         }
 1722:     }
 1723:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1724:         $defaultquota = '20';
 1725:         $authorquota = '500';
 1726:         if (ref($settings) eq 'HASH') {
 1727:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1728:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1729:             } elsif (defined($settings->{'default'})) {
 1730:                 $defaultquota = $settings->{'default'};
 1731:             }
 1732:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1733:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1734:             }
 1735:         }
 1736:     }
 1737:     $typecount ++;
 1738:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1739:     $datatable .= '<tr'.$css_class.'>'.
 1740:                   '<td>'.$othertitle.'</td>'.
 1741:                   '<td class="LC_left_item">';
 1742:     if ($context eq 'requestcourses') {
 1743:         $datatable .= '<table><tr>';
 1744:     }
 1745:     my %defcell;
 1746:     foreach my $item (@usertools) {
 1747:         if ($context eq 'requestcourses') {
 1748:             my ($curroption,$currlimit);
 1749:             if (ref($settings) eq 'HASH') {
 1750:                 if (ref($settings->{$item}) eq 'HASH') {
 1751:                     $curroption = $settings->{$item}->{'default'};
 1752:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1753:                         $currlimit = $1;
 1754:                     }
 1755:                 }
 1756:             }
 1757:             if (!$curroption) {
 1758:                 $curroption = 'norequest';
 1759:             }
 1760:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1761:             foreach my $option (@options) {
 1762:                 my $val = $option;
 1763:                 if ($option eq 'norequest') {
 1764:                     $val = 0;
 1765:                 }
 1766:                 if ($option eq 'validate') {
 1767:                     my $canvalidate = 0;
 1768:                     if (ref($validations{$item}) eq 'HASH') {
 1769:                         if ($validations{$item}{'default'}) {
 1770:                             $canvalidate = 1;
 1771:                         }
 1772:                     }
 1773:                     next if (!$canvalidate);
 1774:                 }
 1775:                 my $checked = '';
 1776:                 if ($option eq $curroption) {
 1777:                     $checked = ' checked="checked"';
 1778:                 } elsif ($option eq 'autolimit') {
 1779:                     if ($curroption =~ /^autolimit/) {
 1780:                         $checked = ' checked="checked"';
 1781:                     }
 1782:                 }
 1783:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1784:                                   '<input type="radio" name="crsreq_'.$item.
 1785:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1786:                                   $titles{$option}.'</label>';
 1787:                 if ($option eq 'autolimit') {
 1788:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1789:                                        $item.'_limit_default" size="1" '.
 1790:                                        'value="'.$currlimit.'" />';
 1791:                 }
 1792:                 $defcell{$item} .= '</span> ';
 1793:                 if ($option eq 'autolimit') {
 1794:                     $defcell{$item} .= $titles{'unlimited'};
 1795:                 }
 1796:             }
 1797:         } elsif ($context eq 'requestauthor') {
 1798:             my $curroption;
 1799:             if (ref($settings) eq 'HASH') {
 1800:                 $curroption = $settings->{'default'};
 1801:             }
 1802:             if (!$curroption) {
 1803:                 $curroption = 'norequest';
 1804:             }
 1805:             foreach my $option (@options) {
 1806:                 my $val = $option;
 1807:                 if ($option eq 'norequest') {
 1808:                     $val = 0;
 1809:                 }
 1810:                 my $checked = '';
 1811:                 if ($option eq $curroption) {
 1812:                     $checked = ' checked="checked"';
 1813:                 }
 1814:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1815:                               '<input type="radio" name="authorreq_default"'.
 1816:                               ' value="'.$val.'"'.$checked.' />'.
 1817:                               $titles{$option}.'</label></span>&nbsp; ';
 1818:             }
 1819:         } else {
 1820:             my $checked = 'checked="checked" ';
 1821:             if (ref($settings) eq 'HASH') {
 1822:                 if (ref($settings->{$item}) eq 'HASH') {
 1823:                     if ($settings->{$item}->{'default'} == 0) {
 1824:                         $checked = '';
 1825:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1826:                         $checked = 'checked="checked" ';
 1827:                     }
 1828:                 }
 1829:             }
 1830:             $datatable .= '<span class="LC_nobreak"><label>'.
 1831:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1832:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1833:                           '</label></span>&nbsp; ';
 1834:         }
 1835:     }
 1836:     if ($context eq 'requestcourses') {
 1837:         $datatable .= '</tr><tr>';
 1838:         foreach my $item (@usertools) {
 1839:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1840:         }
 1841:         $datatable .= '</tr></table>';
 1842:     }
 1843:     $datatable .= '</td>';
 1844:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1845:         $datatable .= '<td class="LC_right_item">'.
 1846:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1847:                       '<input type="text" name="defaultquota" value="'.
 1848:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1849:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1850:                       '<input type="text" name="authorquota" value="'.
 1851:                       $authorquota.'" size="5" /></span></td>';
 1852:     }
 1853:     $datatable .= '</tr>';
 1854:     $typecount ++;
 1855:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1856:     $datatable .= '<tr'.$css_class.'>'.
 1857:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 1858:     if ($context eq 'requestcourses') {
 1859:         $datatable .= &mt('(overrides affiliation, if set)').
 1860:                       '</td>'.
 1861:                       '<td class="LC_left_item">'.
 1862:                       '<table><tr>';
 1863:     } else {
 1864:         $datatable .= &mt('(overrides affiliation, if checked)').
 1865:                       '</td>'.
 1866:                       '<td class="LC_left_item" colspan="2">'.
 1867:                       '<br />';
 1868:     }
 1869:     my %advcell;
 1870:     foreach my $item (@usertools) {
 1871:         if ($context eq 'requestcourses') {
 1872:             my ($curroption,$currlimit);
 1873:             if (ref($settings) eq 'HASH') {
 1874:                 if (ref($settings->{$item}) eq 'HASH') {
 1875:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1876:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1877:                         $currlimit = $1;
 1878:                     }
 1879:                 }
 1880:             }
 1881:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1882:             my $checked = '';
 1883:             if ($curroption eq '') {
 1884:                 $checked = ' checked="checked"';
 1885:             }
 1886:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1887:                                '<input type="radio" name="crsreq_'.$item.
 1888:                                '__LC_adv" value=""'.$checked.' />'.
 1889:                                &mt('No override set').'</label></span>&nbsp; ';
 1890:             foreach my $option (@options) {
 1891:                 my $val = $option;
 1892:                 if ($option eq 'norequest') {
 1893:                     $val = 0;
 1894:                 }
 1895:                 if ($option eq 'validate') {
 1896:                     my $canvalidate = 0;
 1897:                     if (ref($validations{$item}) eq 'HASH') {
 1898:                         if ($validations{$item}{'_LC_adv'}) {
 1899:                             $canvalidate = 1;
 1900:                         }
 1901:                     }
 1902:                     next if (!$canvalidate);
 1903:                 }
 1904:                 my $checked = '';
 1905:                 if ($val eq $curroption) {
 1906:                     $checked = ' checked="checked"';
 1907:                 } elsif ($option eq 'autolimit') {
 1908:                     if ($curroption =~ /^autolimit/) {
 1909:                         $checked = ' checked="checked"';
 1910:                     }
 1911:                 }
 1912:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1913:                                   '<input type="radio" name="crsreq_'.$item.
 1914:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1915:                                   $titles{$option}.'</label>';
 1916:                 if ($option eq 'autolimit') {
 1917:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1918:                                        $item.'_limit__LC_adv" size="1" '.
 1919:                                        'value="'.$currlimit.'" />';
 1920:                 }
 1921:                 $advcell{$item} .= '</span> ';
 1922:                 if ($option eq 'autolimit') {
 1923:                     $advcell{$item} .= $titles{'unlimited'};
 1924:                 }
 1925:             }
 1926:         } elsif ($context eq 'requestauthor') {
 1927:             my $curroption;
 1928:             if (ref($settings) eq 'HASH') {
 1929:                 $curroption = $settings->{'_LC_adv'};
 1930:             }
 1931:             my $checked = '';
 1932:             if ($curroption eq '') {
 1933:                 $checked = ' checked="checked"';
 1934:             }
 1935:             $datatable .= '<span class="LC_nobreak"><label>'.
 1936:                           '<input type="radio" name="authorreq__LC_adv"'.
 1937:                           ' value=""'.$checked.' />'.
 1938:                           &mt('No override set').'</label></span>&nbsp; ';
 1939:             foreach my $option (@options) {
 1940:                 my $val = $option;
 1941:                 if ($option eq 'norequest') {
 1942:                     $val = 0;
 1943:                 }
 1944:                 my $checked = '';
 1945:                 if ($val eq $curroption) {
 1946:                     $checked = ' checked="checked"';
 1947:                 }
 1948:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1949:                               '<input type="radio" name="authorreq__LC_adv"'.
 1950:                               ' value="'.$val.'"'.$checked.' />'.
 1951:                               $titles{$option}.'</label></span>&nbsp; ';
 1952:             }
 1953:         } else {
 1954:             my $checked = 'checked="checked" ';
 1955:             if (ref($settings) eq 'HASH') {
 1956:                 if (ref($settings->{$item}) eq 'HASH') {
 1957:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1958:                         $checked = '';
 1959:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1960:                         $checked = 'checked="checked" ';
 1961:                     }
 1962:                 }
 1963:             }
 1964:             $datatable .= '<span class="LC_nobreak"><label>'.
 1965:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1966:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1967:                           '</label></span>&nbsp; ';
 1968:         }
 1969:     }
 1970:     if ($context eq 'requestcourses') {
 1971:         $datatable .= '</tr><tr>';
 1972:         foreach my $item (@usertools) {
 1973:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1974:         }
 1975:         $datatable .= '</tr></table>';
 1976:     }
 1977:     $datatable .= '</td></tr>';
 1978:     $$rowtotal += $typecount;
 1979:     return $datatable;
 1980: }
 1981: 
 1982: sub print_requestmail {
 1983:     my ($dom,$action,$settings,$rowtotal) = @_;
 1984:     my ($now,$datatable,%currapp);
 1985:     $now = time;
 1986:     if (ref($settings) eq 'HASH') {
 1987:         if (ref($settings->{'notify'}) eq 'HASH') {
 1988:             if ($settings->{'notify'}{'approval'} ne '') {
 1989:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 1990:             }
 1991:         }
 1992:     }
 1993:     my $numinrow = 2;
 1994:     my $css_class;
 1995:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 1996:     my $text;
 1997:     if ($action eq 'requestcourses') {
 1998:         $text = &mt('Receive notification of course requests requiring approval');
 1999:     } elsif ($action eq 'requestauthor') {
 2000:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2001:     } else {
 2002:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2003:     }
 2004:     $datatable = '<tr'.$css_class.'>'.
 2005:                  ' <td>'.$text.'</td>'.
 2006:                  ' <td class="LC_left_item">';
 2007:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2008:                                                  $action.'notifyapproval',%currapp);
 2009:     if ($numdc > 0) {
 2010:         $datatable .= $table;
 2011:     } else {
 2012:         $datatable .= &mt('There are no active Domain Coordinators');
 2013:     }
 2014:     $datatable .='</td></tr>';
 2015:     $$rowtotal += $rows;
 2016:     return $datatable;
 2017: }
 2018: 
 2019: sub print_studentcode {
 2020:     my ($settings,$rowtotal) = @_;
 2021:     my $rownum = 0; 
 2022:     my ($output,%current);
 2023:     my @crstypes = ('official','unofficial','community','textbook');
 2024:     if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2025:         foreach my $type (@crstypes) {
 2026:             $current{$type} = $settings->{'uniquecode'}{$type};
 2027:         }
 2028:     }
 2029:     $output .= '<tr>'.
 2030:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2031:                '<td class="LC_left_item">';
 2032:     foreach my $type (@crstypes) {
 2033:         my $check = ' ';
 2034:         if ($current{$type}) {
 2035:             $check = ' checked="checked" ';
 2036:         }
 2037:         $output .= '<span class="LC_nobreak"><label>'.
 2038:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2039:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2040:     }
 2041:     $output .= '</td></tr>';
 2042:     $$rowtotal ++;
 2043:     return $output;
 2044: }
 2045: 
 2046: sub print_textbookcourses {
 2047:     my ($dom,$settings,$rowtotal) = @_;
 2048:     my $rownum = 0;
 2049:     my $css_class;
 2050:     my $itemcount = 1;
 2051:     my $maxnum = 0;
 2052:     my $bookshash;
 2053:     if (ref($settings) eq 'HASH') {
 2054:         $bookshash = $settings->{'textbooks'};
 2055:     }
 2056:     my %ordered;
 2057:     if (ref($bookshash) eq 'HASH') {
 2058:         foreach my $item (keys(%{$bookshash})) {
 2059:             if (ref($bookshash->{$item}) eq 'HASH') {
 2060:                 my $num = $bookshash->{$item}{'order'};
 2061:                 $ordered{$num} = $item;
 2062:             }
 2063:         }
 2064:     }
 2065:     my $confname = $dom.'-domainconfig';
 2066:     my $switchserver = &check_switchserver($dom,$confname);
 2067:     my $maxnum = scalar(keys(%ordered));
 2068:     my $datatable = &textbookcourses_javascript(\%ordered);
 2069:     if (keys(%ordered)) {
 2070:         my @items = sort { $a <=> $b } keys(%ordered);
 2071:         for (my $i=0; $i<@items; $i++) {
 2072:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2073:             my $key = $ordered{$items[$i]};
 2074:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2075:             my $coursetitle = $coursehash{'description'};
 2076:             my ($subject,$title,$author,$image,$imgsrc,$cdom,$cnum);
 2077:             if (ref($bookshash->{$key}) eq 'HASH') {
 2078:                 $subject = $bookshash->{$key}->{'subject'};
 2079:                 $title = $bookshash->{$key}->{'title'};
 2080:                 $author = $bookshash->{$key}->{'author'};
 2081:                 $image = $bookshash->{$key}->{'image'};
 2082:                 if ($image ne '') {
 2083:                     my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2084:                     my $imagethumb = "$path/tn-".$imagefile;
 2085:                     $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2086:                 }
 2087:             }
 2088:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$key'".');"';
 2089:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2090:                          .'<select name="'.$key.'"'.$chgstr.'>';
 2091:             for (my $k=0; $k<=$maxnum; $k++) {
 2092:                 my $vpos = $k+1;
 2093:                 my $selstr;
 2094:                 if ($k == $i) {
 2095:                     $selstr = ' selected="selected" ';
 2096:                 }
 2097:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2098:             }
 2099:             $datatable .= '</select>'.('&nbsp;'x2).
 2100:                 '<label><input type="checkbox" name="book_del" value="'.$key.'" />'.
 2101:                 &mt('Delete?').'</label></span></td>'.
 2102:                 '<td colspan="2">'.
 2103:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="book_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2104:                 ('&nbsp;'x2).
 2105:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="book_title_'.$i.'" value="'.$title.'" /></span> '.
 2106:                 ('&nbsp;'x2).
 2107:                 '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="book_author_'.$i.'" value="'.$author.'" /></span> '.
 2108:                 ('&nbsp;'x2).
 2109:                 '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2110:             if ($image) {
 2111:                 $datatable .= '<span class="LC_nobreak">'.
 2112:                               $imgsrc.
 2113:                               '<label><input type="checkbox" name="book_image_del"'.
 2114:                               ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2115:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2116:             }
 2117:             if ($switchserver) {
 2118:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2119:             } else {
 2120:                 $datatable .= '<input type="file" name="book_image_'.$i.'" value="" />';
 2121:             }
 2122:             $datatable .= '<input type="hidden" name="book_id_'.$i.'" value="'.$key.'" /></span> '.
 2123:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2124:                           $coursetitle.'</span></td></tr>'."\n";
 2125:             $itemcount ++;
 2126:         }
 2127:     }
 2128:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2129:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'addbook_pos'".');"';
 2130:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2131:                   '<input type="hidden" name="book_maxnum" value="'.$maxnum.'" />'."\n".
 2132:                   '<select name="addbook_pos"'.$chgstr.'>';
 2133:     for (my $k=0; $k<$maxnum+1; $k++) {
 2134:         my $vpos = $k+1;
 2135:         my $selstr;
 2136:         if ($k == $maxnum) {
 2137:             $selstr = ' selected="selected" ';
 2138:         }
 2139:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2140:     }
 2141:     $datatable .= '</select>&nbsp;'."\n".
 2142:                   '<input type="checkbox" name="addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2143:                   '<td colspan="2">'.
 2144:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="addbook_subject" value="" /></span> '."\n".
 2145:                   ('&nbsp;'x2).
 2146:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="addbook_title" value="" /></span> '."\n".
 2147:                   ('&nbsp;'x2).
 2148:                   '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="addbook_author" value="" /></span> '."\n".
 2149:                   ('&nbsp;'x2).
 2150:                   '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2151:     if ($switchserver) {
 2152:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2153:     } else {
 2154:         $datatable .= '<input type="file" name="addbook_image" value="" />';
 2155:     }
 2156:     $datatable .= '</span>'."\n".
 2157:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2158:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},'addbook_cdom').
 2159:                   '<input type="text" size="25" name="addbook_cnum" value="" />'.
 2160:                   &Apache::loncommon::selectcourse_link
 2161:                       ('display','addbook_cnum','addbook_cdom',undef,undef,undef,'Course');
 2162:                   '</span></td>'."\n".
 2163:                   '</tr>'."\n";
 2164:     $itemcount ++;
 2165:     return $datatable;
 2166: }
 2167: 
 2168: sub textbookcourses_javascript {
 2169:     my ($textbooks) = @_;
 2170:     return unless(ref($textbooks) eq 'HASH');
 2171:     my $num = scalar(keys(%{$textbooks}));
 2172:     my @jsarray;
 2173:     foreach my $item (sort {$a <=> $b } (keys(%{$textbooks}))) {
 2174:         push(@jsarray,$textbooks->{$item});
 2175:     }
 2176:     my $jstext = '    var textbooks = Array('."'".join("','",@jsarray)."'".');'."\n";
 2177:     return <<"ENDSCRIPT";
 2178: <script type="text/javascript">
 2179: // <![CDATA[
 2180: function reorderBooks(form,item) {
 2181:     var changedVal;
 2182: $jstext 
 2183:     var newpos = 'addbook_pos';
 2184:     var current = new Array;
 2185:     var maxh = 1 + $num;
 2186:     var current = new Array;
 2187:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2188:     if (item == newpos) {
 2189:         changedVal = newitemVal;
 2190:     } else {
 2191:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2192:         current[newitemVal] = newpos;
 2193:     }
 2194:     for (var i=0; i<textbooks.length; i++) {
 2195:         var elementName = textbooks[i];
 2196:         if (elementName != item) {
 2197:             if (form.elements[elementName]) {
 2198:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2199:                 current[currVal] = elementName;
 2200:             }
 2201:         }
 2202:     }
 2203:     var oldVal;
 2204:     for (var j=0; j<maxh; j++) {
 2205:         if (current[j] == undefined) {
 2206:             oldVal = j;
 2207:         }
 2208:     }
 2209:     if (oldVal < changedVal) {
 2210:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2211:            var elementName = current[k];
 2212:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2213:         }
 2214:     } else {
 2215:         for (var k=changedVal; k<oldVal; k++) {
 2216:             var elementName = current[k];
 2217:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2218:         }
 2219:     }
 2220:     return;
 2221: }
 2222: 
 2223: // ]]>
 2224: </script>
 2225: 
 2226: ENDSCRIPT
 2227: }
 2228: 
 2229: sub print_autoenroll {
 2230:     my ($dom,$settings,$rowtotal) = @_;
 2231:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2232:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 2233:     if (ref($settings) eq 'HASH') {
 2234:         if (exists($settings->{'run'})) {
 2235:             if ($settings->{'run'} eq '0') {
 2236:                 $runoff = ' checked="checked" ';
 2237:                 $runon = ' ';
 2238:             } else {
 2239:                 $runon = ' checked="checked" ';
 2240:                 $runoff = ' ';
 2241:             }
 2242:         } else {
 2243:             if ($autorun) {
 2244:                 $runon = ' checked="checked" ';
 2245:                 $runoff = ' ';
 2246:             } else {
 2247:                 $runoff = ' checked="checked" ';
 2248:                 $runon = ' ';
 2249:             }
 2250:         }
 2251:         if (exists($settings->{'co-owners'})) {
 2252:             if ($settings->{'co-owners'} eq '0') {
 2253:                 $coownersoff = ' checked="checked" ';
 2254:                 $coownerson = ' ';
 2255:             } else {
 2256:                 $coownerson = ' checked="checked" ';
 2257:                 $coownersoff = ' ';
 2258:             }
 2259:         } else {
 2260:             $coownersoff = ' checked="checked" ';
 2261:             $coownerson = ' ';
 2262:         }
 2263:         if (exists($settings->{'sender_domain'})) {
 2264:             $defdom = $settings->{'sender_domain'};
 2265:         }
 2266:     } else {
 2267:         if ($autorun) {
 2268:             $runon = ' checked="checked" ';
 2269:             $runoff = ' ';
 2270:         } else {
 2271:             $runoff = ' checked="checked" ';
 2272:             $runon = ' ';
 2273:         }
 2274:     }
 2275:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2276:     my $notif_sender;
 2277:     if (ref($settings) eq 'HASH') {
 2278:         $notif_sender = $settings->{'sender_uname'};
 2279:     }
 2280:     my $datatable='<tr class="LC_odd_row">'.
 2281:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2282:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2283:                   '<input type="radio" name="autoenroll_run"'.
 2284:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2285:                   '<label><input type="radio" name="autoenroll_run"'.
 2286:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2287:                   '</tr><tr>'.
 2288:                   '<td>'.&mt('Notification messages - sender').
 2289:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2290:                   &mt('username').':&nbsp;'.
 2291:                   '<input type="text" name="sender_uname" value="'.
 2292:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2293:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2294:                   '<tr class="LC_odd_row">'.
 2295:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2296:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2297:                   '<input type="radio" name="autoassign_coowners"'.
 2298:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2299:                   '<label><input type="radio" name="autoassign_coowners"'.
 2300:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2301:                   '</tr>';
 2302:     $$rowtotal += 3;
 2303:     return $datatable;
 2304: }
 2305: 
 2306: sub print_autoupdate {
 2307:     my ($position,$dom,$settings,$rowtotal) = @_;
 2308:     my $datatable;
 2309:     if ($position eq 'top') {
 2310:         my $updateon = ' ';
 2311:         my $updateoff = ' checked="checked" ';
 2312:         my $classlistson = ' ';
 2313:         my $classlistsoff = ' checked="checked" ';
 2314:         if (ref($settings) eq 'HASH') {
 2315:             if ($settings->{'run'} eq '1') {
 2316:                 $updateon = $updateoff;
 2317:                 $updateoff = ' ';
 2318:             }
 2319:             if ($settings->{'classlists'} eq '1') {
 2320:                 $classlistson = $classlistsoff;
 2321:                 $classlistsoff = ' ';
 2322:             }
 2323:         }
 2324:         my %title = (
 2325:                    run => 'Auto-update active?',
 2326:                    classlists => 'Update information in classlists?',
 2327:                     );
 2328:         $datatable = '<tr class="LC_odd_row">'. 
 2329:                   '<td>'.&mt($title{'run'}).'</td>'.
 2330:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2331:                   '<input type="radio" name="autoupdate_run"'.
 2332:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2333:                   '<label><input type="radio" name="autoupdate_run"'.
 2334:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2335:                   '</tr><tr>'.
 2336:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2337:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2338:                   '<label><input type="radio" name="classlists"'.
 2339:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2340:                   '<label><input type="radio" name="classlists"'.
 2341:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2342:                   '</tr>';
 2343:         $$rowtotal += 2;
 2344:     } elsif ($position eq 'middle') {
 2345:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2346:         my $numinrow = 3;
 2347:         my $locknamesettings;
 2348:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2349:                                      $dom,$numinrow,$othertitle,
 2350:                                     'lockablenames');
 2351:         $$rowtotal ++;
 2352:     } else {
 2353:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2354:         my @fields = ('lastname','firstname','middlename','generation',
 2355:                       'permanentemail','id');
 2356:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2357:         my $numrows = 0;
 2358:         if (ref($types) eq 'ARRAY') {
 2359:             if (@{$types} > 0) {
 2360:                 $datatable = 
 2361:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2362:                                          \@fields,$types,\$numrows);
 2363:                     $$rowtotal += @{$types}; 
 2364:             }
 2365:         }
 2366:         $datatable .= 
 2367:             &usertype_update_row($settings,{'default' => $othertitle},
 2368:                                  \%fieldtitles,\@fields,['default'],
 2369:                                  \$numrows);
 2370:         $$rowtotal ++;     
 2371:     }
 2372:     return $datatable;
 2373: }
 2374: 
 2375: sub print_autocreate {
 2376:     my ($dom,$settings,$rowtotal) = @_;
 2377:     my (%createon,%createoff,%currhash);
 2378:     my @types = ('xml','req');
 2379:     if (ref($settings) eq 'HASH') {
 2380:         foreach my $item (@types) {
 2381:             $createoff{$item} = ' checked="checked" ';
 2382:             $createon{$item} = ' ';
 2383:             if (exists($settings->{$item})) {
 2384:                 if ($settings->{$item}) {
 2385:                     $createon{$item} = ' checked="checked" ';
 2386:                     $createoff{$item} = ' ';
 2387:                 }
 2388:             }
 2389:         }
 2390:         if ($settings->{'xmldc'} ne '') {
 2391:             $currhash{$settings->{'xmldc'}} = 1;
 2392:         }
 2393:     } else {
 2394:         foreach my $item (@types) {
 2395:             $createoff{$item} = ' checked="checked" ';
 2396:             $createon{$item} = ' ';
 2397:         }
 2398:     }
 2399:     $$rowtotal += 2;
 2400:     my $numinrow = 2;
 2401:     my $datatable='<tr class="LC_odd_row">'.
 2402:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2403:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2404:                   '<input type="radio" name="autocreate_xml"'.
 2405:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2406:                   '<label><input type="radio" name="autocreate_xml"'.
 2407:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2408:                   '</td></tr><tr>'.
 2409:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2410:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2411:                   '<input type="radio" name="autocreate_req"'.
 2412:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2413:                   '<label><input type="radio" name="autocreate_req"'.
 2414:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2415:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2416:                                                    'autocreate_xmldc',%currhash);
 2417:     if ($numdc > 1) {
 2418:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2419:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2420:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2421:     } else {
 2422:         $datatable .= $dctable.'</td></tr>';
 2423:     }
 2424:     $$rowtotal += $rows;
 2425:     return $datatable;
 2426: }
 2427: 
 2428: sub print_directorysrch {
 2429:     my ($dom,$settings,$rowtotal) = @_;
 2430:     my $srchon = ' ';
 2431:     my $srchoff = ' checked="checked" ';
 2432:     my ($exacton,$containson,$beginson);
 2433:     my $localon = ' ';
 2434:     my $localoff = ' checked="checked" ';
 2435:     if (ref($settings) eq 'HASH') {
 2436:         if ($settings->{'available'} eq '1') {
 2437:             $srchon = $srchoff;
 2438:             $srchoff = ' ';
 2439:         }
 2440:         if ($settings->{'localonly'} eq '1') {
 2441:             $localon = $localoff;
 2442:             $localoff = ' ';
 2443:         }
 2444:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2445:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2446:                 if ($type eq 'exact') {
 2447:                     $exacton = ' checked="checked" ';
 2448:                 } elsif ($type eq 'contains') {
 2449:                     $containson = ' checked="checked" ';
 2450:                 } elsif ($type eq 'begins') {
 2451:                     $beginson = ' checked="checked" ';
 2452:                 }
 2453:             }
 2454:         } else {
 2455:             if ($settings->{'searchtypes'} eq 'exact') {
 2456:                 $exacton = ' checked="checked" ';
 2457:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2458:                 $containson = ' checked="checked" ';
 2459:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2460:                 $exacton = ' checked="checked" ';
 2461:                 $containson = ' checked="checked" ';
 2462:             }
 2463:         }
 2464:     }
 2465:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2466:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2467: 
 2468:     my $numinrow = 4;
 2469:     my $cansrchrow = 0;
 2470:     my $datatable='<tr class="LC_odd_row">'.
 2471:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2472:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2473:                   '<input type="radio" name="dirsrch_available"'.
 2474:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2475:                   '<label><input type="radio" name="dirsrch_available"'.
 2476:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2477:                   '</tr><tr>'.
 2478:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2479:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2480:                   '<input type="radio" name="dirsrch_localonly"'.
 2481:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2482:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2483:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2484:                   '</tr>';
 2485:     $$rowtotal += 2;
 2486:     if (ref($usertypes) eq 'HASH') {
 2487:         if (keys(%{$usertypes}) > 0) {
 2488:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2489:                                          $numinrow,$othertitle,'cansearch');
 2490:             $cansrchrow = 1;
 2491:         }
 2492:     }
 2493:     if ($cansrchrow) {
 2494:         $$rowtotal ++;
 2495:         $datatable .= '<tr>';
 2496:     } else {
 2497:         $datatable .= '<tr class="LC_odd_row">';
 2498:     }
 2499:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2500:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2501:     foreach my $title (@{$titleorder}) {
 2502:         if (defined($searchtitles->{$title})) {
 2503:             my $check = ' ';
 2504:             if (ref($settings) eq 'HASH') {
 2505:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2506:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2507:                         $check = ' checked="checked" ';
 2508:                     }
 2509:                 }
 2510:             }
 2511:             $datatable .= '<td class="LC_left_item">'.
 2512:                           '<span class="LC_nobreak"><label>'.
 2513:                           '<input type="checkbox" name="searchby" '.
 2514:                           'value="'.$title.'"'.$check.'/>'.
 2515:                           $searchtitles->{$title}.'</label></span></td>';
 2516:         }
 2517:     }
 2518:     $datatable .= '</tr></table></td></tr>';
 2519:     $$rowtotal ++;
 2520:     if ($cansrchrow) {
 2521:         $datatable .= '<tr class="LC_odd_row">';
 2522:     } else {
 2523:         $datatable .= '<tr>';
 2524:     }
 2525:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2526:                   '<td class="LC_left_item" colspan="2">'.
 2527:                   '<span class="LC_nobreak"><label>'.
 2528:                   '<input type="checkbox" name="searchtypes" '.
 2529:                   $exacton.' value="exact" />'.&mt('Exact match').
 2530:                   '</label>&nbsp;'.
 2531:                   '<label><input type="checkbox" name="searchtypes" '.
 2532:                   $beginson.' value="begins" />'.&mt('Begins with').
 2533:                   '</label>&nbsp;'.
 2534:                   '<label><input type="checkbox" name="searchtypes" '.
 2535:                   $containson.' value="contains" />'.&mt('Contains').
 2536:                   '</label></span></td></tr>';
 2537:     $$rowtotal ++;
 2538:     return $datatable;
 2539: }
 2540: 
 2541: sub print_contacts {
 2542:     my ($dom,$settings,$rowtotal) = @_;
 2543:     my $datatable;
 2544:     my @contacts = ('adminemail','supportemail');
 2545:     my (%checked,%to,%otheremails,%bccemails);
 2546:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2547:                     'requestsmail','updatesmail','idconflictsmail');
 2548:     foreach my $type (@mailings) {
 2549:         $otheremails{$type} = '';
 2550:     }
 2551:     $bccemails{'helpdeskmail'} = '';
 2552:     if (ref($settings) eq 'HASH') {
 2553:         foreach my $item (@contacts) {
 2554:             if (exists($settings->{$item})) {
 2555:                 $to{$item} = $settings->{$item};
 2556:             }
 2557:         }
 2558:         foreach my $type (@mailings) {
 2559:             if (exists($settings->{$type})) {
 2560:                 if (ref($settings->{$type}) eq 'HASH') {
 2561:                     foreach my $item (@contacts) {
 2562:                         if ($settings->{$type}{$item}) {
 2563:                             $checked{$type}{$item} = ' checked="checked" ';
 2564:                         }
 2565:                     }
 2566:                     $otheremails{$type} = $settings->{$type}{'others'};
 2567:                     if ($type eq 'helpdeskmail') {
 2568:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2569:                     }
 2570:                 }
 2571:             } elsif ($type eq 'lonstatusmail') {
 2572:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2573:             }
 2574:         }
 2575:     } else {
 2576:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2577:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2578:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2579:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2580:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2581:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2582:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2583:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2584:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2585:     }
 2586:     my ($titles,$short_titles) = &contact_titles();
 2587:     my $rownum = 0;
 2588:     my $css_class;
 2589:     foreach my $item (@contacts) {
 2590:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2591:         $datatable .= '<tr'.$css_class.'>'. 
 2592:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2593:                   '</span></td><td class="LC_right_item">'.
 2594:                   '<input type="text" name="'.$item.'" value="'.
 2595:                   $to{$item}.'" /></td></tr>';
 2596:         $rownum ++;
 2597:     }
 2598:     foreach my $type (@mailings) {
 2599:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2600:         $datatable .= '<tr'.$css_class.'>'.
 2601:                       '<td><span class="LC_nobreak">'.
 2602:                       $titles->{$type}.': </span></td>'.
 2603:                       '<td class="LC_left_item">'.
 2604:                       '<span class="LC_nobreak">';
 2605:         foreach my $item (@contacts) {
 2606:             $datatable .= '<label>'.
 2607:                           '<input type="checkbox" name="'.$type.'"'.
 2608:                           $checked{$type}{$item}.
 2609:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2610:                           '</label>&nbsp;';
 2611:         }
 2612:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2613:                       '<input type="text" name="'.$type.'_others" '.
 2614:                       'value="'.$otheremails{$type}.'"  />';
 2615:         if ($type eq 'helpdeskmail') {
 2616:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2617:                           '<input type="text" name="'.$type.'_bcc" '.
 2618:                           'value="'.$bccemails{$type}.'"  />';
 2619:         }
 2620:         $datatable .= '</td></tr>'."\n";
 2621:         $rownum ++;
 2622:     }
 2623:     my %choices;
 2624:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2625:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2626:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2627:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2628:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2629:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2630:     my @toggles = ('reporterrors','reportupdates');
 2631:     my %defaultchecked = ('reporterrors'  => 'on',
 2632:                           'reportupdates' => 'on');
 2633:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2634:                                                \%choices,$rownum);
 2635:     $datatable .= $reports;
 2636:     $$rowtotal += $rownum;
 2637:     return $datatable;
 2638: }
 2639: 
 2640: sub print_helpsettings {
 2641:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2642:     my ($datatable,$itemcount);
 2643:     $itemcount = 1;
 2644:     my (%choices,%defaultchecked,@toggles);
 2645:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2646:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2647:                                  &mt('LON-CAPA bug tracker'),600,500));
 2648:     %defaultchecked = ('submitbugs' => 'on');
 2649:     @toggles = ('submitbugs',);
 2650: 
 2651:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2652:                                                  \%choices,$itemcount);
 2653:     return $datatable;
 2654: }
 2655: 
 2656: sub radiobutton_prefs {
 2657:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2658:         $additional) = @_;
 2659:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2660:                    (ref($choices) eq 'HASH'));
 2661: 
 2662:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2663: 
 2664:     foreach my $item (@{$toggles}) {
 2665:         if ($defaultchecked->{$item} eq 'on') {
 2666:             $checkedon{$item} = ' checked="checked" ';
 2667:             $checkedoff{$item} = ' ';
 2668:         } elsif ($defaultchecked->{$item} eq 'off') {
 2669:             $checkedoff{$item} = ' checked="checked" ';
 2670:             $checkedon{$item} = ' ';
 2671:         }
 2672:     }
 2673:     if (ref($settings) eq 'HASH') {
 2674:         foreach my $item (@{$toggles}) {
 2675:             if ($settings->{$item} eq '1') {
 2676:                 $checkedon{$item} =  ' checked="checked" ';
 2677:                 $checkedoff{$item} = ' ';
 2678:             } elsif ($settings->{$item} eq '0') {
 2679:                 $checkedoff{$item} =  ' checked="checked" ';
 2680:                 $checkedon{$item} = ' ';
 2681:             }
 2682:         }
 2683:     }
 2684:     if ($onclick) {
 2685:         $onclick = ' onclick="'.$onclick.'"';
 2686:     }
 2687:     foreach my $item (@{$toggles}) {
 2688:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2689:         $datatable .=
 2690:             '<tr'.$css_class.'><td valign="top">'.
 2691:             '<span class="LC_nobreak">'.$choices->{$item}.
 2692:             '</span></td>'.
 2693:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2694:             '<label><input type="radio" name="'.
 2695:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2696:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2697:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2698:             '</span>'.$additional.
 2699:             '</td>'.
 2700:             '</tr>';
 2701:         $itemcount ++;
 2702:     }
 2703:     return ($datatable,$itemcount);
 2704: }
 2705: 
 2706: sub print_coursedefaults {
 2707:     my ($position,$dom,$settings,$rowtotal) = @_;
 2708:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2709:     my $itemcount = 1;
 2710:     my %choices =  &Apache::lonlocal::texthash (
 2711:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2712:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2713:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2714:         coursecredits        => 'Credits can be specified for courses',
 2715:     );
 2716:     my %staticdefaults = (
 2717:                            anonsurvey_threshold => 10,
 2718:                            uploadquota          => 500,
 2719:                          );
 2720:     if ($position eq 'top') {
 2721:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2722:         @toggles = ('canuse_pdfforms');
 2723:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2724:                                                  \%choices,$itemcount);
 2725:     } else {
 2726:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2727:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits,$def_textbook_credits,
 2728:             %curruploadquota);
 2729:         my $currusecredits = 0;
 2730:         my @types = ('official','unofficial','community','textbook');
 2731:         if (ref($settings) eq 'HASH') {
 2732:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2733:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2734:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 2735:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 2736:                 }
 2737:             }
 2738:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2739:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2740:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2741:                 $def_textbook_credits = $settings->{'coursecredits'}->{'textbook'};
 2742:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '') ||
 2743:                     ($def_textbook_credits ne '')) {
 2744:                     $currusecredits = 1;
 2745:                 }
 2746:             }
 2747:         }
 2748:         if (!$currdefresponder) {
 2749:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 2750:         } elsif ($currdefresponder < 1) {
 2751:             $currdefresponder = 1;
 2752:         }
 2753:         foreach my $type (@types) {
 2754:             if ($curruploadquota{$type} eq '') {
 2755:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 2756:             }
 2757:         }
 2758:         $datatable .=
 2759:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2760:                 $choices{'anonsurvey_threshold'}.
 2761:                 '</span></td>'.
 2762:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2763:                 '<input type="text" name="anonsurvey_threshold"'.
 2764:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2765:                 '</td></tr>'."\n".
 2766:                 '<tr><td><span class="LC_nobreak">'.
 2767:                 $choices{'uploadquota'}.
 2768:                 '</span></td>'.
 2769:                 '<td align="right" class="LC_right_item">'.
 2770:                 '<table><tr>';
 2771:         foreach my $type (@types) {
 2772:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 2773:                            '<input type="text" name="uploadquota_'.$type.'"'.
 2774:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 2775:         }
 2776:         $datatable .= '</tr></table></td></tr>'."\n";
 2777:         $itemcount += 2;
 2778:         my $onclick = 'toggleCredits(this.form);';
 2779:         my $display = 'none';
 2780:         if ($currusecredits) {
 2781:             $display = 'block';
 2782:         }
 2783:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2784:                          '<span class="LC_nobreak">'.
 2785:                          &mt('Default credits for official courses [_1]',
 2786:                          '<input type="text" name="official_credits" value="'.
 2787:                          $def_official_credits.'" size="3" />').
 2788:                          '</span><br />'.
 2789:                          '<span class="LC_nobreak">'.
 2790:                          &mt('Default credits for unofficial courses [_1]',
 2791:                          '<input type="text" name="unofficial_credits" value="'.
 2792:                          $def_unofficial_credits.'" size="3" />').
 2793:                          '</span><br />'.
 2794:                          '<span class="LC_nobreak">'.
 2795:                          &mt('Default credits for textbook courses [_1]',
 2796:                          '<input type="text" name="textbook_credits" value="'.
 2797:                          $def_textbook_credits.'" size="3" />').
 2798:                          '</span></div>'."\n";
 2799:         %defaultchecked = ('coursecredits' => 'off');
 2800:         @toggles = ('coursecredits');
 2801:         my $current = {
 2802:                         'coursecredits' => $currusecredits,
 2803:                       };
 2804:         (my $table,$itemcount) =
 2805:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2806:                                \%choices,$itemcount,$onclick,$additional);
 2807:         $datatable .= $table;
 2808:     }
 2809:     $$rowtotal += $itemcount;
 2810:     return $datatable;
 2811: }
 2812: 
 2813: sub print_usersessions {
 2814:     my ($position,$dom,$settings,$rowtotal) = @_;
 2815:     my ($css_class,$datatable,%checked,%choices);
 2816:     my (%by_ip,%by_location,@intdoms);
 2817:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 2818: 
 2819:     my @alldoms = &Apache::lonnet::all_domains();
 2820:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 2821:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2822:     my %altids = &id_for_thisdom(%servers);
 2823:     my $itemcount = 1;
 2824:     if ($position eq 'top') {
 2825:         if (keys(%serverhomes) > 1) {
 2826:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 2827:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 2828:         } else {
 2829:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2830:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 2831:         }
 2832:     } else {
 2833:         if (keys(%by_location) == 0) {
 2834:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2835:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 2836:         } else {
 2837:             my %lt = &usersession_titles();
 2838:             my $numinrow = 5;
 2839:             my $prefix;
 2840:             my @types;
 2841:             if ($position eq 'bottom') {
 2842:                 $prefix = 'remote';
 2843:                 @types = ('version','excludedomain','includedomain');
 2844:             } else {
 2845:                 $prefix = 'hosted';
 2846:                 @types = ('excludedomain','includedomain');
 2847:             }
 2848:             my (%current,%checkedon,%checkedoff);
 2849:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 2850:             my @locations = sort(keys(%by_location));
 2851:             foreach my $type (@types) {
 2852:                 $checkedon{$type} = '';
 2853:                 $checkedoff{$type} = ' checked="checked"';
 2854:             }
 2855:             if (ref($settings) eq 'HASH') {
 2856:                 if (ref($settings->{$prefix}) eq 'HASH') {
 2857:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 2858:                         $current{$key} = $settings->{$prefix}{$key};
 2859:                         if ($key eq 'version') {
 2860:                             if ($current{$key} ne '') {
 2861:                                 $checkedon{$key} = ' checked="checked"';
 2862:                                 $checkedoff{$key} = '';
 2863:                             }
 2864:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 2865:                             $checkedon{$key} = ' checked="checked"';
 2866:                             $checkedoff{$key} = '';
 2867:                         }
 2868:                     }
 2869:                 }
 2870:             }
 2871:             foreach my $type (@types) {
 2872:                 next if ($type ne 'version' && !@locations);
 2873:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2874:                 $datatable .= '<tr'.$css_class.'>
 2875:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 2876:                                <span class="LC_nobreak">&nbsp;
 2877:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 2878:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 2879:                 if ($type eq 'version') {
 2880:                     my $selector = '<select name="'.$prefix.'_version">';
 2881:                     foreach my $version (@lcversions) {
 2882:                         my $selected = '';
 2883:                         if ($current{'version'} eq $version) {
 2884:                             $selected = ' selected="selected"';
 2885:                         }
 2886:                         $selector .= ' <option value="'.$version.'"'.
 2887:                                      $selected.'>'.$version.'</option>';
 2888:                     }
 2889:                     $selector .= '</select> ';
 2890:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 2891:                 } else {
 2892:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 2893:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 2894:                                  ' />'.('&nbsp;'x2).
 2895:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 2896:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 2897:                                  "\n".
 2898:                                  '</div><div><table>';
 2899:                     my $rem;
 2900:                     for (my $i=0; $i<@locations; $i++) {
 2901:                         my ($showloc,$value,$checkedtype);
 2902:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 2903:                             my $ip = $by_location{$locations[$i]}->[0];
 2904:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 2905:                                  $value = join(':',@{$by_ip{$ip}});
 2906:                                 $showloc = join(', ',@{$by_ip{$ip}});
 2907:                                 if (ref($current{$type}) eq 'ARRAY') {
 2908:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 2909:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 2910:                                             $checkedtype = ' checked="checked"';
 2911:                                             last;
 2912:                                         }
 2913:                                     }
 2914:                                 }
 2915:                             }
 2916:                         }
 2917:                         $rem = $i%($numinrow);
 2918:                         if ($rem == 0) {
 2919:                             if ($i > 0) {
 2920:                                 $datatable .= '</tr>';
 2921:                             }
 2922:                             $datatable .= '<tr>';
 2923:                         }
 2924:                         $datatable .= '<td class="LC_left_item">'.
 2925:                                       '<span class="LC_nobreak"><label>'.
 2926:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 2927:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 2928:                                       '</label></span></td>';
 2929:                     }
 2930:                     $rem = @locations%($numinrow);
 2931:                     my $colsleft = $numinrow - $rem;
 2932:                     if ($colsleft > 1 ) {
 2933:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2934:                                       '&nbsp;</td>';
 2935:                     } elsif ($colsleft == 1) {
 2936:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 2937:                     }
 2938:                     $datatable .= '</tr></table>';
 2939:                 }
 2940:                 $datatable .= '</td></tr>';
 2941:                 $itemcount ++;
 2942:             }
 2943:         }
 2944:     }
 2945:     $$rowtotal += $itemcount;
 2946:     return $datatable;
 2947: }
 2948: 
 2949: sub build_location_hashes {
 2950:     my ($intdoms,$by_ip,$by_location) = @_;
 2951:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 2952:                   (ref($by_location) eq 'HASH')); 
 2953:     my %iphost = &Apache::lonnet::get_iphost();
 2954:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2955:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 2956:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 2957:         foreach my $id (@{$iphost{$primary_ip}}) {
 2958:             my $intdom = &Apache::lonnet::internet_dom($id);
 2959:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 2960:                 push(@{$intdoms},$intdom);
 2961:             }
 2962:         }
 2963:     }
 2964:     foreach my $ip (keys(%iphost)) {
 2965:         if (ref($iphost{$ip}) eq 'ARRAY') {
 2966:             foreach my $id (@{$iphost{$ip}}) {
 2967:                 my $location = &Apache::lonnet::internet_dom($id);
 2968:                 if ($location) {
 2969:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 2970:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2971:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 2972:                             push(@{$by_ip->{$ip}},$location);
 2973:                         }
 2974:                     } else {
 2975:                         $by_ip->{$ip} = [$location];
 2976:                     }
 2977:                 }
 2978:             }
 2979:         }
 2980:     }
 2981:     foreach my $ip (sort(keys(%{$by_ip}))) {
 2982:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2983:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 2984:             my $first = $by_ip->{$ip}->[0];
 2985:             if (ref($by_location->{$first}) eq 'ARRAY') {
 2986:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 2987:                     push(@{$by_location->{$first}},$ip);
 2988:                 }
 2989:             } else {
 2990:                 $by_location->{$first} = [$ip];
 2991:             }
 2992:         }
 2993:     }
 2994:     return;
 2995: }
 2996: 
 2997: sub current_offloads_to {
 2998:     my ($dom,$settings,$servers) = @_;
 2999:     my (%spareid,%otherdomconfigs);
 3000:     if (ref($servers) eq 'HASH') {
 3001:         foreach my $lonhost (sort(keys(%{$servers}))) {
 3002:             my $gotspares;
 3003:             if (ref($settings) eq 'HASH') {
 3004:                 if (ref($settings->{'spares'}) eq 'HASH') {
 3005:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 3006:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 3007:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 3008:                         $gotspares = 1;
 3009:                     }
 3010:                 }
 3011:             }
 3012:             unless ($gotspares) {
 3013:                 my $gotspares;
 3014:                 my $serverhomeID =
 3015:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 3016:                 my $serverhomedom =
 3017:                     &Apache::lonnet::host_domain($serverhomeID);
 3018:                 if ($serverhomedom ne $dom) {
 3019:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 3020:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3021:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3022:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3023:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3024:                                 $gotspares = 1;
 3025:                             }
 3026:                         }
 3027:                     } else {
 3028:                         $otherdomconfigs{$serverhomedom} =
 3029:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 3030:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 3031:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3032:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3033:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 3034:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3035:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3036:                                         $gotspares = 1;
 3037:                                     }
 3038:                                 }
 3039:                             }
 3040:                         }
 3041:                     }
 3042:                 }
 3043:             }
 3044:             unless ($gotspares) {
 3045:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3046:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3047:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3048:                } else {
 3049:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 3050:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 3051:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3052:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3053:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3054:                     } else {
 3055:                         my %what = (
 3056:                              spareid => 1,
 3057:                         );
 3058:                         my ($result,$returnhash) = 
 3059:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 3060:                         if ($result eq 'ok') { 
 3061:                             if (ref($returnhash) eq 'HASH') {
 3062:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 3063:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 3064:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 3065:                                 }
 3066:                             }
 3067:                         }
 3068:                     }
 3069:                 }
 3070:             }
 3071:         }
 3072:     }
 3073:     return %spareid;
 3074: }
 3075: 
 3076: sub spares_row {
 3077:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 3078:     my $css_class;
 3079:     my $numinrow = 4;
 3080:     my $itemcount = 1;
 3081:     my $datatable;
 3082:     my %typetitles = &sparestype_titles();
 3083:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3084:         foreach my $server (sort(keys(%{$servers}))) {
 3085:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 3086:             my ($othercontrol,$serverdom);
 3087:             if ($serverhome ne $server) {
 3088:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 3089:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3090:             } else {
 3091:                 $serverdom = &Apache::lonnet::host_domain($server);
 3092:                 if ($serverdom ne $dom) {
 3093:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3094:                 }
 3095:             }
 3096:             next unless (ref($spareid->{$server}) eq 'HASH');
 3097:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3098:             $datatable .= '<tr'.$css_class.'>
 3099:                            <td rowspan="2">
 3100:                             <span class="LC_nobreak">'.
 3101:                           &mt('[_1] when busy, offloads to:'
 3102:                               ,'<b>'.$server.'</b>').
 3103:                           "\n";
 3104:             my (%current,%canselect);
 3105:             my @choices = 
 3106:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 3107:             foreach my $type ('primary','default') {
 3108:                 if (ref($spareid->{$server}) eq 'HASH') {
 3109:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 3110:                         my @spares = @{$spareid->{$server}{$type}};
 3111:                         if (@spares > 0) {
 3112:                             if ($othercontrol) {
 3113:                                 $current{$type} = join(', ',@spares);
 3114:                             } else {
 3115:                                 $current{$type} .= '<table>';
 3116:                                 my $numspares = scalar(@spares);
 3117:                                 for (my $i=0;  $i<@spares; $i++) {
 3118:                                     my $rem = $i%($numinrow);
 3119:                                     if ($rem == 0) {
 3120:                                         if ($i > 0) {
 3121:                                             $current{$type} .= '</tr>';
 3122:                                         }
 3123:                                         $current{$type} .= '<tr>';
 3124:                                     }
 3125:                                     $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;'.
 3126:                                                        $spareid->{$server}{$type}[$i].
 3127:                                                        '</label></td>'."\n";
 3128:                                 }
 3129:                                 my $rem = @spares%($numinrow);
 3130:                                 my $colsleft = $numinrow - $rem;
 3131:                                 if ($colsleft > 1 ) {
 3132:                                     $current{$type} .= '<td colspan="'.$colsleft.
 3133:                                                        '" class="LC_left_item">'.
 3134:                                                        '&nbsp;</td>';
 3135:                                 } elsif ($colsleft == 1) {
 3136:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 3137:                                 }
 3138:                                 $current{$type} .= '</tr></table>';
 3139:                             }
 3140:                         }
 3141:                     }
 3142:                     if ($current{$type} eq '') {
 3143:                         $current{$type} = &mt('None specified');
 3144:                     }
 3145:                     if ($othercontrol) {
 3146:                         if ($type eq 'primary') {
 3147:                             $canselect{$type} = $othercontrol;
 3148:                         }
 3149:                     } else {
 3150:                         $canselect{$type} = 
 3151:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 3152:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 3153:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 3154:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 3155:                         if (@choices > 0) {
 3156:                             foreach my $lonhost (@choices) {
 3157:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 3158:                             }
 3159:                         }
 3160:                         $canselect{$type} .= '</select>'."\n";
 3161:                     }
 3162:                 } else {
 3163:                     $current{$type} = &mt('Could not be determined');
 3164:                     if ($type eq 'primary') {
 3165:                         $canselect{$type} =  $othercontrol;
 3166:                     }
 3167:                 }
 3168:                 if ($type eq 'default') {
 3169:                     $datatable .= '<tr'.$css_class.'>';
 3170:                 }
 3171:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 3172:                               '<td>'.$current{$type}.'</td>'."\n".
 3173:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 3174:             }
 3175:             $itemcount ++;
 3176:         }
 3177:     }
 3178:     $$rowtotal += $itemcount;
 3179:     return $datatable;
 3180: }
 3181: 
 3182: sub possible_newspares {
 3183:     my ($server,$currspares,$serverhomes,$altids) = @_;
 3184:     my $serverhostname = &Apache::lonnet::hostname($server);
 3185:     my %excluded;
 3186:     if ($serverhostname ne '') {
 3187:         %excluded = (
 3188:                        $serverhostname => 1,
 3189:                     );
 3190:     }
 3191:     if (ref($currspares) eq 'HASH') {
 3192:         foreach my $type (keys(%{$currspares})) {
 3193:             if (ref($currspares->{$type}) eq 'ARRAY') {
 3194:                 if (@{$currspares->{$type}} > 0) {
 3195:                     foreach my $curr (@{$currspares->{$type}}) {
 3196:                         my $hostname = &Apache::lonnet::hostname($curr);
 3197:                         $excluded{$hostname} = 1;
 3198:                     }
 3199:                 }
 3200:             }
 3201:         }
 3202:     }
 3203:     my @choices;
 3204:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3205:         if (keys(%{$serverhomes}) > 1) {
 3206:             foreach my $name (sort(keys(%{$serverhomes}))) {
 3207:                 unless ($excluded{$name}) {
 3208:                     if (exists($altids->{$serverhomes->{$name}})) {
 3209:                         push(@choices,$altids->{$serverhomes->{$name}});
 3210:                     } else {
 3211:                         push(@choices,$serverhomes->{$name});
 3212:                     }
 3213:                 }
 3214:             }
 3215:         }
 3216:     }
 3217:     return sort(@choices);
 3218: }
 3219: 
 3220: sub print_loadbalancing {
 3221:     my ($dom,$settings,$rowtotal) = @_;
 3222:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 3223:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 3224:     my $numinrow = 1;
 3225:     my $datatable;
 3226:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3227:     my (%currbalancer,%currtargets,%currrules,%existing);
 3228:     if (ref($settings) eq 'HASH') {
 3229:         %existing = %{$settings};
 3230:     }
 3231:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 3232:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 3233:                                   \%currtargets,\%currrules);
 3234:     } else {
 3235:         return;
 3236:     }
 3237:     my ($othertitle,$usertypes,$types) =
 3238:         &Apache::loncommon::sorted_inst_types($dom);
 3239:     my $rownum = 8;
 3240:     if (ref($types) eq 'ARRAY') {
 3241:         $rownum += scalar(@{$types});
 3242:     }
 3243:     my @css_class = ('LC_odd_row','LC_even_row');
 3244:     my $balnum = 0;
 3245:     my $islast;
 3246:     my (@toshow,$disabledtext);
 3247:     if (keys(%currbalancer) > 0) {
 3248:         @toshow = sort(keys(%currbalancer));
 3249:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3250:             push(@toshow,'');
 3251:         }
 3252:     } else {
 3253:         @toshow = ('');
 3254:         $disabledtext = &mt('No existing load balancer');
 3255:     }
 3256:     foreach my $lonhost (@toshow) {
 3257:         if ($balnum == scalar(@toshow)-1) {
 3258:             $islast = 1;
 3259:         } else {
 3260:             $islast = 0;
 3261:         }
 3262:         my $cssidx = $balnum%2;
 3263:         my $targets_div_style = 'display: none';
 3264:         my $disabled_div_style = 'display: block';
 3265:         my $homedom_div_style = 'display: none';
 3266:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3267:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3268:                       '<p>';
 3269:         if ($lonhost eq '') {
 3270:             $datatable .= '<span class="LC_nobreak">';
 3271:             if (keys(%currbalancer) > 0) {
 3272:                 $datatable .= &mt('Add balancer:');
 3273:             } else {
 3274:                 $datatable .= &mt('Enable balancer:');
 3275:             }
 3276:             $datatable .= '&nbsp;'.
 3277:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3278:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3279:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3280:                           '<option value="" selected="selected">'.&mt('None').
 3281:                           '</option>'."\n";
 3282:             foreach my $server (sort(keys(%servers))) {
 3283:                 next if ($currbalancer{$server});
 3284:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3285:             }
 3286:             $datatable .=
 3287:                 '</select>'."\n".
 3288:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3289:         } else {
 3290:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3291:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3292:                            &mt('Stop balancing').'</label>'.
 3293:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3294:             $targets_div_style = 'display: block';
 3295:             $disabled_div_style = 'display: none';
 3296:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3297:                 $homedom_div_style = 'display: block';
 3298:             }
 3299:         }
 3300:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3301:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3302:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3303:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3304:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3305:         my @sparestypes = ('primary','default');
 3306:         my %typetitles = &sparestype_titles();
 3307:         foreach my $sparetype (@sparestypes) {
 3308:             my $targettable;
 3309:             for (my $i=0; $i<$numspares; $i++) {
 3310:                 my $checked;
 3311:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3312:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3313:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3314:                             $checked = ' checked="checked"';
 3315:                         }
 3316:                     }
 3317:                 }
 3318:                 my ($chkboxval,$disabled);
 3319:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3320:                     $chkboxval = $spares[$i];
 3321:                 }
 3322:                 if (exists($currbalancer{$spares[$i]})) {
 3323:                     $disabled = ' disabled="disabled"';
 3324:                 }
 3325:                 $targettable .=
 3326:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3327:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3328:                     '</span></label></td>';
 3329:                 my $rem = $i%($numinrow);
 3330:                 if ($rem == 0) {
 3331:                     if (($i > 0) && ($i < $numspares-1)) {
 3332:                         $targettable .= '</tr>';
 3333:                     }
 3334:                     if ($i < $numspares-1) {
 3335:                         $targettable .= '<tr>';
 3336:                     }
 3337:                 }
 3338:             }
 3339:             if ($targettable ne '') {
 3340:                 my $rem = $numspares%($numinrow);
 3341:                 my $colsleft = $numinrow - $rem;
 3342:                 if ($colsleft > 1 ) {
 3343:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3344:                                     '&nbsp;</td>';
 3345:                 } elsif ($colsleft == 1) {
 3346:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3347:                 }
 3348:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3349:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3350:             }
 3351:         }
 3352:         $datatable .= '</div></td></tr>'.
 3353:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3354:                                            $othertitle,$usertypes,$types,\%servers,
 3355:                                            \%currbalancer,$lonhost,
 3356:                                            $targets_div_style,$homedom_div_style,
 3357:                                            $css_class[$cssidx],$balnum,$islast);
 3358:         $$rowtotal += $rownum;
 3359:         $balnum ++;
 3360:     }
 3361:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3362:     return $datatable;
 3363: }
 3364: 
 3365: sub get_loadbalancers_config {
 3366:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3367:     return unless ((ref($servers) eq 'HASH') &&
 3368:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3369:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3370:     if (keys(%{$existing}) > 0) {
 3371:         my $oldlonhost;
 3372:         foreach my $key (sort(keys(%{$existing}))) {
 3373:             if ($key eq 'lonhost') {
 3374:                 $oldlonhost = $existing->{'lonhost'};
 3375:                 $currbalancer->{$oldlonhost} = 1;
 3376:             } elsif ($key eq 'targets') {
 3377:                 if ($oldlonhost) {
 3378:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3379:                 }
 3380:             } elsif ($key eq 'rules') {
 3381:                 if ($oldlonhost) {
 3382:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3383:                 }
 3384:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3385:                 $currbalancer->{$key} = 1;
 3386:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3387:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3388:             }
 3389:         }
 3390:     } else {
 3391:         my ($balancerref,$targetsref) =
 3392:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3393:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3394:             foreach my $server (sort(keys(%{$balancerref}))) {
 3395:                 $currbalancer->{$server} = 1;
 3396:                 $currtargets->{$server} = $targetsref->{$server};
 3397:             }
 3398:         }
 3399:     }
 3400:     return;
 3401: }
 3402: 
 3403: sub loadbalancing_rules {
 3404:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3405:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3406:         $css_class,$balnum,$islast) = @_;
 3407:     my $output;
 3408:     my $num = 0;
 3409:     my ($alltypes,$othertypes,$titles) =
 3410:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3411:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3412:         foreach my $type (@{$alltypes}) {
 3413:             $num ++;
 3414:             my $current;
 3415:             if (ref($currrules) eq 'HASH') {
 3416:                 $current = $currrules->{$type};
 3417:             }
 3418:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3419:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3420:                     $current = '';
 3421:                 }
 3422:             }
 3423:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3424:                                              $servers,$currbalancer,$lonhost,$dom,
 3425:                                              $targets_div_style,$homedom_div_style,
 3426:                                              $css_class,$balnum,$num,$islast);
 3427:         }
 3428:     }
 3429:     return $output;
 3430: }
 3431: 
 3432: sub loadbalancing_titles {
 3433:     my ($dom,$intdom,$usertypes,$types) = @_;
 3434:     my %othertypes = (
 3435:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3436:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3437:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3438:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3439:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 3440:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 3441:                      );
 3442:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 3443:     if (ref($types) eq 'ARRAY') {
 3444:         unshift(@alltypes,@{$types},'default');
 3445:     }
 3446:     my %titles;
 3447:     foreach my $type (@alltypes) {
 3448:         if ($type =~ /^_LC_/) {
 3449:             $titles{$type} = $othertypes{$type};
 3450:         } elsif ($type eq 'default') {
 3451:             $titles{$type} = &mt('All users from [_1]',$dom);
 3452:             if (ref($types) eq 'ARRAY') {
 3453:                 if (@{$types} > 0) {
 3454:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3455:                 }
 3456:             }
 3457:         } elsif (ref($usertypes) eq 'HASH') {
 3458:             $titles{$type} = $usertypes->{$type};
 3459:         }
 3460:     }
 3461:     return (\@alltypes,\%othertypes,\%titles);
 3462: }
 3463: 
 3464: sub loadbalance_rule_row {
 3465:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3466:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3467:     my @rulenames;
 3468:     my %ruletitles = &offloadtype_text();
 3469:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 3470:         @rulenames = ('balancer','offloadedto');
 3471:     } else {
 3472:         @rulenames = ('default','homeserver');
 3473:         if ($type eq '_LC_external') {
 3474:             push(@rulenames,'externalbalancer');
 3475:         } else {
 3476:             push(@rulenames,'specific');
 3477:         }
 3478:         push(@rulenames,'none');
 3479:     }
 3480:     my $style = $targets_div_style;
 3481:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3482:         $style = $homedom_div_style;
 3483:     }
 3484:     my $space;
 3485:     if ($islast && $num == 1) {
 3486:         $space = '<div display="inline-block">&nbsp;</div>';
 3487:     }
 3488:     my $output =
 3489:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3490:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3491:         '<td valaign="top">'.$space.
 3492:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3493:     for (my $i=0; $i<@rulenames; $i++) {
 3494:         my $rule = $rulenames[$i];
 3495:         my ($checked,$extra);
 3496:         if ($rulenames[$i] eq 'default') {
 3497:             $rule = '';
 3498:         }
 3499:         if ($rulenames[$i] eq 'specific') {
 3500:             if (ref($servers) eq 'HASH') {
 3501:                 my $default;
 3502:                 if (($current ne '') && (exists($servers->{$current}))) {
 3503:                     $checked = ' checked="checked"';
 3504:                 }
 3505:                 unless ($checked) {
 3506:                     $default = ' selected="selected"';
 3507:                 }
 3508:                 $extra =
 3509:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3510:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3511:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3512:                     '<option value=""'.$default.'></option>'."\n";
 3513:                 foreach my $server (sort(keys(%{$servers}))) {
 3514:                     if (ref($currbalancer) eq 'HASH') {
 3515:                         next if (exists($currbalancer->{$server}));
 3516:                     }
 3517:                     my $selected;
 3518:                     if ($server eq $current) {
 3519:                         $selected = ' selected="selected"';
 3520:                     }
 3521:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3522:                 }
 3523:                 $extra .= '</select>';
 3524:             }
 3525:         } elsif ($rule eq $current) {
 3526:             $checked = ' checked="checked"';
 3527:         }
 3528:         $output .= '<span class="LC_nobreak"><label>'.
 3529:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3530:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3531:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3532:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3533:                    '</label>'.$extra.'</span><br />'."\n";
 3534:     }
 3535:     $output .= '</div></td></tr>'."\n";
 3536:     return $output;
 3537: }
 3538: 
 3539: sub offloadtype_text {
 3540:     my %ruletitles = &Apache::lonlocal::texthash (
 3541:            'default'          => 'Offloads to default destinations',
 3542:            'homeserver'       => "Offloads to user's home server",
 3543:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3544:            'specific'         => 'Offloads to specific server',
 3545:            'none'             => 'No offload',
 3546:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 3547:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 3548:     );
 3549:     return %ruletitles;
 3550: }
 3551: 
 3552: sub sparestype_titles {
 3553:     my %typestitles = &Apache::lonlocal::texthash (
 3554:                           'primary' => 'primary',
 3555:                           'default' => 'default',
 3556:                       );
 3557:     return %typestitles;
 3558: }
 3559: 
 3560: sub contact_titles {
 3561:     my %titles = &Apache::lonlocal::texthash (
 3562:                    'supportemail' => 'Support E-mail address',
 3563:                    'adminemail'   => 'Default Server Admin E-mail address',
 3564:                    'errormail'    => 'Error reports to be e-mailed to',
 3565:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3566:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3567:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3568:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3569:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3570:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 3571:                  );
 3572:     my %short_titles = &Apache::lonlocal::texthash (
 3573:                            adminemail   => 'Admin E-mail address',
 3574:                            supportemail => 'Support E-mail',
 3575:                        );   
 3576:     return (\%titles,\%short_titles);
 3577: }
 3578: 
 3579: sub tool_titles {
 3580:     my %titles = &Apache::lonlocal::texthash (
 3581:                      aboutme    => 'Personal web page',
 3582:                      blog       => 'Blog',
 3583:                      webdav     => 'WebDAV',
 3584:                      portfolio  => 'Portfolio',
 3585:                      official   => 'Official courses (with institutional codes)',
 3586:                      unofficial => 'Unofficial courses',
 3587:                      community  => 'Communities',
 3588:                      textbook   => 'Textbook courses',
 3589:                  );
 3590:     return %titles;
 3591: }
 3592: 
 3593: sub courserequest_titles {
 3594:     my %titles = &Apache::lonlocal::texthash (
 3595:                                    official   => 'Official',
 3596:                                    unofficial => 'Unofficial',
 3597:                                    community  => 'Communities',
 3598:                                    textbook   => 'Textbook',
 3599:                                    norequest  => 'Not allowed',
 3600:                                    approval   => 'Approval by Dom. Coord.',
 3601:                                    validate   => 'With validation',
 3602:                                    autolimit  => 'Numerical limit',
 3603:                                    unlimited  => '(blank for unlimited)',
 3604:                  );
 3605:     return %titles;
 3606: }
 3607: 
 3608: sub authorrequest_titles {
 3609:     my %titles = &Apache::lonlocal::texthash (
 3610:                                    norequest  => 'Not allowed',
 3611:                                    approval   => 'Approval by Dom. Coord.',
 3612:                                    automatic  => 'Automatic approval',
 3613:                  );
 3614:     return %titles;
 3615: }
 3616: 
 3617: sub courserequest_conditions {
 3618:     my %conditions = &Apache::lonlocal::texthash (
 3619:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3620:        validate   => '(Processing of request subject to institutional validation).',
 3621:                  );
 3622:     return %conditions;
 3623: }
 3624: 
 3625: 
 3626: sub print_usercreation {
 3627:     my ($position,$dom,$settings,$rowtotal) = @_;
 3628:     my $numinrow = 4;
 3629:     my $datatable;
 3630:     if ($position eq 'top') {
 3631:         $$rowtotal ++;
 3632:         my $rowcount = 0;
 3633:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3634:         if (ref($rules) eq 'HASH') {
 3635:             if (keys(%{$rules}) > 0) {
 3636:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3637:                                                 $ruleorder,$numinrow,$rowcount);
 3638:                 $$rowtotal ++;
 3639:                 $rowcount ++;
 3640:             }
 3641:         }
 3642:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3643:         if (ref($idrules) eq 'HASH') {
 3644:             if (keys(%{$idrules}) > 0) {
 3645:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3646:                                                 $idruleorder,$numinrow,$rowcount);
 3647:                 $$rowtotal ++;
 3648:                 $rowcount ++;
 3649:             }
 3650:         }
 3651:         if ($rowcount == 0) {
 3652:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3653:             $$rowtotal ++;
 3654:             $rowcount ++;
 3655:         }
 3656:     } elsif ($position eq 'middle') {
 3657:         my @creators = ('author','course','requestcrs');
 3658:         my ($rules,$ruleorder) =
 3659:             &Apache::lonnet::inst_userrules($dom,'username');
 3660:         my %lt = &usercreation_types();
 3661:         my %checked;
 3662:         if (ref($settings) eq 'HASH') {
 3663:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 3664:                 foreach my $item (@creators) {
 3665:                     $checked{$item} = $settings->{'cancreate'}{$item};
 3666:                 }
 3667:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3668:                 foreach my $item (@creators) {
 3669:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3670:                         $checked{$item} = 'none';
 3671:                     }
 3672:                 }
 3673:             }
 3674:         }
 3675:         my $rownum = 0;
 3676:         foreach my $item (@creators) {
 3677:             $rownum ++;
 3678:             if ($checked{$item} eq '') {
 3679:                 $checked{$item} = 'any';
 3680:             }
 3681:             my $css_class;
 3682:             if ($rownum%2) {
 3683:                 $css_class = '';
 3684:             } else {
 3685:                 $css_class = ' class="LC_odd_row" ';
 3686:             }
 3687:             $datatable .= '<tr'.$css_class.'>'.
 3688:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3689:                          '</span></td><td align="right">';
 3690:             my @options = ('any');
 3691:             if (ref($rules) eq 'HASH') {
 3692:                 if (keys(%{$rules}) > 0) {
 3693:                     push(@options,('official','unofficial'));
 3694:                 }
 3695:             }
 3696:             push(@options,'none');
 3697:             foreach my $option (@options) {
 3698:                 my $type = 'radio';
 3699:                 my $check = ' ';
 3700:                 if ($checked{$item} eq $option) {
 3701:                     $check = ' checked="checked" ';
 3702:                 } 
 3703:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3704:                               '<input type="'.$type.'" name="can_createuser_'.
 3705:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3706:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3707:             }
 3708:             $datatable .= '</td></tr>';
 3709:         }
 3710:     } else {
 3711:         my @contexts = ('author','course','domain');
 3712:         my @authtypes = ('int','krb4','krb5','loc');
 3713:         my %checked;
 3714:         if (ref($settings) eq 'HASH') {
 3715:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3716:                 foreach my $item (@contexts) {
 3717:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3718:                         foreach my $auth (@authtypes) {
 3719:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3720:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3721:                             }
 3722:                         }
 3723:                     }
 3724:                 }
 3725:             }
 3726:         } else {
 3727:             foreach my $item (@contexts) {
 3728:                 foreach my $auth (@authtypes) {
 3729:                     $checked{$item}{$auth} = ' checked="checked" ';
 3730:                 }
 3731:             }
 3732:         }
 3733:         my %title = &context_names();
 3734:         my %authname = &authtype_names();
 3735:         my $rownum = 0;
 3736:         my $css_class; 
 3737:         foreach my $item (@contexts) {
 3738:             if ($rownum%2) {
 3739:                 $css_class = '';
 3740:             } else {
 3741:                 $css_class = ' class="LC_odd_row" ';
 3742:             }
 3743:             $datatable .=   '<tr'.$css_class.'>'.
 3744:                             '<td>'.$title{$item}.
 3745:                             '</td><td class="LC_left_item">'.
 3746:                             '<span class="LC_nobreak">';
 3747:             foreach my $auth (@authtypes) {
 3748:                 $datatable .= '<label>'. 
 3749:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3750:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3751:                               $authname{$auth}.'</label>&nbsp;';
 3752:             }
 3753:             $datatable .= '</span></td></tr>';
 3754:             $rownum ++;
 3755:         }
 3756:         $$rowtotal += $rownum;
 3757:     }
 3758:     return $datatable;
 3759: }
 3760: 
 3761: sub print_selfcreation {
 3762:     my ($position,$dom,$settings,$rowtotal) = @_;
 3763:     my (@selfcreate,$createsettings,$datatable);
 3764:     if (ref($settings) eq 'HASH') {
 3765:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 3766:             $createsettings = $settings->{'cancreate'};
 3767:             if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3768:                 @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3769:             } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3770:                 if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3771:                     @selfcreate = ('email','login','sso');
 3772:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3773:                     @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3774:                 }
 3775:             }
 3776:         }
 3777:     }
 3778:     my %radiohash;
 3779:     my $numinrow = 4;
 3780:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 3781:     if ($position eq 'top') {
 3782:         my %choices = &Apache::lonlocal::texthash (
 3783:                                                       cancreate_login      => 'Institutional Login',
 3784:                                                       cancreate_sso        => 'Institutional Single Sign On',
 3785:                                                   );
 3786:         my @toggles = sort(keys(%choices));
 3787:         my %defaultchecked = (
 3788:                                'cancreate_login' => 'off',
 3789:                                'cancreate_sso'   => 'off',
 3790:                              );
 3791:         my ($onclick,$itemcount);
 3792:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 3793:                                                      \%choices,$itemcount,$onclick);
 3794:         $$rowtotal += $itemcount;
 3795: 
 3796:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3797: 
 3798:         if (ref($usertypes) eq 'HASH') {
 3799:             if (keys(%{$usertypes}) > 0) {
 3800:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3801:                                              $dom,$numinrow,$othertitle,
 3802:                                              'statustocreate',$$rowtotal);
 3803:                 $$rowtotal ++;
 3804:             }
 3805:         }
 3806:     } elsif ($position eq 'middle') {
 3807:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 3808:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3809:         $usertypes->{'default'} = $othertitle;
 3810:         if (ref($types) eq 'ARRAY') {
 3811:             push(@{$types},'default');
 3812:             $usertypes->{'default'} = $othertitle;
 3813:             foreach my $status (@{$types}) {
 3814:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 3815:                                                        $numinrow,$$rowtotal,$usertypes);
 3816:                 $$rowtotal ++;
 3817:             }
 3818:         }
 3819:     } else {
 3820:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 3821:         my %choices =
 3822:             &Apache::lonlocal::texthash(
 3823:                                           email         => 'Approved automatically',
 3824:                                           emailapproval => 'Queued for approval by DC',
 3825:                                           off           => 'Not enabled',
 3826:                                        );
 3827:         $datatable .= '<tr'.$css_class.'>'.
 3828:                       '<td>'.&mt('E-mail address as username').
 3829:                       '</td><td class="LC_left_item">'.
 3830:                       '<span class="LC_nobreak">';
 3831:         foreach my $option ('email','emailapproval','off') {
 3832:             my $checked;
 3833:             if ($option eq 'email') {
 3834:                 if ($radiohash{'cancreate_email'}) {
 3835:                     $checked = 'checked="checked"';
 3836:                 }
 3837:             } elsif ($option eq 'emailapproval') {
 3838:                 if ($radiohash{'cancreate_emailapproval'}) {
 3839:                     $checked = 'checked="checked"';
 3840:                 }
 3841:             } else {
 3842:                 if ((!$radiohash{'cancreate_email'}) && (!$radiohash{'cancreate_emailapproval'})) {
 3843:                     $checked = 'checked="checked"';  
 3844:                 }
 3845:             }
 3846:             $datatable .= '<label>'.
 3847:                           '<input type="radio" name="cancreate_email" '.
 3848:                           $checked.' value="'.$option.'" />'.
 3849:                           $choices{$option}.'</label>&nbsp;';
 3850:         }
 3851:         $$rowtotal ++;
 3852:         $datatable .= '</span></td></tr>'.
 3853:                       &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 3854:         $$rowtotal ++;
 3855:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 3856:         $numinrow = 1;
 3857:         my ($othertitle,$usertypes,$types) =  &Apache::loncommon::sorted_inst_types($dom);
 3858:         $usertypes->{'default'} = $othertitle;
 3859:         if (ref($types) eq 'ARRAY') {
 3860:             push(@{$types},'default');
 3861:             $usertypes->{'default'} = $othertitle;
 3862:             foreach my $status (@{$types}) {
 3863:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 3864:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 3865:                 $$rowtotal ++;
 3866:             }
 3867:         }
 3868:         my ($emailrules,$emailruleorder) =
 3869:             &Apache::lonnet::inst_userrules($dom,'email');
 3870:         if (ref($emailrules) eq 'HASH') {
 3871:             if (keys(%{$emailrules}) > 0) {
 3872:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 3873:                                                 $emailruleorder,$numinrow,$$rowtotal);
 3874:                 $$rowtotal ++;
 3875:             }
 3876:         }
 3877:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 3878:     }
 3879:     return $datatable;
 3880: }
 3881: 
 3882: sub captcha_choice {
 3883:     my ($context,$settings,$itemcount) = @_;
 3884:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 3885:     my %lt = &captcha_phrases();
 3886:     $keyentry = 'hidden';
 3887:     if ($context eq 'cancreate') {
 3888:         $rowname = &mt('CAPTCHA validation');
 3889:     } elsif ($context eq 'login') {
 3890:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 3891:     }
 3892:     if (ref($settings) eq 'HASH') {
 3893:         if ($settings->{'captcha'}) {
 3894:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 3895:         } else {
 3896:             $checked{'original'} = ' checked="checked"';
 3897:         }
 3898:         if ($settings->{'captcha'} eq 'recaptcha') {
 3899:             $pubtext = $lt{'pub'};
 3900:             $privtext = $lt{'priv'};
 3901:             $keyentry = 'text';
 3902:         }
 3903:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 3904:             $currpub = $settings->{'recaptchakeys'}{'public'};
 3905:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 3906:         }
 3907:     } else {
 3908:         $checked{'original'} = ' checked="checked"';
 3909:     }
 3910:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3911:     my $output = '<tr'.$css_class.'>'.
 3912:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 3913:                  '<table><tr><td>'."\n";
 3914:     foreach my $option ('original','recaptcha','notused') {
 3915:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 3916:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 3917:                    $lt{$option}.'</label></span>';
 3918:         unless ($option eq 'notused') {
 3919:             $output .= ('&nbsp;'x2)."\n";
 3920:         }
 3921:     }
 3922: #
 3923: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 3924: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 3925: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 3926: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 3927: #
 3928:     $output .= '</td></tr>'."\n".
 3929:                '<tr><td>'."\n".
 3930:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 3931:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 3932:                $currpub.'" size="40" /></span><br />'."\n".
 3933:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 3934:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 3935:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 3936:                '</td></tr>';
 3937:     return $output;
 3938: }
 3939: 
 3940: sub user_formats_row {
 3941:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 3942:     my $output;
 3943:     my %text = (
 3944:                    'username' => 'new usernames',
 3945:                    'id'       => 'IDs',
 3946:                    'email'    => 'self-created accounts (e-mail)',
 3947:                );
 3948:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3949:     $output = '<tr '.$css_class.'>'.
 3950:               '<td><span class="LC_nobreak">';
 3951:     if ($type eq 'email') {
 3952:         $output .= &mt("Formats disallowed for $text{$type}: ");
 3953:     } else {
 3954:         $output .= &mt("Format rules to check for $text{$type}: ");
 3955:     }
 3956:     $output .= '</span></td>'.
 3957:                '<td class="LC_left_item" colspan="2"><table>';
 3958:     my $rem;
 3959:     if (ref($ruleorder) eq 'ARRAY') {
 3960:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 3961:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 3962:                 my $rem = $i%($numinrow);
 3963:                 if ($rem == 0) {
 3964:                     if ($i > 0) {
 3965:                         $output .= '</tr>';
 3966:                     }
 3967:                     $output .= '<tr>';
 3968:                 }
 3969:                 my $check = ' ';
 3970:                 if (ref($settings) eq 'HASH') {
 3971:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 3972:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 3973:                             $check = ' checked="checked" ';
 3974:                         }
 3975:                     }
 3976:                 }
 3977:                 $output .= '<td class="LC_left_item">'.
 3978:                            '<span class="LC_nobreak"><label>'.
 3979:                            '<input type="checkbox" name="'.$type.'_rule" '.
 3980:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 3981:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 3982:             }
 3983:         }
 3984:         $rem = @{$ruleorder}%($numinrow);
 3985:     }
 3986:     my $colsleft = $numinrow - $rem;
 3987:     if ($colsleft > 1 ) {
 3988:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3989:                    '&nbsp;</td>';
 3990:     } elsif ($colsleft == 1) {
 3991:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 3992:     }
 3993:     $output .= '</tr></table></td></tr>';
 3994:     return $output;
 3995: }
 3996: 
 3997: sub usercreation_types {
 3998:     my %lt = &Apache::lonlocal::texthash (
 3999:                     author     => 'When adding a co-author',
 4000:                     course     => 'When adding a user to a course',
 4001:                     requestcrs => 'When requesting a course',
 4002:                     any        => 'Any',
 4003:                     official   => 'Institutional only ',
 4004:                     unofficial => 'Non-institutional only',
 4005:                     none       => 'None',
 4006:     );
 4007:     return %lt;
 4008: }
 4009: 
 4010: sub selfcreation_types {
 4011:     my %lt = &Apache::lonlocal::texthash (
 4012:                     selfcreate => 'User creates own account',
 4013:                     any        => 'Any',
 4014:                     official   => 'Institutional only ',
 4015:                     unofficial => 'Non-institutional only',
 4016:                     email      => 'E-mail address',
 4017:                     login      => 'Institutional Login',
 4018:                     sso        => 'SSO',
 4019:              );
 4020: }
 4021: 
 4022: sub authtype_names {
 4023:     my %lt = &Apache::lonlocal::texthash(
 4024:                       int    => 'Internal',
 4025:                       krb4   => 'Kerberos 4',
 4026:                       krb5   => 'Kerberos 5',
 4027:                       loc    => 'Local',
 4028:                   );
 4029:     return %lt;
 4030: }
 4031: 
 4032: sub context_names {
 4033:     my %context_title = &Apache::lonlocal::texthash(
 4034:        author => 'Creating users when an Author',
 4035:        course => 'Creating users when in a course',
 4036:        domain => 'Creating users when a Domain Coordinator',
 4037:     );
 4038:     return %context_title;
 4039: }
 4040: 
 4041: sub print_usermodification {
 4042:     my ($position,$dom,$settings,$rowtotal) = @_;
 4043:     my $numinrow = 4;
 4044:     my ($context,$datatable,$rowcount);
 4045:     if ($position eq 'top') {
 4046:         $rowcount = 0;
 4047:         $context = 'author'; 
 4048:         foreach my $role ('ca','aa') {
 4049:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4050:                                                    $numinrow,$rowcount);
 4051:             $$rowtotal ++;
 4052:             $rowcount ++;
 4053:         }
 4054:     } elsif ($position eq 'middle') {
 4055:         $context = 'course';
 4056:         $rowcount = 0;
 4057:         foreach my $role ('st','ep','ta','in','cr') {
 4058:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 4059:                                                    $numinrow,$rowcount);
 4060:             $$rowtotal ++;
 4061:             $rowcount ++;
 4062:         }
 4063:     }
 4064:     return $datatable;
 4065: }
 4066: 
 4067: sub print_defaults {
 4068:     my ($dom,$settings,$rowtotal) = @_;
 4069:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 4070:                  'datelocale_def','portal_def');
 4071:     my %defaults;
 4072:     if (ref($settings) eq 'HASH') {
 4073:         %defaults = %{$settings};
 4074:     } else {
 4075:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 4076:         foreach my $item (@items) {
 4077:             $defaults{$item} = $domdefaults{$item};
 4078:         }
 4079:     }
 4080:     my $titles = &defaults_titles($dom);
 4081:     my $rownum = 0;
 4082:     my ($datatable,$css_class);
 4083:     foreach my $item (@items) {
 4084:         if ($rownum%2) {
 4085:             $css_class = '';
 4086:         } else {
 4087:             $css_class = ' class="LC_odd_row" ';
 4088:         }
 4089:         $datatable .= '<tr'.$css_class.'>'.
 4090:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 4091:                   '</span></td><td class="LC_right_item">';
 4092:         if ($item eq 'auth_def') {
 4093:             my @authtypes = ('internal','krb4','krb5','localauth');
 4094:             my %shortauth = (
 4095:                              internal => 'int',
 4096:                              krb4 => 'krb4',
 4097:                              krb5 => 'krb5',
 4098:                              localauth  => 'loc'
 4099:                            );
 4100:             my %authnames = &authtype_names();
 4101:             foreach my $auth (@authtypes) {
 4102:                 my $checked = ' ';
 4103:                 if ($defaults{$item} eq $auth) {
 4104:                     $checked = ' checked="checked" ';
 4105:                 }
 4106:                 $datatable .= '<label><input type="radio" name="'.$item.
 4107:                               '" value="'.$auth.'"'.$checked.'/>'.
 4108:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 4109:             }
 4110:         } elsif ($item eq 'timezone_def') {
 4111:             my $includeempty = 1;
 4112:             $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 4113:         } elsif ($item eq 'datelocale_def') {
 4114:             my $includeempty = 1;
 4115:             $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 4116:         } elsif ($item eq 'lang_def') {
 4117:             my %langchoices = &get_languages_hash();
 4118:             $langchoices{''} = 'No language preference';
 4119:             %langchoices = &Apache::lonlocal::texthash(%langchoices);
 4120:             $datatable .= &Apache::loncommon::select_form($defaults{$item},$item,
 4121:                                                           \%langchoices);
 4122:         } else {
 4123:             my $size;
 4124:             if ($item eq 'portal_def') {
 4125:                 $size = ' size="25"';
 4126:             }
 4127:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 4128:                           $defaults{$item}.'"'.$size.' />';
 4129:         }
 4130:         $datatable .= '</td></tr>';
 4131:         $rownum ++;
 4132:     }
 4133:     $$rowtotal += $rownum;
 4134:     return $datatable;
 4135: }
 4136: 
 4137: sub get_languages_hash {
 4138:     my %langchoices;
 4139:     foreach my $id (&Apache::loncommon::languageids()) {
 4140:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 4141:         if ($code ne '') {
 4142:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 4143:         }
 4144:     }
 4145:     return %langchoices;
 4146: }
 4147: 
 4148: sub defaults_titles {
 4149:     my ($dom) = @_;
 4150:     my %titles = &Apache::lonlocal::texthash (
 4151:                    'auth_def'      => 'Default authentication type',
 4152:                    'auth_arg_def'  => 'Default authentication argument',
 4153:                    'lang_def'      => 'Default language',
 4154:                    'timezone_def'  => 'Default timezone',
 4155:                    'datelocale_def' => 'Default locale for dates',
 4156:                    'portal_def'     => 'Portal/Default URL',
 4157:                  );
 4158:     if ($dom) {
 4159:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 4160:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 4161:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 4162:         $protocol = 'http' if ($protocol ne 'https');
 4163:         if ($uint_dom) {
 4164:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 4165:                                          $uint_dom);
 4166:         }
 4167:     }
 4168:     return (\%titles);
 4169: }
 4170: 
 4171: sub print_scantronformat {
 4172:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 4173:     my $itemcount = 1;
 4174:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 4175:         %confhash);
 4176:     my $switchserver = &check_switchserver($dom,$confname);
 4177:     my %lt = &Apache::lonlocal::texthash (
 4178:                 default => 'Default bubblesheet format file error',
 4179:                 custom  => 'Custom bubblesheet format file error',
 4180:              );
 4181:     my %scantronfiles = (
 4182:         default => 'default.tab',
 4183:         custom => 'custom.tab',
 4184:     );
 4185:     foreach my $key (keys(%scantronfiles)) {
 4186:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 4187:                               .$scantronfiles{$key};
 4188:     }
 4189:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 4190:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 4191:         if (!$switchserver) {
 4192:             my $servadm = $r->dir_config('lonAdmEMail');
 4193:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 4194:             if ($configuserok eq 'ok') {
 4195:                 if ($author_ok eq 'ok') {
 4196:                     my %legacyfile = (
 4197:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 4198:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 4199:                     );
 4200:                     my %md5chk;
 4201:                     foreach my $type (keys(%legacyfile)) {
 4202:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 4203:                         chomp($md5chk{$type});
 4204:                     }
 4205:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 4206:                         foreach my $type (keys(%legacyfile)) {
 4207:                             ($scantronurls{$type},my $error) = 
 4208:                                 &legacy_scantronformat($r,$dom,$confname,
 4209:                                                  $type,$legacyfile{$type},
 4210:                                                  $scantronurls{$type},
 4211:                                                  $scantronfiles{$type});
 4212:                             if ($error ne '') {
 4213:                                 $error{$type} = $error;
 4214:                             }
 4215:                         }
 4216:                         if (keys(%error) == 0) {
 4217:                             $is_custom = 1;
 4218:                             $confhash{'scantron'}{'scantronformat'} = 
 4219:                                 $scantronurls{'custom'};
 4220:                             my $putresult = 
 4221:                                 &Apache::lonnet::put_dom('configuration',
 4222:                                                          \%confhash,$dom);
 4223:                             if ($putresult ne 'ok') {
 4224:                                 $error{'custom'} = 
 4225:                                     '<span class="LC_error">'.
 4226:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4227:                             }
 4228:                         }
 4229:                     } else {
 4230:                         ($scantronurls{'default'},my $error) =
 4231:                             &legacy_scantronformat($r,$dom,$confname,
 4232:                                           'default',$legacyfile{'default'},
 4233:                                           $scantronurls{'default'},
 4234:                                           $scantronfiles{'default'});
 4235:                         if ($error eq '') {
 4236:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 4237:                             my $putresult =
 4238:                                 &Apache::lonnet::put_dom('configuration',
 4239:                                                          \%confhash,$dom);
 4240:                             if ($putresult ne 'ok') {
 4241:                                 $error{'default'} =
 4242:                                     '<span class="LC_error">'.
 4243:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4244:                             }
 4245:                         } else {
 4246:                             $error{'default'} = $error;
 4247:                         }
 4248:                     }
 4249:                 }
 4250:             }
 4251:         } else {
 4252:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 4253:         }
 4254:     }
 4255:     if (ref($settings) eq 'HASH') {
 4256:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 4257:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 4258:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 4259:                 $scantronurl = '';
 4260:             } else {
 4261:                 $scantronurl = $settings->{'scantronformat'};
 4262:             }
 4263:             $is_custom = 1;
 4264:         } else {
 4265:             $scantronurl = $scantronurls{'default'};
 4266:         }
 4267:     } else {
 4268:         if ($is_custom) {
 4269:             $scantronurl = $scantronurls{'custom'};
 4270:         } else {
 4271:             $scantronurl = $scantronurls{'default'};
 4272:         }
 4273:     }
 4274:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4275:     $datatable .= '<tr'.$css_class.'>';
 4276:     if (!$is_custom) {
 4277:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 4278:                       '<span class="LC_nobreak">';
 4279:         if ($scantronurl) {
 4280:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 4281:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 4282:         } else {
 4283:             $datatable = &mt('File unavailable for display');
 4284:         }
 4285:         $datatable .= '</span></td>';
 4286:         if (keys(%error) == 0) { 
 4287:             $datatable .= '<td valign="bottom">';
 4288:             if (!$switchserver) {
 4289:                 $datatable .= &mt('Upload:').'<br />';
 4290:             }
 4291:         } else {
 4292:             my $errorstr;
 4293:             foreach my $key (sort(keys(%error))) {
 4294:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4295:             }
 4296:             $datatable .= '<td>'.$errorstr;
 4297:         }
 4298:     } else {
 4299:         if (keys(%error) > 0) {
 4300:             my $errorstr;
 4301:             foreach my $key (sort(keys(%error))) {
 4302:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4303:             } 
 4304:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 4305:         } elsif ($scantronurl) {
 4306:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 4307:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4308:             $datatable .= '<td><span class="LC_nobreak">'.
 4309:                           $link.
 4310:                           '<label><input type="checkbox" name="scantronformat_del"'.
 4311:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 4312:                           '<td><span class="LC_nobreak">&nbsp;'.
 4313:                           &mt('Replace:').'</span><br />';
 4314:         }
 4315:     }
 4316:     if (keys(%error) == 0) {
 4317:         if ($switchserver) {
 4318:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4319:         } else {
 4320:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4321:                          '<input type="file" name="scantronformat" /></span>';
 4322:         }
 4323:     }
 4324:     $datatable .= '</td></tr>';
 4325:     $$rowtotal ++;
 4326:     return $datatable;
 4327: }
 4328: 
 4329: sub legacy_scantronformat {
 4330:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 4331:     my ($url,$error);
 4332:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 4333:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 4334:         (my $result,$url) =
 4335:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 4336:                          '','',$newfile);
 4337:         if ($result ne 'ok') {
 4338:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 4339:         }
 4340:     }
 4341:     return ($url,$error);
 4342: }
 4343: 
 4344: sub print_coursecategories {
 4345:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 4346:     my $datatable;
 4347:     if ($position eq 'top') {
 4348:         my $toggle_cats_crs = ' ';
 4349:         my $toggle_cats_dom = ' checked="checked" ';
 4350:         my $can_cat_crs = ' ';
 4351:         my $can_cat_dom = ' checked="checked" ';
 4352:         my $toggle_catscomm_comm = ' ';
 4353:         my $toggle_catscomm_dom = ' checked="checked" ';
 4354:         my $can_catcomm_comm = ' ';
 4355:         my $can_catcomm_dom = ' checked="checked" ';
 4356: 
 4357:         if (ref($settings) eq 'HASH') {
 4358:             if ($settings->{'togglecats'} eq 'crs') {
 4359:                 $toggle_cats_crs = $toggle_cats_dom;
 4360:                 $toggle_cats_dom = ' ';
 4361:             }
 4362:             if ($settings->{'categorize'} eq 'crs') {
 4363:                 $can_cat_crs = $can_cat_dom;
 4364:                 $can_cat_dom = ' ';
 4365:             }
 4366:             if ($settings->{'togglecatscomm'} eq 'comm') {
 4367:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 4368:                 $toggle_catscomm_dom = ' ';
 4369:             }
 4370:             if ($settings->{'categorizecomm'} eq 'comm') {
 4371:                 $can_catcomm_comm = $can_catcomm_dom;
 4372:                 $can_catcomm_dom = ' ';
 4373:             }
 4374:         }
 4375:         my %title = &Apache::lonlocal::texthash (
 4376:                      togglecats     => 'Show/Hide a course in catalog',
 4377:                      togglecatscomm => 'Show/Hide a community in catalog',
 4378:                      categorize     => 'Assign a category to a course',
 4379:                      categorizecomm => 'Assign a category to a community',
 4380:                     );
 4381:         my %level = &Apache::lonlocal::texthash (
 4382:                      dom  => 'Set in Domain',
 4383:                      crs  => 'Set in Course',
 4384:                      comm => 'Set in Community',
 4385:                     );
 4386:         $datatable = '<tr class="LC_odd_row">'.
 4387:                   '<td>'.$title{'togglecats'}.'</td>'.
 4388:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4389:                   '<input type="radio" name="togglecats"'.
 4390:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4391:                   '<label><input type="radio" name="togglecats"'.
 4392:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4393:                   '</tr><tr>'.
 4394:                   '<td>'.$title{'categorize'}.'</td>'.
 4395:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4396:                   '<label><input type="radio" name="categorize"'.
 4397:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4398:                   '<label><input type="radio" name="categorize"'.
 4399:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4400:                   '</tr><tr class="LC_odd_row">'.
 4401:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 4402:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4403:                   '<input type="radio" name="togglecatscomm"'.
 4404:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4405:                   '<label><input type="radio" name="togglecatscomm"'.
 4406:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4407:                   '</tr><tr>'.
 4408:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 4409:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4410:                   '<label><input type="radio" name="categorizecomm"'.
 4411:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4412:                   '<label><input type="radio" name="categorizecomm"'.
 4413:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4414:                   '</tr>';
 4415:         $$rowtotal += 4;
 4416:     } else {
 4417:         my $css_class;
 4418:         my $itemcount = 1;
 4419:         my $cathash; 
 4420:         if (ref($settings) eq 'HASH') {
 4421:             $cathash = $settings->{'cats'};
 4422:         }
 4423:         if (ref($cathash) eq 'HASH') {
 4424:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4425:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4426:                                                    \%allitems,\%idx,\@jsarray);
 4427:             my $maxdepth = scalar(@cats);
 4428:             my $colattrib = '';
 4429:             if ($maxdepth > 2) {
 4430:                 $colattrib = ' colspan="2" ';
 4431:             }
 4432:             my @path;
 4433:             if (@cats > 0) {
 4434:                 if (ref($cats[0]) eq 'ARRAY') {
 4435:                     my $numtop = @{$cats[0]};
 4436:                     my $maxnum = $numtop;
 4437:                     my %default_names = (
 4438:                           instcode    => &mt('Official courses'),
 4439:                           communities => &mt('Communities'),
 4440:                     );
 4441: 
 4442:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4443:                         ($cathash->{'instcode::0'} eq '') ||
 4444:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4445:                         ($cathash->{'communities::0'} eq '')) {
 4446:                         $maxnum ++;
 4447:                     }
 4448:                     my $lastidx;
 4449:                     for (my $i=0; $i<$numtop; $i++) {
 4450:                         my $parent = $cats[0][$i];
 4451:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4452:                         my $item = &escape($parent).'::0';
 4453:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4454:                         $lastidx = $idx{$item};
 4455:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4456:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4457:                         for (my $k=0; $k<=$maxnum; $k++) {
 4458:                             my $vpos = $k+1;
 4459:                             my $selstr;
 4460:                             if ($k == $i) {
 4461:                                 $selstr = ' selected="selected" ';
 4462:                             }
 4463:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4464:                         }
 4465:                         $datatable .= '</select></span></td><td>';
 4466:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4467:                             $datatable .=  '<span class="LC_nobreak">'
 4468:                                            .$default_names{$parent}.'</span>';
 4469:                             if ($parent eq 'instcode') {
 4470:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4471:                                               .&mt('with institutional codes')
 4472:                                               .')</span></td><td'.$colattrib.'>';
 4473:                             } else {
 4474:                                 $datatable .= '<table><tr><td>';
 4475:                             }
 4476:                             $datatable .= '<span class="LC_nobreak">'
 4477:                                           .'<label><input type="radio" name="'
 4478:                                           .$parent.'" value="1" checked="checked" />'
 4479:                                           .&mt('Display').'</label>';
 4480:                             if ($parent eq 'instcode') {
 4481:                                 $datatable .= '&nbsp;';
 4482:                             } else {
 4483:                                 $datatable .= '</span></td></tr><tr><td>'
 4484:                                               .'<span class="LC_nobreak">';
 4485:                             }
 4486:                             $datatable .= '<label><input type="radio" name="'
 4487:                                           .$parent.'" value="0" />'
 4488:                                           .&mt('Do not display').'</label></span>';
 4489:                             if ($parent eq 'communities') {
 4490:                                 $datatable .= '</td></tr></table>';
 4491:                             }
 4492:                             $datatable .= '</td>';
 4493:                         } else {
 4494:                             $datatable .= $parent
 4495:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 4496:                                           .'<input type="checkbox" name="deletecategory" '
 4497:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 4498:                         }
 4499:                         my $depth = 1;
 4500:                         push(@path,$parent);
 4501:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 4502:                         pop(@path);
 4503:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 4504:                         $itemcount ++;
 4505:                     }
 4506:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4507:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 4508:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 4509:                     for (my $k=0; $k<=$maxnum; $k++) {
 4510:                         my $vpos = $k+1;
 4511:                         my $selstr;
 4512:                         if ($k == $numtop) {
 4513:                             $selstr = ' selected="selected" ';
 4514:                         }
 4515:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4516:                     }
 4517:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 4518:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 4519:                                   .'</tr>'."\n";
 4520:                     $itemcount ++;
 4521:                     foreach my $default ('instcode','communities') {
 4522:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 4523:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4524:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 4525:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 4526:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 4527:                             for (my $k=0; $k<=$maxnum; $k++) {
 4528:                                 my $vpos = $k+1;
 4529:                                 my $selstr;
 4530:                                 if ($k == $maxnum) {
 4531:                                     $selstr = ' selected="selected" ';
 4532:                                 }
 4533:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4534:                             }
 4535:                             $datatable .= '</select></span></td>'.
 4536:                                           '<td><span class="LC_nobreak">'.
 4537:                                           $default_names{$default}.'</span>';
 4538:                             if ($default eq 'instcode') {
 4539:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 4540:                                               .&mt('with institutional codes').')</span>';
 4541:                             }
 4542:                             $datatable .= '</td>'
 4543:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 4544:                                           .&mt('Display').'</label>&nbsp;'
 4545:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 4546:                                           .&mt('Do not display').'</label></span></td></tr>';
 4547:                         }
 4548:                     }
 4549:                 }
 4550:             } else {
 4551:                 $datatable .= &initialize_categories($itemcount);
 4552:             }
 4553:         } else {
 4554:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 4555:                           .&initialize_categories($itemcount);
 4556:         }
 4557:         $$rowtotal += $itemcount;
 4558:     }
 4559:     return $datatable;
 4560: }
 4561: 
 4562: sub print_serverstatuses {
 4563:     my ($dom,$settings,$rowtotal) = @_;
 4564:     my $datatable;
 4565:     my @pages = &serverstatus_pages();
 4566:     my (%namedaccess,%machineaccess);
 4567:     foreach my $type (@pages) {
 4568:         $namedaccess{$type} = '';
 4569:         $machineaccess{$type}= '';
 4570:     }
 4571:     if (ref($settings) eq 'HASH') {
 4572:         foreach my $type (@pages) {
 4573:             if (exists($settings->{$type})) {
 4574:                 if (ref($settings->{$type}) eq 'HASH') {
 4575:                     foreach my $key (keys(%{$settings->{$type}})) {
 4576:                         if ($key eq 'namedusers') {
 4577:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4578:                         } elsif ($key eq 'machines') {
 4579:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4580:                         }
 4581:                     }
 4582:                 }
 4583:             }
 4584:         }
 4585:     }
 4586:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4587:     my $rownum = 0;
 4588:     my $css_class;
 4589:     foreach my $type (@pages) {
 4590:         $rownum ++;
 4591:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4592:         $datatable .= '<tr'.$css_class.'>'.
 4593:                       '<td><span class="LC_nobreak">'.
 4594:                       $titles->{$type}.'</span></td>'.
 4595:                       '<td class="LC_left_item">'.
 4596:                       '<input type="text" name="'.$type.'_namedusers" '.
 4597:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4598:                       '<td class="LC_right_item">'.
 4599:                       '<span class="LC_nobreak">'.
 4600:                       '<input type="text" name="'.$type.'_machines" '.
 4601:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4602:                       '</td></tr>'."\n";
 4603:     }
 4604:     $$rowtotal += $rownum;
 4605:     return $datatable;
 4606: }
 4607: 
 4608: sub serverstatus_pages {
 4609:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4610:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 4611:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
 4612:             'uniquecodes','diskusage');
 4613: }
 4614: 
 4615: sub coursecategories_javascript {
 4616:     my ($settings) = @_;
 4617:     my ($output,$jstext,$cathash);
 4618:     if (ref($settings) eq 'HASH') {
 4619:         $cathash = $settings->{'cats'};
 4620:     }
 4621:     if (ref($cathash) eq 'HASH') {
 4622:         my (@cats,@jsarray,%idx);
 4623:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4624:         if (@jsarray > 0) {
 4625:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4626:             for (my $i=0; $i<@jsarray; $i++) {
 4627:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4628:                     my $catstr = join('","',@{$jsarray[$i]});
 4629:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4630:                 }
 4631:             }
 4632:         }
 4633:     } else {
 4634:         $jstext  = '    var categories = Array(1);'."\n".
 4635:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4636:     }
 4637:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4638:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4639:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4640:     $output = <<"ENDSCRIPT";
 4641: <script type="text/javascript">
 4642: // <![CDATA[
 4643: function reorderCats(form,parent,item,idx) {
 4644:     var changedVal;
 4645: $jstext
 4646:     var newpos = 'addcategory_pos';
 4647:     var current = new Array;
 4648:     if (parent == '') {
 4649:         var has_instcode = 0;
 4650:         var maxtop = categories[idx].length;
 4651:         for (var j=0; j<maxtop; j++) {
 4652:             if (categories[idx][j] == 'instcode::0') {
 4653:                 has_instcode == 1;
 4654:             }
 4655:         }
 4656:         if (has_instcode == 0) {
 4657:             categories[idx][maxtop] = 'instcode_pos';
 4658:         }
 4659:     } else {
 4660:         newpos += '_'+parent;
 4661:     }
 4662:     var maxh = 1 + categories[idx].length;
 4663:     var current = new Array;
 4664:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4665:     if (item == newpos) {
 4666:         changedVal = newitemVal;
 4667:     } else {
 4668:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4669:         current[newitemVal] = newpos;
 4670:     }
 4671:     for (var i=0; i<categories[idx].length; i++) {
 4672:         var elementName = categories[idx][i];
 4673:         if (elementName != item) {
 4674:             if (form.elements[elementName]) {
 4675:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4676:                 current[currVal] = elementName;
 4677:             }
 4678:         }
 4679:     }
 4680:     var oldVal;
 4681:     for (var j=0; j<maxh; j++) {
 4682:         if (current[j] == undefined) {
 4683:             oldVal = j;
 4684:         }
 4685:     }
 4686:     if (oldVal < changedVal) {
 4687:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4688:            var elementName = current[k];
 4689:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4690:         }
 4691:     } else {
 4692:         for (var k=changedVal; k<oldVal; k++) {
 4693:             var elementName = current[k];
 4694:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4695:         }
 4696:     }
 4697:     return;
 4698: }
 4699: 
 4700: function categoryCheck(form) {
 4701:     if (form.elements['addcategory_name'].value == 'instcode') {
 4702:         alert('$instcode_reserved\\n$choose_again');
 4703:         return false;
 4704:     }
 4705:     if (form.elements['addcategory_name'].value == 'communities') {
 4706:         alert('$communities_reserved\\n$choose_again');
 4707:         return false;
 4708:     }
 4709:     return true;
 4710: }
 4711: 
 4712: // ]]>
 4713: </script>
 4714: 
 4715: ENDSCRIPT
 4716:     return $output;
 4717: }
 4718: 
 4719: sub initialize_categories {
 4720:     my ($itemcount) = @_;
 4721:     my ($datatable,$css_class,$chgstr);
 4722:     my %default_names = (
 4723:                       instcode    => 'Official courses (with institutional codes)',
 4724:                       communities => 'Communities',
 4725:                         );
 4726:     my $select0 = ' selected="selected"';
 4727:     my $select1 = '';
 4728:     foreach my $default ('instcode','communities') {
 4729:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4730:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4731:         if ($default eq 'communities') {
 4732:             $select1 = $select0;
 4733:             $select0 = '';
 4734:         }
 4735:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4736:                      .'<select name="'.$default.'_pos">'
 4737:                      .'<option value="0"'.$select0.'>1</option>'
 4738:                      .'<option value="1"'.$select1.'>2</option>'
 4739:                      .'<option value="2">3</option></select>&nbsp;'
 4740:                      .$default_names{$default}
 4741:                      .'</span></td><td><span class="LC_nobreak">'
 4742:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4743:                      .&mt('Display').'</label>&nbsp;<label>'
 4744:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4745:                  .'</label></span></td></tr>';
 4746:         $itemcount ++;
 4747:     }
 4748:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4749:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4750:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4751:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4752:                   .'<option value="0">1</option>'
 4753:                   .'<option value="1">2</option>'
 4754:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4755:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4756:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4757:     return $datatable;
 4758: }
 4759: 
 4760: sub build_category_rows {
 4761:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 4762:     my ($text,$name,$item,$chgstr);
 4763:     if (ref($cats) eq 'ARRAY') {
 4764:         my $maxdepth = scalar(@{$cats});
 4765:         if (ref($cats->[$depth]) eq 'HASH') {
 4766:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 4767:                 my $numchildren = @{$cats->[$depth]{$parent}};
 4768:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4769:                 $text .= '<td><table class="LC_data_table">';
 4770:                 my ($idxnum,$parent_name,$parent_item);
 4771:                 my $higher = $depth - 1;
 4772:                 if ($higher == 0) {
 4773:                     $parent_name = &escape($parent).'::'.$higher;
 4774:                 } else {
 4775:                     if (ref($path) eq 'ARRAY') {
 4776:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4777:                     }
 4778:                 }
 4779:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4780:                 for (my $j=0; $j<=$numchildren; $j++) {
 4781:                     if ($j < $numchildren) {
 4782:                         $name = $cats->[$depth]{$parent}[$j];
 4783:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4784:                         $idxnum = $idx->{$item};
 4785:                     } else {
 4786:                         $name = $parent_name;
 4787:                         $item = $parent_item;
 4788:                     }
 4789:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4790:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4791:                     for (my $i=0; $i<=$numchildren; $i++) {
 4792:                         my $vpos = $i+1;
 4793:                         my $selstr;
 4794:                         if ($j == $i) {
 4795:                             $selstr = ' selected="selected" ';
 4796:                         }
 4797:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4798:                     }
 4799:                     $text .= '</select>&nbsp;';
 4800:                     if ($j < $numchildren) {
 4801:                         my $deeper = $depth+1;
 4802:                         $text .= $name.'&nbsp;'
 4803:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4804:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4805:                         if(ref($path) eq 'ARRAY') {
 4806:                             push(@{$path},$name);
 4807:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4808:                             pop(@{$path});
 4809:                         }
 4810:                     } else {
 4811:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 4812:                         if ($j == $numchildren) {
 4813:                             $text .= $name;
 4814:                         } else {
 4815:                             $text .= $item;
 4816:                         }
 4817:                         $text .= '" value="" />';
 4818:                     }
 4819:                     $text .= '</td></tr>';
 4820:                 }
 4821:                 $text .= '</table></td>';
 4822:             } else {
 4823:                 my $higher = $depth-1;
 4824:                 if ($higher == 0) {
 4825:                     $name = &escape($parent).'::'.$higher;
 4826:                 } else {
 4827:                     if (ref($path) eq 'ARRAY') {
 4828:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4829:                     }
 4830:                 }
 4831:                 my $colspan;
 4832:                 if ($parent ne 'instcode') {
 4833:                     $colspan = $maxdepth - $depth - 1;
 4834:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 4835:                 }
 4836:             }
 4837:         }
 4838:     }
 4839:     return $text;
 4840: }
 4841: 
 4842: sub modifiable_userdata_row {
 4843:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 4844:     my ($role,$rolename,$statustype);
 4845:     $role = $item;
 4846:     if ($context eq 'cancreate') {
 4847:         if ($item =~ /^emailusername_(.+)$/) {
 4848:             $statustype = $1;
 4849:             $role = 'emailusername';
 4850:             if (ref($usertypes) eq 'HASH') {
 4851:                 if ($usertypes->{$statustype}) {
 4852:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 4853:                 } else {
 4854:                     $rolename = &mt('Data provided by user');
 4855:                 }
 4856:             }
 4857:         }
 4858:     } elsif ($context eq 'selfcreate') {
 4859:         if (ref($usertypes) eq 'HASH') {
 4860:             $rolename = $usertypes->{$role};
 4861:         } else {
 4862:             $rolename = $role;
 4863:         }
 4864:     } else {
 4865:         if ($role eq 'cr') {
 4866:             $rolename = &mt('Custom role');
 4867:         } else {
 4868:             $rolename = &Apache::lonnet::plaintext($role);
 4869:         }
 4870:     }
 4871:     my (@fields,%fieldtitles);
 4872:     if (ref($fieldsref) eq 'ARRAY') {
 4873:         @fields = @{$fieldsref};
 4874:     } else {
 4875:         @fields = ('lastname','firstname','middlename','generation',
 4876:                    'permanentemail','id');
 4877:     }
 4878:     if ((ref($titlesref) eq 'HASH')) {
 4879:         %fieldtitles = %{$titlesref};
 4880:     } else {
 4881:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4882:     }
 4883:     my $output;
 4884:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4885:     $output = '<tr '.$css_class.'>'.
 4886:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 4887:               '<td class="LC_left_item" colspan="2"><table>';
 4888:     my $rem;
 4889:     my %checks;
 4890:     if (ref($settings) eq 'HASH') {
 4891:         if (ref($settings->{$context}) eq 'HASH') {
 4892:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 4893:                 my $hashref = $settings->{$context}->{$role};
 4894:                 if ($role eq 'emailusername') {
 4895:                     if ($statustype) {
 4896:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 4897:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 4898:                             if (ref($hashref) eq 'HASH') {
 4899:                                 foreach my $field (@fields) {
 4900:                                     if ($hashref->{$field}) {
 4901:                                         $checks{$field} = $hashref->{$field};
 4902:                                     }
 4903:                                 }
 4904:                             }
 4905:                         }
 4906:                     }
 4907:                 } else {
 4908:                     if (ref($hashref) eq 'HASH') {
 4909:                         foreach my $field (@fields) {
 4910:                             if ($hashref->{$field}) {
 4911:                                 $checks{$field} = ' checked="checked" ';
 4912:                             }
 4913:                         }
 4914:                     }
 4915:                 }
 4916:             }
 4917:         }
 4918:     }
 4919: 
 4920:     for (my $i=0; $i<@fields; $i++) {
 4921:         my $rem = $i%($numinrow);
 4922:         if ($rem == 0) {
 4923:             if ($i > 0) {
 4924:                 $output .= '</tr>';
 4925:             }
 4926:             $output .= '<tr>';
 4927:         }
 4928:         my $check = ' ';
 4929:         unless ($role eq 'emailusername') {
 4930:             if (exists($checks{$fields[$i]})) {
 4931:                 $check = $checks{$fields[$i]}
 4932:             } else {
 4933:                 if ($role eq 'st') {
 4934:                     if (ref($settings) ne 'HASH') {
 4935:                         $check = ' checked="checked" ';
 4936:                     }
 4937:                 }
 4938:             }
 4939:         }
 4940:         $output .= '<td class="LC_left_item">'.
 4941:                    '<span class="LC_nobreak">';
 4942:         if ($role eq 'emailusername') {
 4943:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 4944:                 $checks{$fields[$i]} = 'omit';
 4945:             }
 4946:             foreach my $option ('required','optional','omit') {
 4947:                 my $checked='';
 4948:                 if ($checks{$fields[$i]} eq $option) {
 4949:                     $checked='checked="checked" ';
 4950:                 }
 4951:                 $output .= '<label>'.
 4952:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 4953:                            &mt($option).'</label>'.('&nbsp;' x2);
 4954:             }
 4955:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 4956:         } else {
 4957:             $output .= '<label>'.
 4958:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 4959:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 4960:                        '</label>';
 4961:         }
 4962:         $output .= '</span></td>';
 4963:         $rem = @fields%($numinrow);
 4964:     }
 4965:     my $colsleft = $numinrow - $rem;
 4966:     if ($colsleft > 1 ) {
 4967:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4968:                    '&nbsp;</td>';
 4969:     } elsif ($colsleft == 1) {
 4970:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4971:     }
 4972:     $output .= '</tr></table></td></tr>';
 4973:     return $output;
 4974: }
 4975: 
 4976: sub insttypes_row {
 4977:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 4978:     my %lt = &Apache::lonlocal::texthash (
 4979:                       cansearch => 'Users allowed to search',
 4980:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 4981:                       lockablenames => 'User preference to lock name',
 4982:              );
 4983:     my $showdom;
 4984:     if ($context eq 'cansearch') {
 4985:         $showdom = ' ('.$dom.')';
 4986:     }
 4987:     my $class = 'LC_left_item';
 4988:     if ($context eq 'statustocreate') {
 4989:         $class = 'LC_right_item';
 4990:     }
 4991:     my $css_class = ' class="LC_odd_row"';
 4992:     if ($rownum ne '') { 
 4993:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 4994:     }
 4995:     my $output = '<tr'.$css_class.'>'.
 4996:                  '<td>'.$lt{$context}.$showdom.
 4997:                  '</td><td class="'.$class.'" colspan="2"><table>';
 4998:     my $rem;
 4999:     if (ref($types) eq 'ARRAY') {
 5000:         for (my $i=0; $i<@{$types}; $i++) {
 5001:             if (defined($usertypes->{$types->[$i]})) {
 5002:                 my $rem = $i%($numinrow);
 5003:                 if ($rem == 0) {
 5004:                     if ($i > 0) {
 5005:                         $output .= '</tr>';
 5006:                     }
 5007:                     $output .= '<tr>';
 5008:                 }
 5009:                 my $check = ' ';
 5010:                 if (ref($settings) eq 'HASH') {
 5011:                     if (ref($settings->{$context}) eq 'ARRAY') {
 5012:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 5013:                             $check = ' checked="checked" ';
 5014:                         }
 5015:                     } elsif ($context eq 'statustocreate') {
 5016:                         $check = ' checked="checked" ';
 5017:                     }
 5018:                 }
 5019:                 $output .= '<td class="LC_left_item">'.
 5020:                            '<span class="LC_nobreak"><label>'.
 5021:                            '<input type="checkbox" name="'.$context.'" '.
 5022:                            'value="'.$types->[$i].'"'.$check.'/>'.
 5023:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 5024:             }
 5025:         }
 5026:         $rem = @{$types}%($numinrow);
 5027:     }
 5028:     my $colsleft = $numinrow - $rem;
 5029:     if (($rem == 0) && (@{$types} > 0)) {
 5030:         $output .= '<tr>';
 5031:     }
 5032:     if ($colsleft > 1) {
 5033:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5034:     } else {
 5035:         $output .= '<td class="LC_left_item">';
 5036:     }
 5037:     my $defcheck = ' ';
 5038:     if (ref($settings) eq 'HASH') {  
 5039:         if (ref($settings->{$context}) eq 'ARRAY') {
 5040:             if (grep(/^default$/,@{$settings->{$context}})) {
 5041:                 $defcheck = ' checked="checked" ';
 5042:             }
 5043:         } elsif ($context eq 'statustocreate') {
 5044:             $defcheck = ' checked="checked" ';
 5045:         }
 5046:     }
 5047:     $output .= '<span class="LC_nobreak"><label>'.
 5048:                '<input type="checkbox" name="'.$context.'" '.
 5049:                'value="default"'.$defcheck.'/>'.
 5050:                $othertitle.'</label></span></td>'.
 5051:                '</tr></table></td></tr>';
 5052:     return $output;
 5053: }
 5054: 
 5055: sub sorted_searchtitles {
 5056:     my %searchtitles = &Apache::lonlocal::texthash(
 5057:                          'uname' => 'username',
 5058:                          'lastname' => 'last name',
 5059:                          'lastfirst' => 'last name, first name',
 5060:                      );
 5061:     my @titleorder = ('uname','lastname','lastfirst');
 5062:     return (\%searchtitles,\@titleorder);
 5063: }
 5064: 
 5065: sub sorted_searchtypes {
 5066:     my %srchtypes_desc = (
 5067:                            exact    => 'is exact match',
 5068:                            contains => 'contains ..',
 5069:                            begins   => 'begins with ..',
 5070:                          );
 5071:     my @srchtypeorder = ('exact','begins','contains');
 5072:     return (\%srchtypes_desc,\@srchtypeorder);
 5073: }
 5074: 
 5075: sub usertype_update_row {
 5076:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 5077:     my $datatable;
 5078:     my $numinrow = 4;
 5079:     foreach my $type (@{$types}) {
 5080:         if (defined($usertypes->{$type})) {
 5081:             $$rownums ++;
 5082:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 5083:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 5084:                           '</td><td class="LC_left_item"><table>';
 5085:             for (my $i=0; $i<@{$fields}; $i++) {
 5086:                 my $rem = $i%($numinrow);
 5087:                 if ($rem == 0) {
 5088:                     if ($i > 0) {
 5089:                         $datatable .= '</tr>';
 5090:                     }
 5091:                     $datatable .= '<tr>';
 5092:                 }
 5093:                 my $check = ' ';
 5094:                 if (ref($settings) eq 'HASH') {
 5095:                     if (ref($settings->{'fields'}) eq 'HASH') {
 5096:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 5097:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 5098:                                 $check = ' checked="checked" ';
 5099:                             }
 5100:                         }
 5101:                     }
 5102:                 }
 5103: 
 5104:                 if ($i == @{$fields}-1) {
 5105:                     my $colsleft = $numinrow - $rem;
 5106:                     if ($colsleft > 1) {
 5107:                         $datatable .= '<td colspan="'.$colsleft.'">';
 5108:                     } else {
 5109:                         $datatable .= '<td>';
 5110:                     }
 5111:                 } else {
 5112:                     $datatable .= '<td>';
 5113:                 }
 5114:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5115:                               '<input type="checkbox" name="updateable_'.$type.
 5116:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 5117:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 5118:             }
 5119:             $datatable .= '</tr></table></td></tr>';
 5120:         }
 5121:     }
 5122:     return $datatable;
 5123: }
 5124: 
 5125: sub modify_login {
 5126:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 5127:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 5128:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 5129:     %title = ( coursecatalog => 'Display course catalog',
 5130:                adminmail => 'Display administrator E-mail address',
 5131:                helpdesk  => 'Display "Contact Helpdesk" link',
 5132:                newuser => 'Link for visitors to create a user account',
 5133:                loginheader => 'Log-in box header');
 5134:     @offon = ('off','on');
 5135:     if (ref($domconfig{login}) eq 'HASH') {
 5136:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 5137:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 5138:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 5139:             }
 5140:         }
 5141:     }
 5142:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 5143:                                            \%domconfig,\%loginhash);
 5144:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5145:     foreach my $item (@toggles) {
 5146:         $loginhash{login}{$item} = $env{'form.'.$item};
 5147:     }
 5148:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 5149:     if (ref($colchanges{'login'}) eq 'HASH') {  
 5150:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 5151:                                          \%loginhash);
 5152:     }
 5153: 
 5154:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5155:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 5156:     if (keys(%servers) > 1) {
 5157:         foreach my $lonhost (keys(%servers)) {
 5158:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 5159:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 5160:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 5161:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 5162:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 5163:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5164:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5165:                         $changes{'loginvia'}{$lonhost} = 1;
 5166:                     } else {
 5167:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 5168:                         $changes{'loginvia'}{$lonhost} = 1;
 5169:                     }
 5170:                 } else {
 5171:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5172:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5173:                         $changes{'loginvia'}{$lonhost} = 1;
 5174:                     }
 5175:                 }
 5176:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 5177:                     foreach my $item (@loginvia_attribs) {
 5178:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 5179:                     }
 5180:                 } else {
 5181:                     foreach my $item (@loginvia_attribs) {
 5182:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5183:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5184:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 5185:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5186:                                 $new = '/';
 5187:                             }
 5188:                         }
 5189:                         if (($item eq 'custompath') && 
 5190:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5191:                             $new = '';
 5192:                         }
 5193:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 5194:                             $changes{'loginvia'}{$lonhost} = 1;
 5195:                         }
 5196:                         if ($item eq 'exempt') {
 5197:                             $new =~ s/^\s+//;
 5198:                             $new =~ s/\s+$//;
 5199:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 5200:                             my @okips;
 5201:                             foreach my $ip (@poss_ips) {
 5202:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 5203:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 5204:                                         push(@okips,$ip); 
 5205:                                     }
 5206:                                 }
 5207:                             }
 5208:                             if (@okips > 0) {
 5209:                                 $new = join(',',@okips); 
 5210:                             } else {
 5211:                                 $new = ''; 
 5212:                             }
 5213:                         }
 5214:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5215:                     }
 5216:                 }
 5217:             } else {
 5218:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5219:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5220:                     $changes{'loginvia'}{$lonhost} = 1;
 5221:                     foreach my $item (@loginvia_attribs) {
 5222:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5223:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5224:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5225:                                 $new = '/';
 5226:                             }
 5227:                         }
 5228:                         if (($item eq 'custompath') && 
 5229:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5230:                             $new = '';
 5231:                         }
 5232:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5233:                     }
 5234:                 }
 5235:             }
 5236:         }
 5237:     }
 5238: 
 5239:     my $servadm = $r->dir_config('lonAdmEMail');
 5240:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 5241:     if (ref($domconfig{'login'}) eq 'HASH') {
 5242:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 5243:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 5244:                 if ($lang eq 'nolang') {
 5245:                     push(@currlangs,$lang);
 5246:                 } elsif (defined($langchoices{$lang})) {
 5247:                     push(@currlangs,$lang);
 5248:                 } else {
 5249:                     next;
 5250:                 }
 5251:             }
 5252:         }
 5253:     }
 5254:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 5255:     if (@currlangs > 0) {
 5256:         foreach my $lang (@currlangs) {
 5257:             if (grep(/^\Q$lang\E$/,@delurls)) {
 5258:                 $changes{'helpurl'}{$lang} = 1;
 5259:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 5260:                 $changes{'helpurl'}{$lang} = 1;
 5261:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 5262:                 push(@newlangs,$lang);
 5263:             } else {
 5264:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5265:             }
 5266:         }
 5267:     }
 5268:     unless (grep(/^nolang$/,@currlangs)) {
 5269:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 5270:             $changes{'helpurl'}{'nolang'} = 1;
 5271:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 5272:             push(@newlangs,'nolang');
 5273:         }
 5274:     }
 5275:     if ($env{'form.loginhelpurl_add_lang'}) {
 5276:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 5277:             ($env{'form.loginhelpurl_add_file.filename'})) {
 5278:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 5279:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 5280:         }
 5281:     }
 5282:     if ((@newlangs > 0) || ($addedfile)) {
 5283:         my $error;
 5284:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 5285:         if ($configuserok eq 'ok') {
 5286:             if ($switchserver) {
 5287:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 5288:             } elsif ($author_ok eq 'ok') {
 5289:                 my @allnew = @newlangs;
 5290:                 if ($addedfile ne '') {
 5291:                     push(@allnew,$addedfile);
 5292:                 }
 5293:                 foreach my $lang (@allnew) {
 5294:                     my $formelem = 'loginhelpurl_'.$lang;
 5295:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 5296:                         $formelem = 'loginhelpurl_add_file';
 5297:                     }
 5298:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 5299:                                                                "help/$lang",'','',$newfile{$lang});
 5300:                     if ($result eq 'ok') {
 5301:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 5302:                         $changes{'helpurl'}{$lang} = 1;
 5303:                     } else {
 5304:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 5305:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 5306:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 5307:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 5308: 
 5309:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5310:                         }
 5311:                     }
 5312:                 }
 5313:             } else {
 5314:                 $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);
 5315:             }
 5316:         } else {
 5317:             $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);
 5318:         }
 5319:         if ($error) {
 5320:             &Apache::lonnet::logthis($error);
 5321:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5322:         }
 5323:     }
 5324:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 5325: 
 5326:     my $defaulthelpfile = '/adm/loginproblems.html';
 5327:     my $defaulttext = &mt('Default in use');
 5328: 
 5329:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 5330:                                              $dom);
 5331:     if ($putresult eq 'ok') {
 5332:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5333:         my %defaultchecked = (
 5334:                     'coursecatalog' => 'on',
 5335:                     'helpdesk'      => 'on',
 5336:                     'adminmail'     => 'off',
 5337:                     'newuser'       => 'off',
 5338:         );
 5339:         if (ref($domconfig{'login'}) eq 'HASH') {
 5340:             foreach my $item (@toggles) {
 5341:                 if ($defaultchecked{$item} eq 'on') { 
 5342:                     if (($domconfig{'login'}{$item} eq '0') &&
 5343:                         ($env{'form.'.$item} eq '1')) {
 5344:                         $changes{$item} = 1;
 5345:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5346:                               $domconfig{'login'}{$item} eq '1') &&
 5347:                              ($env{'form.'.$item} eq '0')) {
 5348:                         $changes{$item} = 1;
 5349:                     }
 5350:                 } elsif ($defaultchecked{$item} eq 'off') {
 5351:                     if (($domconfig{'login'}{$item} eq '1') &&
 5352:                         ($env{'form.'.$item} eq '0')) {
 5353:                         $changes{$item} = 1;
 5354:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5355:                               $domconfig{'login'}{$item} eq '0') &&
 5356:                              ($env{'form.'.$item} eq '1')) {
 5357:                         $changes{$item} = 1;
 5358:                     }
 5359:                 }
 5360:             }
 5361:         }
 5362:         if (keys(%changes) > 0 || $colchgtext) {
 5363:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5364:             if (ref($lastactref) eq 'HASH') {
 5365:                 $lastactref->{'domainconfig'} = 1;
 5366:             }
 5367:             $resulttext = &mt('Changes made:').'<ul>';
 5368:             foreach my $item (sort(keys(%changes))) {
 5369:                 if ($item eq 'loginvia') {
 5370:                     if (ref($changes{$item}) eq 'HASH') {
 5371:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 5372:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 5373:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 5374:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 5375:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 5376:                                     $protocol = 'http' if ($protocol ne 'https');
 5377:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 5378: 
 5379:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 5380:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 5381:                                     } else {
 5382:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 5383:                                     }
 5384:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 5385:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 5386:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 5387:                                     }
 5388:                                     $resulttext .= '</li>';
 5389:                                 } else {
 5390:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 5391:                                 }
 5392:                             } else {
 5393:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 5394:                             }
 5395:                         }
 5396:                         $resulttext .= '</ul></li>';
 5397:                     }
 5398:                 } elsif ($item eq 'helpurl') {
 5399:                     if (ref($changes{$item}) eq 'HASH') {
 5400:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 5401:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 5402:                                 my ($chg,$link);
 5403:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 5404:                                 if ($lang eq 'nolang') {
 5405:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 5406:                                 } else {
 5407:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 5408:                                 }
 5409:                                 $resulttext .= '<li>'.$chg.'</li>';
 5410:                             } else {
 5411:                                 my $chg;
 5412:                                 if ($lang eq 'nolang') {
 5413:                                     $chg = &mt('custom log-in help file for no preferred language');
 5414:                                 } else {
 5415:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 5416:                                 }
 5417:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 5418:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 5419:                                                       '?inhibitmenu=yes',$chg,600,500).
 5420:                                                '</li>';
 5421:                             }
 5422:                         }
 5423:                     }
 5424:                 } elsif ($item eq 'captcha') {
 5425:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5426:                         my $chgtxt;
 5427:                         if ($loginhash{'login'}{$item} eq 'notused') {
 5428:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 5429:                         } else {
 5430:                             my %captchas = &captcha_phrases();
 5431:                             if ($captchas{$loginhash{'login'}{$item}}) {
 5432:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 5433:                             } else {
 5434:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 5435:                             }
 5436:                         }
 5437:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5438:                     }
 5439:                 } elsif ($item eq 'recaptchakeys') {
 5440:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5441:                         my ($privkey,$pubkey);
 5442:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 5443:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 5444:                             $privkey = $loginhash{'login'}{$item}{'private'};
 5445:                         }
 5446:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 5447:                         if (!$pubkey) {
 5448:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 5449:                         } else {
 5450:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 5451:                         }
 5452:                         if (!$privkey) {
 5453:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 5454:                         } else {
 5455:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 5456:                         }
 5457:                         $chgtxt .= '</ul>';
 5458:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5459:                     }
 5460:                 } else {
 5461:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 5462:                 }
 5463:             }
 5464:             $resulttext .= $colchgtext.'</ul>';
 5465:         } else {
 5466:             $resulttext = &mt('No changes made to log-in page settings');
 5467:         }
 5468:     } else {
 5469:         $resulttext = '<span class="LC_error">'.
 5470: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5471:     }
 5472:     if ($errors) {
 5473:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 5474:                        $errors.'</ul>';
 5475:     }
 5476:     return $resulttext;
 5477: }
 5478: 
 5479: sub color_font_choices {
 5480:     my %choices =
 5481:         &Apache::lonlocal::texthash (
 5482:             img => "Header",
 5483:             bgs => "Background colors",
 5484:             links => "Link colors",
 5485:             images => "Images",
 5486:             font => "Font color",
 5487:             fontmenu => "Font menu",
 5488:             pgbg => "Page",
 5489:             tabbg => "Header",
 5490:             sidebg => "Border",
 5491:             link => "Link",
 5492:             alink => "Active link",
 5493:             vlink => "Visited link",
 5494:         );
 5495:     return %choices;
 5496: }
 5497: 
 5498: sub modify_rolecolors {
 5499:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 5500:     my ($resulttext,%rolehash);
 5501:     $rolehash{'rolecolors'} = {};
 5502:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 5503:         if ($domconfig{'rolecolors'} eq '') {
 5504:             $domconfig{'rolecolors'} = {};
 5505:         }
 5506:     }
 5507:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 5508:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 5509:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 5510:                                              $dom);
 5511:     if ($putresult eq 'ok') {
 5512:         if (keys(%changes) > 0) {
 5513:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5514:             if (ref($lastactref) eq 'HASH') {
 5515:                 $lastactref->{'domainconfig'} = 1;
 5516:             }
 5517:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 5518:                                              $rolehash{'rolecolors'});
 5519:         } else {
 5520:             $resulttext = &mt('No changes made to default color schemes');
 5521:         }
 5522:     } else {
 5523:         $resulttext = '<span class="LC_error">'.
 5524: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5525:     }
 5526:     if ($errors) {
 5527:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 5528:                        $errors.'</ul>';
 5529:     }
 5530:     return $resulttext;
 5531: }
 5532: 
 5533: sub modify_colors {
 5534:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 5535:     my (%changes,%choices);
 5536:     my @bgs;
 5537:     my @links = ('link','alink','vlink');
 5538:     my @logintext;
 5539:     my @images;
 5540:     my $servadm = $r->dir_config('lonAdmEMail');
 5541:     my $errors;
 5542:     my %defaults;
 5543:     foreach my $role (@{$roles}) {
 5544:         if ($role eq 'login') {
 5545:             %choices = &login_choices();
 5546:             @logintext = ('textcol','bgcol');
 5547:         } else {
 5548:             %choices = &color_font_choices();
 5549:         }
 5550:         if ($role eq 'login') {
 5551:             @images = ('img','logo','domlogo','login');
 5552:             @bgs = ('pgbg','mainbg','sidebg');
 5553:         } else {
 5554:             @images = ('img');
 5555:             @bgs = ('pgbg','tabbg','sidebg');
 5556:         }
 5557:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 5558:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 5559:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 5560:         }
 5561:         if ($role eq 'login') {
 5562:             foreach my $item (@logintext) {
 5563:                 unless ($env{'form.'.$role.'_'.$item} eq  $defaults{'logintext'}{$item}) {
 5564:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5565:                 }
 5566:             }
 5567:         } else {
 5568:             unless($env{'form.'.$role.'_fontmenu'} eq $defaults{'fontmenu'}) {
 5569:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 5570:             }
 5571:         }
 5572:         foreach my $item (@bgs) {
 5573:             unless ($env{'form.'.$role.'_'.$item} eq $defaults{'bgs'}{$item} ) {
 5574:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5575:             }
 5576:         }
 5577:         foreach my $item (@links) {
 5578:             unless ($env{'form.'.$role.'_'.$item} eq  $defaults{'links'}{$item}) {
 5579:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5580:             }
 5581:         }
 5582:         my ($configuserok,$author_ok,$switchserver) = 
 5583:             &config_check($dom,$confname,$servadm);
 5584:         my ($width,$height) = &thumb_dimensions();
 5585:         if (ref($domconfig->{$role}) ne 'HASH') {
 5586:             $domconfig->{$role} = {};
 5587:         }
 5588:         foreach my $img (@images) {
 5589:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 5590:                 if (defined($env{'form.login_showlogo_'.$img})) {
 5591:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 5592:                 } else { 
 5593:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 5594:                 }
 5595:             } 
 5596: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 5597: 		 && !defined($domconfig->{$role}{$img})
 5598: 		 && !$env{'form.'.$role.'_del_'.$img}
 5599: 		 && $env{'form.'.$role.'_import_'.$img}) {
 5600: 		# import the old configured image from the .tab setting
 5601: 		# if they haven't provided a new one 
 5602: 		$domconfig->{$role}{$img} = 
 5603: 		    $env{'form.'.$role.'_import_'.$img};
 5604: 	    }
 5605:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 5606:                 my $error;
 5607:                 if ($configuserok eq 'ok') {
 5608:                     if ($switchserver) {
 5609:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 5610:                     } else {
 5611:                         if ($author_ok eq 'ok') {
 5612:                             my ($result,$logourl) = 
 5613:                                 &publishlogo($r,'upload',$role.'_'.$img,
 5614:                                            $dom,$confname,$img,$width,$height);
 5615:                             if ($result eq 'ok') {
 5616:                                 $confhash->{$role}{$img} = $logourl;
 5617:                                 $changes{$role}{'images'}{$img} = 1;
 5618:                             } else {
 5619:                                 $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);
 5620:                             }
 5621:                         } else {
 5622:                             $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);
 5623:                         }
 5624:                     }
 5625:                 } else {
 5626:                     $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);
 5627:                 }
 5628:                 if ($error) {
 5629:                     &Apache::lonnet::logthis($error);
 5630:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5631:                 }
 5632:             } elsif ($domconfig->{$role}{$img} ne '') {
 5633:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 5634:                     my $error;
 5635:                     if ($configuserok eq 'ok') {
 5636: # is confname an author?
 5637:                         if ($switchserver eq '') {
 5638:                             if ($author_ok eq 'ok') {
 5639:                                 my ($result,$logourl) = 
 5640:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 5641:                                             $dom,$confname,$img,$width,$height);
 5642:                                 if ($result eq 'ok') {
 5643:                                     $confhash->{$role}{$img} = $logourl;
 5644: 				    $changes{$role}{'images'}{$img} = 1;
 5645:                                 }
 5646:                             }
 5647:                         }
 5648:                     }
 5649:                 }
 5650:             }
 5651:         }
 5652:         if (ref($domconfig) eq 'HASH') {
 5653:             if (ref($domconfig->{$role}) eq 'HASH') {
 5654:                 foreach my $img (@images) {
 5655:                     if ($domconfig->{$role}{$img} ne '') {
 5656:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5657:                             $confhash->{$role}{$img} = '';
 5658:                             $changes{$role}{'images'}{$img} = 1;
 5659:                         } else {
 5660:                             if ($confhash->{$role}{$img} eq '') {
 5661:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 5662:                             }
 5663:                         }
 5664:                     } else {
 5665:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5666:                             $confhash->{$role}{$img} = '';
 5667:                             $changes{$role}{'images'}{$img} = 1;
 5668:                         } 
 5669:                     }
 5670:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 5671:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 5672:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 5673:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 5674:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 5675:                             }
 5676:                         } else {
 5677:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5678:                                 $changes{$role}{'showlogo'}{$img} = 1;
 5679:                             }
 5680:                         }
 5681:                     }
 5682:                 }
 5683:                 if ($domconfig->{$role}{'font'} ne '') {
 5684:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 5685:                         $changes{$role}{'font'} = 1;
 5686:                     }
 5687:                 } else {
 5688:                     if ($confhash->{$role}{'font'}) {
 5689:                         $changes{$role}{'font'} = 1;
 5690:                     }
 5691:                 }
 5692:                 if ($role ne 'login') {
 5693:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 5694:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 5695:                             $changes{$role}{'fontmenu'} = 1;
 5696:                         }
 5697:                     } else {
 5698:                         if ($confhash->{$role}{'fontmenu'}) {
 5699:                             $changes{$role}{'fontmenu'} = 1;
 5700:                         }
 5701:                     }
 5702:                 }
 5703:                 foreach my $item (@bgs) {
 5704:                     if ($domconfig->{$role}{$item} ne '') {
 5705:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5706:                             $changes{$role}{'bgs'}{$item} = 1;
 5707:                         } 
 5708:                     } else {
 5709:                         if ($confhash->{$role}{$item}) {
 5710:                             $changes{$role}{'bgs'}{$item} = 1;
 5711:                         }
 5712:                     }
 5713:                 }
 5714:                 foreach my $item (@links) {
 5715:                     if ($domconfig->{$role}{$item} ne '') {
 5716:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5717:                             $changes{$role}{'links'}{$item} = 1;
 5718:                         }
 5719:                     } else {
 5720:                         if ($confhash->{$role}{$item}) {
 5721:                             $changes{$role}{'links'}{$item} = 1;
 5722:                         }
 5723:                     }
 5724:                 }
 5725:                 foreach my $item (@logintext) {
 5726:                     if ($domconfig->{$role}{$item} ne '') {
 5727:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5728:                             $changes{$role}{'logintext'}{$item} = 1;
 5729:                         }
 5730:                     } else {
 5731:                         if ($confhash->{$role}{$item}) {
 5732:                             $changes{$role}{'logintext'}{$item} = 1;
 5733:                         }
 5734:                     }
 5735:                 }
 5736:             } else {
 5737:                 &default_change_checker($role,\@images,\@links,\@bgs,
 5738:                                         \@logintext,$confhash,\%changes); 
 5739:             }
 5740:         } else {
 5741:             &default_change_checker($role,\@images,\@links,\@bgs,
 5742:                                     \@logintext,$confhash,\%changes); 
 5743:         }
 5744:     }
 5745:     return ($errors,%changes);
 5746: }
 5747: 
 5748: sub config_check {
 5749:     my ($dom,$confname,$servadm) = @_;
 5750:     my ($configuserok,$author_ok,$switchserver,%currroles);
 5751:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 5752:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 5753:                                                    $confname,$servadm);
 5754:     if ($configuserok eq 'ok') {
 5755:         $switchserver = &check_switchserver($dom,$confname);
 5756:         if ($switchserver eq '') {
 5757:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 5758:         }
 5759:     }
 5760:     return ($configuserok,$author_ok,$switchserver);
 5761: }
 5762: 
 5763: sub default_change_checker {
 5764:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 5765:     foreach my $item (@{$links}) {
 5766:         if ($confhash->{$role}{$item}) {
 5767:             $changes->{$role}{'links'}{$item} = 1;
 5768:         }
 5769:     }
 5770:     foreach my $item (@{$bgs}) {
 5771:         if ($confhash->{$role}{$item}) {
 5772:             $changes->{$role}{'bgs'}{$item} = 1;
 5773:         }
 5774:     }
 5775:     foreach my $item (@{$logintext}) {
 5776:         if ($confhash->{$role}{$item}) {
 5777:             $changes->{$role}{'logintext'}{$item} = 1;
 5778:         }
 5779:     }
 5780:     foreach my $img (@{$images}) {
 5781:         if ($env{'form.'.$role.'_del_'.$img}) {
 5782:             $confhash->{$role}{$img} = '';
 5783:             $changes->{$role}{'images'}{$img} = 1;
 5784:         }
 5785:         if ($role eq 'login') {
 5786:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5787:                 $changes->{$role}{'showlogo'}{$img} = 1;
 5788:             }
 5789:         }
 5790:     }
 5791:     if ($confhash->{$role}{'font'}) {
 5792:         $changes->{$role}{'font'} = 1;
 5793:     }
 5794: }
 5795: 
 5796: sub display_colorchgs {
 5797:     my ($dom,$changes,$roles,$confhash) = @_;
 5798:     my (%choices,$resulttext);
 5799:     if (!grep(/^login$/,@{$roles})) {
 5800:         $resulttext = &mt('Changes made:').'<br />';
 5801:     }
 5802:     foreach my $role (@{$roles}) {
 5803:         if ($role eq 'login') {
 5804:             %choices = &login_choices();
 5805:         } else {
 5806:             %choices = &color_font_choices();
 5807:         }
 5808:         if (ref($changes->{$role}) eq 'HASH') {
 5809:             if ($role ne 'login') {
 5810:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 5811:             }
 5812:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 5813:                 if ($role ne 'login') {
 5814:                     $resulttext .= '<ul>';
 5815:                 }
 5816:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 5817:                     if ($role ne 'login') {
 5818:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 5819:                     }
 5820:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 5821:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 5822:                             if ($confhash->{$role}{$key}{$item}) {
 5823:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 5824:                             } else {
 5825:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 5826:                             }
 5827:                         } elsif ($confhash->{$role}{$item} eq '') {
 5828:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 5829:                         } else {
 5830:                             my $newitem = $confhash->{$role}{$item};
 5831:                             if ($key eq 'images') {
 5832:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 5833:                             }
 5834:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 5835:                         }
 5836:                     }
 5837:                     if ($role ne 'login') {
 5838:                         $resulttext .= '</ul></li>';
 5839:                     }
 5840:                 } else {
 5841:                     if ($confhash->{$role}{$key} eq '') {
 5842:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 5843:                     } else {
 5844:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 5845:                     }
 5846:                 }
 5847:                 if ($role ne 'login') {
 5848:                     $resulttext .= '</ul>';
 5849:                 }
 5850:             }
 5851:         }
 5852:     }
 5853:     return $resulttext;
 5854: }
 5855: 
 5856: sub thumb_dimensions {
 5857:     return ('200','50');
 5858: }
 5859: 
 5860: sub check_dimensions {
 5861:     my ($inputfile) = @_;
 5862:     my ($fullwidth,$fullheight);
 5863:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 5864:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 5865:             my $imageinfo = <PIPE>;
 5866:             if (!close(PIPE)) {
 5867:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 5868:             }
 5869:             chomp($imageinfo);
 5870:             my ($fullsize) = 
 5871:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 5872:             if ($fullsize) {
 5873:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 5874:             }
 5875:         }
 5876:     }
 5877:     return ($fullwidth,$fullheight);
 5878: }
 5879: 
 5880: sub check_configuser {
 5881:     my ($uhome,$dom,$confname,$servadm) = @_;
 5882:     my ($configuserok,%currroles);
 5883:     if ($uhome eq 'no_host') {
 5884:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 5885:         my $configpass = &LONCAPA::Enrollment::create_password();
 5886:         $configuserok = 
 5887:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 5888:                              $configpass,'','','','','',undef,$servadm);
 5889:     } else {
 5890:         $configuserok = 'ok';
 5891:         %currroles = 
 5892:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 5893:     }
 5894:     return ($configuserok,%currroles);
 5895: }
 5896: 
 5897: sub check_authorstatus {
 5898:     my ($dom,$confname,%currroles) = @_;
 5899:     my $author_ok;
 5900:     if (!$currroles{':'.$dom.':au'}) {
 5901:         my $start = time;
 5902:         my $end = 0;
 5903:         $author_ok = 
 5904:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 5905:                                         'au',$end,$start,'','','domconfig');
 5906:     } else {
 5907:         $author_ok = 'ok';
 5908:     }
 5909:     return $author_ok;
 5910: }
 5911: 
 5912: sub publishlogo {
 5913:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 5914:     my ($output,$fname,$logourl);
 5915:     if ($action eq 'upload') {
 5916:         $fname=$env{'form.'.$formname.'.filename'};
 5917:         chop($env{'form.'.$formname});
 5918:     } else {
 5919:         ($fname) = ($formname =~ /([^\/]+)$/);
 5920:     }
 5921:     if ($savefileas ne '') {
 5922:         $fname = $savefileas;
 5923:     }
 5924:     $fname=&Apache::lonnet::clean_filename($fname);
 5925: # See if there is anything left
 5926:     unless ($fname) { return ('error: no uploaded file'); }
 5927:     $fname="$subdir/$fname";
 5928:     my $docroot=$r->dir_config('lonDocRoot');
 5929:     my $filepath="$docroot/priv";
 5930:     my $relpath = "$dom/$confname";
 5931:     my ($fnamepath,$file,$fetchthumb);
 5932:     $file=$fname;
 5933:     if ($fname=~m|/|) {
 5934:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 5935:     }
 5936:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 5937:     my $count;
 5938:     for ($count=5;$count<=$#parts;$count++) {
 5939:         $filepath.="/$parts[$count]";
 5940:         if ((-e $filepath)!=1) {
 5941:             mkdir($filepath,02770);
 5942:         }
 5943:     }
 5944:     # Check for bad extension and disallow upload
 5945:     if ($file=~/\.(\w+)$/ &&
 5946:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 5947:         $output = 
 5948:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 5949:     } elsif ($file=~/\.(\w+)$/ &&
 5950:         !defined(&Apache::loncommon::fileembstyle($1))) {
 5951:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 5952:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 5953:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 5954:     } elsif (-d "$filepath/$file") {
 5955:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 5956:     } else {
 5957:         my $source = $filepath.'/'.$file;
 5958:         my $logfile;
 5959:         if (!open($logfile,">>$source".'.log')) {
 5960:             return (&mt('No write permission to Authoring Space'));
 5961:         }
 5962:         print $logfile
 5963: "\n================= Publish ".localtime()." ================\n".
 5964: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 5965: # Save the file
 5966:         if (!open(FH,'>'.$source)) {
 5967:             &Apache::lonnet::logthis('Failed to create '.$source);
 5968:             return (&mt('Failed to create file'));
 5969:         }
 5970:         if ($action eq 'upload') {
 5971:             if (!print FH ($env{'form.'.$formname})) {
 5972:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 5973:                 return (&mt('Failed to write file'));
 5974:             }
 5975:         } else {
 5976:             my $original = &Apache::lonnet::filelocation('',$formname);
 5977:             if(!copy($original,$source)) {
 5978:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 5979:                 return (&mt('Failed to write file'));
 5980:             }
 5981:         }
 5982:         close(FH);
 5983:         chmod(0660, $source); # Permissions to rw-rw---.
 5984: 
 5985:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 5986:         my $copyfile=$targetdir.'/'.$file;
 5987: 
 5988:         my @parts=split(/\//,$targetdir);
 5989:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 5990:         for (my $count=5;$count<=$#parts;$count++) {
 5991:             $path.="/$parts[$count]";
 5992:             if (!-e $path) {
 5993:                 print $logfile "\nCreating directory ".$path;
 5994:                 mkdir($path,02770);
 5995:             }
 5996:         }
 5997:         my $versionresult;
 5998:         if (-e $copyfile) {
 5999:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 6000:         } else {
 6001:             $versionresult = 'ok';
 6002:         }
 6003:         if ($versionresult eq 'ok') {
 6004:             if (copy($source,$copyfile)) {
 6005:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 6006:                 $output = 'ok';
 6007:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 6008:                 push(@{$modified_urls},[$copyfile,$source]);
 6009:                 my $metaoutput = 
 6010:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 6011:                 unless ($registered_cleanup) {
 6012:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6013:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6014:                     $registered_cleanup=1;
 6015:                 }
 6016:             } else {
 6017:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 6018:                 $output = &mt('Failed to copy file to RES space').", $!";
 6019:             }
 6020:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 6021:                 my $inputfile = $filepath.'/'.$file;
 6022:                 my $outfile = $filepath.'/'.'tn-'.$file;
 6023:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 6024:                 if ($fullwidth ne '' && $fullheight ne '') { 
 6025:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 6026:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 6027:                         system("convert -sample $thumbsize $inputfile $outfile");
 6028:                         chmod(0660, $filepath.'/tn-'.$file);
 6029:                         if (-e $outfile) {
 6030:                             my $copyfile=$targetdir.'/tn-'.$file;
 6031:                             if (copy($outfile,$copyfile)) {
 6032:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 6033:                                 my $thumb_metaoutput = 
 6034:                                     &write_metadata($dom,$confname,$formname,
 6035:                                                     $targetdir,'tn-'.$file,$logfile);
 6036:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 6037:                                 unless ($registered_cleanup) {
 6038:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 6039:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 6040:                                     $registered_cleanup=1;
 6041:                                 }
 6042:                             } else {
 6043:                                 print $logfile "\nUnable to write ".$copyfile.
 6044:                                                ':'.$!."\n";
 6045:                             }
 6046:                         }
 6047:                     }
 6048:                 }
 6049:             }
 6050:         } else {
 6051:             $output = $versionresult;
 6052:         }
 6053:     }
 6054:     return ($output,$logourl);
 6055: }
 6056: 
 6057: sub logo_versioning {
 6058:     my ($targetdir,$file,$logfile) = @_;
 6059:     my $target = $targetdir.'/'.$file;
 6060:     my ($maxversion,$fn,$extn,$output);
 6061:     $maxversion = 0;
 6062:     if ($file =~ /^(.+)\.(\w+)$/) {
 6063:         $fn=$1;
 6064:         $extn=$2;
 6065:     }
 6066:     opendir(DIR,$targetdir);
 6067:     while (my $filename=readdir(DIR)) {
 6068:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 6069:             $maxversion=($1>$maxversion)?$1:$maxversion;
 6070:         }
 6071:     }
 6072:     $maxversion++;
 6073:     print $logfile "\nCreating old version ".$maxversion."\n";
 6074:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 6075:     if (copy($target,$copyfile)) {
 6076:         print $logfile "Copied old target to ".$copyfile."\n";
 6077:         $copyfile=$copyfile.'.meta';
 6078:         if (copy($target.'.meta',$copyfile)) {
 6079:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 6080:             $output = 'ok';
 6081:         } else {
 6082:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 6083:             $output = &mt('Failed to copy old meta').", $!, ";
 6084:         }
 6085:     } else {
 6086:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 6087:         $output = &mt('Failed to copy old target').", $!, ";
 6088:     }
 6089:     return $output;
 6090: }
 6091: 
 6092: sub write_metadata {
 6093:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 6094:     my (%metadatafields,%metadatakeys,$output);
 6095:     $metadatafields{'title'}=$formname;
 6096:     $metadatafields{'creationdate'}=time;
 6097:     $metadatafields{'lastrevisiondate'}=time;
 6098:     $metadatafields{'copyright'}='public';
 6099:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 6100:                                          $env{'user.domain'};
 6101:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 6102:     $metadatafields{'domain'}=$dom;
 6103:     {
 6104:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 6105:         my $mfh;
 6106:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 6107:             foreach (sort(keys(%metadatafields))) {
 6108:                 unless ($_=~/\./) {
 6109:                     my $unikey=$_;
 6110:                     $unikey=~/^([A-Za-z]+)/;
 6111:                     my $tag=$1;
 6112:                     $tag=~tr/A-Z/a-z/;
 6113:                     print $mfh "\n\<$tag";
 6114:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 6115:                         my $value=$metadatafields{$unikey.'.'.$_};
 6116:                         $value=~s/\"/\'\'/g;
 6117:                         print $mfh ' '.$_.'="'.$value.'"';
 6118:                     }
 6119:                     print $mfh '>'.
 6120:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 6121:                             .'</'.$tag.'>';
 6122:                 }
 6123:             }
 6124:             $output = 'ok';
 6125:             print $logfile "\nWrote metadata";
 6126:             close($mfh);
 6127:         } else {
 6128:             print $logfile "\nFailed to open metadata file";
 6129:             $output = &mt('Could not write metadata');
 6130:         }
 6131:     }
 6132:     return $output;
 6133: }
 6134: 
 6135: sub notifysubscribed {
 6136:     foreach my $targetsource (@{$modified_urls}){
 6137:         next unless (ref($targetsource) eq 'ARRAY');
 6138:         my ($target,$source)=@{$targetsource};
 6139:         if ($source ne '') {
 6140:             if (open(my $logfh,'>>'.$source.'.log')) {
 6141:                 print $logfh "\nCleanup phase: Notifications\n";
 6142:                 my @subscribed=&subscribed_hosts($target);
 6143:                 foreach my $subhost (@subscribed) {
 6144:                     print $logfh "\nNotifying host ".$subhost.':';
 6145:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 6146:                     print $logfh $reply;
 6147:                 }
 6148:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 6149:                 foreach my $subhost (@subscribedmeta) {
 6150:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 6151:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 6152:                                                         $subhost);
 6153:                     print $logfh $reply;
 6154:                 }
 6155:                 print $logfh "\n============ Done ============\n";
 6156:                 close($logfh);
 6157:             }
 6158:         }
 6159:     }
 6160:     return OK;
 6161: }
 6162: 
 6163: sub subscribed_hosts {
 6164:     my ($target) = @_;
 6165:     my @subscribed;
 6166:     if (open(my $fh,"<$target.subscription")) {
 6167:         while (my $subline=<$fh>) {
 6168:             if ($subline =~ /^($match_lonid):/) {
 6169:                 my $host = $1;
 6170:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 6171:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 6172:                         push(@subscribed,$host);
 6173:                     }
 6174:                 }
 6175:             }
 6176:         }
 6177:     }
 6178:     return @subscribed;
 6179: }
 6180: 
 6181: sub check_switchserver {
 6182:     my ($dom,$confname) = @_;
 6183:     my ($allowed,$switchserver);
 6184:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 6185:     if ($home eq 'no_host') {
 6186:         $home = &Apache::lonnet::domain($dom,'primary');
 6187:     }
 6188:     my @ids=&Apache::lonnet::current_machine_ids();
 6189:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 6190:     if (!$allowed) {
 6191: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 6192:     }
 6193:     return $switchserver;
 6194: }
 6195: 
 6196: sub modify_quotas {
 6197:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 6198:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 6199:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 6200:         $author_ok,$switchserver,$errors);
 6201:     if ($action eq 'quotas') {
 6202:         $context = 'tools'; 
 6203:     } else {
 6204:         $context = $action;
 6205:     }
 6206:     if ($context eq 'requestcourses') {
 6207:         @usertools = ('official','unofficial','community','textbook');
 6208:         @options =('norequest','approval','validate','autolimit');
 6209:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 6210:         %titles = &courserequest_titles();
 6211:         $toolregexp = join('|',@usertools);
 6212:         %conditions = &courserequest_conditions();
 6213:         $confname = $dom.'-domainconfig';
 6214:         my $servadm = $r->dir_config('lonAdmEMail');
 6215:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6216:     } elsif ($context eq 'requestauthor') {
 6217:         @usertools = ('author');
 6218:         %titles = &authorrequest_titles();
 6219:     } else {
 6220:         @usertools = ('aboutme','blog','webdav','portfolio');
 6221:         %titles = &tool_titles();
 6222:     }
 6223:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6224:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6225:     foreach my $key (keys(%env)) {
 6226:         if ($context eq 'requestcourses') {
 6227:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 6228:                 my $item = $1;
 6229:                 my $type = $2;
 6230:                 if ($type =~ /^limit_(.+)/) {
 6231:                     $limithash{$item}{$1} = $env{$key};
 6232:                 } else {
 6233:                     $confhash{$item}{$type} = $env{$key};
 6234:                 }
 6235:             }
 6236:         } elsif ($context eq 'requestauthor') {
 6237:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 6238:                 $confhash{$1} = $env{$key};
 6239:             }
 6240:         } else {
 6241:             if ($key =~ /^form\.quota_(.+)$/) {
 6242:                 $confhash{'defaultquota'}{$1} = $env{$key};
 6243:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 6244:                 $confhash{'authorquota'}{$1} = $env{$key};
 6245:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 6246:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 6247:             }
 6248:         }
 6249:     }
 6250:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6251:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 6252:         @approvalnotify = sort(@approvalnotify);
 6253:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 6254:         my @crstypes = ('official','unofficial','community','textbook');
 6255:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 6256:         foreach my $type (@hasuniquecode) {
 6257:             if (grep(/^\Q$type\E$/,@crstypes)) {
 6258:                 $confhash{'uniquecode'}{$type} = 1;
 6259:             }
 6260:         }
 6261:         my ($newbook,@allpos);
 6262:         if ($context eq 'requestcourses') {
 6263:             if ($env{'form.addbook'}) {
 6264:                 if (($env{'form.addbook_cnum'} =~ /^$match_courseid$/) &&
 6265:                     ($env{'form.addbook_cdom'} =~ /^$match_domain$/)) {
 6266:                     if (&Apache::lonnet::homeserver($env{'form.addbook_cnum'},
 6267:                                                     $env{'form.addbook_cdom'}) eq 'no_host') {
 6268:                         $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
 6269:                                    '</span></li>';
 6270:                     } else {
 6271:                         $newbook = $env{'form.addbook_cdom'}.'_'.$env{'form.addbook_cnum'};
 6272:                         my $position = $env{'form.addbook_pos'};
 6273:                         $position =~ s/\D+//g;
 6274:                         if ($position ne '') {
 6275:                             $allpos[$position] = $newbook;
 6276:                         }
 6277:                     }
 6278:                 } else {
 6279:                     $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
 6280:                                '</span></li>';
 6281:                 }
 6282:             }
 6283:         }
 6284:         if (ref($domconfig{$action}) eq 'HASH') {
 6285:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 6286:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 6287:                     $changes{'notify'}{'approval'} = 1;
 6288:                 }
 6289:             } else {
 6290:                 if ($confhash{'notify'}{'approval'}) {
 6291:                     $changes{'notify'}{'approval'} = 1;
 6292:                 }
 6293:             }
 6294:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 6295:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 6296:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 6297:                         unless ($confhash{'uniquecode'}{$crstype}) {
 6298:                             $changes{'uniquecode'} = 1;
 6299:                         }
 6300:                     }
 6301:                     unless ($changes{'uniquecode'}) {
 6302:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 6303:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 6304:                                 $changes{'uniquecode'} = 1;
 6305:                             }
 6306:                         }
 6307:                     }
 6308:                } else {
 6309:                    $changes{'uniquecode'} = 1;
 6310:                }
 6311:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 6312:                 $changes{'uniquecode'} = 1;
 6313:             }
 6314:             if ($context eq 'requestcourses') {
 6315:                 if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
 6316:                     my %deletions;
 6317:                     my @todelete = &Apache::loncommon::get_env_multiple('form.book_del');
 6318:                     if (@todelete) {
 6319:                         map { $deletions{$_} = 1; } @todelete;
 6320:                     }
 6321:                     my %imgdeletions;
 6322:                     my @todeleteimages = &Apache::loncommon::get_env_multiple('form.book_image_del');
 6323:                     if (@todeleteimages) {
 6324:                         map { $imgdeletions{$_} = 1; } @todeleteimages;
 6325:                     }
 6326:                     my $maxnum = $env{'form.book_maxnum'};
 6327:                     for (my $i=0; $i<=$maxnum; $i++) {
 6328:                         my $key = $env{'form.book_id_'.$i};
 6329:                         if (ref($domconfig{$action}{'textbooks'}{$key}) eq 'HASH') {
 6330:                             if ($deletions{$key}) {
 6331:                                 if ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
 6332:                                     #FIXME need to obsolete item in RES space
 6333:                                 }
 6334:                                 next;
 6335:                             } else {
 6336:                                 my $newpos = $env{'form.'.$key};
 6337:                                 $newpos =~ s/\D+//g;
 6338:                                 foreach my $item ('subject','title','author') {
 6339:                                     $confhash{'textbooks'}{$key}{$item} = $env{'form.book_'.$item.'_'.$i};
 6340:                                     if ($domconfig{$action}{'textbooks'}{$key}{$item} ne $confhash{'textbooks'}{$key}{$item}) {
 6341:                                         $changes{'textbooks'}{$key} = 1;
 6342:                                     }
 6343:                                 }
 6344:                                 $allpos[$newpos] = $key;
 6345:                             }
 6346:                             if ($imgdeletions{$key}) {
 6347:                                 $changes{'textbooks'}{$key} = 1;
 6348:                                 #FIXME need to obsolete item in RES space
 6349:                             } elsif ($env{'form.book_image_'.$i.'.filename'}) {
 6350:                                 my ($cdom,$cnum) = split(/_/,$key);
 6351:                                 my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,'book_image_'.$i,
 6352:                                                                               $cdom,$cnum,$configuserok,
 6353:                                                                               $switchserver,$author_ok);
 6354:                                 if ($imgurl) {
 6355:                                     $confhash{'textbooks'}{$key}{'image'} = $imgurl;
 6356:                                     $changes{'textbooks'}{$key} = 1; 
 6357:                                 }
 6358:                                 if ($error) {
 6359:                                     &Apache::lonnet::logthis($error);
 6360:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6361:                                 } 
 6362:                             } elsif ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
 6363:                                 $confhash{'textbooks'}{$key}{'image'} = 
 6364:                                     $domconfig{$action}{'textbooks'}{$key}{'image'};
 6365:                             }
 6366:                         }
 6367:                     }
 6368:                 }
 6369:             }
 6370:         } else {
 6371:             if ($confhash{'notify'}{'approval'}) {
 6372:                 $changes{'notify'}{'approval'} = 1;
 6373:             }
 6374:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 6375:                 $changes{'uniquecode'} = 1;
 6376:             }
 6377:         }
 6378:         if ($context eq 'requestcourses') {
 6379:             if ($newbook) {
 6380:                 $changes{'textbooks'}{$newbook} = 1;
 6381:                 foreach my $item ('subject','title','author') {
 6382:                     $env{'form.addbook_'.$item} =~ s/(`)/'/g;
 6383:                     if ($env{'form.addbook_'.$item}) {
 6384:                         $confhash{'textbooks'}{$newbook}{$item} = $env{'form.addbook_'.$item};
 6385:                     }
 6386:                 }
 6387:                 if ($env{'form.addbook_image.filename'} ne '') {
 6388:                     my ($cdom,$cnum) = split(/_/,$newbook);
 6389:                     my ($imageurl,$error) = 
 6390:                         &process_textbook_image($r,$dom,$confname,'addbook_image',$cdom,$cnum,$configuserok,
 6391:                                                 $switchserver,$author_ok);
 6392:                     if ($imageurl) {
 6393:                         $confhash{'textbooks'}{$newbook}{'image'} = $imageurl;
 6394:                     }
 6395:                     if ($error) {
 6396:                         &Apache::lonnet::logthis($error);
 6397:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6398:                     }
 6399:                 }
 6400:             }
 6401:             if (@allpos > 0) {
 6402:                 my $idx = 0;
 6403:                 foreach my $item (@allpos) {
 6404:                     if ($item ne '') {
 6405:                         $confhash{'textbooks'}{$item}{'order'} = $idx;
 6406:                         if (ref($domconfig{$action}) eq 'HASH') {
 6407:                             if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
 6408:                                 if (ref($domconfig{$action}{'textbooks'}{$item}) eq 'HASH') {
 6409:                                     if ($domconfig{$action}{'textbooks'}{$item}{'order'} ne $idx) {
 6410:                                         $changes{'textbooks'}{$item} = 1;
 6411:                                     }
 6412:                                 }
 6413:                             }
 6414:                         }
 6415:                         $idx ++;
 6416:                     }
 6417:                 }
 6418:             }
 6419:         }
 6420:     } else {
 6421:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 6422:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 6423:     }
 6424:     foreach my $item (@usertools) {
 6425:         foreach my $type (@{$types},'default','_LC_adv') {
 6426:             my $unset; 
 6427:             if ($context eq 'requestcourses') {
 6428:                 $unset = '0';
 6429:                 if ($type eq '_LC_adv') {
 6430:                     $unset = '';
 6431:                 }
 6432:                 if ($confhash{$item}{$type} eq 'autolimit') {
 6433:                     $confhash{$item}{$type} .= '=';
 6434:                     unless ($limithash{$item}{$type} =~ /\D/) {
 6435:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 6436:                     }
 6437:                 }
 6438:             } elsif ($context eq 'requestauthor') {
 6439:                 $unset = '0';
 6440:                 if ($type eq '_LC_adv') {
 6441:                     $unset = '';
 6442:                 }
 6443:             } else {
 6444:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 6445:                     $confhash{$item}{$type} = 1;
 6446:                 } else {
 6447:                     $confhash{$item}{$type} = 0;
 6448:                 }
 6449:             }
 6450:             if (ref($domconfig{$action}) eq 'HASH') {
 6451:                 if ($action eq 'requestauthor') {
 6452:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 6453:                         $changes{$type} = 1;
 6454:                     }
 6455:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 6456:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 6457:                         $changes{$item}{$type} = 1;
 6458:                     }
 6459:                 } else {
 6460:                     if ($context eq 'requestcourses') {
 6461:                         if ($confhash{$item}{$type} ne $unset) {
 6462:                             $changes{$item}{$type} = 1;
 6463:                         }
 6464:                     } else {
 6465:                         if (!$confhash{$item}{$type}) {
 6466:                             $changes{$item}{$type} = 1;
 6467:                         }
 6468:                     }
 6469:                 }
 6470:             } else {
 6471:                 if ($context eq 'requestcourses') {
 6472:                     if ($confhash{$item}{$type} ne $unset) {
 6473:                         $changes{$item}{$type} = 1;
 6474:                     }
 6475:                 } elsif ($context eq 'requestauthor') {
 6476:                     if ($confhash{$type} ne $unset) {
 6477:                         $changes{$type} = 1;
 6478:                     }
 6479:                 } else {
 6480:                     if (!$confhash{$item}{$type}) {
 6481:                         $changes{$item}{$type} = 1;
 6482:                     }
 6483:                 }
 6484:             }
 6485:         }
 6486:     }
 6487:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6488:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 6489:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 6490:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 6491:                     if (exists($confhash{'defaultquota'}{$key})) {
 6492:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 6493:                             $changes{'defaultquota'}{$key} = 1;
 6494:                         }
 6495:                     } else {
 6496:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 6497:                     }
 6498:                 }
 6499:             } else {
 6500:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 6501:                     if (exists($confhash{'defaultquota'}{$key})) {
 6502:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 6503:                             $changes{'defaultquota'}{$key} = 1;
 6504:                         }
 6505:                     } else {
 6506:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 6507:                     }
 6508:                 }
 6509:             }
 6510:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 6511:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 6512:                     if (exists($confhash{'authorquota'}{$key})) {
 6513:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 6514:                             $changes{'authorquota'}{$key} = 1;
 6515:                         }
 6516:                     } else {
 6517:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 6518:                     }
 6519:                 }
 6520:             }
 6521:         }
 6522:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 6523:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 6524:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 6525:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 6526:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 6527:                             $changes{'defaultquota'}{$key} = 1;
 6528:                         }
 6529:                     } else {
 6530:                         if (!exists($domconfig{'quotas'}{$key})) {
 6531:                             $changes{'defaultquota'}{$key} = 1;
 6532:                         }
 6533:                     }
 6534:                 } else {
 6535:                     $changes{'defaultquota'}{$key} = 1;
 6536:                 }
 6537:             }
 6538:         }
 6539:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 6540:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 6541:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 6542:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 6543:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 6544:                             $changes{'authorquota'}{$key} = 1;
 6545:                         }
 6546:                     } else {
 6547:                         $changes{'authorquota'}{$key} = 1;
 6548:                     }
 6549:                 } else {
 6550:                     $changes{'authorquota'}{$key} = 1;
 6551:                 }
 6552:             }
 6553:         }
 6554:     }
 6555: 
 6556:     if ($context eq 'requestauthor') {
 6557:         $domdefaults{'requestauthor'} = \%confhash;
 6558:     } else {
 6559:         foreach my $key (keys(%confhash)) {
 6560:             unless (($context eq 'requestcourses') && ($key eq 'textbooks')) {
 6561:                 $domdefaults{$key} = $confhash{$key};
 6562:             }
 6563:         }
 6564:     }
 6565: 
 6566:     my %quotahash = (
 6567:                       $action => { %confhash }
 6568:                     );
 6569:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 6570:                                              $dom);
 6571:     if ($putresult eq 'ok') {
 6572:         if (keys(%changes) > 0) {
 6573:             my $cachetime = 24*60*60;
 6574:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 6575:             if (ref($lastactref) eq 'HASH') {
 6576:                 $lastactref->{'domdefaults'} = 1;
 6577:             }
 6578:             $resulttext = &mt('Changes made:').'<ul>';
 6579:             unless (($context eq 'requestcourses') ||
 6580:                     ($context eq 'requestauthor')) {
 6581:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 6582:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 6583:                     foreach my $type (@{$types},'default') {
 6584:                         if (defined($changes{'defaultquota'}{$type})) {
 6585:                             my $typetitle = $usertypes->{$type};
 6586:                             if ($type eq 'default') {
 6587:                                 $typetitle = $othertitle;
 6588:                             }
 6589:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 6590:                         }
 6591:                     }
 6592:                     $resulttext .= '</ul></li>';
 6593:                 }
 6594:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 6595:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 6596:                     foreach my $type (@{$types},'default') {
 6597:                         if (defined($changes{'authorquota'}{$type})) {
 6598:                             my $typetitle = $usertypes->{$type};
 6599:                             if ($type eq 'default') {
 6600:                                 $typetitle = $othertitle;
 6601:                             }
 6602:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 6603:                         }
 6604:                     }
 6605:                     $resulttext .= '</ul></li>';
 6606:                 }
 6607:             }
 6608:             my %newenv;
 6609:             foreach my $item (@usertools) {
 6610:                 my (%haschgs,%inconf);
 6611:                 if ($context eq 'requestauthor') {
 6612:                     %haschgs = %changes;
 6613:                     %inconf = %confhash;
 6614:                 } else {
 6615:                     if (ref($changes{$item}) eq 'HASH') {
 6616:                         %haschgs = %{$changes{$item}};
 6617:                     }
 6618:                     if (ref($confhash{$item}) eq 'HASH') {
 6619:                         %inconf = %{$confhash{$item}};
 6620:                     }
 6621:                 }
 6622:                 if (keys(%haschgs) > 0) {
 6623:                     my $newacc = 
 6624:                         &Apache::lonnet::usertools_access($env{'user.name'},
 6625:                                                           $env{'user.domain'},
 6626:                                                           $item,'reload',$context);
 6627:                     if (($context eq 'requestcourses') ||
 6628:                         ($context eq 'requestauthor')) {
 6629:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 6630:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 6631:                         }
 6632:                     } else {
 6633:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 6634:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 6635:                         }
 6636:                     }
 6637:                     unless ($context eq 'requestauthor') {
 6638:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 6639:                     }
 6640:                     foreach my $type (@{$types},'default','_LC_adv') {
 6641:                         if ($haschgs{$type}) {
 6642:                             my $typetitle = $usertypes->{$type};
 6643:                             if ($type eq 'default') {
 6644:                                 $typetitle = $othertitle;
 6645:                             } elsif ($type eq '_LC_adv') {
 6646:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 6647:                             }
 6648:                             if ($inconf{$type}) {
 6649:                                 if ($context eq 'requestcourses') {
 6650:                                     my $cond;
 6651:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 6652:                                         if ($1 eq '') {
 6653:                                             $cond = &mt('(Automatic processing of any request).');
 6654:                                         } else {
 6655:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 6656:                                         }
 6657:                                     } else { 
 6658:                                         $cond = $conditions{$inconf{$type}};
 6659:                                     }
 6660:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 6661:                                 } elsif ($context eq 'requestauthor') {
 6662:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 6663:                                                              $titles{$inconf{$type}},$typetitle);
 6664: 
 6665:                                 } else {
 6666:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 6667:                                 }
 6668:                             } else {
 6669:                                 if ($type eq '_LC_adv') {
 6670:                                     if ($inconf{$type} eq '0') {
 6671:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 6672:                                     } else { 
 6673:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 6674:                                     }
 6675:                                 } else {
 6676:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 6677:                                 }
 6678:                             }
 6679:                         }
 6680:                     }
 6681:                     unless ($context eq 'requestauthor') {
 6682:                         $resulttext .= '</ul></li>';
 6683:                     }
 6684:                 }
 6685:             }
 6686:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 6687:                 if (ref($changes{'notify'}) eq 'HASH') {
 6688:                     if ($changes{'notify'}{'approval'}) {
 6689:                         if (ref($confhash{'notify'}) eq 'HASH') {
 6690:                             if ($confhash{'notify'}{'approval'}) {
 6691:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 6692:                             } else {
 6693:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 6694:                             }
 6695:                         }
 6696:                     }
 6697:                 }
 6698:             }
 6699:             if ($action eq 'requestcourses') {
 6700:                 my @offon = ('off','on');
 6701:                 if ($changes{'uniquecode'}) {
 6702:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 6703:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 6704:                         $resulttext .= '<li>'.
 6705:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 6706:                                        '</li>';
 6707:                     } else {
 6708:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 6709:                                        '</li>';
 6710:                     }
 6711:                 }
 6712:                 if (ref($changes{'textbooks'}) eq 'HASH') {
 6713:                     $resulttext .= '<li>'.&mt('Available textbooks updated').'<ul>';
 6714:                     foreach my $key (sort(keys(%{$changes{'textbooks'}}))) {
 6715:                         my %coursehash = &Apache::lonnet::coursedescription($key);
 6716:                         my $coursetitle = $coursehash{'description'};
 6717:                         my $position = $confhash{'textbooks'}{$key}{'order'} + 1;
 6718:                         $resulttext .= '<li>';
 6719:                         foreach my $item ('subject','title','author') {
 6720:                             my $name = $item.':';
 6721:                             $name =~ s/^(\w)/\U$1/;
 6722:                             $resulttext .= &mt($name).' '.$confhash{'textbooks'}{$key}{$item}.'<br />';
 6723:                         }
 6724:                         $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 6725:                         if ($confhash{'textbooks'}{$key}{'image'}) {
 6726:                             $resulttext .= ' '.&mt('Image: [_1]',
 6727:                                                '<img src="'.$confhash{'textbooks'}{$key}{'image'}.'"'.
 6728:                                                ' alt="Textbook cover" />').'<br />';
 6729:                         }
 6730:                         $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 6731:                     }
 6732:                     $resulttext .= '</ul></li>';
 6733:                 }
 6734:             }
 6735:             $resulttext .= '</ul>';
 6736:             if (keys(%newenv)) {
 6737:                 &Apache::lonnet::appenv(\%newenv);
 6738:             }
 6739:         } else {
 6740:             if ($context eq 'requestcourses') {
 6741:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 6742:             } elsif ($context eq 'requestauthor') {
 6743:                 $resulttext = &mt('No changes made to rights to request author space.');
 6744:             } else {
 6745:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 6746:             }
 6747:         }
 6748:     } else {
 6749:         $resulttext = '<span class="LC_error">'.
 6750: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6751:     }
 6752:     if ($errors) {
 6753:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 6754:                        '<ul>'.$errors.'</ul></p>';
 6755:     }
 6756:     return $resulttext;
 6757: }
 6758: 
 6759: sub process_textbook_image {
 6760:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$configuserok,$switchserver,$author_ok) = @_;
 6761:     my $filename = $env{'form.'.$caller.'.filename'};
 6762:     my ($error,$url);
 6763:     my ($width,$height) = (50,50);
 6764:     if ($configuserok eq 'ok') {
 6765:         if ($switchserver) {
 6766:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 6767:                          $switchserver);
 6768:         } elsif ($author_ok eq 'ok') {
 6769:             my ($result,$imageurl) =
 6770:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 6771:                              "textbooks/$dom/$cnum/cover",$width,$height);
 6772:             if ($result eq 'ok') {
 6773:                 $url = $imageurl;
 6774:             } else {
 6775:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 6776:             }
 6777:         } else {
 6778:             $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);
 6779:         }
 6780:     } else {
 6781:         $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);
 6782:     }
 6783:     return ($url,$error);
 6784: }
 6785: 
 6786: sub modify_autoenroll {
 6787:     my ($dom,$lastactref,%domconfig) = @_;
 6788:     my ($resulttext,%changes);
 6789:     my %currautoenroll;
 6790:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 6791:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 6792:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 6793:         }
 6794:     }
 6795:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 6796:     my %title = ( run => 'Auto-enrollment active',
 6797:                   sender => 'Sender for notification messages',
 6798:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 6799:     my @offon = ('off','on');
 6800:     my $sender_uname = $env{'form.sender_uname'};
 6801:     my $sender_domain = $env{'form.sender_domain'};
 6802:     if ($sender_domain eq '') {
 6803:         $sender_uname = '';
 6804:     } elsif ($sender_uname eq '') {
 6805:         $sender_domain = '';
 6806:     }
 6807:     my $coowners = $env{'form.autoassign_coowners'};
 6808:     my %autoenrollhash =  (
 6809:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 6810:                                        'sender_uname' => $sender_uname,
 6811:                                        'sender_domain' => $sender_domain,
 6812:                                        'co-owners' => $coowners,
 6813:                                 }
 6814:                      );
 6815:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 6816:                                              $dom);
 6817:     if ($putresult eq 'ok') {
 6818:         if (exists($currautoenroll{'run'})) {
 6819:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 6820:                  $changes{'run'} = 1;
 6821:              }
 6822:         } elsif ($autorun) {
 6823:             if ($env{'form.autoenroll_run'} ne '1') {
 6824:                  $changes{'run'} = 1;
 6825:             }
 6826:         }
 6827:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 6828:             $changes{'sender'} = 1;
 6829:         }
 6830:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 6831:             $changes{'sender'} = 1;
 6832:         }
 6833:         if ($currautoenroll{'co-owners'} ne '') {
 6834:             if ($currautoenroll{'co-owners'} ne $coowners) {
 6835:                 $changes{'coowners'} = 1;
 6836:             }
 6837:         } elsif ($coowners) {
 6838:             $changes{'coowners'} = 1;
 6839:         }      
 6840:         if (keys(%changes) > 0) {
 6841:             $resulttext = &mt('Changes made:').'<ul>';
 6842:             if ($changes{'run'}) {
 6843:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 6844:             }
 6845:             if ($changes{'sender'}) {
 6846:                 if ($sender_uname eq '' || $sender_domain eq '') {
 6847:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 6848:                 } else {
 6849:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 6850:                 }
 6851:             }
 6852:             if ($changes{'coowners'}) {
 6853:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 6854:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 6855:                 if (ref($lastactref) eq 'HASH') {
 6856:                     $lastactref->{'domainconfig'} = 1;
 6857:                 }
 6858:             }
 6859:             $resulttext .= '</ul>';
 6860:         } else {
 6861:             $resulttext = &mt('No changes made to auto-enrollment settings');
 6862:         }
 6863:     } else {
 6864:         $resulttext = '<span class="LC_error">'.
 6865: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6866:     }
 6867:     return $resulttext;
 6868: }
 6869: 
 6870: sub modify_autoupdate {
 6871:     my ($dom,%domconfig) = @_;
 6872:     my ($resulttext,%currautoupdate,%fields,%changes);
 6873:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 6874:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 6875:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 6876:         }
 6877:     }
 6878:     my @offon = ('off','on');
 6879:     my %title = &Apache::lonlocal::texthash (
 6880:                    run => 'Auto-update:',
 6881:                    classlists => 'Updates to user information in classlists?'
 6882:                 );
 6883:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6884:     my %fieldtitles = &Apache::lonlocal::texthash (
 6885:                         id => 'Student/Employee ID',
 6886:                         permanentemail => 'E-mail address',
 6887:                         lastname => 'Last Name',
 6888:                         firstname => 'First Name',
 6889:                         middlename => 'Middle Name',
 6890:                         generation => 'Generation',
 6891:                       );
 6892:     $othertitle = &mt('All users');
 6893:     if (keys(%{$usertypes}) >  0) {
 6894:         $othertitle = &mt('Other users');
 6895:     }
 6896:     foreach my $key (keys(%env)) {
 6897:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 6898:             my ($usertype,$item) = ($1,$2);
 6899:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 6900:                 if ($usertype eq 'default') {   
 6901:                     push(@{$fields{$1}},$2);
 6902:                 } elsif (ref($types) eq 'ARRAY') {
 6903:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 6904:                         push(@{$fields{$1}},$2);
 6905:                     }
 6906:                 }
 6907:             }
 6908:         }
 6909:     }
 6910:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 6911:     @lockablenames = sort(@lockablenames);
 6912:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 6913:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6914:         if (@changed) {
 6915:             $changes{'lockablenames'} = 1;
 6916:         }
 6917:     } else {
 6918:         if (@lockablenames) {
 6919:             $changes{'lockablenames'} = 1;
 6920:         }
 6921:     }
 6922:     my %updatehash = (
 6923:                       autoupdate => { run => $env{'form.autoupdate_run'},
 6924:                                       classlists => $env{'form.classlists'},
 6925:                                       fields => {%fields},
 6926:                                       lockablenames => \@lockablenames,
 6927:                                     }
 6928:                      );
 6929:     foreach my $key (keys(%currautoupdate)) {
 6930:         if (($key eq 'run') || ($key eq 'classlists')) {
 6931:             if (exists($updatehash{autoupdate}{$key})) {
 6932:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 6933:                     $changes{$key} = 1;
 6934:                 }
 6935:             }
 6936:         } elsif ($key eq 'fields') {
 6937:             if (ref($currautoupdate{$key}) eq 'HASH') {
 6938:                 foreach my $item (@{$types},'default') {
 6939:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 6940:                         my $change = 0;
 6941:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 6942:                             if (!exists($fields{$item})) {
 6943:                                 $change = 1;
 6944:                                 last;
 6945:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 6946:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 6947:                                     $change = 1;
 6948:                                     last;
 6949:                                 }
 6950:                             }
 6951:                         }
 6952:                         if ($change) {
 6953:                             push(@{$changes{$key}},$item);
 6954:                         }
 6955:                     } 
 6956:                 }
 6957:             }
 6958:         } elsif ($key eq 'lockablenames') {
 6959:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 6960:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6961:                 if (@changed) {
 6962:                     $changes{'lockablenames'} = 1;
 6963:                 }
 6964:             } else {
 6965:                 if (@lockablenames) {
 6966:                     $changes{'lockablenames'} = 1;
 6967:                 }
 6968:             }
 6969:         }
 6970:     }
 6971:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 6972:         if (@lockablenames) {
 6973:             $changes{'lockablenames'} = 1;
 6974:         }
 6975:     }
 6976:     foreach my $item (@{$types},'default') {
 6977:         if (defined($fields{$item})) {
 6978:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 6979:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 6980:                     my $change = 0;
 6981:                     if (ref($fields{$item}) eq 'ARRAY') {
 6982:                         foreach my $type (@{$fields{$item}}) {
 6983:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 6984:                                 $change = 1;
 6985:                                 last;
 6986:                             }
 6987:                         }
 6988:                     }
 6989:                     if ($change) {
 6990:                         push(@{$changes{'fields'}},$item);
 6991:                     }
 6992:                 } else {
 6993:                     push(@{$changes{'fields'}},$item);
 6994:                 }
 6995:             } else {
 6996:                 push(@{$changes{'fields'}},$item);
 6997:             }
 6998:         }
 6999:     }
 7000:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 7001:                                              $dom);
 7002:     if ($putresult eq 'ok') {
 7003:         if (keys(%changes) > 0) {
 7004:             $resulttext = &mt('Changes made:').'<ul>';
 7005:             foreach my $key (sort(keys(%changes))) {
 7006:                 if ($key eq 'lockablenames') {
 7007:                     $resulttext .= '<li>';
 7008:                     if (@lockablenames) {
 7009:                         $usertypes->{'default'} = $othertitle;
 7010:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 7011:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 7012:                     } else {
 7013:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 7014:                     }
 7015:                     $resulttext .= '</li>';
 7016:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 7017:                     foreach my $item (@{$changes{$key}}) {
 7018:                         my @newvalues;
 7019:                         foreach my $type (@{$fields{$item}}) {
 7020:                             push(@newvalues,$fieldtitles{$type});
 7021:                         }
 7022:                         my $newvaluestr;
 7023:                         if (@newvalues > 0) {
 7024:                             $newvaluestr = join(', ',@newvalues);
 7025:                         } else {
 7026:                             $newvaluestr = &mt('none');
 7027:                         }
 7028:                         if ($item eq 'default') {
 7029:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 7030:                         } else {
 7031:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 7032:                         }
 7033:                     }
 7034:                 } else {
 7035:                     my $newvalue;
 7036:                     if ($key eq 'run') {
 7037:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 7038:                     } else {
 7039:                         $newvalue = $offon[$env{'form.'.$key}];
 7040:                     }
 7041:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 7042:                 }
 7043:             }
 7044:             $resulttext .= '</ul>';
 7045:         } else {
 7046:             $resulttext = &mt('No changes made to autoupdates');
 7047:         }
 7048:     } else {
 7049:         $resulttext = '<span class="LC_error">'.
 7050: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7051:     }
 7052:     return $resulttext;
 7053: }
 7054: 
 7055: sub modify_autocreate {
 7056:     my ($dom,%domconfig) = @_;
 7057:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 7058:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 7059:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 7060:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 7061:         }
 7062:     }
 7063:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 7064:                  req => 'Auto-creation of validated requests for official courses',
 7065:                  xmldc => 'Identity of course creator of courses from XML files',
 7066:                );
 7067:     my @types = ('xml','req');
 7068:     foreach my $item (@types) {
 7069:         $newvals{$item} = $env{'form.autocreate_'.$item};
 7070:         $newvals{$item} =~ s/\D//g;
 7071:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 7072:     }
 7073:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 7074:     my %domcoords = &get_active_dcs($dom);
 7075:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 7076:         $newvals{'xmldc'} = '';
 7077:     } 
 7078:     %autocreatehash =  (
 7079:                         autocreate => { xml => $newvals{'xml'},
 7080:                                         req => $newvals{'req'},
 7081:                                       }
 7082:                        );
 7083:     if ($newvals{'xmldc'} ne '') {
 7084:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 7085:     }
 7086:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 7087:                                              $dom);
 7088:     if ($putresult eq 'ok') {
 7089:         my @items = @types;
 7090:         if ($newvals{'xml'}) {
 7091:             push(@items,'xmldc');
 7092:         }
 7093:         foreach my $item (@items) {
 7094:             if (exists($currautocreate{$item})) {
 7095:                 if ($currautocreate{$item} ne $newvals{$item}) {
 7096:                     $changes{$item} = 1;
 7097:                 }
 7098:             } elsif ($newvals{$item}) {
 7099:                 $changes{$item} = 1;
 7100:             }
 7101:         }
 7102:         if (keys(%changes) > 0) {
 7103:             my @offon = ('off','on'); 
 7104:             $resulttext = &mt('Changes made:').'<ul>';
 7105:             foreach my $item (@types) {
 7106:                 if ($changes{$item}) {
 7107:                     my $newtxt = $offon[$newvals{$item}];
 7108:                     $resulttext .= '<li>'.
 7109:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 7110:                                        '<b>','</b>').
 7111:                                    '</li>';
 7112:                 }
 7113:             }
 7114:             if ($changes{'xmldc'}) {
 7115:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 7116:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 7117:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 7118:             }
 7119:             $resulttext .= '</ul>';
 7120:         } else {
 7121:             $resulttext = &mt('No changes made to auto-creation settings');
 7122:         }
 7123:     } else {
 7124:         $resulttext = '<span class="LC_error">'.
 7125:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7126:     }
 7127:     return $resulttext;
 7128: }
 7129: 
 7130: sub modify_directorysrch {
 7131:     my ($dom,%domconfig) = @_;
 7132:     my ($resulttext,%changes);
 7133:     my %currdirsrch;
 7134:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 7135:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 7136:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 7137:         }
 7138:     }
 7139:     my %title = ( available => 'Directory search available',
 7140:                   localonly => 'Other domains can search',
 7141:                   searchby => 'Search types',
 7142:                   searchtypes => 'Search latitude');
 7143:     my @offon = ('off','on');
 7144:     my @otherdoms = ('Yes','No');
 7145: 
 7146:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 7147:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 7148:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 7149: 
 7150:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7151:     if (keys(%{$usertypes}) == 0) {
 7152:         @cansearch = ('default');
 7153:     } else {
 7154:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 7155:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 7156:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 7157:                     push(@{$changes{'cansearch'}},$type);
 7158:                 }
 7159:             }
 7160:             foreach my $type (@cansearch) {
 7161:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 7162:                     push(@{$changes{'cansearch'}},$type);
 7163:                 }
 7164:             }
 7165:         } else {
 7166:             push(@{$changes{'cansearch'}},@cansearch);
 7167:         }
 7168:     }
 7169: 
 7170:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 7171:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 7172:             if (!grep(/^\Q$by\E$/,@searchby)) {
 7173:                 push(@{$changes{'searchby'}},$by);
 7174:             }
 7175:         }
 7176:         foreach my $by (@searchby) {
 7177:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 7178:                 push(@{$changes{'searchby'}},$by);
 7179:             }
 7180:         }
 7181:     } else {
 7182:         push(@{$changes{'searchby'}},@searchby);
 7183:     }
 7184: 
 7185:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 7186:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 7187:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 7188:                 push(@{$changes{'searchtypes'}},$type);
 7189:             }
 7190:         }
 7191:         foreach my $type (@searchtypes) {
 7192:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 7193:                 push(@{$changes{'searchtypes'}},$type);
 7194:             }
 7195:         }
 7196:     } else {
 7197:         if (exists($currdirsrch{'searchtypes'})) {
 7198:             foreach my $type (@searchtypes) {  
 7199:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 7200:                     push(@{$changes{'searchtypes'}},$type);
 7201:                 }
 7202:             }
 7203:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 7204:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 7205:             }   
 7206:         } else {
 7207:             push(@{$changes{'searchtypes'}},@searchtypes); 
 7208:         }
 7209:     }
 7210: 
 7211:     my %dirsrch_hash =  (
 7212:             directorysrch => { available => $env{'form.dirsrch_available'},
 7213:                                cansearch => \@cansearch,
 7214:                                localonly => $env{'form.dirsrch_localonly'},
 7215:                                searchby => \@searchby,
 7216:                                searchtypes => \@searchtypes,
 7217:                              }
 7218:             );
 7219:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 7220:                                              $dom);
 7221:     if ($putresult eq 'ok') {
 7222:         if (exists($currdirsrch{'available'})) {
 7223:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 7224:                  $changes{'available'} = 1;
 7225:              }
 7226:         } else {
 7227:             if ($env{'form.dirsrch_available'} eq '1') {
 7228:                 $changes{'available'} = 1;
 7229:             }
 7230:         }
 7231:         if (exists($currdirsrch{'localonly'})) {
 7232:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 7233:                  $changes{'localonly'} = 1;
 7234:              }
 7235:         } else {
 7236:             if ($env{'form.dirsrch_localonly'} eq '1') {
 7237:                 $changes{'localonly'} = 1;
 7238:             }
 7239:         }
 7240:         if (keys(%changes) > 0) {
 7241:             $resulttext = &mt('Changes made:').'<ul>';
 7242:             if ($changes{'available'}) {
 7243:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 7244:             }
 7245:             if ($changes{'localonly'}) {
 7246:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 7247:             }
 7248: 
 7249:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 7250:                 my $chgtext;
 7251:                 if (ref($usertypes) eq 'HASH') {
 7252:                     if (keys(%{$usertypes}) > 0) {
 7253:                         foreach my $type (@{$types}) {
 7254:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 7255:                                 $chgtext .= $usertypes->{$type}.'; ';
 7256:                             }
 7257:                         }
 7258:                         if (grep(/^default$/,@cansearch)) {
 7259:                             $chgtext .= $othertitle;
 7260:                         } else {
 7261:                             $chgtext =~ s/\; $//;
 7262:                         }
 7263:                         $resulttext .=
 7264:                             '<li>'.
 7265:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 7266:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 7267:                             '</li>';
 7268:                     }
 7269:                 }
 7270:             }
 7271:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 7272:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 7273:                 my $chgtext;
 7274:                 foreach my $type (@{$titleorder}) {
 7275:                     if (grep(/^\Q$type\E$/,@searchby)) {
 7276:                         if (defined($searchtitles->{$type})) {
 7277:                             $chgtext .= $searchtitles->{$type}.'; ';
 7278:                         }
 7279:                     }
 7280:                 }
 7281:                 $chgtext =~ s/\; $//;
 7282:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 7283:             }
 7284:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 7285:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 7286:                 my $chgtext;
 7287:                 foreach my $type (@{$srchtypeorder}) {
 7288:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 7289:                         if (defined($srchtypes_desc->{$type})) {
 7290:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 7291:                         }
 7292:                     }
 7293:                 }
 7294:                 $chgtext =~ s/\; $//;
 7295:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 7296:             }
 7297:             $resulttext .= '</ul>';
 7298:         } else {
 7299:             $resulttext = &mt('No changes made to institution directory search settings');
 7300:         }
 7301:     } else {
 7302:         $resulttext = '<span class="LC_error">'.
 7303:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 7304:     }
 7305:     return $resulttext;
 7306: }
 7307: 
 7308: sub modify_contacts {
 7309:     my ($dom,$lastactref,%domconfig) = @_;
 7310:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 7311:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 7312:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 7313:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 7314:         }
 7315:     }
 7316:     my (%others,%to,%bcc);
 7317:     my @contacts = ('supportemail','adminemail');
 7318:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 7319:                     'requestsmail','updatesmail','idconflictsmail');
 7320:     my @toggles = ('reporterrors','reportupdates');
 7321:     foreach my $type (@mailings) {
 7322:         @{$newsetting{$type}} = 
 7323:             &Apache::loncommon::get_env_multiple('form.'.$type);
 7324:         foreach my $item (@contacts) {
 7325:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 7326:                 $contacts_hash{contacts}{$type}{$item} = 1;
 7327:             } else {
 7328:                 $contacts_hash{contacts}{$type}{$item} = 0;
 7329:             }
 7330:         }  
 7331:         $others{$type} = $env{'form.'.$type.'_others'};
 7332:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 7333:         if ($type eq 'helpdeskmail') {
 7334:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 7335:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 7336:         }
 7337:     }
 7338:     foreach my $item (@contacts) {
 7339:         $to{$item} = $env{'form.'.$item};
 7340:         $contacts_hash{'contacts'}{$item} = $to{$item};
 7341:     }
 7342:     foreach my $item (@toggles) {
 7343:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 7344:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 7345:         }
 7346:     }
 7347:     if (keys(%currsetting) > 0) {
 7348:         foreach my $item (@contacts) {
 7349:             if ($to{$item} ne $currsetting{$item}) {
 7350:                 $changes{$item} = 1;
 7351:             }
 7352:         }
 7353:         foreach my $type (@mailings) {
 7354:             foreach my $item (@contacts) {
 7355:                 if (ref($currsetting{$type}) eq 'HASH') {
 7356:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 7357:                         push(@{$changes{$type}},$item);
 7358:                     }
 7359:                 } else {
 7360:                     push(@{$changes{$type}},@{$newsetting{$type}});
 7361:                 }
 7362:             }
 7363:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 7364:                 push(@{$changes{$type}},'others');
 7365:             }
 7366:             if ($type eq 'helpdeskmail') {   
 7367:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 7368:                     push(@{$changes{$type}},'bcc'); 
 7369:                 }
 7370:             }
 7371:         }
 7372:     } else {
 7373:         my %default;
 7374:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 7375:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 7376:         $default{'errormail'} = 'adminemail';
 7377:         $default{'packagesmail'} = 'adminemail';
 7378:         $default{'helpdeskmail'} = 'supportemail';
 7379:         $default{'lonstatusmail'} = 'adminemail';
 7380:         $default{'requestsmail'} = 'adminemail';
 7381:         $default{'updatesmail'} = 'adminemail';
 7382:         foreach my $item (@contacts) {
 7383:            if ($to{$item} ne $default{$item}) {
 7384:               $changes{$item} = 1;
 7385:            }
 7386:         }
 7387:         foreach my $type (@mailings) {
 7388:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 7389:                
 7390:                 push(@{$changes{$type}},@{$newsetting{$type}});
 7391:             }
 7392:             if ($others{$type} ne '') {
 7393:                 push(@{$changes{$type}},'others');
 7394:             }
 7395:             if ($type eq 'helpdeskmail') {
 7396:                 if ($bcc{$type} ne '') {
 7397:                     push(@{$changes{$type}},'bcc');
 7398:                 }
 7399:             }
 7400:         }
 7401:     }
 7402:     foreach my $item (@toggles) {
 7403:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 7404:             $changes{$item} = 1;
 7405:         } elsif ((!$env{'form.'.$item}) &&
 7406:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 7407:             $changes{$item} = 1;
 7408:         }
 7409:     }
 7410:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 7411:                                              $dom);
 7412:     if ($putresult eq 'ok') {
 7413:         if (keys(%changes) > 0) {
 7414:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7415:             if (ref($lastactref) eq 'HASH') {
 7416:                 $lastactref->{'domainconfig'} = 1;
 7417:             }
 7418:             my ($titles,$short_titles)  = &contact_titles();
 7419:             $resulttext = &mt('Changes made:').'<ul>';
 7420:             foreach my $item (@contacts) {
 7421:                 if ($changes{$item}) {
 7422:                     $resulttext .= '<li>'.$titles->{$item}.
 7423:                                     &mt(' set to: ').
 7424:                                     '<span class="LC_cusr_emph">'.
 7425:                                     $to{$item}.'</span></li>';
 7426:                 }
 7427:             }
 7428:             foreach my $type (@mailings) {
 7429:                 if (ref($changes{$type}) eq 'ARRAY') {
 7430:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 7431:                     my @text;
 7432:                     foreach my $item (@{$newsetting{$type}}) {
 7433:                         push(@text,$short_titles->{$item});
 7434:                     }
 7435:                     if ($others{$type} ne '') {
 7436:                         push(@text,$others{$type});
 7437:                     }
 7438:                     $resulttext .= '<span class="LC_cusr_emph">'.
 7439:                                    join(', ',@text).'</span>';
 7440:                     if ($type eq 'helpdeskmail') {
 7441:                         if ($bcc{$type} ne '') {
 7442:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 7443:                         }
 7444:                     }
 7445:                     $resulttext .= '</li>';
 7446:                 }
 7447:             }
 7448:             my @offon = ('off','on');
 7449:             if ($changes{'reporterrors'}) {
 7450:                 $resulttext .= '<li>'.
 7451:                                &mt('E-mail error reports to [_1] set to "'.
 7452:                                    $offon[$env{'form.reporterrors'}].'".',
 7453:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 7454:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 7455:                                '</li>';
 7456:             }
 7457:             if ($changes{'reportupdates'}) {
 7458:                 $resulttext .= '<li>'.
 7459:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 7460:                                     $offon[$env{'form.reportupdates'}].'".',
 7461:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 7462:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 7463:                                 '</li>';
 7464:             }
 7465:             $resulttext .= '</ul>';
 7466:         } else {
 7467:             $resulttext = &mt('No changes made to contact information');
 7468:         }
 7469:     } else {
 7470:         $resulttext = '<span class="LC_error">'.
 7471:             &mt('An error occurred: [_1].',$putresult).'</span>';
 7472:     }
 7473:     return $resulttext;
 7474: }
 7475: 
 7476: sub modify_usercreation {
 7477:     my ($dom,%domconfig) = @_;
 7478:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
 7479:     my $warningmsg;
 7480:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 7481:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 7482:             if ($key eq 'cancreate') {
 7483:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 7484:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 7485:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 7486:                             ($item eq 'captcha') || ($item eq 'recaptchakeys')) {
 7487:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 7488:                         } else {
 7489:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 7490:                         }
 7491:                     }
 7492:                 }
 7493:             } elsif ($key eq 'email_rule') {
 7494:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 7495:             } else {
 7496:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 7497:             }
 7498:         }
 7499:     }
 7500:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 7501:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 7502:     my @contexts = ('author','course','requestcrs');
 7503:     foreach my $item(@contexts) {
 7504:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
 7505:     }
 7506:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 7507:         foreach my $item (@contexts) {
 7508:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 7509:                 push(@{$changes{'cancreate'}},$item);
 7510:             }
 7511:         }
 7512:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 7513:         foreach my $item (@contexts) {
 7514:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 7515:                 if ($cancreate{$item} ne 'any') {
 7516:                     push(@{$changes{'cancreate'}},$item);
 7517:                 }
 7518:             } else {
 7519:                 if ($cancreate{$item} ne 'none') {
 7520:                     push(@{$changes{'cancreate'}},$item);
 7521:                 }
 7522:             }
 7523:         }
 7524:     } else {
 7525:         foreach my $item (@contexts)  {
 7526:             push(@{$changes{'cancreate'}},$item);
 7527:         }
 7528:     }
 7529: 
 7530:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 7531:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 7532:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 7533:                 push(@{$changes{'username_rule'}},$type);
 7534:             }
 7535:         }
 7536:         foreach my $type (@username_rule) {
 7537:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 7538:                 push(@{$changes{'username_rule'}},$type);
 7539:             }
 7540:         }
 7541:     } else {
 7542:         push(@{$changes{'username_rule'}},@username_rule);
 7543:     }
 7544: 
 7545:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 7546:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 7547:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 7548:                 push(@{$changes{'id_rule'}},$type);
 7549:             }
 7550:         }
 7551:         foreach my $type (@id_rule) {
 7552:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 7553:                 push(@{$changes{'id_rule'}},$type);
 7554:             }
 7555:         }
 7556:     } else {
 7557:         push(@{$changes{'id_rule'}},@id_rule);
 7558:     }
 7559: 
 7560:     my @authen_contexts = ('author','course','domain');
 7561:     my @authtypes = ('int','krb4','krb5','loc');
 7562:     my %authhash;
 7563:     foreach my $item (@authen_contexts) {
 7564:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 7565:         foreach my $auth (@authtypes) {
 7566:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 7567:                 $authhash{$item}{$auth} = 1;
 7568:             } else {
 7569:                 $authhash{$item}{$auth} = 0;
 7570:             }
 7571:         }
 7572:     }
 7573:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 7574:         foreach my $item (@authen_contexts) {
 7575:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 7576:                 foreach my $auth (@authtypes) {
 7577:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 7578:                         push(@{$changes{'authtypes'}},$item);
 7579:                         last;
 7580:                     }
 7581:                 }
 7582:             }
 7583:         }
 7584:     } else {
 7585:         foreach my $item (@authen_contexts) {
 7586:             push(@{$changes{'authtypes'}},$item);
 7587:         }
 7588:     }
 7589: 
 7590:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
 7591:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
 7592:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
 7593:     $save_usercreate{'id_rule'} = \@id_rule;
 7594:     $save_usercreate{'username_rule'} = \@username_rule,
 7595:     $save_usercreate{'authtypes'} = \%authhash;
 7596: 
 7597:     my %usercreation_hash =  (
 7598:         usercreation     => \%save_usercreate,
 7599:     );
 7600: 
 7601:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 7602:                                              $dom);
 7603: 
 7604:     if ($putresult eq 'ok') {
 7605:         if (keys(%changes) > 0) {
 7606:             $resulttext = &mt('Changes made:').'<ul>';
 7607:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 7608:                 my %lt = &usercreation_types();
 7609:                 foreach my $type (@{$changes{'cancreate'}}) {
 7610:                     my $chgtext = $lt{$type}.', ';
 7611:                     if ($cancreate{$type} eq 'none') {
 7612:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 7613:                     } elsif ($cancreate{$type} eq 'any') {
 7614:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 7615:                     } elsif ($cancreate{$type} eq 'official') {
 7616:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 7617:                     } elsif ($cancreate{$type} eq 'unofficial') {
 7618:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 7619:                     }
 7620:                     $resulttext .= '<li>'.$chgtext.'</li>';
 7621:                 }
 7622:             }
 7623:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 7624:                 my ($rules,$ruleorder) = 
 7625:                     &Apache::lonnet::inst_userrules($dom,'username');
 7626:                 my $chgtext = '<ul>';
 7627:                 foreach my $type (@username_rule) {
 7628:                     if (ref($rules->{$type}) eq 'HASH') {
 7629:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 7630:                     }
 7631:                 }
 7632:                 $chgtext .= '</ul>';
 7633:                 if (@username_rule > 0) {
 7634:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 7635:                 } else {
 7636:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 7637:                 }
 7638:             }
 7639:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 7640:                 my ($idrules,$idruleorder) = 
 7641:                     &Apache::lonnet::inst_userrules($dom,'id');
 7642:                 my $chgtext = '<ul>';
 7643:                 foreach my $type (@id_rule) {
 7644:                     if (ref($idrules->{$type}) eq 'HASH') {
 7645:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 7646:                     }
 7647:                 }
 7648:                 $chgtext .= '</ul>';
 7649:                 if (@id_rule > 0) {
 7650:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 7651:                 } else {
 7652:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 7653:                 }
 7654:             }
 7655:             my %authname = &authtype_names();
 7656:             my %context_title = &context_names();
 7657:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 7658:                 my $chgtext = '<ul>';
 7659:                 foreach my $type (@{$changes{'authtypes'}}) {
 7660:                     my @allowed;
 7661:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 7662:                     foreach my $auth (@authtypes) {
 7663:                         if ($authhash{$type}{$auth}) {
 7664:                             push(@allowed,$authname{$auth});
 7665:                         }
 7666:                     }
 7667:                     if (@allowed > 0) {
 7668:                         $chgtext .= join(', ',@allowed).'</li>';
 7669:                     } else {
 7670:                         $chgtext .= &mt('none').'</li>';
 7671:                     }
 7672:                 }
 7673:                 $chgtext .= '</ul>';
 7674:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 7675:                 $resulttext .= '</li>';
 7676:             }
 7677:             $resulttext .= '</ul>';
 7678:         } else {
 7679:             $resulttext = &mt('No changes made to user creation settings');
 7680:         }
 7681:     } else {
 7682:         $resulttext = '<span class="LC_error">'.
 7683:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7684:     }
 7685:     if ($warningmsg ne '') {
 7686:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 7687:     }
 7688:     return $resulttext;
 7689: }
 7690: 
 7691: sub modify_selfcreation {
 7692:     my ($dom,%domconfig) = @_;
 7693:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
 7694:     my (%save_usercreate,%save_usermodify);
 7695:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7696:     if (ref($types) eq 'ARRAY') {
 7697:         $usertypes->{'default'} = $othertitle;
 7698:         push(@{$types},'default');
 7699:     }
 7700: #
 7701: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
 7702: #
 7703:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 7704:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 7705:             if ($key eq 'cancreate') {
 7706:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
 7707:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
 7708:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
 7709:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
 7710:                             ($item eq 'emailusername') || ($item eq 'notify')) {
 7711:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 7712:                         } else {
 7713:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
 7714:                         }
 7715:                     }
 7716:                 }
 7717:             } elsif ($key eq 'email_rule') {
 7718:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 7719:             } else {
 7720:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
 7721:             }
 7722:         }
 7723:     }
 7724: #
 7725: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
 7726: #
 7727:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 7728:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 7729:             if ($key eq 'selfcreate') {
 7730:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
 7731:             } else {
 7732:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
 7733:             }
 7734:         }
 7735:     }
 7736: 
 7737:     my @contexts = ('selfcreate');
 7738:     @{$cancreate{'selfcreate'}} = ();
 7739:     %{$cancreate{'emailusername'}} = ();
 7740:     @{$cancreate{'statustocreate'}} = ();
 7741:     my %selfcreatetypes = (
 7742:                              sso   => 'users authenticated by institutional single sign on',
 7743:                              login => 'users authenticated by institutional log-in',
 7744:                              email => 'users who provide a valid e-mail address for use as username (automatic creation)',
 7745:                              emailapproval => 'users who provide a valid e-mail address for use as username (queued for Domain Coordinator review)',
 7746:                           );
 7747: #
 7748: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
 7749: # is permitted.
 7750: #
 7751:     foreach my $item ('login','sso','email') {
 7752:         if ($item eq 'email') {
 7753:             if ($env{'form.cancreate_email'} eq 'email') {
 7754:                 push(@{$cancreate{'selfcreate'}},'email');
 7755:             } elsif ($env{'form.cancreate_email'} eq 'emailapproval') {
 7756:                 push(@{$cancreate{'selfcreate'}},'emailapproval');
 7757:             }
 7758:         } else {
 7759:             if ($env{'form.cancreate_'.$item}) {
 7760:                 push(@{$cancreate{'selfcreate'}},$item);
 7761:             }
 7762:         }
 7763:     }
 7764:     my (@email_rule,%userinfo,%savecaptcha);
 7765:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 7766: #
 7767: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
 7768: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
 7769: #
 7770:     if (($env{'form.cancreate_email'} eq 'email') || ($env{'form.cancreate_email'} eq 'emailapproval')) {
 7771:         push(@contexts,'emailusername'); 
 7772:         if (ref($types) eq 'ARRAY') {
 7773:             foreach my $type (@{$types}) {
 7774:                 if (ref($infofields) eq 'ARRAY') {
 7775:                     foreach my $field (@{$infofields}) {
 7776:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
 7777:                             $cancreate{'emailusername'}{$type}{$field} = $1;
 7778:                         }
 7779:                     }
 7780:                 }
 7781:             }
 7782:         }
 7783: #
 7784: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
 7785: # queued requests for self-creation of account using e-mail address as username
 7786: #
 7787: 
 7788:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
 7789:         @approvalnotify = sort(@approvalnotify);
 7790:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
 7791:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 7792:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
 7793:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
 7794:                     push(@{$changes{'cancreate'}},'notify');
 7795:                 }
 7796:             } else {
 7797:                 if ($cancreate{'notify'}{'approval'}) {
 7798:                     push(@{$changes{'cancreate'}},'notify');
 7799:                 }
 7800:             }
 7801:         } elsif ($cancreate{'notify'}{'approval'}) {
 7802:             push(@{$changes{'cancreate'}},'notify');
 7803:         }
 7804: 
 7805: #
 7806: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
 7807: #
 7808:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 7809:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
 7810:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 7811:             if (@{$curr_usercreation{'email_rule'}} > 0) {
 7812:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 7813:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
 7814:                         push(@{$changes{'email_rule'}},$type);
 7815:                     }
 7816:                 }
 7817:             }
 7818:             if (@email_rule > 0) {
 7819:                 foreach my $type (@email_rule) {
 7820:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 7821:                         push(@{$changes{'email_rule'}},$type);
 7822:                     }
 7823:                 }
 7824:             }
 7825:         } elsif (@email_rule > 0) {
 7826:             push(@{$changes{'email_rule'}},@email_rule);
 7827:         }
 7828:     }
 7829: #  
 7830: # Check if domain default is set appropriately, if selef-creation of accounts is to be available for
 7831: # institutional log-in.
 7832: #
 7833:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
 7834:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 7835:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
 7836:                ($domdefaults{'auth_def'} eq 'localauth'))) {
 7837:             $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.').' '.
 7838:                           &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.');
 7839:         }
 7840:     }
 7841:     my @fields = ('lastname','firstname','middlename','generation',
 7842:                   'permanentemail','id');
 7843:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7844: #
 7845: # Where usernames may created for institutional log-in and/or institutional single sign on:
 7846: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
 7847: # may self-create accounts 
 7848: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
 7849: # which the user may supply, if institutional data is unavailable.
 7850: #
 7851:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
 7852:         if (ref($types) eq 'ARRAY') {
 7853:             if (@{$types} > 1) {
 7854:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
 7855:                 push(@contexts,'statustocreate');
 7856:             } else {
 7857:                 undef($cancreate{'statustocreate'});
 7858:             } 
 7859:             foreach my $type (@{$types}) {
 7860:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
 7861:                 foreach my $field (@fields) {
 7862:                     if (grep(/^\Q$field\E$/,@modifiable)) {
 7863:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
 7864:                     } else {
 7865:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
 7866:                     }
 7867:                 }
 7868:             }
 7869:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
 7870:                 foreach my $type (@{$types}) {
 7871:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
 7872:                         foreach my $field (@fields) {
 7873:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
 7874:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
 7875:                                 push(@{$changes{'selfcreate'}},$type);
 7876:                                 last;
 7877:                             }
 7878:                         }
 7879:                     }
 7880:                 }
 7881:             } else {
 7882:                 foreach my $type (@{$types}) {
 7883:                     push(@{$changes{'selfcreate'}},$type);
 7884:                 }
 7885:             }
 7886:         }
 7887:     }
 7888:     foreach my $item (@contexts) {
 7889:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 7890:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 7891:                 if (ref($cancreate{$item}) eq 'ARRAY') {
 7892:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 7893:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7894:                             push(@{$changes{'cancreate'}},$item);
 7895:                         }
 7896:                     }
 7897:                 }
 7898:             }
 7899:             if (ref($cancreate{$item}) eq 'ARRAY') {
 7900:                 foreach my $type (@{$cancreate{$item}}) {
 7901:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 7902:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7903:                             push(@{$changes{'cancreate'}},$item);
 7904:                         }
 7905:                     }
 7906:                 }
 7907:             }
 7908:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
 7909:             if (ref($cancreate{$item}) eq 'HASH') {
 7910:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
 7911:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
 7912:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
 7913:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
 7914:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7915:                                     push(@{$changes{'cancreate'}},$item);
 7916:                                 }
 7917:                             }
 7918:                         }
 7919:                     } else {
 7920:                         if (!$cancreate{$item}{$curr}) {
 7921:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7922:                                 push(@{$changes{'cancreate'}},$item);
 7923:                             }
 7924:                         }
 7925:                     }
 7926:                 }
 7927:                 foreach my $field (keys(%{$cancreate{$item}})) {
 7928:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
 7929:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
 7930:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
 7931:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
 7932:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7933:                                         push(@{$changes{'cancreate'}},$item);
 7934:                                     }
 7935:                                 }
 7936:                             } else {
 7937:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7938:                                     push(@{$changes{'cancreate'}},$item);
 7939:                                 }
 7940:                             }
 7941:                         }
 7942:                     } else {
 7943:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
 7944:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7945:                                 push(@{$changes{'cancreate'}},$item);
 7946:                             }
 7947:                         }
 7948:                     }
 7949:                 }
 7950:             }
 7951:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
 7952:             if (ref($cancreate{$item}) eq 'ARRAY') {
 7953:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
 7954:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7955:                         push(@{$changes{'cancreate'}},$item);
 7956:                     }
 7957:                 }
 7958:             } elsif (ref($cancreate{$item}) eq 'HASH') {
 7959:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
 7960:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7961:                         push(@{$changes{'cancreate'}},$item);
 7962:                     }
 7963:                 }
 7964:             }
 7965:         } elsif ($item eq 'emailusername') {
 7966:             if (ref($cancreate{$item}) eq 'HASH') {
 7967:                 foreach my $type (keys(%{$cancreate{$item}})) {
 7968:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
 7969:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
 7970:                             if ($cancreate{$item}{$type}{$field}) {
 7971:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7972:                                     push(@{$changes{'cancreate'}},$item);
 7973:                                 }
 7974:                                 last;
 7975:                             }
 7976:                         }
 7977:                     }
 7978:                 }
 7979:             }
 7980:         }
 7981:     }
 7982: #
 7983: # Populate %save_usercreate hash with updates to self-creation configuration.
 7984: #
 7985:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
 7986:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
 7987:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
 7988:     if (ref($cancreate{'notify'}) eq 'HASH') {
 7989:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
 7990:     }
 7991:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 7992:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
 7993:     }
 7994:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
 7995:     $save_usercreate{'emailrule'} = \@email_rule;
 7996: 
 7997:     my %userconfig_hash = (
 7998:             usercreation     => \%save_usercreate,
 7999:             usermodification => \%save_usermodify,
 8000:     );
 8001:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
 8002:                                              $dom);
 8003: #
 8004: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
 8005: #
 8006:     if ($putresult eq 'ok') {
 8007:         if (keys(%changes) > 0) {
 8008:             $resulttext = &mt('Changes made:').'<ul>';
 8009:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8010:                 my %lt = &selfcreation_types();
 8011:                 foreach my $type (@{$changes{'cancreate'}}) {
 8012:                     my $chgtext;
 8013:                     if ($type eq 'selfcreate') {
 8014:                         if (@{$cancreate{$type}} == 0) {
 8015:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
 8016:                         } else {
 8017:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
 8018:                                         '<ul>';
 8019:                             foreach my $case (@{$cancreate{$type}}) {
 8020:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 8021:                             }
 8022:                             $chgtext .= '</ul>';
 8023:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 8024:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 8025:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 8026:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 8027:                                             $chgtext .= '<br />'.
 8028:                                                         '<span class="LC_warning">'.
 8029:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8030:                                                         '</span>';
 8031:                                         }
 8032:                                     }
 8033:                                 }
 8034:                             }
 8035:                         }
 8036:                     } elsif ($type eq 'statustocreate') {
 8037:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 8038:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 8039:                             if (@{$cancreate{'selfcreate'}} > 0) {
 8040:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 8041:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 8042:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 8043:                                         $chgtext .= '<br />'.
 8044:                                                     '<span class="LC_warning">'.
 8045:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
 8046:                                                     '</span>';
 8047:                                     }
 8048:                                 } elsif (ref($usertypes) eq 'HASH') {
 8049:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8050:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 8051:                                     } else {
 8052:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 8053:                                     }
 8054:                                     $chgtext .= '<ul>';
 8055:                                     foreach my $case (@{$cancreate{$type}}) {
 8056:                                         if ($case eq 'default') {
 8057:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 8058:                                         } else {
 8059:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 8060:                                         }
 8061:                                     }
 8062:                                     $chgtext .= '</ul>';
 8063:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 8064:                                         $chgtext .= '<br /><span class="LC_warning">'.
 8065:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
 8066:                                                     '</span>';
 8067:                                     }
 8068:                                 }
 8069:                             } else {
 8070:                                 if (@{$cancreate{$type}} == 0) {
 8071:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 8072:                                 } else {
 8073:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 8074:                                 }
 8075:                             }
 8076:                         }
 8077:                     } elsif ($type eq 'captcha') {
 8078:                         if ($savecaptcha{$type} eq 'notused') {
 8079:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 8080:                         } else {
 8081:                             my %captchas = &captcha_phrases();
 8082:                             if ($captchas{$savecaptcha{$type}}) {
 8083:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
 8084:                             } else {
 8085:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 8086:                             }
 8087:                         }
 8088:                     } elsif ($type eq 'recaptchakeys') {
 8089:                         my ($privkey,$pubkey);
 8090:                         if (ref($savecaptcha{$type}) eq 'HASH') {
 8091:                             $pubkey = $savecaptcha{$type}{'public'};
 8092:                             $privkey = $savecaptcha{$type}{'private'};
 8093:                         }
 8094:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 8095:                         if (!$pubkey) {
 8096:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 8097:                         } else {
 8098:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8099:                         }
 8100:                         if (!$privkey) {
 8101:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 8102:                         } else {
 8103:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 8104:                         }
 8105:                         $chgtext .= '</ul>';
 8106:                     } elsif ($type eq 'emailusername') {
 8107:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
 8108:                             if (ref($types) eq 'ARRAY') {
 8109:                                 foreach my $type (@{$types}) {
 8110:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
 8111:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
 8112:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',$usertypes->{$type}).
 8113:                                                     '<ul>';
 8114:                                             foreach my $field (@{$infofields}) {
 8115:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
 8116:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
 8117:                                                 }
 8118:                                             }
 8119:                                         }
 8120:                                         $chgtext .= '</ul>';
 8121:                                     } else {
 8122:                                         $chgtext .= &mt('When self creating account with e-mail as username, no information besides e-mail address will be provided by [_1].',$usertypes->{$type}).'<br />';
 8123:                                     }
 8124:                                 }
 8125:                             }
 8126:                         }
 8127:                     } elsif ($type eq 'notify') {
 8128:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
 8129:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
 8130:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
 8131:                                 if ($cancreate{'notify'}{'approval'}) {
 8132:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
 8133:                                 }
 8134:                             }
 8135:                         }
 8136:                     }
 8137:                     if ($chgtext) {
 8138:                         $resulttext .= '<li>'.$chgtext.'</li>';
 8139:                     }
 8140:                 }
 8141:             }
 8142:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 8143:                 my ($emailrules,$emailruleorder) =
 8144:                     &Apache::lonnet::inst_userrules($dom,'email');
 8145:                 my $chgtext = '<ul>';
 8146:                 foreach my $type (@email_rule) {
 8147:                     if (ref($emailrules->{$type}) eq 'HASH') {
 8148:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 8149:                     }
 8150:                 }
 8151:                 $chgtext .= '</ul>';
 8152:                 if (@email_rule > 0) {
 8153:                     $resulttext .= '<li>'.
 8154:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
 8155:                                        $chgtext.
 8156:                                    '</li>';
 8157:                 } else {
 8158:                     $resulttext .= '<li>'.
 8159:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
 8160:                                    '</li>';
 8161:                 }
 8162:             }
 8163:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
 8164:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
 8165:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8166:                 foreach my $type (@{$changes{'selfcreate'}}) {
 8167:                     my $typename = $type;
 8168:                     if (ref($usertypes) eq 'HASH') {
 8169:                         if ($usertypes->{$type} ne '') {
 8170:                             $typename = $usertypes->{$type};
 8171:                         }
 8172:                     }
 8173:                     my @modifiable;
 8174:                     $resulttext .= '<li>'.
 8175:                                     &mt('Self-creation of account by users with status: [_1]',
 8176:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
 8177:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
 8178:                     foreach my $field (@fields) {
 8179:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
 8180:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
 8181:                         }
 8182:                     }
 8183:                     if (@modifiable > 0) {
 8184:                         $resulttext .= join(', ',@modifiable);
 8185:                     } else {
 8186:                         $resulttext .= &mt('none');
 8187:                     }
 8188:                     $resulttext .= '</li>';
 8189:                 }
 8190:                 $resulttext .= '</ul></li>';
 8191:             }
 8192:             $resulttext .= '</ul>';
 8193:         } else {
 8194:             $resulttext = &mt('No changes made to self-creation settings');
 8195:         }
 8196:     } else {
 8197:         $resulttext = '<span class="LC_error">'.
 8198:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8199:     }
 8200:     if ($warningmsg ne '') {
 8201:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 8202:     }
 8203:     return $resulttext;
 8204: }
 8205: 
 8206: sub process_captcha {
 8207:     my ($container,$changes,$newsettings,$current) = @_;
 8208:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 8209:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 8210:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 8211:         $newsettings->{'captcha'} = 'original';
 8212:     }
 8213:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 8214:         if ($container eq 'cancreate') {
 8215:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 8216:                 push(@{$changes->{'cancreate'}},'captcha');
 8217:             } elsif (!defined($changes->{'cancreate'})) {
 8218:                 $changes->{'cancreate'} = ['captcha'];
 8219:             }
 8220:         } else {
 8221:             $changes->{'captcha'} = 1;
 8222:         }
 8223:     }
 8224:     my ($newpub,$newpriv,$currpub,$currpriv);
 8225:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 8226:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 8227:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 8228:         $newpub =~ s/\W//g;
 8229:         $newpriv =~ s/\W//g;
 8230:         $newsettings->{'recaptchakeys'} = {
 8231:                                              public  => $newpub,
 8232:                                              private => $newpriv,
 8233:                                           };
 8234:     }
 8235:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 8236:         $currpub = $current->{'recaptchakeys'}{'public'};
 8237:         $currpriv = $current->{'recaptchakeys'}{'private'};
 8238:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 8239:             $newsettings->{'recaptchakeys'} = {
 8240:                                                  public  => '',
 8241:                                                  private => '',
 8242:                                               }
 8243:         }
 8244:     }
 8245:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 8246:         if ($container eq 'cancreate') {
 8247:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 8248:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 8249:             } elsif (!defined($changes->{'cancreate'})) {
 8250:                 $changes->{'cancreate'} = ['recaptchakeys'];
 8251:             }
 8252:         } else {
 8253:             $changes->{'recaptchakeys'} = 1;
 8254:         }
 8255:     }
 8256:     return;
 8257: }
 8258: 
 8259: sub modify_usermodification {
 8260:     my ($dom,%domconfig) = @_;
 8261:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
 8262:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 8263:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 8264:             if ($key eq 'selfcreate') {
 8265:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
 8266:             } else {  
 8267:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 8268:             }
 8269:         }
 8270:     }
 8271:     my @contexts = ('author','course');
 8272:     my %context_title = (
 8273:                            author => 'In author context',
 8274:                            course => 'In course context',
 8275:                         );
 8276:     my @fields = ('lastname','firstname','middlename','generation',
 8277:                   'permanentemail','id');
 8278:     my %roles = (
 8279:                   author => ['ca','aa'],
 8280:                   course => ['st','ep','ta','in','cr'],
 8281:                 );
 8282:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8283:     foreach my $context (@contexts) {
 8284:         foreach my $role (@{$roles{$context}}) {
 8285:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 8286:             foreach my $item (@fields) {
 8287:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 8288:                     $modifyhash{$context}{$role}{$item} = 1;
 8289:                 } else {
 8290:                     $modifyhash{$context}{$role}{$item} = 0;
 8291:                 }
 8292:             }
 8293:         }
 8294:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 8295:             foreach my $role (@{$roles{$context}}) {
 8296:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 8297:                     foreach my $field (@fields) {
 8298:                         if ($modifyhash{$context}{$role}{$field} ne 
 8299:                                 $curr_usermodification{$context}{$role}{$field}) {
 8300:                             push(@{$changes{$context}},$role);
 8301:                             last;
 8302:                         }
 8303:                     }
 8304:                 }
 8305:             }
 8306:         } else {
 8307:             foreach my $context (@contexts) {
 8308:                 foreach my $role (@{$roles{$context}}) {
 8309:                     push(@{$changes{$context}},$role);
 8310:                 }
 8311:             }
 8312:         }
 8313:     }
 8314:     my %usermodification_hash =  (
 8315:                                    usermodification => \%modifyhash,
 8316:                                  );
 8317:     my $putresult = &Apache::lonnet::put_dom('configuration',
 8318:                                              \%usermodification_hash,$dom);
 8319:     if ($putresult eq 'ok') {
 8320:         if (keys(%changes) > 0) {
 8321:             $resulttext = &mt('Changes made: ').'<ul>';
 8322:             foreach my $context (@contexts) {
 8323:                 if (ref($changes{$context}) eq 'ARRAY') {
 8324:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 8325:                     if (ref($changes{$context}) eq 'ARRAY') {
 8326:                         foreach my $role (@{$changes{$context}}) {
 8327:                             my $rolename;
 8328:                             if ($role eq 'cr') {
 8329:                                 $rolename = &mt('Custom');
 8330:                             } else {
 8331:                                 $rolename = &Apache::lonnet::plaintext($role);
 8332:                             }
 8333:                             my @modifiable;
 8334:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 8335:                             foreach my $field (@fields) {
 8336:                                 if ($modifyhash{$context}{$role}{$field}) {
 8337:                                     push(@modifiable,$fieldtitles{$field});
 8338:                                 }
 8339:                             }
 8340:                             if (@modifiable > 0) {
 8341:                                 $resulttext .= join(', ',@modifiable);
 8342:                             } else {
 8343:                                 $resulttext .= &mt('none'); 
 8344:                             }
 8345:                             $resulttext .= '</li>';
 8346:                         }
 8347:                         $resulttext .= '</ul></li>';
 8348:                     }
 8349:                 }
 8350:             }
 8351:             $resulttext .= '</ul>';
 8352:         } else {
 8353:             $resulttext = &mt('No changes made to user modification settings');
 8354:         }
 8355:     } else {
 8356:         $resulttext = '<span class="LC_error">'.
 8357:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8358:     }
 8359:     return $resulttext;
 8360: }
 8361: 
 8362: sub modify_defaults {
 8363:     my ($dom,$lastactref,%domconfig) = @_;
 8364:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 8365:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8366:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 8367:     my @authtypes = ('internal','krb4','krb5','localauth');
 8368:     foreach my $item (@items) {
 8369:         $newvalues{$item} = $env{'form.'.$item};
 8370:         if ($item eq 'auth_def') {
 8371:             if ($newvalues{$item} ne '') {
 8372:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 8373:                     push(@errors,$item);
 8374:                 }
 8375:             }
 8376:         } elsif ($item eq 'lang_def') {
 8377:             if ($newvalues{$item} ne '') {
 8378:                 if ($newvalues{$item} =~ /^(\w+)/) {
 8379:                     my $langcode = $1;
 8380:                     if ($langcode ne 'x_chef') {
 8381:                         if (code2language($langcode) eq '') {
 8382:                             push(@errors,$item);
 8383:                         }
 8384:                     }
 8385:                 } else {
 8386:                     push(@errors,$item);
 8387:                 }
 8388:             }
 8389:         } elsif ($item eq 'timezone_def') {
 8390:             if ($newvalues{$item} ne '') {
 8391:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 8392:                     push(@errors,$item);   
 8393:                 }
 8394:             }
 8395:         } elsif ($item eq 'datelocale_def') {
 8396:             if ($newvalues{$item} ne '') {
 8397:                 my @datelocale_ids = DateTime::Locale->ids();
 8398:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 8399:                     push(@errors,$item);
 8400:                 }
 8401:             }
 8402:         } elsif ($item eq 'portal_def') {
 8403:             if ($newvalues{$item} ne '') {
 8404:                 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])\/?$/) {
 8405:                     push(@errors,$item);
 8406:                 }
 8407:             }
 8408:         }
 8409:         if (grep(/^\Q$item\E$/,@errors)) {
 8410:             $newvalues{$item} = $domdefaults{$item};
 8411:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 8412:             $changes{$item} = 1;
 8413:         }
 8414:         $domdefaults{$item} = $newvalues{$item};
 8415:     }
 8416:     my %defaults_hash = (
 8417:                          defaults => \%newvalues,
 8418:                         );
 8419:     my $title = &defaults_titles();
 8420:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 8421:                                              $dom);
 8422:     if ($putresult eq 'ok') {
 8423:         if (keys(%changes) > 0) {
 8424:             $resulttext = &mt('Changes made:').'<ul>';
 8425:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 8426:             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";
 8427:             foreach my $item (sort(keys(%changes))) {
 8428:                 my $value = $env{'form.'.$item};
 8429:                 if ($value eq '') {
 8430:                     $value = &mt('none');
 8431:                 } elsif ($item eq 'auth_def') {
 8432:                     my %authnames = &authtype_names();
 8433:                     my %shortauth = (
 8434:                              internal => 'int',
 8435:                              krb4 => 'krb4',
 8436:                              krb5 => 'krb5',
 8437:                              localauth  => 'loc',
 8438:                     );
 8439:                     $value = $authnames{$shortauth{$value}};
 8440:                 }
 8441:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 8442:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 8443:             }
 8444:             $resulttext .= '</ul>';
 8445:             $mailmsgtext .= "\n";
 8446:             my $cachetime = 24*60*60;
 8447:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8448:             if (ref($lastactref) eq 'HASH') {
 8449:                 $lastactref->{'domdefaults'} = 1;
 8450:             }
 8451:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 8452:                 my $notify = 1;
 8453:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 8454:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 8455:                         $notify = 0;
 8456:                     }
 8457:                 }
 8458:                 if ($notify) {
 8459:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 8460:                                                "LON-CAPA Domain Settings Change - $dom",
 8461:                                                $mailmsgtext);
 8462:                 }
 8463:             }
 8464:         } else {
 8465:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 8466:         }
 8467:     } else {
 8468:         $resulttext = '<span class="LC_error">'.
 8469:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8470:     }
 8471:     if (@errors > 0) {
 8472:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 8473:         foreach my $item (@errors) {
 8474:             $resulttext .= ' "'.$title->{$item}.'",';
 8475:         }
 8476:         $resulttext =~ s/,$//;
 8477:     }
 8478:     return $resulttext;
 8479: }
 8480: 
 8481: sub modify_scantron {
 8482:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 8483:     my ($resulttext,%confhash,%changes,$errors);
 8484:     my $custom = 'custom.tab';
 8485:     my $default = 'default.tab';
 8486:     my $servadm = $r->dir_config('lonAdmEMail');
 8487:     my ($configuserok,$author_ok,$switchserver) = 
 8488:         &config_check($dom,$confname,$servadm);
 8489:     if ($env{'form.scantronformat.filename'} ne '') {
 8490:         my $error;
 8491:         if ($configuserok eq 'ok') {
 8492:             if ($switchserver) {
 8493:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 8494:             } else {
 8495:                 if ($author_ok eq 'ok') {
 8496:                     my ($result,$scantronurl) =
 8497:                         &publishlogo($r,'upload','scantronformat',$dom,
 8498:                                      $confname,'scantron','','',$custom);
 8499:                     if ($result eq 'ok') {
 8500:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 8501:                         $changes{'scantronformat'} = 1;
 8502:                     } else {
 8503:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 8504:                     }
 8505:                 } else {
 8506:                     $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);
 8507:                 }
 8508:             }
 8509:         } else {
 8510:             $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);
 8511:         }
 8512:         if ($error) {
 8513:             &Apache::lonnet::logthis($error);
 8514:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8515:         }
 8516:     }
 8517:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 8518:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 8519:             if ($env{'form.scantronformat_del'}) {
 8520:                 $confhash{'scantron'}{'scantronformat'} = '';
 8521:                 $changes{'scantronformat'} = 1;
 8522:             }
 8523:         }
 8524:     }
 8525:     if (keys(%confhash) > 0) {
 8526:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 8527:                                                  $dom);
 8528:         if ($putresult eq 'ok') {
 8529:             if (keys(%changes) > 0) {
 8530:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 8531:                     $resulttext = &mt('Changes made:').'<ul>';
 8532:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 8533:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 8534:                     } else {
 8535:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 8536:                     }
 8537:                     $resulttext .= '</ul>';
 8538:                 } else {
 8539:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 8540:                 }
 8541:                 $resulttext .= '</ul>';
 8542:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 8543:                 if (ref($lastactref) eq 'HASH') {
 8544:                     $lastactref->{'domainconfig'} = 1;
 8545:                 }
 8546:             } else {
 8547:                 $resulttext = &mt('No changes made to bubblesheet format file');
 8548:             }
 8549:         } else {
 8550:             $resulttext = '<span class="LC_error">'.
 8551:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 8552:         }
 8553:     } else {
 8554:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 8555:     }
 8556:     if ($errors) {
 8557:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 8558:                        $errors.'</ul>';
 8559:     }
 8560:     return $resulttext;
 8561: }
 8562: 
 8563: sub modify_coursecategories {
 8564:     my ($dom,%domconfig) = @_;
 8565:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 8566:         $cathash);
 8567:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 8568:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 8569:         $cathash = $domconfig{'coursecategories'}{'cats'};
 8570:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 8571:             $changes{'togglecats'} = 1;
 8572:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 8573:         }
 8574:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 8575:             $changes{'categorize'} = 1;
 8576:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 8577:         }
 8578:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 8579:             $changes{'togglecatscomm'} = 1;
 8580:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 8581:         }
 8582:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 8583:             $changes{'categorizecomm'} = 1;
 8584:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 8585:         }
 8586:     } else {
 8587:         $changes{'togglecats'} = 1;
 8588:         $changes{'categorize'} = 1;
 8589:         $changes{'togglecatscomm'} = 1;
 8590:         $changes{'categorizecomm'} = 1;
 8591:         $domconfig{'coursecategories'} = {
 8592:                                              togglecats => $env{'form.togglecats'},
 8593:                                              categorize => $env{'form.categorize'},
 8594:                                              togglecatscomm => $env{'form.togglecatscomm'},
 8595:                                              categorizecomm => $env{'form.categorizecomm'},
 8596:                                          };
 8597:     }
 8598:     if (ref($cathash) eq 'HASH') {
 8599:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 8600:             push (@deletecategory,'instcode::0');
 8601:         }
 8602:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 8603:             push(@deletecategory,'communities::0');
 8604:         }
 8605:     }
 8606:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 8607:     if (ref($cathash) eq 'HASH') {
 8608:         if (@deletecategory > 0) {
 8609:             #FIXME Need to remove category from all courses using a deleted category 
 8610:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 8611:             foreach my $item (@deletecategory) {
 8612:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 8613:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 8614:                     $deletions{$item} = 1;
 8615:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 8616:                 }
 8617:             }
 8618:         }
 8619:         foreach my $item (keys(%{$cathash})) {
 8620:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8621:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 8622:                 $reorderings{$item} = 1;
 8623:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 8624:             }
 8625:             if ($env{'form.addcategory_name_'.$item} ne '') {
 8626:                 my $newcat = $env{'form.addcategory_name_'.$item};
 8627:                 my $newdepth = $depth+1;
 8628:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 8629:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 8630:                 $adds{$newitem} = 1; 
 8631:             }
 8632:             if ($env{'form.subcat_'.$item} ne '') {
 8633:                 my $newcat = $env{'form.subcat_'.$item};
 8634:                 my $newdepth = $depth+1;
 8635:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 8636:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 8637:                 $adds{$newitem} = 1;
 8638:             }
 8639:         }
 8640:     }
 8641:     if ($env{'form.instcode'} eq '1') {
 8642:         if (ref($cathash) eq 'HASH') {
 8643:             my $newitem = 'instcode::0';
 8644:             if ($cathash->{$newitem} eq '') {  
 8645:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 8646:                 $adds{$newitem} = 1;
 8647:             }
 8648:         } else {
 8649:             my $newitem = 'instcode::0';
 8650:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 8651:             $adds{$newitem} = 1;
 8652:         }
 8653:     }
 8654:     if ($env{'form.communities'} eq '1') {
 8655:         if (ref($cathash) eq 'HASH') {
 8656:             my $newitem = 'communities::0';
 8657:             if ($cathash->{$newitem} eq '') {
 8658:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 8659:                 $adds{$newitem} = 1;
 8660:             }
 8661:         } else {
 8662:             my $newitem = 'communities::0';
 8663:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 8664:             $adds{$newitem} = 1;
 8665:         }
 8666:     }
 8667:     if ($env{'form.addcategory_name'} ne '') {
 8668:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 8669:             ($env{'form.addcategory_name'} ne 'communities')) {
 8670:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 8671:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 8672:             $adds{$newitem} = 1;
 8673:         }
 8674:     }
 8675:     my $putresult;
 8676:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 8677:         if (keys(%deletions) > 0) {
 8678:             foreach my $key (keys(%deletions)) {
 8679:                 if ($predelallitems{$key} ne '') {
 8680:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 8681:                 }
 8682:             }
 8683:         }
 8684:         my (@chkcats,@chktrails,%chkallitems);
 8685:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 8686:         if (ref($chkcats[0]) eq 'ARRAY') {
 8687:             my $depth = 0;
 8688:             my $chg = 0;
 8689:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 8690:                 my $name = $chkcats[0][$i];
 8691:                 my $item;
 8692:                 if ($name eq '') {
 8693:                     $chg ++;
 8694:                 } else {
 8695:                     $item = &escape($name).'::0';
 8696:                     if ($chg) {
 8697:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 8698:                     }
 8699:                     $depth ++; 
 8700:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 8701:                     $depth --;
 8702:                 }
 8703:             }
 8704:         }
 8705:     }
 8706:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 8707:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 8708:         if ($putresult eq 'ok') {
 8709:             my %title = (
 8710:                          togglecats     => 'Show/Hide a course in catalog',
 8711:                          categorize     => 'Assign a category to a course',
 8712:                          togglecatscomm => 'Show/Hide a community in catalog',
 8713:                          categorizecomm => 'Assign a category to a community',
 8714:                         );
 8715:             my %level = (
 8716:                          dom  => 'set in Domain ("Modify Course/Community")',
 8717:                          crs  => 'set in Course ("Course Configuration")',
 8718:                          comm => 'set in Community ("Community Configuration")',
 8719:                         );
 8720:             $resulttext = &mt('Changes made:').'<ul>';
 8721:             if ($changes{'togglecats'}) {
 8722:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 8723:             }
 8724:             if ($changes{'categorize'}) {
 8725:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 8726:             }
 8727:             if ($changes{'togglecatscomm'}) {
 8728:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 8729:             }
 8730:             if ($changes{'categorizecomm'}) {
 8731:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 8732:             }
 8733:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 8734:                 my $cathash;
 8735:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 8736:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 8737:                 } else {
 8738:                     $cathash = {};
 8739:                 } 
 8740:                 my (@cats,@trails,%allitems);
 8741:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 8742:                 if (keys(%deletions) > 0) {
 8743:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 8744:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 8745:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 8746:                     }
 8747:                     $resulttext .= '</ul></li>';
 8748:                 }
 8749:                 if (keys(%reorderings) > 0) {
 8750:                     my %sort_by_trail;
 8751:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 8752:                     foreach my $key (keys(%reorderings)) {
 8753:                         if ($allitems{$key} ne '') {
 8754:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 8755:                         }
 8756:                     }
 8757:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 8758:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 8759:                     }
 8760:                     $resulttext .= '</ul></li>';
 8761:                 }
 8762:                 if (keys(%adds) > 0) {
 8763:                     my %sort_by_trail;
 8764:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 8765:                     foreach my $key (keys(%adds)) {
 8766:                         if ($allitems{$key} ne '') {
 8767:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 8768:                         }
 8769:                     }
 8770:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 8771:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 8772:                     }
 8773:                     $resulttext .= '</ul></li>';
 8774:                 }
 8775:             }
 8776:             $resulttext .= '</ul>';
 8777:         } else {
 8778:             $resulttext = '<span class="LC_error">'.
 8779:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8780:         }
 8781:     } else {
 8782:         $resulttext = &mt('No changes made to course and community categories');
 8783:     }
 8784:     return $resulttext;
 8785: }
 8786: 
 8787: sub modify_serverstatuses {
 8788:     my ($dom,%domconfig) = @_;
 8789:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 8790:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 8791:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 8792:     }
 8793:     my @pages = &serverstatus_pages();
 8794:     foreach my $type (@pages) {
 8795:         $newserverstatus{$type}{'namedusers'} = '';
 8796:         $newserverstatus{$type}{'machines'} = '';
 8797:         if (defined($env{'form.'.$type.'_namedusers'})) {
 8798:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 8799:             my @okusers;
 8800:             foreach my $user (@users) {
 8801:                 my ($uname,$udom) = split(/:/,$user);
 8802:                 if (($udom =~ /^$match_domain$/) &&   
 8803:                     (&Apache::lonnet::domain($udom)) &&
 8804:                     ($uname =~ /^$match_username$/)) {
 8805:                     if (!grep(/^\Q$user\E/,@okusers)) {
 8806:                         push(@okusers,$user);
 8807:                     }
 8808:                 }
 8809:             }
 8810:             if (@okusers > 0) {
 8811:                  @okusers = sort(@okusers);
 8812:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 8813:             }
 8814:         }
 8815:         if (defined($env{'form.'.$type.'_machines'})) {
 8816:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 8817:             my @okmachines;
 8818:             foreach my $ip (@machines) {
 8819:                 my @parts = split(/\./,$ip);
 8820:                 next if (@parts < 4);
 8821:                 my $badip = 0;
 8822:                 for (my $i=0; $i<4; $i++) {
 8823:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 8824:                         $badip = 1;
 8825:                         last;
 8826:                     }
 8827:                 }
 8828:                 if (!$badip) {
 8829:                     push(@okmachines,$ip);     
 8830:                 }
 8831:             }
 8832:             @okmachines = sort(@okmachines);
 8833:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 8834:         }
 8835:     }
 8836:     my %serverstatushash =  (
 8837:                                 serverstatuses => \%newserverstatus,
 8838:                             );
 8839:     foreach my $type (@pages) {
 8840:         foreach my $setting ('namedusers','machines') {
 8841:             my (@current,@new);
 8842:             if (ref($currserverstatus{$type}) eq 'HASH') {
 8843:                 if ($currserverstatus{$type}{$setting} ne '') { 
 8844:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 8845:                 }
 8846:             }
 8847:             if ($newserverstatus{$type}{$setting} ne '') {
 8848:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 8849:             }
 8850:             if (@current > 0) {
 8851:                 if (@new > 0) {
 8852:                     foreach my $item (@current) {
 8853:                         if (!grep(/^\Q$item\E$/,@new)) {
 8854:                             $changes{$type}{$setting} = 1;
 8855:                             last;
 8856:                         }
 8857:                     }
 8858:                     foreach my $item (@new) {
 8859:                         if (!grep(/^\Q$item\E$/,@current)) {
 8860:                             $changes{$type}{$setting} = 1;
 8861:                             last;
 8862:                         }
 8863:                     }
 8864:                 } else {
 8865:                     $changes{$type}{$setting} = 1;
 8866:                 }
 8867:             } elsif (@new > 0) {
 8868:                 $changes{$type}{$setting} = 1;
 8869:             }
 8870:         }
 8871:     }
 8872:     if (keys(%changes) > 0) {
 8873:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 8874:         my $putresult = &Apache::lonnet::put_dom('configuration',
 8875:                                                  \%serverstatushash,$dom);
 8876:         if ($putresult eq 'ok') {
 8877:             $resulttext .= &mt('Changes made:').'<ul>';
 8878:             foreach my $type (@pages) {
 8879:                 if (ref($changes{$type}) eq 'HASH') {
 8880:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 8881:                     if ($changes{$type}{'namedusers'}) {
 8882:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 8883:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 8884:                         } else {
 8885:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 8886:                         }
 8887:                     }
 8888:                     if ($changes{$type}{'machines'}) {
 8889:                         if ($newserverstatus{$type}{'machines'} eq '') {
 8890:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 8891:                         } else {
 8892:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 8893:                         }
 8894: 
 8895:                     }
 8896:                     $resulttext .= '</ul></li>';
 8897:                 }
 8898:             }
 8899:             $resulttext .= '</ul>';
 8900:         } else {
 8901:             $resulttext = '<span class="LC_error">'.
 8902:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 8903: 
 8904:         }
 8905:     } else {
 8906:         $resulttext = &mt('No changes made to access to server status pages');
 8907:     }
 8908:     return $resulttext;
 8909: }
 8910: 
 8911: sub modify_helpsettings {
 8912:     my ($r,$dom,$confname,%domconfig) = @_;
 8913:     my ($resulttext,$errors,%changes,%helphash);
 8914:     my %defaultchecked = ('submitbugs' => 'on');
 8915:     my @offon = ('off','on');
 8916:     my @toggles = ('submitbugs');
 8917:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 8918:         foreach my $item (@toggles) {
 8919:             if ($defaultchecked{$item} eq 'on') { 
 8920:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 8921:                     if ($env{'form.'.$item} eq '0') {
 8922:                         $changes{$item} = 1;
 8923:                     }
 8924:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 8925:                     $changes{$item} = 1;
 8926:                 }
 8927:             } elsif ($defaultchecked{$item} eq 'off') {
 8928:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 8929:                     if ($env{'form.'.$item} eq '1') {
 8930:                         $changes{$item} = 1;
 8931:                     }
 8932:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 8933:                     $changes{$item} = 1;
 8934:                 }
 8935:             }
 8936:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 8937:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 8938:             }
 8939:         }
 8940:     }
 8941:     my $putresult;
 8942:     if (keys(%changes) > 0) {
 8943:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 8944:         if ($putresult eq 'ok') {
 8945:             $resulttext = &mt('Changes made:').'<ul>';
 8946:             foreach my $item (sort(keys(%changes))) {
 8947:                 if ($item eq 'submitbugs') {
 8948:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 8949:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 8950:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 8951:                 }
 8952:             }
 8953:             $resulttext .= '</ul>';
 8954:         } else {
 8955:             $resulttext = &mt('No changes made to help settings');
 8956:             $errors .= '<li><span class="LC_error">'.
 8957:                        &mt('An error occurred storing the settings: [_1]',
 8958:                            $putresult).'</span></li>';
 8959:         }
 8960:     }
 8961:     if ($errors) {
 8962:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 8963:                        $errors.'</ul>';
 8964:     }
 8965:     return $resulttext;
 8966: }
 8967: 
 8968: sub modify_coursedefaults {
 8969:     my ($dom,$lastactref,%domconfig) = @_;
 8970:     my ($resulttext,$errors,%changes,%defaultshash);
 8971:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 8972:     my @toggles = ('canuse_pdfforms');
 8973:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
 8974:                    'uploadquota_community','uploadquota_textbook');
 8975:     my @types = ('official','unofficial','community','textbook');
 8976:     my %staticdefaults = (
 8977:                            anonsurvey_threshold => 10,
 8978:                            uploadquota          => 500,
 8979:                          );
 8980: 
 8981:     $defaultshash{'coursedefaults'} = {};
 8982: 
 8983:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 8984:         if ($domconfig{'coursedefaults'} eq '') {
 8985:             $domconfig{'coursedefaults'} = {};
 8986:         }
 8987:     }
 8988: 
 8989:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 8990:         foreach my $item (@toggles) {
 8991:             if ($defaultchecked{$item} eq 'on') {
 8992:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 8993:                     ($env{'form.'.$item} eq '0')) {
 8994:                     $changes{$item} = 1;
 8995:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 8996:                     $changes{$item} = 1;
 8997:                 }
 8998:             } elsif ($defaultchecked{$item} eq 'off') {
 8999:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 9000:                     ($env{'form.'.$item} eq '1')) {
 9001:                     $changes{$item} = 1;
 9002:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 9003:                     $changes{$item} = 1;
 9004:                 }
 9005:             }
 9006:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 9007:         }
 9008:         foreach my $item (@numbers) {
 9009:             my ($currdef,$newdef);
 9010:             $newdef = $env{'form.'.$item};
 9011:             if ($item eq 'anonsurvey_threshold') {
 9012:                 $currdef = $domconfig{'coursedefaults'}{$item};
 9013:                 $newdef =~ s/\D//g;
 9014:                 if ($newdef eq '' || $newdef < 1) {
 9015:                     $newdef = 1;
 9016:                 }
 9017:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
 9018:             } else {
 9019:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
 9020:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 9021:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 9022:                 }
 9023:                 $newdef =~ s/[^\w.\-]//g;
 9024:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
 9025:             }
 9026:             if ($currdef ne $newdef) {
 9027:                 my $staticdef;
 9028:                 if ($item eq 'anonsurvey_threshold') {
 9029:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
 9030:                         $changes{$item} = 1;
 9031:                     }
 9032:                 } else {
 9033:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
 9034:                         $changes{'uploadquota'} = 1;
 9035:                     }
 9036:                 }
 9037:             }
 9038:         }
 9039:         my $officialcreds = $env{'form.official_credits'};
 9040:         $officialcreds =~ s/[^\d.]+//g;
 9041:         my $unofficialcreds = $env{'form.unofficial_credits'};
 9042:         $unofficialcreds =~ s/[^\d.]+//g;
 9043:         my $textbookcreds = $env{'form.textbook_credits'};
 9044:         $textbookcreds =~ s/[^\d.]+//g;
 9045:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
 9046:                 ($env{'form.coursecredits'} eq '1')) {
 9047:                 $changes{'coursecredits'} = 1;
 9048:         } else {
 9049:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
 9050:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds) ||
 9051:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'textbook'} ne $textbookcreds)) {
 9052:                 $changes{'coursecredits'} = 1;
 9053:             }
 9054:         }
 9055:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
 9056:             official   => $officialcreds,
 9057:             unofficial => $unofficialcreds,
 9058:             textbook   => $textbookcreds,
 9059:         }
 9060:     }
 9061:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 9062:                                              $dom);
 9063:     if ($putresult eq 'ok') {
 9064:         if (keys(%changes) > 0) {
 9065:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9066:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'}) || ($changes{'uploadquota'})) {
 9067:                 if ($changes{'canuse_pdfforms'}) {
 9068:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 9069:                 }
 9070:                 if ($changes{'coursecredits'}) {
 9071:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 9072:                         $domdefaults{'officialcredits'} =
 9073:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
 9074:                         $domdefaults{'unofficialcredits'} =
 9075:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
 9076:                         $domdefaults{'textbookcredits'} =
 9077:                             $domdefaults{'coursedefaults'}{'coursecredits'}{'textbook'};
 9078:                     }
 9079:                 }
 9080:                 if ($changes{'uploadquota'}) {
 9081:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 9082:                         foreach my $type (@types) {
 9083:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
 9084:                         }
 9085:                     }
 9086:                 }
 9087:                 my $cachetime = 24*60*60;
 9088:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9089:                 if (ref($lastactref) eq 'HASH') {
 9090:                     $lastactref->{'domdefaults'} = 1;
 9091:                 }
 9092:             }
 9093:             $resulttext = &mt('Changes made:').'<ul>';
 9094:             foreach my $item (sort(keys(%changes))) {
 9095:                 if ($item eq 'canuse_pdfforms') {
 9096:                     if ($env{'form.'.$item} eq '1') {
 9097:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 9098:                     } else {
 9099:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 9100:                     }
 9101:                 } elsif ($item eq 'anonsurvey_threshold') {
 9102:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 9103:                 } elsif ($item eq 'uploadquota') {
 9104:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 9105:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
 9106:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
 9107:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
 9108:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
 9109: 
 9110:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
 9111:                                        '</ul>'.
 9112:                                        '</li>';
 9113:                     } else {
 9114:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
 9115:                     }
 9116:                 } elsif ($item eq 'coursecredits') {
 9117:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 9118:                         if (($domdefaults{'officialcredits'} eq '') &&
 9119:                             ($domdefaults{'unofficialcredits'} eq '') &&
 9120:                             ($domdefaults{'textbookcredits'} eq '')) {
 9121:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 9122:                         } else {
 9123:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
 9124:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
 9125:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
 9126:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
 9127:                                            '</ul>'.
 9128:                                            '</li>';
 9129:                         }
 9130:                     } else {
 9131:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 9132:                     }
 9133:                 }
 9134:             }
 9135:             $resulttext .= '</ul>';
 9136:         } else {
 9137:             $resulttext = &mt('No changes made to course defaults');
 9138:         }
 9139:     } else {
 9140:         $resulttext = '<span class="LC_error">'.
 9141:             &mt('An error occurred: [_1]',$putresult).'</span>';
 9142:     }
 9143:     return $resulttext;
 9144: }
 9145: 
 9146: sub modify_usersessions {
 9147:     my ($dom,$lastactref,%domconfig) = @_;
 9148:     my @hostingtypes = ('version','excludedomain','includedomain');
 9149:     my @offloadtypes = ('primary','default');
 9150:     my %types = (
 9151:                   remote => \@hostingtypes,
 9152:                   hosted => \@hostingtypes,
 9153:                   spares => \@offloadtypes,
 9154:                 );
 9155:     my @prefixes = ('remote','hosted','spares');
 9156:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 9157:     my (%by_ip,%by_location,@intdoms);
 9158:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 9159:     my @locations = sort(keys(%by_location));
 9160:     my (%defaultshash,%changes);
 9161:     foreach my $prefix (@prefixes) {
 9162:         $defaultshash{'usersessions'}{$prefix} = {};
 9163:     }
 9164:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9165:     my $resulttext;
 9166:     my %iphost = &Apache::lonnet::get_iphost();
 9167:     foreach my $prefix (@prefixes) {
 9168:         next if ($prefix eq 'spares');
 9169:         foreach my $type (@{$types{$prefix}}) {
 9170:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 9171:             if ($type eq 'version') {
 9172:                 my $value = $env{'form.'.$prefix.'_'.$type};
 9173:                 my $okvalue;
 9174:                 if ($value ne '') {
 9175:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 9176:                         $okvalue = $value;
 9177:                     }
 9178:                 }
 9179:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 9180:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 9181:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 9182:                             if ($inuse == 0) {
 9183:                                 $changes{$prefix}{$type} = 1;
 9184:                             } else {
 9185:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 9186:                                     $changes{$prefix}{$type} = 1;
 9187:                                 }
 9188:                                 if ($okvalue ne '') {
 9189:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9190:                                 } 
 9191:                             }
 9192:                         } else {
 9193:                             if (($inuse == 1) && ($okvalue ne '')) {
 9194:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9195:                                 $changes{$prefix}{$type} = 1;
 9196:                             }
 9197:                         }
 9198:                     } else {
 9199:                         if (($inuse == 1) && ($okvalue ne '')) {
 9200:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9201:                             $changes{$prefix}{$type} = 1;
 9202:                         }
 9203:                     }
 9204:                 } else {
 9205:                     if (($inuse == 1) && ($okvalue ne '')) {
 9206:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 9207:                         $changes{$prefix}{$type} = 1;
 9208:                     }
 9209:                 }
 9210:             } else {
 9211:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 9212:                 my @okvals;
 9213:                 foreach my $val (@vals) {
 9214:                     if ($val =~ /:/) {
 9215:                         my @items = split(/:/,$val);
 9216:                         foreach my $item (@items) {
 9217:                             if (ref($by_location{$item}) eq 'ARRAY') {
 9218:                                 push(@okvals,$item);
 9219:                             }
 9220:                         }
 9221:                     } else {
 9222:                         if (ref($by_location{$val}) eq 'ARRAY') {
 9223:                             push(@okvals,$val);
 9224:                         }
 9225:                     }
 9226:                 }
 9227:                 @okvals = sort(@okvals);
 9228:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 9229:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 9230:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 9231:                             if ($inuse == 0) {
 9232:                                 $changes{$prefix}{$type} = 1; 
 9233:                             } else {
 9234:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9235:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 9236:                                 if (@changed > 0) {
 9237:                                     $changes{$prefix}{$type} = 1;
 9238:                                 }
 9239:                             }
 9240:                         } else {
 9241:                             if ($inuse == 1) {
 9242:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9243:                                 $changes{$prefix}{$type} = 1;
 9244:                             }
 9245:                         } 
 9246:                     } else {
 9247:                         if ($inuse == 1) {
 9248:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9249:                             $changes{$prefix}{$type} = 1;
 9250:                         }
 9251:                     }
 9252:                 } else {
 9253:                     if ($inuse == 1) {
 9254:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 9255:                         $changes{$prefix}{$type} = 1;
 9256:                     }
 9257:                 }
 9258:             }
 9259:         }
 9260:     }
 9261: 
 9262:     my @alldoms = &Apache::lonnet::all_domains();
 9263:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9264:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 9265:     my $savespares;
 9266: 
 9267:     foreach my $lonhost (sort(keys(%servers))) {
 9268:         my $serverhomeID =
 9269:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 9270:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 9271:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 9272:         my %spareschg;
 9273:         foreach my $type (@{$types{'spares'}}) {
 9274:             my @okspares;
 9275:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 9276:             foreach my $server (@checked) {
 9277:                 if (&Apache::lonnet::hostname($server) ne '') {
 9278:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 9279:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 9280:                             push(@okspares,$server);
 9281:                         }
 9282:                     }
 9283:                 }
 9284:             }
 9285:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 9286:             my $newspare;
 9287:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 9288:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 9289:                     $newspare = $new;
 9290:                 }
 9291:             }
 9292:             my @spares;
 9293:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 9294:                 @spares = sort(@okspares,$newspare);
 9295:             } else {
 9296:                 @spares = sort(@okspares);
 9297:             }
 9298:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 9299:             if (ref($spareid{$lonhost}) eq 'HASH') {
 9300:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 9301:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 9302:                     if (@diffs > 0) {
 9303:                         $spareschg{$type} = 1;
 9304:                     }
 9305:                 }
 9306:             }
 9307:         }
 9308:         if (keys(%spareschg) > 0) {
 9309:             $changes{'spares'}{$lonhost} = \%spareschg;
 9310:         }
 9311:     }
 9312: 
 9313:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 9314:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 9315:             if (ref($changes{'spares'}) eq 'HASH') {
 9316:                 if (keys(%{$changes{'spares'}}) > 0) {
 9317:                     $savespares = 1;
 9318:                 }
 9319:             }
 9320:         } else {
 9321:             $savespares = 1;
 9322:         }
 9323:     }
 9324: 
 9325:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 9326:     if ((keys(%changes) > 0) || ($savespares)) {
 9327:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 9328:                                                  $dom);
 9329:         if ($putresult eq 'ok') {
 9330:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 9331:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 9332:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 9333:                 }
 9334:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 9335:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 9336:                 }
 9337:             }
 9338:             my $cachetime = 24*60*60;
 9339:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9340:             if (ref($lastactref) eq 'HASH') {
 9341:                 $lastactref->{'domdefaults'} = 1;
 9342:             }
 9343:             if (keys(%changes) > 0) {
 9344:                 my %lt = &usersession_titles();
 9345:                 $resulttext = &mt('Changes made:').'<ul>';
 9346:                 foreach my $prefix (@prefixes) {
 9347:                     if (ref($changes{$prefix}) eq 'HASH') {
 9348:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 9349:                         if ($prefix eq 'spares') {
 9350:                             if (ref($changes{$prefix}) eq 'HASH') {
 9351:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 9352:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 9353:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 9354:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
 9355:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
 9356:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 9357:                                         foreach my $type (@{$types{$prefix}}) {
 9358:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 9359:                                                 my $offloadto = &mt('None');
 9360:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 9361:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 9362:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 9363:                                                     }
 9364:                                                 }
 9365:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 9366:                                             }
 9367:                                         }
 9368:                                     }
 9369:                                     $resulttext .= '</li>';
 9370:                                 }
 9371:                             }
 9372:                         } else {
 9373:                             foreach my $type (@{$types{$prefix}}) {
 9374:                                 if (defined($changes{$prefix}{$type})) {
 9375:                                     my $newvalue;
 9376:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 9377:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 9378:                                             if ($type eq 'version') {
 9379:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 9380:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 9381:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 9382:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 9383:                                                 }
 9384:                                             }
 9385:                                         }
 9386:                                     }
 9387:                                     if ($newvalue eq '') {
 9388:                                         if ($type eq 'version') {
 9389:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 9390:                                         } else {
 9391:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 9392:                                         }
 9393:                                     } else {
 9394:                                         if ($type eq 'version') {
 9395:                                             $newvalue .= ' '.&mt('(or later)'); 
 9396:                                         }
 9397:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 9398:                                     }
 9399:                                 }
 9400:                             }
 9401:                         }
 9402:                         $resulttext .= '</ul>';
 9403:                     }
 9404:                 }
 9405:                 $resulttext .= '</ul>';
 9406:             } else {
 9407:                 $resulttext = $nochgmsg;
 9408:             }
 9409:         } else {
 9410:             $resulttext = '<span class="LC_error">'.
 9411:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 9412:         }
 9413:     } else {
 9414:         $resulttext = $nochgmsg;
 9415:     }
 9416:     return $resulttext;
 9417: }
 9418: 
 9419: sub modify_loadbalancing {
 9420:     my ($dom,%domconfig) = @_;
 9421:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 9422:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 9423:     my ($othertitle,$usertypes,$types) =
 9424:         &Apache::loncommon::sorted_inst_types($dom);
 9425:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9426:     my @sparestypes = ('primary','default');
 9427:     my %typetitles = &sparestype_titles();
 9428:     my $resulttext;
 9429:     my (%currbalancer,%currtargets,%currrules,%existing);
 9430:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 9431:         %existing = %{$domconfig{'loadbalancing'}};
 9432:     }
 9433:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 9434:                               \%currtargets,\%currrules);
 9435:     my ($saveloadbalancing,%defaultshash,%changes);
 9436:     my ($alltypes,$othertypes,$titles) =
 9437:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 9438:     my %ruletitles = &offloadtype_text();
 9439:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
 9440:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
 9441:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
 9442:         if ($balancer eq '') {
 9443:             next;
 9444:         }
 9445:         if (!exists($servers{$balancer})) {
 9446:             if (exists($currbalancer{$balancer})) {
 9447:                 push(@{$changes{'delete'}},$balancer);
 9448:             }
 9449:             next;
 9450:         }
 9451:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
 9452:             push(@{$changes{'delete'}},$balancer);
 9453:             next;
 9454:         }
 9455:         if (!exists($currbalancer{$balancer})) {
 9456:             push(@{$changes{'add'}},$balancer);
 9457:         }
 9458:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
 9459:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
 9460:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
 9461:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 9462:             $saveloadbalancing = 1;
 9463:         }
 9464:         foreach my $sparetype (@sparestypes) {
 9465:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
 9466:             my @offloadto;
 9467:             foreach my $target (@targets) {
 9468:                 if (($servers{$target}) && ($target ne $balancer)) {
 9469:                     if ($sparetype eq 'default') {
 9470:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
 9471:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
 9472:                         }
 9473:                     }
 9474:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
 9475:                         push(@offloadto,$target);
 9476:                     }
 9477:                 }
 9478:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
 9479:             }
 9480:         }
 9481:         if (ref($currtargets{$balancer}) eq 'HASH') {
 9482:             foreach my $sparetype (@sparestypes) {
 9483:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
 9484:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
 9485:                     if (@targetdiffs > 0) {
 9486:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 9487:                     }
 9488:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 9489:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 9490:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 9491:                     }
 9492:                 }
 9493:             }
 9494:         } else {
 9495:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
 9496:                 foreach my $sparetype (@sparestypes) {
 9497:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 9498:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 9499:                             $changes{'curr'}{$balancer}{'targets'} = 1;
 9500:                         }
 9501:                     }
 9502:                 }
 9503:             }
 9504:         }
 9505:         my $ishomedom;
 9506:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 9507:             $ishomedom = 1;
 9508:         }
 9509:         if (ref($alltypes) eq 'ARRAY') {
 9510:             foreach my $type (@{$alltypes}) {
 9511:                 my $rule;
 9512:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
 9513:                          (!$ishomedom)) {
 9514:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
 9515:                 }
 9516:                 if ($rule eq 'specific') {
 9517:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
 9518:                 }
 9519:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
 9520:                 if (ref($currrules{$balancer}) eq 'HASH') {
 9521:                     if ($rule ne $currrules{$balancer}{$type}) {
 9522:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 9523:                     }
 9524:                 } elsif ($rule ne '') {
 9525:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 9526:                 }
 9527:             }
 9528:         }
 9529:     }
 9530:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 9531:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 9532:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
 9533:             $defaultshash{'loadbalancing'} = {};
 9534:         }
 9535:         my $putresult = &Apache::lonnet::put_dom('configuration',
 9536:                                                  \%defaultshash,$dom);
 9537:         if ($putresult eq 'ok') {
 9538:             if (keys(%changes) > 0) {
 9539:                 if (ref($changes{'delete'}) eq 'ARRAY') {
 9540:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
 9541:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
 9542:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
 9543:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
 9544:                     }
 9545:                 }
 9546:                 if (ref($changes{'add'}) eq 'ARRAY') {
 9547:                     foreach my $balancer (sort(@{$changes{'add'}})) {
 9548:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
 9549:                     }
 9550:                 }
 9551:                 if (ref($changes{'curr'}) eq 'HASH') {
 9552:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
 9553:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
 9554:                             if ($changes{'curr'}{$balancer}{'targets'}) {
 9555:                                 my %offloadstr;
 9556:                                 foreach my $sparetype (@sparestypes) {
 9557:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 9558:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 9559:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
 9560:                                         }
 9561:                                     }
 9562:                                 }
 9563:                                 if (keys(%offloadstr) == 0) {
 9564:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 9565:                                 } else {
 9566:                                     my $showoffload;
 9567:                                     foreach my $sparetype (@sparestypes) {
 9568:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
 9569:                                         if (defined($offloadstr{$sparetype})) {
 9570:                                             $showoffload .= $offloadstr{$sparetype};
 9571:                                         } else {
 9572:                                             $showoffload .= &mt('None');
 9573:                                         }
 9574:                                         $showoffload .= ('&nbsp;'x3);
 9575:                                     }
 9576:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
 9577:                                 }
 9578:                             }
 9579:                         }
 9580:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
 9581:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
 9582:                                 foreach my $type (@{$alltypes}) {
 9583:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
 9584:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
 9585:                                         my $balancetext;
 9586:                                         if ($rule eq '') {
 9587:                                             $balancetext =  $ruletitles{'default'};
 9588:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
 9589:                                                  ($rule eq 'balancer') || ($rule eq 'offloadedto')) {
 9590:                                             $balancetext =  $ruletitles{$rule};
 9591:                                         } else {
 9592:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
 9593:                                         }
 9594:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
 9595:                                     }
 9596:                                 }
 9597:                             }
 9598:                         }
 9599:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
 9600:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
 9601:                     }
 9602:                 }
 9603:                 if ($resulttext ne '') {
 9604:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
 9605:                 } else {
 9606:                     $resulttext = $nochgmsg;
 9607:                 }
 9608:             } else {
 9609:                 $resulttext = $nochgmsg;
 9610:             }
 9611:         } else {
 9612:             $resulttext = '<span class="LC_error">'.
 9613:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 9614:         }
 9615:     } else {
 9616:         $resulttext = $nochgmsg;
 9617:     }
 9618:     return $resulttext;
 9619: }
 9620: 
 9621: sub recurse_check {
 9622:     my ($chkcats,$categories,$depth,$name) = @_;
 9623:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 9624:         my $chg = 0;
 9625:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 9626:             my $category = $chkcats->[$depth]{$name}[$j];
 9627:             my $item;
 9628:             if ($category eq '') {
 9629:                 $chg ++;
 9630:             } else {
 9631:                 my $deeper = $depth + 1;
 9632:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 9633:                 if ($chg) {
 9634:                     $categories->{$item} -= $chg;
 9635:                 }
 9636:                 &recurse_check($chkcats,$categories,$deeper,$category);
 9637:                 $deeper --;
 9638:             }
 9639:         }
 9640:     }
 9641:     return;
 9642: }
 9643: 
 9644: sub recurse_cat_deletes {
 9645:     my ($item,$coursecategories,$deletions) = @_;
 9646:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 9647:     my $subdepth = $depth + 1;
 9648:     if (ref($coursecategories) eq 'HASH') {
 9649:         foreach my $subitem (keys(%{$coursecategories})) {
 9650:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 9651:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 9652:                 delete($coursecategories->{$subitem});
 9653:                 $deletions->{$subitem} = 1;
 9654:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 9655:             }
 9656:         }
 9657:     }
 9658:     return;
 9659: }
 9660: 
 9661: sub get_active_dcs {
 9662:     my ($dom) = @_;
 9663:     my $now = time;
 9664:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
 9665:     my %domcoords;
 9666:     my $numdcs = 0;
 9667:     foreach my $server (keys(%dompersonnel)) {
 9668:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 9669:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 9670:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 9671:         }
 9672:     }
 9673:     return %domcoords;
 9674: }
 9675: 
 9676: sub active_dc_picker {
 9677:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
 9678:     my %domcoords = &get_active_dcs($dom); 
 9679:     my @domcoord = keys(%domcoords);
 9680:     if (keys(%currhash)) {
 9681:         foreach my $dc (keys(%currhash)) {
 9682:             unless (exists($domcoords{$dc})) {
 9683:                 push(@domcoord,$dc);
 9684:             }
 9685:         }
 9686:     }
 9687:     @domcoord = sort(@domcoord);
 9688:     my $numdcs = scalar(@domcoord);
 9689:     my $rows = 0;
 9690:     my $table;
 9691:     if ($numdcs > 1) {
 9692:         $table = '<table>';
 9693:         for (my $i=0; $i<@domcoord; $i++) {
 9694:             my $rem = $i%($numinrow);
 9695:             if ($rem == 0) {
 9696:                 if ($i > 0) {
 9697:                     $table .= '</tr>';
 9698:                 }
 9699:                 $table .= '<tr>';
 9700:                 $rows ++;
 9701:             }
 9702:             my $check = '';
 9703:             if ($inputtype eq 'radio') {
 9704:                 if (keys(%currhash) == 0) {
 9705:                     if (!$i) {
 9706:                         $check = ' checked="checked"';
 9707:                     }
 9708:                 } elsif (exists($currhash{$domcoord[$i]})) {
 9709:                     $check = ' checked="checked"';
 9710:                 }
 9711:             } else {
 9712:                 if (exists($currhash{$domcoord[$i]})) {
 9713:                     $check = ' checked="checked"';
 9714:                 }
 9715:             }
 9716:             if ($i == @domcoord - 1) {
 9717:                 my $colsleft = $numinrow - $rem;
 9718:                 if ($colsleft > 1) {
 9719:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
 9720:                 } else {
 9721:                     $table .= '<td class="LC_left_item">';
 9722:                 }
 9723:             } else {
 9724:                 $table .= '<td class="LC_left_item">';
 9725:             }
 9726:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
 9727:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
 9728:             $table .= '<span class="LC_nobreak"><label>'.
 9729:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
 9730:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
 9731:             if ($user ne $dcname.':'.$dcdom) {
 9732:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
 9733:             }
 9734:             $table .= '</label></span></td>';
 9735:         }
 9736:         $table .= '</tr></table>';
 9737:     } elsif ($numdcs == 1) {
 9738:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
 9739:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
 9740:         if ($inputtype eq 'radio') {
 9741:             $table .= '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
 9742:             if ($user ne $dcname.':'.$dcdom) {
 9743:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
 9744:             }
 9745:         } else {
 9746:             my $check;
 9747:             if (exists($currhash{$domcoord[0]})) {
 9748:                 $check = ' checked="checked"';
 9749:             }
 9750:             $table .= '<span class="LC_nobreak"><label>'.
 9751:                       '<input type="checkbox" name="'.$name.'" '.
 9752:                       'value="'.$domcoord[0].'"'.$check.' />'.$user;
 9753:             if ($user ne $dcname.':'.$dcdom) {
 9754:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
 9755:             }
 9756:             $table .= '</label></span>';
 9757:             $rows ++;
 9758:         }
 9759:     }
 9760:     return ($numdcs,$table,$rows);
 9761: }
 9762: 
 9763: sub usersession_titles {
 9764:     return &Apache::lonlocal::texthash(
 9765:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
 9766:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
 9767:                spares => 'Servers offloaded to, when busy',
 9768:                version => 'LON-CAPA version requirement',
 9769:                excludedomain => 'Allow all, but exclude specific domains',
 9770:                includedomain => 'Deny all, but include specific domains',
 9771:                primary => 'Primary (checked first)',
 9772:                default => 'Default',
 9773:            );
 9774: }
 9775: 
 9776: sub id_for_thisdom {
 9777:     my (%servers) = @_;
 9778:     my %altids;
 9779:     foreach my $server (keys(%servers)) {
 9780:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 9781:         if ($serverhome ne $server) {
 9782:             $altids{$serverhome} = $server;
 9783:         }
 9784:     }
 9785:     return %altids;
 9786: }
 9787: 
 9788: sub count_servers {
 9789:     my ($currbalancer,%servers) = @_;
 9790:     my (@spares,$numspares);
 9791:     foreach my $lonhost (sort(keys(%servers))) {
 9792:         next if ($currbalancer eq $lonhost);
 9793:         push(@spares,$lonhost);
 9794:     }
 9795:     if ($currbalancer) {
 9796:         $numspares = scalar(@spares);
 9797:     } else {
 9798:         $numspares = scalar(@spares) - 1;
 9799:     }
 9800:     return ($numspares,@spares);
 9801: }
 9802: 
 9803: sub lonbalance_targets_js {
 9804:     my ($dom,$types,$servers,$settings) = @_;
 9805:     my $select = &mt('Select');
 9806:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
 9807:     if (ref($servers) eq 'HASH') {
 9808:         $alltargets = join("','",sort(keys(%{$servers})));
 9809:         my @homedoms;
 9810:         foreach my $server (sort(keys(%{$servers}))) {
 9811:             if (&Apache::lonnet::host_domain($server) eq $dom) {
 9812:                 push(@homedoms,'1');
 9813:             } else {
 9814:                 push(@homedoms,'0');
 9815:             }
 9816:         }
 9817:         $allishome = join("','",@homedoms);
 9818:     }
 9819:     if (ref($types) eq 'ARRAY') {
 9820:         if (@{$types} > 0) {
 9821:             @alltypes = @{$types};
 9822:         }
 9823:     }
 9824:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 9825:     $allinsttypes = join("','",@alltypes);
 9826:     my (%currbalancer,%currtargets,%currrules,%existing);
 9827:     if (ref($settings) eq 'HASH') {
 9828:         %existing = %{$settings};
 9829:     }
 9830:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
 9831:                               \%currtargets,\%currrules);
 9832:     my $balancers = join("','",sort(keys(%currbalancer)));
 9833:     return <<"END";
 9834: 
 9835: <script type="text/javascript">
 9836: // <![CDATA[
 9837: 
 9838: currBalancers = new Array('$balancers');
 9839: 
 9840: function toggleTargets(balnum) {
 9841:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 9842:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
 9843:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
 9844:     var prevbalancer = prevhostitem.value;
 9845:     var baltotal = document.getElementById('loadbalancing_total').value;
 9846:     prevhostitem.value = balancer;
 9847:     if (prevbalancer != '') {
 9848:         var prevIdx = currBalancers.indexOf(prevbalancer);
 9849:         if (prevIdx != -1) {
 9850:             currBalancers.splice(prevIdx,1);
 9851:         }
 9852:     }
 9853:     if (balancer == '') {
 9854:         hideSpares(balnum);
 9855:     } else {
 9856:         var currIdx = currBalancers.indexOf(balancer);
 9857:         if (currIdx == -1) {
 9858:             currBalancers.push(balancer);
 9859:         }
 9860:         var homedoms = new Array('$allishome');
 9861:         var ishomedom = homedoms[lonhostitem.selectedIndex];
 9862:         showSpares(balancer,ishomedom,balnum);
 9863:     }
 9864:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
 9865:     return;
 9866: }
 9867: 
 9868: function showSpares(balancer,ishomedom,balnum) {
 9869:     var alltargets = new Array('$alltargets');
 9870:     var insttypes = new Array('$allinsttypes');
 9871:     var offloadtypes = new Array('primary','default');
 9872: 
 9873:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
 9874:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
 9875:  
 9876:     for (var i=0; i<offloadtypes.length; i++) {
 9877:         var count = 0;
 9878:         for (var j=0; j<alltargets.length; j++) {
 9879:             if (alltargets[j] != balancer) {
 9880:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
 9881:                 item.value = alltargets[j];
 9882:                 item.style.textAlign='left';
 9883:                 item.style.textFace='normal';
 9884:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
 9885:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
 9886:                     item.disabled = '';
 9887:                 } else {
 9888:                     item.disabled = 'disabled';
 9889:                     item.checked = false;
 9890:                 }
 9891:                 count ++;
 9892:             }
 9893:         }
 9894:     }
 9895:     for (var k=0; k<insttypes.length; k++) {
 9896:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
 9897:             if (ishomedom == 1) {
 9898:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 9899:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 9900:             } else {
 9901:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 9902:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 9903:             }
 9904:         } else {
 9905:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 9906:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 9907:         }
 9908:         if ((insttypes[k] != '_LC_external') && 
 9909:             ((insttypes[k] != '_LC_internetdom') ||
 9910:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
 9911:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
 9912:             item.options.length = 0;
 9913:             item.options[0] = new Option("","",true,true);
 9914:             var idx = 0;
 9915:             for (var m=0; m<alltargets.length; m++) {
 9916:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
 9917:                     idx ++;
 9918:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 9919:                 }
 9920:             }
 9921:         }
 9922:     }
 9923:     return;
 9924: }
 9925: 
 9926: function hideSpares(balnum) {
 9927:     var alltargets = new Array('$alltargets');
 9928:     var insttypes = new Array('$allinsttypes');
 9929:     var offloadtypes = new Array('primary','default');
 9930: 
 9931:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
 9932:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
 9933: 
 9934:     var total = alltargets.length - 1;
 9935:     for (var i=0; i<offloadtypes; i++) {
 9936:         for (var j=0; j<total; j++) {
 9937:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
 9938:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
 9939:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
 9940:         }
 9941:     }
 9942:     for (var k=0; k<insttypes.length; k++) {
 9943:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 9944:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 9945:         if (insttypes[k] != '_LC_external') {
 9946:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
 9947:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
 9948:         }
 9949:     }
 9950:     return;
 9951: }
 9952: 
 9953: function checkOffloads(item,balnum,type) {
 9954:     var alltargets = new Array('$alltargets');
 9955:     var offloadtypes = new Array('primary','default');
 9956:     if (item.checked) {
 9957:         var total = alltargets.length - 1;
 9958:         var other;
 9959:         if (type == offloadtypes[0]) {
 9960:             other = offloadtypes[1];
 9961:         } else {
 9962:             other = offloadtypes[0];
 9963:         }
 9964:         for (var i=0; i<total; i++) {
 9965:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
 9966:             if (server == item.value) {
 9967:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
 9968:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
 9969:                 }
 9970:             }
 9971:         }
 9972:     }
 9973:     return;
 9974: }
 9975: 
 9976: function singleServerToggle(balnum,type) {
 9977:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
 9978:     if (offloadtoSelIdx == 0) {
 9979:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
 9980:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 9981: 
 9982:     } else {
 9983:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
 9984:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 9985:     }
 9986:     return;
 9987: }
 9988: 
 9989: function balanceruleChange(formname,balnum,type) {
 9990:     if (type == '_LC_external') {
 9991:         return;
 9992:     }
 9993:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
 9994:     for (var i=0; i<typesRules.length; i++) {
 9995:         if (formname.elements[typesRules[i]].checked) {
 9996:             if (formname.elements[typesRules[i]].value != 'specific') {
 9997:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
 9998:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 9999:             } else {
10000:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
10001:             }
10002:         }
10003:     }
10004:     return;
10005: }
10006: 
10007: function balancerDeleteChange(balnum) {
10008:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
10009:     var baltotal = document.getElementById('loadbalancing_total').value;
10010:     var addtarget;
10011:     var removetarget;
10012:     var action = 'delete';
10013:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
10014:         var lonhost = hostitem.value;
10015:         var currIdx = currBalancers.indexOf(lonhost);
10016:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
10017:             if (currIdx != -1) {
10018:                 currBalancers.splice(currIdx,1);
10019:             }
10020:             addtarget = lonhost;
10021:         } else {
10022:             if (currIdx == -1) {
10023:                 currBalancers.push(lonhost);
10024:             }
10025:             removetarget = lonhost;
10026:             action = 'undelete';
10027:         }
10028:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
10029:     }
10030:     return;
10031: }
10032: 
10033: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
10034:     if (baltotal > 1) {
10035:         var offloadtypes = new Array('primary','default');
10036:         var alltargets = new Array('$alltargets');
10037:         var insttypes = new Array('$allinsttypes');
10038:         for (var i=0; i<baltotal; i++) {
10039:             if (i != balnum) {
10040:                 for (var j=0; j<offloadtypes.length; j++) {
10041:                     var total = alltargets.length - 1;
10042:                     for (var k=0; k<total; k++) {
10043:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
10044:                         var server = serveritem.value;
10045:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
10046:                             if (server == addtarget) {
10047:                                 serveritem.disabled = '';
10048:                             }
10049:                         }
10050:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
10051:                             if (server == removetarget) {
10052:                                 serveritem.disabled = 'disabled';
10053:                                 serveritem.checked = false;
10054:                             }
10055:                         }
10056:                     }
10057:                 }
10058:                 for (var j=0; j<insttypes.length; j++) {
10059:                     if (insttypes[j] != '_LC_external') {
10060:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
10061:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
10062:                             var currSel = singleserver.selectedIndex;
10063:                             var currVal = singleserver.options[currSel].value;
10064:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
10065:                                 var numoptions = singleserver.options.length;
10066:                                 var needsnew = 1;
10067:                                 for (var k=0; k<numoptions; k++) {
10068:                                     if (singleserver.options[k] == addtarget) {
10069:                                         needsnew = 0;
10070:                                         break;
10071:                                     }
10072:                                 }
10073:                                 if (needsnew == 1) {
10074:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
10075:                                 }
10076:                             }
10077:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
10078:                                 singleserver.options.length = 0;
10079:                                 if ((currVal) && (currVal != removetarget)) {
10080:                                     singleserver.options[0] = new Option("","",false,false);
10081:                                 } else {
10082:                                     singleserver.options[0] = new Option("","",true,true);
10083:                                 }
10084:                                 var idx = 0;
10085:                                 for (var m=0; m<alltargets.length; m++) {
10086:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
10087:                                         idx ++;
10088:                                         if (currVal == alltargets[m]) {
10089:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
10090:                                         } else {
10091:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
10092:                                         }
10093:                                     }
10094:                                 }
10095:                             }
10096:                         }
10097:                     }
10098:                 }
10099:             }
10100:         }
10101:     }
10102:     return;
10103: }
10104: 
10105: // ]]>
10106: </script>
10107: 
10108: END
10109: }
10110: 
10111: sub new_spares_js {
10112:     my @sparestypes = ('primary','default');
10113:     my $types = join("','",@sparestypes);
10114:     my $select = &mt('Select');
10115:     return <<"END";
10116: 
10117: <script type="text/javascript">
10118: // <![CDATA[
10119: 
10120: function updateNewSpares(formname,lonhost) {
10121:     var types = new Array('$types');
10122:     var include = new Array();
10123:     var exclude = new Array();
10124:     for (var i=0; i<types.length; i++) {
10125:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
10126:         for (var j=0; j<spareboxes.length; j++) {
10127:             if (formname.elements[spareboxes[j]].checked) {
10128:                 exclude.push(formname.elements[spareboxes[j]].value);
10129:             } else {
10130:                 include.push(formname.elements[spareboxes[j]].value);
10131:             }
10132:         }
10133:     }
10134:     for (var i=0; i<types.length; i++) {
10135:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
10136:         var selIdx = newSpare.selectedIndex;
10137:         var currnew = newSpare.options[selIdx].value;
10138:         var okSpares = new Array();
10139:         for (var j=0; j<newSpare.options.length; j++) {
10140:             var possible = newSpare.options[j].value;
10141:             if (possible != '') {
10142:                 if (exclude.indexOf(possible) == -1) {
10143:                     okSpares.push(possible);
10144:                 } else {
10145:                     if (currnew == possible) {
10146:                         selIdx = 0;
10147:                     }
10148:                 }
10149:             }
10150:         }
10151:         for (var k=0; k<include.length; k++) {
10152:             if (okSpares.indexOf(include[k]) == -1) {
10153:                 okSpares.push(include[k]);
10154:             }
10155:         }
10156:         okSpares.sort();
10157:         newSpare.options.length = 0;
10158:         if (selIdx == 0) {
10159:             newSpare.options[0] = new Option("$select","",true,true);
10160:         } else {
10161:             newSpare.options[0] = new Option("$select","",false,false);
10162:         }
10163:         for (var m=0; m<okSpares.length; m++) {
10164:             var idx = m+1;
10165:             var selThis = 0;
10166:             if (selIdx != 0) {
10167:                 if (okSpares[m] == currnew) {
10168:                     selThis = 1;
10169:                 }
10170:             }
10171:             if (selThis == 1) {
10172:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
10173:             } else {
10174:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
10175:             }
10176:         }
10177:     }
10178:     return;
10179: }
10180: 
10181: function checkNewSpares(lonhost,type) {
10182:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
10183:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
10184:     if (chosen != '') { 
10185:         var othertype;
10186:         var othernewSpare;
10187:         if (type == 'primary') {
10188:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
10189:         }
10190:         if (type == 'default') {
10191:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
10192:         }
10193:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
10194:             othernewSpare.selectedIndex = 0;
10195:         }
10196:     }
10197:     return;
10198: }
10199: 
10200: // ]]>
10201: </script>
10202: 
10203: END
10204: 
10205: }
10206: 
10207: sub common_domprefs_js {
10208:     return <<"END";
10209: 
10210: <script type="text/javascript">
10211: // <![CDATA[
10212: 
10213: function getIndicesByName(formname,item) {
10214:     var group = new Array();
10215:     for (var i=0;i<formname.elements.length;i++) {
10216:         if (formname.elements[i].name == item) {
10217:             group.push(formname.elements[i].id);
10218:         }
10219:     }
10220:     return group;
10221: }
10222: 
10223: // ]]>
10224: </script>
10225: 
10226: END
10227: 
10228: }
10229: 
10230: sub recaptcha_js {
10231:     my %lt = &captcha_phrases();
10232:     return <<"END";
10233: 
10234: <script type="text/javascript">
10235: // <![CDATA[
10236: 
10237: function updateCaptcha(caller,context) {
10238:     var privitem;
10239:     var pubitem;
10240:     var privtext;
10241:     var pubtext;
10242:     if (document.getElementById(context+'_recaptchapub')) {
10243:         pubitem = document.getElementById(context+'_recaptchapub');
10244:     } else {
10245:         return;
10246:     }
10247:     if (document.getElementById(context+'_recaptchapriv')) {
10248:         privitem = document.getElementById(context+'_recaptchapriv');
10249:     } else {
10250:         return;
10251:     }
10252:     if (document.getElementById(context+'_recaptchapubtxt')) {
10253:         pubtext = document.getElementById(context+'_recaptchapubtxt');
10254:     } else {
10255:         return;
10256:     }
10257:     if (document.getElementById(context+'_recaptchaprivtxt')) {
10258:         privtext = document.getElementById(context+'_recaptchaprivtxt');
10259:     } else {
10260:         return;
10261:     }
10262:     if (caller.checked) {
10263:         if (caller.value == 'recaptcha') {
10264:             pubitem.type = 'text';
10265:             privitem.type = 'text';
10266:             pubitem.size = '40';
10267:             privitem.size = '40';
10268:             pubtext.innerHTML = "$lt{'pub'}";
10269:             privtext.innerHTML = "$lt{'priv'}";
10270:         } else {
10271:             pubitem.type = 'hidden';
10272:             privitem.type = 'hidden';
10273:             pubtext.innerHTML = '';
10274:             privtext.innerHTML = '';
10275:         }
10276:     }
10277:     return;
10278: }
10279: 
10280: // ]]>
10281: </script>
10282: 
10283: END
10284: 
10285: }
10286: 
10287: sub credits_js {
10288:     return <<"END";
10289: 
10290: <script type="text/javascript">
10291: // <![CDATA[
10292: 
10293: function toggleCredits(domForm) {
10294:     if (document.getElementById('credits')) {
10295:         creditsitem = document.getElementById('credits');
10296:         var creditsLength = domForm.coursecredits.length;
10297:         if (creditsLength) {
10298:             var currval;
10299:             for (var i=0; i<creditsLength; i++) {
10300:                 if (domForm.coursecredits[i].checked) {
10301:                    currval = domForm.coursecredits[i].value;
10302:                 }
10303:             }
10304:             if (currval == 1) {
10305:                 creditsitem.style.display = 'block';
10306:             } else {
10307:                 creditsitem.style.display = 'none';
10308:             }
10309:         }
10310:     }
10311:     return;
10312: }
10313: 
10314: // ]]>
10315: </script>
10316: 
10317: END
10318: 
10319: }
10320: 
10321: sub captcha_phrases {
10322:     return &Apache::lonlocal::texthash (
10323:                  priv => 'Private key',
10324:                  pub  => 'Public key',
10325:                  original  => 'original (CAPTCHA)',
10326:                  recaptcha => 'successor (ReCAPTCHA)',
10327:                  notused   => 'unused',
10328:     );
10329: }
10330: 
10331: sub devalidate_remote_domconfs {
10332:     my ($dom,$cachekeys) = @_;
10333:     return unless (ref($cachekeys) eq 'HASH');
10334:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10335:     my %thismachine;
10336:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
10337:     my @posscached = ('domainconfig','domdefaults');
10338:     if (keys(%servers) > 1) {
10339:         foreach my $server (keys(%servers)) {
10340:             next if ($thismachine{$server});
10341:             my @cached;
10342:             foreach my $name (@posscached) {
10343:                 if ($cachekeys->{$name}) {
10344:                     push(@cached,&escape($name).':'.&escape($dom));
10345:                 }
10346:             }
10347:             if (@cached) {
10348:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
10349:             }
10350:         }
10351:     }
10352:     return;
10353: }
10354: 
10355: 1;

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