File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.217: download - view: text, annotated - select for diffs
Tue Dec 24 21:59:36 2013 UTC (10 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Javascript to update select boxes when reordering available textbooks
  for use in "Textbook" courses.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.217 2013/12/24 21:59:36 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','helpsettings',
  215:                 'coursedefaults','usersessions','loadbalancing',
  216:                 'requestauthor'],$dom);
  217:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  218:                        'autoupdate','autocreate','directorysrch','contacts',
  219:                        'usercreation','usermodification','scantron',
  220:                        'requestcourses','requestauthor','coursecategories',
  221:                        'serverstatuses','helpsettings',
  222:                        'coursedefaults','usersessions');
  223:     my %existing;
  224:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  225:         %existing = %{$domconfig{'loadbalancing'}};
  226:     }
  227:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  228:         push(@prefs_order,'loadbalancing');
  229:     }
  230:     my %prefs = (
  231:         'rolecolors' =>
  232:                    { text => 'Default color schemes',
  233:                      help => 'Domain_Configuration_Color_Schemes',
  234:                      header => [{col1 => 'Student Settings',
  235:                                  col2 => '',},
  236:                                 {col1 => 'Coordinator Settings',
  237:                                  col2 => '',},
  238:                                 {col1 => 'Author Settings',
  239:                                  col2 => '',},
  240:                                 {col1 => 'Administrator Settings',
  241:                                  col2 => '',}],
  242:                     },
  243:         'login' =>
  244:                     { text => 'Log-in page options',
  245:                       help => 'Domain_Configuration_Login_Page',
  246:                       header => [{col1 => 'Log-in Page Items',
  247:                                   col2 => '',},
  248:                                  {col1 => 'Log-in Help',
  249:                                   col2 => 'Value'}],
  250:                     },
  251:         'defaults' => 
  252:                     { text => 'Default authentication/language/timezone/portal',
  253:                       help => 'Domain_Configuration_LangTZAuth',
  254:                       header => [{col1 => 'Setting',
  255:                                   col2 => 'Value'}],
  256:                     },
  257:         'quotas' => 
  258:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  259:                       help => 'Domain_Configuration_Quotas',
  260:                       header => [{col1 => 'User affiliation',
  261:                                   col2 => 'Available tools',
  262:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  263:                     },
  264:         'autoenroll' =>
  265:                    { text => 'Auto-enrollment settings',
  266:                      help => 'Domain_Configuration_Auto_Enrollment',
  267:                      header => [{col1 => 'Configuration setting',
  268:                                  col2 => 'Value(s)'}],
  269:                    },
  270:         'autoupdate' => 
  271:                    { text => 'Auto-update settings',
  272:                      help => 'Domain_Configuration_Auto_Updates',
  273:                      header => [{col1 => 'Setting',
  274:                                  col2 => 'Value',},
  275:                                 {col1 => 'Setting',
  276:                                  col2 => 'Affiliation'},
  277:                                 {col1 => 'User population',
  278:                                  col2 => 'Updateable user data'}],
  279:                   },
  280:         'autocreate' => 
  281:                   { text => 'Auto-course creation settings',
  282:                      help => 'Domain_Configuration_Auto_Creation',
  283:                      header => [{col1 => 'Configuration Setting',
  284:                                  col2 => 'Value',}],
  285:                   },
  286:         'directorysrch' => 
  287:                   { text => 'Institutional directory searches',
  288:                     help => 'Domain_Configuration_InstDirectory_Search',
  289:                     header => [{col1 => 'Setting',
  290:                                 col2 => 'Value',}],
  291:                   },
  292:         'contacts' =>
  293:                   { text => 'Contact Information',
  294:                     help => 'Domain_Configuration_Contact_Info',
  295:                     header => [{col1 => 'Setting',
  296:                                 col2 => 'Value',}],
  297:                   },
  298: 
  299:         'usercreation' => 
  300:                   { text => 'User creation',
  301:                     help => 'Domain_Configuration_User_Creation',
  302:                     header => [{col1 => 'Format rule type',
  303:                                 col2 => 'Format rules in force'},
  304:                                {col1 => 'User account creation',
  305:                                 col2 => 'Usernames which may be created',},
  306:                                {col1 => 'Context',
  307:                                 col2 => 'Assignable authentication types'}],
  308:                   },
  309:         'usermodification' =>
  310:                   { text => 'User modification',
  311:                     help => 'Domain_Configuration_User_Modification',
  312:                     header => [{col1 => 'Target user has role',
  313:                                 col2 => 'User information updateable in author context'},
  314:                                {col1 => 'Target user has role',
  315:                                 col2 => 'User information updateable in course context'},
  316:                                {col1 => "Status of user",
  317:                                 col2 => 'Information settable when self-creating account (if directory data blank)'}],
  318:                   },
  319:         'scantron' =>
  320:                   { text => 'Bubblesheet format file',
  321:                     help => 'Domain_Configuration_Scantron_Format',
  322:                     header => [ {col1 => 'Item',
  323:                                  col2 => '',
  324:                               }],
  325:                   },
  326:         'requestcourses' => 
  327:                  {text => 'Request creation of courses',
  328:                   help => 'Domain_Configuration_Request_Courses',
  329:                   header => [{col1 => 'User affiliation',
  330:                               col2 => 'Availability/Processing of requests',},
  331:                              {col1 => 'Setting',
  332:                               col2 => 'Value'},
  333:                              {col1 => 'Available textbooks',
  334:                               col2 => ''}],
  335:                  },
  336:         'requestauthor' =>
  337:                  {text => 'Request authoring space',
  338:                   help => 'Domain_Configuration_Request_Author',
  339:                   header => [{col1 => 'User affiliation',
  340:                               col2 => 'Availability/Processing of requests',},
  341:                              {col1 => 'Setting',
  342:                               col2 => 'Value'}],
  343:                  },
  344:         'coursecategories' =>
  345:                   { text => 'Cataloging of courses/communities',
  346:                     help => 'Domain_Configuration_Cataloging_Courses',
  347:                     header => [{col1 => 'Category settings',
  348:                                 col2 => '',},
  349:                                {col1 => 'Categories',
  350:                                 col2 => '',
  351:                                }],
  352:                   },
  353:         'serverstatuses' =>
  354:                  {text   => 'Access to server status pages',
  355:                   help   => 'Domain_Configuration_Server_Status',
  356:                   header => [{col1 => 'Status Page',
  357:                               col2 => 'Other named users',
  358:                               col3 => 'Specific IPs',
  359:                             }],
  360:                  },
  361:         'helpsettings' =>
  362:                  {text   => 'Help page settings',
  363:                   help   => 'Domain_Configuration_Help_Settings',
  364:                   header => [{col1 => 'Help Settings (logged-in users)',
  365:                               col2 => 'Value'}],
  366:                  },
  367:         'coursedefaults' => 
  368:                  {text => 'Course/Community defaults',
  369:                   help => 'Domain_Configuration_Course_Defaults',
  370:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  371:                               col2 => 'Value',},
  372:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  373:                               col2 => 'Value',},],
  374:                  },
  375:         'privacy' => 
  376:                  {text   => 'User Privacy',
  377:                   help   => 'Domain_Configuration_User_Privacy',
  378:                   header => [{col1 => 'Setting',
  379:                               col2 => 'Value',}],
  380:                  },
  381:         'usersessions' =>
  382:                  {text  => 'User session hosting/offloading',
  383:                   help  => 'Domain_Configuration_User_Sessions',
  384:                   header => [{col1 => 'Domain server',
  385:                               col2 => 'Servers to offload sessions to when busy'},
  386:                              {col1 => 'Hosting of users from other domains',
  387:                               col2 => 'Rules'},
  388:                              {col1 => "Hosting domain's own users elsewhere",
  389:                               col2 => 'Rules'}],
  390:                  },
  391:          'loadbalancing' =>
  392:                  {text  => 'Dedicated Load Balancer(s)',
  393:                   help  => 'Domain_Configuration_Load_Balancing',
  394:                   header => [{col1 => 'Balancers',
  395:                               col2 => 'Default destinations',
  396:                               col3 => 'User affiliation',
  397:                               col4 => 'Overrides'},
  398:                             ],
  399:                  },
  400:     );
  401:     if (keys(%servers) > 1) {
  402:         $prefs{'login'}  = { text   => 'Log-in page options',
  403:                              help   => 'Domain_Configuration_Login_Page',
  404:                             header => [{col1 => 'Log-in Service',
  405:                                         col2 => 'Server Setting',},
  406:                                        {col1 => 'Log-in Page Items',
  407:                                         col2 => ''},
  408:                                        {col1 => 'Log-in Help',
  409:                                         col2 => 'Value'}],
  410:                            };
  411:     }
  412: 
  413:     my @roles = ('student','coordinator','author','admin');
  414:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  415:     &Apache::lonhtmlcommon::add_breadcrumb
  416:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  417:       text=>"Settings to display/modify"});
  418:     my $confname = $dom.'-domainconfig';
  419: 
  420:     if ($phase eq 'process') {
  421:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  422:                                                               \%prefs,\%domconfig,$confname,\@roles);
  423:         if (ref($result) eq 'HASH') {
  424:             $r->rflush();
  425:             &devalidate_remote_domconfs($dom,$result);
  426:         }
  427:     } elsif ($phase eq 'display') {
  428:         my $js = &recaptcha_js().
  429:                  &credits_js();
  430:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  431:             my ($othertitle,$usertypes,$types) =
  432:                 &Apache::loncommon::sorted_inst_types($dom);
  433:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  434:                                           $domconfig{'loadbalancing'}).
  435:                    &new_spares_js().
  436:                    &common_domprefs_js().
  437:                    &Apache::loncommon::javascript_array_indexof();
  438:         }
  439:         if (grep(/^requestcourses$/,@actions)) {
  440:             my $javascript_validations;
  441:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  442:             $js .= <<END;
  443: <script type="text/javascript">
  444: $javascript_validations
  445: </script>
  446: $coursebrowserjs
  447: END
  448:         }
  449:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  450:     } else {
  451: # check if domconfig user exists for the domain.
  452:         my $servadm = $r->dir_config('lonAdmEMail');
  453:         my ($configuserok,$author_ok,$switchserver) =
  454:             &config_check($dom,$confname,$servadm);
  455:         unless ($configuserok eq 'ok') {
  456:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  457:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  458:                           $confname).
  459:                       '<br />'
  460:             );
  461:             if ($switchserver) {
  462:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  463:                           '<br />'.
  464:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  465:                           '<br />'.
  466:                           &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).
  467:                           '<br />'.
  468:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  469:                 );
  470:             } else {
  471:                 $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.').
  472:                           '<br />'.
  473:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  474:                 );
  475:             }
  476:             $r->print(&Apache::loncommon::end_page());
  477:             return OK;
  478:         }
  479:         if (keys(%domconfig) == 0) {
  480:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  481:             my @ids=&Apache::lonnet::current_machine_ids();
  482:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  483:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  484:                 my @loginimages = ('img','logo','domlogo','login');
  485:                 my $custom_img_count = 0;
  486:                 foreach my $img (@loginimages) {
  487:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  488:                         $custom_img_count ++;
  489:                     }
  490:                 }
  491:                 foreach my $role (@roles) {
  492:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  493:                         $custom_img_count ++;
  494:                     }
  495:                 }
  496:                 if ($custom_img_count > 0) {
  497:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  498:                     my $switch_server = &check_switchserver($dom,$confname);
  499:                     $r->print(
  500:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  501:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  502:     &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 />'.
  503:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  504:                     if ($switch_server) {
  505:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  506:                     }
  507:                     $r->print(&Apache::loncommon::end_page());
  508:                     return OK;
  509:                 }
  510:             }
  511:         }
  512:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  513:     }
  514:     return OK;
  515: }
  516: 
  517: sub process_changes {
  518:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  519:     my %domconfig;
  520:     if (ref($values) eq 'HASH') {
  521:         %domconfig = %{$values};
  522:     }
  523:     my $output;
  524:     if ($action eq 'login') {
  525:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  526:     } elsif ($action eq 'rolecolors') {
  527:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  528:                                      $lastactref,%domconfig);
  529:     } elsif ($action eq 'quotas') {
  530:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  531:     } elsif ($action eq 'autoenroll') {
  532:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  533:     } elsif ($action eq 'autoupdate') {
  534:         $output = &modify_autoupdate($dom,%domconfig);
  535:     } elsif ($action eq 'autocreate') {
  536:         $output = &modify_autocreate($dom,%domconfig);
  537:     } elsif ($action eq 'directorysrch') {
  538:         $output = &modify_directorysrch($dom,%domconfig);
  539:     } elsif ($action eq 'usercreation') {
  540:         $output = &modify_usercreation($dom,%domconfig);
  541:     } elsif ($action eq 'usermodification') {
  542:         $output = &modify_usermodification($dom,%domconfig);
  543:     } elsif ($action eq 'contacts') {
  544:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  545:     } elsif ($action eq 'defaults') {
  546:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  547:     } elsif ($action eq 'scantron') {
  548:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  549:     } elsif ($action eq 'coursecategories') {
  550:         $output = &modify_coursecategories($dom,%domconfig);
  551:     } elsif ($action eq 'serverstatuses') {
  552:         $output = &modify_serverstatuses($dom,%domconfig);
  553:     } elsif ($action eq 'requestcourses') {
  554:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  555:     } elsif ($action eq 'requestauthor') {
  556:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  557:     } elsif ($action eq 'helpsettings') {
  558:         $output = &modify_helpsettings($r,$dom,$confname,%domconfig);
  559:     } elsif ($action eq 'coursedefaults') {
  560:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  561:     } elsif ($action eq 'usersessions') {
  562:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  563:     } elsif ($action eq 'loadbalancing') {
  564:         $output = &modify_loadbalancing($dom,%domconfig);
  565:     }
  566:     return $output;
  567: }
  568: 
  569: sub print_config_box {
  570:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  571:     my $rowtotal = 0;
  572:     my $output;
  573:     if ($action eq 'coursecategories') {
  574:         $output = &coursecategories_javascript($settings);
  575:     }
  576:     $output .= 
  577:          '<table class="LC_nested_outer">
  578:           <tr>
  579:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  580:            &mt($item->{text}).'&nbsp;'.
  581:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  582:           '</tr>';
  583:     $rowtotal ++;
  584:     my $numheaders = 1;
  585:     if (ref($item->{'header'}) eq 'ARRAY') {
  586:         $numheaders = scalar(@{$item->{'header'}});
  587:     }
  588:     if ($numheaders > 1) {
  589:         my $colspan = '';
  590:         my $rightcolspan = '';
  591:         if (($action eq 'rolecolors') || ($action eq 'coursecategories') ||
  592:             (($action eq 'login') && ($numheaders < 3))) {
  593:             $colspan = ' colspan="2"';
  594:         }
  595:         if ($action eq 'usersessions') {
  596:             $rightcolspan = ' colspan="3"'; 
  597:         }
  598:         $output .= '
  599:           <tr>
  600:            <td>
  601:             <table class="LC_nested">
  602:              <tr class="LC_info_row">
  603:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  604:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  605:              </tr>';
  606:         $rowtotal ++;
  607:         if ($action eq 'autoupdate') {
  608:             $output .= &print_autoupdate('top',$dom,$settings,\$rowtotal);
  609:         } elsif ($action eq 'usercreation') {
  610:             $output .= &print_usercreation('top',$dom,$settings,\$rowtotal);
  611:         } elsif ($action eq 'usermodification') {
  612:             $output .= &print_usermodification('top',$dom,$settings,\$rowtotal);
  613:         } elsif ($action eq 'coursecategories') {
  614:             $output .= &print_coursecategories('top',$dom,$item,$settings,\$rowtotal);
  615:         } elsif ($action eq 'login') {
  616:             if ($numheaders == 3) {
  617:                 $colspan = ' colspan="2"';
  618:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  619:             } else {
  620:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  621:             }
  622:         } elsif ($action eq 'requestcourses') {
  623:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  624:         } elsif ($action eq 'requestauthor') {
  625:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  626:         } elsif ($action eq 'usersessions') {
  627:             $output .= &print_usersessions('top',$dom,$settings,\$rowtotal); 
  628:         } elsif ($action eq 'rolecolors') {
  629:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  630:         } elsif ($action eq 'coursedefaults') {
  631:             $output .= &print_coursedefaults('top',$dom,$settings,\$rowtotal);
  632:         }
  633:         $output .= '
  634:            </table>
  635:           </td>
  636:          </tr>
  637:          <tr>
  638:            <td>
  639:             <table class="LC_nested">
  640:              <tr class="LC_info_row">
  641:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>';
  642:         $output .= '
  643:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  644:              </tr>';
  645:             $rowtotal ++;
  646:         if ($action eq 'autoupdate') {
  647:             $output .= &print_autoupdate('middle',$dom,$settings,\$rowtotal).'
  648:            </table>
  649:           </td>
  650:          </tr>
  651:          <tr>
  652:            <td>
  653:             <table class="LC_nested">
  654:              <tr class="LC_info_row">
  655:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  656:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  657:             &print_autoupdate('bottom',$dom,$settings,\$rowtotal);
  658:             $rowtotal ++;
  659:         } elsif ($action eq 'usercreation') {
  660:             $output .= &print_usercreation('middle',$dom,$settings,\$rowtotal).'
  661:            </table>
  662:           </td>
  663:          </tr>
  664:          <tr>
  665:            <td>
  666:             <table class="LC_nested">
  667:              <tr class="LC_info_row">
  668:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  669:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>             </tr>'.
  670:             &print_usercreation('bottom',$dom,$settings,\$rowtotal);
  671:             $rowtotal ++;
  672:         } elsif ($action eq 'usermodification') {
  673:             $output .= &print_usermodification('middle',$dom,$settings,\$rowtotal).'
  674:            </table>
  675:           </td>
  676:          </tr>
  677:          <tr>
  678:            <td>
  679:             <table class="LC_nested">
  680:              <tr class="LC_info_row">
  681:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  682:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  683:                        &print_usermodification('bottom',$dom,$settings,\$rowtotal);
  684:             $rowtotal ++;
  685:         } elsif ($action eq 'coursecategories') {
  686:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  687:         } elsif ($action eq 'login') {
  688:             if ($numheaders == 3) {
  689:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  690:            </table>
  691:           </td>
  692:          </tr>
  693:          <tr>
  694:            <td>
  695:             <table class="LC_nested">
  696:              <tr class="LC_info_row">
  697:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  698:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  699:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  700:                 $rowtotal ++;
  701:             } else {
  702:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  703:             }
  704:         } elsif ($action eq 'requestcourses') {
  705:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal).
  706:                        &print_studentcode($settings,\$rowtotal).'
  707:            </table>
  708:           </td>
  709:          </tr>
  710:          <tr>
  711:            <td>
  712:             <table class="LC_nested">
  713:              <tr class="LC_info_row">
  714:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  715:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  716:                        &print_textbookcourses($dom,$settings,\$rowtotal);
  717:         } elsif ($action eq 'requestauthor') {
  718:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  719:         } elsif ($action eq 'usersessions') {
  720:             $output .= &print_usersessions('middle',$dom,$settings,\$rowtotal).'
  721:            </table>
  722:           </td>
  723:          </tr>
  724:          <tr>
  725:            <td>
  726:             <table class="LC_nested">
  727:              <tr class="LC_info_row">
  728:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  729:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  730:                        &print_usersessions('bottom',$dom,$settings,\$rowtotal);
  731:             $rowtotal ++;
  732:         } elsif ($action eq 'coursedefaults') {
  733:             $output .= &print_coursedefaults('bottom',$dom,$settings,\$rowtotal);
  734:         } elsif ($action eq 'rolecolors') {
  735:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  736:            </table>
  737:           </td>
  738:          </tr>
  739:          <tr>
  740:            <td>
  741:             <table class="LC_nested">
  742:              <tr class="LC_info_row">
  743:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  744:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  745:               <td class="LC_right_item" valign="top">'.
  746:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  747:              </tr>'.
  748:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  749:            </table>
  750:           </td>
  751:          </tr>
  752:          <tr>
  753:            <td>
  754:             <table class="LC_nested">
  755:              <tr class="LC_info_row">
  756:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  757:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  758:              </tr>'.
  759:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  760:             $rowtotal += 2;
  761:         }
  762:     } else {
  763:         $output .= '
  764:           <tr>
  765:            <td>
  766:             <table class="LC_nested">
  767:              <tr class="LC_info_row">';
  768:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  769:             $output .= '  
  770:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  771:         } elsif ($action eq 'serverstatuses') {
  772:             $output .= '
  773:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  774:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  775: 
  776:         } else {
  777:             $output .= '
  778:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  779:         }
  780:         if (defined($item->{'header'}->[0]->{'col3'})) {
  781:             $output .= '<td class="LC_left_item" valign="top">'.
  782:                        &mt($item->{'header'}->[0]->{'col2'});
  783:             if ($action eq 'serverstatuses') {
  784:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  785:             } 
  786:         } else {
  787:             $output .= '<td class="LC_right_item" valign="top">'.
  788:                        &mt($item->{'header'}->[0]->{'col2'});
  789:         }
  790:         $output .= '</td>';
  791:         if ($item->{'header'}->[0]->{'col3'}) {
  792:             if (defined($item->{'header'}->[0]->{'col4'})) {
  793:                 $output .= '<td class="LC_left_item" valign="top">'.
  794:                             &mt($item->{'header'}->[0]->{'col3'});
  795:             } else {
  796:                 $output .= '<td class="LC_right_item" valign="top">'.
  797:                            &mt($item->{'header'}->[0]->{'col3'});
  798:             }
  799:             if ($action eq 'serverstatuses') {
  800:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  801:             }
  802:             $output .= '</td>';
  803:         }
  804:         if ($item->{'header'}->[0]->{'col4'}) {
  805:             $output .= '<td class="LC_right_item" valign="top">'.
  806:                        &mt($item->{'header'}->[0]->{'col4'});
  807:         }
  808:         $output .= '</tr>';
  809:         $rowtotal ++;
  810:         if ($action eq 'quotas') {
  811:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  812:         } elsif ($action eq 'autoenroll') {
  813:             $output .= &print_autoenroll($dom,$settings,\$rowtotal);
  814:         } elsif ($action eq 'autocreate') {
  815:             $output .= &print_autocreate($dom,$settings,\$rowtotal);
  816:         } elsif ($action eq 'directorysrch') {
  817:             $output .= &print_directorysrch($dom,$settings,\$rowtotal);
  818:         } elsif ($action eq 'contacts') {
  819:             $output .= &print_contacts($dom,$settings,\$rowtotal);
  820:         } elsif ($action eq 'defaults') {
  821:             $output .= &print_defaults($dom,$settings,\$rowtotal);
  822:         } elsif ($action eq 'scantron') {
  823:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  824:         } elsif ($action eq 'serverstatuses') {
  825:             $output .= &print_serverstatuses($dom,$settings,\$rowtotal);
  826:         } elsif ($action eq 'helpsettings') {
  827:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
  828:         } elsif ($action eq 'loadbalancing') {
  829:             $output .= &print_loadbalancing($dom,$settings,\$rowtotal);
  830:         }
  831:     }
  832:     $output .= '
  833:    </table>
  834:   </td>
  835:  </tr>
  836: </table><br />';
  837:     return ($output,$rowtotal);
  838: }
  839: 
  840: sub print_login {
  841:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  842:     my ($css_class,$datatable);
  843:     my %choices = &login_choices();
  844: 
  845:     if ($caller eq 'service') {
  846:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
  847:         my $choice = $choices{'disallowlogin'};
  848:         $css_class = ' class="LC_odd_row"';
  849:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  850:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  851:                       '<th>'.$choices{'server'}.'</th>'.
  852:                       '<th>'.$choices{'serverpath'}.'</th>'.
  853:                       '<th>'.$choices{'custompath'}.'</th>'.
  854:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  855:         my %disallowed;
  856:         if (ref($settings) eq 'HASH') {
  857:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  858:                %disallowed = %{$settings->{'loginvia'}};
  859:             }
  860:         }
  861:         foreach my $lonhost (sort(keys(%servers))) {
  862:             my $direct = 'selected="selected"';
  863:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  864:                 if ($disallowed{$lonhost}{'server'} ne '') {
  865:                     $direct = '';
  866:                 }
  867:             }
  868:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  869:                           '<td><select name="'.$lonhost.'_server">'.
  870:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  871:                           '</option>';
  872:             foreach my $hostid (sort(keys(%servers))) {
  873:                 next if ($servers{$hostid} eq $servers{$lonhost});
  874:                 my $selected = '';
  875:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  876:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  877:                         $selected = 'selected="selected"';
  878:                     }
  879:                 }
  880:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  881:                               $servers{$hostid}.'</option>';
  882:             }
  883:             $datatable .= '</select></td>'.
  884:                           '<td><select name="'.$lonhost.'_serverpath">';
  885:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  886:                 my $pathname = $path;
  887:                 if ($path eq 'custom') {
  888:                     $pathname = &mt('Custom Path').' ->';
  889:                 }
  890:                 my $selected = '';
  891:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  892:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  893:                         $selected = 'selected="selected"';
  894:                     }
  895:                 } elsif ($path eq '') {
  896:                     $selected = 'selected="selected"';
  897:                 }
  898:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  899:             }
  900:             $datatable .= '</select></td>';
  901:             my ($custom,$exempt);
  902:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  903:                 $custom = $disallowed{$lonhost}{'custompath'};
  904:                 $exempt = $disallowed{$lonhost}{'exempt'};
  905:             }
  906:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  907:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  908:                           '</tr>';
  909:         }
  910:         $datatable .= '</table></td></tr>';
  911:         return $datatable;
  912:     } elsif ($caller eq 'page') {
  913:         my %defaultchecked = ( 
  914:                                'coursecatalog' => 'on',
  915:                                'helpdesk'      => 'on',
  916:                                'adminmail'     => 'off',
  917:                                'newuser'       => 'off',
  918:                              );
  919:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
  920:         my (%checkedon,%checkedoff);
  921:         foreach my $item (@toggles) {
  922:             if ($defaultchecked{$item} eq 'on') { 
  923:                 $checkedon{$item} = ' checked="checked" ';
  924:                 $checkedoff{$item} = ' ';
  925:             } elsif ($defaultchecked{$item} eq 'off') {
  926:                 $checkedoff{$item} = ' checked="checked" ';
  927:                 $checkedon{$item} = ' ';
  928:             }
  929:         }
  930:         my @images = ('img','logo','domlogo','login');
  931:         my @logintext = ('textcol','bgcol');
  932:         my @bgs = ('pgbg','mainbg','sidebg');
  933:         my @links = ('link','alink','vlink');
  934:         my %designhash = &Apache::loncommon::get_domainconf($dom);
  935:         my %defaultdesign = %Apache::loncommon::defaultdesign;
  936:         my (%is_custom,%designs);
  937:         my %defaults = (
  938:                        font => $defaultdesign{'login.font'},
  939:                        );
  940:         foreach my $item (@images) {
  941:             $defaults{$item} = $defaultdesign{'login.'.$item};
  942:             $defaults{'showlogo'}{$item} = 1;
  943:         }
  944:         foreach my $item (@bgs) {
  945:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  946:         }
  947:         foreach my $item (@logintext) {
  948:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  949:         }
  950:         foreach my $item (@links) {
  951:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  952:         }
  953:         if (ref($settings) eq 'HASH') {
  954:             foreach my $item (@toggles) {
  955:                 if ($settings->{$item} eq '1') {
  956:                     $checkedon{$item} =  ' checked="checked" ';
  957:                     $checkedoff{$item} = ' ';
  958:                 } elsif ($settings->{$item} eq '0') {
  959:                     $checkedoff{$item} =  ' checked="checked" ';
  960:                     $checkedon{$item} = ' ';
  961:                 }
  962:             }
  963:             foreach my $item (@images) {
  964:                 if (defined($settings->{$item})) {
  965:                     $designs{$item} = $settings->{$item};
  966:                     $is_custom{$item} = 1;
  967:                 }
  968:                 if (defined($settings->{'showlogo'}{$item})) {
  969:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  970:                 }
  971:             }
  972:             foreach my $item (@logintext) {
  973:                 if ($settings->{$item} ne '') {
  974:                     $designs{'logintext'}{$item} = $settings->{$item};
  975:                     $is_custom{$item} = 1;
  976:                 }
  977:             }
  978:             if ($settings->{'font'} ne '') {
  979:                 $designs{'font'} = $settings->{'font'};
  980:                 $is_custom{'font'} = 1;
  981:             }
  982:             foreach my $item (@bgs) {
  983:                 if ($settings->{$item} ne '') {
  984:                     $designs{'bgs'}{$item} = $settings->{$item};
  985:                     $is_custom{$item} = 1;
  986:                 }
  987:             }
  988:             foreach my $item (@links) {
  989:                 if ($settings->{$item} ne '') {
  990:                     $designs{'links'}{$item} = $settings->{$item};
  991:                     $is_custom{$item} = 1;
  992:                 }
  993:             }
  994:         } else {
  995:             if ($designhash{$dom.'.login.font'} ne '') {
  996:                 $designs{'font'} = $designhash{$dom.'.login.font'};
  997:                 $is_custom{'font'} = 1;
  998:             }
  999:             foreach my $item (@images) {
 1000:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1001:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1002:                     $is_custom{$item} = 1;
 1003:                 }
 1004:             }
 1005:             foreach my $item (@bgs) {
 1006:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1007:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1008:                     $is_custom{$item} = 1;
 1009:                 }
 1010:             }
 1011:             foreach my $item (@links) {
 1012:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1013:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1014:                     $is_custom{$item} = 1;
 1015:                 }
 1016:             }
 1017:         }
 1018:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1019:                                                       logo => 'Institution Logo',
 1020:                                                       domlogo => 'Domain Logo',
 1021:                                                       login => 'Login box');
 1022:         my $itemcount = 1;
 1023:         foreach my $item (@toggles) {
 1024:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1025:             $datatable .=  
 1026:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1027:                 '</td><td>'.
 1028:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1029:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1030:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1031:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1032:                 '</tr>';
 1033:             $itemcount ++;
 1034:         }
 1035:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1036:         $datatable .= '</tr></table></td></tr>';
 1037:     } elsif ($caller eq 'help') {
 1038:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1039:         my $switchserver = &check_switchserver($dom,$confname);
 1040:         my $itemcount = 1;
 1041:         $defaulturl = '/adm/loginproblems.html';
 1042:         $defaulttype = 'default';
 1043:         %lt = &Apache::lonlocal::texthash (
 1044:                      del     => 'Delete?',
 1045:                      rep     => 'Replace:',
 1046:                      upl     => 'Upload:',
 1047:                      default => 'Default',
 1048:                      custom  => 'Custom',
 1049:                                              );
 1050:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1051:         my @currlangs;
 1052:         if (ref($settings) eq 'HASH') {
 1053:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1054:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1055:                     next if ($settings->{'helpurl'}{$key} eq '');
 1056:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1057:                     $type{$key} = 'custom';
 1058:                     unless ($key eq 'nolang') {
 1059:                         push(@currlangs,$key);
 1060:                     }
 1061:                 }
 1062:             } elsif ($settings->{'helpurl'} ne '') {
 1063:                 $type{'nolang'} = 'custom';
 1064:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1065:             }
 1066:         }
 1067:         foreach my $lang ('nolang',sort(@currlangs)) {
 1068:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1069:             $datatable .= '<tr'.$css_class.'>';
 1070:             if ($url{$lang} eq '') {
 1071:                 $url{$lang} = $defaulturl;
 1072:             }
 1073:             if ($type{$lang} eq '') {
 1074:                 $type{$lang} = $defaulttype;
 1075:             }
 1076:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1077:             if ($lang eq 'nolang') {
 1078:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1079:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1080:             } else {
 1081:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1082:                                   $langchoices{$lang},
 1083:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1084:             }
 1085:             $datatable .= '</span></td>'."\n".
 1086:                           '<td class="LC_left_item">';
 1087:             if ($type{$lang} eq 'custom') {
 1088:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1089:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1090:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1091:             } else {
 1092:                 $datatable .= $lt{'upl'};
 1093:             }
 1094:             $datatable .='<br />';
 1095:             if ($switchserver) {
 1096:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1097:             } else {
 1098:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1099:             }
 1100:             $datatable .= '</td></tr>';
 1101:             $itemcount ++;
 1102:         }
 1103:         my @addlangs;
 1104:         foreach my $lang (sort(keys(%langchoices))) {
 1105:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1106:             push(@addlangs,$lang);
 1107:         }
 1108:         if (@addlangs > 0) {
 1109:             my %toadd;
 1110:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1111:             $toadd{''} = &mt('Select');
 1112:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1113:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1114:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1115:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1116:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1117:             if ($switchserver) {
 1118:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1119:             } else {
 1120:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1121:             }
 1122:             $datatable .= '</td></tr>';
 1123:             $itemcount ++;
 1124:         }
 1125:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1126:     }
 1127:     return $datatable;
 1128: }
 1129: 
 1130: sub login_choices {
 1131:     my %choices =
 1132:         &Apache::lonlocal::texthash (
 1133:             coursecatalog => 'Display Course/Community Catalog link?',
 1134:             adminmail     => "Display Administrator's E-mail Address?",
 1135:             helpdesk      => 'Display "Contact Helpdesk" link',
 1136:             disallowlogin => "Login page requests redirected",
 1137:             hostid        => "Server",
 1138:             server        => "Redirect to:",
 1139:             serverpath    => "Path",
 1140:             custompath    => "Custom", 
 1141:             exempt        => "Exempt IP(s)",
 1142:             directlogin   => "No redirect",
 1143:             newuser       => "Link to create a user account",
 1144:             img           => "Header",
 1145:             logo          => "Main Logo",
 1146:             domlogo       => "Domain Logo",
 1147:             login         => "Log-in Header", 
 1148:             textcol       => "Text color",
 1149:             bgcol         => "Box color",
 1150:             bgs           => "Background colors",
 1151:             links         => "Link colors",
 1152:             font          => "Font color",
 1153:             pgbg          => "Header",
 1154:             mainbg        => "Page",
 1155:             sidebg        => "Login box",
 1156:             link          => "Link",
 1157:             alink         => "Active link",
 1158:             vlink         => "Visited link",
 1159:         );
 1160:     return %choices;
 1161: }
 1162: 
 1163: sub print_rolecolors {
 1164:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1165:     my %choices = &color_font_choices();
 1166:     my @bgs = ('pgbg','tabbg','sidebg');
 1167:     my @links = ('link','alink','vlink');
 1168:     my @images = ('img');
 1169:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1170:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1171:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1172:     my (%is_custom,%designs);
 1173:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1174:     if (ref($settings) eq 'HASH') {
 1175:         if (ref($settings->{$role}) eq 'HASH') {
 1176:             if ($settings->{$role}->{'img'} ne '') {
 1177:                 $designs{'img'} = $settings->{$role}->{'img'};
 1178:                 $is_custom{'img'} = 1;
 1179:             }
 1180:             if ($settings->{$role}->{'font'} ne '') {
 1181:                 $designs{'font'} = $settings->{$role}->{'font'};
 1182:                 $is_custom{'font'} = 1;
 1183:             }
 1184:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1185:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1186:                 $is_custom{'fontmenu'} = 1;
 1187:             }
 1188:             foreach my $item (@bgs) {
 1189:                 if ($settings->{$role}->{$item} ne '') {
 1190:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1191:                     $is_custom{$item} = 1;
 1192:                 }
 1193:             }
 1194:             foreach my $item (@links) {
 1195:                 if ($settings->{$role}->{$item} ne '') {
 1196:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1197:                     $is_custom{$item} = 1;
 1198:                 }
 1199:             }
 1200:         }
 1201:     } else {
 1202:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1203:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1204:             $is_custom{'img'} = 1;
 1205:         }
 1206:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1207:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1208:             $is_custom{'fontmenu'} = 1; 
 1209:         }
 1210:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1211:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1212:             $is_custom{'font'} = 1;
 1213:         }
 1214:         foreach my $item (@bgs) {
 1215:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1216:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1217:                 $is_custom{$item} = 1;
 1218:             
 1219:             }
 1220:         }
 1221:         foreach my $item (@links) {
 1222:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1223:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1224:                 $is_custom{$item} = 1;
 1225:             }
 1226:         }
 1227:     }
 1228:     my $itemcount = 1;
 1229:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1230:     $datatable .= '</tr></table></td></tr>';
 1231:     return $datatable;
 1232: }
 1233: 
 1234: sub role_defaults {
 1235:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1236:     my %defaults;
 1237:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1238:         return %defaults;
 1239:     }
 1240:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1241:     if ($role eq 'login') {
 1242:         %defaults = (
 1243:                        font => $defaultdesign{$role.'.font'},
 1244:                     );
 1245:         if (ref($logintext) eq 'ARRAY') {
 1246:             foreach my $item (@{$logintext}) {
 1247:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1248:             }
 1249:         }
 1250:         foreach my $item (@{$images}) {
 1251:             $defaults{'showlogo'}{$item} = 1;
 1252:         }
 1253:     } else {
 1254:         %defaults = (
 1255:                        img => $defaultdesign{$role.'.img'},
 1256:                        font => $defaultdesign{$role.'.font'},
 1257:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1258:                     );
 1259:     }
 1260:     foreach my $item (@{$bgs}) {
 1261:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1262:     }
 1263:     foreach my $item (@{$links}) {
 1264:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1265:     }
 1266:     foreach my $item (@{$images}) {
 1267:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1268:     }
 1269:     return %defaults;
 1270: }
 1271: 
 1272: sub display_color_options {
 1273:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1274:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1275:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1276:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1277:     my $datatable = '<tr'.$css_class.'>'.
 1278:         '<td>'.$choices->{'font'}.'</td>';
 1279:     if (!$is_custom->{'font'}) {
 1280:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1281:     } else {
 1282:         $datatable .= '<td>&nbsp;</td>';
 1283:     }
 1284:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1285: 
 1286:     $datatable .= '<td><span class="LC_nobreak">'.
 1287:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1288:                   ' value="'.$current_color.'" />&nbsp;'.
 1289:                   '&nbsp;</td></tr>';
 1290:     unless ($role eq 'login') { 
 1291:         $datatable .= '<tr'.$css_class.'>'.
 1292:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1293:         if (!$is_custom->{'fontmenu'}) {
 1294:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1295:         } else {
 1296:             $datatable .= '<td>&nbsp;</td>';
 1297:         }
 1298: 	$current_color = $designs->{'fontmenu'} ?
 1299: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1300:         $datatable .= '<td><span class="LC_nobreak">'.
 1301:                       '<input class="colorchooser" type="text" size="10" name="'
 1302: 		      .$role.'_fontmenu"'.
 1303:                       ' value="'.$current_color.'" />&nbsp;'.
 1304:                       '&nbsp;</td></tr>';
 1305:     }
 1306:     my $switchserver = &check_switchserver($dom,$confname);
 1307:     foreach my $img (@{$images}) {
 1308: 	$itemcount ++;
 1309:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1310:         $datatable .= '<tr'.$css_class.'>'.
 1311:                       '<td>'.$choices->{$img};
 1312:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1313:         if ($role eq 'login') {
 1314:             if ($img eq 'login') {
 1315:                 $login_hdr_pick =
 1316:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1317:                 $logincolors =
 1318:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1319:                                        $designs,$defaults);
 1320:             } elsif ($img ne 'domlogo') {
 1321:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1322:             }
 1323:         }
 1324:         $datatable .= '</td>';
 1325:         if ($designs->{$img} ne '') {
 1326:             $imgfile = $designs->{$img};
 1327: 	    $img_import = ($imgfile =~ m{^/adm/});
 1328:         } else {
 1329:             $imgfile = $defaults->{$img};
 1330:         }
 1331:         if ($imgfile) {
 1332:             my ($showfile,$fullsize);
 1333:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1334:                 my $urldir = $1;
 1335:                 my $filename = $2;
 1336:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1337:                 if (@info) {
 1338:                     my $thumbfile = 'tn-'.$filename;
 1339:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1340:                     if (@thumb) {
 1341:                         $showfile = $urldir.'/'.$thumbfile;
 1342:                     } else {
 1343:                         $showfile = $imgfile;
 1344:                     }
 1345:                 } else {
 1346:                     $showfile = '';
 1347:                 }
 1348:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1349:                 $showfile = $imgfile;
 1350:                 my $imgdir = $1;
 1351:                 my $filename = $2;
 1352:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1353:                     $showfile = "/$imgdir/tn-".$filename;
 1354:                 } else {
 1355:                     my $input = $londocroot.$imgfile;
 1356:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1357:                     if (!-e $output) {
 1358:                         my ($width,$height) = &thumb_dimensions();
 1359:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1360:                         if ($fullwidth ne '' && $fullheight ne '') {
 1361:                             if ($fullwidth > $width && $fullheight > $height) { 
 1362:                                 my $size = $width.'x'.$height;
 1363:                                 system("convert -sample $size $input $output");
 1364:                                 $showfile = "/$imgdir/tn-".$filename;
 1365:                             }
 1366:                         }
 1367:                     }
 1368:                 }
 1369:             }
 1370:             if ($showfile) {
 1371:                 if ($showfile =~ m{^/(adm|res)/}) {
 1372:                     if ($showfile =~ m{^/res/}) {
 1373:                         my $local_showfile =
 1374:                             &Apache::lonnet::filelocation('',$showfile);
 1375:                         &Apache::lonnet::repcopy($local_showfile);
 1376:                     }
 1377:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1378:                 }
 1379:                 if ($imgfile) {
 1380:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1381:                         if ($imgfile =~ m{^/res/}) {
 1382:                             my $local_imgfile =
 1383:                                 &Apache::lonnet::filelocation('',$imgfile);
 1384:                             &Apache::lonnet::repcopy($local_imgfile);
 1385:                         }
 1386:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1387:                     } else {
 1388:                         $fullsize = $imgfile;
 1389:                     }
 1390:                 }
 1391:                 $datatable .= '<td>';
 1392:                 if ($img eq 'login') {
 1393:                     $datatable .= $login_hdr_pick;
 1394:                 } 
 1395:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1396:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1397:             } else {
 1398:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1399:                               &mt('Upload:').'<br />';
 1400:             }
 1401:         } else {
 1402:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1403:                           &mt('Upload:').'<br />';
 1404:         }
 1405:         if ($switchserver) {
 1406:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1407:         } else {
 1408:             if ($img ne 'login') { # suppress file selection for Log-in header
 1409:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1410:             }
 1411:         }
 1412:         $datatable .= '</td></tr>';
 1413:     }
 1414:     $itemcount ++;
 1415:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1416:     $datatable .= '<tr'.$css_class.'>'.
 1417:                   '<td>'.$choices->{'bgs'}.'</td>';
 1418:     my $bgs_def;
 1419:     foreach my $item (@{$bgs}) {
 1420:         if (!$is_custom->{$item}) {
 1421:             $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>';
 1422:         }
 1423:     }
 1424:     if ($bgs_def) {
 1425:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1426:     } else {
 1427:         $datatable .= '<td>&nbsp;</td>';
 1428:     }
 1429:     $datatable .= '<td class="LC_right_item">'.
 1430:                   '<table border="0"><tr>';
 1431: 
 1432:     foreach my $item (@{$bgs}) {
 1433:         $datatable .= '<td align="center">'.$choices->{$item};
 1434: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1435:         if ($designs->{'bgs'}{$item}) {
 1436:             $datatable .= '&nbsp;';
 1437:         }
 1438:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1439:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1440:     }
 1441:     $datatable .= '</tr></table></td></tr>';
 1442:     $itemcount ++;
 1443:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1444:     $datatable .= '<tr'.$css_class.'>'.
 1445:                   '<td>'.$choices->{'links'}.'</td>';
 1446:     my $links_def;
 1447:     foreach my $item (@{$links}) {
 1448:         if (!$is_custom->{$item}) {
 1449:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1450:         }
 1451:     }
 1452:     if ($links_def) {
 1453:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1454:     } else {
 1455:         $datatable .= '<td>&nbsp;</td>';
 1456:     }
 1457:     $datatable .= '<td class="LC_right_item">'.
 1458:                   '<table border="0"><tr>';
 1459:     foreach my $item (@{$links}) {
 1460: 	my $color = $designs->{'link'}{$item} ? $designs->{'link'}{$item} : $defaults->{'links'}{$item};
 1461:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1462:         if ($designs->{'links'}{$item}) {
 1463:             $datatable.='&nbsp;';
 1464:         }
 1465:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1466:                       '" /></td>';
 1467:     }
 1468:     $$rowtotal += $itemcount;
 1469:     return $datatable;
 1470: }
 1471: 
 1472: sub logo_display_options {
 1473:     my ($img,$defaults,$designs) = @_;
 1474:     my $checkedon;
 1475:     if (ref($defaults) eq 'HASH') {
 1476:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1477:             if ($defaults->{'showlogo'}{$img}) {
 1478:                 $checkedon = 'checked="checked" ';     
 1479:             }
 1480:         } 
 1481:     }
 1482:     if (ref($designs) eq 'HASH') {
 1483:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1484:             if (defined($designs->{'showlogo'}{$img})) {
 1485:                 if ($designs->{'showlogo'}{$img} == 0) {
 1486:                     $checkedon = '';
 1487:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1488:                     $checkedon = 'checked="checked" ';
 1489:                 }
 1490:             }
 1491:         }
 1492:     }
 1493:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1494:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1495:            &mt('show').'</label>'."\n";
 1496: }
 1497: 
 1498: sub login_header_options  {
 1499:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1500:     my $output = '';
 1501:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1502:         $output .= &mt('Text default(s):').'<br />';
 1503:         if (!$is_custom->{'textcol'}) {
 1504:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1505:                        '&nbsp;&nbsp;&nbsp;';
 1506:         }
 1507:         if (!$is_custom->{'bgcol'}) {
 1508:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1509:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1510:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1511:         }
 1512:         $output .= '<br />';
 1513:     }
 1514:     $output .='<br />';
 1515:     return $output;
 1516: }
 1517: 
 1518: sub login_text_colors {
 1519:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1520:     my $color_menu = '<table border="0"><tr>';
 1521:     foreach my $item (@{$logintext}) {
 1522:         $color_menu .= '<td align="center">'.$choices->{$item};
 1523:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1524:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1525:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1526:     }
 1527:     $color_menu .= '</tr></table><br />';
 1528:     return $color_menu;
 1529: }
 1530: 
 1531: sub image_changes {
 1532:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1533:     my $output;
 1534:     if ($img eq 'login') {
 1535:             # suppress image for Log-in header
 1536:     } elsif (!$is_custom) {
 1537:         if ($img ne 'domlogo') {
 1538:             $output .= &mt('Default image:').'<br />';
 1539:         } else {
 1540:             $output .= &mt('Default in use:').'<br />';
 1541:         }
 1542:     }
 1543:     if ($img eq 'login') { # suppress image for Log-in header
 1544:         $output .= '<td>'.$logincolors;
 1545:     } else {
 1546:         if ($img_import) {
 1547:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1548:         }
 1549:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1550:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1551:         if ($is_custom) {
 1552:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1553:                        '<input type="checkbox" name="'.
 1554:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1555:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1556:         } else {
 1557:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1558:         }
 1559:     }
 1560:     return $output;
 1561: }
 1562: 
 1563: sub print_quotas {
 1564:     my ($dom,$settings,$rowtotal,$action) = @_;
 1565:     my $context;
 1566:     if ($action eq 'quotas') {
 1567:         $context = 'tools';
 1568:     } else {
 1569:         $context = $action;
 1570:     }
 1571:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1572:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1573:     my $typecount = 0;
 1574:     my ($css_class,%titles);
 1575:     if ($context eq 'requestcourses') {
 1576:         @usertools = ('official','unofficial','community','textbook');
 1577:         @options =('norequest','approval','validate','autolimit');
 1578:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1579:         %titles = &courserequest_titles();
 1580:     } elsif ($context eq 'requestauthor') {
 1581:         @usertools = ('author');
 1582:         @options = ('norequest','approval','automatic');
 1583:         %titles = &authorrequest_titles();
 1584:     } else {
 1585:         @usertools = ('aboutme','blog','webdav','portfolio');
 1586:         %titles = &tool_titles();
 1587:     }
 1588:     if (ref($types) eq 'ARRAY') {
 1589:         foreach my $type (@{$types}) {
 1590:             my ($currdefquota,$currauthorquota);
 1591:             unless (($context eq 'requestcourses') ||
 1592:                     ($context eq 'requestauthor')) {
 1593:                 if (ref($settings) eq 'HASH') {
 1594:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1595:                         $currdefquota = $settings->{defaultquota}->{$type};
 1596:                     } else {
 1597:                         $currdefquota = $settings->{$type};
 1598:                     }
 1599:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1600:                         $currauthorquota = $settings->{authorquota}->{$type};
 1601:                     }
 1602:                 }
 1603:             }
 1604:             if (defined($usertypes->{$type})) {
 1605:                 $typecount ++;
 1606:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1607:                 $datatable .= '<tr'.$css_class.'>'.
 1608:                               '<td>'.$usertypes->{$type}.'</td>'.
 1609:                               '<td class="LC_left_item">';
 1610:                 if ($context eq 'requestcourses') {
 1611:                     $datatable .= '<table><tr>';
 1612:                 }
 1613:                 my %cell;  
 1614:                 foreach my $item (@usertools) {
 1615:                     if ($context eq 'requestcourses') {
 1616:                         my ($curroption,$currlimit);
 1617:                         if (ref($settings) eq 'HASH') {
 1618:                             if (ref($settings->{$item}) eq 'HASH') {
 1619:                                 $curroption = $settings->{$item}->{$type};
 1620:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1621:                                     $currlimit = $1; 
 1622:                                 }
 1623:                             }
 1624:                         }
 1625:                         if (!$curroption) {
 1626:                             $curroption = 'norequest';
 1627:                         }
 1628:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1629:                         foreach my $option (@options) {
 1630:                             my $val = $option;
 1631:                             if ($option eq 'norequest') {
 1632:                                 $val = 0;  
 1633:                             }
 1634:                             if ($option eq 'validate') {
 1635:                                 my $canvalidate = 0;
 1636:                                 if (ref($validations{$item}) eq 'HASH') { 
 1637:                                     if ($validations{$item}{$type}) {
 1638:                                         $canvalidate = 1;
 1639:                                     }
 1640:                                 }
 1641:                                 next if (!$canvalidate);
 1642:                             }
 1643:                             my $checked = '';
 1644:                             if ($option eq $curroption) {
 1645:                                 $checked = ' checked="checked"';
 1646:                             } elsif ($option eq 'autolimit') {
 1647:                                 if ($curroption =~ /^autolimit/) {
 1648:                                     $checked = ' checked="checked"';
 1649:                                 }                       
 1650:                             } 
 1651:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1652:                                   '<input type="radio" name="crsreq_'.$item.
 1653:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1654:                                   $titles{$option}.'</label>';
 1655:                             if ($option eq 'autolimit') {
 1656:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1657:                                                 $item.'_limit_'.$type.'" size="1" '.
 1658:                                                 'value="'.$currlimit.'" />';
 1659:                             }
 1660:                             $cell{$item} .= '</span> ';
 1661:                             if ($option eq 'autolimit') {
 1662:                                 $cell{$item} .= $titles{'unlimited'};
 1663:                             }
 1664:                         }
 1665:                     } elsif ($context eq 'requestauthor') {
 1666:                         my $curroption;
 1667:                         if (ref($settings) eq 'HASH') {
 1668:                             $curroption = $settings->{$type};
 1669:                         }
 1670:                         if (!$curroption) {
 1671:                             $curroption = 'norequest';
 1672:                         }
 1673:                         foreach my $option (@options) {
 1674:                             my $val = $option;
 1675:                             if ($option eq 'norequest') {
 1676:                                 $val = 0;
 1677:                             }
 1678:                             my $checked = '';
 1679:                             if ($option eq $curroption) {
 1680:                                 $checked = ' checked="checked"';
 1681:                             }
 1682:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1683:                                   '<input type="radio" name="authorreq_'.$type.
 1684:                                   '" value="'.$val.'"'.$checked.' />'.
 1685:                                   $titles{$option}.'</label></span>&nbsp; ';
 1686:                         }
 1687:                     } else {
 1688:                         my $checked = 'checked="checked" ';
 1689:                         if (ref($settings) eq 'HASH') {
 1690:                             if (ref($settings->{$item}) eq 'HASH') {
 1691:                                 if ($settings->{$item}->{$type} == 0) {
 1692:                                     $checked = '';
 1693:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1694:                                     $checked =  'checked="checked" ';
 1695:                                 }
 1696:                             }
 1697:                         }
 1698:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1699:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1700:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1701:                                       '</label></span>&nbsp; ';
 1702:                     }
 1703:                 }
 1704:                 if ($context eq 'requestcourses') {
 1705:                     $datatable .= '</tr><tr>';
 1706:                     foreach my $item (@usertools) {
 1707:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 1708:                     }
 1709:                     $datatable .= '</tr></table>';
 1710:                 }
 1711:                 $datatable .= '</td>';
 1712:                 unless (($context eq 'requestcourses') ||
 1713:                         ($context eq 'requestauthor')) {
 1714:                     $datatable .= 
 1715:                               '<td class="LC_right_item">'.
 1716:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1717:                               '<input type="text" name="quota_'.$type.
 1718:                               '" value="'.$currdefquota.
 1719:                               '" size="5" /></span>'.('&nbsp;' x 2).
 1720:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1721:                               '<input type="text" name="authorquota_'.$type.
 1722:                               '" value="'.$currauthorquota.
 1723:                               '" size="5" /></span></td>';
 1724:                 }
 1725:                 $datatable .= '</tr>';
 1726:             }
 1727:         }
 1728:     }
 1729:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1730:         $defaultquota = '20';
 1731:         $authorquota = '500';
 1732:         if (ref($settings) eq 'HASH') {
 1733:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1734:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1735:             } elsif (defined($settings->{'default'})) {
 1736:                 $defaultquota = $settings->{'default'};
 1737:             }
 1738:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 1739:                 $authorquota = $settings->{'authorquota'}->{'default'};
 1740:             }
 1741:         }
 1742:     }
 1743:     $typecount ++;
 1744:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1745:     $datatable .= '<tr'.$css_class.'>'.
 1746:                   '<td>'.$othertitle.'</td>'.
 1747:                   '<td class="LC_left_item">';
 1748:     if ($context eq 'requestcourses') {
 1749:         $datatable .= '<table><tr>';
 1750:     }
 1751:     my %defcell;
 1752:     foreach my $item (@usertools) {
 1753:         if ($context eq 'requestcourses') {
 1754:             my ($curroption,$currlimit);
 1755:             if (ref($settings) eq 'HASH') {
 1756:                 if (ref($settings->{$item}) eq 'HASH') {
 1757:                     $curroption = $settings->{$item}->{'default'};
 1758:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1759:                         $currlimit = $1;
 1760:                     }
 1761:                 }
 1762:             }
 1763:             if (!$curroption) {
 1764:                 $curroption = 'norequest';
 1765:             }
 1766:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1767:             foreach my $option (@options) {
 1768:                 my $val = $option;
 1769:                 if ($option eq 'norequest') {
 1770:                     $val = 0;
 1771:                 }
 1772:                 if ($option eq 'validate') {
 1773:                     my $canvalidate = 0;
 1774:                     if (ref($validations{$item}) eq 'HASH') {
 1775:                         if ($validations{$item}{'default'}) {
 1776:                             $canvalidate = 1;
 1777:                         }
 1778:                     }
 1779:                     next if (!$canvalidate);
 1780:                 }
 1781:                 my $checked = '';
 1782:                 if ($option eq $curroption) {
 1783:                     $checked = ' checked="checked"';
 1784:                 } elsif ($option eq 'autolimit') {
 1785:                     if ($curroption =~ /^autolimit/) {
 1786:                         $checked = ' checked="checked"';
 1787:                     }
 1788:                 }
 1789:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1790:                                   '<input type="radio" name="crsreq_'.$item.
 1791:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1792:                                   $titles{$option}.'</label>';
 1793:                 if ($option eq 'autolimit') {
 1794:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1795:                                        $item.'_limit_default" size="1" '.
 1796:                                        'value="'.$currlimit.'" />';
 1797:                 }
 1798:                 $defcell{$item} .= '</span> ';
 1799:                 if ($option eq 'autolimit') {
 1800:                     $defcell{$item} .= $titles{'unlimited'};
 1801:                 }
 1802:             }
 1803:         } elsif ($context eq 'requestauthor') {
 1804:             my $curroption;
 1805:             if (ref($settings) eq 'HASH') {
 1806:                 $curroption = $settings->{'default'};
 1807:             }
 1808:             if (!$curroption) {
 1809:                 $curroption = 'norequest';
 1810:             }
 1811:             foreach my $option (@options) {
 1812:                 my $val = $option;
 1813:                 if ($option eq 'norequest') {
 1814:                     $val = 0;
 1815:                 }
 1816:                 my $checked = '';
 1817:                 if ($option eq $curroption) {
 1818:                     $checked = ' checked="checked"';
 1819:                 }
 1820:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1821:                               '<input type="radio" name="authorreq_default"'.
 1822:                               ' value="'.$val.'"'.$checked.' />'.
 1823:                               $titles{$option}.'</label></span>&nbsp; ';
 1824:             }
 1825:         } else {
 1826:             my $checked = 'checked="checked" ';
 1827:             if (ref($settings) eq 'HASH') {
 1828:                 if (ref($settings->{$item}) eq 'HASH') {
 1829:                     if ($settings->{$item}->{'default'} == 0) {
 1830:                         $checked = '';
 1831:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1832:                         $checked = 'checked="checked" ';
 1833:                     }
 1834:                 }
 1835:             }
 1836:             $datatable .= '<span class="LC_nobreak"><label>'.
 1837:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1838:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1839:                           '</label></span>&nbsp; ';
 1840:         }
 1841:     }
 1842:     if ($context eq 'requestcourses') {
 1843:         $datatable .= '</tr><tr>';
 1844:         foreach my $item (@usertools) {
 1845:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 1846:         }
 1847:         $datatable .= '</tr></table>';
 1848:     }
 1849:     $datatable .= '</td>';
 1850:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 1851:         $datatable .= '<td class="LC_right_item">'.
 1852:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 1853:                       '<input type="text" name="defaultquota" value="'.
 1854:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 1855:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 1856:                       '<input type="text" name="authorquota" value="'.
 1857:                       $authorquota.'" size="5" /></span></td>';
 1858:     }
 1859:     $datatable .= '</tr>';
 1860:     $typecount ++;
 1861:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1862:     $datatable .= '<tr'.$css_class.'>'.
 1863:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 1864:     if ($context eq 'requestcourses') {
 1865:         $datatable .= &mt('(overrides affiliation, if set)').
 1866:                       '</td>'.
 1867:                       '<td class="LC_left_item">'.
 1868:                       '<table><tr>';
 1869:     } else {
 1870:         $datatable .= &mt('(overrides affiliation, if checked)').
 1871:                       '</td>'.
 1872:                       '<td class="LC_left_item" colspan="2">'.
 1873:                       '<br />';
 1874:     }
 1875:     my %advcell;
 1876:     foreach my $item (@usertools) {
 1877:         if ($context eq 'requestcourses') {
 1878:             my ($curroption,$currlimit);
 1879:             if (ref($settings) eq 'HASH') {
 1880:                 if (ref($settings->{$item}) eq 'HASH') {
 1881:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1882:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1883:                         $currlimit = $1;
 1884:                     }
 1885:                 }
 1886:             }
 1887:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1888:             my $checked = '';
 1889:             if ($curroption eq '') {
 1890:                 $checked = ' checked="checked"';
 1891:             }
 1892:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1893:                                '<input type="radio" name="crsreq_'.$item.
 1894:                                '__LC_adv" value=""'.$checked.' />'.
 1895:                                &mt('No override set').'</label></span>&nbsp; ';
 1896:             foreach my $option (@options) {
 1897:                 my $val = $option;
 1898:                 if ($option eq 'norequest') {
 1899:                     $val = 0;
 1900:                 }
 1901:                 if ($option eq 'validate') {
 1902:                     my $canvalidate = 0;
 1903:                     if (ref($validations{$item}) eq 'HASH') {
 1904:                         if ($validations{$item}{'_LC_adv'}) {
 1905:                             $canvalidate = 1;
 1906:                         }
 1907:                     }
 1908:                     next if (!$canvalidate);
 1909:                 }
 1910:                 my $checked = '';
 1911:                 if ($val eq $curroption) {
 1912:                     $checked = ' checked="checked"';
 1913:                 } elsif ($option eq 'autolimit') {
 1914:                     if ($curroption =~ /^autolimit/) {
 1915:                         $checked = ' checked="checked"';
 1916:                     }
 1917:                 }
 1918:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1919:                                   '<input type="radio" name="crsreq_'.$item.
 1920:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1921:                                   $titles{$option}.'</label>';
 1922:                 if ($option eq 'autolimit') {
 1923:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1924:                                        $item.'_limit__LC_adv" size="1" '.
 1925:                                        'value="'.$currlimit.'" />';
 1926:                 }
 1927:                 $advcell{$item} .= '</span> ';
 1928:                 if ($option eq 'autolimit') {
 1929:                     $advcell{$item} .= $titles{'unlimited'};
 1930:                 }
 1931:             }
 1932:         } elsif ($context eq 'requestauthor') {
 1933:             my $curroption;
 1934:             if (ref($settings) eq 'HASH') {
 1935:                 $curroption = $settings->{'_LC_adv'};
 1936:             }
 1937:             my $checked = '';
 1938:             if ($curroption eq '') {
 1939:                 $checked = ' checked="checked"';
 1940:             }
 1941:             $datatable .= '<span class="LC_nobreak"><label>'.
 1942:                           '<input type="radio" name="authorreq__LC_adv"'.
 1943:                           ' value=""'.$checked.' />'.
 1944:                           &mt('No override set').'</label></span>&nbsp; ';
 1945:             foreach my $option (@options) {
 1946:                 my $val = $option;
 1947:                 if ($option eq 'norequest') {
 1948:                     $val = 0;
 1949:                 }
 1950:                 my $checked = '';
 1951:                 if ($val eq $curroption) {
 1952:                     $checked = ' checked="checked"';
 1953:                 }
 1954:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1955:                               '<input type="radio" name="authorreq__LC_adv"'.
 1956:                               ' value="'.$val.'"'.$checked.' />'.
 1957:                               $titles{$option}.'</label></span>&nbsp; ';
 1958:             }
 1959:         } else {
 1960:             my $checked = 'checked="checked" ';
 1961:             if (ref($settings) eq 'HASH') {
 1962:                 if (ref($settings->{$item}) eq 'HASH') {
 1963:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1964:                         $checked = '';
 1965:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1966:                         $checked = 'checked="checked" ';
 1967:                     }
 1968:                 }
 1969:             }
 1970:             $datatable .= '<span class="LC_nobreak"><label>'.
 1971:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1972:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1973:                           '</label></span>&nbsp; ';
 1974:         }
 1975:     }
 1976:     if ($context eq 'requestcourses') {
 1977:         $datatable .= '</tr><tr>';
 1978:         foreach my $item (@usertools) {
 1979:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 1980:         }
 1981:         $datatable .= '</tr></table>';
 1982:     }
 1983:     $datatable .= '</td></tr>';
 1984:     $$rowtotal += $typecount;
 1985:     return $datatable;
 1986: }
 1987: 
 1988: sub print_requestmail {
 1989:     my ($dom,$action,$settings,$rowtotal) = @_;
 1990:     my ($now,$datatable,%currapp);
 1991:     $now = time;
 1992:     if (ref($settings) eq 'HASH') {
 1993:         if (ref($settings->{'notify'}) eq 'HASH') {
 1994:             if ($settings->{'notify'}{'approval'} ne '') {
 1995:                map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 1996:             }
 1997:         }
 1998:     }
 1999:     my $numinrow = 2;
 2000:     my $css_class = 'class="LC_odd_row"';
 2001:     my $text;
 2002:     if ($action eq 'requestcourses') {
 2003:         $text = &mt('Receive notification of course requests requiring approval');
 2004:     } else {
 2005:         $text = &mt('Receive notification of authoring space requests requiring approval')
 2006:     }
 2007:     $datatable = '<tr '.$css_class.'>'.
 2008:                  ' <td>'.$text.'</td>'.
 2009:                  ' <td class="LC_left_item">';
 2010:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2011:                                                  'reqapprovalnotify',%currapp);
 2012:     if ($numdc > 0) {
 2013:         $datatable .= $table;
 2014:     } else {
 2015:         $datatable .= &mt('There are no active Domain Coordinators');
 2016:     }
 2017:     $datatable .='</td></tr>';
 2018:     $$rowtotal += $rows;
 2019:     return $datatable;
 2020: }
 2021: 
 2022: sub print_studentcode {
 2023:     my ($settings,$rowtotal) = @_;
 2024:     my $rownum = 0; 
 2025:     my %choices;
 2026:     $choices{'uniquecode'} = &mt('Generate unique six character code as course identifier?');
 2027:     my @toggles = ('uniquecode');
 2028:     my %defaultchecked = ('uniquecode'  => 'off');
 2029:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2030:                                                \%choices,$rownum);
 2031:     $$rowtotal += $rownum;
 2032:     return $reports;
 2033: }
 2034: 
 2035: sub print_textbookcourses {
 2036:     my ($dom,$settings,$rowtotal) = @_;
 2037:     my $rownum = 0;
 2038:     my $css_class;
 2039:     my $itemcount = 1;
 2040:     my $maxnum = 0;
 2041:     my $bookshash;
 2042:     if (ref($settings) eq 'HASH') {
 2043:         $bookshash = $settings->{'textbooks'};
 2044:     }
 2045:     my %ordered;
 2046:     if (ref($bookshash) eq 'HASH') {
 2047:         foreach my $item (keys(%{$bookshash})) {
 2048:             if (ref($bookshash->{$item}) eq 'HASH') {
 2049:                 my $num = $bookshash->{$item}{'order'};
 2050:                 $ordered{$num} = $item;
 2051:             }
 2052:         }
 2053:     }
 2054:     my $confname = $dom.'-domainconfig';
 2055:     my $switchserver = &check_switchserver($dom,$confname);
 2056:     my $maxnum = scalar(keys(%ordered));
 2057:     my $datatable = &textbookcourses_javascript(\%ordered);
 2058:     if (keys(%ordered)) {
 2059:         my @items = sort { $a <=> $b } keys(%ordered);
 2060:         for (my $i=0; $i<@items; $i++) {
 2061:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2062:             my $key = $ordered{$items[$i]};
 2063:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2064:             my $coursetitle = $coursehash{'description'};
 2065:             my ($subject,$title,$author,$image,$imgsrc,$cdom,$cnum);
 2066:             if (ref($bookshash->{$key}) eq 'HASH') {
 2067:                 $subject = $bookshash->{$key}->{'subject'};
 2068:                 $title = $bookshash->{$key}->{'title'};
 2069:                 $author = $bookshash->{$key}->{'author'};
 2070:                 $image = $bookshash->{$key}->{'image'};
 2071:                 if ($image ne '') {
 2072:                     my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2073:                     my $imagethumb = "$path/tn-".$imagefile;
 2074:                     $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2075:                 }
 2076:             }
 2077:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$key'".');"';
 2078:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2079:                          .'<select name="'.$key.'"'.$chgstr.'>';
 2080:             for (my $k=0; $k<=$maxnum; $k++) {
 2081:                 my $vpos = $k+1;
 2082:                 my $selstr;
 2083:                 if ($k == $i) {
 2084:                     $selstr = ' selected="selected" ';
 2085:                 }
 2086:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2087:             }
 2088:             $datatable .= '</select>'.('&nbsp;'x2).
 2089:                 '<label><input type="checkbox" name="book_del" value="'.$key.'" />'.
 2090:                 &mt('Delete?').'</label></span></td>'.
 2091:                 '<td colspan="2">'.
 2092:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="book_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2093:                 ('&nbsp;'x2).
 2094:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="book_title_'.$i.'" value="'.$title.'" /></span> '.
 2095:                 ('&nbsp;'x2).
 2096:                 '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="book_author_'.$i.'" value="'.$author.'" /></span> '.
 2097:                 ('&nbsp;'x2).
 2098:                 '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2099:             if ($image) {
 2100:                 $datatable .= '<span class="LC_nobreak">'.
 2101:                               $imgsrc.
 2102:                               '<label><input type="checkbox" name="book_image_del"'.
 2103:                               ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2104:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2105:             }
 2106:             if ($switchserver) {
 2107:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2108:             } else {
 2109:                 $datatable .= '<input type="file" name="book_image_'.$i.'" value="" />';
 2110:             }
 2111:             $datatable .= '<input type="hidden" name="book_id_'.$i.'" value="'.$key.'" /></span> '.
 2112:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2113:                           $coursetitle.'</span></td></tr>'."\n";
 2114:             $itemcount ++;
 2115:         }
 2116:     }
 2117:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2118:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'addbook_pos'".');"';
 2119:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2120:                   '<input type="hidden" name="book_maxnum" value="'.$maxnum.'" />'."\n".
 2121:                   '<select name="addbook_pos"'.$chgstr.'>';
 2122:     for (my $k=0; $k<$maxnum+1; $k++) {
 2123:         my $vpos = $k+1;
 2124:         my $selstr;
 2125:         if ($k == $maxnum) {
 2126:             $selstr = ' selected="selected" ';
 2127:         }
 2128:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2129:     }
 2130:     $datatable .= '</select>&nbsp;'."\n".
 2131:                   '<input type="checkbox" name="addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2132:                   '<td colspan="2">'.
 2133:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="addbook_subject" value="" /></span> '."\n".
 2134:                   ('&nbsp;'x2).
 2135:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="addbook_title" value="" /></span> '."\n".
 2136:                   ('&nbsp;'x2).
 2137:                   '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="addbook_author" value="" /></span> '."\n".
 2138:                   ('&nbsp;'x2).
 2139:                   '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2140:     if ($switchserver) {
 2141:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2142:     } else {
 2143:         $datatable .= '<input type="file" name="addbook_image" value="" />';
 2144:     }
 2145:     $datatable .= '</span>'."\n".
 2146:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2147:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},'addbook_cdom').
 2148:                   '<input type="text" size="25" name="addbook_cnum" value="" />'.
 2149:                   &Apache::loncommon::selectcourse_link
 2150:                       ('display','addbook_cnum','addbook_cdom',undef,undef,undef,'Course');
 2151:                   '</span></td>'."\n".
 2152:                   '</tr>'."\n";
 2153:     $itemcount ++;
 2154:     return $datatable;
 2155: }
 2156: 
 2157: sub textbookcourses_javascript {
 2158:     my ($textbooks) = @_;
 2159:     return unless(ref($textbooks) eq 'HASH');
 2160:     my $num = scalar(keys(%{$textbooks}));
 2161:     my @jsarray;
 2162:     foreach my $item (sort {$a <=> $b } (keys(%{$textbooks}))) {
 2163:         push(@jsarray,$textbooks->{$item});
 2164:     }
 2165:     my $jstext = '    var textbooks = Array('."'".join("','",@jsarray)."'".');'."\n";
 2166:     return <<"ENDSCRIPT";
 2167: <script type="text/javascript">
 2168: // <![CDATA[
 2169: function reorderBooks(form,item) {
 2170:     var changedVal;
 2171: $jstext 
 2172:     var newpos = 'addbook_pos';
 2173:     var current = new Array;
 2174:     var maxh = 1 + $num;
 2175:     var current = new Array;
 2176:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2177:     if (item == newpos) {
 2178:         changedVal = newitemVal;
 2179:     } else {
 2180:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2181:         current[newitemVal] = newpos;
 2182:     }
 2183:     for (var i=0; i<textbooks.length; i++) {
 2184:         var elementName = textbooks[i];
 2185:         if (elementName != item) {
 2186:             if (form.elements[elementName]) {
 2187:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2188:                 current[currVal] = elementName;
 2189:             }
 2190:         }
 2191:     }
 2192:     var oldVal;
 2193:     for (var j=0; j<maxh; j++) {
 2194:         if (current[j] == undefined) {
 2195:             oldVal = j;
 2196:         }
 2197:     }
 2198:     if (oldVal < changedVal) {
 2199:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2200:            var elementName = current[k];
 2201:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2202:         }
 2203:     } else {
 2204:         for (var k=changedVal; k<oldVal; k++) {
 2205:             var elementName = current[k];
 2206:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2207:         }
 2208:     }
 2209:     return;
 2210: }
 2211: 
 2212: // ]]>
 2213: </script>
 2214: 
 2215: ENDSCRIPT
 2216: }
 2217: 
 2218: sub print_autoenroll {
 2219:     my ($dom,$settings,$rowtotal) = @_;
 2220:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2221:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 2222:     if (ref($settings) eq 'HASH') {
 2223:         if (exists($settings->{'run'})) {
 2224:             if ($settings->{'run'} eq '0') {
 2225:                 $runoff = ' checked="checked" ';
 2226:                 $runon = ' ';
 2227:             } else {
 2228:                 $runon = ' checked="checked" ';
 2229:                 $runoff = ' ';
 2230:             }
 2231:         } else {
 2232:             if ($autorun) {
 2233:                 $runon = ' checked="checked" ';
 2234:                 $runoff = ' ';
 2235:             } else {
 2236:                 $runoff = ' checked="checked" ';
 2237:                 $runon = ' ';
 2238:             }
 2239:         }
 2240:         if (exists($settings->{'co-owners'})) {
 2241:             if ($settings->{'co-owners'} eq '0') {
 2242:                 $coownersoff = ' checked="checked" ';
 2243:                 $coownerson = ' ';
 2244:             } else {
 2245:                 $coownerson = ' checked="checked" ';
 2246:                 $coownersoff = ' ';
 2247:             }
 2248:         } else {
 2249:             $coownersoff = ' checked="checked" ';
 2250:             $coownerson = ' ';
 2251:         }
 2252:         if (exists($settings->{'sender_domain'})) {
 2253:             $defdom = $settings->{'sender_domain'};
 2254:         }
 2255:     } else {
 2256:         if ($autorun) {
 2257:             $runon = ' checked="checked" ';
 2258:             $runoff = ' ';
 2259:         } else {
 2260:             $runoff = ' checked="checked" ';
 2261:             $runon = ' ';
 2262:         }
 2263:     }
 2264:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2265:     my $notif_sender;
 2266:     if (ref($settings) eq 'HASH') {
 2267:         $notif_sender = $settings->{'sender_uname'};
 2268:     }
 2269:     my $datatable='<tr class="LC_odd_row">'.
 2270:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2271:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2272:                   '<input type="radio" name="autoenroll_run"'.
 2273:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2274:                   '<label><input type="radio" name="autoenroll_run"'.
 2275:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2276:                   '</tr><tr>'.
 2277:                   '<td>'.&mt('Notification messages - sender').
 2278:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2279:                   &mt('username').':&nbsp;'.
 2280:                   '<input type="text" name="sender_uname" value="'.
 2281:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2282:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2283:                   '<tr class="LC_odd_row">'.
 2284:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2285:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2286:                   '<input type="radio" name="autoassign_coowners"'.
 2287:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2288:                   '<label><input type="radio" name="autoassign_coowners"'.
 2289:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2290:                   '</tr>';
 2291:     $$rowtotal += 3;
 2292:     return $datatable;
 2293: }
 2294: 
 2295: sub print_autoupdate {
 2296:     my ($position,$dom,$settings,$rowtotal) = @_;
 2297:     my $datatable;
 2298:     if ($position eq 'top') {
 2299:         my $updateon = ' ';
 2300:         my $updateoff = ' checked="checked" ';
 2301:         my $classlistson = ' ';
 2302:         my $classlistsoff = ' checked="checked" ';
 2303:         if (ref($settings) eq 'HASH') {
 2304:             if ($settings->{'run'} eq '1') {
 2305:                 $updateon = $updateoff;
 2306:                 $updateoff = ' ';
 2307:             }
 2308:             if ($settings->{'classlists'} eq '1') {
 2309:                 $classlistson = $classlistsoff;
 2310:                 $classlistsoff = ' ';
 2311:             }
 2312:         }
 2313:         my %title = (
 2314:                    run => 'Auto-update active?',
 2315:                    classlists => 'Update information in classlists?',
 2316:                     );
 2317:         $datatable = '<tr class="LC_odd_row">'. 
 2318:                   '<td>'.&mt($title{'run'}).'</td>'.
 2319:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2320:                   '<input type="radio" name="autoupdate_run"'.
 2321:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2322:                   '<label><input type="radio" name="autoupdate_run"'.
 2323:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2324:                   '</tr><tr>'.
 2325:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2326:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2327:                   '<label><input type="radio" name="classlists"'.
 2328:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2329:                   '<label><input type="radio" name="classlists"'.
 2330:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2331:                   '</tr>';
 2332:         $$rowtotal += 2;
 2333:     } elsif ($position eq 'middle') {
 2334:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2335:         my $numinrow = 3;
 2336:         my $locknamesettings;
 2337:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2338:                                      $dom,$numinrow,$othertitle,
 2339:                                     'lockablenames');
 2340:         $$rowtotal ++;
 2341:     } else {
 2342:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2343:         my @fields = ('lastname','firstname','middlename','generation',
 2344:                       'permanentemail','id');
 2345:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2346:         my $numrows = 0;
 2347:         if (ref($types) eq 'ARRAY') {
 2348:             if (@{$types} > 0) {
 2349:                 $datatable = 
 2350:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2351:                                          \@fields,$types,\$numrows);
 2352:                     $$rowtotal += @{$types}; 
 2353:             }
 2354:         }
 2355:         $datatable .= 
 2356:             &usertype_update_row($settings,{'default' => $othertitle},
 2357:                                  \%fieldtitles,\@fields,['default'],
 2358:                                  \$numrows);
 2359:         $$rowtotal ++;     
 2360:     }
 2361:     return $datatable;
 2362: }
 2363: 
 2364: sub print_autocreate {
 2365:     my ($dom,$settings,$rowtotal) = @_;
 2366:     my (%createon,%createoff,%currhash);
 2367:     my @types = ('xml','req');
 2368:     if (ref($settings) eq 'HASH') {
 2369:         foreach my $item (@types) {
 2370:             $createoff{$item} = ' checked="checked" ';
 2371:             $createon{$item} = ' ';
 2372:             if (exists($settings->{$item})) {
 2373:                 if ($settings->{$item}) {
 2374:                     $createon{$item} = ' checked="checked" ';
 2375:                     $createoff{$item} = ' ';
 2376:                 }
 2377:             }
 2378:         }
 2379:         if ($settings->{'xmldc'} ne '') {
 2380:             $currhash{$settings->{'xmldc'}} = 1;
 2381:         }
 2382:     } else {
 2383:         foreach my $item (@types) {
 2384:             $createoff{$item} = ' checked="checked" ';
 2385:             $createon{$item} = ' ';
 2386:         }
 2387:     }
 2388:     $$rowtotal += 2;
 2389:     my $numinrow = 2;
 2390:     my $datatable='<tr class="LC_odd_row">'.
 2391:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2392:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2393:                   '<input type="radio" name="autocreate_xml"'.
 2394:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2395:                   '<label><input type="radio" name="autocreate_xml"'.
 2396:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2397:                   '</td></tr><tr>'.
 2398:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2399:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2400:                   '<input type="radio" name="autocreate_req"'.
 2401:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2402:                   '<label><input type="radio" name="autocreate_req"'.
 2403:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2404:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2405:                                                    'autocreate_xmldc',%currhash);
 2406:     if ($numdc > 1) {
 2407:         $datatable .= '</td></tr><tr class="LC_odd_row"><td>'.
 2408:                       &mt('Course creation processed as: (choose Dom. Coord.)').
 2409:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 2410:     } else {
 2411:         $datatable .= $dctable.'</td></tr>';
 2412:     }
 2413:     $$rowtotal += $rows;
 2414:     return $datatable;
 2415: }
 2416: 
 2417: sub print_directorysrch {
 2418:     my ($dom,$settings,$rowtotal) = @_;
 2419:     my $srchon = ' ';
 2420:     my $srchoff = ' checked="checked" ';
 2421:     my ($exacton,$containson,$beginson);
 2422:     my $localon = ' ';
 2423:     my $localoff = ' checked="checked" ';
 2424:     if (ref($settings) eq 'HASH') {
 2425:         if ($settings->{'available'} eq '1') {
 2426:             $srchon = $srchoff;
 2427:             $srchoff = ' ';
 2428:         }
 2429:         if ($settings->{'localonly'} eq '1') {
 2430:             $localon = $localoff;
 2431:             $localoff = ' ';
 2432:         }
 2433:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2434:             foreach my $type (@{$settings->{'searchtypes'}}) {
 2435:                 if ($type eq 'exact') {
 2436:                     $exacton = ' checked="checked" ';
 2437:                 } elsif ($type eq 'contains') {
 2438:                     $containson = ' checked="checked" ';
 2439:                 } elsif ($type eq 'begins') {
 2440:                     $beginson = ' checked="checked" ';
 2441:                 }
 2442:             }
 2443:         } else {
 2444:             if ($settings->{'searchtypes'} eq 'exact') {
 2445:                 $exacton = ' checked="checked" ';
 2446:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 2447:                 $containson = ' checked="checked" ';
 2448:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 2449:                 $exacton = ' checked="checked" ';
 2450:                 $containson = ' checked="checked" ';
 2451:             }
 2452:         }
 2453:     }
 2454:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2455:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2456: 
 2457:     my $numinrow = 4;
 2458:     my $cansrchrow = 0;
 2459:     my $datatable='<tr class="LC_odd_row">'.
 2460:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 2461:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2462:                   '<input type="radio" name="dirsrch_available"'.
 2463:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2464:                   '<label><input type="radio" name="dirsrch_available"'.
 2465:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2466:                   '</tr><tr>'.
 2467:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 2468:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2469:                   '<input type="radio" name="dirsrch_localonly"'.
 2470:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2471:                   '<label><input type="radio" name="dirsrch_localonly"'.
 2472:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 2473:                   '</tr>';
 2474:     $$rowtotal += 2;
 2475:     if (ref($usertypes) eq 'HASH') {
 2476:         if (keys(%{$usertypes}) > 0) {
 2477:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2478:                                          $numinrow,$othertitle,'cansearch');
 2479:             $cansrchrow = 1;
 2480:         }
 2481:     }
 2482:     if ($cansrchrow) {
 2483:         $$rowtotal ++;
 2484:         $datatable .= '<tr>';
 2485:     } else {
 2486:         $datatable .= '<tr class="LC_odd_row">';
 2487:     }
 2488:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2489:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2490:     foreach my $title (@{$titleorder}) {
 2491:         if (defined($searchtitles->{$title})) {
 2492:             my $check = ' ';
 2493:             if (ref($settings) eq 'HASH') {
 2494:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2495:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2496:                         $check = ' checked="checked" ';
 2497:                     }
 2498:                 }
 2499:             }
 2500:             $datatable .= '<td class="LC_left_item">'.
 2501:                           '<span class="LC_nobreak"><label>'.
 2502:                           '<input type="checkbox" name="searchby" '.
 2503:                           'value="'.$title.'"'.$check.'/>'.
 2504:                           $searchtitles->{$title}.'</label></span></td>';
 2505:         }
 2506:     }
 2507:     $datatable .= '</tr></table></td></tr>';
 2508:     $$rowtotal ++;
 2509:     if ($cansrchrow) {
 2510:         $datatable .= '<tr class="LC_odd_row">';
 2511:     } else {
 2512:         $datatable .= '<tr>';
 2513:     }
 2514:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2515:                   '<td class="LC_left_item" colspan="2">'.
 2516:                   '<span class="LC_nobreak"><label>'.
 2517:                   '<input type="checkbox" name="searchtypes" '.
 2518:                   $exacton.' value="exact" />'.&mt('Exact match').
 2519:                   '</label>&nbsp;'.
 2520:                   '<label><input type="checkbox" name="searchtypes" '.
 2521:                   $beginson.' value="begins" />'.&mt('Begins with').
 2522:                   '</label>&nbsp;'.
 2523:                   '<label><input type="checkbox" name="searchtypes" '.
 2524:                   $containson.' value="contains" />'.&mt('Contains').
 2525:                   '</label></span></td></tr>';
 2526:     $$rowtotal ++;
 2527:     return $datatable;
 2528: }
 2529: 
 2530: sub print_contacts {
 2531:     my ($dom,$settings,$rowtotal) = @_;
 2532:     my $datatable;
 2533:     my @contacts = ('adminemail','supportemail');
 2534:     my (%checked,%to,%otheremails,%bccemails);
 2535:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 2536:                     'requestsmail','updatesmail','idconflictsmail');
 2537:     foreach my $type (@mailings) {
 2538:         $otheremails{$type} = '';
 2539:     }
 2540:     $bccemails{'helpdeskmail'} = '';
 2541:     if (ref($settings) eq 'HASH') {
 2542:         foreach my $item (@contacts) {
 2543:             if (exists($settings->{$item})) {
 2544:                 $to{$item} = $settings->{$item};
 2545:             }
 2546:         }
 2547:         foreach my $type (@mailings) {
 2548:             if (exists($settings->{$type})) {
 2549:                 if (ref($settings->{$type}) eq 'HASH') {
 2550:                     foreach my $item (@contacts) {
 2551:                         if ($settings->{$type}{$item}) {
 2552:                             $checked{$type}{$item} = ' checked="checked" ';
 2553:                         }
 2554:                     }
 2555:                     $otheremails{$type} = $settings->{$type}{'others'};
 2556:                     if ($type eq 'helpdeskmail') {
 2557:                         $bccemails{$type} = $settings->{$type}{'bcc'};
 2558:                     }
 2559:                 }
 2560:             } elsif ($type eq 'lonstatusmail') {
 2561:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 2562:             }
 2563:         }
 2564:     } else {
 2565:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 2566:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 2567:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 2568:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 2569:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 2570:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 2571:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 2572:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 2573:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 2574:     }
 2575:     my ($titles,$short_titles) = &contact_titles();
 2576:     my $rownum = 0;
 2577:     my $css_class;
 2578:     foreach my $item (@contacts) {
 2579:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2580:         $datatable .= '<tr'.$css_class.'>'. 
 2581:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2582:                   '</span></td><td class="LC_right_item">'.
 2583:                   '<input type="text" name="'.$item.'" value="'.
 2584:                   $to{$item}.'" /></td></tr>';
 2585:         $rownum ++;
 2586:     }
 2587:     foreach my $type (@mailings) {
 2588:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2589:         $datatable .= '<tr'.$css_class.'>'.
 2590:                       '<td><span class="LC_nobreak">'.
 2591:                       $titles->{$type}.': </span></td>'.
 2592:                       '<td class="LC_left_item">'.
 2593:                       '<span class="LC_nobreak">';
 2594:         foreach my $item (@contacts) {
 2595:             $datatable .= '<label>'.
 2596:                           '<input type="checkbox" name="'.$type.'"'.
 2597:                           $checked{$type}{$item}.
 2598:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2599:                           '</label>&nbsp;';
 2600:         }
 2601:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2602:                       '<input type="text" name="'.$type.'_others" '.
 2603:                       'value="'.$otheremails{$type}.'"  />';
 2604:         if ($type eq 'helpdeskmail') {
 2605:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 2606:                           '<input type="text" name="'.$type.'_bcc" '.
 2607:                           'value="'.$bccemails{$type}.'"  />';
 2608:         }
 2609:         $datatable .= '</td></tr>'."\n";
 2610:         $rownum ++;
 2611:     }
 2612:     my %choices;
 2613:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 2614:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2615:                                    &mt('LON-CAPA core group - MSU'),600,500));
 2616:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 2617:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 2618:                                     &mt('LON-CAPA core group - MSU'),600,500));
 2619:     my @toggles = ('reporterrors','reportupdates');
 2620:     my %defaultchecked = ('reporterrors'  => 'on',
 2621:                           'reportupdates' => 'on');
 2622:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2623:                                                \%choices,$rownum);
 2624:     $datatable .= $reports;
 2625:     $$rowtotal += $rownum;
 2626:     return $datatable;
 2627: }
 2628: 
 2629: sub print_helpsettings {
 2630:     my ($dom,$confname,$settings,$rowtotal) = @_;
 2631:     my ($datatable,$itemcount);
 2632:     $itemcount = 1;
 2633:     my (%choices,%defaultchecked,@toggles);
 2634:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 2635:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 2636:                                  &mt('LON-CAPA bug tracker'),600,500));
 2637:     %defaultchecked = ('submitbugs' => 'on');
 2638:     @toggles = ('submitbugs',);
 2639: 
 2640:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2641:                                                  \%choices,$itemcount);
 2642:     return $datatable;
 2643: }
 2644: 
 2645: sub radiobutton_prefs {
 2646:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 2647:         $additional) = @_;
 2648:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2649:                    (ref($choices) eq 'HASH'));
 2650: 
 2651:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2652: 
 2653:     foreach my $item (@{$toggles}) {
 2654:         if ($defaultchecked->{$item} eq 'on') {
 2655:             $checkedon{$item} = ' checked="checked" ';
 2656:             $checkedoff{$item} = ' ';
 2657:         } elsif ($defaultchecked->{$item} eq 'off') {
 2658:             $checkedoff{$item} = ' checked="checked" ';
 2659:             $checkedon{$item} = ' ';
 2660:         }
 2661:     }
 2662:     if (ref($settings) eq 'HASH') {
 2663:         foreach my $item (@{$toggles}) {
 2664:             if ($settings->{$item} eq '1') {
 2665:                 $checkedon{$item} =  ' checked="checked" ';
 2666:                 $checkedoff{$item} = ' ';
 2667:             } elsif ($settings->{$item} eq '0') {
 2668:                 $checkedoff{$item} =  ' checked="checked" ';
 2669:                 $checkedon{$item} = ' ';
 2670:             }
 2671:         }
 2672:     }
 2673:     if ($onclick) {
 2674:         $onclick = ' onclick="'.$onclick.'"';
 2675:     }
 2676:     foreach my $item (@{$toggles}) {
 2677:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2678:         $datatable .=
 2679:             '<tr'.$css_class.'><td valign="top">'.
 2680:             '<span class="LC_nobreak">'.$choices->{$item}.
 2681:             '</span></td>'.
 2682:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2683:             '<label><input type="radio" name="'.
 2684:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 2685:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2686:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 2687:             '</span>'.$additional.
 2688:             '</td>'.
 2689:             '</tr>';
 2690:         $itemcount ++;
 2691:     }
 2692:     return ($datatable,$itemcount);
 2693: }
 2694: 
 2695: sub print_coursedefaults {
 2696:     my ($position,$dom,$settings,$rowtotal) = @_;
 2697:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 2698:     my $itemcount = 1;
 2699:     my %choices =  &Apache::lonlocal::texthash (
 2700:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 2701:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 2702:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 2703:         coursecredits        => 'Credits can be specified for courses',
 2704:     );
 2705:     my %staticdefaults = (
 2706:                            anonsurvey_threshold => 10,
 2707:                            uploadquota          => 500,
 2708:                          );
 2709:     if ($position eq 'top') {
 2710:         %defaultchecked = ('canuse_pdfforms' => 'off');
 2711:         @toggles = ('canuse_pdfforms');
 2712:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2713:                                                  \%choices,$itemcount);
 2714:     } else {
 2715:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2716:         my ($currdefresponder,$def_official_credits,$def_unofficial_credits,$def_textbook_credits,
 2717:             %curruploadquota);
 2718:         my $currusecredits = 0;
 2719:         my @types = ('official','unofficial','community','textbook');
 2720:         if (ref($settings) eq 'HASH') {
 2721:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 2722:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 2723:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 2724:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 2725:                 }
 2726:             }
 2727:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 2728:                 $def_official_credits = $settings->{'coursecredits'}->{'official'};
 2729:                 $def_unofficial_credits = $settings->{'coursecredits'}->{'unofficial'};
 2730:                 $def_textbook_credits = $settings->{'coursecredits'}->{'textbook'};
 2731:                 if (($def_official_credits ne '') || ($def_unofficial_credits ne '') ||
 2732:                     ($def_textbook_credits ne '')) {
 2733:                     $currusecredits = 1;
 2734:                 }
 2735:             }
 2736:         }
 2737:         if (!$currdefresponder) {
 2738:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 2739:         } elsif ($currdefresponder < 1) {
 2740:             $currdefresponder = 1;
 2741:         }
 2742:         foreach my $type (@types) {
 2743:             if ($curruploadquota{$type} eq '') {
 2744:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 2745:             }
 2746:         }
 2747:         $datatable .=
 2748:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 2749:                 $choices{'anonsurvey_threshold'}.
 2750:                 '</span></td>'.
 2751:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 2752:                 '<input type="text" name="anonsurvey_threshold"'.
 2753:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 2754:                 '</td></tr>'."\n".
 2755:                 '<tr><td><span class="LC_nobreak">'.
 2756:                 $choices{'uploadquota'}.
 2757:                 '</span></td>'.
 2758:                 '<td align="right" class="LC_right_item">'.
 2759:                 '<table><tr>';
 2760:         foreach my $type (@types) {
 2761:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 2762:                            '<input type="text" name="uploadquota_'.$type.'"'.
 2763:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 2764:         }
 2765:         $datatable .= '</tr></table></td></tr>'."\n";
 2766:         $itemcount += 2;
 2767:         my $onclick = 'toggleCredits(this.form);';
 2768:         my $display = 'none';
 2769:         if ($currusecredits) {
 2770:             $display = 'block';
 2771:         }
 2772:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 2773:                          '<span class="LC_nobreak">'.
 2774:                          &mt('Default credits for official courses [_1]',
 2775:                          '<input type="text" name="official_credits" value="'.
 2776:                          $def_official_credits.'" size="3" />').
 2777:                          '</span><br />'.
 2778:                          '<span class="LC_nobreak">'.
 2779:                          &mt('Default credits for unofficial courses [_1]',
 2780:                          '<input type="text" name="unofficial_credits" value="'.
 2781:                          $def_unofficial_credits.'" size="3" />').
 2782:                          '</span><br />'.
 2783:                          '<span class="LC_nobreak">'.
 2784:                          &mt('Default credits for textbook courses [_1]',
 2785:                          '<input type="text" name="textbook_credits" value="'.
 2786:                          $def_textbook_credits.'" size="3" />').
 2787:                          '</span></div>'."\n";
 2788:         %defaultchecked = ('coursecredits' => 'off');
 2789:         @toggles = ('coursecredits');
 2790:         my $current = {
 2791:                         'coursecredits' => $currusecredits,
 2792:                       };
 2793:         (my $table,$itemcount) =
 2794:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 2795:                                \%choices,$itemcount,$onclick,$additional);
 2796:         $datatable .= $table;
 2797:     }
 2798:     $$rowtotal += $itemcount;
 2799:     return $datatable;
 2800: }
 2801: 
 2802: sub print_usersessions {
 2803:     my ($position,$dom,$settings,$rowtotal) = @_;
 2804:     my ($css_class,$datatable,%checked,%choices);
 2805:     my (%by_ip,%by_location,@intdoms);
 2806:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 2807: 
 2808:     my @alldoms = &Apache::lonnet::all_domains();
 2809:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 2810:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 2811:     my %altids = &id_for_thisdom(%servers);
 2812:     my $itemcount = 1;
 2813:     if ($position eq 'top') {
 2814:         if (keys(%serverhomes) > 1) {
 2815:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 2816:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$rowtotal);
 2817:         } else {
 2818:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2819:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
 2820:         }
 2821:     } else {
 2822:         if (keys(%by_location) == 0) {
 2823:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 2824:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
 2825:         } else {
 2826:             my %lt = &usersession_titles();
 2827:             my $numinrow = 5;
 2828:             my $prefix;
 2829:             my @types;
 2830:             if ($position eq 'bottom') {
 2831:                 $prefix = 'remote';
 2832:                 @types = ('version','excludedomain','includedomain');
 2833:             } else {
 2834:                 $prefix = 'hosted';
 2835:                 @types = ('excludedomain','includedomain');
 2836:             }
 2837:             my (%current,%checkedon,%checkedoff);
 2838:             my @lcversions = &Apache::lonnet::all_loncaparevs();
 2839:             my @locations = sort(keys(%by_location));
 2840:             foreach my $type (@types) {
 2841:                 $checkedon{$type} = '';
 2842:                 $checkedoff{$type} = ' checked="checked"';
 2843:             }
 2844:             if (ref($settings) eq 'HASH') {
 2845:                 if (ref($settings->{$prefix}) eq 'HASH') {
 2846:                     foreach my $key (keys(%{$settings->{$prefix}})) {
 2847:                         $current{$key} = $settings->{$prefix}{$key};
 2848:                         if ($key eq 'version') {
 2849:                             if ($current{$key} ne '') {
 2850:                                 $checkedon{$key} = ' checked="checked"';
 2851:                                 $checkedoff{$key} = '';
 2852:                             }
 2853:                         } elsif (ref($current{$key}) eq 'ARRAY') {
 2854:                             $checkedon{$key} = ' checked="checked"';
 2855:                             $checkedoff{$key} = '';
 2856:                         }
 2857:                     }
 2858:                 }
 2859:             }
 2860:             foreach my $type (@types) {
 2861:                 next if ($type ne 'version' && !@locations);
 2862:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 2863:                 $datatable .= '<tr'.$css_class.'>
 2864:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
 2865:                                <span class="LC_nobreak">&nbsp;
 2866:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 2867:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 2868:                 if ($type eq 'version') {
 2869:                     my $selector = '<select name="'.$prefix.'_version">';
 2870:                     foreach my $version (@lcversions) {
 2871:                         my $selected = '';
 2872:                         if ($current{'version'} eq $version) {
 2873:                             $selected = ' selected="selected"';
 2874:                         }
 2875:                         $selector .= ' <option value="'.$version.'"'.
 2876:                                      $selected.'>'.$version.'</option>';
 2877:                     }
 2878:                     $selector .= '</select> ';
 2879:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 2880:                 } else {
 2881:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 2882:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 2883:                                  ' />'.('&nbsp;'x2).
 2884:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
 2885:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 2886:                                  "\n".
 2887:                                  '</div><div><table>';
 2888:                     my $rem;
 2889:                     for (my $i=0; $i<@locations; $i++) {
 2890:                         my ($showloc,$value,$checkedtype);
 2891:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
 2892:                             my $ip = $by_location{$locations[$i]}->[0];
 2893:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
 2894:                                  $value = join(':',@{$by_ip{$ip}});
 2895:                                 $showloc = join(', ',@{$by_ip{$ip}});
 2896:                                 if (ref($current{$type}) eq 'ARRAY') {
 2897:                                     foreach my $loc (@{$by_ip{$ip}}) {  
 2898:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 2899:                                             $checkedtype = ' checked="checked"';
 2900:                                             last;
 2901:                                         }
 2902:                                     }
 2903:                                 }
 2904:                             }
 2905:                         }
 2906:                         $rem = $i%($numinrow);
 2907:                         if ($rem == 0) {
 2908:                             if ($i > 0) {
 2909:                                 $datatable .= '</tr>';
 2910:                             }
 2911:                             $datatable .= '<tr>';
 2912:                         }
 2913:                         $datatable .= '<td class="LC_left_item">'.
 2914:                                       '<span class="LC_nobreak"><label>'.
 2915:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
 2916:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 2917:                                       '</label></span></td>';
 2918:                     }
 2919:                     $rem = @locations%($numinrow);
 2920:                     my $colsleft = $numinrow - $rem;
 2921:                     if ($colsleft > 1 ) {
 2922:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2923:                                       '&nbsp;</td>';
 2924:                     } elsif ($colsleft == 1) {
 2925:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 2926:                     }
 2927:                     $datatable .= '</tr></table>';
 2928:                 }
 2929:                 $datatable .= '</td></tr>';
 2930:                 $itemcount ++;
 2931:             }
 2932:         }
 2933:     }
 2934:     $$rowtotal += $itemcount;
 2935:     return $datatable;
 2936: }
 2937: 
 2938: sub build_location_hashes {
 2939:     my ($intdoms,$by_ip,$by_location) = @_;
 2940:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 2941:                   (ref($by_location) eq 'HASH')); 
 2942:     my %iphost = &Apache::lonnet::get_iphost();
 2943:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 2944:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 2945:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 2946:         foreach my $id (@{$iphost{$primary_ip}}) {
 2947:             my $intdom = &Apache::lonnet::internet_dom($id);
 2948:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 2949:                 push(@{$intdoms},$intdom);
 2950:             }
 2951:         }
 2952:     }
 2953:     foreach my $ip (keys(%iphost)) {
 2954:         if (ref($iphost{$ip}) eq 'ARRAY') {
 2955:             foreach my $id (@{$iphost{$ip}}) {
 2956:                 my $location = &Apache::lonnet::internet_dom($id);
 2957:                 if ($location) {
 2958:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
 2959:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2960:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 2961:                             push(@{$by_ip->{$ip}},$location);
 2962:                         }
 2963:                     } else {
 2964:                         $by_ip->{$ip} = [$location];
 2965:                     }
 2966:                 }
 2967:             }
 2968:         }
 2969:     }
 2970:     foreach my $ip (sort(keys(%{$by_ip}))) {
 2971:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 2972:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 2973:             my $first = $by_ip->{$ip}->[0];
 2974:             if (ref($by_location->{$first}) eq 'ARRAY') {
 2975:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 2976:                     push(@{$by_location->{$first}},$ip);
 2977:                 }
 2978:             } else {
 2979:                 $by_location->{$first} = [$ip];
 2980:             }
 2981:         }
 2982:     }
 2983:     return;
 2984: }
 2985: 
 2986: sub current_offloads_to {
 2987:     my ($dom,$settings,$servers) = @_;
 2988:     my (%spareid,%otherdomconfigs);
 2989:     if (ref($servers) eq 'HASH') {
 2990:         foreach my $lonhost (sort(keys(%{$servers}))) {
 2991:             my $gotspares;
 2992:             if (ref($settings) eq 'HASH') {
 2993:                 if (ref($settings->{'spares'}) eq 'HASH') {
 2994:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 2995:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 2996:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 2997:                         $gotspares = 1;
 2998:                     }
 2999:                 }
 3000:             }
 3001:             unless ($gotspares) {
 3002:                 my $gotspares;
 3003:                 my $serverhomeID =
 3004:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 3005:                 my $serverhomedom =
 3006:                     &Apache::lonnet::host_domain($serverhomeID);
 3007:                 if ($serverhomedom ne $dom) {
 3008:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 3009:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 3010:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 3011:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3012:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3013:                                 $gotspares = 1;
 3014:                             }
 3015:                         }
 3016:                     } else {
 3017:                         $otherdomconfigs{$serverhomedom} =
 3018:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 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:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 3023:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 3024:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 3025:                                         $gotspares = 1;
 3026:                                     }
 3027:                                 }
 3028:                             }
 3029:                         }
 3030:                     }
 3031:                 }
 3032:             }
 3033:             unless ($gotspares) {
 3034:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3035:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3036:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3037:                } else {
 3038:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 3039:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 3040:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 3041:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 3042:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 3043:                     } else {
 3044:                         my %what = (
 3045:                              spareid => 1,
 3046:                         );
 3047:                         my ($result,$returnhash) = 
 3048:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 3049:                         if ($result eq 'ok') { 
 3050:                             if (ref($returnhash) eq 'HASH') {
 3051:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 3052:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 3053:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 3054:                                 }
 3055:                             }
 3056:                         }
 3057:                     }
 3058:                 }
 3059:             }
 3060:         }
 3061:     }
 3062:     return %spareid;
 3063: }
 3064: 
 3065: sub spares_row {
 3066:     my ($dom,$servers,$spareid,$serverhomes,$altids,$rowtotal) = @_;
 3067:     my $css_class;
 3068:     my $numinrow = 4;
 3069:     my $itemcount = 1;
 3070:     my $datatable;
 3071:     my %typetitles = &sparestype_titles();
 3072:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3073:         foreach my $server (sort(keys(%{$servers}))) {
 3074:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 3075:             my ($othercontrol,$serverdom);
 3076:             if ($serverhome ne $server) {
 3077:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 3078:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3079:             } else {
 3080:                 $serverdom = &Apache::lonnet::host_domain($server);
 3081:                 if ($serverdom ne $dom) {
 3082:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 3083:                 }
 3084:             }
 3085:             next unless (ref($spareid->{$server}) eq 'HASH');
 3086:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 3087:             $datatable .= '<tr'.$css_class.'>
 3088:                            <td rowspan="2">
 3089:                             <span class="LC_nobreak">'.
 3090:                           &mt('[_1] when busy, offloads to:'
 3091:                               ,'<b>'.$server.'</b>').
 3092:                           "\n";
 3093:             my (%current,%canselect);
 3094:             my @choices = 
 3095:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 3096:             foreach my $type ('primary','default') {
 3097:                 if (ref($spareid->{$server}) eq 'HASH') {
 3098:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 3099:                         my @spares = @{$spareid->{$server}{$type}};
 3100:                         if (@spares > 0) {
 3101:                             if ($othercontrol) {
 3102:                                 $current{$type} = join(', ',@spares);
 3103:                             } else {
 3104:                                 $current{$type} .= '<table>';
 3105:                                 my $numspares = scalar(@spares);
 3106:                                 for (my $i=0;  $i<@spares; $i++) {
 3107:                                     my $rem = $i%($numinrow);
 3108:                                     if ($rem == 0) {
 3109:                                         if ($i > 0) {
 3110:                                             $current{$type} .= '</tr>';
 3111:                                         }
 3112:                                         $current{$type} .= '<tr>';
 3113:                                     }
 3114:                                     $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;'.
 3115:                                                        $spareid->{$server}{$type}[$i].
 3116:                                                        '</label></td>'."\n";
 3117:                                 }
 3118:                                 my $rem = @spares%($numinrow);
 3119:                                 my $colsleft = $numinrow - $rem;
 3120:                                 if ($colsleft > 1 ) {
 3121:                                     $current{$type} .= '<td colspan="'.$colsleft.
 3122:                                                        '" class="LC_left_item">'.
 3123:                                                        '&nbsp;</td>';
 3124:                                 } elsif ($colsleft == 1) {
 3125:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 3126:                                 }
 3127:                                 $current{$type} .= '</tr></table>';
 3128:                             }
 3129:                         }
 3130:                     }
 3131:                     if ($current{$type} eq '') {
 3132:                         $current{$type} = &mt('None specified');
 3133:                     }
 3134:                     if ($othercontrol) {
 3135:                         if ($type eq 'primary') {
 3136:                             $canselect{$type} = $othercontrol;
 3137:                         }
 3138:                     } else {
 3139:                         $canselect{$type} = 
 3140:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 3141:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 3142:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 3143:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 3144:                         if (@choices > 0) {
 3145:                             foreach my $lonhost (@choices) {
 3146:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 3147:                             }
 3148:                         }
 3149:                         $canselect{$type} .= '</select>'."\n";
 3150:                     }
 3151:                 } else {
 3152:                     $current{$type} = &mt('Could not be determined');
 3153:                     if ($type eq 'primary') {
 3154:                         $canselect{$type} =  $othercontrol;
 3155:                     }
 3156:                 }
 3157:                 if ($type eq 'default') {
 3158:                     $datatable .= '<tr'.$css_class.'>';
 3159:                 }
 3160:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 3161:                               '<td>'.$current{$type}.'</td>'."\n".
 3162:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 3163:             }
 3164:             $itemcount ++;
 3165:         }
 3166:     }
 3167:     $$rowtotal += $itemcount;
 3168:     return $datatable;
 3169: }
 3170: 
 3171: sub possible_newspares {
 3172:     my ($server,$currspares,$serverhomes,$altids) = @_;
 3173:     my $serverhostname = &Apache::lonnet::hostname($server);
 3174:     my %excluded;
 3175:     if ($serverhostname ne '') {
 3176:         %excluded = (
 3177:                        $serverhostname => 1,
 3178:                     );
 3179:     }
 3180:     if (ref($currspares) eq 'HASH') {
 3181:         foreach my $type (keys(%{$currspares})) {
 3182:             if (ref($currspares->{$type}) eq 'ARRAY') {
 3183:                 if (@{$currspares->{$type}} > 0) {
 3184:                     foreach my $curr (@{$currspares->{$type}}) {
 3185:                         my $hostname = &Apache::lonnet::hostname($curr);
 3186:                         $excluded{$hostname} = 1;
 3187:                     }
 3188:                 }
 3189:             }
 3190:         }
 3191:     }
 3192:     my @choices;
 3193:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 3194:         if (keys(%{$serverhomes}) > 1) {
 3195:             foreach my $name (sort(keys(%{$serverhomes}))) {
 3196:                 unless ($excluded{$name}) {
 3197:                     if (exists($altids->{$serverhomes->{$name}})) {
 3198:                         push(@choices,$altids->{$serverhomes->{$name}});
 3199:                     } else {
 3200:                         push(@choices,$serverhomes->{$name});
 3201:                     }
 3202:                 }
 3203:             }
 3204:         }
 3205:     }
 3206:     return sort(@choices);
 3207: }
 3208: 
 3209: sub print_loadbalancing {
 3210:     my ($dom,$settings,$rowtotal) = @_;
 3211:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 3212:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 3213:     my $numinrow = 1;
 3214:     my $datatable;
 3215:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 3216:     my (%currbalancer,%currtargets,%currrules,%existing);
 3217:     if (ref($settings) eq 'HASH') {
 3218:         %existing = %{$settings};
 3219:     }
 3220:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 3221:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 3222:                                   \%currtargets,\%currrules);
 3223:     } else {
 3224:         return;
 3225:     }
 3226:     my ($othertitle,$usertypes,$types) =
 3227:         &Apache::loncommon::sorted_inst_types($dom);
 3228:     my $rownum = 8;
 3229:     if (ref($types) eq 'ARRAY') {
 3230:         $rownum += scalar(@{$types});
 3231:     }
 3232:     my @css_class = ('LC_odd_row','LC_even_row');
 3233:     my $balnum = 0;
 3234:     my $islast;
 3235:     my (@toshow,$disabledtext);
 3236:     if (keys(%currbalancer) > 0) {
 3237:         @toshow = sort(keys(%currbalancer));
 3238:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 3239:             push(@toshow,'');
 3240:         }
 3241:     } else {
 3242:         @toshow = ('');
 3243:         $disabledtext = &mt('No existing load balancer');
 3244:     }
 3245:     foreach my $lonhost (@toshow) {
 3246:         if ($balnum == scalar(@toshow)-1) {
 3247:             $islast = 1;
 3248:         } else {
 3249:             $islast = 0;
 3250:         }
 3251:         my $cssidx = $balnum%2;
 3252:         my $targets_div_style = 'display: none';
 3253:         my $disabled_div_style = 'display: block';
 3254:         my $homedom_div_style = 'display: none';
 3255:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 3256:                       '<td rowspan="'.$rownum.'" valign="top">'.
 3257:                       '<p>';
 3258:         if ($lonhost eq '') {
 3259:             $datatable .= '<span class="LC_nobreak">';
 3260:             if (keys(%currbalancer) > 0) {
 3261:                 $datatable .= &mt('Add balancer:');
 3262:             } else {
 3263:                 $datatable .= &mt('Enable balancer:');
 3264:             }
 3265:             $datatable .= '&nbsp;'.
 3266:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 3267:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 3268:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 3269:                           '<option value="" selected="selected">'.&mt('None').
 3270:                           '</option>'."\n";
 3271:             foreach my $server (sort(keys(%servers))) {
 3272:                 next if ($currbalancer{$server});
 3273:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 3274:             }
 3275:             $datatable .=
 3276:                 '</select>'."\n".
 3277:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 3278:         } else {
 3279:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 3280:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 3281:                            &mt('Stop balancing').'</label>'.
 3282:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 3283:             $targets_div_style = 'display: block';
 3284:             $disabled_div_style = 'display: none';
 3285:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 3286:                 $homedom_div_style = 'display: block';
 3287:             }
 3288:         }
 3289:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 3290:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 3291:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 3292:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 3293:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 3294:         my @sparestypes = ('primary','default');
 3295:         my %typetitles = &sparestype_titles();
 3296:         foreach my $sparetype (@sparestypes) {
 3297:             my $targettable;
 3298:             for (my $i=0; $i<$numspares; $i++) {
 3299:                 my $checked;
 3300:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 3301:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 3302:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 3303:                             $checked = ' checked="checked"';
 3304:                         }
 3305:                     }
 3306:                 }
 3307:                 my ($chkboxval,$disabled);
 3308:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 3309:                     $chkboxval = $spares[$i];
 3310:                 }
 3311:                 if (exists($currbalancer{$spares[$i]})) {
 3312:                     $disabled = ' disabled="disabled"';
 3313:                 }
 3314:                 $targettable .=
 3315:                     '<td><label><input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 3316:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 3317:                     '</span></label></td>';
 3318:                 my $rem = $i%($numinrow);
 3319:                 if ($rem == 0) {
 3320:                     if (($i > 0) && ($i < $numspares-1)) {
 3321:                         $targettable .= '</tr>';
 3322:                     }
 3323:                     if ($i < $numspares-1) {
 3324:                         $targettable .= '<tr>';
 3325:                     }
 3326:                 }
 3327:             }
 3328:             if ($targettable ne '') {
 3329:                 my $rem = $numspares%($numinrow);
 3330:                 my $colsleft = $numinrow - $rem;
 3331:                 if ($colsleft > 1 ) {
 3332:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3333:                                     '&nbsp;</td>';
 3334:                 } elsif ($colsleft == 1) {
 3335:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 3336:                 }
 3337:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 3338:                                '<table><tr>'.$targettable.'</tr></table><br />';
 3339:             }
 3340:         }
 3341:         $datatable .= '</div></td></tr>'.
 3342:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 3343:                                            $othertitle,$usertypes,$types,\%servers,
 3344:                                            \%currbalancer,$lonhost,
 3345:                                            $targets_div_style,$homedom_div_style,
 3346:                                            $css_class[$cssidx],$balnum,$islast);
 3347:         $$rowtotal += $rownum;
 3348:         $balnum ++;
 3349:     }
 3350:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 3351:     return $datatable;
 3352: }
 3353: 
 3354: sub get_loadbalancers_config {
 3355:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 3356:     return unless ((ref($servers) eq 'HASH') &&
 3357:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 3358:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 3359:     if (keys(%{$existing}) > 0) {
 3360:         my $oldlonhost;
 3361:         foreach my $key (sort(keys(%{$existing}))) {
 3362:             if ($key eq 'lonhost') {
 3363:                 $oldlonhost = $existing->{'lonhost'};
 3364:                 $currbalancer->{$oldlonhost} = 1;
 3365:             } elsif ($key eq 'targets') {
 3366:                 if ($oldlonhost) {
 3367:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 3368:                 }
 3369:             } elsif ($key eq 'rules') {
 3370:                 if ($oldlonhost) {
 3371:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 3372:                 }
 3373:             } elsif (ref($existing->{$key}) eq 'HASH') {
 3374:                 $currbalancer->{$key} = 1;
 3375:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 3376:                 $currrules->{$key} = $existing->{$key}{'rules'};
 3377:             }
 3378:         }
 3379:     } else {
 3380:         my ($balancerref,$targetsref) =
 3381:                 &Apache::lonnet::get_lonbalancer_config($servers);
 3382:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 3383:             foreach my $server (sort(keys(%{$balancerref}))) {
 3384:                 $currbalancer->{$server} = 1;
 3385:                 $currtargets->{$server} = $targetsref->{$server};
 3386:             }
 3387:         }
 3388:     }
 3389:     return;
 3390: }
 3391: 
 3392: sub loadbalancing_rules {
 3393:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 3394:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 3395:         $css_class,$balnum,$islast) = @_;
 3396:     my $output;
 3397:     my $num = 0;
 3398:     my ($alltypes,$othertypes,$titles) =
 3399:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 3400:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 3401:         foreach my $type (@{$alltypes}) {
 3402:             $num ++;
 3403:             my $current;
 3404:             if (ref($currrules) eq 'HASH') {
 3405:                 $current = $currrules->{$type};
 3406:             }
 3407:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3408:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 3409:                     $current = '';
 3410:                 }
 3411:             }
 3412:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 3413:                                              $servers,$currbalancer,$lonhost,$dom,
 3414:                                              $targets_div_style,$homedom_div_style,
 3415:                                              $css_class,$balnum,$num,$islast);
 3416:         }
 3417:     }
 3418:     return $output;
 3419: }
 3420: 
 3421: sub loadbalancing_titles {
 3422:     my ($dom,$intdom,$usertypes,$types) = @_;
 3423:     my %othertypes = (
 3424:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 3425:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 3426:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 3427:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 3428:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 3429:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 3430:                      );
 3431:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 3432:     if (ref($types) eq 'ARRAY') {
 3433:         unshift(@alltypes,@{$types},'default');
 3434:     }
 3435:     my %titles;
 3436:     foreach my $type (@alltypes) {
 3437:         if ($type =~ /^_LC_/) {
 3438:             $titles{$type} = $othertypes{$type};
 3439:         } elsif ($type eq 'default') {
 3440:             $titles{$type} = &mt('All users from [_1]',$dom);
 3441:             if (ref($types) eq 'ARRAY') {
 3442:                 if (@{$types} > 0) {
 3443:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 3444:                 }
 3445:             }
 3446:         } elsif (ref($usertypes) eq 'HASH') {
 3447:             $titles{$type} = $usertypes->{$type};
 3448:         }
 3449:     }
 3450:     return (\@alltypes,\%othertypes,\%titles);
 3451: }
 3452: 
 3453: sub loadbalance_rule_row {
 3454:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 3455:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 3456:     my @rulenames;
 3457:     my %ruletitles = &offloadtype_text();
 3458:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 3459:         @rulenames = ('balancer','offloadedto');
 3460:     } else {
 3461:         @rulenames = ('default','homeserver');
 3462:         if ($type eq '_LC_external') {
 3463:             push(@rulenames,'externalbalancer');
 3464:         } else {
 3465:             push(@rulenames,'specific');
 3466:         }
 3467:         push(@rulenames,'none');
 3468:     }
 3469:     my $style = $targets_div_style;
 3470:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom') || ($type eq '_LC_ipchange')) {
 3471:         $style = $homedom_div_style;
 3472:     }
 3473:     my $space;
 3474:     if ($islast && $num == 1) {
 3475:         $space = '<div display="inline-block">&nbsp;</div>';
 3476:     }
 3477:     my $output =
 3478:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 3479:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 3480:         '<td valaign="top">'.$space.
 3481:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 3482:     for (my $i=0; $i<@rulenames; $i++) {
 3483:         my $rule = $rulenames[$i];
 3484:         my ($checked,$extra);
 3485:         if ($rulenames[$i] eq 'default') {
 3486:             $rule = '';
 3487:         }
 3488:         if ($rulenames[$i] eq 'specific') {
 3489:             if (ref($servers) eq 'HASH') {
 3490:                 my $default;
 3491:                 if (($current ne '') && (exists($servers->{$current}))) {
 3492:                     $checked = ' checked="checked"';
 3493:                 }
 3494:                 unless ($checked) {
 3495:                     $default = ' selected="selected"';
 3496:                 }
 3497:                 $extra =
 3498:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3499:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 3500:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 3501:                     '<option value=""'.$default.'></option>'."\n";
 3502:                 foreach my $server (sort(keys(%{$servers}))) {
 3503:                     if (ref($currbalancer) eq 'HASH') {
 3504:                         next if (exists($currbalancer->{$server}));
 3505:                     }
 3506:                     my $selected;
 3507:                     if ($server eq $current) {
 3508:                         $selected = ' selected="selected"';
 3509:                     }
 3510:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 3511:                 }
 3512:                 $extra .= '</select>';
 3513:             }
 3514:         } elsif ($rule eq $current) {
 3515:             $checked = ' checked="checked"';
 3516:         }
 3517:         $output .= '<span class="LC_nobreak"><label>'.
 3518:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 3519:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 3520:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 3521:                    ')"'.$checked.' />&nbsp;'.$ruletitles{$rulenames[$i]}.
 3522:                    '</label>'.$extra.'</span><br />'."\n";
 3523:     }
 3524:     $output .= '</div></td></tr>'."\n";
 3525:     return $output;
 3526: }
 3527: 
 3528: sub offloadtype_text {
 3529:     my %ruletitles = &Apache::lonlocal::texthash (
 3530:            'default'          => 'Offloads to default destinations',
 3531:            'homeserver'       => "Offloads to user's home server",
 3532:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 3533:            'specific'         => 'Offloads to specific server',
 3534:            'none'             => 'No offload',
 3535:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 3536:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 3537:     );
 3538:     return %ruletitles;
 3539: }
 3540: 
 3541: sub sparestype_titles {
 3542:     my %typestitles = &Apache::lonlocal::texthash (
 3543:                           'primary' => 'primary',
 3544:                           'default' => 'default',
 3545:                       );
 3546:     return %typestitles;
 3547: }
 3548: 
 3549: sub contact_titles {
 3550:     my %titles = &Apache::lonlocal::texthash (
 3551:                    'supportemail' => 'Support E-mail address',
 3552:                    'adminemail'   => 'Default Server Admin E-mail address',
 3553:                    'errormail'    => 'Error reports to be e-mailed to',
 3554:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 3555:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 3556:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 3557:                    'requestsmail' => 'E-mail from course requests requiring approval',
 3558:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 3559:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 3560:                  );
 3561:     my %short_titles = &Apache::lonlocal::texthash (
 3562:                            adminemail   => 'Admin E-mail address',
 3563:                            supportemail => 'Support E-mail',
 3564:                        );   
 3565:     return (\%titles,\%short_titles);
 3566: }
 3567: 
 3568: sub tool_titles {
 3569:     my %titles = &Apache::lonlocal::texthash (
 3570:                      aboutme    => 'Personal web page',
 3571:                      blog       => 'Blog',
 3572:                      webdav     => 'WebDAV',
 3573:                      portfolio  => 'Portfolio',
 3574:                      official   => 'Official courses (with institutional codes)',
 3575:                      unofficial => 'Unofficial courses',
 3576:                      community  => 'Communities',
 3577:                      textbook   => 'Textbook courses',
 3578:                  );
 3579:     return %titles;
 3580: }
 3581: 
 3582: sub courserequest_titles {
 3583:     my %titles = &Apache::lonlocal::texthash (
 3584:                                    official   => 'Official',
 3585:                                    unofficial => 'Unofficial',
 3586:                                    community  => 'Communities',
 3587:                                    textbook   => 'Textbook',
 3588:                                    norequest  => 'Not allowed',
 3589:                                    approval   => 'Approval by Dom. Coord.',
 3590:                                    validate   => 'With validation',
 3591:                                    autolimit  => 'Numerical limit',
 3592:                                    unlimited  => '(blank for unlimited)',
 3593:                  );
 3594:     return %titles;
 3595: }
 3596: 
 3597: sub authorrequest_titles {
 3598:     my %titles = &Apache::lonlocal::texthash (
 3599:                                    norequest  => 'Not allowed',
 3600:                                    approval   => 'Approval by Dom. Coord.',
 3601:                                    automatic  => 'Automatic approval',
 3602:                  );
 3603:     return %titles;
 3604: }
 3605: 
 3606: sub courserequest_conditions {
 3607:     my %conditions = &Apache::lonlocal::texthash (
 3608:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 3609:        validate   => '(Processing of request subject to institutional validation).',
 3610:                  );
 3611:     return %conditions;
 3612: }
 3613: 
 3614: 
 3615: sub print_usercreation {
 3616:     my ($position,$dom,$settings,$rowtotal) = @_;
 3617:     my $numinrow = 4;
 3618:     my $datatable;
 3619:     if ($position eq 'top') {
 3620:         $$rowtotal ++;
 3621:         my $rowcount = 0;
 3622:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 3623:         if (ref($rules) eq 'HASH') {
 3624:             if (keys(%{$rules}) > 0) {
 3625:                 $datatable .= &user_formats_row('username',$settings,$rules,
 3626:                                                 $ruleorder,$numinrow,$rowcount);
 3627:                 $$rowtotal ++;
 3628:                 $rowcount ++;
 3629:             }
 3630:         }
 3631:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 3632:         if (ref($idrules) eq 'HASH') {
 3633:             if (keys(%{$idrules}) > 0) {
 3634:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 3635:                                                 $idruleorder,$numinrow,$rowcount);
 3636:                 $$rowtotal ++;
 3637:                 $rowcount ++;
 3638:             }
 3639:         }
 3640:         my ($emailrules,$emailruleorder) = 
 3641:             &Apache::lonnet::inst_userrules($dom,'email');
 3642:         if (ref($emailrules) eq 'HASH') {
 3643:             if (keys(%{$emailrules}) > 0) {
 3644:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 3645:                                                 $emailruleorder,$numinrow,$rowcount);
 3646:                 $$rowtotal ++;
 3647:                 $rowcount ++;
 3648:             }
 3649:         }
 3650:         if ($rowcount == 0) {
 3651:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 3652:             $$rowtotal ++;
 3653:             $rowcount ++;
 3654:         }
 3655:     } elsif ($position eq 'middle') {
 3656:         my @creators = ('author','course','requestcrs','selfcreate');
 3657:         my ($rules,$ruleorder) =
 3658:             &Apache::lonnet::inst_userrules($dom,'username');
 3659:         my %lt = &usercreation_types();
 3660:         my %checked;
 3661:         my @selfcreate; 
 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:                 if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 3668:                     @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 3669:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 3670:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 3671:                         @selfcreate = ('email','login','sso');
 3672:                     } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 3673:                         @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 3674:                     }
 3675:                 }
 3676:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 3677:                 foreach my $item (@creators) {
 3678:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 3679:                         $checked{$item} = 'none';
 3680:                     }
 3681:                 }
 3682:             }
 3683:         }
 3684:         my $rownum = 0;
 3685:         foreach my $item (@creators) {
 3686:             $rownum ++;
 3687:             if ($item ne 'selfcreate') {  
 3688:                 if ($checked{$item} eq '') {
 3689:                     $checked{$item} = 'any';
 3690:                 }
 3691:             }
 3692:             my $css_class;
 3693:             if ($rownum%2) {
 3694:                 $css_class = '';
 3695:             } else {
 3696:                 $css_class = ' class="LC_odd_row" ';
 3697:             }
 3698:             $datatable .= '<tr'.$css_class.'>'.
 3699:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 3700:                          '</span></td><td align="right">';
 3701:             my @options;
 3702:             if ($item eq 'selfcreate') {
 3703:                 push(@options,('email','login','sso'));
 3704:             } else {
 3705:                 @options = ('any');
 3706:                 if (ref($rules) eq 'HASH') {
 3707:                     if (keys(%{$rules}) > 0) {
 3708:                         push(@options,('official','unofficial'));
 3709:                     }
 3710:                 }
 3711:                 push(@options,'none');
 3712:             }
 3713:             foreach my $option (@options) {
 3714:                 my $type = 'radio';
 3715:                 my $check = ' ';
 3716:                 if ($item eq 'selfcreate') {
 3717:                     $type = 'checkbox';
 3718:                     if (grep(/^\Q$option\E$/,@selfcreate)) {
 3719:                         $check = ' checked="checked" ';
 3720:                     }
 3721:                 } else {
 3722:                     if ($checked{$item} eq $option) {
 3723:                         $check = ' checked="checked" ';
 3724:                     }
 3725:                 } 
 3726:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3727:                               '<input type="'.$type.'" name="can_createuser_'.
 3728:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 3729:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 3730:             }
 3731:             $datatable .= '</td></tr>';
 3732:         }
 3733:         my ($othertitle,$usertypes,$types) =
 3734:             &Apache::loncommon::sorted_inst_types($dom);
 3735:         my $createsettings;
 3736:         if (ref($settings) eq 'HASH') {
 3737:             $createsettings = $settings->{cancreate};
 3738:         }
 3739:         if (ref($usertypes) eq 'HASH') {
 3740:             if (keys(%{$usertypes}) > 0) {
 3741:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 3742:                                              $dom,$numinrow,$othertitle,
 3743:                                              'statustocreate');
 3744:                 $$rowtotal ++;
 3745:                 $rownum ++;
 3746:             }
 3747:         }
 3748:         $datatable .= &captcha_choice('cancreate',$createsettings,$rownum);
 3749:     } else {
 3750:         my @contexts = ('author','course','domain');
 3751:         my @authtypes = ('int','krb4','krb5','loc');
 3752:         my %checked;
 3753:         if (ref($settings) eq 'HASH') {
 3754:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 3755:                 foreach my $item (@contexts) {
 3756:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 3757:                         foreach my $auth (@authtypes) {
 3758:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 3759:                                 $checked{$item}{$auth} = ' checked="checked" ';
 3760:                             }
 3761:                         }
 3762:                     }
 3763:                 }
 3764:             }
 3765:         } else {
 3766:             foreach my $item (@contexts) {
 3767:                 foreach my $auth (@authtypes) {
 3768:                     $checked{$item}{$auth} = ' checked="checked" ';
 3769:                 }
 3770:             }
 3771:         }
 3772:         my %title = &context_names();
 3773:         my %authname = &authtype_names();
 3774:         my $rownum = 0;
 3775:         my $css_class; 
 3776:         foreach my $item (@contexts) {
 3777:             if ($rownum%2) {
 3778:                 $css_class = '';
 3779:             } else {
 3780:                 $css_class = ' class="LC_odd_row" ';
 3781:             }
 3782:             $datatable .=   '<tr'.$css_class.'>'.
 3783:                             '<td>'.$title{$item}.
 3784:                             '</td><td class="LC_left_item">'.
 3785:                             '<span class="LC_nobreak">';
 3786:             foreach my $auth (@authtypes) {
 3787:                 $datatable .= '<label>'. 
 3788:                               '<input type="checkbox" name="'.$item.'_auth" '.
 3789:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 3790:                               $authname{$auth}.'</label>&nbsp;';
 3791:             }
 3792:             $datatable .= '</span></td></tr>';
 3793:             $rownum ++;
 3794:         }
 3795:         $$rowtotal += $rownum;
 3796:     }
 3797:     return $datatable;
 3798: }
 3799: 
 3800: sub captcha_choice {
 3801:     my ($context,$settings,$itemcount) = @_;
 3802:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
 3803:     my %lt = &captcha_phrases();
 3804:     $keyentry = 'hidden';
 3805:     if ($context eq 'cancreate') {
 3806:         $rowname = &mt('CAPTCHA validation (e-mail as username)');
 3807:     } elsif ($context eq 'login') {
 3808:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 3809:     }
 3810:     if (ref($settings) eq 'HASH') {
 3811:         if ($settings->{'captcha'}) {
 3812:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 3813:         } else {
 3814:             $checked{'original'} = ' checked="checked"';
 3815:         }
 3816:         if ($settings->{'captcha'} eq 'recaptcha') {
 3817:             $pubtext = $lt{'pub'};
 3818:             $privtext = $lt{'priv'};
 3819:             $keyentry = 'text';
 3820:         }
 3821:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 3822:             $currpub = $settings->{'recaptchakeys'}{'public'};
 3823:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 3824:         }
 3825:     } else {
 3826:         $checked{'original'} = ' checked="checked"';
 3827:     }
 3828:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3829:     my $output = '<tr'.$css_class.'>'.
 3830:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 3831:                  '<table><tr><td>'."\n";
 3832:     foreach my $option ('original','recaptcha','notused') {
 3833:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 3834:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 3835:                    $lt{$option}.'</label></span>';
 3836:         unless ($option eq 'notused') {
 3837:             $output .= ('&nbsp;'x2)."\n";
 3838:         }
 3839:     }
 3840: #
 3841: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 3842: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 3843: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 3844: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 3845: #
 3846:     $output .= '</td></tr>'."\n".
 3847:                '<tr><td>'."\n".
 3848:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 3849:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 3850:                $currpub.'" size="40" /></span><br />'."\n".
 3851:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 3852:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 3853:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
 3854:                '</td></tr>';
 3855:     return $output;
 3856: }
 3857: 
 3858: sub user_formats_row {
 3859:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 3860:     my $output;
 3861:     my %text = (
 3862:                    'username' => 'new usernames',
 3863:                    'id'       => 'IDs',
 3864:                    'email'    => 'self-created accounts (e-mail)',
 3865:                );
 3866:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3867:     $output = '<tr '.$css_class.'>'.
 3868:               '<td><span class="LC_nobreak">';
 3869:     if ($type eq 'email') {
 3870:         $output .= &mt("Formats disallowed for $text{$type}: ");
 3871:     } else {
 3872:         $output .= &mt("Format rules to check for $text{$type}: ");
 3873:     }
 3874:     $output .= '</span></td>'.
 3875:                '<td class="LC_left_item" colspan="2"><table>';
 3876:     my $rem;
 3877:     if (ref($ruleorder) eq 'ARRAY') {
 3878:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 3879:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 3880:                 my $rem = $i%($numinrow);
 3881:                 if ($rem == 0) {
 3882:                     if ($i > 0) {
 3883:                         $output .= '</tr>';
 3884:                     }
 3885:                     $output .= '<tr>';
 3886:                 }
 3887:                 my $check = ' ';
 3888:                 if (ref($settings) eq 'HASH') {
 3889:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 3890:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 3891:                             $check = ' checked="checked" ';
 3892:                         }
 3893:                     }
 3894:                 }
 3895:                 $output .= '<td class="LC_left_item">'.
 3896:                            '<span class="LC_nobreak"><label>'.
 3897:                            '<input type="checkbox" name="'.$type.'_rule" '.
 3898:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 3899:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 3900:             }
 3901:         }
 3902:         $rem = @{$ruleorder}%($numinrow);
 3903:     }
 3904:     my $colsleft = $numinrow - $rem;
 3905:     if ($colsleft > 1 ) {
 3906:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3907:                    '&nbsp;</td>';
 3908:     } elsif ($colsleft == 1) {
 3909:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 3910:     }
 3911:     $output .= '</tr></table></td></tr>';
 3912:     return $output;
 3913: }
 3914: 
 3915: sub usercreation_types {
 3916:     my %lt = &Apache::lonlocal::texthash (
 3917:                     author     => 'When adding a co-author',
 3918:                     course     => 'When adding a user to a course',
 3919:                     requestcrs => 'When requesting a course',
 3920:                     selfcreate => 'User creates own account', 
 3921:                     any        => 'Any',
 3922:                     official   => 'Institutional only ',
 3923:                     unofficial => 'Non-institutional only',
 3924:                     email      => 'E-mail address',
 3925:                     login      => 'Institutional Login',
 3926:                     sso        => 'SSO', 
 3927:                     none       => 'None',
 3928:     );
 3929:     return %lt;
 3930: }
 3931: 
 3932: sub authtype_names {
 3933:     my %lt = &Apache::lonlocal::texthash(
 3934:                       int    => 'Internal',
 3935:                       krb4   => 'Kerberos 4',
 3936:                       krb5   => 'Kerberos 5',
 3937:                       loc    => 'Local',
 3938:                   );
 3939:     return %lt;
 3940: }
 3941: 
 3942: sub context_names {
 3943:     my %context_title = &Apache::lonlocal::texthash(
 3944:        author => 'Creating users when an Author',
 3945:        course => 'Creating users when in a course',
 3946:        domain => 'Creating users when a Domain Coordinator',
 3947:     );
 3948:     return %context_title;
 3949: }
 3950: 
 3951: sub print_usermodification {
 3952:     my ($position,$dom,$settings,$rowtotal) = @_;
 3953:     my $numinrow = 4;
 3954:     my ($context,$datatable,$rowcount);
 3955:     if ($position eq 'top') {
 3956:         $rowcount = 0;
 3957:         $context = 'author'; 
 3958:         foreach my $role ('ca','aa') {
 3959:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3960:                                                    $numinrow,$rowcount);
 3961:             $$rowtotal ++;
 3962:             $rowcount ++;
 3963:         }
 3964:     } elsif ($position eq 'middle') {
 3965:         $context = 'course';
 3966:         $rowcount = 0;
 3967:         foreach my $role ('st','ep','ta','in','cr') {
 3968:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 3969:                                                    $numinrow,$rowcount);
 3970:             $$rowtotal ++;
 3971:             $rowcount ++;
 3972:         }
 3973:     } elsif ($position eq 'bottom') {
 3974:         $context = 'selfcreate';
 3975:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3976:         $usertypes->{'default'} = $othertitle;
 3977:         if (ref($types) eq 'ARRAY') {
 3978:             push(@{$types},'default');
 3979:             $usertypes->{'default'} = $othertitle;
 3980:             foreach my $status (@{$types}) {
 3981:                 $datatable .= &modifiable_userdata_row($context,$status,$settings,
 3982:                                                        $numinrow,$rowcount,$usertypes);
 3983:                 $$rowtotal ++;
 3984:                 $rowcount ++;
 3985:             }
 3986:         }
 3987:     }
 3988:     return $datatable;
 3989: }
 3990: 
 3991: sub print_defaults {
 3992:     my ($dom,$settings,$rowtotal) = @_;
 3993:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 3994:                  'datelocale_def','portal_def');
 3995:     my %defaults;
 3996:     if (ref($settings) eq 'HASH') {
 3997:         %defaults = %{$settings};
 3998:     } else {
 3999:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 4000:         foreach my $item (@items) {
 4001:             $defaults{$item} = $domdefaults{$item};
 4002:         }
 4003:     }
 4004:     my $titles = &defaults_titles($dom);
 4005:     my $rownum = 0;
 4006:     my ($datatable,$css_class);
 4007:     foreach my $item (@items) {
 4008:         if ($rownum%2) {
 4009:             $css_class = '';
 4010:         } else {
 4011:             $css_class = ' class="LC_odd_row" ';
 4012:         }
 4013:         $datatable .= '<tr'.$css_class.'>'.
 4014:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 4015:                   '</span></td><td class="LC_right_item">';
 4016:         if ($item eq 'auth_def') {
 4017:             my @authtypes = ('internal','krb4','krb5','localauth');
 4018:             my %shortauth = (
 4019:                              internal => 'int',
 4020:                              krb4 => 'krb4',
 4021:                              krb5 => 'krb5',
 4022:                              localauth  => 'loc'
 4023:                            );
 4024:             my %authnames = &authtype_names();
 4025:             foreach my $auth (@authtypes) {
 4026:                 my $checked = ' ';
 4027:                 if ($defaults{$item} eq $auth) {
 4028:                     $checked = ' checked="checked" ';
 4029:                 }
 4030:                 $datatable .= '<label><input type="radio" name="'.$item.
 4031:                               '" value="'.$auth.'"'.$checked.'/>'.
 4032:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 4033:             }
 4034:         } elsif ($item eq 'timezone_def') {
 4035:             my $includeempty = 1;
 4036:             $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 4037:         } elsif ($item eq 'datelocale_def') {
 4038:             my $includeempty = 1;
 4039:             $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 4040:         } elsif ($item eq 'lang_def') {
 4041:             my %langchoices = &get_languages_hash();
 4042:             $langchoices{''} = 'No language preference';
 4043:             %langchoices = &Apache::lonlocal::texthash(%langchoices);
 4044:             $datatable .= &Apache::loncommon::select_form($defaults{$item},$item,
 4045:                                                           \%langchoices);
 4046:         } else {
 4047:             my $size;
 4048:             if ($item eq 'portal_def') {
 4049:                 $size = ' size="25"';
 4050:             }
 4051:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 4052:                           $defaults{$item}.'"'.$size.' />';
 4053:         }
 4054:         $datatable .= '</td></tr>';
 4055:         $rownum ++;
 4056:     }
 4057:     $$rowtotal += $rownum;
 4058:     return $datatable;
 4059: }
 4060: 
 4061: sub get_languages_hash {
 4062:     my %langchoices;
 4063:     foreach my $id (&Apache::loncommon::languageids()) {
 4064:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 4065:         if ($code ne '') {
 4066:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 4067:         }
 4068:     }
 4069:     return %langchoices;
 4070: }
 4071: 
 4072: sub defaults_titles {
 4073:     my ($dom) = @_;
 4074:     my %titles = &Apache::lonlocal::texthash (
 4075:                    'auth_def'      => 'Default authentication type',
 4076:                    'auth_arg_def'  => 'Default authentication argument',
 4077:                    'lang_def'      => 'Default language',
 4078:                    'timezone_def'  => 'Default timezone',
 4079:                    'datelocale_def' => 'Default locale for dates',
 4080:                    'portal_def'     => 'Portal/Default URL',
 4081:                  );
 4082:     if ($dom) {
 4083:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 4084:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 4085:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 4086:         $protocol = 'http' if ($protocol ne 'https');
 4087:         if ($uint_dom) {
 4088:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 4089:                                          $uint_dom);
 4090:         }
 4091:     }
 4092:     return (\%titles);
 4093: }
 4094: 
 4095: sub print_scantronformat {
 4096:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 4097:     my $itemcount = 1;
 4098:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 4099:         %confhash);
 4100:     my $switchserver = &check_switchserver($dom,$confname);
 4101:     my %lt = &Apache::lonlocal::texthash (
 4102:                 default => 'Default bubblesheet format file error',
 4103:                 custom  => 'Custom bubblesheet format file error',
 4104:              );
 4105:     my %scantronfiles = (
 4106:         default => 'default.tab',
 4107:         custom => 'custom.tab',
 4108:     );
 4109:     foreach my $key (keys(%scantronfiles)) {
 4110:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 4111:                               .$scantronfiles{$key};
 4112:     }
 4113:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 4114:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 4115:         if (!$switchserver) {
 4116:             my $servadm = $r->dir_config('lonAdmEMail');
 4117:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 4118:             if ($configuserok eq 'ok') {
 4119:                 if ($author_ok eq 'ok') {
 4120:                     my %legacyfile = (
 4121:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 4122:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 4123:                     );
 4124:                     my %md5chk;
 4125:                     foreach my $type (keys(%legacyfile)) {
 4126:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 4127:                         chomp($md5chk{$type});
 4128:                     }
 4129:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 4130:                         foreach my $type (keys(%legacyfile)) {
 4131:                             ($scantronurls{$type},my $error) = 
 4132:                                 &legacy_scantronformat($r,$dom,$confname,
 4133:                                                  $type,$legacyfile{$type},
 4134:                                                  $scantronurls{$type},
 4135:                                                  $scantronfiles{$type});
 4136:                             if ($error ne '') {
 4137:                                 $error{$type} = $error;
 4138:                             }
 4139:                         }
 4140:                         if (keys(%error) == 0) {
 4141:                             $is_custom = 1;
 4142:                             $confhash{'scantron'}{'scantronformat'} = 
 4143:                                 $scantronurls{'custom'};
 4144:                             my $putresult = 
 4145:                                 &Apache::lonnet::put_dom('configuration',
 4146:                                                          \%confhash,$dom);
 4147:                             if ($putresult ne 'ok') {
 4148:                                 $error{'custom'} = 
 4149:                                     '<span class="LC_error">'.
 4150:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4151:                             }
 4152:                         }
 4153:                     } else {
 4154:                         ($scantronurls{'default'},my $error) =
 4155:                             &legacy_scantronformat($r,$dom,$confname,
 4156:                                           'default',$legacyfile{'default'},
 4157:                                           $scantronurls{'default'},
 4158:                                           $scantronfiles{'default'});
 4159:                         if ($error eq '') {
 4160:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 4161:                             my $putresult =
 4162:                                 &Apache::lonnet::put_dom('configuration',
 4163:                                                          \%confhash,$dom);
 4164:                             if ($putresult ne 'ok') {
 4165:                                 $error{'default'} =
 4166:                                     '<span class="LC_error">'.
 4167:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 4168:                             }
 4169:                         } else {
 4170:                             $error{'default'} = $error;
 4171:                         }
 4172:                     }
 4173:                 }
 4174:             }
 4175:         } else {
 4176:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 4177:         }
 4178:     }
 4179:     if (ref($settings) eq 'HASH') {
 4180:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 4181:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 4182:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 4183:                 $scantronurl = '';
 4184:             } else {
 4185:                 $scantronurl = $settings->{'scantronformat'};
 4186:             }
 4187:             $is_custom = 1;
 4188:         } else {
 4189:             $scantronurl = $scantronurls{'default'};
 4190:         }
 4191:     } else {
 4192:         if ($is_custom) {
 4193:             $scantronurl = $scantronurls{'custom'};
 4194:         } else {
 4195:             $scantronurl = $scantronurls{'default'};
 4196:         }
 4197:     }
 4198:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4199:     $datatable .= '<tr'.$css_class.'>';
 4200:     if (!$is_custom) {
 4201:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 4202:                       '<span class="LC_nobreak">';
 4203:         if ($scantronurl) {
 4204:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 4205:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 4206:         } else {
 4207:             $datatable = &mt('File unavailable for display');
 4208:         }
 4209:         $datatable .= '</span></td>';
 4210:         if (keys(%error) == 0) { 
 4211:             $datatable .= '<td valign="bottom">';
 4212:             if (!$switchserver) {
 4213:                 $datatable .= &mt('Upload:').'<br />';
 4214:             }
 4215:         } else {
 4216:             my $errorstr;
 4217:             foreach my $key (sort(keys(%error))) {
 4218:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4219:             }
 4220:             $datatable .= '<td>'.$errorstr;
 4221:         }
 4222:     } else {
 4223:         if (keys(%error) > 0) {
 4224:             my $errorstr;
 4225:             foreach my $key (sort(keys(%error))) {
 4226:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 4227:             } 
 4228:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 4229:         } elsif ($scantronurl) {
 4230:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 4231:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 4232:             $datatable .= '<td><span class="LC_nobreak">'.
 4233:                           $link.
 4234:                           '<label><input type="checkbox" name="scantronformat_del"'.
 4235:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 4236:                           '<td><span class="LC_nobreak">&nbsp;'.
 4237:                           &mt('Replace:').'</span><br />';
 4238:         }
 4239:     }
 4240:     if (keys(%error) == 0) {
 4241:         if ($switchserver) {
 4242:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4243:         } else {
 4244:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 4245:                          '<input type="file" name="scantronformat" /></span>';
 4246:         }
 4247:     }
 4248:     $datatable .= '</td></tr>';
 4249:     $$rowtotal ++;
 4250:     return $datatable;
 4251: }
 4252: 
 4253: sub legacy_scantronformat {
 4254:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 4255:     my ($url,$error);
 4256:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 4257:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 4258:         (my $result,$url) =
 4259:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 4260:                          '','',$newfile);
 4261:         if ($result ne 'ok') {
 4262:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 4263:         }
 4264:     }
 4265:     return ($url,$error);
 4266: }
 4267: 
 4268: sub print_coursecategories {
 4269:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 4270:     my $datatable;
 4271:     if ($position eq 'top') {
 4272:         my $toggle_cats_crs = ' ';
 4273:         my $toggle_cats_dom = ' checked="checked" ';
 4274:         my $can_cat_crs = ' ';
 4275:         my $can_cat_dom = ' checked="checked" ';
 4276:         my $toggle_catscomm_comm = ' ';
 4277:         my $toggle_catscomm_dom = ' checked="checked" ';
 4278:         my $can_catcomm_comm = ' ';
 4279:         my $can_catcomm_dom = ' checked="checked" ';
 4280: 
 4281:         if (ref($settings) eq 'HASH') {
 4282:             if ($settings->{'togglecats'} eq 'crs') {
 4283:                 $toggle_cats_crs = $toggle_cats_dom;
 4284:                 $toggle_cats_dom = ' ';
 4285:             }
 4286:             if ($settings->{'categorize'} eq 'crs') {
 4287:                 $can_cat_crs = $can_cat_dom;
 4288:                 $can_cat_dom = ' ';
 4289:             }
 4290:             if ($settings->{'togglecatscomm'} eq 'comm') {
 4291:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 4292:                 $toggle_catscomm_dom = ' ';
 4293:             }
 4294:             if ($settings->{'categorizecomm'} eq 'comm') {
 4295:                 $can_catcomm_comm = $can_catcomm_dom;
 4296:                 $can_catcomm_dom = ' ';
 4297:             }
 4298:         }
 4299:         my %title = &Apache::lonlocal::texthash (
 4300:                      togglecats     => 'Show/Hide a course in catalog',
 4301:                      togglecatscomm => 'Show/Hide a community in catalog',
 4302:                      categorize     => 'Assign a category to a course',
 4303:                      categorizecomm => 'Assign a category to a community',
 4304:                     );
 4305:         my %level = &Apache::lonlocal::texthash (
 4306:                      dom  => 'Set in Domain',
 4307:                      crs  => 'Set in Course',
 4308:                      comm => 'Set in Community',
 4309:                     );
 4310:         $datatable = '<tr class="LC_odd_row">'.
 4311:                   '<td>'.$title{'togglecats'}.'</td>'.
 4312:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4313:                   '<input type="radio" name="togglecats"'.
 4314:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4315:                   '<label><input type="radio" name="togglecats"'.
 4316:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4317:                   '</tr><tr>'.
 4318:                   '<td>'.$title{'categorize'}.'</td>'.
 4319:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4320:                   '<label><input type="radio" name="categorize"'.
 4321:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4322:                   '<label><input type="radio" name="categorize"'.
 4323:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 4324:                   '</tr><tr class="LC_odd_row">'.
 4325:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 4326:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4327:                   '<input type="radio" name="togglecatscomm"'.
 4328:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4329:                   '<label><input type="radio" name="togglecatscomm"'.
 4330:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4331:                   '</tr><tr>'.
 4332:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 4333:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 4334:                   '<label><input type="radio" name="categorizecomm"'.
 4335:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 4336:                   '<label><input type="radio" name="categorizecomm"'.
 4337:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 4338:                   '</tr>';
 4339:         $$rowtotal += 4;
 4340:     } else {
 4341:         my $css_class;
 4342:         my $itemcount = 1;
 4343:         my $cathash; 
 4344:         if (ref($settings) eq 'HASH') {
 4345:             $cathash = $settings->{'cats'};
 4346:         }
 4347:         if (ref($cathash) eq 'HASH') {
 4348:             my (@cats,@trails,%allitems,%idx,@jsarray);
 4349:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 4350:                                                    \%allitems,\%idx,\@jsarray);
 4351:             my $maxdepth = scalar(@cats);
 4352:             my $colattrib = '';
 4353:             if ($maxdepth > 2) {
 4354:                 $colattrib = ' colspan="2" ';
 4355:             }
 4356:             my @path;
 4357:             if (@cats > 0) {
 4358:                 if (ref($cats[0]) eq 'ARRAY') {
 4359:                     my $numtop = @{$cats[0]};
 4360:                     my $maxnum = $numtop;
 4361:                     my %default_names = (
 4362:                           instcode    => &mt('Official courses'),
 4363:                           communities => &mt('Communities'),
 4364:                     );
 4365: 
 4366:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 4367:                         ($cathash->{'instcode::0'} eq '') ||
 4368:                         (!grep(/^communities$/,@{$cats[0]})) || 
 4369:                         ($cathash->{'communities::0'} eq '')) {
 4370:                         $maxnum ++;
 4371:                     }
 4372:                     my $lastidx;
 4373:                     for (my $i=0; $i<$numtop; $i++) {
 4374:                         my $parent = $cats[0][$i];
 4375:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4376:                         my $item = &escape($parent).'::0';
 4377:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 4378:                         $lastidx = $idx{$item};
 4379:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4380:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 4381:                         for (my $k=0; $k<=$maxnum; $k++) {
 4382:                             my $vpos = $k+1;
 4383:                             my $selstr;
 4384:                             if ($k == $i) {
 4385:                                 $selstr = ' selected="selected" ';
 4386:                             }
 4387:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4388:                         }
 4389:                         $datatable .= '</select></span></td><td>';
 4390:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 4391:                             $datatable .=  '<span class="LC_nobreak">'
 4392:                                            .$default_names{$parent}.'</span>';
 4393:                             if ($parent eq 'instcode') {
 4394:                                 $datatable .= '<br /><span class="LC_nobreak">('
 4395:                                               .&mt('with institutional codes')
 4396:                                               .')</span></td><td'.$colattrib.'>';
 4397:                             } else {
 4398:                                 $datatable .= '<table><tr><td>';
 4399:                             }
 4400:                             $datatable .= '<span class="LC_nobreak">'
 4401:                                           .'<label><input type="radio" name="'
 4402:                                           .$parent.'" value="1" checked="checked" />'
 4403:                                           .&mt('Display').'</label>';
 4404:                             if ($parent eq 'instcode') {
 4405:                                 $datatable .= '&nbsp;';
 4406:                             } else {
 4407:                                 $datatable .= '</span></td></tr><tr><td>'
 4408:                                               .'<span class="LC_nobreak">';
 4409:                             }
 4410:                             $datatable .= '<label><input type="radio" name="'
 4411:                                           .$parent.'" value="0" />'
 4412:                                           .&mt('Do not display').'</label></span>';
 4413:                             if ($parent eq 'communities') {
 4414:                                 $datatable .= '</td></tr></table>';
 4415:                             }
 4416:                             $datatable .= '</td>';
 4417:                         } else {
 4418:                             $datatable .= $parent
 4419:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 4420:                                           .'<input type="checkbox" name="deletecategory" '
 4421:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 4422:                         }
 4423:                         my $depth = 1;
 4424:                         push(@path,$parent);
 4425:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 4426:                         pop(@path);
 4427:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 4428:                         $itemcount ++;
 4429:                     }
 4430:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4431:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 4432:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 4433:                     for (my $k=0; $k<=$maxnum; $k++) {
 4434:                         my $vpos = $k+1;
 4435:                         my $selstr;
 4436:                         if ($k == $numtop) {
 4437:                             $selstr = ' selected="selected" ';
 4438:                         }
 4439:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4440:                     }
 4441:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 4442:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 4443:                                   .'</tr>'."\n";
 4444:                     $itemcount ++;
 4445:                     foreach my $default ('instcode','communities') {
 4446:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 4447:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4448:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 4449:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 4450:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 4451:                             for (my $k=0; $k<=$maxnum; $k++) {
 4452:                                 my $vpos = $k+1;
 4453:                                 my $selstr;
 4454:                                 if ($k == $maxnum) {
 4455:                                     $selstr = ' selected="selected" ';
 4456:                                 }
 4457:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4458:                             }
 4459:                             $datatable .= '</select></span></td>'.
 4460:                                           '<td><span class="LC_nobreak">'.
 4461:                                           $default_names{$default}.'</span>';
 4462:                             if ($default eq 'instcode') {
 4463:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 4464:                                               .&mt('with institutional codes').')</span>';
 4465:                             }
 4466:                             $datatable .= '</td>'
 4467:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 4468:                                           .&mt('Display').'</label>&nbsp;'
 4469:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 4470:                                           .&mt('Do not display').'</label></span></td></tr>';
 4471:                         }
 4472:                     }
 4473:                 }
 4474:             } else {
 4475:                 $datatable .= &initialize_categories($itemcount);
 4476:             }
 4477:         } else {
 4478:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 4479:                           .&initialize_categories($itemcount);
 4480:         }
 4481:         $$rowtotal += $itemcount;
 4482:     }
 4483:     return $datatable;
 4484: }
 4485: 
 4486: sub print_serverstatuses {
 4487:     my ($dom,$settings,$rowtotal) = @_;
 4488:     my $datatable;
 4489:     my @pages = &serverstatus_pages();
 4490:     my (%namedaccess,%machineaccess);
 4491:     foreach my $type (@pages) {
 4492:         $namedaccess{$type} = '';
 4493:         $machineaccess{$type}= '';
 4494:     }
 4495:     if (ref($settings) eq 'HASH') {
 4496:         foreach my $type (@pages) {
 4497:             if (exists($settings->{$type})) {
 4498:                 if (ref($settings->{$type}) eq 'HASH') {
 4499:                     foreach my $key (keys(%{$settings->{$type}})) {
 4500:                         if ($key eq 'namedusers') {
 4501:                             $namedaccess{$type} = $settings->{$type}->{$key};
 4502:                         } elsif ($key eq 'machines') {
 4503:                             $machineaccess{$type} = $settings->{$type}->{$key};
 4504:                         }
 4505:                     }
 4506:                 }
 4507:             }
 4508:         }
 4509:     }
 4510:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 4511:     my $rownum = 0;
 4512:     my $css_class;
 4513:     foreach my $type (@pages) {
 4514:         $rownum ++;
 4515:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4516:         $datatable .= '<tr'.$css_class.'>'.
 4517:                       '<td><span class="LC_nobreak">'.
 4518:                       $titles->{$type}.'</span></td>'.
 4519:                       '<td class="LC_left_item">'.
 4520:                       '<input type="text" name="'.$type.'_namedusers" '.
 4521:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 4522:                       '<td class="LC_right_item">'.
 4523:                       '<span class="LC_nobreak">'.
 4524:                       '<input type="text" name="'.$type.'_machines" '.
 4525:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 4526:                       '</td></tr>'."\n";
 4527:     }
 4528:     $$rowtotal += $rownum;
 4529:     return $datatable;
 4530: }
 4531: 
 4532: sub serverstatus_pages {
 4533:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 4534:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
 4535:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf');
 4536: }
 4537: 
 4538: sub coursecategories_javascript {
 4539:     my ($settings) = @_;
 4540:     my ($output,$jstext,$cathash);
 4541:     if (ref($settings) eq 'HASH') {
 4542:         $cathash = $settings->{'cats'};
 4543:     }
 4544:     if (ref($cathash) eq 'HASH') {
 4545:         my (@cats,@jsarray,%idx);
 4546:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 4547:         if (@jsarray > 0) {
 4548:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 4549:             for (my $i=0; $i<@jsarray; $i++) {
 4550:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 4551:                     my $catstr = join('","',@{$jsarray[$i]});
 4552:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 4553:                 }
 4554:             }
 4555:         }
 4556:     } else {
 4557:         $jstext  = '    var categories = Array(1);'."\n".
 4558:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 4559:     }
 4560:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 4561:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 4562:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category'); 
 4563:     $output = <<"ENDSCRIPT";
 4564: <script type="text/javascript">
 4565: // <![CDATA[
 4566: function reorderCats(form,parent,item,idx) {
 4567:     var changedVal;
 4568: $jstext
 4569:     var newpos = 'addcategory_pos';
 4570:     var current = new Array;
 4571:     if (parent == '') {
 4572:         var has_instcode = 0;
 4573:         var maxtop = categories[idx].length;
 4574:         for (var j=0; j<maxtop; j++) {
 4575:             if (categories[idx][j] == 'instcode::0') {
 4576:                 has_instcode == 1;
 4577:             }
 4578:         }
 4579:         if (has_instcode == 0) {
 4580:             categories[idx][maxtop] = 'instcode_pos';
 4581:         }
 4582:     } else {
 4583:         newpos += '_'+parent;
 4584:     }
 4585:     var maxh = 1 + categories[idx].length;
 4586:     var current = new Array;
 4587:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4588:     if (item == newpos) {
 4589:         changedVal = newitemVal;
 4590:     } else {
 4591:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4592:         current[newitemVal] = newpos;
 4593:     }
 4594:     for (var i=0; i<categories[idx].length; i++) {
 4595:         var elementName = categories[idx][i];
 4596:         if (elementName != item) {
 4597:             if (form.elements[elementName]) {
 4598:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4599:                 current[currVal] = elementName;
 4600:             }
 4601:         }
 4602:     }
 4603:     var oldVal;
 4604:     for (var j=0; j<maxh; j++) {
 4605:         if (current[j] == undefined) {
 4606:             oldVal = j;
 4607:         }
 4608:     }
 4609:     if (oldVal < changedVal) {
 4610:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4611:            var elementName = current[k];
 4612:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4613:         }
 4614:     } else {
 4615:         for (var k=changedVal; k<oldVal; k++) {
 4616:             var elementName = current[k];
 4617:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4618:         }
 4619:     }
 4620:     return;
 4621: }
 4622: 
 4623: function categoryCheck(form) {
 4624:     if (form.elements['addcategory_name'].value == 'instcode') {
 4625:         alert('$instcode_reserved\\n$choose_again');
 4626:         return false;
 4627:     }
 4628:     if (form.elements['addcategory_name'].value == 'communities') {
 4629:         alert('$communities_reserved\\n$choose_again');
 4630:         return false;
 4631:     }
 4632:     return true;
 4633: }
 4634: 
 4635: // ]]>
 4636: </script>
 4637: 
 4638: ENDSCRIPT
 4639:     return $output;
 4640: }
 4641: 
 4642: sub initialize_categories {
 4643:     my ($itemcount) = @_;
 4644:     my ($datatable,$css_class,$chgstr);
 4645:     my %default_names = (
 4646:                       instcode    => 'Official courses (with institutional codes)',
 4647:                       communities => 'Communities',
 4648:                         );
 4649:     my $select0 = ' selected="selected"';
 4650:     my $select1 = '';
 4651:     foreach my $default ('instcode','communities') {
 4652:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4653:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 4654:         if ($default eq 'communities') {
 4655:             $select1 = $select0;
 4656:             $select0 = '';
 4657:         }
 4658:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4659:                      .'<select name="'.$default.'_pos">'
 4660:                      .'<option value="0"'.$select0.'>1</option>'
 4661:                      .'<option value="1"'.$select1.'>2</option>'
 4662:                      .'<option value="2">3</option></select>&nbsp;'
 4663:                      .$default_names{$default}
 4664:                      .'</span></td><td><span class="LC_nobreak">'
 4665:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 4666:                      .&mt('Display').'</label>&nbsp;<label>'
 4667:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 4668:                  .'</label></span></td></tr>';
 4669:         $itemcount ++;
 4670:     }
 4671:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4672:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 4673:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4674:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 4675:                   .'<option value="0">1</option>'
 4676:                   .'<option value="1">2</option>'
 4677:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 4678:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 4679:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 4680:     return $datatable;
 4681: }
 4682: 
 4683: sub build_category_rows {
 4684:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 4685:     my ($text,$name,$item,$chgstr);
 4686:     if (ref($cats) eq 'ARRAY') {
 4687:         my $maxdepth = scalar(@{$cats});
 4688:         if (ref($cats->[$depth]) eq 'HASH') {
 4689:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 4690:                 my $numchildren = @{$cats->[$depth]{$parent}};
 4691:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4692:                 $text .= '<td><table class="LC_data_table">';
 4693:                 my ($idxnum,$parent_name,$parent_item);
 4694:                 my $higher = $depth - 1;
 4695:                 if ($higher == 0) {
 4696:                     $parent_name = &escape($parent).'::'.$higher;
 4697:                 } else {
 4698:                     if (ref($path) eq 'ARRAY') {
 4699:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4700:                     }
 4701:                 }
 4702:                 $parent_item = 'addcategory_pos_'.$parent_name;
 4703:                 for (my $j=0; $j<=$numchildren; $j++) {
 4704:                     if ($j < $numchildren) {
 4705:                         $name = $cats->[$depth]{$parent}[$j];
 4706:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 4707:                         $idxnum = $idx->{$item};
 4708:                     } else {
 4709:                         $name = $parent_name;
 4710:                         $item = $parent_item;
 4711:                     }
 4712:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 4713:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 4714:                     for (my $i=0; $i<=$numchildren; $i++) {
 4715:                         my $vpos = $i+1;
 4716:                         my $selstr;
 4717:                         if ($j == $i) {
 4718:                             $selstr = ' selected="selected" ';
 4719:                         }
 4720:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 4721:                     }
 4722:                     $text .= '</select>&nbsp;';
 4723:                     if ($j < $numchildren) {
 4724:                         my $deeper = $depth+1;
 4725:                         $text .= $name.'&nbsp;'
 4726:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 4727:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 4728:                         if(ref($path) eq 'ARRAY') {
 4729:                             push(@{$path},$name);
 4730:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 4731:                             pop(@{$path});
 4732:                         }
 4733:                     } else {
 4734:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 4735:                         if ($j == $numchildren) {
 4736:                             $text .= $name;
 4737:                         } else {
 4738:                             $text .= $item;
 4739:                         }
 4740:                         $text .= '" value="" />';
 4741:                     }
 4742:                     $text .= '</td></tr>';
 4743:                 }
 4744:                 $text .= '</table></td>';
 4745:             } else {
 4746:                 my $higher = $depth-1;
 4747:                 if ($higher == 0) {
 4748:                     $name = &escape($parent).'::'.$higher;
 4749:                 } else {
 4750:                     if (ref($path) eq 'ARRAY') {
 4751:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 4752:                     }
 4753:                 }
 4754:                 my $colspan;
 4755:                 if ($parent ne 'instcode') {
 4756:                     $colspan = $maxdepth - $depth - 1;
 4757:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 4758:                 }
 4759:             }
 4760:         }
 4761:     }
 4762:     return $text;
 4763: }
 4764: 
 4765: sub modifiable_userdata_row {
 4766:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes) = @_;
 4767:     my $rolename;
 4768:     if ($context eq 'selfcreate') {
 4769:         if (ref($usertypes) eq 'HASH') {
 4770:             $rolename = $usertypes->{$role};
 4771:         } else {
 4772:             $rolename = $role;
 4773:         }
 4774:     } else {
 4775:         if ($role eq 'cr') {
 4776:             $rolename = &mt('Custom role');
 4777:         } else {
 4778:             $rolename = &Apache::lonnet::plaintext($role);
 4779:         }
 4780:     }
 4781:     my @fields = ('lastname','firstname','middlename','generation',
 4782:                   'permanentemail','id');
 4783:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4784:     my $output;
 4785:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 4786:     $output = '<tr '.$css_class.'>'.
 4787:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 4788:               '<td class="LC_left_item" colspan="2"><table>';
 4789:     my $rem;
 4790:     my %checks;
 4791:     if (ref($settings) eq 'HASH') {
 4792:         if (ref($settings->{$context}) eq 'HASH') {
 4793:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 4794:                 foreach my $field (@fields) {
 4795:                     if ($settings->{$context}->{$role}->{$field}) {
 4796:                         $checks{$field} = ' checked="checked" ';
 4797:                     }
 4798:                 }
 4799:             }
 4800:         }
 4801:     }
 4802:     for (my $i=0; $i<@fields; $i++) {
 4803:         my $rem = $i%($numinrow);
 4804:         if ($rem == 0) {
 4805:             if ($i > 0) {
 4806:                 $output .= '</tr>';
 4807:             }
 4808:             $output .= '<tr>';
 4809:         }
 4810:         my $check = ' ';
 4811:         if (exists($checks{$fields[$i]})) {
 4812:             $check = $checks{$fields[$i]}
 4813:         } else {
 4814:             if ($role eq 'st') {
 4815:                 if (ref($settings) ne 'HASH') {
 4816:                     $check = ' checked="checked" '; 
 4817:                 }
 4818:             }
 4819:         }
 4820:         $output .= '<td class="LC_left_item">'.
 4821:                    '<span class="LC_nobreak"><label>'.
 4822:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 4823:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 4824:                    '</label></span></td>';
 4825:         $rem = @fields%($numinrow);
 4826:     }
 4827:     my $colsleft = $numinrow - $rem;
 4828:     if ($colsleft > 1 ) {
 4829:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4830:                    '&nbsp;</td>';
 4831:     } elsif ($colsleft == 1) {
 4832:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 4833:     }
 4834:     $output .= '</tr></table></td></tr>';
 4835:     return $output;
 4836: }
 4837: 
 4838: sub insttypes_row {
 4839:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context) = @_;
 4840:     my %lt = &Apache::lonlocal::texthash (
 4841:                       cansearch => 'Users allowed to search',
 4842:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 4843:                       lockablenames => 'User preference to lock name',
 4844:              );
 4845:     my $showdom;
 4846:     if ($context eq 'cansearch') {
 4847:         $showdom = ' ('.$dom.')';
 4848:     }
 4849:     my $class = 'LC_left_item';
 4850:     if ($context eq 'statustocreate') {
 4851:         $class = 'LC_right_item';
 4852:     }
 4853:     my $output =  '<tr class="LC_odd_row">'.
 4854:                   '<td>'.$lt{$context}.$showdom.
 4855:                   '</td><td class="'.$class.'" colspan="2"><table>';
 4856:     my $rem;
 4857:     if (ref($types) eq 'ARRAY') {
 4858:         for (my $i=0; $i<@{$types}; $i++) {
 4859:             if (defined($usertypes->{$types->[$i]})) {
 4860:                 my $rem = $i%($numinrow);
 4861:                 if ($rem == 0) {
 4862:                     if ($i > 0) {
 4863:                         $output .= '</tr>';
 4864:                     }
 4865:                     $output .= '<tr>';
 4866:                 }
 4867:                 my $check = ' ';
 4868:                 if (ref($settings) eq 'HASH') {
 4869:                     if (ref($settings->{$context}) eq 'ARRAY') {
 4870:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 4871:                             $check = ' checked="checked" ';
 4872:                         }
 4873:                     } elsif ($context eq 'statustocreate') {
 4874:                         $check = ' checked="checked" ';
 4875:                     }
 4876:                 }
 4877:                 $output .= '<td class="LC_left_item">'.
 4878:                            '<span class="LC_nobreak"><label>'.
 4879:                            '<input type="checkbox" name="'.$context.'" '.
 4880:                            'value="'.$types->[$i].'"'.$check.'/>'.
 4881:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 4882:             }
 4883:         }
 4884:         $rem = @{$types}%($numinrow);
 4885:     }
 4886:     my $colsleft = $numinrow - $rem;
 4887:     if (($rem == 0) && (@{$types} > 0)) {
 4888:         $output .= '<tr>';
 4889:     }
 4890:     if ($colsleft > 1) {
 4891:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 4892:     } else {
 4893:         $output .= '<td class="LC_left_item">';
 4894:     }
 4895:     my $defcheck = ' ';
 4896:     if (ref($settings) eq 'HASH') {  
 4897:         if (ref($settings->{$context}) eq 'ARRAY') {
 4898:             if (grep(/^default$/,@{$settings->{$context}})) {
 4899:                 $defcheck = ' checked="checked" ';
 4900:             }
 4901:         } elsif ($context eq 'statustocreate') {
 4902:             $defcheck = ' checked="checked" ';
 4903:         }
 4904:     }
 4905:     $output .= '<span class="LC_nobreak"><label>'.
 4906:                '<input type="checkbox" name="'.$context.'" '.
 4907:                'value="default"'.$defcheck.'/>'.
 4908:                $othertitle.'</label></span></td>'.
 4909:                '</tr></table></td></tr>';
 4910:     return $output;
 4911: }
 4912: 
 4913: sub sorted_searchtitles {
 4914:     my %searchtitles = &Apache::lonlocal::texthash(
 4915:                          'uname' => 'username',
 4916:                          'lastname' => 'last name',
 4917:                          'lastfirst' => 'last name, first name',
 4918:                      );
 4919:     my @titleorder = ('uname','lastname','lastfirst');
 4920:     return (\%searchtitles,\@titleorder);
 4921: }
 4922: 
 4923: sub sorted_searchtypes {
 4924:     my %srchtypes_desc = (
 4925:                            exact    => 'is exact match',
 4926:                            contains => 'contains ..',
 4927:                            begins   => 'begins with ..',
 4928:                          );
 4929:     my @srchtypeorder = ('exact','begins','contains');
 4930:     return (\%srchtypes_desc,\@srchtypeorder);
 4931: }
 4932: 
 4933: sub usertype_update_row {
 4934:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 4935:     my $datatable;
 4936:     my $numinrow = 4;
 4937:     foreach my $type (@{$types}) {
 4938:         if (defined($usertypes->{$type})) {
 4939:             $$rownums ++;
 4940:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 4941:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 4942:                           '</td><td class="LC_left_item"><table>';
 4943:             for (my $i=0; $i<@{$fields}; $i++) {
 4944:                 my $rem = $i%($numinrow);
 4945:                 if ($rem == 0) {
 4946:                     if ($i > 0) {
 4947:                         $datatable .= '</tr>';
 4948:                     }
 4949:                     $datatable .= '<tr>';
 4950:                 }
 4951:                 my $check = ' ';
 4952:                 if (ref($settings) eq 'HASH') {
 4953:                     if (ref($settings->{'fields'}) eq 'HASH') {
 4954:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 4955:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 4956:                                 $check = ' checked="checked" ';
 4957:                             }
 4958:                         }
 4959:                     }
 4960:                 }
 4961: 
 4962:                 if ($i == @{$fields}-1) {
 4963:                     my $colsleft = $numinrow - $rem;
 4964:                     if ($colsleft > 1) {
 4965:                         $datatable .= '<td colspan="'.$colsleft.'">';
 4966:                     } else {
 4967:                         $datatable .= '<td>';
 4968:                     }
 4969:                 } else {
 4970:                     $datatable .= '<td>';
 4971:                 }
 4972:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4973:                               '<input type="checkbox" name="updateable_'.$type.
 4974:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 4975:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 4976:             }
 4977:             $datatable .= '</tr></table></td></tr>';
 4978:         }
 4979:     }
 4980:     return $datatable;
 4981: }
 4982: 
 4983: sub modify_login {
 4984:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 4985:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 4986:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 4987:     %title = ( coursecatalog => 'Display course catalog',
 4988:                adminmail => 'Display administrator E-mail address',
 4989:                helpdesk  => 'Display "Contact Helpdesk" link',
 4990:                newuser => 'Link for visitors to create a user account',
 4991:                loginheader => 'Log-in box header');
 4992:     @offon = ('off','on');
 4993:     if (ref($domconfig{login}) eq 'HASH') {
 4994:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 4995:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 4996:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 4997:             }
 4998:         }
 4999:     }
 5000:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 5001:                                            \%domconfig,\%loginhash);
 5002:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5003:     foreach my $item (@toggles) {
 5004:         $loginhash{login}{$item} = $env{'form.'.$item};
 5005:     }
 5006:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 5007:     if (ref($colchanges{'login'}) eq 'HASH') {  
 5008:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 5009:                                          \%loginhash);
 5010:     }
 5011: 
 5012:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5013:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 5014:     if (keys(%servers) > 1) {
 5015:         foreach my $lonhost (keys(%servers)) {
 5016:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 5017:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 5018:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 5019:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 5020:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 5021:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5022:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5023:                         $changes{'loginvia'}{$lonhost} = 1;
 5024:                     } else {
 5025:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 5026:                         $changes{'loginvia'}{$lonhost} = 1;
 5027:                     }
 5028:                 } else {
 5029:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5030:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5031:                         $changes{'loginvia'}{$lonhost} = 1;
 5032:                     }
 5033:                 }
 5034:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 5035:                     foreach my $item (@loginvia_attribs) {
 5036:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 5037:                     }
 5038:                 } else {
 5039:                     foreach my $item (@loginvia_attribs) {
 5040:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5041:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5042:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 5043:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5044:                                 $new = '/';
 5045:                             }
 5046:                         }
 5047:                         if (($item eq 'custompath') && 
 5048:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5049:                             $new = '';
 5050:                         }
 5051:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 5052:                             $changes{'loginvia'}{$lonhost} = 1;
 5053:                         }
 5054:                         if ($item eq 'exempt') {
 5055:                             $new =~ s/^\s+//;
 5056:                             $new =~ s/\s+$//;
 5057:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 5058:                             my @okips;
 5059:                             foreach my $ip (@poss_ips) {
 5060:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 5061:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 5062:                                         push(@okips,$ip); 
 5063:                                     }
 5064:                                 }
 5065:                             }
 5066:                             if (@okips > 0) {
 5067:                                 $new = join(',',@okips); 
 5068:                             } else {
 5069:                                 $new = ''; 
 5070:                             }
 5071:                         }
 5072:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5073:                     }
 5074:                 }
 5075:             } else {
 5076:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 5077:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 5078:                     $changes{'loginvia'}{$lonhost} = 1;
 5079:                     foreach my $item (@loginvia_attribs) {
 5080:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 5081:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 5082:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 5083:                                 $new = '/';
 5084:                             }
 5085:                         }
 5086:                         if (($item eq 'custompath') && 
 5087:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 5088:                             $new = '';
 5089:                         }
 5090:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 5091:                     }
 5092:                 }
 5093:             }
 5094:         }
 5095:     }
 5096: 
 5097:     my $servadm = $r->dir_config('lonAdmEMail');
 5098:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 5099:     if (ref($domconfig{'login'}) eq 'HASH') {
 5100:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 5101:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 5102:                 if ($lang eq 'nolang') {
 5103:                     push(@currlangs,$lang);
 5104:                 } elsif (defined($langchoices{$lang})) {
 5105:                     push(@currlangs,$lang);
 5106:                 } else {
 5107:                     next;
 5108:                 }
 5109:             }
 5110:         }
 5111:     }
 5112:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 5113:     if (@currlangs > 0) {
 5114:         foreach my $lang (@currlangs) {
 5115:             if (grep(/^\Q$lang\E$/,@delurls)) {
 5116:                 $changes{'helpurl'}{$lang} = 1;
 5117:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 5118:                 $changes{'helpurl'}{$lang} = 1;
 5119:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 5120:                 push(@newlangs,$lang);
 5121:             } else {
 5122:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5123:             }
 5124:         }
 5125:     }
 5126:     unless (grep(/^nolang$/,@currlangs)) {
 5127:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 5128:             $changes{'helpurl'}{'nolang'} = 1;
 5129:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 5130:             push(@newlangs,'nolang');
 5131:         }
 5132:     }
 5133:     if ($env{'form.loginhelpurl_add_lang'}) {
 5134:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 5135:             ($env{'form.loginhelpurl_add_file.filename'})) {
 5136:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 5137:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 5138:         }
 5139:     }
 5140:     if ((@newlangs > 0) || ($addedfile)) {
 5141:         my $error;
 5142:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 5143:         if ($configuserok eq 'ok') {
 5144:             if ($switchserver) {
 5145:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 5146:             } elsif ($author_ok eq 'ok') {
 5147:                 my @allnew = @newlangs;
 5148:                 if ($addedfile ne '') {
 5149:                     push(@allnew,$addedfile);
 5150:                 }
 5151:                 foreach my $lang (@allnew) {
 5152:                     my $formelem = 'loginhelpurl_'.$lang;
 5153:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 5154:                         $formelem = 'loginhelpurl_add_file';
 5155:                     }
 5156:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 5157:                                                                "help/$lang",'','',$newfile{$lang});
 5158:                     if ($result eq 'ok') {
 5159:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 5160:                         $changes{'helpurl'}{$lang} = 1;
 5161:                     } else {
 5162:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 5163:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 5164:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 5165:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 5166: 
 5167:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 5168:                         }
 5169:                     }
 5170:                 }
 5171:             } else {
 5172:                 $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);
 5173:             }
 5174:         } else {
 5175:             $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);
 5176:         }
 5177:         if ($error) {
 5178:             &Apache::lonnet::logthis($error);
 5179:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5180:         }
 5181:     }
 5182:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 5183: 
 5184:     my $defaulthelpfile = '/adm/loginproblems.html';
 5185:     my $defaulttext = &mt('Default in use');
 5186: 
 5187:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 5188:                                              $dom);
 5189:     if ($putresult eq 'ok') {
 5190:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 5191:         my %defaultchecked = (
 5192:                     'coursecatalog' => 'on',
 5193:                     'helpdesk'      => 'on',
 5194:                     'adminmail'     => 'off',
 5195:                     'newuser'       => 'off',
 5196:         );
 5197:         if (ref($domconfig{'login'}) eq 'HASH') {
 5198:             foreach my $item (@toggles) {
 5199:                 if ($defaultchecked{$item} eq 'on') { 
 5200:                     if (($domconfig{'login'}{$item} eq '0') &&
 5201:                         ($env{'form.'.$item} eq '1')) {
 5202:                         $changes{$item} = 1;
 5203:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5204:                               $domconfig{'login'}{$item} eq '1') &&
 5205:                              ($env{'form.'.$item} eq '0')) {
 5206:                         $changes{$item} = 1;
 5207:                     }
 5208:                 } elsif ($defaultchecked{$item} eq 'off') {
 5209:                     if (($domconfig{'login'}{$item} eq '1') &&
 5210:                         ($env{'form.'.$item} eq '0')) {
 5211:                         $changes{$item} = 1;
 5212:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 5213:                               $domconfig{'login'}{$item} eq '0') &&
 5214:                              ($env{'form.'.$item} eq '1')) {
 5215:                         $changes{$item} = 1;
 5216:                     }
 5217:                 }
 5218:             }
 5219:         }
 5220:         if (keys(%changes) > 0 || $colchgtext) {
 5221:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5222:             if (ref($lastactref) eq 'HASH') {
 5223:                 $lastactref->{'domainconfig'} = 1;
 5224:             }
 5225:             $resulttext = &mt('Changes made:').'<ul>';
 5226:             foreach my $item (sort(keys(%changes))) {
 5227:                 if ($item eq 'loginvia') {
 5228:                     if (ref($changes{$item}) eq 'HASH') {
 5229:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 5230:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 5231:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 5232:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 5233:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 5234:                                     $protocol = 'http' if ($protocol ne 'https');
 5235:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 5236: 
 5237:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 5238:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 5239:                                     } else {
 5240:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 5241:                                     }
 5242:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 5243:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 5244:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 5245:                                     }
 5246:                                     $resulttext .= '</li>';
 5247:                                 } else {
 5248:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 5249:                                 }
 5250:                             } else {
 5251:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 5252:                             }
 5253:                         }
 5254:                         $resulttext .= '</ul></li>';
 5255:                     }
 5256:                 } elsif ($item eq 'helpurl') {
 5257:                     if (ref($changes{$item}) eq 'HASH') {
 5258:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 5259:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 5260:                                 my ($chg,$link);
 5261:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 5262:                                 if ($lang eq 'nolang') {
 5263:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 5264:                                 } else {
 5265:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 5266:                                 }
 5267:                                 $resulttext .= '<li>'.$chg.'</li>';
 5268:                             } else {
 5269:                                 my $chg;
 5270:                                 if ($lang eq 'nolang') {
 5271:                                     $chg = &mt('custom log-in help file for no preferred language');
 5272:                                 } else {
 5273:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 5274:                                 }
 5275:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 5276:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 5277:                                                       '?inhibitmenu=yes',$chg,600,500).
 5278:                                                '</li>';
 5279:                             }
 5280:                         }
 5281:                     }
 5282:                 } elsif ($item eq 'captcha') {
 5283:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5284:                         my $chgtxt;
 5285:                         if ($loginhash{'login'}{$item} eq 'notused') {
 5286:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 5287:                         } else {
 5288:                             my %captchas = &captcha_phrases();
 5289:                             if ($captchas{$loginhash{'login'}{$item}}) {
 5290:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 5291:                             } else {
 5292:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 5293:                             }
 5294:                         }
 5295:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5296:                     }
 5297:                 } elsif ($item eq 'recaptchakeys') {
 5298:                     if (ref($loginhash{'login'}) eq 'HASH') {
 5299:                         my ($privkey,$pubkey);
 5300:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 5301:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 5302:                             $privkey = $loginhash{'login'}{$item}{'private'};
 5303:                         }
 5304:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 5305:                         if (!$pubkey) {
 5306:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 5307:                         } else {
 5308:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 5309:                         }
 5310:                         if (!$privkey) {
 5311:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 5312:                         } else {
 5313:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 5314:                         }
 5315:                         $chgtxt .= '</ul>';
 5316:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 5317:                     }
 5318:                 } else {
 5319:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 5320:                 }
 5321:             }
 5322:             $resulttext .= $colchgtext.'</ul>';
 5323:         } else {
 5324:             $resulttext = &mt('No changes made to log-in page settings');
 5325:         }
 5326:     } else {
 5327:         $resulttext = '<span class="LC_error">'.
 5328: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5329:     }
 5330:     if ($errors) {
 5331:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 5332:                        $errors.'</ul>';
 5333:     }
 5334:     return $resulttext;
 5335: }
 5336: 
 5337: sub color_font_choices {
 5338:     my %choices =
 5339:         &Apache::lonlocal::texthash (
 5340:             img => "Header",
 5341:             bgs => "Background colors",
 5342:             links => "Link colors",
 5343:             images => "Images",
 5344:             font => "Font color",
 5345:             fontmenu => "Font menu",
 5346:             pgbg => "Page",
 5347:             tabbg => "Header",
 5348:             sidebg => "Border",
 5349:             link => "Link",
 5350:             alink => "Active link",
 5351:             vlink => "Visited link",
 5352:         );
 5353:     return %choices;
 5354: }
 5355: 
 5356: sub modify_rolecolors {
 5357:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 5358:     my ($resulttext,%rolehash);
 5359:     $rolehash{'rolecolors'} = {};
 5360:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 5361:         if ($domconfig{'rolecolors'} eq '') {
 5362:             $domconfig{'rolecolors'} = {};
 5363:         }
 5364:     }
 5365:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 5366:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 5367:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 5368:                                              $dom);
 5369:     if ($putresult eq 'ok') {
 5370:         if (keys(%changes) > 0) {
 5371:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 5372:             if (ref($lastactref) eq 'HASH') {
 5373:                 $lastactref->{'domainconfig'} = 1;
 5374:             }
 5375:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 5376:                                              $rolehash{'rolecolors'});
 5377:         } else {
 5378:             $resulttext = &mt('No changes made to default color schemes');
 5379:         }
 5380:     } else {
 5381:         $resulttext = '<span class="LC_error">'.
 5382: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 5383:     }
 5384:     if ($errors) {
 5385:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 5386:                        $errors.'</ul>';
 5387:     }
 5388:     return $resulttext;
 5389: }
 5390: 
 5391: sub modify_colors {
 5392:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 5393:     my (%changes,%choices);
 5394:     my @bgs;
 5395:     my @links = ('link','alink','vlink');
 5396:     my @logintext;
 5397:     my @images;
 5398:     my $servadm = $r->dir_config('lonAdmEMail');
 5399:     my $errors;
 5400:     my %defaults;
 5401:     foreach my $role (@{$roles}) {
 5402:         if ($role eq 'login') {
 5403:             %choices = &login_choices();
 5404:             @logintext = ('textcol','bgcol');
 5405:         } else {
 5406:             %choices = &color_font_choices();
 5407:         }
 5408:         if ($role eq 'login') {
 5409:             @images = ('img','logo','domlogo','login');
 5410:             @bgs = ('pgbg','mainbg','sidebg');
 5411:         } else {
 5412:             @images = ('img');
 5413:             @bgs = ('pgbg','tabbg','sidebg');
 5414:         }
 5415:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 5416:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 5417:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 5418:         }
 5419:         if ($role eq 'login') {
 5420:             foreach my $item (@logintext) {
 5421:                 unless ($env{'form.'.$role.'_'.$item} eq  $defaults{'logintext'}{$item}) {
 5422:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5423:                 }
 5424:             }
 5425:         } else {
 5426:             unless($env{'form.'.$role.'_fontmenu'} eq $defaults{'fontmenu'}) {
 5427:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 5428:             }
 5429:         }
 5430:         foreach my $item (@bgs) {
 5431:             unless ($env{'form.'.$role.'_'.$item} eq $defaults{'bgs'}{$item} ) {
 5432:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5433:             }
 5434:         }
 5435:         foreach my $item (@links) {
 5436:             unless ($env{'form.'.$role.'_'.$item} eq  $defaults{'links'}{$item}) {
 5437:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 5438:             }
 5439:         }
 5440:         my ($configuserok,$author_ok,$switchserver) = 
 5441:             &config_check($dom,$confname,$servadm);
 5442:         my ($width,$height) = &thumb_dimensions();
 5443:         if (ref($domconfig->{$role}) ne 'HASH') {
 5444:             $domconfig->{$role} = {};
 5445:         }
 5446:         foreach my $img (@images) {
 5447:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 5448:                 if (defined($env{'form.login_showlogo_'.$img})) {
 5449:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 5450:                 } else { 
 5451:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 5452:                 }
 5453:             } 
 5454: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 5455: 		 && !defined($domconfig->{$role}{$img})
 5456: 		 && !$env{'form.'.$role.'_del_'.$img}
 5457: 		 && $env{'form.'.$role.'_import_'.$img}) {
 5458: 		# import the old configured image from the .tab setting
 5459: 		# if they haven't provided a new one 
 5460: 		$domconfig->{$role}{$img} = 
 5461: 		    $env{'form.'.$role.'_import_'.$img};
 5462: 	    }
 5463:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 5464:                 my $error;
 5465:                 if ($configuserok eq 'ok') {
 5466:                     if ($switchserver) {
 5467:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 5468:                     } else {
 5469:                         if ($author_ok eq 'ok') {
 5470:                             my ($result,$logourl) = 
 5471:                                 &publishlogo($r,'upload',$role.'_'.$img,
 5472:                                            $dom,$confname,$img,$width,$height);
 5473:                             if ($result eq 'ok') {
 5474:                                 $confhash->{$role}{$img} = $logourl;
 5475:                                 $changes{$role}{'images'}{$img} = 1;
 5476:                             } else {
 5477:                                 $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);
 5478:                             }
 5479:                         } else {
 5480:                             $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);
 5481:                         }
 5482:                     }
 5483:                 } else {
 5484:                     $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);
 5485:                 }
 5486:                 if ($error) {
 5487:                     &Apache::lonnet::logthis($error);
 5488:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5489:                 }
 5490:             } elsif ($domconfig->{$role}{$img} ne '') {
 5491:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 5492:                     my $error;
 5493:                     if ($configuserok eq 'ok') {
 5494: # is confname an author?
 5495:                         if ($switchserver eq '') {
 5496:                             if ($author_ok eq 'ok') {
 5497:                                 my ($result,$logourl) = 
 5498:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 5499:                                             $dom,$confname,$img,$width,$height);
 5500:                                 if ($result eq 'ok') {
 5501:                                     $confhash->{$role}{$img} = $logourl;
 5502: 				    $changes{$role}{'images'}{$img} = 1;
 5503:                                 }
 5504:                             }
 5505:                         }
 5506:                     }
 5507:                 }
 5508:             }
 5509:         }
 5510:         if (ref($domconfig) eq 'HASH') {
 5511:             if (ref($domconfig->{$role}) eq 'HASH') {
 5512:                 foreach my $img (@images) {
 5513:                     if ($domconfig->{$role}{$img} ne '') {
 5514:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5515:                             $confhash->{$role}{$img} = '';
 5516:                             $changes{$role}{'images'}{$img} = 1;
 5517:                         } else {
 5518:                             if ($confhash->{$role}{$img} eq '') {
 5519:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 5520:                             }
 5521:                         }
 5522:                     } else {
 5523:                         if ($env{'form.'.$role.'_del_'.$img}) {
 5524:                             $confhash->{$role}{$img} = '';
 5525:                             $changes{$role}{'images'}{$img} = 1;
 5526:                         } 
 5527:                     }
 5528:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 5529:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 5530:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 5531:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 5532:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 5533:                             }
 5534:                         } else {
 5535:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5536:                                 $changes{$role}{'showlogo'}{$img} = 1;
 5537:                             }
 5538:                         }
 5539:                     }
 5540:                 }
 5541:                 if ($domconfig->{$role}{'font'} ne '') {
 5542:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 5543:                         $changes{$role}{'font'} = 1;
 5544:                     }
 5545:                 } else {
 5546:                     if ($confhash->{$role}{'font'}) {
 5547:                         $changes{$role}{'font'} = 1;
 5548:                     }
 5549:                 }
 5550:                 if ($role ne 'login') {
 5551:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 5552:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 5553:                             $changes{$role}{'fontmenu'} = 1;
 5554:                         }
 5555:                     } else {
 5556:                         if ($confhash->{$role}{'fontmenu'}) {
 5557:                             $changes{$role}{'fontmenu'} = 1;
 5558:                         }
 5559:                     }
 5560:                 }
 5561:                 foreach my $item (@bgs) {
 5562:                     if ($domconfig->{$role}{$item} ne '') {
 5563:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5564:                             $changes{$role}{'bgs'}{$item} = 1;
 5565:                         } 
 5566:                     } else {
 5567:                         if ($confhash->{$role}{$item}) {
 5568:                             $changes{$role}{'bgs'}{$item} = 1;
 5569:                         }
 5570:                     }
 5571:                 }
 5572:                 foreach my $item (@links) {
 5573:                     if ($domconfig->{$role}{$item} ne '') {
 5574:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5575:                             $changes{$role}{'links'}{$item} = 1;
 5576:                         }
 5577:                     } else {
 5578:                         if ($confhash->{$role}{$item}) {
 5579:                             $changes{$role}{'links'}{$item} = 1;
 5580:                         }
 5581:                     }
 5582:                 }
 5583:                 foreach my $item (@logintext) {
 5584:                     if ($domconfig->{$role}{$item} ne '') {
 5585:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 5586:                             $changes{$role}{'logintext'}{$item} = 1;
 5587:                         }
 5588:                     } else {
 5589:                         if ($confhash->{$role}{$item}) {
 5590:                             $changes{$role}{'logintext'}{$item} = 1;
 5591:                         }
 5592:                     }
 5593:                 }
 5594:             } else {
 5595:                 &default_change_checker($role,\@images,\@links,\@bgs,
 5596:                                         \@logintext,$confhash,\%changes); 
 5597:             }
 5598:         } else {
 5599:             &default_change_checker($role,\@images,\@links,\@bgs,
 5600:                                     \@logintext,$confhash,\%changes); 
 5601:         }
 5602:     }
 5603:     return ($errors,%changes);
 5604: }
 5605: 
 5606: sub config_check {
 5607:     my ($dom,$confname,$servadm) = @_;
 5608:     my ($configuserok,$author_ok,$switchserver,%currroles);
 5609:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 5610:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 5611:                                                    $confname,$servadm);
 5612:     if ($configuserok eq 'ok') {
 5613:         $switchserver = &check_switchserver($dom,$confname);
 5614:         if ($switchserver eq '') {
 5615:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 5616:         }
 5617:     }
 5618:     return ($configuserok,$author_ok,$switchserver);
 5619: }
 5620: 
 5621: sub default_change_checker {
 5622:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 5623:     foreach my $item (@{$links}) {
 5624:         if ($confhash->{$role}{$item}) {
 5625:             $changes->{$role}{'links'}{$item} = 1;
 5626:         }
 5627:     }
 5628:     foreach my $item (@{$bgs}) {
 5629:         if ($confhash->{$role}{$item}) {
 5630:             $changes->{$role}{'bgs'}{$item} = 1;
 5631:         }
 5632:     }
 5633:     foreach my $item (@{$logintext}) {
 5634:         if ($confhash->{$role}{$item}) {
 5635:             $changes->{$role}{'logintext'}{$item} = 1;
 5636:         }
 5637:     }
 5638:     foreach my $img (@{$images}) {
 5639:         if ($env{'form.'.$role.'_del_'.$img}) {
 5640:             $confhash->{$role}{$img} = '';
 5641:             $changes->{$role}{'images'}{$img} = 1;
 5642:         }
 5643:         if ($role eq 'login') {
 5644:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 5645:                 $changes->{$role}{'showlogo'}{$img} = 1;
 5646:             }
 5647:         }
 5648:     }
 5649:     if ($confhash->{$role}{'font'}) {
 5650:         $changes->{$role}{'font'} = 1;
 5651:     }
 5652: }
 5653: 
 5654: sub display_colorchgs {
 5655:     my ($dom,$changes,$roles,$confhash) = @_;
 5656:     my (%choices,$resulttext);
 5657:     if (!grep(/^login$/,@{$roles})) {
 5658:         $resulttext = &mt('Changes made:').'<br />';
 5659:     }
 5660:     foreach my $role (@{$roles}) {
 5661:         if ($role eq 'login') {
 5662:             %choices = &login_choices();
 5663:         } else {
 5664:             %choices = &color_font_choices();
 5665:         }
 5666:         if (ref($changes->{$role}) eq 'HASH') {
 5667:             if ($role ne 'login') {
 5668:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 5669:             }
 5670:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 5671:                 if ($role ne 'login') {
 5672:                     $resulttext .= '<ul>';
 5673:                 }
 5674:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 5675:                     if ($role ne 'login') {
 5676:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 5677:                     }
 5678:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 5679:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 5680:                             if ($confhash->{$role}{$key}{$item}) {
 5681:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 5682:                             } else {
 5683:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 5684:                             }
 5685:                         } elsif ($confhash->{$role}{$item} eq '') {
 5686:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 5687:                         } else {
 5688:                             my $newitem = $confhash->{$role}{$item};
 5689:                             if ($key eq 'images') {
 5690:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 5691:                             }
 5692:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 5693:                         }
 5694:                     }
 5695:                     if ($role ne 'login') {
 5696:                         $resulttext .= '</ul></li>';
 5697:                     }
 5698:                 } else {
 5699:                     if ($confhash->{$role}{$key} eq '') {
 5700:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 5701:                     } else {
 5702:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 5703:                     }
 5704:                 }
 5705:                 if ($role ne 'login') {
 5706:                     $resulttext .= '</ul>';
 5707:                 }
 5708:             }
 5709:         }
 5710:     }
 5711:     return $resulttext;
 5712: }
 5713: 
 5714: sub thumb_dimensions {
 5715:     return ('200','50');
 5716: }
 5717: 
 5718: sub check_dimensions {
 5719:     my ($inputfile) = @_;
 5720:     my ($fullwidth,$fullheight);
 5721:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 5722:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 5723:             my $imageinfo = <PIPE>;
 5724:             if (!close(PIPE)) {
 5725:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 5726:             }
 5727:             chomp($imageinfo);
 5728:             my ($fullsize) = 
 5729:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 5730:             if ($fullsize) {
 5731:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 5732:             }
 5733:         }
 5734:     }
 5735:     return ($fullwidth,$fullheight);
 5736: }
 5737: 
 5738: sub check_configuser {
 5739:     my ($uhome,$dom,$confname,$servadm) = @_;
 5740:     my ($configuserok,%currroles);
 5741:     if ($uhome eq 'no_host') {
 5742:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 5743:         my $configpass = &LONCAPA::Enrollment::create_password();
 5744:         $configuserok = 
 5745:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 5746:                              $configpass,'','','','','',undef,$servadm);
 5747:     } else {
 5748:         $configuserok = 'ok';
 5749:         %currroles = 
 5750:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 5751:     }
 5752:     return ($configuserok,%currroles);
 5753: }
 5754: 
 5755: sub check_authorstatus {
 5756:     my ($dom,$confname,%currroles) = @_;
 5757:     my $author_ok;
 5758:     if (!$currroles{':'.$dom.':au'}) {
 5759:         my $start = time;
 5760:         my $end = 0;
 5761:         $author_ok = 
 5762:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 5763:                                         'au',$end,$start,'','','domconfig');
 5764:     } else {
 5765:         $author_ok = 'ok';
 5766:     }
 5767:     return $author_ok;
 5768: }
 5769: 
 5770: sub publishlogo {
 5771:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 5772:     my ($output,$fname,$logourl);
 5773:     if ($action eq 'upload') {
 5774:         $fname=$env{'form.'.$formname.'.filename'};
 5775:         chop($env{'form.'.$formname});
 5776:     } else {
 5777:         ($fname) = ($formname =~ /([^\/]+)$/);
 5778:     }
 5779:     if ($savefileas ne '') {
 5780:         $fname = $savefileas;
 5781:     }
 5782:     $fname=&Apache::lonnet::clean_filename($fname);
 5783: # See if there is anything left
 5784:     unless ($fname) { return ('error: no uploaded file'); }
 5785:     $fname="$subdir/$fname";
 5786:     my $docroot=$r->dir_config('lonDocRoot');
 5787:     my $filepath="$docroot/priv";
 5788:     my $relpath = "$dom/$confname";
 5789:     my ($fnamepath,$file,$fetchthumb);
 5790:     $file=$fname;
 5791:     if ($fname=~m|/|) {
 5792:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 5793:     }
 5794:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 5795:     my $count;
 5796:     for ($count=5;$count<=$#parts;$count++) {
 5797:         $filepath.="/$parts[$count]";
 5798:         if ((-e $filepath)!=1) {
 5799:             mkdir($filepath,02770);
 5800:         }
 5801:     }
 5802:     # Check for bad extension and disallow upload
 5803:     if ($file=~/\.(\w+)$/ &&
 5804:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 5805:         $output = 
 5806:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 5807:     } elsif ($file=~/\.(\w+)$/ &&
 5808:         !defined(&Apache::loncommon::fileembstyle($1))) {
 5809:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 5810:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 5811:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 5812:     } elsif (-d "$filepath/$file") {
 5813:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 5814:     } else {
 5815:         my $source = $filepath.'/'.$file;
 5816:         my $logfile;
 5817:         if (!open($logfile,">>$source".'.log')) {
 5818:             return (&mt('No write permission to Authoring Space'));
 5819:         }
 5820:         print $logfile
 5821: "\n================= Publish ".localtime()." ================\n".
 5822: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 5823: # Save the file
 5824:         if (!open(FH,'>'.$source)) {
 5825:             &Apache::lonnet::logthis('Failed to create '.$source);
 5826:             return (&mt('Failed to create file'));
 5827:         }
 5828:         if ($action eq 'upload') {
 5829:             if (!print FH ($env{'form.'.$formname})) {
 5830:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 5831:                 return (&mt('Failed to write file'));
 5832:             }
 5833:         } else {
 5834:             my $original = &Apache::lonnet::filelocation('',$formname);
 5835:             if(!copy($original,$source)) {
 5836:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 5837:                 return (&mt('Failed to write file'));
 5838:             }
 5839:         }
 5840:         close(FH);
 5841:         chmod(0660, $source); # Permissions to rw-rw---.
 5842: 
 5843:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 5844:         my $copyfile=$targetdir.'/'.$file;
 5845: 
 5846:         my @parts=split(/\//,$targetdir);
 5847:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 5848:         for (my $count=5;$count<=$#parts;$count++) {
 5849:             $path.="/$parts[$count]";
 5850:             if (!-e $path) {
 5851:                 print $logfile "\nCreating directory ".$path;
 5852:                 mkdir($path,02770);
 5853:             }
 5854:         }
 5855:         my $versionresult;
 5856:         if (-e $copyfile) {
 5857:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 5858:         } else {
 5859:             $versionresult = 'ok';
 5860:         }
 5861:         if ($versionresult eq 'ok') {
 5862:             if (copy($source,$copyfile)) {
 5863:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 5864:                 $output = 'ok';
 5865:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 5866:                 push(@{$modified_urls},[$copyfile,$source]);
 5867:                 my $metaoutput = 
 5868:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 5869:                 unless ($registered_cleanup) {
 5870:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5871:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5872:                     $registered_cleanup=1;
 5873:                 }
 5874:             } else {
 5875:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 5876:                 $output = &mt('Failed to copy file to RES space').", $!";
 5877:             }
 5878:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 5879:                 my $inputfile = $filepath.'/'.$file;
 5880:                 my $outfile = $filepath.'/'.'tn-'.$file;
 5881:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 5882:                 if ($fullwidth ne '' && $fullheight ne '') { 
 5883:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 5884:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 5885:                         system("convert -sample $thumbsize $inputfile $outfile");
 5886:                         chmod(0660, $filepath.'/tn-'.$file);
 5887:                         if (-e $outfile) {
 5888:                             my $copyfile=$targetdir.'/tn-'.$file;
 5889:                             if (copy($outfile,$copyfile)) {
 5890:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 5891:                                 my $thumb_metaoutput = 
 5892:                                     &write_metadata($dom,$confname,$formname,
 5893:                                                     $targetdir,'tn-'.$file,$logfile);
 5894:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 5895:                                 unless ($registered_cleanup) {
 5896:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 5897:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 5898:                                     $registered_cleanup=1;
 5899:                                 }
 5900:                             } else {
 5901:                                 print $logfile "\nUnable to write ".$copyfile.
 5902:                                                ':'.$!."\n";
 5903:                             }
 5904:                         }
 5905:                     }
 5906:                 }
 5907:             }
 5908:         } else {
 5909:             $output = $versionresult;
 5910:         }
 5911:     }
 5912:     return ($output,$logourl);
 5913: }
 5914: 
 5915: sub logo_versioning {
 5916:     my ($targetdir,$file,$logfile) = @_;
 5917:     my $target = $targetdir.'/'.$file;
 5918:     my ($maxversion,$fn,$extn,$output);
 5919:     $maxversion = 0;
 5920:     if ($file =~ /^(.+)\.(\w+)$/) {
 5921:         $fn=$1;
 5922:         $extn=$2;
 5923:     }
 5924:     opendir(DIR,$targetdir);
 5925:     while (my $filename=readdir(DIR)) {
 5926:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 5927:             $maxversion=($1>$maxversion)?$1:$maxversion;
 5928:         }
 5929:     }
 5930:     $maxversion++;
 5931:     print $logfile "\nCreating old version ".$maxversion."\n";
 5932:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 5933:     if (copy($target,$copyfile)) {
 5934:         print $logfile "Copied old target to ".$copyfile."\n";
 5935:         $copyfile=$copyfile.'.meta';
 5936:         if (copy($target.'.meta',$copyfile)) {
 5937:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 5938:             $output = 'ok';
 5939:         } else {
 5940:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 5941:             $output = &mt('Failed to copy old meta').", $!, ";
 5942:         }
 5943:     } else {
 5944:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 5945:         $output = &mt('Failed to copy old target').", $!, ";
 5946:     }
 5947:     return $output;
 5948: }
 5949: 
 5950: sub write_metadata {
 5951:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 5952:     my (%metadatafields,%metadatakeys,$output);
 5953:     $metadatafields{'title'}=$formname;
 5954:     $metadatafields{'creationdate'}=time;
 5955:     $metadatafields{'lastrevisiondate'}=time;
 5956:     $metadatafields{'copyright'}='public';
 5957:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 5958:                                          $env{'user.domain'};
 5959:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 5960:     $metadatafields{'domain'}=$dom;
 5961:     {
 5962:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 5963:         my $mfh;
 5964:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 5965:             foreach (sort(keys(%metadatafields))) {
 5966:                 unless ($_=~/\./) {
 5967:                     my $unikey=$_;
 5968:                     $unikey=~/^([A-Za-z]+)/;
 5969:                     my $tag=$1;
 5970:                     $tag=~tr/A-Z/a-z/;
 5971:                     print $mfh "\n\<$tag";
 5972:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 5973:                         my $value=$metadatafields{$unikey.'.'.$_};
 5974:                         $value=~s/\"/\'\'/g;
 5975:                         print $mfh ' '.$_.'="'.$value.'"';
 5976:                     }
 5977:                     print $mfh '>'.
 5978:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 5979:                             .'</'.$tag.'>';
 5980:                 }
 5981:             }
 5982:             $output = 'ok';
 5983:             print $logfile "\nWrote metadata";
 5984:             close($mfh);
 5985:         } else {
 5986:             print $logfile "\nFailed to open metadata file";
 5987:             $output = &mt('Could not write metadata');
 5988:         }
 5989:     }
 5990:     return $output;
 5991: }
 5992: 
 5993: sub notifysubscribed {
 5994:     foreach my $targetsource (@{$modified_urls}){
 5995:         next unless (ref($targetsource) eq 'ARRAY');
 5996:         my ($target,$source)=@{$targetsource};
 5997:         if ($source ne '') {
 5998:             if (open(my $logfh,'>>'.$source.'.log')) {
 5999:                 print $logfh "\nCleanup phase: Notifications\n";
 6000:                 my @subscribed=&subscribed_hosts($target);
 6001:                 foreach my $subhost (@subscribed) {
 6002:                     print $logfh "\nNotifying host ".$subhost.':';
 6003:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 6004:                     print $logfh $reply;
 6005:                 }
 6006:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 6007:                 foreach my $subhost (@subscribedmeta) {
 6008:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 6009:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 6010:                                                         $subhost);
 6011:                     print $logfh $reply;
 6012:                 }
 6013:                 print $logfh "\n============ Done ============\n";
 6014:                 close($logfh);
 6015:             }
 6016:         }
 6017:     }
 6018:     return OK;
 6019: }
 6020: 
 6021: sub subscribed_hosts {
 6022:     my ($target) = @_;
 6023:     my @subscribed;
 6024:     if (open(my $fh,"<$target.subscription")) {
 6025:         while (my $subline=<$fh>) {
 6026:             if ($subline =~ /^($match_lonid):/) {
 6027:                 my $host = $1;
 6028:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 6029:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 6030:                         push(@subscribed,$host);
 6031:                     }
 6032:                 }
 6033:             }
 6034:         }
 6035:     }
 6036:     return @subscribed;
 6037: }
 6038: 
 6039: sub check_switchserver {
 6040:     my ($dom,$confname) = @_;
 6041:     my ($allowed,$switchserver);
 6042:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 6043:     if ($home eq 'no_host') {
 6044:         $home = &Apache::lonnet::domain($dom,'primary');
 6045:     }
 6046:     my @ids=&Apache::lonnet::current_machine_ids();
 6047:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 6048:     if (!$allowed) {
 6049: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 6050:     }
 6051:     return $switchserver;
 6052: }
 6053: 
 6054: sub modify_quotas {
 6055:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 6056:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 6057:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 6058:         $author_ok,$switchserver,$errors);
 6059:     if ($action eq 'quotas') {
 6060:         $context = 'tools'; 
 6061:     } else {
 6062:         $context = $action;
 6063:     }
 6064:     if ($context eq 'requestcourses') {
 6065:         @usertools = ('official','unofficial','community','textbook');
 6066:         @options =('norequest','approval','validate','autolimit');
 6067:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 6068:         %titles = &courserequest_titles();
 6069:         $toolregexp = join('|',@usertools);
 6070:         %conditions = &courserequest_conditions();
 6071:         $confname = $dom.'-domainconfig';
 6072:         my $servadm = $r->dir_config('lonAdmEMail');
 6073:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 6074:     } elsif ($context eq 'requestauthor') {
 6075:         @usertools = ('author');
 6076:         %titles = &authorrequest_titles();
 6077:     } else {
 6078:         @usertools = ('aboutme','blog','webdav','portfolio');
 6079:         %titles = &tool_titles();
 6080:     }
 6081:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6082:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6083:     foreach my $key (keys(%env)) {
 6084:         if ($context eq 'requestcourses') {
 6085:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 6086:                 my $item = $1;
 6087:                 my $type = $2;
 6088:                 if ($type =~ /^limit_(.+)/) {
 6089:                     $limithash{$item}{$1} = $env{$key};
 6090:                 } else {
 6091:                     $confhash{$item}{$type} = $env{$key};
 6092:                 }
 6093:             }
 6094:         } elsif ($context eq 'requestauthor') {
 6095:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 6096:                 $confhash{$1} = $env{$key};
 6097:             }
 6098:         } else {
 6099:             if ($key =~ /^form\.quota_(.+)$/) {
 6100:                 $confhash{'defaultquota'}{$1} = $env{$key};
 6101:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 6102:                 $confhash{'authorquota'}{$1} = $env{$key};
 6103:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 6104:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 6105:             }
 6106:         }
 6107:     }
 6108:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6109:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.reqapprovalnotify');
 6110:         @approvalnotify = sort(@approvalnotify);
 6111:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 6112:         if ($env{'form.uniquecode'}) {
 6113:             $confhash{'uniquecode'} = 1;
 6114:         } else {
 6115:             $confhash{'uniquecode'} = '';
 6116:         }
 6117:         my ($newbook,@allpos);
 6118:         if ($context eq 'requestcourses') {
 6119:             if ($env{'form.addbook'}) {
 6120:                 if (($env{'form.addbook_cnum'} =~ /^$match_courseid$/) &&
 6121:                     ($env{'form.addbook_cdom'} =~ /^$match_domain$/)) {
 6122:                     if (&Apache::lonnet::homeserver($env{'form.addbook_cnum'},
 6123:                                                     $env{'form.addbook_cdom'}) eq 'no_host') {
 6124:                         $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
 6125:                                    '</span></li>';
 6126:                     } else {
 6127:                         $newbook = $env{'form.addbook_cdom'}.'_'.$env{'form.addbook_cnum'};
 6128:                         my $position = $env{'form.addbook_pos'};
 6129:                         $position =~ s/\D+//g;
 6130:                         if ($position ne '') {
 6131:                             $allpos[$position] = $newbook;
 6132:                         }
 6133:                     }
 6134:                 } else {
 6135:                     $errors .= '<li><span class="LC_error">'.&mt('Invalid LON-CAPA course for textbook').
 6136:                                '</span></li>';
 6137:                 }
 6138:             }
 6139:         }
 6140:         if (ref($domconfig{$action}) eq 'HASH') {
 6141:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 6142:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 6143:                     $changes{'notify'}{'approval'} = 1;
 6144:                 }
 6145:             } else {
 6146:                 if ($confhash{'notify'}{'approval'}) {
 6147:                     $changes{'notify'}{'approval'} = 1;
 6148:                 }
 6149:             }
 6150:             if ($domconfig{$action}{'uniquecode'}) {
 6151:                 unless ($confhash{'uniquecode'}) {
 6152:                     $changes{'uniquecode'} = 1;
 6153:                 }
 6154:             } else {
 6155:                 if ($confhash{'uniquecode'}) {
 6156:                     $changes{'uniquecode'} = 1;
 6157:                 }
 6158:             }
 6159:             if ($context eq 'requestcourses') {
 6160:                 if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
 6161:                     my %deletions;
 6162:                     my @todelete = &Apache::loncommon::get_env_multiple('form.book_del');
 6163:                     if (@todelete) {
 6164:                         map { $deletions{$_} = 1; } @todelete;
 6165:                     }
 6166:                     my %imgdeletions;
 6167:                     my @todeleteimages = &Apache::loncommon::get_env_multiple('form.book_image_del');
 6168:                     if (@todeleteimages) {
 6169:                         map { $imgdeletions{$_} = 1; } @todeleteimages;
 6170:                     }
 6171:                     my $maxnum = $env{'form.book_maxnum'};
 6172:                     for (my $i=0; $i<=$maxnum; $i++) {
 6173:                         my $key = $env{'form.book_id_'.$i};
 6174:                         if (ref($domconfig{$action}{'textbooks'}{$key}) eq 'HASH') {
 6175:                             if ($deletions{$key}) {
 6176:                                 if ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
 6177:                                     #FIXME need to obsolete item in RES space
 6178:                                 }
 6179:                                 next;
 6180:                             } else {
 6181:                                 my $newpos = $env{'form.'.$key};
 6182:                                 $newpos =~ s/\D+//g;
 6183:                                 foreach my $item ('subject','title','author') {
 6184:                                     $confhash{'textbooks'}{$key}{$item} = $env{'form.book_'.$item.'_'.$i};
 6185:                                     if ($domconfig{$action}{'textbooks'}{$key}{$item} ne $confhash{'textbooks'}{$key}{$item}) {
 6186:                                         $changes{'textbooks'}{$key} = 1;
 6187:                                     }
 6188:                                 }
 6189:                                 $allpos[$newpos] = $key;
 6190:                             }
 6191:                             if ($imgdeletions{$key}) {
 6192:                                 $changes{'textbooks'}{$key} = 1;
 6193:                                 #FIXME need to obsolete item in RES space
 6194:                             } elsif ($env{'form.book_image_'.$i.'.filename'}) {
 6195:                                 my ($cdom,$cnum) = split(/_/,$key);
 6196:                                 my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,'book_image_'.$i,
 6197:                                                                               $cdom,$cnum,$configuserok,
 6198:                                                                               $switchserver,$author_ok);
 6199:                                 if ($imgurl) {
 6200:                                     $confhash{'textbooks'}{$key}{'image'} = $imgurl;
 6201:                                     $changes{'textbooks'}{$key} = 1; 
 6202:                                 }
 6203:                                 if ($error) {
 6204:                                     &Apache::lonnet::logthis($error);
 6205:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6206:                                 } 
 6207:                             } elsif ($domconfig{$action}{'textbooks'}{$key}{'image'}) {
 6208:                                 $confhash{'textbooks'}{$key}{'image'} = 
 6209:                                     $domconfig{$action}{'textbooks'}{$key}{'image'};
 6210:                             }
 6211:                         }
 6212:                     }
 6213:                 }
 6214:             }
 6215:         } else {
 6216:             if ($confhash{'notify'}{'approval'}) {
 6217:                 $changes{'notify'}{'approval'} = 1;
 6218:             }
 6219:             if ($confhash{'uniquecode'}) {
 6220:                 $changes{'uniquecode'} = 1;
 6221:             }
 6222:         }
 6223:         if ($context eq 'requestcourses') {
 6224:             if ($newbook) {
 6225:                 $changes{'textbooks'}{$newbook} = 1;
 6226:                 foreach my $item ('subject','title','author') {
 6227:                     $env{'form.addbook_'.$item} =~ s/(`)/'/g;
 6228:                     if ($env{'form.addbook_'.$item}) {
 6229:                         $confhash{'textbooks'}{$newbook}{$item} = $env{'form.addbook_'.$item};
 6230:                     }
 6231:                 }
 6232:                 if ($env{'form.addbook_image.filename'} ne '') {
 6233:                     my ($cdom,$cnum) = split(/_/,$newbook);
 6234:                     my ($imageurl,$error) = 
 6235:                         &process_textbook_image($r,$dom,$confname,'addbook_image',$cdom,$cnum,$configuserok,
 6236:                                                 $switchserver,$author_ok);
 6237:                     if ($imageurl) {
 6238:                         $confhash{'textbooks'}{$newbook}{'image'} = $imageurl;
 6239:                     }
 6240:                     if ($error) {
 6241:                         &Apache::lonnet::logthis($error);
 6242:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 6243:                     }
 6244:                 }
 6245:             }
 6246:             if (@allpos > 0) {
 6247:                 my $idx = 0;
 6248:                 foreach my $item (@allpos) {
 6249:                     if ($item ne '') {
 6250:                         $confhash{'textbooks'}{$item}{'order'} = $idx;
 6251:                         if (ref($domconfig{$action}) eq 'HASH') {
 6252:                             if (ref($domconfig{$action}{'textbooks'}) eq 'HASH') {
 6253:                                 if (ref($domconfig{$action}{'textbooks'}{$item}) eq 'HASH') {
 6254:                                     if ($domconfig{$action}{'textbooks'}{$item}{'order'} ne $idx) {
 6255:                                         $changes{'textbooks'}{$item} = 1;
 6256:                                     }
 6257:                                 }
 6258:                             }
 6259:                         }
 6260:                         $idx ++;
 6261:                     }
 6262:                 }
 6263:             }
 6264:         }
 6265:     } else {
 6266:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 6267:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 6268:     }
 6269:     foreach my $item (@usertools) {
 6270:         foreach my $type (@{$types},'default','_LC_adv') {
 6271:             my $unset; 
 6272:             if ($context eq 'requestcourses') {
 6273:                 $unset = '0';
 6274:                 if ($type eq '_LC_adv') {
 6275:                     $unset = '';
 6276:                 }
 6277:                 if ($confhash{$item}{$type} eq 'autolimit') {
 6278:                     $confhash{$item}{$type} .= '=';
 6279:                     unless ($limithash{$item}{$type} =~ /\D/) {
 6280:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 6281:                     }
 6282:                 }
 6283:             } elsif ($context eq 'requestauthor') {
 6284:                 $unset = '0';
 6285:                 if ($type eq '_LC_adv') {
 6286:                     $unset = '';
 6287:                 }
 6288:             } else {
 6289:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 6290:                     $confhash{$item}{$type} = 1;
 6291:                 } else {
 6292:                     $confhash{$item}{$type} = 0;
 6293:                 }
 6294:             }
 6295:             if (ref($domconfig{$action}) eq 'HASH') {
 6296:                 if ($action eq 'requestauthor') {
 6297:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 6298:                         $changes{$type} = 1;
 6299:                     }
 6300:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 6301:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 6302:                         $changes{$item}{$type} = 1;
 6303:                     }
 6304:                 } else {
 6305:                     if ($context eq 'requestcourses') {
 6306:                         if ($confhash{$item}{$type} ne $unset) {
 6307:                             $changes{$item}{$type} = 1;
 6308:                         }
 6309:                     } else {
 6310:                         if (!$confhash{$item}{$type}) {
 6311:                             $changes{$item}{$type} = 1;
 6312:                         }
 6313:                     }
 6314:                 }
 6315:             } else {
 6316:                 if ($context eq 'requestcourses') {
 6317:                     if ($confhash{$item}{$type} ne $unset) {
 6318:                         $changes{$item}{$type} = 1;
 6319:                     }
 6320:                 } elsif ($context eq 'requestauthor') {
 6321:                     if ($confhash{$type} ne $unset) {
 6322:                         $changes{$type} = 1;
 6323:                     }
 6324:                 } else {
 6325:                     if (!$confhash{$item}{$type}) {
 6326:                         $changes{$item}{$type} = 1;
 6327:                     }
 6328:                 }
 6329:             }
 6330:         }
 6331:     }
 6332:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 6333:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 6334:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 6335:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 6336:                     if (exists($confhash{'defaultquota'}{$key})) {
 6337:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 6338:                             $changes{'defaultquota'}{$key} = 1;
 6339:                         }
 6340:                     } else {
 6341:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 6342:                     }
 6343:                 }
 6344:             } else {
 6345:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 6346:                     if (exists($confhash{'defaultquota'}{$key})) {
 6347:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 6348:                             $changes{'defaultquota'}{$key} = 1;
 6349:                         }
 6350:                     } else {
 6351:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 6352:                     }
 6353:                 }
 6354:             }
 6355:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 6356:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 6357:                     if (exists($confhash{'authorquota'}{$key})) {
 6358:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 6359:                             $changes{'authorquota'}{$key} = 1;
 6360:                         }
 6361:                     } else {
 6362:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 6363:                     }
 6364:                 }
 6365:             }
 6366:         }
 6367:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 6368:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 6369:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 6370:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 6371:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 6372:                             $changes{'defaultquota'}{$key} = 1;
 6373:                         }
 6374:                     } else {
 6375:                         if (!exists($domconfig{'quotas'}{$key})) {
 6376:                             $changes{'defaultquota'}{$key} = 1;
 6377:                         }
 6378:                     }
 6379:                 } else {
 6380:                     $changes{'defaultquota'}{$key} = 1;
 6381:                 }
 6382:             }
 6383:         }
 6384:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 6385:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 6386:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 6387:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 6388:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 6389:                             $changes{'authorquota'}{$key} = 1;
 6390:                         }
 6391:                     } else {
 6392:                         $changes{'authorquota'}{$key} = 1;
 6393:                     }
 6394:                 } else {
 6395:                     $changes{'authorquota'}{$key} = 1;
 6396:                 }
 6397:             }
 6398:         }
 6399:     }
 6400: 
 6401:     if ($context eq 'requestauthor') {
 6402:         $domdefaults{'requestauthor'} = \%confhash;
 6403:     } else {
 6404:         foreach my $key (keys(%confhash)) {
 6405:             unless (($context eq 'requestcourses') && ($key eq 'textbooks')) {
 6406:                 $domdefaults{$key} = $confhash{$key};
 6407:             }
 6408:         }
 6409:     }
 6410: 
 6411:     my %quotahash = (
 6412:                       $action => { %confhash }
 6413:                     );
 6414:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 6415:                                              $dom);
 6416:     if ($putresult eq 'ok') {
 6417:         if (keys(%changes) > 0) {
 6418:             my $cachetime = 24*60*60;
 6419:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 6420:             if (ref($lastactref) eq 'HASH') {
 6421:                 $lastactref->{'domdefaults'} = 1;
 6422:             }
 6423:             $resulttext = &mt('Changes made:').'<ul>';
 6424:             unless (($context eq 'requestcourses') ||
 6425:                     ($context eq 'requestauthor')) {
 6426:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 6427:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 6428:                     foreach my $type (@{$types},'default') {
 6429:                         if (defined($changes{'defaultquota'}{$type})) {
 6430:                             my $typetitle = $usertypes->{$type};
 6431:                             if ($type eq 'default') {
 6432:                                 $typetitle = $othertitle;
 6433:                             }
 6434:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 6435:                         }
 6436:                     }
 6437:                     $resulttext .= '</ul></li>';
 6438:                 }
 6439:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 6440:                     $resulttext .= '<li>'.&mt('Authoring space default quotas').'<ul>';
 6441:                     foreach my $type (@{$types},'default') {
 6442:                         if (defined($changes{'authorquota'}{$type})) {
 6443:                             my $typetitle = $usertypes->{$type};
 6444:                             if ($type eq 'default') {
 6445:                                 $typetitle = $othertitle;
 6446:                             }
 6447:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 6448:                         }
 6449:                     }
 6450:                     $resulttext .= '</ul></li>';
 6451:                 }
 6452:             }
 6453:             my %newenv;
 6454:             foreach my $item (@usertools) {
 6455:                 my (%haschgs,%inconf);
 6456:                 if ($context eq 'requestauthor') {
 6457:                     %haschgs = %changes;
 6458:                     %inconf = %confhash;
 6459:                 } else {
 6460:                     if (ref($changes{$item}) eq 'HASH') {
 6461:                         %haschgs = %{$changes{$item}};
 6462:                     }
 6463:                     if (ref($confhash{$item}) eq 'HASH') {
 6464:                         %inconf = %{$confhash{$item}};
 6465:                     }
 6466:                 }
 6467:                 if (keys(%haschgs) > 0) {
 6468:                     my $newacc = 
 6469:                         &Apache::lonnet::usertools_access($env{'user.name'},
 6470:                                                           $env{'user.domain'},
 6471:                                                           $item,'reload',$context);
 6472:                     if (($context eq 'requestcourses') ||
 6473:                         ($context eq 'requestauthor')) {
 6474:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 6475:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 6476:                         }
 6477:                     } else {
 6478:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 6479:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 6480:                         }
 6481:                     }
 6482:                     unless ($context eq 'requestauthor') {
 6483:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 6484:                     }
 6485:                     foreach my $type (@{$types},'default','_LC_adv') {
 6486:                         if ($haschgs{$type}) {
 6487:                             my $typetitle = $usertypes->{$type};
 6488:                             if ($type eq 'default') {
 6489:                                 $typetitle = $othertitle;
 6490:                             } elsif ($type eq '_LC_adv') {
 6491:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 6492:                             }
 6493:                             if ($inconf{$type}) {
 6494:                                 if ($context eq 'requestcourses') {
 6495:                                     my $cond;
 6496:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 6497:                                         if ($1 eq '') {
 6498:                                             $cond = &mt('(Automatic processing of any request).');
 6499:                                         } else {
 6500:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 6501:                                         }
 6502:                                     } else { 
 6503:                                         $cond = $conditions{$inconf{$type}};
 6504:                                     }
 6505:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 6506:                                 } elsif ($context eq 'requestauthor') {
 6507:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 6508:                                                              $titles{$inconf{$type}},$typetitle);
 6509: 
 6510:                                 } else {
 6511:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 6512:                                 }
 6513:                             } else {
 6514:                                 if ($type eq '_LC_adv') {
 6515:                                     if ($inconf{$type} eq '0') {
 6516:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 6517:                                     } else { 
 6518:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 6519:                                     }
 6520:                                 } else {
 6521:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 6522:                                 }
 6523:                             }
 6524:                         }
 6525:                     }
 6526:                     unless ($context eq 'requestauthor') {
 6527:                         $resulttext .= '</ul></li>';
 6528:                     }
 6529:                 }
 6530:             }
 6531:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 6532:                 if (ref($changes{'notify'}) eq 'HASH') {
 6533:                     if ($changes{'notify'}{'approval'}) {
 6534:                         if (ref($confhash{'notify'}) eq 'HASH') {
 6535:                             if ($confhash{'notify'}{'approval'}) {
 6536:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 6537:                             } else {
 6538:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 6539:                             }
 6540:                         }
 6541:                     }
 6542:                 }
 6543:             }
 6544:             if ($action eq 'requestcourses') {
 6545:                 my @offon = ('off','on');
 6546:                 if ($changes{'uniquecode'}) {
 6547:                     $resulttext .= '<li>'.
 6548:                                    &mt('Generation of six character code as course identifier for distribution to students set to '.
 6549:                                        $offon[$env{'form.uniquecode'}]).
 6550:                                    '</li>';
 6551:                 }
 6552:                 if (ref($changes{'textbooks'}) eq 'HASH') {
 6553:                     $resulttext .= '<li>'.&mt('Available textbooks updated').'<ul>';
 6554:                     foreach my $key (sort(keys(%{$changes{'textbooks'}}))) {
 6555:                         my %coursehash = &Apache::lonnet::coursedescription($key);
 6556:                         my $coursetitle = $coursehash{'description'};
 6557:                         my $position = $confhash{'textbooks'}{$key}{'order'} + 1;
 6558:                         $resulttext .= '<li>';
 6559:                         foreach my $item ('subject','title','author') {
 6560:                             my $name = $item.':';
 6561:                             $name =~ s/^(\w)/\U$1/;
 6562:                             $resulttext .= &mt($name).' '.$confhash{'textbooks'}{$key}{$item}.'<br />';
 6563:                         }
 6564:                         $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 6565:                         if ($confhash{'textbooks'}{$key}{'image'}) {
 6566:                             $resulttext .= ' '.&mt('Image: [_1]',
 6567:                                                '<img src="'.$confhash{'textbooks'}{$key}{'image'}.'"'.
 6568:                                                ' alt="Textbook cover" />').'<br />';
 6569:                         }
 6570:                         $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 6571:                     }
 6572:                     $resulttext .= '</ul></li>';
 6573:                 }
 6574:             }
 6575:             $resulttext .= '</ul>';
 6576:             if (keys(%newenv)) {
 6577:                 &Apache::lonnet::appenv(\%newenv);
 6578:             }
 6579:         } else {
 6580:             if ($context eq 'requestcourses') {
 6581:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 6582:             } elsif ($context eq 'requestauthor') {
 6583:                 $resulttext = &mt('No changes made to rights to request author space.');
 6584:             } else {
 6585:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 6586:             }
 6587:         }
 6588:     } else {
 6589:         $resulttext = '<span class="LC_error">'.
 6590: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6591:     }
 6592:     if ($errors) {
 6593:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 6594:                        '<ul>'.$errors.'</ul></p>';
 6595:     }
 6596:     return $resulttext;
 6597: }
 6598: 
 6599: sub process_textbook_image {
 6600:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$configuserok,$switchserver,$author_ok) = @_;
 6601:     my $filename = $env{'form.'.$caller.'.filename'};
 6602:     my ($error,$url);
 6603:     my ($width,$height) = (50,50);
 6604:     if ($configuserok eq 'ok') {
 6605:         if ($switchserver) {
 6606:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 6607:                          $switchserver);
 6608:         } elsif ($author_ok eq 'ok') {
 6609:             my ($result,$imageurl) =
 6610:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 6611:                              "textbooks/$dom/$cnum/cover",$width,$height);
 6612:             if ($result eq 'ok') {
 6613:                 $url = $imageurl;
 6614:             } else {
 6615:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 6616:             }
 6617:         } else {
 6618:             $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);
 6619:         }
 6620:     } else {
 6621:         $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);
 6622:     }
 6623:     return ($url,$error);
 6624: }
 6625: 
 6626: sub modify_autoenroll {
 6627:     my ($dom,$lastactref,%domconfig) = @_;
 6628:     my ($resulttext,%changes);
 6629:     my %currautoenroll;
 6630:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 6631:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 6632:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 6633:         }
 6634:     }
 6635:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 6636:     my %title = ( run => 'Auto-enrollment active',
 6637:                   sender => 'Sender for notification messages',
 6638:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 6639:     my @offon = ('off','on');
 6640:     my $sender_uname = $env{'form.sender_uname'};
 6641:     my $sender_domain = $env{'form.sender_domain'};
 6642:     if ($sender_domain eq '') {
 6643:         $sender_uname = '';
 6644:     } elsif ($sender_uname eq '') {
 6645:         $sender_domain = '';
 6646:     }
 6647:     my $coowners = $env{'form.autoassign_coowners'};
 6648:     my %autoenrollhash =  (
 6649:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
 6650:                                        'sender_uname' => $sender_uname,
 6651:                                        'sender_domain' => $sender_domain,
 6652:                                        'co-owners' => $coowners,
 6653:                                 }
 6654:                      );
 6655:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 6656:                                              $dom);
 6657:     if ($putresult eq 'ok') {
 6658:         if (exists($currautoenroll{'run'})) {
 6659:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 6660:                  $changes{'run'} = 1;
 6661:              }
 6662:         } elsif ($autorun) {
 6663:             if ($env{'form.autoenroll_run'} ne '1') {
 6664:                  $changes{'run'} = 1;
 6665:             }
 6666:         }
 6667:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 6668:             $changes{'sender'} = 1;
 6669:         }
 6670:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 6671:             $changes{'sender'} = 1;
 6672:         }
 6673:         if ($currautoenroll{'co-owners'} ne '') {
 6674:             if ($currautoenroll{'co-owners'} ne $coowners) {
 6675:                 $changes{'coowners'} = 1;
 6676:             }
 6677:         } elsif ($coowners) {
 6678:             $changes{'coowners'} = 1;
 6679:         }      
 6680:         if (keys(%changes) > 0) {
 6681:             $resulttext = &mt('Changes made:').'<ul>';
 6682:             if ($changes{'run'}) {
 6683:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 6684:             }
 6685:             if ($changes{'sender'}) {
 6686:                 if ($sender_uname eq '' || $sender_domain eq '') {
 6687:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 6688:                 } else {
 6689:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 6690:                 }
 6691:             }
 6692:             if ($changes{'coowners'}) {
 6693:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 6694:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 6695:                 if (ref($lastactref) eq 'HASH') {
 6696:                     $lastactref->{'domainconfig'} = 1;
 6697:                 }
 6698:             }
 6699:             $resulttext .= '</ul>';
 6700:         } else {
 6701:             $resulttext = &mt('No changes made to auto-enrollment settings');
 6702:         }
 6703:     } else {
 6704:         $resulttext = '<span class="LC_error">'.
 6705: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6706:     }
 6707:     return $resulttext;
 6708: }
 6709: 
 6710: sub modify_autoupdate {
 6711:     my ($dom,%domconfig) = @_;
 6712:     my ($resulttext,%currautoupdate,%fields,%changes);
 6713:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 6714:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 6715:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 6716:         }
 6717:     }
 6718:     my @offon = ('off','on');
 6719:     my %title = &Apache::lonlocal::texthash (
 6720:                    run => 'Auto-update:',
 6721:                    classlists => 'Updates to user information in classlists?'
 6722:                 );
 6723:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6724:     my %fieldtitles = &Apache::lonlocal::texthash (
 6725:                         id => 'Student/Employee ID',
 6726:                         permanentemail => 'E-mail address',
 6727:                         lastname => 'Last Name',
 6728:                         firstname => 'First Name',
 6729:                         middlename => 'Middle Name',
 6730:                         generation => 'Generation',
 6731:                       );
 6732:     $othertitle = &mt('All users');
 6733:     if (keys(%{$usertypes}) >  0) {
 6734:         $othertitle = &mt('Other users');
 6735:     }
 6736:     foreach my $key (keys(%env)) {
 6737:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 6738:             my ($usertype,$item) = ($1,$2);
 6739:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
 6740:                 if ($usertype eq 'default') {   
 6741:                     push(@{$fields{$1}},$2);
 6742:                 } elsif (ref($types) eq 'ARRAY') {
 6743:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
 6744:                         push(@{$fields{$1}},$2);
 6745:                     }
 6746:                 }
 6747:             }
 6748:         }
 6749:     }
 6750:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
 6751:     @lockablenames = sort(@lockablenames);
 6752:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
 6753:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6754:         if (@changed) {
 6755:             $changes{'lockablenames'} = 1;
 6756:         }
 6757:     } else {
 6758:         if (@lockablenames) {
 6759:             $changes{'lockablenames'} = 1;
 6760:         }
 6761:     }
 6762:     my %updatehash = (
 6763:                       autoupdate => { run => $env{'form.autoupdate_run'},
 6764:                                       classlists => $env{'form.classlists'},
 6765:                                       fields => {%fields},
 6766:                                       lockablenames => \@lockablenames,
 6767:                                     }
 6768:                      );
 6769:     foreach my $key (keys(%currautoupdate)) {
 6770:         if (($key eq 'run') || ($key eq 'classlists')) {
 6771:             if (exists($updatehash{autoupdate}{$key})) {
 6772:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 6773:                     $changes{$key} = 1;
 6774:                 }
 6775:             }
 6776:         } elsif ($key eq 'fields') {
 6777:             if (ref($currautoupdate{$key}) eq 'HASH') {
 6778:                 foreach my $item (@{$types},'default') {
 6779:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 6780:                         my $change = 0;
 6781:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 6782:                             if (!exists($fields{$item})) {
 6783:                                 $change = 1;
 6784:                                 last;
 6785:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 6786:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 6787:                                     $change = 1;
 6788:                                     last;
 6789:                                 }
 6790:                             }
 6791:                         }
 6792:                         if ($change) {
 6793:                             push(@{$changes{$key}},$item);
 6794:                         }
 6795:                     } 
 6796:                 }
 6797:             }
 6798:         } elsif ($key eq 'lockablenames') {
 6799:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
 6800:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
 6801:                 if (@changed) {
 6802:                     $changes{'lockablenames'} = 1;
 6803:                 }
 6804:             } else {
 6805:                 if (@lockablenames) {
 6806:                     $changes{'lockablenames'} = 1;
 6807:                 }
 6808:             }
 6809:         }
 6810:     }
 6811:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
 6812:         if (@lockablenames) {
 6813:             $changes{'lockablenames'} = 1;
 6814:         }
 6815:     }
 6816:     foreach my $item (@{$types},'default') {
 6817:         if (defined($fields{$item})) {
 6818:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 6819:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
 6820:                     my $change = 0;
 6821:                     if (ref($fields{$item}) eq 'ARRAY') {
 6822:                         foreach my $type (@{$fields{$item}}) {
 6823:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
 6824:                                 $change = 1;
 6825:                                 last;
 6826:                             }
 6827:                         }
 6828:                     }
 6829:                     if ($change) {
 6830:                         push(@{$changes{'fields'}},$item);
 6831:                     }
 6832:                 } else {
 6833:                     push(@{$changes{'fields'}},$item);
 6834:                 }
 6835:             } else {
 6836:                 push(@{$changes{'fields'}},$item);
 6837:             }
 6838:         }
 6839:     }
 6840:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 6841:                                              $dom);
 6842:     if ($putresult eq 'ok') {
 6843:         if (keys(%changes) > 0) {
 6844:             $resulttext = &mt('Changes made:').'<ul>';
 6845:             foreach my $key (sort(keys(%changes))) {
 6846:                 if ($key eq 'lockablenames') {
 6847:                     $resulttext .= '<li>';
 6848:                     if (@lockablenames) {
 6849:                         $usertypes->{'default'} = $othertitle;
 6850:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
 6851:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
 6852:                     } else {
 6853:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
 6854:                     }
 6855:                     $resulttext .= '</li>';
 6856:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
 6857:                     foreach my $item (@{$changes{$key}}) {
 6858:                         my @newvalues;
 6859:                         foreach my $type (@{$fields{$item}}) {
 6860:                             push(@newvalues,$fieldtitles{$type});
 6861:                         }
 6862:                         my $newvaluestr;
 6863:                         if (@newvalues > 0) {
 6864:                             $newvaluestr = join(', ',@newvalues);
 6865:                         } else {
 6866:                             $newvaluestr = &mt('none');
 6867:                         }
 6868:                         if ($item eq 'default') {
 6869:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 6870:                         } else {
 6871:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 6872:                         }
 6873:                     }
 6874:                 } else {
 6875:                     my $newvalue;
 6876:                     if ($key eq 'run') {
 6877:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 6878:                     } else {
 6879:                         $newvalue = $offon[$env{'form.'.$key}];
 6880:                     }
 6881:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 6882:                 }
 6883:             }
 6884:             $resulttext .= '</ul>';
 6885:         } else {
 6886:             $resulttext = &mt('No changes made to autoupdates');
 6887:         }
 6888:     } else {
 6889:         $resulttext = '<span class="LC_error">'.
 6890: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 6891:     }
 6892:     return $resulttext;
 6893: }
 6894: 
 6895: sub modify_autocreate {
 6896:     my ($dom,%domconfig) = @_;
 6897:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 6898:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 6899:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 6900:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 6901:         }
 6902:     }
 6903:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 6904:                  req => 'Auto-creation of validated requests for official courses',
 6905:                  xmldc => 'Identity of course creator of courses from XML files',
 6906:                );
 6907:     my @types = ('xml','req');
 6908:     foreach my $item (@types) {
 6909:         $newvals{$item} = $env{'form.autocreate_'.$item};
 6910:         $newvals{$item} =~ s/\D//g;
 6911:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 6912:     }
 6913:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 6914:     my %domcoords = &get_active_dcs($dom);
 6915:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 6916:         $newvals{'xmldc'} = '';
 6917:     } 
 6918:     %autocreatehash =  (
 6919:                         autocreate => { xml => $newvals{'xml'},
 6920:                                         req => $newvals{'req'},
 6921:                                       }
 6922:                        );
 6923:     if ($newvals{'xmldc'} ne '') {
 6924:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 6925:     }
 6926:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 6927:                                              $dom);
 6928:     if ($putresult eq 'ok') {
 6929:         my @items = @types;
 6930:         if ($newvals{'xml'}) {
 6931:             push(@items,'xmldc');
 6932:         }
 6933:         foreach my $item (@items) {
 6934:             if (exists($currautocreate{$item})) {
 6935:                 if ($currautocreate{$item} ne $newvals{$item}) {
 6936:                     $changes{$item} = 1;
 6937:                 }
 6938:             } elsif ($newvals{$item}) {
 6939:                 $changes{$item} = 1;
 6940:             }
 6941:         }
 6942:         if (keys(%changes) > 0) {
 6943:             my @offon = ('off','on'); 
 6944:             $resulttext = &mt('Changes made:').'<ul>';
 6945:             foreach my $item (@types) {
 6946:                 if ($changes{$item}) {
 6947:                     my $newtxt = $offon[$newvals{$item}];
 6948:                     $resulttext .= '<li>'.
 6949:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
 6950:                                        '<b>','</b>').
 6951:                                    '</li>';
 6952:                 }
 6953:             }
 6954:             if ($changes{'xmldc'}) {
 6955:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 6956:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 6957:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
 6958:             }
 6959:             $resulttext .= '</ul>';
 6960:         } else {
 6961:             $resulttext = &mt('No changes made to auto-creation settings');
 6962:         }
 6963:     } else {
 6964:         $resulttext = '<span class="LC_error">'.
 6965:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6966:     }
 6967:     return $resulttext;
 6968: }
 6969: 
 6970: sub modify_directorysrch {
 6971:     my ($dom,%domconfig) = @_;
 6972:     my ($resulttext,%changes);
 6973:     my %currdirsrch;
 6974:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 6975:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 6976:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 6977:         }
 6978:     }
 6979:     my %title = ( available => 'Directory search available',
 6980:                   localonly => 'Other domains can search',
 6981:                   searchby => 'Search types',
 6982:                   searchtypes => 'Search latitude');
 6983:     my @offon = ('off','on');
 6984:     my @otherdoms = ('Yes','No');
 6985: 
 6986:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 6987:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 6988:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 6989: 
 6990:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6991:     if (keys(%{$usertypes}) == 0) {
 6992:         @cansearch = ('default');
 6993:     } else {
 6994:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 6995:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 6996:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 6997:                     push(@{$changes{'cansearch'}},$type);
 6998:                 }
 6999:             }
 7000:             foreach my $type (@cansearch) {
 7001:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 7002:                     push(@{$changes{'cansearch'}},$type);
 7003:                 }
 7004:             }
 7005:         } else {
 7006:             push(@{$changes{'cansearch'}},@cansearch);
 7007:         }
 7008:     }
 7009: 
 7010:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 7011:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 7012:             if (!grep(/^\Q$by\E$/,@searchby)) {
 7013:                 push(@{$changes{'searchby'}},$by);
 7014:             }
 7015:         }
 7016:         foreach my $by (@searchby) {
 7017:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 7018:                 push(@{$changes{'searchby'}},$by);
 7019:             }
 7020:         }
 7021:     } else {
 7022:         push(@{$changes{'searchby'}},@searchby);
 7023:     }
 7024: 
 7025:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 7026:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 7027:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 7028:                 push(@{$changes{'searchtypes'}},$type);
 7029:             }
 7030:         }
 7031:         foreach my $type (@searchtypes) {
 7032:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 7033:                 push(@{$changes{'searchtypes'}},$type);
 7034:             }
 7035:         }
 7036:     } else {
 7037:         if (exists($currdirsrch{'searchtypes'})) {
 7038:             foreach my $type (@searchtypes) {  
 7039:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 7040:                     push(@{$changes{'searchtypes'}},$type);
 7041:                 }
 7042:             }
 7043:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 7044:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 7045:             }   
 7046:         } else {
 7047:             push(@{$changes{'searchtypes'}},@searchtypes); 
 7048:         }
 7049:     }
 7050: 
 7051:     my %dirsrch_hash =  (
 7052:             directorysrch => { available => $env{'form.dirsrch_available'},
 7053:                                cansearch => \@cansearch,
 7054:                                localonly => $env{'form.dirsrch_localonly'},
 7055:                                searchby => \@searchby,
 7056:                                searchtypes => \@searchtypes,
 7057:                              }
 7058:             );
 7059:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 7060:                                              $dom);
 7061:     if ($putresult eq 'ok') {
 7062:         if (exists($currdirsrch{'available'})) {
 7063:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 7064:                  $changes{'available'} = 1;
 7065:              }
 7066:         } else {
 7067:             if ($env{'form.dirsrch_available'} eq '1') {
 7068:                 $changes{'available'} = 1;
 7069:             }
 7070:         }
 7071:         if (exists($currdirsrch{'localonly'})) {
 7072:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 7073:                  $changes{'localonly'} = 1;
 7074:              }
 7075:         } else {
 7076:             if ($env{'form.dirsrch_localonly'} eq '1') {
 7077:                 $changes{'localonly'} = 1;
 7078:             }
 7079:         }
 7080:         if (keys(%changes) > 0) {
 7081:             $resulttext = &mt('Changes made:').'<ul>';
 7082:             if ($changes{'available'}) {
 7083:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 7084:             }
 7085:             if ($changes{'localonly'}) {
 7086:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 7087:             }
 7088: 
 7089:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 7090:                 my $chgtext;
 7091:                 if (ref($usertypes) eq 'HASH') {
 7092:                     if (keys(%{$usertypes}) > 0) {
 7093:                         foreach my $type (@{$types}) {
 7094:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 7095:                                 $chgtext .= $usertypes->{$type}.'; ';
 7096:                             }
 7097:                         }
 7098:                         if (grep(/^default$/,@cansearch)) {
 7099:                             $chgtext .= $othertitle;
 7100:                         } else {
 7101:                             $chgtext =~ s/\; $//;
 7102:                         }
 7103:                         $resulttext .=
 7104:                             '<li>'.
 7105:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
 7106:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
 7107:                             '</li>';
 7108:                     }
 7109:                 }
 7110:             }
 7111:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 7112:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 7113:                 my $chgtext;
 7114:                 foreach my $type (@{$titleorder}) {
 7115:                     if (grep(/^\Q$type\E$/,@searchby)) {
 7116:                         if (defined($searchtitles->{$type})) {
 7117:                             $chgtext .= $searchtitles->{$type}.'; ';
 7118:                         }
 7119:                     }
 7120:                 }
 7121:                 $chgtext =~ s/\; $//;
 7122:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 7123:             }
 7124:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 7125:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 7126:                 my $chgtext;
 7127:                 foreach my $type (@{$srchtypeorder}) {
 7128:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 7129:                         if (defined($srchtypes_desc->{$type})) {
 7130:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 7131:                         }
 7132:                     }
 7133:                 }
 7134:                 $chgtext =~ s/\; $//;
 7135:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
 7136:             }
 7137:             $resulttext .= '</ul>';
 7138:         } else {
 7139:             $resulttext = &mt('No changes made to institution directory search settings');
 7140:         }
 7141:     } else {
 7142:         $resulttext = '<span class="LC_error">'.
 7143:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 7144:     }
 7145:     return $resulttext;
 7146: }
 7147: 
 7148: sub modify_contacts {
 7149:     my ($dom,$lastactref,%domconfig) = @_;
 7150:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 7151:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 7152:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 7153:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 7154:         }
 7155:     }
 7156:     my (%others,%to,%bcc);
 7157:     my @contacts = ('supportemail','adminemail');
 7158:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 7159:                     'requestsmail','updatesmail','idconflictsmail');
 7160:     my @toggles = ('reporterrors','reportupdates');
 7161:     foreach my $type (@mailings) {
 7162:         @{$newsetting{$type}} = 
 7163:             &Apache::loncommon::get_env_multiple('form.'.$type);
 7164:         foreach my $item (@contacts) {
 7165:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 7166:                 $contacts_hash{contacts}{$type}{$item} = 1;
 7167:             } else {
 7168:                 $contacts_hash{contacts}{$type}{$item} = 0;
 7169:             }
 7170:         }  
 7171:         $others{$type} = $env{'form.'.$type.'_others'};
 7172:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 7173:         if ($type eq 'helpdeskmail') {
 7174:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
 7175:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
 7176:         }
 7177:     }
 7178:     foreach my $item (@contacts) {
 7179:         $to{$item} = $env{'form.'.$item};
 7180:         $contacts_hash{'contacts'}{$item} = $to{$item};
 7181:     }
 7182:     foreach my $item (@toggles) {
 7183:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
 7184:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
 7185:         }
 7186:     }
 7187:     if (keys(%currsetting) > 0) {
 7188:         foreach my $item (@contacts) {
 7189:             if ($to{$item} ne $currsetting{$item}) {
 7190:                 $changes{$item} = 1;
 7191:             }
 7192:         }
 7193:         foreach my $type (@mailings) {
 7194:             foreach my $item (@contacts) {
 7195:                 if (ref($currsetting{$type}) eq 'HASH') {
 7196:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 7197:                         push(@{$changes{$type}},$item);
 7198:                     }
 7199:                 } else {
 7200:                     push(@{$changes{$type}},@{$newsetting{$type}});
 7201:                 }
 7202:             }
 7203:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 7204:                 push(@{$changes{$type}},'others');
 7205:             }
 7206:             if ($type eq 'helpdeskmail') {   
 7207:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
 7208:                     push(@{$changes{$type}},'bcc'); 
 7209:                 }
 7210:             }
 7211:         }
 7212:     } else {
 7213:         my %default;
 7214:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 7215:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 7216:         $default{'errormail'} = 'adminemail';
 7217:         $default{'packagesmail'} = 'adminemail';
 7218:         $default{'helpdeskmail'} = 'supportemail';
 7219:         $default{'lonstatusmail'} = 'adminemail';
 7220:         $default{'requestsmail'} = 'adminemail';
 7221:         $default{'updatesmail'} = 'adminemail';
 7222:         foreach my $item (@contacts) {
 7223:            if ($to{$item} ne $default{$item}) {
 7224:               $changes{$item} = 1;
 7225:            }
 7226:         }
 7227:         foreach my $type (@mailings) {
 7228:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 7229:                
 7230:                 push(@{$changes{$type}},@{$newsetting{$type}});
 7231:             }
 7232:             if ($others{$type} ne '') {
 7233:                 push(@{$changes{$type}},'others');
 7234:             }
 7235:             if ($type eq 'helpdeskmail') {
 7236:                 if ($bcc{$type} ne '') {
 7237:                     push(@{$changes{$type}},'bcc');
 7238:                 }
 7239:             }
 7240:         }
 7241:     }
 7242:     foreach my $item (@toggles) {
 7243:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
 7244:             $changes{$item} = 1;
 7245:         } elsif ((!$env{'form.'.$item}) &&
 7246:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
 7247:             $changes{$item} = 1;
 7248:         }
 7249:     }
 7250:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 7251:                                              $dom);
 7252:     if ($putresult eq 'ok') {
 7253:         if (keys(%changes) > 0) {
 7254:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7255:             if (ref($lastactref) eq 'HASH') {
 7256:                 $lastactref->{'domainconfig'} = 1;
 7257:             }
 7258:             my ($titles,$short_titles)  = &contact_titles();
 7259:             $resulttext = &mt('Changes made:').'<ul>';
 7260:             foreach my $item (@contacts) {
 7261:                 if ($changes{$item}) {
 7262:                     $resulttext .= '<li>'.$titles->{$item}.
 7263:                                     &mt(' set to: ').
 7264:                                     '<span class="LC_cusr_emph">'.
 7265:                                     $to{$item}.'</span></li>';
 7266:                 }
 7267:             }
 7268:             foreach my $type (@mailings) {
 7269:                 if (ref($changes{$type}) eq 'ARRAY') {
 7270:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 7271:                     my @text;
 7272:                     foreach my $item (@{$newsetting{$type}}) {
 7273:                         push(@text,$short_titles->{$item});
 7274:                     }
 7275:                     if ($others{$type} ne '') {
 7276:                         push(@text,$others{$type});
 7277:                     }
 7278:                     $resulttext .= '<span class="LC_cusr_emph">'.
 7279:                                    join(', ',@text).'</span>';
 7280:                     if ($type eq 'helpdeskmail') {
 7281:                         if ($bcc{$type} ne '') {
 7282:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
 7283:                         }
 7284:                     }
 7285:                     $resulttext .= '</li>';
 7286:                 }
 7287:             }
 7288:             my @offon = ('off','on');
 7289:             if ($changes{'reporterrors'}) {
 7290:                 $resulttext .= '<li>'.
 7291:                                &mt('E-mail error reports to [_1] set to "'.
 7292:                                    $offon[$env{'form.reporterrors'}].'".',
 7293:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 7294:                                        &mt('LON-CAPA core group - MSU'),600,500)).
 7295:                                '</li>';
 7296:             }
 7297:             if ($changes{'reportupdates'}) {
 7298:                 $resulttext .= '<li>'.
 7299:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
 7300:                                     $offon[$env{'form.reportupdates'}].'".',
 7301:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 7302:                                         &mt('LON-CAPA core group - MSU'),600,500)).
 7303:                                 '</li>';
 7304:             }
 7305:             $resulttext .= '</ul>';
 7306:         } else {
 7307:             $resulttext = &mt('No changes made to contact information');
 7308:         }
 7309:     } else {
 7310:         $resulttext = '<span class="LC_error">'.
 7311:             &mt('An error occurred: [_1].',$putresult).'</span>';
 7312:     }
 7313:     return $resulttext;
 7314: }
 7315: 
 7316: sub modify_usercreation {
 7317:     my ($dom,%domconfig) = @_;
 7318:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate);
 7319:     my $warningmsg;
 7320:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 7321:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 7322:             $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 7323:         }
 7324:     }
 7325:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 7326:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 7327:     my @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 7328:     my @contexts = ('author','course','requestcrs','selfcreate');
 7329:     foreach my $item(@contexts) {
 7330:         if ($item eq 'selfcreate') {
 7331:             @{$cancreate{$item}} = &Apache::loncommon::get_env_multiple('form.can_createuser_'.$item);
 7332:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 7333:             if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth'))) {
 7334:                 if (ref($cancreate{$item}) eq 'ARRAY') { 
 7335:                     if (grep(/^login$/,@{$cancreate{$item}})) {
 7336:                         $warningmsg = &mt('Although account creation has been set to be available for institutional logins, currently default authentication in this domain has not been set to support this.').' '.&mt('You need to set the default authentication type to Kerberos 4 or 5 (with a Kerberos domain specified), or to Local authentication, if the localauth module has been customized in your domain to authenticate institutional logins.');   
 7337:                     }
 7338:                 }
 7339:             }
 7340:         } else {
 7341:             $cancreate{$item} = $env{'form.can_createuser_'.$item};
 7342:         }
 7343:     }
 7344:     my ($othertitle,$usertypes,$types) = 
 7345:         &Apache::loncommon::sorted_inst_types($dom);
 7346:     if (ref($types) eq 'ARRAY') {
 7347:         if (@{$types} > 0) {
 7348:             @{$cancreate{'statustocreate'}} = 
 7349:                 &Apache::loncommon::get_env_multiple('form.statustocreate');
 7350:         } else {
 7351:             @{$cancreate{'statustocreate'}} = ();
 7352:         }
 7353:         push(@contexts,'statustocreate');
 7354:     }
 7355:     &process_captcha('cancreate',\%changes,\%cancreate,\%curr_usercreation);
 7356:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 7357:         foreach my $item (@contexts) {
 7358:             if (($item eq 'selfcreate') || ($item eq 'statustocreate')) {
 7359:                 if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 7360:                     foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 7361:                         if (ref($cancreate{$item}) eq 'ARRAY') {
 7362:                             if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 7363:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7364:                                     push(@{$changes{'cancreate'}},$item);
 7365:                                 }
 7366:                             }
 7367:                         }
 7368:                     }
 7369:                 } else {
 7370:                     if ($curr_usercreation{'cancreate'}{$item} eq '') {
 7371:                         if (@{$cancreate{$item}} > 0) {
 7372:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7373:                                 push(@{$changes{'cancreate'}},$item);
 7374:                             }
 7375:                         }
 7376:                     } else {
 7377:                         if ($curr_usercreation{'cancreate'}{$item} eq 'any') {
 7378:                             if (@{$cancreate{$item}} < 3) {
 7379:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7380:                                     push(@{$changes{'cancreate'}},$item);
 7381:                                 }
 7382:                             }
 7383:                         } elsif ($curr_usercreation{'cancreate'}{$item} eq 'none') {
 7384:                             if (@{$cancreate{$item}} > 0) {
 7385:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7386:                                     push(@{$changes{'cancreate'}},$item);
 7387:                                 }
 7388:                             }
 7389:                         } elsif (!grep(/^$curr_usercreation{'cancreate'}{$item}$/,@{$cancreate{$item}})) {
 7390:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7391:                                 push(@{$changes{'cancreate'}},$item);
 7392:                             }
 7393:                         }
 7394:                     }
 7395:                 }
 7396:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7397:                     foreach my $type (@{$cancreate{$item}}) {
 7398:                         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 7399:                             if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 7400:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7401:                                     push(@{$changes{'cancreate'}},$item);
 7402:                                 }
 7403:                             }
 7404:                         } elsif (($curr_usercreation{'cancreate'}{$item} ne 'any') &&
 7405:                                  ($curr_usercreation{'cancreate'}{$item} ne 'none')) {
 7406:                             if ($curr_usercreation{'cancreate'}{$item} ne $type) {
 7407:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 7408:                                     push(@{$changes{'cancreate'}},$item);
 7409:                                 }
 7410:                             }
 7411:                         }
 7412:                     }
 7413:                 }
 7414:             } else {
 7415:                 if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 7416:                     push(@{$changes{'cancreate'}},$item);
 7417:                 }
 7418:             }
 7419:         }
 7420:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 7421:         foreach my $item (@contexts) {
 7422:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 7423:                 if ($cancreate{$item} ne 'any') {
 7424:                     push(@{$changes{'cancreate'}},$item);
 7425:                 }
 7426:             } else {
 7427:                 if ($cancreate{$item} ne 'none') {
 7428:                     push(@{$changes{'cancreate'}},$item);
 7429:                 }
 7430:             }
 7431:         }
 7432:     } else {
 7433:         foreach my $item (@contexts)  {
 7434:             push(@{$changes{'cancreate'}},$item);
 7435:         }
 7436:     }
 7437: 
 7438:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 7439:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 7440:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 7441:                 push(@{$changes{'username_rule'}},$type);
 7442:             }
 7443:         }
 7444:         foreach my $type (@username_rule) {
 7445:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 7446:                 push(@{$changes{'username_rule'}},$type);
 7447:             }
 7448:         }
 7449:     } else {
 7450:         push(@{$changes{'username_rule'}},@username_rule);
 7451:     }
 7452: 
 7453:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 7454:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 7455:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 7456:                 push(@{$changes{'id_rule'}},$type);
 7457:             }
 7458:         }
 7459:         foreach my $type (@id_rule) {
 7460:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 7461:                 push(@{$changes{'id_rule'}},$type);
 7462:             }
 7463:         }
 7464:     } else {
 7465:         push(@{$changes{'id_rule'}},@id_rule);
 7466:     }
 7467: 
 7468:     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 7469:         foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 7470:             if (!grep(/^\Q$type\E$/,@email_rule)) {
 7471:                 push(@{$changes{'email_rule'}},$type);
 7472:             }
 7473:         }
 7474:         foreach my $type (@email_rule) {
 7475:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 7476:                 push(@{$changes{'email_rule'}},$type);
 7477:             }
 7478:         }
 7479:     } else {
 7480:         push(@{$changes{'email_rule'}},@email_rule);
 7481:     }
 7482: 
 7483:     my @authen_contexts = ('author','course','domain');
 7484:     my @authtypes = ('int','krb4','krb5','loc');
 7485:     my %authhash;
 7486:     foreach my $item (@authen_contexts) {
 7487:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 7488:         foreach my $auth (@authtypes) {
 7489:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 7490:                 $authhash{$item}{$auth} = 1;
 7491:             } else {
 7492:                 $authhash{$item}{$auth} = 0;
 7493:             }
 7494:         }
 7495:     }
 7496:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 7497:         foreach my $item (@authen_contexts) {
 7498:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 7499:                 foreach my $auth (@authtypes) {
 7500:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 7501:                         push(@{$changes{'authtypes'}},$item);
 7502:                         last;
 7503:                     }
 7504:                 }
 7505:             }
 7506:         }
 7507:     } else {
 7508:         foreach my $item (@authen_contexts) {
 7509:             push(@{$changes{'authtypes'}},$item);
 7510:         }
 7511:     }
 7512: 
 7513:     my %usercreation_hash =  (
 7514:             usercreation => {
 7515:                               cancreate     => \%cancreate,
 7516:                               username_rule => \@username_rule,
 7517:                               id_rule       => \@id_rule,
 7518:                               email_rule    => \@email_rule,
 7519:                               authtypes     => \%authhash,
 7520:                             }
 7521:             );
 7522: 
 7523:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 7524:                                              $dom);
 7525: 
 7526:     my %selfcreatetypes = (
 7527:                              sso   => 'users authenticated by institutional single sign on',
 7528:                              login => 'users authenticated by institutional log-in',
 7529:                              email => 'users who provide a valid e-mail address for use as the username',
 7530:                           );
 7531:     if ($putresult eq 'ok') {
 7532:         if (keys(%changes) > 0) {
 7533:             $resulttext = &mt('Changes made:').'<ul>';
 7534:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 7535:                 my %lt = &usercreation_types();
 7536:                 foreach my $type (@{$changes{'cancreate'}}) {
 7537:                     my $chgtext;
 7538:                     unless (($type eq 'statustocreate') || ($type eq 'captcha') || ($type eq 'recaptchakeys')) {
 7539:                         $chgtext = $lt{$type}.', ';
 7540:                     }
 7541:                     if ($type eq 'selfcreate') {
 7542:                         if (@{$cancreate{$type}} == 0) {
 7543:                             $chgtext .= &mt('creation of a new user account is not permitted.');
 7544:                         } else {
 7545:                             $chgtext .= &mt('creation of a new account is permitted for:').'<ul>';
 7546:                             foreach my $case (@{$cancreate{$type}}) {
 7547:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 7548:                             }
 7549:                             $chgtext .= '</ul>';
 7550:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 7551:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 7552:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 7553:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 7554:                                             $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 7555:                                         }
 7556:                                     }
 7557:                                 }
 7558:                             }
 7559:                         }
 7560:                     } elsif ($type eq 'statustocreate') {
 7561:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 7562:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 7563:                             if (@{$cancreate{'selfcreate'}} > 0) {
 7564:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 7565: 
 7566:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 7567:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 7568:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 7569:                                     } 
 7570:                                 } elsif (ref($usertypes) eq 'HASH') {
 7571:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 7572:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 7573:                                     } else {
 7574:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 7575:                                     }
 7576:                                     $chgtext .= '<ul>';
 7577:                                     foreach my $case (@{$cancreate{$type}}) {
 7578:                                         if ($case eq 'default') {
 7579:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 7580:                                         } else {
 7581:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 7582:                                         }
 7583:                                     }
 7584:                                     $chgtext .= '</ul>';
 7585:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 7586:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').'</span>';
 7587:                                     }
 7588:                                 }
 7589:                             } else {
 7590:                                 if (@{$cancreate{$type}} == 0) {
 7591:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 7592:                                 } else {
 7593:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 7594:                                 }
 7595:                             }
 7596:                         }
 7597:                     } elsif ($type eq 'captcha') {
 7598:                         if ($cancreate{$type} eq 'notused') {
 7599:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
 7600:                         } else {
 7601:                             my %captchas = &captcha_phrases();
 7602:                             if ($captchas{$cancreate{$type}}) {
 7603:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$cancreate{$type}}.");
 7604:                             } else {
 7605:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
 7606:                             }
 7607:                         }
 7608:                     } elsif ($type eq 'recaptchakeys') {
 7609:                         my ($privkey,$pubkey);
 7610:                         if (ref($cancreate{$type}) eq 'HASH') {
 7611:                             $pubkey = $cancreate{$type}{'public'};
 7612:                             $privkey = $cancreate{$type}{'private'};
 7613:                         }
 7614:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
 7615:                         if (!$pubkey) {
 7616:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
 7617:                         } else {
 7618:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 7619:                         }
 7620:                         if (!$privkey) {
 7621:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
 7622:                         } else {
 7623:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
 7624:                         }
 7625:                         $chgtext .= '</ul>';
 7626:                     } else {
 7627:                         if ($cancreate{$type} eq 'none') {
 7628:                             $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 7629:                         } elsif ($cancreate{$type} eq 'any') {
 7630:                             $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 7631:                         } elsif ($cancreate{$type} eq 'official') {
 7632:                             $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 7633:                         } elsif ($cancreate{$type} eq 'unofficial') {
 7634:                             $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 7635:                         }
 7636:                     }
 7637:                     $resulttext .= '<li>'.$chgtext.'</li>';
 7638:                 }
 7639:             }
 7640:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 7641:                 my ($rules,$ruleorder) = 
 7642:                     &Apache::lonnet::inst_userrules($dom,'username');
 7643:                 my $chgtext = '<ul>';
 7644:                 foreach my $type (@username_rule) {
 7645:                     if (ref($rules->{$type}) eq 'HASH') {
 7646:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 7647:                     }
 7648:                 }
 7649:                 $chgtext .= '</ul>';
 7650:                 if (@username_rule > 0) {
 7651:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 7652:                 } else {
 7653:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 7654:                 }
 7655:             }
 7656:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 7657:                 my ($idrules,$idruleorder) = 
 7658:                     &Apache::lonnet::inst_userrules($dom,'id');
 7659:                 my $chgtext = '<ul>';
 7660:                 foreach my $type (@id_rule) {
 7661:                     if (ref($idrules->{$type}) eq 'HASH') {
 7662:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 7663:                     }
 7664:                 }
 7665:                 $chgtext .= '</ul>';
 7666:                 if (@id_rule > 0) {
 7667:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 7668:                 } else {
 7669:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 7670:                 }
 7671:             }
 7672:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 7673:                 my ($emailrules,$emailruleorder) =
 7674:                     &Apache::lonnet::inst_userrules($dom,'email');
 7675:                 my $chgtext = '<ul>';
 7676:                 foreach my $type (@email_rule) {
 7677:                     if (ref($emailrules->{$type}) eq 'HASH') {
 7678:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 7679:                     }
 7680:                 }
 7681:                 $chgtext .= '</ul>';
 7682:                 if (@email_rule > 0) {
 7683:                     $resulttext .= '<li>'.&mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').$chgtext.'</li>';
 7684:                 } else {
 7685:                     $resulttext .= '<li>'.&mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').'</li>';
 7686:                 }
 7687:             }
 7688: 
 7689:             my %authname = &authtype_names();
 7690:             my %context_title = &context_names();
 7691:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 7692:                 my $chgtext = '<ul>';
 7693:                 foreach my $type (@{$changes{'authtypes'}}) {
 7694:                     my @allowed;
 7695:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 7696:                     foreach my $auth (@authtypes) {
 7697:                         if ($authhash{$type}{$auth}) {
 7698:                             push(@allowed,$authname{$auth});
 7699:                         }
 7700:                     }
 7701:                     if (@allowed > 0) {
 7702:                         $chgtext .= join(', ',@allowed).'</li>';
 7703:                     } else {
 7704:                         $chgtext .= &mt('none').'</li>';
 7705:                     }
 7706:                 }
 7707:                 $chgtext .= '</ul>';
 7708:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 7709:                 $resulttext .= '</li>';
 7710:             }
 7711:             $resulttext .= '</ul>';
 7712:         } else {
 7713:             $resulttext = &mt('No changes made to user creation settings');
 7714:         }
 7715:     } else {
 7716:         $resulttext = '<span class="LC_error">'.
 7717:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7718:     }
 7719:     if ($warningmsg ne '') {
 7720:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 7721:     }
 7722:     return $resulttext;
 7723: }
 7724: 
 7725: sub process_captcha {
 7726:     my ($container,$changes,$newsettings,$current) = @_;
 7727:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
 7728:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
 7729:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
 7730:         $newsettings->{'captcha'} = 'original';
 7731:     }
 7732:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
 7733:         if ($container eq 'cancreate') {
 7734:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7735:                 push(@{$changes->{'cancreate'}},'captcha');
 7736:             } elsif (!defined($changes->{'cancreate'})) {
 7737:                 $changes->{'cancreate'} = ['captcha'];
 7738:             }
 7739:         } else {
 7740:             $changes->{'captcha'} = 1;
 7741:         }
 7742:     }
 7743:     my ($newpub,$newpriv,$currpub,$currpriv);
 7744:     if ($newsettings->{'captcha'} eq 'recaptcha') {
 7745:         $newpub = $env{'form.'.$container.'_recaptchapub'};
 7746:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
 7747:         $newpub =~ s/\W//g;
 7748:         $newpriv =~ s/\W//g;
 7749:         $newsettings->{'recaptchakeys'} = {
 7750:                                              public  => $newpub,
 7751:                                              private => $newpriv,
 7752:                                           };
 7753:     }
 7754:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
 7755:         $currpub = $current->{'recaptchakeys'}{'public'};
 7756:         $currpriv = $current->{'recaptchakeys'}{'private'};
 7757:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
 7758:             $newsettings->{'recaptchakeys'} = {
 7759:                                                  public  => '',
 7760:                                                  private => '',
 7761:                                               }
 7762:         }
 7763:     }
 7764:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
 7765:         if ($container eq 'cancreate') {
 7766:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
 7767:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
 7768:             } elsif (!defined($changes->{'cancreate'})) {
 7769:                 $changes->{'cancreate'} = ['recaptchakeys'];
 7770:             }
 7771:         } else {
 7772:             $changes->{'recaptchakeys'} = 1;
 7773:         }
 7774:     }
 7775:     return;
 7776: }
 7777: 
 7778: sub modify_usermodification {
 7779:     my ($dom,%domconfig) = @_;
 7780:     my ($resulttext,%curr_usermodification,%changes);
 7781:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 7782:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 7783:             $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 7784:         }
 7785:     }
 7786:     my @contexts = ('author','course','selfcreate');
 7787:     my %context_title = (
 7788:                            author => 'In author context',
 7789:                            course => 'In course context',
 7790:                            selfcreate => 'When self creating account', 
 7791:                         );
 7792:     my @fields = ('lastname','firstname','middlename','generation',
 7793:                   'permanentemail','id');
 7794:     my %roles = (
 7795:                   author => ['ca','aa'],
 7796:                   course => ['st','ep','ta','in','cr'],
 7797:                 );
 7798:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7799:     if (ref($types) eq 'ARRAY') {
 7800:         push(@{$types},'default');
 7801:         $usertypes->{'default'} = $othertitle;
 7802:     }
 7803:     $roles{'selfcreate'} = $types;  
 7804:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7805:     my %modifyhash;
 7806:     foreach my $context (@contexts) {
 7807:         foreach my $role (@{$roles{$context}}) {
 7808:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 7809:             foreach my $item (@fields) {
 7810:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 7811:                     $modifyhash{$context}{$role}{$item} = 1;
 7812:                 } else {
 7813:                     $modifyhash{$context}{$role}{$item} = 0;
 7814:                 }
 7815:             }
 7816:         }
 7817:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 7818:             foreach my $role (@{$roles{$context}}) {
 7819:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 7820:                     foreach my $field (@fields) {
 7821:                         if ($modifyhash{$context}{$role}{$field} ne 
 7822:                                 $curr_usermodification{$context}{$role}{$field}) {
 7823:                             push(@{$changes{$context}},$role);
 7824:                             last;
 7825:                         }
 7826:                     }
 7827:                 }
 7828:             }
 7829:         } else {
 7830:             foreach my $context (@contexts) {
 7831:                 foreach my $role (@{$roles{$context}}) {
 7832:                     push(@{$changes{$context}},$role);
 7833:                 }
 7834:             }
 7835:         }
 7836:     }
 7837:     my %usermodification_hash =  (
 7838:                                    usermodification => \%modifyhash,
 7839:                                  );
 7840:     my $putresult = &Apache::lonnet::put_dom('configuration',
 7841:                                              \%usermodification_hash,$dom);
 7842:     if ($putresult eq 'ok') {
 7843:         if (keys(%changes) > 0) {
 7844:             $resulttext = &mt('Changes made: ').'<ul>';
 7845:             foreach my $context (@contexts) {
 7846:                 if (ref($changes{$context}) eq 'ARRAY') {
 7847:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 7848:                     if (ref($changes{$context}) eq 'ARRAY') {
 7849:                         foreach my $role (@{$changes{$context}}) {
 7850:                             my $rolename;
 7851:                             if ($context eq 'selfcreate') {
 7852:                                 $rolename = $role;
 7853:                                 if (ref($usertypes) eq 'HASH') {
 7854:                                     if ($usertypes->{$role} ne '') {
 7855:                                         $rolename = $usertypes->{$role};
 7856:                                     }
 7857:                                 }
 7858:                             } else {
 7859:                                 if ($role eq 'cr') {
 7860:                                     $rolename = &mt('Custom');
 7861:                                 } else {
 7862:                                     $rolename = &Apache::lonnet::plaintext($role);
 7863:                                 }
 7864:                             }
 7865:                             my @modifiable;
 7866:                             if ($context eq 'selfcreate') {
 7867:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Self-creation of account by users with status: [_1]',$rolename).'</span> - '.&mt('modifiable fields (if institutional data blank): ');
 7868:                             } else {
 7869:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 7870:                             }
 7871:                             foreach my $field (@fields) {
 7872:                                 if ($modifyhash{$context}{$role}{$field}) {
 7873:                                     push(@modifiable,$fieldtitles{$field});
 7874:                                 }
 7875:                             }
 7876:                             if (@modifiable > 0) {
 7877:                                 $resulttext .= join(', ',@modifiable);
 7878:                             } else {
 7879:                                 $resulttext .= &mt('none'); 
 7880:                             }
 7881:                             $resulttext .= '</li>';
 7882:                         }
 7883:                         $resulttext .= '</ul></li>';
 7884:                     }
 7885:                 }
 7886:             }
 7887:             $resulttext .= '</ul>';
 7888:         } else {
 7889:             $resulttext = &mt('No changes made to user modification settings');
 7890:         }
 7891:     } else {
 7892:         $resulttext = '<span class="LC_error">'.
 7893:             &mt('An error occurred: [_1]',$putresult).'</span>';
 7894:     }
 7895:     return $resulttext;
 7896: }
 7897: 
 7898: sub modify_defaults {
 7899:     my ($dom,$lastactref,%domconfig) = @_;
 7900:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 7901:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 7902:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
 7903:     my @authtypes = ('internal','krb4','krb5','localauth');
 7904:     foreach my $item (@items) {
 7905:         $newvalues{$item} = $env{'form.'.$item};
 7906:         if ($item eq 'auth_def') {
 7907:             if ($newvalues{$item} ne '') {
 7908:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 7909:                     push(@errors,$item);
 7910:                 }
 7911:             }
 7912:         } elsif ($item eq 'lang_def') {
 7913:             if ($newvalues{$item} ne '') {
 7914:                 if ($newvalues{$item} =~ /^(\w+)/) {
 7915:                     my $langcode = $1;
 7916:                     if ($langcode ne 'x_chef') {
 7917:                         if (code2language($langcode) eq '') {
 7918:                             push(@errors,$item);
 7919:                         }
 7920:                     }
 7921:                 } else {
 7922:                     push(@errors,$item);
 7923:                 }
 7924:             }
 7925:         } elsif ($item eq 'timezone_def') {
 7926:             if ($newvalues{$item} ne '') {
 7927:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 7928:                     push(@errors,$item);   
 7929:                 }
 7930:             }
 7931:         } elsif ($item eq 'datelocale_def') {
 7932:             if ($newvalues{$item} ne '') {
 7933:                 my @datelocale_ids = DateTime::Locale->ids();
 7934:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 7935:                     push(@errors,$item);
 7936:                 }
 7937:             }
 7938:         } elsif ($item eq 'portal_def') {
 7939:             if ($newvalues{$item} ne '') {
 7940:                 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])\/?$/) {
 7941:                     push(@errors,$item);
 7942:                 }
 7943:             }
 7944:         }
 7945:         if (grep(/^\Q$item\E$/,@errors)) {
 7946:             $newvalues{$item} = $domdefaults{$item};
 7947:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 7948:             $changes{$item} = 1;
 7949:         }
 7950:         $domdefaults{$item} = $newvalues{$item};
 7951:     }
 7952:     my %defaults_hash = (
 7953:                          defaults => \%newvalues,
 7954:                         );
 7955:     my $title = &defaults_titles();
 7956:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 7957:                                              $dom);
 7958:     if ($putresult eq 'ok') {
 7959:         if (keys(%changes) > 0) {
 7960:             $resulttext = &mt('Changes made:').'<ul>';
 7961:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
 7962:             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";
 7963:             foreach my $item (sort(keys(%changes))) {
 7964:                 my $value = $env{'form.'.$item};
 7965:                 if ($value eq '') {
 7966:                     $value = &mt('none');
 7967:                 } elsif ($item eq 'auth_def') {
 7968:                     my %authnames = &authtype_names();
 7969:                     my %shortauth = (
 7970:                              internal => 'int',
 7971:                              krb4 => 'krb4',
 7972:                              krb5 => 'krb5',
 7973:                              localauth  => 'loc',
 7974:                     );
 7975:                     $value = $authnames{$shortauth{$value}};
 7976:                 }
 7977:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 7978:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 7979:             }
 7980:             $resulttext .= '</ul>';
 7981:             $mailmsgtext .= "\n";
 7982:             my $cachetime = 24*60*60;
 7983:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 7984:             if (ref($lastactref) eq 'HASH') {
 7985:                 $lastactref->{'domdefaults'} = 1;
 7986:             }
 7987:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 7988:                 my $notify = 1;
 7989:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
 7990:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
 7991:                         $notify = 0;
 7992:                     }
 7993:                 }
 7994:                 if ($notify) {
 7995:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
 7996:                                                "LON-CAPA Domain Settings Change - $dom",
 7997:                                                $mailmsgtext);
 7998:                 }
 7999:             }
 8000:         } else {
 8001:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 8002:         }
 8003:     } else {
 8004:         $resulttext = '<span class="LC_error">'.
 8005:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8006:     }
 8007:     if (@errors > 0) {
 8008:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 8009:         foreach my $item (@errors) {
 8010:             $resulttext .= ' "'.$title->{$item}.'",';
 8011:         }
 8012:         $resulttext =~ s/,$//;
 8013:     }
 8014:     return $resulttext;
 8015: }
 8016: 
 8017: sub modify_scantron {
 8018:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 8019:     my ($resulttext,%confhash,%changes,$errors);
 8020:     my $custom = 'custom.tab';
 8021:     my $default = 'default.tab';
 8022:     my $servadm = $r->dir_config('lonAdmEMail');
 8023:     my ($configuserok,$author_ok,$switchserver) = 
 8024:         &config_check($dom,$confname,$servadm);
 8025:     if ($env{'form.scantronformat.filename'} ne '') {
 8026:         my $error;
 8027:         if ($configuserok eq 'ok') {
 8028:             if ($switchserver) {
 8029:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
 8030:             } else {
 8031:                 if ($author_ok eq 'ok') {
 8032:                     my ($result,$scantronurl) =
 8033:                         &publishlogo($r,'upload','scantronformat',$dom,
 8034:                                      $confname,'scantron','','',$custom);
 8035:                     if ($result eq 'ok') {
 8036:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 8037:                         $changes{'scantronformat'} = 1;
 8038:                     } else {
 8039:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 8040:                     }
 8041:                 } else {
 8042:                     $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);
 8043:                 }
 8044:             }
 8045:         } else {
 8046:             $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);
 8047:         }
 8048:         if ($error) {
 8049:             &Apache::lonnet::logthis($error);
 8050:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8051:         }
 8052:     }
 8053:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 8054:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 8055:             if ($env{'form.scantronformat_del'}) {
 8056:                 $confhash{'scantron'}{'scantronformat'} = '';
 8057:                 $changes{'scantronformat'} = 1;
 8058:             }
 8059:         }
 8060:     }
 8061:     if (keys(%confhash) > 0) {
 8062:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 8063:                                                  $dom);
 8064:         if ($putresult eq 'ok') {
 8065:             if (keys(%changes) > 0) {
 8066:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 8067:                     $resulttext = &mt('Changes made:').'<ul>';
 8068:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 8069:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 8070:                     } else {
 8071:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 8072:                     }
 8073:                     $resulttext .= '</ul>';
 8074:                 } else {
 8075:                     $resulttext = &mt('Changes made to bubblesheet format file.');
 8076:                 }
 8077:                 $resulttext .= '</ul>';
 8078:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 8079:                 if (ref($lastactref) eq 'HASH') {
 8080:                     $lastactref->{'domainconfig'} = 1;
 8081:                 }
 8082:             } else {
 8083:                 $resulttext = &mt('No changes made to bubblesheet format file');
 8084:             }
 8085:         } else {
 8086:             $resulttext = '<span class="LC_error">'.
 8087:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 8088:         }
 8089:     } else {
 8090:         $resulttext = &mt('No changes made to bubblesheet format file'); 
 8091:     }
 8092:     if ($errors) {
 8093:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 8094:                        $errors.'</ul>';
 8095:     }
 8096:     return $resulttext;
 8097: }
 8098: 
 8099: sub modify_coursecategories {
 8100:     my ($dom,%domconfig) = @_;
 8101:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 8102:         $cathash);
 8103:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 8104:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 8105:         $cathash = $domconfig{'coursecategories'}{'cats'};
 8106:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 8107:             $changes{'togglecats'} = 1;
 8108:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 8109:         }
 8110:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 8111:             $changes{'categorize'} = 1;
 8112:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 8113:         }
 8114:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 8115:             $changes{'togglecatscomm'} = 1;
 8116:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 8117:         }
 8118:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 8119:             $changes{'categorizecomm'} = 1;
 8120:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 8121:         }
 8122:     } else {
 8123:         $changes{'togglecats'} = 1;
 8124:         $changes{'categorize'} = 1;
 8125:         $changes{'togglecatscomm'} = 1;
 8126:         $changes{'categorizecomm'} = 1;
 8127:         $domconfig{'coursecategories'} = {
 8128:                                              togglecats => $env{'form.togglecats'},
 8129:                                              categorize => $env{'form.categorize'},
 8130:                                              togglecatscomm => $env{'form.togglecatscomm'},
 8131:                                              categorizecomm => $env{'form.categorizecomm'},
 8132:                                          };
 8133:     }
 8134:     if (ref($cathash) eq 'HASH') {
 8135:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 8136:             push (@deletecategory,'instcode::0');
 8137:         }
 8138:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 8139:             push(@deletecategory,'communities::0');
 8140:         }
 8141:     }
 8142:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 8143:     if (ref($cathash) eq 'HASH') {
 8144:         if (@deletecategory > 0) {
 8145:             #FIXME Need to remove category from all courses using a deleted category 
 8146:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 8147:             foreach my $item (@deletecategory) {
 8148:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 8149:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 8150:                     $deletions{$item} = 1;
 8151:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 8152:                 }
 8153:             }
 8154:         }
 8155:         foreach my $item (keys(%{$cathash})) {
 8156:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 8157:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 8158:                 $reorderings{$item} = 1;
 8159:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 8160:             }
 8161:             if ($env{'form.addcategory_name_'.$item} ne '') {
 8162:                 my $newcat = $env{'form.addcategory_name_'.$item};
 8163:                 my $newdepth = $depth+1;
 8164:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 8165:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 8166:                 $adds{$newitem} = 1; 
 8167:             }
 8168:             if ($env{'form.subcat_'.$item} ne '') {
 8169:                 my $newcat = $env{'form.subcat_'.$item};
 8170:                 my $newdepth = $depth+1;
 8171:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 8172:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 8173:                 $adds{$newitem} = 1;
 8174:             }
 8175:         }
 8176:     }
 8177:     if ($env{'form.instcode'} eq '1') {
 8178:         if (ref($cathash) eq 'HASH') {
 8179:             my $newitem = 'instcode::0';
 8180:             if ($cathash->{$newitem} eq '') {  
 8181:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 8182:                 $adds{$newitem} = 1;
 8183:             }
 8184:         } else {
 8185:             my $newitem = 'instcode::0';
 8186:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 8187:             $adds{$newitem} = 1;
 8188:         }
 8189:     }
 8190:     if ($env{'form.communities'} eq '1') {
 8191:         if (ref($cathash) eq 'HASH') {
 8192:             my $newitem = 'communities::0';
 8193:             if ($cathash->{$newitem} eq '') {
 8194:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 8195:                 $adds{$newitem} = 1;
 8196:             }
 8197:         } else {
 8198:             my $newitem = 'communities::0';
 8199:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 8200:             $adds{$newitem} = 1;
 8201:         }
 8202:     }
 8203:     if ($env{'form.addcategory_name'} ne '') {
 8204:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 8205:             ($env{'form.addcategory_name'} ne 'communities')) {
 8206:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 8207:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 8208:             $adds{$newitem} = 1;
 8209:         }
 8210:     }
 8211:     my $putresult;
 8212:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 8213:         if (keys(%deletions) > 0) {
 8214:             foreach my $key (keys(%deletions)) {
 8215:                 if ($predelallitems{$key} ne '') {
 8216:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 8217:                 }
 8218:             }
 8219:         }
 8220:         my (@chkcats,@chktrails,%chkallitems);
 8221:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 8222:         if (ref($chkcats[0]) eq 'ARRAY') {
 8223:             my $depth = 0;
 8224:             my $chg = 0;
 8225:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 8226:                 my $name = $chkcats[0][$i];
 8227:                 my $item;
 8228:                 if ($name eq '') {
 8229:                     $chg ++;
 8230:                 } else {
 8231:                     $item = &escape($name).'::0';
 8232:                     if ($chg) {
 8233:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 8234:                     }
 8235:                     $depth ++; 
 8236:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 8237:                     $depth --;
 8238:                 }
 8239:             }
 8240:         }
 8241:     }
 8242:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 8243:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 8244:         if ($putresult eq 'ok') {
 8245:             my %title = (
 8246:                          togglecats     => 'Show/Hide a course in catalog',
 8247:                          categorize     => 'Assign a category to a course',
 8248:                          togglecatscomm => 'Show/Hide a community in catalog',
 8249:                          categorizecomm => 'Assign a category to a community',
 8250:                         );
 8251:             my %level = (
 8252:                          dom  => 'set in Domain ("Modify Course/Community")',
 8253:                          crs  => 'set in Course ("Course Configuration")',
 8254:                          comm => 'set in Community ("Community Configuration")',
 8255:                         );
 8256:             $resulttext = &mt('Changes made:').'<ul>';
 8257:             if ($changes{'togglecats'}) {
 8258:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 8259:             }
 8260:             if ($changes{'categorize'}) {
 8261:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 8262:             }
 8263:             if ($changes{'togglecatscomm'}) {
 8264:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 8265:             }
 8266:             if ($changes{'categorizecomm'}) {
 8267:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 8268:             }
 8269:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 8270:                 my $cathash;
 8271:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 8272:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 8273:                 } else {
 8274:                     $cathash = {};
 8275:                 } 
 8276:                 my (@cats,@trails,%allitems);
 8277:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 8278:                 if (keys(%deletions) > 0) {
 8279:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 8280:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 8281:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 8282:                     }
 8283:                     $resulttext .= '</ul></li>';
 8284:                 }
 8285:                 if (keys(%reorderings) > 0) {
 8286:                     my %sort_by_trail;
 8287:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 8288:                     foreach my $key (keys(%reorderings)) {
 8289:                         if ($allitems{$key} ne '') {
 8290:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 8291:                         }
 8292:                     }
 8293:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 8294:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 8295:                     }
 8296:                     $resulttext .= '</ul></li>';
 8297:                 }
 8298:                 if (keys(%adds) > 0) {
 8299:                     my %sort_by_trail;
 8300:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 8301:                     foreach my $key (keys(%adds)) {
 8302:                         if ($allitems{$key} ne '') {
 8303:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 8304:                         }
 8305:                     }
 8306:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 8307:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 8308:                     }
 8309:                     $resulttext .= '</ul></li>';
 8310:                 }
 8311:             }
 8312:             $resulttext .= '</ul>';
 8313:         } else {
 8314:             $resulttext = '<span class="LC_error">'.
 8315:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8316:         }
 8317:     } else {
 8318:         $resulttext = &mt('No changes made to course and community categories');
 8319:     }
 8320:     return $resulttext;
 8321: }
 8322: 
 8323: sub modify_serverstatuses {
 8324:     my ($dom,%domconfig) = @_;
 8325:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 8326:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 8327:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 8328:     }
 8329:     my @pages = &serverstatus_pages();
 8330:     foreach my $type (@pages) {
 8331:         $newserverstatus{$type}{'namedusers'} = '';
 8332:         $newserverstatus{$type}{'machines'} = '';
 8333:         if (defined($env{'form.'.$type.'_namedusers'})) {
 8334:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 8335:             my @okusers;
 8336:             foreach my $user (@users) {
 8337:                 my ($uname,$udom) = split(/:/,$user);
 8338:                 if (($udom =~ /^$match_domain$/) &&   
 8339:                     (&Apache::lonnet::domain($udom)) &&
 8340:                     ($uname =~ /^$match_username$/)) {
 8341:                     if (!grep(/^\Q$user\E/,@okusers)) {
 8342:                         push(@okusers,$user);
 8343:                     }
 8344:                 }
 8345:             }
 8346:             if (@okusers > 0) {
 8347:                  @okusers = sort(@okusers);
 8348:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 8349:             }
 8350:         }
 8351:         if (defined($env{'form.'.$type.'_machines'})) {
 8352:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 8353:             my @okmachines;
 8354:             foreach my $ip (@machines) {
 8355:                 my @parts = split(/\./,$ip);
 8356:                 next if (@parts < 4);
 8357:                 my $badip = 0;
 8358:                 for (my $i=0; $i<4; $i++) {
 8359:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 8360:                         $badip = 1;
 8361:                         last;
 8362:                     }
 8363:                 }
 8364:                 if (!$badip) {
 8365:                     push(@okmachines,$ip);     
 8366:                 }
 8367:             }
 8368:             @okmachines = sort(@okmachines);
 8369:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 8370:         }
 8371:     }
 8372:     my %serverstatushash =  (
 8373:                                 serverstatuses => \%newserverstatus,
 8374:                             );
 8375:     foreach my $type (@pages) {
 8376:         foreach my $setting ('namedusers','machines') {
 8377:             my (@current,@new);
 8378:             if (ref($currserverstatus{$type}) eq 'HASH') {
 8379:                 if ($currserverstatus{$type}{$setting} ne '') { 
 8380:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 8381:                 }
 8382:             }
 8383:             if ($newserverstatus{$type}{$setting} ne '') {
 8384:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 8385:             }
 8386:             if (@current > 0) {
 8387:                 if (@new > 0) {
 8388:                     foreach my $item (@current) {
 8389:                         if (!grep(/^\Q$item\E$/,@new)) {
 8390:                             $changes{$type}{$setting} = 1;
 8391:                             last;
 8392:                         }
 8393:                     }
 8394:                     foreach my $item (@new) {
 8395:                         if (!grep(/^\Q$item\E$/,@current)) {
 8396:                             $changes{$type}{$setting} = 1;
 8397:                             last;
 8398:                         }
 8399:                     }
 8400:                 } else {
 8401:                     $changes{$type}{$setting} = 1;
 8402:                 }
 8403:             } elsif (@new > 0) {
 8404:                 $changes{$type}{$setting} = 1;
 8405:             }
 8406:         }
 8407:     }
 8408:     if (keys(%changes) > 0) {
 8409:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 8410:         my $putresult = &Apache::lonnet::put_dom('configuration',
 8411:                                                  \%serverstatushash,$dom);
 8412:         if ($putresult eq 'ok') {
 8413:             $resulttext .= &mt('Changes made:').'<ul>';
 8414:             foreach my $type (@pages) {
 8415:                 if (ref($changes{$type}) eq 'HASH') {
 8416:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 8417:                     if ($changes{$type}{'namedusers'}) {
 8418:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 8419:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 8420:                         } else {
 8421:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 8422:                         }
 8423:                     }
 8424:                     if ($changes{$type}{'machines'}) {
 8425:                         if ($newserverstatus{$type}{'machines'} eq '') {
 8426:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 8427:                         } else {
 8428:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 8429:                         }
 8430: 
 8431:                     }
 8432:                     $resulttext .= '</ul></li>';
 8433:                 }
 8434:             }
 8435:             $resulttext .= '</ul>';
 8436:         } else {
 8437:             $resulttext = '<span class="LC_error">'.
 8438:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 8439: 
 8440:         }
 8441:     } else {
 8442:         $resulttext = &mt('No changes made to access to server status pages');
 8443:     }
 8444:     return $resulttext;
 8445: }
 8446: 
 8447: sub modify_helpsettings {
 8448:     my ($r,$dom,$confname,%domconfig) = @_;
 8449:     my ($resulttext,$errors,%changes,%helphash);
 8450:     my %defaultchecked = ('submitbugs' => 'on');
 8451:     my @offon = ('off','on');
 8452:     my @toggles = ('submitbugs');
 8453:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 8454:         foreach my $item (@toggles) {
 8455:             if ($defaultchecked{$item} eq 'on') { 
 8456:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 8457:                     if ($env{'form.'.$item} eq '0') {
 8458:                         $changes{$item} = 1;
 8459:                     }
 8460:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 8461:                     $changes{$item} = 1;
 8462:                 }
 8463:             } elsif ($defaultchecked{$item} eq 'off') {
 8464:                 if ($domconfig{'helpsettings'}{$item} eq '') {
 8465:                     if ($env{'form.'.$item} eq '1') {
 8466:                         $changes{$item} = 1;
 8467:                     }
 8468:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
 8469:                     $changes{$item} = 1;
 8470:                 }
 8471:             }
 8472:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
 8473:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
 8474:             }
 8475:         }
 8476:     }
 8477:     my $putresult;
 8478:     if (keys(%changes) > 0) {
 8479:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
 8480:         if ($putresult eq 'ok') {
 8481:             $resulttext = &mt('Changes made:').'<ul>';
 8482:             foreach my $item (sort(keys(%changes))) {
 8483:                 if ($item eq 'submitbugs') {
 8484:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
 8485:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 8486:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
 8487:                 }
 8488:             }
 8489:             $resulttext .= '</ul>';
 8490:         } else {
 8491:             $resulttext = &mt('No changes made to help settings');
 8492:             $errors .= '<li><span class="LC_error">'.
 8493:                        &mt('An error occurred storing the settings: [_1]',
 8494:                            $putresult).'</span></li>';
 8495:         }
 8496:     }
 8497:     if ($errors) {
 8498:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 8499:                        $errors.'</ul>';
 8500:     }
 8501:     return $resulttext;
 8502: }
 8503: 
 8504: sub modify_coursedefaults {
 8505:     my ($dom,$lastactref,%domconfig) = @_;
 8506:     my ($resulttext,$errors,%changes,%defaultshash);
 8507:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 8508:     my @toggles = ('canuse_pdfforms');
 8509:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
 8510:                    'uploadquota_community','uploadquota_textbook');
 8511:     my @types = ('official','unofficial','community','textbook');
 8512:     my %staticdefaults = (
 8513:                            anonsurvey_threshold => 10,
 8514:                            uploadquota          => 500,
 8515:                          );
 8516: 
 8517:     $defaultshash{'coursedefaults'} = {};
 8518: 
 8519:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 8520:         if ($domconfig{'coursedefaults'} eq '') {
 8521:             $domconfig{'coursedefaults'} = {};
 8522:         }
 8523:     }
 8524: 
 8525:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 8526:         foreach my $item (@toggles) {
 8527:             if ($defaultchecked{$item} eq 'on') {
 8528:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 8529:                     ($env{'form.'.$item} eq '0')) {
 8530:                     $changes{$item} = 1;
 8531:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 8532:                     $changes{$item} = 1;
 8533:                 }
 8534:             } elsif ($defaultchecked{$item} eq 'off') {
 8535:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 8536:                     ($env{'form.'.$item} eq '1')) {
 8537:                     $changes{$item} = 1;
 8538:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 8539:                     $changes{$item} = 1;
 8540:                 }
 8541:             }
 8542:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 8543:         }
 8544:         foreach my $item (@numbers) {
 8545:             my ($currdef,$newdef);
 8546:             $newdef = $env{'form.'.$item};
 8547:             if ($item eq 'anonsurvey_threshold') {
 8548:                 $currdef = $domconfig{'coursedefaults'}{$item};
 8549:                 $newdef =~ s/\D//g;
 8550:                 if ($newdef eq '' || $newdef < 1) {
 8551:                     $newdef = 1;
 8552:                 }
 8553:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
 8554:             } else {
 8555:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
 8556:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 8557:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 8558:                 }
 8559:                 $newdef =~ s/[^\w.\-]//g;
 8560:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
 8561:             }
 8562:             if ($currdef ne $newdef) {
 8563:                 my $staticdef;
 8564:                 if ($item eq 'anonsurvey_threshold') {
 8565:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
 8566:                         $changes{$item} = 1;
 8567:                     }
 8568:                 } else {
 8569:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
 8570:                         $changes{'uploadquota'} = 1;
 8571:                     }
 8572:                 }
 8573:             }
 8574:         }
 8575:         my $officialcreds = $env{'form.official_credits'};
 8576:         $officialcreds =~ s/[^\d.]+//g;
 8577:         my $unofficialcreds = $env{'form.unofficial_credits'};
 8578:         $unofficialcreds =~ s/[^\d.]+//g;
 8579:         my $textbookcreds = $env{'form.textbook_credits'};
 8580:         $textbookcreds =~ s/[^\d.]+//g;
 8581:         if (ref($domconfig{'coursedefaults'}{'coursecredits'} ne 'HASH') &&
 8582:                 ($env{'form.coursecredits'} eq '1')) {
 8583:                 $changes{'coursecredits'} = 1;
 8584:         } else {
 8585:             if (($domconfig{'coursedefaults'}{'coursecredits'}{'official'} ne $officialcreds)  ||
 8586:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'} ne $unofficialcreds) ||
 8587:                 ($domconfig{'coursedefaults'}{'coursecredits'}{'textbook'} ne $textbookcreds)) {
 8588:                 $changes{'coursecredits'} = 1;
 8589:             }
 8590:         }
 8591:         $defaultshash{'coursedefaults'}{'coursecredits'} = {
 8592:             official   => $officialcreds,
 8593:             unofficial => $unofficialcreds,
 8594:             textbook   => $textbookcreds,
 8595:         }
 8596:     }
 8597:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 8598:                                              $dom);
 8599:     if ($putresult eq 'ok') {
 8600:         if (keys(%changes) > 0) {
 8601:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8602:             if (($changes{'canuse_pdfforms'}) || ($changes{'coursecredits'}) || ($changes{'uploadquota'})) {
 8603:                 if ($changes{'canuse_pdfforms'}) {
 8604:                     $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 8605:                 }
 8606:                 if ($changes{'coursecredits'}) {
 8607:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 8608:                         $domdefaults{'officialcredits'} =
 8609:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'official'};
 8610:                         $domdefaults{'unofficialcredits'} =
 8611:                             $defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'};
 8612:                         $domdefaults{'textbookcredits'} =
 8613:                             $domdefaults{'coursedefaults'}{'coursecredits'}{'textbook'};
 8614:                     }
 8615:                 }
 8616:                 if ($changes{'uploadquota'}) {
 8617:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 8618:                         foreach my $type (@types) {
 8619:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
 8620:                         }
 8621:                     }
 8622:                 }
 8623:                 my $cachetime = 24*60*60;
 8624:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8625:                 if (ref($lastactref) eq 'HASH') {
 8626:                     $lastactref->{'domdefaults'} = 1;
 8627:                 }
 8628:             }
 8629:             $resulttext = &mt('Changes made:').'<ul>';
 8630:             foreach my $item (sort(keys(%changes))) {
 8631:                 if ($item eq 'canuse_pdfforms') {
 8632:                     if ($env{'form.'.$item} eq '1') {
 8633:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 8634:                     } else {
 8635:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 8636:                     }
 8637:                 } elsif ($item eq 'anonsurvey_threshold') {
 8638:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
 8639:                 } elsif ($item eq 'uploadquota') {
 8640:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 8641:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
 8642:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
 8643:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
 8644:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
 8645: 
 8646:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
 8647:                                        '</ul>'.
 8648:                                        '</li>';
 8649:                     } else {
 8650:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
 8651:                     }
 8652:                 } elsif ($item eq 'coursecredits') {
 8653:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 8654:                         if (($domdefaults{'officialcredits'} eq '') &&
 8655:                             ($domdefaults{'unofficialcredits'} eq '') &&
 8656:                             ($domdefaults{'textbookcredits'} eq '')) {
 8657:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 8658:                         } else {
 8659:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
 8660:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
 8661:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
 8662:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
 8663:                                            '</ul>'.
 8664:                                            '</li>';
 8665:                         }
 8666:                     } else {
 8667:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
 8668:                     }
 8669:                 }
 8670:             }
 8671:             $resulttext .= '</ul>';
 8672:         } else {
 8673:             $resulttext = &mt('No changes made to course defaults');
 8674:         }
 8675:     } else {
 8676:         $resulttext = '<span class="LC_error">'.
 8677:             &mt('An error occurred: [_1]',$putresult).'</span>';
 8678:     }
 8679:     return $resulttext;
 8680: }
 8681: 
 8682: sub modify_usersessions {
 8683:     my ($dom,$lastactref,%domconfig) = @_;
 8684:     my @hostingtypes = ('version','excludedomain','includedomain');
 8685:     my @offloadtypes = ('primary','default');
 8686:     my %types = (
 8687:                   remote => \@hostingtypes,
 8688:                   hosted => \@hostingtypes,
 8689:                   spares => \@offloadtypes,
 8690:                 );
 8691:     my @prefixes = ('remote','hosted','spares');
 8692:     my @lcversions = &Apache::lonnet::all_loncaparevs();
 8693:     my (%by_ip,%by_location,@intdoms);
 8694:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
 8695:     my @locations = sort(keys(%by_location));
 8696:     my (%defaultshash,%changes);
 8697:     foreach my $prefix (@prefixes) {
 8698:         $defaultshash{'usersessions'}{$prefix} = {};
 8699:     }
 8700:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8701:     my $resulttext;
 8702:     my %iphost = &Apache::lonnet::get_iphost();
 8703:     foreach my $prefix (@prefixes) {
 8704:         next if ($prefix eq 'spares');
 8705:         foreach my $type (@{$types{$prefix}}) {
 8706:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
 8707:             if ($type eq 'version') {
 8708:                 my $value = $env{'form.'.$prefix.'_'.$type};
 8709:                 my $okvalue;
 8710:                 if ($value ne '') {
 8711:                     if (grep(/^\Q$value\E$/,@lcversions)) {
 8712:                         $okvalue = $value;
 8713:                     }
 8714:                 }
 8715:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8716:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 8717:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
 8718:                             if ($inuse == 0) {
 8719:                                 $changes{$prefix}{$type} = 1;
 8720:                             } else {
 8721:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
 8722:                                     $changes{$prefix}{$type} = 1;
 8723:                                 }
 8724:                                 if ($okvalue ne '') {
 8725:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8726:                                 } 
 8727:                             }
 8728:                         } else {
 8729:                             if (($inuse == 1) && ($okvalue ne '')) {
 8730:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8731:                                 $changes{$prefix}{$type} = 1;
 8732:                             }
 8733:                         }
 8734:                     } else {
 8735:                         if (($inuse == 1) && ($okvalue ne '')) {
 8736:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8737:                             $changes{$prefix}{$type} = 1;
 8738:                         }
 8739:                     }
 8740:                 } else {
 8741:                     if (($inuse == 1) && ($okvalue ne '')) {
 8742:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
 8743:                         $changes{$prefix}{$type} = 1;
 8744:                     }
 8745:                 }
 8746:             } else {
 8747:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
 8748:                 my @okvals;
 8749:                 foreach my $val (@vals) {
 8750:                     if ($val =~ /:/) {
 8751:                         my @items = split(/:/,$val);
 8752:                         foreach my $item (@items) {
 8753:                             if (ref($by_location{$item}) eq 'ARRAY') {
 8754:                                 push(@okvals,$item);
 8755:                             }
 8756:                         }
 8757:                     } else {
 8758:                         if (ref($by_location{$val}) eq 'ARRAY') {
 8759:                             push(@okvals,$val);
 8760:                         }
 8761:                     }
 8762:                 }
 8763:                 @okvals = sort(@okvals);
 8764:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8765:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
 8766:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 8767:                             if ($inuse == 0) {
 8768:                                 $changes{$prefix}{$type} = 1; 
 8769:                             } else {
 8770:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8771:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
 8772:                                 if (@changed > 0) {
 8773:                                     $changes{$prefix}{$type} = 1;
 8774:                                 }
 8775:                             }
 8776:                         } else {
 8777:                             if ($inuse == 1) {
 8778:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8779:                                 $changes{$prefix}{$type} = 1;
 8780:                             }
 8781:                         } 
 8782:                     } else {
 8783:                         if ($inuse == 1) {
 8784:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8785:                             $changes{$prefix}{$type} = 1;
 8786:                         }
 8787:                     }
 8788:                 } else {
 8789:                     if ($inuse == 1) {
 8790:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
 8791:                         $changes{$prefix}{$type} = 1;
 8792:                     }
 8793:                 }
 8794:             }
 8795:         }
 8796:     }
 8797: 
 8798:     my @alldoms = &Apache::lonnet::all_domains();
 8799:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8800:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
 8801:     my $savespares;
 8802: 
 8803:     foreach my $lonhost (sort(keys(%servers))) {
 8804:         my $serverhomeID =
 8805:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
 8806:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
 8807:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
 8808:         my %spareschg;
 8809:         foreach my $type (@{$types{'spares'}}) {
 8810:             my @okspares;
 8811:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
 8812:             foreach my $server (@checked) {
 8813:                 if (&Apache::lonnet::hostname($server) ne '') {
 8814:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
 8815:                         unless (grep(/^\Q$server\E$/,@okspares)) {
 8816:                             push(@okspares,$server);
 8817:                         }
 8818:                     }
 8819:                 }
 8820:             }
 8821:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
 8822:             my $newspare;
 8823:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
 8824:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
 8825:                     $newspare = $new;
 8826:                 }
 8827:             }
 8828:             my @spares;
 8829:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
 8830:                 @spares = sort(@okspares,$newspare);
 8831:             } else {
 8832:                 @spares = sort(@okspares);
 8833:             }
 8834:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
 8835:             if (ref($spareid{$lonhost}) eq 'HASH') {
 8836:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
 8837:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
 8838:                     if (@diffs > 0) {
 8839:                         $spareschg{$type} = 1;
 8840:                     }
 8841:                 }
 8842:             }
 8843:         }
 8844:         if (keys(%spareschg) > 0) {
 8845:             $changes{'spares'}{$lonhost} = \%spareschg;
 8846:         }
 8847:     }
 8848: 
 8849:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 8850:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 8851:             if (ref($changes{'spares'}) eq 'HASH') {
 8852:                 if (keys(%{$changes{'spares'}}) > 0) {
 8853:                     $savespares = 1;
 8854:                 }
 8855:             }
 8856:         } else {
 8857:             $savespares = 1;
 8858:         }
 8859:     }
 8860: 
 8861:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
 8862:     if ((keys(%changes) > 0) || ($savespares)) {
 8863:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 8864:                                                  $dom);
 8865:         if ($putresult eq 'ok') {
 8866:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8867:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
 8868:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
 8869:                 }
 8870:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
 8871:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
 8872:                 }
 8873:             }
 8874:             my $cachetime = 24*60*60;
 8875:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 8876:             if (ref($lastactref) eq 'HASH') {
 8877:                 $lastactref->{'domdefaults'} = 1;
 8878:             }
 8879:             if (keys(%changes) > 0) {
 8880:                 my %lt = &usersession_titles();
 8881:                 $resulttext = &mt('Changes made:').'<ul>';
 8882:                 foreach my $prefix (@prefixes) {
 8883:                     if (ref($changes{$prefix}) eq 'HASH') {
 8884:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
 8885:                         if ($prefix eq 'spares') {
 8886:                             if (ref($changes{$prefix}) eq 'HASH') {
 8887:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
 8888:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
 8889:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
 8890:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
 8891:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
 8892:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
 8893:                                         foreach my $type (@{$types{$prefix}}) {
 8894:                                             if ($changes{$prefix}{$lonhost}{$type}) {
 8895:                                                 my $offloadto = &mt('None');
 8896:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
 8897:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
 8898:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
 8899:                                                     }
 8900:                                                 }
 8901:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
 8902:                                             }
 8903:                                         }
 8904:                                     }
 8905:                                     $resulttext .= '</li>';
 8906:                                 }
 8907:                             }
 8908:                         } else {
 8909:                             foreach my $type (@{$types{$prefix}}) {
 8910:                                 if (defined($changes{$prefix}{$type})) {
 8911:                                     my $newvalue;
 8912:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
 8913:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
 8914:                                             if ($type eq 'version') {
 8915:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
 8916:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
 8917:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
 8918:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
 8919:                                                 }
 8920:                                             }
 8921:                                         }
 8922:                                     }
 8923:                                     if ($newvalue eq '') {
 8924:                                         if ($type eq 'version') {
 8925:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
 8926:                                         } else {
 8927:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
 8928:                                         }
 8929:                                     } else {
 8930:                                         if ($type eq 'version') {
 8931:                                             $newvalue .= ' '.&mt('(or later)'); 
 8932:                                         }
 8933:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
 8934:                                     }
 8935:                                 }
 8936:                             }
 8937:                         }
 8938:                         $resulttext .= '</ul>';
 8939:                     }
 8940:                 }
 8941:                 $resulttext .= '</ul>';
 8942:             } else {
 8943:                 $resulttext = $nochgmsg;
 8944:             }
 8945:         } else {
 8946:             $resulttext = '<span class="LC_error">'.
 8947:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 8948:         }
 8949:     } else {
 8950:         $resulttext = $nochgmsg;
 8951:     }
 8952:     return $resulttext;
 8953: }
 8954: 
 8955: sub modify_loadbalancing {
 8956:     my ($dom,%domconfig) = @_;
 8957:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8958:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8959:     my ($othertitle,$usertypes,$types) =
 8960:         &Apache::loncommon::sorted_inst_types($dom);
 8961:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8962:     my @sparestypes = ('primary','default');
 8963:     my %typetitles = &sparestype_titles();
 8964:     my $resulttext;
 8965:     my (%currbalancer,%currtargets,%currrules,%existing);
 8966:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8967:         %existing = %{$domconfig{'loadbalancing'}};
 8968:     }
 8969:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 8970:                               \%currtargets,\%currrules);
 8971:     my ($saveloadbalancing,%defaultshash,%changes);
 8972:     my ($alltypes,$othertypes,$titles) =
 8973:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 8974:     my %ruletitles = &offloadtype_text();
 8975:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
 8976:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
 8977:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
 8978:         if ($balancer eq '') {
 8979:             next;
 8980:         }
 8981:         if (!exists($servers{$balancer})) {
 8982:             if (exists($currbalancer{$balancer})) {
 8983:                 push(@{$changes{'delete'}},$balancer);
 8984:             }
 8985:             next;
 8986:         }
 8987:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
 8988:             push(@{$changes{'delete'}},$balancer);
 8989:             next;
 8990:         }
 8991:         if (!exists($currbalancer{$balancer})) {
 8992:             push(@{$changes{'add'}},$balancer);
 8993:         }
 8994:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
 8995:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
 8996:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
 8997:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 8998:             $saveloadbalancing = 1;
 8999:         }
 9000:         foreach my $sparetype (@sparestypes) {
 9001:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
 9002:             my @offloadto;
 9003:             foreach my $target (@targets) {
 9004:                 if (($servers{$target}) && ($target ne $balancer)) {
 9005:                     if ($sparetype eq 'default') {
 9006:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
 9007:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
 9008:                         }
 9009:                     }
 9010:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
 9011:                         push(@offloadto,$target);
 9012:                     }
 9013:                 }
 9014:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
 9015:             }
 9016:         }
 9017:         if (ref($currtargets{$balancer}) eq 'HASH') {
 9018:             foreach my $sparetype (@sparestypes) {
 9019:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
 9020:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
 9021:                     if (@targetdiffs > 0) {
 9022:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 9023:                     }
 9024:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 9025:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 9026:                         $changes{'curr'}{$balancer}{'targets'} = 1;
 9027:                     }
 9028:                 }
 9029:             }
 9030:         } else {
 9031:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
 9032:                 foreach my $sparetype (@sparestypes) {
 9033:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 9034:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 9035:                             $changes{'curr'}{$balancer}{'targets'} = 1;
 9036:                         }
 9037:                     }
 9038:                 }
 9039:             }
 9040:         }
 9041:         my $ishomedom;
 9042:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
 9043:             $ishomedom = 1;
 9044:         }
 9045:         if (ref($alltypes) eq 'ARRAY') {
 9046:             foreach my $type (@{$alltypes}) {
 9047:                 my $rule;
 9048:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
 9049:                          (!$ishomedom)) {
 9050:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
 9051:                 }
 9052:                 if ($rule eq 'specific') {
 9053:                     $rule = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
 9054:                 }
 9055:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
 9056:                 if (ref($currrules{$balancer}) eq 'HASH') {
 9057:                     if ($rule ne $currrules{$balancer}{$type}) {
 9058:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 9059:                     }
 9060:                 } elsif ($rule ne '') {
 9061:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
 9062:                 }
 9063:             }
 9064:         }
 9065:     }
 9066:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
 9067:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
 9068:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
 9069:             $defaultshash{'loadbalancing'} = {};
 9070:         }
 9071:         my $putresult = &Apache::lonnet::put_dom('configuration',
 9072:                                                  \%defaultshash,$dom);
 9073:         if ($putresult eq 'ok') {
 9074:             if (keys(%changes) > 0) {
 9075:                 if (ref($changes{'delete'}) eq 'ARRAY') {
 9076:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
 9077:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
 9078:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
 9079:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
 9080:                     }
 9081:                 }
 9082:                 if (ref($changes{'add'}) eq 'ARRAY') {
 9083:                     foreach my $balancer (sort(@{$changes{'add'}})) {
 9084:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
 9085:                     }
 9086:                 }
 9087:                 if (ref($changes{'curr'}) eq 'HASH') {
 9088:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
 9089:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
 9090:                             if ($changes{'curr'}{$balancer}{'targets'}) {
 9091:                                 my %offloadstr;
 9092:                                 foreach my $sparetype (@sparestypes) {
 9093:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
 9094:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
 9095:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
 9096:                                         }
 9097:                                     }
 9098:                                 }
 9099:                                 if (keys(%offloadstr) == 0) {
 9100:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
 9101:                                 } else {
 9102:                                     my $showoffload;
 9103:                                     foreach my $sparetype (@sparestypes) {
 9104:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
 9105:                                         if (defined($offloadstr{$sparetype})) {
 9106:                                             $showoffload .= $offloadstr{$sparetype};
 9107:                                         } else {
 9108:                                             $showoffload .= &mt('None');
 9109:                                         }
 9110:                                         $showoffload .= ('&nbsp;'x3);
 9111:                                     }
 9112:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
 9113:                                 }
 9114:                             }
 9115:                         }
 9116:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
 9117:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
 9118:                                 foreach my $type (@{$alltypes}) {
 9119:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
 9120:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
 9121:                                         my $balancetext;
 9122:                                         if ($rule eq '') {
 9123:                                             $balancetext =  $ruletitles{'default'};
 9124:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
 9125:                                                  ($rule eq 'balancer') || ($rule eq 'offloadedto')) {
 9126:                                             $balancetext =  $ruletitles{$rule};
 9127:                                         } else {
 9128:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
 9129:                                         }
 9130:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
 9131:                                     }
 9132:                                 }
 9133:                             }
 9134:                         }
 9135:                         my $cachekey = &escape('loadbalancing').':'.&escape($dom);
 9136:                         &Apache::lonnet::remote_devalidate_cache($balancer,[$cachekey]);
 9137:                     }
 9138:                 }
 9139:                 if ($resulttext ne '') {
 9140:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
 9141:                 } else {
 9142:                     $resulttext = $nochgmsg;
 9143:                 }
 9144:             } else {
 9145:                 $resulttext = $nochgmsg;
 9146:             }
 9147:         } else {
 9148:             $resulttext = '<span class="LC_error">'.
 9149:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 9150:         }
 9151:     } else {
 9152:         $resulttext = $nochgmsg;
 9153:     }
 9154:     return $resulttext;
 9155: }
 9156: 
 9157: sub recurse_check {
 9158:     my ($chkcats,$categories,$depth,$name) = @_;
 9159:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 9160:         my $chg = 0;
 9161:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 9162:             my $category = $chkcats->[$depth]{$name}[$j];
 9163:             my $item;
 9164:             if ($category eq '') {
 9165:                 $chg ++;
 9166:             } else {
 9167:                 my $deeper = $depth + 1;
 9168:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 9169:                 if ($chg) {
 9170:                     $categories->{$item} -= $chg;
 9171:                 }
 9172:                 &recurse_check($chkcats,$categories,$deeper,$category);
 9173:                 $deeper --;
 9174:             }
 9175:         }
 9176:     }
 9177:     return;
 9178: }
 9179: 
 9180: sub recurse_cat_deletes {
 9181:     my ($item,$coursecategories,$deletions) = @_;
 9182:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 9183:     my $subdepth = $depth + 1;
 9184:     if (ref($coursecategories) eq 'HASH') {
 9185:         foreach my $subitem (keys(%{$coursecategories})) {
 9186:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 9187:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 9188:                 delete($coursecategories->{$subitem});
 9189:                 $deletions->{$subitem} = 1;
 9190:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 9191:             }
 9192:         }
 9193:     }
 9194:     return;
 9195: }
 9196: 
 9197: sub get_active_dcs {
 9198:     my ($dom) = @_;
 9199:     my $now = time;
 9200:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
 9201:     my %domcoords;
 9202:     my $numdcs = 0;
 9203:     foreach my $server (keys(%dompersonnel)) {
 9204:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 9205:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 9206:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 9207:         }
 9208:     }
 9209:     return %domcoords;
 9210: }
 9211: 
 9212: sub active_dc_picker {
 9213:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
 9214:     my %domcoords = &get_active_dcs($dom); 
 9215:     my @domcoord = keys(%domcoords);
 9216:     if (keys(%currhash)) {
 9217:         foreach my $dc (keys(%currhash)) {
 9218:             unless (exists($domcoords{$dc})) {
 9219:                 push(@domcoord,$dc);
 9220:             }
 9221:         }
 9222:     }
 9223:     @domcoord = sort(@domcoord);
 9224:     my $numdcs = scalar(@domcoord);
 9225:     my $rows = 0;
 9226:     my $table;
 9227:     if ($numdcs > 1) {
 9228:         $table = '<table>';
 9229:         for (my $i=0; $i<@domcoord; $i++) {
 9230:             my $rem = $i%($numinrow);
 9231:             if ($rem == 0) {
 9232:                 if ($i > 0) {
 9233:                     $table .= '</tr>';
 9234:                 }
 9235:                 $table .= '<tr>';
 9236:                 $rows ++;
 9237:             }
 9238:             my $check = '';
 9239:             if ($inputtype eq 'radio') {
 9240:                 if (keys(%currhash) == 0) {
 9241:                     if (!$i) {
 9242:                         $check = ' checked="checked"';
 9243:                     }
 9244:                 } elsif (exists($currhash{$domcoord[$i]})) {
 9245:                     $check = ' checked="checked"';
 9246:                 }
 9247:             } else {
 9248:                 if (exists($currhash{$domcoord[$i]})) {
 9249:                     $check = ' checked="checked"';
 9250:                 }
 9251:             }
 9252:             if ($i == @domcoord - 1) {
 9253:                 my $colsleft = $numinrow - $rem;
 9254:                 if ($colsleft > 1) {
 9255:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
 9256:                 } else {
 9257:                     $table .= '<td class="LC_left_item">';
 9258:                 }
 9259:             } else {
 9260:                 $table .= '<td class="LC_left_item">';
 9261:             }
 9262:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
 9263:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
 9264:             $table .= '<span class="LC_nobreak"><label>'.
 9265:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
 9266:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
 9267:             if ($user ne $dcname.':'.$dcdom) {
 9268:                 $table .=  ' ('.$dcname.':'.$dcdom.')'.
 9269:                            '</label></span></td>';
 9270:             }
 9271:         }
 9272:         $table .= '</tr></table>';
 9273:     } elsif ($numdcs == 1) {
 9274:         if ($inputtype eq 'radio') {
 9275:             $table .= '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />';
 9276:         } else {
 9277:             my $check;
 9278:             if (exists($currhash{$domcoord[0]})) {
 9279:                 $check = ' checked="checked"';
 9280:             }
 9281:             $table .= '<input type="checkbox" name="'.$name.'" '.
 9282:                       'value="'.$domcoord[0].'"'.$check.' />';
 9283:             $rows ++;
 9284:         }
 9285:     }
 9286:     return ($numdcs,$table,$rows);
 9287: }
 9288: 
 9289: sub usersession_titles {
 9290:     return &Apache::lonlocal::texthash(
 9291:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
 9292:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
 9293:                spares => 'Servers offloaded to, when busy',
 9294:                version => 'LON-CAPA version requirement',
 9295:                excludedomain => 'Allow all, but exclude specific domains',
 9296:                includedomain => 'Deny all, but include specific domains',
 9297:                primary => 'Primary (checked first)',
 9298:                default => 'Default',
 9299:            );
 9300: }
 9301: 
 9302: sub id_for_thisdom {
 9303:     my (%servers) = @_;
 9304:     my %altids;
 9305:     foreach my $server (keys(%servers)) {
 9306:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 9307:         if ($serverhome ne $server) {
 9308:             $altids{$serverhome} = $server;
 9309:         }
 9310:     }
 9311:     return %altids;
 9312: }
 9313: 
 9314: sub count_servers {
 9315:     my ($currbalancer,%servers) = @_;
 9316:     my (@spares,$numspares);
 9317:     foreach my $lonhost (sort(keys(%servers))) {
 9318:         next if ($currbalancer eq $lonhost);
 9319:         push(@spares,$lonhost);
 9320:     }
 9321:     if ($currbalancer) {
 9322:         $numspares = scalar(@spares);
 9323:     } else {
 9324:         $numspares = scalar(@spares) - 1;
 9325:     }
 9326:     return ($numspares,@spares);
 9327: }
 9328: 
 9329: sub lonbalance_targets_js {
 9330:     my ($dom,$types,$servers,$settings) = @_;
 9331:     my $select = &mt('Select');
 9332:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
 9333:     if (ref($servers) eq 'HASH') {
 9334:         $alltargets = join("','",sort(keys(%{$servers})));
 9335:         my @homedoms;
 9336:         foreach my $server (sort(keys(%{$servers}))) {
 9337:             if (&Apache::lonnet::host_domain($server) eq $dom) {
 9338:                 push(@homedoms,'1');
 9339:             } else {
 9340:                 push(@homedoms,'0');
 9341:             }
 9342:         }
 9343:         $allishome = join("','",@homedoms);
 9344:     }
 9345:     if (ref($types) eq 'ARRAY') {
 9346:         if (@{$types} > 0) {
 9347:             @alltypes = @{$types};
 9348:         }
 9349:     }
 9350:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
 9351:     $allinsttypes = join("','",@alltypes);
 9352:     my (%currbalancer,%currtargets,%currrules,%existing);
 9353:     if (ref($settings) eq 'HASH') {
 9354:         %existing = %{$settings};
 9355:     }
 9356:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
 9357:                               \%currtargets,\%currrules);
 9358:     my $balancers = join("','",sort(keys(%currbalancer)));
 9359:     return <<"END";
 9360: 
 9361: <script type="text/javascript">
 9362: // <![CDATA[
 9363: 
 9364: currBalancers = new Array('$balancers');
 9365: 
 9366: function toggleTargets(balnum) {
 9367:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 9368:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
 9369:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
 9370:     var prevbalancer = prevhostitem.value;
 9371:     var baltotal = document.getElementById('loadbalancing_total').value;
 9372:     prevhostitem.value = balancer;
 9373:     if (prevbalancer != '') {
 9374:         var prevIdx = currBalancers.indexOf(prevbalancer);
 9375:         if (prevIdx != -1) {
 9376:             currBalancers.splice(prevIdx,1);
 9377:         }
 9378:     }
 9379:     if (balancer == '') {
 9380:         hideSpares(balnum);
 9381:     } else {
 9382:         var currIdx = currBalancers.indexOf(balancer);
 9383:         if (currIdx == -1) {
 9384:             currBalancers.push(balancer);
 9385:         }
 9386:         var homedoms = new Array('$allishome');
 9387:         var ishomedom = homedoms[lonhostitem.selectedIndex];
 9388:         showSpares(balancer,ishomedom,balnum);
 9389:     }
 9390:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
 9391:     return;
 9392: }
 9393: 
 9394: function showSpares(balancer,ishomedom,balnum) {
 9395:     var alltargets = new Array('$alltargets');
 9396:     var insttypes = new Array('$allinsttypes');
 9397:     var offloadtypes = new Array('primary','default');
 9398: 
 9399:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
 9400:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
 9401:  
 9402:     for (var i=0; i<offloadtypes.length; i++) {
 9403:         var count = 0;
 9404:         for (var j=0; j<alltargets.length; j++) {
 9405:             if (alltargets[j] != balancer) {
 9406:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
 9407:                 item.value = alltargets[j];
 9408:                 item.style.textAlign='left';
 9409:                 item.style.textFace='normal';
 9410:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
 9411:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
 9412:                     item.disabled = '';
 9413:                 } else {
 9414:                     item.disabled = 'disabled';
 9415:                     item.checked = false;
 9416:                 }
 9417:                 count ++;
 9418:             }
 9419:         }
 9420:     }
 9421:     for (var k=0; k<insttypes.length; k++) {
 9422:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
 9423:             if (ishomedom == 1) {
 9424:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 9425:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 9426:             } else {
 9427:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 9428:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 9429:             }
 9430:         } else {
 9431:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
 9432:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
 9433:         }
 9434:         if ((insttypes[k] != '_LC_external') && 
 9435:             ((insttypes[k] != '_LC_internetdom') ||
 9436:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
 9437:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
 9438:             item.options.length = 0;
 9439:             item.options[0] = new Option("","",true,true);
 9440:             var idx = 0;
 9441:             for (var m=0; m<alltargets.length; m++) {
 9442:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
 9443:                     idx ++;
 9444:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 9445:                 }
 9446:             }
 9447:         }
 9448:     }
 9449:     return;
 9450: }
 9451: 
 9452: function hideSpares(balnum) {
 9453:     var alltargets = new Array('$alltargets');
 9454:     var insttypes = new Array('$allinsttypes');
 9455:     var offloadtypes = new Array('primary','default');
 9456: 
 9457:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
 9458:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
 9459: 
 9460:     var total = alltargets.length - 1;
 9461:     for (var i=0; i<offloadtypes; i++) {
 9462:         for (var j=0; j<total; j++) {
 9463:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
 9464:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
 9465:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
 9466:         }
 9467:     }
 9468:     for (var k=0; k<insttypes.length; k++) {
 9469:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
 9470:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
 9471:         if (insttypes[k] != '_LC_external') {
 9472:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
 9473:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
 9474:         }
 9475:     }
 9476:     return;
 9477: }
 9478: 
 9479: function checkOffloads(item,balnum,type) {
 9480:     var alltargets = new Array('$alltargets');
 9481:     var offloadtypes = new Array('primary','default');
 9482:     if (item.checked) {
 9483:         var total = alltargets.length - 1;
 9484:         var other;
 9485:         if (type == offloadtypes[0]) {
 9486:             other = offloadtypes[1];
 9487:         } else {
 9488:             other = offloadtypes[0];
 9489:         }
 9490:         for (var i=0; i<total; i++) {
 9491:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
 9492:             if (server == item.value) {
 9493:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
 9494:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
 9495:                 }
 9496:             }
 9497:         }
 9498:     }
 9499:     return;
 9500: }
 9501: 
 9502: function singleServerToggle(balnum,type) {
 9503:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
 9504:     if (offloadtoSelIdx == 0) {
 9505:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
 9506:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 9507: 
 9508:     } else {
 9509:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
 9510:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 9511:     }
 9512:     return;
 9513: }
 9514: 
 9515: function balanceruleChange(formname,balnum,type) {
 9516:     if (type == '_LC_external') {
 9517:         return;
 9518:     }
 9519:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
 9520:     for (var i=0; i<typesRules.length; i++) {
 9521:         if (formname.elements[typesRules[i]].checked) {
 9522:             if (formname.elements[typesRules[i]].value != 'specific') {
 9523:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
 9524:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
 9525:             } else {
 9526:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
 9527:             }
 9528:         }
 9529:     }
 9530:     return;
 9531: }
 9532: 
 9533: function balancerDeleteChange(balnum) {
 9534:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
 9535:     var baltotal = document.getElementById('loadbalancing_total').value;
 9536:     var addtarget;
 9537:     var removetarget;
 9538:     var action = 'delete';
 9539:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
 9540:         var lonhost = hostitem.value;
 9541:         var currIdx = currBalancers.indexOf(lonhost);
 9542:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
 9543:             if (currIdx != -1) {
 9544:                 currBalancers.splice(currIdx,1);
 9545:             }
 9546:             addtarget = lonhost;
 9547:         } else {
 9548:             if (currIdx == -1) {
 9549:                 currBalancers.push(lonhost);
 9550:             }
 9551:             removetarget = lonhost;
 9552:             action = 'undelete';
 9553:         }
 9554:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
 9555:     }
 9556:     return;
 9557: }
 9558: 
 9559: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
 9560:     if (baltotal > 1) {
 9561:         var offloadtypes = new Array('primary','default');
 9562:         var alltargets = new Array('$alltargets');
 9563:         var insttypes = new Array('$allinsttypes');
 9564:         for (var i=0; i<baltotal; i++) {
 9565:             if (i != balnum) {
 9566:                 for (var j=0; j<offloadtypes.length; j++) {
 9567:                     var total = alltargets.length - 1;
 9568:                     for (var k=0; k<total; k++) {
 9569:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
 9570:                         var server = serveritem.value;
 9571:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
 9572:                             if (server == addtarget) {
 9573:                                 serveritem.disabled = '';
 9574:                             }
 9575:                         }
 9576:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 9577:                             if (server == removetarget) {
 9578:                                 serveritem.disabled = 'disabled';
 9579:                                 serveritem.checked = false;
 9580:                             }
 9581:                         }
 9582:                     }
 9583:                 }
 9584:                 for (var j=0; j<insttypes.length; j++) {
 9585:                     if (insttypes[j] != '_LC_external') {
 9586:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
 9587:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
 9588:                             var currSel = singleserver.selectedIndex;
 9589:                             var currVal = singleserver.options[currSel].value;
 9590:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
 9591:                                 var numoptions = singleserver.options.length;
 9592:                                 var needsnew = 1;
 9593:                                 for (var k=0; k<numoptions; k++) {
 9594:                                     if (singleserver.options[k] == addtarget) {
 9595:                                         needsnew = 0;
 9596:                                         break;
 9597:                                     }
 9598:                                 }
 9599:                                 if (needsnew == 1) {
 9600:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
 9601:                                 }
 9602:                             }
 9603:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
 9604:                                 singleserver.options.length = 0;
 9605:                                 if ((currVal) && (currVal != removetarget)) {
 9606:                                     singleserver.options[0] = new Option("","",false,false);
 9607:                                 } else {
 9608:                                     singleserver.options[0] = new Option("","",true,true);
 9609:                                 }
 9610:                                 var idx = 0;
 9611:                                 for (var m=0; m<alltargets.length; m++) {
 9612:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
 9613:                                         idx ++;
 9614:                                         if (currVal == alltargets[m]) {
 9615:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
 9616:                                         } else {
 9617:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
 9618:                                         }
 9619:                                     }
 9620:                                 }
 9621:                             }
 9622:                         }
 9623:                     }
 9624:                 }
 9625:             }
 9626:         }
 9627:     }
 9628:     return;
 9629: }
 9630: 
 9631: // ]]>
 9632: </script>
 9633: 
 9634: END
 9635: }
 9636: 
 9637: sub new_spares_js {
 9638:     my @sparestypes = ('primary','default');
 9639:     my $types = join("','",@sparestypes);
 9640:     my $select = &mt('Select');
 9641:     return <<"END";
 9642: 
 9643: <script type="text/javascript">
 9644: // <![CDATA[
 9645: 
 9646: function updateNewSpares(formname,lonhost) {
 9647:     var types = new Array('$types');
 9648:     var include = new Array();
 9649:     var exclude = new Array();
 9650:     for (var i=0; i<types.length; i++) {
 9651:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
 9652:         for (var j=0; j<spareboxes.length; j++) {
 9653:             if (formname.elements[spareboxes[j]].checked) {
 9654:                 exclude.push(formname.elements[spareboxes[j]].value);
 9655:             } else {
 9656:                 include.push(formname.elements[spareboxes[j]].value);
 9657:             }
 9658:         }
 9659:     }
 9660:     for (var i=0; i<types.length; i++) {
 9661:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
 9662:         var selIdx = newSpare.selectedIndex;
 9663:         var currnew = newSpare.options[selIdx].value;
 9664:         var okSpares = new Array();
 9665:         for (var j=0; j<newSpare.options.length; j++) {
 9666:             var possible = newSpare.options[j].value;
 9667:             if (possible != '') {
 9668:                 if (exclude.indexOf(possible) == -1) {
 9669:                     okSpares.push(possible);
 9670:                 } else {
 9671:                     if (currnew == possible) {
 9672:                         selIdx = 0;
 9673:                     }
 9674:                 }
 9675:             }
 9676:         }
 9677:         for (var k=0; k<include.length; k++) {
 9678:             if (okSpares.indexOf(include[k]) == -1) {
 9679:                 okSpares.push(include[k]);
 9680:             }
 9681:         }
 9682:         okSpares.sort();
 9683:         newSpare.options.length = 0;
 9684:         if (selIdx == 0) {
 9685:             newSpare.options[0] = new Option("$select","",true,true);
 9686:         } else {
 9687:             newSpare.options[0] = new Option("$select","",false,false);
 9688:         }
 9689:         for (var m=0; m<okSpares.length; m++) {
 9690:             var idx = m+1;
 9691:             var selThis = 0;
 9692:             if (selIdx != 0) {
 9693:                 if (okSpares[m] == currnew) {
 9694:                     selThis = 1;
 9695:                 }
 9696:             }
 9697:             if (selThis == 1) {
 9698:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
 9699:             } else {
 9700:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
 9701:             }
 9702:         }
 9703:     }
 9704:     return;
 9705: }
 9706: 
 9707: function checkNewSpares(lonhost,type) {
 9708:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
 9709:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
 9710:     if (chosen != '') { 
 9711:         var othertype;
 9712:         var othernewSpare;
 9713:         if (type == 'primary') {
 9714:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
 9715:         }
 9716:         if (type == 'default') {
 9717:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
 9718:         }
 9719:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
 9720:             othernewSpare.selectedIndex = 0;
 9721:         }
 9722:     }
 9723:     return;
 9724: }
 9725: 
 9726: // ]]>
 9727: </script>
 9728: 
 9729: END
 9730: 
 9731: }
 9732: 
 9733: sub common_domprefs_js {
 9734:     return <<"END";
 9735: 
 9736: <script type="text/javascript">
 9737: // <![CDATA[
 9738: 
 9739: function getIndicesByName(formname,item) {
 9740:     var group = new Array();
 9741:     for (var i=0;i<formname.elements.length;i++) {
 9742:         if (formname.elements[i].name == item) {
 9743:             group.push(formname.elements[i].id);
 9744:         }
 9745:     }
 9746:     return group;
 9747: }
 9748: 
 9749: // ]]>
 9750: </script>
 9751: 
 9752: END
 9753: 
 9754: }
 9755: 
 9756: sub recaptcha_js {
 9757:     my %lt = &captcha_phrases();
 9758:     return <<"END";
 9759: 
 9760: <script type="text/javascript">
 9761: // <![CDATA[
 9762: 
 9763: function updateCaptcha(caller,context) {
 9764:     var privitem;
 9765:     var pubitem;
 9766:     var privtext;
 9767:     var pubtext;
 9768:     if (document.getElementById(context+'_recaptchapub')) {
 9769:         pubitem = document.getElementById(context+'_recaptchapub');
 9770:     } else {
 9771:         return;
 9772:     }
 9773:     if (document.getElementById(context+'_recaptchapriv')) {
 9774:         privitem = document.getElementById(context+'_recaptchapriv');
 9775:     } else {
 9776:         return;
 9777:     }
 9778:     if (document.getElementById(context+'_recaptchapubtxt')) {
 9779:         pubtext = document.getElementById(context+'_recaptchapubtxt');
 9780:     } else {
 9781:         return;
 9782:     }
 9783:     if (document.getElementById(context+'_recaptchaprivtxt')) {
 9784:         privtext = document.getElementById(context+'_recaptchaprivtxt');
 9785:     } else {
 9786:         return;
 9787:     }
 9788:     if (caller.checked) {
 9789:         if (caller.value == 'recaptcha') {
 9790:             pubitem.type = 'text';
 9791:             privitem.type = 'text';
 9792:             pubitem.size = '40';
 9793:             privitem.size = '40';
 9794:             pubtext.innerHTML = "$lt{'pub'}";
 9795:             privtext.innerHTML = "$lt{'priv'}";
 9796:         } else {
 9797:             pubitem.type = 'hidden';
 9798:             privitem.type = 'hidden';
 9799:             pubtext.innerHTML = '';
 9800:             privtext.innerHTML = '';
 9801:         }
 9802:     }
 9803:     return;
 9804: }
 9805: 
 9806: // ]]>
 9807: </script>
 9808: 
 9809: END
 9810: 
 9811: }
 9812: 
 9813: sub credits_js {
 9814:     return <<"END";
 9815: 
 9816: <script type="text/javascript">
 9817: // <![CDATA[
 9818: 
 9819: function toggleCredits(domForm) {
 9820:     if (document.getElementById('credits')) {
 9821:         creditsitem = document.getElementById('credits');
 9822:         var creditsLength = domForm.coursecredits.length;
 9823:         if (creditsLength) {
 9824:             var currval;
 9825:             for (var i=0; i<creditsLength; i++) {
 9826:                 if (domForm.coursecredits[i].checked) {
 9827:                    currval = domForm.coursecredits[i].value;
 9828:                 }
 9829:             }
 9830:             if (currval == 1) {
 9831:                 creditsitem.style.display = 'block';
 9832:             } else {
 9833:                 creditsitem.style.display = 'none';
 9834:             }
 9835:         }
 9836:     }
 9837:     return;
 9838: }
 9839: 
 9840: // ]]>
 9841: </script>
 9842: 
 9843: END
 9844: 
 9845: }
 9846: 
 9847: sub captcha_phrases {
 9848:     return &Apache::lonlocal::texthash (
 9849:                  priv => 'Private key',
 9850:                  pub  => 'Public key',
 9851:                  original  => 'original (CAPTCHA)',
 9852:                  recaptcha => 'successor (ReCAPTCHA)',
 9853:                  notused   => 'unused',
 9854:     );
 9855: }
 9856: 
 9857: sub devalidate_remote_domconfs {
 9858:     my ($dom,$cachekeys) = @_;
 9859:     return unless (ref($cachekeys) eq 'HASH');
 9860:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9861:     my %thismachine;
 9862:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
 9863:     my @posscached = ('domainconfig','domdefaults');
 9864:     if (keys(%servers) > 1) {
 9865:         foreach my $server (keys(%servers)) {
 9866:             next if ($thismachine{$server});
 9867:             my @cached;
 9868:             foreach my $name (@posscached) {
 9869:                 if ($cachekeys->{$name}) {
 9870:                     push(@cached,&escape($name).':'.&escape($dom));
 9871:                 }
 9872:             }
 9873:             if (@cached) {
 9874:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
 9875:             }
 9876:         }
 9877:     }
 9878:     return;
 9879: }
 9880: 
 9881: 1;

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